call vim dirver adapter rollback 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, 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
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         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')
170
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')
174
175         vdus = ignore_case_get(self.data, "vdus")
176         # create_vdus(vdus)
177         JobUtil.add_job_status(self.job_id, 75, 'Nf instancing create resource(vms) finish')
178
179         JobUtil.add_job_status(self.job_id, 20, 'Nf instancing apply grant finish')
180         logger.info("[NF instantiation] create resource end")
181
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']
185         for vnfc in vnfcs:
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'])
189
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']
193
194         for vnfc in vnfcs:
195             if 'failed' == vnfc['status']:
196                 continue
197             compute_resource = vnfc['computeresource']
198             vminst = VmInstModel.objects.filter(resouceid=compute_resource['resourceid']).first()
199             VNFCInstModel.objects.create(
200                 vnfcinstanceid=vnfc['vnfcinstanceid'],
201                 vduid=vnfc['vduid'],
202                 vdutype=vnfc['vdutype'],
203                 nfinstid=self.nf_inst_id,
204                 vmid=vminst.vmid)
205         for vl in vls:
206             if 'failed' == vl['status']:
207                 continue
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'],
215                 ownertype='0',
216                 ownerid=self.nf_inst_id,
217                 relatednetworkid=networkinst.networkid,
218                 relatedsubnetworkid=subnetinst.subnetworkid)
219         # # for vs in vss:
220         for cp in cps:
221             if 'failed' == cp['status']:
222                 continue
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'],
228                 cpdid=cp['cpdid'],
229                 relatedtype='2',
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")
236
237     def wait_inst_finish(self, args):
238         try:
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': {}}
246
247     def lcm_notify(self, args):
248         try:
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': {}}
256
257     def rollback(self, args):
258         try:
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': {}}
266
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()
270         if reg_info:
271             self.nfvo_inst_id = reg_info.nfvoid
272             logger.info("[NF instantiation] Registered nfvo id is [%s]"%self.nfvo_inst_id)
273         else:
274             raise NFLCMException("Nfvo was not registered")
275         logger.info("[NF instantiation]get nfvo connection info end")
276
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)
282
283     def do_rollback(self, progress, msgid, args_=None):
284         # logger.info('add job, progress=%s, msgid=%s, args=%s' % (progress, msgid, args_))
285
286         # adaptor.delete_vim_res(self.inst_resource, self.do_notify_delete)
287
288         StorageInstModel.objects.filter(instid=self.nf_inst_id).delete()
289         NetworkInstModel.objects.filter(instid=self.nf_inst_id).delete()
290         SubNetworkInstModel.objects.filter(instid=self.nf_inst_id).delete()
291         PortInstModel.objects.filter(instid=self.nf_inst_id).delete()
292         FlavourInstModel.objects.filter(instid=self.nf_inst_id).delete()
293         VmInstModel.objects.filter(instid=self.nf_inst_id).delete()
294         JobUtil.add_job_status(self.job_id, 255, 'Create resource failed')
295
296     def do_notify_delete(ret):
297         logger.error('Delete [%s] resource'%ret)
298
299