diff --git a/pkg/modelregistry/jsonvalues/convertJsonRo_test.go b/pkg/modelregistry/jsonvalues/convertJsonDs1_test.go similarity index 51% rename from pkg/modelregistry/jsonvalues/convertJsonRo_test.go rename to pkg/modelregistry/jsonvalues/convertJsonDs1_test.go index c0331eead..48cc585aa 100644 --- a/pkg/modelregistry/jsonvalues/convertJsonRo_test.go +++ b/pkg/modelregistry/jsonvalues/convertJsonDs1_test.go @@ -15,84 +15,34 @@ 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" + "time" ) -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", @@ -114,17 +64,92 @@ 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, "", "") + + // 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) + + 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) + time.Sleep(10 * time.Millisecond) + + 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) } } } diff --git a/pkg/modelregistry/jsonvalues/convertJsonRw_test.go b/pkg/modelregistry/jsonvalues/convertJsonRw_test.go deleted file mode 100644 index 221576ff0..000000000 --- a/pkg/modelregistry/jsonvalues/convertJsonRw_test.go +++ /dev/null @@ -1,150 +0,0 @@ -// 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 jsonvalues - -import ( - "fmt" - 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/openconfig/goyang/pkg/yang" - "gotest.tools/assert" - "io/ioutil" - "testing" -) - -func Test_correctJsonPathRwValuesSubInterfaces(t *testing.T) { - - var modelPluginTest modelPluginTest - - ds1Schema, err := modelPluginTest.Schema() - assert.NilError(t, err) - assert.Equal(t, len(ds1Schema), 137) - - 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) - - 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()) - } - assert.Equal(t, len(correctedPathValues), 8) - - for _, correctedPathValue := range correctedPathValues { - switch correctedPathValue.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, correctedPathValue.GetValue().GetType(), devicechange.ValueType_STRING, correctedPathValue.Path) - assert.Equal(t, len(correctedPathValue.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, correctedPathValue.GetValue().GetType(), devicechange.ValueType_BOOL, correctedPathValue.Path) - assert.Equal(t, len(correctedPathValue.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, correctedPathValue.GetValue().GetType(), devicechange.ValueType_UINT, correctedPathValue.Path) - assert.Equal(t, len(correctedPathValue.GetValue().GetTypeOpts()), 0) - default: - t.Fatal("Unexpected path", correctedPathValue.Path) - } - } -} - -func Test_correctJsonPathRwValuesSystemLogging(t *testing.T) { - - var modelPluginTest modelPluginTest - - ds1Schema, err := modelPluginTest.Schema() - 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) - - correctedPathValues, err := DecomposeJSONWithPaths(sampleTree, readOnlyPaths, readWritePaths) - assert.NilError(t, err) - for _, v := range correctedPathValues { - fmt.Printf("%s %v\n", (*v).Path, v.String()) - } - assert.Equal(t, len(correctedPathValues), 5) - - for _, correctedPathValue := range correctedPathValues { - switch correctedPathValue.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, correctedPathValue.GetValue().GetType(), devicechange.ValueType_STRING, correctedPathValue.Path) - assert.Equal(t, len(correctedPathValue.GetValue().GetTypeOpts()), 0) - default: - t.Fatal("Unexpected path", correctedPathValue.Path) - } - } -} - -func Test_correctJsonPathRwValues2(t *testing.T) { - - td1Schema, _ := td1.UnzipSchema() - - 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) - - 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()) - } - - for _, correctedPathValue := range correctedPathValues { - switch correctedPathValue.Path { - case - "/cont1a/cont2a/leaf2a": - assert.Equal(t, correctedPathValue.GetValue().GetType(), devicechange.ValueType_UINT, correctedPathValue.Path) - assert.Equal(t, len(correctedPathValue.GetValue().GetTypeOpts()), 0) - assert.Equal(t, correctedPathValue.GetValue().ValueToString(), "12") - case - "/cont1a/cont2a/leaf2b": - 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) - } - } -} diff --git a/pkg/modelregistry/jsonvalues/convertJsonTd2Rw_test.go b/pkg/modelregistry/jsonvalues/convertJsonTd2_test.go similarity index 63% rename from pkg/modelregistry/jsonvalues/convertJsonTd2Rw_test.go rename to pkg/modelregistry/jsonvalues/convertJsonTd2_test.go index 09541f682..2b44a21bc 100644 --- a/pkg/modelregistry/jsonvalues/convertJsonTd2Rw_test.go +++ b/pkg/modelregistry/jsonvalues/convertJsonTd2_test.go @@ -25,11 +25,20 @@ import ( "gotest.tools/assert" "io/ioutil" "testing" + "time" ) // For testing TestDevice-2.0.0 - which has an augmented YANG with Choice and Case type modelPluginTestDevice2 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 modelPluginTestDevice2) ModelData() (string, string, []*gnmi.ModelData, string) { return modelTypeTest, modelVersionTest, modelData, moduleNameTest } @@ -73,26 +82,23 @@ func Test_JsonPathValuesTd2_config(t *testing.T) { readOnlyPaths, readWritePaths := modelregistry.ExtractPaths(td2Schema["Device"], yang.TSUnset, "", "") assert.Equal(t, 15, len(readWritePaths)) - // All values are taken from testdata/sample-testdevice2-config.json sampleTree, err := ioutil.ReadFile("./testdata/sample-testdevice2-config.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), 10) - for _, v := range correctedPathValues { - fmt.Printf("%s %v\n", (*v).Path, v.String()) - } + assert.Equal(t, len(pathValues), 10) - for _, correctedPathValue := range correctedPathValues { - switch correctedPathValue.Path { + for _, pathValue := range pathValues { + //t.Logf("%v", pathValue) + switch pathValue.Path { case "/cont1a/leaf1a": - assert.Equal(t, correctedPathValue.GetValue().GetType(), devicechange.ValueType_STRING, correctedPathValue.Path) + assert.Equal(t, pathValue.GetValue().GetType(), devicechange.ValueType_STRING, pathValue.Path) case "/cont1a/cont2a/leaf2e": - assert.Equal(t, correctedPathValue.GetValue().GetType(), devicechange.ValueType_LEAFLIST_INT, correctedPathValue.Path) - leaf2eLl := (*devicechange.TypedLeafListInt64)(correctedPathValue.Value) + assert.Equal(t, pathValue.GetValue().GetType(), devicechange.ValueType_LEAFLIST_INT, pathValue.Path) + leaf2eLl := (*devicechange.TypedLeafListInt64)(pathValue.Value) assert.Equal(t, 5, len(leaf2eLl.List())) assert.Equal(t, 5, leaf2eLl.List()[0]) assert.Equal(t, 1, leaf2eLl.List()[4]) @@ -102,20 +108,20 @@ func Test_JsonPathValuesTd2_config(t *testing.T) { "/cont1a/list2a[name=l2a1]/rx-power", "/cont1a/list2a[name=l2a2]/tx-power", "/cont1a/list2a[name=l2a2]/rx-power": - assert.Equal(t, correctedPathValue.GetValue().GetType(), devicechange.ValueType_UINT, correctedPathValue.Path) + assert.Equal(t, pathValue.GetValue().GetType(), devicechange.ValueType_UINT, pathValue.Path) case "/cont1a/cont2a/leaf2b", "/cont1a/cont2a/leaf2d": - assert.Equal(t, correctedPathValue.GetValue().GetType(), devicechange.ValueType_DECIMAL, correctedPathValue.Path) + assert.Equal(t, pathValue.GetValue().GetType(), devicechange.ValueType_DECIMAL, pathValue.Path) case "/cont1a/cont2a/leaf2f": - assert.Equal(t, correctedPathValue.GetValue().GetType(), devicechange.ValueType_BYTES, correctedPathValue.Path) + assert.Equal(t, pathValue.GetValue().GetType(), devicechange.ValueType_BYTES, pathValue.Path) case "/cont1a/cont2a/leaf2g": - assert.Equal(t, correctedPathValue.GetValue().GetType(), devicechange.ValueType_BOOL, correctedPathValue.Path) + assert.Equal(t, pathValue.GetValue().GetType(), devicechange.ValueType_BOOL, pathValue.Path) default: - t.Fatal("Unexpected path", correctedPathValue.Path) + t.Fatal("Unexpected path", pathValue.Path) } } ygotValues, err := modelPluginTest.UnmarshalConfigValues(sampleTree) @@ -134,29 +140,24 @@ func Test_correctJsonPathValuesTd2(t *testing.T) { assert.Equal(t, len(td2Schema), 8) 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) - correctedPathValues, err := DecomposeJSONWithPaths(sampleTree, readOnlyPaths, readWritePaths) + pathValues, 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(pathValues), 2) - for _, correctedPathValue := range correctedPathValues { - switch correctedPathValue.Path { + for _, pathValue := range pathValues { + //t.Logf("%v", pathValue) + switch pathValue.Path { case "/cont1a/cont2d/leaf2d3c", "/cont1a/cont2d/chocolate": - 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) default: - t.Fatal("Unexpected path", correctedPathValue.Path) + t.Fatal("Unexpected path", pathValue.Path) } } ygotValues, err := modelPluginTest.UnmarshalConfigValues(sampleTree) @@ -189,3 +190,45 @@ func Test_correctJsonPathValuesTd2Wrong(t *testing.T) { "are present and should not be. Raise issue on YGOT. %v", err) //assert.Error(t, err, "choice") } + +func Test_correctJsonPathValuesTd(t *testing.T) { + + td2Schema, err := td2.UnzipSchema() + assert.NilError(t, err) + assert.Equal(t, len(td2Schema), 8) + + readOnlyPaths, _ := modelregistry.ExtractPaths(td2Schema["Device"], yang.TSUnset, "", "") + assert.Equal(t, len(readOnlyPaths), 2) + + sampleTree, err := ioutil.ReadFile("./testdata/sample-testdevice2-opstate.json") + assert.NilError(t, err) + time.Sleep(10 * time.Millisecond) + + pathValues, err := DecomposeJSONWithPaths(sampleTree, readOnlyPaths, nil) + assert.NilError(t, err) + assert.Equal(t, 8, len(pathValues)) + + for _, pathValue := range pathValues { + //t.Logf("%v", pathValue) + switch pathValue.Path { + case + "/cont1a/cont2a/leaf2c", + "/cont1b-state/list2b[index1=101][index2=102]/leaf3c", + "/cont1b-state/list2b[index1=101][index2=103]/leaf3c", + "/cont1b-state/list2b[index1=101][index2=102]/leaf3d", + "/cont1b-state/list2b[index1=101][index2=103]/leaf3d", + "/cont1b-state/cont2c/leaf3b": + assert.Equal(t, pathValue.GetValue().GetType(), devicechange.ValueType_STRING, pathValue.Path) + assert.Equal(t, len(pathValue.GetValue().GetTypeOpts()), 0) + case + "/cont1b-state/leaf2d": + assert.Equal(t, pathValue.GetValue().GetType(), devicechange.ValueType_UINT, pathValue.Path) + assert.Equal(t, len(pathValue.GetValue().GetTypeOpts()), 0) + case + "/cont1b-state/cont2c/leaf3a": + assert.Equal(t, pathValue.GetValue().GetType(), devicechange.ValueType_BOOL, pathValue.Path) + default: + t.Fatal("Unexpected path", pathValue.Path) + } + } +} diff --git a/pkg/modelregistry/jsonvalues/convertJsonTdRo_test.go b/pkg/modelregistry/jsonvalues/convertJsonTdRo_test.go deleted file mode 100644 index 1f3735a9d..000000000 --- a/pkg/modelregistry/jsonvalues/convertJsonTdRo_test.go +++ /dev/null @@ -1,73 +0,0 @@ -// 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 jsonvalues - -import ( - "fmt" - devicechange "github.com/onosproject/onos-config/api/types/change/device" - "github.com/onosproject/onos-config/pkg/modelregistry" - "github.com/openconfig/goyang/pkg/yang" - "gotest.tools/assert" - "io/ioutil" - "testing" -) - -func Test_correctJsonPathValuesTd(t *testing.T) { - - var modelPluginTest modelPluginTestDevice2 - - td2Schema, err := modelPluginTest.Schema() - assert.NilError(t, err) - assert.Equal(t, len(td2Schema), 8) - - readOnlyPaths, _ := modelregistry.ExtractPaths(td2Schema["Device"], yang.TSUnset, "", "") - assert.Equal(t, len(readOnlyPaths), 2) - - // All values are taken from testdata/sample-testdevice-opstate.json and defined - // here in the intermediate jsonToValues format - sampleTree, err := ioutil.ReadFile("./testdata/sample-testdevice2-opstate.json") - assert.NilError(t, err) - - correctedPathValues, err := DecomposeJSONWithPaths(sampleTree, readOnlyPaths, nil) - assert.NilError(t, err) - assert.Equal(t, 8, len(correctedPathValues)) - - for _, v := range correctedPathValues { - fmt.Printf("%s %v\n", (*v).Path, v.String()) - } - - for _, correctedPathValue := range correctedPathValues { - switch correctedPathValue.Path { - case - "/cont1a/cont2a/leaf2c", - "/cont1b-state/list2b[index1=101][index2=102]/leaf3c", - "/cont1b-state/list2b[index1=101][index2=103]/leaf3c", - "/cont1b-state/list2b[index1=101][index2=102]/leaf3d", - "/cont1b-state/list2b[index1=101][index2=103]/leaf3d", - "/cont1b-state/cont2c/leaf3b": - assert.Equal(t, correctedPathValue.GetValue().GetType(), devicechange.ValueType_STRING, correctedPathValue.Path) - assert.Equal(t, len(correctedPathValue.GetValue().GetTypeOpts()), 0) - case - "/cont1b-state/leaf2d": - assert.Equal(t, correctedPathValue.GetValue().GetType(), devicechange.ValueType_UINT, correctedPathValue.Path) - assert.Equal(t, len(correctedPathValue.GetValue().GetTypeOpts()), 0) - case - "/cont1b-state/cont2c/leaf3a": - assert.Equal(t, correctedPathValue.GetValue().GetType(), devicechange.ValueType_BOOL, correctedPathValue.Path) - default: - t.Fatal("Unexpected path", correctedPathValue.Path) - } - } -} diff --git a/pkg/modelregistry/jsonvalues/jsonToValues.go b/pkg/modelregistry/jsonvalues/jsonToValues.go index f12841562..e307b5ba1 100644 --- a/pkg/modelregistry/jsonvalues/jsonToValues.go +++ b/pkg/modelregistry/jsonvalues/jsonToValues.go @@ -61,8 +61,6 @@ func DecomposeJSONWithPaths(genericJSON []byte, ropaths modelregistry.ReadOnlyPa // extractValuesIntermediate recursively walks a JSON tree to create a flat set // of paths and values. -// Note: it is not possible to find indices of lists and accurate devicechange directly -// from json - for that the RO Paths must be consulted func extractValuesWithPaths(f interface{}, parentPath string, modelROpaths modelregistry.ReadOnlyPathMap, modelRWpaths modelregistry.ReadWritePathMap) ([]*devicechange.PathValue, error) { @@ -287,7 +285,7 @@ func handleAttribute(value interface{}, parentPath string, modelROpaths modelreg } typedValue = devicechange.NewTypedValueBytes(dstBytes) default: - typedValue, err = handleAttributeLeafList(modelPath, modeltype, value) + typedValue, err = handleAttributeLeafList(modeltype, value) if err != nil { return nil, err } @@ -296,7 +294,7 @@ func handleAttribute(value interface{}, parentPath string, modelROpaths modelreg } // A continuation of handle attribute above -func handleAttributeLeafList(modelPath string, modeltype devicechange.ValueType, +func handleAttributeLeafList(modeltype devicechange.ValueType, value interface{}) (*devicechange.TypedValue, error) { var typedValue *devicechange.TypedValue diff --git a/pkg/modelregistry/jsonvalues/jsonToValues_test.go b/pkg/modelregistry/jsonvalues/jsonToValues_test.go index 85c6b993b..3f0db7697 100644 --- a/pkg/modelregistry/jsonvalues/jsonToValues_test.go +++ b/pkg/modelregistry/jsonvalues/jsonToValues_test.go @@ -46,13 +46,13 @@ func Test_DecomposeTree(t *testing.T) { assert.Assert(t, len(sampleTree) > 0, "Empty sample tree", len(sampleTree)) ds1RoPaths, ds1RwPaths := setUpRwPaths() - values, err := DecomposeJSONWithPaths(sampleTree, ds1RoPaths, ds1RwPaths) + pathValues, err := DecomposeJSONWithPaths(sampleTree, ds1RoPaths, ds1RwPaths) assert.NilError(t, err) - assert.Equal(t, len(values), 25) + assert.Equal(t, len(pathValues), 25) - for _, v := range values { - t.Logf("%s %s\n", (*v).Path, (*v).GetValue().ValueToString()) - switch v.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/address", "/system/openflow/controllers/controller[name=main]/connections/connection[aux-id=10]/state/aux-id", @@ -74,18 +74,18 @@ func Test_DecomposeTree(t *testing.T) { "/system/openflow/controllers/controller[name=second]/connections/connection[aux-id=11]/state/port", "/system/openflow/controllers/controller[name=second]/connections/connection[aux-id=11]/state/source-interface", "/system/openflow/controllers/controller[name=second]/connections/connection[aux-id=11]/state/transport": - assert.Equal(t, devicechange.ValueType_STRING, v.GetValue().GetType(), v.Path) + assert.Equal(t, devicechange.ValueType_STRING, pathValue.GetValue().GetType(), pathValue.Path) case "/interfaces/interface[name=admin]/config/enabled": - assert.Equal(t, devicechange.ValueType_BOOL, v.GetValue().GetType(), v.Path) + assert.Equal(t, devicechange.ValueType_BOOL, pathValue.GetValue().GetType(), pathValue.Path) 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, devicechange.ValueType_UINT, v.GetValue().GetType(), v.Path) + assert.Equal(t, devicechange.ValueType_UINT, pathValue.GetValue().GetType(), pathValue.Path) default: - t.Fatal("Unexpected jsonPath", v.Path) + t.Fatal("Unexpected jsonPath", pathValue.Path) } } } @@ -97,12 +97,12 @@ func Test_DecomposeTreeConfigOnly(t *testing.T) { assert.Assert(t, len(sampleTree) > 0, "Empty sample tree", len(sampleTree)) _, ds1RwPaths := setUpRwPaths() - values, err := DecomposeJSONWithPaths(sampleTree, nil, ds1RwPaths) + pathValues, err := DecomposeJSONWithPaths(sampleTree, nil, ds1RwPaths) assert.NilError(t, err) - assert.Equal(t, len(values), 6) + assert.Equal(t, len(pathValues), 6) - for _, v := range values { - t.Logf("%s %s\n", (*v).Path, (*v).GetValue().ValueToString()) + for _, v := range pathValues { + //t.Logf("%v", v) switch v.Path { case "/system/logging/remote-servers/remote-server[host=h2]/config/host", diff --git a/pkg/modelregistry/jsonvalues/testdata/sample-device1.json b/pkg/modelregistry/jsonvalues/testdata/sample-device1.json deleted file mode 100644 index ff1d8c646..000000000 --- a/pkg/modelregistry/jsonvalues/testdata/sample-device1.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "cont1a": { - "cont2a": { - "leaf2a": 12, - "leaf2b": 1.21 - } - } -}