-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathodl-topo.py
More file actions
62 lines (49 loc) · 1.84 KB
/
odl-topo.py
File metadata and controls
62 lines (49 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import json
import networkx as nx
from networkx.readwrite import json_graph
import httplib2
baseUrl = 'http://localhost:8080/controller/nb/v2/'
containerName = 'default/'
h = httplib2.Http(".cache")
h.add_credentials('admin', 'admin')
# Get all the edges/links
resp, content = h.request(baseUrl + 'topology/' + containerName, "GET")
edgeProperties = json.loads(content)
odlEdges = edgeProperties['edgeProperties']
# Get all the nodes/switches
resp, content = h.request(baseUrl + 'switchmanager/' + containerName + 'nodes/', "GET")
nodeProperties = json.loads(content)
odlNodes = nodeProperties['nodeProperties']
# Get all the actie hosts
resp, content = h.request(baseUrl + 'hosttracker/' + containerName + 'hosts/active', "GET")
hostProperties = json.loads(content)
hosts = hostProperties["hostConfig"]
# Initialize the graph
graph = nx.Graph()
# Put switches in the graph
for node in odlNodes:
graph.add_node(node['node']['id'])
# Put all the edges between switches
for edge in odlEdges:
e = (edge['edge']['headNodeConnector']['node']['id'], edge['edge']['tailNodeConnector']['node']['id'])
graph.add_edge(*e)
# Put hosts in the graph and the relevant edges
for host in hosts:
graph.add_node(host['networkAddress'])
e = (host['networkAddress'], host['nodeId'])
graph.add_edge(*e)
# Print out graph info as a sanity check
print "Number of nodes add in the graph:", graph.number_of_nodes()
print "Nodes are:", graph.nodes()
print "Paths from 10.0.0.1 -> 10.0.0.3"
asp = nx.all_simple_paths(graph, source="10.0.0.1", target="10.0.0.3")
for p in asp:
print p
print "Paths from 10.0.0.2 -> 10.0.0.4"
asp = nx.all_simple_paths(graph, source="10.0.0.2", target="10.0.0.4")
for p in asp:
print p
# write json formatted data to use in visualization
d = json_graph.node_link_data(graph)
json.dump(d, open('topo.json','w'))
print('Wrote node-link JSON data')