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
20 from lcm.pub.exceptions import NFLCMException
21 from lcm.pub.msapi.nfvolcm import vnfd_rawdata_get
22 from lcm.pub.utils.jobutil import JobUtil
24 logger = logging.getLogger(__name__)
27 class InstVnf(Thread):
28 def __init__(self, data, nf_inst_id, job_id):
29 super(InstVnf, self).__init__()
31 self.nf_inst_id = nf_inst_id
36 self.inst_pre(self.nf_inst_id)
38 # self.apply_grant(args)
39 # self.apply_res(args)
40 # self.check_res_status(args)
41 # self.wait_inst_finish(args)
42 # self.lcm_notify(args)
43 except NFLCMException as e:
44 self.inst_exception(e.message)
47 logger.error(traceback.format_exc())
48 self.inst_exception('unexpected exception')
50 def inst_pre(self, args):
51 logger.info('inst_pre, args=%s' % args)
52 is_exist = NfInstModel.objects.filter(nfinstid=self.nf_inst_id).exists()
53 logger.debug("check_ns_inst_name_exist::is_exist=%s" % is_exist)
55 logger.error("VNF nf_inst_id is not exist.")
56 JobUtil.add_job_status(self.job_id, 255, "VNF nf_inst_id is not exist.")
57 raise NFLCMException('VNF nf_inst_id is not exist.')
59 vnf_inst = NfInstModel.objects.get(nfinstid=self.nf_inst_id)
60 if vnf_inst.instantiationState != 'NOT_INSTANTIATED':
61 logger.error("VNF instantiationState is not NOT_INSTANTIATED.")
62 JobUtil.add_job_status(self.job_id, 255, "VNF instantiationState is not NOT_INSTANTIATED.")
63 raise NFLCMException('VNF instantiationState is not NOT_INSTANTIATED.')
65 #get rawdata by vnfd_id
66 ret = vnfd_rawdata_get(vnf_inst.vnfdid)
68 raise NFLCMException("Get vnfd_raw_data failed.")
69 dst_plan = json.JSONDecoder().decode(ret[1])
72 if cp not in dst_plan:
73 logger.error("[%s] is not defined in vnfd_info."%cp)
74 JobUtil.add_job_status(self.job_id, 255, "Input parameter is not defined in vnfd_info.")
75 raise NFLCMException('Input parameter is not defined in vnfd_info.')
77 JobUtil.add_job_status(self.job_id, 2, 'GET_NFVO_CONNECTION_INFO')
78 self.load_global_config()
80 JobUtil.add_job_status(self.job_id, 100, "Instantiate Vnf success.")
81 is_exist = JobStatusModel.objects.filter(jobid=self.job_id).exists()
82 logger.debug("check_ns_inst_name_exist::is_exist=%s" % is_exist)
84 def apply_grant(self, args):
86 logger.info('apply_grant, args=%s' % args)
87 # ApplyGrantTask(args).do_biz()
88 return {'result': '100', 'msg': 'Nf instancing apply grant finish', 'context': {}}
89 except Exception as e:
90 logger.error('Nf instancing apply grant exception=%s' % e.message)
91 logger.error(traceback.format_exc())
92 return {'result': '255', 'msg': 'Nf instancing apply grant exception', 'context': {}}
94 def apply_res(self, args):
96 logger.info('apply_res, args=%s' % args)
97 # ApplyResTask(args).do_biz()
98 return {'result': '100', 'msg': 'Nf instancing apply resource finish', 'context': {}}
99 except Exception as e:
100 logger.error('Nf instancing apply resource exception=%s' % e.message)
101 logger.error(traceback.format_exc())
102 return {'result': '255', 'msg': 'Nf instancing apply resource exception', 'context': {}}
104 def check_res_status(self, args):
106 logger.info('check_res_status, args=%s' % args)
107 # CheckResStatusTask(args).do_biz()
108 return {'result': '100', 'msg': 'Nf instancing check resource status finish', 'context': {}}
109 except Exception as e:
110 logger.error('Nf instancing check resource status exception=%s' % e.message)
111 logger.error(traceback.format_exc())
112 return {'result': '255', 'msg': 'Nf instancing check resource status exception', 'context': {}}
114 def wait_inst_finish(self, args):
116 logger.info('wait_inst_finish, args=%s' % args)
117 # WaitInstFinishTask(args).do_biz()
118 return {'result': '100', 'msg': 'Nf instancing wait finish', 'context': {}}
119 except Exception as e:
120 logger.error('Nf instancing wait exception=%s' % e.message)
121 logger.error(traceback.format_exc())
122 return {'result': '255', 'msg': 'Nf instancing wait exception', 'context': {}}
124 def lcm_notify(self, args):
126 logger.info('lcm_notify, args=%s' % args)
127 # LcmNotifyTask(args).do_biz()
128 return {'result': '100', 'msg': 'Nf instancing lcm notify finish', 'context': {}}
129 except Exception as e:
130 logger.error('Nf instancing lcm notify exception=%s' % e.message)
131 logger.error(traceback.format_exc())
132 return {'result': '255', 'msg': 'Nf instancing lcm notify exception', 'context': {}}
134 def inst_exception(self, args):
136 logger.info('inst_exception, args=%s' % args)
137 # InstExceptionTask(args).do_biz()
138 return {'result': '100', 'msg': 'Nf instancing exception process finish', 'context': {}}
139 except Exception as e:
140 logger.error('Nf instancing exception process exception=%s' % e.message)
141 logger.error(traceback.format_exc())
142 return {'result': '255', 'msg': 'Nf instancing exception process exception', 'context': {}}
144 def load_global_config(self):
145 logger.info("[NF instantiation]get nfvo connection info start")
146 reg_info = NfvoRegInfoModel.objects.filter(vnfminstid='vnfm111').first()
148 self.nfvo_reg_info = reg_info.nfvoid
149 logger.info("[NF instantiation] Registered nfvo id is [%s]"%self.nfvo_reg_info)
151 JobUtil.add_job_status(self.job_id, 255, "Nfvo was not registered")
152 raise NFLCMException("Nfvo was not registered")
153 logger.info("[NF instantiation]get nfvo connection info end")