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.
18 from lcm.ns.sfcs.utils import get_fp_model_by_fp_inst_id
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 CreateFlowClassifier(object):
27 def __init__(self, data):
28 self.ns_model_data = data["ns_model_data"]
29 self.fp_inst_id = data["fpinstid"]
30 self.flow_classifiers_model = get_fp_model_by_fp_inst_id(data["ns_model_data"], self.fp_inst_id)["properties"][
32 self.sdnControllerId = ""
36 self.source_port_range = ""
37 self.dest_port_range = ""
38 self.source_ip_range = ""
39 self.dest_ip_range = ""
40 self.flow_classfier_id = ""
43 logger.info("CreateFlowClassifier start:")
44 self.init_data(self.flow_classifiers_model)
45 self.create_flow_classfier()
47 logger.info("CreateFlowClassifier end:")
49 def init_data(self, flow_classifiers_model):
50 fp_database_info = FPInstModel.objects.filter(fpinstid=self.fp_inst_id).get()
51 self.sdnControllerId = fp_database_info.sdncontrollerid
52 self.url = extsys.get_sdn_controller_by_id(self.sdnControllerId)["url"]
53 self.dscp = flow_classifiers_model["criteria"]["dscp"]
54 self.ip_proto = flow_classifiers_model["criteria"]["ip_protocol"]
55 self.source_port_range = flow_classifiers_model["criteria"]["source_port_range"]
56 self.dest_port_range = flow_classifiers_model["criteria"]["dest_port_range"]
57 self.dest_ip_range = flow_classifiers_model["criteria"]["dest_ip_range"]
58 self.source_ip_range = flow_classifiers_model["criteria"]["source_ip_range"]
60 def update_fp_inst(self):
61 fp_inst_info = FPInstModel.objects.filter(fpinstid=self.fp_inst_id).get()
62 fp_inst_info.flowclassifiers = self.flow_classfier_id
63 FPInstModel.objects.filter(fpinstid=self.fp_inst_id).update(flowclassifiers=fp_inst_info.flowclassifiers)
65 def create_flow_classfier(self):
67 "sdnControllerId": self.sdnControllerId,
72 "ip_proto": self.ip_proto,
73 "source_port_range": self.source_port_range,
74 "dest_port_range": self.dest_port_range,
75 "source_ip_range": self.concat_str(self.source_ip_range),
76 "dest_ip_range": self.concat_str(self.dest_ip_range)
78 # req_param = json.JSONEncoder().encoding(data)
79 # url = "/api/sdncdriver/v1.0/createflowclassfier"
80 # ret = req_by_msb(url,"POST", data)
82 # logger.error('Send Flow Classifier request to Driver failed.')
83 # utils.sfc_inst_failed_handle(self.fp_inst_id, "Send Flow Classifier request to Driver failed.")
84 # raise NSLCMException('Send Flow Classifier request to Driver failed.')
85 # resp_body = json.loads(ret[1])
86 self.flow_classfier_id = sdncdriver.create_flow_classfier(data)
88 def concat_str(self, str_list):
91 final_str += str + ","