1 # Copyright 2016-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.
19 from threading import Thread
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.pub.config.config import CUST_NAME, CUST_LAT, CUST_LONG
39 from lcm.ns_vnfs.biz.subscribe import SubscriptionCreation
41 logger = logging.getLogger(__name__)
44 def prepare_create_params():
45 nf_inst_id = str(uuid.uuid4())
46 NfInstModel(nfinstid=nf_inst_id, status=VNF_STATUS.INSTANTIATING, create_time=now_time(),
47 lastuptime=now_time()).save()
48 job_id = JobUtil.create_job(INST_TYPE_NAME.VNF, JOB_TYPE.CREATE_VNF, nf_inst_id)
49 JobUtil.add_job_status(job_id, 0, 'create vnf record in database.', 0)
50 return nf_inst_id, job_id
53 class CreateVnfs(Thread):
54 def __init__(self, data, nf_inst_id, job_id):
55 super(CreateVnfs, self).__init__()
57 self.nf_inst_id = nf_inst_id
62 self.ns_inst_name = ''
65 self.vnf_inst_name = ''
66 self.vnfm_inst_id = ''
68 self.nf_package_info = ''
69 self.vnfm_nf_inst_id = ''
71 self.vnfm_inst_name = ''
77 self.check_nf_name_exist()
80 self.create_vnf_in_aai()
81 self.check_nf_package_valid()
82 self.send_nf_init_request_to_vnfm()
83 self.send_homing_request_to_OOF()
84 self.send_get_vnfm_request_to_extsys()
85 self.send_create_vnf_request_to_resmgr()
86 self.wait_vnfm_job_finish()
88 self.write_vnf_creation_info()
89 self.save_info_to_db()
90 JobUtil.add_job_status(self.job_id, 100, 'vnf instantiation success', 0)
91 except NSLCMException as e:
92 self.vnf_inst_failed_handle(e.message)
94 logger.error(traceback.format_exc())
95 self.vnf_inst_failed_handle('unexpected exception')
98 self.ns_inst_id = self.data['ns_instance_id']
99 vnf_index = int(float(self.data['vnf_index'])) - 1
100 additional_vnf_info = self.data['additional_param_for_vnf'][vnf_index]
101 self.vnf_id = ignore_case_get(additional_vnf_info, 'vnfProfileId')
102 additional_param = ignore_case_get(additional_vnf_info, 'additionalParam')
103 self.properties = ignore_case_get(additional_param, 'properties')
104 self.vnfm_inst_id = ignore_case_get(additional_param, 'vnfmInstanceId')
105 para = ignore_case_get(additional_param, 'inputs')
106 self.inputs = json.loads(para) if isinstance(para, (str, unicode)) else para
107 self.vim_id = ignore_case_get(additional_param, 'vimId')
108 self.vnfd_id = ignore_case_get(additional_param, 'vnfdId')
110 def check_nf_name_exist(self):
111 is_exist = NfInstModel.objects.filter(nf_name=self.vnf_inst_name).exists()
113 logger.error('The name of NF instance already exists.')
114 raise NSLCMException('The name of NF instance already exists.')
116 def get_vnfd_id(self):
118 logger.debug("need not get vnfd_id")
119 self.nsd_model = {'vnfs': [], 'vls': [], 'vnffgs': []}
120 self.vnf_inst_name = self.vnfd_id + str(uuid.uuid4())
121 self.vnf_inst_name = self.vnf_inst_name[:30]
123 ns_inst_info = NSInstModel.objects.get(id=self.ns_inst_id)
124 self.ns_inst_name = ns_inst_info.name
125 self.nsd_model = json.loads(ns_inst_info.nsd_model)
126 for vnf_info in self.nsd_model['vnfs']:
127 if self.vnf_id == vnf_info['vnf_id']:
128 self.vnfd_id = vnf_info['properties']['id']
129 if 'name' not in vnf_info['properties']:
130 # HW vnf instance name must start with alphabet
131 self.vnf_inst_name = 'vnf' + self.vnfd_id[:10] + str(uuid.uuid4())
133 self.vnf_inst_name = vnf_info['properties']['name'] + str(uuid.uuid4())
134 self.vnf_inst_name = self.vnf_inst_name[:30]
135 self.vnf_inst_name = self.vnf_inst_name.replace("-", "_")
137 logger.error('Can not found vnf in nsd model')
138 raise NSLCMException('Can not found vnf in nsd model')
140 def check_nf_package_valid(self):
141 nfpackage_info = query_vnfpackage_by_id(self.vnfd_id)
142 self.nf_package_info = nfpackage_info["packageInfo"]
143 self.vnfd_model = ignore_case_get(self.nf_package_info, "vnfdModel")
144 self.vnfd_model = json.loads(self.vnfd_model)
146 def get_virtual_link_info(self, vnf_id):
147 virtual_link_list, ext_virtual_link = [], []
148 for vnf_info in self.nsd_model['vnfs']:
149 if vnf_info['vnf_id'] != vnf_id:
151 for network_info in vnf_info['networks']:
152 vl_instance = VLInstModel.objects.get(
153 vldid=network_info['vl_id'],
154 ownertype=OWNER_TYPE.NS,
155 ownerid=self.ns_inst_id)
156 vl_instance_id = vl_instance.vlinstanceid
157 network_name, subnet_name = self.get_network_info_of_vl(network_info['vl_id'])
158 virtual_link_list.append({
159 'network_name': network_name,
160 'key_name': network_info['key_name'],
161 'subnetwork_name': subnet_name,
162 'vl_instance_id': vl_instance_id
164 vim_id = json.JSONDecoder().decode(vl_instance.vimid) if isinstance(vl_instance.vimid, (str, unicode)) \
165 else vl_instance.vimid
166 ext_virtual_link.append({
167 "vlInstanceId": vl_instance_id,
168 "resourceId": vl_instance.relatednetworkid,
169 "resourceSubnetId": vl_instance.relatedsubnetworkid,
170 "cpdId": self.get_cpd_id_of_vl(network_info['key_name']),
175 "id": vl_instance_id,
176 "vimConnectionId": vl_instance.vimid,
177 "extCps": self.get_cpds_of_vl(network_info['key_name'])
179 return virtual_link_list, ext_virtual_link
181 def get_cpds_of_vl(self, vl_key):
183 logger.debug("vl_keya; %s" % vl_key)
184 for cpd in self.vnfd_model["vnf_exposed"]["external_cps"]:
185 logger.debug("exposed_cpd; %s" % cpd)
186 if vl_key == cpd["key_name"]:
187 cp = {"cpdId": cpd["cpd_id"], "cpConfig": []}
191 def get_cpd_id_of_vl(self, vl_key):
192 for cpd in self.vnfd_model["vnf_exposed"]["external_cps"]:
193 if vl_key == cpd["key_name"]:
197 def get_network_info_of_vl(self, vl_id):
198 for vnf_info in self.nsd_model['vls']:
199 if vnf_info['vl_id'] == vl_id:
200 return vnf_info['properties']['vl_profile']['networkName'], vnf_info['properties']['vl_profile']['networkName'] # ['initiationParameters']['name']
203 def send_nf_init_request_to_vnfm(self):
204 virtual_link_list, ext_virtual_link = self.get_virtual_link_info(self.vnf_id)
205 req_param = json.JSONEncoder().encode({
206 'vnfInstanceName': self.vnf_inst_name,
207 'vnfPackageId': ignore_case_get(self.nf_package_info, "vnfPackageId"),
208 'vnfDescriptorId': self.vnfd_id,
209 'flavourId': "default",
210 'extVirtualLink': ext_virtual_link,
212 "properties": self.properties,
213 "inputs": self.inputs,
214 "vimId": self.vim_id,
215 "extVirtualLinks": virtual_link_list
218 rsp = send_nf_init_request(self.vnfm_inst_id, req_param)
219 self.vnfm_job_id = ignore_case_get(rsp, 'jobId')
220 self.vnfm_nf_inst_id = ignore_case_get(rsp, 'vnfInstanceId')
222 NfInstModel.objects.filter(nfinstid=self.nf_inst_id).update(
223 mnfinstid=self.vnfm_nf_inst_id,
224 nf_name=self.vnf_inst_name,
226 package_id=ignore_case_get(self.nf_package_info, "vnfPackageId"),
227 vnfm_inst_id=self.vnfm_inst_id,
228 ns_inst_id=self.ns_inst_id,
229 version=ignore_case_get(self.nf_package_info, "vnfdVersion"),
230 vendor=ignore_case_get(self.nf_package_info, "vnfdProvider"),
231 vnfd_model=self.vnfd_model,
232 input_params=json.JSONEncoder().encode(self.inputs),
233 lastuptime=now_time())
235 def build_homing_request(self):
236 id = str(uuid.uuid4())
237 callback_uri = "http://{vfcBaseUrl}/api/nslcm/v1/ns/placevnf"
238 IP = REG_TO_MSB_REG_PARAM["nodes"][0]["ip"]
239 PORT = REG_TO_MSB_REG_PARAM["nodes"][0]["port"]
240 vfcBaseUrl = IP + ':' + PORT
241 callback_uri = callback_uri.format(vfcBaseUrl=vfcBaseUrl)
242 modelInvariantId = "no-resourceModelInvariantId"
243 modelVersionId = "no-resourceModelVersionId"
244 nsInfo = NSInstModel.objects.filter(id=self.ns_inst_id)
246 "resourceModuleName": self.vnf_id,
247 "serviceResourceId": self.vnfm_nf_inst_id,
248 "resourceModelInfo": {
249 "modelInvariantId": modelInvariantId,
250 "modelVersionId": modelVersionId
255 # vim_info = self.vim_id.split("_")
256 # identifiers = list()
257 # identifiers.append(vim_info[1])
258 # cloudOwner = vim_info[0]
260 identifiers.append(self.vim_id['cloud_regionid'])
261 cloudOwner = self.vim_id['cloud_owner']
262 required_candidate = [
264 "identifierType": "vimId",
265 "cloudOwner": cloudOwner,
266 "identifiers": identifiers
269 placementDemand["requiredCandidates"] = required_candidate
275 "callbackUrl": callback_uri,
277 "requestType": "create",
279 "optimizers": ["placement"],
283 "requestParameters": {
284 "customerLatitude": CUST_LAT,
285 "customerLongitude": CUST_LONG,
286 "customerName": CUST_NAME
289 "globalSubscriberId": "",
290 "subscriberName": "",
291 "subscriberCommonSiteId": "",
293 "placementDemands": []
296 "serviceInstanceId": self.ns_inst_id,
297 "serviceName": self.ns_inst_name,
299 "modelInvariantId": nsInfo[0].nsd_invariant_id,
300 "modelVersionId": nsInfo[0].nsd_id
304 req_body["placementInfo"]["placementDemands"].append(placementDemand)
305 # Stored the init request info inside DB
306 OOFDataModel.objects.create(
309 request_status="init",
310 request_module_name=self.vnf_id,
311 service_resource_id=self.vnfm_nf_inst_id,
319 def send_homing_request_to_OOF(self):
320 req_body = self.build_homing_request()
321 base_url = OOF_BASE_URL
322 resources = "/api/oof/v1/placement"
323 resp = restcall.call_req(base_url=base_url, user=OOF_USER, passwd=OOF_PASSWD,
324 auth_type=restcall.rest_oneway_auth, resource=resources,
325 method="POST", content=json.dumps(req_body), additional_headers="")
327 resp_status = resp[-1]
329 logger.debug("Got OOF sync response")
331 logger.warn("Missing OOF sync response")
332 logger.debug(("OOF sync response code is %s") % resp_status)
333 if str(resp_status) != '202' or resp[0] != 0:
334 OOFDataModel.objects.filter(request_id=req_body["requestInfo"]["requestId"],
335 transaction_id=req_body["requestInfo"]["transactionId"]).update(
336 request_status="failed",
339 cloud_region_id="none",
342 raise Exception("Received a Bad Sync from OOF with response code %s" % resp_status)
343 logger.info("Completed Homing request to OOF")
345 def send_get_vnfm_request_to_extsys(self):
346 resp_body = get_vnfm_by_id(self.vnfm_inst_id)
347 self.vnfm_inst_name = ignore_case_get(resp_body, 'name')
349 def send_create_vnf_request_to_resmgr(self):
350 pkg_vnfd = self.vnfd_model
352 'nf_inst_id': self.nf_inst_id,
353 'vnfm_nf_inst_id': self.vnfm_nf_inst_id,
354 'vnf_inst_name': self.vnf_inst_name,
355 'ns_inst_id': self.ns_inst_id,
356 'ns_inst_name': self.ns_inst_name,
357 'nf_inst_name': self.vnf_inst_name,
358 'vnfm_inst_id': self.vnfm_inst_id,
359 'vnfm_inst_name': self.vnfm_inst_name,
360 'vnfd_name': pkg_vnfd['metadata'].get('name', 'undefined'),
361 'vnfd_id': self.vnfd_id,
362 'job_id': self.job_id,
363 'nf_inst_status': VNF_STATUS.INSTANTIATING,
364 'vnf_type': pkg_vnfd['metadata'].get('vnf_type', 'undefined'),
365 'nf_package_id': ignore_case_get(self.nf_package_info, "vnfPackageId")
369 def wait_vnfm_job_finish(self):
370 ret = wait_job_finish(vnfm_id=self.vnfm_inst_id,
371 vnfo_job_id=self.job_id,
372 vnfm_job_id=self.vnfm_job_id,
373 progress_range=[10, 90],
374 timeout=NFVO_VNF_INST_TIMEOUT_SECOND)
376 if ret != JOB_MODEL_STATUS.FINISHED:
377 logger.error('VNF instantiation failed on VNFM side. ret=[%s]', ret)
378 raise NSLCMException('VNF instantiation failed on VNFM side.')
382 'vnfInstanceId': self.vnfm_nf_inst_id,
383 'vnfmId': self.vnfm_inst_id
385 SubscriptionCreation(data).do_biz()
387 def write_vnf_creation_info(self):
388 logger.debug("write_vnf_creation_info start")
389 vm_inst_infos = VmInstModel.objects.filter(insttype=INST_TYPE.VNF, instid=self.nf_inst_id)
391 'nf_inst_id': self.nf_inst_id,
392 'ns_inst_id': self.ns_inst_id,
393 'vnfm_inst_id': self.vnfm_inst_id,
394 'vms': [{'vmId': vm_inst_info.resouceid, 'vmName': vm_inst_info.vmname, 'vmStatus': 'ACTIVE'} for
395 vm_inst_info in vm_inst_infos]}
396 create_vnf_creation_info(data)
397 logger.debug("write_vnf_creation_info end")
399 def save_info_to_db(self):
400 logger.debug("save_info_to_db start")
401 do_biz_with_share_lock("set-vnflist-in-vnffginst-%s" % self.ns_inst_id, self.save_vnf_inst_id_in_vnffg)
402 NfInstModel.objects.filter(nfinstid=self.nf_inst_id).update(status=VNF_STATUS.ACTIVE, lastuptime=now_time())
403 logger.debug("save_info_to_db end")
405 def vnf_inst_failed_handle(self, error_msg):
406 logger.error('VNF instantiation failed, detail message: %s' % error_msg)
407 NfInstModel.objects.filter(nfinstid=self.nf_inst_id).update(status=VNF_STATUS.FAILED,
408 lastuptime=now_time())
409 JobUtil.add_job_status(self.job_id, 255, 'VNF instantiation failed, detail message: %s' % error_msg, 0)
411 def save_vnf_inst_id_in_vnffg(self):
412 vnffgs = self.nsd_model['vnffgs']
414 if self.vnf_id not in vnffg['members']:
416 vnffg_inst_infos = VNFFGInstModel.objects.filter(vnffgdid=vnffg['vnffg_Id'], nsinstid=self.ns_inst_id)
417 if not vnffg_inst_infos:
418 logger.error('Vnffg instance not exist.')
419 raise NSLCMException('Vnffg instance not exist.')
420 vnf_list = vnffg_inst_infos[0].vnflist
421 vnffg_inst_infos.update(vnf_list=vnf_list + ',' + self.nf_inst_id if vnf_list else self.nf_inst_id)
423 def create_vnf_in_aai(self):
424 logger.debug("CreateVnfs::create_vnf_in_aai::report vnf instance[%s] to aai." % self.nf_inst_id)
426 ns_insts = NSInstModel.objects.filter(id=self.ns_inst_id)
427 self.global_customer_id = ns_insts[0].global_customer_id
428 self.service_type = ns_insts[0].service_type
430 "vnf-id": self.nf_inst_id,
431 "vnf-name": self.vnf_inst_name,
432 "vnf-type": "vnf-type-test111",
433 "service-id": self.ns_inst_id,
435 "is-closed-loop-disabled": False,
436 "relationship-list": {
439 "related-to": "service-instance",
440 "relationship-data": [
442 "relationship-key": "customer.global-customer-id",
443 "relationship-value": self.global_customer_id
446 "relationship-key": "service-subscription.service-type",
447 "relationship-value": self.service_type
450 "relationship-key": "service-instance.service-instance-id",
451 "relationship-value": self.ns_inst_id
458 resp_data, resp_status = create_vnf_aai(self.nf_inst_id, data)
459 logger.debug("Success to create vnf[%s] to aai, ns instance=[%s], resp_status: [%s]."
460 % (self.nf_inst_id, self.ns_inst_id, resp_status))
461 except NSLCMException as e:
462 logger.debug("Fail to create vnf[%s] to aai, ns instance=[%s], detail message: %s"
463 % (self.nf_inst_id, self.ns_inst_id, e.message))
465 logger.error(traceback.format_exc())