-
Notifications
You must be signed in to change notification settings - Fork 0
/
VMMigr_phase2.py
executable file
·1538 lines (1408 loc) · 70.1 KB
/
VMMigr_phase2.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
import os, sys, signal, argparse
import jinja2
from jinja2.utils import concat
import re
import requests
import time
import base64
import xmltodict
#import subprocess
import json
from pprint import pprint
from collections import OrderedDict
#import uuid
from pprint import pprint
import logging
import logging.handlers
import copy
import glbl
import common
pyVer = sys.version_info
if pyVer.major == 3:
import http.client as httplib
else:
import httplib
#import vnms
vnms = None
analy = None
cntlr = None
cust = None
admin = None
debug = 0
mlog = None
mdict = None
NOT_DEPLOYED = 0
MYLINES = 0
MYCOL = 0
class bcolors:
""" the background colors
"""
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
OKCHECK = '\033[96m'
OKWARN = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
def argcheck():
""" This performs adds the argument and checks the requisite inputs
"""
global args
mystr = os.path.basename(sys.argv[0])
parser = argparse.ArgumentParser(prog=os.path.basename(sys.argv[0]),description='%(prog)s Help:',usage='%(prog)s -f filename [options]', add_help=False)
parser.add_argument('-f','--file',required=True, help='input file [required ]' )
parser.add_argument('-r','--read',required=False, action='store_true', help='input file [required ]' )
parser.add_argument('-d','--debug',default=0, help='set/unset debug flag')
try:
args = vars(parser.parse_args())
except:
usage()
sys.exit(0)
def usage():
mystr = os.path.basename(sys.argv[0])
print(bcolors.OKCHECK)
print( """\
Usage:
To change versions use:
%(mystr)s --f/-f <infile>
To re-read input data :
%(mystr)s -f vm_phase3.json -r [ Note : the file MUST be vm_phase3.json and NOT vm_phase2.json ]
To add more debug:
%(mystr)s -f <infile> --debug/-d [0/1]
""" %locals())
print(bcolors.ENDC)
def json_loads(_str,**kwargs):
global mlog
try:
_jstr = json.loads(_str,**kwargs)
return _jstr
except Exception as ex:
mlog.error('Json load failed: {}'.format(ex))
sys.exit('Json load failed: {}'.format(ex))
def find_wan( _str):
if _str in glbl.vnms.data['wanNtwk']:
return glbl.vnms.data['wanNtwk'][_str]
else:
return None
def create_controller( _method, _uri,_payload,resp='200', name="Controller"):
global vnms, analy, cntlr, cust, mlog
mlog.info("In function " + create_controller.__name__)
mlog.warn(bcolors.OKWARN + "Have you performed a erase config on the controller: {0} and verified that services are running.\nIf not do so now".format(name) + bcolors.ENDC)
ret = yes_or_no2("Continue: " )
if ret == 0 or ret == 2 : return
resp = '200'
vdict = {}
vdict = {'body': _payload, 'resp': resp, 'method': _method, 'uri': _uri}
[status, resp_str] = common.call(vdict,content_type='json',ncs_cmd="no")
if status == 1:
mlog.warn("Creation of New Controller = {0} sucessful ".format(name))
if len (resp_str) > 3 :
newjstr = json_loads(resp_str)
mlog.info("Return json from POST on Controller: {0} = {1}".format(name,json.dumps(newjstr,indent=4)))
else:
mlog.error("Creation of New Controller = {0} NOT sucessful ".format(name))
sys.exit("Creation of New Controller = {0} NOT sucessful ".format(name))
# the below is payload of the POST not the response
jstr = json_loads(_payload)
mycntlr = None
for mcntlr in glbl.cntlr.data['new_cntlr']:
if mcntlr["controllerName"] == name:
mycntlr = mcntlr
break
if mycntlr is None:
mlog.error("Can not find controller: {0} in my list ".format(name))
sys.exit("Can not find controller: {0} in my list ".format(name))
# from the GET we need to save the public ip address of the controller. this is needed in phase 3
if "versanms.sdwan-controller-workflow" in jstr:
if "peerControllers" in jstr["versanms.sdwan-controller-workflow"]:
mycntlr["peerControllers"] = []
mycntlr["peerControllers"] = jstr["versanms.sdwan-controller-workflow"]["peerControllers"]
if "baremetalController" in jstr["versanms.sdwan-controller-workflow"] and "wanInterfaces" in jstr["versanms.sdwan-controller-workflow"]["baremetalController"] :
wanlist = jstr["versanms.sdwan-controller-workflow"]["baremetalController"]["wanInterfaces"]
for wan in wanlist:
if "unitInfoList" in wan:
unitinfoList = wan["unitInfoList"]
for unitinfo in unitinfoList:
if "networkName" in unitinfo:
x = find_wan(unitinfo["networkName"])
if x and x == "Internet" and "publicIPAddress" in unitinfo:
mycntlr["inet_public_ip_address"] = unitinfo["publicIPAddress"]
elif x and x == "MPLS" and "ipv4address" in unitinfo:
y = unitinfo["ipv4address"]
mycntlr["mpls_public_ip_address"] = y[0].rsplit("/")[0]
else:
mlog.warn("Getting unknown Ntwk Name = {0}".format(unitinfo["networkName"]))
write_outfile(glbl.vnms,glbl.analy,glbl.cntlr,glbl.cust, glbl.admin)
return
def get_default( _method, _uri,_payload,resp='200', ofile=None):
global vnms, analy, cntlr, cust, mlog
vdict = {}
mlog.info("In function " + get_default.__name__)
vdict = {'body': _payload, 'resp': resp, 'method': _method, 'uri': _uri}
[out, resp_str] = common.call(vdict,content_type='json',ncs_cmd="no",jsonflag=1)
jstr = json_loads(resp_str)
print(jstr)
if ofile is None:
mlog.error("No file provided .. exiting")
sys.exit("No file provided .. exiting")
out = common.create_out_data("POST","200","/vnms/sdwan/workflow/controllers/controller", jstr)
fp=open(ofile,"w+")
out1 = json.dumps(out, indent=4)
fp.write(out1)
fp.close()
return
def create_controller_build( _method, _uri,_payload,resp='202',name="Controller"):
global vnms, analy, cntlr, cust, mlog
vdict = {}
mlog.info("In function " + create_controller_build.__name__)
[status, resp_str] = common.check_controller_status(name=name)
if status == 1 and len(resp_str) > 3:
jstr = json_loads(resp_str)
if "syncStatus" in jstr and jstr["syncStatus"] == "IN_SYNC":
mlog.info("Controller {0} is in sync ".format(name))
vdict = {'body': _payload, 'resp': resp, 'method': _method, 'uri': _uri}
common.call(vdict,content_type='json',ncs_cmd="no")
else:
mlog.error ("Controller {0} is NOT in sync -- Exiting ".format(name))
sys.exit("Controller {0} is NOT in sync -- Exiting ".format(name))
else:
mlog.error ("Did not get correct resp for Controller {0} is NOT in sync -- Exiting ".format(name))
sys.exit("Did not get correct resp for Controller {0} is NOT in sync -- Exiting ".format(name))
return
def deploy_controller( _method, _uri,_payload,resp='202',name="Controller"):
global vnms, analy, cntlr, cust, mlog
vdict = {}
mlog.info("In function " + deploy_controller.__name__)
vdict = {'body': _payload, 'resp': resp, 'method': _method, 'uri': _uri}
[status, resp_str] = common.call(vdict,content_type='json', max_retry_for_task_completion=50, ncs_cmd="no")
if status == 1:
mlog.warn("Deploy of New Controller = {0} sucessful ".format(name))
if len (resp_str) > 3 :
newjstr = json_loads(resp_str)
mlog.info("Return json from Deploy POST on Controller: {0} = {1}".format(name,json.dumps(newjstr,indent=4)))
else:
mlog.error("Deploy of New Controller = {0} NOT sucessful ".format(name))
sys.exit("Deploy of New Controller = {0} NOT sucessful ".format(name))
# Now we need to check the status
mlog.warn("Checking Status of New Controller = {0}. Please be patient".format(name))
found = 0
for i in range(0,5):
time.sleep(5)
[out,resp_str] = common.check_controller_status(name=name)
if out == 1 and len(resp_str) > 3:
jstr = json_loads(resp_str)
if "syncStatus" in jstr and jstr["syncStatus"] == "IN_SYNC":
mlog.warn("New Controller = {0} in Sync. ".format(name))
found = 1
break
if found == 0:
mlog.error("Controller not in proper state for Controller: {0}".format(name))
sys.exit("Controller not in proper state for Controller: {0}".format(name))
return
def get_dir_release_info ( _method, _uri, _payload,resp='200', _name=None,_ofile=None):
global vnms, analy, cntlr, cust, admin, mlog
mlog.info("In function " + get_dir_release_info.__name__)
resp2 = '202'
vdict = {}
vdict = {'body': _payload, 'resp': resp, 'resp2': resp2, 'method': _method, 'uri': _uri}
[out, resp_str] = common.call(vdict,content_type='json',ncs_cmd="no",jsonflag=1)
if len(resp_str) > 3:
jstr = json_loads(resp_str)
mlog.info("Release Info list = {0}".format(json.dumps(jstr,indent=4)))
if "package-info" in jstr:
if ("major-version" in jstr["package-info"][0] and "minor-version" in jstr["package-info"][0]
and "service-version" in jstr["package-info"][0]):
newstr = jstr["package-info"][0]["major-version"] + "." \
+ jstr["package-info"][0]["minor-version"] + "." \
+ jstr["package-info"][0]["service-version"]
if newstr != glbl.vnms.data["rel"]:
mlog.error("Release data does not match")
sys.exit("Release data does not match")
else:
mlog.info("Release data matches")
elif "error" in jstr and jstr["error"]['http_status_code'] == 401 :
mlog.error("This is most likely a password issue. Please change in input json file: vm_phase2.json and re-run")
sys.exit("This is most likely a password issue. Please change in input json file: vm_phase2.json and re-run")
else :
mlog.error("This is most likely a password issue. Please change in input json file: vm_phase2.json and re-run")
sys.exit("This is most likely a password issue. Please change in input json file: vm_phase2.json and re-run")
else:
mlog.error("This is most likely a password issue. Please change in input json file: vm_phase2.json and re-run")
sys.exit("This is most likely a password issue. Please change in input json file: vm_phase2.json and re-run")
return ''
def get_dir_time_zones ( _method, _uri, _payload,resp='200', _name=None,_ofile=None):
global vnms, analy, cntlr, cust, mlog
mlog.info("In function {0} with outfile={1}".format(get_dir_time_zones.__name__,_ofile))
resp2 = '202'
vdict = {}
# first we delete and then create
vdict = {'body': _payload, 'resp': resp, 'resp2': resp2, 'method': "DELETE", 'uri': _uri}
mlog.info("Deleting in {0}".format(get_dir_time_zones.__name__))
[out, resp_str] = common.call(vdict,content_type='json',ncs_cmd="no",jsonflag=1)
if len(resp_str) > 3:
jstr = json_loads(resp_str)
mlog.info("Time Zone = {0}".format(json.dumps(jstr,indent=4)))
vdict = {'body': _payload, 'resp': resp, 'resp2': resp2, 'method': _method, 'uri': _uri}
mlog.info("Creating in {0}".format(get_dir_time_zones.__name__))
[out, resp_str] = common.call(vdict,content_type='json',ncs_cmd="no",jsonflag=1)
if len(resp_str) > 3:
jstr = json_loads(resp_str)
mlog.info("Timezone Info = {0}".format(json.dumps(jstr,indent=4)))
return ''
def get_dir_ntp_server ( _method, _uri, _payload,resp='200', _name=None,_ofile=None):
global vnms, analy, cntlr, cust, mlog
mlog.info("In function {0} with outfile={1}".format(get_dir_ntp_server.__name__,_ofile))
resp2 = '202'
vdict = {}
vdict = {'body': _payload, 'resp': resp, 'resp2': resp2, 'method': "DELETE", 'uri': _uri}
mlog.info("Deleting in {0}".format(get_dir_ntp_server.__name__))
[out, resp_str] = common.call(vdict,content_type='json',ncs_cmd="no",jsonflag=1)
if len(resp_str) > 3:
jstr = json_loads(resp_str)
mlog.info("NTP info = {0}".format(json.dumps(jstr,indent=4)))
vdict = {'body': _payload, 'resp': resp, 'resp2': resp2, 'method': _method, 'uri': _uri}
[out, resp_str] = common.call(vdict,content_type='json',ncs_cmd="no",jsonflag=1)
if len(resp_str) > 3:
jstr = json_loads(resp_str)
mlog.info("NTP Info = {0}".format(json.dumps(jstr,indent=4)))
return ''
def get_dir_dns_server ( _method, _uri, _payload,resp='200', _name=None,_ofile=None):
global vnms, analy, cntlr, cust, mlog
mlog.info("In function {0} with outfile={1}".format(get_dir_dns_server.__name__,_ofile))
resp2 = '202'
vdict = {}
dnspayload = {"dns": {}}
dnspayloadstr =json.dumps(dnspayload)
vdict = {'body': dnspayloadstr , 'resp': resp, 'resp2': resp2, 'method': "PUT", 'uri': _uri}
mlog.info("Deleting in {0}".format(get_dir_dns_server.__name__))
[out, resp_str] = common.call(vdict,content_type='json',ncs_cmd="yes",jsonflag=1)
if len(resp_str) > 3:
jstr = json_loads(resp_str)
mlog.info("DNS Info after PUT = {0}".format(json.dumps(jstr,indent=4)))
vdict = {'body': _payload, 'resp': resp, 'resp2': resp2, 'method': _method, 'uri': _uri}
[out, resp_str] = common.call(vdict,content_type='json',ncs_cmd="yes",jsonflag=1)
if len(resp_str) > 3:
jstr = json_loads(resp_str)
mlog.info("DNS Info = {0}".format(json.dumps(jstr,indent=4)))
return ''
# this is primarily to check that NEW Director is NOT able to connect to OLD Controller
# Do not use this function anywhere
def controller_connect(device):
global vnms, analy, cntlr, cust, admin, auth, debug, mlog
resp = '200'
resp2 = '202'
_uri = "/api/config/devices/device/" + device["name"] + "/_operations/connect"
_payload = {}
vdict = {}
vdict = {'body': _payload, 'resp': resp, 'resp2': resp2, 'method': "POST", 'uri': _uri }
[out, resp_str] = common.call(vdict,content_type='json',ncs_cmd="no",jsonflag=1)
if out == 1 and len(resp_str) > 3:
jstr = json_loads(resp_str)
if "output" in jstr and "result" in jstr["output"] and jstr["output"]["result"] == 0:
mlog.warn("No connection from new Director to contoller={0} -- Good! Any previous errors can be ignored ".format(device["name"]))
return True
else:
return False
else:
mlog.warn("No connection from new Director to contoller={0} -- Good! Any previous errors can be ignored ".format(device["name"]))
return True
def set_nms_provider( _method, _uri, _payload,resp='200', _name=None,_ofile=None):
global vnms, analy, cntlr, cust, mlog
mlog.info("In function {0} with outfile={1}".format(set_nms_provider.__name__,_ofile))
resp2 = '202'
vdict = {}
# before we do this step lets delete the default auth connector
mlog.info("In function {0} Deleting default auth-connector".format(set_nms_provider.__name__))
payload1 = {}
uri1 = '/api/config/nms/provider/default-auth-connector'
vdict = {'body': payload1, 'resp': resp, 'resp2': resp2, 'method': "DELETE", 'uri': uri1}
[out, resp_str] = common.call(vdict,content_type='json',ncs_cmd="yes",jsonflag=1)
if len(resp_str) > 3:
jstr = json_loads(resp_str)
pprint(jstr)
mlog.info("NMS Info after DELETE = {0}".format(json.dumps(jstr,indent=4)))
#return ''
vdict = {'body': _payload, 'resp': resp, 'resp2': resp2, 'method': _method, 'uri': _uri}
[out, resp_str] = common.call(vdict,content_type='json',ncs_cmd="yes",jsonflag=1)
if len(resp_str) > 3:
jstr = json_loads(resp_str)
mlog.info("NMS Info after PUT = {0}".format(json.dumps(jstr,indent=4)))
return ''
def get_dir_analytics_cluster ( _method, _uri, _payload,resp='200', _name=None,_ofile=None):
global vnms, analy, cntlr, cust, mlog
mlog.info("In function {0} with outfile={1}".format(get_dir_analytics_cluster.__name__,_ofile))
resp2 = '202'
vdict = {}
vdict = {'body': _payload, 'resp': resp, 'resp2': resp2, 'method': "DELETE", 'uri': _uri}
mlog.info("Deleting in {0}".format(get_dir_analytics_cluster.__name__))
[out, resp_str] = common.call(vdict,content_type='json',ncs_cmd="no",jsonflag=1)
if len(resp_str) > 3:
jstr = json_loads(resp_str)
mlog.info("Analytic Cluster ater DELETE = {0}".format(json.dumps(jstr,indent=4)))
vdict = {'body': _payload, 'resp': resp, 'resp2': resp2, 'method': _method, 'uri': _uri}
[out, resp_str] = common.call(vdict,content_type='json',ncs_cmd="no",jsonflag=1)
if len(resp_str) > 3:
jstr = json_loads(resp_str)
mlog.info("Analytic Cluster = {0}".format(json.dumps(jstr,indent=4)))
return ''
def get_dir_auth_connector ( _method, _uri, _payload,resp='200', _name=None,_ofile=None):
global vnms, analy, cntlr, cust, mlog
mlog.info("In function {0} with outfile={1}".format(get_dir_auth_connector.__name__,_ofile))
resp2 = '202'
vdict = {}
vdict = {'body': _payload, 'resp': resp, 'resp2': resp2, 'method': "DELETE", 'uri': _uri}
mlog.info("Deleting in {0}".format(get_dir_auth_connector.__name__))
[out, resp_str] = common.call(vdict,content_type='json',ncs_cmd="no",jsonflag=1)
if len(resp_str) > 3:
jstr = json_loads(resp_str)
pprint(jstr)
mlog.info("Create in {0}".format(get_dir_auth_connector.__name__))
vdict = {'body': _payload, 'resp': resp, 'resp2': resp2, 'method': _method, 'uri': _uri}
[out, resp_str] = common.call(vdict,content_type='json',ncs_cmd="no",jsonflag=1)
if len(resp_str) > 3:
jstr = json_loads(resp_str)
pprint(jstr)
return ''
def get_dir_default_auth_connector ( _method, _uri, _payload,resp='200', _name=None,_ofile=None):
global vnms, analy, cntlr, cust, mlog
mlog.info("In function {0} with outfile={1}".format(get_dir_default_auth_connector.__name__,_ofile))
resp2 = '202'
vdict = {}
vdict = {'body': _payload, 'resp': resp, 'resp2': resp2, 'method': _method, 'uri': _uri}
[out, resp_str] = common.call(vdict,content_type='json',ncs_cmd="no",jsonflag=1)
if len(resp_str) > 3:
jstr = json_loads(resp_str)
pprint(jstr)
return ''
def get_dir_auth_connector_config ( _method, _uri, _payload,resp='200', _name=None,_ofile=None):
global vnms, analy, cntlr, cust, mlog
mlog.info("In function {0} with outfile={1}".format(get_dir_auth_connector_config.__name__,_ofile))
resp2 = '202'
vdict = {}
vdict = {'body': _payload, 'resp': resp, 'resp2': resp2, 'method': _method, 'uri': _uri}
[out, resp_str] = common.call(vdict,content_type='json',ncs_cmd="no",jsonflag=1)
if len(resp_str) > 3:
jstr = json_loads(resp_str)
pprint(jstr)
return ''
def deploy_org_workflow( _method, _uri, _payload,resp='200', name=None,_ofile=None):
global vnms, analy, cntlr, cust, mlog
mlog.info("In function {0} with outfile={1}".format(deploy_org_workflow.__name__,_ofile))
resp2 = '202'
if name is None:
mlog.error("Can not continue without Customer Name ")
sys.exit("Can not continue without Customer Name ")
vdict = {}
payload1 = {}
mlog.info("In function {0} calling Get ".format(deploy_org_workflow.__name__))
vdict = {'body': payload1, 'resp': resp, 'resp2': resp2, 'method': "GET", 'uri': _uri}
[out, resp_str] = common.call(vdict,content_type='json',ncs_cmd="no",max_retry_for_task_completion=50,jsonflag=1)
if out == 1:
mlog.warn("Deploy of org workflow successful")
if len(resp_str) > 3:
jstr = json_loads(resp_str)
orig_jstr = json_loads(_payload)
if "versanms.sdwan-org-workflow" in jstr:
# we need to copy the controller and the Analytics data from newdir to olddir
# and not the reverse way
if "controllers" in orig_jstr["versanms.sdwan-org-workflow"]:
jstr["versanms.sdwan-org-workflow"]["controllers"] = []
jstr["versanms.sdwan-org-workflow"]["controllers"] = orig_jstr["versanms.sdwan-org-workflow"]["controllers"]
else:
mlog.error("No controller information -- can not continue. In function {0}".format(deploy_org_workflow.__name__))
sys.exit("No controller information -- can not continue. In function {0}".format(deploy_org_workflow.__name__))
if "analyticsClusters" in orig_jstr["versanms.sdwan-org-workflow"]:
jstr["versanms.sdwan-org-workflow"]["analyticsClusters"] = []
jstr["versanms.sdwan-org-workflow"]["analyticsClusters"] = orig_jstr["versanms.sdwan-org-workflow"]["analyticsClusters"]
else:
mlog.error("No Analytics information -- can not continue. In function {0}".format(deploy_org_workflow.__name__))
sys.exit("No Analytics information -- can not continue. In function {0}".format(deploy_org_workflow.__name__))
_newpayload = json.dumps(jstr)
vdict = {'body': _newpayload, 'resp': resp, 'resp2': resp2, 'method': _method, 'uri': _uri}
mlog.info("In function {0} calling PUT ".format(deploy_org_workflow.__name__))
[out, resp_str] = common.call(vdict,content_type='json',ncs_cmd="no",jsonflag=1)
if len(resp_str) > 3:
jstr = json_loads(resp_str)
pprint(jstr)
_newuri = '/vnms/sdwan/workflow/orgs/org/deploy/' + name
vdict = {'body': _newpayload, 'resp': resp, 'resp2': resp2, 'method': "POST" , 'uri': _newuri}
mlog.info("In function {0} calling POST ".format(deploy_org_workflow.__name__))
[out, resp_str] = common.call(vdict,content_type='json',ncs_cmd="no",jsonflag=1)
if len(resp_str) > 3:
jstr = json_loads(resp_str)
pprint(jstr)
return ''
def get_sdwan_workflow_list( _method, _uri, _payload,resp='200', _ofile=None):
global vnms, analy, cntlr, cust, mlog
resp2 = '202'
vdict = {}
mlog.info("In function {0} ".format(get_sdwan_workflow_list.__name__))
vdict = {'body': _payload, 'resp': resp, 'resp2': resp2, 'method': _method, 'uri': _uri}
[out, resp_str] = common.call(vdict,content_type='json',ncs_cmd="no",jsonflag=1)
if out == 1 and len(resp_str) > 3:
jstr = json_loads(resp_str)
if "versanms.sdwan-controller-list" in jstr:
if len(jstr["versanms.sdwan-controller-list"]) == 0 or len(jstr["versanms.sdwan-controller-list"]) > 2 :
print("Something wrong")
mlog.error("Number of Controllers bad = {0} in return .. exiting".format(len(jstr["versanms.sdwan-controller-list"])))
sys.exit("Number of Controllers bad = {0} in return .. exiting".format(len(jstr["versanms.sdwan-controller-list"])))
else:
glbl.cntlr.data['new_cntlr'].append( jstr["versanms.sdwan-controller-list"][0])
glbl.cntlr.data['new_cntlr'].append( jstr["versanms.sdwan-controller-list"][1])
#if debug : pprint(glbl.cntlr.data)
write_outfile(glbl.vnms,glbl.analy,glbl.cntlr,glbl.cust, glbl.admin)
return ''
def deploy_template_workflow(dev):
global vnms, analy, cntlr, cust, mlog
mlog.warn("Deploying Template Workflow: {0}".format(dev["poststaging-template"]))
resp = '200'
resp2 = '202'
vdict = {}
_payload = {}
_uri = "/vnms/sdwan/workflow/templates/template/" + dev["poststaging-template"]
vdict = {'body': _payload, 'resp': resp, 'resp2': resp2, 'method': "GET", 'uri': _uri}
[out, resp_str] = common.call(vdict,content_type='json',ncs_cmd="no",jsonflag=1)
if out == 1 and len(resp_str) > 3:
jstr = json_loads(resp_str)
if "versanms.sdwan-template-workflow" in jstr and "templateName" in jstr["versanms.sdwan-template-workflow"]:
if dev["poststaging-template"] not in jstr["versanms.sdwan-template-workflow"]["templateName"]:
mlog.warn("This is a HA template with template={0} with template={1}".format(dev["poststaging-template"],jstr["versanms.sdwan-template-workflow"]["templateName"]))
dev["deployed"] = ""
dev["deployed"] = "1"
return True
if "versanms.sdwan-template-workflow" in jstr and "controllers" in jstr["versanms.sdwan-template-workflow"]:
cntlr_list = [glbl.cntlr.data['new_cntlr'][0]["controllerName"], glbl.cntlr.data['new_cntlr'][1]["controllerName"]]
jstr["versanms.sdwan-template-workflow"]["controllers"]= cntlr_list
vdict = {}
_payload = json.dumps(jstr)
_uri = "/vnms/sdwan/workflow/templates/template/" + dev["poststaging-template"]
vdict = {'body': _payload, 'resp': resp, 'resp2': resp2, 'method': "PUT", 'uri': _uri}
[out, resp_str] = common.call(vdict,content_type='json',ncs_cmd="no",jsonflag=1)
if out == 0:
mlog.error("Workflow template PUT deploy = {0} was NOT successful".format(dev["poststaging-template"]))
dev["deployed"] = ""
dev["deployed"] = "0"
if "redundantPair" in jstr["versanms.sdwan-template-workflow"]:
dev["redundantPair_templ"] = ""
dev["redundantPair_templ"] = jstr["versanms.sdwan-template-workflow"]["redundantPair"]["templateName"]
return False
#GET_OLD
#GET_NEW
#MODIFY
#PATCH
# Now the deploy
newpayload = {}
_uri = "/vnms/sdwan/workflow/templates/template/deploy/" + dev["poststaging-template"]
vdict = {'body': newpayload, 'resp': resp, 'resp2': resp2, 'method': "POST", 'uri': _uri}
[out, resp_str] = common.call(vdict,content_type='json',ncs_cmd="no",jsonflag=1)
if out == 0:
mlog.error("Deploy Template Workflow POST = {0} was NOT successful".format(dev["poststaging-template"]))
dev["deployed"] = ""
dev["deployed"] = "0"
if "redundantPair" in jstr["versanms.sdwan-template-workflow"]:
dev["redundantPair_templ"] = ""
dev["redundantPair_templ"] = jstr["versanms.sdwan-template-workflow"]["redundantPair"]["templateName"]
return False
else:
mlog.info("Deploy Template Workflow = {0} was successful".format(dev["poststaging-template"]))
dev["deployed"] = ""
dev["deployed"] = "1"
return True
else:
# the template for a paired device
# will need to figure out how to deal with these
mlog.info("Deploy Template Workflow = {0} was not successful".format(dev["poststaging-template"]))
dev["deployed"] = ""
dev["deployed"] = "0"
return False
return True
def get_vnf_manager(device):
global vnms, analy, cntlr, cust, mlog
if device is None:
mlog.error("Bad inputs in function {0}. Input File is None ".format(get_vnf_manager.__name__))
return
mlog.info("In function {0} with device = {1} ".format(get_vnf_manager.__name__, device["name"]))
resp = '200'
resp2 = '202'
_uri = "/api/config/devices/template/" + device["poststaging-template"] + "/config/system/vnf-manager"
_payload = {}
vdict = {}
vdict = {'body': _payload , 'resp': resp, 'resp2': resp2, 'method': "GET", 'uri': _uri}
[out, resp_str] = common.call(vdict,content_type='json',ncs_cmd="no",jsonflag=1)
if out == 1 and len(resp_str) > 3:
south_ip = [i['director_southIP'] + "/32" for i in glbl.vnms.data['director']]
domain_ips = glbl.vnms.data['domain_ips']
#print("S IP = " + " ".join(south_ip))
jstr = json_loads(resp_str)
#mlog.info("VNF Manager Info = {0}".format(json.dumps(jstr,indent=4)))
if "vnf-manager" in jstr and "ip-addresses" in jstr["vnf-manager"]:
#x= jstr["vnf-manager"]["ip-addresses"] + south_ip
jstr["vnf-manager"]["ip-addresses"] = []
jstr["vnf-manager"]["ip-addresses"] = south_ip + domain_ips
del jstr["vnf-manager"]["vnf-manager"]
payload = json.dumps(jstr)
vdict = {'body': payload , 'resp': resp, 'resp2': resp2, 'method': "PUT", 'uri': _uri}
[out, resp_str] = common.call(vdict,content_type='json',ncs_cmd="no",jsonflag=1)
if len(resp_str) > 3:
jstr = json_loads(resp_str)
mlog.info("VNF Manager after PUT = {0}".format(json.dumps(jstr,indent=4)))
return ''
def create_dns_config( _method, _uri,_payload,resp='200'):
vdict = {}
vdict = {'body': _payload, 'resp': resp, 'method': _method, 'uri': _uri}
[out, resp_str] = common.call(vdict,content_type='json',ncs_cmd="no")
if len(resp_str) > 3:
jstr = json_loads(resp_str)
mlog.info("Response = {0}".format(json.dumps(jstr,indent=4)))
return ''
def deploy_device_workflow(dev):
global vnms, analy, cntlr, cust, mlog
mlog.warn("Deploy Device Workflow for device: {0}".format(dev["name"]))
resp = '200'
resp2 = '202'
vdict = {}
_uri = "/vnms/sdwan/workflow/devices/device/" + dev["name"]
_payload = {}
vdict = {'body': _payload , 'resp': resp, 'resp2': resp2, 'method': "GET", 'uri': _uri}
[out, resp_str] = common.call(vdict,content_type='json',ncs_cmd="no",jsonflag=1)
if out == 1 and len(resp_str) > 3:
jstr = json_loads(resp_str)
for mcntlr in glbl.cntlr.data['new_cntlr']:
a = jstr['versanms.sdwan-device-workflow']['postStagingTemplateInfo']['templateData']['device-template-variable']['variable-binding']['attrs']
local_auth_key = '{$v_'+ glbl.cust.data["custName"] + '_' + mcntlr["controllerName"] + '_Local_auth_email_key__IKELKey}'
local_auth_identity = '{$v_'+ glbl.cust.data["custName"] + '_' + mcntlr["controllerName"] + '_Local_auth_email_identifier__IKELIdentifier}'
for val in a:
#print val
if "name" in val and val["name"] == local_auth_key :
if "peerControllers" in mcntlr:
dev["local_auth_key"] = ""
dev["local_auth_key"] = val["value"]
else:
dev["local1_auth_key"] = ""
dev["local1_auth_key"] = val["value"]
elif "name" in val and val["name"] == local_auth_identity :
if "peerControllers" in mcntlr:
dev["local_auth_identity"] = ""
dev["local_auth_identity"] = val["value"]
else:
dev["local1_auth_identity"] = ""
dev["local1_auth_identity"] = val["value"]
if "peerControllers" in mcntlr:
dev["remote_auth_identity"] = ""
dev["remote_auth_identity"] = mcntlr["controllerName"] + '@' + glbl.cust.data["custName"] + '.com'
else:
dev["remote1_auth_identity"] = ""
dev["remote1_auth_identity"] = mcntlr["controllerName"] + '@' + glbl.cust.data["custName"] + '.com'
# the above for the cntlr loop has ended here
vdict = {'body': resp_str , 'resp': resp, 'resp2': resp2, 'method': "PUT", 'uri': _uri}
[out, resp_str] = common.call(vdict,content_type='json',ncs_cmd="no",jsonflag=1)
if out == 1:
mlog.info("Deploy Device Workflow PUT was successful for device: {0}".format(dev["name"]))
time.sleep(10)
_uri = "/vnms/sdwan/workflow/devices/device/deploy/" + dev["name"]
_payload = {}
vdict = {'body': _payload , 'resp': resp, 'resp2': resp2, 'method': "POST", 'uri': _uri}
[ret, resp_str] = common.call(vdict,content_type='json',ncs_cmd="no",jsonflag=1)
if ret == 1:
mlog.info("Deploy Device Workflow POST was successful for device: {0}".format(dev["name"]))
# before we return back we need to change the VNF Manager in the device template on the new director
dev["deployed"] = ""
dev["deployed"] = "1"
get_vnf_manager(dev)
return True
else:
mlog.error("Deploy Device Workflow POST was NOT successful for device: {0}".format(dev["name"]))
dev["deployed"] = ""
dev["deployed"] = "0"
return False
else:
mlog.error("Deploy Device Workflow PUT was NOT successful for device: {0}".format(dev["name"]))
dev["deployed"] = ""
dev["deployed"] = "0"
return False
else:
mlog.error("Deploy Device Workflow GET was NOT successful for device: {0}".format(dev["name"]))
dev["deployed"] = ""
dev["deployed"] = "0"
return False
def get_device_group_new( _method, _uri, _payload,resp='200', _name=None,_ofile=None):
global vnms, analy, cntlr, cust, mlog
resp2 = '202'
vdict = {}
mlog.info("In function {0} ".format(get_device_group_new.__name__))
count = 0
totalcnt = -1
errorlist = list(filter(lambda x: "deployed" in x and (x['deployed'] != "1"), glbl.vnms.data["devices"]))
found = 0
while 1:
newuri = _uri + "&offset={0}&limit=25".format(count)
vdict = {'body': _payload, 'resp': resp, 'resp2': resp2, 'method': _method, 'uri': newuri}
[out, resp_str] = common.call(vdict,content_type='json',ncs_cmd="no",jsonflag=1)
if out == 1 and len(resp_str) > 3:
jstr = json_loads(resp_str)
if count == 0 and totalcnt == -1:
if "totalCount" in jstr: totalcnt = int(jstr["totalCount"])
else: sys.exit("did not get totalCount")
if "device-group" in jstr:
for i in range(len(jstr["device-group"])):
if ("inventory-name" in jstr["device-group"][i] and "poststaging-template" in jstr["device-group"][i] and
len(jstr["device-group"][i]["inventory-name"]) > 0 ) :
for devname in jstr["device-group"][i]["inventory-name"]:
for j in range( len( errorlist)):
if devname == errorlist[j]["name"]:
mlog.info("Found device in list {0} ".format(devname))
found = found + 1
if totalcnt <= (count + 25): break
else: count = count + 25
# Now we need to a) delete the devices that do not have a post-staging template
# b) deploy the device templates with the Controller info
#if found != len(errorlist):
# mlog.warn("Found {0:d} devices while we should have {1:d} in such a state".format(found,len(errorlist)))
# sys.exit("Found {0:d} devices while we should have {1:d} in such a state".format(found,len(errorlist)))
# for each of the devices call the template workflow and the device workflow
workflow_template_list = []
for dev in errorlist:
if dev["poststaging-template"] not in workflow_template_list:
if deploy_template_workflow(dev) :
workflow_template_list.append(dev["poststaging-template"])
#deploy_device_workflow(dev)
else:
# the workflow template has already been deployed sucessfully
deploy_device_workflow(dev)
write_outfile(glbl.vnms,glbl.analy,glbl.cntlr,glbl.cust, glbl.admin)
return ''
def get_device_group( _method, _uri, _payload,resp='200', _name=None,_ofile=None):
global vnms, analy, cntlr, cust, mlog
resp2 = '202'
vdict = {}
mlog.info("In function {0} ".format(get_device_group.__name__))
count = 0
totalcnt = -1
while 1:
newuri = _uri + "&offset={0}&limit=25".format(count)
vdict = {'body': _payload, 'resp': resp, 'resp2': resp2, 'method': _method, 'uri': newuri}
[out, resp_str] = common.call(vdict,content_type='json',ncs_cmd="no",jsonflag=1)
if out == 1 and len(resp_str) > 3:
jstr = json_loads(resp_str)
if count == 0 and totalcnt == -1:
if "totalCount" in jstr: totalcnt = int(jstr["totalCount"])
else: sys.exit("did not get totalCount")
if "device-group" in jstr:
if len(jstr["device-group"]) <= 0 :
sys.exit("Got 0 devices in device group")
for i in range(len(jstr["device-group"])):
if ("inventory-name" in jstr["device-group"][i] and "poststaging-template" in jstr["device-group"][i] and
len(jstr["device-group"][i]["inventory-name"]) > 0 ) :
for devname in jstr["device-group"][i]["inventory-name"]:
for j in range( len(glbl.vnms.data['devices'])):
if devname == glbl.vnms.data['devices'][j]["name"]:
mlog.info("Found device in list {0} ".format(devname))
glbl.vnms.data['devices'][j]["poststaging-template"] = ""
glbl.vnms.data['devices'][j]["dg-group"] = ""
glbl.vnms.data['devices'][j]["poststaging-template"] = jstr["device-group"][i]["poststaging-template"]
glbl.vnms.data['devices'][j]["dg-group"] = jstr["device-group"][i]["name"]
glbl.vnms.data['devices'][j]["deployed"] = ""
glbl.vnms.data['devices'][j]["deployed"] = "0"
if totalcnt <= (count + 25): break
else: count = count + 25
# Now we need to a) delete the devices that do not have a post-staging template
# b) deploy the device templates with the Controller info
newdevice = []
for dev in glbl.vnms.data['devices']:
if "poststaging-template" not in dev or "deployed" not in dev:
mlog.warn("Skipping Device={0} from the list because post-staging template was not found or device not in proper state".format(dev["name"]))
glbl.vnms.data['devices'].remove(dev)
# for each of the devices call the template workflow and the device workflow
workflow_template_list = []
for dev in glbl.vnms.data['devices']:
if dev["poststaging-template"] not in workflow_template_list:
if deploy_template_workflow(dev) :
workflow_template_list.append(dev["poststaging-template"])
deploy_device_workflow(dev)
else:
# the workflow template has already been deployed sucessfully
deploy_device_workflow(dev)
write_outfile(glbl.vnms,glbl.analy,glbl.cntlr,glbl.cust, glbl.admin)
return ''
def get_wan_ntwk( _method, _uri, _payload,resp='200', _name=None,_ofile=None):
global vnms, analy, cntlr, cust, mlog
resp2 = '202'
vdict = {}
vdict = {'body': _payload, 'resp': resp, 'resp2': resp2, 'method': _method, 'uri': _uri}
mlog.info("In function {0} with outfile={1}".format(get_wan_ntwk.__name__,_ofile))
[out, resp_str] = common.call(vdict,content_type='json',ncs_cmd="no",jsonflag=1)
if out == 1 and len(resp_str) > 3:
jstr = json_loads(resp_str)
mywan = {}
for i in jstr:
if 'name' in i:
mywan[i["name"]] = i["transport-domains"][0]
else:
mlog.error("Could not get Wan Ntwk")
glbl.vnms.data['wanNtwk'] = {}
glbl.vnms.data['wanNtwk'] = mywan
write_outfile(glbl.vnms,glbl.analy,glbl.cntlr,glbl.cust, glbl.admin)
else:
mlog.error("Could not get Wan Ntwk")
return ''
def get_parent_orgid( _method, _uri, _payload,resp='200',_name=None, _ofile=None):
global vnms, analy, cntlr, cust, mlog
resp2 = '202'
vdict = {}
vdict = {'body': _payload, 'resp': resp, 'resp2': resp2, 'method': _method, 'uri': _uri}
mlog.info("In function {0} with outfile={1}".format(get_parent_orgid.__name__,_ofile))
[out, resp_str] = common.call(vdict,content_type='json',ncs_cmd="no",jsonflag=1)
if out == 1 and len(resp_str) > 3:
jstr = json_loads(resp_str)
if 'uuid' in jstr:
glbl.vnms.data['parentOrgId'] = ""
glbl.vnms.data['parentOrgId'] = jstr['uuid']
write_outfile(glbl.vnms,glbl.analy,glbl.cntlr,glbl.cust, glbl.admin)
else:
mlog.error("Could not get parent Org UUID")
else:
mlog.error("Could not get parent Org UUID")
return ''
def get_controller_workflow( _method, _uri, _payload,resp='200', _ofile=None):
global vnms, analy, cntlr, cust, mlog
resp2 = '202'
vdict = {}
vdict = {'body': _payload, 'resp': resp, 'resp2': resp2, 'method': _method, 'uri': _uri}
mlog.info("In function {0} with outfile={1}".format(get_controller_workflow.__name__,_ofile))
if _ofile is None:
mlog.error("No file provided .. exiting")
sys.exit("No file provided .. exiting")
[out, resp_str] = common.call(vdict,content_type='json',ncs_cmd="no")
if out == 1 and len(resp_str) > 3:
jstr = json_loads(resp_str)
out = common.create_out_data("POST","200","/vnms/sdwan/workflow/controllers/controller", jstr)
fp=open(_ofile,"w+")
out1 = json.dumps(out, indent=4)
fp.write(out1)
fp.close()
return ''
def delete_controller_workflow(_name ):
resp2 = '202'
vdict = {}
_uri = '/vnms/sdwan/workflow/controllers/controller/' + _name
_payload = {}
#np = json_loads(a)
mlog.info("In function {0} ".format(delete_controller_workflow.__name__))
vdict = {'body': _payload , 'resp': '200', 'resp2': resp2, 'method': "DELETE", 'uri': _uri}
[out, resp_str] = common.call(vdict,content_type='json',ncs_cmd="yes")
if out == 1 and len(resp_str) > 3:
jstr = json_loads(resp_str)
pprint(jstr)
return
def delete_controller_by_uuid( _uuid ):
resp2 = '202'
vdict = {}
_uri = '/api/config/nms/actions/_operations/delete-appliance'
_payload = {}
_payload = { "delete-appliance": {"applianceuuid": "%s" % str(_uuid),
"clean-config": "false",
"reset-config": "false",
"load-defaults": "false" }}
a=json.dumps(_payload)
#np = json_loads(a)
mlog.info("In function {0} ".format(delete_controller_by_uuid.__name__))
vdict = {'body': a, 'resp': '200', 'resp2': resp2, 'method': "POST", 'uri': _uri}
[out, resp_str] = common.call(vdict,content_type='json',ncs_cmd="yes")
if out == 1 and len(resp_str) > 3:
jstr = json_loads(resp_str)
#pprint(jstr)
return
def post_script():
# we need to write a few files so it is easier on the next phase
global vnms, analy, cntlr, cust, admin, mlog
mlog.info("In function {0} ".format(post_script.__name__))
'''
i = 0
for dev in glbl.cntlr.data['old_cntlr']:
jstr = {}
_uri = '/vnms/sdwan/workflow/controllers/controller/' + dev['name']
out = common.create_out_data("GET","200",_uri, jstr)
if i == 0:
fname = "in_phase3/" + "{:03d}_GET_CONTROLLER_WORKFLOW.json".format(i)
else:
fname = "in_phase3/" + "{:03d}_GET_PEER_CONTROLLER_WORKFLOW.json".format(i)
fp=open(fname,"w+")
out1 = json.dumps(out, indent=4)
fp.write(out1)
fp.close()
i = i + 1
'''
_str = '/api/config/devices/template/'+ '{{templateName}}'
for i in range(13,17):
jstr = {}
if i == 13:
_uri= _str + '/config/orgs/org/alpha/sd-wan/controllers/controller'
fname = "in_phase3/" + "{:03d}_GET_SDWAN_CONTROLLER.json".format(i)
elif i == 14:
_uri= _str + '/config/system/sd-wan/controllers/controller?deep=true&offset=0&limit=25'
fname = "in_phase3/" + "{:03d}_GET_SYSTEM_CONTROLLER.json".format(i)
elif i == 15:
_uri= _str + '/config/orgs/org-services/alpha/ipsec/vpn-profile?deep=true&offset=0&limit=25'
fname = "in_phase3/" + "{:03d}_GET_IPSEC_VPN_PROFILE.json".format(i)
elif i == 16:
_uri= _str + '/config/system/vnf-manager'
fname = "in_phase3/" + "{:03d}_GET_VNF_MANAGER.json".format(i)
out = common.create_out_data("GET","200",_uri, jstr)
fp=open(fname,"w+")
out1 = json.dumps(out, indent=4)
fp.write(out1)
fp.close()
_str = '/api/config/devices/device/'+ '{{deviceName}}'
for i in range(23,27):
jstr = {}
if i == 23:
_uri= _str + '/config/orgs/org/alpha/sd-wan/controllers/controller?deep=true&offset=0&limit=25'
fname = "in_phase3/" + "{:03d}_GET_DEVICE_SDWAN_CONTROLLER.json".format(i)
elif i == 24:
_uri= _str + '/config/system/sd-wan/controllers/controller?deep=true&offset=0&limit=25'
fname = "in_phase3/" + "{:03d}_GET_DEVICE_SYSTEM_CONTROLLER.json".format(i)
elif i == 25:
_uri= _str + '/config/orgs/org-services/alpha/ipsec/vpn-profile?deep=true&offset=0&limit=25'
fname = "in_phase3/" + "{:03d}_GET_DEVICE_IPSEC_VPN_PROFILE.json".format(i)
elif i == 26:
_uri= _str + '/config/system/vnf-manager'
fname = "in_phase3/" + "{:03d}_GET_DEVICE_VNF_MANAGER.json".format(i)
out = common.create_out_data("GET","200",_uri, jstr)
fp=open(fname,"w+")
out1 = json.dumps(out, indent=4)
fp.write(out1)
fp.close()
return
def get_existing_controller():
global vnms, analy, cntlr, cust, admin, mlog
resp2 = '202'
vdict = {}
_payload = {}
mlog.info("In function {0} ".format(get_existing_controller.__name__))
vdict = {'body': _payload, 'resp': '200', 'resp2': resp2, 'method': "GET", 'uri':"/vnms/appliance/summary"}
[out, resp_str] = common.call(vdict,content_type='json',ncs_cmd="no",jsonflag=1)
old_cntlr_list = []
if out == 1 and len(resp_str) > 3:
jstr = json_loads(resp_str)
mlog.info("Device list = {0}".format(json.dumps(jstr,indent=4)))
old_cntlr_list = list(filter(lambda x: x['type'] == 'controller', jstr))
dev_list = list(filter(lambda x: x['type'] != 'controller', jstr))
hub_cntlr_list = list(filter(lambda x: x['type'] == 'hub-controller', jstr))
if len(hub_cntlr_list) > 0:
new_hub_cntlr_list = copy.deepcopy( hub_cntlr_list )
glbl.vnms.data['hub_cntlr_present'] = 1
glbl.vnms.data['hub_cntlr_devices'] = []
glbl.vnms.data['hub_cntlr_devices'] = new_hub_cntlr_list
mlog.warn("Hub Controller Num = {0} present".format(len(new_hub_cntlr_list)))
else:
glbl.vnms.data['hub_cntlr_present'] = 0
glbl.vnms.data['devices'] = []
glbl.vnms.data['devices'] = dev_list
write_outfile(glbl.vnms,glbl.analy,glbl.cntlr,glbl.cust, glbl.admin)
#pprint(jstr)
if len(old_cntlr_list) != 2:
mlog.error("Number of Existing Controllers not 2.It is = {0}".format(len(old_cntlr_list)))
# Make sure the cntrollers are NOT the New controllers
# this can happen if this file is executed more than once
found = 0
for dev in old_cntlr_list:
if ((dev["name"] == glbl.cntlr.data["new_cntlr"][0]["controllerName"]) or
(dev["name"] == glbl.cntlr.data["new_cntlr"][1]["controllerName"])):
found = found + 1
if found > 1 :
# We have already made the necessary check in the main function
# it must be in glbl.vnms.data["old_cntlr"] or we fail
if len(glbl.cntlr.data["old_cntlr"]) > 0:
old_cntlr_list = glbl.cntlr.data["old_cntlr"]
else:
mlog.error("Can not find old controller data -- exiting")
sys.exit("Can not find old controller data")
else:
pass