khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [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 | */ |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 16 | package core |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 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" |
| 23 | "github.com/opencord/voltha-go/common/log" |
| 24 | "github.com/opencord/voltha-go/protos/common" |
| 25 | "github.com/opencord/voltha-go/protos/openflow_13" |
| 26 | "github.com/opencord/voltha-go/protos/voltha" |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 27 | "google.golang.org/grpc/codes" |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 28 | "google.golang.org/grpc/metadata" |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 29 | "google.golang.org/grpc/status" |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 30 | ) |
| 31 | |
| 32 | type APIHandler struct { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 33 | deviceMgr *DeviceManager |
| 34 | logicalDeviceMgr *LogicalDeviceManager |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 35 | da.DefaultAPIHandler |
| 36 | } |
| 37 | |
khenaidoo | 9a46896 | 2018-09-19 15:33:13 -0400 | [diff] [blame] | 38 | func NewAPIHandler(deviceMgr *DeviceManager, lDeviceMgr *LogicalDeviceManager) *APIHandler { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 39 | handler := &APIHandler{deviceMgr: deviceMgr, |
khenaidoo | 9a46896 | 2018-09-19 15:33:13 -0400 | [diff] [blame] | 40 | logicalDeviceMgr: lDeviceMgr} |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 41 | return handler |
| 42 | } |
| 43 | func isTestMode(ctx context.Context) bool { |
| 44 | md, _ := metadata.FromIncomingContext(ctx) |
| 45 | _, exist := md[common.TestModeKeys_api_test.String()] |
| 46 | return exist |
| 47 | } |
| 48 | |
| 49 | func (handler *APIHandler) UpdateLogLevel(ctx context.Context, logging *voltha.Logging) (*empty.Empty, error) { |
| 50 | log.Debugw("UpdateLogLevel-request", log.Fields{"newloglevel": logging.Level, "intval": int(logging.Level)}) |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame^] | 51 | out := new(empty.Empty) |
| 52 | log.SetPackageLogLevel(logging.PackageName, int(logging.Level)) |
| 53 | return out, nil |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 54 | } |
| 55 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 56 | func processEnableDevicePort(ctx context.Context, id *voltha.LogicalPortId, ch chan error) { |
| 57 | log.Debugw("processEnableDevicePort", log.Fields{"id": id, "test": common.TestModeKeys_api_test.String()}) |
| 58 | ch <- status.Errorf(100, "%d-%s", 100, "erreur") |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 59 | } |
| 60 | |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 61 | func (handler *APIHandler) EnableLogicalDevicePort(ctx context.Context, id *voltha.LogicalPortId) (*empty.Empty, error) { |
| 62 | log.Debugw("EnableLogicalDevicePort-request", log.Fields{"id": id, "test": common.TestModeKeys_api_test.String()}) |
| 63 | if isTestMode(ctx) { |
| 64 | out := new(empty.Empty) |
| 65 | return out, nil |
| 66 | } |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 67 | ch := make(chan error) |
| 68 | go processEnableDevicePort(ctx, id, ch) |
| 69 | select { |
| 70 | case resp := <-ch: |
| 71 | close(ch) |
| 72 | return new(empty.Empty), resp |
| 73 | case <-ctx.Done(): |
| 74 | return nil, ctx.Err() |
| 75 | } |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | func (handler *APIHandler) DisableLogicalDevicePort(ctx context.Context, id *voltha.LogicalPortId) (*empty.Empty, error) { |
| 79 | log.Debugw("DisableLogicalDevicePort-request", log.Fields{"id": id, "test": common.TestModeKeys_api_test.String()}) |
| 80 | if isTestMode(ctx) { |
| 81 | out := new(empty.Empty) |
| 82 | return out, nil |
| 83 | } |
| 84 | return nil, errors.New("Unimplemented") |
| 85 | } |
| 86 | |
| 87 | func (handler *APIHandler) UpdateLogicalDeviceFlowTable(ctx context.Context, flow *openflow_13.FlowTableUpdate) (*empty.Empty, error) { |
| 88 | log.Debugw("UpdateLogicalDeviceFlowTable-request", log.Fields{"flow": flow, "test": common.TestModeKeys_api_test.String()}) |
| 89 | if isTestMode(ctx) { |
| 90 | out := new(empty.Empty) |
| 91 | return out, nil |
| 92 | } |
| 93 | return nil, errors.New("Unimplemented") |
| 94 | } |
| 95 | |
| 96 | func (handler *APIHandler) UpdateLogicalDeviceFlowGroupTable(ctx context.Context, flow *openflow_13.FlowGroupTableUpdate) (*empty.Empty, error) { |
| 97 | log.Debugw("UpdateLogicalDeviceFlowGroupTable-request", log.Fields{"flow": flow, "test": common.TestModeKeys_api_test.String()}) |
| 98 | if isTestMode(ctx) { |
| 99 | out := new(empty.Empty) |
| 100 | return out, nil |
| 101 | } |
| 102 | return nil, errors.New("Unimplemented") |
| 103 | } |
| 104 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 105 | // GetDevice must be implemented in the read-only containers - should it also be implemented here? |
| 106 | func (handler *APIHandler) GetDevice(ctx context.Context, id *voltha.ID) (*voltha.Device, error) { |
| 107 | log.Debugw("GetDevice-request", log.Fields{"id": id}) |
| 108 | return handler.deviceMgr.getDevice(id.Id) |
| 109 | } |
| 110 | |
| 111 | // GetDevice must be implemented in the read-only containers - should it also be implemented here? |
| 112 | func (handler *APIHandler) ListDevices(ctx context.Context, empty *empty.Empty) (*voltha.Devices, error) { |
| 113 | log.Debug("ListDevices") |
| 114 | return handler.deviceMgr.ListDevices() |
| 115 | } |
| 116 | |
| 117 | // GetLogicalDevice must be implemented in the read-only containers - should it also be implemented here? |
| 118 | func (handler *APIHandler) GetLogicalDevice(ctx context.Context, id *voltha.ID) (*voltha.LogicalDevice, error) { |
| 119 | log.Debugw("GetLogicalDevice-request", log.Fields{"id": id}) |
| 120 | return handler.logicalDeviceMgr.getLogicalDevice(id.Id) |
| 121 | } |
| 122 | |
| 123 | // ListLogicalDevices must be implemented in the read-only containers - should it also be implemented here? |
| 124 | func (handler *APIHandler) ListLogicalDevices(ctx context.Context, empty *empty.Empty) (*voltha.LogicalDevices, error) { |
| 125 | log.Debug("ListLogicalDevices") |
| 126 | return handler.logicalDeviceMgr.listLogicalDevices() |
| 127 | } |
| 128 | |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 129 | func (handler *APIHandler) CreateDevice(ctx context.Context, device *voltha.Device) (*voltha.Device, error) { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 130 | log.Debugw("createdevice", log.Fields{"device": *device}) |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 131 | if isTestMode(ctx) { |
| 132 | return &voltha.Device{Id: device.Id}, nil |
| 133 | } |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 134 | ch := make(chan interface{}) |
| 135 | defer close(ch) |
| 136 | go handler.deviceMgr.createDevice(ctx, device, ch) |
| 137 | select { |
| 138 | case res := <-ch: |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame^] | 139 | if res != nil { |
| 140 | if err, ok := res.(error); ok { |
| 141 | return &voltha.Device{}, err |
| 142 | } |
| 143 | if d, ok := res.(*voltha.Device); ok { |
| 144 | return d, nil |
| 145 | } |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 146 | } |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame^] | 147 | log.Warnw("create-device-unexpected-return-type", log.Fields{"result": res}) |
| 148 | err := status.Errorf(codes.Internal, "%s", res) |
| 149 | return &voltha.Device{}, err |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 150 | case <-ctx.Done(): |
| 151 | log.Debug("createdevice-client-timeout") |
| 152 | return nil, ctx.Err() |
| 153 | } |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | func (handler *APIHandler) EnableDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 157 | log.Debugw("enabledevice", log.Fields{"id": id}) |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 158 | if isTestMode(ctx) { |
| 159 | out := new(empty.Empty) |
| 160 | return out, nil |
| 161 | } |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 162 | ch := make(chan interface{}) |
| 163 | defer close(ch) |
| 164 | go handler.deviceMgr.enableDevice(ctx, id, ch) |
| 165 | select { |
| 166 | case res := <-ch: |
| 167 | if res == nil { |
| 168 | return new(empty.Empty), nil |
| 169 | } else if err, ok := res.(error); ok { |
| 170 | return new(empty.Empty), err |
| 171 | } else { |
| 172 | log.Warnw("enable-device-unexpected-return-type", log.Fields{"result": res}) |
| 173 | err = status.Errorf(codes.Internal, "%s", res) |
| 174 | return new(empty.Empty), err |
| 175 | } |
| 176 | case <-ctx.Done(): |
| 177 | log.Debug("enabledevice-client-timeout") |
| 178 | return nil, ctx.Err() |
| 179 | } |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | func (handler *APIHandler) DisableDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) { |
| 183 | log.Debugw("disabledevice-request", log.Fields{"id": id}) |
| 184 | if isTestMode(ctx) { |
| 185 | out := new(empty.Empty) |
| 186 | return out, nil |
| 187 | } |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame^] | 188 | ch := make(chan interface{}) |
| 189 | defer close(ch) |
| 190 | go handler.deviceMgr.disableDevice(ctx, id, ch) |
| 191 | select { |
| 192 | case res := <-ch: |
| 193 | if res == nil { |
| 194 | return new(empty.Empty), nil |
| 195 | } else if err, ok := res.(error); ok { |
| 196 | return new(empty.Empty), err |
| 197 | } else { |
| 198 | log.Warnw("disable-device-unexpected-return-type", log.Fields{"result": res}) |
| 199 | err = status.Errorf(codes.Internal, "%s", res) |
| 200 | return new(empty.Empty), err |
| 201 | } |
| 202 | case <-ctx.Done(): |
| 203 | log.Debug("enabledevice-client-timeout") |
| 204 | return nil, ctx.Err() |
| 205 | } |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 206 | return nil, errors.New("Unimplemented") |
| 207 | } |
| 208 | |
| 209 | func (handler *APIHandler) RebootDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) { |
| 210 | log.Debugw("disabledevice-request", log.Fields{"id": id}) |
| 211 | if isTestMode(ctx) { |
| 212 | out := new(empty.Empty) |
| 213 | return out, nil |
| 214 | } |
| 215 | return nil, errors.New("Unimplemented") |
| 216 | } |
| 217 | |
| 218 | func (handler *APIHandler) DeleteDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) { |
| 219 | log.Debugw("deletedevice-request", log.Fields{"id": id}) |
| 220 | if isTestMode(ctx) { |
| 221 | out := new(empty.Empty) |
| 222 | return out, nil |
| 223 | } |
| 224 | return nil, errors.New("Unimplemented") |
| 225 | } |
| 226 | |
| 227 | func (handler *APIHandler) DownloadImage(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) { |
| 228 | log.Debugw("DownloadImage-request", log.Fields{"img": *img}) |
| 229 | if isTestMode(ctx) { |
| 230 | resp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS} |
| 231 | return resp, nil |
| 232 | } |
| 233 | |
| 234 | return nil, errors.New("UnImplemented") |
| 235 | } |
| 236 | |
| 237 | func (handler *APIHandler) CancelImageDownload(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) { |
| 238 | log.Debugw("CancelImageDownload-request", log.Fields{"img": *img}) |
| 239 | if isTestMode(ctx) { |
| 240 | resp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS} |
| 241 | return resp, nil |
| 242 | } |
| 243 | return nil, errors.New("UnImplemented") |
| 244 | } |
| 245 | |
| 246 | func (handler *APIHandler) ActivateImageUpdate(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) { |
| 247 | log.Debugw("ActivateImageUpdate-request", log.Fields{"img": *img}) |
| 248 | if isTestMode(ctx) { |
| 249 | resp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS} |
| 250 | return resp, nil |
| 251 | } |
| 252 | return nil, errors.New("UnImplemented") |
| 253 | } |
| 254 | |
| 255 | func (handler *APIHandler) RevertImageUpdate(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) { |
| 256 | log.Debugw("RevertImageUpdate-request", log.Fields{"img": *img}) |
| 257 | if isTestMode(ctx) { |
| 258 | resp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS} |
| 259 | return resp, nil |
| 260 | } |
| 261 | return nil, errors.New("UnImplemented") |
| 262 | } |
| 263 | |
| 264 | func (handler *APIHandler) UpdateDevicePmConfigs(ctx context.Context, configs *voltha.PmConfigs) (*empty.Empty, error) { |
| 265 | log.Debugw("UpdateDevicePmConfigs-request", log.Fields{"configs": *configs}) |
| 266 | if isTestMode(ctx) { |
| 267 | out := new(empty.Empty) |
| 268 | return out, nil |
| 269 | } |
| 270 | return nil, errors.New("UnImplemented") |
| 271 | } |
| 272 | |
| 273 | func (handler *APIHandler) CreateAlarmFilter(ctx context.Context, filter *voltha.AlarmFilter) (*voltha.AlarmFilter, error) { |
| 274 | log.Debugw("CreateAlarmFilter-request", log.Fields{"filter": *filter}) |
| 275 | if isTestMode(ctx) { |
| 276 | f := &voltha.AlarmFilter{Id: filter.Id} |
| 277 | return f, nil |
| 278 | } |
| 279 | return nil, errors.New("UnImplemented") |
| 280 | } |
| 281 | |
| 282 | func (handler *APIHandler) UpdateAlarmFilter(ctx context.Context, filter *voltha.AlarmFilter) (*voltha.AlarmFilter, error) { |
| 283 | log.Debugw("UpdateAlarmFilter-request", log.Fields{"filter": *filter}) |
| 284 | if isTestMode(ctx) { |
| 285 | f := &voltha.AlarmFilter{Id: filter.Id} |
| 286 | return f, nil |
| 287 | } |
| 288 | return nil, errors.New("UnImplemented") |
| 289 | } |
| 290 | |
| 291 | func (handler *APIHandler) DeleteAlarmFilter(ctx context.Context, id *voltha.ID) (*empty.Empty, error) { |
| 292 | log.Debugw("DeleteAlarmFilter-request", log.Fields{"id": *id}) |
| 293 | if isTestMode(ctx) { |
| 294 | out := new(empty.Empty) |
| 295 | return out, nil |
| 296 | } |
| 297 | return nil, errors.New("UnImplemented") |
| 298 | } |
| 299 | |
| 300 | func (handler *APIHandler) SelfTest(ctx context.Context, id *voltha.ID) (*voltha.SelfTestResponse, error) { |
| 301 | log.Debugw("SelfTest-request", log.Fields{"id": id}) |
| 302 | if isTestMode(ctx) { |
| 303 | resp := &voltha.SelfTestResponse{Result: voltha.SelfTestResponse_SUCCESS} |
| 304 | return resp, nil |
| 305 | } |
| 306 | return nil, errors.New("UnImplemented") |
| 307 | } |