update version of lcm
[vfc/nfvo/lcm.git] / lcm / ns / sfcs / create_port_chain.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 json
17 import logging
18
19 from lcm.pub.database.models import FPInstModel
20 from lcm.pub.msapi import extsys
21 from lcm.pub.msapi import sdncdriver
22
23 logger = logging.getLogger(__name__)
24
25
26 class CreatePortChain(object):
27     def __init__(self, data):
28         self.fp_inst_id = data["fpinstid"]
29         self.ns_model_info = data["ns_model_data"]
30         self.sdnControllerId = ""
31         self.symmetric = ""
32         self.port_pair_groups_ids = []
33         self.flow_classifier_ids = []
34
35     def do_biz(self):
36         logger.info("CreatePortChain start:")
37         self.init_data()
38         self.create_sfc()
39         logger.info("CreatePortChain end:")
40
41     def init_data(self):
42         fp_inst_info = FPInstModel.objects.filter(fpinstid=self.fp_inst_id).get()
43         self.sdnControllerId = fp_inst_info.sdncontrollerid
44         self.symmetric = "true" if fp_inst_info.symmetric == 1 else "false"
45         flow_classfier_str = fp_inst_info.flowclassifiers
46         self.flow_classifier_ids = [flow_classfier_str]
47         portpairgroup_ids = []
48         for portpairgroup in json.loads(fp_inst_info.portpairgroups):
49             portpairgroup_ids.append(portpairgroup["groupid"])
50         self.port_pair_groups_ids = portpairgroup_ids
51
52     def create_sfc(self):
53         data = {
54             "sdnControllerId": self.sdnControllerId,
55             "url": extsys.get_sdn_controller_by_id(self.sdnControllerId)["url"],
56             "flowClassifiers": self.flow_classifier_ids,
57             "portPairGroups": self.port_pair_groups_ids,
58             "symmetric": self.symmetric
59         }
60
61         # url = "/api/sdncdriver/v1.0/createchain"
62         # req_param = json.JSONEncoder.encoding(data)
63         # ret = req_by_msb(url, "POST", req_param)
64         # ret = req_by_msb("OPENAPI_CREATE_SERVICE_PORT_CHAIN",data)
65         # if ret[0] > 0:
66         #     logger.error('Send SFC Create request to Driver failed.')
67         #     sfc_inst_failed_handle( "Send SFC Create request to Driver failed.")
68         #     raise NSLCMException('Send SFC Create request to Driver failed.')
69         # resp_body = json.loads(ret[1])
70         # sfc_id = resp_body["id"]
71         sfc_id = sdncdriver.create_port_chain(data)
72         FPInstModel.objects.filter(fpinstid=self.fp_inst_id).update(sfcid=sfc_id)
73
74         # def get_url_by_sdncontrollerid(self):
75         #     try:
76         #         logger.warn("query sdncontroller by id begins:")
77         #
78         #         url = "/api/aai-esr-server/v1/sdncontrollers/%s" % (self.sdnControllerId)
79         #         ret = req_by_msb(url, "GET")
80         #         if ret[0] > 0:
81         #             logger.error('query sdncontroller failed.')
82         #             raise VnfoException('query sdncontroller failed.')
83         #         resp_body = json.JSONDecoder().decode(ret[1])
84         #         logger.warn("query sdncontroller by id ends:")
85         #     except:
86         #         if ret[0] > 0:
87         #             logger.error('Send Flow Classifier request to Driver failed.')
88         #             self.sfc_inst_failed_handle(self.fp_inst_id, "Send Flow Classifier request to Driver failed.")
89         #             raise VnfoException('Send Flow Classifier request to Driver failed.')
90         #
91         #     return resp_body('url')
92
93         # def sfc_inst_failed_handle(fp_inst_id, error_msg):
94         #     logger.error('create sfc  failed, detail message: %s' % error_msg)
95         #     FPInstModel.objects.filter(fpid=fp_inst_id).update(status="disabled").get()