update version of lcm
[vfc/nfvo/lcm.git] / lcm / ns_sfcs / biz / create_sfc_worker.py
1 # Copyright 2016 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
16 import logging
17 import traceback
18 from threading import Thread
19
20 from lcm.ns_sfcs.biz.create_flowcla import CreateFlowClassifier
21 from lcm.ns_sfcs.biz.create_port_chain import CreatePortChain
22 from lcm.ns_sfcs.biz.create_portpairgp import CreatePortPairGroup
23
24 from lcm.pub.exceptions import NSLCMException
25 from lcm.pub.utils.jobutil import JobUtil
26 from lcm.ns_sfcs.biz.utils import update_fp_status
27
28 logger = logging.getLogger(__name__)
29
30
31 class CreateSfcWorker(Thread):
32     def __init__(self, data):
33         super(CreateSfcWorker, self).__init__()
34         self.ns_inst_id = data["nsinstid"]
35         self.ns_model_data = data["ns_model_data"]
36         self.fp_id = data["fpindex"]
37         self.sdnControllerId = data["sdncontrollerid"]
38         self.fp_inst_id = data["fpinstid"]
39         self.data = data
40         self.job_id = ""
41
42     def init_data(self):
43         self.job_id = JobUtil.create_job("SFC", "sfc_init", self.ns_inst_id + "_" + self.fp_id)
44         return self.job_id
45
46     def run(self):
47         try:
48             logger.info("Service Function Chain Worker  start : ")
49
50             CreateFlowClassifier(self.data).do_biz()
51             JobUtil.add_job_status(self.job_id, 50, "create flow classifer successfully!", "")
52             CreatePortPairGroup(self.data).do_biz()
53             JobUtil.add_job_status(self.job_id, 75, "create port pair group successfully!", "")
54             CreatePortChain(self.data).do_biz()
55             update_fp_status(self.fp_inst_id, "active")
56             JobUtil.add_job_status(self.job_id, 100, "create port chain successful!", "")
57             logger.info("Service Function Chain Worker end : ")
58         except NSLCMException as e:
59             self.handle_exception(e)
60         except Exception as e:
61             self.handle_exception(e)
62
63     def handle_exception(self, e):
64         detail = "sfc instantiation failed, detail message: %s" % e.message
65         JobUtil.add_job_status(self.job_id, 255, "create sfc failed!", "")
66         logger.error(traceback.format_exc())
67         logger.error(detail)
68         update_fp_status(self.fp_inst_id, "failed")