Trigger subscription in nslcm.
[vfc/nfvo/lcm.git] / lcm / ns_vnfs / biz / subscribe.py
1 # Copyright 2018 ZTE Corporation.
2 #
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
6 #
7 #         http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 import json
16 import logging
17
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
21
22 logger = logging.getLogger(__name__)
23
24
25 class Subscription(object):
26     def __init__(self, data):
27         self.data = 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 = {}
32         pass
33
34     def do_biz(self):
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.')
40
41     def prepare_lccn_subscription_request_data(self):
42         vnfm_info = get_vnfm_by_id(self.vnfm_id)
43         self.subscription_request_data = {
44             "filter": {
45                 "notificationTypes": ["VnfLcmOperationOccurrenceNotification"],
46                 "operationTypes": [
47                     "INSTANTIATE",
48                     "SCALE",
49                     "SCALE_TO_LEVEL",
50                     "CHANGE_FLAVOUR",
51                     "TERMINATE",
52                     "HEAL",
53                     "OPERATE",
54                     "CHANGE_EXT_CONN",
55                     "MODIFY_INFO"
56                 ],
57                 "operationStates": [
58                     "STARTING",
59                     "PROCESSING",
60                     "COMPLETED",
61                     "FAILED_TEMP",
62                     "FAILED",
63                     "ROLLING_BACK",
64                     "ROLLED_BACK"
65                 ],
66                 "vnfInstanceSubscriptionFilter": {
67                     "vnfdIds": [],
68                     "vnfInstanceIds": [self.vnf_instance_id],
69                     "vnfInstanceNames": [],
70                     "vnfProductsFromProviders": {}
71                 }
72             },
73             "callbackUri": "api/gvnfmdriver/v1/vnfs/lifecyclechangesnotification",  # TODO: need reconfirming
74             "authentication": {
75                 "authType": ["BASIC"],
76                 "paramsBasic": {
77                     "userName": vnfm_info['userName'],
78                     "password": vnfm_info['password']
79                 }
80             }
81         }
82
83     def subscribe_lccn_notification(self):
84         ret = req_by_msb('api/gvnfmdrvier/v1/%s/subscriptions' % self.vnfm_id, self.subscription_request_data)
85         if ret[0] != 0:
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])
89
90     def save_subscription_response_data(self):
91         pass