1 # ============LICENSE_START===================================================
2 # Copyright (C) 2020 Nordix Foundation.
3 # ============================================================================
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
16 # SPDX-License-Identifier: Apache-2.0
17 # ============LICENSE_END=====================================================
19 import mod.aai_client as aai
20 from mod import logger
21 from mod.subscription import AdministrativeState
24 class SubscriptionHandler:
25 def __init__(self, mr_pub, app, app_conf, aai_event_thread, policy_event_thread):
28 self.app_conf = app_conf
29 self.aai_event_thread = aai_event_thread
30 self.policy_event_thread = policy_event_thread
34 Checks for changes of administrative state in config and proceeds to process
35 the Subscription if a change has occurred
37 self.app.app_context().push()
38 local_admin_state = self.app_conf.subscription.get_local_sub_admin_state()
39 new_administrative_state = self.app_conf.subscription.administrativeState
41 if local_admin_state == new_administrative_state:
42 logger.info('Administrative State did not change in the Config')
44 if new_administrative_state == AdministrativeState.UNLOCKED.value:
45 self._activate(local_admin_state, new_administrative_state)
46 elif local_admin_state == AdministrativeState.PENDING.value:
47 logger.info('Administrative State is PENDING')
49 self._deactivate(local_admin_state, new_administrative_state)
50 except Exception as err:
51 logger.error(f'Error occurred during the activation/deactivation process {err}',
54 def _activate(self, local_admin_state, new_administrative_state):
55 logger.info(f'Administrative State has changed from {local_admin_state} '
56 f'to {new_administrative_state}.')
57 nfs_in_aai = aai.get_pmsh_nfs_from_aai(self.app_conf)
58 self.app_conf.subscription.activate_subscription(nfs_in_aai, self.mr_pub,
60 self.app_conf.subscription.update_subscription_status()
61 logger.info('Start listening for new NFs on AAI-EVENT topic in MR.')
62 self.aai_event_thread.start()
63 self.policy_event_thread.start()
65 def _deactivate(self, local_admin_state, new_administrative_state):
66 logger.info(f'Administrative State has changed from {local_admin_state} '
67 f'to {new_administrative_state}.')
68 self.aai_event_thread.cancel()
69 logger.info('Stop listening for NFs on AAI-EVENT topic in MR.')
70 self.app_conf.subscription.deactivate_subscription(self.mr_pub, self.app_conf)
71 self.app_conf.subscription.update_subscription_status()