From eb0c6ac326a34de3ce9d09abad9c6cf3d0572833 Mon Sep 17 00:00:00 2001 From: laili Date: Fri, 14 Sep 2018 18:27:52 +0800 Subject: [PATCH] Trigger subscription in nslcm. Change-Id: Ibd5b64f61e130b0ef39817eaeecb4c2bf368c489 Issue-ID: VFC-1126 Signed-off-by: laili --- lcm/ns_vnfs/biz/create_vnfs.py | 9 +++ lcm/ns_vnfs/biz/subscribe.py | 91 +++++++++++++++++++++++++ lcm/ns_vnfs/serializers/grant_vnf_serializer.py | 4 +- lcm/ns_vnfs/tests/const.py | 4 +- lcm/ns_vnfs/views/vnf_views.py | 3 +- 5 files changed, 106 insertions(+), 5 deletions(-) create mode 100644 lcm/ns_vnfs/biz/subscribe.py diff --git a/lcm/ns_vnfs/biz/create_vnfs.py b/lcm/ns_vnfs/biz/create_vnfs.py index 9952fd6e..ec3cc312 100644 --- a/lcm/ns_vnfs/biz/create_vnfs.py +++ b/lcm/ns_vnfs/biz/create_vnfs.py @@ -34,6 +34,7 @@ from lcm.pub.utils import restcall from lcm.ns_vnfs.const import VNF_STATUS, NFVO_VNF_INST_TIMEOUT_SECOND, INST_TYPE, INST_TYPE_NAME from lcm.ns_vnfs.biz.wait_job import wait_job_finish from lcm.pub.config.config import REG_TO_MSB_REG_PARAM, OOF_BASE_URL, OOF_PASSWD, OOF_USER +from lcm.ns_vnfs.biz.subscribe import Subscription logger = logging.getLogger(__name__) @@ -81,6 +82,7 @@ class CreateVnfs(Thread): self.send_get_vnfm_request_to_extsys() self.send_create_vnf_request_to_resmgr() self.wait_vnfm_job_finish() + self.subscribe() self.write_vnf_creation_info() self.save_info_to_db() JobUtil.add_job_status(self.job_id, 100, 'vnf instantiation success', 0) @@ -328,6 +330,13 @@ class CreateVnfs(Thread): logger.error('VNF instantiation failed on VNFM side. ret=[%s]', ret) raise NSLCMException('VNF instantiation failed on VNFM side.') + def subscribe(self): + data = { + 'vnfInstanceId': self.vnfm_nf_inst_id, + 'vnfmId': self.vnfm_inst_id + } + Subscription(data).do_biz() + def write_vnf_creation_info(self): logger.debug("write_vnf_creation_info start") vm_inst_infos = VmInstModel.objects.filter(insttype=INST_TYPE.VNF, instid=self.nf_inst_id) diff --git a/lcm/ns_vnfs/biz/subscribe.py b/lcm/ns_vnfs/biz/subscribe.py new file mode 100644 index 00000000..71ef6ccd --- /dev/null +++ b/lcm/ns_vnfs/biz/subscribe.py @@ -0,0 +1,91 @@ +# Copyright 2018 ZTE Corporation. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import json +import logging + +from lcm.pub.exceptions import NSLCMException +from lcm.pub.msapi.extsys import get_vnfm_by_id +from lcm.pub.utils.restcall import req_by_msb + +logger = logging.getLogger(__name__) + + +class Subscription(object): + def __init__(self, data): + self.data = data + self.vnf_instance_id = self.data['vnfInstanceId'] + self.vnfm_id = self.data['vnfmId'] + self.subscription_request_data = {} + self.subscription_response_data = {} + pass + + def do_biz(self): + logger.debug('Start subscribing...') + self.prepare_subscription_request_data() + self.subscribe_lccn_notification() + self.save_subscription_response_data() + logger.debug('Subscribing has completed.') + + def prepare_lccn_subscription_request_data(self): + vnfm_info = get_vnfm_by_id(self.vnfm_id) + self.subscription_request_data = { + "filter": { + "notificationTypes": ["VnfLcmOperationOccurrenceNotification"], + "operationTypes": [ + "INSTANTIATE", + "SCALE", + "SCALE_TO_LEVEL", + "CHANGE_FLAVOUR", + "TERMINATE", + "HEAL", + "OPERATE", + "CHANGE_EXT_CONN", + "MODIFY_INFO" + ], + "operationStates": [ + "STARTING", + "PROCESSING", + "COMPLETED", + "FAILED_TEMP", + "FAILED", + "ROLLING_BACK", + "ROLLED_BACK" + ], + "vnfInstanceSubscriptionFilter": { + "vnfdIds": [], + "vnfInstanceIds": [self.vnf_instance_id], + "vnfInstanceNames": [], + "vnfProductsFromProviders": {} + } + }, + "callbackUri": "api/gvnfmdriver/v1/vnfs/lifecyclechangesnotification", # TODO: need reconfirming + "authentication": { + "authType": ["BASIC"], + "paramsBasic": { + "userName": vnfm_info['userName'], + "password": vnfm_info['password'] + } + } + } + + def subscribe_lccn_notification(self): + ret = req_by_msb('api/gvnfmdrvier/v1/%s/subscriptions' % self.vnfm_id, self.subscription_request_data) + if ret[0] != 0: + logger.error("Status code is %s, detail is %s.", ret[2], ret[1]) + raise NSLCMException("Failed to subscribe from vnfm(%s)." % self.vnfm_id) + self.subscription_response_data = json.JSONDecoder().decode(ret[1]) + + def save_subscription_response_data(self): + pass diff --git a/lcm/ns_vnfs/serializers/grant_vnf_serializer.py b/lcm/ns_vnfs/serializers/grant_vnf_serializer.py index 31ea9d73..b4d41023 100644 --- a/lcm/ns_vnfs/serializers/grant_vnf_serializer.py +++ b/lcm/ns_vnfs/serializers/grant_vnf_serializer.py @@ -1037,7 +1037,7 @@ class VnfIdentifierCreationNotificationSerializer(serializers.Serializer): allow_null=True, allow_blank=True ) - timeStamp = serializers.DateField( + timeStamp = serializers.DateTimeField( help_text="Date-time of the generation of the notification.", required=True, allow_null=False, @@ -1079,7 +1079,7 @@ class VnfIdentifierDeletionNotificationSerializer(serializers.Serializer): allow_null=True, allow_blank=True ) - timeStamp = serializers.DateField( + timeStamp = serializers.DateTimeField( help_text="Date-time of the generation of the notification.", required=True, allow_null=False, diff --git a/lcm/ns_vnfs/tests/const.py b/lcm/ns_vnfs/tests/const.py index 08c5bdfd..c639ea62 100644 --- a/lcm/ns_vnfs/tests/const.py +++ b/lcm/ns_vnfs/tests/const.py @@ -203,7 +203,7 @@ VNF_IDENTIFIER_CREATION_NOTIFICATION_DATA = { 'id': 'Identifier of this notification', 'notificationType': 'VnfIdentifierCreationNotification', 'subscriptionId': 'Identifier of the subscription', - 'timeStamp': '2018-9-11', + 'timeStamp': '2018-9-12T00:00:00', 'vnfInstanceId': '2', '_links': { 'vnfInstance': {'href': 'URI of the referenced resource'}, @@ -216,7 +216,7 @@ VNF_IDENTIFIER_DELETION_NOTIFICATION_DATA = { 'id': 'Identifier of this notification', 'notificationType': 'VnfIdentifierDeletionNotification', 'subscriptionId': 'Identifier of the subscription', - 'timeStamp': '2018-9-11', + 'timeStamp': '2018-9-12T00:00:00', 'vnfInstanceId': '2', '_links': { 'vnfInstance': {'href': 'URI of the referenced resource'}, diff --git a/lcm/ns_vnfs/views/vnf_views.py b/lcm/ns_vnfs/views/vnf_views.py index a672ed5f..31906a22 100644 --- a/lcm/ns_vnfs/views/vnf_views.py +++ b/lcm/ns_vnfs/views/vnf_views.py @@ -11,6 +11,7 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. + import logging import traceback @@ -20,9 +21,9 @@ from rest_framework.response import Response from rest_framework.views import APIView from lcm.ns_vnfs.biz.grant_vnf import GrantVnf -from lcm.ns_vnfs.biz.handle_notification import HandleVnfLcmOocNotification, HandleVnfIdentifierCreationNotification, HandleVnfIdentifierDeletionNotification from lcm.ns_vnfs.serializers.grant_vnf_serializer import GrantRequestSerializer from lcm.ns_vnfs.serializers.grant_vnf_serializer import GrantSerializer +from lcm.ns_vnfs.biz.handle_notification import HandleVnfLcmOocNotification, HandleVnfIdentifierCreationNotification, HandleVnfIdentifierDeletionNotification from lcm.ns_vnfs.serializers.grant_vnf_serializer import VnfLcmOperationOccurrenceNotificationSerializer, VnfIdentifierCreationNotificationSerializer, VnfIdentifierDeletionNotificationSerializer logger = logging.getLogger(__name__) -- 2.16.6