Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement subscribe poll for the devicesim #248

Merged
merged 2 commits into from
May 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 11 additions & 4 deletions tools/test/devicesim/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,24 @@ RUN go install -v \
github.com/google/gnxi/gnoi_target \
github.com/google/gnxi/gnoi_cert


ENV ONOS_CONFIG_ROOT=$GOPATH/src/github.com/onosproject/onos-config/
ENV GNMI_PORT=10161
ENV GNOI_PORT=50001
ENV SIM_MODE=1

COPY ./target_configs target_configs
COPY ./certs certs
COPY ./scripts scripts
COPY ./gnmi_target gnmi_target
RUN mkdir -p $ONOS_CONFIG_ROOT/tools/test/devicesim

COPY target_configs target_configs
COPY certs certs
COPY scripts scripts
COPY gnmi_target gnmi_target
COPY gnmi $GOPATH/src/github.com/onosproject/onos-config/tools/test/devicesim/gnmi


RUN cd $GOPATH/src/github.com/onosproject/onos-config/tools/test/devicesim/gnmi && go install
RUN cd ./gnmi_target && go install
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This gives me a

# _/home/devicesim/gnmi_target
./gnmi_utils.go:115:1: syntax error: non-declaration statement outside function body


RUN chmod +x ./scripts/run_targets.sh

CMD ["./scripts/run_targets.sh"]
7 changes: 4 additions & 3 deletions tools/test/devicesim/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Docker compose manages the running of several docker images at once.

For example to run 3 **SIM_MODE=1** (gNMI only devices) and **localhost** mode, use:
```bash
cd docker_compose
docker-compose -f docker-compose-gnmi.yml up
```

Expand All @@ -51,6 +52,7 @@ If you are fortunate enough to be using Docker on Linux, then you can use the
above method __or__ using the command below to start in **SIM_MODE=1** and **network** mode:

```bash
cd docker_compose
docker-compose -f docker-compose-linux.yml up
```

Expand Down Expand Up @@ -82,7 +84,6 @@ docker run --env "HOSTNAME=localhost" --env "SIM_MODE=3" \
```
To stop it use "docker kill"


## Create the docker image
By default the docker compose command will pull down the latest docker
image from the Docker Hub. If you need to build it locally, run:
Expand All @@ -94,7 +95,7 @@ docker build -t onosproject/devicesim:stable -f Dockerfile .
You can access to the information about client tools for each SIM_MODE
including troubleshooting tips using the following links:

[gNMI Client_User Manual](gnmi_user_manual.md)
[gNMI Client_User Manual](docs/gnmi_user_manual.md)

[gNOI Client_User Manual](gnoi_user_manual.md)
[gNOI Client_User Manual](docs/gnoi_user_manual.md)

Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,26 @@ This gives a response like this.
}
}
```
### Subscribe POLL
```bash
gnmi_cli -address localhost:10161 \
-proto "subscribe:<mode: 2, prefix:<>, subscription:<path: <elem: <name: 'openconfig-system:system'> elem: <name: 'clock' > elem: <name: 'config'> elem: <name: 'timezone-name'>>>>" \
-timeout 5s -alsologtostderr \
-polling_interval 5s \
-client_crt certs/client1.crt -client_key certs/client1.key -ca_crt certs/onfca.crt
```
After running the above command the following output will be printed on the screen every 5 seconds.
```bash
{
"system": {
"clock": {
"config": {
"timezone-name": "Europe/Dublin"
}
}
}
}
```

## Troubleshooting

Expand Down
2 changes: 2 additions & 0 deletions tools/test/devicesim/gnmi/README.md
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.
88 changes: 88 additions & 0 deletions tools/test/devicesim/gnmi/model.go
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
}
17 changes: 17 additions & 0 deletions tools/test/devicesim/gnmi/modeldata/gostruct/gen.go
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"
Loading