Skip to content

Commit 211f17e

Browse files
author
Prabhu Sreenivasan
committed
Merge branch 'transformer-phase1' into transformer_merge
2 parents f032ec5 + 045cbf9 commit 211f17e

39 files changed

+6315
-11
lines changed

Makefile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ cli: rest-server
7575
cvl: go-deps
7676
$(MAKE) -C src/cvl
7777
$(MAKE) -C src/cvl/schema
78+
$(MAKE) -C src/cvl/testdata/schema
79+
7880
cvl-test:
7981
$(MAKE) -C src/cvl gotest
8082

@@ -99,13 +101,20 @@ cd ../; cp $(TOPDIR)/ygot-modified-files/ygot.patch .; \
99101
patch -p1 < ygot.patch; rm -f ygot.patch; \
100102
$(GO) install -v -gcflags "-N -l" $(BUILD_GOPATH)/src/github.com/openconfig/ygot/ygot
101103

104+
102105
install:
103106
$(INSTALL) -D $(REST_BIN) $(DESTDIR)/usr/sbin/rest_server
104107
$(INSTALL) -D $(CERTGEN_BIN) $(DESTDIR)/usr/sbin/generate_cert
105108
$(INSTALL) -d $(DESTDIR)/usr/sbin/schema/
106109
$(INSTALL) -d $(DESTDIR)/usr/sbin/lib/
110+
$(INSTALL) -d $(DESTDIR)/usr/models/yang/
107111
$(INSTALL) -D $(TOPDIR)/src/cvl/schema/*.yin $(DESTDIR)/usr/sbin/schema/
108112
$(INSTALL) -D $(TOPDIR)/src/cvl/testdata/schema/*.yin $(DESTDIR)/usr/sbin/schema/
113+
$(INSTALL) -D $(TOPDIR)/src/cvl/schema/*.yang $(DESTDIR)/usr/models/yang/
114+
$(INSTALL) -D $(TOPDIR)/models/yang/*.yang $(DESTDIR)/usr/models/yang/
115+
$(INSTALL) -D $(TOPDIR)/config/transformer/models_list $(DESTDIR)/usr/models/yang/
116+
$(INSTALL) -D $(TOPDIR)/models/yang/common/*.yang $(DESTDIR)/usr/models/yang/
117+
$(INSTALL) -D $(TOPDIR)/models/yang/annotations/*.yang $(DESTDIR)/usr/models/yang/
109118
cp -rf $(TOPDIR)/build/rest_server/dist/ui/ $(DESTDIR)/rest_ui/
110119
cp -rf $(TOPDIR)/build/cli $(DESTDIR)/usr/sbin/
111120
cp -rf $(TOPDIR)/build/swagger_client_py/ $(DESTDIR)/usr/sbin/lib/

config/transformer/models_list

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#List yang models transformer need to load
2+
3+
openconfig-acl.yang
4+
openconfig-acl-annot.yang

goyang-modified-files/README.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# goyang
2+
YANG parser and compiler for Go programs.
3+
4+
The yang package (pkg/yang) is used to convert a YANG schema into either an
5+
in memory abstract syntax trees (ast) or more fully resolved, in memory, "Entry"
6+
trees. An Entry tree consists only of Entry structures and has had
7+
augmentation, imports, and includes all applied.
8+
9+
goyang is a sample program that uses the yang (pkg/yang) package.
10+
11+
goyang uses the yang package to create an in-memory tree representation of
12+
schemas defined in YANG and then dumps out the contents in several forms.
13+
The forms include:
14+
15+
* tree - a simple tree representation
16+
* types - list understood types extracted from the schema
17+
* annotate - a template file to annotate the yang modules
18+
19+
The yang package, and the goyang program, are not complete and are a work in
20+
progress.
21+
22+
For more complex output types, such as Go structs, and protobuf messages
23+
please use the [openconfig/ygot](https://github.com/openconfig/ygot) package,
24+
which uses this package as its backend.
25+
26+
### Getting started
27+
28+
To build goyang, ensure you have go language tools installed
29+
(available at [golang.org](https://golang.org/dl)) and that the `GOPATH`
30+
environment variable is set to your Go workspace.
31+
32+
1. `go get github.com/openconfig/goyang`
33+
* This will download goyang code and dependencies into the src
34+
subdirectory in your workspace.
35+
36+
2. `cd <workspace>/src/github.com/openconfig/goyang`
37+
38+
3. `go build`
39+
40+
* This will build the goyang binary and place it in the bin
41+
subdirectory in your workspace.
42+
43+
### Contributing to goyang
44+
45+
goyang is still a work-in-progress and we welcome contributions. Please see
46+
the `CONTRIBUTING` file for information about how to contribute to the codebase.
47+
48+
### Disclaimer
49+
50+
This is not an official Google product.

goyang-modified-files/annotate.go

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
// Copyright 2015 Google Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package main
16+
17+
import (
18+
"fmt"
19+
"io"
20+
"strings"
21+
22+
"github.com/openconfig/goyang/pkg/yang"
23+
)
24+
25+
var allimports = make(map[string]string)
26+
var modules = make(map[string]*yang.Module)
27+
28+
func init() {
29+
register(&formatter{
30+
name: "annotate",
31+
f: genAnnotate,
32+
utilf: getFile,
33+
help: "generate template file for yang annotations",
34+
})
35+
}
36+
37+
// Get the modules for which annotation file needs to be generated
38+
func getFile(files []string, mods map[string]*yang.Module) {
39+
for _, name := range files {
40+
slash := strings.Split(name, "/")
41+
if strings.HasSuffix(name, ".yang") {
42+
modname := slash[len(slash)-1]
43+
modname = strings.TrimSuffix(modname, ".yang");
44+
/* Save the yang.Module entries we are interested in */
45+
modules[modname] = mods[modname]
46+
}
47+
}
48+
}
49+
50+
func genAnnotate(w io.Writer, entries []*yang.Entry) {
51+
/* Get all the imported modules in the entries */
52+
GetAllImports(entries)
53+
for _, e := range entries {
54+
if _, ok := modules[e.Name]; ok {
55+
var path string = ""
56+
generate(w, e, path)
57+
// { Add closing brace for each module
58+
fmt.Fprintln(w, "}")
59+
fmt.Fprintln(w)
60+
}
61+
}
62+
}
63+
64+
// 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+
if e.Parent == nil {
67+
if e.Name != "" {
68+
fmt.Fprintf(w, "module %s-annot {\n", e.Name) //}
69+
fmt.Fprintln(w)
70+
fmt.Fprintf(w, " yang-version \"%s\"\n", getYangVersion(e.Name, modules))
71+
fmt.Fprintln(w)
72+
fmt.Fprintf(w, " namespace \"http://openconfig.net/yang/annotation\";\n")
73+
if e.Prefix != nil {
74+
fmt.Fprintf(w, " prefix \"%s-annot\" \n", e.Prefix.Name)
75+
}
76+
fmt.Fprintln(w)
77+
78+
var imports = make(map[string]string)
79+
imports = getImportModules(e.Name, modules)
80+
for k := range imports {
81+
if e.Name != k {
82+
fmt.Fprintf(w, " import %s { prefix %s }\n", k, allimports[k])
83+
}
84+
}
85+
86+
fmt.Fprintln(w)
87+
}
88+
}
89+
90+
name := e.Name
91+
if e.Prefix != nil {
92+
name = e.Prefix.Name + ":" + name
93+
}
94+
95+
delim := ""
96+
if path != "" {
97+
delim = "/"
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)
106+
107+
var names []string
108+
for k := range e.Dir {
109+
names = append(names, k)
110+
}
111+
112+
for _, k := range names {
113+
generate(w, e.Dir[k], path)
114+
}
115+
116+
}
117+
118+
// Save to map all imported modules
119+
func GetAllImports(entries []*yang.Entry) {
120+
for _, e := range entries {
121+
allimports[e.Name] = e.Prefix.Name
122+
}
123+
}
124+
125+
//Get Yang version from the yang.Modules
126+
func getYangVersion(modname string, mods map[string]*yang.Module) string {
127+
if (mods[modname].YangVersion != nil) {
128+
return mods[modname].YangVersion.Name
129+
}
130+
return ""
131+
132+
}
133+
134+
// Get imported modules for a given module from yang.Module
135+
func getImportModules(modname string, mods map[string]*yang.Module) map[string]string {
136+
imports := map[string]string{}
137+
if (mods[modname].Import != nil) {
138+
for _, imp := range mods[modname].Import {
139+
imports[imp.Name] = imp.Prefix.Name
140+
}
141+
}
142+
return imports
143+
}

0 commit comments

Comments
 (0)