Skip to content
Snippets Groups Projects
Commit 4ea513ab authored by André Sterba's avatar André Sterba
Browse files

Remove submodule

parent ee0d2a6f
No related branches found
No related tags found
2 merge requests!303Migrate yang models in monorepo,!302Get device model when adding a device
Pipeline #100735 passed
google @ cd6b043b
Subproject commit cd6b043b7fd0b36a25a27289c8d719ee19833329
/* Copyright 2017 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package gnmi
import (
"errors"
"fmt"
"reflect"
"sort"
"github.com/openconfig/goyang/pkg/yang"
"github.com/openconfig/ygot/ygot"
"github.com/openconfig/ygot/ytypes"
pb "github.com/openconfig/gnmi/proto/gnmi"
)
// JSONUnmarshaler is the signature of the Unmarshal() function in the GoStruct code generated by openconfig ygot library.
type JSONUnmarshaler func([]byte, ygot.ValidatedGoStruct, ...ytypes.UnmarshalOpt) error
// GoStructEnumData is the data type to maintain GoStruct enum type.
type GoStructEnumData map[string]map[int64]ygot.EnumDefinition
// Model contains the model data and GoStruct information for the device to config.
type Model struct {
modelData []*pb.ModelData
structRootType reflect.Type
schemaTreeRoot *yang.Entry
jsonUnmarshaler JSONUnmarshaler
enumData GoStructEnumData
}
// NewModel returns an instance of Model struct.
func NewModel(m []*pb.ModelData, t reflect.Type, r *yang.Entry, f JSONUnmarshaler, e GoStructEnumData) *Model {
return &Model{
modelData: m,
structRootType: t,
schemaTreeRoot: r,
jsonUnmarshaler: f,
enumData: e,
}
}
func (m *Model) newRootValue() interface{} {
return reflect.New(m.structRootType.Elem()).Interface()
}
// NewConfigStruct creates a ValidatedGoStruct of this model from jsonConfig. If jsonConfig is nil, creates an empty GoStruct.
func (m *Model) NewConfigStruct(jsonConfig []byte) (ygot.ValidatedGoStruct, error) {
rootStruct, ok := m.newRootValue().(ygot.ValidatedGoStruct)
if !ok {
return nil, errors.New("root node is not a ygot.ValidatedGoStruct")
}
if jsonConfig != nil {
if err := m.jsonUnmarshaler(jsonConfig, rootStruct); err != nil {
return nil, err
}
if err := rootStruct.ΛValidate(); err != nil {
return nil, err
}
}
return rootStruct, nil
}
// SupportedModels returns a list of supported models.
func (m *Model) SupportedModels() []string {
mDesc := make([]string, len(m.modelData))
for i, m := range m.modelData {
mDesc[i] = fmt.Sprintf("%s %s", m.Name, m.Version)
}
sort.Strings(mDesc)
return mDesc
}
package gostruct
//go:generate sh -c "go get -u github.com/openconfig/ygot; (cd $GOPATH/src/github.com/openconfig/ygot && go get -t -d ./...); go get -u github.com/openconfig/public; go get -u github.com/google/go-cmp/cmp; go get -u github.com/openconfig/gnmi/ctree; go get -u github.com/openconfig/gnmi/proto/gnmi; go get -u github.com/openconfig/gnmi/value; go get -u github.com/YangModels/yang; go get -u github.com/golang/glog; go get -u github.com/golang/protobuf/proto; go get -u github.com/kylelemons/godebug/pretty; go get -u github.com/openconfig/goyang/pkg/yang; go get -u google.golang.org/grpc; cd $GOPATH/src && go run github.com/openconfig/ygot/generator/generator.go -generate_fakeroot -output_file github.com/google/gnxi/gnmi/modeldata/gostruct/generated.go -package_name gostruct -exclude_modules ietf-interfaces -path github.com/openconfig/public,github.com/YangModels/yang github.com/openconfig/public/release/models/interfaces/openconfig-interfaces.yang github.com/openconfig/public/release/models/openflow/openconfig-openflow.yang github.com/openconfig/public/release/models/platform/openconfig-platform.yang github.com/openconfig/public/release/models/system/openconfig-system.yang"
This diff is collapsed.
/* Copyright 2017 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Package modeldata contains the following model data in gnmi proto struct:
// openconfig-interfaces 2.0.0,
// openconfig-openflow 0.1.0,
// openconfig-platform 0.5.0,
// openconfig-system 0.2.0.
package modeldata
import (
pb "github.com/openconfig/gnmi/proto/gnmi"
)
const (
// OpenconfigInterfacesModel is the openconfig YANG model for interfaces.
OpenconfigInterfacesModel = "openconfig-interfaces"
// OpenconfigOpenflowModel is the openconfig YANG model for openflow.
OpenconfigOpenflowModel = "openconfig-openflow"
// OpenconfigPlatformModel is the openconfig YANG model for platform.
OpenconfigPlatformModel = "openconfig-platform"
// OpenconfigSystemModel is the openconfig YANG model for system.
OpenconfigSystemModel = "openconfig-system"
)
var (
// ModelData is a list of supported models.
ModelData = []*pb.ModelData{{
Name: OpenconfigInterfacesModel,
Organization: "OpenConfig working group",
Version: "2.0.0",
}, {
Name: OpenconfigOpenflowModel,
Organization: "OpenConfig working group",
Version: "0.1.0",
}, {
Name: OpenconfigPlatformModel,
Organization: "OpenConfig working group",
Version: "0.5.0",
}, {
Name: OpenconfigSystemModel,
Organization: "OpenConfig working group",
Version: "0.2.0",
}}
)
This diff is collapsed.
This diff is collapsed.
package gnmi
import (
"fmt"
"strconv"
log "github.com/golang/glog"
"github.com/openconfig/goyang/pkg/yang"
pb "github.com/openconfig/gnmi/proto/gnmi"
)
// getChildNode gets a node's child with corresponding schema specified by path
// element. If not found and createIfNotExist is set as true, an empty node is
// created and returned.
func getChildNode(node map[string]interface{}, schema *yang.Entry, elem *pb.PathElem, createIfNotExist bool) (interface{}, *yang.Entry) {
var nextSchema *yang.Entry
var ok bool
if nextSchema, ok = schema.Dir[elem.Name]; !ok {
return nil, nil
}
var nextNode interface{}
if elem.GetKey() == nil {
if nextNode, ok = node[elem.Name]; !ok {
if createIfNotExist {
node[elem.Name] = make(map[string]interface{})
nextNode = node[elem.Name]
}
}
return nextNode, nextSchema
}
nextNode = getKeyedListEntry(node, elem, createIfNotExist)
return nextNode, nextSchema
}
// getKeyedListEntry finds the keyed list entry in node by the name and key of
// path elem. If entry is not found and createIfNotExist is true, an empty entry
// will be created (the list will be created if necessary).
func getKeyedListEntry(node map[string]interface{}, elem *pb.PathElem, createIfNotExist bool) map[string]interface{} {
curNode, ok := node[elem.Name]
if !ok {
if !createIfNotExist {
return nil
}
// Create a keyed list as node child and initialize an entry.
m := make(map[string]interface{})
for k, v := range elem.Key {
m[k] = v
if vAsNum, err := strconv.ParseFloat(v, 64); err == nil {
m[k] = vAsNum
}
}
node[elem.Name] = []interface{}{m}
return m
}
// Search entry in keyed list.
keyedList, ok := curNode.([]interface{})
if !ok {
return nil
}
for _, n := range keyedList {
m, ok := n.(map[string]interface{})
if !ok {
log.Errorf("wrong keyed list entry type: %T", n)
return nil
}
keyMatching := true
// must be exactly match
for k, v := range elem.Key {
attrVal, ok := m[k]
if !ok {
return nil
}
if v != fmt.Sprintf("%v", attrVal) {
keyMatching = false
break
}
}
if keyMatching {
return m
}
}
if !createIfNotExist {
return nil
}
// Create an entry in keyed list.
m := make(map[string]interface{})
for k, v := range elem.Key {
m[k] = v
if vAsNum, err := strconv.ParseFloat(v, 64); err == nil {
m[k] = vAsNum
}
}
node[elem.Name] = append(keyedList, m)
return m
}
sdasd
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment