@@ -24,6 +24,7 @@ import (
24
24
25
25
var allimports = make (map [string ]string )
26
26
var modules = make (map [string ]* yang.Module )
27
+ var allmodules = make (map [string ]* yang.Module )
27
28
28
29
func init () {
29
30
register (& formatter {
@@ -36,14 +37,13 @@ func init() {
36
37
37
38
// Get the modules for which annotation file needs to be generated
38
39
func getFile (files []string , mods map [string ]* yang.Module ) {
40
+ allmodules = mods
39
41
for _ , name := range files {
40
42
slash := strings .Split (name , "/" )
41
- if strings .HasSuffix (name , ".yang" ) {
42
43
modname := slash [len (slash )- 1 ]
43
44
modname = strings .TrimSuffix (modname , ".yang" );
44
45
/* Save the yang.Module entries we are interested in */
45
46
modules [modname ] = mods [modname ]
46
- }
47
47
}
48
48
}
49
49
@@ -53,7 +53,8 @@ func genAnnotate(w io.Writer, entries []*yang.Entry) {
53
53
for _ , e := range entries {
54
54
if _ , ok := modules [e .Name ]; ok {
55
55
var path string = ""
56
- generate (w , e , path )
56
+ var prefix string = ""
57
+ generate (w , e , path , prefix )
57
58
// { Add closing brace for each module
58
59
fmt .Fprintln (w , "}" )
59
60
fmt .Fprintln (w )
@@ -62,66 +63,97 @@ func genAnnotate(w io.Writer, entries []*yang.Entry) {
62
63
}
63
64
64
65
// generate writes to stdoutput a template annotation file entry for the selected modules.
65
- func generate (w io.Writer , e * yang.Entry , path string ) {
66
+ func generate (w io.Writer , e * yang.Entry , path string , prefix string ) {
66
67
if e .Parent == nil {
67
68
if e .Name != "" {
68
69
fmt .Fprintf (w , "module %s-annot {\n " , e .Name ) //}
69
70
fmt .Fprintln (w )
70
- fmt .Fprintf (w , " yang-version \" %s\" \n " , getYangVersion (e .Name , modules ))
71
+ fmt .Fprintf (w , " yang-version \" %s\" ; \n " , getYangVersion (e .Name , modules ))
71
72
fmt .Fprintln (w )
72
- fmt .Fprintf (w , " namespace \" http://openconfig.net/yang/annotation\" ;\n " )
73
+ fmt .Fprintf (w , " namespace \" http://openconfig.net/yang/annotation/%s-annot \" ;\n " , e . Prefix . Name )
73
74
if e .Prefix != nil {
74
- fmt .Fprintf (w , " prefix \" %s-annot\" \n " , e .Prefix .Name )
75
+ fmt .Fprintf (w , " prefix \" %s-annot\" ; \n " , e .Prefix .Name )
75
76
}
76
77
fmt .Fprintln (w )
77
78
78
79
var imports = make (map [string ]string )
79
80
imports = getImportModules (e .Name , modules )
80
81
for k := range imports {
81
82
if e .Name != k {
82
- fmt .Fprintf (w , " import %s { prefix %s }\n " , k , allimports [k ])
83
+ fmt .Fprintf (w , " import %s { prefix %s; }\n " , k , allimports [k ])
83
84
}
84
85
}
86
+ // Include the module for which annotation is being generated
87
+ fmt .Fprintf (w , " import %s { prefix %s; }\n " , e .Name , e .Prefix .Name )
85
88
86
89
fmt .Fprintln (w )
87
90
}
88
91
}
89
92
90
93
name := e .Name
91
- if e .Prefix != nil {
92
- name = e .Prefix .Name + ":" + name
94
+ if prefix == "" && e .Prefix != nil {
95
+ prefix = e .Prefix .Name
93
96
}
97
+ name = prefix + ":" + name
94
98
95
- delim : = ""
96
- if path != "" {
97
- delim = "/"
99
+ if ( e . Node . Kind () ! = "module" ) {
100
+ path = path + "/" + name
101
+ printDeviation ( w , path )
98
102
}
99
- path = path + delim + name
100
-
101
- fmt .Fprintf (w , " deviation %s {\n " , path )
102
- fmt .Fprintf (w , " deviate add {\n " )
103
- fmt .Fprintf (w , " }\n " )
104
- fmt .Fprintf (w , " }\n " )
105
- fmt .Fprintln (w )
106
103
107
104
var names []string
108
105
for k := range e .Dir {
109
106
names = append (names , k )
110
107
}
111
108
109
+ if (e .Node .Kind () == "module" ) {
110
+ if len (e .Node .(* yang.Module ).Augment ) > 0 {
111
+ for _ ,a := range e .Node .(* yang.Module ).Augment {
112
+ pathList := strings .Split (a .Name , "/" )
113
+ pathList = pathList [1 :]
114
+ for i , pvar := range pathList {
115
+ if len (pvar ) > 0 && ! strings .Contains (pvar , ":" ) {
116
+ pvar = e .Prefix .Name + ":" + pvar
117
+ pathList [i ] = pvar
118
+ }
119
+ }
120
+ path = "/" + strings .Join (pathList , "/" )
121
+ handleAugments (w , a , e .Node .(* yang.Module ).Grouping , e .Prefix .Name , path )
122
+ }
123
+ }
124
+ }
125
+
112
126
for _ , k := range names {
113
- generate (w , e .Dir [k ], path )
127
+ generate (w , e .Dir [k ], path , prefix )
114
128
}
115
129
116
130
}
117
131
132
+ func printDeviation (w io.Writer , path string ){
133
+ fmt .Fprintf (w , " deviation %s {\n " , path )
134
+ fmt .Fprintf (w , " deviate add {\n " )
135
+ fmt .Fprintf (w , " }\n " )
136
+ fmt .Fprintf (w , " }\n " )
137
+ fmt .Fprintln (w )
138
+ }
139
+
140
+
118
141
// Save to map all imported modules
119
142
func GetAllImports (entries []* yang.Entry ) {
120
143
for _ , e := range entries {
121
144
allimports [e .Name ] = e .Prefix .Name
122
145
}
123
146
}
124
147
148
+ func GetModuleFromPrefix (prefix string ) string {
149
+ for m , p := range allimports {
150
+ if prefix == p {
151
+ return m
152
+ }
153
+ }
154
+ return ""
155
+ }
156
+
125
157
//Get Yang version from the yang.Modules
126
158
func getYangVersion (modname string , mods map [string ]* yang.Module ) string {
127
159
if (mods [modname ].YangVersion != nil ) {
@@ -141,3 +173,223 @@ func getImportModules(modname string, mods map[string]*yang.Module) map[string]s
141
173
}
142
174
return imports
143
175
}
176
+
177
+ func handleAugments (w io.Writer , a * yang.Augment , grp []* yang.Grouping , prefix string , path string ) {
178
+ for _ , u := range a .Uses {
179
+ grpN := u .Name
180
+ for _ , g := range grp {
181
+ if grpN == g .Name {
182
+ if len (g .Container ) > 0 {
183
+ handleContainer (w , g .Container , grp , prefix , path )
184
+ }
185
+ if len (g .List ) > 0 {
186
+ handleList (w , g .List , grp , prefix , path )
187
+ }
188
+ if len (g .LeafList ) > 0 {
189
+ handleLeafList (w , g .LeafList , prefix , path )
190
+ }
191
+ if len (g .Leaf ) > 0 {
192
+ handleLeaf (w , g .Leaf , prefix , path )
193
+ }
194
+ if len (g .Choice ) > 0 {
195
+ handleChoice (w , g .Choice , grp , prefix , path )
196
+ }
197
+ if len (g .Uses ) > 0 {
198
+ handleUses (w , g .Uses , grp , prefix , path )
199
+ }
200
+ }
201
+ }
202
+ }
203
+
204
+ }
205
+
206
+ func handleUses (w io.Writer , u []* yang.Uses , grp []* yang.Grouping , prefix string , path string ) {
207
+ for _ , u := range u {
208
+ grpN := u .Name
209
+ if strings .Contains (grpN , ":" ) {
210
+ tokens := strings .Split (grpN , ":" )
211
+ nprefix := tokens [0 ]
212
+ grpN = tokens [1 ]
213
+ mod := GetModuleFromPrefix (nprefix )
214
+ grp = allmodules [mod ].Grouping
215
+ }
216
+ for _ , g := range grp {
217
+ if grpN == g .Name {
218
+ if len (g .Container ) > 0 {
219
+ handleContainer (w , g .Container , grp , prefix , path )
220
+ }
221
+ if len (g .List ) > 0 {
222
+ handleList (w , g .List , grp , prefix , path )
223
+ }
224
+ if len (g .LeafList ) > 0 {
225
+ handleLeafList (w , g .LeafList , prefix , path )
226
+ }
227
+ if len (g .Leaf ) > 0 {
228
+ handleLeaf (w , g .Leaf , prefix , path )
229
+ }
230
+ if len (g .Choice ) > 0 {
231
+ handleChoice (w , g .Choice , grp , prefix , path )
232
+ }
233
+ if len (g .Uses ) > 0 {
234
+ handleUses (w , g .Uses , grp , prefix , path )
235
+ }
236
+
237
+ }
238
+ }
239
+ }
240
+
241
+ }
242
+
243
+ func handleContainer (w io.Writer , ctr []* yang.Container , grp []* yang.Grouping , prefix string , path string ) {
244
+ for _ , c := range ctr {
245
+ npath := path + "/" + prefix + ":" + c .Name
246
+ printDeviation (w , npath )
247
+ if len (c .Container ) > 0 {
248
+ handleContainer (w , c .Container , grp , prefix , npath )
249
+ }
250
+ if len (c .List ) > 0 {
251
+ handleList (w , c .List , grp , prefix , npath )
252
+ }
253
+ if len (c .LeafList ) > 0 {
254
+ handleLeafList (w , c .LeafList , prefix , npath )
255
+ }
256
+ if len (c .Leaf ) > 0 {
257
+ handleLeaf (w , c .Leaf , prefix , npath )
258
+ }
259
+ if len (c .Choice ) > 0 {
260
+ handleChoice (w , c .Choice , grp , prefix , npath )
261
+ }
262
+ if len (c .Grouping ) > 0 {
263
+ handleGrouping (w , c .Grouping , grp , prefix , npath )
264
+ }
265
+ if len (c .Uses ) > 0 {
266
+ handleUses (w , c .Uses , grp , prefix , npath )
267
+ }
268
+ }
269
+ }
270
+
271
+ func handleList (w io.Writer , lst []* yang.List , grp []* yang.Grouping , prefix string , path string ) {
272
+ for _ , l := range lst {
273
+ npath := path + "/" + prefix + ":" + l .Name
274
+ printDeviation (w , npath )
275
+ if len (l .Container ) > 0 {
276
+ handleContainer (w , l .Container , grp , prefix , npath )
277
+ }
278
+ if len (l .List ) > 0 {
279
+ handleList (w , l .List , grp , prefix , npath )
280
+ }
281
+ if len (l .LeafList ) > 0 {
282
+ handleLeafList (w , l .LeafList , prefix , npath )
283
+ }
284
+ if len (l .Leaf ) > 0 {
285
+ handleLeaf (w , l .Leaf , prefix , npath )
286
+ }
287
+ if len (l .Choice ) > 0 {
288
+ handleChoice (w , l .Choice , grp , prefix , npath )
289
+ }
290
+ if len (l .Grouping ) > 0 {
291
+ handleGrouping (w , l .Grouping , grp , prefix , npath )
292
+ }
293
+ if len (l .Uses ) > 0 {
294
+ handleUses (w , l .Uses , grp , prefix , npath )
295
+ }
296
+
297
+ }
298
+ }
299
+
300
+ func handleGrouping (w io.Writer , grp []* yang.Grouping , grptop []* yang.Grouping , prefix string , path string ) {
301
+ for _ , g := range grp {
302
+ npath := path + "/" + prefix + ":" + g .Name
303
+ printDeviation (w , npath )
304
+ if len (g .Container ) > 0 {
305
+ handleContainer (w , g .Container , grptop , prefix , npath )
306
+ }
307
+ if len (g .List ) > 0 {
308
+ handleList (w , g .List , grptop , prefix , npath )
309
+ }
310
+ if len (g .LeafList ) > 0 {
311
+ handleLeafList (w , g .LeafList , prefix , npath )
312
+ }
313
+ if len (g .Leaf ) > 0 {
314
+ handleLeaf (w , g .Leaf , prefix , npath )
315
+ }
316
+ if len (g .Choice ) > 0 {
317
+ handleChoice (w , g .Choice , grptop , prefix , npath )
318
+ }
319
+ if len (g .Grouping ) > 0 {
320
+ handleGrouping (w , g .Grouping , grptop , prefix , npath )
321
+ }
322
+ if len (g .Uses ) > 0 {
323
+ handleUses (w , g .Uses , grptop , prefix , npath )
324
+ }
325
+
326
+ }
327
+ }
328
+
329
+ func handleLeaf (w io.Writer , lf []* yang.Leaf , prefix string , path string ) {
330
+ if len (lf ) > 0 {
331
+ for _ , l := range lf {
332
+ npath := path + "/" + prefix + ":" + l .Name
333
+ printDeviation (w , npath )
334
+ }
335
+ }
336
+
337
+ }
338
+
339
+ func handleLeafList (w io.Writer , llst []* yang.LeafList , prefix string , path string ) {
340
+ if len (llst ) > 0 {
341
+ for _ , l := range llst {
342
+ npath := path + "/" + prefix + ":" + l .Name
343
+ printDeviation (w , npath )
344
+ }
345
+ }
346
+ }
347
+
348
+ func handleChoice (w io.Writer , ch []* yang.Choice , grp []* yang.Grouping , prefix string , path string ) {
349
+ for _ , c := range ch {
350
+ npath := path + "/" + prefix + ":" + c .Name
351
+ printDeviation (w , npath )
352
+ if len (c .Container ) > 0 {
353
+ handleContainer (w , c .Container , grp , prefix , npath )
354
+ }
355
+ if len (c .List ) > 0 {
356
+ handleList (w , c .List , grp , prefix , npath )
357
+ }
358
+ if len (c .LeafList ) > 0 {
359
+ handleLeafList (w , c .LeafList , prefix , npath )
360
+ }
361
+ if len (c .Leaf ) > 0 {
362
+ handleLeaf (w , c .Leaf , prefix , npath )
363
+ }
364
+ if len (c .Case ) > 0 {
365
+ handleCase (w , c .Case , grp , prefix , npath )
366
+ }
367
+ }
368
+ }
369
+
370
+ func handleCase (w io.Writer , ch []* yang.Case , grp []* yang.Grouping , prefix string , path string ) {
371
+ for _ , c := range ch {
372
+ npath := path + "/" + prefix + ":" + c .Name
373
+ printDeviation (w , npath )
374
+ if len (c .Container ) > 0 {
375
+ handleContainer (w , c .Container , grp , prefix , npath )
376
+ }
377
+ if len (c .List ) > 0 {
378
+ handleList (w , c .List , grp , prefix , npath )
379
+ }
380
+ if len (c .LeafList ) > 0 {
381
+ handleLeafList (w , c .LeafList , prefix , npath )
382
+ }
383
+ if len (c .Leaf ) > 0 {
384
+ handleLeaf (w , c .Leaf , prefix , npath )
385
+ }
386
+ if len (c .Choice ) > 0 {
387
+ handleChoice (w , c .Choice , grp , prefix , npath )
388
+ }
389
+ if len (c .Uses ) > 0 {
390
+ handleUses (w , c .Uses , grp , prefix , npath )
391
+ }
392
+
393
+ }
394
+ }
395
+
0 commit comments