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 | |
| 57 | // ModelProxyManager controls requests made to the miscellaneous data path agents |
| 58 | type ModelProxyManager struct { |
| 59 | modelProxy map[string]*ModelProxy |
| 60 | clusterDataProxy *model.Proxy |
| 61 | } |
| 62 | |
| 63 | func newModelProxyManager(cdProxy *model.Proxy) *ModelProxyManager { |
| 64 | var mgr ModelProxyManager |
| 65 | mgr.modelProxy = make(map[string]*ModelProxy) |
| 66 | mgr.clusterDataProxy = cdProxy |
| 67 | return &mgr |
| 68 | } |
| 69 | |
| 70 | // GetDeviceType returns the device type associated to the provided id |
| 71 | func (mpMgr *ModelProxyManager) GetVoltha(ctx context.Context) (*voltha.Voltha, error) { |
| 72 | log.Debug("GetVoltha") |
| 73 | |
| 74 | var agent *ModelProxy |
| 75 | var exists bool |
| 76 | |
| 77 | if agent, exists = mpMgr.modelProxy[Voltha.String()]; !exists { |
| 78 | agent = newModelProxy("", mpMgr.clusterDataProxy) |
| 79 | mpMgr.modelProxy[Voltha.String()] = agent |
| 80 | } |
| 81 | |
| 82 | if instance, _ := agent.Get(); instance != nil { |
| 83 | return instance.(*voltha.Voltha), nil |
| 84 | } |
| 85 | |
| 86 | return &voltha.Voltha{}, status.Errorf(codes.NotFound, "no-voltha-instance") |
| 87 | } |
| 88 | |
| 89 | // ListCoreInstances returns all the core instances known to the system |
| 90 | func (mpMgr *ModelProxyManager) ListCoreInstances(ctx context.Context) (*voltha.CoreInstances, error) { |
| 91 | log.Debug("ListCoreInstances") |
| 92 | |
| 93 | // TODO: Need to retrieve the list of registered cores |
| 94 | |
| 95 | return &voltha.CoreInstances{}, status.Errorf(codes.NotFound, "no-core-instances") |
| 96 | } |
| 97 | |
| 98 | // GetCoreInstance returns the core instance associated to the provided id |
| 99 | func (mpMgr *ModelProxyManager) GetCoreInstance(ctx context.Context, id string) (*voltha.CoreInstance, error) { |
| 100 | log.Debugw("GetCoreInstance", log.Fields{"id": id}) |
| 101 | |
| 102 | // TODO: Need to retrieve the list of registered cores |
| 103 | |
| 104 | return &voltha.CoreInstance{}, status.Errorf(codes.NotFound, "core-instance-%s", id) |
| 105 | } |
| 106 | |
| 107 | // ListAdapters returns all the device types known to the system |
| 108 | func (mpMgr *ModelProxyManager) ListAdapters(ctx context.Context) (*voltha.Adapters, error) { |
| 109 | log.Debug("ListAdapters") |
| 110 | |
| 111 | var agent *ModelProxy |
| 112 | var exists bool |
David K. Bainbridge | be88396 | 2019-05-22 14:49:12 -0700 | [diff] [blame] | 113 | var adapter *voltha.Adapter |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 114 | |
| 115 | if agent, exists = mpMgr.modelProxy[Adapters.String()]; !exists { |
| 116 | agent = newModelProxy("adapters", mpMgr.clusterDataProxy) |
| 117 | mpMgr.modelProxy[Adapters.String()] = agent |
| 118 | } |
| 119 | |
| 120 | adapters := &voltha.Adapters{} |
| 121 | if items, _ := agent.Get(); items != nil { |
| 122 | for _, item := range items.([]interface{}) { |
David K. Bainbridge | be88396 | 2019-05-22 14:49:12 -0700 | [diff] [blame] | 123 | adapter = item.(*voltha.Adapter) |
| 124 | if adapter.Id != SENTINEL_ADAPTER_ID { // don't report the sentinel |
| 125 | adapters.Items = append(adapters.Items, adapter) |
| 126 | } |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 127 | } |
| 128 | log.Debugw("retrieved-adapters", log.Fields{"adapters": adapters}) |
| 129 | return adapters, nil |
| 130 | } |
| 131 | |
| 132 | return adapters, status.Errorf(codes.NotFound, "no-adapters") |
| 133 | } |
| 134 | |
| 135 | // ListDeviceTypes returns all the device types known to the system |
| 136 | func (mpMgr *ModelProxyManager) ListDeviceTypes(ctx context.Context) (*voltha.DeviceTypes, error) { |
| 137 | log.Debug("ListDeviceTypes") |
| 138 | |
| 139 | var agent *ModelProxy |
| 140 | var exists bool |
| 141 | |
| 142 | if agent, exists = mpMgr.modelProxy[DeviceTypes.String()]; !exists { |
| 143 | agent = newModelProxy("device_types", mpMgr.clusterDataProxy) |
| 144 | mpMgr.modelProxy[DeviceTypes.String()] = agent |
| 145 | } |
| 146 | |
| 147 | deviceTypes := &voltha.DeviceTypes{} |
| 148 | if items, _ := agent.Get(); items != nil { |
| 149 | for _, item := range items.([]interface{}) { |
| 150 | deviceTypes.Items = append(deviceTypes.Items, item.(*voltha.DeviceType)) |
| 151 | } |
| 152 | return deviceTypes, nil |
| 153 | } |
| 154 | |
| 155 | return deviceTypes, status.Errorf(codes.NotFound, "no-device-types") |
| 156 | } |
| 157 | |
| 158 | // GetDeviceType returns the device type associated to the provided id |
| 159 | func (mpMgr *ModelProxyManager) GetDeviceType(ctx context.Context, id string) (*voltha.DeviceType, error) { |
| 160 | log.Debugw("GetDeviceType", log.Fields{"id": id}) |
| 161 | |
| 162 | var agent *ModelProxy |
| 163 | var exists bool |
| 164 | |
| 165 | if agent, exists = mpMgr.modelProxy[DeviceTypes.String()]; !exists { |
| 166 | agent = newModelProxy("device_types", mpMgr.clusterDataProxy) |
| 167 | mpMgr.modelProxy[DeviceTypes.String()] = agent |
| 168 | } |
| 169 | |
| 170 | if deviceType, _ := agent.Get(id); deviceType != nil { |
| 171 | return deviceType.(*voltha.DeviceType), nil |
| 172 | } |
| 173 | |
| 174 | return &voltha.DeviceType{}, status.Errorf(codes.NotFound, "device-type-%s", id) |
| 175 | } |
| 176 | |
| 177 | // ListDeviceGroups returns all the device groups known to the system |
| 178 | func (mpMgr *ModelProxyManager) ListDeviceGroups(ctx context.Context) (*voltha.DeviceGroups, error) { |
| 179 | log.Debug("ListDeviceGroups") |
| 180 | |
| 181 | var agent *ModelProxy |
| 182 | var exists bool |
| 183 | |
| 184 | if agent, exists = mpMgr.modelProxy[DeviceGroups.String()]; !exists { |
| 185 | agent = newModelProxy("device_groups", mpMgr.clusterDataProxy) |
| 186 | mpMgr.modelProxy[DeviceGroups.String()] = agent |
| 187 | } |
| 188 | |
| 189 | deviceGroups := &voltha.DeviceGroups{} |
| 190 | if items, _ := agent.Get(); items != nil { |
| 191 | for _, item := range items.([]interface{}) { |
| 192 | deviceGroups.Items = append(deviceGroups.Items, item.(*voltha.DeviceGroup)) |
| 193 | } |
| 194 | return deviceGroups, nil |
| 195 | } |
| 196 | |
| 197 | return deviceGroups, status.Errorf(codes.NotFound, "no-device-groups") |
| 198 | } |
| 199 | |
| 200 | // GetDeviceGroup returns the device group associated to the provided id |
| 201 | func (mpMgr *ModelProxyManager) GetDeviceGroup(ctx context.Context, id string) (*voltha.DeviceGroup, error) { |
| 202 | log.Debugw("GetDeviceGroup", log.Fields{"id": id}) |
| 203 | |
| 204 | var agent *ModelProxy |
| 205 | var exists bool |
| 206 | |
| 207 | if agent, exists = mpMgr.modelProxy[DeviceGroups.String()]; !exists { |
| 208 | agent = newModelProxy("device_groups", mpMgr.clusterDataProxy) |
| 209 | mpMgr.modelProxy[DeviceGroups.String()] = agent |
| 210 | } |
| 211 | |
| 212 | if deviceGroup, _ := agent.Get(id); deviceGroup != nil { |
| 213 | return deviceGroup.(*voltha.DeviceGroup), nil |
| 214 | } |
| 215 | |
| 216 | return &voltha.DeviceGroup{}, status.Errorf(codes.NotFound, "device-group-%s", id) |
| 217 | } |
| 218 | |
| 219 | // ListAlarmFilters returns all the alarm filters known to the system |
| 220 | func (mpMgr *ModelProxyManager) ListAlarmFilters(ctx context.Context) (*voltha.AlarmFilters, error) { |
| 221 | log.Debug("ListAlarmFilters") |
| 222 | |
| 223 | var agent *ModelProxy |
| 224 | var exists bool |
| 225 | |
| 226 | if agent, exists = mpMgr.modelProxy[AlarmFilters.String()]; !exists { |
| 227 | agent = newModelProxy("alarm_filters", mpMgr.clusterDataProxy) |
| 228 | mpMgr.modelProxy[AlarmFilters.String()] = agent |
| 229 | } |
| 230 | |
| 231 | alarmFilters := &voltha.AlarmFilters{} |
| 232 | if items, _ := agent.Get(); items != nil { |
| 233 | for _, item := range items.([]interface{}) { |
| 234 | alarmFilters.Filters = append(alarmFilters.Filters, item.(*voltha.AlarmFilter)) |
| 235 | } |
| 236 | return alarmFilters, nil |
| 237 | } |
| 238 | |
| 239 | return alarmFilters, status.Errorf(codes.NotFound, "no-alarm-filters") |
| 240 | } |
| 241 | |
| 242 | // GetAlarmFilter returns the alarm filter associated to the provided id |
| 243 | func (mpMgr *ModelProxyManager) GetAlarmFilter(ctx context.Context, id string) (*voltha.AlarmFilter, error) { |
| 244 | log.Debugw("GetAlarmFilter", log.Fields{"id": id}) |
| 245 | |
| 246 | var agent *ModelProxy |
| 247 | var exists bool |
| 248 | |
| 249 | if agent, exists = mpMgr.modelProxy[AlarmFilters.String()]; !exists { |
| 250 | agent = newModelProxy("alarm_filters", mpMgr.clusterDataProxy) |
| 251 | mpMgr.modelProxy[AlarmFilters.String()] = agent |
| 252 | } |
| 253 | |
| 254 | if alarmFilter, _ := agent.Get(id); alarmFilter != nil { |
| 255 | return alarmFilter.(*voltha.AlarmFilter), nil |
| 256 | } |
| 257 | return &voltha.AlarmFilter{}, status.Errorf(codes.NotFound, "alarm-filter-%s", id) |
| 258 | } |