diff --git a/example/README b/example/README new file mode 100644 index 0000000000000000000000000000000000000000..e3e8179f1d3556e2821daea13c2b2cc7d9c8a664 --- /dev/null +++ b/example/README @@ -0,0 +1,28 @@ +One of Floodlight's main goals is extensibility and flexibility. + +To prove that point, this directory includes a number of useful +utilities as examples of what can do with this extensibility. + +UTILITIES: +-------------------------- + +graphDeps.py and graphTopo.py + + Read the module dependencies (graphDeps.py) or the topology + from the REST API and output it in the 'dot' format used by the + popular graphviz (www.graphviz.org) package so that they can + be visualized. + + Example usage: + ./graphTopo.py $hostname # generate .dot file + dot -Tpdf -o $hostname.pdf $hostname.dot # convert to PDF + open $hostname.pdf # open to view topology + + + +packetStreamerClientExample.py + + Example client for the packet streamer server in floodlight. + Allows you to intercept packets from floodlight's packet_in + processing chain and read them. + diff --git a/example/graphDeps.py b/example/graphDeps.py new file mode 100755 index 0000000000000000000000000000000000000000..505d51642b7e180b0b498a75224a3cbc4f991f45 --- /dev/null +++ b/example/graphDeps.py @@ -0,0 +1,72 @@ +#!/usr/bin/python + +import urllib2 +import json +import sys + + +def simple_json_get(url): + return json.loads(urllib2.urlopen(url).read()) + + +def shorten(s): + return s.replace('net.floodlightcontroller','n.f' + ).replace('com.bigswitch','c.b') + +def usage(s): + sys.stderr.write("Usage:\ngrahDeps.py hostname [port]\n%s" % s) + sys.stderr.write("\n\n\n\n writes data to 'hostname.dot' for use with graphviz\n") + sys.exit(1) + + +if __name__ == '__main__': + + host='localhost' + port=8080 + + if len(sys.argv) == 1 or sys.argv[1] == '-h' or sys.argv[1] == '--help': + usage("need to specify hostname") + + host = sys.argv[1] + if len(sys.argv) > 2: + port = int(sys.argv[2]) + + sys.stderr.write("Connecting to %s:%d ..." % (host,port)) + URL="http://%s:%d/wm/core/module/loaded/json" % (host,port) + + deps = simple_json_get(URL) + serviceMap = {} + nodeMap = {} + nodeCount = 0 + + sys.stderr.write("Writing to %s.dot ..." % (host)) + f = open("%s.dot" % host, 'w') + + f.write( "digraph Deps {\n") + + for mod, info in deps.iteritems(): + # sys.stderr.write("Discovered module %s\n" % mod) + nodeMap[mod] = "n%d" % nodeCount + nodeCount += 1 + label = shorten(mod) + "\\n" + for service, serviceImpl in info['provides'].iteritems(): + # sys.stderr.write(" Discovered service %s implemented with %s\n" % (service,serviceImpl)) + label += "\\nService=%s" % shorten(service) + serviceMap[serviceImpl] = mod + f.write(" %s [ label=\"%s\", color=\"blue\"];\n" % (nodeMap[mod], label)) + + f.write("\n") # for readability + + for mod, info in deps.iteritems(): + for dep, serviceImpl in info['depends'].iteritems(): + f.write(" %s -> %s [ label=\"%s\"];\n" % ( + nodeMap[mod], + shorten(nodeMap[serviceMap[serviceImpl]]), + shorten(dep))) + + + f.write("}\n") + f.close(); + sys.stderr.write("Now type\ndot -Tpdf -o %s.pdf %s.dot\n" % ( + host, host)) + diff --git a/example/graphTopo.py b/example/graphTopo.py new file mode 100755 index 0000000000000000000000000000000000000000..b89a76358bb3157fc46c6577a030730175c28f8e --- /dev/null +++ b/example/graphTopo.py @@ -0,0 +1,77 @@ +#!/usr/bin/python + +import urllib2 +import json +import sys + + +def simple_json_get(url): + return json.loads(urllib2.urlopen(url).read()) + + +def shorten(s): + return s.replace('net.floodlightcontroller','n.f' + ).replace('com.bigswitch','c.b') + +def usage(s): + sys.stderr.write("Usage:\ngrahTopo.py hostname [port]\n%s" % s) + sys.stderr.write("\n\n\n\n writes data to 'hostname.dot' for use with graphviz\n") + sys.exit(1) + + +if __name__ == '__main__': + + host='localhost' + port=8080 + + if len(sys.argv) == 1 or sys.argv[1] == '-h' or sys.argv[1] == '--help': + usage("need to specify hostname") + + host = sys.argv[1] + if len(sys.argv) > 2: + port = int(sys.argv[2]) + + sys.stderr.write("Connecting to %s:%d ..." % (host,port)) + URL="http://%s:%d/wm/topology/links/json" % (host,port) + + # { + # "dst-port": 2, + # "dst-switch": "00:00:00:00:00:00:00:0a", + # "src-port": 3, + # "src-switch": "00:00:00:00:00:00:00:0c" + # } + + links = simple_json_get(URL) + nodeMap = {} + + sys.stderr.write("Writing to %s.dot ..." % (host)) + f = open("%s.dot" % host, 'w') + + f.write( "digraph Deps {\n") + + for link in links: + # sys.stderr.write("Discovered module %s\n" % mod) + if not link['dst-switch'] in nodeMap: + sw = link['dst-switch'] + nodeMap[sw] = "n%d" % len(nodeMap) + f.write(" %s [ label=\"dpid=%s\", color=\"blue\"];\n" % (nodeMap[sw], sw)) + + if not link['src-switch'] in nodeMap: + sw = link['src-switch'] + nodeMap[sw] = "n%d" % len(nodeMap) + f.write(" %s [ label=\"dpid=%s\", color=\"blue\"];\n" % (nodeMap[sw], sw)) + + + f.write(" %s -> %s [ label=\"%s\"];\n" % ( + nodeMap[link['dst-switch']], + nodeMap[link['src-switch']], + "src_port %d --> dst_port %d" % (link['src-port'],link['dst-port']) + ) + ) + + + f.write("}\n") + f.close(); + sys.stderr.write("Now type\ndot -Tpdf -o %s.pdf %s.dot\n" % ( + host, host)) +