Skip to content

Commit

Permalink
JSON handling: new method DecomposeJSONWithPaths for decomposing in t…
Browse files Browse the repository at this point in the history
…o Path Values Fixes #1197 (#1199)

* JSON handling: new method for decomposing in to Path Values Fixes #1197

* Added some variety to aux-id values
  • Loading branch information
SeanCondon committed Sep 6, 2020
1 parent fe23a03 commit 8752b1f
Show file tree
Hide file tree
Showing 23 changed files with 802 additions and 873 deletions.
226 changes: 27 additions & 199 deletions pkg/modelregistry/jsonvalues/convertJsonRo_test.go

Large diffs are not rendered by default.

45 changes: 16 additions & 29 deletions pkg/modelregistry/jsonvalues/convertJsonRw_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
td1 "github.com/onosproject/config-models/modelplugin/testdevice-1.0.0/testdevice_1_0_0"
devicechange "github.com/onosproject/onos-config/api/types/change/device"
"github.com/onosproject/onos-config/pkg/modelregistry"
"github.com/onosproject/onos-config/pkg/store"
"github.com/openconfig/goyang/pkg/yang"
"gotest.tools/assert"
"io/ioutil"
Expand All @@ -34,20 +33,17 @@ func Test_correctJsonPathRwValuesSubInterfaces(t *testing.T) {
assert.NilError(t, err)
assert.Equal(t, len(ds1Schema), 137)

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

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

values, err := store.DecomposeTree(sampleTree)
assert.NilError(t, err)
assert.Equal(t, len(values), 11)

correctedPathValues, err := CorrectJSONPaths("/interfaces", values, readWritePaths, true)
correctedPathValues, err := DecomposeJSONWithPaths(sampleTree, readOnlyPaths, readWritePaths)
assert.NilError(t, err)
assert.Equal(t, len(correctedPathValues), 8)

for _, v := range correctedPathValues {
fmt.Printf("%s %v\n", (*v).Path, v.String())
Expand Down Expand Up @@ -86,32 +82,28 @@ func Test_correctJsonPathRwValuesSystemLogging(t *testing.T) {
ds1Schema, err := modelPluginTest.Schema()
assert.NilError(t, err)

_, readWritePaths := modelregistry.ExtractPaths(ds1Schema["Device"], yang.TSUnset, "", "")
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)

values, err := store.DecomposeTree(sampleTree)
correctedPathValues, err := DecomposeJSONWithPaths(sampleTree, readOnlyPaths, readWritePaths)
assert.NilError(t, err)
assert.Equal(t, len(values), 10)

correctedPathValues, err := CorrectJSONPaths("/system/logging", values, readWritePaths, true)
assert.NilError(t, err)

for _, v := range correctedPathValues {
fmt.Printf("%s %v\n", (*v).Path, v.String())
}
assert.Equal(t, len(correctedPathValues), 4)
assert.Equal(t, len(correctedPathValues), 5)

for _, correctedPathValue := range correctedPathValues {
switch correctedPathValue.Path {
case
"/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":
"/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, correctedPathValue.GetValue().GetType(), devicechange.ValueType_STRING, correctedPathValue.Path)
assert.Equal(t, len(correctedPathValue.GetValue().GetTypeOpts()), 0)
default:
Expand All @@ -124,25 +116,20 @@ func Test_correctJsonPathRwValues2(t *testing.T) {

td1Schema, _ := td1.UnzipSchema()

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

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

values, err := store.DecomposeTree(sampleTree)
correctedPathValues, err := DecomposeJSONWithPaths(sampleTree, readOnlyPaths, readWritePaths)
assert.NilError(t, err)
assert.Equal(t, len(values), 2)

correctedPathValues, err := CorrectJSONPaths("/test1:cont1a/cont2a", values, readWritePaths, true)
assert.NilError(t, err)

assert.Equal(t, len(correctedPathValues), 2)
for _, v := range correctedPathValues {
fmt.Printf("%s %v\n", (*v).Path, v.String())
}
assert.Equal(t, len(correctedPathValues), 2)

for _, correctedPathValue := range correctedPathValues {
switch correctedPathValue.Path {
Expand All @@ -153,8 +140,8 @@ func Test_correctJsonPathRwValues2(t *testing.T) {
assert.Equal(t, correctedPathValue.GetValue().ValueToString(), "12")
case
"/cont1a/cont2a/leaf2b":
assert.Equal(t, correctedPathValue.GetValue().GetType(), devicechange.ValueType_FLOAT, correctedPathValue.Path)
assert.Equal(t, len(correctedPathValue.GetValue().GetTypeOpts()), 0)
assert.Equal(t, correctedPathValue.GetValue().GetType(), devicechange.ValueType_DECIMAL, correctedPathValue.Path)
assert.Equal(t, 1, len(correctedPathValue.GetValue().GetTypeOpts()), "expected 1 typeopt on Decimal")
assert.Equal(t, correctedPathValue.GetValue().ValueToString(), "1.210000")
default:
t.Fatal("Unexpected path", correctedPathValue.Path)
Expand Down
12 changes: 3 additions & 9 deletions pkg/modelregistry/jsonvalues/convertJsonTd2Rw_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
td2 "github.com/onosproject/config-models/modelplugin/testdevice-2.0.0/testdevice_2_0_0"
devicechange "github.com/onosproject/onos-config/api/types/change/device"
"github.com/onosproject/onos-config/pkg/modelregistry"
"github.com/onosproject/onos-config/pkg/store"
"github.com/openconfig/gnmi/proto/gnmi"
"github.com/openconfig/goyang/pkg/yang"
"github.com/openconfig/ygot/ygot"
Expand Down Expand Up @@ -71,25 +70,20 @@ func Test_correctJsonPathValuesTd2(t *testing.T) {
assert.NilError(t, err)
assert.Equal(t, len(td2Schema), 8)

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

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

values, err := store.DecomposeTree(sampleTree)
assert.NilError(t, err)
assert.Equal(t, len(values), 2)

correctedPathValues, err := CorrectJSONPaths("", values, readWritePaths, true)
correctedPathValues, err := DecomposeJSONWithPaths(sampleTree, readOnlyPaths, readWritePaths)
assert.NilError(t, err)

assert.Equal(t, len(correctedPathValues), 2)
for _, v := range correctedPathValues {
fmt.Printf("%s %v\n", (*v).Path, v.String())
}
assert.Equal(t, len(correctedPathValues), 2)

for _, correctedPathValue := range correctedPathValues {
switch correctedPathValue.Path {
Expand Down
9 changes: 2 additions & 7 deletions pkg/modelregistry/jsonvalues/convertJsonTdRo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
td1 "github.com/onosproject/config-models/modelplugin/testdevice-1.0.0/testdevice_1_0_0"
devicechange "github.com/onosproject/onos-config/api/types/change/device"
"github.com/onosproject/onos-config/pkg/modelregistry"
"github.com/onosproject/onos-config/pkg/store"
"github.com/openconfig/gnmi/proto/gnmi"
"github.com/openconfig/goyang/pkg/yang"
"github.com/openconfig/ygot/ygot"
Expand Down Expand Up @@ -77,17 +76,13 @@ func Test_correctJsonPathValuesTd(t *testing.T) {
sampleTree, err := ioutil.ReadFile("./testdata/sample-testdevice-opstate.json")
assert.NilError(t, err)

values, err := store.DecomposeTree(sampleTree)
assert.NilError(t, err)
assert.Equal(t, len(values), 6)

correctedPathValues, err := CorrectJSONPaths("", values, readOnlyPaths, true)
correctedPathValues, err := DecomposeJSONWithPaths(sampleTree, readOnlyPaths, nil)
assert.NilError(t, err)
assert.Equal(t, 4, len(correctedPathValues))

for _, v := range correctedPathValues {
fmt.Printf("%s %v\n", (*v).Path, v.String())
}
assert.Equal(t, len(correctedPathValues), 4)

for _, correctedPathValue := range correctedPathValues {
switch correctedPathValue.Path {
Expand Down
Loading

0 comments on commit 8752b1f

Please sign in to comment.