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..03d2717f 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,33 @@ 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 + "]" + 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 + "]" + } + } + + 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) + + 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. */