summaryrefslogtreecommitdiffstats
path: root/catalog/packages/biz
diff options
context:
space:
mode:
authorhongyuzhao <zhao.hongyu@zte.com.cn>2019-12-17 16:37:25 +0800
committerhongyuzhao <zhao.hongyu@zte.com.cn>2020-01-04 15:35:30 +0800
commit2479a0739895b15a13a07aa8260598483249c621 (patch)
treead6028ebf9490fa63e19dc5f150d555c003517df /catalog/packages/biz
parent47aa1d9d81d42c23f128b5bc7fec52b3f404cc8b (diff)
Refactor the notification process code
Change-Id: Ie12527c77e5cacc55bdc3bc1505ec130e9d3c582 Issue-ID: MODELING-269 Signed-off-by: hongyuzhao <zhao.hongyu@zte.com.cn>
Diffstat (limited to 'catalog/packages/biz')
-rw-r--r--catalog/packages/biz/notificationsutil.py228
-rw-r--r--catalog/packages/biz/ns_descriptor.py24
-rw-r--r--catalog/packages/biz/pnf_descriptor.py15
-rw-r--r--catalog/packages/biz/vnf_package.py16
4 files changed, 161 insertions, 122 deletions
diff --git a/catalog/packages/biz/notificationsutil.py b/catalog/packages/biz/notificationsutil.py
index 8a653b4..8cdfd80 100644
--- a/catalog/packages/biz/notificationsutil.py
+++ b/catalog/packages/biz/notificationsutil.py
@@ -17,51 +17,50 @@ import uuid
import requests
from rest_framework import status
from catalog.packages import const
-from catalog.pub.database.models import VnfPkgSubscriptionModel, NsdmSubscriptionModel
-from catalog.pub.database.models import VnfPackageModel
+from catalog.pub.database.models import VnfPackageModel, VnfPkgSubscriptionModel, NsdmSubscriptionModel
import catalog.pub.utils.timeutil
from catalog.pub.utils.values import remove_none_key
from catalog.pub.config import config as pub_config
import traceback
from django.db.models import Q
+from catalog.packages.serializers.vnf_pkg_notifications import PkgChangeNotificationSerializer, \
+ PkgOnboardingNotificationSerializer
logger = logging.getLogger(__name__)
class NotificationsUtil(object):
- def __init__(self):
+ def __init__(self, notification_type):
+ self.notification_type = notification_type
+ self.notifyserializer = None
+
+ def prepare_notification(self, **kwargs):
pass
- def send_notification(self, notification, filters, isvnfpkg):
- subscriptions_filter = {v + "__contains": notification[k] for k, v in filters.items()}
+ def send_notification(self):
+ notification = self.prepare_notification()
+
+ subscriptions_filter = {v + "__contains": notification[k] for k, v in self.filters.items()}
subscriptions_filter = remove_none_key(subscriptions_filter)
logger.debug('send_notification subscriptions_filter = %s' % subscriptions_filter)
q1 = Q()
q1.connector = 'OR'
for k, v in subscriptions_filter.items():
q1.children.append((k, v))
- if isvnfpkg:
- subscriptions = VnfPkgSubscriptionModel.objects.filter(q1)
- subscription_root_uri = const.VNFPKG_SUBSCRIPTION_ROOT_URI
- else:
- subscriptions = NsdmSubscriptionModel.objects.filter(q1)
- subscription_root_uri = const.NSDM_SUBSCRIPTION_ROOT_URI
+ subscriptions = self.SubscriptionModel.objects.filter(q1)
if not subscriptions.exists():
logger.info("No subscriptions created for the filters %s" % notification)
return
logger.info("Start sending notifications")
for sub in subscriptions:
# set subscription id
- if isvnfpkg:
- notification["subscriptionId"] = sub.subscription_id
- else:
- notification["subscriptionId"] = sub.subscriptionid
+ notification["subscriptionId"] = sub.get_subscription_id()
notification['_links']['subscription'] = {
'href': 'http://%s:%s/%s%s' % (pub_config.MSB_SERVICE_IP,
pub_config.MSB_SERVICE_PORT,
- subscription_root_uri,
+ self.subscription_root_uri,
notification["subscriptionId"])
}
callbackuri = sub.callback_uri
@@ -70,6 +69,10 @@ class NotificationsUtil(object):
if auth_info["authType"] == const.OAUTH2_CLIENT_CREDENTIALS:
pass
"""
+ if self.notifyserializer:
+ serialized_data = self.notifyserializer(data=notification)
+ if not serialized_data.is_valid():
+ logger.error('Notification Data is invalid:%s.' % serialized_data.errors)
self.post_notification(callbackuri, notification)
def post_notification(self, callbackuri, notification):
@@ -96,76 +99,135 @@ class NotificationsUtil(object):
logger.error(traceback.format_exc())
-def prepare_vnfpkg_notification(vnf_pkg_id, notification_type, pkg_change_type, operational_state):
- logger.info('Start to prepare notification')
- vnf_pkg = VnfPackageModel.objects.filter(vnfPackageId=vnf_pkg_id)
- vnfd_id = None
- if vnf_pkg:
- vnfd_id = vnf_pkg[0].vnfdId
- notification_content = {
- 'id': str(uuid.uuid4()), # shall be the same if sent multiple times due to multiple subscriptions.
- 'notificationType': notification_type,
- # set 'subscriptionId' after filtering for subscribers
- 'timeStamp': catalog.pub.utils.timeutil.now_time(),
- 'vnfPkgId': vnf_pkg_id,
- 'vnfdId': vnfd_id,
- '_links': {
- 'vnfPackage': {
- 'href': 'http://%s:%s/%s/vnf_packages/%s' % (pub_config.MSB_SERVICE_IP,
- pub_config.MSB_SERVICE_PORT,
- const.PKG_URL_PREFIX,
- vnf_pkg_id)
+class PkgNotifications(NotificationsUtil):
+ def __init__(self, notification_type, vnf_pkg_id, change_type=None, operational_state=None):
+ super(PkgNotifications, self).__init__(notification_type)
+ self.filters = {
+ 'vnfdId': 'vnfd_id',
+ 'vnfPkgId': 'vnf_pkg_id'
+ }
+ self.vnf_pkg_id = vnf_pkg_id
+ self.change_type = change_type
+ self.operational_state = operational_state
+ self.SubscriptionModel = VnfPkgSubscriptionModel
+ self.subscription_root_uri = const.VNFPKG_SUBSCRIPTION_ROOT_URI
+ if self.notification_type == "VnfPackageChangeNotification":
+ self.notifyserializer = PkgChangeNotificationSerializer
+ else:
+ self.notifyserializer = PkgOnboardingNotificationSerializer
+
+ def prepare_notification(self):
+ logger.info('Start to prepare Pkgnotification')
+
+ vnf_pkg = VnfPackageModel.objects.filter(vnfPackageId=self.vnf_pkg_id)
+ vnfd_id = None
+ if vnf_pkg:
+ vnfd_id = vnf_pkg[0].vnfdId
+ notification_content = {
+ 'id': str(uuid.uuid4()), # shall be the same if sent multiple times due to multiple subscriptions.
+ 'notificationType': self.notification_type,
+ # set 'subscriptionId' after filtering for subscribers
+ 'timeStamp': catalog.pub.utils.timeutil.now_time(),
+ 'vnfPkgId': self.vnf_pkg_id,
+ 'vnfdId': vnfd_id,
+ '_links': {
+ 'vnfPackage': {
+ 'href': 'http://%s:%s/%s/vnf_packages/%s' % (pub_config.MSB_SERVICE_IP,
+ pub_config.MSB_SERVICE_PORT,
+ const.PKG_URL_PREFIX,
+ self.vnf_pkg_id)
+ }
}
}
- }
-
- if notification_type == "VnfPackageChangeNotification":
- notification_content['changeType'] = pkg_change_type
- notification_content['operationalState'] = operational_state
-
- return notification_content
-
-
-def prepare_nsd_notification(nsd_info_id, nsd_id, notification_type, failure_details=None, operational_state=None):
- logger.info('Start to prepare notification')
- notification_content = {
- 'id': str(uuid.uuid4()), # shall be the same if sent multiple times due to multiple subscriptions.
- 'notificationType': notification_type,
- # set 'subscriptionId' after filtering for subscribers
- 'timeStamp': catalog.pub.utils.timeutil.now_time(),
- 'nsdInfoId': nsd_info_id,
- 'nsdId': nsd_id,
- 'onboardingFailureDetails': failure_details,
- 'nsdOperationalState': operational_state,
- '_links': {
- 'nsdInfo': {
- 'href': 'http://%s:%s/%s/ns_descriptors/%s' % (pub_config.MSB_SERVICE_IP,
- pub_config.MSB_SERVICE_PORT,
- const.NSD_URL_PREFIX,
- nsd_info_id)
+
+ if self.notification_type == "VnfPackageChangeNotification":
+ notification_content['changeType'] = self.change_type
+ notification_content['operationalState'] = self.operational_state
+
+ return notification_content
+
+
+class NsdNotifications(NotificationsUtil):
+ def __init__(self, notification_type, nsd_info_id, nsd_id, failure_details=None, operational_state=None):
+ super(NsdNotifications, self).__init__(notification_type)
+ self.filters = {
+ 'nsdInfoId': 'nsdInfoId',
+ 'nsdId': 'nsdId',
+ }
+ self.SubscriptionModel = NsdmSubscriptionModel
+ self.subscription_root_uri = const.NSDM_SUBSCRIPTION_ROOT_URI
+ self.nsd_info_id = nsd_info_id
+ self.nsd_id = nsd_id
+ self.failure_details = failure_details
+ self.operational_state = operational_state
+ # todo:
+ # if self.notification_type == "VnfPackageChangeNotification":
+ # self.notifyserializer = PkgChangeNotificationSerializer
+ # else:
+ # self.notifyserializer = PkgOnboardingNotificationSerializer
+
+ def prepare_notification(self):
+ logger.info('Start to prepare Nsdnotification')
+
+ notification_content = {
+ 'id': str(uuid.uuid4()), # shall be the same if sent multiple times due to multiple subscriptions.
+ 'notificationType': self.notification_type,
+ # set 'subscriptionId' after filtering for subscribers
+ 'timeStamp': catalog.pub.utils.timeutil.now_time(),
+ 'nsdInfoId': self.nsd_info_id,
+ 'nsdId': self.nsd_id,
+ '_links': {
+ 'nsdInfo': {
+ 'href': 'http://%s:%s/%s/ns_descriptors/%s' % (pub_config.MSB_SERVICE_IP,
+ pub_config.MSB_SERVICE_PORT,
+ const.NSD_URL_PREFIX,
+ self.nsd_info_id)
+ }
}
}
- }
- return notification_content
-
-
-def prepare_pnfd_notification(pnfd_info_id, pnfd_id, notification_type, failure_details=None):
- logger.info('Start to prepare notification')
- notification_content = {
- 'id': str(uuid.uuid4()), # shall be the same if sent multiple times due to multiple subscriptions.
- 'notificationType': notification_type,
- # set 'subscriptionId' after filtering for subscribers
- 'timeStamp': catalog.pub.utils.timeutil.now_time(),
- 'pnfdInfoIds': pnfd_info_id,
- 'pnfdId': pnfd_id,
- 'onboardingFailureDetails': failure_details,
- '_links': {
- 'pnfdInfo': {
- 'href': 'http://%s:%s/%s/pnf_descriptors/%s' % (pub_config.MSB_SERVICE_IP,
- pub_config.MSB_SERVICE_PORT,
- const.NSD_URL_PREFIX,
- pnfd_info_id)
+ if self.notification_type == "NsdOnboardingFailureNotification":
+ notification_content['onboardingFailureDetails'] = self.failure_details
+ if self.notification_type == "NsdChangeNotification":
+ notification_content['nsdOperationalState'] = self.operational_state
+ return notification_content
+
+
+class PnfNotifications(NotificationsUtil):
+ def __init__(self, notification_type, pnfd_info_id, pnfd_id, failure_details=None):
+ super(PnfNotifications, self).__init__(notification_type)
+ self.filters = {
+ 'pnfdId': 'pnfdId',
+ 'pnfdInfoIds': 'pnfdInfoIds',
+ }
+ self.SubscriptionModel = NsdmSubscriptionModel
+ self.subscription_root_uri = const.NSDM_SUBSCRIPTION_ROOT_URI
+ self.pnfd_info_id = pnfd_info_id
+ self.pnfd_id = pnfd_id
+ self.failure_details = failure_details
+ # todo
+ # if self.notification_type == "VnfPackageChangeNotification":
+ # self.notifyserializer = PkgChangeNotificationSerializer
+ # else:
+ # self.notifyserializer = PkgOnboardingNotificationSerializer
+
+ def prepare_notification(self, *args, **kwargs):
+ logger.info('Start to prepare Pnfnotification')
+ notification_content = {
+ 'id': str(uuid.uuid4()), # shall be the same if sent multiple times due to multiple subscriptions.
+ 'notificationType': self.notification_type,
+ # set 'subscriptionId' after filtering for subscribers
+ 'timeStamp': catalog.pub.utils.timeutil.now_time(),
+ 'pnfdInfoIds': self.pnfd_info_id,
+ 'pnfdId': self.pnfd_id,
+ '_links': {
+ 'pnfdInfo': {
+ 'href': 'http://%s:%s/%s/pnf_descriptors/%s' % (pub_config.MSB_SERVICE_IP,
+ pub_config.MSB_SERVICE_PORT,
+ const.NSD_URL_PREFIX,
+ self.pnfd_info_id)
+ }
}
}
- }
- return notification_content
+ if self.notification_type == "PnfdOnboardingFailureNotification":
+ notification_content['onboardingFailureDetails'] = self.failure_details
+ return notification_content
diff --git a/catalog/packages/biz/ns_descriptor.py b/catalog/packages/biz/ns_descriptor.py
index ca1b684..8b337dc 100644
--- a/catalog/packages/biz/ns_descriptor.py
+++ b/catalog/packages/biz/ns_descriptor.py
@@ -24,7 +24,7 @@ from catalog.pub.database.models import NSPackageModel, PnfPackageModel, VnfPack
from catalog.pub.exceptions import CatalogException, ResourceNotFoundException
from catalog.pub.utils import fileutil, toscaparser
from catalog.pub.utils.values import ignore_case_get
-from catalog.packages.biz.notificationsutil import prepare_nsd_notification, NotificationsUtil
+from catalog.packages.biz.notificationsutil import NsdNotifications
from catalog.packages import const
logger = logging.getLogger(__name__)
@@ -141,8 +141,10 @@ class NsDescriptor(object):
raise CatalogException("nsd_id(%s) does not exist in metadata." % nsd_id)
other_nspkg = NSPackageModel.objects.filter(nsdId=nsd_id)
if other_nspkg and other_nspkg[0].nsPackageId != nsd_info_id:
- logger.warn("NSD(%s,%s) already exists.", nsd_id, other_nspkg[0].nsPackageId)
- send_notification(const.NSD_NOTIFICATION_TYPE.NSD_ONBOARDING_FAILURE, nsd_info_id, nsd_id)
+ failure_details = "NSD(%s,%s) already exists.", nsd_id, other_nspkg[0].nsPackageId
+ logger.warn(failure_details)
+ send_notification(const.NSD_NOTIFICATION_TYPE.NSD_ONBOARDING_FAILURE, nsd_info_id, nsd_id,
+ failure_details=failure_details)
raise CatalogException("NSD(%s) already exists." % nsd_id)
for vnf in nsd["vnfs"]:
@@ -244,15 +246,7 @@ class NsDescriptor(object):
def send_notification(type, nsd_info_id, nsd_id=None, failure_details=None, operational_state=None):
- data = prepare_nsd_notification(nsd_info_id=nsd_info_id,
- nsd_id=nsd_id,
- notification_type=type,
- failure_details=failure_details,
- operational_state=operational_state)
- filters = {
- 'nsdInfoId': 'nsdInfoId',
- 'nsdId': 'nsdId',
- }
- logger.debug('Notify request data = %s' % data)
- logger.debug('Notify request filters = %s' % filters)
- NotificationsUtil().send_notification(data, filters, False)
+ notify = NsdNotifications(type, nsd_info_id, nsd_id,
+ failure_details=failure_details,
+ operational_state=operational_state)
+ notify.send_notification()
diff --git a/catalog/packages/biz/pnf_descriptor.py b/catalog/packages/biz/pnf_descriptor.py
index 6f45729..dd6a236 100644
--- a/catalog/packages/biz/pnf_descriptor.py
+++ b/catalog/packages/biz/pnf_descriptor.py
@@ -25,7 +25,7 @@ from catalog.pub.database.models import NSPackageModel, PnfPackageModel
from catalog.pub.exceptions import CatalogException, ResourceNotFoundException
from catalog.pub.utils import fileutil, toscaparser
from catalog.pub.utils.values import ignore_case_get
-from catalog.packages.biz.notificationsutil import prepare_pnfd_notification, NotificationsUtil
+from catalog.packages.biz.notificationsutil import PnfNotifications
from catalog.packages import const
logger = logging.getLogger(__name__)
@@ -237,14 +237,5 @@ class PnfDescriptor(object):
def send_notification(type, pnfd_info_id, pnfd_id=None, failure_details=None):
- data = prepare_pnfd_notification(pnfd_info_id=pnfd_info_id,
- pnfd_id=pnfd_id,
- notification_type=type,
- failure_details=failure_details)
- filters = {
- 'pnfdId': 'pnfdId',
- 'pnfdInfoIds': 'pnfdInfoIds',
- }
- logger.debug('Notify request data = %s' % data)
- logger.debug('Notify request filters = %s' % filters)
- NotificationsUtil().send_notification(data, filters, False)
+ notify = PnfNotifications(type, pnfd_info_id, pnfd_id, failure_details=failure_details)
+ notify.send_notification()
diff --git a/catalog/packages/biz/vnf_package.py b/catalog/packages/biz/vnf_package.py
index be73595..daf2fb2 100644
--- a/catalog/packages/biz/vnf_package.py
+++ b/catalog/packages/biz/vnf_package.py
@@ -24,7 +24,7 @@ import zipfile
from catalog.packages import const
from catalog.packages.biz.common import parse_file_range, read, save
-from catalog.packages.biz.notificationsutil import prepare_vnfpkg_notification, NotificationsUtil
+from catalog.packages.biz.notificationsutil import PkgNotifications
from catalog.pub.config.config import CATALOG_ROOT_PATH
from catalog.pub.database.models import VnfPackageModel, NSPackageModel
from catalog.pub.exceptions import CatalogException, ResourceNotFoundException
@@ -293,14 +293,6 @@ def handle_upload_failed(vnf_pkg_id):
def send_notification(pkg_id, type, pkg_change_type, operational_state=None):
- data = prepare_vnfpkg_notification(vnf_pkg_id=pkg_id,
- notification_type=type,
- pkg_change_type=pkg_change_type,
- operational_state=operational_state)
- filters = {
- 'vnfdId': 'vnfd_id',
- 'vnfPkgId': 'vnf_pkg_id'
- }
- logger.debug('Notify request data = %s' % data)
- logger.debug('Notify request filters = %s' % filters)
- NotificationsUtil().send_notification(data, filters, True)
+ notify = PkgNotifications(type, pkg_id, change_type=pkg_change_type,
+ operational_state=operational_state)
+ notify.send_notification()