-
Notifications
You must be signed in to change notification settings - Fork 6
/
clab2drawio.py
1329 lines (1150 loc) · 52.5 KB
/
clab2drawio.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# from N2G import drawio_diagram
from lib.CustomDrawioDiagram import CustomDrawioDiagram
from lib.Link import Link
from lib.Node import Node
from lib.Grafana import GrafanaDashboard
from lib.Yaml_processor import YAMLProcessor
from collections import defaultdict
from prompt_toolkit.shortcuts import checkboxlist_dialog, yes_no_dialog
import yaml
import argparse
import os
import re
import random
def add_ports(diagram, styles, verbose=True):
nodes = diagram.nodes
# Calculate and set port positions for all nodes
for node in nodes.values():
links = node.get_all_links()
# Group links by their direction
direction_groups = {}
for link in links:
direction = link.direction
if direction not in direction_groups:
direction_groups[direction] = []
direction_groups[direction].append(link)
for direction, group in direction_groups.items():
if diagram.layout == "vertical":
if direction == "downstream":
# Sort downstream links by x position of source and target
sorted_links = sorted(
group, key=lambda link: (link.source.pos_x, link.target.pos_x)
)
num_links = len(sorted_links)
spacing = styles["node_width"] / (num_links + 1)
for i, link in enumerate(sorted_links):
port_x = (
node.pos_x + (i + 1) * spacing - styles["port_width"] / 2
)
port_y = (
node.pos_y
+ styles["node_height"]
- styles["port_height"] / 2
)
link.port_pos = (port_x, port_y)
elif direction == "upstream":
# Sort upstream links by x position of source and target
sorted_links = sorted(
group, key=lambda link: (link.source.pos_x, link.target.pos_x)
)
num_links = len(sorted_links)
spacing = styles["node_width"] / (num_links + 1)
for i, link in enumerate(sorted_links):
port_x = (
node.pos_x + (i + 1) * spacing - styles["port_width"] / 2
)
port_y = node.pos_y - styles["port_height"] / 2
link.port_pos = (port_x, port_y)
else:
# Sort lateral links by y position of source and target
sorted_links = sorted(
group, key=lambda link: (link.source.pos_y, link.target.pos_y)
)
num_links = len(sorted_links)
spacing = styles["node_height"] / (num_links + 1)
for i, link in enumerate(sorted_links):
if link.target.pos_x > link.source.pos_x:
# Lateral link to the right
port_x = node.pos_x + styles["node_width"]
else:
# Lateral link to the left
port_x = node.pos_x
port_y = node.pos_y + (i + 1) * spacing
link.port_pos = (port_x, port_y)
elif diagram.layout == "horizontal":
if direction == "downstream":
# Sort downstream links by y position of source and target
sorted_links = sorted(
group, key=lambda link: (link.source.pos_y, link.target.pos_y)
)
num_links = len(sorted_links)
spacing = styles["node_height"] / (num_links + 1)
for i, link in enumerate(sorted_links):
port_x = node.pos_x + styles["node_width"]
port_y = node.pos_y + (i + 1) * spacing
link.port_pos = (port_x, port_y)
elif direction == "upstream":
# Sort upstream links by y position of source and target
sorted_links = sorted(
group, key=lambda link: (link.source.pos_y, link.target.pos_y)
)
num_links = len(sorted_links)
spacing = styles["node_height"] / (num_links + 1)
for i, link in enumerate(sorted_links):
port_x = node.pos_x
port_y = node.pos_y + (i + 1) * spacing
link.port_pos = (port_x, port_y)
else:
# Sort lateral links by x position of source and target
sorted_links = sorted(
group, key=lambda link: (link.source.pos_x, link.target.pos_x)
)
num_links = len(sorted_links)
spacing = styles["node_width"] / (num_links + 1)
for i, link in enumerate(sorted_links):
if link.target.pos_y > link.source.pos_y:
# Lateral link to the bottom
port_y = node.pos_y + styles["node_height"]
else:
# Lateral link to the top
port_y = node.pos_y
port_x = node.pos_x + (i + 1) * spacing
link.port_pos = (port_x, port_y)
connector_dict = {}
# Create connectors and links using the calculated port positions
processed_connections = set()
for node in nodes.values():
downstream_links = node.get_downstream_links()
lateral_links = node.get_lateral_links()
links = downstream_links + lateral_links
for link in links:
connection_id = frozenset(
{
(link.source.name, link.source_intf),
(link.target.name, link.target_intf),
}
)
if connection_id not in processed_connections:
processed_connections.add(connection_id)
# print(connection_id)
# source connector
source_cID = f"{link.source.name}:{link.source_intf}:{link.target.name}:{link.target_intf}"
source_label = re.findall(r"\d+", link.source_intf)[-1]
source_connector_pos = link.port_pos
port_width = styles["port_width"]
port_height = styles["port_height"]
# Add the source connector ID to the source connector dictionary
if link.source.name not in connector_dict:
connector_dict[link.source.name] = []
connector_dict[link.source.name].append(source_cID)
# target connector
target_cID = f"{link.target.name}:{link.target_intf}:{link.source.name}:{link.source_intf}"
target_link = diagram.get_target_link(link)
target_connector_pos = target_link.port_pos
target_label = re.findall(r"\d+", target_link.source_intf)[-1]
if link.target.name not in connector_dict:
connector_dict[link.target.name] = []
connector_dict[link.target.name].append(target_cID)
# Adjust port positions if source and target have different numbers of links
source_downstream_links = link.source.get_downstream_links()
target_upstream_links = link.target.get_upstream_links()
if diagram.layout == "vertical":
if link.source.pos_x == link.target.pos_x:
if len(source_downstream_links) != len(target_upstream_links):
if len(source_downstream_links) < len(
target_upstream_links
):
# Adjust source port position to align with the corresponding target port
adjusted_x = target_connector_pos[0]
source_connector_pos = (
adjusted_x,
source_connector_pos[1],
)
else:
# Adjust target port position to align with the corresponding source port
adjusted_x = source_connector_pos[0]
target_connector_pos = (
adjusted_x,
target_connector_pos[1],
)
elif diagram.layout == "horizontal":
if link.source.pos_y == link.target.pos_y:
# pass
if len(source_downstream_links) != len(target_upstream_links):
if len(source_downstream_links) < len(
target_upstream_links
):
# Adjust source port position to align with the corresponding target port
adjusted_y = target_connector_pos[1]
source_connector_pos = (
source_connector_pos[0],
adjusted_y,
)
else:
# Adjust target port position to align with the corresponding source port
adjusted_y = source_connector_pos[1]
target_connector_pos = (
target_connector_pos[0],
adjusted_y,
)
diagram.add_node(
id=source_cID,
label=source_label,
x_pos=source_connector_pos[0],
y_pos=source_connector_pos[1],
width=port_width,
height=port_height,
style=styles["port_style"],
)
diagram.add_node(
id=target_cID,
label=target_label,
x_pos=target_connector_pos[0],
y_pos=target_connector_pos[1],
width=port_width,
height=port_height,
style=styles["port_style"],
)
# Calculate center positions
source_center = (
source_connector_pos[0] + port_width / 2,
source_connector_pos[1] + port_height / 2,
)
target_center = (
target_connector_pos[0] + port_width / 2,
target_connector_pos[1] + port_height / 2,
)
# Calculate the real middle between the centers for the midpoint connector
midpoint_center_x = (source_center[0] + target_center[0]) / 2
midpoint_center_y = (source_center[1] + target_center[1]) / 2
# Generate a random offset within the range of ±10
random_offset = random.choice(
[random.uniform(-20, -10), random.uniform(10, 20)]
)
# Determine the direction of the link
dx = target_center[0] - source_center[0]
dy = target_center[1] - source_center[1]
# Calculate the normalized direction vector for the line
magnitude = (dx**2 + dy**2) ** 0.5
if magnitude != 0:
direction_dx = dx / magnitude
direction_dy = dy / magnitude
else:
# If the magnitude is zero, the source and target are at the same position
# In this case, we don't need to move the midpoint
direction_dx = 0
direction_dy = 0
# Apply the offset
midpoint_center_x += direction_dx * random_offset
midpoint_center_y += direction_dy * random_offset
midpoint_top_left_x = midpoint_center_x - 2
midpoint_top_left_y = midpoint_center_y - 2
# Create midpoint connector between source and target ports
midpoint_id = f"mid:{link.source.name}:{link.source_intf}:{link.target.name}:{link.target_intf}"
diagram.add_node(
id=midpoint_id,
label="\u200b",
x_pos=midpoint_top_left_x,
y_pos=midpoint_top_left_y,
width=styles["connector_width"],
height=styles["connector_height"],
style=styles["connector_style"],
)
diagram.add_link(
source=source_cID,
target=midpoint_id,
style=styles["link_style"],
label="\u200b",
link_id=f"{source_cID}",
)
diagram.add_link(
source=target_cID,
target=midpoint_id,
style=styles["link_style"],
label="\u200b",
link_id=f"{target_cID}",
)
# Create groups for each node and its connectors
for node_name, connector_ids in connector_dict.items():
group_id = f"group-{node_name}"
member_objects = connector_ids + [node_name]
diagram.group_nodes(
member_objects=member_objects, group_id=group_id, style="group"
)
def add_links(diagram, styles):
nodes = diagram.nodes
global_seen_links = set()
for node in nodes.values():
downstream_links = node.get_downstream_links()
lateral_links = node.get_lateral_links()
links = downstream_links + lateral_links
# Filter links globally
filtered_links = []
for link in links:
source_id = f"{link.source.name}:{link.source_intf}"
target_id = f"{link.target.name}:{link.target_intf}"
link_pair = tuple(sorted([source_id, target_id]))
if link_pair not in global_seen_links:
global_seen_links.add(link_pair)
filtered_links.append(link)
# Group links by their target
target_groups = {}
for link in filtered_links:
target = link.target
if target not in target_groups:
target_groups[target] = []
target_groups[target].append(link)
for target, group in target_groups.items():
for i, link in enumerate(group):
source_x, source_y = link.source.pos_x, link.source.pos_y
target_x, target_y = link.target.pos_x, link.target.pos_y
# Determine directionality
left_to_right = source_x < target_x
above_to_below = source_y < target_y
# Calculate step for multiple links with the same target
step = 0.5 if len(group) == 1 else 0.25 + 0.5 * (i / (len(group) - 1))
if diagram.layout == "horizontal":
if link.level_diff > 0:
entryX, exitX = (0, 1) if left_to_right else (1, 0)
entryY = exitY = step
else:
if above_to_below:
entryY, exitY = (0, 1)
else:
entryY, exitY = (1, 0)
entryX = exitX = step
elif diagram.layout == "vertical":
if link.level_diff > 0:
entryY, exitY = (0, 1) if above_to_below else (1, 0)
entryX = exitX = step
# Same graph level
else:
if left_to_right:
entryX, exitX = (0, 1)
else:
entryX, exitX = (1, 0)
entryY = exitY = step
style = f"{styles['link_style']}entryY={entryY};exitY={exitY};entryX={entryX};exitX={exitX};"
# Create label nodes for source and target interfaces
source_label_id = f"label:{link.source.name}:{link.source_intf}"
target_label_id = f"label:{link.target.name}:{link.target_intf}"
if not styles["default_labels"]:
# Calculate label positions using the get_label_positions method
(
(source_label_x, source_label_y),
(target_label_x, target_label_y),
) = link.get_label_positions(entryX, entryY, exitX, exitY, styles)
diagram.add_link(
link_id=f"link:{link.source.name}:{link.source_intf}:{link.target.name}:{link.target_intf}",
source=link.source.name,
target=link.target.name,
style=style,
)
# Add label nodes
diagram.add_node(
id=source_label_id,
# label should node name + interface name
label=f"{link.source_intf}",
x_pos=source_label_x,
y_pos=source_label_y,
width=styles["label_width"],
height=styles["label_height"],
style=styles["src_label_style"],
)
diagram.add_node(
id=target_label_id,
label=f"{link.target_intf}",
x_pos=target_label_x,
y_pos=target_label_y,
width=styles["label_width"],
height=styles["label_height"],
style=styles["trgt_label_style"],
)
else:
diagram.add_link(
link_id=f"link:{link.source.name}:{link.source_intf}:{link.target.name}:{link.target_intf}",
source=link.source.name,
target=link.target.name,
src_label=link.source_intf,
trgt_label=link.target_intf,
src_label_style=styles["src_label_style"],
trgt_label_style=styles["trgt_label_style"],
style=style,
)
def add_nodes(diagram, nodes, styles):
base_style = styles["base_style"]
custom_styles = styles["custom_styles"]
icon_to_group_mapping = styles["icon_to_group_mapping"]
for node in nodes.values():
# Check for 'graph_icon' attribute and map it to the corresponding group
if node.graph_icon in icon_to_group_mapping:
group = icon_to_group_mapping[node.graph_icon]
else:
# Determine the group based on the node's name if 'graph_icon' is not specified
if "client" in node.name:
group = "server"
elif "leaf" in node.name:
group = "leaf"
elif "spine" in node.name:
group = "spine"
elif "dcgw" in node.name:
group = "dcgw"
else:
group = (
"default" # Fallback to 'default' if none of the conditions are met
)
style = custom_styles.get(group, base_style)
x_pos, y_pos = node.pos_x, node.pos_y
# Add each node to the diagram with the given x and y position.
diagram.add_node(
id=node.name,
label=node.label,
x_pos=x_pos,
y_pos=y_pos,
style=style,
width=node.width,
height=node.height,
)
def adjust_intermediary_nodes(intermediaries, layout, verbose=False):
if not intermediaries:
return
# group the intermediaries by their graph level
intermediaries_by_level = defaultdict(list)
for node in intermediaries:
intermediaries_by_level[node.graph_level].append(node)
selected_level = max(
intermediaries_by_level.keys(),
key=lambda lvl: len(intermediaries_by_level[lvl]),
)
selected_group = intermediaries_by_level[selected_level]
if len(selected_group) == 1:
node = selected_group[0]
if layout == "vertical":
node.pos_x = node.pos_x - 100
else:
node.pos_y = node.pos_y - 100
else:
for i, node in enumerate(selected_group):
if layout == "vertical":
node.pos_x = node.pos_x - 100 + i * 200
else:
node.pos_y = node.pos_y - 100 + i * 200
pass
def center_align_nodes(nodes_by_graphlevel, layout="vertical", verbose=False):
"""
Center align nodes within each graphlevel based on the layout layout and ensure
they are nicely distributed to align with the graphlevel above.
"""
attr_x, attr_y = ("pos_x", "pos_y") if layout == "vertical" else ("pos_y", "pos_x")
prev_graphlevel_center = None
for graphlevel, nodes in sorted(nodes_by_graphlevel.items()):
graphlevel_centers = [getattr(node, attr_x) for node in nodes]
if prev_graphlevel_center is None:
# For the first graphlevel, calculate its center and use it as the previous center for the next level
prev_graphlevel_center = (
min(graphlevel_centers) + max(graphlevel_centers)
) / 2
else:
# Calculate current graphlevel's center
graphlevel_center = sum(graphlevel_centers) / len(nodes)
# Calculate offset to align current graphlevel's center with the previous graphlevel's center
offset = prev_graphlevel_center - graphlevel_center
# Apply offset to each node in the current graphlevel
for node in nodes:
setattr(node, attr_x, getattr(node, attr_x) + offset)
# Update prev_graphlevel_center for the next level
prev_graphlevel_center = sum(getattr(node, attr_x) for node in nodes) / len(
nodes
)
def calculate_positions(diagram, layout="vertical", verbose=False):
"""
Calculates and assigns positions to nodes for graph visualization based on their hierarchical levels and connectivity.
Organizes nodes by graph level, applies prioritization within levels based on connectivity, and adjusts positions to enhance readability.
Aligns and adjusts intermediary nodes to address alignment issues and improve visual clarity.
"""
nodes = diagram.nodes
nodes = sorted(nodes.values(), key=lambda node: (node.graph_level, node.name))
x_start, y_start = 100, 100
padding_x, padding_y = 150, 175
if verbose:
print("Nodes before calculate_positions:", nodes)
def prioritize_placement(nodes, level, verbose=False):
if level == diagram.get_max_level():
# If it's the maximum level, simply sort nodes by name
ordered_nodes = sorted(nodes, key=lambda node: node.name)
else:
# Separate nodes by their connection count within the level
multi_connection_nodes = [
node for node in nodes if node.get_connection_count_within_level() > 1
]
single_connection_nodes = [
node for node in nodes if node.get_connection_count_within_level() == 1
]
zero_connection_nodes = [
node for node in nodes if node.get_connection_count_within_level() == 0
]
# Separate multi-connection nodes with lateral links
multi_connection_nodes_with_lateral = []
multi_connection_nodes_without_lateral = []
for node in multi_connection_nodes:
if any(
link.target in multi_connection_nodes
for link in node.get_lateral_links()
):
multi_connection_nodes_with_lateral.append(node)
else:
multi_connection_nodes_without_lateral.append(node)
# Sort multi-connection nodes with lateral links wisely
sorted_multi_connection_nodes_with_lateral = []
while multi_connection_nodes_with_lateral:
node = multi_connection_nodes_with_lateral.pop(0)
sorted_multi_connection_nodes_with_lateral.append(node)
for link in node.get_lateral_links():
if link.target in multi_connection_nodes_with_lateral:
multi_connection_nodes_with_lateral.remove(link.target)
sorted_multi_connection_nodes_with_lateral.append(link.target)
# sort by name
multi_connection_nodes_without_lateral = sorted(
multi_connection_nodes_without_lateral, key=lambda node: node.name
)
sorted_multi_connection_nodes_with_lateral = sorted(
sorted_multi_connection_nodes_with_lateral, key=lambda node: node.name
)
single_connection_nodes = sorted(
single_connection_nodes, key=lambda node: node.name
)
# Merge single, multi-connection (with and without lateral), and zero-connection nodes
ordered_nodes = (
single_connection_nodes[: len(single_connection_nodes) // 2]
+ multi_connection_nodes_without_lateral
+ sorted_multi_connection_nodes_with_lateral
+ single_connection_nodes[len(single_connection_nodes) // 2 :]
+ zero_connection_nodes
)
return ordered_nodes
# Organize nodes by graphlevel and order within each graphlevel
nodes_by_graphlevel = defaultdict(list)
for node in nodes:
nodes_by_graphlevel[node.graph_level].append(node)
for graphlevel, graphlevel_nodes in nodes_by_graphlevel.items():
ordered_nodes = prioritize_placement(
graphlevel_nodes, graphlevel, verbose=verbose
)
for i, node in enumerate(ordered_nodes):
if layout == "vertical":
node.pos_x = x_start + i * padding_x
node.pos_y = y_start + graphlevel * padding_y
else:
node.pos_x = x_start + graphlevel * padding_x
node.pos_y = y_start + i * padding_y
center_align_nodes(nodes_by_graphlevel, layout=layout, verbose=verbose)
intermediaries_x, intermediaries_y = diagram.get_nodes_between_interconnected()
if diagram.layout == "vertical":
adjust_intermediary_nodes(
intermediaries_x, layout=diagram.layout, verbose=verbose
)
else:
adjust_intermediary_nodes(
intermediaries_y, layout=diagram.layout, verbose=verbose
)
def adjust_node_levels(diagram):
used_levels = diagram.get_used_levels()
max_level = diagram.get_max_level()
min_level = diagram.get_min_level()
# print(f"Initial used levels: {used_levels}")
if len(used_levels) <= 1:
# print("Only one level present, no adjustment needed.")
return # Only one level present, no adjustment needed
current_level = min_level
while current_level < max_level + 1:
# if level is the first used level or the last used level, skip it
if current_level == min_level:
# print(f"Skip Level: {current_level} because it is the first or last level")
current_level += 1
continue
nodes_at_current_level = diagram.get_nodes_by_level(current_level)
before_level = current_level - 1
nodes_to_move = []
if len(nodes_at_current_level.items()) == 1:
# print(f"Only one node found at level {current_level}. No adjustment needed.")
current_level += 1
continue
for node_name, node in nodes_at_current_level.items():
has_upstream_connection = any(
node.get_upstream_links_towards_level(before_level)
)
if not has_upstream_connection:
nodes_to_move.append(node)
if len(nodes_to_move) == len(nodes_at_current_level):
# print(f"Nothing to move here")
current_level += 1
continue
if nodes_to_move:
# print(f"Because we need to move, we are increasing all node_graphlevels from the next Levels Nodes by one level")
for level in range(max_level, current_level, -1):
nodes_at_level = diagram.get_nodes_by_level(level)
for node in nodes_at_level.values():
node.graph_level += 1
# print(f" Moving node {node.name} from level {level} to level {level + 1}.")
# Move the nodes marked for movement to the next level
for node in nodes_to_move:
node.graph_level += 1
# print(f" Moving node {node.name} from level {current_level} to level {next_level}")
# print(f"Moved nodes at level {current_level} to level {next_level}.")
update_links(diagram.get_links_from_nodes())
max_level = diagram.get_max_level()
max_level = diagram.get_max_level()
current_level += 1
# Check all levels starting from the last level
for level in range(max_level, min_level - 1, -1):
nodes_at_level = diagram.get_nodes_by_level(level)
for node in nodes_at_level.values():
upstream_links = node.get_upstream_links()
can_move = True
for link in upstream_links:
level_diff = node.graph_level - link.target.graph_level
if level_diff == 1:
can_move = False
break # Stop checking if any upstream link has a level difference of 1
if can_move:
for link in upstream_links:
level_diff = node.graph_level - link.target.graph_level
if level_diff > 1:
node.graph_level -= 1
# print(f" Moving node {node.name} from level {level} to level {level - 1} due to upstream link with level difference > 1")
update_links(diagram.get_links_from_nodes())
max_level = diagram.get_max_level()
break # Stop moving the node after adjusting its level once
def update_links(links):
for link in links:
source_level = link.source.graph_level
target_level = link.target.graph_level
link.level_diff = target_level - source_level
if link.level_diff > 0:
link.direction = "downstream"
elif link.level_diff < 0:
link.direction = "upstream"
else:
link.direction = "lateral"
def assign_graphlevels(diagram, verbose=False):
"""
Assigns hierarchical graph levels to nodes based on connections or optional labels
Returns a sorted list of nodes and their graph levels.
"""
nodes = diagram.get_nodes()
# Check if all nodes already have a graphlevel != -1
if all(node.graph_level != -1 for node in nodes.values()):
already_set = True
else:
already_set = False
print(
"Not all graph levels set in the .clab file. Assigning graph levels based on downstream links. Expect experimental output. Please consider assigning graph levels to your .clab file, or use it with -I for interactive mode. Find more information here: https://github.com/srl-labs/clab-io-draw/blob/grafana_style/docs/clab2drawio.md#influencing-node-placement"
)
# Helper function to assign graphlevel by recursively checking connections
def set_graphlevel(node, current_graphlevel, visited=None):
if visited is None:
visited = set()
if node.name in visited:
return
visited.add(node.name)
if node.graph_level < current_graphlevel:
node.graph_level = current_graphlevel
for link in node.get_downstream_links():
target_node = nodes[link.target.name]
set_graphlevel(target_node, current_graphlevel + 1, visited)
# Start by setting the graphlevel to -1 if they don't already have a graphlevel
for node in nodes.values():
if node.graph_level != -1:
continue
# Setting the graphlevel of nodes with no upstream connections
elif not node.get_upstream_links():
set_graphlevel(node, 0)
else:
set_graphlevel(node, node.graph_level)
# Update the links of each node
for node in nodes.values():
node.update_links()
if not already_set:
adjust_node_levels(diagram)
for node in nodes.values():
node.update_links()
sorted_nodes = sorted(
nodes.values(), key=lambda node: (node.graph_level, node.name)
)
return sorted_nodes
def load_styles_from_config(config_path):
try:
with open(config_path, "r") as file:
config = yaml.safe_load(file)
except FileNotFoundError:
error_message = (
f"Error: The specified config file '{config_path}' does not exist."
)
print(error_message)
exit()
except Exception as e:
error_message = f"An error occurred while loading the config: {e}"
print(error_message)
exit()
# Parse the base style into a dictionary
base_style_dict = {
item.split("=")[0]: item.split("=")[1]
for item in config.get("base_style", "").split(";")
if item
}
# Initialize styles dictionary with configuration values
styles = {
"background": config.get("background", "#FFFFFF"),
"shadow": config.get("shadow", "1"),
"grid": config.get("grid", "1"),
"pagew": config.get("pagew", "827"),
"pageh": config.get("pageh", "1169"),
"base_style": config.get("base_style", ""),
"link_style": config.get("link_style", ""),
"src_label_style": config.get("src_label_style", ""),
"trgt_label_style": config.get("trgt_label_style", ""),
"port_style": config.get("port_style", ""),
"connector_style": config.get("connector_style", ""),
"icon_to_group_mapping": config.get("icon_to_group_mapping", {}),
"custom_styles": {},
}
# Merge base style with custom styles
for key, custom_style in config.get("custom_styles", {}).items():
custom_style_dict = {
item.split("=")[0]: item.split("=")[1]
for item in custom_style.split(";")
if item
}
merged_style_dict = {
**base_style_dict,
**custom_style_dict,
} # custom style overrides base style
merged_style = ";".join(f"{k}={v}" for k, v in merged_style_dict.items())
styles["custom_styles"][key] = merged_style
# Read all other configuration values
for key, value in config.items():
if key not in styles:
styles[key] = value
return styles
def interactive_mode(
nodes,
icon_to_group_mapping,
containerlab_data,
output_file,
processor,
prefix,
lab_name,
):
# Initialize previous summary with existing node labels
previous_summary = {"Levels": {}, "Icons": {}}
for node_name, node in nodes.items():
try:
level = node.graph_level
previous_summary["Levels"].setdefault(level, []).append(node_name)
icon = node.graph_icon
previous_summary["Icons"].setdefault(icon, []).append(node_name)
except AttributeError:
continue
while True:
summary = {"Levels": {}, "Icons": {}}
tmp_nodes = list(nodes.keys())
level = 0
# Assign levels to nodes
while tmp_nodes:
level += 1
valid_nodes = [(node, node) for node in tmp_nodes]
if valid_nodes:
level_nodes = checkboxlist_dialog(
title=f"Level {level} nodes",
text=f"Choose the nodes for level {level}:",
values=valid_nodes,
default_values=previous_summary["Levels"].get(level, []),
).run()
else:
break
if level_nodes is None:
return # Exit the function if cancel button is clicked
if len(level_nodes) == 0:
continue
# Update node labels and summary with assigned levels
for node_name in level_nodes:
nodes[node_name].graph_level = level
summary["Levels"].setdefault(level, []).append(node_name)
tmp_nodes.remove(node_name)
# Get the unformatted node name
unformatted_node_name = node_name.replace(f"{prefix}-{lab_name}-", "")
# Check if 'labels' section exists, create it if necessary
if (
"labels"
not in containerlab_data["topology"]["nodes"][unformatted_node_name]
):
containerlab_data["topology"]["nodes"][unformatted_node_name][
"labels"
] = {}
# Update containerlab_data with graph-level
containerlab_data["topology"]["nodes"][unformatted_node_name]["labels"][
"graph-level"
] = level
tmp_nodes = list(nodes.keys())
icons = list(icon_to_group_mapping.keys())
# Assign icons to nodes
for icon in icons:
valid_nodes = [(node, node) for node in tmp_nodes]
if valid_nodes:
icon_nodes = checkboxlist_dialog(
title=f"Choose {icon} nodes",
text=f"Select the nodes for the {icon} icon:",
values=valid_nodes,
default_values=previous_summary["Icons"].get(icon, []),
).run()
else:
icon_nodes = []
if icon_nodes is None:
return # Exit the function if cancel button is clicked
if not icon_nodes:
continue
# Update node labels and summary with assigned icons
for node_name in icon_nodes:
nodes[node_name].graph_icon = icon
summary["Icons"].setdefault(icon, []).append(node_name)
tmp_nodes.remove(node_name)
# Get the unformatted node name
unformatted_node_name = node_name.replace(f"{prefix}-{lab_name}-", "")
# Check if 'labels' section exists, create it if necessary
if (
"labels"
not in containerlab_data["topology"]["nodes"][unformatted_node_name]
):
containerlab_data["topology"]["nodes"][unformatted_node_name][
"labels"
] = {}
# Update containerlab_data with graph-icon
containerlab_data["topology"]["nodes"][unformatted_node_name]["labels"][
"graph-icon"
] = icon
# Generate summary tree with combined levels and icons
summary_tree = ""
for level, node_list in summary["Levels"].items():
summary_tree += f"Level {level}: "
node_items = []
# Calculate the indentation based on "Level {level}: "
indent = " " * (len(f"Level {level}: "))
for i, node in enumerate(node_list, start=1):
icon = nodes[node].graph_icon
# Append the node and its icon to the node_items list
node_items.append(f"{node} ({icon})")
if i % 3 == 0 and i < len(node_list):
node_items.append("\n" + indent)
# Join the node items, now including the newline and indentation at the correct position
summary_tree += ", ".join(node_items).replace(indent + ", ", indent) + "\n"
summary_tree += "\nDo you want to keep it like this? Select < No > to edit your configuration."
# Prompt user for confirmation
result = yes_no_dialog(title="SUMMARY", text=summary_tree).run()
if result is None:
return # Exit the function if cancel button is clicked
elif result:
break # Exit the loop if user confirms the summary
# Prompt user if they want to update the ContainerLab file
update_file = yes_no_dialog(
title="Update ContainerLab File",
text="Do you want to save a new ContainerLab file with the new configuration?",
).run()
if update_file:
# Save the updated containerlab_data to the output file using processor.save_yaml
modified_output_file = os.path.splitext(output_file)[0] + ".mod.yaml"
processor.save_yaml(containerlab_data, modified_output_file)
print(f"ContainerLab file has been updated: {modified_output_file}")
else:
print("ContainerLab file has not been updated.")
return summary
def format_node_name(base_name, prefix, lab_name):
if prefix == "":
return base_name
elif prefix == "clab" and not prefix:
return f"clab-{lab_name}-{base_name}"
else:
return f"{prefix}-{lab_name}-{base_name}"