call vim adapter create res
[vfc/gvnfm/vnflcm.git] / lcm / lcm / nf / vnfs / vnf_create / inst_vnf.py
1 # Copyright 2017 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 import json
15 import logging
16 import traceback
17 from threading import Thread
18
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
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
27
28 logger = logging.getLogger(__name__)
29
30
31 class InstVnf(Thread):
32     def __init__(self, data, nf_inst_id, job_id):
33         super(InstVnf, self).__init__()
34         self.data = data
35         self.nf_inst_id = nf_inst_id
36         self.job_id = job_id
37         self.nfvo_inst_id = ''
38         self.vnfm_inst_id = ''
39         self.vnfd_info = []
40         self.inst_resource = {'volumn':[],#'volumn':[{"vim_id": "1"}, {"res_id": "2"}]
41                               'network':[],
42                               'subnet':[],
43                               'port':[],
44                               'flavor':[],
45                               'vm':[],
46                               }
47         self.create_res_result = {
48             'jobid': 'res_001',
49             'resourceResult': [{'name': 'vm01'}, {'name': 'vm02'}],
50             'resource_result':{
51                 'affectedvnfc':[
52                     {
53                         'status':'success',
54                         'vnfcinstanceid':'1',
55                         'computeresource':{'resourceid':'11'},
56                         'vduid':'111',
57                         'vdutype':'1111'
58                     }
59                 ],
60                 'affectedvirtuallink':[
61                     {
62                         'status': 'success',
63                         'virtuallinkinstanceid':'',
64                         'networkresource':{'resourceid':'1'},
65                         'subnetworkresource':{'resourceid':'1'},
66                         'virtuallinkdescid': '',
67                     }
68                 ],
69                 'affectedcp':[{
70                     'status': 'success',
71                     'portresource':{'resourceid':'1'},
72                     'cpinstanceid':'2',
73                     'cpdid':'22',
74                     'ownertype':'222',
75                     'ownerid':'2222',
76                     'virtuallinkinstanceid':'22222',
77
78                 }],
79
80             }
81         }
82
83     def run(self):
84         try:
85             self.inst_pre()
86             self.apply_grant()
87             self.create_res()
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)
97         except:
98             self.vnf_inst_failed_handle('unexpected exception')
99             logger.error(traceback.format_exc())
100             # self.rollback('unexpected exception')
101
102     def inst_pre(self):
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.')
106
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.')
110
111         #get rawdata by vnfd_id
112         ret = vnfd_rawdata_get(vnf_insts[0].vnfdid)
113         if ret[0] != 0:
114             raise NFLCMException("Get vnfd_raw_data failed.")
115         self.vnfd_info = json.JSONDecoder().decode(ret[1])
116         #checkParameterExist
117         for cp in self.data:
118             if cp not in self.vnfd_info:
119                 raise NFLCMException('Input parameter is not defined in vnfd_info.')
120         #get nfvo info
121         JobUtil.add_job_status(self.job_id, 5, 'GET_NFVO_CONNECTION_INFO')
122         self.load_nfvo_config()
123
124         #update NfInstModel
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")
134
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': {}}
142
143         vdus = self.vnfd_info['vdus']
144         res_index = 1
145         for vdu in 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)
152             res_index += 1
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)
156         if resp[0] != 0:
157             raise NFLCMException('Nf instancing apply grant exception')
158
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")
163
164     def create_res(self):
165         logger.info("[NF instantiation] create resource start")
166
167         adaptor.create_vim_res(self.vnfd_info, self.do_notify, self.do_rollback)
168
169         # volumns = ignore_case_get(self.data, "volumn_storages")
170         #create_volumns(volumns)
171         # JobUtil.add_job_status(self.job_id, 35, 'Nf instancing create resource(volumn_storages) finish')
172         #
173         # vls = ignore_case_get(self.data, "vls")
174         # # create_networks(vls)
175         # JobUtil.add_job_status(self.job_id, 55, 'Nf instancing create resource(networks) finish')
176         #
177         # vdus = ignore_case_get(self.data, "vdus")
178         # # create_vdus(vdus)
179         # JobUtil.add_job_status(self.job_id, 75, 'Nf instancing create resource(vms) finish')
180
181         JobUtil.add_job_status(self.job_id, 70, '[NF instantiation] create resource finish')
182         logger.info("[NF instantiation] create resource finish")
183
184     def check_res_status(self):
185         logger.info("[NF instantiation] confirm all vms are active start")
186         vnfcs = self.create_res_result['resource_result']['affectedvnfc']
187         for vnfc in vnfcs:
188             if 'success' != vnfc['status']:
189                 logger.error("VNFC_STATUS_IS_NOT_ACTIVE[vduid=%s]" % vnfc['vduId'])
190                 raise NFLCMException(msgid="VNFC_STATUS_IS_NOT_ACTIVE[vduid=%s]", args=vnfc['vduId'])
191
192         JobUtil.add_job_status(self.job_id, 80, 'SAVE_VNFC_TO_DB')
193         vls = self.create_res_result['resource_result']['affectedvirtuallink']
194         cps = self.create_res_result['resource_result']['affectedcp']
195
196         for vnfc in vnfcs:
197             if 'failed' == vnfc['status']:
198                 continue
199             compute_resource = vnfc['computeresource']
200             vminst = VmInstModel.objects.filter(resouceid=compute_resource['resourceid']).first()
201             VNFCInstModel.objects.create(
202                 vnfcinstanceid=vnfc['vnfcinstanceid'],
203                 vduid=vnfc['vduid'],
204                 vdutype=vnfc['vdutype'],
205                 nfinstid=self.nf_inst_id,
206                 vmid=vminst.vmid)
207         for vl in vls:
208             if 'failed' == vl['status']:
209                 continue
210             network_resource = vl['networkresource']
211             subnet_resource = vl['subnetworkresource']
212             networkinst = NetworkInstModel.objects.filter(resouceid=network_resource['resourceid']).first()
213             subnetinst = SubNetworkInstModel.objects.filter(resouceid=subnet_resource['resourceid']).first()
214             VLInstModel.objects.create(
215                 vlinstanceid=vl['virtuallinkinstanceid'],
216                 vldid=vl['virtuallinkdescid'],
217                 ownertype='0',
218                 ownerid=self.nf_inst_id,
219                 relatednetworkid=networkinst.networkid,
220                 relatedsubnetworkid=subnetinst.subnetworkid)
221         # # for vs in vss:
222         for cp in cps:
223             if 'failed' == cp['status']:
224                 continue
225             port_resource = cp['portresource']
226             portinst = PortInstModel.objects.filter(resouceid=port_resource['resourceid']).first()
227             ttt = portinst.portid
228             CPInstModel.objects.create(
229                 cpinstanceid=cp['cpinstanceid'],
230                 cpdid=cp['cpdid'],
231                 relatedtype='2',
232                 relatedport=portinst.portid,
233                 ownertype=cp['ownertype'],
234                 ownerid=cp['ownerid'],
235                 vlinstanceid=cp['virtuallinkinstanceid'])
236         # self.add_job(43, 'INST_DPLY_VM_PRGS')
237         logger.info("[NF instantiation] confirm all vms are active end")
238
239     def wait_inst_finish(self, args):
240         try:
241             logger.info('wait_inst_finish, args=%s' % args)
242             # WaitInstFinishTask(args).do_biz()
243             return {'result': '100', 'msg': 'Nf instancing wait finish', 'context': {}}
244         except Exception as e:
245             logger.error('Nf instancing wait exception=%s' % e.message)
246             logger.error(traceback.format_exc())
247             return {'result': '255', 'msg': 'Nf instancing wait exception', 'context': {}}
248
249     def lcm_notify(self, args):
250         try:
251             logger.info('lcm_notify, args=%s' % args)
252             # LcmNotifyTask(args).do_biz()
253             return {'result': '100', 'msg': 'Nf instancing lcm notify finish', 'context': {}}
254         except Exception as e:
255             logger.error('Nf instancing lcm notify exception=%s' % e.message)
256             logger.error(traceback.format_exc())
257             return {'result': '255', 'msg': 'Nf instancing lcm notify exception', 'context': {}}
258
259     def rollback(self, args):
260         try:
261             logger.info('inst_exception, args=%s' % args)
262             # InstExceptionTask(args).do_biz()
263             return {'result': '100', 'msg': 'Nf instancing exception process finish', 'context': {}}
264         except Exception as e:
265             logger.error('Nf instancing exception process exception=%s' % e.message)
266             logger.error(traceback.format_exc())
267             return {'result': '255', 'msg': 'Nf instancing exception process exception', 'context': {}}
268
269     def load_nfvo_config(self):
270         logger.info("[NF instantiation]get nfvo connection info start")
271         reg_info = NfvoRegInfoModel.objects.filter(vnfminstid='vnfm111').first()
272         if reg_info:
273             self.nfvo_inst_id = reg_info.nfvoid
274             logger.info("[NF instantiation] Registered nfvo id is [%s]"%self.nfvo_inst_id)
275         else:
276             raise NFLCMException("Nfvo was not registered")
277         logger.info("[NF instantiation]get nfvo connection info end")
278
279     def vnf_inst_failed_handle(self, error_msg):
280         logger.error('VNF instantiation failed, detail message: %s' % error_msg)
281         NfInstModel.objects.filter(nfinstid=self.nf_inst_id).update(status='failed', lastuptime=now_time())
282         JobUtil.add_job_status(self.job_id, 255, error_msg)
283         # JobUtil.add_job_status(self.job_id, 255, 'VNF instantiation failed, detail message: %s' % error_msg, 0)
284
285     def do_notify(self, res_type, progress, ret):
286         # logger.info('add job, progress=%s, msgid=%s, args=%s' % (progress, msgid, args_))
287         progress = 20 + int(progress/2)     #20-70
288         if res_type == adaptor.RES_VOLUME:
289             logger.info('Create vloumns!')
290             if ret["returnCode"] == adaptor.RES_NEW:#new create
291                 self.inst_resource['volumn'].append({"vim_id": ret["vim_id"]}, {"res_id": ret["res_id"]})
292             JobUtil.add_job_status(self.job_id, progress, 'Create vloumns!')
293             StorageInstModel.objects.create(
294                 storageid='1',
295                 vimid='1',
296                 resouceid='1',
297                 name='40G',
298                 tenant='admin',
299                 insttype=0,
300                 instid=self.nf_inst_id)
301         elif res_type == adaptor.RES_NETWORK:
302             logger.info('Create networks!')
303             if ret["returnCode"] == adaptor.RES_NEW:
304                 self.inst_resource['network'].append({"vim_id": ret["vim_id"]}, {"res_id": ret["res_id"]})
305             # self.inst_resource['network'].append({"vim_id": "1"}, {"res_id": "2"})
306             JobUtil.add_job_status(self.job_id, progress, 'Create networks!')
307             NetworkInstModel.objects.create(
308                 networkid='1',
309                 vimid='1',
310                 resouceid='1',
311                 name='pnet_network',
312                 tenant='admin',
313                 insttype=0,
314                 instid=self.nf_inst_id)
315         elif res_type == adaptor.RES_SUBNET:
316             logger.info('Create subnets!')
317             if ret["returnCode"] == adaptor.RES_NEW:
318                 self.inst_resource['subnet'].append({"vim_id": ret["vim_id"]}, {"res_id": ret["res_id"]})
319             # self.inst_resource['subnet'].append({"vim_id": "1"}, {"res_id": "2"})
320             JobUtil.add_job_status(self.job_id, progress, 'Create subnets!')
321             SubNetworkInstModel.objects.create(
322                 subnetworkid='1',
323                 vimid='1',
324                 resouceid='1',
325                 networkid='1',
326                 name='sub_pnet',
327                 tenant='admin',
328                 insttype=0,
329                 instid=self.nf_inst_id)
330         elif res_type == adaptor.RES_PORT:
331             logger.info('Create ports!')
332             if ret["returnCode"] == adaptor.RES_NEW:
333                 self.inst_resource['port'].append({"vim_id": ret["vim_id"]}, {"res_id": ret["res_id"]})
334             # self.inst_resource['port'].append({"vim_id": "1"}, {"res_id": "2"})
335             JobUtil.add_job_status(self.job_id, progress, 'Create ports!')
336             PortInstModel.objects.create(
337                 portid='1',
338                 networkid='1',
339                 subnetworkid='1',
340                 vimid='1',
341                 resouceid='1',
342                 name='aaa_pnet_cp',
343                 tenant='admin',
344                 insttype=0,
345                 instid=self.nf_inst_id)
346         elif res_type == adaptor.RES_FLAVOR:
347             logger.info('Create flavors!')
348             if ret["returnCode"] == adaptor.RES_NEW:
349                 self.inst_resource['flavor'].append({"vim_id": ret["vim_id"]}, {"res_id": ret["res_id"]})
350             # self.inst_resource['flavor'].append({"vim_id": "1"}, {"res_id": "2"})
351             JobUtil.add_job_status(self.job_id, progress, 'Create flavors!')
352             FlavourInstModel.objects.create(
353                 falavourid='1',
354                 name='1',
355                 vcpu='1',
356                 extraspecs='1',
357                 instid=self.nf_inst_id)
358         elif res_type == adaptor.RES_VM:
359             logger.info('Create vms!')
360             if ret["returnCode"] == adaptor.RES_NEW:
361                 self.inst_resource['vm'].append({"vim_id": ret["vim_id"]}, {"res_id": ret["res_id"]})
362             # self.inst_resource['vm'].append({"vim_id": "1"}, {"res_id": "2"})
363             JobUtil.add_job_status(self.job_id, progress, 'Create vms!')
364             VmInstModel.objects.create(
365                 vmid="1",
366                 vimid="1",
367                 resouceid="11",
368                 insttype=0,
369                 instid=self.nf_inst_id,
370                 vmname="test_01",
371                 operationalstate=1)
372
373     def do_rollback(self, args_=None):
374         logger.error('error info : %s'%(args_))
375
376         adaptor.delete_vim_res(self.inst_resource, self.do_notify_delete)
377
378         StorageInstModel.objects.filter(instid=self.nf_inst_id).delete()
379         NetworkInstModel.objects.filter(instid=self.nf_inst_id).delete()
380         SubNetworkInstModel.objects.filter(instid=self.nf_inst_id).delete()
381         PortInstModel.objects.filter(instid=self.nf_inst_id).delete()
382         FlavourInstModel.objects.filter(instid=self.nf_inst_id).delete()
383         VmInstModel.objects.filter(instid=self.nf_inst_id).delete()
384
385         raise NFLCMException("Create resource failed")
386
387     def do_notify_delete(ret):
388         logger.error('Delete [%s] resource'%ret)
389
390