Skip to content

Commit

Permalink
feat: update profile cache to reflect new profile model
Browse files Browse the repository at this point in the history
device profile model has been updated in edgexfoundry/go-mod-core-contracts#540
update the profile cache for consistent and better usage.

Signed-off-by: Chris Hung <[email protected]>
  • Loading branch information
Chris Hung committed Apr 6, 2021
1 parent 34bcbad commit 7dcc612
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 133 deletions.
140 changes: 45 additions & 95 deletions internal/cache/profiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,10 @@ package cache

import (
"fmt"
"strings"
"sync"

"github.com/edgexfoundry/go-mod-core-contracts/v2/errors"
"github.com/edgexfoundry/go-mod-core-contracts/v2/v2"
"github.com/edgexfoundry/go-mod-core-contracts/v2/v2/models"

"github.com/edgexfoundry/device-sdk-go/v2/internal/common"
)

var (
Expand All @@ -29,36 +25,33 @@ type ProfileCache interface {
Update(profile models.DeviceProfile) errors.EdgeX
RemoveByName(name string) errors.EdgeX
DeviceResource(profileName string, resourceName string) (models.DeviceResource, bool)
CommandExists(profileName string, cmd string, method string) (bool, errors.EdgeX)
ResourceOperations(profileName string, cmd string, method string) ([]models.ResourceOperation, errors.EdgeX)
ResourceOperation(profileName string, deviceResource string, method string) (models.ResourceOperation, errors.EdgeX)
DeviceCommand(profileName string, commandName string) (models.DeviceCommand, bool)
CommandExists(profileName string, cmd string) (bool, errors.EdgeX)
ResourceOperation(profileName string, deviceResource string) (models.ResourceOperation, errors.EdgeX)
}

type profileCache struct {
deviceProfileMap map[string]*models.DeviceProfile // key is DeviceProfile name
deviceResourceMap map[string]map[string]models.DeviceResource
getResourceOperationsMap map[string]map[string][]models.ResourceOperation
setResourceOperationsMap map[string]map[string][]models.ResourceOperation
mutex sync.RWMutex
deviceProfileMap map[string]*models.DeviceProfile // key is DeviceProfile name
deviceResourceMap map[string]map[string]models.DeviceResource
deviceCommandMap map[string]map[string]models.DeviceCommand
mutex sync.RWMutex
}

func newProfileCache(profiles []models.DeviceProfile) ProfileCache {
defaultSize := len(profiles)
dpMap := make(map[string]*models.DeviceProfile, defaultSize)
drMap := make(map[string]map[string]models.DeviceResource, defaultSize)
getRoMap := make(map[string]map[string][]models.ResourceOperation, defaultSize)
setRoMap := make(map[string]map[string][]models.ResourceOperation, defaultSize)
dcMap := make(map[string]map[string]models.DeviceCommand, defaultSize)
for _, dp := range profiles {
dpMap[dp.Name] = &dp
drMap[dp.Name] = deviceResourceSliceToMap(dp.DeviceResources)
getRoMap[dp.Name], setRoMap[dp.Name] = deviceCommandSliceToMap(dp.DeviceCommands)
dcMap[dp.Name] = deviceCommandSliceToMap(dp.DeviceCommands)
}

pc = &profileCache{
deviceProfileMap: dpMap,
deviceResourceMap: drMap,
getResourceOperationsMap: getRoMap,
setResourceOperationsMap: setRoMap,
deviceProfileMap: dpMap,
deviceResourceMap: drMap,
deviceCommandMap: dcMap,
}
return pc
}
Expand Down Expand Up @@ -106,7 +99,7 @@ func (p *profileCache) add(profile models.DeviceProfile) errors.EdgeX {

p.deviceProfileMap[profile.Name] = &profile
p.deviceResourceMap[profile.Name] = deviceResourceSliceToMap(profile.DeviceResources)
p.getResourceOperationsMap[profile.Name], p.setResourceOperationsMap[profile.Name] = deviceCommandSliceToMap(profile.DeviceCommands)
p.deviceCommandMap[profile.Name] = deviceCommandSliceToMap(profile.DeviceCommands)
return nil
}

Expand All @@ -119,19 +112,13 @@ func deviceResourceSliceToMap(deviceResources []models.DeviceResource) map[strin
return result
}

func deviceCommandSliceToMap(deviceCommands []models.DeviceCommand) (map[string][]models.ResourceOperation, map[string][]models.ResourceOperation) {
getResult := make(map[string][]models.ResourceOperation, len(deviceCommands))
setResult := make(map[string][]models.ResourceOperation, len(deviceCommands))
for _, deviceCommand := range deviceCommands {
if strings.Contains(deviceCommand.ReadWrite, v2.ReadWrite_R) {
getResult[deviceCommand.Name] = deviceCommand.ResourceOperations
}
if strings.Contains(deviceCommand.ReadWrite, v2.ReadWrite_W) {
setResult[deviceCommand.Name] = deviceCommand.ResourceOperations
}
func deviceCommandSliceToMap(deviceCommands []models.DeviceCommand) map[string]models.DeviceCommand {
result := make(map[string]models.DeviceCommand, len(deviceCommands))
for _, dc := range deviceCommands {
result[dc.Name] = dc
}

return getResult, setResult
return result
}

// Update updates the profile in the cache
Expand Down Expand Up @@ -163,8 +150,7 @@ func (p *profileCache) removeByName(name string) errors.EdgeX {

delete(p.deviceProfileMap, name)
delete(p.deviceResourceMap, name)
delete(p.getResourceOperationsMap, name)
delete(p.setResourceOperationsMap, name)
delete(p.deviceCommandMap, name)
return nil
}

Expand All @@ -182,79 +168,61 @@ func (p *profileCache) DeviceResource(profileName string, resourceName string) (
return dr, ok
}

// CommandExists returns a bool indicating whether the specified command exists for the
// specified (by name) profile. If the specified profile doesn't exist, an error is returned.
func (p *profileCache) CommandExists(profileName string, cmd string, method string) (bool, errors.EdgeX) {
// DeviceCommand returns the DeviceCommand with given profileName and commandName
func (p *profileCache) DeviceCommand(profileName string, commandName string) (models.DeviceCommand, bool) {
p.mutex.RLock()
defer p.mutex.RUnlock()

_, ok := p.deviceProfileMap[profileName]
dcs, ok := p.deviceCommandMap[profileName]
if !ok {
errMsg := fmt.Sprintf("failed to find Profile %s in cache", profileName)
return false, errors.NewCommonEdgeX(errors.KindEntityDoesNotExist, errMsg, nil)
}
// Check whether cmd exists in deviceCommands.
var deviceCommands map[string][]models.ResourceOperation
if strings.ToLower(method) == common.GetCmdMethod {
deviceCommands, _ = p.getResourceOperationsMap[profileName]
} else {
deviceCommands, _ = p.setResourceOperationsMap[profileName]
}

if _, ok := deviceCommands[cmd]; !ok {
return false, nil
return models.DeviceCommand{}, ok
}

return true, nil
dc, ok := dcs[commandName]
return dc, ok
}

// ResourceOperations returns the ResourceOperations with given command and method.
func (p *profileCache) ResourceOperations(profileName string, cmd string, method string) ([]models.ResourceOperation, errors.EdgeX) {
// CommandExists returns a bool indicating whether the specified command exists for the
// specified (by name) profile. If the specified profile doesn't exist, an error is returned.
func (p *profileCache) CommandExists(profileName string, cmd string) (bool, errors.EdgeX) {
p.mutex.RLock()
defer p.mutex.RUnlock()

if err := p.verifyProfileExists(profileName); err != nil {
return nil, err
}

rosMap, err := p.verifyResourceOperationsExists(method, profileName)
if err != nil {
return nil, err
return false, errors.NewCommonEdgeXWrapper(err)
}

var ok bool
var ros []models.ResourceOperation
if ros, ok = rosMap[cmd]; !ok {
errMsg := fmt.Sprintf("failed to find DeviceCommand %s in Profile %s", cmd, profileName)
return nil, errors.NewCommonEdgeX(errors.KindEntityDoesNotExist, errMsg, nil)
// Check whether cmd exists in deviceCommands.
var deviceCommands map[string]models.DeviceCommand
deviceCommands, ok := p.deviceCommandMap[profileName]
if ok {
if _, ok := deviceCommands[cmd]; ok {
return true, nil
}
return false, nil
}

return ros, nil
return false, nil
}

// ResourceOperation returns the first matched ResourceOperation with given deviceResource and method
func (p *profileCache) ResourceOperation(profileName string, deviceResource string, method string) (models.ResourceOperation, errors.EdgeX) {
// ResourceOperation returns the first matched ResourceOperation with given resourceName
func (p *profileCache) ResourceOperation(profileName string, resourceName string) (models.ResourceOperation, errors.EdgeX) {
p.mutex.RLock()
defer p.mutex.RUnlock()

if err := p.verifyProfileExists(profileName); err != nil {
return models.ResourceOperation{}, err
}

rosMap, err := p.verifyResourceOperationsExists(method, profileName)
if err != nil {
return models.ResourceOperation{}, err
}

for _, ros := range rosMap {
for _, ro := range ros {
if ro.DeviceResource == deviceResource {
deviceCommandMap := p.deviceCommandMap[profileName]
for _, dc := range deviceCommandMap {
for _, ro := range dc.ResourceOperations {
if ro.DeviceResource == resourceName {
return ro, nil
}
}
}

errMsg := fmt.Sprintf("failed to find ResourceOpertaion with DeviceResource %s in Profile %s", deviceResource, profileName)
errMsg := fmt.Sprintf("failed to find ResourceOpertaion with DeviceResource %s in Profile %s", resourceName, profileName)
return models.ResourceOperation{}, errors.NewCommonEdgeX(errors.KindEntityDoesNotExist, errMsg, nil)
}

Expand All @@ -266,24 +234,6 @@ func (p *profileCache) verifyProfileExists(profileName string) errors.EdgeX {
return nil
}

func (p *profileCache) verifyResourceOperationsExists(method string, profileName string) (map[string][]models.ResourceOperation, errors.EdgeX) {
var ok bool
var rosMap map[string][]models.ResourceOperation

if strings.ToLower(method) == common.GetCmdMethod {
rosMap, ok = p.getResourceOperationsMap[profileName]
} else if strings.ToLower(method) == common.SetCmdMethod {
rosMap, ok = p.setResourceOperationsMap[profileName]
}

if !ok {
errMsg := fmt.Sprintf("failed to find %s ResourceOperations in Profile %s", method, profileName)
return rosMap, errors.NewCommonEdgeX(errors.KindEntityDoesNotExist, errMsg, nil)
}

return rosMap, nil
}

func Profiles() ProfileCache {
return pc
}
64 changes: 26 additions & 38 deletions internal/cache/profiles_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,56 +141,46 @@ func Test_profileCache_DeviceResource(t *testing.T) {
}
}

func Test_profileCache_CommandExists(t *testing.T) {
func Test_profileCache_DeviceCommand(t *testing.T) {
newProfileCache([]models.DeviceProfile{testProfile})

tests := []struct {
name string
profile string
cmd string
method string
expected bool
name string
profileName string
commandName string
deviceCommand models.DeviceCommand
expected bool
}{
{"Invalid - nonexistent Profile name", "nil", TestDeviceCommand, "GET", false},
{"Invalid - nonexistent Command name", TestProfile, "nil", "GET", false},
{"Invalid - invalid method", TestProfile, TestDeviceCommand, "INVALID", false},
{"Invalid - nonexistent method", TestProfile, TestDeviceCommand, "SET", false},
{"Valid", TestProfile, TestDeviceCommand, "gEt", true},
{"Invalid - nonexistent Profile name", "nil", TestDeviceCommand, models.DeviceCommand{}, false},
{"Invalid - nonexistent Command name", TestProfile, "nil", models.DeviceCommand{}, false},
{"Valid", TestProfile, TestDeviceCommand, testProfile.DeviceCommands[0], true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ok, _ := pc.CommandExists(tt.profile, tt.cmd, tt.method)
assert.Equal(t, ok, tt.expected)
res, ok := pc.DeviceCommand(tt.profileName, tt.commandName)
assert.Equal(t, res, tt.deviceCommand, "DeviceResource returns wrong deviceResource")
assert.Equal(t, ok, tt.expected, "DeviceResource returns opposite result")
})
}
}

func Test_profileCache_ResourceOperations(t *testing.T) {
func Test_profileCache_CommandExists(t *testing.T) {
newProfileCache([]models.DeviceProfile{testProfile})

tests := []struct {
name string
profile string
cmd string
method string
res []models.ResourceOperation
expectedError bool
name string
profile string
cmd string
expected bool
}{
{"Invalid - nonexistent Profile name", "nil", TestDeviceCommand, "GET", nil, true},
{"Invalid - nonexistent Command name", TestProfile, "nil", "GET", nil, true},
{"Invalid - invalid method", TestProfile, TestDeviceCommand, "INVALID", nil, true},
{"Valid", TestProfile, TestDeviceCommand, "GET", testProfile.DeviceCommands[0].ResourceOperations, false},
{"Invalid - nonexistent Profile name", "nil", TestDeviceCommand, false},
{"Invalid - nonexistent Command name", TestProfile, "nil", false},
{"Valid", TestProfile, TestDeviceCommand, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
res, err := pc.ResourceOperations(tt.profile, tt.cmd, tt.method)
if tt.expectedError {
assert.NotNil(t, err)
assert.Nil(t, res)
} else {
assert.Nil(t, err)
assert.Equal(t, res, tt.res)
}
ok, _ := pc.CommandExists(tt.profile, tt.cmd)
assert.Equal(t, ok, tt.expected)
})
}
}
Expand All @@ -202,18 +192,16 @@ func Test_profileCache_ResourceOperation(t *testing.T) {
name string
profile string
resource string
method string
res models.ResourceOperation
expectedError bool
}{
{"Invalid - nonexistent Profile name", "nil", TestDeviceResource, "GET", models.ResourceOperation{}, true},
{"Invalid - nonexistent DeviceResource name", TestProfile, "nil", "GET", models.ResourceOperation{}, true},
{"Invalid - invalid method", TestProfile, TestDeviceResource, "INVALID", models.ResourceOperation{}, true},
{"Valid", TestProfile, TestDeviceResource, "Get", testProfile.DeviceCommands[0].ResourceOperations[0], false},
{"Invalid - nonexistent Profile name", "nil", TestDeviceResource, models.ResourceOperation{}, true},
{"Invalid - nonexistent DeviceResource name", TestProfile, "nil", models.ResourceOperation{}, true},
{"Valid", TestProfile, TestDeviceResource, testProfile.DeviceCommands[0].ResourceOperations[0], false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ro, err := pc.ResourceOperation(tt.profile, tt.resource, tt.method)
ro, err := pc.ResourceOperation(tt.profile, tt.resource)
if tt.expectedError {
assert.NotNil(t, err)
assert.Equal(t, ro, tt.res)
Expand Down

0 comments on commit 7dcc612

Please sign in to comment.