def delete_flow(node_id, table_id, flow_id): credentials = (odl_user, odl_pass) odl = ODLInstance(odl_server, credentials) data = json.loads(flask.request.get_data().decode('utf-8')) delete_all = data['delete_all'] try: # Get the node object node = odl.get_node_by_id(node_id) # Get the table object table = node.get_table_by_id(table_id) # Get the flow flow = table.get_all_flows()[flow_id] except (NodeNotFound, TableNotFound, FlowNotFound) as e: flask.abort(404) if delete_all: nodes = odl.get_nodes() for node in nodes.values(): node.delete_config_flows_by_name(flow.name) else: flow.delete() return flask.redirect("/")
def path_stats(node_id, table_id, clean_flow_id, diff): credentials = (odl_user, odl_pass) odl = ODLInstance(odl_server, credentials) try: node = odl.get_node_by_id(node_id) table = node.get_table_by_id(table_id) flow = table.get_flow_by_clean_id(clean_flow_id) except (NodeNotFound, TableNotFound, FlowNotFound) as e: return flask.abort(404) nodes = odl.get_nodes() data = []; eth_type = flow.get_ethernet_type() eth_src = flow.get_ethernet_source() eth_dest = flow.get_ethernet_destination() ipv4_src = flow.get_ipv4_source() ipv4_dest = flow.get_ipv4_destination() if eth_type == 0x0800 and (eth_src != '*' or eth_dest != '*' or ipv4_src != '*' or ipv4_dest != '*'): path = explore_path(nodes, flow, eth_type, eth_src, eth_dest, ipv4_src, ipv4_dest) for n in path: ndata = { 'id': n['id'], 'main': get_rrd_stats(n['id'], n['main']['table_id'], n['main']['clean_flow_id'], diff) } if n.has_key('oposite'): ndata['oposite'] = get_rrd_stats(n['id'], n['oposite']['table_id'], n['oposite']['clean_flow_id'], diff) data.append(ndata) else: data.append({'id': node.id, 'main': get_rrd_stats(node.id, table.id, flow.clean_id, diff)}) return flask.jsonify(data)
def all_stats(): """ Get all statistics information for each node """ credentials = (odl_user, odl_pass) odl = ODLInstance(odl_server, credentials) nodes = odl.get_nodes() all_stats = {} for node in nodes.values(): ports = node.get_connectors() for port in ports.values(): connector = port.id stat = get_port_rrd_stats(node.id, port.id, 120) rates = map(lambda x: x['bytes'], stat) rate = sum(rates) / len(rates) if rates else 0 all_stats[connector] = rate # tables = node.get_tables() # for table in tables.values(): # for flow in table.get_all_flows().values(): # for action in flow.get_actions(): # if action['type'] == 'output-action': # connector = node.id + ":" + action['value'] # stat = get_rrd_stats(node.id, table.id, flow.clean_id, 120) # rates = map(lambda x: x['bytes'], stat) # rate = sum(rates) / len(rates) if rates else 0 # all_stats[connector] = all_stats.get(connector, 0) + rate return flask.jsonify(all_stats)
def delete_flow(node_id, table_id, flow_id): credentials = (odl_user, odl_pass) odl = ODLInstance(odl_server, credentials) data = json.loads(flask.request.get_data().decode('utf-8')) delete_all = data['delete_all'] try: # Get the node object node = odl.get_node_by_id(node_id) # Get the table object table = node.get_table_by_id(table_id) # Get the flow flow = table.get_all_flows()[flow_id] except (NodeNotFound, TableNotFound, FlowNotFound) as e: flask.abort(404) if delete_all: nodes = odl.get_nodes() for node in nodes.values(): node.delete_config_flows_by_name(flow.name) else: flow.delete() return flask.redirect("/")
def flow_stats_all(name, diff): credentials = (odl_user, odl_pass) odl = ODLInstance(odl_server, credentials) nodes = odl.get_nodes() stats = {} for node in nodes.values(): tables = node.get_tables() node_stats = {} for table in tables.values(): flows = table.get_config_flows_by_name(name) for flow in flows: node_stats[flow.clean_id] = get_rrd_stats(node.id, table.id, flow.clean_id, diff) if node_stats: stats[node.id] = node_stats return flask.jsonify(stats)
def flow_stats_all(name, diff): credentials = (odl_user, odl_pass) odl = ODLInstance(odl_server, credentials) nodes = odl.get_nodes() stats = {} for node in nodes.values(): tables = node.get_tables() node_stats = {} for table in tables.values(): flows = table.get_config_flows_by_name(name) for flow in flows: node_stats[flow.clean_id] = get_rrd_stats( node.id, table.id, flow.clean_id, diff) if node_stats: stats[node.id] = node_stats return flask.jsonify(stats)
def path_stats(node_id, table_id, clean_flow_id, diff): credentials = (odl_user, odl_pass) odl = ODLInstance(odl_server, credentials) try: node = odl.get_node_by_id(node_id) table = node.get_table_by_id(table_id) flow = table.get_flow_by_clean_id(clean_flow_id) except (NodeNotFound, TableNotFound, FlowNotFound) as e: return flask.abort(404) nodes = odl.get_nodes() data = [] eth_type = flow.get_ethernet_type() eth_src = flow.get_ethernet_source() eth_dest = flow.get_ethernet_destination() ipv4_src = flow.get_ipv4_source() ipv4_dest = flow.get_ipv4_destination() if eth_type == 0x0800 and (eth_src != '*' or eth_dest != '*' or ipv4_src != '*' or ipv4_dest != '*'): path = explore_path(nodes, flow, eth_type, eth_src, eth_dest, ipv4_src, ipv4_dest) for n in path: ndata = { 'id': n['id'], 'main': get_rrd_stats(n['id'], n['main']['table_id'], n['main']['clean_flow_id'], diff) } if n.has_key('oposite'): ndata['oposite'] = get_rrd_stats(n['id'], n['oposite']['table_id'], n['oposite']['clean_flow_id'], diff) data.append(ndata) else: data.append({ 'id': node.id, 'main': get_rrd_stats(node.id, table.id, flow.clean_id, diff) }) return flask.jsonify(data)
def all_stats(): """ Get all statistics information for each node """ credentials = (odl_user, odl_pass) odl = ODLInstance(odl_server, credentials) nodes = odl.get_nodes() all_stats = {} for node in nodes.values(): tables = node.get_tables() for table in tables.values(): for flow in table.get_all_flows().values(): for action in flow.get_actions(): if action['type'] == 'output-action': connector = node.id + ":" + action['value'] stat = get_rrd_stats(node.id, table.id, flow.clean_id, 120) rates = map(lambda x: x['bytes'], stat) rate = sum(rates) / len(rates) if rates else 0 all_stats[connector] = all_stats.get(connector, 0) + rate return flask.jsonify(all_stats)
def delete_flow(node_id, table_id, flow_id): credentials = (odl_user, odl_pass) odl = ODLInstance(odl_server, credentials) delete_all = flask.request.form.get('delete_all') try: # Get the node object node = odl.get_node_by_id(node_id) # Get the table object table = node.get_table_by_id(table_id) # Get the flow flow = table.get_flow_by_id(flow_id) except (NodeNotFound, TableNotFound, FlowNotFound) as e: flask.abort(404) if delete_all: nodes = odl.get_nodes() for node in nodes.values(): node.delete_config_flows_by_name(flow.name) else: flow.delete() return flask.redirect("/")
pw = args.get("<password>") if not pw: pw = "admin" ffile = args.get("<flow_file>") info =\ """File : %s Server: %s User : %s Passwd: %s\n""" % (ffile, url, user, "*****" if pw != "admin" else pw) print info try: f = open(ffile, 'r') fstr = f.read() flows = json.loads(fstr) except Exception, e: print "Error: %s" % e odl = ODLInstance(url, (user, pw)) nodes = odl.get_nodes() for flow in flows: sw = flow['switch'] tid = flow['flow']['table_id'] tables = nodes[sw].get_tables() tables[tid].put_flow_from_data_json(json.dumps({"flow": flow['flow']}), flow['id']) f.close()
class ODLCmd(cmd.Cmd): def __init__(self, url, user, pw): self.prompt = col.PROMPT + "odl-cli> " + col.ENDC self.config = {} self.cwc = self.config self.cwd_list = [] self.odl = ODLInstance(url, (user, pw)) self.util = Util() self.node = None self.table = None self.pp = pprint.PrettyPrinter(indent=1, width=80, depth=None, stream=None) cmd.Cmd.__init__(self) def emptyline(self): pass def do_cd(self, path): '''Change the current level of view of the config to be at <key> cd <key>''' if path=="" or path[0]=="/": new_wd_list = path[1:].split("/") else: new_wd_list = self.cwd_list + path.split("/") try: cwc, new_wd_list = self._conf_for_list(new_wd_list) except ConfigurationError as e: print col.FAIL + str(e) + col.ENDC return self.cwd_list = new_wd_list self.cwc = cwc def complete_cd(self, text, l, b, e): return [ x[b-3:] for x,y in self.cwc.iteritems() if x.startswith(l[3:])] def do_ls(self, key): '''Show the top level of the current working config, or top level of config under [key] ls [key]''' conf = self.cwc if key: try: conf = conf[key] except KeyError: print "No such key %s" % key return try: for k,v in conf.iteritems(): if isinstance(v, ODLNode): try: hostname = socket.gethostbyaddr(v.ip_address)[0] except: hostname = v.ip_address print col.DIR + k + col.ENDC + ": (\"%s\", \"%s\", \"%s\")" % \ (hostname, v.description, v.manufacturer) elif isinstance(v, dict) or isinstance(v, list): print col.DIR + k + col.ENDC else: print "%s: %s" % (k, v) except: print "%s" % conf def complete_ls(self, text, l, b, e): return [ x[b-3:] for x,y in self.cwc.iteritems() if x.startswith(l[3:]) ] def do_lsd(self, key): '''Show all config from current level down... or all config under [key] lsd [key]''' conf = self.cwc if key: try: conf = conf[key] except KeyError: print "No such key %s" % key self.pp.pprint(conf) def complete_lsd(self, text, l, b, e): return [ x for x,y in self.cwc.iteritems() if isinstance(y, dict) and x.startswith(text) ] def do_pwd(self, key): '''Show current path in config separated by slashes pwd''' print "/" + "/".join(self.cwd_list) def do_get_nodes(self, args): '''Get all or a specific node from ODL get_nodes [node]''' try: nodes = self.odl.get_nodes() self.config = nodes self._set_cwc() except Exception as e: print "Error: %s" % e def do_get_topo(self, args): '''Get ODL topology''' try: t = ODLTopology(None, None, self.odl) topo = t.get_topology() self.config = topo self._set_cwc() except Exception as e: print "Error: %s" % e def do_del_flow(self, fid): try: if not self.cwd_list[-3] == "tables": raise except: print col.FAIL + "Not in a valid table leaf!" + col.ENDC return if not fid: print col.FAIL + "Must specify flow id or '*'" + col.ENDC return table = self.cwd_list[-2] if fid == "*": yn = self.util.query_yes_no("Delete ALL flows in table %s" % (table)) if yn: flows = self.tables[int(table)].get_config_flows() for f in flows.values(): try: f.delete() except Exception as e: print "Error deleting flow: %s" % e else: return else: yn = self.util.query_yes_no("Delete flow %s in table %s" % (fid, table)) if yn: try: flows = self.tables[int(table)].get_config_flows() flows[fid].delete() except Exception as e: print "Error deleting flow: %s" % e else: return self.do_update(None) self.do_get_nodes(None) def complete_del_flow(self, text, l, b, e): return [ x[b-9:] for x,y in self.cwc.iteritems() if x.startswith(l[9:]) ] def do_add_flow(self, args): '''Add a flow, will prompt for user input''' if not self.node: print col.FAIL + "No ODL node selected!" + col.ENDC return file_path = self.util.get_string("Input file: ", None) if not file_path: print "You must include a json file containing the ODL flow(s)" return try: with open(file_path, 'r') as in_file: raw_json = in_file.read() flow_requests = json.loads(raw_json) except IOError, e: print "Could not open file %s" % file_path return except ValueError, e: print "Invalid json formatting in request" return
class ODLCmd(cmd.Cmd): def __init__(self, url, user, pw): self.prompt = col.PROMPT + "odl-cli> " + col.ENDC self.config = {} self.cwc = self.config self.cwd_list = [] self.odl = ODLInstance(url, (user, pw)) self.util = Util() self.node = None self.table = None self.pp = pprint.PrettyPrinter(indent=1, width=80, depth=None, stream=None) cmd.Cmd.__init__(self) def emptyline(self): pass def do_cd(self, path): '''Change the current level of view of the config to be at <key> cd <key>''' if path == "" or path[0] == "/": new_wd_list = path[1:].split("/") else: new_wd_list = self.cwd_list + path.split("/") try: cwc, new_wd_list = self._conf_for_list(new_wd_list) except ConfigurationError as e: print col.FAIL + str(e) + col.ENDC return self.cwd_list = new_wd_list self.cwc = cwc def complete_cd(self, text, l, b, e): return [ x[b - 3:] for x, y in self.cwc.iteritems() if x.startswith(l[3:]) ] def do_ls(self, key): '''Show the top level of the current working config, or top level of config under [key] ls [key]''' conf = self.cwc if key: try: conf = conf[key] except KeyError: print "No such key %s" % key return try: for k, v in conf.iteritems(): if isinstance(v, ODLNode): try: hostname = socket.gethostbyaddr(v.ip_address)[0] except: hostname = v.ip_address print col.DIR + k + col.ENDC + ": (\"%s\", \"%s\", \"%s\")" % \ (hostname, v.description, v.manufacturer) elif isinstance(v, dict) or isinstance(v, list): print col.DIR + k + col.ENDC else: print "%s: %s" % (k, v) except: print "%s" % conf def complete_ls(self, text, l, b, e): return [ x[b - 3:] for x, y in self.cwc.iteritems() if x.startswith(l[3:]) ] def do_lsd(self, key): '''Show all config from current level down... or all config under [key] lsd [key]''' conf = self.cwc if key: try: conf = conf[key] except KeyError: print "No such key %s" % key self.pp.pprint(conf) def complete_lsd(self, text, l, b, e): return [ x for x, y in self.cwc.iteritems() if isinstance(y, dict) and x.startswith(text) ] def do_pwd(self, key): '''Show current path in config separated by slashes pwd''' print "/" + "/".join(self.cwd_list) def do_get_nodes(self, args): '''Get all or a specific node from ODL get_nodes [node]''' try: nodes = self.odl.get_nodes() self.config = nodes self._set_cwc() except Exception as e: print "Error: %s" % e def do_get_topo(self, args): '''Get ODL topology''' try: t = ODLTopology(None, None, self.odl) topo = t.get_topology() self.config = topo self._set_cwc() except Exception as e: print "Error: %s" % e def do_del_flow(self, fid): try: if not self.cwd_list[-3] == "tables": raise except: print col.FAIL + "Not in a valid table leaf!" + col.ENDC return if not fid: print col.FAIL + "Must specify flow id or '*'" + col.ENDC return table = self.cwd_list[-2] if fid == "*": yn = self.util.query_yes_no("Delete ALL flows in table %s" % (table)) if yn: flows = self.tables[int(table)].get_config_flows() for f in flows.values(): try: f.delete() except Exception as e: print "Error deleting flow: %s" % e else: return else: yn = self.util.query_yes_no("Delete flow %s in table %s" % (fid, table)) if yn: try: flows = self.tables[int(table)].get_config_flows() flows[fid].delete() except Exception as e: print "Error deleting flow: %s" % e else: return self.do_update(None) self.do_get_nodes(None) def complete_del_flow(self, text, l, b, e): return [ x[b - 9:] for x, y in self.cwc.iteritems() if x.startswith(l[9:]) ] def do_add_flow(self, args): '''Add a flow, will prompt for user input''' if not self.node: print col.FAIL + "No ODL node selected!" + col.ENDC return file_path = self.util.get_string("Input file: ", None) if not file_path: print "You must include a json file containing the ODL flow(s)" return try: with open(file_path, 'r') as in_file: raw_json = in_file.read() flow_requests = json.loads(raw_json) except IOError, e: print "Could not open file %s" % file_path return except ValueError, e: print "Invalid json formatting in request" return
pw = args.get("<password>") if not pw: pw = "admin" ffile = args.get("<flow_file>") info =\ """File : %s Server: %s User : %s Passwd: %s\n""" % (ffile, url, user, "*****" if pw != "admin" else pw) print info try: f = open(ffile, 'r') fstr = f.read() flows = json.loads(fstr) except Exception, e: print "Error: %s" % e odl = ODLInstance(url, (user, pw)) nodes = odl.get_nodes() for flow in flows: sw = flow['switch'] tid = flow['flow']['table_id'] tables = nodes[sw].get_tables() tables[tid].put_flow_from_data_json(json.dumps({"flow": flow['flow']}), flow['id']) f.close()