diff --git a/sdk/cpp/gnmi/samples/CMakeLists.txt b/sdk/cpp/gnmi/samples/CMakeLists.txt index 5f9efce73..43bbee205 100644 --- a/sdk/cpp/gnmi/samples/CMakeLists.txt +++ b/sdk/cpp/gnmi/samples/CMakeLists.txt @@ -13,7 +13,7 @@ set(samples bgp_gnmi_service bgp_gnmi_subscribe # gnmi_ifc_read -# xr_int_oper_gnmi_subscribe + xr_int_oper_gnmi_subscribe ) # set default build type if not specified by user diff --git a/sdk/cpp/gnmi/tests/test_gnmi_service.cpp b/sdk/cpp/gnmi/tests/test_gnmi_service.cpp index 4705bd053..ea75ddbfd 100644 --- a/sdk/cpp/gnmi/tests/test_gnmi_service.cpp +++ b/sdk/cpp/gnmi/tests/test_gnmi_service.cpp @@ -73,16 +73,16 @@ void read_sub(const char * subscribe_response) void gnmi_service_subscribe_callback(const char * subscribe_response) { - //read_sub(subscribe_response); - string response = subscribe_response; - REQUIRE(response.find(int_update) != string::npos); + //read_sub(subscribe_response); + string response = subscribe_response; + REQUIRE(response.find(int_update) != string::npos); } void gnmi_service_subscribe_multiples_callback(const char * subscribe_response) { - //read_sub(subscribe_response); - string response = subscribe_response; - REQUIRE(response.find(int_update) != string::npos); + //read_sub(subscribe_response); + string response = subscribe_response; + REQUIRE(response.find(int_update) != string::npos); REQUIRE(response.find(bgp_update) != string::npos); } diff --git a/sdk/go/core/tests/service_codec_test.go b/sdk/go/core/tests/service_codec_test.go index 264a9045e..974cfef7e 100644 --- a/sdk/go/core/tests/service_codec_test.go +++ b/sdk/go/core/tests/service_codec_test.go @@ -458,7 +458,7 @@ func (suite *CodecTestSuite) TestXMLEncodeDecodeMultiple() { config := types.NewConfig(&runnerConfig, &nativeConfig) - suite.Provider.Encoding = encoding.XML + suite.Provider.Encoding = encoding.XML payload := suite.Codec.Encode(&suite.Provider, &config) entity := suite.Codec.Decode(&suite.Provider, payload) @@ -483,7 +483,7 @@ func (suite *CodecTestSuite) TestPassiveInterfaceCodec() { runner.One.Ospf = append(runner.One.Ospf, &ospf) suite.Provider.Encoding = encoding.XML payload := suite.Codec.Encode(&suite.Provider, &runner) - suite.Equal(payload, + suite.Equal(payload, ` @@ -531,7 +531,7 @@ func (suite *CodecTestSuite) TestOneKeyList() { suite.NotNil(entity) ydk.YLogDebug(fmt.Sprintf("For key: %v, Found Entity: %v", key, types.EntityToString(entity))) } - + // Remove element from the ylist i, rdata := ylist.Get(runner.TwoList.Ldata, 22) suite.Equal(i, 1) @@ -559,6 +559,17 @@ func (suite *CodecTestSuite) TestListNoKeys() { suite.Equal(types.EntityEqual(&runner, runnerDecode), true) } +func (suite *CodecTestSuite) TestLeafList() { + runner := ysanity.Runner{} + runner.Ytypes.BuiltInT.EnumLlist = append(runner.Ytypes.BuiltInT.EnumLlist, ysanity.YdkEnumTest_local) + runner.Ytypes.BuiltInT.EnumLlist = append(runner.Ytypes.BuiltInT.EnumLlist, ysanity.YdkEnumTest_remote) + + payload := suite.Codec.Encode(&suite.Provider, &runner) + fmt.Printf("%s\n", payload) + runnerDecode := suite.Codec.Decode(&suite.Provider, payload) + suite.True(types.EntityEqual(&runner, runnerDecode)) +} + func (suite *CodecTestSuite) TestNative() { // Build loopback configuration address := ysanity.Native_Interface_Loopback_Ipv4_Address{} @@ -571,12 +582,13 @@ func (suite *CodecTestSuite) TestNative() { native := ysanity.Native{} native.Interface.Loopback = append(native.Interface.Loopback, &loopback) - + suite.Provider.Encoding = encoding.JSON payload := suite.Codec.Encode(&suite.Provider, &native) fmt.Printf("%s\n", payload) runnerDecode := suite.Codec.Decode(&suite.Provider, payload) - suite.Equal(types.EntityEqual(&native, runnerDecode), true)} + suite.Equal(types.EntityEqual(&native, runnerDecode), true) +} func TestCodecTestSuite(t *testing.T) { if testing.Verbose() { diff --git a/sdk/go/core/tests/types_core_test.go b/sdk/go/core/tests/types_core_test.go new file mode 100644 index 000000000..7ff6f5219 --- /dev/null +++ b/sdk/go/core/tests/types_core_test.go @@ -0,0 +1,468 @@ +package test + +import ( + "fmt" + ysanity "github.com/CiscoDevNet/ydk-go/ydk/models/ydktest/sanity" + "github.com/CiscoDevNet/ydk-go/ydk" + "github.com/CiscoDevNet/ydk-go/ydk/types" + "github.com/CiscoDevNet/ydk-go/ydk/types/ylist" + "github.com/stretchr/testify/suite" + "testing" + "sort" +) + +type CoreTypesTestSuite struct { + suite.Suite +} + +func (suite *CoreTypesTestSuite) SetupSuite() { +} + +func (suite *CoreTypesTestSuite) TestEntityCollection() { + // Create Data entities and access values + runner := ysanity.Runner{} + native := ysanity.Native{} + runnerPath := types.GetSegmentPath(&runner) + nativePath := types.GetSegmentPath(&native) + suite.Equal(types.EntityToString(&runner), "Type: *sanity.Runner, Path: ydktest-sanity:runner") + suite.Equal(types.EntityToString(&native), "Type: *sanity.Native, Path: ydktest-sanity:native") + + // Initialization + config := types.NewEntityCollection() + suite.Equal(config.Len(), 0) + suite.Equal(config.String(), "EntityCollection is empty") + + config = types.NewEntityCollection(&runner) + suite.Equal(config.Len(), 1) + + config = types.NewEntityCollection(&runner, &native) + suite.Equal(config.Len(), 2) + suite.Equal(config.String(), "EntityCollection [Type: *sanity.Runner, Path: ydktest-sanity:runner; Type: *sanity.Native, Path: ydktest-sanity:native]") + + // Add + config = types.NewEntityCollection() + config.Append([]types.Entity{&runner, &native}) + suite.Equal(config.Len(), 2) + + config.Add(&runner) + suite.Equal(config.Len(), 2) + + // Get + e, _ := config.Get(runnerPath) + suite.NotNil(e) + suite.IsType(&runner, e) + + // HasKey + suite.Equal(config.HasKey(runnerPath), true) + suite.Equal(config.HasKey(nativePath), true) + suite.Equal(config.HasKey("oc_bgp"), false) + + // Get all keys + suite.Equal(config.Keys(), []string{runnerPath, nativePath}) + + // Get all entities + ydk.YLogDebug("All entities:") + for _, entity := range config.Entities() { + ydk.YLogDebug(types.EntityToString(entity)) + } + + // Delete entity + e, _ = config.Pop(runnerPath) + suite.NotNil(e) + suite.IsType(&runner, e) + suite.Equal(config.Keys(), []string{nativePath}) + + ydk.YLogDebug("All entities after Runner deleted:") + for _, key := range config.Keys() { + en, exist := config.Get(key) + if exist { + ydk.YLogDebug(fmt.Sprintf("%s", types.EntityToString(en))) + } + } + + // Add back and test order + config.Add(&runner) + suite.Equal(config.Keys(), []string{nativePath, runnerPath}) + + // Getting enities by item number + ydk.YLogDebug("Getting enities by item number:") + for i:=0; i<3; i++ { + e = config.GetItem(i) + if e != nil { + ydk.YLogDebug(fmt.Sprintf("%d: %s", i, types.EntityToString(e))) + } else { + ydk.YLogDebug(fmt.Sprintf("%d: nil\n", i)) + } + } + // Clear collection + config.Clear() + suite.Equal(config.Len(), 0) + + // Testing passing parameters and return values + ret1 := testParams("test1", config) + col1 := types.EntityToCollection(ret1) + suite.Equal(col1.Len(), 0) + + ret2 := testParams("test2", &runner) + suite.Equal(types.EntityEqual(ret2, &runner), true) + + config.Add(&runner, &native) + ret3 := testParams("test3", config) + col3 := types.EntityToCollection(ret3) + suite.Equal(col3.Len(), 2) + + // Testing Config and Filter aliases + cfg := types.NewConfig() + cfg.Add(&runner, &native) + suite.Equal(types.IsEntityCollection(cfg), true) + filter := types.NewFilter(&native) + suite.Equal(types.IsEntityCollection(filter), true) + suite.Equal(filter.Len(), 1) +} + +func (suite *CoreTypesTestSuite) TestListTwoKeys() { + runner := ysanity.Runner{} + l1 := ysanity.Runner_TwoKeyList{First: "f1", Second: 11} + l2 := ysanity.Runner_TwoKeyList{First: "f2", Second: 22} + runner.TwoKeyList = []*ysanity.Runner_TwoKeyList {&l1, &l2} + + ldataKeys := ylist.Keys(runner.TwoKeyList) + suite.Equal(fmt.Sprintf("%v", ldataKeys), "[[f1 11] [f2 22]]") + + for _, lkey := range ldataKeys { + _, ldata := ylist.Get(runner.TwoKeyList, lkey); + suite.NotNil(ldata) + } + _, ldata := ylist.Get(runner.TwoKeyList, "f1", 11) + suite.Equal(types.EntityEqual(ldata, &l1), true) + _, ldata = ylist.Get(runner.TwoKeyList, "f2", 22) + suite.Equal(types.EntityEqual(ldata, &l2), true) + + i, ldata := ylist.Get(runner.TwoKeyList, "f1", 11) + suite.Equal(i, 0) + suite.NotNil(ldata) + suite.Equal(types.EntityEqual(ldata, &l1), true) + if ldata != nil { + runner.TwoKeyList = append(runner.TwoKeyList[:i], runner.TwoKeyList[i+1:]...) + i, ldata = ylist.Get(runner.TwoKeyList, "f1", 11) + suite.Nil(ldata) + } +} + +func (suite *CoreTypesTestSuite) TestIdentityList() { + runner := ysanity.Runner{} + i1 := ysanity.Runner_IdentityList{Name: "first"} + i2 := ysanity.Runner_IdentityList{Name: "second"} + i3 := ysanity.Runner_IdentityList{Name: "third"} + runner.IdentityList = []*ysanity.Runner_IdentityList {&i1, &i2, &i3} + + ldataKeys := ylist.Keys(runner.IdentityList) + suite.Equal(fmt.Sprintf("%v", ldataKeys), "[first second third]") + + var ldata types.Entity + for _, lkey := range ldataKeys { + _, ldata = ylist.Get(runner.IdentityList, lkey); + suite.NotNil(ldata) + } + _, ldata = ylist.Get(runner.IdentityList, "first") + suite.Equal(types.EntityEqual(ldata, &i1), true) + _, ldata = ylist.Get(runner.IdentityList, "third") + suite.Equal(types.EntityEqual(ldata, &i3), true) +} + +func (suite *CoreTypesTestSuite) TestEnumList() { + runner := ysanity.Runner{} + i1 := ysanity.Runner_EnumList{Name: ysanity.YdkEnumTest_none} + i2 := ysanity.Runner_EnumList{Name: ysanity.YdkEnumTest_local} + i3 := ysanity.Runner_EnumList{Name: ysanity.YdkEnumTest_remote} + runner.EnumList = []*ysanity.Runner_EnumList{&i1, &i2, &i3} + + ldataKeys := ylist.Keys(runner.EnumList) + suite.Equal(fmt.Sprintf("%v", ldataKeys), "[none local remote]") + + var ldata types.Entity + for _, lkey := range ldataKeys { + _, ldata = ylist.Get(runner.EnumList, lkey); + suite.NotNil(ldata) + } + _, ldata = ylist.Get(runner.EnumList, "none") + suite.Equal(types.EntityEqual(ldata, &i1), true) + _, ldata = ylist.Get(runner.EnumList, "remote") + suite.Equal(types.EntityEqual(ldata, &i3), true) +} + +func (suite *CoreTypesTestSuite) TestListNoKeys() { + runner := ysanity.Runner{} + t1 := ysanity.Runner_NoKeyList{Test: "t1"} + t2 := ysanity.Runner_NoKeyList{Test: "t2"} + t3 := ysanity.Runner_NoKeyList{Test: "t3"} + runner.NoKeyList = []*ysanity.Runner_NoKeyList {&t1, &t2, &t3} + + suite.Equal(len(ylist.Keys(runner.NoKeyList)), 0) + var count string + for _, elem := range runner.NoKeyList { + count = count + elem.Test.(string) + } + suite.Equal(count, "t1t2t3") +} + +func checkEmptyStringValue(v string) string { + if len(v) == 0 { + v = "Exists" + } + return v +} + +func printDictionary(legend string, entdict map[string]string) { + fmt.Printf("\n------> DICTIONARY%s\n", legend) + var keys []string + for k := range entdict { + keys = append(keys, k) + } + sort.Strings(keys) + for _, k := range keys { + fmt.Printf("%s: %s\n", k, checkEmptyStringValue(entdict[k])) + } +} + +func printDiff(suite *CoreTypesTestSuite, diff map[string]types.StringPair, entLeft, entRight types.Entity ) { + fmt.Printf("\n------> DIFFS:\n") + var keys []string + for k := range diff { + keys = append(keys, k) + } + sort.Strings(keys) + for _, key := range keys { + valuePair := diff[key] + fmt.Printf("%s: %s vs %s\n", key, + checkEmptyStringValue(valuePair.Left), + checkEmptyStringValue(valuePair.Right)) + ent := entLeft + if valuePair.Left == "None" { + ent = entRight + } + suite.NotNil(types.PathToEntity(ent, key)) + } +} + +func (suite *CoreTypesTestSuite) TestEntityDiff_TwoKeys() { + runner1 := ysanity.Runner{} + l1 := ysanity.Runner_TwoKeyList{First: "f1", Second: 11, Property: 82} + l2 := ysanity.Runner_TwoKeyList{First: "f2", Second: 22, Property: 83} + runner1.TwoKeyList = []*ysanity.Runner_TwoKeyList {&l1, &l2} + types.SetAllParents(&runner1) + + entDict1 := types.EntityToDict(&runner1) + suite.Equal(len(entDict1), 4) + printDictionary("-ORIGINAL", entDict1) + + runner2 := ysanity.Runner{} + ll1 := ysanity.Runner_TwoKeyList{First: "f1", Second: 11, Property: 82} + ll2 := ysanity.Runner_TwoKeyList{First: "f2", Second: 22, Property: 83} + runner2.TwoKeyList = []*ysanity.Runner_TwoKeyList {&ll1, &ll2} + types.SetAllParents(&runner2) + + diff := types.EntityDiff(&runner1, &runner2) + suite.Equal(0, len(diff)) + + ll1.Property = 83 + entDict2 := types.EntityToDict(&runner2) + printDictionary("-CHANGED-LEAF", entDict2) + + diff = types.EntityDiff(&runner1, &runner2) + suite.Equal(1, len(diff)) + printDiff(suite, diff, &runner1, &runner2) + + ll1.First = "f2" + entDict3 := types.EntityToDict(&runner2) + printDictionary("-CHANGED-KEY", entDict3) + + diff = types.EntityDiff(&runner1, &runner2) + suite.Equal(2, len(diff)) + printDiff(suite, diff, &runner1, &runner2) + + clone := types.EntityClone(&runner2) + diff = types.EntityDiff(clone, &runner2) + suite.Equal(0, len(diff)) +} + +func (suite *CoreTypesTestSuite) TestEntityDiff_Presence() { + runner := ysanity.Runner{} + runner.Runner2.SomeLeaf = "some_leaf" + types.SetPresenceFlag(&runner.Runner2) + types.SetAllParents(&runner) + + entDict1 := types.EntityToDict(&runner) + suite.Equal(len(entDict1), 2) + printDictionary("-LEFT", entDict1) + + emptyRunner := ysanity.Runner{} + types.SetAllParents(&emptyRunner) + entDict2 := types.EntityToDict(&emptyRunner) + suite.Equal(len(entDict2), 0) + printDictionary("-RIGHT", entDict2); + + diff := types.EntityDiff(&runner, &emptyRunner) + suite.Equal(1, len(diff)) + printDiff(suite, diff, &runner, &emptyRunner) + + clone := types.EntityClone(&runner) + diff = types.EntityDiff(&runner, clone) + suite.Equal(0, len(diff)) +} + +func (suite *CoreTypesTestSuite) TestEntityDiff_TwoListPos() { + runner1 := ysanity.Runner{} + elem1 := ysanity.Runner_TwoList_Ldata{} + elem2 := ysanity.Runner_TwoList_Ldata{} + elem1.Number = 1 + elem1.Name = "runner:twolist:ldata:1" + elem2.Number = 2 + elem2.Name = "runner:twolist:ldata:2" + + elem11 := ysanity.Runner_TwoList_Ldata_Subl1{} + elem12 := ysanity.Runner_TwoList_Ldata_Subl1{} + elem11.Number = 11 + elem11.Name = "runner:twolist:ldata:1:subl1:11" + elem12.Number = 12 + elem12.Name = "runner:twolist:ldata:1:subl1:12" + + elem1.Subl1 = append(elem1.Subl1, &elem11) + elem1.Subl1 = append(elem1.Subl1, &elem12) + + elem21 := ysanity.Runner_TwoList_Ldata_Subl1{} + elem22 := ysanity.Runner_TwoList_Ldata_Subl1{} + elem21.Number = 21 + elem21.Name = "runner:twolist:ldata:2:subl1:21" + elem22.Number = 22 + elem22.Name = "runner:twolist:ldata:2:subl1:22" + + elem2.Subl1 = append(elem2.Subl1, &elem21) + elem2.Subl1 = append(elem2.Subl1, &elem22) + + runner1.TwoList.Ldata = append(runner1.TwoList.Ldata, &elem1) + runner1.TwoList.Ldata = append(runner1.TwoList.Ldata, &elem2) + types.SetAllParents(&runner1) + + entDict := types.EntityToDict(&runner1) + suite.Equal(12, len(entDict)) + printDictionary("-LEFT", entDict) + + runner2 := ysanity.Runner{} + elem1 = ysanity.Runner_TwoList_Ldata{} + elem1.Number = 1 + elem1.Name = "runner:twolist:ldata:1" + elem11 = ysanity.Runner_TwoList_Ldata_Subl1{} + elem12 = ysanity.Runner_TwoList_Ldata_Subl1{} + elem11.Number = 11 + elem11.Name = "runner:twolist:ldata:1:subl1:11" + elem12.Number = 12 + elem12.Name = "runner:twolist:ldata:1:subl1:12" + + elem1.Subl1 = append(elem1.Subl1, &elem11) + elem1.Subl1 = append(elem1.Subl1, &elem12) + + runner2.TwoList.Ldata = append(runner2.TwoList.Ldata, &elem1) + types.SetAllParents(&runner2) + + entDict = types.EntityToDict(&runner2) + suite.Equal(6, len(entDict)) + printDictionary("-RIGHT", entDict) + + diff := types.EntityDiff(&runner1, &runner2) + suite.Equal(1, len(diff)) + printDiff(suite, diff, &runner1, &runner2) + + clone := types.EntityClone(&runner1) + diff = types.EntityDiff(&runner1, clone) + suite.Equal(0, len(diff)) +} + +func (suite *CoreTypesTestSuite) TestEntityDiff_ListNoKeys() { + t1 := ysanity.Runner_NoKeyList{Test: "t1"} + t2 := ysanity.Runner_NoKeyList{Test: "tt"} + t3 := ysanity.Runner_NoKeyList{Test: "t3"} + runner1 := ysanity.Runner{} + runner1.NoKeyList = []*ysanity.Runner_NoKeyList {&t1, &t2, &t3} + + entDict := types.EntityToDict(&runner1) + suite.Equal(6, len(entDict)) + printDictionary("-LEFT", entDict) + + t11 := ysanity.Runner_NoKeyList{Test: "t1"} + t22 := ysanity.Runner_NoKeyList{Test: "t2"} + runner2 := ysanity.Runner{} + runner2.NoKeyList = []*ysanity.Runner_NoKeyList {&t11, &t22} + + entDict = types.EntityToDict(&runner2) + suite.Equal(4, len(entDict)) + printDictionary("-RIGHT", entDict) + + diff := types.EntityDiff(&runner1, &runner2) + suite.Equal(2, len(diff)) + printDiff(suite, diff, &runner1, &runner2) + + diff = types.EntityDiff(&runner2, &runner1) + suite.Equal(2, len(diff)) + printDiff(suite, diff, &runner2, &runner1) + + clone := types.EntityClone(&runner1) + diff = types.EntityDiff(&runner1, clone) + suite.Equal(0, len(diff)) +} + +func (suite *CoreTypesTestSuite) TestEntityDiff_OneAugList() { + oneAugList := ysanity.Runner_OneList_OneAugList{} + oneAugList.Enabled = true + + elem1 := ysanity.Runner_OneList_OneAugList_Ldata{} + elem1.Number = 1 + elem1.Name = "elem1" + + elem2 := ysanity.Runner_OneList_OneAugList_Ldata{} + elem2.Number = 2 + elem2.Name = "elem2" + + oneAugList.Ldata = []*ysanity.Runner_OneList_OneAugList_Ldata {&elem1, &elem2} + types.SetAllParents(&oneAugList) + + entDict := types.EntityToDict(&oneAugList) + suite.Equal(5, len(entDict)) + printDictionary("", entDict) + + clone := types.EntityClone(&oneAugList) + diff := types.EntityDiff(&oneAugList, clone) + suite.Equal(0, len(diff)) +} + +// TODO: leaf-list encoding error +func (suite *CoreTypesTestSuite) TestEntityDiff_EnumLeafList() { + runner := ysanity.Runner{} + runner.Ytypes.BuiltInT.EnumLlist = append(runner.Ytypes.BuiltInT.EnumLlist, ysanity.YdkEnumTest_local) + runner.Ytypes.BuiltInT.EnumLlist = append(runner.Ytypes.BuiltInT.EnumLlist, ysanity.YdkEnumTest_remote) + entDict := types.EntityToDict(&runner) + // suite.Equal(4, len(entDict)) + printDictionary("-LEFT", entDict) + // suite.Equal(types.EntityEqual(entityRead, &runner), true) + + clone := types.EntityClone(&runner) + diff := types.EntityDiff(&runner, clone) + suite.Equal(0, len(diff)) +} + +func testParams(testName string, entity types.Entity) types.Entity { + ec := types.EntityToCollection(entity) + if ec == nil { + ydk.YLogDebug(fmt.Sprintf("%s: %s\n", testName, types.EntityToString(entity))) + return entity + } + ydk.YLogDebug(fmt.Sprintf("%s: %s\n", testName, ec.String())) + return ec +} + +func TestCoreTypesTestSuite(t *testing.T) { + if testing.Verbose() { + ydk.EnableLogging(ydk.Debug) + } + suite.Run(t, new(CoreTypesTestSuite)) +} diff --git a/sdk/go/core/tests/types_test.go b/sdk/go/core/tests/types_test.go index cbcefe087..b953291b2 100644 --- a/sdk/go/core/tests/types_test.go +++ b/sdk/go/core/tests/types_test.go @@ -8,11 +8,9 @@ import ( "github.com/CiscoDevNet/ydk-go/ydk/providers" "github.com/CiscoDevNet/ydk-go/ydk/services" "github.com/CiscoDevNet/ydk-go/ydk/types" - "github.com/CiscoDevNet/ydk-go/ydk/types/ylist" "github.com/stretchr/testify/suite" "strconv" "testing" - "sort" ) type SanityTypesTestSuite struct { @@ -386,413 +384,6 @@ func (suite *SanityTypesTestSuite) TestPresence() { suite.Equal(runner.Outer.Inner.YPresence, runnerRead.Outer.Inner.YPresence) } -func (suite *SanityTypesTestSuite) TestEntityCollection() { - // Create Data entities and access values - runner := ysanity.Runner{} - native := ysanity.Native{} - runnerPath := types.GetSegmentPath(&runner) - nativePath := types.GetSegmentPath(&native) - suite.Equal(types.EntityToString(&runner), "Type: *sanity.Runner, Path: ydktest-sanity:runner") - suite.Equal(types.EntityToString(&native), "Type: *sanity.Native, Path: ydktest-sanity:native") - - // Initialization - config := types.NewEntityCollection() - suite.Equal(config.Len(), 0) - suite.Equal(config.String(), "EntityCollection is empty") - - config = types.NewEntityCollection(&runner) - suite.Equal(config.Len(), 1) - - config = types.NewEntityCollection(&runner, &native) - suite.Equal(config.Len(), 2) - suite.Equal(config.String(), "EntityCollection [Type: *sanity.Runner, Path: ydktest-sanity:runner; Type: *sanity.Native, Path: ydktest-sanity:native]") - - // Add - config = types.NewEntityCollection() - config.Append([]types.Entity{&runner, &native}) - suite.Equal(config.Len(), 2) - - config.Add(&runner) - suite.Equal(config.Len(), 2) - - // Get - e, _ := config.Get(runnerPath) - suite.NotNil(e) - suite.IsType(&runner, e) - - // HasKey - suite.Equal(config.HasKey(runnerPath), true) - suite.Equal(config.HasKey(nativePath), true) - suite.Equal(config.HasKey("oc_bgp"), false) - - // Get all keys - suite.Equal(config.Keys(), []string{runnerPath, nativePath}) - - // Get all entities - ydk.YLogDebug("All entities:") - for _, entity := range config.Entities() { - ydk.YLogDebug(types.EntityToString(entity)) - } - - // Delete entity - e, _ = config.Pop(runnerPath) - suite.NotNil(e) - suite.IsType(&runner, e) - suite.Equal(config.Keys(), []string{nativePath}) - - ydk.YLogDebug("All entities after Runner deleted:") - for _, key := range config.Keys() { - en, exist := config.Get(key) - if exist { - ydk.YLogDebug(fmt.Sprintf("%s", types.EntityToString(en))) - } - } - - // Add back and test order - config.Add(&runner) - suite.Equal(config.Keys(), []string{nativePath, runnerPath}) - - // Getting enities by item number - ydk.YLogDebug("Getting enities by item number:") - for i:=0; i<3; i++ { - e = config.GetItem(i) - if e != nil { - ydk.YLogDebug(fmt.Sprintf("%d: %s", i, types.EntityToString(e))) - } else { - ydk.YLogDebug(fmt.Sprintf("%d: nil\n", i)) - } - } - // Clear collection - config.Clear() - suite.Equal(config.Len(), 0) - - // Testing passing parameters and return values - ret1 := testParams("test1", config) - col1 := types.EntityToCollection(ret1) - suite.Equal(col1.Len(), 0) - - ret2 := testParams("test2", &runner) - suite.Equal(types.EntityEqual(ret2, &runner), true) - - config.Add(&runner, &native) - ret3 := testParams("test3", config) - col3 := types.EntityToCollection(ret3) - suite.Equal(col3.Len(), 2) - - // Testing Config and Filter aliases - cfg := types.NewConfig() - cfg.Add(&runner, &native) - suite.Equal(types.IsEntityCollection(cfg), true) - filter := types.NewFilter(&native) - suite.Equal(types.IsEntityCollection(filter), true) - suite.Equal(filter.Len(), 1) -} - -func (suite *SanityTypesTestSuite) TestListTwoKeys() { - runner := ysanity.Runner{} - l1 := ysanity.Runner_TwoKeyList{First: "f1", Second: 11} - l2 := ysanity.Runner_TwoKeyList{First: "f2", Second: 22} - runner.TwoKeyList = []*ysanity.Runner_TwoKeyList {&l1, &l2} - - ldataKeys := ylist.Keys(runner.TwoKeyList) - suite.Equal(fmt.Sprintf("%v", ldataKeys), "[[f1 11] [f2 22]]") - - for _, lkey := range ldataKeys { - _, ldata := ylist.Get(runner.TwoKeyList, lkey); - suite.NotNil(ldata) - } - _, ldata := ylist.Get(runner.TwoKeyList, "f1", 11) - suite.Equal(types.EntityEqual(ldata, &l1), true) - _, ldata = ylist.Get(runner.TwoKeyList, "f2", 22) - suite.Equal(types.EntityEqual(ldata, &l2), true) - - i, ldata := ylist.Get(runner.TwoKeyList, "f1", 11) - suite.Equal(i, 0) - suite.NotNil(ldata) - suite.Equal(types.EntityEqual(ldata, &l1), true) - if ldata != nil { - runner.TwoKeyList = append(runner.TwoKeyList[:i], runner.TwoKeyList[i+1:]...) - i, ldata = ylist.Get(runner.TwoKeyList, "f1", 11) - suite.Nil(ldata) - } -} - -func (suite *SanityTypesTestSuite) TestIdentityList() { - runner := ysanity.Runner{} - i1 := ysanity.Runner_IdentityList{Name: "first"} - i2 := ysanity.Runner_IdentityList{Name: "second"} - i3 := ysanity.Runner_IdentityList{Name: "third"} - runner.IdentityList = []*ysanity.Runner_IdentityList {&i1, &i2, &i3} - - ldataKeys := ylist.Keys(runner.IdentityList) - suite.Equal(fmt.Sprintf("%v", ldataKeys), "[first second third]") - - var ldata types.Entity - for _, lkey := range ldataKeys { - _, ldata = ylist.Get(runner.IdentityList, lkey); - suite.NotNil(ldata) - } - _, ldata = ylist.Get(runner.IdentityList, "first") - suite.Equal(types.EntityEqual(ldata, &i1), true) - _, ldata = ylist.Get(runner.IdentityList, "third") - suite.Equal(types.EntityEqual(ldata, &i3), true) -} - -func (suite *SanityTypesTestSuite) TestEnumList() { - runner := ysanity.Runner{} - i1 := ysanity.Runner_EnumList{Name: ysanity.YdkEnumTest_none} - i2 := ysanity.Runner_EnumList{Name: ysanity.YdkEnumTest_local} - i3 := ysanity.Runner_EnumList{Name: ysanity.YdkEnumTest_remote} - runner.EnumList = []*ysanity.Runner_EnumList{&i1, &i2, &i3} - - ldataKeys := ylist.Keys(runner.EnumList) - suite.Equal(fmt.Sprintf("%v", ldataKeys), "[none local remote]") - - var ldata types.Entity - for _, lkey := range ldataKeys { - _, ldata = ylist.Get(runner.EnumList, lkey); - suite.NotNil(ldata) - } - _, ldata = ylist.Get(runner.EnumList, "none") - suite.Equal(types.EntityEqual(ldata, &i1), true) - _, ldata = ylist.Get(runner.EnumList, "remote") - suite.Equal(types.EntityEqual(ldata, &i3), true) -} - -func (suite *SanityTypesTestSuite) TestListNoKeys() { - runner := ysanity.Runner{} - t1 := ysanity.Runner_NoKeyList{Test: "t1"} - t2 := ysanity.Runner_NoKeyList{Test: "t2"} - t3 := ysanity.Runner_NoKeyList{Test: "t3"} - runner.NoKeyList = []*ysanity.Runner_NoKeyList {&t1, &t2, &t3} - - suite.Equal(len(ylist.Keys(runner.NoKeyList)), 0) - var count string - for _, elem := range runner.NoKeyList { - count = count + elem.Test.(string) - } - suite.Equal(count, "t1t2t3") -} - -func checkEmptyStringValue(v string) string { - if len(v) == 0 { - v = "Exists" - } - return v -} - -func printDictionary(legend string, entdict map[string]string) { - fmt.Printf("\n------> DICTIONARY%s\n", legend) - var keys []string - for k := range entdict { - keys = append(keys, k) - } - sort.Strings(keys) - for _, k := range keys { - fmt.Printf("%s: %s\n", k, checkEmptyStringValue(entdict[k])) - } -} - -func printDiff(suite *SanityTypesTestSuite, diff map[string]types.StringPair, entLeft, entRight types.Entity ) { - fmt.Printf("\n------> DIFFS:\n") - var keys []string - for k := range diff { - keys = append(keys, k) - } - sort.Strings(keys) - for _, key := range keys { - valuePair := diff[key] - fmt.Printf("%s: %s vs %s\n", key, - checkEmptyStringValue(valuePair.Left), - checkEmptyStringValue(valuePair.Right)) - ent := entLeft - if valuePair.Left == "None" { - ent = entRight - } - suite.NotNil(types.PathToEntity(ent, key)) - } -} - -func (suite *SanityTypesTestSuite) TestEntityDiff_TwoKeys() { - runner1 := ysanity.Runner{} - l1 := ysanity.Runner_TwoKeyList{First: "f1", Second: 11, Property: 82} - l2 := ysanity.Runner_TwoKeyList{First: "f2", Second: 22, Property: 83} - runner1.TwoKeyList = []*ysanity.Runner_TwoKeyList {&l1, &l2} - types.SetAllParents(&runner1) - - entDict1 := types.EntityToDict(&runner1) - suite.Equal(len(entDict1), 4) - printDictionary("-ORIGINAL", entDict1) - - runner2 := ysanity.Runner{} - ll1 := ysanity.Runner_TwoKeyList{First: "f1", Second: 11, Property: 82} - ll2 := ysanity.Runner_TwoKeyList{First: "f2", Second: 22, Property: 83} - runner2.TwoKeyList = []*ysanity.Runner_TwoKeyList {&ll1, &ll2} - types.SetAllParents(&runner2) - - diff := types.EntityDiff(&runner1, &runner2) - suite.Equal(0, len(diff)) - - ll1.Property = 83 - entDict2 := types.EntityToDict(&runner2) - printDictionary("-CHANGED-LEAF", entDict2) - - diff = types.EntityDiff(&runner1, &runner2) - suite.Equal(1, len(diff)) - printDiff(suite, diff, &runner1, &runner2) - - ll1.First = "f2" - entDict3 := types.EntityToDict(&runner2) - printDictionary("-CHANGED-KEY", entDict3) - - diff = types.EntityDiff(&runner1, &runner2) - suite.Equal(2, len(diff)) - printDiff(suite, diff, &runner1, &runner2) -} - -func (suite *SanityTypesTestSuite) TestEntityDiff_Presence() { - runner := ysanity.Runner{} - runner.Runner2.SomeLeaf = "some-leaf" - types.SetPresenceFlag(&runner.Runner2) - types.SetAllParents(&runner) - - entDict1 := types.EntityToDict(&runner) - suite.Equal(len(entDict1), 2) - printDictionary("-LEFT", entDict1) - - emptyRunner := ysanity.Runner{} - types.SetAllParents(&emptyRunner) - entDict2 := types.EntityToDict(&emptyRunner) - suite.Equal(len(entDict2), 0) - printDictionary("-RIGHT", entDict2); - - diff := types.EntityDiff(&runner, &emptyRunner) - suite.Equal(1, len(diff)) - printDiff(suite, diff, &runner, &emptyRunner) -} - -func (suite *SanityTypesTestSuite) TestEntityDiff_TwoListPos() { - runner1 := ysanity.Runner{} - elem1 := ysanity.Runner_TwoList_Ldata{} - elem2 := ysanity.Runner_TwoList_Ldata{} - elem1.Number = 1 - elem1.Name = "runner:twolist:ldata:1" - elem2.Number = 2 - elem2.Name = "runner:twolist:ldata:2" - - elem11 := ysanity.Runner_TwoList_Ldata_Subl1{} - elem12 := ysanity.Runner_TwoList_Ldata_Subl1{} - elem11.Number = 11 - elem11.Name = "runner:twolist:ldata:1:subl1:11" - elem12.Number = 12 - elem12.Name = "runner:twolist:ldata:1:subl1:12" - - elem1.Subl1 = append(elem1.Subl1, &elem11) - elem1.Subl1 = append(elem1.Subl1, &elem12) - - elem21 := ysanity.Runner_TwoList_Ldata_Subl1{} - elem22 := ysanity.Runner_TwoList_Ldata_Subl1{} - elem21.Number = 21 - elem21.Name = "runner:twolist:ldata:2:subl1:21" - elem22.Number = 22 - elem22.Name = "runner:twolist:ldata:2:subl1:22" - - elem2.Subl1 = append(elem2.Subl1, &elem21) - elem2.Subl1 = append(elem2.Subl1, &elem22) - - runner1.TwoList.Ldata = append(runner1.TwoList.Ldata, &elem1) - runner1.TwoList.Ldata = append(runner1.TwoList.Ldata, &elem2) - types.SetAllParents(&runner1) - - entDict := types.EntityToDict(&runner1) - suite.Equal(12, len(entDict)) - printDictionary("-LEFT", entDict) - - runner2 := ysanity.Runner{} - elem1 = ysanity.Runner_TwoList_Ldata{} - elem1.Number = 1 - elem1.Name = "runner:twolist:ldata:1" - elem11 = ysanity.Runner_TwoList_Ldata_Subl1{} - elem12 = ysanity.Runner_TwoList_Ldata_Subl1{} - elem11.Number = 11 - elem11.Name = "runner:twolist:ldata:1:subl1:11" - elem12.Number = 12 - elem12.Name = "runner:twolist:ldata:1:subl1:12" - - elem1.Subl1 = append(elem1.Subl1, &elem11) - elem1.Subl1 = append(elem1.Subl1, &elem12) - - runner2.TwoList.Ldata = append(runner2.TwoList.Ldata, &elem1) - types.SetAllParents(&runner2) - - entDict = types.EntityToDict(&runner2) - suite.Equal(6, len(entDict)) - printDictionary("-RIGHT", entDict) - - diff := types.EntityDiff(&runner1, &runner2) - suite.Equal(1, len(diff)) - printDiff(suite, diff, &runner1, &runner2) -} - -func (suite *SanityTypesTestSuite) TestEntityDiff_ListNoKeys() { - t1 := ysanity.Runner_NoKeyList{Test: "t1"} - t2 := ysanity.Runner_NoKeyList{Test: "tt"} - t3 := ysanity.Runner_NoKeyList{Test: "t3"} - runner1 := ysanity.Runner{} - runner1.NoKeyList = []*ysanity.Runner_NoKeyList {&t1, &t2, &t3} - - entDict := types.EntityToDict(&runner1) - suite.Equal(6, len(entDict)) - printDictionary("-LEFT", entDict) - - t11 := ysanity.Runner_NoKeyList{Test: "t1"} - t22 := ysanity.Runner_NoKeyList{Test: "t2"} - runner2 := ysanity.Runner{} - runner2.NoKeyList = []*ysanity.Runner_NoKeyList {&t11, &t22} - - entDict = types.EntityToDict(&runner2) - suite.Equal(4, len(entDict)) - printDictionary("-RIGHT", entDict) - - diff := types.EntityDiff(&runner1, &runner2) - suite.Equal(2, len(diff)) - printDiff(suite, diff, &runner1, &runner2) - - diff = types.EntityDiff(&runner2, &runner1) - suite.Equal(2, len(diff)) - printDiff(suite, diff, &runner2, &runner1) -} - -func (suite *SanityTypesTestSuite) TestEntityDiff_OneAugList() { - oneAugList := ysanity.Runner_OneList_OneAugList{} - oneAugList.Enabled = true - - elem1 := ysanity.Runner_OneList_OneAugList_Ldata{} - elem1.Number = 1 - elem1.Name = "elem1" - - elem2 := ysanity.Runner_OneList_OneAugList_Ldata{} - elem2.Number = 2 - elem2.Name = "elem2" - - oneAugList.Ldata = []*ysanity.Runner_OneList_OneAugList_Ldata {&elem1, &elem2} - types.SetAllParents(&oneAugList) - - entDict := types.EntityToDict(&oneAugList) - suite.Equal(5, len(entDict)) - printDictionary("", entDict) -} - -func testParams(testName string, entity types.Entity) types.Entity { - ec := types.EntityToCollection(entity) - if ec == nil { - ydk.YLogDebug(fmt.Sprintf("%s: %s\n", testName, types.EntityToString(entity))) - return entity - } - ydk.YLogDebug(fmt.Sprintf("%s: %s\n", testName, ec.String())) - return ec -} - func TestSanityTypesTestSuite(t *testing.T) { if testing.Verbose() { ydk.EnableLogging(ydk.Debug) diff --git a/sdk/go/core/ydk/types/types.go b/sdk/go/core/ydk/types/types.go index 3cdc80c85..83936e63b 100644 --- a/sdk/go/core/ydk/types/types.go +++ b/sdk/go/core/ydk/types/types.go @@ -253,7 +253,7 @@ func EntityToCollection (e Entity) *EntityCollection { ec := NewEntityCollection() return &ec } - + ec, ok := e.(EntityCollection) if ok { return &ec @@ -271,7 +271,7 @@ func IsEntityCollection (e Entity) bool { if e == nil { return false } - + _, ok := e.(EntityCollection) if ok { return true @@ -280,7 +280,7 @@ func IsEntityCollection (e Entity) bool { if ok { return true } - } + } return false } @@ -372,7 +372,7 @@ func (ec *EntityCollection) SetFilter(filter yfilter.YFilter) { for i:=0; i