Skip to content

Commit ca4b330

Browse files
Merge pull request #173 from project-arlo/transformer-phase2
Transformer phase2
2 parents 211f17e + eea6d40 commit ca4b330

29 files changed

+2460
-591
lines changed

Makefile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ codegen:
9494

9595
yamlGen:
9696
$(MAKE) -C models/yang
97+
$(MAKE) -C models/yang/sonic
9798

9899
go-patch: go-deps
99100
cd $(BUILD_GOPATH)/src/github.com/openconfig/ygot/; git reset --hard HEAD; git checkout 724a6b18a9224343ef04fe49199dfb6020ce132a 2>/dev/null ; true; \
@@ -108,9 +109,10 @@ install:
108109
$(INSTALL) -d $(DESTDIR)/usr/sbin/schema/
109110
$(INSTALL) -d $(DESTDIR)/usr/sbin/lib/
110111
$(INSTALL) -d $(DESTDIR)/usr/models/yang/
112+
$(INSTALL) -D $(TOPDIR)/models/yang/sonic/*.yang $(DESTDIR)/usr/models/yang/
113+
$(INSTALL) -D $(TOPDIR)/models/yang/sonic/common/*.yang $(DESTDIR)/usr/models/yang/
111114
$(INSTALL) -D $(TOPDIR)/src/cvl/schema/*.yin $(DESTDIR)/usr/sbin/schema/
112115
$(INSTALL) -D $(TOPDIR)/src/cvl/testdata/schema/*.yin $(DESTDIR)/usr/sbin/schema/
113-
$(INSTALL) -D $(TOPDIR)/src/cvl/schema/*.yang $(DESTDIR)/usr/models/yang/
114116
$(INSTALL) -D $(TOPDIR)/models/yang/*.yang $(DESTDIR)/usr/models/yang/
115117
$(INSTALL) -D $(TOPDIR)/config/transformer/models_list $(DESTDIR)/usr/models/yang/
116118
$(INSTALL) -D $(TOPDIR)/models/yang/common/*.yang $(DESTDIR)/usr/models/yang/

goyang-modified-files/annotate.go

Lines changed: 273 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424

2525
var allimports = make(map[string]string)
2626
var modules = make(map[string]*yang.Module)
27+
var allmodules = make(map[string]*yang.Module)
2728

2829
func init() {
2930
register(&formatter{
@@ -36,14 +37,13 @@ func init() {
3637

3738
// Get the modules for which annotation file needs to be generated
3839
func getFile(files []string, mods map[string]*yang.Module) {
40+
allmodules = mods
3941
for _, name := range files {
4042
slash := strings.Split(name, "/")
41-
if strings.HasSuffix(name, ".yang") {
4243
modname := slash[len(slash)-1]
4344
modname = strings.TrimSuffix(modname, ".yang");
4445
/* Save the yang.Module entries we are interested in */
4546
modules[modname] = mods[modname]
46-
}
4747
}
4848
}
4949

@@ -53,7 +53,8 @@ func genAnnotate(w io.Writer, entries []*yang.Entry) {
5353
for _, e := range entries {
5454
if _, ok := modules[e.Name]; ok {
5555
var path string = ""
56-
generate(w, e, path)
56+
var prefix string = ""
57+
generate(w, e, path, prefix)
5758
// { Add closing brace for each module
5859
fmt.Fprintln(w, "}")
5960
fmt.Fprintln(w)
@@ -62,66 +63,97 @@ func genAnnotate(w io.Writer, entries []*yang.Entry) {
6263
}
6364

6465
// 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) {
6667
if e.Parent == nil {
6768
if e.Name != "" {
6869
fmt.Fprintf(w, "module %s-annot {\n", e.Name) //}
6970
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))
7172
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)
7374
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)
7576
}
7677
fmt.Fprintln(w)
7778

7879
var imports = make(map[string]string)
7980
imports = getImportModules(e.Name, modules)
8081
for k := range imports {
8182
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])
8384
}
8485
}
86+
// Include the module for which annotation is being generated
87+
fmt.Fprintf(w, " import %s { prefix %s; }\n", e.Name, e.Prefix.Name)
8588

8689
fmt.Fprintln(w)
8790
}
8891
}
8992

9093
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
9396
}
97+
name = prefix + ":" + name
9498

95-
delim := ""
96-
if path != "" {
97-
delim = "/"
99+
if (e.Node.Kind() != "module") {
100+
path = path + "/" + name
101+
printDeviation(w, path)
98102
}
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)
106103

107104
var names []string
108105
for k := range e.Dir {
109106
names = append(names, k)
110107
}
111108

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+
112126
for _, k := range names {
113-
generate(w, e.Dir[k], path)
127+
generate(w, e.Dir[k], path, prefix)
114128
}
115129

116130
}
117131

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+
118141
// Save to map all imported modules
119142
func GetAllImports(entries []*yang.Entry) {
120143
for _, e := range entries {
121144
allimports[e.Name] = e.Prefix.Name
122145
}
123146
}
124147

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+
125157
//Get Yang version from the yang.Modules
126158
func getYangVersion(modname string, mods map[string]*yang.Module) string {
127159
if (mods[modname].YangVersion != nil) {
@@ -141,3 +173,223 @@ func getImportModules(modname string, mods map[string]*yang.Module) map[string]s
141173
}
142174
return imports
143175
}
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

Comments
 (0)