blob: a641a95f675dbd3d562756f6467064d38a69ffcc [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
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 Shatov1369bea2018-01-10 11:00:50 -050066 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_sh9d980ce2017-08-23 17:30:56 -040076 DeployHandler._lazy_init()
Alex Shatov1369bea2018-01-10 11:00:50 -050077 msg = {
78 "catch_up" : catch_up,
79 "latest_policies" : latest_policies,
80 "removed_policies" : removed_policies,
81 "errored_policies" : errored_policies
82 }
Alex Shatov42a989a2017-09-12 13:00:25 -040083 sub_aud = Audit(aud_parent=audit, targetEntity=DeployHandler._target_entity,
84 targetServiceName=DeployHandler._url_path)
alex_sh9d980ce2017-08-23 17:30:56 -040085 headers = {REQUEST_X_ECOMP_REQUESTID : sub_aud.request_id}
86
87 msg_str = json.dumps(msg)
88 headers_str = json.dumps(headers)
89
Alex Shatov1369bea2018-01-10 11:00:50 -050090 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 Shatov42a989a2017-09-12 13:00:25 -040093 log_line = "post to deployment-handler {0} msg={1} headers={2}".format(
94 DeployHandler._url_path, msg_str, headers_str)
Alex Shatov1369bea2018-01-10 11:00:50 -050095
alex_sh9d980ce2017-08-23 17:30:56 -040096 DeployHandler._logger.info(log_line)
Alex Shatov1369bea2018-01-10 11:00:50 -050097 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_sh9d980ce2017-08-23 17:30:56 -0400106
107 res = None
108 try:
Alex Shatov541b5bc2017-09-13 16:22:17 -0400109 res = DeployHandler._requests_session.post(
110 DeployHandler._url_path, json=msg, headers=headers
111 )
alex_sh9d980ce2017-08-23 17:30:56 -0400112 except requests.exceptions.RequestException as ex:
113 error_msg = "failed to post to deployment-handler {0} {1} msg={2} headers={3}" \
Alex Shatov42a989a2017-09-12 13:00:25 -0400114 .format(DeployHandler._url_path, str(ex), msg_str, headers_str)
alex_sh9d980ce2017-08-23 17:30:56 -0400115 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 Shatov42a989a2017-09-12 13:00:25 -0400124 sub_aud.metrics(
alex_sh9d980ce2017-08-23 17:30:56 -0400125 "response from deployment-handler to post {0}: {1} msg={2} text={3} headers={4}" \
Alex Shatov42a989a2017-09-12 13:00:25 -0400126 .format(DeployHandler._url_path, res.status_code, msg_str, res.text,
127 res.request.headers))
alex_sh9d980ce2017-08-23 17:30:56 -0400128
129 if res.status_code == requests.codes.ok:
130 return res.json()