Add instantiation state check
[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
15 import logging
16 import traceback
17 from threading import Thread
18
19 import time
20
21 from lcm.pub.database.models import NfInstModel, JobStatusModel
22 from lcm.pub.exceptions import NFLCMException
23 from lcm.pub.utils.jobutil import JobUtil
24
25 logger = logging.getLogger(__name__)
26
27
28 class InstVnf(Thread):
29     def __init__(self, data, nf_inst_id, job_id):
30         super(InstVnf, self).__init__()
31         self.data = data
32         self.nf_inst_id = nf_inst_id
33         self.job_id = job_id
34
35     def run(self):
36         try:
37             self.inst_pre(self.nf_inst_id)
38
39             # self.apply_grant(args)
40             # self.apply_res(args)
41             # self.check_res_status(args)
42             # self.wait_inst_finish(args)
43             # self.lcm_notify(args)
44         except NFLCMException as e:
45             self.inst_exception(e.message)
46             pass
47         except Exception:
48             logger.error(traceback.format_exc())
49             self.inst_exception('unexpected exception')
50
51     def inst_pre(self, args):
52         try:
53             logger.info('inst_pre, args=%s' % args)
54             is_exist = NfInstModel.objects.filter(nfinstid=self.nf_inst_id).exists()
55             logger.debug("check_ns_inst_name_exist::is_exist=%s" % is_exist)
56             if not is_exist:
57                 JobUtil.add_job_status(self.job_id, 255, "VNF nf_inst_id is not exist.")
58                 raise NFLCMException('VNF nf_inst_id is not exist.')
59
60             vnf_inst = NfInstModel.objects.get(nfinstid=self.nf_inst_id)
61             if vnf_inst.instantiationState != '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             JobUtil.add_job_status(self.job_id, 100, "Instantiate Vnf success.")
66             is_exist = JobStatusModel.objects.filter(jobid=self.job_id).exists()
67             logger.debug("check_ns_inst_name_exist::is_exist=%s" % is_exist)
68         except Exception as e:
69             logger.error('Nf instancing preprocess exception=%s' % e.message)
70             logger.error(traceback.format_exc())
71             return {'result': '255', 'msg': 'Nf instancing preprocess exception', 'context': {}}
72
73     def apply_grant(self, args):
74         try:
75             logger.info('apply_grant, args=%s' % args)
76             # ApplyGrantTask(args).do_biz()
77             return {'result': '100', 'msg': 'Nf instancing apply grant finish', 'context': {}}
78         except Exception as e:
79             logger.error('Nf instancing apply grant exception=%s' % e.message)
80             logger.error(traceback.format_exc())
81             return {'result': '255', 'msg': 'Nf instancing apply grant exception', 'context': {}}
82
83     def apply_res(self, args):
84         try:
85             logger.info('apply_res, args=%s' % args)
86             # ApplyResTask(args).do_biz()
87             return {'result': '100', 'msg': 'Nf instancing apply resource finish', 'context': {}}
88         except Exception as e:
89             logger.error('Nf instancing apply resource exception=%s' % e.message)
90             logger.error(traceback.format_exc())
91             return {'result': '255', 'msg': 'Nf instancing apply resource exception', 'context': {}}
92
93     def check_res_status(self, args):
94         try:
95             logger.info('check_res_status, args=%s' % args)
96             # CheckResStatusTask(args).do_biz()
97             return {'result': '100', 'msg': 'Nf instancing check resource status finish', 'context': {}}
98         except Exception as e:
99             logger.error('Nf instancing check resource status exception=%s' % e.message)
100             logger.error(traceback.format_exc())
101             return {'result': '255', 'msg': 'Nf instancing check resource status exception', 'context': {}}
102
103     def wait_inst_finish(self, args):
104         try:
105             logger.info('wait_inst_finish, args=%s' % args)
106             # WaitInstFinishTask(args).do_biz()
107             return {'result': '100', 'msg': 'Nf instancing wait finish', 'context': {}}
108         except Exception as e:
109             logger.error('Nf instancing wait exception=%s' % e.message)
110             logger.error(traceback.format_exc())
111             return {'result': '255', 'msg': 'Nf instancing wait exception', 'context': {}}
112
113     def lcm_notify(self, args):
114         try:
115             logger.info('lcm_notify, args=%s' % args)
116             # LcmNotifyTask(args).do_biz()
117             return {'result': '100', 'msg': 'Nf instancing lcm notify finish', 'context': {}}
118         except Exception as e:
119             logger.error('Nf instancing lcm notify exception=%s' % e.message)
120             logger.error(traceback.format_exc())
121             return {'result': '255', 'msg': 'Nf instancing lcm notify exception', 'context': {}}
122
123     def inst_exception(self, args):
124         try:
125             logger.info('inst_exception, args=%s' % args)
126             # InstExceptionTask(args).do_biz()
127             return {'result': '100', 'msg': 'Nf instancing exception process finish', 'context': {}}
128         except Exception as e:
129             logger.error('Nf instancing exception process exception=%s' % e.message)
130             logger.error(traceback.format_exc())
131             return {'result': '255', 'msg': 'Nf instancing exception process exception', 'context': {}}