From 331a93a59c60f570c2ab231b425f9cd46c07ccf4 Mon Sep 17 00:00:00 2001 From: emartin Date: Wed, 11 Dec 2019 11:48:53 +0000 Subject: Pull config from CBS Signed-off-by: emartin Issue-ID: DCAEGEN2-1843 Change-Id: Id97960dc02aca5531be016d70ea388473b556683 --- .../pmsh_service/mod/config_handler.py | 78 ++++++++++++++ .../pmsh_service/pmsh_service.py | 2 +- components/pm-subscription-handler/setup.py | 3 +- .../tests/config_handler_test.py | 112 +++++++++++++++++++++ .../tests/expected_config.json | 68 +++++++++++++ components/pm-subscription-handler/tox.ini | 2 + 6 files changed, 262 insertions(+), 3 deletions(-) create mode 100755 components/pm-subscription-handler/pmsh_service/mod/config_handler.py create mode 100755 components/pm-subscription-handler/tests/config_handler_test.py create mode 100755 components/pm-subscription-handler/tests/expected_config.json diff --git a/components/pm-subscription-handler/pmsh_service/mod/config_handler.py b/components/pm-subscription-handler/pmsh_service/mod/config_handler.py new file mode 100755 index 00000000..e9edbca4 --- /dev/null +++ b/components/pm-subscription-handler/pmsh_service/mod/config_handler.py @@ -0,0 +1,78 @@ +# ============LICENSE_START=================================================== +# Copyright (C) 2019-2020 Nordix Foundation. +# ============================================================================ +# 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. +# +# SPDX-License-Identifier: Apache-2.0 +# ============LICENSE_END===================================================== + +import json +from os import environ + +import requests +from tenacity import retry, wait_fixed, stop_after_attempt + +from pmsh_service.mod import pmsh_logging as logger + + +class ConfigHandler: + """ Handles retrieval of PMSH's configuration from Configbinding service.""" + + def __init__(self): + self.cbs_url = f'http://{self.cbs_hostname}:{str(self.cbs_port)}/' \ + f'service_component_all/{self.hostname}' + self._config = None + + @property + def cbs_hostname(self): + return _get_environment_variable('CONFIG_BINDING_SERVICE_SERVICE_HOST') + + @property + def cbs_port(self): + return _get_environment_variable('CONFIG_BINDING_SERVICE_SERVICE_PORT') + + @property + def hostname(self): + return _get_environment_variable('HOSTNAME') + + @retry(wait=wait_fixed(2), stop=stop_after_attempt(5)) + def get_config(self): + """ Retrieves PMSH's configuration from Configbinding service. If a non-2xx response + is received, it retries after 2 seconds for 5 times before raising an exception. + + Returns: + dict: Dictionary representation of the the service configuration + + Raises: + Exception: If any error occurred pulling configuration from Configbinding service. + """ + if self._config is None: + logger.debug('No configuration found, pulling from Configbinding Service.') + try: + response = requests.get(self.cbs_url) + response.raise_for_status() + self._config = response.json() + logger.debug(f'PMSH Configuration from Configbinding Service: {self._config}') + return json.loads(self._config) + except Exception as err: + raise Exception(f'Error retrieving configuration from CBS: {err}') + else: + return self._config + + +def _get_environment_variable(env_var_key): + try: + env_var = environ[env_var_key] + except KeyError as error: + raise KeyError(f'Environment variable {env_var_key} must be set. {error}') + return env_var diff --git a/components/pm-subscription-handler/pmsh_service/pmsh_service.py b/components/pm-subscription-handler/pmsh_service/pmsh_service.py index 6764c75f..8832f570 100755 --- a/components/pm-subscription-handler/pmsh_service/pmsh_service.py +++ b/components/pm-subscription-handler/pmsh_service/pmsh_service.py @@ -1,5 +1,5 @@ # ============LICENSE_START=================================================== -# Copyright (C) 2019 Nordix Foundation. +# Copyright (C) 2019-2020 Nordix Foundation. # ============================================================================ # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. diff --git a/components/pm-subscription-handler/setup.py b/components/pm-subscription-handler/setup.py index 9012a04c..a4d9ada4 100644 --- a/components/pm-subscription-handler/setup.py +++ b/components/pm-subscription-handler/setup.py @@ -30,6 +30,5 @@ setup( python_requires='>=3', install_requires=[ "requests==2.22.0", - "aiohttp==3.6.2", - "onappylog==1.0.9"], + "tenacity==6.0.0"], ) diff --git a/components/pm-subscription-handler/tests/config_handler_test.py b/components/pm-subscription-handler/tests/config_handler_test.py new file mode 100755 index 00000000..fcc25d60 --- /dev/null +++ b/components/pm-subscription-handler/tests/config_handler_test.py @@ -0,0 +1,112 @@ +# ============LICENSE_START=================================================== +# Copyright (C) 2019-2020 Nordix Foundation. +# ============================================================================ +# 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. +# +# SPDX-License-Identifier: Apache-2.0 +# ============LICENSE_END===================================================== + +import json +import unittest +from os import environ +from os import path +from unittest.mock import patch + +import requests +import responses +from tenacity import wait_none + +from pmsh_service.mod.config_handler import ConfigHandler + + +class ConfigHandlerTest(unittest.TestCase): + + def setUp(self): + self.env_vars = {'CONFIG_BINDING_SERVICE_SERVICE_HOST': 'cbs_hostname', + 'CONFIG_BINDING_SERVICE_SERVICE_PORT': '10000', + 'HOSTNAME': 'hostname'} + for key, value in self.env_vars.items(): + environ[key] = value + self.cbs_url = 'http://cbs_hostname:10000/service_component_all/hostname' + self.expected_config = self._get_expected_config() + + def test_missing_environment_variable(self): + for key, value in self.env_vars.items(): + with self.assertRaises(KeyError): + environ.pop(key) + test_value = globals()[value] + test_value() + environ[key] = value + + @responses.activate + def test_get_config_success(self): + responses.add(responses.GET, self.cbs_url, json=json.dumps(self.expected_config), + status=200) + + config_handler = ConfigHandler() + config_handler.get_config.retry.wait = wait_none() + + self.assertEqual(self.expected_config, config_handler.get_config()) + + def test_get_config_already_exists(self): + config_handler = ConfigHandler() + expected_config = self._get_expected_config() + config_handler._config = expected_config + + with patch.object(requests, 'get') as mock_get_request: + actual_config = config_handler.get_config() + + self.assertEqual(0, mock_get_request.call_count) + self.assertEqual(expected_config, actual_config) + + @responses.activate + def test_get_config_error(self): + responses.add(responses.GET, self.cbs_url, status=404) + config_handler = ConfigHandler() + config_handler.get_config.retry.wait = wait_none() + + with self.assertRaises(Exception): + config_handler.get_config() + + @responses.activate + def test_get_config_max_retries_error(self): + retry_limit = 5 + config_handler = ConfigHandler() + config_handler.get_config.retry.wait = wait_none() + + for __ in range(retry_limit): + responses.add(responses.GET, self.cbs_url, status=500) + + with self.assertRaises(Exception): + config_handler.get_config() + self.assertEqual(retry_limit, len(responses.calls)) + + @responses.activate + def test_get_config_less_than_5_retries_success(self): + retry_attempts = 4 + responses.add(responses.GET, self.cbs_url, status=500) + responses.add(responses.GET, self.cbs_url, status=400) + responses.add(responses.GET, self.cbs_url, status=300) + responses.add(responses.GET, self.cbs_url, json=json.dumps(self.expected_config), + status=200) + + config_handler = ConfigHandler() + config_handler.get_config.retry.wait = wait_none() + config_handler.get_config() + + self.assertEqual(retry_attempts, len(responses.calls)) + + @staticmethod + def _get_expected_config(): + with open(path.join(path.dirname(__file__), 'expected_config.json'))as json_file: + return json.load(json_file) diff --git a/components/pm-subscription-handler/tests/expected_config.json b/components/pm-subscription-handler/tests/expected_config.json new file mode 100755 index 00000000..43f67e88 --- /dev/null +++ b/components/pm-subscription-handler/tests/expected_config.json @@ -0,0 +1,68 @@ +{ + "config":{}, + "policy": { + "subscription": { + "subscriptionName": "someExtraPM-AllKista-gNB-R2B", + "administrativeState": "UNLOCKED", + "fileBasedGP": 15, + "fileLocation": "c:\/\/PM", + "nfTypeModelInvariantId": "2829292", + "nfFilter": { + "swVersions": [ + "A21", + "B" + ], + "nfNames": [ + "ABC", + "DEF", + "foo.*" + ] + }, + "measurementGroups": [ + { + "measurementGroup": { + "measurementTypes": [ + { + "measurementType": "countera" + }, + { + "measurementType": "counterb" + } + ], + "managedObjectDNsBasic": [ + { + "DN": "dna" + }, + { + "DN": "dnb" + } + ] + } + }, + { + "measurementGroup": { + "measurementTypes": [ + { + "measurementType": "counterc" + }, + { + "measurementType": "counterd" + } + ], + "managedObjectDNsBasic": [ + { + "DN": "dnc" + }, + { + "DN": "dnd" + } + ] + } + } + ] + } +} + + + +} \ No newline at end of file diff --git a/components/pm-subscription-handler/tox.ini b/components/pm-subscription-handler/tox.ini index fcbb647c..523338d1 100644 --- a/components/pm-subscription-handler/tox.ini +++ b/components/pm-subscription-handler/tox.ini @@ -26,6 +26,7 @@ deps= pytest coverage pytest-cov + responses==0.10.7 setenv = PYTHONPATH={toxinidir}/pmsh_service:{toxinidir}/pmsh_service/mod:{toxinidir}/tests commands= @@ -39,3 +40,4 @@ commands = flake8 pmsh_service tests [flake8] max-line-length=100 +ignore = E999 -- cgit 1.2.3-korg