1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
|
# ================================================================================
# Copyright (c) 2019 AT&T Intellectual Property. All rights reserved.
# ================================================================================
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============LICENSE_END=========================================================
#
"""policy-client communicates with policy-engine thru REST API"""
import copy
import json
from threading import Lock
import requests
from ..config import Config, Settings
from ..onap.audit import AuditHttpCode, AuditResponseCode, Metrics
from ..utils import Utils
_LOGGER = Utils.get_logger(__file__)
class DmaapMr(object):
"""using the http API to policy-engine"""
_lazy_inited = False
DEFAULT_TIMEOUT_IN_SECS = 60
_lock = Lock()
_settings = Settings(Config.DMAAP_MR)
_requests_session = None
_long_polling = False
_target_entity = None
_url = None
_query = {}
_headers = None
_custom_kwargs = {}
_timeout_in_secs = DEFAULT_TIMEOUT_IN_SECS
@staticmethod
def _init(audit):
"""init static config"""
DmaapMr._custom_kwargs = {}
tls_ca_mode = None
if not DmaapMr._requests_session:
DmaapMr._requests_session = requests.Session()
DmaapMr._requests_session.mount(
'https://', requests.adapters.HTTPAdapter(pool_connections=1, pool_maxsize=1,
pool_block=True))
DmaapMr._requests_session.mount(
'http://', requests.adapters.HTTPAdapter(pool_connections=1, pool_maxsize=1,
pool_block=True))
_, config = DmaapMr._settings.get_by_key(Config.DMAAP_MR)
if config:
DmaapMr._url = config.get("url")
DmaapMr._headers = config.get("headers", {})
DmaapMr._query = copy.deepcopy(config.get("query", {}))
if DmaapMr._query.get(Config.QUERY_TIMEOUT, 0) < 1000:
DmaapMr._query[Config.QUERY_TIMEOUT] = 15000
DmaapMr._target_entity = config.get("target_entity", Config.DMAAP_MR)
tls_ca_mode = config.get(Config.TLS_CA_MODE)
DmaapMr._custom_kwargs = Config.get_requests_kwargs(tls_ca_mode)
DmaapMr._timeout_in_secs = config.get(Config.TIMEOUT_IN_SECS)
if not DmaapMr._timeout_in_secs or DmaapMr._timeout_in_secs < 1:
DmaapMr._timeout_in_secs = DmaapMr.DEFAULT_TIMEOUT_IN_SECS
_LOGGER.info(
audit.info(("config DMaaP MR({}) url({}) query({}) headers({}) "
"tls_ca_mode({}) custom_kwargs({}) timeout_in_secs({}): {}").format(
DmaapMr._target_entity, DmaapMr._url,
Metrics.json_dumps(DmaapMr._query),
Metrics.json_dumps(DmaapMr._headers), tls_ca_mode,
json.dumps(DmaapMr._custom_kwargs), DmaapMr._timeout_in_secs,
DmaapMr._settings)))
DmaapMr._settings.commit_change()
DmaapMr._lazy_inited = True
@staticmethod
def reconfigure(audit):
"""reconfigure"""
with DmaapMr._lock:
DmaapMr._settings.set_config(Config.discovered_config)
if not DmaapMr._settings.is_changed():
DmaapMr._settings.commit_change()
return False
DmaapMr._lazy_inited = False
DmaapMr._long_polling = False
DmaapMr._init(audit)
return True
@staticmethod
def _lazy_init(audit):
"""init static config"""
if DmaapMr._lazy_inited:
return
with DmaapMr._lock:
if DmaapMr._lazy_inited:
return
DmaapMr._settings.set_config(Config.discovered_config)
DmaapMr._long_polling = False
DmaapMr._init(audit)
@staticmethod
def get_policy_updates(audit):
"""
get from DMaaP MR - returns json list of stringified messages
example [
"{\"deployed-policies\":[
{\"policy-type\":\"onap.policies.monitoring.cdap.tca.hi.lo.app\",
\"policy-type-version\":\"1.0.0\",
\"policy-id\":\"onap.scaleout.tca\",
\"policy-version\":\"2.2.2\",
\"success-count\":3,
\"failure-count\":0
}],
\"undeployed-policies\":[
{\"policy-type\":\"onap.policies.monitoring.cdap.tca.hi.lo.app\",
\"policy-type-version\":\"1.0.0\",
\"policy-id\":\"onap.scaleout.tca\",
\"policy-version\":\"1.0.0\",
\"success-count\":3,
\"failure-count\":0
}]}"
]
"""
DmaapMr._lazy_init(audit)
if not DmaapMr._url:
_LOGGER.error(
audit.error("no url for DMaaP MR", error_code=AuditResponseCode.AVAILABILITY_ERROR))
audit.set_http_status_code(AuditHttpCode.SERVER_INTERNAL_ERROR.value)
return None
with DmaapMr._lock:
target_entity = DmaapMr._target_entity
url = DmaapMr._url
params = copy.deepcopy(DmaapMr._query) if DmaapMr._long_polling else None
headers = copy.deepcopy(DmaapMr._headers)
timeout_in_secs = DmaapMr._timeout_in_secs
custom_kwargs = copy.deepcopy(DmaapMr._custom_kwargs)
DmaapMr._long_polling = True
metrics = Metrics(aud_parent=audit, targetEntity=target_entity, targetServiceName=url)
headers = metrics.put_request_id_into_headers(headers)
log_line = (
"get from {} at {} with params={}, headers={}, custom_kwargs({}) timeout_in_secs({})"
.format(target_entity, url, json.dumps(params), Metrics.json_dumps(headers),
json.dumps(custom_kwargs), timeout_in_secs))
_LOGGER.info(metrics.metrics_start(log_line))
res = None
try:
res = DmaapMr._requests_session.get(url, params=params, headers=headers,
timeout=timeout_in_secs, **custom_kwargs)
except Exception as ex:
error_code = (AuditHttpCode.SERVICE_UNAVAILABLE_ERROR.value
if isinstance(ex, requests.exceptions.RequestException)
else AuditHttpCode.SERVER_INTERNAL_ERROR.value)
error_msg = ("failed {}: {} to {}".format(type(ex).__name__, str(ex), log_line))
_LOGGER.exception(metrics.fatal(error_msg))
metrics.set_http_status_code(error_code)
audit.set_http_status_code(error_code)
metrics.metrics(error_msg)
return None
log_line = "response {} from {}: text={} headers={}".format(
res.status_code, log_line, res.text, Metrics.json_dumps(dict(res.headers.items())))
_LOGGER.info(log_line)
metrics.set_http_status_code(res.status_code)
audit.set_http_status_code(res.status_code)
metrics.metrics(log_line)
policy_updates = None
if res.status_code == requests.codes.ok:
policy_updates = res.json()
return policy_updates
|