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 | |
| 30 | class DeployHandler(object): |
| 31 | """ deploy-handler """ |
| 32 | _logger = logging.getLogger("policy_handler.deploy_handler") |
alex_sh | 9d980ce | 2017-08-23 17:30:56 -0400 | [diff] [blame] | 33 | _lazy_inited = False |
| 34 | _config = None |
| 35 | _url = None |
Alex Shatov | 42a989a | 2017-09-12 13:00:25 -0400 | [diff] [blame^] | 36 | _url_path = None |
alex_sh | 9d980ce | 2017-08-23 17:30:56 -0400 | [diff] [blame] | 37 | _target_entity = None |
| 38 | |
| 39 | @staticmethod |
| 40 | def _lazy_init(): |
| 41 | """ set static properties """ |
| 42 | if DeployHandler._lazy_inited: |
| 43 | return |
| 44 | DeployHandler._lazy_inited = True |
| 45 | DeployHandler._target_entity = Config.config["deploy_handler"] |
| 46 | DeployHandler._url = DiscoveryClient.get_service_url(DeployHandler._target_entity) |
Alex Shatov | 42a989a | 2017-09-12 13:00:25 -0400 | [diff] [blame^] | 47 | DeployHandler._url_path = DeployHandler._url + '/policy' |
alex_sh | 9d980ce | 2017-08-23 17:30:56 -0400 | [diff] [blame] | 48 | DeployHandler._logger.info("DeployHandler url(%s)", DeployHandler._url) |
| 49 | |
| 50 | @staticmethod |
| 51 | def policy_update(audit, latest_policies): |
| 52 | """ post policy_updated message to deploy-handler """ |
| 53 | DeployHandler._lazy_init() |
| 54 | msg = {"latest_policies":latest_policies} |
Alex Shatov | 42a989a | 2017-09-12 13:00:25 -0400 | [diff] [blame^] | 55 | sub_aud = Audit(aud_parent=audit, targetEntity=DeployHandler._target_entity, |
| 56 | targetServiceName=DeployHandler._url_path) |
alex_sh | 9d980ce | 2017-08-23 17:30:56 -0400 | [diff] [blame] | 57 | headers = {REQUEST_X_ECOMP_REQUESTID : sub_aud.request_id} |
| 58 | |
| 59 | msg_str = json.dumps(msg) |
| 60 | headers_str = json.dumps(headers) |
| 61 | |
Alex Shatov | 42a989a | 2017-09-12 13:00:25 -0400 | [diff] [blame^] | 62 | log_line = "post to deployment-handler {0} msg={1} headers={2}".format( |
| 63 | DeployHandler._url_path, msg_str, headers_str) |
alex_sh | 9d980ce | 2017-08-23 17:30:56 -0400 | [diff] [blame] | 64 | sub_aud.metrics_start(log_line) |
| 65 | DeployHandler._logger.info(log_line) |
| 66 | |
| 67 | res = None |
| 68 | try: |
Alex Shatov | 42a989a | 2017-09-12 13:00:25 -0400 | [diff] [blame^] | 69 | res = requests.post(DeployHandler._url_path, json=msg, headers=headers) |
alex_sh | 9d980ce | 2017-08-23 17:30:56 -0400 | [diff] [blame] | 70 | except requests.exceptions.RequestException as ex: |
| 71 | 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^] | 72 | .format(DeployHandler._url_path, str(ex), msg_str, headers_str) |
alex_sh | 9d980ce | 2017-08-23 17:30:56 -0400 | [diff] [blame] | 73 | DeployHandler._logger.exception(error_msg) |
| 74 | sub_aud.set_http_status_code(AuditHttpCode.SERVICE_UNAVAILABLE_ERROR.value) |
| 75 | audit.set_http_status_code(AuditHttpCode.SERVICE_UNAVAILABLE_ERROR.value) |
| 76 | sub_aud.metrics(error_msg) |
| 77 | return |
| 78 | |
| 79 | sub_aud.set_http_status_code(res.status_code) |
| 80 | audit.set_http_status_code(res.status_code) |
| 81 | |
Alex Shatov | 42a989a | 2017-09-12 13:00:25 -0400 | [diff] [blame^] | 82 | sub_aud.metrics( |
alex_sh | 9d980ce | 2017-08-23 17:30:56 -0400 | [diff] [blame] | 83 | "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^] | 84 | .format(DeployHandler._url_path, res.status_code, msg_str, res.text, |
| 85 | res.request.headers)) |
alex_sh | 9d980ce | 2017-08-23 17:30:56 -0400 | [diff] [blame] | 86 | |
| 87 | if res.status_code == requests.codes.ok: |
| 88 | return res.json() |