From a9471063ca47ff3d376e11d824f092714f234f02 Mon Sep 17 00:00:00 2001 From: pettershao-ragilenetworks Date: Tue, 25 Oct 2022 16:39:19 +0800 Subject: [PATCH 1/2] client add num for key and the server sort keys by num --- Makefile | 1 + patches/xpath.patch | 34 ++++++++++++++++++++++++++++++++++ transl_utils/transl_utils.go | 34 +++++++++++++++++++++++++++++++--- 3 files changed, 66 insertions(+), 3 deletions(-) create mode 100644 patches/xpath.patch diff --git a/Makefile b/Makefile index 2813f3f6..1d65bc75 100644 --- a/Makefile +++ b/Makefile @@ -41,6 +41,7 @@ $(GO_DEPS): go.mod $(PATCHES) patch -d vendor -p0 < patches/gnmi_cli.all.patch patch -d vendor -p0 < patches/gnmi_set.patch patch -d vendor -p0 < patches/gnmi_get.patch + patch -d vendor -p0 < patches/xpath.patch git apply patches/0001-Updated-to-filter-and-write-to-file.patch touch $@ diff --git a/patches/xpath.patch b/patches/xpath.patch new file mode 100644 index 00000000..68bce9f2 --- /dev/null +++ b/patches/xpath.patch @@ -0,0 +1,34 @@ +--- ./github.com/jipanyang/gnxi/utils/xpath/xpath.go 2022-10-25 16:16:00.239783329 +0800 ++++ ./github.com/jipanyang/gnxi/utils/xpath/xpath.go 2022-10-25 15:56:18.336168598 +0800 +@@ -20,6 +20,7 @@ + "fmt" + "regexp" + "strings" ++ "strconv" + ) + + var ( +@@ -135,9 +136,10 @@ + // For example, given + // "[k1=v1][k2=v2]", + // this API returns +-// map[string]string{"k1": "v1", "k2": "v2"}. ++// map[string]string{"[0]k1": "v1", "[1]k2": "v2"}. + func parseKeyValueString(str string) (map[string]string, error) { + keyValuePairs := make(map[string]string) ++ var num int = 0 + // begin marks the beginning of a key-value pair. + begin := 0 + // end marks the end of a key-value pair. +@@ -170,7 +172,10 @@ + // Recover escaped '[' and ']'. + val = strings.Replace(val, `\]`, `]`, -1) + val = strings.Replace(val, `\[`, `[`, -1) +- keyValuePairs[key] = val ++// keyValuePairs[key] = val ++ key_with_num := "[" + strconv.Itoa(num) + "]" + key ++ keyValuePairs[key_with_num] = val ++ num++ + begin = end + 1 + } + end++ diff --git a/transl_utils/transl_utils.go b/transl_utils/transl_utils.go index b6b33857..c798f47c 100644 --- a/transl_utils/transl_utils.go +++ b/transl_utils/transl_utils.go @@ -12,7 +12,8 @@ import ( "context" "log/syslog" "github.com/Azure/sonic-mgmt-common/translib/tlerr" - + "strconv" + "sort" ) var ( @@ -80,6 +81,9 @@ func ConvertToURI(prefix *gnmipb.Path, path *gnmipb.Path, req *string) error { elems := fullPath.GetElem() *req = "/" + num_key_map := make(map[int]string) + key_val_map := make(map[string]string) + if elems != nil { /* Iterate through elements. */ for i, elem := range elems { @@ -94,8 +98,32 @@ func ConvertToURI(prefix *gnmipb.Path, path *gnmipb.Path, req *string) error { /* If keys are present , process the keys. */ if key != nil { for k, v := range key { - log.V(6).Infof("elem : %#v %#v", k, v) - *req += "[" + k + "=" + v + "]" + //log.V(6).Infof("elem : %#v %#v", k, v) + //*req += "[" + k + "=" + v + "]" + num_str := k[strings.Index(k, "[") + 1 : strings.Index(k, "]")] + log.V(6).Infof("num : %#v", num_str) + key_del_num := k[strings.Index(k, "]") + 1 : len(k)] + log.V(6).Infof("elem : %#v %#v", key_del_num, v) + num, err := strconv.Atoi(num_str) + if err != nil { + return err + } + num_key_map[num] = key_del_num + key_val_map[key_del_num] = v + } + + log.V(6).Infof("num_key_map : %#v", num_key_map) + log.V(6).Infof("key_val_map : %#v", key_val_map) + + if num_key_map != nil { + var num_list []int + for num_key_map_k := range num_key_map { + num_list = append(num_list, num_key_map_k) + } + sort.Ints(num_list) + for _, num_list_v := range num_list { + *req += "[" + num_key_map[num_list_v] + "=" + key_val_map[num_key_map[num_list_v]] + "]" + } } /* Append "/" after all keys are processed. */ From 14d8130a494dafea38cbab0b8975a56f851876d7 Mon Sep 17 00:00:00 2001 From: pettershao-ragilenetworks <81281940+pettershao-ragilenetworks@users.noreply.github.com> Date: Wed, 26 Oct 2022 14:48:01 +0800 Subject: [PATCH 2/2] add key without tag for test module,suggestion fix --- transl_utils/transl_utils.go | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/transl_utils/transl_utils.go b/transl_utils/transl_utils.go index c798f47c..03d2717f 100644 --- a/transl_utils/transl_utils.go +++ b/transl_utils/transl_utils.go @@ -98,24 +98,25 @@ func ConvertToURI(prefix *gnmipb.Path, path *gnmipb.Path, req *string) error { /* If keys are present , process the keys. */ if key != nil { for k, v := range key { - //log.V(6).Infof("elem : %#v %#v", k, v) - //*req += "[" + k + "=" + v + "]" - num_str := k[strings.Index(k, "[") + 1 : strings.Index(k, "]")] - log.V(6).Infof("num : %#v", num_str) - key_del_num := k[strings.Index(k, "]") + 1 : len(k)] - log.V(6).Infof("elem : %#v %#v", key_del_num, v) - num, err := strconv.Atoi(num_str) - if err != nil { - return err + if strings.Contains(k, "[") && strings.Contains(k, "]") { + num_str := k[strings.Index(k, "[") + 1 : strings.Index(k, "]")] + key_del_num := k[strings.Index(k, "]") + 1 : len(k)] + num, err := strconv.Atoi(num_str) + if err != nil { + return err + } + num_key_map[num] = key_del_num + key_val_map[key_del_num] = v + } else { + log.V(6).Infof("elem : %#v %#v", k, v) + *req += "[" + k + "=" + v + "]" } - num_key_map[num] = key_del_num - key_val_map[key_del_num] = v } - log.V(6).Infof("num_key_map : %#v", num_key_map) - log.V(6).Infof("key_val_map : %#v", key_val_map) + if len(num_key_map) != 0 { + log.V(6).Infof("num_key_map : %#v", num_key_map) + log.V(6).Infof("key_val_map : %#v", key_val_map) - if num_key_map != nil { var num_list []int for num_key_map_k := range num_key_map { num_list = append(num_list, num_key_map_k)