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