Permalink
Browse files

Added two REST to pdf utilities: graphDeps and graphTopo

also added a README file documenting the exmaples.
  • Loading branch information...
1 parent 8443d51 commit 498a5da429a316fe6aa89e9d5b94b283dd168541 @capveg capveg committed Mar 8, 2012
Showing with 177 additions and 0 deletions.
  1. +28 −0 example/README
  2. +72 −0 example/graphDeps.py
  3. +77 −0 example/graphTopo.py
View
@@ -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.
+
View
@@ -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))
+
View
@@ -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))
+

0 comments on commit 498a5da

Please sign in to comment.