-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
10,924 additions
and
38 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# gNMI Server | ||
Package gnmi implements a gnmi server to mock a device with YANG models. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// Copyright 2019-present Open Networking Foundation. | ||
// | ||
// 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 | ||
// | ||
// http://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/experimental/ygotutils" | ||
"github.com/openconfig/ygot/ygot" | ||
"github.com/openconfig/ygot/ytypes" | ||
|
||
pb "github.com/openconfig/gnmi/proto/gnmi" | ||
cpb "google.golang.org/genproto/googleapis/rpc/code" | ||
) | ||
|
||
// JSONUnmarshaler is the signature of the Unmarshal() function in the GoStruct code generated by openconfig ygot library. | ||
type JSONUnmarshaler func([]byte, ygot.GoStruct, ...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, | ||
} | ||
} | ||
|
||
// 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) { | ||
rootNode, stat := ygotutils.NewNode(m.structRootType, &pb.Path{}) | ||
if stat.GetCode() != int32(cpb.Code_OK) { | ||
return nil, fmt.Errorf("cannot create root node: %v", stat) | ||
} | ||
|
||
rootStruct, ok := rootNode.(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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Copyright 2019-present Open Networking Foundation. | ||
// | ||
// 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 | ||
// | ||
// http://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 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/YangModels/yang; 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" |
Oops, something went wrong.