def createXML_v1(): xml_data = xmltodict.parse( request.data, dict_constructor=dict )["mspl-set"]["it-resource"]["configuration"]["rule"] # print(xml_data) tcp_src_port = None tcp_dst_port = None udp_src_port = None udp_dst_port = None protocol = getValIfKeyExists( xml_data["condition"]["packet-filter-condition"], "protocol") if protocol == "TCP": tcp_src_port = getValIfKeyExists( xml_data["condition"]["packet-filter-condition"], "source-port") tcp_dst_port = getValIfKeyExists( xml_data["condition"]["packet-filter-condition"], "destination-port") elif protocol == "UDP": udp_src_port = getValIfKeyExists( xml_data["condition"]["packet-filter-condition"], "source-port") udp_dst_port = getValIfKeyExists( xml_data["condition"]["packet-filter-condition"], "destination-port") dict_flow = { "nw_src": getValIfKeyExists(xml_data["condition"]["packet-filter-condition"], "source-address"), "nw_dst": getValIfKeyExists(xml_data["condition"]["packet-filter-condition"], "destination-address"), "priority": getValIfKeyExists(xml_data, "priority"), "protocol": protocol, "tcp_src": tcp_src_port, "tcp_dst": tcp_dst_port, "udp_src": udp_src_port, "udp_dst": udp_dst_port, "actions": getValIfKeyExists(xml_data, "action") } FlowController(json_events=[dict_flow]).set_flows() # FlowController(globalVar.current_flow_list, json_events=json_data).set_flows() # print("########### RULES END #############") # print(globalVar.current_flow_list) return "Flow created!"
def createXML_v2(): xml_datas = [] rules = xmltodict.parse( request.data, dict_constructor=dict )["mspl-set"]["it-resource"]["configuration"]["rule"] if isinstance(rules, list): for rule in rules: xml_datas.append(rule) else: xml_datas.append(rules) print(xml_datas) rate_limit = None dict_flows = [] for xml_data in xml_datas: if getValIfKeyExists(xml_data["condition"], "traffic-flow-condition") is not None: rate_limit = getValIfKeyExists( xml_data["condition"]["traffic-flow-condition"], "rate-limit") dict_flows.append({ "ip_src": getValIfKeyExists(xml_data["condition"]["packet-filter-condition"], "source-address"), "ip_dst": getValIfKeyExists(xml_data["condition"]["packet-filter-condition"], "destination-address"), "priority": getValIfKeyExists(xml_data, "priority"), "protocol": getValIfKeyExists(xml_data["condition"]["packet-filter-condition"], "protocol"), "port_src": getValIfKeyExists(xml_data["condition"]["packet-filter-condition"], "source-port"), "port_dst": getValIfKeyExists(xml_data["condition"]["packet-filter-condition"], "destination-port"), "actions": getValIfKeyExists(xml_data, "action"), "rate_limit": rate_limit }) # response = "Test.." # response = AbstractFlowController(json_events=dict_flows).set_flows() response = AbstractFlowController( json_events=dict_flows).set_flows_performance() # FlowController(globalVar.current_flow_list, json_events=json_data).set_flows() # print("########### RULES END #############") # print(globalVar.current_flow_list) return jsonify(response)
def set(self, dict_representation): self.id = getValIfKeyExists(dict_representation, "id") self.ip_src = getValIfKeyExists(dict_representation, "ip_src") self.ip_dst = getValIfKeyExists(dict_representation, "ip_dst") self.protocol = getValIfKeyExists(dict_representation, "protocol").lower() self.port_src = getValIfKeyExists(dict_representation, "port_src") self.port_dst = getValIfKeyExists(dict_representation, "port_dst") self.rate_limit = getValIfKeyExists(dict_representation, "rate_limit") self.actions = getValIfKeyExists(dict_representation, "actions")
def abstract_to_traffic_control_flow(abstract_dict): tc_dict = { "ip_src": getValIfKeyExists(abstract_dict, "ip_src"), "ip_dst": getValIfKeyExists(abstract_dict, "ip_dst"), "protocol": getValIfKeyExists(abstract_dict, "protocol"), "port_src": getValIfKeyExists(dict_representation, "port_src"), "port_dst": getValIfKeyExists(dict_representation, "port_dst"), "actions": getValIfKeyExists(abstract_dict, "actions"), "rate_limit": getValIfKeyExists(abstract_dict, "rate_limit") } return tc_dict
def abstract_to_ovs_flow(abstract_dict): tcp_src_port = None tcp_dst_port = None udp_src_port = None udp_dst_port = None if getValIfKeyExists(abstract_dict, "protocol").lower() == "tcp": print("*********************TCP************************") tcp_src_port = getValIfKeyExists(abstract_dict, "port_src") tcp_dst_port = getValIfKeyExists(abstract_dict, "port_dst") elif getValIfKeyExists(abstract_dict, "protocol").lower() == "udp": print("*********************UDP************************") udp_src_port = getValIfKeyExists(abstract_dict, "port_src") udp_dst_port = getValIfKeyExists(abstract_dict, "port_dst") print("*********************************************") print(getValIfKeyExists(abstract_dict, "protocol").lower()) print(tcp_src_port) print(tcp_dst_port) print(udp_src_port) print(udp_dst_port) ovs_dict = { "nw_src": getValIfKeyExists(abstract_dict, "ip_src"), "nw_dst": getValIfKeyExists(abstract_dict, "ip_dst"), "priority": getValIfKeyExists(abstract_dict, "priority"), "protocol": getValIfKeyExists(abstract_dict, "protocol"), "tcp_src": tcp_src_port, "tcp_dst": tcp_dst_port, "udp_src": udp_src_port, "udp_dst": udp_dst_port, "actions": getValIfKeyExists(abstract_dict, "actions") } return ovs_dict
def set(self, dict_representation): self.id = getValIfKeyExists(dict_representation, "id") self.ip_src = getValIfKeyExists(dict_representation, "ip_src") self.ip_dst = getValIfKeyExists(dict_representation, "ip_dst") self.priority = getValIfKeyExists(dict_representation, "priority") self.protocol = getValIfKeyExists(dict_representation, "protocol").lower() self.port_src = getValIfKeyExists(dict_representation, "port_src") self.port_dst = getValIfKeyExists(dict_representation, "port_dst") self.rate_limit = getValIfKeyExists(dict_representation, "rate_limit") self.actions = getValIfKeyExists(dict_representation, "actions") def abstract_to_ovs_flow(abstract_dict): tcp_src_port = None tcp_dst_port = None udp_src_port = None udp_dst_port = None if getValIfKeyExists(abstract_dict, "protocol").lower() == "tcp": print("*********************TCP************************") tcp_src_port = getValIfKeyExists(abstract_dict, "port_src") tcp_dst_port = getValIfKeyExists(abstract_dict, "port_dst") elif getValIfKeyExists(abstract_dict, "protocol").lower() == "udp": print("*********************UDP************************") udp_src_port = getValIfKeyExists(abstract_dict, "port_src") udp_dst_port = getValIfKeyExists(abstract_dict, "port_dst") print("*********************************************") print(getValIfKeyExists(abstract_dict, "protocol").lower()) print(tcp_src_port) print(tcp_dst_port) print(udp_src_port) print(udp_dst_port) ovs_dict = { "nw_src": getValIfKeyExists(abstract_dict, "ip_src"), "nw_dst": getValIfKeyExists(abstract_dict, "ip_dst"), "priority": getValIfKeyExists(abstract_dict, "priority"), "protocol": getValIfKeyExists(abstract_dict, "protocol"), "tcp_src": tcp_src_port, "tcp_dst": tcp_dst_port, "udp_src": udp_src_port, "udp_dst": udp_dst_port, "actions": getValIfKeyExists(abstract_dict, "actions") } return ovs_dict def abstract_to_traffic_control_flow(abstract_dict): tc_dict = { "ip_src": getValIfKeyExists(abstract_dict, "ip_src"), "ip_dst": getValIfKeyExists(abstract_dict, "ip_dst"), "protocol": getValIfKeyExists(abstract_dict, "protocol"), "port_src": getValIfKeyExists(dict_representation, "port_src"), "port_dst": getValIfKeyExists(dict_representation, "port_dst"), "actions": getValIfKeyExists(abstract_dict, "actions"), "rate_limit": getValIfKeyExists(abstract_dict, "rate_limit") } return tc_dict if self.actions is not None: if self.actions == "drop": self.ovs_flow = OvsFlow() ovs_dict = abstract_to_ovs_flow(dict_representation) self.ovs_flow.set(ovs_dict) elif self.actions == "accept": self.traffic_control_flow = TrafficControlFlow() traffic_control_dict = abstract_to_traffic_control_flow( dict_representation) self.traffic_control_flow.set(traffic_control_dict)
def set(self, dict_representation): self.id = getValIfKeyExists(dict_representation, "id") self.nw_src = getValIfKeyExists(dict_representation, "nw_src") self.nw_dst = getValIfKeyExists(dict_representation, "nw_dst") self.n_packets = getValIfKeyExists(dict_representation, "n_packets") self.n_bytes = getValIfKeyExists(dict_representation, "n_bytes") self.cookie = getValIfKeyExists(dict_representation, "cookie") self.idle_age = getValIfKeyExists(dict_representation, "idle_age") self.duration = getValIfKeyExists(dict_representation, "duration") self.priority = getValIfKeyExists(dict_representation, "priority") self.table = getValIfKeyExists(dict_representation, "table") self.protocol = getValIfKeyExists(dict_representation, "protocol").lower() self.tcp_src = getValIfKeyExists(dict_representation, "tcp_src") self.tcp_dst = getValIfKeyExists(dict_representation, "tcp_dst") self.udp_src = getValIfKeyExists(dict_representation, "udp_src") self.udp_dst = getValIfKeyExists(dict_representation, "udp_dst") self.actions = getValIfKeyExists(dict_representation, "actions")