blob: 7d9c51393b9acd9ddee1f70091ef5c3e256f436f [file] [log] [blame]
alex_sh9d980ce2017-08-23 17:30:56 -04001""" 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
22import 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
30class DeployHandler(object):
31 """ deploy-handler """
32 _logger = logging.getLogger("policy_handler.deploy_handler")
alex_sh9d980ce2017-08-23 17:30:56 -040033 _lazy_inited = False
34 _config = None
35 _url = None
Alex Shatov42a989a2017-09-12 13:00:25 -040036 _url_path = None
alex_sh9d980ce2017-08-23 17:30:56 -040037 _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 Shatov42a989a2017-09-12 13:00:25 -040047 DeployHandler._url_path = DeployHandler._url + '/policy'
alex_sh9d980ce2017-08-23 17:30:56 -040048 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 Shatov42a989a2017-09-12 13:00:25 -040055 sub_aud = Audit(aud_parent=audit, targetEntity=DeployHandler._target_entity,
56 targetServiceName=DeployHandler._url_path)
alex_sh9d980ce2017-08-23 17:30:56 -040057 headers = {REQUEST_X_ECOMP_REQUESTID : sub_aud.request_id}
58
59 msg_str = json.dumps(msg)
60 headers_str = json.dumps(headers)
61
Alex Shatov42a989a2017-09-12 13:00:25 -040062 log_line = "post to deployment-handler {0} msg={1} headers={2}".format(
63 DeployHandler._url_path, msg_str, headers_str)
alex_sh9d980ce2017-08-23 17:30:56 -040064 sub_aud.metrics_start(log_line)
65 DeployHandler._logger.info(log_line)
66
67 res = None
68 try:
Alex Shatov42a989a2017-09-12 13:00:25 -040069 res = requests.post(DeployHandler._url_path, json=msg, headers=headers)
alex_sh9d980ce2017-08-23 17:30:56 -040070 except requests.exceptions.RequestException as ex:
71 error_msg = "failed to post to deployment-handler {0} {1} msg={2} headers={3}" \
Alex Shatov42a989a2017-09-12 13:00:25 -040072 .format(DeployHandler._url_path, str(ex), msg_str, headers_str)
alex_sh9d980ce2017-08-23 17:30:56 -040073 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 Shatov42a989a2017-09-12 13:00:25 -040082 sub_aud.metrics(
alex_sh9d980ce2017-08-23 17:30:56 -040083 "response from deployment-handler to post {0}: {1} msg={2} text={3} headers={4}" \
Alex Shatov42a989a2017-09-12 13:00:25 -040084 .format(DeployHandler._url_path, res.status_code, msg_str, res.text,
85 res.request.headers))
alex_sh9d980ce2017-08-23 17:30:56 -040086
87 if res.status_code == requests.codes.ok:
88 return res.json()