-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathlf_cleanup.py
More file actions
executable file
·810 lines (689 loc) · 32.6 KB
/
lf_cleanup.py
File metadata and controls
executable file
·810 lines (689 loc) · 32.6 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
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
#!/usr/bin/env python3
r"""
NAME: lf_cleanup.py
PURPOSE: Remove present test configuration, resetting system(s) to a reproducible base state.
EXAMPLE:
# Remove all stations with CLI
./lf_cleanup.py --sta
# Remove all stations with CLI for a specific resource
./lf_cleanup.py --sta --resource 2
# Full cleanup (ports, L3 CXs/endpoints, L4-7 endpoints with CLI
./lf_cleanup.py --sanitize
# Remove all CXs and endpoints with CLI
./lf_cleanup.py --cxs
# Remove all endpoints with CLI
./lf_cleanup.py --endp
# Remove all bridge with CLI
./lf_cleanup.py --br
# Remove all STAs with unexpected names with CLI (often from misuse of or bugs in automation)
# This includes names 'phy' (not 'wiphy') and '1.1.eth'
./lf_cleanup.py --misc
# Remove all stations with JSON
"args": ["--mgr", "192.168.30.12", "--resource", "1", "--sta"]
# Full cleanup (ports, L3 CXs/endpoints, L4-7 endpoints with JSON
"args": ["--mgr", "192.168.30.12", "--resource", "1", "--sanitize"]
# Remove all CXs and endpoints with JSON
"args": ["--mgr", "192.168.30.12", "--resource", "1", "--cxs"]
# Remove all endpoints with JSON
"args": ["--mgr", "192.168.30.12", "--resource", "1", "--endp"]
# Remove all bridge with JSON
"args": ["--mgr", "192.168.30.12", "--resource", "1", "--br"]
# Remove all STAs with unexpected names with CLI (often from misuse of or bugs in automation)
# This includes names 'phy' (not 'wiphy') and '1.1.eth'
"args": ["--mgr", "192.168.30.12", "--resource", "1", "--misc"]
SCRIPT_CLASSIFICATION:
Deletion
SCRIPT_CATEGORIES:
Functional
NOTES: The script will only cleanup what is present in the GUI. If object creation (e.g. port or CX)
is in process but the object is not yet present in the GUI, then this script may need to be run
multiple times for deletion to take effect.
VERIFIED_ON:
Working date: 03/17/2023
Build version: 5.4.6
Kernel version: 5.19.17+
LICENSE: Free to distribute and modify. LANforge systems must be licensed.
Copyright (C) 2020-2026 Candela Technologies Inc
"""
import sys
import os
import importlib
import argparse
import time
import logging
if sys.version_info[0] != 3:
print("This script requires Python 3")
exit(1)
sys.path.append(os.path.join(os.path.abspath(__file__ + "../../../")))
LFUtils = importlib.import_module("py-json.LANforge.LFUtils")
realm = importlib.import_module("py-json.realm")
Realm = realm.Realm
logger = logging.getLogger(__name__)
lf_logger_config = importlib.import_module("py-scripts.lf_logger_config")
class lf_clean(Realm):
def __init__(self,
host="localhost",
port=8080,
resource=None,
clean_cxs=None,
clean_endp=None,
clean_sta=None,
clean_port_mgr=None,
clean_misc=None):
super().__init__(lfclient_host=host, lfclient_port=port)
self.host = host
self.port = port
self.resource = resource
self.clean_cxs = clean_cxs
self.clean_endp = clean_endp
self.clean_sta = clean_sta
self.clean_port_mgr = clean_port_mgr
self.clean_misc = clean_misc
self.cxs_done = False
self.endp_done = False
self.sta_done = False
self.port_mgr_done = False
self.br_done = False
self.misc_done = False
def layer4_endp_clean(self):
"""Delete L4-7 endpoints (see the Layer 4-7 tab of the LANforge GUI)."""
still_looking_endp = True
iterations_endp = 0
while still_looking_endp and iterations_endp <= 10:
iterations_endp += 1
logger.debug(f"layer4_endp_clean: iterations_endp: {iterations_endp}")
layer4_endp_json = super().json_get("layer4")
# logger.info(layer4_endp_json)
if layer4_endp_json is not None and 'empty' not in layer4_endp_json:
logger.info("Removing old Layer 4-7 endpoints")
layer4_endp_json.pop("handler")
layer4_endp_json.pop("uri")
if 'warnings' in layer4_endp_json:
layer4_endp_json.pop("warnings")
for name in list(layer4_endp_json):
if name == 'endpoint':
# Single endpoint
if type(layer4_endp_json['endpoint']) is dict:
endp_name = layer4_endp_json['endpoint']['name']
# Delete Layer 4-7 cross connection:
req_url = "cli-json/rm_cx"
data = {
"test_mgr": "default_tm",
"cx_name": "CX_" + endp_name
}
logger.info("Removing {endp_name}...".format(endp_name="CX_" + endp_name))
super().json_post(req_url, data)
# Delete Layer 4-7 endpoint
req_url = "cli-json/rm_endp"
data = {
"endp_name": endp_name
}
logger.info("Removing {endp_name}...".format(endp_name=endp_name))
super().json_post(req_url, data)
# More than one endpoint
else:
for endp_num in layer4_endp_json['endpoint']:
# get L4-Endp name:
for endp_values in endp_num.values():
endp_name = endp_values['name']
if endp_name != '':
# Delete Layer 4-7 cross connections:
req_url = "cli-json/rm_cx"
data = {
"test_mgr": "default_tm",
"cx_name": "CX_" + endp_name
}
logger.info("Removing {endp_name}...".format(endp_name="CX_" + endp_name))
super().json_post(req_url, data)
# Delete Layer 4-7 endpoint
req_url = "cli-json/rm_endp"
data = {
"endp_name": endp_name
}
logger.info("Removing {endp_name}...".format(endp_name=endp_name))
super().json_post(req_url, data)
time.sleep(1)
else:
logger.info("No further endpoints found")
still_looking_endp = False
logger.debug(f"clean_endp still_looking_endp {still_looking_endp}")
if not still_looking_endp:
self.endp_done = True
return still_looking_endp
def cxs_clean(self):
"""
Deletes Layer-3 CXs. Does not remove Layer-3 endpoints.
See the 'Layer-3' and 'L3 Endps' tabs in the LANforge GUI.
NOTE: Previously this function removed Layer-3 endpoints as well.
"""
still_looking_cxs = True
iterations_cxs = 1
while still_looking_cxs and iterations_cxs <= 10:
iterations_cxs += 1
logger.debug("cxs_clean: iterations_cxs: {iterations_cxs}".format(iterations_cxs=iterations_cxs))
cx_json = super().json_get("cx")
# endp_json = super().json_get("endp")
if cx_json is not None and 'empty' not in cx_json:
logger.debug(cx_json.keys())
logger.debug("Removing old cross connects")
# delete L3-CX based upon the L3-Endp name & the resource value from
# the e.i.d of the associated L3-Endps
cx_json.pop("handler")
cx_json.pop("uri")
if 'warnings' in cx_json:
cx_json.pop("warnings")
for cx_name in list(cx_json):
cxs_eid = cx_json[cx_name]['entity id']
cxs_eid_split = cxs_eid.split('.')
resource_eid = str(cxs_eid_split[1])
# logger.info(resource_eid)
if resource_eid in self.resource or 'all' in self.resource:
# remove Layer-3 cx:
req_url = "cli-json/rm_cx"
data = {
"test_mgr": "default_tm",
"cx_name": cx_name
}
logger.debug(f"Removing {cx_name}")
super().json_post(req_url, data)
time.sleep(5)
else:
logger.info("No further Layer-3 CXs found")
still_looking_cxs = False
logger.debug(f"clean_cxs still_looking_cxs {still_looking_cxs}")
if not still_looking_cxs:
self.cxs_done = True
return still_looking_cxs
def get_json1(self):
response = self.json_get("port/all")
return response
def layer3_endp_clean(self):
"""
Delete Layer-3 endpoints with no associated Layer-3 CX.
To delete a Layer-3 traffic pair in full with this function,
first cleanup the CX then cleanup its associated Layer-3 endpoints.
See the 'Layer-3' and 'L3 Endps' tabs in the LANforge GUI.
"""
still_looking_endp = True
iterations_endp = 0
while still_looking_endp and iterations_endp <= 10:
iterations_endp += 1
logger.debug("layer3_endp_clean: iterations_endp: {iterations_endp}".format(iterations_endp=iterations_endp))
endp_json = super().json_get("endp")
# logger.info(endp_json)
if endp_json is not None:
logger.debug("Removing old Layer 3 endpoints")
# Single endpoint
if type(endp_json['endpoint']) is dict:
endp_name = endp_json['endpoint']['name']
req_url = "cli-json/rm_endp"
data = {
"endp_name": endp_name
}
# logger.info(data)
logger.debug(f"Removing {endp_name}")
super().json_post(req_url, data)
# More than one endpoint
else:
for name in list(endp_json['endpoint']):
endp_name = list(name)[0]
if name[list(name)[0]]["name"] == '':
continue
req_url = "cli-json/rm_endp"
data = {
"endp_name": endp_name
}
# logger.info(data)
logger.debug(f"Removing {endp_name}")
super().json_post(req_url, data)
time.sleep(1)
else:
logger.info("No further Layer-3 endpoints found")
still_looking_endp = False
logger.debug(f"layer3_clean_endp still_looking_endp {still_looking_endp}")
if not still_looking_endp:
self.endp_done = True
return still_looking_endp
def sta_clean(self):
still_looking_sta = True
iterations_sta = 0
while still_looking_sta and iterations_sta <= 10:
iterations_sta += 1
logger.debug(f"sta_clean: iterations_sta: {iterations_sta}")
try:
sta_json = super().json_get("/port/?fields=alias")['interfaces']
# logger.info(sta_json)
except TypeError:
# TODO: When would this be the case
sta_json = None
logger.warning("sta_json set to None")
# TODO: Refactor this to make common w/ port removal
# And delete on type not on alias
# get and remove current stations
if sta_json is not None:
logger.debug("Removing old stations")
for name in list(sta_json):
for alias in list(name):
info = self.name_to_eid(alias)
sta_resource = str(info[1])
if sta_resource in self.resource or 'all' in self.resource:
# logger.info("alias {alias}".format(alias=alias))
if 'sta' in alias:
info = self.name_to_eid(alias)
req_url = "cli-json/rm_vlan"
data = {
"shelf": info[0],
"resource": info[1],
"port": info[2]
}
# logger.info(data)
logger.debug(f"Removing {alias}")
super().json_post(req_url, data)
if 'wlan' in alias:
info = self.name_to_eid(alias)
req_url = "cli-json/rm_vlan"
data = {
"shelf": info[0],
"resource": info[1],
"port": info[2]
}
# logger.info(data)
logger.debug(f"Removing {alias}")
super().json_post(req_url, data)
# TODO: This isn't a station type port
if 'moni' in alias:
info = self.name_to_eid(alias)
req_url = "cli-json/rm_vlan"
data = {
"shelf": info[0],
"resource": info[1],
"port": info[2]
}
# logger.info(data)
logger.debug(f"Removing {alias}")
super().json_post(req_url, data)
# TODO: Move this to misc cleanup logic
if 'Unknown' in alias:
info = self.name_to_eid(alias)
req_url = "cli-json/rm_vlan"
data = {
"shelf": info[0],
"resource": info[1],
"port": info[2]
}
# logger.info(data)
logger.debug(f"Removing {alias}")
super().json_post(req_url, data)
time.sleep(1)
else:
logger.info("No further stations found")
still_looking_sta = False
logger.debug(f"clean_sta still_looking_sta {still_looking_sta}")
if not still_looking_sta:
self.sta_done = True
return still_looking_sta
# cleans all gui or script created objects from Port Mgr tab
def port_mgr_clean(self):
"""
Delete all virtual interfaces.
Read differently, this function attempts to delete anything
that isn't a physical port on the system.
"""
still_looking_san = True
iterations_san = 0
while still_looking_san and iterations_san <= 10:
iterations_san += 1
try:
port_mgr_json = super().json_get("/port/?fields=port+type,alias")['interfaces']
# logger.info(port_mgr_json)
# logger.info(len(port_mgr_json))
except TypeError:
port_mgr_json = None
logger.warning("port_mgr_json set to None")
# get and remove LF Port Mgr objects
if port_mgr_json is not None:
logger.debug("Removing old stations ")
for name in list(port_mgr_json):
# logger.info(name)
# alias is the eid (ex: 1.1.eth0)
for alias in list(name):
# logger.info(alias)
port_type = name[alias]['port type']
# logger.info(port_type)
if port_type != 'Ethernet' and port_type != 'WIFI-Radio' and port_type != 'NA':
info = self.name_to_eid(alias)
port_mgr_resource = str(info[1])
if port_mgr_resource in self.resource or 'all' in self.resource:
req_url = "cli-json/rm_vlan"
data = {
"shelf": info[0],
"resource": info[1],
"port": info[2]
}
# logger.info(data)
logger.debug(f"Removing {alias}")
super().json_post(req_url, data)
time.sleep(1)
else:
logger.info("No further ports found")
still_looking_san = False
logger.debug(f"port_mgr_clean still_looking_san {still_looking_san}")
if not still_looking_san:
self.port_mgr_done = True
return still_looking_san
def bridge_clean(self):
still_looking_br = True
iterations_br = 0
# TODO: Merge this w/ prot deletion logic
while still_looking_br and iterations_br <= 10:
iterations_br += 1
logger.debug(f"bridge_clean: iterations_br: {iterations_br}")
try:
# br_json = super().json_get("port/1/1/list?field=alias")['interfaces']
br_json = super().json_get("/port/?fields=port+type,alias")['interfaces']
except TypeError:
br_json = None
# get and remove current stations
if br_json is not None:
# logger.info(br_json)
logger.debug("Removing old bridges")
for name in list(br_json):
for alias in list(name):
port_type = name[alias]['port type']
# if 'br' in alias:
if 'Bridge' in port_type:
# logger.info(alias)
info = self.name_to_eid(alias)
bridge_resource = str(info[1])
if bridge_resource in self.resource or 'all' in self.resource:
req_url = "cli-json/rm_vlan"
data = {
"shelf": info[0],
"resource": info[1],
"port": info[2]
}
# logger.info(data)
logger.debug(f"Removing {alias}")
super().json_post(req_url, data)
time.sleep(1)
else:
logger.info("No further bridge ports found")
still_looking_br = False
logger.debug(f"clean_bridge still_looking_br {still_looking_br}")
if not still_looking_br:
self.br_done = True
return still_looking_br
# Some test have various station names or a station named 1.1.eth2
def misc_clean(self):
still_looking_misc = True
iterations_misc = 0
while still_looking_misc and iterations_misc <= 10:
iterations_misc += 1
logger.debug(f"misc_clean: iterations_misc: {iterations_misc}")
try:
# misc_json = super().json_get("port/1/1/list?field=alias")['interfaces']
misc_json = super().json_get("/port/?fields=alias")['interfaces']
except TypeError:
misc_json = None
# get and remove current stations
if misc_json is not None:
# logger.info(misc_json)
logger.debug("Removing misc station names phy, 1.1.eth (malformed station name) ")
for name in list(misc_json):
for alias in list(name):
if 'phy' in alias and 'wiphy' not in alias:
# logger.info(alias)
info = self.name_to_eid(alias)
misc_resource = str(info[1])
if misc_resource in self.resource or 'all' in self.resource:
req_url = "cli-json/rm_vlan"
data = {
"shelf": info[0],
"resource": info[1],
"port": info[2]
}
# logger.info(data)
logger.debug(f"Removing {alias}")
super().json_post(req_url, data)
if '1.1.1.1.eth' in alias:
logger.debug(f"alias 1.1.1.1.eth {alias}")
# need to hand construct for delete.
info = alias.split('.')
logger.debug(f'info {info}')
req_url = "cli-json/rm_vlan"
# info_2 = "{info2}.{info3}.{info4}".format(info2=info[2], info3=info[3], info4=info[4])
misc_resource = str(info[3])
if misc_resource in self.resource or 'all' in self.resource:
data = {
"shelf": info[2],
"resource": info[3],
"port": info[4]
}
# logger.info(data)
logger.debug(f"Removing {alias}")
super().json_post(req_url, data)
time.sleep(1)
else:
logger.info("No further miscellaneous ports found")
still_looking_misc = False
logger.debug(f"clean_misc still_looking_misc {still_looking_misc}")
if not still_looking_misc:
self.misc_done = True
return still_looking_misc
def sanitize_all(self):
"""Run comprehensive, multi-step cleanup
1: Delete Layer-3 CXs
2: Delete Layer-3 endpoints
3: Delete Layer-4 endpoints
4: Delete ports
NOTE: When deleting ports before Layer-3 CXs, any CXs
which use the deleted ports will then appear as phantom
"""
# 1. clean Layer-3 tab:
finished_clean_cxs = self.cxs_clean()
logger.debug(f"clean_cxs: finished_clean_cxs {finished_clean_cxs}")
# 2. clean L3 Endps tab:
finished_clean_endp = self.layer3_endp_clean()
logger.debug(f"layer3_clean_endp: finished_clean_endp {finished_clean_endp}")
# 3. clean Layer 4-7 tab:
finished_clean_l4 = self.layer4_endp_clean()
logger.debug(f"clean_l4_endp: finished_clean_l4 {finished_clean_l4}")
# 4. clean Port Mgr tab:
finished_clean_port_mgr = self.port_mgr_clean()
logger.debug(f"clean_sta: finished_clean_port_mgr {finished_clean_port_mgr}")
def parse_args():
parser = argparse.ArgumentParser(
prog='lf_cleanup.py',
formatter_class=argparse.RawTextHelpFormatter,
epilog='''\
Clean up cxs and endpoints
''',
description=r'''
NAME: lf_cleanup.py
PURPOSE: Remove present test configuration, resetting system(s) to
a reproducible base state.
EXAMPLE:
# Remove all stations with CLI
./lf_cleanup.py --sta
# Remove all stations with CLI for a specific resource
./lf_cleanup.py --sta --resource 2
# Full cleanup (ports, L3 CXs/endpoints, L4-7 endpoints with CLI
./lf_cleanup.py --sanitize
# Remove all CXs and endpoints with CLI
./lf_cleanup.py --cxs
# Remove all endpoints with CLI
./lf_cleanup.py --endp
# Remove all bridge with CLI
./lf_cleanup.py --br
# Remove all STAs with unexpected names with CLI (often from misuse of or bugs in automation)
# This includes names 'phy' (not 'wiphy') and '1.1.eth'
./lf_cleanup.py --misc
# Remove all stations with JSON
"args": ["--mgr", "192.168.30.12", "--resource", "1", "--sta"]
# Full cleanup (ports, L3 CXs/endpoints, L4-7 endpoints with JSON
"args": ["--mgr", "192.168.30.12", "--resource", "1", "--sanitize"]
# Remove all CXs and endpoints with JSON
"args": ["--mgr", "192.168.30.12", "--resource", "1", "--cxs"]
# Remove all endpoints with JSON
"args": ["--mgr", "192.168.30.12", "--resource", "1", "--endp"]
# Remove all bridge with JSON
"args": ["--mgr", "192.168.30.12", "--resource", "1", "--br"]
# Remove all STAs with unexpected names with CLI (often from misuse of or bugs in automation)
# This includes names 'phy' (not 'wiphy') and '1.1.eth'
"args": ["--mgr", "192.168.30.12", "--resource", "1", "--misc"]
SCRIPT_CLASSIFICATION:
Deletion
SCRIPT_CATEGORIES:
Functional
NOTES: The script will only cleanup what is present in the GUI. If object creation (e.g. port or CX)
is in process but the object is not yet present in the GUI, then this script may need to be run
multiple times for deletion to take effect.
VERIFIED_ON:
Working date: 03/17/2023
Build version: 5.4.6
Kernel version: 5.19.17+
LICENSE: Free to distribute and modify. LANforge systems must be licensed.
Copyright (C) 2020-2026 Candela Technologies Inc
''')
# Base options
parser.add_argument('--mgr',
'--lfmgr',
dest='mgr',
help='--mgr <hostname for where LANforge GUI is running>',
default='localhost')
# Cleanup configuration options
parser.add_argument('--res',
'--resource',
dest='resource',
help='--resource <realm resource> to clear a specific resource, or --resource <all> to cleanup all resources',
default='all')
parser.add_argument('--cx',
'--cxs',
dest='cxs',
help="Delete all Layer-3 CXs and Layer-3 endpoints",
action='store_true')
parser.add_argument('--l3_endp',
help="Delete all Layer-3 endpoints with no associated L3 CX",
action='store_true')
parser.add_argument('--sta',
'--station',
'--stations',
dest='sta',
help="Delete all WiFi stations",
action='store_true')
parser.add_argument('--port',
'--ports',
'--port_mgr',
dest='port_mgr',
help="Delete all virtual ports (e.g. WiFi station, MAC-VLAN, virtual AP, bridge). "
"Does not delete physical ports (e.g. Ethernet, WiFi radio)",
action='store_true')
parser.add_argument('--br',
'--bridge',
'--bridges',
dest='br',
help="Delete all bridge ports",
action='store_true')
parser.add_argument('--misc',
help="Attempts to delete stations w/ misconfigured names",
action='store_true')
parser.add_argument('--layer4',
help="Delete all created L4-7 endpoints ",
action='store_true')
parser.add_argument('--sanitize',
help="Equivalent to '--port_mgr', '--cxs', and '--layer4'",
action='store_true')
parser.add_argument('--sleep',
help="Time in seconds to sleep after cleanup",
default=0)
# Logging configuration options
parser.add_argument("--debug",
help="Enable debug logging",
action="store_true")
parser.add_argument("--lf_logger_config_json",
help="Logger JSON configuration file")
parser.add_argument('--log_level', default=None,
help='Set logging level: debug | info | warning | error | critical')
# Misc options
parser.add_argument('--help_summary',
help='Show summary of what this script does',
default=None,
action="store_true")
return parser.parse_args()
def validate_args(args):
"""Ensure arguments specified for program are valid."""
if not (args.cxs
or args.l3_endp
or args.sta
or args.br
or args.misc
or args.port_mgr
or args.layer4
or args.sanitize):
logger.error("No required clean option specified. Re-run with '--help' for more information.")
exit(1)
def main():
help_summary = '''\
This script is used for cleaning the cross-connections, layer-3-endpoints, stations and bridges in Lanforge.
This script is also used to sanitize the lanforge unit, which means will clean the Port Mgr, Layer-3, L3 Endps,
and Layer 4-7 tabs.
'''
args = parse_args()
# Print help summary
if args.help_summary:
print(help_summary)
exit(0)
validate_args(args)
# Configure logging
logger_config = lf_logger_config.lf_logger_config()
logger_config.set_level(level=args.log_level)
logger_config.set_json(json_file=args.lf_logger_config_json)
if args.debug:
logger_config.set_level("debug")
clean = lf_clean(host=args.mgr,
resource=args.resource,
clean_cxs=args.cxs,
clean_endp=args.l3_endp,
clean_sta=args.sta,
clean_port_mgr=args.port_mgr,
clean_misc=args.misc)
logger.debug("cleaning cxs: {cxs} endpoints: {endp} stations: {sta} start".format(cxs=args.cxs, endp=args.l3_endp, sta=args.sta))
response = clean.get_json1()
logger.debug(response)
logger.debug("The objects that are present in the port Manager")
for i in range(len(response["interfaces"])):
response2 = list(response["interfaces"][i].keys())
logger.debug(response2)
if args.cxs:
logger.info("Deleting Layer-3 CXs")
logger.info("Requesting CX cleanup will also cleanup endpoints")
clean.cxs_clean()
clean.layer3_endp_clean()
if args.l3_endp:
logger.info("Deleting Layer-3 endpoints")
clean.layer3_endp_clean()
if args.sta:
logger.info("Deleting stations")
clean.sta_clean()
if args.port_mgr:
logger.info("Deleting ports")
clean.port_mgr_clean()
if args.br:
logger.info("Deleting bridges")
clean.bridge_clean()
if args.misc:
logger.info("Deleting miscellaneous ports")
clean.misc_clean()
if args.layer4:
logger.info("Deleting Layer-4 endpoints")
clean.layer4_endp_clean()
if args.sanitize:
logger.info("Deleting ports, Layer-3 CXs and endpoints, and Layer-4 endpoints")
clean.sanitize_all()
# Optional sleep after performing requested cleanup
if args.sleep > 0:
logger.info(f"Sleeping for {args.sleep} seconds post cleanup")
time.sleep(args.sleep)
logger.info("Requested cleanup complete")
if __name__ == "__main__":
main()