SOL003 API Align
[vfc/nfvo/lcm.git] / lcm / ns_sfcs / biz / create_flowcla.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
18 from lcm.pub.database.models import FPInstModel
19 from lcm.pub.msapi import extsys
20 from lcm.pub.msapi import sdncdriver
21 from lcm.ns_sfcs.biz.utils import get_fp_model_by_fp_inst_id
22
23 logger = logging.getLogger(__name__)
24
25
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"][
31             "policy"]
32         self.sdnControllerId = ""
33         self.url = ""
34         self.dscp = ""
35         self.ip_proto = ""
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 = ""
41
42     def do_biz(self):
43         logger.info("CreateFlowClassifier start:")
44         self.init_data(self.flow_classifiers_model)
45         self.create_flow_classfier()
46         self.update_fp_inst()
47         logger.info("CreateFlowClassifier end:")
48
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"]
59
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)
64
65     def create_flow_classfier(self):
66         data = {
67             "sdnControllerId": self.sdnControllerId,
68             "url": self.url,
69             "name": "",
70             "description": "",
71             "dscp": self.dscp,
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)
77         }
78         # req_param = json.JSONEncoder().encoding(data)
79         # url = "/api/sdncdriver/v1.0/createflowclassfier"
80         # ret = req_by_msb(url,"POST", data)
81         # if ret[0] > 0:
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)
87
88     def concat_str(self, str_list):
89         final_str = ""
90         for str in str_list:
91             final_str += str + ","
92         return final_str[:-1]