Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes #issue 42. add num for each key and sort keys by num. make sure keys in map can be sorted. #50

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 $@

Expand Down
34 changes: 34 additions & 0 deletions patches/xpath.patch
Original file line number Diff line number Diff line change
@@ -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"}.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about compatibility?
If we add num for each key, other GNMI client can't communicate with SONiC telemetry?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The scope of this numbering within parameters of a path element.

@pettershao-ragilenetworks -- please correct my statement if needed.

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)
Copy link
Contributor

@renukamanavalan renukamanavalan Oct 25, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good find!
I meant the patch that orders the keys as present in path.

- keyValuePairs[key] = val
+// keyValuePairs[key] = val
+ key_with_num := "[" + strconv.Itoa(num) + "]" + key
+ keyValuePairs[key_with_num] = val
+ num++
begin = end + 1
}
end++
35 changes: 32 additions & 3 deletions transl_utils/transl_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import (
"context"
"log/syslog"
"github.com/Azure/sonic-mgmt-common/translib/tlerr"

"strconv"
"sort"
)

var (
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be already sorted?
xPath patch uses it in sequence.

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. */
Expand Down