VNF config SBI
[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
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, 2, 'GET_NFVO_CONNECTION_INFO')
78         self.load_global_config()
79
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)
83
84     def apply_grant(self, args):
85         try:
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': {}}
93
94     def apply_res(self, args):
95         try:
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': {}}
103
104     def check_res_status(self, args):
105         try:
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': {}}
113
114     def wait_inst_finish(self, args):
115         try:
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': {}}
123
124     def lcm_notify(self, args):
125         try:
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': {}}
133
134     def inst_exception(self, args):
135         try:
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': {}}
143
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()
147         if reg_info:
148             self.nfvo_reg_info = reg_info.nfvoid
149             logger.info("[NF instantiation] Registered nfvo id is [%s]"%self.nfvo_reg_info)
150         else:
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")