def __init__(self, ctr_ip=None): self.controller_ip = ctr_ip if ctr_ip else const.DEFAULT_ODL_CONTROLLER_IP self.odlRequest = ODLRestRequest(self.controller_ip)
class OFManager(object): def __init__(self, ctr_ip=None): self.controller_ip = ctr_ip if ctr_ip else const.DEFAULT_ODL_CONTROLLER_IP self.odlRequest = ODLRestRequest(self.controller_ip) def write_flow(self, flowbody): return self.odlRequest.config_openflow(self.controller_ip, flowbody) def get_input_elem(self, doc): input = doc.createElement('input') input.setAttribute("xmlns", "urn:opendaylight:flow:service") doc.appendChild(input) return input def get_node_elem(self, node_id, doc): node = doc.createElement("node") node.setAttribute("xmlns:inv", "urn:opendaylight:inventory") node_id = doc.createTextNode(node_id) node.appendChild(node_id) return node def get_table_elem(self, table_id, doc): table = doc.createElement("table_id") table_id = doc.createTextNode(str(table_id)) table.appendChild(table_id) return table def get_priority_elem(self, priority, doc): priority_node = doc.createElement("priority") prio_value = doc.createTextNode(str(priority)) priority_node.appendChild(prio_value) return priority_node def convert_dict_to_xml(self, dict_obj, doc): node_list = [] for key, value in dict_obj.iteritems(): key_node = doc.createElement(key) if isinstance(value, dict): value_nodes = self.convert_dict_to_xml(value, doc) for node in value_nodes: key_node.appendChild(node) elif isinstance(value, list): value_nodes = self.get_list_node(value, doc) for node in value_nodes: key_node.appendChild(node) else: value = str(value) value_node = doc.createTextNode(str(value)) key_node.appendChild(value_node) node_list.append(key_node) return node_list def get_flow_match(self, match, doc): match_nodes = self.convert_dict_to_xml(match, doc) return match_nodes def get_list_node(self, value_list, doc): node_list = [] for value in value_list: if isinstance(value, str) or isinstance(value, int): value_node = doc.createTextNode(value) node_list.append(value_node) elif isinstance(value, dict): value_nodes = self.convert_dict_to_xml(value, doc) node_list.extend(value_nodes) return node_list def convert_flow_toxml(self, node_id, table_id, priority, matches, action_list): doc = Document() input = self.get_input_elem(doc) node_id = "/inv:nodes/inv:node[inv:id=\"%s\"]" % node_id node = self.get_node_elem(node_id, doc) input.appendChild(node) table = self.get_table_elem(table_id, doc) input.appendChild(table) priority = self.get_priority_elem(priority, doc) input.appendChild(priority) match = self.get_flow_match(matches, doc) for node in match: input.appendChild(node) instructions = doc.createElement("instructions") instruction = doc.createElement("instruction") appl_action = doc.createElement("apply-actions") appl_action_order = doc.createElement("order") appl_acion_order_value = doc.createTextNode("0") appl_action_order.appendChild(appl_acion_order_value) action_list_nodes = self.convert_dict_to_xml({"action": action_list}, doc) for action_node in action_list_nodes: appl_action.appendChild(action_node) instruction.appendChild(appl_action_order) instruction.appendChild(appl_action) instructions.appendChild(instruction) input.appendChild(instructions) return input, doc def get_flow(self, node_id, table_id, priority=0, flow_id=None, flow_name=None, output_port=None, mod_dmac=None, ret_pkt=False, **kwargs): action_list = [] if output_port: action_list.append(ofutils.set_output_action(output_port)) if mod_dmac: action_list.append(ofutils.set_mod_dmac_action(mod_dmac)) if ret_pkt: action_list.append(ofutils.set_return_pkt_action()) if not flow_id: flow_id = 1 flow = { "input": { "transaction-uri": "odlTraining8", "flow-ref": ofutils.set_flow_ref(node_id, table_id, flow_id), "node": ofutils.set_node(node_id), "flow-table": ofutils.set_flow_table(node_id, table_id), "flow": { "priority": priority, "table_id": table_id, "flow-name": flow_name if flow_name else ofutils.generate_flow_name( node_id, table_id, flow_id), "match": ofutils.set_match_field(**kwargs), "instructions": ofutils.set_actions(action_list) } } } return flow def construct_flow(self, node_id, table_id, priority=0, flow_name=None, output_port=None, mod_dmac=None, ret_pkt=False, **kwargs): flow = self.get_flow(node_id, table_id, priority, flow_name=flow_name, output_port=output_port, mod_dmac=mod_dmac, ret_pkt=ret_pkt, **kwargs) matches = {"match": flow["input"]["flow"]["match"]} action_list = flow["input"]["flow"]["instructions"]["instruction"][0][ "apply-actions"]["action"] flow_xml, doc = self.convert_flow_toxml(node_id, table_id, priority, matches, action_list) flow_body = doc.toprettyxml() print flow_body self.write_flow(flow_body) return flow_body def construct_flow_to_json(self, node_id, table_id, flow_id, priority=0, flow_name=None, output_port=None, mod_dmac=None, ret_pkt=False, **kwargs): flow = self.get_flow(node_id, table_id, flow_id, priority, flow_name, output_port, mod_dmac, ret_pkt, **kwargs) print(flow) self.write_flow(flow) return flow
class OFManager(object): def __init__(self, ctr_ip=None): self.controller_ip = ctr_ip if ctr_ip else const.DEFAULT_ODL_CONTROLLER_IP self.odlRequest = ODLRestRequest(self.controller_ip) def write_flow(self, flowbody): return self.odlRequest.config_openflow(self.controller_ip, flowbody) def get_input_elem(self, doc): input = doc.createElement('input') input.setAttribute("xmlns","urn:opendaylight:flow:service") doc.appendChild(input) return input def get_node_elem(self, node_id, doc): node = doc.createElement("node") node.setAttribute("xmlns:inv","urn:opendaylight:inventory") node_id = doc.createTextNode(node_id) node.appendChild(node_id) return node def get_table_elem(self, table_id, doc): table = doc.createElement("table_id") table_id = doc.createTextNode(str(table_id)) table.appendChild(table_id) return table def get_priority_elem(self, priority, doc): priority_node = doc.createElement("priority") prio_value = doc.createTextNode(str(priority)) priority_node.appendChild(prio_value) return priority_node def convert_dict_to_xml(self, dict_obj, doc): node_list = [] for key, value in dict_obj.iteritems(): key_node = doc.createElement(key) if isinstance(value, dict): value_nodes = self.convert_dict_to_xml(value,doc) for node in value_nodes: key_node.appendChild(node) elif isinstance(value, list): value_nodes = self.get_list_node(value, doc) for node in value_nodes: key_node.appendChild(node) else: value = str(value) value_node = doc.createTextNode(str(value)) key_node.appendChild(value_node) node_list.append(key_node) return node_list def get_flow_match(self, match, doc): match_nodes = self.convert_dict_to_xml(match, doc) return match_nodes def get_list_node(self, value_list, doc): node_list = [] for value in value_list: if isinstance(value, str) or isinstance(value, int): value_node = doc.createTextNode(value) node_list.append(value_node) elif isinstance(value,dict): value_nodes = self.convert_dict_to_xml(value, doc) node_list.extend(value_nodes) return node_list def convert_flow_toxml(self, node_id, table_id, priority, matches, action_list): doc = Document() input = self.get_input_elem(doc) node_id = "/inv:nodes/inv:node[inv:id=\"%s\"]"%node_id node = self.get_node_elem(node_id, doc) input.appendChild(node) table = self.get_table_elem(table_id,doc) input.appendChild(table) priority = self.get_priority_elem(priority, doc) input.appendChild(priority) match = self.get_flow_match(matches, doc) for node in match: input.appendChild(node) instructions = doc.createElement("instructions") instruction = doc.createElement("instruction") appl_action = doc.createElement("apply-actions") appl_action_order = doc.createElement("order") appl_acion_order_value = doc.createTextNode("0") appl_action_order.appendChild(appl_acion_order_value) action_list_nodes = self.convert_dict_to_xml({"action":action_list}, doc) for action_node in action_list_nodes: appl_action.appendChild(action_node) instruction.appendChild(appl_action_order) instruction.appendChild(appl_action) instructions.appendChild(instruction) input.appendChild(instructions) return input,doc def get_flow(self, node_id, table_id, priority=0, flow_id = None,flow_name=None, output_port=None, mod_dmac=None, ret_pkt=False, **kwargs): action_list = [] if output_port: action_list.append(ofutils.set_output_action(output_port)) if mod_dmac: action_list.append(ofutils.set_mod_dmac_action(mod_dmac)) if ret_pkt: action_list.append(ofutils.set_return_pkt_action()) if not flow_id: flow_id = 1 flow = {"input": { "transaction-uri":"odlTraining8", "flow-ref":ofutils.set_flow_ref(node_id,table_id, flow_id), "node":ofutils.set_node(node_id), "flow-table":ofutils.set_flow_table(node_id, table_id), "flow":{ "priority":priority, "table_id":table_id, "flow-name":flow_name if flow_name else ofutils.generate_flow_name(node_id,table_id,flow_id), "match":ofutils.set_match_field(**kwargs), "instructions": ofutils.set_actions(action_list) } } } return flow def construct_flow(self, node_id, table_id, priority=0, flow_name=None, output_port=None, mod_dmac=None, ret_pkt=False, **kwargs): flow = self.get_flow(node_id, table_id, priority, flow_name=flow_name, output_port=output_port, mod_dmac=mod_dmac, ret_pkt=ret_pkt, **kwargs) matches = {"match": flow["input"]["flow"]["match"]} action_list =flow["input"]["flow"]["instructions"]["instruction"][0]["apply-actions"]["action"] flow_xml,doc = self.convert_flow_toxml(node_id, table_id, priority, matches, action_list) flow_body = doc.toprettyxml() print flow_body self.write_flow(flow_body) return flow_body def construct_flow_to_json(self, node_id, table_id, flow_id, priority=0, flow_name=None,output_port=None, mod_dmac=None, ret_pkt=False,**kwargs): flow = self.get_flow(node_id, table_id, flow_id, priority, flow_name, output_port, mod_dmac, ret_pkt, **kwargs) print(flow) self.write_flow(flow) return flow
class ODLRscGetter(object): def __init__(self, ctr_ip=None): self.controller_ip = ctr_ip if ctr_ip else const.DEFAULT_ODL_CONTROLLER_IP self.odlRequest = ODLRestRequest(self.controller_ip) def generateSwithNode(self, ofNode, node_id): node_id = node_id node_mac = ofNode["flow-node-inventory:hardware-address"] node_name = ofNode["flow-node-inventory:name"].partition("-")[0] node_type = const.SWITCH node = OFNodeBase() node.set_attributes(id=node_id, name=node_name, mac=node_mac, type=node_type) return node def generateHostNode(self, ofNode): node_mac = ofNode["address-tracker:addresses"][0]["mac"] node_ip = str(ofNode["address-tracker:addresses"][0]["ip"]) node_name = "host:" + node_ip node_type = const.HOST node_profile = "ip:" + node_ip node_id = "host:" + node_mac node = OFNodeBase() node.set_attributes(id=node_id, name=node_name, mac=node_mac, type=node_type, profile=node_profile) return node def getOFNodes(self): ofnode_configs = {} node_list = self.odlRequest.list_openflow_nodes() for ofnode in node_list: group_node = ofnode["node-connector"] node_id = ofnode["id"] ofnode_configs.update({node_id: {"host": []}}) for node in group_node: node_type = node["flow-node-inventory:port-number"] if node_type == "LOCAL": switch = self.generateSwithNode(node, node_id) ofnode_configs[node_id].update({const.SWITCH: switch}) elif node.get("address-tracker:addresses", None): host = self.generateHostNode(node) ofnode_configs[node_id][const.HOST].append(host) else: continue return ofnode_configs def _get_ofnode_no(self, node): return node.split(":")[1] def _get_flow_id(self, lnk_src_node, lnk_dst_node): return "openflow:" + self._get_ofnode_no( lnk_src_node) + ":" + self._get_ofnode_no(lnk_dst_node) def _generate_links(self, lnk): lnk_src_port = lnk["source"]["source-tp"] lnk_src_node = lnk["source"]["source-node"] lnk_dst_port = lnk["destination"]["dest-tp"] lnk_dst_node = lnk["destination"]["dest-node"] if lnk_src_node.find("openflow") >= 0 and lnk_dst_node.find( "openflow") >= 0: lnk_id = self._get_flow_id(lnk_src_node, lnk_dst_node) rev_lnk_id = self._get_flow_id(lnk_dst_node, lnk_src_node) else: lnk_id = lnk["link-id"] src, splitor, dst = lnk["link-id"].partition("/") rev_lnk_id = "/".join([dst, src]) of_link = OfLink() of_link.set_attributes(link_id=lnk_id, srcnode=lnk_src_node, dstnode=lnk_dst_node, srcport=lnk_src_port, dstport=lnk_dst_port) rvs_link = OfLink() rvs_link.set_attributes(link_id=rev_lnk_id, srcnode=lnk_dst_node, dstnode=lnk_src_node, srcport=lnk_dst_port, dstport=lnk_src_port) return of_link, rvs_link def _is_oflink_exist(self, of_links, lnk): for link in of_links: if link.snode == lnk.snode and link.dnode == lnk.dnode: return True return False def getOFLinks(self, expect_count): of_links = [] retry = 0 while (retry < MAX_RETRY_TIMES): topology = self.odlRequest.get_openflow_topology() if topology.get("link", []): links = topology["link"] print("getOFLinks: get link count: " + str(len(links))) if len(links) < expect_count: print("OFlinks got less then expect") continue for lnk in links: of_link, rev_link = self._generate_links(lnk) if not self._is_oflink_exist(of_links, of_link): of_links.append(of_link) if not self._is_oflink_exist(of_links, rev_link): of_links.append(rev_link) return of_links time.sleep(0.5) def countSw(self): node_list = self.odlRequest.list_openflow_nodes() return len(node_list) def _getAllOFSwitches(self, ofnode_configs): switches = [] for cfg in ofnode_configs.values(): switch = cfg.get(const.SWITCH, None) if switch: switches.append(switch) return switches def _getAllHosts(self, ofnode_configs): hosts = [] for cfg in ofnode_configs.values(): host_list = cfg.get(const.HOST, []) if host_list: hosts.extend(host_list) return hosts
class ODLRscGetter(object): def __init__(self, ctr_ip=None): self.controller_ip = ctr_ip if ctr_ip else const.DEFAULT_ODL_CONTROLLER_IP self.odlRequest = ODLRestRequest(self.controller_ip) def generateSwithNode(self, ofNode, node_id): node_id = node_id node_mac = ofNode["flow-node-inventory:hardware-address"] node_name = ofNode["flow-node-inventory:name"].partition("-")[0] node_type = const.SWITCH node = OFNodeBase() node.set_attributes(id=node_id,name=node_name, mac=node_mac, type=node_type) return node def generateHostNode(self, ofNode): node_mac = ofNode["address-tracker:addresses"][0]["mac"] node_ip = str(ofNode["address-tracker:addresses"][0]["ip"]) node_name = "host:"+node_ip node_type = const.HOST node_profile = "ip:"+node_ip node_id = "host:"+node_mac node = OFNodeBase() node.set_attributes(id=node_id,name=node_name, mac=node_mac, type=node_type, profile=node_profile) return node def getOFNodes(self): ofnode_configs = {} node_list = self.odlRequest.list_openflow_nodes() for ofnode in node_list: group_node = ofnode["node-connector"] node_id = ofnode["id"] ofnode_configs.update({node_id:{"host":[]}}) for node in group_node: node_type = node["flow-node-inventory:port-number"] if node_type == "LOCAL": switch = self.generateSwithNode(node, node_id) ofnode_configs[node_id].update({const.SWITCH:switch}) elif node.get("address-tracker:addresses", None): host = self.generateHostNode(node) ofnode_configs[node_id][const.HOST].append(host) else: continue return ofnode_configs def _get_ofnode_no(self, node): return node.split(":")[1] def _get_flow_id(self, lnk_src_node, lnk_dst_node): return "openflow:"+self._get_ofnode_no(lnk_src_node) + ":" + self._get_ofnode_no(lnk_dst_node) def _generate_links(self, lnk): lnk_src_port = lnk["source"]["source-tp"] lnk_src_node = lnk["source"]["source-node"] lnk_dst_port = lnk["destination"]["dest-tp"] lnk_dst_node = lnk["destination"]["dest-node"] if lnk_src_node.find("openflow") >= 0 and lnk_dst_node.find("openflow")>=0 : lnk_id = self._get_flow_id(lnk_src_node, lnk_dst_node) rev_lnk_id = self._get_flow_id(lnk_dst_node, lnk_src_node) else: lnk_id = lnk["link-id"] src, splitor, dst = lnk["link-id"].partition("/") rev_lnk_id = "/".join([dst, src]) of_link = OfLink() of_link.set_attributes(link_id=lnk_id,srcnode=lnk_src_node,dstnode=lnk_dst_node,srcport=lnk_src_port, dstport=lnk_dst_port) rvs_link = OfLink() rvs_link.set_attributes(link_id=rev_lnk_id,srcnode=lnk_dst_node,dstnode=lnk_src_node,srcport=lnk_dst_port, dstport=lnk_src_port) return of_link, rvs_link def _is_oflink_exist(self, of_links, lnk): for link in of_links: if link.snode == lnk.snode and link.dnode == lnk.dnode: return True return False def getOFLinks(self, expect_count): of_links =[] retry = 0 while(retry < MAX_RETRY_TIMES): topology = self.odlRequest.get_openflow_topology() if topology.get("link", []): links = topology["link"] print("getOFLinks: get link count: " + str(len(links))) if len(links) < expect_count: print("OFlinks got less then expect") continue for lnk in links: of_link, rev_link = self._generate_links(lnk) if not self._is_oflink_exist(of_links, of_link): of_links.append(of_link) if not self._is_oflink_exist(of_links, rev_link): of_links.append(rev_link) return of_links time.sleep(0.5) def countSw(self): node_list = self.odlRequest.list_openflow_nodes() return len(node_list) def _getAllOFSwitches(self, ofnode_configs): switches = [] for cfg in ofnode_configs.values(): switch = cfg.get(const.SWITCH, None) if switch: switches.append(switch) return switches def _getAllHosts(self, ofnode_configs): hosts = [] for cfg in ofnode_configs.values(): host_list = cfg.get(const.HOST,[]) if host_list: hosts.extend(host_list) return hosts