@@ -419,43 +419,35 @@ def parse_xpath_to_gnmi_path(self, xpath, origin=None):
419
419
path .elem .extend (path_elems )
420
420
return path
421
421
422
- def combine_configs (self , payload , last_xpath , xpath , config ):
422
+ def combine_configs (self , payload , last_xpath , cfg ):
423
423
"""Walking from end to finish 2 xpaths merge so combine them
424
-
425
- |---last xpath config
426
- ----|
427
- |---this xpath config
428
-
424
+ |--config
425
+ |---last xpath config--|
426
+ ----| |--config
427
+ |
428
+ | pick these up --> |--config
429
+ |---this xpath config--|
430
+ |--config
429
431
Parameters
430
432
----------
431
433
payload: dict of partial payload
432
434
last_xpath: last xpath that was processed
433
435
xpath: colliding xpath
434
436
config: dict of values associated to colliding xpath
435
437
"""
436
- last_set = set (last_xpath .split ('/' ))
437
- curr_diff = set (xpath .split ('/' )) - last_set
438
- if len (curr_diff ) > 1 :
439
- print ('combine_configs() error1' )
440
- return payload
441
- index = curr_diff .pop ()
442
- curr_xpath = xpath [xpath .find (index ):]
443
- curr_xpath = curr_xpath .split ('/' )
444
- curr_xpath .reverse ()
445
- for seg in curr_xpath :
446
- config = {seg : config }
447
-
448
- last_diff = last_set - set (xpath .split ('/' ))
449
- if len (last_diff ) > 1 :
450
- print ('combine_configs() error2' )
451
- return payload
452
- last_xpath = last_xpath [last_xpath .find (last_diff .pop ()):]
453
- last_xpath = last_xpath .split ('/' )
454
- last_xpath .reverse ()
455
- for seg in last_xpath :
456
- if seg not in payload :
457
- payload = {seg : payload }
458
- payload .update (config )
438
+ xpath , config , is_key = cfg
439
+ lp = last_xpath .split ('/' )
440
+ xp = xpath .split ('/' )
441
+ base = []
442
+ top = ''
443
+ for i , seg in enumerate (zip (lp , xp )):
444
+ if seg [0 ] != seg [1 ]:
445
+ top = seg [1 ]
446
+ break
447
+ base = '/' + '/' .join (xp [i :])
448
+ cfg = (base , config , False )
449
+ extended_payload = {top : self .xpath_to_json ([cfg ])}
450
+ payload .update (extended_payload )
459
451
return payload
460
452
461
453
def xpath_to_json (self , configs , last_xpath = '' , payload = {}):
@@ -474,11 +466,11 @@ def xpath_to_json(self, configs, last_xpath='', payload={}):
474
466
for i , cfg in enumerate (configs , 1 ):
475
467
xpath , config , is_key = cfg
476
468
if last_xpath and xpath not in last_xpath :
477
- # Branched config here
478
- # |---last xpath config
479
- # --|
469
+ # Branched config here |---config
470
+ # |---last xpath config--|
471
+ # --| |---config
480
472
# |---this xpath config
481
- payload = self .combine_configs (payload , last_xpath , xpath , config )
473
+ payload = self .combine_configs (payload , last_xpath , cfg )
482
474
return self .xpath_to_json (configs [i :], xpath , payload )
483
475
xpath_segs = xpath .split ('/' )
484
476
xpath_segs .reverse ()
@@ -733,7 +725,6 @@ def xml_path_to_path_elem(self, request):
733
725
if __name__ == '__main__' :
734
726
from pprint import pprint as pp
735
727
import grpc
736
- from cisco_gnmi import Client
737
728
from cisco_gnmi .auth import CiscoAuthPlugin
738
729
channel = grpc .secure_channel (
739
730
'127.0.0.1:9339' ,
@@ -790,7 +781,7 @@ def xml_path_to_path_elem(self, request):
790
781
}
791
782
]
792
783
}
793
- modules , message , origin = client .xpath_to_path_elem (request )
784
+ modules , message , origin = client .xml_path_to_path_elem (request )
794
785
pp (modules )
795
786
pp (message )
796
787
pp (origin )
0 commit comments