1 # Copyright 2017 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.
17 from threading import Thread
19 from lcm.pub.database.models import NfInstModel, JobStatusModel, NfvoRegInfoModel, VmInstModel, VNFCInstModel, \
20 NetworkInstModel, SubNetworkInstModel, VLInstModel, PortInstModel, CPInstModel, StorageInstModel, FlavourInstModel
21 from lcm.pub.exceptions import NFLCMException
22 from lcm.pub.msapi.nfvolcm import vnfd_rawdata_get, apply_grant_to_nfvo, apply_res_to_nfvo
23 from lcm.pub.utils.jobutil import JobUtil
24 from lcm.pub.utils.timeutil import now_time
25 from lcm.pub.utils.values import ignore_case_get
26 from lcm.pub.vimapi import adaptor
28 logger = logging.getLogger(__name__)
31 class InstVnf(Thread):
32 def __init__(self, data, nf_inst_id, job_id):
33 super(InstVnf, self).__init__()
35 self.nf_inst_id = nf_inst_id
37 self.nfvo_inst_id = ''
38 self.vnfm_inst_id = ''
40 self.inst_resource = {'volumn':[],#'volumn':[{"vim_id": "1"}, {"res_id": "2"}]
47 self.create_res_result = {
49 'resourceResult': [{'name': 'vm01'}, {'name': 'vm02'}],
55 'computeresource':{'resourceid':'11'},
60 'affectedvirtuallink':[
63 'virtuallinkinstanceid':'',
64 'networkresource':{'resourceid':'1'},
65 'subnetworkresource':{'resourceid':'1'},
66 'virtuallinkdescid': '',
71 'portresource':{'resourceid':'1'},
76 'virtuallinkinstanceid':'22222',
88 self.check_res_status()
89 # self.wait_inst_finish(args)
90 # self.lcm_notify(args)
91 JobUtil.add_job_status(self.job_id, 100, "Instantiate Vnf success.")
92 is_exist = JobStatusModel.objects.filter(jobid=self.job_id).exists()
93 logger.debug("check_ns_inst_name_exist::is_exist=%s" % is_exist)
94 except NFLCMException as e:
95 self.vnf_inst_failed_handle(e.message)
96 # self.rollback(e.message)
98 # self.vnf_inst_failed_handle('unexpected exception')
99 logger.error(traceback.format_exc())
100 # self.rollback('unexpected exception')
103 vnf_insts = NfInstModel.objects.filter(nfinstid=self.nf_inst_id)
104 if not vnf_insts.exists():
105 raise NFLCMException('VNF nf_inst_id is not exist.')
107 self.vnfm_inst_id = vnf_insts[0].vnfm_inst_id
108 if vnf_insts[0].instantiationState != 'NOT_INSTANTIATED':
109 raise NFLCMException('VNF instantiationState is not NOT_INSTANTIATED.')
111 #get rawdata by vnfd_id
112 ret = vnfd_rawdata_get(vnf_insts[0].vnfdid)
114 raise NFLCMException("Get vnfd_raw_data failed.")
115 self.vnfd_info = json.JSONDecoder().decode(ret[1])
118 if cp not in self.vnfd_info:
119 raise NFLCMException('Input parameter is not defined in vnfd_info.')
121 JobUtil.add_job_status(self.job_id, 5, 'GET_NFVO_CONNECTION_INFO')
122 self.load_nfvo_config()
125 NfInstModel.objects.filter(nfinstid=self.nf_inst_id).update(flavour_id=ignore_case_get(self.data, "flavourId"),
126 vnf_level=ignore_case_get(self.data, 'instantiationLevelId'),
127 input_params=ignore_case_get(self.data, 'additionalParams'),
128 extension=ignore_case_get(self.data, ''),
129 initallocatedata=self.vnfd_info,
130 localizationLanguage=ignore_case_get(self.data, 'localizationLanguage'),
131 lastuptime=now_time())
132 JobUtil.add_job_status(self.job_id, 10, 'Nf instancing pre-check finish')
133 logger.info("Nf instancing pre-check finish")
135 def apply_grant(self):
136 logger.info('[NF instantiation] send resource grand request to nfvo start')
137 #self.check_vm_capacity()
138 content_args = {'nfvoInstanceId': self.nfvo_inst_id, 'vnfmInstanceId': self.vnfm_inst_id,
139 'nfInstanceId': self.nf_inst_id, 'nfDescriptorId': '',
140 'lifecycleOperation': 'Instantiate', 'jobId': self.job_id, 'addResource': [],
141 'removeResource': [], 'placementConstraint': [], 'exVimIdList': [], 'additionalParam': {}}
143 vdus = self.vnfd_info['vdus']
146 res_def = {'type': 'VDU', 'resourceDefinitionId': str(res_index), 'vduId': vdu['vdu_id'],
147 'vimid': '', 'tenant': ''}
148 if self.vnfd_info['metadata']['cross_dc']:
149 res_def['vimid'] = vdu['properties']['location_info']['vimId']
150 res_def['tenant'] = vdu['properties']['location_info']['tenant']
151 content_args['addResource'].append(res_def)
153 logger.info('content_args=%s' % content_args)
154 resp = apply_grant_to_nfvo(content_args)
155 logger.info("[NF instantiation] get grant response = %s" % resp)
157 raise NFLCMException('Nf instancing apply grant exception')
159 #update_resources_table()
160 NfInstModel.objects.filter(nfinstid=self.nf_inst_id).update(instantiationState='INSTANTIATED', lastuptime=now_time())
161 JobUtil.add_job_status(self.job_id, 20, 'Nf instancing apply grant finish')
162 logger.info("Nf instancing apply grant finish")
164 def create_res(self):
165 logger.info("[NF instantiation] create resource start")
166 volumns = ignore_case_get(self.data, "volumn_storages")
167 #create_vim_res(data, do_notify, do_rollback)
168 #create_volumns(volumns)
169 JobUtil.add_job_status(self.job_id, 35, 'Nf instancing create resource(volumn_storages) finish')
171 vls = ignore_case_get(self.data, "vls")
172 # create_networks(vls)
173 JobUtil.add_job_status(self.job_id, 55, 'Nf instancing create resource(networks) finish')
175 vdus = ignore_case_get(self.data, "vdus")
177 JobUtil.add_job_status(self.job_id, 75, 'Nf instancing create resource(vms) finish')
179 JobUtil.add_job_status(self.job_id, 20, 'Nf instancing apply grant finish')
180 logger.info("[NF instantiation] create resource end")
182 def check_res_status(self):
183 logger.info("[NF instantiation] confirm all vms are active start")
184 vnfcs = self.create_res_result['resource_result']['affectedvnfc']
186 if 'success' != vnfc['status']:
187 logger.error("VNFC_STATUS_IS_NOT_ACTIVE[vduid=%s]" % vnfc['vduId'])
188 raise NFLCMException(msgid="VNFC_STATUS_IS_NOT_ACTIVE[vduid=%s]", args=vnfc['vduId'])
190 JobUtil.add_job_status(self.job_id, 80, 'SAVE_VNFC_TO_DB')
191 vls = self.create_res_result['resource_result']['affectedvirtuallink']
192 cps = self.create_res_result['resource_result']['affectedcp']
195 if 'failed' == vnfc['status']:
197 compute_resource = vnfc['computeresource']
198 vminst = VmInstModel.objects.filter(resouceid=compute_resource['resourceid']).first()
199 VNFCInstModel.objects.create(
200 vnfcinstanceid=vnfc['vnfcinstanceid'],
202 vdutype=vnfc['vdutype'],
203 nfinstid=self.nf_inst_id,
206 if 'failed' == vl['status']:
208 network_resource = vl['networkresource']
209 subnet_resource = vl['subnetworkresource']
210 networkinst = NetworkInstModel.objects.filter(resouceid=network_resource['resourceid']).first()
211 subnetinst = SubNetworkInstModel.objects.filter(resouceid=subnet_resource['resourceid']).first()
212 VLInstModel.objects.create(
213 vlinstanceid=vl['virtuallinkinstanceid'],
214 vldid=vl['virtuallinkdescid'],
216 ownerid=self.nf_inst_id,
217 relatednetworkid=networkinst.networkid,
218 relatedsubnetworkid=subnetinst.subnetworkid)
221 if 'failed' == cp['status']:
223 port_resource = cp['portresource']
224 portinst = PortInstModel.objects.filter(resouceid=port_resource['resourceid']).first()
225 ttt = portinst.portid
226 CPInstModel.objects.create(
227 cpinstanceid=cp['cpinstanceid'],
230 relatedport=portinst.portid,
231 ownertype=cp['ownertype'],
232 ownerid=cp['ownerid'],
233 vlinstanceid=cp['virtuallinkinstanceid'])
234 # self.add_job(43, 'INST_DPLY_VM_PRGS')
235 logger.info("[NF instantiation] confirm all vms are active end")
237 def wait_inst_finish(self, args):
239 logger.info('wait_inst_finish, args=%s' % args)
240 # WaitInstFinishTask(args).do_biz()
241 return {'result': '100', 'msg': 'Nf instancing wait finish', 'context': {}}
242 except Exception as e:
243 logger.error('Nf instancing wait exception=%s' % e.message)
244 logger.error(traceback.format_exc())
245 return {'result': '255', 'msg': 'Nf instancing wait exception', 'context': {}}
247 def lcm_notify(self, args):
249 logger.info('lcm_notify, args=%s' % args)
250 # LcmNotifyTask(args).do_biz()
251 return {'result': '100', 'msg': 'Nf instancing lcm notify finish', 'context': {}}
252 except Exception as e:
253 logger.error('Nf instancing lcm notify exception=%s' % e.message)
254 logger.error(traceback.format_exc())
255 return {'result': '255', 'msg': 'Nf instancing lcm notify exception', 'context': {}}
257 def rollback(self, args):
259 logger.info('inst_exception, args=%s' % args)
260 # InstExceptionTask(args).do_biz()
261 return {'result': '100', 'msg': 'Nf instancing exception process finish', 'context': {}}
262 except Exception as e:
263 logger.error('Nf instancing exception process exception=%s' % e.message)
264 logger.error(traceback.format_exc())
265 return {'result': '255', 'msg': 'Nf instancing exception process exception', 'context': {}}
267 def load_nfvo_config(self):
268 logger.info("[NF instantiation]get nfvo connection info start")
269 reg_info = NfvoRegInfoModel.objects.filter(vnfminstid='vnfm111').first()
271 self.nfvo_inst_id = reg_info.nfvoid
272 logger.info("[NF instantiation] Registered nfvo id is [%s]"%self.nfvo_inst_id)
274 raise NFLCMException("Nfvo was not registered")
275 logger.info("[NF instantiation]get nfvo connection info end")
277 def vnf_inst_failed_handle(self, error_msg):
278 logger.error('VNF instantiation failed, detail message: %s' % error_msg)
279 NfInstModel.objects.filter(nfinstid=self.nf_inst_id).update(status='failed', lastuptime=now_time())
280 JobUtil.add_job_status(self.job_id, 255, error_msg)
281 # JobUtil.add_job_status(self.job_id, 255, 'VNF instantiation failed, detail message: %s' % error_msg, 0)
283 def do_notify(res_type, progress, ret):
284 # logger.info('add job, progress=%s, msgid=%s, args=%s' % (progress, msgid, args_))
285 progress = 20 + int(progress/2) #20-70
286 if res_type == adaptor.RES_VOLUME:
287 logger.info('Create vloumns!')
289 # if ret['rescode'] == 0:#new create
290 # self.inst_resource['volumn'].append({"vim_id": "1"}, {"res_id": "2"})
291 self.inst_resource['volumn'].append({"vim_id": "1"}, {"res_id": "2"})
292 JobUtil.add_job_status(self.job_id, progress, 'Create vloumns!')
293 StorageInstModel.objects.create(
300 instid=self.nf_inst_id)
301 elif res_type == adaptor.RES_NETWORK:
302 logger.info('Create networks!')
303 # self.inst_resource['network'] = ret
304 self.inst_resource['network'].append({"vim_id": "1"}, {"res_id": "2"})
305 JobUtil.add_job_status(self.job_id, progress, 'Create networks!')
306 NetworkInstModel.objects.create(
313 instid=self.nf_inst_id)
314 elif res_type == adaptor.RES_SUBNET:
315 logger.info('Create subnets!')
316 # self.inst_resource['subnet'] = ret
317 self.inst_resource['subnet'].append({"vim_id": "1"}, {"res_id": "2"})
318 JobUtil.add_job_status(self.job_id, progress, 'Create subnets!')
319 SubNetworkInstModel.objects.create(
327 instid=self.nf_inst_id)
328 elif res_type == adaptor.RES_PORT:
329 logger.info('Create ports!')
330 # self.inst_resource['port'] = ret
331 self.inst_resource['port'].append({"vim_id": "1"}, {"res_id": "2"})
332 JobUtil.add_job_status(self.job_id, progress, 'Create ports!')
333 PortInstModel.objects.create(
342 instid=self.nf_inst_id)
343 elif res_type == adaptor.RES_FLAVOR:
344 logger.info('Create flavors!')
345 # self.inst_resource['flavor'] = ret
346 self.inst_resource['flavor'].append({"vim_id": "1"}, {"res_id": "2"})
347 JobUtil.add_job_status(self.job_id, progress, 'Create flavors!')
348 FlavourInstModel.objects.create(
353 instid=self.nf_inst_id)
354 elif res_type == adaptor.RES_VM:
355 logger.info('Create vms!')
356 self.inst_resource['vm'].append({"vim_id": "1"}, {"res_id": "2"})
357 JobUtil.add_job_status(self.job_id, progress, 'Create vms!')
358 VmInstModel.objects.create(
363 instid=self.nf_inst_id,
366 def do_rollback(self, progress, msgid, args_=None):
367 # logger.info('add job, progress=%s, msgid=%s, args=%s' % (progress, msgid, args_))
369 # adaptor.delete_vim_res(self.inst_resource, self.do_notify_delete)
371 StorageInstModel.objects.filter(instid=self.nf_inst_id).delete()
372 NetworkInstModel.objects.filter(instid=self.nf_inst_id).delete()
373 SubNetworkInstModel.objects.filter(instid=self.nf_inst_id).delete()
374 PortInstModel.objects.filter(instid=self.nf_inst_id).delete()
375 FlavourInstModel.objects.filter(instid=self.nf_inst_id).delete()
376 VmInstModel.objects.filter(instid=self.nf_inst_id).delete()
377 JobUtil.add_job_status(self.job_id, 255, 'Create resource failed')
379 def do_notify_delete(ret):
380 logger.error('Delete [%s] resource'%ret)