blob: b89a76358bb3157fc46c6577a030730175c28f8e [file] [log] [blame]
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001#!/usr/bin/python
2
3import urllib2
4import json
5import sys
6
7
8def simple_json_get(url):
9 return json.loads(urllib2.urlopen(url).read())
10
11
12def shorten(s):
13 return s.replace('net.floodlightcontroller','n.f'
14 ).replace('com.bigswitch','c.b')
15
16def usage(s):
17 sys.stderr.write("Usage:\ngrahTopo.py hostname [port]\n%s" % s)
18 sys.stderr.write("\n\n\n\n writes data to 'hostname.dot' for use with graphviz\n")
19 sys.exit(1)
20
21
22if __name__ == '__main__':
23
24 host='localhost'
25 port=8080
26
27 if len(sys.argv) == 1 or sys.argv[1] == '-h' or sys.argv[1] == '--help':
28 usage("need to specify hostname")
29
30 host = sys.argv[1]
31 if len(sys.argv) > 2:
32 port = int(sys.argv[2])
33
34 sys.stderr.write("Connecting to %s:%d ..." % (host,port))
35 URL="http://%s:%d/wm/topology/links/json" % (host,port)
36
37 # {
38 # "dst-port": 2,
39 # "dst-switch": "00:00:00:00:00:00:00:0a",
40 # "src-port": 3,
41 # "src-switch": "00:00:00:00:00:00:00:0c"
42 # }
43
44 links = simple_json_get(URL)
45 nodeMap = {}
46
47 sys.stderr.write("Writing to %s.dot ..." % (host))
48 f = open("%s.dot" % host, 'w')
49
50 f.write( "digraph Deps {\n")
51
52 for link in links:
53 # sys.stderr.write("Discovered module %s\n" % mod)
54 if not link['dst-switch'] in nodeMap:
55 sw = link['dst-switch']
56 nodeMap[sw] = "n%d" % len(nodeMap)
57 f.write(" %s [ label=\"dpid=%s\", color=\"blue\"];\n" % (nodeMap[sw], sw))
58
59 if not link['src-switch'] in nodeMap:
60 sw = link['src-switch']
61 nodeMap[sw] = "n%d" % len(nodeMap)
62 f.write(" %s [ label=\"dpid=%s\", color=\"blue\"];\n" % (nodeMap[sw], sw))
63
64
65 f.write(" %s -> %s [ label=\"%s\"];\n" % (
66 nodeMap[link['dst-switch']],
67 nodeMap[link['src-switch']],
68 "src_port %d --> dst_port %d" % (link['src-port'],link['dst-port'])
69 )
70 )
71
72
73 f.write("}\n")
74 f.close();
75 sys.stderr.write("Now type\ndot -Tpdf -o %s.pdf %s.dot\n" % (
76 host, host))
77