Merge "[Datalake] Admin UI release 1.1.0"
[dcaegen2/services.git] / components / pm-subscription-handler / pmsh_service / mod / subscription_handler.py
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
7 #
8 #      http://www.apache.org/licenses/LICENSE-2.0
9 #
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.
15 #
16 # SPDX-License-Identifier: Apache-2.0
17 # ============LICENSE_END=====================================================
18
19 import mod.aai_client as aai
20 from mod import logger
21 from mod.subscription import AdministrativeState
22
23
24 class SubscriptionHandler:
25     def __init__(self, mr_pub, app, app_conf, aai_event_thread, policy_event_thread):
26         self.mr_pub = mr_pub
27         self.app = app
28         self.app_conf = app_conf
29         self.aai_event_thread = aai_event_thread
30         self.policy_event_thread = policy_event_thread
31
32     def execute(self):
33         """
34         Checks for changes of administrative state in config and proceeds to process
35         the Subscription if a change has occurred
36         """
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
40         try:
41             if local_admin_state == new_administrative_state:
42                 logger.info('Administrative State did not change in the Config')
43             else:
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')
48                 else:
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}',
52                          exc_info=True)
53
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,
59                                                          self.app_conf)
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()
64
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()