4
4
from __future__ import print_function
5
5
import yang as ly
6
6
import syslog
7
-
8
7
from json import dump , dumps , loads
9
8
from xmltodict import parse
10
9
from glob import glob
11
10
11
+ qos_maps_model = ['DSCP_TO_TC_MAP_LIST' ,
12
+ 'DOT1P_TO_TC_MAP_LIST' ,
13
+ 'TC_TO_PRIORITY_GROUP_MAP_LIST' ,
14
+ 'TC_TO_QUEUE_MAP_LIST' ,
15
+ 'MAP_PFC_PRIORITY_TO_QUEUE_LIST' ,
16
+ 'PFC_PRIORITY_TO_PRIORITY_GROUP_MAP_LIST' ]
17
+
12
18
"""
13
19
This is the Exception thrown out of all public function of this class.
14
20
"""
@@ -406,6 +412,108 @@ def _yangConvert(val):
406
412
407
413
return vValue
408
414
415
+ """
416
+ Xlate a Qos Maps list
417
+ This function will xlate from a dict in config DB to a Yang JSON list
418
+ using yang model. Output will be go in self.xlateJson
419
+
420
+ Note: Exceptions from this function are collected in exceptionList and
421
+ are displayed only when an entry is not xlated properly from ConfigDB
422
+ to sonic_yang.json.
423
+
424
+ QOS MAPS Yang has inner list, which is diffrent from config DB.
425
+ Each field value in config db should be converted to inner list with
426
+ key and value.
427
+ Example:
428
+
429
+ Config DB:
430
+ "DSCP_TO_TC_MAP": {
431
+ "Dscp_to_tc_map1": {
432
+ "1": "1",
433
+ "2": "2"
434
+ }
435
+ }
436
+
437
+ YANG Model:
438
+ module: sonic-dscp-tc-map
439
+ +--rw sonic-dscp-tc-map
440
+ +--rw DSCP_TO_TC_MAP
441
+ +--rw DSCP_TO_TC_MAP_LIST* [name]
442
+ +--rw name string
443
+ +--rw DSCP_TO_TC_MAP* [dscp]
444
+ +--rw dscp string
445
+ +--rw tc? string
446
+
447
+ YANG JSON:
448
+ "sonic-dscp-tc-map:sonic-dscp-tc-map": {
449
+ "sonic-dscp-tc-map:DSCP_TO_TC_MAP": {
450
+ "DSCP_TO_TC_MAP_LIST": [
451
+ {
452
+ "name": "map3",
453
+ "DSCP_TO_TC_MAP": [
454
+ {
455
+ "dscp": "64",
456
+ "tc": "1"
457
+ },
458
+ {
459
+ "dscp":"2",
460
+ "tc":"2"
461
+ }
462
+ ]
463
+ }
464
+ ]
465
+ }
466
+ }
467
+ """
468
+ def _xlateQosMapList (self , model , yang , config , table , exceptionList ):
469
+
470
+ #create a dict to map each key under primary key with a dict yang model.
471
+ #This is done to improve performance of mapping from values of TABLEs in
472
+ #config DB to leaf in YANG LIST.
473
+ inner_clist = model .get ('list' )
474
+ if inner_clist :
475
+ inner_listKey = inner_clist ['key' ]['@value' ]
476
+ inner_leafDict = self ._createLeafDict (inner_clist , table )
477
+ for lkey in inner_leafDict :
478
+ if inner_listKey != lkey :
479
+ inner_listVal = lkey
480
+
481
+ # get keys from YANG model list itself
482
+ listKeys = model ['key' ]['@value' ]
483
+ self .sysLog (msg = "xlateList keyList:{}" .format (listKeys ))
484
+ primaryKeys = list (config .keys ())
485
+ for pkey in primaryKeys :
486
+ try :
487
+ vKey = None
488
+ self .sysLog (syslog .LOG_DEBUG , "xlateList Extract pkey:{}" .\
489
+ format (pkey ))
490
+ # Find and extracts key from each dict in config
491
+ keyDict = self ._extractKey (pkey , listKeys )
492
+
493
+ if inner_clist :
494
+ inner_yang_list = list ()
495
+ for vKey in config [pkey ]:
496
+ inner_keyDict = dict ()
497
+ self .sysLog (syslog .LOG_DEBUG , "xlateList Key {} vkey {} Val {} vval {}" .\
498
+ format (inner_listKey , str (vKey ), inner_listVal , str (config [pkey ][vKey ])))
499
+ inner_keyDict [inner_listKey ] = str (vKey )
500
+ inner_keyDict [inner_listVal ] = str (config [pkey ][vKey ])
501
+ inner_yang_list .append (inner_keyDict )
502
+
503
+ keyDict [inner_clist ['@name' ]] = inner_yang_list
504
+ yang .append (keyDict )
505
+ # delete pkey from config, done to match one key with one list
506
+ del config [pkey ]
507
+
508
+ except Exception as e :
509
+ # log debug, because this exception may occur with multilists
510
+ self .sysLog (msg = "xlateList Exception:{}" .format (str (e )), \
511
+ debug = syslog .LOG_DEBUG , doPrint = True )
512
+ exceptionList .append (str (e ))
513
+ # with multilist, we continue matching other keys.
514
+ continue
515
+ return
516
+
409
517
"""
410
518
Xlate a list
411
519
This function will xlate from a dict in config DB to a Yang JSON list
@@ -416,16 +524,22 @@ def _yangConvert(val):
416
524
to sonic_yang.json.
417
525
"""
418
526
def _xlateList (self , model , yang , config , table , exceptionList ):
527
+
528
+ #Qos Map lists needs special handling because of inner yang list and
529
+ #config db format.
530
+ if model ['@name' ] in qos_maps_model :
531
+ self .sysLog (msg = "_xlateQosMapList: {}" .format (model ['@name' ]))
532
+ self ._xlateQosMapList (model , yang ,config , table , exceptionList )
533
+ return
419
534
420
535
#create a dict to map each key under primary key with a dict yang model.
421
536
#This is done to improve performance of mapping from values of TABLEs in
422
537
#config DB to leaf in YANG LIST.
423
- leafDict = self ._createLeafDict (model , table )
424
538
539
+ leafDict = self ._createLeafDict (model , table )
425
540
# get keys from YANG model list itself
426
541
listKeys = model ['key' ]['@value' ]
427
542
self .sysLog (msg = "xlateList keyList:{}" .format (listKeys ))
428
-
429
543
primaryKeys = list (config .keys ())
430
544
for pkey in primaryKeys :
431
545
try :
@@ -459,7 +573,6 @@ def _xlateList(self, model, yang, config, table, exceptionList):
459
573
"""
460
574
def _xlateListInContainer (self , model , yang , configC , table , exceptionList ):
461
575
clist = model
462
- #print(clist['@name'])
463
576
yang [clist ['@name' ]] = list ()
464
577
self .sysLog (msg = "xlateProcessListOfContainer: {}" .format (clist ['@name' ]))
465
578
self ._xlateList (clist , yang [clist ['@name' ]], configC , table , exceptionList )
@@ -629,10 +742,93 @@ def _revYangConvert(val):
629
742
630
743
return vValue
631
744
745
+ """
746
+ Rev xlate from <TABLE>_LIST to table in config DB
747
+ QOS MAP Yang has inner list, each inner list key:val should
748
+ be mapped to field:value in Config DB.
749
+ Example:
750
+
751
+ YANG:
752
+ module: sonic-dscp-tc-map
753
+ +--rw sonic-dscp-tc-map
754
+ +--rw DSCP_TO_TC_MAP
755
+ +--rw DSCP_TO_TC_MAP_LIST* [name]
756
+ +--rw name string
757
+ +--rw DSCP_TO_TC_MAP* [dscp]
758
+ +--rw dscp string
759
+ +--rw tc? string
760
+
761
+ YANG JSON:
762
+ "sonic-dscp-tc-map:sonic-dscp-tc-map": {
763
+ "sonic-dscp-tc-map:DSCP_TO_TC_MAP": {
764
+ "DSCP_TO_TC_MAP_LIST": [
765
+ {
766
+ "name": "map3",
767
+ "DSCP_TO_TC_MAP": [
768
+ {
769
+ "dscp": "64",
770
+ "tc": "1"
771
+ },
772
+ {
773
+ "dscp":"2",
774
+ "tc":"2"
775
+ }
776
+ ]
777
+ }
778
+ ]
779
+ }
780
+ }
781
+
782
+ Config DB:
783
+ "DSCP_TO_TC_MAP": {
784
+ "Dscp_to_tc_map1": {
785
+ "1": "1",
786
+ "2": "2"
787
+ }
788
+ }
789
+ """
790
+
791
+ def _revQosMapXlateList (self , model , yang , config , table ):
792
+ # get keys from YANG model list itself
793
+ listKeys = model ['key' ]['@value' ]
794
+ # create a dict to map each key under primary key with a dict yang model.
795
+ # This is done to improve performance of mapping from values of TABLEs in
796
+ # config DB to leaf in YANG LIST.
797
+
798
+ # Gather inner list key and value from model
799
+ inner_clist = model .get ('list' )
800
+ if inner_clist :
801
+ inner_listKey = inner_clist ['key' ]['@value' ]
802
+ inner_leafDict = self ._createLeafDict (inner_clist , table )
803
+ for lkey in inner_leafDict :
804
+ if inner_listKey != lkey :
805
+ inner_listVal = lkey
806
+
807
+ # list with name <NAME>_LIST should be removed,
808
+ if "_LIST" in model ['@name' ]:
809
+ for entry in yang :
810
+ # create key of config DB table
811
+ pkey , pkeydict = self ._createKey (entry , listKeys )
812
+ self .sysLog (syslog .LOG_DEBUG , "revXlateList pkey:{}" .format (pkey ))
813
+ config [pkey ]= dict ()
814
+ # fill rest of the entries
815
+ inner_list = entry [inner_clist ['@name' ]]
816
+ for index in range (len (inner_list )):
817
+ self .sysLog (syslog .LOG_DEBUG , "revXlateList fkey:{} fval {}" .\
818
+ format (str (inner_list [index ][inner_listKey ]),\
819
+ str (inner_list [index ][inner_listVal ])))
820
+ config [pkey ][str (inner_list [index ][inner_listKey ])] = str (inner_list [index ][inner_listVal ])
821
+ return
822
+
632
823
"""
633
824
Rev xlate from <TABLE>_LIST to table in config DB
634
825
"""
635
826
def _revXlateList (self , model , yang , config , table ):
827
+
828
+ # special processing for QOS Map table.
829
+ if model ['@name' ] in qos_maps_model :
830
+ self ._revQosMapXlateList (model , yang , config , table )
831
+ return
636
832
637
833
# get keys from YANG model list itself
638
834
listKeys = model ['key' ]['@value' ]
0 commit comments