VOL-1660 - check return type from KV GET and handle errors
Change-Id: I38a8d0fe6815a0bc4c6b8431d3230e951cca3ccb
diff --git a/Makefile b/Makefile
index 3632dc2..64a8621 100644
--- a/Makefile
+++ b/Makefile
@@ -25,6 +25,7 @@
DOCKER_LABEL_VCS_DIRTY = true
endif
## Docker related
+DOCKER_EXTRA_ARGS ?=
DOCKER_REGISTRY ?=
DOCKER_REPOSITORY ?=
DOCKER_TAG ?= ${VERSION}$(shell [[ ${DOCKER_LABEL_VCS_DIRTY} == "true" ]] && echo "-dirty" || true)
@@ -47,6 +48,7 @@
DOCKER_LABEL_COMMIT_DATE = $(shell git show -s --format=%cd --date=iso-strict HEAD)
DOCKER_BUILD_ARGS ?= \
+ ${DOCKER_EXTRA_ARGS} \
--build-arg org_label_schema_version="${VERSION}" \
--build-arg org_label_schema_vcs_url="${DOCKER_LABEL_VCS_URL}" \
--build-arg org_label_schema_vcs_ref="${DOCKER_LABEL_VCS_REF}" \
diff --git a/ro_core/core/model_proxy_manager.go b/ro_core/core/model_proxy_manager.go
index dec1e45..60295c0 100644
--- a/ro_core/core/model_proxy_manager.go
+++ b/ro_core/core/model_proxy_manager.go
@@ -54,6 +54,8 @@
return commonTypes[t-1]
}
+const MultipleValuesMsg = "Expected a single value for KV query for an instance (%s) of type '%s', but received multiple values"
+
// ModelProxyManager controls requests made to the miscellaneous data path agents
type ModelProxyManager struct {
modelProxy map[string]*ModelProxy
@@ -71,19 +73,12 @@
func (mpMgr *ModelProxyManager) GetVoltha(ctx context.Context) (*voltha.Voltha, error) {
log.Debug("GetVoltha")
- var agent *ModelProxy
- var exists bool
-
- if agent, exists = mpMgr.modelProxy[Voltha.String()]; !exists {
- agent = newModelProxy("", mpMgr.clusterDataProxy)
- mpMgr.modelProxy[Voltha.String()] = agent
- }
-
- if instance, _ := agent.Get(); instance != nil {
- return instance.(*voltha.Voltha), nil
- }
-
- return &voltha.Voltha{}, status.Errorf(codes.NotFound, "no-voltha-instance")
+ // TODO: Need to retrieve VOLTHA core information, for now return empty
+ // value
+ return &voltha.Voltha{
+ // TODO: hardcoded for now until ldflags are supported
+ Version: "2.1.0-dev",
+ }, nil
}
// ListCoreInstances returns all the core instances known to the system
@@ -118,8 +113,14 @@
}
adapters := &voltha.Adapters{}
- if items, _ := agent.Get(); items != nil {
- for _, item := range items.([]interface{}) {
+ if items, err := agent.Get(); err != nil {
+ return nil, status.Errorf(codes.Internal, err.Error())
+ } else if items != nil {
+ list, ok := items.([]interface{})
+ if !ok {
+ list = []interface{}{items}
+ }
+ for _, item := range list {
adapter = item.(*voltha.Adapter)
if adapter.Id != SENTINEL_ADAPTER_ID { // don't report the sentinel
adapters.Items = append(adapters.Items, adapter)
@@ -145,8 +146,14 @@
}
deviceTypes := &voltha.DeviceTypes{}
- if items, _ := agent.Get(); items != nil {
- for _, item := range items.([]interface{}) {
+ if items, err := agent.Get(); err != nil {
+ return nil, status.Errorf(codes.Internal, err.Error())
+ } else if items != nil {
+ list, ok := items.([]interface{})
+ if !ok {
+ list = []interface{}{items}
+ }
+ for _, item := range list {
deviceTypes.Items = append(deviceTypes.Items, item.(*voltha.DeviceType))
}
return deviceTypes, nil
@@ -167,7 +174,14 @@
mpMgr.modelProxy[DeviceTypes.String()] = agent
}
- if deviceType, _ := agent.Get(id); deviceType != nil {
+ if deviceType, err := agent.Get(id); err != nil {
+ return nil, status.Errorf(codes.Internal, err.Error())
+ } else if deviceType != nil {
+ _, ok := deviceType.(*voltha.DeviceType)
+ if !ok {
+ return nil, status.Errorf(codes.Internal, MultipleValuesMsg,
+ id, DeviceTypes.String())
+ }
return deviceType.(*voltha.DeviceType), nil
}
@@ -187,8 +201,14 @@
}
deviceGroups := &voltha.DeviceGroups{}
- if items, _ := agent.Get(); items != nil {
- for _, item := range items.([]interface{}) {
+ if items, err := agent.Get(); err != nil {
+ return nil, status.Errorf(codes.Internal, err.Error())
+ } else if items != nil {
+ list, ok := items.([]interface{})
+ if !ok {
+ list = []interface{}{items}
+ }
+ for _, item := range list {
deviceGroups.Items = append(deviceGroups.Items, item.(*voltha.DeviceGroup))
}
return deviceGroups, nil
@@ -209,7 +229,14 @@
mpMgr.modelProxy[DeviceGroups.String()] = agent
}
- if deviceGroup, _ := agent.Get(id); deviceGroup != nil {
+ if deviceGroup, err := agent.Get(id); err != nil {
+ return nil, status.Errorf(codes.Internal, err.Error())
+ } else if deviceGroup != nil {
+ _, ok := deviceGroup.(*voltha.DeviceGroup)
+ if !ok {
+ return nil, status.Errorf(codes.Internal, MultipleValuesMsg,
+ id, DeviceGroups.String())
+ }
return deviceGroup.(*voltha.DeviceGroup), nil
}
@@ -229,8 +256,14 @@
}
alarmFilters := &voltha.AlarmFilters{}
- if items, _ := agent.Get(); items != nil {
- for _, item := range items.([]interface{}) {
+ if items, err := agent.Get(); err != nil {
+ return nil, status.Errorf(codes.Internal, err.Error())
+ } else if items != nil {
+ list, ok := items.([]interface{})
+ if !ok {
+ list = []interface{}{items}
+ }
+ for _, item := range list {
alarmFilters.Filters = append(alarmFilters.Filters, item.(*voltha.AlarmFilter))
}
return alarmFilters, nil
@@ -251,7 +284,14 @@
mpMgr.modelProxy[AlarmFilters.String()] = agent
}
- if alarmFilter, _ := agent.Get(id); alarmFilter != nil {
+ if alarmFilter, err := agent.Get(id); err != nil {
+ return nil, status.Errorf(codes.Internal, err.Error())
+ } else if alarmFilter != nil {
+ _, ok := alarmFilter.(*voltha.AlarmFilter)
+ if !ok {
+ return nil, status.Errorf(codes.Internal, MultipleValuesMsg,
+ id, AlarmFilters.String())
+ }
return alarmFilter.(*voltha.AlarmFilter), nil
}
return &voltha.AlarmFilter{}, status.Errorf(codes.NotFound, "alarm-filter-%s", id)