diff options
author | ERIMROB <robertas.rimkus@est.tech> | 2020-02-27 10:05:37 +0000 |
---|---|---|
committer | ERIMROB <robertas.rimkus@est.tech> | 2020-03-23 14:17:18 +0000 |
commit | b074a929a43629a5d4ced09f1ebe4106241d776f (patch) | |
tree | ed2477888b4b5091736be7209b332ee2c43ab58b /components/pm-subscription-handler/pmsh_service/pmsh_service_main.py | |
parent | 06ab83c7455d6474548d63a146754748f830922c (diff) |
[PMSH] Refactor subscription processor and policy response handler
Signed-off-by: ERIMROB <robertas.rimkus@est.tech>
Change-Id: I91964848df8f7455169650b138b46d8dfc326b6f
Issue-ID: DCAEGEN2-1820
Diffstat (limited to 'components/pm-subscription-handler/pmsh_service/pmsh_service_main.py')
-rwxr-xr-x | components/pm-subscription-handler/pmsh_service/pmsh_service_main.py | 78 |
1 files changed, 19 insertions, 59 deletions
diff --git a/components/pm-subscription-handler/pmsh_service/pmsh_service_main.py b/components/pm-subscription-handler/pmsh_service/pmsh_service_main.py index 8245466b..af5aece2 100755 --- a/components/pm-subscription-handler/pmsh_service/pmsh_service_main.py +++ b/components/pm-subscription-handler/pmsh_service/pmsh_service_main.py @@ -15,61 +15,18 @@ # # SPDX-License-Identifier: Apache-2.0 # ============LICENSE_END===================================================== + import sys -import threading import mod.aai_client as aai import mod.pmsh_logging as logger from mod import db, create_app, launch_api_server -from mod.aai_event_handler import process_aai_events from mod.config_handler import ConfigHandler from mod.pmsh_utils import AppConfig, PeriodicTask +from mod.policy_response_handler import PolicyResponseHandler from mod.subscription import Subscription, AdministrativeState - - -def subscription_processor(config_handler, administrative_state, mr_pub, app, - mr_aai_event_subscriber): - """ - Checks for changes of administrative state in config and proceeds to process - the Subscription if a change has occurred - - Args: - config_handler (ConfigHandler): Configuration Handler used to get config - administrative_state (str): The administrative state - mr_pub (_MrPub): MR publisher - app (db): DB application - mr_aai_event_subscriber (_MrSub): AAI events MR subscriber - """ - app.app_context().push() - config = config_handler.get_config() - app_conf = AppConfig(**config['config']) - new_administrative_state = config['policy']['subscription']['administrativeState'] - polling_period = 30.0 - - try: - if administrative_state == new_administrative_state: - logger.debug('Administrative State did not change in the Config') - else: - logger.debug(f'Administrative State changed from "{administrative_state}" "to ' - f'"{new_administrative_state}".') - sub, nfs = aai.get_pmsh_subscription_data(config) - sub.process_subscription(nfs, mr_pub, app_conf) - aai_event_thread = PeriodicTask(10, process_aai_events, args=( - mr_aai_event_subscriber, sub, mr_pub, app, app_conf)) - - if new_administrative_state == AdministrativeState.UNLOCKED.value: - logger.debug('Listening to AAI-EVENT topic in MR.') - aai_event_thread.start() - else: - logger.debug('Stopping to listen to AAI-EVENT topic in MR.') - aai_event_thread.cancel() - - except Exception as err: - logger.debug(f'Error occurred during the activation/deactivation process {err}') - - threading.Timer(polling_period, subscription_processor, - [config_handler, new_administrative_state, mr_pub, app, - mr_aai_event_subscriber]).start() +from mod.subscription_handler import SubscriptionHandler +from mod.aai_event_handler import process_aai_events def main(): @@ -81,21 +38,24 @@ def main(): app.app_context().push() db.create_all(app=app) sub, nfs = aai.get_pmsh_subscription_data(config) - mr_pub = app_conf.get_mr_pub('policy_pm_publisher') - mr_sub = app_conf.get_mr_sub('policy_pm_subscriber') - mr_aai_event_subscriber = app_conf.get_mr_sub('aai_subscriber') - initial_start_delay = 5.0 - - administrative_state = AdministrativeState.LOCKED.value + policy_mr_pub = app_conf.get_mr_pub('policy_pm_publisher') + policy_mr_sub = app_conf.get_mr_sub('policy_pm_subscriber') + mr_aai_event_sub = app_conf.get_mr_sub('aai_subscriber') subscription_in_db = Subscription.get(sub.subscriptionName) - if subscription_in_db is not None: - administrative_state = subscription_in_db.status + administrative_state = subscription_in_db.status if subscription_in_db \ + else AdministrativeState.LOCKED.value + + aai_event_thread = PeriodicTask(10, process_aai_events, + args=(mr_aai_event_sub, sub, policy_mr_pub, app, app_conf)) + subscription_handler = SubscriptionHandler(config_handler, administrative_state, + policy_mr_pub, aai_event_thread, app, app_conf) + policy_response_handler = PolicyResponseHandler(policy_mr_sub, sub.subscriptionName, app) - threading.Timer(initial_start_delay, subscription_processor, - [config_handler, administrative_state, mr_pub, - app, mr_aai_event_subscriber]).start() + subscription_handler_thread = PeriodicTask(30, subscription_handler.execute) + policy_response_handler_thread = PeriodicTask(5, policy_response_handler.poll_policy_topic) - threading.Timer(20.0, mr_sub.poll_policy_topic, [sub.subscriptionName, app]).start() + subscription_handler_thread.start() + policy_response_handler_thread.start() launch_api_server(app_conf) |