Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019-present Open Networking Foundation |
| 3 | |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package core |
| 17 | |
| 18 | import ( |
| 19 | "context" |
| 20 | "github.com/opencord/voltha-go/common/log" |
| 21 | "github.com/opencord/voltha-go/db/model" |
William Kurkian | daa6bb2 | 2019-03-07 12:26:28 -0500 | [diff] [blame] | 22 | "github.com/opencord/voltha-protos/go/voltha" |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 23 | "google.golang.org/grpc/codes" |
| 24 | "google.golang.org/grpc/status" |
| 25 | ) |
| 26 | |
| 27 | // Enumerated type to keep track of miscellaneous data path agents |
| 28 | type DataModelType int |
| 29 | |
| 30 | // Enumerated list of data path agents |
| 31 | const ( |
| 32 | Adapters DataModelType = 1 + iota |
| 33 | AlarmFilters |
| 34 | CoreInstances |
| 35 | DeviceTypes |
| 36 | DeviceGroups |
| 37 | Voltha |
| 38 | ) |
| 39 | |
David K. Bainbridge | be88396 | 2019-05-22 14:49:12 -0700 | [diff] [blame] | 40 | const SENTINEL_ADAPTER_ID = "adapter_sentinel" |
| 41 | |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 42 | // String equivalent for data path agents |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 43 | var commonTypes = []string{ |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 44 | "Adapters", |
| 45 | "AlarmFilters", |
| 46 | "CoreInstances", |
| 47 | "DeviceTypes", |
| 48 | "DeviceGroups", |
| 49 | "Voltha", |
| 50 | } |
| 51 | |
| 52 | // String converts the enumerated data path agent value to its string equivalent |
| 53 | func (t DataModelType) String() string { |
| 54 | return commonTypes[t-1] |
| 55 | } |
| 56 | |
David K. Bainbridge | e14914d | 2019-05-24 13:43:05 -0700 | [diff] [blame^] | 57 | const MultipleValuesMsg = "Expected a single value for KV query for an instance (%s) of type '%s', but received multiple values" |
| 58 | |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 59 | // ModelProxyManager controls requests made to the miscellaneous data path agents |
| 60 | type ModelProxyManager struct { |
| 61 | modelProxy map[string]*ModelProxy |
| 62 | clusterDataProxy *model.Proxy |
| 63 | } |
| 64 | |
| 65 | func newModelProxyManager(cdProxy *model.Proxy) *ModelProxyManager { |
| 66 | var mgr ModelProxyManager |
| 67 | mgr.modelProxy = make(map[string]*ModelProxy) |
| 68 | mgr.clusterDataProxy = cdProxy |
| 69 | return &mgr |
| 70 | } |
| 71 | |
| 72 | // GetDeviceType returns the device type associated to the provided id |
| 73 | func (mpMgr *ModelProxyManager) GetVoltha(ctx context.Context) (*voltha.Voltha, error) { |
| 74 | log.Debug("GetVoltha") |
| 75 | |
David K. Bainbridge | e14914d | 2019-05-24 13:43:05 -0700 | [diff] [blame^] | 76 | // TODO: Need to retrieve VOLTHA core information, for now return empty |
| 77 | // value |
| 78 | return &voltha.Voltha{ |
| 79 | // TODO: hardcoded for now until ldflags are supported |
| 80 | Version: "2.1.0-dev", |
| 81 | }, nil |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | // ListCoreInstances returns all the core instances known to the system |
| 85 | func (mpMgr *ModelProxyManager) ListCoreInstances(ctx context.Context) (*voltha.CoreInstances, error) { |
| 86 | log.Debug("ListCoreInstances") |
| 87 | |
| 88 | // TODO: Need to retrieve the list of registered cores |
| 89 | |
| 90 | return &voltha.CoreInstances{}, status.Errorf(codes.NotFound, "no-core-instances") |
| 91 | } |
| 92 | |
| 93 | // GetCoreInstance returns the core instance associated to the provided id |
| 94 | func (mpMgr *ModelProxyManager) GetCoreInstance(ctx context.Context, id string) (*voltha.CoreInstance, error) { |
| 95 | log.Debugw("GetCoreInstance", log.Fields{"id": id}) |
| 96 | |
| 97 | // TODO: Need to retrieve the list of registered cores |
| 98 | |
| 99 | return &voltha.CoreInstance{}, status.Errorf(codes.NotFound, "core-instance-%s", id) |
| 100 | } |
| 101 | |
| 102 | // ListAdapters returns all the device types known to the system |
| 103 | func (mpMgr *ModelProxyManager) ListAdapters(ctx context.Context) (*voltha.Adapters, error) { |
| 104 | log.Debug("ListAdapters") |
| 105 | |
| 106 | var agent *ModelProxy |
| 107 | var exists bool |
David K. Bainbridge | be88396 | 2019-05-22 14:49:12 -0700 | [diff] [blame] | 108 | var adapter *voltha.Adapter |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 109 | |
| 110 | if agent, exists = mpMgr.modelProxy[Adapters.String()]; !exists { |
| 111 | agent = newModelProxy("adapters", mpMgr.clusterDataProxy) |
| 112 | mpMgr.modelProxy[Adapters.String()] = agent |
| 113 | } |
| 114 | |
| 115 | adapters := &voltha.Adapters{} |
David K. Bainbridge | e14914d | 2019-05-24 13:43:05 -0700 | [diff] [blame^] | 116 | if items, err := agent.Get(); err != nil { |
| 117 | return nil, status.Errorf(codes.Internal, err.Error()) |
| 118 | } else if items != nil { |
| 119 | list, ok := items.([]interface{}) |
| 120 | if !ok { |
| 121 | list = []interface{}{items} |
| 122 | } |
| 123 | for _, item := range list { |
David K. Bainbridge | be88396 | 2019-05-22 14:49:12 -0700 | [diff] [blame] | 124 | adapter = item.(*voltha.Adapter) |
| 125 | if adapter.Id != SENTINEL_ADAPTER_ID { // don't report the sentinel |
| 126 | adapters.Items = append(adapters.Items, adapter) |
| 127 | } |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 128 | } |
| 129 | log.Debugw("retrieved-adapters", log.Fields{"adapters": adapters}) |
| 130 | return adapters, nil |
| 131 | } |
| 132 | |
| 133 | return adapters, status.Errorf(codes.NotFound, "no-adapters") |
| 134 | } |
| 135 | |
| 136 | // ListDeviceTypes returns all the device types known to the system |
| 137 | func (mpMgr *ModelProxyManager) ListDeviceTypes(ctx context.Context) (*voltha.DeviceTypes, error) { |
| 138 | log.Debug("ListDeviceTypes") |
| 139 | |
| 140 | var agent *ModelProxy |
| 141 | var exists bool |
| 142 | |
| 143 | if agent, exists = mpMgr.modelProxy[DeviceTypes.String()]; !exists { |
| 144 | agent = newModelProxy("device_types", mpMgr.clusterDataProxy) |
| 145 | mpMgr.modelProxy[DeviceTypes.String()] = agent |
| 146 | } |
| 147 | |
| 148 | deviceTypes := &voltha.DeviceTypes{} |
David K. Bainbridge | e14914d | 2019-05-24 13:43:05 -0700 | [diff] [blame^] | 149 | if items, err := agent.Get(); err != nil { |
| 150 | return nil, status.Errorf(codes.Internal, err.Error()) |
| 151 | } else if items != nil { |
| 152 | list, ok := items.([]interface{}) |
| 153 | if !ok { |
| 154 | list = []interface{}{items} |
| 155 | } |
| 156 | for _, item := range list { |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 157 | deviceTypes.Items = append(deviceTypes.Items, item.(*voltha.DeviceType)) |
| 158 | } |
| 159 | return deviceTypes, nil |
| 160 | } |
| 161 | |
| 162 | return deviceTypes, status.Errorf(codes.NotFound, "no-device-types") |
| 163 | } |
| 164 | |
| 165 | // GetDeviceType returns the device type associated to the provided id |
| 166 | func (mpMgr *ModelProxyManager) GetDeviceType(ctx context.Context, id string) (*voltha.DeviceType, error) { |
| 167 | log.Debugw("GetDeviceType", log.Fields{"id": id}) |
| 168 | |
| 169 | var agent *ModelProxy |
| 170 | var exists bool |
| 171 | |
| 172 | if agent, exists = mpMgr.modelProxy[DeviceTypes.String()]; !exists { |
| 173 | agent = newModelProxy("device_types", mpMgr.clusterDataProxy) |
| 174 | mpMgr.modelProxy[DeviceTypes.String()] = agent |
| 175 | } |
| 176 | |
David K. Bainbridge | e14914d | 2019-05-24 13:43:05 -0700 | [diff] [blame^] | 177 | if deviceType, err := agent.Get(id); err != nil { |
| 178 | return nil, status.Errorf(codes.Internal, err.Error()) |
| 179 | } else if deviceType != nil { |
| 180 | _, ok := deviceType.(*voltha.DeviceType) |
| 181 | if !ok { |
| 182 | return nil, status.Errorf(codes.Internal, MultipleValuesMsg, |
| 183 | id, DeviceTypes.String()) |
| 184 | } |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 185 | return deviceType.(*voltha.DeviceType), nil |
| 186 | } |
| 187 | |
| 188 | return &voltha.DeviceType{}, status.Errorf(codes.NotFound, "device-type-%s", id) |
| 189 | } |
| 190 | |
| 191 | // ListDeviceGroups returns all the device groups known to the system |
| 192 | func (mpMgr *ModelProxyManager) ListDeviceGroups(ctx context.Context) (*voltha.DeviceGroups, error) { |
| 193 | log.Debug("ListDeviceGroups") |
| 194 | |
| 195 | var agent *ModelProxy |
| 196 | var exists bool |
| 197 | |
| 198 | if agent, exists = mpMgr.modelProxy[DeviceGroups.String()]; !exists { |
| 199 | agent = newModelProxy("device_groups", mpMgr.clusterDataProxy) |
| 200 | mpMgr.modelProxy[DeviceGroups.String()] = agent |
| 201 | } |
| 202 | |
| 203 | deviceGroups := &voltha.DeviceGroups{} |
David K. Bainbridge | e14914d | 2019-05-24 13:43:05 -0700 | [diff] [blame^] | 204 | if items, err := agent.Get(); err != nil { |
| 205 | return nil, status.Errorf(codes.Internal, err.Error()) |
| 206 | } else if items != nil { |
| 207 | list, ok := items.([]interface{}) |
| 208 | if !ok { |
| 209 | list = []interface{}{items} |
| 210 | } |
| 211 | for _, item := range list { |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 212 | deviceGroups.Items = append(deviceGroups.Items, item.(*voltha.DeviceGroup)) |
| 213 | } |
| 214 | return deviceGroups, nil |
| 215 | } |
| 216 | |
| 217 | return deviceGroups, status.Errorf(codes.NotFound, "no-device-groups") |
| 218 | } |
| 219 | |
| 220 | // GetDeviceGroup returns the device group associated to the provided id |
| 221 | func (mpMgr *ModelProxyManager) GetDeviceGroup(ctx context.Context, id string) (*voltha.DeviceGroup, error) { |
| 222 | log.Debugw("GetDeviceGroup", log.Fields{"id": id}) |
| 223 | |
| 224 | var agent *ModelProxy |
| 225 | var exists bool |
| 226 | |
| 227 | if agent, exists = mpMgr.modelProxy[DeviceGroups.String()]; !exists { |
| 228 | agent = newModelProxy("device_groups", mpMgr.clusterDataProxy) |
| 229 | mpMgr.modelProxy[DeviceGroups.String()] = agent |
| 230 | } |
| 231 | |
David K. Bainbridge | e14914d | 2019-05-24 13:43:05 -0700 | [diff] [blame^] | 232 | if deviceGroup, err := agent.Get(id); err != nil { |
| 233 | return nil, status.Errorf(codes.Internal, err.Error()) |
| 234 | } else if deviceGroup != nil { |
| 235 | _, ok := deviceGroup.(*voltha.DeviceGroup) |
| 236 | if !ok { |
| 237 | return nil, status.Errorf(codes.Internal, MultipleValuesMsg, |
| 238 | id, DeviceGroups.String()) |
| 239 | } |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 240 | return deviceGroup.(*voltha.DeviceGroup), nil |
| 241 | } |
| 242 | |
| 243 | return &voltha.DeviceGroup{}, status.Errorf(codes.NotFound, "device-group-%s", id) |
| 244 | } |
| 245 | |
| 246 | // ListAlarmFilters returns all the alarm filters known to the system |
| 247 | func (mpMgr *ModelProxyManager) ListAlarmFilters(ctx context.Context) (*voltha.AlarmFilters, error) { |
| 248 | log.Debug("ListAlarmFilters") |
| 249 | |
| 250 | var agent *ModelProxy |
| 251 | var exists bool |
| 252 | |
| 253 | if agent, exists = mpMgr.modelProxy[AlarmFilters.String()]; !exists { |
| 254 | agent = newModelProxy("alarm_filters", mpMgr.clusterDataProxy) |
| 255 | mpMgr.modelProxy[AlarmFilters.String()] = agent |
| 256 | } |
| 257 | |
| 258 | alarmFilters := &voltha.AlarmFilters{} |
David K. Bainbridge | e14914d | 2019-05-24 13:43:05 -0700 | [diff] [blame^] | 259 | if items, err := agent.Get(); err != nil { |
| 260 | return nil, status.Errorf(codes.Internal, err.Error()) |
| 261 | } else if items != nil { |
| 262 | list, ok := items.([]interface{}) |
| 263 | if !ok { |
| 264 | list = []interface{}{items} |
| 265 | } |
| 266 | for _, item := range list { |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 267 | alarmFilters.Filters = append(alarmFilters.Filters, item.(*voltha.AlarmFilter)) |
| 268 | } |
| 269 | return alarmFilters, nil |
| 270 | } |
| 271 | |
| 272 | return alarmFilters, status.Errorf(codes.NotFound, "no-alarm-filters") |
| 273 | } |
| 274 | |
| 275 | // GetAlarmFilter returns the alarm filter associated to the provided id |
| 276 | func (mpMgr *ModelProxyManager) GetAlarmFilter(ctx context.Context, id string) (*voltha.AlarmFilter, error) { |
| 277 | log.Debugw("GetAlarmFilter", log.Fields{"id": id}) |
| 278 | |
| 279 | var agent *ModelProxy |
| 280 | var exists bool |
| 281 | |
| 282 | if agent, exists = mpMgr.modelProxy[AlarmFilters.String()]; !exists { |
| 283 | agent = newModelProxy("alarm_filters", mpMgr.clusterDataProxy) |
| 284 | mpMgr.modelProxy[AlarmFilters.String()] = agent |
| 285 | } |
| 286 | |
David K. Bainbridge | e14914d | 2019-05-24 13:43:05 -0700 | [diff] [blame^] | 287 | if alarmFilter, err := agent.Get(id); err != nil { |
| 288 | return nil, status.Errorf(codes.Internal, err.Error()) |
| 289 | } else if alarmFilter != nil { |
| 290 | _, ok := alarmFilter.(*voltha.AlarmFilter) |
| 291 | if !ok { |
| 292 | return nil, status.Errorf(codes.Internal, MultipleValuesMsg, |
| 293 | id, AlarmFilters.String()) |
| 294 | } |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 295 | return alarmFilter.(*voltha.AlarmFilter), nil |
| 296 | } |
| 297 | return &voltha.AlarmFilter{}, status.Errorf(codes.NotFound, "alarm-filter-%s", id) |
| 298 | } |