1 # Copyright (C) 2018 Verizon. All Rights Reserved.
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.
18 from threading import Thread
20 from lcm.pub.database.models import NfInstModel, VmInstModel
21 from lcm.pub.exceptions import NFLCMException
22 from lcm.pub.msapi.gvnfmdriver import notify_lcm_to_nfvo, prepare_notification_data
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 from lcm.nf.biz.grant_vnf import grant_resource
28 from lcm.nf.const import VNF_STATUS, RESOURCE_MAP, GRANT_TYPE
30 logger = logging.getLogger(__name__)
33 class OperateVnf(Thread):
34 def __init__(self, data, nf_inst_id, job_id):
35 super(OperateVnf, self).__init__()
37 self.nf_inst_id = nf_inst_id
39 self.grant_type = GRANT_TYPE.OPERATE
40 self.changeStateTo = ignore_case_get(self.data, "changeStateTo")
41 self.stopType = ignore_case_get(self.data, "stopType")
42 self.gracefulStopTimeout = ignore_case_get(self.data, "gracefulStopTimeout")
43 self.inst_resource = {'vm': []}
48 self.query_inst_resource()
49 self.operate_resource()
50 JobUtil.add_job_status(self.job_id, 100, "Operate Vnf success.")
51 NfInstModel.objects.filter(nfinstid=self.nf_inst_id).update(status='INSTANTIATED', lastuptime=now_time(), operationState=self.changeStateTo)
53 except NFLCMException as e:
54 self.vnf_operate_failed_handle(e.message)
55 except Exception as e:
56 logger.error(e.message)
57 self.vnf_operate_failed_handle(traceback.format_exc())
59 def apply_grant(self):
60 vdus = VmInstModel.objects.filter(instid=self.nf_inst_id, is_predefined=1)
61 apply_result = grant_resource(data=self.data, nf_inst_id=self.nf_inst_id, job_id=self.job_id,
62 grant_type=self.grant_type, vdus=vdus)
63 logger.info("Grant resource, response: %s" % apply_result)
64 JobUtil.add_job_status(self.job_id, 20, 'Nf Operate grant_resource finish')
66 def query_inst_resource(self):
67 logger.info('Query resource begin')
68 # Querying only vm resources now
70 resource_table = globals().get(resource_type + 'InstModel')
71 resource_insts = resource_table.objects.filter(instid=self.nf_inst_id)
72 for resource_inst in resource_insts:
73 if not resource_inst.resouceid:
75 self.inst_resource[RESOURCE_MAP.get(resource_type)].append(self.get_resource(resource_inst))
76 logger.info('Query resource end, resource=%s' % self.inst_resource)
78 def get_resource(self, resource):
80 "vim_id": resource.vimid,
81 "tenant_id": resource.tenant,
82 "id": resource.resouceid
85 def operate_resource(self):
86 logger.info('Operate resource begin')
87 adaptor.operate_vim_res(self.inst_resource, self.changeStateTo, self.stopType, self.gracefulStopTimeout, self.do_notify_op)
88 logger.info('Operate resource complete')
91 notification_content = prepare_notification_data(self.nf_inst_id, self.job_id, "MODIFIED")
92 logger.info('Notify request data = %s' % notification_content)
93 resp = notify_lcm_to_nfvo(json.dumps(notification_content))
94 logger.info('Lcm notify end, response %s' % resp)
96 def vnf_operate_failed_handle(self, error_msg):
97 logger.error('VNF Operation failed, detail message: %s' % error_msg)
98 NfInstModel.objects.filter(nfinstid=self.nf_inst_id).update(status=VNF_STATUS.FAILED, lastuptime=now_time())
99 JobUtil.add_job_status(self.job_id, 255, error_msg)
101 def do_notify_op(self, status, resid):
102 logger.error('VNF resource %s updated to: %s' % (resid, status))
103 VmInstModel.objects.filter(instid=self.nf_inst_id, resouceid=resid).update(operationalstate=status)