diff options
Diffstat (limited to 'policyhandler/deploy_handler.py')
-rw-r--r-- | policyhandler/deploy_handler.py | 170 |
1 files changed, 119 insertions, 51 deletions
diff --git a/policyhandler/deploy_handler.py b/policyhandler/deploy_handler.py index 1d50fc3..ea703f4 100644 --- a/policyhandler/deploy_handler.py +++ b/policyhandler/deploy_handler.py @@ -1,8 +1,5 @@ -""" send notification to deploy-handler""" - -# org.onap.dcae # ================================================================================ -# Copyright (c) 2017 AT&T Intellectual Property. All rights reserved. +# Copyright (c) 2017-2018 AT&T Intellectual Property. All rights reserved. # ================================================================================ # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -19,13 +16,17 @@ # # ECOMP is a trademark and service mark of AT&T Intellectual Property. -import logging +""" send notification to deploy-handler""" + import json +import logging + import requests from .config import Config +from .customize import CustomizerUser from .discovery import DiscoveryClient -from .onap.audit import REQUEST_X_ECOMP_REQUESTID, Audit, AuditHttpCode +from .onap.audit import REQUEST_X_ECOMP_REQUESTID, AuditHttpCode, Metrics POOL_SIZE = 1 @@ -37,69 +38,136 @@ class DeployHandler(object): _requests_session = None _config = None _url = None - _url_path = None + _url_policy = None _target_entity = None + _custom_kwargs = None + _server_instance_uuid = None @staticmethod - def _lazy_init(): + def _lazy_init(audit, rediscover=False): """ set static properties """ - if DeployHandler._lazy_inited: + if DeployHandler._lazy_inited and not rediscover: return - DeployHandler._lazy_inited = True - - DeployHandler._requests_session = requests.Session() - DeployHandler._requests_session.mount( - 'https://', - requests.adapters.HTTPAdapter(pool_connections=POOL_SIZE, pool_maxsize=POOL_SIZE) - ) - DeployHandler._requests_session.mount( - 'http://', - requests.adapters.HTTPAdapter(pool_connections=POOL_SIZE, pool_maxsize=POOL_SIZE) - ) - - DeployHandler._target_entity = Config.config["deploy_handler"] - DeployHandler._url = DiscoveryClient.get_service_url(DeployHandler._target_entity) - DeployHandler._url_path = DeployHandler._url + '/policy' - DeployHandler._logger.info("DeployHandler url(%s)", DeployHandler._url) + + DeployHandler._custom_kwargs = (CustomizerUser.get_customizer() + .get_deploy_handler_kwargs(audit)) + if (not DeployHandler._custom_kwargs + or not isinstance(DeployHandler._custom_kwargs, dict)): + DeployHandler._custom_kwargs = {} + + if not DeployHandler._requests_session: + DeployHandler._requests_session = requests.Session() + DeployHandler._requests_session.mount( + 'https://', + requests.adapters.HTTPAdapter(pool_connections=POOL_SIZE, pool_maxsize=POOL_SIZE) + ) + DeployHandler._requests_session.mount( + 'http://', + requests.adapters.HTTPAdapter(pool_connections=POOL_SIZE, pool_maxsize=POOL_SIZE) + ) + + config_dh = Config.settings.get("deploy_handler") + if config_dh and isinstance(config_dh, dict): + # dns based routing to deployment-handler + # config for policy-handler >= 2.4.0 + # "deploy_handler" : { + # "target_entity" : "deployment_handler", + # "url" : "http://deployment_handler:8188" + # } + DeployHandler._target_entity = config_dh.get("target_entity", "deployment_handler") + DeployHandler._url = config_dh.get("url") + DeployHandler._logger.info("dns based routing to %s: url(%s)", + DeployHandler._target_entity, DeployHandler._url) + + if not DeployHandler._url: + # discover routing to deployment-handler at consul-services + if not isinstance(config_dh, dict): + # config for policy-handler <= 2.3.1 + # "deploy_handler" : "deployment_handler" + DeployHandler._target_entity = str(config_dh or "deployment_handler") + DeployHandler._url = DiscoveryClient.get_service_url(audit, + DeployHandler._target_entity) + + DeployHandler._url_policy = str(DeployHandler._url or "") + '/policy' + DeployHandler._logger.info( + "got %s policy url(%s)", DeployHandler._target_entity, DeployHandler._url_policy) + + DeployHandler._lazy_inited = bool(DeployHandler._url) + @staticmethod - def policy_update(audit, latest_policies): - """ post policy_updated message to deploy-handler """ - DeployHandler._lazy_init() - msg = {"latest_policies":latest_policies} - sub_aud = Audit(aud_parent=audit, targetEntity=DeployHandler._target_entity, - targetServiceName=DeployHandler._url_path) - headers = {REQUEST_X_ECOMP_REQUESTID : sub_aud.request_id} - - msg_str = json.dumps(msg) + def policy_update(audit, message, rediscover=False): + """ + post policy_updated message to deploy-handler + + returns condition whether it needs to catch_up + """ + if not message: + return + + DeployHandler._lazy_init(audit, rediscover) + metrics = Metrics(aud_parent=audit, targetEntity=DeployHandler._target_entity, + targetServiceName=DeployHandler._url_policy) + headers = {REQUEST_X_ECOMP_REQUESTID : metrics.request_id} + + msg_str = json.dumps(message) headers_str = json.dumps(headers) - log_line = "post to deployment-handler {0} msg={1} headers={2}".format( - DeployHandler._url_path, msg_str, headers_str) - sub_aud.metrics_start(log_line) + log_action = "post to {0} at {1}".format( + DeployHandler._target_entity, DeployHandler._url_policy) + log_data = " msg={0} headers={1}".format(msg_str, headers_str) + log_line = log_action + log_data DeployHandler._logger.info(log_line) + metrics.metrics_start(log_line) + + if not DeployHandler._url: + error_msg = "no url found to {0}".format(log_line) + DeployHandler._logger.error(error_msg) + metrics.set_http_status_code(AuditHttpCode.SERVICE_UNAVAILABLE_ERROR.value) + audit.set_http_status_code(AuditHttpCode.SERVICE_UNAVAILABLE_ERROR.value) + metrics.metrics(error_msg) + return res = None try: res = DeployHandler._requests_session.post( - DeployHandler._url_path, json=msg, headers=headers + DeployHandler._url_policy, json=message, headers=headers, + **DeployHandler._custom_kwargs ) - except requests.exceptions.RequestException as ex: - error_msg = "failed to post to deployment-handler {0} {1} msg={2} headers={3}" \ - .format(DeployHandler._url_path, str(ex), msg_str, headers_str) + except Exception as ex: + error_code = (AuditHttpCode.SERVICE_UNAVAILABLE_ERROR.value + if isinstance(ex, requests.exceptions.RequestException) + else AuditHttpCode.SERVER_INTERNAL_ERROR.value) + error_msg = ("failed to {0} {1}: {2}{3}" + .format(log_action, type(ex).__name__, str(ex), log_data)) DeployHandler._logger.exception(error_msg) - sub_aud.set_http_status_code(AuditHttpCode.SERVICE_UNAVAILABLE_ERROR.value) - audit.set_http_status_code(AuditHttpCode.SERVICE_UNAVAILABLE_ERROR.value) - sub_aud.metrics(error_msg) + metrics.set_http_status_code(error_code) + audit.set_http_status_code(error_code) + metrics.metrics(error_msg) return - sub_aud.set_http_status_code(res.status_code) + metrics.set_http_status_code(res.status_code) audit.set_http_status_code(res.status_code) - sub_aud.metrics( - "response from deployment-handler to post {0}: {1} msg={2} text={3} headers={4}" \ - .format(DeployHandler._url_path, res.status_code, msg_str, res.text, - res.request.headers)) + log_line = "response {0} from {1}: text={2}{3}" \ + .format(res.status_code, log_action, res.text, log_data) + metrics.metrics(log_line) - if res.status_code == requests.codes.ok: - return res.json() + if res.status_code != requests.codes.ok: + DeployHandler._logger.error(log_line) + return + + DeployHandler._logger.info(log_line) + result = res.json() or {} + prev_server_instance_uuid = DeployHandler._server_instance_uuid + DeployHandler._server_instance_uuid = result.get("server_instance_uuid") + + deployment_handler_changed = (prev_server_instance_uuid + and prev_server_instance_uuid != DeployHandler._server_instance_uuid) + if deployment_handler_changed: + log_line = ("deployment_handler_changed: {1} != {0}" + .format(prev_server_instance_uuid, DeployHandler._server_instance_uuid)) + metrics.info(log_line) + DeployHandler._logger.info(log_line) + + return deployment_handler_changed |