alex_sh | 9d980ce | 2017-08-23 17:30:56 -0400 | [diff] [blame] | 1 | # org.onap.dcae |
| 2 | # ================================================================================ |
Alex Shatov | d80b5d5 | 2018-02-05 13:01:14 -0500 | [diff] [blame] | 3 | # Copyright (c) 2017-2018 AT&T Intellectual Property. All rights reserved. |
alex_sh | 9d980ce | 2017-08-23 17:30:56 -0400 | [diff] [blame] | 4 | # ================================================================================ |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | # ============LICENSE_END========================================================= |
| 17 | # |
| 18 | # ECOMP is a trademark and service mark of AT&T Intellectual Property. |
| 19 | |
Alex Shatov | f53e5e7 | 2018-01-11 11:15:56 -0500 | [diff] [blame] | 20 | """ send notification to deploy-handler""" |
| 21 | |
alex_sh | 9d980ce | 2017-08-23 17:30:56 -0400 | [diff] [blame] | 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 | ac779d3 | 2018-02-01 14:16:56 -0500 | [diff] [blame] | 66 | def policy_update(audit, message): |
Alex Shatov | 1369bea | 2018-01-10 11:00:50 -0500 | [diff] [blame] | 67 | """post policy_updated message to deploy-handler""" |
Alex Shatov | ac779d3 | 2018-02-01 14:16:56 -0500 | [diff] [blame] | 68 | if not message: |
Alex Shatov | 1369bea | 2018-01-10 11:00:50 -0500 | [diff] [blame] | 69 | return |
| 70 | |
alex_sh | 9d980ce | 2017-08-23 17:30:56 -0400 | [diff] [blame] | 71 | DeployHandler._lazy_init() |
Alex Shatov | 42a989a | 2017-09-12 13:00:25 -0400 | [diff] [blame] | 72 | sub_aud = Audit(aud_parent=audit, targetEntity=DeployHandler._target_entity, |
| 73 | targetServiceName=DeployHandler._url_path) |
alex_sh | 9d980ce | 2017-08-23 17:30:56 -0400 | [diff] [blame] | 74 | headers = {REQUEST_X_ECOMP_REQUESTID : sub_aud.request_id} |
| 75 | |
Alex Shatov | ac779d3 | 2018-02-01 14:16:56 -0500 | [diff] [blame] | 76 | msg_str = json.dumps(message) |
alex_sh | 9d980ce | 2017-08-23 17:30:56 -0400 | [diff] [blame] | 77 | headers_str = json.dumps(headers) |
| 78 | |
Alex Shatov | ac779d3 | 2018-02-01 14:16:56 -0500 | [diff] [blame] | 79 | DeployHandler._logger.info("message: %s", msg_str) |
Alex Shatov | 42a989a | 2017-09-12 13:00:25 -0400 | [diff] [blame] | 80 | log_line = "post to deployment-handler {0} msg={1} headers={2}".format( |
| 81 | DeployHandler._url_path, msg_str, headers_str) |
Alex Shatov | 1369bea | 2018-01-10 11:00:50 -0500 | [diff] [blame] | 82 | |
alex_sh | 9d980ce | 2017-08-23 17:30:56 -0400 | [diff] [blame] | 83 | DeployHandler._logger.info(log_line) |
Alex Shatov | 1369bea | 2018-01-10 11:00:50 -0500 | [diff] [blame] | 84 | sub_aud.metrics_start(log_line) |
| 85 | |
| 86 | if not DeployHandler._url: |
| 87 | error_msg = "no url found to {0}".format(log_line) |
| 88 | DeployHandler._logger.error(error_msg) |
| 89 | sub_aud.set_http_status_code(AuditHttpCode.SERVICE_UNAVAILABLE_ERROR.value) |
| 90 | audit.set_http_status_code(AuditHttpCode.SERVICE_UNAVAILABLE_ERROR.value) |
| 91 | sub_aud.metrics(error_msg) |
| 92 | return |
alex_sh | 9d980ce | 2017-08-23 17:30:56 -0400 | [diff] [blame] | 93 | |
| 94 | res = None |
| 95 | try: |
Alex Shatov | 541b5bc | 2017-09-13 16:22:17 -0400 | [diff] [blame] | 96 | res = DeployHandler._requests_session.post( |
Alex Shatov | ac779d3 | 2018-02-01 14:16:56 -0500 | [diff] [blame] | 97 | DeployHandler._url_path, json=message, headers=headers |
Alex Shatov | 541b5bc | 2017-09-13 16:22:17 -0400 | [diff] [blame] | 98 | ) |
alex_sh | 9d980ce | 2017-08-23 17:30:56 -0400 | [diff] [blame] | 99 | except requests.exceptions.RequestException as ex: |
| 100 | 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] | 101 | .format(DeployHandler._url_path, str(ex), msg_str, headers_str) |
alex_sh | 9d980ce | 2017-08-23 17:30:56 -0400 | [diff] [blame] | 102 | DeployHandler._logger.exception(error_msg) |
| 103 | sub_aud.set_http_status_code(AuditHttpCode.SERVICE_UNAVAILABLE_ERROR.value) |
| 104 | audit.set_http_status_code(AuditHttpCode.SERVICE_UNAVAILABLE_ERROR.value) |
| 105 | sub_aud.metrics(error_msg) |
| 106 | return |
| 107 | |
| 108 | sub_aud.set_http_status_code(res.status_code) |
| 109 | audit.set_http_status_code(res.status_code) |
| 110 | |
Alex Shatov | 42a989a | 2017-09-12 13:00:25 -0400 | [diff] [blame] | 111 | sub_aud.metrics( |
alex_sh | 9d980ce | 2017-08-23 17:30:56 -0400 | [diff] [blame] | 112 | "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] | 113 | .format(DeployHandler._url_path, res.status_code, msg_str, res.text, |
| 114 | res.request.headers)) |
alex_sh | 9d980ce | 2017-08-23 17:30:56 -0400 | [diff] [blame] | 115 | |
| 116 | if res.status_code == requests.codes.ok: |
| 117 | return res.json() |