-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathp4_read_topo.py
executable file
·48 lines (46 loc) · 1.32 KB
/
p4_read_topo.py
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
import json
def read_topo():
data = {}
with open("./topo/topology.json", "r") as f:
topo = json.load(f)
nodes = []
edges = []
i = 0
for host in topo["hosts"].items():
i += 1
nodes.append({
"id": i,
"label": host[0],
"ip": host[1]["ip"],
"mac": host[1]["mac"],
"image": "imgPC",
"shape": 'image'
})
for sw in topo["switches"]:
i += 1
nodes.append({
"id": i,
"label": sw,
"image": "imgRouter",
"shape": 'image'
})
for link in topo["links"]:
for j in range(len(link)):
if isinstance(link[j], str):
if link[j][0] == "h":
link[j] += "-eth0"
elif link[j][0] == "s":
link[j] = link[j].replace("p", "eth")
elif isinstance(link[j], int):
pass
i += 1
edges.append({
"id": i,
"from": link[0],
"to": link[1]
})
data = {"nodes":nodes, "edges":edges}
print(data)
return data
if __name__ == "__main__":
read_topo()