def __init__(self, info): self.namespaces = None self.path = None if isinstance(info, dict): p1 = '#text' p2 = '@xmlns' try: path = info[p1] namespaces = [] for k, v in info.items(): if k.startswith(p2): pfx = yang_nsname_to_prefix(v) d = {'ns': v, 'pfx': pfx} namespaces.append(d) nickname = k.split(':')[-1] path = path.replace(nickname, pfx) self.namespaces = namespaces self.path = path except: msg = "DEBUG: failed to process info '%s'" % info dbg_print(msg) elif isinstance(info, basestring): self.path = info else: msg = "DEBUG: info=%s, " \ "unexpected data format '%s'" % (info, type(info)) dbg_print(msg)
def __init__(self, event): p = 'path' if isinstance(event, dict): for k, v in event.items(): if k == p: self.path_info = PathInfo(v) else: setattr(self, k, v) else: msg = ("DEBUG: event=%s, unexpected data format '%s'" % (event, type(event))) dbg_print(msg)
def __init__(self, event): self.added_switches = [] self.removed_switches = [] self.added_hosts = [] self.removed_hosts = [] self.added_links = [] self.removed_links = [] d = xmltodict.parse(event) try: p1 = 'notification' notification = d[p1] p2 = 'eventTime' self.timestamp = notification[p2] self.events = [] p3 = 'data-changed-notification' p4 = 'data-change-event' events = notification[p3][p4] if isinstance(events, list): for item in events: tc_evt = TopoChangeEvent(item) self.events.append(tc_evt) elif isinstance(events, dict): tc_evt = TopoChangeEvent(events) self.events.append(tc_evt) else: msg = ("DEBUG: events=%s, unexpected data format '%s'" % (events, type(events))) dbg_print(msg) for event in self.events: if event.created(): if event.is_switch(): self.added_switches.append(event.get_node_id()) elif event.is_host(): self.added_hosts.append(event.get_node_id()) elif event.is_link(): self.added_links.append(event.get_link_id()) elif event.deleted(): if event.is_switch(): self.removed_switches.append(event.get_node_id()) elif event.is_host(): self.removed_hosts.append(event.get_node_id()) elif event.is_link(): self.removed_links.append(event.get_link_id()) except(Exception): msg = "DEBUG: failed to process event '%s'" % event dbg_print(msg)
def __init__(self, event): self.added_switches = [] self.removed_switches = [] self.added_hosts = [] self.removed_hosts = [] self.added_links = [] self.removed_links = [] d = xmltodict.parse(event) try: p1 = 'notification' notification = d[p1] p2 = 'eventTime' self.timestamp = notification[p2] self.events = [] p3 = 'data-changed-notification' p4 = 'data-change-event' events = notification[p3][p4] if isinstance(events, list): for item in events: tc_evt = TopoChangeEvent(item) self.events.append(tc_evt) elif isinstance(events, dict): tc_evt = TopoChangeEvent(events) self.events.append(tc_evt) else: msg = ("DEBUG: events=%s, unexpected data format '%s'" % (events, type(events))) dbg_print(msg) for event in self.events: if event.created(): if event.is_switch(): self.added_switches.append(event.get_node_id()) elif event.is_host(): self.added_hosts.append(event.get_node_id()) elif event.is_link(): self.added_links.append(event.get_link_id()) elif event.deleted(): if event.is_switch(): self.removed_switches.append(event.get_node_id()) elif event.is_host(): self.removed_hosts.append(event.get_node_id()) elif event.is_link(): self.removed_links.append(event.get_link_id()) except (Exception): msg = "DEBUG: failed to process event '%s'" % event dbg_print(msg)
def __init__(self, event): self.added_nodes = [] self.removed_nodes = [] self.added_flows = [] self.removed_flows = [] d = xmltodict.parse(event) try: p1 = 'notification' notification = d[p1] p2 = 'eventTime' self.timestamp = notification[p2] self.events = [] p3 = 'data-changed-notification' p4 = 'data-change-event' events = notification[p3][p4] if isinstance(events, list): for item in events: evt = InventoryChangeEvent(item) self.events.append(evt) elif isinstance(events, dict): evt = InventoryChangeEvent(events) self.events.append(evt) else: msg = ("DEBUG: events=%s, unexpected data format '%s'" % (events, type(events))) dbg_print(msg) for event in self.events: if event.created(): if event.is_node(): self.added_nodes.append(event.get_node_id()) elif event.is_flow_entry(): flow_info = FlowInfo(event) self.added_flows.append(flow_info) elif event.deleted(): if event.is_node(): self.removed_nodes.append(event.get_node_id()) elif event.is_flow_entry(): flow_info = FlowInfo(event) self.removed_flows.append(flow_info) except(Exception) as e: print "Error, %s" % e
def __init__(self, event): self.added_nodes = [] self.removed_nodes = [] self.added_flows = [] self.removed_flows = [] d = xmltodict.parse(event) try: p1 = 'notification' notification = d[p1] p2 = 'eventTime' self.timestamp = notification[p2] self.events = [] p3 = 'data-changed-notification' p4 = 'data-change-event' events = notification[p3][p4] if isinstance(events, list): for item in events: evt = InventoryChangeEvent(item) self.events.append(evt) elif isinstance(events, dict): evt = InventoryChangeEvent(events) self.events.append(evt) else: msg = ("DEBUG: events=%s, unexpected data format '%s'" % (events, type(events))) dbg_print(msg) for event in self.events: if event.created(): if event.is_node(): self.added_nodes.append(event.get_node_id()) elif event.is_flow_entry(): flow_info = FlowInfo(event) self.added_flows.append(flow_info) elif event.deleted(): if event.is_node(): self.removed_nodes.append(event.get_node_id()) elif event.is_flow_entry(): flow_info = FlowInfo(event) self.removed_flows.append(flow_info) except (Exception) as e: print "Error, %s" % e
def __init__(self, event): self.node_id = None self.table_id = None self.flow_id = None if isinstance(event, InventoryChangeEvent) and event.is_flow_entry(): path = event.get_path() try: chunks = path.split('/') l = [] p = ':id=' for s in chunks: idx = s.find(p) if idx >= 0: l.append(s[idx + len(p):].translate(None, "'[]")) self.node_id = l[0] self.table_id = l[1] self.flow_id = l[2] except(Exception): msg = "DEBUG: unexpected string format: %s" % path dbg_print(msg) else: msg = "wrong class usage" dbg_print(msg)
def __init__(self, event): self.node_id = None self.table_id = None self.flow_id = None if isinstance(event, InventoryChangeEvent) and event.is_flow_entry(): path = event.get_path() try: chunks = path.split('/') l = [] p = ':id=' for s in chunks: idx = s.find(p) if idx >= 0: l.append(s[idx + len(p):].translate(None, "'[]")) self.node_id = l[0] self.table_id = l[1] self.flow_id = l[2] except (Exception): msg = "DEBUG: unexpected string format: %s" % path dbg_print(msg) else: msg = "wrong class usage" dbg_print(msg)
def add_flow(self, options): parser = argparse.ArgumentParser( prog=self.prog, description="Add flow entries to the Controller's cache", usage="%(prog)s add-flow -s=SWITCHID|--switch=SWICTHID\n" " -f <path>|--file <path>\n" " [--dry-run]\n" "\n\n" "Add flow entries to the Controller's cache\n\n" "\n\n" "Options:\n" " -s, --switch switch identifier\n" " -f, --file path to the file containing flow entries\n" " (default is './flow.json')\n" " -dry-run show content of flow(s) to be created" ) parser.add_argument('-s', '--switch', metavar = "SWITCHID") parser.add_argument('-f', '--file', metavar="<path>", dest='flow_file', help="path to the file containing flow entries " "(default is './flow.json')", default="./flow.json") parser.add_argument('-U', action="store_true", dest="usage", help=argparse.SUPPRESS) parser.add_argument('--dry-run', action="store_true", dest='dry_run', default=False) args = parser.parse_args(options) if(args.usage): parser.print_usage() print "\n".strip() return if(args.dry_run): flows = self.read_flows(args.flow_file) if flows: for flow in flows: print json.dumps(flow, indent=4) return if (args.switch == None): msg = "option -s (or --switch) is required" parser.error(msg) flows = self.read_flows(args.flow_file) if not flows: print "Failed to execute command, exit" exit(1) print "\n".strip() print " [Controller '%s']" % self.ctrl_cfg.to_string() print "\n".strip() ctrl = Controller(self.ctrl_cfg.ip_addr, self.ctrl_cfg.tcp_port, self.ctrl_cfg.admin_name, self.ctrl_cfg.admin_pswd) ofswitch = OFSwitch(ctrl, args.switch) try: for flow in flows: fid = flow['id'] tid = flow['table_id'] js = json.dumps(flow, default=lambda o: o.__dict__) result = ofswitch.add_modify_flow_json(table_id=tid, flow_id=fid,flow_json=js) status = result.get_status() if(status.eq(STATUS.OK) == True): print "Flow id '%s', success" % fid else: print "Flow id '%s', failure, reason: %s" % (fid, status.detailed()) print "\n".strip() except(Exception) as e: msg = "Error: %s" % repr(e) dbg_print(msg)