Skip to content

Commit

Permalink
Merge pull request #173 from project-arlo/transformer-phase2
Browse files Browse the repository at this point in the history
Transformer phase2
  • Loading branch information
PrabhuSreenivasan authored Oct 13, 2019
2 parents 211f17e + eea6d40 commit ca4b330
Show file tree
Hide file tree
Showing 29 changed files with 2,460 additions and 591 deletions.
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ codegen:

yamlGen:
$(MAKE) -C models/yang
$(MAKE) -C models/yang/sonic

go-patch: go-deps
cd $(BUILD_GOPATH)/src/github.com/openconfig/ygot/; git reset --hard HEAD; git checkout 724a6b18a9224343ef04fe49199dfb6020ce132a 2>/dev/null ; true; \
Expand All @@ -108,9 +109,10 @@ install:
$(INSTALL) -d $(DESTDIR)/usr/sbin/schema/
$(INSTALL) -d $(DESTDIR)/usr/sbin/lib/
$(INSTALL) -d $(DESTDIR)/usr/models/yang/
$(INSTALL) -D $(TOPDIR)/models/yang/sonic/*.yang $(DESTDIR)/usr/models/yang/
$(INSTALL) -D $(TOPDIR)/models/yang/sonic/common/*.yang $(DESTDIR)/usr/models/yang/
$(INSTALL) -D $(TOPDIR)/src/cvl/schema/*.yin $(DESTDIR)/usr/sbin/schema/
$(INSTALL) -D $(TOPDIR)/src/cvl/testdata/schema/*.yin $(DESTDIR)/usr/sbin/schema/
$(INSTALL) -D $(TOPDIR)/src/cvl/schema/*.yang $(DESTDIR)/usr/models/yang/
$(INSTALL) -D $(TOPDIR)/models/yang/*.yang $(DESTDIR)/usr/models/yang/
$(INSTALL) -D $(TOPDIR)/config/transformer/models_list $(DESTDIR)/usr/models/yang/
$(INSTALL) -D $(TOPDIR)/models/yang/common/*.yang $(DESTDIR)/usr/models/yang/
Expand Down
294 changes: 273 additions & 21 deletions goyang-modified-files/annotate.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (

var allimports = make(map[string]string)
var modules = make(map[string]*yang.Module)
var allmodules = make(map[string]*yang.Module)

func init() {
register(&formatter{
Expand All @@ -36,14 +37,13 @@ func init() {

// Get the modules for which annotation file needs to be generated
func getFile(files []string, mods map[string]*yang.Module) {
allmodules = mods
for _, name := range files {
slash := strings.Split(name, "/")
if strings.HasSuffix(name, ".yang") {
modname := slash[len(slash)-1]
modname = strings.TrimSuffix(modname, ".yang");
/* Save the yang.Module entries we are interested in */
modules[modname] = mods[modname]
}
}
}

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

// generate writes to stdoutput a template annotation file entry for the selected modules.
func generate(w io.Writer, e *yang.Entry, path string) {
func generate(w io.Writer, e *yang.Entry, path string, prefix string) {
if e.Parent == nil {
if e.Name != "" {
fmt.Fprintf(w, "module %s-annot {\n", e.Name) //}
fmt.Fprintln(w)
fmt.Fprintf(w, " yang-version \"%s\"\n", getYangVersion(e.Name, modules))
fmt.Fprintf(w, " yang-version \"%s\";\n", getYangVersion(e.Name, modules))
fmt.Fprintln(w)
fmt.Fprintf(w, " namespace \"http://openconfig.net/yang/annotation\";\n")
fmt.Fprintf(w, " namespace \"http://openconfig.net/yang/annotation/%s-annot\";\n", e.Prefix.Name)
if e.Prefix != nil {
fmt.Fprintf(w, " prefix \"%s-annot\" \n", e.Prefix.Name)
fmt.Fprintf(w, " prefix \"%s-annot\";\n", e.Prefix.Name)
}
fmt.Fprintln(w)

var imports = make(map[string]string)
imports = getImportModules(e.Name, modules)
for k := range imports {
if e.Name != k {
fmt.Fprintf(w, " import %s { prefix %s }\n", k, allimports[k])
fmt.Fprintf(w, " import %s { prefix %s; }\n", k, allimports[k])
}
}
// Include the module for which annotation is being generated
fmt.Fprintf(w, " import %s { prefix %s; }\n", e.Name, e.Prefix.Name)

fmt.Fprintln(w)
}
}

name := e.Name
if e.Prefix != nil {
name = e.Prefix.Name + ":" + name
if prefix == "" && e.Prefix != nil {
prefix = e.Prefix.Name
}
name = prefix + ":" + name

delim := ""
if path != "" {
delim = "/"
if (e.Node.Kind() != "module") {
path = path + "/" + name
printDeviation(w, path)
}
path = path + delim + name

fmt.Fprintf(w, " deviation %s {\n", path)
fmt.Fprintf(w, " deviate add {\n")
fmt.Fprintf(w, " }\n")
fmt.Fprintf(w, " }\n")
fmt.Fprintln(w)

var names []string
for k := range e.Dir {
names = append(names, k)
}

if (e.Node.Kind() == "module") {
if len(e.Node.(*yang.Module).Augment) > 0 {
for _,a := range e.Node.(*yang.Module).Augment {
pathList := strings.Split(a.Name, "/")
pathList = pathList[1:]
for i, pvar := range pathList {
if len(pvar) > 0 && !strings.Contains(pvar, ":") {
pvar = e.Prefix.Name + ":" + pvar
pathList[i] = pvar
}
}
path = "/" + strings.Join(pathList, "/")
handleAugments(w, a, e.Node.(*yang.Module).Grouping, e.Prefix.Name, path)
}
}
}

for _, k := range names {
generate(w, e.Dir[k], path)
generate(w, e.Dir[k], path, prefix)
}

}

func printDeviation(w io.Writer, path string){
fmt.Fprintf(w, " deviation %s {\n", path)
fmt.Fprintf(w, " deviate add {\n")
fmt.Fprintf(w, " }\n")
fmt.Fprintf(w, " }\n")
fmt.Fprintln(w)
}


// Save to map all imported modules
func GetAllImports(entries []*yang.Entry) {
for _, e := range entries {
allimports[e.Name] = e.Prefix.Name
}
}

func GetModuleFromPrefix(prefix string) string {
for m, p := range allimports {
if prefix == p {
return m
}
}
return ""
}

//Get Yang version from the yang.Modules
func getYangVersion(modname string, mods map[string]*yang.Module) string {
if (mods[modname].YangVersion != nil) {
Expand All @@ -141,3 +173,223 @@ func getImportModules(modname string, mods map[string]*yang.Module) map[string]s
}
return imports
}

func handleAugments(w io.Writer, a *yang.Augment, grp []*yang.Grouping, prefix string, path string) {
for _, u := range a.Uses {
grpN := u.Name
for _, g := range grp {
if grpN == g.Name {
if len(g.Container) > 0 {
handleContainer(w, g.Container, grp, prefix, path)
}
if len(g.List) > 0 {
handleList(w, g.List, grp, prefix, path)
}
if len(g.LeafList) > 0 {
handleLeafList(w, g.LeafList, prefix, path)
}
if len(g.Leaf) > 0 {
handleLeaf(w, g.Leaf, prefix, path)
}
if len(g.Choice) > 0 {
handleChoice(w, g.Choice, grp, prefix, path)
}
if len(g.Uses) > 0 {
handleUses(w, g.Uses, grp, prefix, path)
}
}
}
}

}

func handleUses(w io.Writer, u []*yang.Uses, grp []*yang.Grouping, prefix string, path string) {
for _, u := range u {
grpN := u.Name
if strings.Contains(grpN, ":") {
tokens := strings.Split(grpN, ":")
nprefix := tokens[0]
grpN = tokens[1]
mod := GetModuleFromPrefix(nprefix)
grp = allmodules[mod].Grouping
}
for _, g := range grp {
if grpN == g.Name {
if len(g.Container) > 0 {
handleContainer(w, g.Container, grp, prefix, path)
}
if len(g.List) > 0 {
handleList(w, g.List, grp, prefix, path)
}
if len(g.LeafList) > 0 {
handleLeafList(w, g.LeafList, prefix, path)
}
if len(g.Leaf) > 0 {
handleLeaf(w, g.Leaf, prefix, path)
}
if len(g.Choice) > 0 {
handleChoice(w, g.Choice, grp, prefix, path)
}
if len(g.Uses) > 0 {
handleUses(w, g.Uses, grp, prefix, path)
}

}
}
}

}

func handleContainer(w io.Writer, ctr []*yang.Container, grp []*yang.Grouping, prefix string, path string) {
for _, c := range ctr {
npath := path + "/" + prefix + ":" + c.Name
printDeviation(w, npath)
if len(c.Container) > 0 {
handleContainer(w, c.Container, grp, prefix, npath)
}
if len(c.List) > 0 {
handleList(w, c.List, grp, prefix, npath)
}
if len(c.LeafList) > 0 {
handleLeafList(w, c.LeafList, prefix, npath)
}
if len(c.Leaf) > 0 {
handleLeaf(w, c.Leaf, prefix, npath)
}
if len(c.Choice) > 0 {
handleChoice(w, c.Choice, grp, prefix, npath)
}
if len(c.Grouping) > 0 {
handleGrouping(w, c.Grouping, grp, prefix, npath)
}
if len(c.Uses) > 0 {
handleUses(w, c.Uses, grp, prefix, npath)
}
}
}

func handleList(w io.Writer, lst []*yang.List, grp []*yang.Grouping, prefix string, path string) {
for _, l := range lst {
npath := path + "/" + prefix + ":" + l.Name
printDeviation(w, npath)
if len(l.Container) > 0 {
handleContainer(w, l.Container, grp, prefix, npath)
}
if len(l.List) > 0 {
handleList(w, l.List, grp, prefix, npath)
}
if len(l.LeafList) > 0 {
handleLeafList(w, l.LeafList, prefix, npath)
}
if len(l.Leaf) > 0 {
handleLeaf(w, l.Leaf, prefix, npath)
}
if len(l.Choice) > 0 {
handleChoice(w, l.Choice, grp, prefix, npath)
}
if len(l.Grouping) > 0 {
handleGrouping(w, l.Grouping, grp, prefix, npath)
}
if len(l.Uses) > 0 {
handleUses(w, l.Uses, grp, prefix, npath)
}

}
}

func handleGrouping(w io.Writer, grp []*yang.Grouping, grptop []*yang.Grouping, prefix string, path string) {
for _, g := range grp {
npath := path + "/" + prefix + ":" + g.Name
printDeviation(w, npath)
if len(g.Container) > 0 {
handleContainer(w, g.Container, grptop, prefix, npath)
}
if len(g.List) > 0 {
handleList(w, g.List, grptop, prefix, npath)
}
if len(g.LeafList) > 0 {
handleLeafList(w, g.LeafList, prefix, npath)
}
if len(g.Leaf) > 0 {
handleLeaf(w, g.Leaf, prefix, npath)
}
if len(g.Choice) > 0 {
handleChoice(w, g.Choice, grptop, prefix, npath)
}
if len(g.Grouping) > 0 {
handleGrouping(w, g.Grouping, grptop, prefix, npath)
}
if len(g.Uses) > 0 {
handleUses(w, g.Uses, grptop, prefix, npath)
}

}
}

func handleLeaf (w io.Writer, lf []*yang.Leaf, prefix string, path string) {
if len(lf) > 0 {
for _, l := range lf {
npath := path + "/" + prefix + ":" + l.Name
printDeviation(w, npath)
}
}

}

func handleLeafList (w io.Writer, llst []*yang.LeafList, prefix string, path string) {
if len(llst) > 0 {
for _, l := range llst {
npath := path + "/" + prefix + ":" + l.Name
printDeviation(w, npath)
}
}
}

func handleChoice (w io.Writer, ch []*yang.Choice, grp []*yang.Grouping, prefix string, path string) {
for _, c := range ch {
npath := path + "/" + prefix + ":" + c.Name
printDeviation(w, npath)
if len(c.Container) > 0 {
handleContainer(w, c.Container, grp, prefix, npath)
}
if len(c.List) > 0 {
handleList(w, c.List, grp, prefix, npath)
}
if len(c.LeafList) > 0 {
handleLeafList(w, c.LeafList, prefix, npath)
}
if len(c.Leaf) > 0 {
handleLeaf(w, c.Leaf, prefix, npath)
}
if len(c.Case) > 0 {
handleCase(w, c.Case, grp, prefix, npath)
}
}
}

func handleCase (w io.Writer, ch []*yang.Case, grp []*yang.Grouping, prefix string, path string) {
for _, c := range ch {
npath := path + "/" + prefix + ":" + c.Name
printDeviation(w, npath)
if len(c.Container) > 0 {
handleContainer(w, c.Container, grp, prefix, npath)
}
if len(c.List) > 0 {
handleList(w, c.List, grp, prefix, npath)
}
if len(c.LeafList) > 0 {
handleLeafList(w, c.LeafList, prefix, npath)
}
if len(c.Leaf) > 0 {
handleLeaf(w, c.Leaf, prefix, npath)
}
if len(c.Choice) > 0 {
handleChoice(w, c.Choice, grp, prefix, npath)
}
if len(c.Uses) > 0 {
handleUses(w, c.Uses, grp, prefix, npath)
}

}
}

Loading

0 comments on commit ca4b330

Please sign in to comment.