-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathreport_svg.py
executable file
·315 lines (297 loc) · 14 KB
/
report_svg.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#!/usr/bin/python3
import json
import os
from optparse import OptionParser
import svgwrite
from svgwrite import mm, px
def get_hosts(netmap):
hosts = list(netmap['machines'])
hosts.sort()
return hosts
def get_switches_left(netmap):
hosts = list(netmap['switches'])
hosts.sort()
return hosts[0::2]
def get_switches_right(netmap):
hosts = list(netmap['switches'])
hosts.sort()
return hosts[1::2]
def init_side_pane(netmap, items):
meta = {
'max_width': 0,
'max_height': 0,
'count' : len(items),
'items': items
}
for item in items:
meta[item] = {}
meta[item]['width'] = len(item)
meta[item]['vlans'] = {}
meta[item]['ports'] = []
for vlan in netmap['switches'][item]['vlans']:
meta[item]['vlans'][vlan] = {'ports': []}
for port in netmap['switches'][item]['vlans'][vlan]:
if len(item) > meta[item]['width']:
meta[item]['width'] = len(item)
meta[item]['vlans'][vlan]['ports'].append(port)
meta[item]['ports'].append(port)
meta[item][port] = {}
meta[item]['height'] = len(netmap['switches'][item]['ports'])+len(netmap['switches'][item]['vlans'])
if meta[item]['width'] > meta['max_width']:
meta['max_width'] = meta[item]['width']
if meta[item]['height'] > meta['max_height']:
meta['max_height'] = meta[item]['height']
return meta
def init_center_pane(netmap, items, peers):
meta = {
'max_width': 0,
'max_height': 0,
'count' : len(items),
'items': items
}
for item in items:
meta[item] = {}
meta[item]['width'] = len(item)
meta[item]['ports_left'] = []
meta[item]['ports_right'] = []
meta[item]['vlans'] = {}
meta[item]['ports'] = []
port_left_max_width=0
port_right_max_width=0
for vlan in netmap['machines'][item]['vlans']:
meta[item]['vlans'][vlan] = {'ports': []}
for port in netmap['machines'][item]['vlans'][vlan]:
if netmap['machines'][item]['ports'][port]['chassis'] in peers['left']:
meta[item]['ports_left'].append(port)
meta[item]['ports'].append(port)
meta[item]['vlans'][vlan]['ports'].append(port)
meta[item][port] = {'align': 'left'}
if len(port) > port_left_max_width:
port_left_max_width = len(port)
if netmap['machines'][item]['ports'][port]['chassis'] in peers['right']:
meta[item]['ports_right'].append(port)
meta[item]['ports'].append(port)
meta[item][port] = {'align': 'right'}
if len(port) > port_right_max_width:
port_right_max_width = len(port)
if meta[item]['width'] < port_right_max_width + port_left_max_width:
meta[item]['width'] = port_right_max_width + port_left_max_width
if meta[item]['width'] > meta['max_width']:
meta['max_width'] = meta[item]['width']
meta[item]['height'] = max(len(meta[item]['ports_left']),
len(meta[item]['ports_right'])
) + len(netmap['machines'][item]['vlans'])
if meta[item]['height'] > meta['max_height']:
meta['max_height'] = meta[item]['height']
return meta
def prepare_topology_metadata(netmap):
meta = {}
items = {}
items['left'] = get_switches_left(netmap)
items['right'] = get_switches_right(netmap)
items['center'] = get_hosts(netmap)
meta['left'] = init_side_pane( netmap, items['left'])
meta['right'] = init_side_pane( netmap, items['right'])
meta['center'] = init_center_pane( netmap, items['center'], items )
return meta
def prepare_placement_metadata(meta, options):
max_height = 0
for side in ['left', 'center', 'right']:
height = (meta[side]['max_height'] * options['port_height'] + options['item_label_height'] + options['item_hspace'])*meta[side]['count']
if height > max_height:
max_height = height
meta['left']['x'] = options['img_left_padding']
meta['center']['x'] = meta['left']['x'] + meta['left']['max_width']* options['col_width'] + options['item_vspace']
meta['right']['x'] = meta['center']['x'] + meta['center']['max_width'] * options['col_width'] + options['item_vspace']
for side in ['left', 'center', 'right']:
meta[side]['box_height'] = max_height / meta[side]['count']
y = 0
for item in meta[side]['items']:
box_x = meta[side]['x']
box_y = meta[side]['box_height']*y
meta[side][item]['width'] = meta[side]['max_width'] * options['col_width']
meta[side][item]['height'] = meta[side][item]['height'] * options['port_height'] + options['item_label_height']
meta[side][item]['x'] = box_x
meta[side][item]['y'] = box_y + (meta[side]['box_height'] - meta[side][item]['height']) / 2
if side == 'center':
for col in ['left', 'right']:
dy = 0
for port in meta[side][item]['ports_' + col]:
meta[side][item][port]['width'] = (meta[side]['max_width'] / 2) * options['col_width']
meta[side][item][port]['height'] = options['port_height']
meta[side][item][port]['y'] = meta[side][item]['y'] + options['item_label_height'] + dy * meta[side][item][port]['height']
meta[side][item][port]['anchor_y'] = meta[side][item][port]['y'] + meta[side][item][port]['height'] / 2
if col == 'left':
meta[side][item][port]['x'] = meta[side][item]['x']
meta[side][item][port]['anchor_x'] = meta[side][item]['x']
if col == 'right':
meta[side][item][port]['x'] = meta[side][item]['x'] + (meta[side]['max_width'] / 2) * options['col_width']
meta[side][item][port]['anchor_x'] = meta[side][item][port]['x'] + meta[side][item][port]['width']
dy += 1
else:
dy = 0
for port in meta[side][item]['ports']:
meta[side][item][port]['width'] = meta[side]['max_width'] * options['col_width']
meta[side][item][port]['height'] = options['port_height']
meta[side][item][port]['x'] = meta[side][item]['x']
meta[side][item][port]['y'] = meta[side][item]['y'] + options['item_label_height'] + dy * meta[side][item][port]['height']
meta[side][item][port]['anchor_y'] = meta[side][item][port]['y'] + meta[side][item][port]['height'] / 2
if side == 'left':
meta[side][item][port]['anchor_x'] = meta[side][item]['x'] + meta[side][item][port]['width']
if side == 'right':
meta[side][item][port]['anchor_x'] = meta[side][item]['x']
dy += 1
y += 1
return meta
def port_color(netmap, item, port):
if item in netmap['machines']:
port_data = netmap['machines'][item]['ports'][port]
if item in item in netmap['switches']:
port_data = netmap['switches'][item]['ports'][port]
if 'vlan' in port_data:
vlan_id = int(port_data['vlan'])
else:
vlan_id = 0
return vlan_color(vlan_id)
def vlan_color(vlan_id):
if vlan_id == 0:
return 'gray'
rev = int(str(vlan_id)[::-1])
color = {}
color['r'] = (rev * (( rev / 10 ) % 10)) % 256
color['g'] = (rev * (( rev / 100 ) % 10)) % 256
color['b'] = (rev * (( rev / 1000 ) % 10)) % 256
return svgwrite.rgb(color['r'], color['g'], color['b'])
def render_svg(netmap, outfile):
dwg = svgwrite.Drawing(filename=outfile, debug=True)
shapes = dwg.add(dwg.g(id='shapes'))
options = {
'col_width' : 2.2,
'port_height' : 5,
'item_hspace' : 1,
'item_vspace' : 100,
'img_left_padding' : 10,
'item_label_height' : 5,
'format' : {
'line_width': 3,
'left' : {
'item' : {
'fill': 'none',
'stroke': 'black',
},
'item_label' : {
'text_anchor': 'middle'
},
'port' : {
'stroke': 'black'
},
'port_label' : {
'text_anchor': 'middle'
},
},
'center' : {
'item' : {
'fill': 'none',
'stroke': 'black'
},
'item_label' : {
'text_anchor': 'middle'
},
'port' : {
'stroke': 'black'
},
'port_label' : {
'text_anchor': 'middle'
},
},
'right' : {
'item' : {
'fill': 'none',
'stroke': 'green'
},
'item_label' : {
'text_anchor': 'middle'
},
'port' : {
'stroke': 'black'
},
'port_label' : {
'text_anchor': 'middle'
},
},
}
}
topology = prepare_topology_metadata(netmap)
meta = prepare_placement_metadata(topology, options)
unit = mm
for side in ['left', 'center', 'right']:
for item in meta[side]['items']:
rect = dwg.rect(insert = (
meta[side][item]['x'] * unit,
meta[side][item]['y'] * unit),
size = (
meta[side][item]['width'] * unit,
meta[side][item]['height'] * unit),
**options['format'][side]['item'])
label = dwg.text(item,
insert = (
(meta[side][item]['x'] + meta[side][item]['width'] / 2) * unit,
(meta[side][item]['y'] + options['item_label_height'])*unit),
**options['format'][side]['item_label']
)
shapes.add(rect)
shapes.add(label)
for port in meta[side][item]['ports']:
dwg_opts = options['format'][side]['port'].copy()
if 'fill' not in dwg_opts:
dwg_opts['fill'] = port_color(netmap, item, port)
rect = dwg.rect(insert=(meta[side][item][port]['x'] * unit, meta[side][item][port]['y'] * unit),
size=(meta[side][item][port]['width'] * unit, meta[side][item][port]['height'] * unit),
**dwg_opts)
label = dwg.text(port,
insert=(
(meta[side][item][port]['x'] + meta[side][item][port]['width'] / 2 ) * unit,
(meta[side][item][port]['y'] + options['item_label_height']) * unit),
**options['format'][side]['port_label']
)
shapes.add(rect)
shapes.add(label)
for link in netmap['links']:
if link['source_host'] == item and link['source_port'] == port:
for dst_side in ['left', 'center', 'right']:
for dst_item in meta[dst_side]['items']:
if dst_item == link['destination_host']:
for dst_port in meta[dst_side][dst_item]['ports']:
if dst_port == link['destination_port']:
if 'line_stroke' not in options['format']:
stroke = port_color(netmap, item, port)
else:
stroke = options['format']['line_stroke']
line = dwg.line(
start = (
meta[side][item][port]['anchor_x'] * unit,
meta[side][item][port]['anchor_y'] * unit,
),
end = (
meta[dst_side][dst_item][dst_port]['anchor_x'] * unit,
meta[dst_side][dst_item][dst_port]['anchor_y'] * unit,
),
stroke_width = options['format']['line_width'],
stroke = stroke
)
shapes.add(line)
dwg.save()
def main(options):
netmap = {}
with open(options.infile) as f:
netmap = json.load(f)
render_svg(netmap, options.outfile)
if __name__ == "__main__":
usage = "usage: %prog [options] arg1 arg2"
parser = OptionParser(usage=usage)
parser.add_option("-i", "--input",
action="store", type="string", dest="infile", default='netmap.json', help="Input file")
parser.add_option("-o", "--output",
action="store", type="string", dest="outfile", default='netmap.svg', help="Output file")
(options, args) = parser.parse_args()
main(options)