@@ -44,7 +44,6 @@ def loadYangModel(self):
44
44
self ._loadJsonYangModel ()
45
45
# create a map from config DB table to yang container
46
46
self ._createDBTableToModuleMap ()
47
-
48
47
except Exception as e :
49
48
self .sysLog (msg = "Yang Models Load failed:{}" .format (str (e )), \
50
49
debug = syslog .LOG_ERR , doPrint = True )
@@ -71,6 +70,70 @@ def _loadJsonYangModel(self):
71
70
72
71
return
73
72
73
+ def _preProcessYangGrouping (self , moduleName , module ):
74
+ '''
75
+ PreProcess Grouping Section of YANG models, and store it in
76
+ self.preProcessedYang['grouping'] as
77
+ {'<moduleName>':
78
+ {'<groupingName>':
79
+ [<List of Leafs>]
80
+ }
81
+ }
82
+
83
+ Parameters:
84
+ moduleName (str): name of yang module.
85
+ module (dict): json format of yang module.
86
+
87
+ Returns:
88
+ void
89
+ '''
90
+ try :
91
+ # create grouping dict
92
+ if self .preProcessedYang .get ('grouping' ) is None :
93
+ self .preProcessedYang ['grouping' ] = dict ()
94
+ self .preProcessedYang ['grouping' ][moduleName ] = dict ()
95
+
96
+ # get groupings from yang module
97
+ groupings = module ['grouping' ]
98
+
99
+ # if grouping is a dict, make it a list for common processing
100
+ if isinstance (groupings , dict ):
101
+ groupings = [groupings ]
102
+
103
+ for grouping in groupings :
104
+ gName = grouping ["@name" ]
105
+ gLeaf = grouping ["leaf" ]
106
+ self .preProcessedYang ['grouping' ][moduleName ][gName ] = gLeaf
107
+
108
+ except Exception as e :
109
+ self .sysLog (msg = "_preProcessYangGrouping failed:{}" .format (str (e )), \
110
+ debug = syslog .LOG_ERR , doPrint = True )
111
+ raise e
112
+ return
113
+
114
+ # preProcesss Generic Yang Objects
115
+ def _preProcessYang (self , moduleName , module ):
116
+ '''
117
+ PreProcess Generic Section of YANG models by calling
118
+ _preProcessYang<SectionName> methods.
119
+
120
+ Parameters:
121
+ moduleName (str): name of yang module.
122
+ module (dict): json format of yang module.
123
+
124
+ Returns:
125
+ void
126
+ '''
127
+ try :
128
+ # preProcesss Grouping
129
+ if module .get ('grouping' ) is not None :
130
+ self ._preProcessYangGrouping (moduleName , module )
131
+ except Exception as e :
132
+ self .sysLog (msg = "_preProcessYang failed:{}" .format (str (e )), \
133
+ debug = syslog .LOG_ERR , doPrint = True )
134
+ raise e
135
+ return
136
+
74
137
"""
75
138
Create a map from config DB tables to container in yang model
76
139
This module name and topLevelContainer are fetched considering YANG models are
@@ -82,6 +145,8 @@ def _createDBTableToModuleMap(self):
82
145
for j in self .yJson :
83
146
# get module name
84
147
moduleName = j ['module' ]['@name' ]
148
+ # preProcesss Generic Yang Objects
149
+ self ._preProcessYang (moduleName , j ['module' ])
85
150
# get top level container
86
151
topLevelContainer = j ['module' ].get ('container' )
87
152
# if top level container is none, this is common yang files, which may
@@ -104,14 +169,16 @@ def _createDBTableToModuleMap(self):
104
169
self .confDbYangMap [c ['@name' ]] = {
105
170
"module" : moduleName ,
106
171
"topLevelContainer" : topLevelContainer ['@name' ],
107
- "container" : c
172
+ "container" : c ,
173
+ "yangModule" : j ['module' ]
108
174
}
109
175
# container is a dict
110
176
else :
111
177
self .confDbYangMap [container ['@name' ]] = {
112
178
"module" : moduleName ,
113
179
"topLevelContainer" : topLevelContainer ['@name' ],
114
- "container" : container
180
+ "container" : container ,
181
+ "yangModule" : j ['module' ]
115
182
}
116
183
return
117
184
@@ -201,13 +268,87 @@ def _fillSteps(leaf):
201
268
202
269
return
203
270
204
- """
205
- create a dict to map each key under primary key with a dict yang model.
206
- This is done to improve performance of mapping from values of TABLEs in
207
- config DB to leaf in YANG LIST.
208
- """
209
- def _createLeafDict (self , model ):
271
+ def _findYangModuleFromPrefix (self , prefix , module ):
272
+ '''
273
+ Find yang module name from prefix used in given yang module.
210
274
275
+ Parameters:
276
+ prefix (str): prefix used in given yang module.
277
+ module (dict): json format of yang module.
278
+
279
+ Returns:
280
+ (str): module name or None
281
+ '''
282
+ try :
283
+ # get imports
284
+ yangImports = module .get ("import" );
285
+ if yangImports is None :
286
+ return None
287
+ # make a list
288
+ if isinstance (yangImports , dict ):
289
+ yangImports = [yangImports ]
290
+ # find module for given prefix
291
+ for yImport in yangImports :
292
+ if yImport ['prefix' ]['@value' ] == prefix :
293
+ return yImport ['@module' ]
294
+ except Exception as e :
295
+ self .sysLog (msg = "_findYangModuleFromPrefix failed:{}" .format (str (e )), \
296
+ debug = syslog .LOG_ERR , doPrint = True )
297
+ raise e
298
+ return None
299
+
300
+ def _fillLeafDictUses (self , uses_s , table , leafDict ):
301
+ '''
302
+ Find the leaf(s) in a grouping which maps to given uses statement,
303
+ then fill leafDict with leaf(s) information.
304
+
305
+ Parameters:
306
+ uses_s (str): uses statement in yang module.
307
+ table (str): config DB table, this table is being translated.
308
+ leafDict (dict): dict with leaf(s) information for List\Container
309
+ corresponding to config DB table.
310
+
311
+ Returns:
312
+ (void)
313
+ '''
314
+ try :
315
+ # make a list
316
+ if isinstance (uses_s , dict ):
317
+ uses_s = [uses_s ]
318
+ # find yang module for current table
319
+ table_module = self .confDbYangMap [table ]['yangModule' ]
320
+ # uses Example: "@name": "bgpcmn:sonic-bgp-cmn"
321
+ for uses in uses_s :
322
+ # Assume ':' means reference to another module
323
+ if ':' in uses ['@name' ]:
324
+ prefix = uses ['@name' ].split (':' )[0 ].strip ()
325
+ uses_module = self ._findYangModuleFromPrefix (prefix , table_module )
326
+ else :
327
+ uses_module = table_module
328
+ grouping = uses ['@name' ].split (':' )[- 1 ].strip ()
329
+ leafs = self .preProcessedYang ['grouping' ][uses_module ][grouping ]
330
+ self ._fillLeafDict (leafs , leafDict )
331
+ except Exception as e :
332
+ self .sysLog (msg = "_fillLeafDictUses failed:{}" .format (str (e )), \
333
+ debug = syslog .LOG_ERR , doPrint = True )
334
+ raise e
335
+
336
+ return
337
+
338
+ def _createLeafDict (self , model , table ):
339
+ '''
340
+ create a dict to map each key under primary key with a leaf in yang model.
341
+ This is done to improve performance of mapping from values of TABLEs in
342
+ config DB to leaf in YANG LIST.
343
+
344
+ Parameters:
345
+ module (dict): json format of yang module.
346
+ table (str): config DB table, this table is being translated.
347
+
348
+ Returns:
349
+ leafDict (dict): dict with leaf(s) information for List\Container
350
+ corresponding to config DB table.
351
+ '''
211
352
leafDict = dict ()
212
353
#Iterate over leaf, choices and leaf-list.
213
354
self ._fillLeafDict (model .get ('leaf' ), leafDict )
@@ -223,6 +364,10 @@ def _createLeafDict(self, model):
223
364
# leaf-lists
224
365
self ._fillLeafDict (model .get ('leaf-list' ), leafDict , True )
225
366
367
+ # uses should map to grouping,
368
+ if model .get ('uses' ) is not None :
369
+ self ._fillLeafDictUses (model .get ('uses' ), table , leafDict )
370
+
226
371
return leafDict
227
372
228
373
"""
@@ -245,7 +390,7 @@ def _yangConvert(val):
245
390
elif 'leafref' in type :
246
391
vValue = val
247
392
#TODO: find type in sonic-head, as of now, all are enumeration
248
- elif 'head :' in type :
393
+ elif 'stypes :' in type :
249
394
vValue = val
250
395
else :
251
396
vValue = val
@@ -275,7 +420,7 @@ def _xlateList(self, model, yang, config, table, exceptionList):
275
420
#create a dict to map each key under primary key with a dict yang model.
276
421
#This is done to improve performance of mapping from values of TABLEs in
277
422
#config DB to leaf in YANG LIST.
278
- leafDict = self ._createLeafDict (model )
423
+ leafDict = self ._createLeafDict (model , table )
279
424
280
425
# get keys from YANG model list itself
281
426
listKeys = model ['key' ]['@value' ]
@@ -380,7 +525,7 @@ def _xlateContainer(self, model, yang, config, table):
380
525
self ._xlateContainerInContainer (modelContainer , yang , configC , table )
381
526
382
527
## Handle other leaves in container,
383
- leafDict = self ._createLeafDict (model )
528
+ leafDict = self ._createLeafDict (model , table )
384
529
vKeys = list (configC .keys ())
385
530
for vKey in vKeys :
386
531
#vkey must be a leaf\leaf-list\choice in container
@@ -494,7 +639,7 @@ def _revXlateList(self, model, yang, config, table):
494
639
# create a dict to map each key under primary key with a dict yang model.
495
640
# This is done to improve performance of mapping from values of TABLEs in
496
641
# config DB to leaf in YANG LIST.
497
- leafDict = self ._createLeafDict (model )
642
+ leafDict = self ._createLeafDict (model , table )
498
643
499
644
# list with name <NAME>_LIST should be removed,
500
645
if "_LIST" in model ['@name' ]:
@@ -559,7 +704,7 @@ def _revXlateContainer(self, model, yang, config, table):
559
704
self ._revXlateContainerInContainer (modelContainer , yang , config , table )
560
705
561
706
## Handle other leaves in container,
562
- leafDict = self ._createLeafDict (model )
707
+ leafDict = self ._createLeafDict (model , table )
563
708
for vKey in yang :
564
709
#vkey must be a leaf\leaf-list\choice in container
565
710
if leafDict .get (vKey ):
0 commit comments