blob: c839e5c6256bab92c9f579c4c80cb70b95d58d50 [file] [log] [blame]
alex_sh9d980ce2017-08-23 17:30:56 -04001# org.onap.dcae
2# ================================================================================
Alex Shatovd80b5d52018-02-05 13:01:14 -05003# Copyright (c) 2017-2018 AT&T Intellectual Property. All rights reserved.
alex_sh9d980ce2017-08-23 17:30:56 -04004# ================================================================================
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 Shatovf53e5e72018-01-11 11:15:56 -050020""" send notification to deploy-handler"""
21
alex_sh9d980ce2017-08-23 17:30:56 -040022import logging
23import json
24import requests
25
26from .config import Config
27from .discovery import DiscoveryClient
28from .onap.audit import REQUEST_X_ECOMP_REQUESTID, Audit, AuditHttpCode
29
Alex Shatov541b5bc2017-09-13 16:22:17 -040030POOL_SIZE = 1
31
alex_sh9d980ce2017-08-23 17:30:56 -040032class DeployHandler(object):
33 """ deploy-handler """
34 _logger = logging.getLogger("policy_handler.deploy_handler")
alex_sh9d980ce2017-08-23 17:30:56 -040035 _lazy_inited = False
Alex Shatov541b5bc2017-09-13 16:22:17 -040036
37 _requests_session = None
alex_sh9d980ce2017-08-23 17:30:56 -040038 _config = None
39 _url = None
Alex Shatov42a989a2017-09-12 13:00:25 -040040 _url_path = None
alex_sh9d980ce2017-08-23 17:30:56 -040041 _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 Shatov541b5bc2017-09-13 16:22:17 -040049
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_sh9d980ce2017-08-23 17:30:56 -040060 DeployHandler._target_entity = Config.config["deploy_handler"]
61 DeployHandler._url = DiscoveryClient.get_service_url(DeployHandler._target_entity)
Alex Shatov1369bea2018-01-10 11:00:50 -050062 DeployHandler._url_path = (DeployHandler._url or "") + '/policy'
alex_sh9d980ce2017-08-23 17:30:56 -040063 DeployHandler._logger.info("DeployHandler url(%s)", DeployHandler._url)
64
65 @staticmethod
Alex Shatovac779d32018-02-01 14:16:56 -050066 def policy_update(audit, message):
Alex Shatov1369bea2018-01-10 11:00:50 -050067 """post policy_updated message to deploy-handler"""
Alex Shatovac779d32018-02-01 14:16:56 -050068 if not message:
Alex Shatov1369bea2018-01-10 11:00:50 -050069 return
70
alex_sh9d980ce2017-08-23 17:30:56 -040071 DeployHandler._lazy_init()
Alex Shatov42a989a2017-09-12 13:00:25 -040072 sub_aud = Audit(aud_parent=audit, targetEntity=DeployHandler._target_entity,
73 targetServiceName=DeployHandler._url_path)
alex_sh9d980ce2017-08-23 17:30:56 -040074 headers = {REQUEST_X_ECOMP_REQUESTID : sub_aud.request_id}
75
Alex Shatovac779d32018-02-01 14:16:56 -050076 msg_str = json.dumps(message)
alex_sh9d980ce2017-08-23 17:30:56 -040077 headers_str = json.dumps(headers)
78
Alex Shatovac779d32018-02-01 14:16:56 -050079 DeployHandler._logger.info("message: %s", msg_str)
Alex Shatov42a989a2017-09-12 13:00:25 -040080 log_line = "post to deployment-handler {0} msg={1} headers={2}".format(
81 DeployHandler._url_path, msg_str, headers_str)
Alex Shatov1369bea2018-01-10 11:00:50 -050082
alex_sh9d980ce2017-08-23 17:30:56 -040083 DeployHandler._logger.info(log_line)
Alex Shatov1369bea2018-01-10 11:00:50 -050084 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_sh9d980ce2017-08-23 17:30:56 -040093
94 res = None
95 try:
Alex Shatov541b5bc2017-09-13 16:22:17 -040096 res = DeployHandler._requests_session.post(
Alex Shatovac779d32018-02-01 14:16:56 -050097 DeployHandler._url_path, json=message, headers=headers
Alex Shatov541b5bc2017-09-13 16:22:17 -040098 )
alex_sh9d980ce2017-08-23 17:30:56 -040099 except requests.exceptions.RequestException as ex:
100 error_msg = "failed to post to deployment-handler {0} {1} msg={2} headers={3}" \
Alex Shatov42a989a2017-09-12 13:00:25 -0400101 .format(DeployHandler._url_path, str(ex), msg_str, headers_str)
alex_sh9d980ce2017-08-23 17:30:56 -0400102 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 Shatov42a989a2017-09-12 13:00:25 -0400111 sub_aud.metrics(
alex_sh9d980ce2017-08-23 17:30:56 -0400112 "response from deployment-handler to post {0}: {1} msg={2} text={3} headers={4}" \
Alex Shatov42a989a2017-09-12 13:00:25 -0400113 .format(DeployHandler._url_path, res.status_code, msg_str, res.text,
114 res.request.headers))
alex_sh9d980ce2017-08-23 17:30:56 -0400115
116 if res.status_code == requests.codes.ok:
117 return res.json()