db45fd62f1aa1a16e2cd2d1b34c7089dfcba7d40
[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             JobUtil.add_job_status(self.job_id, 100, "Instantiate Vnf success.")
61             is_exist = JobStatusModel.objects.filter(jobid=self.job_id).exists()
62             logger.debug("check_ns_inst_name_exist::is_exist=%s" % is_exist)
63         except Exception as e:
64             logger.error('Nf instancing preprocess exception=%s' % e.message)
65             logger.error(traceback.format_exc())
66             return {'result': '255', 'msg': 'Nf instancing preprocess exception', 'context': {}}
67
68     def apply_grant(self, args):
69         try:
70             logger.info('apply_grant, args=%s' % args)
71             # ApplyGrantTask(args).do_biz()
72             return {'result': '100', 'msg': 'Nf instancing apply grant finish', 'context': {}}
73         except Exception as e:
74             logger.error('Nf instancing apply grant exception=%s' % e.message)
75             logger.error(traceback.format_exc())
76             return {'result': '255', 'msg': 'Nf instancing apply grant exception', 'context': {}}
77
78     def apply_res(self, args):
79         try:
80             logger.info('apply_res, args=%s' % args)
81             # ApplyResTask(args).do_biz()
82             return {'result': '100', 'msg': 'Nf instancing apply resource finish', 'context': {}}
83         except Exception as e:
84             logger.error('Nf instancing apply resource exception=%s' % e.message)
85             logger.error(traceback.format_exc())
86             return {'result': '255', 'msg': 'Nf instancing apply resource exception', 'context': {}}
87
88     def check_res_status(self, args):
89         try:
90             logger.info('check_res_status, args=%s' % args)
91             # CheckResStatusTask(args).do_biz()
92             return {'result': '100', 'msg': 'Nf instancing check resource status finish', 'context': {}}
93         except Exception as e:
94             logger.error('Nf instancing check resource status exception=%s' % e.message)
95             logger.error(traceback.format_exc())
96             return {'result': '255', 'msg': 'Nf instancing check resource status exception', 'context': {}}
97
98     def wait_inst_finish(self, args):
99         try:
100             logger.info('wait_inst_finish, args=%s' % args)
101             # WaitInstFinishTask(args).do_biz()
102             return {'result': '100', 'msg': 'Nf instancing wait finish', 'context': {}}
103         except Exception as e:
104             logger.error('Nf instancing wait exception=%s' % e.message)
105             logger.error(traceback.format_exc())
106             return {'result': '255', 'msg': 'Nf instancing wait exception', 'context': {}}
107
108     def lcm_notify(self, args):
109         try:
110             logger.info('lcm_notify, args=%s' % args)
111             # LcmNotifyTask(args).do_biz()
112             return {'result': '100', 'msg': 'Nf instancing lcm notify finish', 'context': {}}
113         except Exception as e:
114             logger.error('Nf instancing lcm notify exception=%s' % e.message)
115             logger.error(traceback.format_exc())
116             return {'result': '255', 'msg': 'Nf instancing lcm notify exception', 'context': {}}
117
118     def inst_exception(self, args):
119         try:
120             logger.info('inst_exception, args=%s' % args)
121             # InstExceptionTask(args).do_biz()
122             return {'result': '100', 'msg': 'Nf instancing exception process finish', 'context': {}}
123         except Exception as e:
124             logger.error('Nf instancing exception process exception=%s' % e.message)
125             logger.error(traceback.format_exc())
126             return {'result': '255', 'msg': 'Nf instancing exception process exception', 'context': {}}