-
Notifications
You must be signed in to change notification settings - Fork 25
/
realm.py
executable file
·1197 lines (1070 loc) · 46.1 KB
/
realm.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
#!/usr/bin/env python3
# flake8: noqa
# The Realm Class is inherited by most python tests. Realm Class inherites from LFCliBase.
# The Realm Class contains the configurable components for LANforge,
# For example L3 / L4 cross connects, stations. Also contains helper methods
# http://www.candelatech.com/cookbook.php?vol=cli&book=Python_Create_Test_Scripts_With_the_Realm_Class
# Written by Candela Technologies Inc.
# Updated by:
import sys
import os
import importlib
import re
import time
from pprint import pprint
from pprint import pformat
import logging
logger = logging.getLogger(__name__)
# ---- ---- ---- ---- LANforge Base Imports ---- ---- ---- ----
sys.path.append(os.path.join(os.path.abspath(__file__ + "../../../")))
LANforge = importlib.import_module("py-json.LANforge")
LFUtils = importlib.import_module("py-json.LANforge.LFUtils")
lfcli_base = importlib.import_module("py-json.LANforge.lfcli_base")
LFCliBase = lfcli_base.LFCliBase
# ---- ---- ---- ---- Profile Imports ---- ---- ---- ----
l3_cxprofile = importlib.import_module("py-json.l3_cxprofile")
L3CXProfile = l3_cxprofile.L3CXProfile
l4_cxprofile = importlib.import_module("py-json.l4_cxprofile")
L4CXProfile = l4_cxprofile.L4CXProfile
lf_attenmod = importlib.import_module("py-json.lf_attenmod")
ATTENUATORProfile = lf_attenmod.ATTENUATORProfile
multicast_profile = importlib.import_module("py-json.multicast_profile")
MULTICASTProfile = multicast_profile.MULTICASTProfile
http_profile = importlib.import_module("py-json.http_profile")
HTTPProfile = http_profile.HTTPProfile
station_profile = importlib.import_module("py-json.station_profile")
StationProfile = station_profile.StationProfile
fio_endp_profile = importlib.import_module("py-json.fio_endp_profile")
FIOEndpProfile = fio_endp_profile.FIOEndpProfile
test_group_profile = importlib.import_module("py-json.test_group_profile")
TestGroupProfile = test_group_profile.TestGroupProfile
dut_profile = importlib.import_module("py-json.dut_profile")
DUTProfile = dut_profile.DUTProfile
vap_profile = importlib.import_module("py-json.vap_profile")
VAPProfile = vap_profile.VAPProfile
mac_vlan_profile = importlib.import_module("py-json.mac_vlan_profile")
MACVLANProfile = mac_vlan_profile.MACVLANProfile
wifi_monitor_profile = importlib.import_module("py-json.wifi_monitor_profile")
WifiMonitor = wifi_monitor_profile.WifiMonitor
gen_cxprofile = importlib.import_module("py-json.gen_cxprofile")
GenCXProfile = gen_cxprofile.GenCXProfile
qvlan_profile = importlib.import_module("py-json.qvlan_profile")
QVLANProfile = qvlan_profile.QVLANProfile
port_utils = importlib.import_module("py-json.port_utils")
PortUtils = port_utils.PortUtils
lfdata = importlib.import_module("py-json.lfdata")
LFDataCollection = lfdata.LFDataCollection
vr_profile2 = importlib.import_module("py-json.vr_profile2")
VRProfile = vr_profile2.VRProfile
def wpa_ent_list():
return [
"DEFAULT",
"NONE",
"WPA-PSK",
"FT-PSK",
"FT-EAP",
"FT-SAE",
"FT-EAP-SHA384",
"WPA-EAP",
"OSEN",
"IEEE8021X",
"WPA-PSK-SHA256",
"WPA-EAP-SHA256",
"WPA-PSK WPA-EAP",
"WPA-PSK-SHA256 WPA-EAP-SHA256",
"WPA-PSK WPA-EAP WPA-PSK-SHA256 WPA-EAP-SHA256"
"SAE",
"WPA-EAP-SUITE-B",
"WPA-EAP-SUITE-B-192",
"FILS-SHA256",
"FILS-SHA384",
"OWE"
]
class Realm(LFCliBase):
def __init__(self,
lfclient_host="localhost",
lfclient_port=8080,
debug_=False,
_exit_on_error=False,
_exit_on_fail=False,
_proxy_str=None,
_capture_signal_list=None):
super().__init__(_lfjson_host=lfclient_host,
_lfjson_port=lfclient_port,
_debug=debug_,
_exit_on_error=_exit_on_error,
_exit_on_fail=_exit_on_fail,
_proxy_str=_proxy_str,
_capture_signal_list=_capture_signal_list)
if _capture_signal_list is None:
_capture_signal_list = []
self.debug = debug_
# if debug_:
# logger.debug("Realm _proxy_str: %s" % _proxy_str)
# logger.debug(pformat(_proxy_str))
self.check_connect()
self.chan_to_freq = {}
self.freq_to_chan = {}
chan = 1
for freq in range(2412, 2472, 5):
self.freq_to_chan[freq] = chan
self.chan_to_freq[chan] = freq
chan += 1
self.chan_to_freq[14] = 2484
self.chan_to_freq[34] = 5170
self.chan_to_freq[36] = 5180
self.chan_to_freq[38] = 5190
self.chan_to_freq[40] = 5200
self.chan_to_freq[42] = 5210
self.chan_to_freq[44] = 5220
self.chan_to_freq[46] = 5230
self.chan_to_freq[48] = 5240
self.chan_to_freq[52] = 5260
self.chan_to_freq[56] = 5280
self.chan_to_freq[60] = 5300
self.chan_to_freq[64] = 5320
self.chan_to_freq[100] = 5500
self.chan_to_freq[104] = 5520
self.chan_to_freq[108] = 5540
self.chan_to_freq[112] = 5560
self.chan_to_freq[116] = 5580
self.chan_to_freq[120] = 5600
self.chan_to_freq[124] = 5620
self.chan_to_freq[128] = 5640
self.chan_to_freq[132] = 5660
self.chan_to_freq[136] = 5680
self.chan_to_freq[140] = 5700
self.chan_to_freq[144] = 5720
self.chan_to_freq[149] = 5745
self.chan_to_freq[153] = 5765
self.chan_to_freq[157] = 5785
self.chan_to_freq[161] = 5805
self.chan_to_freq[165] = 5825
self.chan_to_freq[169] = 5845
self.chan_to_freq[173] = 5865
self.freq_to_chan[2484] = 14
self.freq_to_chan[5170] = 34
self.freq_to_chan[5180] = 36
self.freq_to_chan[5190] = 38
self.freq_to_chan[5200] = 40
self.freq_to_chan[5210] = 42
self.freq_to_chan[5220] = 44
self.freq_to_chan[5230] = 46
self.freq_to_chan[5240] = 48
self.freq_to_chan[5260] = 52
self.freq_to_chan[5280] = 56
self.freq_to_chan[5300] = 60
self.freq_to_chan[5320] = 64
self.freq_to_chan[5500] = 100
self.freq_to_chan[5520] = 104
self.freq_to_chan[5540] = 108
self.freq_to_chan[5560] = 112
self.freq_to_chan[5580] = 116
self.freq_to_chan[5600] = 120
self.freq_to_chan[5620] = 124
self.freq_to_chan[5640] = 128
self.freq_to_chan[5660] = 132
self.freq_to_chan[5680] = 136
self.freq_to_chan[5700] = 140
self.freq_to_chan[5720] = 144
self.freq_to_chan[5745] = 149
self.freq_to_chan[5765] = 153
self.freq_to_chan[5785] = 157
self.freq_to_chan[5805] = 161
self.freq_to_chan[5825] = 165
self.freq_to_chan[5845] = 169
self.freq_to_chan[5865] = 173
# 4.9Ghz police band
self.chan_to_freq[183] = 4915
self.chan_to_freq[184] = 4920
self.chan_to_freq[185] = 4925
self.chan_to_freq[187] = 4935
self.chan_to_freq[188] = 4940
self.chan_to_freq[189] = 4945
self.chan_to_freq[191] = 4955
self.chan_to_freq[192] = 4960
self.chan_to_freq[194] = 4970
self.chan_to_freq[195] = 4975
self.chan_to_freq[196] = 4980
#6E
self.chan_to_freq[191] = 5955
self.chan_to_freq[195] = 5975
self.chan_to_freq[199] = 5995
self.chan_to_freq[203] = 6015
self.chan_to_freq[207] = 6035
self.chan_to_freq[211] = 6055
self.chan_to_freq[215] = 6075
self.chan_to_freq[219] = 6095
self.chan_to_freq[223] = 6115
self.chan_to_freq[227] = 6135
self.chan_to_freq[231] = 6155
self.chan_to_freq[235] = 6175
self.chan_to_freq[239] = 6195
self.chan_to_freq[243] = 6215
self.chan_to_freq[247] = 6235
self.chan_to_freq[251] = 6255
self.chan_to_freq[255] = 6275
self.chan_to_freq[259] = 6295
self.chan_to_freq[263] = 6315
self.chan_to_freq[267] = 6335
self.chan_to_freq[271] = 6355
self.chan_to_freq[275] = 6375
self.chan_to_freq[279] = 6395
self.chan_to_freq[283] = 6415
self.chan_to_freq[287] = 6435
self.chan_to_freq[291] = 6455
self.chan_to_freq[295] = 6475
self.chan_to_freq[299] = 6495
self.chan_to_freq[303] = 6515
self.chan_to_freq[307] = 6535
self.chan_to_freq[311] = 6555
self.chan_to_freq[315] = 6575
self.chan_to_freq[319] = 6595
self.chan_to_freq[323] = 6615
self.chan_to_freq[327] = 6635
self.chan_to_freq[331] = 6655
self.chan_to_freq[335] = 6675
self.chan_to_freq[339] = 6695
self.chan_to_freq[343] = 6715
self.chan_to_freq[347] = 6735
self.chan_to_freq[351] = 6755
self.chan_to_freq[355] = 6775
self.chan_to_freq[359] = 6795
self.chan_to_freq[363] = 6815
self.chan_to_freq[367] = 6835
self.chan_to_freq[371] = 6855
self.chan_to_freq[375] = 6875
self.chan_to_freq[379] = 6895
self.chan_to_freq[383] = 6915
self.chan_to_freq[387] = 6935
self.chan_to_freq[391] = 6955
self.chan_to_freq[395] = 6975
self.chan_to_freq[399] = 6995
self.chan_to_freq[403] = 7015
self.chan_to_freq[407] = 7035
self.chan_to_freq[411] = 7055
self.chan_to_freq[415] = 7075
self.chan_to_freq[419] = 7095
self.chan_to_freq[423] = 7115
self.freq_to_chan[4915] = 183
self.freq_to_chan[4920] = 184
self.freq_to_chan[4925] = 185
self.freq_to_chan[4935] = 187
self.freq_to_chan[4940] = 188
self.freq_to_chan[4945] = 189
self.freq_to_chan[4960] = 192
self.freq_to_chan[4970] = 194
self.freq_to_chan[5975] = 195
self.freq_to_chan[4980] = 196
#6E
self.freq_to_chan[5955] = 191
self.freq_to_chan[5975] = 195
self.freq_to_chan[5995] = 199
self.freq_to_chan[6015] = 203
self.freq_to_chan[6035] = 207
self.freq_to_chan[6055] = 211
self.freq_to_chan[6075] = 215
self.freq_to_chan[6095] = 219
self.freq_to_chan[6115] = 223
self.freq_to_chan[6135] = 227
self.freq_to_chan[6155] = 231
self.freq_to_chan[6175] = 235
self.freq_to_chan[6195] = 239
self.freq_to_chan[6215] = 243
self.freq_to_chan[6235] = 247
self.freq_to_chan[6255] = 251
self.freq_to_chan[6275] = 255
self.freq_to_chan[6295] = 259
self.freq_to_chan[6315] = 263
self.freq_to_chan[6335] = 267
self.freq_to_chan[6355] = 271
self.freq_to_chan[6375] = 275
self.freq_to_chan[6395] = 279
self.freq_to_chan[6415] = 283
self.freq_to_chan[6435] = 287
self.freq_to_chan[6455] = 291
self.freq_to_chan[6475] = 295
self.freq_to_chan[6495] = 299
self.freq_to_chan[6515] = 303
self.freq_to_chan[6535] = 307
self.freq_to_chan[6555] = 311
self.freq_to_chan[6575] = 315
self.freq_to_chan[6595] = 319
self.freq_to_chan[6615] = 323
self.freq_to_chan[6635] = 327
self.freq_to_chan[6655] = 331
self.freq_to_chan[6675] = 335
self.freq_to_chan[6695] = 339
self.freq_to_chan[6715] = 343
self.freq_to_chan[6735] = 347
self.freq_to_chan[6755] = 351
self.freq_to_chan[6775] = 355
self.freq_to_chan[6795] = 359
self.freq_to_chan[6815] = 363
self.freq_to_chan[6835] = 367
self.freq_to_chan[6855] = 371
self.freq_to_chan[6875] = 375
self.freq_to_chan[6895] = 379
self.freq_to_chan[6915] = 383
self.freq_to_chan[6935] = 387
self.freq_to_chan[9655] = 391
self.freq_to_chan[6975] = 395
self.freq_to_chan[6995] = 399
self.freq_to_chan[7015] = 403
self.freq_to_chan[7035] = 407
self.freq_to_chan[7055] = 411
self.freq_to_chan[7075] = 415
self.freq_to_chan[7095] = 419
self.freq_to_chan[7115] = 423
def wait_until_ports_appear(self, sta_list=None, debug_=False, timeout=360):
if (sta_list is None) or (len(sta_list) < 1):
logger.info("realm.wait_until_ports_appear: no stations provided")
return
LFUtils.wait_until_ports_appear(base_url=self.lfclient_url,
port_list=sta_list,
debug=debug_,
timeout=timeout)
def wait_until_ports_disappear(self, sta_list=None, debug_=False):
if (sta_list is None) or (len(sta_list) < 1):
logger.info("realm.wait_until_ports_appear: no stations provided")
return
LFUtils.wait_until_ports_disappear(base_url=self.lfclient_url,
port_list=sta_list,
debug=debug_)
def rm_port(self, port_eid, check_exists=True, debug_=None):
if port_eid is None:
logger.critical("realm.rm_port: want a port eid like 1.1.eth1")
raise ValueError("realm.rm_port: want a port eid like 1.1.eth1")
if debug_ is None:
debug_ = self.debug
req_url = "/cli-json/rm_vlan"
eid = self.name_to_eid(port_eid)
if check_exists:
if not self.port_exists(port_eid, debug=False):
return False
data = {
"shelf": eid[0],
"resource": eid[1],
"port": eid[2]
}
self.json_post(req_url, data, debug_=debug_)
return True
def port_exists(self, port_eid, debug=None):
if debug is None:
debug = self.debug
eid = self.name_to_eid(port_eid)
current_stations = self.json_get("/port/%s/%s/%s?fields=alias" % (eid[0], eid[1], eid[2]),
debug_=debug)
if current_stations:
return True
return False
def admin_up(self, port_eid):
# logger.info("186 admin_up port_eid: "+port_eid)
eid = self.name_to_eid(port_eid)
resource = eid[1]
port = eid[2]
request = LFUtils.port_up_request(resource_id=resource, port_name=port, debug_on=self.debug)
# logger.info("192.admin_up request: resource: %s port_name %s"%(resource, port))
dbg_param = ""
if logger.getEffectiveLevel() == logging.DEBUG:
#logger.info("enabling url debugging")
dbg_param = "?__debug=1"
collected_responses = list()
self.json_post("/cli-json/set_port%s" % dbg_param, request, debug_=self.debug,
response_json_list_=collected_responses)
# TODO: when doing admin-up ath10k radios, want a LF complaint about a license exception
# if len(collected_responses) > 0: ...
def admin_down(self, port_eid):
eid = self.name_to_eid(port_eid)
resource = eid[1]
port = eid[2]
request = LFUtils.port_down_request(resource_id=resource, port_name=port)
self.json_post("/cli-json/set_port", request)
def reset_port(self, port_eid):
eid = self.name_to_eid(port_eid)
resource = eid[1]
port = eid[2]
request = LFUtils.port_reset_request(resource_id=resource, port_name=port)
self.json_post("cli-json/reset_port", request)
def rm_cx(self, cx_name):
req_url = "cli-json/rm_cx"
data = {
"test_mgr": "ALL",
"cx_name": cx_name
}
self.json_post(req_url, data)
def rm_endp(self, ename, debug_=False, suppress_related_commands_=True):
req_url = "cli-json/rm_endp"
data = {
"endp_name": ename
}
self.json_post(req_url, data, debug_=debug_, suppress_related_commands_=suppress_related_commands_)
def add_vrcx_(self, vr_name, local_dev, dhcp_min, dhcp_max, resource=1, debug_=False,
suppress_related_commands_=True):
req_url = "/cli-json/add_vrcx"
data = {
"shelf": 1,
"resource": resource,
"vr_name": vr_name,
"local_dev": local_dev,
"dhcp_min": dhcp_min,
"dhcp_max": dhcp_max}
logger.info("Modifying Connection...")
self.json_post(req_url, data, debug_=debug_, suppress_related_commands_=suppress_related_commands_)
def netsmith_apply(self, resource, debug_=False, suppress_related_commands_=True):
data = {
"shelf": 1,
"resource": resource}
logger.info("Applying the Netsmith Config")
self.json_post("/cli-json/apply_vr_cfg", data, debug_=debug_,
suppress_related_commands_=suppress_related_commands_)
time.sleep(1)
def set_endp_details(self, ename, pkt_to_send, debug_=False, suppress_related_commands_=True):
req_url = "cli-json/set_endp_details"
pkt_to_send = pkt_to_send
data = {
"name": ename,
"pkts_to_send": pkt_to_send
}
self.json_post(req_url, data, debug_=debug_, suppress_related_commands_=suppress_related_commands_)
def set_endp_tos(self, ename, _tos, debug_=False, suppress_related_commands_=True):
req_url = "cli-json/set_endp_tos"
tos = _tos
# Convert some human readable values to numeric needed by LANforge.
if _tos == "BK":
tos = "32"
if _tos == "BE":
tos = "96"
if _tos == "VI":
tos = "128"
if _tos == "VO":
tos = "192"
if _tos == "Voice":
tos = "184"
if _tos == "Video":
tos = "56"
data = {
"name": ename,
"tos": tos
}
self.json_post(req_url, data, debug_=debug_, suppress_related_commands_=suppress_related_commands_)
def stop_cx(self, cx_name):
self.json_post("/cli-json/set_cx_state", {
"test_mgr": "ALL",
"cx_name": cx_name,
"cx_state": "STOPPED"
}, debug_=self.debug)
# def quiesce_cx(self, cx_name):
def drain_stop_cx(self, cx_name):
self.json_post("/cli-json/set_cx_state", {
"test_mgr": "ALL",
"cx_name": cx_name,
"cx_state": "QUIESCE"
}, debug_=self.debug)
def cleanup_cxe_prefix(self, prefix):
cx_list = self.cx_list()
if cx_list:
for cx_name in cx_list:
if cx_name.startswith(prefix):
self.rm_cx(cx_name)
endp_list = self.json_get("/endp/list")
if endp_list:
if 'endpoint' in endp_list:
endp_list = list(endp_list['endpoint'])
for idx in range(len(endp_list)):
endp_name = list(endp_list[idx])[0]
if endp_name.startswith(prefix):
self.rm_endp(endp_name)
else:
if self.debug:
logger.debug("cleanup_cxe_prefix no endpoints: endp_list{}".format(endp_list))
def channel_freq(self, channel_=0):
return self.chan_to_freq[channel_]
def freq_channel(self, freq_=0):
return self.freq_to_chan[freq_]
# checks for OK or BUSY when querying cli-json/cv+is_built
def wait_while_building(self, debug_=False):
response_json = []
data = {
"cmd": "cv is_built"
}
last_response = "BUSY"
dbg_param = ""
if debug_:
dbg_param = "?__debug=1"
while last_response != "YES":
self.json_post("/gui-json/cmd%s" % dbg_param, data, debug_=debug_, response_json_list_=response_json)
# logger.info(pformat(response_json))
last_response = response_json[0]["LAST"]["response"]
if last_response != "YES":
last_response = None
response_json = []
time.sleep(1)
else:
return
return
# loads a database
def load(self, name):
if (name is None) or (name == ""):
logger.critical(
"Realm::load: wants a test scenario database name, please find one in the Status tab of the GUI")
raise ValueError(
"Realm::load: wants a test scenario database name, please find one in the Status tab of the GUI")
data = {
"name": name,
"action": "overwrite",
"clean_dut": "yes",
"clean_chambers": "yes"
}
self.json_post("/cli-json/load", _data=data, debug_=self.debug)
time.sleep(1)
# Returns json response from webpage of all layer 3 cross connects
def cx_list(self):
response = self.json_get("/cx/list")
return response
def waitUntilEndpsAppear(self, these_endp, debug=False, timeout=300):
return self.wait_until_endps_appear(these_endp, debug=debug, timeout=timeout)
def wait_until_endps_appear(self, these_endp, debug=False, timeout=100):
wait_more = True
count = 0
while wait_more:
wait_more = False
endp_list = self.json_get("/endp/list")
found_endps = {}
# LAN-2064 the endp_list may be a dict, it shouldbe a list of dictionaries,
# need to modify to handle single endpoint as compared to two
# Realm, wait for endp if there is a single end point it will time out.
# since json type difference between single station and multiple station
# logger.debug("endp_list is type {endp} keys {keys}".format(endp=type(end_list),keys=endp_list.keys()))
if 'endpoint' in endp_list.keys():
logger.debug(" endpoint type {}".format(type(endp_list['endpoint'])))
if not isinstance(endp_list['endpoint'], list):
endp_list['endpoint']= [{endp_list['endpoint']['name']: endp_list['endpoint']}]
logger.debug("endp_list {}".format(endp_list))
if debug:
logger.debug("Waiting on endpoint endp_list {}".format(endp_list))
if endp_list and ("items" not in endp_list):
try:
endp_list = list(endp_list['endpoint'])
for idx in range(len(endp_list)):
name = list(endp_list[idx])[0]
found_endps[name] = name
except:
logger.info(
"non-fatal exception endp_list = list(endp_list['endpoint'] did not exist, will wait some more")
pprint(endp_list)
for req in these_endp:
if req not in found_endps:
if debug:
logger.debug("Waiting on endpoint:{req} count:{count}".format(req=req,count=count) )
wait_more = True
count += 1
if count > timeout:
logger.error("ERROR: Could not find all endpoints: %s" % these_endp)
return False
if wait_more:
time.sleep(1)
return True
def waitUntilCxsAppear(self, these_cx, debug=False, timeout=100):
return self.wait_until_cxs_appear(these_cx, debug=debug, timeout=timeout)
def wait_until_cxs_appear(self, these_cx, debug=False, timeout=100):
wait_more = True
count = 0
while wait_more:
wait_more = False
found_cxs = {}
cx_list = self.cx_list()
not_cx = ['warnings', 'errors', 'handler', 'uri', 'items']
if cx_list:
for cx_name in cx_list:
if cx_name in not_cx:
continue
found_cxs[cx_name] = cx_name
for req in these_cx:
if req not in found_cxs:
if debug:
logger.debug("Waiting on CX: %s" % req)
wait_more = True
count += 1
if count > timeout:
if debug:
logger.error("ERROR: Failed to find all cxs: %s" % these_cx)
return False
if wait_more:
time.sleep(1)
return True
# def wait_until_database_loaded(self):
# Returns map of all stations with port+type == WIFI-STATION
# Key is the EID, value is the map of key/values for the port values.
def station_map(self):
response = super().json_get("/port/list?fields=port,_links,alias,device,port+type")
if (response is None) or ("interfaces" not in response):
logger.critical(pformat(response))
logger.critical("station_list: incomplete response, halting")
exit(1)
sta_map = {}
temp_map = LFUtils.portListToAliasMap(response)
for k, v in temp_map.items():
if v['port type'] == "WIFI-STA":
sta_map[k] = v
temp_map.clear()
del temp_map
del response
return sta_map
# Returns list of all stations with port+type == WIFI-STATION
def station_list(self):
sta_list = []
response = super().json_get("/port/list?fields=_links,alias,device,port+type")
if (response is None) or ("interfaces" not in response):
logger.critical("station_list: incomplete response:")
logger.critical(pformat(response))
exit(1)
for x in range(len(response['interfaces'])):
for k, v in response['interfaces'][x].items():
if v['port type'] == "WIFI-STA":
sta_list.append(response['interfaces'][x])
del response
return sta_list
# Returns list of all ports
def port_list(self):
response = super().json_get("/port/list?fields=all")
if (response is None) or ("interfaces" not in response):
logger.info("port_list: incomplete response:")
logger.info(pformat(response))
return None
return response['interfaces']
# Returns list of all VAPs with "vap" in their name
def vap_list(self):
sta_list = []
response = super().json_get("/port/list?fields=_links,alias,device,port+type")
for x in range(len(response['interfaces'])):
for k, v in response['interfaces'][x].items():
if "vap" in v['device']:
sta_list.append(response['interfaces'][x])
return sta_list
# Returns all attenuators
def atten_list(self):
response = super().json_get("/atten/list")
return response['attenuators']
# EID is shelf.resource.atten-serno.atten-idx
def set_atten(self, eid, atten_ddb):
eid_toks = self.name_to_eid(eid, non_port=True)
req_url = "cli-json/set_attenuator"
data = {
"shelf": eid_toks[0],
"resource": eid_toks[1],
"serno": eid_toks[2],
"atten_idx": eid_toks[3],
"val": atten_ddb,
}
self.json_post(req_url, data)
# removes port by eid/eidpn
def remove_vlan_by_eid(self, eid):
if (eid is None) or (eid == ""):
logger.critical("removeVlanByEid wants eid like 1.1.sta0 but given[%s]" % eid)
raise ValueError("removeVlanByEid wants eid like 1.1.sta0 but given[%s]" % eid)
hunks = self.name_to_eid(eid)
# logger.info("- - - - - - - - - - - - - - - - -")
# logger.info(pformat(hunks))
# logger.info(pformat(self.lfclient_url))
# logger.info("- - - - - - - - - - - - - - - - -")
if (len(hunks) > 3) or (len(hunks) < 2):
raise ValueError("removeVlanByEid wants eid like 1.1.sta0 but given[%s]" % eid)
elif len(hunks) == 3:
LFUtils.removePort(hunks[1], hunks[2], self.lfclient_url)
else:
LFUtils.removePort(hunks[0], hunks[1], self.lfclient_url)
# Searches for ports that match a given pattern and returns a list of names
def find_ports_like(self, pattern="", _fields="_links,alias,device,port+type", resource=0, debug_=False):
if not pattern:
raise ValueError("find_ports_list wants a non-empty pattern")
if resource == 0:
url = "/port/1/list?fields=%s" % _fields
else:
url = "/port/1/%s/list?fields=%s" % (resource, _fields)
response = self.json_get(url)
if debug_:
logger.debug("# find_ports_like r:%s, u:%s #" % (resource, url))
logger.debug(pformat(response))
alias_map = LFUtils.portListToAliasMap(response, debug_=debug_)
if debug_:
logger.debug(pformat(alias_map))
prelim_map = {}
matched_map = {}
for name, record in alias_map.items():
try:
if debug_:
logger.debug("- prelim - - - - - - - - - - - - - - - - - - -")
logger.debug(pformat(record))
if record["port type"] == "WIFI-STA":
prelim_map[name] = record
except Exception as x:
self.error(x)
prefix = ""
try:
if pattern.find("+") > 0:
match = re.search(r"^([^+]+)[+]$", pattern)
if match.group(1):
prefix = match.group(1)
for port_eid, record in prelim_map.items():
if debug_:
logger.debug("name:", port_eid, " Group 1: ", match.group(1))
if port_eid.find(prefix) >= 0:
matched_map[port_eid] = record
elif pattern.find("*") > 0:
match = re.search(r"^([^\*]+)[*]$", pattern)
if match.group(1):
prefix = match.group(1)
if debug_:
logger.debug("group 1: ", prefix)
for port_eid, record in prelim_map.items():
if port_eid.find(prefix) >= 0:
matched_map[port_eid] = record
elif pattern.find("[") > 0:
# TODO: regex below might have too many hack escapes
match = re.search(r"^([^\[]+)\[(\d+)\.\.(\d+)\]$", pattern)
if match.group(0):
if debug_:
logger.debug("[group1]: ", match.group(1))
logger.debug("[group2]: ", match.group(2))
logger.debug("[group3]: ", match.group(3))
prefix = match.group(1)
for port_eid, record in prelim_map.items():
if port_eid.find(prefix) >= 0:
port_suf = record["device"][len(prefix):]
if (port_suf >= match.group(2)) and (port_suf <= match.group(3)):
# logger.info("%s: suffix[%s] between %s:%s" % (port_name, port_name, match.group(2), match.group(3))
matched_map[port_eid] = record
else:
for port_eid, record in prelim_map.items():
if port_eid.startswith(pattern):
matched_map[port_eid] = record
except ValueError as e:
self.error(e)
return matched_map
def name_to_eid(self, eid, debug=False, non_port=False):
if debug:
self.logg(level="debug", mesg="name_to_eid: " + str(eid))
if (type(eid) is list) or (type(eid) is tuple):
return eid
return LFUtils.name_to_eid(eid, non_port=non_port)
def dump_all_port_info(self):
return self.json_get('/port/all')
# timemout_sec of -1 means auto-calculate based on number of stations.
def wait_for_ip(self, station_list=None, ipv4=True, ipv6=False, timeout_sec=360, debug=False):
timeout_auto = False
if not (ipv4 or ipv6):
raise ValueError("wait_for_ip: ipv4 and/or ipv6 must be set!")
if timeout_sec >= 0:
if debug:
logger.debug("Waiting for ips, timeout: %i..." % timeout_sec)
else:
timeout_sec = 60 + len(station_list) * 5
if debug:
logger.debug("Auto-Timeout requested, using: %s" % timeout_sec)
stas_without_ip4s = {}
stas_without_ip6s = {}
sec_elapsed = 0
start_time = int(time.time())
# logger.info(station_list)
waiting_states = ["0.0.0.0", "NA", "", 'DELETED', 'AUTO']
if (station_list is None) or (len(station_list) < 1):
logger.critical("wait_for_ip: expects non-empty list of ports")
raise ValueError("wait_for_ip: expects non-empty list of ports")
wait_more = True
while wait_more:
wait_more = False
stas_without_ip4s = {}
stas_without_ip6s = {}
for sta_eid in station_list:
eid = self.name_to_eid(sta_eid)
response = super().json_get("/port/%s/%s/%s?fields=alias,ip,port+type,ipv6+address" %
(eid[0], eid[1], eid[2]))
# logger.info(pformat(response))
if (response is None) or ("interface" not in response):
logger.info("station_list: incomplete response for eid: %s: wait longer" % sta_eid)
logger.info(pformat(response))
wait_more = True
break
if ipv4:
v = response['interface']
if v['ip'] in waiting_states:
wait_more = True
stas_without_ip4s[sta_eid] = True
if debug:
logger.debug("Waiting for port %s to get IPv4 Address try %s / %s" % (sta_eid, sec_elapsed, timeout_sec))
else:
if debug:
logger.debug("Found IP: %s on port: %s" % (v['ip'], sta_eid))
if ipv6:
v = response['interface']
# logger.info(v)
ip6a = v['ipv6_address']
if ip6a != 'DELETED' and not ip6a.startswith('fe80') and ip6a != 'AUTO':
if debug:
logger.debug("Found IPv6: %s on port: %s" % (ip6a, sta_eid))
else:
stas_without_ip6s[sta_eid] = True
wait_more = True
if debug:
logger.debug("Waiting for port %s to get IPv6 Address try %s / %s, reported: %s." % (sta_eid, sec_elapsed, timeout_sec, ip6a))
# Check if we need to wait more but timed out. Otherwise, continue polling
cur_time = int(time.time())
if wait_more and (cur_time - start_time) > timeout_sec:
break # Timed out. Exit while loop
else:
sec_elapsed += 1
# If not all ports got IP addresses before timeout, and debugging is enabled, then
# add logging.
if len(stas_without_ip4s) + len(stas_without_ip6s) > 0:
if debug:
if len(stas_without_ip4s) > 0:
logger.info('%s did not acquire IPv4 addresses' % stas_without_ip4s.keys())
if len(stas_without_ip6s) > 0:
logger.info('%s did not acquire IPv6 addresses' % stas_without_ip6s.keys())
port_info = self.dump_all_port_info()
logger.debug(pformat(port_info))
return False
else:
if debug:
logger.debug("Found IPs for all requested ports.")
return True
def get_curr_num_ips(self, num_sta_with_ips=0, station_list=None, ipv4=True, ipv6=False, debug=False):
if debug:
logger.debug("checking number of stations with ips...")
waiting_states = ["0.0.0.0", "NA", ""]
if (station_list is None) or (len(station_list) < 1):
raise ValueError("check for num curr ips expects non-empty list of ports")
for sta_eid in station_list:
if debug:
logger.debug("checking sta-eid: %s" % sta_eid)
eid = self.name_to_eid(sta_eid)
response = super().json_get("/port/%s/%s/%s?fields=alias,ip,port+type,ipv6+address" %
(eid[0], eid[1], eid[2]))
if debug:
logger.debug(pformat(response))
if (response is None) or ("interface" not in response):
logger.info("station_list: incomplete response:")
logger.info(pformat(response))
# wait_more = True
break
if ipv4:
v = response['interface']
if v['ip'] in waiting_states:
if debug:
logger.debug("Waiting for port %s to get IPv4 Address." % sta_eid)
else:
if debug:
logger.debug("Found IP: %s on port: %s" % (v['ip'], sta_eid))
logger.debug("Incrementing stations with IP addresses found")
num_sta_with_ips += 1
else:
num_sta_with_ips += 1
if ipv6:
v = response['interface']
if v['ip'] in waiting_states:
if debug:
logger.debug("Waiting for port %s to get IPv6 Address." % sta_eid)
else:
if debug:
logger.debug("Found IP: %s on port: %s" % (v['ip'], sta_eid))
logger.debug("Incrementing stations with IP addresses found")
num_sta_with_ips += 1
else:
num_sta_with_ips += 1
return num_sta_with_ips
@staticmethod
def duration_time_to_milliseconds(time_string):
if isinstance(time_string, str):
pattern = re.compile("^(\d+)(\S+$)")
td = pattern.match(time_string)
if td:
dur_time = int(td.group(1))
dur_measure = str(td.group(2))
if dur_measure == "d":
duration_millisec = dur_time * 24 * 60 * 60 * 1000
elif dur_measure == "h":
duration_millisec = dur_time * 60 * 60 * 1000
elif dur_measure == "m":
duration_millisec = dur_time * 60 * 1000
elif dur_measure == "s":
duration_millisec = dur_time * 1 * 1000
elif dur_measure == "ms":
duration_millisec = dur_time * 1
else:
raise ValueError("Unknown value for time_string %s" % time_string)
else:
raise ValueError("Unknown value for time_string: %s" % time_string)
else:
raise ValueError("time_string must be of type str. Type %s provided" % type(time_string))
return duration_millisec
@staticmethod
def duration_time_to_seconds(time_string):
if isinstance(time_string, str):
pattern = re.compile("^(\d+)([dhms]$)")
td = pattern.match(time_string)