diff --git a/pkg/northbound/gnmi/set.go b/pkg/northbound/gnmi/set.go index 5ff299852..74675044f 100644 --- a/pkg/northbound/gnmi/set.go +++ b/pkg/northbound/gnmi/set.go @@ -282,7 +282,7 @@ func (s *Server) formatUpdateOrReplace(prefix *gnmi.Path, u *gnmi.Update, updates[cv.Path] = cv.GetValue() } } else { - rwPathElem, err := findPathFromModel(path, rwPaths) + rwPathElem, err := findPathFromModel(path, rwPaths, true) if err != nil { return nil, err } @@ -314,7 +314,7 @@ func (s *Server) doDelete(prefix *gnmi.Path, u *gnmi.Path, path = fmt.Sprintf("%s%s", prefixPath, path) } // Checks for read only paths - _, err := findPathFromModel(path, rwPaths) + _, err := findPathFromModel(path, rwPaths, false) if err != nil { return nil, err } @@ -380,7 +380,7 @@ func extractModelForTarget(target devicetype.ID, return rwPaths, nil } -func findPathFromModel(path string, rwPaths modelregistry.ReadWritePathMap) (*modelregistry.ReadWritePathElem, error) { +func findPathFromModel(path string, rwPaths modelregistry.ReadWritePathMap, exact bool) (*modelregistry.ReadWritePathElem, error) { searchpathNoIndices := modelregistry.RemovePathIndices(path) if strings.HasSuffix(path, "]") { //Ends with index indices, _ := modelregistry.ExtractIndexNames(path) @@ -392,8 +392,10 @@ func findPathFromModel(path string, rwPaths modelregistry.ReadWritePathMap) (*mo for modelPath, modelElem := range rwPaths { pathNoIndices := modelregistry.RemovePathIndices(modelPath) // Find a short path - if pathNoIndices == searchpathNoIndices { + if exact && pathNoIndices == searchpathNoIndices { return &modelElem, nil + } else if !exact && strings.HasPrefix(pathNoIndices, searchpathNoIndices) { + return &modelElem, nil // returns the first thing it finds that matches the prefix } } diff --git a/pkg/northbound/gnmi/set_test.go b/pkg/northbound/gnmi/set_test.go index 3aaa86458..ae69d7a98 100644 --- a/pkg/northbound/gnmi/set_test.go +++ b/pkg/northbound/gnmi/set_test.go @@ -661,3 +661,20 @@ func TestSet_BadDeviceType(t *testing.T) { assert.Contains(t, setError.Error(), "target DeviceWithMultipleVersions type given NotTheSameType does not match expected TestDevice") assert.Nil(t, setResponse) } + +func Test_findPathFromModel(t *testing.T) { + _, _, mgr := setUpForGetSetTests(t) + + rwPath, err := findPathFromModel("/cont1a", mgr.ModelRegistry.ModelReadWritePaths["TestDevice-1.0.0"], false) + assert.NoError(t, err) + assert.NotNil(t, rwPath) + + rwPath2, err := findPathFromModel("/cont1a/list2a", mgr.ModelRegistry.ModelReadWritePaths["TestDevice-1.0.0"], false) + assert.NoError(t, err) + assert.NotNil(t, rwPath2) + + rwPath3, err := findPathFromModel("/cont1a/list2a[name=123]/name", mgr.ModelRegistry.ModelReadWritePaths["TestDevice-1.0.0"], true) + assert.NoError(t, err) + assert.NotNil(t, rwPath3) + assert.Equal(t, []string{"4..8"}, rwPath3.Length) +}