-
Notifications
You must be signed in to change notification settings - Fork 0
/
topoviewer.py
executable file
·221 lines (189 loc) · 6.29 KB
/
topoviewer.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#!/usr/bin/env python3
# -*- encoding: utf-8 -*-
import sys
import time
import json
import tkinter
import networkx
import traceback
import threading
from pathlib import Path
from graphtools import Dijkstra, graph_from_topo
from sortundirectednodepair import _sort_pair
from networkx.drawing.layout import spring_layout
from networkx.drawing.nx_pylab import draw_networkx
from networkx.drawing.nx_pylab import draw_networkx_labels
from networkx.drawing.nx_pylab import draw_networkx_nodes
from networkx.drawing.nx_pylab import draw_networkx_edges
from networkx.drawing.nx_pylab import draw_networkx_edge_labels
import matplotlib
from matplotlib import pyplot as plt
from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
matplotlib.use("TkAgg")
def nx_graph_from_topo(topo):
g = networkx.Graph()
g.add_nodes_from(topo[0])
g.add_nodes_from(topo[1])
g.add_edges_from([(x, y) for x, y, z in topo[2]])
for h1, h2, bw in topo[2]:
g[h1][h2]['bw'] = bw
return g
def parse_state(s):
pathb, loadb = [], []
current = 0
for line in s.splitlines():
if line.startswith('@'):
if line.startswith('@start.'):
if line.endswith('path'):
current = 1
elif line.endswith('load'):
current = 2
else:
raise ValueError(f"Start of unexpected block: {line}")
elif line.startswith('@end'):
current = 0
else:
raise ValueError(f"Unexpected command: {line}")
elif current == 1:
pathb.append(eval(line))
elif current == 2:
loadb.append(eval(line))
loads, path_segments = dict(), dict()
for multiplepath in pathb:
ws = sum([weight for weight in multiplepath.values()])
for path, weight in multiplepath.items():
for i in range(len(path)-1):
path_segments[_sort_pair(*path[i:i+2])] = weight/ws
for load in loadb:
loads[_sort_pair(load[0], load[1])] = load[2]
return pathb, path_segments, loads
def get_loaded_switches():
pt = Path("~current.sws.state")
if not pt.exists():
return set()
else:
return set(filter(len, pt.read_text().splitlines()))
CLR = {True: 'FF', False: '00'}
topos = dict()
topoPos = dict()
topoDjkt = dict()
nxgs = dict()
def plot(ax, currentfile):
ax.clear()
ax.axis('off')
if not currentfile.exists():
return
toponame = currentfile.read_text().strip()
statepath = Path(f'{toponame}.state')
if toponame not in topos:
topos[toponame] = json.loads(Path(f'{toponame}.json').read_text())
topo = topos[toponame]
if toponame not in nxgs:
nxgs[toponame] = nx_graph_from_topo(topo)
nxg = nxgs[toponame]
if toponame not in topoPos:
topoPos[toponame] = spring_layout(nxg, iterations=2000)
pos = topoPos[toponame]
if toponame not in topoDjkt:
djkt = Dijkstra(graph_from_topo(topo))
djkt_pairs = list()
for h1 in topo[0]:
for h2 in topo[0]:
if h1 != h2:
h1, h2 = _sort_pair(h1, h2)
djkt_seq = djkt(h1)(h2)[0]
for i in range(len(djkt_seq)-1):
djkt_pairs.append(_sort_pair(*djkt_seq[i:i+2]))
topoDjkt[toponame] = djkt_pairs
djkt = topoDjkt[toponame]
state_txt = ""
if statepath.exists():
state_txt = statepath.read_text()
paths, path_segments, loads = parse_state(state_txt)
labels = dict()
for unsortededge in nxg.edges:
edge = _sort_pair(*list(map(str, unsortededge)))
labels[unsortededge] = "%0.4f" % (loads.get(edge, 0.0),)
# labels[unsortededge] = repr(loads.get(edge, 0.0),)
edlab = draw_networkx_edge_labels(
nxg, pos, ax=ax,
edge_labels=labels,
bbox=dict(
facecolor='none',
edgecolor='none'
)
)
loaded_sws = set(topo[1]).intersection(get_loaded_switches())
unloaded_sws = set(topo[1]).difference(loaded_sws)
for unsortededge in nxg.edges:
edge = _sort_pair(*list(map(str, unsortededge)))
in_djkt = edge in djkt
in_sgmt = min(
1.0,
max(
0.0,
path_segments.get(
edge,
path_segments.get(
tuple(reversed(edge)),
0.0
))))
in_sgmt = ('0'+(hex(round(in_sgmt*255))[2:]))[-2:].upper()
in_unld = (edge[0] in unloaded_sws) or (edge[1] in unloaded_sws)
color = '#'+CLR[in_djkt]+CLR[in_unld]+in_sgmt
draw_networkx_edges(
nxg, pos, ax=ax,
edgelist=[edge], edge_color=color
)
draw_networkx_nodes(
nxg, pos, ax=ax,
nodelist=topo[0], node_color='lightgreen'
)
draw_networkx_nodes(
nxg, pos, ax=ax,
nodelist=unloaded_sws, node_color='pink'
)
draw_networkx_nodes(
nxg, pos, ax=ax,
nodelist=loaded_sws, node_color='cyan'
)
draw_networkx_labels(nxg, pos, ax=ax)
continuePlotting = True
def to_closing_state():
global continuePlotting
continuePlotting = False
def show_window(currentfile):
root = tkinter.Tk()
root.title("Network graph viewer")
root.geometry("1000x600")
lab = tkinter.Label(root, text="Plotting network")
lab.pack()
fig = Figure()
ax = fig.add_subplot(111)
ax.axis('off')
fig.patch.set_facecolor("#D9D9D9")
graph = FigureCanvasTkAgg(fig, master=root)
graph.get_tk_widget().pack(side="top", fill='both', expand=True)
b = tkinter.Button(root, text="Update GUI", command=graph.draw)
b.pack()
def plotter():
while continuePlotting:
try:
plot(ax, currentfile)
b.invoke()
except BaseException:
traceback.print_exc(file=sys.stderr)
time.sleep(0.1)
def close_cmd():
to_closing_state()
root.destroy()
thread = threading.Thread(target=plotter)
root.protocol("WM_DELETE_WINDOW", close_cmd)
thread.start()
root.mainloop()
def main(currentfile):
show_window(currentfile)
if __name__ == "__main__":
currentfile = Path(f'~current.state')
main(currentfile)