summaryrefslogtreecommitdiffstats
path: root/components/pm-subscription-handler/pmsh_service/mod/subscription.py
diff options
context:
space:
mode:
Diffstat (limited to 'components/pm-subscription-handler/pmsh_service/mod/subscription.py')
-rwxr-xr-xcomponents/pm-subscription-handler/pmsh_service/mod/subscription.py77
1 files changed, 15 insertions, 62 deletions
diff --git a/components/pm-subscription-handler/pmsh_service/mod/subscription.py b/components/pm-subscription-handler/pmsh_service/mod/subscription.py
index 97bfc401..21e2399d 100755
--- a/components/pm-subscription-handler/pmsh_service/mod/subscription.py
+++ b/components/pm-subscription-handler/pmsh_service/mod/subscription.py
@@ -19,7 +19,6 @@ from enum import Enum
from mod import db, logger
from mod.api.db_models import SubscriptionModel, NfSubRelationalModel, NetworkFunctionModel
-from mod.network_function import NetworkFunction
class SubNfState(Enum):
@@ -94,30 +93,11 @@ class Subscription:
logger.error(f'Failed to update status of subscription: {self.subscriptionName}: {e}',
exc_info=True)
- def delete_subscription(self):
- """ Deletes a subscription and all its association from the database. A network function
- that is only associated with the subscription being removed will also be deleted."""
- try:
- subscription = SubscriptionModel.query.filter(
- SubscriptionModel.subscription_name == self.subscriptionName).one_or_none()
- if subscription:
- for nf_relationship in subscription.nfs:
- other_nf_relationship = NfSubRelationalModel.query.filter(
- NfSubRelationalModel.subscription_name != self.subscriptionName,
- NfSubRelationalModel.nf_name == nf_relationship.nf_name).one_or_none()
- if not other_nf_relationship:
- db.session.delete(nf_relationship.nf)
- db.session.delete(subscription)
- db.session.commit()
- except Exception as e:
- logger.error(f'Failed to delete subscription: {self.subscriptionName} '
- f'and it\'s relations from the DB: {e}', exc_info=True)
-
- def prepare_subscription_event(self, xnf_name, app_conf):
+ def prepare_subscription_event(self, nf, app_conf):
"""Prepare the sub event for publishing
Args:
- xnf_name: the AAI xnf name.
+ nf (NetworkFunction): the AAI nf.
app_conf (AppConfig): the application configuration.
Returns:
@@ -125,14 +105,16 @@ class Subscription:
"""
try:
clean_sub = {k: v for k, v in self.__dict__.items() if k != 'nfFilter'}
- sub_event = {'nfName': xnf_name, 'policyName': app_conf.operational_policy_name,
+ sub_event = {'nfName': nf.nf_name, 'blueprintName': nf.sdnc_model_name,
+ 'blueprintVersion': nf.sdnc_model_version,
+ 'policyName': app_conf.operational_policy_name,
'changeType': 'DELETE'
if self.administrativeState == AdministrativeState.LOCKED.value
else 'CREATE', 'closedLoopControlName': app_conf.control_loop_name,
'subscription': clean_sub}
return sub_event
except Exception as e:
- logger.error(f'Failed to prep Sub event for xNF {xnf_name}: {e}', exc_info=True)
+ logger.error(f'Failed to prep Sub event for xNF {nf.nf_name}: {e}', exc_info=True)
raise
def add_network_function_to_subscription(self, nf, sub_model):
@@ -159,7 +141,7 @@ class Subscription:
logger.error(f'Failed to add nf {nf.nf_name} to subscription '
f'{self.subscriptionName}: {e}', exc_info=True)
logger.debug(f'Subscription {self.subscriptionName} now contains these XNFs:'
- f'{Subscription.get_nf_names_per_sub(self.subscriptionName)}')
+ f'{[nf.nf_name for nf.nf_name in self.get_network_functions()]}')
def get(self):
""" Retrieves a SubscriptionModel object
@@ -189,32 +171,15 @@ class Subscription:
"""
return SubscriptionModel.query.all()
- @staticmethod
- def get_nf_names_per_sub(subscription_name):
- """ Retrieves a list of network function names related to the subscription
-
- Args:
- subscription_name (str): The subscription name
-
- Returns:
- list(str): List of network function names
- """
- nf_sub_rel = NfSubRelationalModel.query.filter(
- NfSubRelationalModel.subscription_name == subscription_name).all()
- list_of_nfs = []
- for nf in nf_sub_rel:
- list_of_nfs.append(nf.nf_name)
-
- return list_of_nfs
-
def activate_subscription(self, nfs, mr_pub, app_conf):
logger.info(f'Activate subscription initiated for {self.subscriptionName}.')
try:
+ existing_nfs = self.get_network_functions()
sub_model = self.get()
- for nf in nfs:
- mr_pub.publish_subscription_event_data(self, nf.nf_name, app_conf)
+ for nf in set(nfs + existing_nfs):
logger.info(f'Publishing event to activate '
f'Sub: {self.subscriptionName} for the nf: {nf.nf_name}')
+ mr_pub.publish_subscription_event_data(self, nf, app_conf)
self.add_network_function_to_subscription(nf, sub_model)
self.update_sub_nf_status(self.subscriptionName, SubNfState.PENDING_CREATE.value,
nf.nf_name)
@@ -222,12 +187,12 @@ class Subscription:
raise Exception(f'Error publishing activation event to MR: {err}')
def deactivate_subscription(self, mr_pub, app_conf):
- nfs = self.get_network_functions()
try:
+ nfs = self.get_network_functions()
if nfs:
logger.info(f'Deactivate subscription initiated for {self.subscriptionName}.')
for nf in nfs:
- mr_pub.publish_subscription_event_data(self, nf.nf_name, app_conf)
+ mr_pub.publish_subscription_event_data(self, nf, app_conf)
logger.debug(f'Publishing Event to deactivate '
f'Sub: {self.subscriptionName} for the nf: {nf.nf_name}')
self.update_sub_nf_status(self.subscriptionName,
@@ -265,25 +230,13 @@ class Subscription:
logger.error(f'Failed to update status of nf: {nf_name} for subscription: '
f'{subscription_name}: {e}', exc_info=True)
- def _get_nf_models(self):
+ def get_network_functions(self):
nf_sub_relationships = NfSubRelationalModel.query.filter(
NfSubRelationalModel.subscription_name == self.subscriptionName)
- nf_models = []
+ nfs = []
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)
+ nfs.append(nf_model_object.to_nf())
return nfs