alex_sh | 9d980ce | 2017-08-23 17:30:56 -0400 | [diff] [blame] | 1 | """read and use the config""" |
| 2 | |
| 3 | # org.onap.dcae |
| 4 | # ================================================================================ |
| 5 | # Copyright (c) 2017 AT&T Intellectual Property. All rights reserved. |
| 6 | # ================================================================================ |
| 7 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | # you may not use this file except in compliance with the License. |
| 9 | # You may obtain a copy of the License at |
| 10 | # |
| 11 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | # |
| 13 | # Unless required by applicable law or agreed to in writing, software |
| 14 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | # See the License for the specific language governing permissions and |
| 17 | # limitations under the License. |
| 18 | # ============LICENSE_END========================================================= |
| 19 | # |
| 20 | # ECOMP is a trademark and service mark of AT&T Intellectual Property. |
| 21 | |
| 22 | import os |
| 23 | import json |
| 24 | import copy |
| 25 | import base64 |
| 26 | import logging |
| 27 | |
| 28 | from .discovery import DiscoveryClient |
alex_sh | 9d980ce | 2017-08-23 17:30:56 -0400 | [diff] [blame] | 29 | |
| 30 | logging.basicConfig( |
| 31 | filename='logs/policy_handler.log', \ |
| 32 | format='%(asctime)s.%(msecs)03d %(levelname)+8s ' + \ |
| 33 | '%(threadName)s %(name)s.%(funcName)s: %(message)s', \ |
| 34 | datefmt='%Y%m%d_%H%M%S', level=logging.DEBUG) |
| 35 | |
| 36 | class Config(object): |
| 37 | """main config of the application""" |
| 38 | CONFIG_FILE_PATH = "etc/config.json" |
| 39 | LOGGER_CONFIG_FILE_PATH = "etc/common_logger.config" |
alex_sh | 9d980ce | 2017-08-23 17:30:56 -0400 | [diff] [blame] | 40 | SERVICE_NAME_POLICY_HANDLER = "policy_handler" |
| 41 | FIELD_SYSTEM = "system" |
alex_sh | 9d980ce | 2017-08-23 17:30:56 -0400 | [diff] [blame] | 42 | FIELD_WSERVICE_PORT = "wservice_port" |
| 43 | FIELD_POLICY_ENGINE = "policy_engine" |
alex_sh | 9d980ce | 2017-08-23 17:30:56 -0400 | [diff] [blame] | 44 | wservice_port = 25577 |
| 45 | _logger = logging.getLogger("policy_handler.config") |
| 46 | config = None |
| 47 | |
| 48 | @staticmethod |
| 49 | def merge(new_config): |
| 50 | """merge the new_config into current config - override the values""" |
| 51 | if not new_config: |
| 52 | return |
| 53 | |
| 54 | if not Config.config: |
| 55 | Config.config = new_config |
| 56 | return |
| 57 | |
| 58 | new_config = copy.deepcopy(new_config) |
| 59 | Config.config.update(new_config) |
| 60 | |
| 61 | @staticmethod |
alex_sh | 9d980ce | 2017-08-23 17:30:56 -0400 | [diff] [blame] | 62 | def get_system_name(): |
| 63 | """find the name of the policy-handler system |
| 64 | to be used as the key in consul-kv for config of policy-handler |
| 65 | """ |
| 66 | system_name = None |
| 67 | if Config.config: |
| 68 | system_name = Config.config.get(Config.FIELD_SYSTEM) |
| 69 | |
| 70 | return system_name or Config.SERVICE_NAME_POLICY_HANDLER |
| 71 | |
| 72 | @staticmethod |
| 73 | def discover(): |
| 74 | """bring and merge the config settings from the discovery service""" |
| 75 | discovery_key = Config.get_system_name() |
| 76 | new_config = DiscoveryClient.get_value(discovery_key) |
| 77 | |
| 78 | if not new_config or not isinstance(new_config, dict): |
| 79 | Config._logger.warn("unexpected config from discovery: %s", new_config) |
| 80 | return |
| 81 | |
| 82 | Config._logger.debug("loaded config from discovery(%s): %s", \ |
| 83 | discovery_key, json.dumps(new_config)) |
alex_sh | 9d980ce | 2017-08-23 17:30:56 -0400 | [diff] [blame] | 84 | Config._logger.debug("config before merge from discovery: %s", json.dumps(Config.config)) |
Alex Shatov | 42a989a | 2017-09-12 13:00:25 -0400 | [diff] [blame^] | 85 | Config.merge(new_config.get(Config.SERVICE_NAME_POLICY_HANDLER)) |
alex_sh | 9d980ce | 2017-08-23 17:30:56 -0400 | [diff] [blame] | 86 | Config._logger.debug("merged config from discovery: %s", json.dumps(Config.config)) |
| 87 | |
| 88 | @staticmethod |
| 89 | def upload_to_discovery(): |
| 90 | """upload the current config settings to the discovery service""" |
| 91 | if not Config.config or not isinstance(Config.config, dict): |
| 92 | Config._logger.error("unexpected config: %s", Config.config) |
| 93 | return |
| 94 | |
alex_sh | 9d980ce | 2017-08-23 17:30:56 -0400 | [diff] [blame] | 95 | discovery_key = Config.get_system_name() |
Alex Shatov | 42a989a | 2017-09-12 13:00:25 -0400 | [diff] [blame^] | 96 | latest_config = json.dumps({Config.SERVICE_NAME_POLICY_HANDLER:Config.config}) |
alex_sh | 9d980ce | 2017-08-23 17:30:56 -0400 | [diff] [blame] | 97 | DiscoveryClient.put_kv(discovery_key, latest_config) |
| 98 | Config._logger.debug("uploaded config to discovery(%s): %s", \ |
| 99 | discovery_key, latest_config) |
| 100 | |
| 101 | @staticmethod |
| 102 | def load_from_file(file_path=None): |
| 103 | """read and store the config from config file""" |
| 104 | if not file_path: |
| 105 | file_path = Config.CONFIG_FILE_PATH |
| 106 | |
| 107 | loaded_config = None |
| 108 | if os.access(file_path, os.R_OK): |
| 109 | with open(file_path, 'r') as config_json: |
| 110 | loaded_config = json.load(config_json) |
| 111 | |
| 112 | if not loaded_config: |
| 113 | Config._logger.info("config not loaded from file: %s", file_path) |
| 114 | return |
| 115 | |
| 116 | Config._logger.info("config loaded from file: %s", file_path) |
| 117 | logging_config = loaded_config.get("logging") |
| 118 | if logging_config: |
| 119 | logging.config.dictConfig(logging_config) |
| 120 | |
alex_sh | 9d980ce | 2017-08-23 17:30:56 -0400 | [diff] [blame] | 121 | Config.wservice_port = loaded_config.get(Config.FIELD_WSERVICE_PORT, Config.wservice_port) |
| 122 | Config.merge(loaded_config.get(Config.SERVICE_NAME_POLICY_HANDLER)) |
| 123 | return True |
| 124 | |
| 125 | class PolicyEngineConfig(object): |
| 126 | """main config of the application""" |
| 127 | # PATH_TO_PROPERTIES = r'logs/policy_engine.properties' |
| 128 | PATH_TO_PROPERTIES = r'tmp/policy_engine.properties' |
| 129 | PYPDP_URL = "PYPDP_URL = {0}{1}, {2}, {3}\n" |
| 130 | CLIENT_ID = "CLIENT_ID = {0}\n" |
| 131 | CLIENT_KEY = "CLIENT_KEY = {0}\n" |
| 132 | ENVIRONMENT = "ENVIRONMENT = {0}\n" |
| 133 | _logger = logging.getLogger("policy_handler.pe_config") |
| 134 | |
| 135 | @staticmethod |
| 136 | def save_to_file(): |
| 137 | """create the policy_engine.properties for policy-engine client""" |
| 138 | file_path = PolicyEngineConfig.PATH_TO_PROPERTIES |
| 139 | |
| 140 | try: |
| 141 | config = Config.config[Config.FIELD_POLICY_ENGINE] |
| 142 | headers = config["headers"] |
| 143 | client_parts = base64.b64decode(headers["ClientAuth"].split()[1]).split(":") |
| 144 | auth_parts = base64.b64decode(headers["Authorization"].split()[1]).split(":") |
| 145 | |
| 146 | props = PolicyEngineConfig.PYPDP_URL.format(config["url"], config["path_pdp"], |
| 147 | auth_parts[0], auth_parts[1]) |
| 148 | props += PolicyEngineConfig.CLIENT_ID.format(client_parts[0]) |
| 149 | props += PolicyEngineConfig.CLIENT_KEY.format(base64.b64encode(client_parts[1])) |
| 150 | props += PolicyEngineConfig.ENVIRONMENT.format(headers["Environment"]) |
| 151 | |
| 152 | with open(file_path, 'w') as prp_file: |
| 153 | prp_file.write(props) |
| 154 | PolicyEngineConfig._logger.info("created %s", file_path) |
| 155 | except IOError: |
| 156 | PolicyEngineConfig._logger.error("failed to save to %s", file_path) |
| 157 | except KeyError: |
| 158 | PolicyEngineConfig._logger.error("unexpected config for %s", Config.FIELD_POLICY_ENGINE) |