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

JSON Handling: decode LeafLists given in JSON values #1200

Merged
merged 4 commits into from
Sep 8, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -15,84 +15,33 @@
package jsonvalues

import (
"fmt"
ds1 "github.com/onosproject/config-models/modelplugin/devicesim-1.0.0/devicesim_1_0_0"
devicechange "github.com/onosproject/onos-config/api/types/change/device"
"github.com/onosproject/onos-config/pkg/modelregistry"
"github.com/openconfig/gnmi/proto/gnmi"
"github.com/openconfig/goyang/pkg/yang"
"github.com/openconfig/ygot/ygot"
"gotest.tools/assert"
"io/ioutil"
"testing"
)

type modelPluginTest string

const modelTypeTest = "TestModel"
const modelVersionTest = "0.0.1"
const moduleNameTest = "testmodel.so.1.0.0"

var modelData = []*gnmi.ModelData{
{Name: "testmodel", Organization: "Open Networking Lab", Version: "2019-07-10"},
}

func (m modelPluginTest) ModelData() (string, string, []*gnmi.ModelData, string) {
return modelTypeTest, modelVersionTest, modelData, moduleNameTest
}

// UnmarshalConfigValues uses the `generated.go` of the DeviceSim-1.0.0 plugin module
func (m modelPluginTest) UnmarshalConfigValues(jsonTree []byte) (*ygot.ValidatedGoStruct, error) {
device := &ds1.Device{}
vgs := ygot.ValidatedGoStruct(device)

if err := ds1.Unmarshal(jsonTree, device); err != nil {
return nil, err
}

return &vgs, nil
}

// Validate uses the `generated.go` of the DeviceSim-1.0.0 plugin module
func (m modelPluginTest) Validate(ygotModel *ygot.ValidatedGoStruct, opts ...ygot.ValidationOption) error {
deviceDeref := *ygotModel
device, ok := deviceDeref.(*ds1.Device)
if !ok {
return fmt.Errorf("unable to convert model in to testdevice_1_0_0")
}
return device.Validate()
}

// Schema uses the `generated.go` of the DeviceSim-1.0.0 plugin module
func (m modelPluginTest) Schema() (map[string]*yang.Entry, error) {
return ds1.UnzipSchema()
}

func Test_correctJsonPathValues2(t *testing.T) {

var modelPluginTest modelPluginTest

ds1Schema, err := modelPluginTest.Schema()
ds1Schema, err := ds1.UnzipSchema()
assert.NilError(t, err)
assert.Equal(t, len(ds1Schema), 137)

readOnlyPaths, readWritePaths := modelregistry.ExtractPaths(ds1Schema["Device"], yang.TSUnset, "", "")
assert.Equal(t, len(readOnlyPaths), 37)

// All values are taken from testdata/sample-openconfig.json and defined
// here in the intermediate jsonToValues format
sampleTree, err := ioutil.ReadFile("./testdata/sample-openconfig2.json")
assert.NilError(t, err)

correctedPathValues, err := DecomposeJSONWithPaths(sampleTree, readOnlyPaths, readWritePaths)
pathValues, err := DecomposeJSONWithPaths(sampleTree, readOnlyPaths, readWritePaths)
assert.NilError(t, err)
assert.Equal(t, len(correctedPathValues), 24)
for _, v := range correctedPathValues {
fmt.Printf("%s %v\n", (*v).Path, v.String())
}
assert.Equal(t, len(pathValues), 24)

for _, correctedPathValue := range correctedPathValues {
switch correctedPathValue.Path {
for _, pathValue := range pathValues {
t.Logf("%v", pathValue)
switch pathValue.Path {
case
"/system/openflow/controllers/controller[name=main]/connections/connection[aux-id=10]/state/source-interface",
"/system/openflow/controllers/controller[name=main]/connections/connection[aux-id=10]/state/transport",
Expand All @@ -114,17 +63,90 @@ func Test_correctJsonPathValues2(t *testing.T) {
"/system/openflow/controllers/controller[name=second]/connections/connection[aux-id=11]/state/address",
"/system/openflow/controllers/controller[name=second]/connections/connection[aux-id=11]/state/aux-id",
"/system/openflow/controllers/controller[name=second]/connections/connection[aux-id=11]/state/port":
assert.Equal(t, correctedPathValue.GetValue().GetType(), devicechange.ValueType_STRING, correctedPathValue.Path)
assert.Equal(t, len(correctedPathValue.GetValue().GetTypeOpts()), 0)
assert.Equal(t, pathValue.GetValue().GetType(), devicechange.ValueType_STRING, pathValue.Path)
assert.Equal(t, len(pathValue.GetValue().GetTypeOpts()), 0)
case
"/system/openflow/controllers/controller[name=main]/connections/connection[aux-id=10]/state/priority",
"/system/openflow/controllers/controller[name=main]/connections/connection[aux-id=11]/state/priority",
"/system/openflow/controllers/controller[name=second]/connections/connection[aux-id=10]/state/priority",
"/system/openflow/controllers/controller[name=second]/connections/connection[aux-id=11]/state/priority":
assert.Equal(t, correctedPathValue.GetValue().GetType(), devicechange.ValueType_UINT, correctedPathValue.Path)
assert.Equal(t, len(correctedPathValue.GetValue().GetTypeOpts()), 0)
assert.Equal(t, pathValue.GetValue().GetType(), devicechange.ValueType_UINT, pathValue.Path)
assert.Equal(t, len(pathValue.GetValue().GetTypeOpts()), 0)
default:
t.Fatal("Unexpected path", pathValue.Path)
}
}
}

func Test_correctJsonPathRwValuesSubInterfaces(t *testing.T) {

ds1Schema, err := ds1.UnzipSchema()
assert.NilError(t, err)
readOnlyPaths, readWritePaths := modelregistry.ExtractPaths(ds1Schema["Device"], yang.TSUnset, "", "")

sampleTree, err := ioutil.ReadFile("./testdata/sample-openconfig-configuration.json")
assert.NilError(t, err)

pathValues, err := DecomposeJSONWithPaths(sampleTree, readOnlyPaths, readWritePaths)
assert.NilError(t, err)
assert.Equal(t, len(pathValues), 8)

for _, pathValue := range pathValues {
t.Logf("%v", pathValue)
switch pathValue.Path {
case
"/interfaces/interface[name=eth1]/config/description",
"/interfaces/interface[name=eth1]/subinterfaces/subinterface[index=120]/config/description",
"/interfaces/interface[name=eth1]/subinterfaces/subinterface[index=121]/config/description":
assert.Equal(t, pathValue.GetValue().GetType(), devicechange.ValueType_STRING, pathValue.Path)
assert.Equal(t, len(pathValue.GetValue().GetTypeOpts()), 0)
case
"/interfaces/interface[name=eth1]/subinterfaces/subinterface[index=120]/config/enabled",
"/interfaces/interface[name=eth1]/subinterfaces/subinterface[index=121]/config/enabled":
assert.Equal(t, pathValue.GetValue().GetType(), devicechange.ValueType_BOOL, pathValue.Path)
assert.Equal(t, len(pathValue.GetValue().GetTypeOpts()), 0)
case
"/interfaces/interface[name=eth1]/config/mtu",
"/interfaces/interface[name=eth1]/hold-time/config/down",
"/interfaces/interface[name=eth1]/hold-time/config/up":
assert.Equal(t, pathValue.GetValue().GetType(), devicechange.ValueType_UINT, pathValue.Path)
assert.Equal(t, len(pathValue.GetValue().GetTypeOpts()), 0)
default:
t.Fatal("Unexpected path", pathValue.Path)
}
}
}

func Test_correctJsonPathRwValuesSystemLogging(t *testing.T) {

ds1Schema, err := ds1.UnzipSchema()
assert.NilError(t, err)

readOnlyPaths, readWritePaths := modelregistry.ExtractPaths(ds1Schema["Device"], yang.TSUnset, "", "")

// All values are taken from testdata/sample-double-index.json and defined
// here in the intermediate jsonToValues format
sampleTree, err := ioutil.ReadFile("./testdata/sample-openconfig-double-index.json")
assert.NilError(t, err)
assert.Equal(t, 843, len(sampleTree))

pathValues, err := DecomposeJSONWithPaths(sampleTree, readOnlyPaths, readWritePaths)
assert.NilError(t, err)
assert.Equal(t, len(pathValues), 5)

for _, pathValue := range pathValues {
//t.Logf("%v", pathValue)
switch pathValue.Path {
case
"/system/logging/remote-servers/remote-server[host=h1]/config/source-address",
"/system/logging/remote-servers/remote-server[host=h1]/selectors/selector[facility=1][severity=2]/config/severity",
"/system/logging/remote-servers/remote-server[host=h1]/selectors/selector[facility=1][severity=2]/config/facility",
"/system/logging/console/selectors/selector[facility=3][severity=4]/config/facility",
"/system/logging/console/selectors/selector[facility=3][severity=4]/config/severity":
assert.Equal(t, pathValue.GetValue().GetType(), devicechange.ValueType_STRING, pathValue.Path)
assert.Equal(t, len(pathValue.GetValue().GetTypeOpts()), 0)
default:
t.Fatal("Unexpected path", correctedPathValue.Path)
t.Fatal("Unexpected path", pathValue.Path)
}
}
}
150 changes: 0 additions & 150 deletions pkg/modelregistry/jsonvalues/convertJsonRw_test.go

This file was deleted.

Loading