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