alex_sh | 9d980ce | 2017-08-23 17:30:56 -0400 | [diff] [blame] | 1 | """ send notification to deploy-handler""" |
| 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 logging |
| 23 | import json |
| 24 | import requests |
| 25 | |
| 26 | from .config import Config |
| 27 | from .discovery import DiscoveryClient |
| 28 | from .onap.audit import REQUEST_X_ECOMP_REQUESTID, Audit, AuditHttpCode |
| 29 | |
Alex Shatov | 541b5bc | 2017-09-13 16:22:17 -0400 | [diff] [blame] | 30 | POOL_SIZE = 1 |
| 31 | |
alex_sh | 9d980ce | 2017-08-23 17:30:56 -0400 | [diff] [blame] | 32 | class DeployHandler(object): |
| 33 | """ deploy-handler """ |
| 34 | _logger = logging.getLogger("policy_handler.deploy_handler") |
alex_sh | 9d980ce | 2017-08-23 17:30:56 -0400 | [diff] [blame] | 35 | _lazy_inited = False |
Alex Shatov | 541b5bc | 2017-09-13 16:22:17 -0400 | [diff] [blame] | 36 | |
| 37 | _requests_session = None |
alex_sh | 9d980ce | 2017-08-23 17:30:56 -0400 | [diff] [blame] | 38 | _config = None |
| 39 | _url = None |
Alex Shatov | 42a989a | 2017-09-12 13:00:25 -0400 | [diff] [blame] | 40 | _url_path = None |
alex_sh | 9d980ce | 2017-08-23 17:30:56 -0400 | [diff] [blame] | 41 | _target_entity = None |
| 42 | |
| 43 | @staticmethod |
| 44 | def _lazy_init(): |
| 45 | """ set static properties """ |
| 46 | if DeployHandler._lazy_inited: |
| 47 | return |
| 48 | DeployHandler._lazy_inited = True |
Alex Shatov | 541b5bc | 2017-09-13 16:22:17 -0400 | [diff] [blame] | 49 | |
| 50 | DeployHandler._requests_session = requests.Session() |
| 51 | DeployHandler._requests_session.mount( |
| 52 | 'https://', |
| 53 | requests.adapters.HTTPAdapter(pool_connections=POOL_SIZE, pool_maxsize=POOL_SIZE) |
| 54 | ) |
| 55 | DeployHandler._requests_session.mount( |
| 56 | 'http://', |
| 57 | requests.adapters.HTTPAdapter(pool_connections=POOL_SIZE, pool_maxsize=POOL_SIZE) |
| 58 | ) |
| 59 | |
alex_sh | 9d980ce | 2017-08-23 17:30:56 -0400 | [diff] [blame] | 60 | DeployHandler._target_entity = Config.config["deploy_handler"] |
| 61 | DeployHandler._url = DiscoveryClient.get_service_url(DeployHandler._target_entity) |
Alex Shatov | 1369bea | 2018-01-10 11:00:50 -0500 | [diff] [blame^] | 62 | DeployHandler._url_path = (DeployHandler._url or "") + '/policy' |
alex_sh | 9d980ce | 2017-08-23 17:30:56 -0400 | [diff] [blame] | 63 | DeployHandler._logger.info("DeployHandler url(%s)", DeployHandler._url) |
| 64 | |
| 65 | @staticmethod |
Alex Shatov | 1369bea | 2018-01-10 11:00:50 -0500 | [diff] [blame^] | 66 | def policy_update(audit, latest_policies, removed_policies=None, |
| 67 | errored_policies=None, catch_up=False): |
| 68 | """post policy_updated message to deploy-handler""" |
| 69 | if not latest_policies and not removed_policies and not catch_up: |
| 70 | return |
| 71 | |
| 72 | latest_policies = latest_policies or {} |
| 73 | removed_policies = removed_policies or {} |
| 74 | errored_policies = errored_policies or {} |
| 75 | |
alex_sh | 9d980ce | 2017-08-23 17:30:56 -0400 | [diff] [blame] | 76 | DeployHandler._lazy_init() |
Alex Shatov | 1369bea | 2018-01-10 11:00:50 -0500 | [diff] [blame^] | 77 | msg = { |
| 78 | "catch_up" : catch_up, |
| 79 | "latest_policies" : latest_policies, |
| 80 | "removed_policies" : removed_policies, |
| 81 | "errored_policies" : errored_policies |
| 82 | } |
Alex Shatov | 42a989a | 2017-09-12 13:00:25 -0400 | [diff] [blame] | 83 | sub_aud = Audit(aud_parent=audit, targetEntity=DeployHandler._target_entity, |
| 84 | targetServiceName=DeployHandler._url_path) |
alex_sh | 9d980ce | 2017-08-23 17:30:56 -0400 | [diff] [blame] | 85 | headers = {REQUEST_X_ECOMP_REQUESTID : sub_aud.request_id} |
| 86 | |
| 87 | msg_str = json.dumps(msg) |
| 88 | headers_str = json.dumps(headers) |
| 89 | |
Alex Shatov | 1369bea | 2018-01-10 11:00:50 -0500 | [diff] [blame^] | 90 | DeployHandler._logger.info( |
| 91 | "catch_up(%s) latest_policies[%s], removed_policies[%s], errored_policies[%s]", |
| 92 | catch_up, len(latest_policies), len(removed_policies), len(errored_policies)) |
Alex Shatov | 42a989a | 2017-09-12 13:00:25 -0400 | [diff] [blame] | 93 | log_line = "post to deployment-handler {0} msg={1} headers={2}".format( |
| 94 | DeployHandler._url_path, msg_str, headers_str) |
Alex Shatov | 1369bea | 2018-01-10 11:00:50 -0500 | [diff] [blame^] | 95 | |
alex_sh | 9d980ce | 2017-08-23 17:30:56 -0400 | [diff] [blame] | 96 | DeployHandler._logger.info(log_line) |
Alex Shatov | 1369bea | 2018-01-10 11:00:50 -0500 | [diff] [blame^] | 97 | sub_aud.metrics_start(log_line) |
| 98 | |
| 99 | if not DeployHandler._url: |
| 100 | error_msg = "no url found to {0}".format(log_line) |
| 101 | DeployHandler._logger.error(error_msg) |
| 102 | sub_aud.set_http_status_code(AuditHttpCode.SERVICE_UNAVAILABLE_ERROR.value) |
| 103 | audit.set_http_status_code(AuditHttpCode.SERVICE_UNAVAILABLE_ERROR.value) |
| 104 | sub_aud.metrics(error_msg) |
| 105 | return |
alex_sh | 9d980ce | 2017-08-23 17:30:56 -0400 | [diff] [blame] | 106 | |
| 107 | res = None |
| 108 | try: |
Alex Shatov | 541b5bc | 2017-09-13 16:22:17 -0400 | [diff] [blame] | 109 | res = DeployHandler._requests_session.post( |
| 110 | DeployHandler._url_path, json=msg, headers=headers |
| 111 | ) |
alex_sh | 9d980ce | 2017-08-23 17:30:56 -0400 | [diff] [blame] | 112 | except requests.exceptions.RequestException as ex: |
| 113 | error_msg = "failed to post to deployment-handler {0} {1} msg={2} headers={3}" \ |
Alex Shatov | 42a989a | 2017-09-12 13:00:25 -0400 | [diff] [blame] | 114 | .format(DeployHandler._url_path, str(ex), msg_str, headers_str) |
alex_sh | 9d980ce | 2017-08-23 17:30:56 -0400 | [diff] [blame] | 115 | DeployHandler._logger.exception(error_msg) |
| 116 | sub_aud.set_http_status_code(AuditHttpCode.SERVICE_UNAVAILABLE_ERROR.value) |
| 117 | audit.set_http_status_code(AuditHttpCode.SERVICE_UNAVAILABLE_ERROR.value) |
| 118 | sub_aud.metrics(error_msg) |
| 119 | return |
| 120 | |
| 121 | sub_aud.set_http_status_code(res.status_code) |
| 122 | audit.set_http_status_code(res.status_code) |
| 123 | |
Alex Shatov | 42a989a | 2017-09-12 13:00:25 -0400 | [diff] [blame] | 124 | sub_aud.metrics( |
alex_sh | 9d980ce | 2017-08-23 17:30:56 -0400 | [diff] [blame] | 125 | "response from deployment-handler to post {0}: {1} msg={2} text={3} headers={4}" \ |
Alex Shatov | 42a989a | 2017-09-12 13:00:25 -0400 | [diff] [blame] | 126 | .format(DeployHandler._url_path, res.status_code, msg_str, res.text, |
| 127 | res.request.headers)) |
alex_sh | 9d980ce | 2017-08-23 17:30:56 -0400 | [diff] [blame] | 128 | |
| 129 | if res.status_code == requests.codes.ok: |
| 130 | return res.json() |