Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-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 | "errors" |
| 21 | "github.com/golang/protobuf/ptypes/empty" |
| 22 | da "github.com/opencord/voltha-go/common/core/northbound/grpc" |
Scott Baker | cb7c88a | 2019-10-16 18:32:48 -0700 | [diff] [blame] | 23 | "github.com/opencord/voltha-lib-go/pkg/log" |
William Kurkian | daa6bb2 | 2019-03-07 12:26:28 -0500 | [diff] [blame] | 24 | "github.com/opencord/voltha-protos/go/common" |
| 25 | "github.com/opencord/voltha-protos/go/omci" |
| 26 | "github.com/opencord/voltha-protos/go/openflow_13" |
| 27 | "github.com/opencord/voltha-protos/go/voltha" |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 28 | "google.golang.org/grpc/codes" |
| 29 | "google.golang.org/grpc/metadata" |
| 30 | "google.golang.org/grpc/status" |
| 31 | ) |
| 32 | |
| 33 | type APIHandler struct { |
| 34 | commonMgr *ModelProxyManager |
| 35 | deviceMgr *DeviceManager |
| 36 | logicalDeviceMgr *LogicalDeviceManager |
| 37 | da.DefaultAPIHandler |
| 38 | } |
| 39 | |
| 40 | func NewAPIHandler(generalMgr *ModelProxyManager, deviceMgr *DeviceManager, lDeviceMgr *LogicalDeviceManager) *APIHandler { |
| 41 | handler := &APIHandler{ |
| 42 | commonMgr: generalMgr, |
| 43 | deviceMgr: deviceMgr, |
| 44 | logicalDeviceMgr: lDeviceMgr, |
| 45 | } |
| 46 | return handler |
| 47 | } |
| 48 | |
| 49 | // isTestMode is a helper function to determine a function is invoked for testing only |
| 50 | func isTestMode(ctx context.Context) bool { |
| 51 | md, _ := metadata.FromIncomingContext(ctx) |
| 52 | _, exist := md[common.TestModeKeys_api_test.String()] |
| 53 | return exist |
| 54 | } |
| 55 | |
| 56 | // waitForNilResponseOnSuccess is a helper function to wait for a response on channel ch where an nil |
| 57 | // response is expected in a successful scenario |
| 58 | func waitForNilResponseOnSuccess(ctx context.Context, ch chan interface{}) (*empty.Empty, error) { |
| 59 | select { |
| 60 | case res := <-ch: |
| 61 | if res == nil { |
| 62 | return new(empty.Empty), nil |
| 63 | } else if err, ok := res.(error); ok { |
| 64 | return new(empty.Empty), err |
| 65 | } else { |
| 66 | log.Warnw("unexpected-return-type", log.Fields{"result": res}) |
| 67 | err = status.Errorf(codes.Internal, "%s", res) |
| 68 | return new(empty.Empty), err |
| 69 | } |
| 70 | case <-ctx.Done(): |
| 71 | log.Debug("client-timeout") |
| 72 | return nil, ctx.Err() |
| 73 | } |
| 74 | } |
| 75 | |
Scott Baker | 5f40147 | 2019-08-22 08:32:26 -0700 | [diff] [blame] | 76 | func (handler *APIHandler) UpdateLogLevel(ctx context.Context, logging *voltha.Logging) (*empty.Empty, error) { |
| 77 | log.Debugw("UpdateLogLevel-request", log.Fields{"package": logging.PackageName, "intval": int(logging.Level)}) |
| 78 | out := new(empty.Empty) |
| 79 | if logging.PackageName == "" { |
| 80 | log.SetAllLogLevel(int(logging.Level)) |
| 81 | log.SetDefaultLogLevel(int(logging.Level)) |
| 82 | } else if logging.PackageName == "default" { |
| 83 | log.SetDefaultLogLevel(int(logging.Level)) |
| 84 | } else { |
| 85 | log.SetPackageLogLevel(logging.PackageName, int(logging.Level)) |
| 86 | } |
| 87 | |
| 88 | return out, nil |
| 89 | } |
| 90 | |
| 91 | func (aa APIHandler) GetLogLevels(ctx context.Context, in *voltha.LoggingComponent) (*voltha.Loggings, error) { |
| 92 | logLevels := &voltha.Loggings{} |
| 93 | |
| 94 | // do the per-package log levels |
| 95 | for _, packageName := range log.GetPackageNames() { |
| 96 | level, err := log.GetPackageLogLevel(packageName) |
| 97 | if err != nil { |
| 98 | return nil, err |
| 99 | } |
| 100 | logLevel := &voltha.Logging{ |
| 101 | ComponentName: in.ComponentName, |
| 102 | PackageName: packageName, |
| 103 | Level: voltha.LogLevel_LogLevel(level)} |
| 104 | logLevels.Items = append(logLevels.Items, logLevel) |
| 105 | } |
| 106 | |
| 107 | // now do the default log level |
| 108 | logLevel := &voltha.Logging{ |
| 109 | ComponentName: in.ComponentName, |
| 110 | PackageName: "default", |
| 111 | Level: voltha.LogLevel_LogLevel(log.GetDefaultLogLevel())} |
| 112 | logLevels.Items = append(logLevels.Items, logLevel) |
| 113 | |
| 114 | return logLevels, nil |
| 115 | } |
| 116 | |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 117 | // GetVoltha returns the contents of all components (i.e. devices, logical_devices, ...) |
| 118 | func (handler *APIHandler) GetVoltha(ctx context.Context, empty *empty.Empty) (*voltha.Voltha, error) { |
| 119 | log.Debug("GetVoltha") |
| 120 | return handler.commonMgr.GetVoltha(ctx) |
| 121 | } |
| 122 | |
| 123 | // ListCoreInstances returns details on the running core containers |
| 124 | func (handler *APIHandler) ListCoreInstances(ctx context.Context, empty *empty.Empty) (*voltha.CoreInstances, error) { |
| 125 | log.Debug("ListCoreInstances") |
| 126 | return handler.commonMgr.ListCoreInstances(ctx) |
| 127 | } |
| 128 | |
| 129 | // GetCoreInstance returns the details of a specific core container |
| 130 | func (handler *APIHandler) GetCoreInstance(ctx context.Context, id *voltha.ID) (*voltha.CoreInstance, error) { |
| 131 | log.Debugw("GetCoreInstance", log.Fields{"id": id}) |
| 132 | return handler.commonMgr.GetCoreInstance(ctx, id.Id) |
| 133 | } |
| 134 | |
| 135 | // ListAdapters returns the contents of all adapters known to the system |
| 136 | func (handler *APIHandler) ListAdapters(ctx context.Context, empty *empty.Empty) (*voltha.Adapters, error) { |
| 137 | log.Debug("ListDevices") |
| 138 | return handler.commonMgr.ListAdapters(ctx) |
| 139 | } |
| 140 | |
| 141 | // GetDevice returns the details a specific device |
| 142 | func (handler *APIHandler) GetDevice(ctx context.Context, id *voltha.ID) (*voltha.Device, error) { |
| 143 | log.Debugw("GetDevice-request", log.Fields{"id": id}) |
| 144 | return handler.deviceMgr.GetDevice(id.Id) |
| 145 | } |
| 146 | |
| 147 | // ListDevices returns the contents of all devices known to the system |
| 148 | func (handler *APIHandler) ListDevices(ctx context.Context, empty *empty.Empty) (*voltha.Devices, error) { |
| 149 | log.Debug("ListDevices") |
| 150 | return handler.deviceMgr.ListDevices() |
| 151 | } |
| 152 | |
| 153 | // ListDeviceIds returns the list of device ids managed by a voltha core |
| 154 | func (handler *APIHandler) ListDeviceIds(ctx context.Context, empty *empty.Empty) (*voltha.IDs, error) { |
| 155 | log.Debug("ListDeviceIDs") |
| 156 | if isTestMode(ctx) { |
| 157 | out := &voltha.IDs{Items: make([]*voltha.ID, 0)} |
| 158 | return out, nil |
| 159 | } |
| 160 | return handler.deviceMgr.ListDeviceIds() |
| 161 | } |
| 162 | |
| 163 | // ListDevicePorts returns the ports details for a specific device entry |
| 164 | func (handler *APIHandler) ListDevicePorts(ctx context.Context, id *voltha.ID) (*voltha.Ports, error) { |
| 165 | log.Debugw("ListDevicePorts", log.Fields{"deviceid": id}) |
| 166 | return handler.deviceMgr.ListDevicePorts(ctx, id.Id) |
| 167 | } |
| 168 | |
| 169 | // ListDevicePmConfigs returns the PM config details for a specific device entry |
| 170 | func (handler *APIHandler) ListDevicePmConfigs(ctx context.Context, id *voltha.ID) (*voltha.PmConfigs, error) { |
| 171 | log.Debugw("ListDevicePmConfigs", log.Fields{"deviceid": id}) |
| 172 | return handler.deviceMgr.ListDevicePmConfigs(ctx, id.Id) |
| 173 | } |
| 174 | |
| 175 | // ListDeviceFlows returns the flow details for a specific device entry |
| 176 | func (handler *APIHandler) ListDeviceFlows(ctx context.Context, id *voltha.ID) (*voltha.Flows, error) { |
| 177 | log.Debugw("ListDeviceFlows", log.Fields{"deviceid": id}) |
| 178 | return handler.deviceMgr.ListDeviceFlows(ctx, id.Id) |
| 179 | } |
| 180 | |
| 181 | // ListDeviceFlowGroups returns the flow group details for a specific device entry |
| 182 | func (handler *APIHandler) ListDeviceFlowGroups(ctx context.Context, id *voltha.ID) (*voltha.FlowGroups, error) { |
| 183 | log.Debugw("ListDeviceFlowGroups", log.Fields{"deviceid": id}) |
| 184 | return handler.deviceMgr.ListDeviceFlowGroups(ctx, id.Id) |
| 185 | } |
| 186 | |
| 187 | // ListDeviceTypes returns all the device types known to the system |
| 188 | func (handler *APIHandler) ListDeviceTypes(ctx context.Context, empty *empty.Empty) (*voltha.DeviceTypes, error) { |
| 189 | log.Debug("ListDeviceTypes") |
| 190 | return handler.commonMgr.ListDeviceTypes(ctx) |
| 191 | } |
| 192 | |
| 193 | // GetDeviceType returns the device type for a specific device entry |
| 194 | func (handler *APIHandler) GetDeviceType(ctx context.Context, id *voltha.ID) (*voltha.DeviceType, error) { |
| 195 | log.Debugw("GetDeviceType", log.Fields{"typeid": id}) |
| 196 | return handler.commonMgr.GetDeviceType(ctx, id.Id) |
| 197 | } |
| 198 | |
| 199 | // ListDeviceGroups returns all the device groups known to the system |
| 200 | func (handler *APIHandler) ListDeviceGroups(ctx context.Context, empty *empty.Empty) (*voltha.DeviceGroups, error) { |
| 201 | log.Debug("ListDeviceGroups") |
| 202 | return handler.commonMgr.ListDeviceGroups(ctx) |
| 203 | } |
| 204 | |
| 205 | // GetDeviceGroup returns a specific device group entry |
| 206 | func (handler *APIHandler) GetDeviceGroup(ctx context.Context, id *voltha.ID) (*voltha.DeviceGroup, error) { |
| 207 | log.Debugw("GetDeviceGroup", log.Fields{"groupid": id}) |
| 208 | return handler.commonMgr.GetDeviceGroup(ctx, id.Id) |
| 209 | } |
| 210 | |
| 211 | // GetImageDownloadStatus returns the download status for a specific image entry |
| 212 | func (handler *APIHandler) GetImageDownloadStatus(ctx context.Context, img *voltha.ImageDownload) (*voltha.ImageDownload, error) { |
| 213 | log.Debugw("GetImageDownloadStatus", log.Fields{"deviceid": img.GetId(), "imagename": img.GetName()}) |
| 214 | return handler.deviceMgr.GetImageDownloadStatus(ctx, img.GetId(), img.GetName()) |
| 215 | } |
| 216 | |
| 217 | // GetImageDownload return the download details for a specific image entry |
| 218 | func (handler *APIHandler) GetImageDownload(ctx context.Context, img *voltha.ImageDownload) (*voltha.ImageDownload, error) { |
| 219 | log.Debugw("GetImageDownload", log.Fields{"deviceid": img.GetId(), "imagename": img.GetName()}) |
| 220 | return handler.deviceMgr.GetImageDownload(ctx, img.GetId(), img.GetName()) |
| 221 | } |
| 222 | |
| 223 | // ListImageDownloads returns all image downloads known to the system |
| 224 | func (handler *APIHandler) ListImageDownloads(ctx context.Context, id *voltha.ID) (*voltha.ImageDownloads, error) { |
| 225 | log.Debugw("GetImageDownload", log.Fields{"deviceid": id}) |
| 226 | return handler.deviceMgr.ListImageDownloads(ctx, id.Id) |
| 227 | } |
| 228 | |
| 229 | // GetImages returns all images for a specific device entry |
| 230 | func (handler *APIHandler) GetImages(ctx context.Context, id *voltha.ID) (*voltha.Images, error) { |
| 231 | log.Debugw("GetImages", log.Fields{"deviceid": id}) |
| 232 | return handler.deviceMgr.GetImages(ctx, id.Id) |
| 233 | } |
| 234 | |
| 235 | // ListAlarmFilters return all alarm filters known to the system |
| 236 | func (handler *APIHandler) ListAlarmFilters(ctx context.Context, empty *empty.Empty) (*voltha.AlarmFilters, error) { |
| 237 | log.Debug("ListAlarmFilters") |
| 238 | return handler.commonMgr.ListAlarmFilters(ctx) |
| 239 | } |
| 240 | |
| 241 | // GetAlarmFilter returns a specific alarm filter entry |
| 242 | func (handler *APIHandler) GetAlarmFilter(ctx context.Context, id *voltha.ID) (*voltha.AlarmFilter, error) { |
| 243 | log.Debugw("GetAlarmFilter", log.Fields{"alarmid": id}) |
| 244 | return handler.commonMgr.GetAlarmFilter(ctx, id.Id) |
| 245 | } |
| 246 | |
| 247 | //ReconcileDevices is a request to a voltha core to managed a list of devices based on their IDs |
| 248 | func (handler *APIHandler) ReconcileDevices(ctx context.Context, ids *voltha.IDs) (*empty.Empty, error) { |
| 249 | log.Debug("ReconcileDevices") |
| 250 | if isTestMode(ctx) { |
| 251 | out := new(empty.Empty) |
| 252 | return out, nil |
| 253 | } |
| 254 | ch := make(chan interface{}) |
| 255 | defer close(ch) |
| 256 | go handler.deviceMgr.ReconcileDevices(ctx, ids, ch) |
| 257 | return waitForNilResponseOnSuccess(ctx, ch) |
| 258 | } |
| 259 | |
| 260 | // GetLogicalDevice returns the details for a specific logical device entry |
| 261 | func (handler *APIHandler) GetLogicalDevice(ctx context.Context, id *voltha.ID) (*voltha.LogicalDevice, error) { |
| 262 | log.Debugw("GetLogicalDevice-request", log.Fields{"id": id}) |
| 263 | return handler.logicalDeviceMgr.getLogicalDevice(id.Id) |
| 264 | } |
| 265 | |
| 266 | // ListLogicalDevices returns all logical devices known to the system |
| 267 | func (handler *APIHandler) ListLogicalDevices(ctx context.Context, empty *empty.Empty) (*voltha.LogicalDevices, error) { |
| 268 | log.Debug("ListLogicalDevices") |
| 269 | return handler.logicalDeviceMgr.listLogicalDevices() |
| 270 | } |
| 271 | |
| 272 | // ListLogicalDevicePorts returns port details for a specific logical device entry |
| 273 | func (handler *APIHandler) ListLogicalDevicePorts(ctx context.Context, id *voltha.ID) (*voltha.LogicalPorts, error) { |
| 274 | log.Debugw("ListLogicalDevicePorts", log.Fields{"logicaldeviceid": id}) |
| 275 | return handler.logicalDeviceMgr.ListLogicalDevicePorts(ctx, id.Id) |
| 276 | } |
| 277 | |
| 278 | // ListLogicalDeviceFlows returns flow details for a specific logical device entry |
| 279 | func (handler *APIHandler) ListLogicalDeviceFlows(ctx context.Context, id *voltha.ID) (*voltha.Flows, error) { |
| 280 | log.Debugw("ListLogicalDeviceFlows", log.Fields{"logicaldeviceid": id}) |
| 281 | return handler.logicalDeviceMgr.ListLogicalDeviceFlows(ctx, id.Id) |
| 282 | } |
| 283 | |
| 284 | // ListLogicalDeviceFlowGroups returns flow group details for a specific logical device entry |
| 285 | func (handler *APIHandler) ListLogicalDeviceFlowGroups(ctx context.Context, id *voltha.ID) (*voltha.FlowGroups, error) { |
| 286 | log.Debugw("ListLogicalDeviceFlows", log.Fields{"logicaldeviceid": id}) |
| 287 | return handler.logicalDeviceMgr.ListLogicalDeviceFlowGroups(ctx, id.Id) |
| 288 | } |
| 289 | |
| 290 | func (handler *APIHandler) SelfTest(ctx context.Context, id *voltha.ID) (*voltha.SelfTestResponse, error) { |
| 291 | log.Debugw("SelfTest-request", log.Fields{"id": id}) |
| 292 | if isTestMode(ctx) { |
| 293 | resp := &voltha.SelfTestResponse{Result: voltha.SelfTestResponse_SUCCESS} |
| 294 | return resp, nil |
| 295 | } |
| 296 | return nil, errors.New("UnImplemented") |
| 297 | } |
William Kurkian | daa6bb2 | 2019-03-07 12:26:28 -0500 | [diff] [blame] | 298 | |
| 299 | //@TODO useless stub, what should this actually do? |
| 300 | func (handler *APIHandler) GetAlarmDeviceData( |
| 301 | ctx context.Context, |
| 302 | in *common.ID, |
| 303 | ) (*omci.AlarmDeviceData, error) { |
| 304 | log.Debug("GetAlarmDeviceData-stub") |
| 305 | return nil, nil |
| 306 | } |
| 307 | |
| 308 | //@TODO useless stub, what should this actually do? |
| 309 | func (handler *APIHandler) GetMeterStatsOfLogicalDevice( |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 310 | ctx context.Context, |
William Kurkian | daa6bb2 | 2019-03-07 12:26:28 -0500 | [diff] [blame] | 311 | in *common.ID, |
| 312 | ) (*openflow_13.MeterStatsReply, error) { |
| 313 | log.Debug("GetMeterStatsOfLogicalDevice-stub") |
| 314 | return nil, nil |
| 315 | } |
| 316 | |
| 317 | //@TODO useless stub, what should this actually do? |
| 318 | func (handler *APIHandler) GetMibDeviceData( |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 319 | ctx context.Context, |
| 320 | in *common.ID, |
William Kurkian | daa6bb2 | 2019-03-07 12:26:28 -0500 | [diff] [blame] | 321 | ) (*omci.MibDeviceData, error) { |
| 322 | log.Debug("GetMibDeviceData-stub") |
| 323 | return nil, nil |
| 324 | } |
| 325 | |
| 326 | //@TODO useless stub, what should this actually do? |
| 327 | func (handler *APIHandler) SimulateAlarm( |
| 328 | ctx context.Context, |
| 329 | in *voltha.SimulateAlarmRequest, |
| 330 | ) (*common.OperationResp, error) { |
| 331 | log.Debug("SimulateAlarm-stub") |
| 332 | return nil, nil |
| 333 | } |