[PMSH] Fix bug of deactivation during undeploy

Signed-off-by: ERIMROB <robertas.rimkus@est.tech>
Issue-ID: DCAEGEN2-2175
Change-Id: I70998a0d643b899c1b4d3ba86346297271a9b276
diff --git a/components/pm-subscription-handler/Changelog.md b/components/pm-subscription-handler/Changelog.md
index 0ae0197..3d2af6c 100755
--- a/components/pm-subscription-handler/Changelog.md
+++ b/components/pm-subscription-handler/Changelog.md
@@ -8,7 +8,8 @@
 ## [1.0.3]
 ### Fixed
 * Fixed bug where PMSH pushes subscription to xnf regardless of it's orchestration status (DCAEGEN2-2173)
-* Bug fix to prevent aai_event handler from incorrectly LOCKING the subscription (DCAEGEN2-2181)
+* Fixed bug where undeploying PMSH would not deactivate newly added pnfs (DCAEGEN2-2175)
+* Fixed bug to prevent aai_event handler from incorrectly LOCKING the subscription (DCAEGEN2-2181)
 
 ## [1.0.2]
 ### Changed
diff --git a/components/pm-subscription-handler/pmsh_service/mod/exit_handler.py b/components/pm-subscription-handler/pmsh_service/mod/exit_handler.py
index adc4941..3cb05da 100755
--- a/components/pm-subscription-handler/pmsh_service/mod/exit_handler.py
+++ b/components/pm-subscription-handler/pmsh_service/mod/exit_handler.py
@@ -42,7 +42,7 @@
                 logger.debug(f'Cancelling periodic task with thread name: {thread.name}.')
                 thread.cancel()
             current_sub.administrativeState = AdministrativeState.LOCKED.value
-            current_sub.process_subscription(self.subscription_handler.current_nfs,
+            current_sub.process_subscription(current_sub.get_network_functions(),
                                              self.subscription_handler.mr_pub,
                                              self.subscription_handler.app_conf)
         ExitHandler.shutdown_signal_received = True
diff --git a/components/pm-subscription-handler/pmsh_service/mod/subscription.py b/components/pm-subscription-handler/pmsh_service/mod/subscription.py
index 99a787d..3add720 100755
--- a/components/pm-subscription-handler/pmsh_service/mod/subscription.py
+++ b/components/pm-subscription-handler/pmsh_service/mod/subscription.py
@@ -22,7 +22,8 @@
 
 import mod.pmsh_logging as logger
 from mod import db
-from mod.db_models import SubscriptionModel, NfSubRelationalModel
+from mod.db_models import SubscriptionModel, NfSubRelationalModel, NetworkFunctionModel
+from mod.network_function import NetworkFunction
 
 
 class SubNfState(Enum):
@@ -218,3 +219,26 @@
             update({NfSubRelationalModel.nf_sub_status: status}, synchronize_session='evaluate')
 
         db.session.commit()
+
+    def _get_nf_models(self):
+        nf_sub_relationships = NfSubRelationalModel.query.filter(
+            NfSubRelationalModel.subscription_name == self.subscriptionName)
+        nf_models = []
+        for nf_sub_entry in nf_sub_relationships:
+            nf_model_object = NetworkFunctionModel.query.filter(
+                NetworkFunctionModel.nf_name == nf_sub_entry.nf_name).one_or_none()
+            nf_models.append(nf_model_object)
+
+        return nf_models
+
+    def get_network_functions(self):
+        nfs = []
+        nf_models = self._get_nf_models()
+        for nf_model in nf_models:
+            nf = NetworkFunction(
+                nf_name=nf_model.nf_name,
+                orchestration_status=nf_model.orchestration_status
+            )
+            nfs.append(nf)
+
+        return nfs
diff --git a/components/pm-subscription-handler/tests/test_subscription.py b/components/pm-subscription-handler/tests/test_subscription.py
index c95e2ab..e6ee2b5 100755
--- a/components/pm-subscription-handler/tests/test_subscription.py
+++ b/components/pm-subscription-handler/tests/test_subscription.py
@@ -26,6 +26,7 @@
 
 import mod.aai_client as aai_client
 from mod import db, create_app
+from mod.db_models import NetworkFunctionModel
 from mod.network_function import NetworkFunction, NetworkFunctionFilter, OrchestrationStatus
 from mod.pmsh_utils import AppConfig
 from mod.subscription import Subscription
@@ -201,3 +202,19 @@
         actual_sub_event = self.sub_1.prepare_subscription_event(self.nf_1.nf_name, app_conf)
         print(actual_sub_event)
         self.assertEqual(expected_sub_event, actual_sub_event)
+
+    def test_get_nf_models(self):
+        nf_array = [self.nf_1, self.nf_2]
+        self.sub_1.add_network_functions_to_subscription(nf_array)
+        nf_models = self.sub_1._get_nf_models()
+
+        self.assertEqual(2, len(nf_models))
+        self.assertIsInstance(nf_models[0], NetworkFunctionModel)
+
+    def test_get_network_functions(self):
+        nf_array = [self.nf_1, self.nf_2]
+        self.sub_1.add_network_functions_to_subscription(nf_array)
+        nfs = self.sub_1.get_network_functions()
+
+        self.assertEqual(2, len(nfs))
+        self.assertIsInstance(nfs[0], NetworkFunction)