@@ -24,7 +24,6 @@ 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 )
28
27
29
28
func init () {
30
29
register (& formatter {
@@ -37,13 +36,14 @@ func init() {
37
36
38
37
// Get the modules for which annotation file needs to be generated
39
38
func getFile (files []string , mods map [string ]* yang.Module ) {
40
- allmodules = mods
41
39
for _ , name := range files {
42
40
slash := strings .Split (name , "/" )
41
+ if strings .HasSuffix (name , ".yang" ) {
43
42
modname := slash [len (slash )- 1 ]
44
43
modname = strings .TrimSuffix (modname , ".yang" );
45
44
/* Save the yang.Module entries we are interested in */
46
45
modules [modname ] = mods [modname ]
46
+ }
47
47
}
48
48
}
49
49
@@ -53,8 +53,7 @@ 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
- var prefix string = ""
57
- generate (w , e , path , prefix )
56
+ generate (w , e , path )
58
57
// { Add closing brace for each module
59
58
fmt .Fprintln (w , "}" )
60
59
fmt .Fprintln (w )
@@ -63,97 +62,66 @@ func genAnnotate(w io.Writer, entries []*yang.Entry) {
63
62
}
64
63
65
64
// generate writes to stdoutput a template annotation file entry for the selected modules.
66
- func generate (w io.Writer , e * yang.Entry , path string , prefix string ) {
65
+ func generate (w io.Writer , e * yang.Entry , path string ) {
67
66
if e .Parent == nil {
68
67
if e .Name != "" {
69
68
fmt .Fprintf (w , "module %s-annot {\n " , e .Name ) //}
70
69
fmt .Fprintln (w )
71
- fmt .Fprintf (w , " yang-version \" %s\" ; \n " , getYangVersion (e .Name , modules ))
70
+ fmt .Fprintf (w , " yang-version \" %s\" \n " , getYangVersion (e .Name , modules ))
72
71
fmt .Fprintln (w )
73
- fmt .Fprintf (w , " namespace \" http://openconfig.net/yang/annotation/%s-annot \" ;\n " , e . Prefix . Name )
72
+ fmt .Fprintf (w , " namespace \" http://openconfig.net/yang/annotation\" ;\n " )
74
73
if e .Prefix != nil {
75
- fmt .Fprintf (w , " prefix \" %s-annot\" ; \n " , e .Prefix .Name )
74
+ fmt .Fprintf (w , " prefix \" %s-annot\" \n " , e .Prefix .Name )
76
75
}
77
76
fmt .Fprintln (w )
78
77
79
78
var imports = make (map [string ]string )
80
79
imports = getImportModules (e .Name , modules )
81
80
for k := range imports {
82
81
if e .Name != k {
83
- fmt .Fprintf (w , " import %s { prefix %s; }\n " , k , allimports [k ])
82
+ fmt .Fprintf (w , " import %s { prefix %s }\n " , k , allimports [k ])
84
83
}
85
84
}
86
- // Include the module for which annotation is being generated
87
- fmt .Fprintf (w , " import %s { prefix %s; }\n " , e .Name , e .Prefix .Name )
88
85
89
86
fmt .Fprintln (w )
90
87
}
91
88
}
92
89
93
90
name := e .Name
94
- if prefix == "" && e .Prefix != nil {
95
- prefix = e .Prefix .Name
91
+ if e .Prefix != nil {
92
+ name = e .Prefix .Name + ":" + name
96
93
}
97
- name = prefix + ":" + name
98
94
99
- if ( e . Node . Kind () ! = "module" ) {
100
- path = path + "/" + name
101
- printDeviation ( w , path )
95
+ delim : = ""
96
+ if path != "" {
97
+ delim = "/"
102
98
}
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 )
103
106
104
107
var names []string
105
108
for k := range e .Dir {
106
109
names = append (names , k )
107
110
}
108
111
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
-
126
112
for _ , k := range names {
127
- generate (w , e .Dir [k ], path , prefix )
113
+ generate (w , e .Dir [k ], path )
128
114
}
129
115
130
116
}
131
117
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
-
141
118
// Save to map all imported modules
142
119
func GetAllImports (entries []* yang.Entry ) {
143
120
for _ , e := range entries {
144
121
allimports [e .Name ] = e .Prefix .Name
145
122
}
146
123
}
147
124
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
-
157
125
//Get Yang version from the yang.Modules
158
126
func getYangVersion (modname string , mods map [string ]* yang.Module ) string {
159
127
if (mods [modname ].YangVersion != nil ) {
@@ -173,223 +141,3 @@ func getImportModules(modname string, mods map[string]*yang.Module) map[string]s
173
141
}
174
142
return imports
175
143
}
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