Merge "fix NSlcm grant parameter error"
[vfc/nfvo/lcm.git] / lcm / ns_vnfs / biz / create_vnfs.py
1 # Copyright 2016-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 import traceback
18 import uuid
19 from threading import Thread
20
21 from lcm.ns.const import OWNER_TYPE
22 from lcm.pub.config.config import REPORT_TO_AAI
23 from lcm.pub.database.models import NfInstModel, NSInstModel, VmInstModel, VNFFGInstModel, VLInstModel, OOFDataModel
24 from lcm.pub.exceptions import NSLCMException
25 from lcm.pub.msapi.aai import create_vnf_aai
26 from lcm.pub.msapi.extsys import get_vnfm_by_id
27 from lcm.pub.msapi.resmgr import create_vnf, create_vnf_creation_info
28 from lcm.pub.msapi.sdc_run_catalog import query_vnfpackage_by_id
29 from lcm.pub.msapi.vnfmdriver import send_nf_init_request
30 from lcm.pub.utils.jobutil import JOB_MODEL_STATUS, JobUtil, JOB_TYPE
31 from lcm.pub.utils.share_lock import do_biz_with_share_lock
32 from lcm.pub.utils.timeutil import now_time
33 from lcm.pub.utils.values import ignore_case_get
34 from lcm.pub.utils import restcall
35 from lcm.ns_vnfs.const import VNF_STATUS, NFVO_VNF_INST_TIMEOUT_SECOND, INST_TYPE, INST_TYPE_NAME
36 from lcm.ns_vnfs.biz.wait_job import wait_job_finish
37 from lcm.pub.config.config import REG_TO_MSB_REG_PARAM, OOF_BASE_URL, OOF_PASSWD, OOF_USER
38 from lcm.ns_vnfs.biz.subscribe import SubscriptionCreation
39
40 logger = logging.getLogger(__name__)
41
42
43 def prepare_create_params():
44     nf_inst_id = str(uuid.uuid4())
45     NfInstModel(nfinstid=nf_inst_id, status=VNF_STATUS.INSTANTIATING, create_time=now_time(),
46                 lastuptime=now_time()).save()
47     job_id = JobUtil.create_job(INST_TYPE_NAME.VNF, JOB_TYPE.CREATE_VNF, nf_inst_id)
48     JobUtil.add_job_status(job_id, 0, 'create vnf record in database.', 0)
49     return nf_inst_id, job_id
50
51
52 class CreateVnfs(Thread):
53     def __init__(self, data, nf_inst_id, job_id):
54         super(CreateVnfs, self).__init__()
55         self.data = data
56         self.nf_inst_id = nf_inst_id
57         self.job_id = job_id
58         self.ns_inst_id = ''
59         self.vnf_id = ''
60         self.vnfd_id = ''
61         self.ns_inst_name = ''
62         self.nsd_model = ''
63         self.vnfd_model = ''
64         self.vnf_inst_name = ''
65         self.vnfm_inst_id = ''
66         self.inputs = ''
67         self.nf_package_info = ''
68         self.vnfm_nf_inst_id = ''
69         self.vnfm_job_id = ''
70         self.vnfm_inst_name = ''
71         self.vim_id = ''
72
73     def run(self):
74         try:
75             self.get_params()
76             self.check_nf_name_exist()
77             self.get_vnfd_id()
78             if REPORT_TO_AAI:
79                 self.create_vnf_in_aai()
80             self.check_nf_package_valid()
81             self.send_nf_init_request_to_vnfm()
82             self.send_homing_request_to_OOF()
83             self.send_get_vnfm_request_to_extsys()
84             self.send_create_vnf_request_to_resmgr()
85             self.wait_vnfm_job_finish()
86             self.subscribe()
87             self.write_vnf_creation_info()
88             self.save_info_to_db()
89             JobUtil.add_job_status(self.job_id, 100, 'vnf instantiation success', 0)
90         except NSLCMException as e:
91             self.vnf_inst_failed_handle(e.message)
92         except Exception:
93             logger.error(traceback.format_exc())
94             self.vnf_inst_failed_handle('unexpected exception')
95
96     def get_params(self):
97         self.ns_inst_id = self.data['ns_instance_id']
98         vnf_index = int(float(self.data['vnf_index'])) - 1
99         additional_vnf_info = self.data['additional_param_for_vnf'][vnf_index]
100         self.vnf_id = ignore_case_get(additional_vnf_info, 'vnfProfileId')
101         additional_param = ignore_case_get(additional_vnf_info, 'additionalParam')
102         self.properties = ignore_case_get(additional_param, 'properties')
103         self.vnfm_inst_id = ignore_case_get(additional_param, 'vnfmInstanceId')
104         para = ignore_case_get(additional_param, 'inputs')
105         self.inputs = json.loads(para) if isinstance(para, (str, unicode)) else para
106         self.vim_id = ignore_case_get(additional_param, 'vimId')
107         self.vnfd_id = ignore_case_get(additional_param, 'vnfdId')
108
109     def check_nf_name_exist(self):
110         is_exist = NfInstModel.objects.filter(nf_name=self.vnf_inst_name).exists()
111         if is_exist:
112             logger.error('The name of NF instance already exists.')
113             raise NSLCMException('The name of NF instance already exists.')
114
115     def get_vnfd_id(self):
116         if self.vnfd_id:
117             logger.debug("need not get vnfd_id")
118             self.nsd_model = {'vnfs': [], 'vls': [], 'vnffgs': []}
119             self.vnf_inst_name = self.vnfd_id + str(uuid.uuid4())
120             self.vnf_inst_name = self.vnf_inst_name[:30]
121             return
122         ns_inst_info = NSInstModel.objects.get(id=self.ns_inst_id)
123         self.ns_inst_name = ns_inst_info.name
124         self.nsd_model = json.loads(ns_inst_info.nsd_model)
125         for vnf_info in self.nsd_model['vnfs']:
126             if self.vnf_id == vnf_info['vnf_id']:
127                 self.vnfd_id = vnf_info['properties']['id']
128                 if 'name' not in vnf_info['properties']:
129                     # HW vnf instance name must start with alphabet
130                     self.vnf_inst_name = 'vnf' + self.vnfd_id[:10] + str(uuid.uuid4())
131                 else:
132                     self.vnf_inst_name = vnf_info['properties']['name'] + str(uuid.uuid4())
133                 self.vnf_inst_name = self.vnf_inst_name[:30]
134                 self.vnf_inst_name = self.vnf_inst_name.replace("-", "_")
135                 return
136         logger.error('Can not found vnf in nsd model')
137         raise NSLCMException('Can not found vnf in nsd model')
138
139     def check_nf_package_valid(self):
140         nfpackage_info = query_vnfpackage_by_id(self.vnfd_id)
141         self.nf_package_info = nfpackage_info["packageInfo"]
142         self.vnfd_model = ignore_case_get(self.nf_package_info, "vnfdModel")
143         self.vnfd_model = json.loads(self.vnfd_model)
144
145     def get_virtual_link_info(self, vnf_id):
146         virtual_link_list, ext_virtual_link = [], []
147         for vnf_info in self.nsd_model['vnfs']:
148             if vnf_info['vnf_id'] != vnf_id:
149                 continue
150             for network_info in vnf_info['networks']:
151                 vl_instance = VLInstModel.objects.get(
152                     vldid=network_info['vl_id'],
153                     ownertype=OWNER_TYPE.NS,
154                     ownerid=self.ns_inst_id)
155                 vl_instance_id = vl_instance.vlinstanceid
156                 network_name, subnet_name = self.get_network_info_of_vl(network_info['vl_id'])
157                 virtual_link_list.append({
158                     'network_name': network_name,
159                     'key_name': network_info['key_name'],
160                     'subnetwork_name': subnet_name,
161                     'vl_instance_id': vl_instance_id
162                 })
163                 ext_virtual_link.append({
164                     "vlInstanceId": vl_instance_id,
165                     "resourceId": vl_instance.relatednetworkid,
166                     "resourceSubnetId": vl_instance.relatedsubnetworkid,
167                     "cpdId": self.get_cpd_id_of_vl(network_info['key_name']),
168                     "vim": {
169                         "vimid": vl_instance.vimid
170                     },
171                     # SOL 003 align
172                     "id": vl_instance_id,
173                     "vimConnectionId": vl_instance.vimid,
174                     "extCps": [{"cpdId": self.get_cpd_id_of_vl(network_info['key_name']), "cpConfig":[]}]
175                 })
176         return virtual_link_list, ext_virtual_link
177
178     def get_cpd_id_of_vl(self, vl_key):
179         for cpd in self.vnfd_model["vnf_exposed"]["external_cps"]:
180             if vl_key == cpd["key_name"]:
181                 return cpd["cpd_id"]
182         return ""
183
184     def get_network_info_of_vl(self, vl_id):
185         for vnf_info in self.nsd_model['vls']:
186             if vnf_info['vl_id'] == vl_id:
187                 return vnf_info['properties']['vl_profile']['networkName'], vnf_info['properties']['vl_profile']['networkName']  # ['initiationParameters']['name']
188         return '', ''
189
190     def send_nf_init_request_to_vnfm(self):
191         virtual_link_list, ext_virtual_link = self.get_virtual_link_info(self.vnf_id)
192         req_param = json.JSONEncoder().encode({
193             'vnfInstanceName': self.vnf_inst_name,
194             'vnfPackageId': ignore_case_get(self.nf_package_info, "vnfPackageId"),
195             'vnfDescriptorId': self.vnfd_id,
196             'flavourId': "default",
197             'extVirtualLink': ext_virtual_link,
198             'additionalParam': {
199                 "properties": self.properties,
200                 "inputs": self.inputs,
201                 "vimId": self.vim_id,
202                 "extVirtualLinks": virtual_link_list
203             }
204         })
205         rsp = send_nf_init_request(self.vnfm_inst_id, req_param)
206         self.vnfm_job_id = ignore_case_get(rsp, 'jobId')
207         self.vnfm_nf_inst_id = ignore_case_get(rsp, 'vnfInstanceId')
208
209         NfInstModel.objects.filter(nfinstid=self.nf_inst_id).update(
210             mnfinstid=self.vnfm_nf_inst_id,
211             nf_name=self.vnf_inst_name,
212             vnf_id=self.vnf_id,
213             package_id=ignore_case_get(self.nf_package_info, "vnfPackageId"),
214             vnfm_inst_id=self.vnfm_inst_id,
215             ns_inst_id=self.ns_inst_id,
216             version=ignore_case_get(self.nf_package_info, "vnfdVersion"),
217             vendor=ignore_case_get(self.nf_package_info, "vnfdProvider"),
218             vnfd_model=self.vnfd_model,
219             input_params=json.JSONEncoder().encode(self.inputs),
220             lastuptime=now_time())
221
222     def build_homing_request(self):
223         id = str(uuid.uuid4())
224         callback_uri = " {vfcBaseUrl}/api/nslcm/v1/ns/placevnf"
225         IP = REG_TO_MSB_REG_PARAM["nodes"][0]["ip"]
226         PORT = REG_TO_MSB_REG_PARAM["nodes"][0]["port"]
227         vfcBaseUrl = IP + ':' + PORT
228         callback_uri = callback_uri.format(vfcBaseUrl=vfcBaseUrl)
229         modelInvariantId = "no-resourceModelInvariantId"
230         modelVersionId = "no-resourceModelVersionId"
231         nsInfo = NSInstModel.objects.filter(id=self.ns_inst_id)
232         placementDemand = {
233             "resourceModuleName": self.vnf_inst_name,
234             "serviceResourceId": self.vnfm_nf_inst_id,
235             "resourceModelInfo": {
236                 "modelInvariantId": modelInvariantId,
237                 "modelVersionId": modelVersionId
238             }
239         }
240         req_body = {
241             "requestInfo": {
242                 "transactionId": id,
243                 "requestId": id,
244                 "callbackUrl": callback_uri,
245                 "sourceId": "vfc",
246                 "requestType": "create",
247                 "numSolutions": 1,
248                 "optimizers": ["placement"],
249                 "timeout": 600
250             },
251             "placementInfo": {
252                 "placementDemands": []
253             },
254             "serviceInfo": {
255                 "serviceInstanceId": self.ns_inst_id,
256                 "serviceName": self.ns_inst_name,
257                 "modelInfo": {
258                     "modelInvariantId": nsInfo[0].nsd_invariant_id,
259                     "modelVersionId": nsInfo[0].nsd_id
260                 }
261             }
262         }
263         req_body["placementInfo"]["placementDemands"].append(placementDemand)
264         # Stored the init request info inside DB
265         OOFDataModel.objects.create(
266             request_id=id,
267             transaction_id=id,
268             request_status="init",
269             request_module_name=self.vnfm_nf_inst_id,
270             service_resource_id=self.vnf_inst_name,
271             vim_id="",
272             cloud_owner="",
273             cloud_region_id="",
274             vdu_info="",
275         )
276         return req_body
277
278     def send_homing_request_to_OOF(self):
279         req_body = self.build_homing_request()
280         base_url = OOF_BASE_URL
281         resources = "/api/oof/v1/placement"
282         resp = restcall.call_req(base_url=base_url, user=OOF_USER, passwd=OOF_PASSWD,
283                                  auth_type=restcall.rest_oneway_auth, resource=resources,
284                                  method="POST", content=req_body, additional_headers="")
285         resp_body = resp[-2]
286         resp_status = resp[-1]
287         if resp_body:
288             logger.debug("Got OOF sync response")
289         else:
290             logger.warn("Missing OOF sync response")
291         logger.debug(("OOF sync response code is %s") % resp_status)
292         if str(resp_status) != '202' or resp[0] != 0:
293             OOFDataModel.objects.filter(request_id=req_body["requestInfo"]["requestId"],
294                                         transaction_id=req_body["requestInfo"]["transactionId"]).update(
295                 request_status="failed",
296                 vim_id="none",
297                 cloud_owner="none",
298                 cloud_region_id="none",
299                 vdu_info="none"
300             )
301             raise Exception("Received a Bad Sync from OOF with response code %s" % resp_status)
302         logger.info("Completed Homing request to OOF")
303
304     def send_get_vnfm_request_to_extsys(self):
305         resp_body = get_vnfm_by_id(self.vnfm_inst_id)
306         self.vnfm_inst_name = ignore_case_get(resp_body, 'name')
307
308     def send_create_vnf_request_to_resmgr(self):
309         pkg_vnfd = self.vnfd_model
310         data = {
311             'nf_inst_id': self.nf_inst_id,
312             'vnfm_nf_inst_id': self.vnfm_nf_inst_id,
313             'vnf_inst_name': self.vnf_inst_name,
314             'ns_inst_id': self.ns_inst_id,
315             'ns_inst_name': self.ns_inst_name,
316             'nf_inst_name': self.vnf_inst_name,
317             'vnfm_inst_id': self.vnfm_inst_id,
318             'vnfm_inst_name': self.vnfm_inst_name,
319             'vnfd_name': pkg_vnfd['metadata'].get('name', 'undefined'),
320             'vnfd_id': self.vnfd_id,
321             'job_id': self.job_id,
322             'nf_inst_status': VNF_STATUS.INSTANTIATING,
323             'vnf_type': pkg_vnfd['metadata'].get('vnf_type', 'undefined'),
324             'nf_package_id': ignore_case_get(self.nf_package_info, "vnfPackageId")
325         }
326         create_vnf(data)
327
328     def wait_vnfm_job_finish(self):
329         ret = wait_job_finish(vnfm_id=self.vnfm_inst_id,
330                               vnfo_job_id=self.job_id,
331                               vnfm_job_id=self.vnfm_job_id,
332                               progress_range=[10, 90],
333                               timeout=NFVO_VNF_INST_TIMEOUT_SECOND)
334
335         if ret != JOB_MODEL_STATUS.FINISHED:
336             logger.error('VNF instantiation failed on VNFM side. ret=[%s]', ret)
337             raise NSLCMException('VNF instantiation failed on VNFM side.')
338
339     def subscribe(self):
340         data = {
341             'vnfInstanceId': self.vnfm_nf_inst_id,
342             'vnfmId': self.vnfm_inst_id
343         }
344         SubscriptionCreation(data).do_biz()
345
346     def write_vnf_creation_info(self):
347         logger.debug("write_vnf_creation_info start")
348         vm_inst_infos = VmInstModel.objects.filter(insttype=INST_TYPE.VNF, instid=self.nf_inst_id)
349         data = {
350             'nf_inst_id': self.nf_inst_id,
351             'ns_inst_id': self.ns_inst_id,
352             'vnfm_inst_id': self.vnfm_inst_id,
353             'vms': [{'vmId': vm_inst_info.resouceid, 'vmName': vm_inst_info.vmname, 'vmStatus': 'ACTIVE'} for
354                     vm_inst_info in vm_inst_infos]}
355         create_vnf_creation_info(data)
356         logger.debug("write_vnf_creation_info end")
357
358     def save_info_to_db(self):
359         logger.debug("save_info_to_db start")
360         do_biz_with_share_lock("set-vnflist-in-vnffginst-%s" % self.ns_inst_id, self.save_vnf_inst_id_in_vnffg)
361         NfInstModel.objects.filter(nfinstid=self.nf_inst_id).update(status=VNF_STATUS.ACTIVE, lastuptime=now_time())
362         logger.debug("save_info_to_db end")
363
364     def vnf_inst_failed_handle(self, error_msg):
365         logger.error('VNF instantiation failed, detail message: %s' % error_msg)
366         NfInstModel.objects.filter(nfinstid=self.nf_inst_id).update(status=VNF_STATUS.FAILED,
367                                                                     lastuptime=now_time())
368         JobUtil.add_job_status(self.job_id, 255, 'VNF instantiation failed, detail message: %s' % error_msg, 0)
369
370     def save_vnf_inst_id_in_vnffg(self):
371         vnffgs = self.nsd_model['vnffgs']
372         for vnffg in vnffgs:
373             if self.vnf_id not in vnffg['members']:
374                 continue
375             vnffg_inst_infos = VNFFGInstModel.objects.filter(vnffgdid=vnffg['vnffg_Id'], nsinstid=self.ns_inst_id)
376             if not vnffg_inst_infos:
377                 logger.error('Vnffg instance not exist.')
378                 raise NSLCMException('Vnffg instance not exist.')
379             vnf_list = vnffg_inst_infos[0].vnflist
380             vnffg_inst_infos.update(vnf_list=vnf_list + ',' + self.nf_inst_id if vnf_list else self.nf_inst_id)
381
382     def create_vnf_in_aai(self):
383         logger.debug("CreateVnfs::create_vnf_in_aai::report vnf instance[%s] to aai." % self.nf_inst_id)
384         try:
385             ns_insts = NSInstModel.objects.filter(id=self.ns_inst_id)
386             self.global_customer_id = ns_insts[0].global_customer_id
387             self.service_type = ns_insts[0].service_type
388             data = {
389                 "vnf-id": self.nf_inst_id,
390                 "vnf-name": self.vnf_inst_name,
391                 "vnf-type": "vnf-type-test111",
392                 "service-id": self.ns_inst_id,
393                 "in-maint": True,
394                 "is-closed-loop-disabled": False,
395                 "relationship-list": {
396                     "relationship": [
397                         {
398                             "related-to": "service-instance",
399                             "relationship-data": [
400                                 {
401                                     "relationship-key": "customer.global-customer-id",
402                                     "relationship-value": self.global_customer_id
403                                 },
404                                 {
405                                     "relationship-key": "service-subscription.service-type",
406                                     "relationship-value": self.service_type
407                                 },
408                                 {
409                                     "relationship-key": "service-instance.service-instance-id",
410                                     "relationship-value": self.ns_inst_id
411                                 }
412                             ]
413                         }
414                     ]
415                 }
416             }
417             resp_data, resp_status = create_vnf_aai(self.nf_inst_id, data)
418             logger.debug("Success to create vnf[%s] to aai, ns instance=[%s], resp_status: [%s]."
419                          % (self.nf_inst_id, self.ns_inst_id, resp_status))
420         except NSLCMException as e:
421             logger.debug("Fail to create vnf[%s] to aai, ns instance=[%s], detail message: %s"
422                          % (self.nf_inst_id, self.ns_inst_id, e.message))
423         except:
424             logger.error(traceback.format_exc())