-
Notifications
You must be signed in to change notification settings - Fork 0
/
yang_cli.py
547 lines (516 loc) · 21.7 KB
/
yang_cli.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
#!/usr/bin/env python
from __future__ import unicode_literals
from prompt_toolkit import PromptSession
from prompt_toolkit.history import FileHistory
from prompt_toolkit.completion import WordCompleter
from prompt_toolkit import prompt
import json, sys, os
def fill_config(path, config, type):
steps=path.split("/")[:-1]
final=path.split("/")[-1]
aux=config
for s in steps:
if s in aux.keys(): aux=aux[s]
else:
aux[s]={}
aux=aux[s]
if final not in aux.keys():
if type == "leaf-list":
aux[final]=[]
if type == "leaf":
aux[final]=None
else:
aux[final]={}
def fill_data(path, config, content):
steps=path.split("/")[:-1]
final=path.split("/")[-1]
aux=config
for s in steps:
if s in aux.keys(): aux=aux[s]
else:
aux[s]={}
aux=aux[s]
aux[final]=content
def fill_value(path, config, type, value):
steps=path.split("/")[:-1]
final=path.split("/")[-1]
aux=config
for s in steps:
aux=aux[s]
if type=="leaf": aux[final]=value
elif type=="leaf-list":
if len(aux[final])==0:
aux[final] = list()
aux[final].append(value)
else: aux[final].append(value)
def delete_info(cp,config):
steps=cp.split("/")[:-1]
final=cp.split("/")[-1]
aux=config
for s in steps:
if s in aux.keys(): aux=aux[s]
else:
aux[s]={}
aux=aux[s]
del aux[final]
def find_json(path,config):
steps=path.split("/")
aux=config
for s in steps:
aux=aux[s]
return aux
def load_choices(path,data,el,build):
ret=""
for c in data[path]["children"]:
if data[path+"/"+c]["type"] in ["case","choice"]:
if path+"/"+c+"/"+el in data.keys() and data[path+"/"+c+"/"+el]["type"] not in ["case","choice"]: return build+"/"+c
else: ret+=load_choices(path+"/"+c,data,el,build+"/"+c)
return ret
def load_rc(cp,mp,js,data,isfirst):
ret={}
err=False
choices=""
if mp not in data.keys():
parent='/'.join(mp.split("/")[:-1])
el=mp.split("/")[-1]
choices=load_choices(parent,data,el,"")[1:]
if len(choices)!=0:
steps=choices.split("/")
mp=parent+"/"+choices+"/"+el
cp="/".join(cp.split("/")[:-1])+"/"+choices+"/"+el
if err:
print "Error loading: "+mp
else:
ctype=data[mp]["type"]
if ctype=="leaf":
return choices,js
elif ctype=="leaf-list":
return choices,js
elif isinstance(js,dict):
for k in js.keys():
kaux=k.split(":")[-1]
if isfirst:
ext,info=load_rc(kaux,kaux,js[k],data,False)
if len(ext)!=0: kaux=ext+"/"+kaux
fill_data(kaux,ret,info)
else:
ext,info=load_rc(cp+"/"+kaux,mp+"/"+kaux,js[k],data,False)
if len(ext)!=0: kaux=ext+"/"+kaux
fill_data(kaux,ret,info)
else:
if isfirst: print "ERROR"
for k in js:
keyname=data[mp]["key"]
mkaux=keyname.split(" ")
key=""
for w in mkaux:
key+=k[w]+","
ret[key[:-1]]=load_rc(cp+"/"+key,mp,k,data,False)[1]
return choices, ret
def ret_nochoice_keys(path,js,data,skip):
ret=[]
st=skip
for k in js.keys():
if data[path+"/"+k]["type"] in ("choice","case"):
auxret=ret_nochoice_keys(path+"/"+k,js[k],data,skip+"/"+k)
ret+=auxret
else: ret.append(skip+"/"+k)
return ret
def from_dict_to_rc(path,cnt,js,pprefix,isfirst):
ret=None
ctype=cnt[path]["type"]
if ctype=="list":
ret=[]
if isfirst:
auxret={}
for k in js.keys():
nwpath=path+"/"+k
if cnt[nwpath]["type"] in ("choice","case"):
elems = ret_nochoice_keys(nwpath,js[k],cnt,k)
for el in elems:
e=el.split("/")[-1]
nwpath=path+"/"+el
pfx=cnt[nwpath]["orig"]
add=""
spfx=pprefix
if pprefix!=pfx:
add=pfx+":"
spfx=pfx
next=js
for l in el.split("/"):
next=next[l]
auxret[add+e]=from_dict_to_rc(nwpath,cnt,next,spfx,False)
else:
pfx=cnt[nwpath]["orig"]
add=""
spfx=pprefix
if pprefix!=pfx:
add=pfx+":"
spfx=pfx
auxret[add+k]=from_dict_to_rc(nwpath,cnt,js[k],spfx,False)
ret.append(auxret)
else:
for j in js.keys():
auxret={}
for k in js[j].keys():
nwpath=path+"/"+k
if cnt[nwpath]["type"] in ("choice","case"):
elems = ret_nochoice_keys(nwpath,js[j][k],cnt,k)
for el in elems:
e=el.split("/")[-1]
nwpath=path+"/"+el
pfx=cnt[nwpath]["orig"]
add=""
spfx=pprefix
if pprefix!=pfx:
add=pfx+":"
spfx=pfx
next=js[j]
for l in el.split("/"):
next=next[l]
auxret[add+e]=from_dict_to_rc(nwpath,cnt,next,spfx,False)
else:
pfx=cnt[nwpath]["orig"]
add=""
spfx=pprefix
if pprefix!=pfx:
add=pfx+":"
spfx=pfx
auxret[add+k]=from_dict_to_rc(nwpath,cnt,js[j][k],spfx,False)
ret.append(auxret)
elif ctype in ("leaf","leaf-list"): ret=js
else:
ret={}
for k in js.keys():
nwpath=path+"/"+k
if cnt[nwpath]["type"] in ("choice","case"):
elems = ret_nochoice_keys(nwpath,js[k],cnt,k)
for el in elems:
e=el.split("/")[-1]
nwpath=path+"/"+el
pfx=cnt[nwpath]["orig"]
add=""
spfx=pprefix
if pprefix!=pfx:
add=pfx+":"
spfx=pfx
next=js
for l in el.split("/"):
next=next[l]
ret[add+e]=from_dict_to_rc(nwpath,cnt,next,spfx,False)
else:
pfx=cnt[nwpath]["orig"]
add=""
spfx=pprefix
if pprefix!=pfx:
add=pfx+":"
spfx=pfx
ret[add+k]=from_dict_to_rc(nwpath,cnt,js[k],spfx,False)
return ret
def build_url(cfgp, curp, cnt, cfg):
elms=cfgp.split("/")
url="/restconf/data"
pprefix=""
auxpth=""
for el in elms:
if el in curp:
auxpth+=el+"/"
preamble=""
pfx=cnt[auxpth[:-1]]['orig']
if pfx!=pprefix:
preamble=pfx+":"
pprefix=pfx
url+="/"+preamble+el
else: url+="="+el
return url
def restconf(cfgp, curp, cnt, cfg, session):
if cnt[curp]["type"]=="list" and cfgp.split("/")[-1]==curp.split("/")[-1]:
print "This operation is not supported at this level."
else:
completer = WordCompleter(["GET","POST","PUT","PATCH","DELETE","back"], ignore_case=True)
text = session.prompt('select method> ',completer=completer,complete_while_typing=False)
if text=="GET":
print "\nExample request:\n"
print "\tGET "+build_url(cfgp, curp, cnt, cfg)+" HTTP/1.1"
print "\tHost: example.com"
print "\tAccept: application/yang-data+json\n"
elif text=="POST":
auxurl=build_url(cfgp, curp, cnt, cfg)
pprefix=cnt[curp]["orig"]
url='/'.join(auxurl.split("/")[:-1])
node=auxurl.split("/")[-1].split("=")[0]
print "\nExample request:\n"
print "\tPOST "+url+" HTTP/1.1"
print "\tHost: example.com"
print "\tAccept: application/yang-data+json\n"
print(json.dumps({pprefix+":"+node:from_dict_to_rc(curp,cnt,find_json(cfgp,cfg),pprefix, True)}, indent=4, sort_keys=True)+"\n")
#else: print(json.dumps({pprefix+":"+node:find_json(cfgp,cfg)}, indent=4, sort_keys=True)+"\n")
elif text=="PUT":
auxurl=build_url(cfgp, curp, cnt, cfg)
pprefix=cnt[curp]["orig"]
node=auxurl.split("/")[-1].split("=")[0]
print "\nExample request:\n"
print "\tPUT "+auxurl+" HTTP/1.1"
print "\tHost: example.com"
print "\tAccept: application/yang-data+json\n"
print(json.dumps({pprefix+":"+node:from_dict_to_rc(curp,cnt,find_json(cfgp,cfg),pprefix, True)}, indent=4, sort_keys=True)+"\n")
elif text=="PATCH":
auxurl=build_url(cfgp, curp, cnt, cfg)
pprefix=cnt[curp]["orig"]
node=auxurl.split("/")[-1].split("=")[0]
print "\nExample request:\n"
print "\tPATCH "+auxurl+" HTTP/1.1"
print "\tHost: example.com"
print "\tAccept: application/yang-data+json\n"
print(json.dumps({pprefix+":"+node:from_dict_to_rc(curp,cnt,find_json(cfgp,cfg),pprefix, True)}, indent=4, sort_keys=True)+"\n")
elif text=="DELETE":
print "\nExample request:\n"
print "\tDELETE "+build_url(cfgp, curp, cnt, cfg)+" HTTP/1.1"
print "\tHost: example.com"
print "\tAccept: application/yang-data+json\n"
else:
print "Method not supported."
def get_existing_keys(path,config):
steps=path.split("/")
aux=config
try:
for s in steps:
aux=aux[s]
return aux.keys()
except:
return []
def prompt_pyang(content):
aux=sorted(content.keys())
roots=[]
for i in aux:
if "/" not in i:
roots.append(i)
cmds=["save","saverc","load","loadrc","show-config","rc","delete","pwd","back","exit","logout","?"]
our_history = FileHistory('.pynavigate-history-file')
session = PromptSession(history=our_history)
config={}
configpath=""
meta={}
for r in roots:
meta[r]=content[r]["desc"]
current=""
stay=True
completer = WordCompleter(roots+cmds, meta_dict=meta, ignore_case=True)
amiinconfigurable=False
ctype=None
while stay:
txt="yang-cli> "
# If you do not want to update the path, delete the next two lines.
if len(configpath)!=0 and len(configpath)<=50: txt=configpath+"> "
elif len(configpath)!=0: txt="..."+configpath[-50:]+"> "
text = session.prompt(txt,completer=completer,complete_while_typing=False)
# print('%s' % text)
# print('%s' % current)
if text=="exit" or text=="logout":
stay=False
elif text=="pwd":
print configpath
elif text=="show-config":
print(json.dumps(config, indent=4, sort_keys=True))
elif text=="save":
ins=raw_input("file: ")
cfile=open(ins,"w")
cfile.write(json.dumps(config, indent=4, sort_keys=True))
elif text=="saverc":
ins=raw_input("file: ")
cfile=open(ins,"w")
write=True
if len(config.keys())>=1:
completer = WordCompleter(config.keys(), ignore_case=True)
print "Multiple roots"
text = session.prompt('select root> ',completer=completer,complete_while_typing=False)
if text not in config.keys():
print "No valid root selected"
write=False
else:
k=text
else: k=config.keys()[0]
if write:
cfgrc=from_dict_to_rc(k,content,config[k],content[k]["orig"],True)
cfile.write(json.dumps({content[k]["orig"]+":"+k:cfgrc}, indent=4, sort_keys=True))
elif text=="loadrc":
files=[]
for (dirpath, dirnames, filenames) in os.walk("."):
for f in filenames:
if os.path.isfile(dirpath+"/"+f):
if dirpath==".": files.append(f)
else: files.append(dirpath+"/"+f)
auxcomp=completer
completer = WordCompleter(files, ignore_case=True)
ins = session.prompt('file> ',completer=completer,complete_while_typing=False)
completer=auxcomp
#ins=raw_input("file: ")
if os.path.isfile(ins):
with open(ins,"r") as file:
#try:
cfgaux=json.loads(file.read())
auxlst=[]
auxcfg={}
if len(cfgaux.keys())>1:
print "Non-valid json. Multiple roots."
else:
r=cfgaux.keys()[0]
auxcfg.update(load_rc(r.split(":")[-1],r.split(":")[-1],cfgaux,content,True)[1])
config=auxcfg
#except:
# print "Malformed json"
else:
print "File not found"
elif text=="load":
files=[]
for (dirpath, dirnames, filenames) in os.walk("."):
for f in filenames:
if os.path.isfile(dirpath+"/"+f):
if dirpath==".": files.append(f)
else: files.append(dirpath+"/"+f)
auxcomp=completer
completer = WordCompleter(files, ignore_case=True)
ins = session.prompt('file> ',completer=completer,complete_while_typing=False)
completer=auxcomp
if os.path.isfile(ins):
with open(ins,"r") as file:
try:
config=json.loads(file.read())
except:
print "Malformed json"
else:
print "File not found"
elif text=="rc" and len(current)!=0:
restconf(configpath,current,content,config,session)
elif text=="delete":
if current=="":
print "You cannot delete the root!"
else:
delete_info(configpath,config)
ctype=None
if current in roots:
current=""
configpath=""
completer = WordCompleter(roots, ignore_case=True)
else:
if configpath.split("/")[-1] in current:
current='/'.join(current.split("/")[:-1])
configpath='/'.join(configpath.split("/")[:-1])
meta={}
for ch in content[current]["children"]:
meta[ch]=content[current+"/"+ch]["desc"]
completer = WordCompleter(content[current]["children"]+cmds, meta_dict=meta, ignore_case=True)
amiinconfigurable=False
else:
configpath='/'.join(configpath.split("/")[:-1])
extra=get_existing_keys(configpath,config)
if len(content[current]["key"].split(" "))==1:
meta["<"+content[current]["key"]+">"]=content[current+"/"+content[current]["key"]]["desc"]
completer = WordCompleter(["<"+content[current]["key"].replace(" ", ",")+">"]+extra+cmds, meta_dict=meta, ignore_case=True)
amiinconfigurable=True
elif text=="back":
ctype=None
if current in roots:
current=""
configpath=""
completer = WordCompleter(roots, ignore_case=True)
elif current=="":
print "I am (g)root"
else:
if configpath.split("/")[-1] in current:
current='/'.join(current.split("/")[:-1])
configpath='/'.join(configpath.split("/")[:-1])
meta={}
for ch in content[current]["children"]:
meta[ch]=content[current+"/"+ch]["desc"]
completer = WordCompleter(content[current]["children"]+cmds, meta_dict=meta, ignore_case=True)
amiinconfigurable=False
else:
configpath='/'.join(configpath.split("/")[:-1])
extra=get_existing_keys(configpath,config)
if len(content[current]["key"].split(" "))==1:
meta["<"+content[current]["key"]+">"]=content[current+"/"+content[current]["key"]]["desc"]
completer = WordCompleter(["<"+content[current]["key"].replace(" ", ",")+">"]+extra+cmds, meta_dict=meta, ignore_case=True)
amiinconfigurable=True
elif text=="?" and len(current)!=0:
if "desc" in content[current].keys(): print content[current]["desc"]
elif ctype:
fill_value(configpath,config,content[current]["type"], text)
elif " " in text and not amiinconfigurable: #Verify for the tricky situations
text=text.replace(" ","/")
if not (len(current)!=0 and current+"/"+text not in content.keys()) and not (len(current)==0 and text not in content.keys()):
if len(current)==0:
current=text
configpath=text
else:
current=current+"/"+text
configpath=configpath+"/"+text
if "children" in content[current].keys():
meta={}
if content[current]["key"] and not amiinconfigurable:
meta["<"+content[current]["key"]+">"]=content[current+"/"+content[current]["key"]]["desc"]
extra=get_existing_keys(configpath,config)
completer = WordCompleter(["<"+content[current]["key"]+">"]+extra+cmds, meta_dict=meta, ignore_case=True)
amiinconfigurable=True
fill_config(configpath,config,content[current]["type"])
else:
children=[]
for ch in content[current]["children"]:
if current+"/"+ch in content.keys():
meta[ch]=content[current+"/"+ch]["desc"]
children.append(ch)
completer = WordCompleter(children+cmds, meta_dict=meta, ignore_case=True)
amiinconfigurable=False
fill_config(configpath,config,content[current]["type"])
else: completer = WordCompleter(cmds, ignore_case=True)
elif len(text)==0: pass
elif not amiinconfigurable and ((len(current)!=0 and current+"/"+text not in content.keys()) or (len(current)==0 and text not in content.keys())):
print("Invalid command")
else:
if amiinconfigurable:
nkeys=len(content[current]["key"].split())
nentries=len(text.split(","))
if nkeys!=nentries:
print "You have not entered the right number of keys!!!"
continue
if len(configpath)==0:
configpath=text
else:
configpath=configpath+"/"+text
else:
if len(current)==0:
current=text
configpath=text
else:
current=current+"/"+text
configpath=configpath+"/"+text
if "children" in content[current].keys():
meta={}
if content[current]["key"] and not amiinconfigurable:
#TODO: Double check this multi-key corner case
if len(content[current]["key"].split(" "))==1:
meta["<"+content[current]["key"]+">"]=content[current+"/"+content[current]["key"]]["desc"]
extra=get_existing_keys(configpath,config)
completer = WordCompleter(["<"+content[current]["key"].replace(" ", ",")+">"]+extra+cmds, meta_dict=meta, ignore_case=True)
amiinconfigurable=True
fill_config(configpath,config,content[current]["type"])
else:
children=[]
for ch in content[current]["children"]:
if current+"/"+ch in content.keys():
meta[ch]=content[current+"/"+ch]["desc"]
children.append(ch)
completer = WordCompleter(children+cmds, meta_dict=meta, ignore_case=True)
amiinconfigurable=False
fill_config(configpath,config,content[current]["type"])
else:
completer = WordCompleter(["<"+content[current]["itype"]+">"]+cmds, ignore_case=True)
fill_config(configpath,config,content[current]["type"])
ctype=content[current]["type"]
with open(sys.argv[1],"r") as file:
trash=json.loads(file.read())
content=trash
for key in trash.keys():
if trash[key]["config"]:
content[key]=trash[key]
prompt_pyang(content)