GET Vnf Description
[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
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
23
24 logger = logging.getLogger(__name__)
25
26
27 class InstVnf(Thread):
28     def __init__(self, data, nf_inst_id, job_id):
29         super(InstVnf, self).__init__()
30         self.data = data
31         self.nf_inst_id = nf_inst_id
32         self.job_id = job_id
33
34     def run(self):
35         try:
36             self.inst_pre(self.nf_inst_id)
37
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)
45             pass
46         except Exception:
47             logger.error(traceback.format_exc())
48             self.inst_exception('unexpected exception')
49
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)
54         if not 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.')
58
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.')
64
65         #get rawdata by vnfd_id
66         ret = vnfd_rawdata_get(vnf_inst.vnfdid)
67         if ret[0] != 0:
68             raise NFLCMException("Get vnfd_raw_data failed.")
69         dst_plan = json.JSONDecoder().decode(ret[1])
70         #checkParameterExist
71         for cp in self.data:
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.')
76
77         JobUtil.add_job_status(self.job_id, 100, "Instantiate Vnf success.")
78         is_exist = JobStatusModel.objects.filter(jobid=self.job_id).exists()
79         logger.debug("check_ns_inst_name_exist::is_exist=%s" % is_exist)
80
81     def apply_grant(self, args):
82         try:
83             logger.info('apply_grant, args=%s' % args)
84             # ApplyGrantTask(args).do_biz()
85             return {'result': '100', 'msg': 'Nf instancing apply grant finish', 'context': {}}
86         except Exception as e:
87             logger.error('Nf instancing apply grant exception=%s' % e.message)
88             logger.error(traceback.format_exc())
89             return {'result': '255', 'msg': 'Nf instancing apply grant exception', 'context': {}}
90
91     def apply_res(self, args):
92         try:
93             logger.info('apply_res, args=%s' % args)
94             # ApplyResTask(args).do_biz()
95             return {'result': '100', 'msg': 'Nf instancing apply resource finish', 'context': {}}
96         except Exception as e:
97             logger.error('Nf instancing apply resource exception=%s' % e.message)
98             logger.error(traceback.format_exc())
99             return {'result': '255', 'msg': 'Nf instancing apply resource exception', 'context': {}}
100
101     def check_res_status(self, args):
102         try:
103             logger.info('check_res_status, args=%s' % args)
104             # CheckResStatusTask(args).do_biz()
105             return {'result': '100', 'msg': 'Nf instancing check resource status finish', 'context': {}}
106         except Exception as e:
107             logger.error('Nf instancing check resource status exception=%s' % e.message)
108             logger.error(traceback.format_exc())
109             return {'result': '255', 'msg': 'Nf instancing check resource status exception', 'context': {}}
110
111     def wait_inst_finish(self, args):
112         try:
113             logger.info('wait_inst_finish, args=%s' % args)
114             # WaitInstFinishTask(args).do_biz()
115             return {'result': '100', 'msg': 'Nf instancing wait finish', 'context': {}}
116         except Exception as e:
117             logger.error('Nf instancing wait exception=%s' % e.message)
118             logger.error(traceback.format_exc())
119             return {'result': '255', 'msg': 'Nf instancing wait exception', 'context': {}}
120
121     def lcm_notify(self, args):
122         try:
123             logger.info('lcm_notify, args=%s' % args)
124             # LcmNotifyTask(args).do_biz()
125             return {'result': '100', 'msg': 'Nf instancing lcm notify finish', 'context': {}}
126         except Exception as e:
127             logger.error('Nf instancing lcm notify exception=%s' % e.message)
128             logger.error(traceback.format_exc())
129             return {'result': '255', 'msg': 'Nf instancing lcm notify exception', 'context': {}}
130
131     def inst_exception(self, args):
132         try:
133             logger.info('inst_exception, args=%s' % args)
134             # InstExceptionTask(args).do_biz()
135             return {'result': '100', 'msg': 'Nf instancing exception process finish', 'context': {}}
136         except Exception as e:
137             logger.error('Nf instancing exception process exception=%s' % e.message)
138             logger.error(traceback.format_exc())
139             return {'result': '255', 'msg': 'Nf instancing exception process exception', 'context': {}}