From ae21e35b8eb8008cf1a3119bab2ad987db9f9e7f Mon Sep 17 00:00:00 2001 From: efiacor Date: Tue, 21 Apr 2020 13:39:35 +0100 Subject: [PMSH] Adding cbs module support Signed-off-by: efiacor Change-Id: Ie711995a3c7a2111f6cb872952507f511c0de6dd Issue-ID: DCAEGEN2-2156 --- .../pmsh_service/mod/aai_client.py | 4 +- .../pmsh_service/mod/config_handler.py | 74 ---------------------- .../pmsh_service/mod/pmsh_logging.py | 5 +- .../pmsh_service/mod/pmsh_utils.py | 25 ++++++++ .../pmsh_service/mod/subscription_handler.py | 7 +- 5 files changed, 34 insertions(+), 81 deletions(-) delete mode 100755 components/pm-subscription-handler/pmsh_service/mod/config_handler.py (limited to 'components/pm-subscription-handler/pmsh_service/mod') diff --git a/components/pm-subscription-handler/pmsh_service/mod/aai_client.py b/components/pm-subscription-handler/pmsh_service/mod/aai_client.py index 86cecb50..5e71da4d 100755 --- a/components/pm-subscription-handler/pmsh_service/mod/aai_client.py +++ b/components/pm-subscription-handler/pmsh_service/mod/aai_client.py @@ -55,7 +55,7 @@ def _get_all_aai_nf_data(): Return queried nf data from the AAI service. Returns: - json: the json response from AAI query, else None. + dict: the json response from AAI query, else None. """ nf_data = None try: @@ -94,7 +94,7 @@ def _get_aai_service_url(): """ try: aai_service = environ['AAI_SERVICE_HOST'] - aai_ssl_port = environ['AAI_SERVICE_PORT_AAI_SSL'] + aai_ssl_port = environ['AAI_SERVICE_PORT'] return f'https://{aai_service}:{aai_ssl_port}' except KeyError as e: logger.debug(f'Failed to get AAI env vars: {e}') diff --git a/components/pm-subscription-handler/pmsh_service/mod/config_handler.py b/components/pm-subscription-handler/pmsh_service/mod/config_handler.py deleted file mode 100755 index 26b03153..00000000 --- a/components/pm-subscription-handler/pmsh_service/mod/config_handler.py +++ /dev/null @@ -1,74 +0,0 @@ -# ============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===================================================== - -from os import environ - -import requests -from tenacity import retry, wait_fixed, stop_after_attempt, retry_if_exception_type - -import mod.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), retry=retry_if_exception_type(Exception)) - 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. - """ - - 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 self._config - except Exception as err: - raise Exception(f'Error retrieving configuration from CBS: {err}') - - -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/mod/pmsh_logging.py b/components/pm-subscription-handler/pmsh_service/mod/pmsh_logging.py index 885644b4..e0a7e1b1 100644 --- a/components/pm-subscription-handler/pmsh_service/mod/pmsh_logging.py +++ b/components/pm-subscription-handler/pmsh_service/mod/pmsh_logging.py @@ -17,6 +17,7 @@ # ============LICENSE_END===================================================== import datetime import logging as log +import sys from logging.handlers import RotatingFileHandler from os import makedirs @@ -41,7 +42,9 @@ def _create_logger(name, logfile): formatter = log.Formatter("%(message)s") file_handler.setFormatter(formatter) logger.setLevel(log.DEBUG) + stdout_handler = log.StreamHandler(sys.stdout) logger.addHandler(file_handler) + logger.addHandler(stdout_handler) return logger @@ -107,7 +110,7 @@ def debug(msg="n/a"): """ ets = utc() - _DEBUG_LOGGER.info( + _DEBUG_LOGGER.debug( "{ets}|{msg}".format( ets=ets.isoformat(), msg=msg, diff --git a/components/pm-subscription-handler/pmsh_service/mod/pmsh_utils.py b/components/pm-subscription-handler/pmsh_service/mod/pmsh_utils.py index 750b7211..8db3c1f8 100755 --- a/components/pm-subscription-handler/pmsh_service/mod/pmsh_utils.py +++ b/components/pm-subscription-handler/pmsh_service/mod/pmsh_utils.py @@ -20,11 +20,36 @@ import uuid from threading import Timer import requests +from onap_dcae_cbs_docker_client.client import get_all from requests.auth import HTTPBasicAuth +from tenacity import wait_fixed, stop_after_attempt, retry, retry_if_exception_type import mod.pmsh_logging as logger +class ConfigHandler: + """ Handles retrieval of PMSH's configuration from Configbinding service.""" + @staticmethod + @retry(wait=wait_fixed(2), stop=stop_after_attempt(5), retry=retry_if_exception_type(Exception)) + def get_pmsh_config(): + """ Retrieves PMSH's configuration from Config binding 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 Config binding service. + """ + try: + config = get_all() + logger.debug(f'PMSH config from CBS: {config}') + return config + except Exception as err: + logger.debug(f'Failed to get config from CBS: {err}') + raise Exception + + class AppConfig: def __init__(self, **kwargs): self.aaf_creds = {'aaf_id': kwargs.get('aaf_identity'), diff --git a/components/pm-subscription-handler/pmsh_service/mod/subscription_handler.py b/components/pm-subscription-handler/pmsh_service/mod/subscription_handler.py index 40b8c962..4d4c5311 100644 --- a/components/pm-subscription-handler/pmsh_service/mod/subscription_handler.py +++ b/components/pm-subscription-handler/pmsh_service/mod/subscription_handler.py @@ -18,15 +18,14 @@ import mod.aai_client as aai import mod.pmsh_logging as logger +from mod.pmsh_utils import ConfigHandler from mod.subscription import AdministrativeState class SubscriptionHandler: - def __init__(self, config_handler, administrative_state, mr_pub, app, app_conf, - aai_event_thread): + def __init__(self, administrative_state, mr_pub, app, app_conf, aai_event_thread): self.current_nfs = None self.current_sub = None - self.config_handler = config_handler self.administrative_state = administrative_state self.mr_pub = mr_pub self.app = app @@ -39,7 +38,7 @@ class SubscriptionHandler: the Subscription if a change has occurred """ self.app.app_context().push() - config = self.config_handler.get_config() + config = ConfigHandler.get_pmsh_config() new_administrative_state = config['policy']['subscription']['administrativeState'] try: -- cgit 1.2.3-korg