1 # Copyright 2016 ZTE Corporation.
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
19 from lcm.pub.database.models import FPInstModel
20 from lcm.pub.msapi import extsys
21 from lcm.pub.msapi import sdncdriver
23 logger = logging.getLogger(__name__)
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 = ""
32 self.port_pair_groups_ids = []
33 self.flow_classifier_ids = []
36 logger.info("CreatePortChain start:")
39 logger.info("CreatePortChain end:")
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
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
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)
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)
74 # def get_url_by_sdncontrollerid(self):
76 # logger.warn("query sdncontroller by id begins:")
78 # url = "/api/aai-esr-server/v1/sdncontrollers/%s" % (self.sdnControllerId)
79 # ret = req_by_msb(url, "GET")
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:")
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.')
91 # return resp_body('url')
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()