diff options
Diffstat (limited to 'components/pm-subscription-handler/pmsh_service/mod/subscription_handler.py')
-rw-r--r-- | components/pm-subscription-handler/pmsh_service/mod/subscription_handler.py | 63 |
1 files changed, 55 insertions, 8 deletions
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 6238a298..ffe8ef50 100644 --- a/components/pm-subscription-handler/pmsh_service/mod/subscription_handler.py +++ b/components/pm-subscription-handler/pmsh_service/mod/subscription_handler.py @@ -1,5 +1,5 @@ # ============LICENSE_START=================================================== -# Copyright (C) 2020-2021 Nordix Foundation. +# Copyright (C) 2019-2021 Nordix Foundation. # ============================================================================ # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -18,10 +18,11 @@ from jsonschema import ValidationError from mod import logger, aai_client +from mod.aai_client import _filter_nf_data, get_pmsh_nfs_from_aai from mod.aai_event_handler import process_aai_events from mod.network_function import NetworkFunctionFilter from mod.pmsh_utils import PeriodicTask -from mod.subscription import AdministrativeState +from mod.subscription import AdministrativeState, Subscription, get_nfs_for_creation_and_deletion class SubscriptionHandler: @@ -45,12 +46,8 @@ class SubscriptionHandler: else: self.app_conf.refresh_config() self.app_conf.validate_sub_schema() - new_administrative_state = self.app_conf.subscription.administrativeState - if local_admin_state == new_administrative_state: - logger.info(f'Administrative State did not change in the app config: ' - f'{new_administrative_state}') - else: - self._check_state_change(local_admin_state, new_administrative_state) + local_admin_state = self.apply_subscription_changes() + self.compare_admin_state(local_admin_state) except (ValidationError, TypeError) as err: logger.error(f'Error occurred during validation of subscription schema {err}', exc_info=True) @@ -58,6 +55,56 @@ class SubscriptionHandler: logger.error(f'Error occurred during the activation/deactivation process {err}', exc_info=True) + def apply_subscription_changes(self): + """ Applies changes to subscription + + Args: + local_admin_state(enum): The local adminstrative state + + Returns: + Enum: Updated administrative state + """ + local_admin_state = self.app_conf.subscription.get_local_sub_admin_state() + if local_admin_state == AdministrativeState.FILTERING.value: + existing_nfs = self.app_conf.subscription.get_network_functions() + self.app_conf.nf_filter = \ + NetworkFunctionFilter(**self.app_conf.subscription.nfFilter) + new_nfs = get_pmsh_nfs_from_aai(self.app_conf) + self.app_conf.subscription.update_subscription_filter() + get_nfs_for_creation_and_deletion(existing_nfs, new_nfs, 'delete', + self.mr_pub, self.app_conf) + get_nfs_for_creation_and_deletion(existing_nfs, new_nfs, 'create', + self.mr_pub, self.app_conf) + + return local_admin_state + + def compare_admin_state(self, local_admin_state): + """ Check for changes in administrative state + + Args: + local_admin_state(enum): + + """ + new_administrative_state = self.app_conf.subscription.administrativeState + if local_admin_state == new_administrative_state: + logger.info(f'Administrative State did not change in the app config: ' + f'{new_administrative_state}') + else: + self._check_state_change(local_admin_state, new_administrative_state) + + def fetch_aai_nf_data(self): + """ Fetchs AAI data + + Returns: + dict: the json response from AAI query after filter is applied + """ + aai_nf_data = aai_client._get_all_aai_nf_data(self.app_conf) + if aai_nf_data: + new_nfs = _filter_nf_data(aai_nf_data, self.app_conf) + else: + raise RuntimeError('Failed to get data from AAI') + return new_nfs + def _check_state_change(self, local_admin_state, new_administrative_state): if new_administrative_state == AdministrativeState.UNLOCKED.value: logger.info(f'Administrative State has changed from {local_admin_state} ' |