summaryrefslogtreecommitdiffstats
path: root/policyhandler/deploy_handler.py
blob: b4b24684dfaaf37eca7e0dcca1d76a056dd736cc (plain)
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
# ================================================================================
# Copyright (c) 2017-2018 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=========================================================
#
# ECOMP is a trademark and service mark of AT&T Intellectual Property.

"""send policy-update notification to deployment-handler"""

import json
import logging
from copy import copy, deepcopy
from threading import Lock

import requests

from .config import Config, Settings
from .discovery import DiscoveryClient
from .onap.audit import (REQUEST_X_ECOMP_REQUESTID, AuditHttpCode,
                         AuditResponseCode, Metrics)
from .policy_consts import (CATCH_UP, LATEST_POLICIES, POLICIES,
                            POLICY_FILTER_MATCHES, POLICY_FILTERS,
                            REMOVED_POLICIES, TARGET_ENTITY)


class PolicyUpdateMessage(object):
    """class for messages to deployment-handler on policy-update"""
    BYTES_IN_MB = 1 << 2 * 10

    def __init__(self, latest_policies=None,
                 removed_policies=None, policy_filter_matches=None, catch_up=True):
        """init"""
        self._catch_up = catch_up
        self._latest_policies = deepcopy(latest_policies or {})
        self._removed_policies = copy(removed_policies or {})
        self._policy_filter_matches = deepcopy(policy_filter_matches or {})

        self._message = {
            CATCH_UP: self._catch_up,
            LATEST_POLICIES: self._latest_policies,
            REMOVED_POLICIES: self._removed_policies,
            POLICY_FILTER_MATCHES: self._policy_filter_matches
        }
        self.msg_length = 0
        self._calc_stats()

    def _calc_stats(self):
        """generate the message and calc stats"""
        self.msg_length = len(json.dumps(self._message))

    def empty(self):
        """checks whether have any data"""
        return (not self._latest_policies
                and not self._removed_policies
                and not self._policy_filter_matches)

    def add(self, policy_id, latest_policy=None, policy_filter_ids=None, removed_policy=None):
        """add the parts from the other message to the current message"""
        if not policy_id or not (latest_policy or policy_filter_ids or removed_policy):
            return

        if latest_policy:
            self._latest_policies[policy_id] = deepcopy(latest_policy)

        if policy_filter_ids is not None:
            if policy_id not in self._policy_filter_matches:
                self._policy_filter_matches[policy_id] = {}
            self._policy_filter_matches[policy_id].update(policy_filter_ids)

        if removed_policy is not None:
            self._removed_policies[policy_id] = removed_policy

        self._calc_stats()

    def get_message(self):
        """expose the copy of the message"""
        return deepcopy(self._message)

    def __str__(self):
        """to string"""
        return json.dumps(self._message)

    def _iter_over_removed_policies(self):
        """generator of iterator over removed_policies"""
        for (policy_id, value) in self._removed_policies.items():
            yield (policy_id, value)

    def _iter_over_latest_policies(self):
        """generator of iterator over latest_policies and policy_filter_matches"""
        for (policy_id, policy) in self._latest_policies.items():
            yield (policy_id, policy, self._policy_filter_matches.get(policy_id))

    def gen_segmented_messages(self, max_msg_length_mb):
        """
        Break the policy-update message into a list of segmented messages.

        Each segmented message should not exceed the max_msg_length_mb from config.
        """
        max_msg_length_mb = (max_msg_length_mb or 10) * PolicyUpdateMessage.BYTES_IN_MB

        messages = []
        curr_message = PolicyUpdateMessage(catch_up=self._catch_up)

        for (policy_id, value) in self._iter_over_removed_policies():
            if (not curr_message.empty()
                    and (len(policy_id) + len(str(value)) + curr_message.msg_length
                         > max_msg_length_mb)):
                messages.append(curr_message.get_message())
                curr_message = PolicyUpdateMessage(catch_up=self._catch_up)
            curr_message.add(policy_id, removed_policy=value)

        for (policy_id, policy, policy_filter_ids) in self._iter_over_latest_policies():
            if (not curr_message.empty()
                    and (2 * len(policy_id) + len(json.dumps(policy))
                         + len(json.dumps(policy_filter_ids))
                         + curr_message.msg_length > max_msg_length_mb)):
                messages.append(curr_message.get_message())
                curr_message = PolicyUpdateMessage(catch_up=self._catch_up)
            curr_message.add(policy_id, latest_policy=policy, policy_filter_ids=policy_filter_ids)

        if not curr_message.empty():
            messages.append(curr_message.get_message())

        msg_count = len(messages)
        if msg_count > 1:
            msg_count = "/" + str(msg_count)
            for idx, msg in enumerate(messages):
                msg["data_segment"] = str((idx+1)) + msg_count

        return messages


class DeployHandler(object):
    """calling the deployment-handler web apis"""
    _logger = logging.getLogger("policy_handler.deploy_handler")
    DEFAULT_TARGET_ENTITY = "deployment_handler"

    _lazy_inited = False
    _lock = Lock()
    _settings = Settings(Config.POOL_CONNECTIONS, Config.DEPLOY_HANDLER)

    _requests_session = None
    _url = None
    _url_policy = None
    _max_msg_length_mb = 10
    _query = {}
    _target_entity = None
    _custom_kwargs = {}
    _server_instance_uuid = None
    server_instance_changed = False

    @staticmethod
    def _init(audit):
        """set config"""
        DeployHandler._custom_kwargs = {}

        if not DeployHandler._requests_session:
            DeployHandler._requests_session = requests.Session()

        changed, pool_size = DeployHandler._settings.get_by_key(Config.POOL_CONNECTIONS, 10)
        if changed:
            DeployHandler._requests_session.mount(
                'https://', requests.adapters.HTTPAdapter(pool_connections=pool_size,
                                                          pool_maxsize=pool_size))
            DeployHandler._requests_session.mount(
                'http://', requests.adapters.HTTPAdapter(pool_connections=pool_size,
                                                         pool_maxsize=pool_size))

        _, config_dh = DeployHandler._settings.get_by_key(Config.DEPLOY_HANDLER)
        if config_dh and isinstance(config_dh, dict):
            # dns based routing to deployment-handler
            # config for policy-handler >= 2.4.0
            # "deploy_handler" : {
            #     "target_entity" : "deployment_handler",
            #     "url" : "https://deployment_handler:8188",
            #     "max_msg_length_mb" : 10,
            #     "query" : {
            #         "cfy_tenant_name" : "default_tenant"
            #     },
            #     "tls_ca_mode" : "cert_directory"
            # }
            DeployHandler._target_entity = config_dh.get(TARGET_ENTITY,
                                                         DeployHandler.DEFAULT_TARGET_ENTITY)
            DeployHandler._url = config_dh.get("url")
            DeployHandler._max_msg_length_mb = config_dh.get("max_msg_length_mb",
                                                             DeployHandler._max_msg_length_mb)
            DeployHandler._query = deepcopy(config_dh.get("query", {}))
            tls_ca_mode = config_dh.get(Config.TLS_CA_MODE)
            DeployHandler._custom_kwargs = Config.get_requests_kwargs(tls_ca_mode)

            DeployHandler._logger.info(
                "dns based routing to %s: url(%s) tls_ca_mode(%s) custom_kwargs(%s)",
                DeployHandler._target_entity, DeployHandler._url,
                tls_ca_mode, json.dumps(DeployHandler._custom_kwargs))

        if not DeployHandler._url:
            # discover routing to deployment-handler at consul-services
            if not isinstance(config_dh, dict):
                # config for policy-handler <= 2.3.1
                # "deploy_handler" : "deployment_handler"
                DeployHandler._target_entity = str(config_dh or DeployHandler.DEFAULT_TARGET_ENTITY)
            DeployHandler._url = DiscoveryClient.get_service_url(audit,
                                                                 DeployHandler._target_entity)

        DeployHandler._url_policy = str(DeployHandler._url or "") + '/policy'
        DeployHandler._logger.info("got %s policy url(%s): %s", DeployHandler._target_entity,
                                   DeployHandler._url_policy, DeployHandler._settings)

        DeployHandler._settings.commit_change()
        DeployHandler._lazy_inited = bool(DeployHandler._url)

    @staticmethod
    def reconfigure(audit):
        """reconfigure"""
        with DeployHandler._lock:
            DeployHandler._settings.set_config(Config.discovered_config)
            if not DeployHandler._settings.is_changed():
                DeployHandler._settings.commit_change()
                return False

            DeployHandler._lazy_inited = False
            DeployHandler._init(audit)
        return True

    @staticmethod
    def _lazy_init(audit):
        """set config"""
        if DeployHandler._lazy_inited:
            return

        with DeployHandler._lock:
            if DeployHandler._lazy_inited:
                return

            DeployHandler._settings.set_config(Config.discovered_config)
            DeployHandler._init(audit)

    @staticmethod
    def policy_update(audit, policy_update_message):
        """
        segments the big policy_update_message limited by size
        and sequatially sends each segment as put to deployment-handler at /policy.

        param policy_update_message is of PolicyUpdateMessage type
        """
        if not policy_update_message or policy_update_message.empty():
            return

        DeployHandler._lazy_init(audit)

        str_metrics = "policy_update {}".format(str(policy_update_message))

        metrics_total = Metrics(
            aud_parent=audit,
            targetEntity="{} total policy_update".format(DeployHandler._target_entity),
            targetServiceName=DeployHandler._url_policy)

        metrics_total.metrics_start("started {}".format(str_metrics))
        messages = policy_update_message.gen_segmented_messages(DeployHandler._max_msg_length_mb)
        for message in messages:
            DeployHandler._policy_update(audit, message)
            if not audit.is_success():
                break
        metrics_total.metrics("done {}".format(str_metrics))

    @staticmethod
    def _policy_update(audit, message):
        """
        sends the put message to deployment-handler at /policy

        detects whether server_instance_changed condition on deployment-handler
        that is the cause to catch_up
        """
        if not message:
            return

        with DeployHandler._lock:
            session = DeployHandler._requests_session
            target_entity = DeployHandler._target_entity
            url = DeployHandler._url_policy
            params = deepcopy(DeployHandler._query)
            custom_kwargs = deepcopy(DeployHandler._custom_kwargs)

        metrics = Metrics(aud_parent=audit, targetEntity="{} policy_update".format(target_entity),
                          targetServiceName=url)
        headers = {REQUEST_X_ECOMP_REQUESTID : metrics.request_id}

        log_action = "put to {} at {}".format(target_entity, url)
        log_data = "msg={} headers={}, params={} custom_kwargs({})".format(
            json.dumps(message), json.dumps(headers),
            json.dumps(params), json.dumps(custom_kwargs))
        log_line = log_action + " " + log_data

        DeployHandler._logger.info(log_line)
        metrics.metrics_start(log_line)

        if not DeployHandler._url:
            error_msg = "no url found to {0}".format(log_line)
            DeployHandler._logger.error(error_msg)
            metrics.set_http_status_code(AuditHttpCode.SERVICE_UNAVAILABLE_ERROR.value)
            audit.set_http_status_code(AuditHttpCode.SERVICE_UNAVAILABLE_ERROR.value)
            metrics.metrics(error_msg)
            return

        res = None
        try:
            res = session.put(url, json=message, headers=headers, params=params, **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(
                log_action, type(ex).__name__, str(ex), log_data)
            DeployHandler._logger.exception(error_msg)
            metrics.set_http_status_code(error_code)
            audit.set_http_status_code(error_code)
            metrics.metrics(error_msg)
            return

        metrics.set_http_status_code(res.status_code)
        audit.set_http_status_code(res.status_code)

        log_line = "response {} from {}: text={} {}".format(
            res.status_code, log_action, res.text, log_data)
        metrics.metrics(log_line)

        if res.status_code != requests.codes.ok:
            DeployHandler._logger.error(log_line)
            return

        DeployHandler._logger.info(log_line)
        result = res.json() or {}
        DeployHandler._server_instance_changed(result, metrics)


    @staticmethod
    def get_deployed_policies(audit):
        """
        Retrieves policies and policy-filters from components
        that were deployed by deployment-handler
        """
        DeployHandler._lazy_init(audit)

        with DeployHandler._lock:
            session = DeployHandler._requests_session
            target_entity = DeployHandler._target_entity
            url = DeployHandler._url_policy
            params = deepcopy(DeployHandler._query)
            custom_kwargs = deepcopy(DeployHandler._custom_kwargs)

        metrics = Metrics(aud_parent=audit,
                          targetEntity="{} get_deployed_policies".format(target_entity),
                          targetServiceName=url)
        headers = {REQUEST_X_ECOMP_REQUESTID : metrics.request_id}

        log_action = "get from {} at {}".format(target_entity, url)
        log_data = "headers={}, params={} custom_kwargs({})".format(
            json.dumps(headers), json.dumps(params), json.dumps(custom_kwargs))
        log_line = log_action + " " + log_data

        DeployHandler._logger.info(log_line)
        metrics.metrics_start(log_line)

        if not DeployHandler._url:
            error_msg = "no url found to {}".format(log_line)
            DeployHandler._logger.error(error_msg)
            metrics.set_http_status_code(AuditHttpCode.SERVICE_UNAVAILABLE_ERROR.value)
            audit.set_http_status_code(AuditHttpCode.SERVICE_UNAVAILABLE_ERROR.value)
            metrics.metrics(error_msg)
            return None, None

        res = None
        try:
            res = session.get(url, headers=headers, params=params, **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(
                log_action, type(ex).__name__, str(ex), log_data)
            DeployHandler._logger.exception(error_msg)
            metrics.set_http_status_code(error_code)
            audit.set_http_status_code(error_code)
            metrics.metrics(error_msg)
            return None, None

        metrics.set_http_status_code(res.status_code)
        audit.set_http_status_code(res.status_code)

        log_line = "response {} from {}: text={} {}".format(
            res.status_code, log_action, res.text, log_data)
        metrics.metrics(log_line)

        if res.status_code != requests.codes.ok:
            DeployHandler._logger.error(log_line)
            return None, None

        result = res.json() or {}
        DeployHandler._server_instance_changed(result, metrics)

        policies = result.get(POLICIES, {})
        policy_filters = result.get(POLICY_FILTERS, {})
        if not policies and not policy_filters:
            audit.set_http_status_code(AuditHttpCode.DATA_NOT_FOUND_ERROR.value)
            DeployHandler._logger.warning(audit.warn(
                "found no deployed policies or policy-filters: {}".format(log_line),
                error_code=AuditResponseCode.DATA_ERROR))
            return policies, policy_filters

        DeployHandler._logger.info(log_line)
        return policies, policy_filters

    @staticmethod
    def _server_instance_changed(result, metrics):
        """Checks whether the deployment-handler instance changed since last call."""
        prev_server_instance_uuid = DeployHandler._server_instance_uuid
        DeployHandler._server_instance_uuid = result.get("server_instance_uuid")

        if (prev_server_instance_uuid
                and prev_server_instance_uuid != DeployHandler._server_instance_uuid):
            DeployHandler.server_instance_changed = True

            DeployHandler._logger.info(metrics.info(
                "deployment_handler_changed: {1} != {0}"
                .format(prev_server_instance_uuid, DeployHandler._server_instance_uuid)))