1 # Copyright 2018 ZTE Corporation.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
18 from lcm.pub.exceptions import NSLCMException
19 from lcm.pub.msapi.extsys import get_vnfm_by_id
20 from lcm.pub.utils.restcall import req_by_msb
22 logger = logging.getLogger(__name__)
25 class Subscription(object):
26 def __init__(self, data):
28 self.vnf_instance_id = self.data['vnfInstanceId']
29 self.vnfm_id = self.data['vnfmId']
30 self.subscription_request_data = {}
31 self.subscription_response_data = {}
35 logger.debug('Start subscribing...')
36 self.prepare_subscription_request_data()
37 self.subscribe_lccn_notification()
38 self.save_subscription_response_data()
39 logger.debug('Subscribing has completed.')
41 def prepare_lccn_subscription_request_data(self):
42 vnfm_info = get_vnfm_by_id(self.vnfm_id)
43 self.subscription_request_data = {
45 "notificationTypes": ["VnfLcmOperationOccurrenceNotification"],
66 "vnfInstanceSubscriptionFilter": {
68 "vnfInstanceIds": [self.vnf_instance_id],
69 "vnfInstanceNames": [],
70 "vnfProductsFromProviders": {}
73 "callbackUri": "api/gvnfmdriver/v1/vnfs/lifecyclechangesnotification", # TODO: need reconfirming
75 "authType": ["BASIC"],
77 "userName": vnfm_info['userName'],
78 "password": vnfm_info['password']
83 def subscribe_lccn_notification(self):
84 ret = req_by_msb('api/gvnfmdrvier/v1/%s/subscriptions' % self.vnfm_id, self.subscription_request_data)
86 logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
87 raise NSLCMException("Failed to subscribe from vnfm(%s)." % self.vnfm_id)
88 self.subscription_response_data = json.JSONDecoder().decode(ret[1])
90 def save_subscription_response_data(self):