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" |
Richard Jankowski | dbab94a | 2018-12-06 16:20:25 -0500 | [diff] [blame] | 21 | "github.com/golang-collections/go-datastructures/queue" |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 22 | "github.com/golang/protobuf/ptypes/empty" |
| 23 | da "github.com/opencord/voltha-go/common/core/northbound/grpc" |
| 24 | "github.com/opencord/voltha-go/common/log" |
| 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" |
Stephane Barbarie | 6e1bd50 | 2018-11-05 22:44:45 -0500 | [diff] [blame] | 31 | "io" |
| 32 | "time" |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 33 | ) |
| 34 | |
Richard Jankowski | 2755adf | 2019-01-17 17:16:48 -0500 | [diff] [blame] | 35 | //TODO: Move this Tag into the proto file |
| 36 | const OF_CONTROLLER_TAG= "voltha_backend_name" |
| 37 | |
khenaidoo | 9cdc1a6 | 2019-01-24 21:57:40 -0500 | [diff] [blame] | 38 | const MAX_RESPONSE_TIME = int64(500) // milliseconds |
Richard Jankowski | d42826e | 2018-11-02 16:06:37 -0400 | [diff] [blame] | 39 | |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 40 | const ( |
| 41 | IMAGE_DOWNLOAD = iota |
| 42 | CANCEL_IMAGE_DOWNLOAD = iota |
| 43 | ACTIVATE_IMAGE = iota |
| 44 | REVERT_IMAGE = iota |
| 45 | ) |
| 46 | |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 47 | type APIHandler struct { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 48 | deviceMgr *DeviceManager |
| 49 | logicalDeviceMgr *LogicalDeviceManager |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 50 | packetInQueue *queue.Queue |
khenaidoo | 9cdc1a6 | 2019-01-24 21:57:40 -0500 | [diff] [blame] | 51 | coreInCompetingMode bool |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 52 | da.DefaultAPIHandler |
| 53 | } |
| 54 | |
khenaidoo | 9cdc1a6 | 2019-01-24 21:57:40 -0500 | [diff] [blame] | 55 | func NewAPIHandler(deviceMgr *DeviceManager, lDeviceMgr *LogicalDeviceManager, inCompetingMode bool) *APIHandler { |
Stephane Barbarie | 6e1bd50 | 2018-11-05 22:44:45 -0500 | [diff] [blame] | 56 | handler := &APIHandler{ |
| 57 | deviceMgr: deviceMgr, |
| 58 | logicalDeviceMgr: lDeviceMgr, |
khenaidoo | 9cdc1a6 | 2019-01-24 21:57:40 -0500 | [diff] [blame] | 59 | coreInCompetingMode:inCompetingMode, |
Richard Jankowski | dbab94a | 2018-12-06 16:20:25 -0500 | [diff] [blame] | 60 | // TODO: Figure out what the 'hint' parameter to queue.New does |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 61 | packetInQueue: queue.New(10), |
Stephane Barbarie | 6e1bd50 | 2018-11-05 22:44:45 -0500 | [diff] [blame] | 62 | } |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 63 | return handler |
| 64 | } |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 65 | |
| 66 | // isTestMode is a helper function to determine a function is invoked for testing only |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 67 | func isTestMode(ctx context.Context) bool { |
| 68 | md, _ := metadata.FromIncomingContext(ctx) |
| 69 | _, exist := md[common.TestModeKeys_api_test.String()] |
| 70 | return exist |
| 71 | } |
| 72 | |
Richard Jankowski | d42826e | 2018-11-02 16:06:37 -0400 | [diff] [blame] | 73 | // This function attempts to extract the serial number from the request metadata |
| 74 | // and create a KV transaction for that serial number for the current core. |
| 75 | func (handler *APIHandler) createKvTransaction(ctx context.Context) (*KVTransaction, error) { |
| 76 | var ( |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 77 | err error |
| 78 | ok bool |
| 79 | md metadata.MD |
Richard Jankowski | d42826e | 2018-11-02 16:06:37 -0400 | [diff] [blame] | 80 | serNum []string |
| 81 | ) |
| 82 | if md, ok = metadata.FromIncomingContext(ctx); !ok { |
| 83 | err = errors.New("metadata-not-found") |
| 84 | } else if serNum, ok = md["voltha_serial_number"]; !ok { |
| 85 | err = errors.New("serial-number-not-found") |
| 86 | } |
| 87 | if !ok { |
| 88 | log.Error(err) |
| 89 | return nil, err |
| 90 | } |
| 91 | // Create KV transaction |
| 92 | txn := NewKVTransaction(serNum[0]) |
| 93 | return txn, nil |
| 94 | } |
| 95 | |
Richard Jankowski | 2755adf | 2019-01-17 17:16:48 -0500 | [diff] [blame] | 96 | // isOFControllerRequest is a helper function to determine if a request was initiated |
| 97 | // from the OpenFlow controller (or its proxy, e.g. OFAgent) |
| 98 | func isOFControllerRequest(ctx context.Context) bool { |
khenaidoo | 9cdc1a6 | 2019-01-24 21:57:40 -0500 | [diff] [blame] | 99 | if md, ok := metadata.FromIncomingContext(ctx); ok { |
| 100 | // Metadata in context |
| 101 | if _, ok = md[OF_CONTROLLER_TAG]; ok { |
| 102 | // OFAgent field in metadata |
| 103 | return true |
| 104 | } |
Richard Jankowski | 2755adf | 2019-01-17 17:16:48 -0500 | [diff] [blame] | 105 | } |
khenaidoo | 9cdc1a6 | 2019-01-24 21:57:40 -0500 | [diff] [blame] | 106 | return false |
| 107 | } |
| 108 | |
| 109 | // competeForTransaction is a helper function to determine whether every request needs to compete with another |
| 110 | // Core to execute the request |
| 111 | func (handler *APIHandler) competeForTransaction() bool { |
| 112 | return handler.coreInCompetingMode |
| 113 | } |
| 114 | |
| 115 | func (handler *APIHandler) acquireTransaction(ctx context.Context, maxTimeout ...int64) (*KVTransaction, error) { |
| 116 | timeout := MAX_RESPONSE_TIME |
| 117 | if len(maxTimeout) > 0 { |
| 118 | timeout = maxTimeout[0] |
Richard Jankowski | 2755adf | 2019-01-17 17:16:48 -0500 | [diff] [blame] | 119 | } |
khenaidoo | 9cdc1a6 | 2019-01-24 21:57:40 -0500 | [diff] [blame] | 120 | txn, err := handler.createKvTransaction(ctx) |
| 121 | if txn == nil { |
| 122 | return nil, err |
| 123 | } else if txn.Acquired(timeout) { |
| 124 | return txn, nil |
| 125 | } else { |
| 126 | return nil, errors.New("failed-to-seize-request") |
Richard Jankowski | 2755adf | 2019-01-17 17:16:48 -0500 | [diff] [blame] | 127 | } |
Richard Jankowski | 2755adf | 2019-01-17 17:16:48 -0500 | [diff] [blame] | 128 | } |
| 129 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 130 | // waitForNilResponseOnSuccess is a helper function to wait for a response on channel ch where an nil |
| 131 | // response is expected in a successful scenario |
| 132 | func waitForNilResponseOnSuccess(ctx context.Context, ch chan interface{}) (*empty.Empty, error) { |
| 133 | select { |
| 134 | case res := <-ch: |
| 135 | if res == nil { |
| 136 | return new(empty.Empty), nil |
| 137 | } else if err, ok := res.(error); ok { |
| 138 | return new(empty.Empty), err |
| 139 | } else { |
| 140 | log.Warnw("unexpected-return-type", log.Fields{"result": res}) |
| 141 | err = status.Errorf(codes.Internal, "%s", res) |
| 142 | return new(empty.Empty), err |
| 143 | } |
| 144 | case <-ctx.Done(): |
| 145 | log.Debug("client-timeout") |
| 146 | return nil, ctx.Err() |
| 147 | } |
| 148 | } |
| 149 | |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 150 | func (handler *APIHandler) UpdateLogLevel(ctx context.Context, logging *voltha.Logging) (*empty.Empty, error) { |
khenaidoo | 6f2fbe3 | 2019-01-18 16:16:50 -0500 | [diff] [blame] | 151 | log.Debugw("UpdateLogLevel-request", log.Fields{"package": logging.PackageName, "intval": int(logging.Level)}) |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 152 | out := new(empty.Empty) |
khenaidoo | 6f2fbe3 | 2019-01-18 16:16:50 -0500 | [diff] [blame] | 153 | if logging.PackageName == "" { |
| 154 | log.SetAllLogLevel(int(logging.Level)) |
| 155 | } else { |
| 156 | log.SetPackageLogLevel(logging.PackageName, int(logging.Level)) |
| 157 | } |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 158 | return out, nil |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | func (handler *APIHandler) EnableLogicalDevicePort(ctx context.Context, id *voltha.LogicalPortId) (*empty.Empty, error) { |
| 162 | log.Debugw("EnableLogicalDevicePort-request", log.Fields{"id": id, "test": common.TestModeKeys_api_test.String()}) |
| 163 | if isTestMode(ctx) { |
| 164 | out := new(empty.Empty) |
| 165 | return out, nil |
| 166 | } |
Richard Jankowski | 2755adf | 2019-01-17 17:16:48 -0500 | [diff] [blame] | 167 | |
khenaidoo | 9cdc1a6 | 2019-01-24 21:57:40 -0500 | [diff] [blame] | 168 | if handler.competeForTransaction() { |
| 169 | if txn, err := handler.acquireTransaction(ctx); err != nil { |
Richard Jankowski | 2755adf | 2019-01-17 17:16:48 -0500 | [diff] [blame] | 170 | return new(empty.Empty), err |
Richard Jankowski | 2755adf | 2019-01-17 17:16:48 -0500 | [diff] [blame] | 171 | } else { |
khenaidoo | 9cdc1a6 | 2019-01-24 21:57:40 -0500 | [diff] [blame] | 172 | defer txn.Close() |
Richard Jankowski | 2755adf | 2019-01-17 17:16:48 -0500 | [diff] [blame] | 173 | } |
| 174 | } |
khenaidoo | 9cdc1a6 | 2019-01-24 21:57:40 -0500 | [diff] [blame] | 175 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 176 | ch := make(chan interface{}) |
| 177 | defer close(ch) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 178 | go handler.logicalDeviceMgr.enableLogicalPort(ctx, id, ch) |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 179 | return waitForNilResponseOnSuccess(ctx, ch) |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | func (handler *APIHandler) DisableLogicalDevicePort(ctx context.Context, id *voltha.LogicalPortId) (*empty.Empty, error) { |
| 183 | log.Debugw("DisableLogicalDevicePort-request", log.Fields{"id": id, "test": common.TestModeKeys_api_test.String()}) |
| 184 | if isTestMode(ctx) { |
| 185 | out := new(empty.Empty) |
| 186 | return out, nil |
| 187 | } |
Richard Jankowski | 2755adf | 2019-01-17 17:16:48 -0500 | [diff] [blame] | 188 | |
khenaidoo | 9cdc1a6 | 2019-01-24 21:57:40 -0500 | [diff] [blame] | 189 | if handler.competeForTransaction() { |
| 190 | if txn, err := handler.acquireTransaction(ctx); err != nil { |
Richard Jankowski | 2755adf | 2019-01-17 17:16:48 -0500 | [diff] [blame] | 191 | return new(empty.Empty), err |
Richard Jankowski | 2755adf | 2019-01-17 17:16:48 -0500 | [diff] [blame] | 192 | } else { |
khenaidoo | 9cdc1a6 | 2019-01-24 21:57:40 -0500 | [diff] [blame] | 193 | defer txn.Close() |
Richard Jankowski | 2755adf | 2019-01-17 17:16:48 -0500 | [diff] [blame] | 194 | } |
| 195 | } |
khenaidoo | 9cdc1a6 | 2019-01-24 21:57:40 -0500 | [diff] [blame] | 196 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 197 | ch := make(chan interface{}) |
| 198 | defer close(ch) |
| 199 | go handler.logicalDeviceMgr.disableLogicalPort(ctx, id, ch) |
| 200 | return waitForNilResponseOnSuccess(ctx, ch) |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | func (handler *APIHandler) UpdateLogicalDeviceFlowTable(ctx context.Context, flow *openflow_13.FlowTableUpdate) (*empty.Empty, error) { |
| 204 | log.Debugw("UpdateLogicalDeviceFlowTable-request", log.Fields{"flow": flow, "test": common.TestModeKeys_api_test.String()}) |
| 205 | if isTestMode(ctx) { |
| 206 | out := new(empty.Empty) |
| 207 | return out, nil |
| 208 | } |
Richard Jankowski | 2755adf | 2019-01-17 17:16:48 -0500 | [diff] [blame] | 209 | |
khenaidoo | 9cdc1a6 | 2019-01-24 21:57:40 -0500 | [diff] [blame] | 210 | if handler.competeForTransaction() { |
| 211 | if !isOFControllerRequest(ctx) { // No need to acquire the transaction as request is sent to one core only |
| 212 | if txn, err := handler.acquireTransaction(ctx); err != nil { |
| 213 | return new(empty.Empty), err |
| 214 | } else { |
| 215 | defer txn.Close() |
| 216 | } |
Richard Jankowski | 2755adf | 2019-01-17 17:16:48 -0500 | [diff] [blame] | 217 | } |
| 218 | } |
khenaidoo | 9cdc1a6 | 2019-01-24 21:57:40 -0500 | [diff] [blame] | 219 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 220 | ch := make(chan interface{}) |
| 221 | defer close(ch) |
| 222 | go handler.logicalDeviceMgr.updateFlowTable(ctx, flow.Id, flow.FlowMod, ch) |
| 223 | return waitForNilResponseOnSuccess(ctx, ch) |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | func (handler *APIHandler) UpdateLogicalDeviceFlowGroupTable(ctx context.Context, flow *openflow_13.FlowGroupTableUpdate) (*empty.Empty, error) { |
| 227 | log.Debugw("UpdateLogicalDeviceFlowGroupTable-request", log.Fields{"flow": flow, "test": common.TestModeKeys_api_test.String()}) |
| 228 | if isTestMode(ctx) { |
| 229 | out := new(empty.Empty) |
| 230 | return out, nil |
| 231 | } |
Richard Jankowski | 2755adf | 2019-01-17 17:16:48 -0500 | [diff] [blame] | 232 | |
khenaidoo | 9cdc1a6 | 2019-01-24 21:57:40 -0500 | [diff] [blame] | 233 | if handler.competeForTransaction() { |
| 234 | if !isOFControllerRequest(ctx) { // No need to acquire the transaction as request is sent to one core only |
| 235 | if txn, err := handler.acquireTransaction(ctx); err != nil { |
| 236 | return new(empty.Empty), err |
| 237 | } else { |
| 238 | defer txn.Close() |
| 239 | } |
Richard Jankowski | 2755adf | 2019-01-17 17:16:48 -0500 | [diff] [blame] | 240 | } |
| 241 | } |
khenaidoo | 9cdc1a6 | 2019-01-24 21:57:40 -0500 | [diff] [blame] | 242 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 243 | ch := make(chan interface{}) |
| 244 | defer close(ch) |
| 245 | go handler.logicalDeviceMgr.updateGroupTable(ctx, flow.Id, flow.GroupMod, ch) |
| 246 | return waitForNilResponseOnSuccess(ctx, ch) |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 247 | } |
| 248 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 249 | // GetDevice must be implemented in the read-only containers - should it also be implemented here? |
| 250 | func (handler *APIHandler) GetDevice(ctx context.Context, id *voltha.ID) (*voltha.Device, error) { |
| 251 | log.Debugw("GetDevice-request", log.Fields{"id": id}) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 252 | return handler.deviceMgr.GetDevice(id.Id) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | // GetDevice must be implemented in the read-only containers - should it also be implemented here? |
| 256 | func (handler *APIHandler) ListDevices(ctx context.Context, empty *empty.Empty) (*voltha.Devices, error) { |
| 257 | log.Debug("ListDevices") |
| 258 | return handler.deviceMgr.ListDevices() |
| 259 | } |
| 260 | |
khenaidoo | 7ccedd5 | 2018-12-14 16:48:54 -0500 | [diff] [blame] | 261 | // ListDeviceIds returns the list of device ids managed by a voltha core |
| 262 | func (handler *APIHandler) ListDeviceIds(ctx context.Context, empty *empty.Empty) (*voltha.IDs, error) { |
| 263 | log.Debug("ListDeviceIDs") |
| 264 | if isTestMode(ctx) { |
| 265 | out := &voltha.IDs{Items: make([]*voltha.ID, 0)} |
| 266 | return out, nil |
| 267 | } |
| 268 | return handler.deviceMgr.ListDeviceIds() |
| 269 | } |
| 270 | |
| 271 | //ReconcileDevices is a request to a voltha core to managed a list of devices based on their IDs |
| 272 | func (handler *APIHandler) ReconcileDevices(ctx context.Context, ids *voltha.IDs) (*empty.Empty, error) { |
| 273 | log.Debug("ReconcileDevices") |
| 274 | if isTestMode(ctx) { |
| 275 | out := new(empty.Empty) |
| 276 | return out, nil |
| 277 | } |
Richard Jankowski | 2755adf | 2019-01-17 17:16:48 -0500 | [diff] [blame] | 278 | |
khenaidoo | 9cdc1a6 | 2019-01-24 21:57:40 -0500 | [diff] [blame] | 279 | // No need to grab a transaction as this request is core specific |
| 280 | |
khenaidoo | 7ccedd5 | 2018-12-14 16:48:54 -0500 | [diff] [blame] | 281 | ch := make(chan interface{}) |
| 282 | defer close(ch) |
| 283 | go handler.deviceMgr.ReconcileDevices(ctx, ids, ch) |
| 284 | return waitForNilResponseOnSuccess(ctx, ch) |
| 285 | } |
| 286 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 287 | // GetLogicalDevice must be implemented in the read-only containers - should it also be implemented here? |
| 288 | func (handler *APIHandler) GetLogicalDevice(ctx context.Context, id *voltha.ID) (*voltha.LogicalDevice, error) { |
| 289 | log.Debugw("GetLogicalDevice-request", log.Fields{"id": id}) |
| 290 | return handler.logicalDeviceMgr.getLogicalDevice(id.Id) |
| 291 | } |
| 292 | |
| 293 | // ListLogicalDevices must be implemented in the read-only containers - should it also be implemented here? |
| 294 | func (handler *APIHandler) ListLogicalDevices(ctx context.Context, empty *empty.Empty) (*voltha.LogicalDevices, error) { |
| 295 | log.Debug("ListLogicalDevices") |
| 296 | return handler.logicalDeviceMgr.listLogicalDevices() |
| 297 | } |
| 298 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 299 | // ListLogicalDevicePorts must be implemented in the read-only containers - should it also be implemented here? |
| 300 | func (handler *APIHandler) ListLogicalDevicePorts(ctx context.Context, id *voltha.ID) (*voltha.LogicalPorts, error) { |
| 301 | log.Debugw("ListLogicalDevicePorts", log.Fields{"logicaldeviceid": id}) |
| 302 | return handler.logicalDeviceMgr.ListLogicalDevicePorts(ctx, id.Id) |
| 303 | } |
| 304 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 305 | // CreateDevice creates a new parent device in the data model |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 306 | func (handler *APIHandler) CreateDevice(ctx context.Context, device *voltha.Device) (*voltha.Device, error) { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 307 | log.Debugw("createdevice", log.Fields{"device": *device}) |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 308 | if isTestMode(ctx) { |
| 309 | return &voltha.Device{Id: device.Id}, nil |
| 310 | } |
Richard Jankowski | d42826e | 2018-11-02 16:06:37 -0400 | [diff] [blame] | 311 | |
khenaidoo | 9cdc1a6 | 2019-01-24 21:57:40 -0500 | [diff] [blame] | 312 | if handler.competeForTransaction() { |
| 313 | if txn, err := handler.acquireTransaction(ctx); err != nil { |
Richard Jankowski | 2755adf | 2019-01-17 17:16:48 -0500 | [diff] [blame] | 314 | return &voltha.Device{}, err |
Richard Jankowski | 2755adf | 2019-01-17 17:16:48 -0500 | [diff] [blame] | 315 | } else { |
khenaidoo | 9cdc1a6 | 2019-01-24 21:57:40 -0500 | [diff] [blame] | 316 | defer txn.Close() |
Richard Jankowski | 2755adf | 2019-01-17 17:16:48 -0500 | [diff] [blame] | 317 | } |
| 318 | } |
khenaidoo | 9cdc1a6 | 2019-01-24 21:57:40 -0500 | [diff] [blame] | 319 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 320 | ch := make(chan interface{}) |
| 321 | defer close(ch) |
| 322 | go handler.deviceMgr.createDevice(ctx, device, ch) |
| 323 | select { |
| 324 | case res := <-ch: |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 325 | if res != nil { |
| 326 | if err, ok := res.(error); ok { |
| 327 | return &voltha.Device{}, err |
| 328 | } |
| 329 | if d, ok := res.(*voltha.Device); ok { |
| 330 | return d, nil |
| 331 | } |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 332 | } |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 333 | log.Warnw("create-device-unexpected-return-type", log.Fields{"result": res}) |
| 334 | err := status.Errorf(codes.Internal, "%s", res) |
| 335 | return &voltha.Device{}, err |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 336 | case <-ctx.Done(): |
| 337 | log.Debug("createdevice-client-timeout") |
| 338 | return nil, ctx.Err() |
| 339 | } |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 340 | } |
| 341 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 342 | // EnableDevice activates a device by invoking the adopt_device API on the appropriate adapter |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 343 | func (handler *APIHandler) EnableDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 344 | log.Debugw("enabledevice", log.Fields{"id": id}) |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 345 | if isTestMode(ctx) { |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 346 | return new(empty.Empty), nil |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 347 | } |
Richard Jankowski | d42826e | 2018-11-02 16:06:37 -0400 | [diff] [blame] | 348 | |
khenaidoo | 9cdc1a6 | 2019-01-24 21:57:40 -0500 | [diff] [blame] | 349 | if handler.competeForTransaction() { |
| 350 | if txn, err := handler.acquireTransaction(ctx); err != nil { |
Richard Jankowski | 2755adf | 2019-01-17 17:16:48 -0500 | [diff] [blame] | 351 | return new(empty.Empty), err |
Richard Jankowski | 2755adf | 2019-01-17 17:16:48 -0500 | [diff] [blame] | 352 | } else { |
khenaidoo | 9cdc1a6 | 2019-01-24 21:57:40 -0500 | [diff] [blame] | 353 | defer txn.Close() |
Richard Jankowski | 2755adf | 2019-01-17 17:16:48 -0500 | [diff] [blame] | 354 | } |
| 355 | } |
khenaidoo | 9cdc1a6 | 2019-01-24 21:57:40 -0500 | [diff] [blame] | 356 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 357 | ch := make(chan interface{}) |
| 358 | defer close(ch) |
| 359 | go handler.deviceMgr.enableDevice(ctx, id, ch) |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 360 | return waitForNilResponseOnSuccess(ctx, ch) |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 361 | } |
| 362 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 363 | // DisableDevice disables a device along with any child device it may have |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 364 | func (handler *APIHandler) DisableDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) { |
| 365 | log.Debugw("disabledevice-request", log.Fields{"id": id}) |
| 366 | if isTestMode(ctx) { |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 367 | return new(empty.Empty), nil |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 368 | } |
Richard Jankowski | 2755adf | 2019-01-17 17:16:48 -0500 | [diff] [blame] | 369 | |
khenaidoo | 9cdc1a6 | 2019-01-24 21:57:40 -0500 | [diff] [blame] | 370 | if handler.competeForTransaction() { |
| 371 | if txn, err := handler.acquireTransaction(ctx); err != nil { |
Richard Jankowski | 2755adf | 2019-01-17 17:16:48 -0500 | [diff] [blame] | 372 | return new(empty.Empty), err |
Richard Jankowski | 2755adf | 2019-01-17 17:16:48 -0500 | [diff] [blame] | 373 | } else { |
khenaidoo | 9cdc1a6 | 2019-01-24 21:57:40 -0500 | [diff] [blame] | 374 | defer txn.Close() |
Richard Jankowski | 2755adf | 2019-01-17 17:16:48 -0500 | [diff] [blame] | 375 | } |
| 376 | } |
khenaidoo | 9cdc1a6 | 2019-01-24 21:57:40 -0500 | [diff] [blame] | 377 | |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 378 | ch := make(chan interface{}) |
| 379 | defer close(ch) |
| 380 | go handler.deviceMgr.disableDevice(ctx, id, ch) |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 381 | return waitForNilResponseOnSuccess(ctx, ch) |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 382 | } |
| 383 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 384 | //RebootDevice invoked the reboot API to the corresponding adapter |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 385 | func (handler *APIHandler) RebootDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) { |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 386 | log.Debugw("rebootDevice-request", log.Fields{"id": id}) |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 387 | if isTestMode(ctx) { |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 388 | return new(empty.Empty), nil |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 389 | } |
Richard Jankowski | 2755adf | 2019-01-17 17:16:48 -0500 | [diff] [blame] | 390 | |
khenaidoo | 9cdc1a6 | 2019-01-24 21:57:40 -0500 | [diff] [blame] | 391 | if handler.competeForTransaction() { |
| 392 | if txn, err := handler.acquireTransaction(ctx); err != nil { |
Richard Jankowski | 2755adf | 2019-01-17 17:16:48 -0500 | [diff] [blame] | 393 | return new(empty.Empty), err |
Richard Jankowski | 2755adf | 2019-01-17 17:16:48 -0500 | [diff] [blame] | 394 | } else { |
khenaidoo | 9cdc1a6 | 2019-01-24 21:57:40 -0500 | [diff] [blame] | 395 | defer txn.Close() |
Richard Jankowski | 2755adf | 2019-01-17 17:16:48 -0500 | [diff] [blame] | 396 | } |
| 397 | } |
khenaidoo | 9cdc1a6 | 2019-01-24 21:57:40 -0500 | [diff] [blame] | 398 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 399 | ch := make(chan interface{}) |
| 400 | defer close(ch) |
| 401 | go handler.deviceMgr.rebootDevice(ctx, id, ch) |
| 402 | return waitForNilResponseOnSuccess(ctx, ch) |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 403 | } |
| 404 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 405 | // DeleteDevice removes a device from the data model |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 406 | func (handler *APIHandler) DeleteDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) { |
| 407 | log.Debugw("deletedevice-request", log.Fields{"id": id}) |
| 408 | if isTestMode(ctx) { |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 409 | return new(empty.Empty), nil |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 410 | } |
Richard Jankowski | 2755adf | 2019-01-17 17:16:48 -0500 | [diff] [blame] | 411 | |
khenaidoo | 9cdc1a6 | 2019-01-24 21:57:40 -0500 | [diff] [blame] | 412 | if handler.competeForTransaction() { |
| 413 | if txn, err := handler.acquireTransaction(ctx); err != nil { |
Richard Jankowski | 2755adf | 2019-01-17 17:16:48 -0500 | [diff] [blame] | 414 | return new(empty.Empty), err |
Richard Jankowski | 2755adf | 2019-01-17 17:16:48 -0500 | [diff] [blame] | 415 | } else { |
khenaidoo | 9cdc1a6 | 2019-01-24 21:57:40 -0500 | [diff] [blame] | 416 | defer txn.Close() |
Richard Jankowski | 2755adf | 2019-01-17 17:16:48 -0500 | [diff] [blame] | 417 | } |
| 418 | } |
khenaidoo | 9cdc1a6 | 2019-01-24 21:57:40 -0500 | [diff] [blame] | 419 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 420 | ch := make(chan interface{}) |
| 421 | defer close(ch) |
| 422 | go handler.deviceMgr.deleteDevice(ctx, id, ch) |
| 423 | return waitForNilResponseOnSuccess(ctx, ch) |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 424 | } |
| 425 | |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 426 | // processImageRequest is a helper method to execute an image download request |
| 427 | func (handler *APIHandler) processImageRequest(ctx context.Context, img *voltha.ImageDownload, requestType int) (*common.OperationResp, error) { |
| 428 | log.Debugw("processImageDownload", log.Fields{"img": *img, "requestType": requestType}) |
| 429 | if isTestMode(ctx) { |
| 430 | resp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS} |
| 431 | return resp, nil |
| 432 | } |
| 433 | |
khenaidoo | 9cdc1a6 | 2019-01-24 21:57:40 -0500 | [diff] [blame] | 434 | if handler.competeForTransaction() { |
| 435 | if txn, err := handler.acquireTransaction(ctx); err != nil { |
| 436 | return &common.OperationResp{}, err |
| 437 | } else { |
| 438 | defer txn.Close() |
| 439 | } |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | failedresponse := &common.OperationResp{Code:voltha.OperationResp_OPERATION_FAILURE} |
| 443 | |
| 444 | ch := make(chan interface{}) |
| 445 | defer close(ch) |
| 446 | switch requestType { |
| 447 | case IMAGE_DOWNLOAD: |
| 448 | go handler.deviceMgr.downloadImage(ctx, img, ch) |
| 449 | case CANCEL_IMAGE_DOWNLOAD: |
| 450 | go handler.deviceMgr.cancelImageDownload(ctx, img, ch) |
| 451 | case ACTIVATE_IMAGE: |
| 452 | go handler.deviceMgr.activateImage(ctx, img, ch) |
| 453 | case REVERT_IMAGE: |
| 454 | go handler.deviceMgr.revertImage(ctx, img, ch) |
| 455 | default: |
| 456 | log.Warn("invalid-request-type", log.Fields{"requestType": requestType}) |
| 457 | return failedresponse, status.Errorf(codes.InvalidArgument, "%d", requestType) |
| 458 | } |
| 459 | select { |
| 460 | case res := <-ch: |
| 461 | if res != nil { |
| 462 | if err, ok := res.(error); ok { |
| 463 | return failedresponse, err |
| 464 | } |
| 465 | if opResp, ok := res.(*common.OperationResp); ok { |
| 466 | return opResp, nil |
| 467 | } |
| 468 | } |
| 469 | log.Warnw("download-image-unexpected-return-type", log.Fields{"result": res}) |
| 470 | return failedresponse, status.Errorf(codes.Internal, "%s", res) |
| 471 | case <-ctx.Done(): |
| 472 | log.Debug("downloadImage-client-timeout") |
| 473 | return nil, ctx.Err() |
| 474 | } |
| 475 | } |
| 476 | |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 477 | func (handler *APIHandler) DownloadImage(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) { |
| 478 | log.Debugw("DownloadImage-request", log.Fields{"img": *img}) |
| 479 | if isTestMode(ctx) { |
| 480 | resp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS} |
| 481 | return resp, nil |
| 482 | } |
| 483 | |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 484 | return handler.processImageRequest(ctx, img, IMAGE_DOWNLOAD) |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | func (handler *APIHandler) CancelImageDownload(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) { |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 488 | log.Debugw("cancelImageDownload-request", log.Fields{"img": *img}) |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 489 | if isTestMode(ctx) { |
| 490 | resp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS} |
| 491 | return resp, nil |
| 492 | } |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 493 | return handler.processImageRequest(ctx, img, CANCEL_IMAGE_DOWNLOAD) |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | func (handler *APIHandler) ActivateImageUpdate(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) { |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 497 | log.Debugw("activateImageUpdate-request", log.Fields{"img": *img}) |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 498 | if isTestMode(ctx) { |
| 499 | resp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS} |
| 500 | return resp, nil |
| 501 | } |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 502 | |
| 503 | return handler.processImageRequest(ctx, img, ACTIVATE_IMAGE) |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | func (handler *APIHandler) RevertImageUpdate(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) { |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 507 | log.Debugw("revertImageUpdate-request", log.Fields{"img": *img}) |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 508 | if isTestMode(ctx) { |
| 509 | resp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS} |
| 510 | return resp, nil |
| 511 | } |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 512 | |
| 513 | return handler.processImageRequest(ctx, img, REVERT_IMAGE) |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 514 | } |
| 515 | |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 516 | func (handler *APIHandler) GetImageDownloadStatus(ctx context.Context, img *voltha.ImageDownload) (*voltha.ImageDownload, error) { |
| 517 | log.Debugw("getImageDownloadStatus-request", log.Fields{"img": *img}) |
| 518 | if isTestMode(ctx) { |
| 519 | resp := &voltha.ImageDownload{State: voltha.ImageDownload_DOWNLOAD_SUCCEEDED} |
| 520 | return resp, nil |
| 521 | } |
| 522 | |
| 523 | failedresponse := &voltha.ImageDownload{State: voltha.ImageDownload_DOWNLOAD_UNKNOWN} |
| 524 | |
khenaidoo | 9cdc1a6 | 2019-01-24 21:57:40 -0500 | [diff] [blame] | 525 | if handler.competeForTransaction() { |
| 526 | if txn, err := handler.acquireTransaction(ctx); err != nil { |
| 527 | return failedresponse, err |
| 528 | } else { |
| 529 | defer txn.Close() |
| 530 | } |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | ch := make(chan interface{}) |
| 534 | defer close(ch) |
| 535 | go handler.deviceMgr.getImageDownloadStatus(ctx, img, ch) |
| 536 | |
| 537 | select { |
| 538 | case res := <-ch: |
| 539 | if res != nil { |
| 540 | if err, ok := res.(error); ok { |
| 541 | return failedresponse, err |
| 542 | } |
| 543 | if downloadResp, ok := res.(*voltha.ImageDownload); ok { |
| 544 | return downloadResp, nil |
| 545 | } |
| 546 | } |
| 547 | log.Warnw("download-image-status", log.Fields{"result": res}) |
| 548 | return failedresponse, status.Errorf(codes.Internal, "%s", res) |
| 549 | case <-ctx.Done(): |
| 550 | log.Debug("downloadImage-client-timeout") |
| 551 | return failedresponse, ctx.Err() |
| 552 | } |
| 553 | } |
| 554 | |
| 555 | func (handler *APIHandler) GetImageDownload(ctx context.Context, img *voltha.ImageDownload) (*voltha.ImageDownload, error) { |
| 556 | log.Debugw("GetImageDownload-request", log.Fields{"img": *img}) |
| 557 | if isTestMode(ctx) { |
| 558 | resp := &voltha.ImageDownload{State: voltha.ImageDownload_DOWNLOAD_SUCCEEDED} |
| 559 | return resp, nil |
| 560 | } |
| 561 | |
| 562 | if download, err := handler.deviceMgr.getImageDownload(ctx, img); err != nil { |
| 563 | return &voltha.ImageDownload{State: voltha.ImageDownload_DOWNLOAD_UNKNOWN}, err |
| 564 | } else { |
| 565 | return download, nil |
| 566 | } |
| 567 | } |
| 568 | |
| 569 | func (handler *APIHandler) ListImageDownloads(ctx context.Context, id *voltha.ID) (*voltha.ImageDownloads, error) { |
| 570 | log.Debugw("ListImageDownloads-request", log.Fields{"deviceId": id.Id}) |
| 571 | if isTestMode(ctx) { |
| 572 | resp := &voltha.ImageDownloads{Items:[]*voltha.ImageDownload{}} |
| 573 | return resp, nil |
| 574 | } |
| 575 | |
| 576 | if downloads, err := handler.deviceMgr.listImageDownloads(ctx, id.Id); err != nil { |
| 577 | failedResp := &voltha.ImageDownloads{ |
| 578 | Items:[]*voltha.ImageDownload{ |
| 579 | &voltha.ImageDownload{State: voltha.ImageDownload_DOWNLOAD_UNKNOWN}, |
| 580 | }, |
| 581 | } |
| 582 | return failedResp, err |
| 583 | } else { |
| 584 | return downloads, nil |
| 585 | } |
| 586 | } |
| 587 | |
| 588 | |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 589 | func (handler *APIHandler) UpdateDevicePmConfigs(ctx context.Context, configs *voltha.PmConfigs) (*empty.Empty, error) { |
| 590 | log.Debugw("UpdateDevicePmConfigs-request", log.Fields{"configs": *configs}) |
| 591 | if isTestMode(ctx) { |
| 592 | out := new(empty.Empty) |
| 593 | return out, nil |
| 594 | } |
| 595 | return nil, errors.New("UnImplemented") |
| 596 | } |
| 597 | |
| 598 | func (handler *APIHandler) CreateAlarmFilter(ctx context.Context, filter *voltha.AlarmFilter) (*voltha.AlarmFilter, error) { |
| 599 | log.Debugw("CreateAlarmFilter-request", log.Fields{"filter": *filter}) |
| 600 | if isTestMode(ctx) { |
| 601 | f := &voltha.AlarmFilter{Id: filter.Id} |
| 602 | return f, nil |
| 603 | } |
| 604 | return nil, errors.New("UnImplemented") |
| 605 | } |
| 606 | |
| 607 | func (handler *APIHandler) UpdateAlarmFilter(ctx context.Context, filter *voltha.AlarmFilter) (*voltha.AlarmFilter, error) { |
| 608 | log.Debugw("UpdateAlarmFilter-request", log.Fields{"filter": *filter}) |
| 609 | if isTestMode(ctx) { |
| 610 | f := &voltha.AlarmFilter{Id: filter.Id} |
| 611 | return f, nil |
| 612 | } |
| 613 | return nil, errors.New("UnImplemented") |
| 614 | } |
| 615 | |
| 616 | func (handler *APIHandler) DeleteAlarmFilter(ctx context.Context, id *voltha.ID) (*empty.Empty, error) { |
| 617 | log.Debugw("DeleteAlarmFilter-request", log.Fields{"id": *id}) |
| 618 | if isTestMode(ctx) { |
| 619 | out := new(empty.Empty) |
| 620 | return out, nil |
| 621 | } |
| 622 | return nil, errors.New("UnImplemented") |
| 623 | } |
| 624 | |
| 625 | func (handler *APIHandler) SelfTest(ctx context.Context, id *voltha.ID) (*voltha.SelfTestResponse, error) { |
| 626 | log.Debugw("SelfTest-request", log.Fields{"id": id}) |
| 627 | if isTestMode(ctx) { |
| 628 | resp := &voltha.SelfTestResponse{Result: voltha.SelfTestResponse_SUCCESS} |
| 629 | return resp, nil |
| 630 | } |
| 631 | return nil, errors.New("UnImplemented") |
| 632 | } |
Stephane Barbarie | 6e1bd50 | 2018-11-05 22:44:45 -0500 | [diff] [blame] | 633 | |
| 634 | func (handler *APIHandler) forwardPacketOut(packet *openflow_13.PacketOut) { |
| 635 | log.Debugw("forwardPacketOut-request", log.Fields{"packet": packet}) |
Richard Jankowski | 2755adf | 2019-01-17 17:16:48 -0500 | [diff] [blame] | 636 | agent := handler.logicalDeviceMgr.getLogicalDeviceAgent(packet.Id) |
| 637 | agent.packetOut(packet.PacketOut) |
Stephane Barbarie | 6e1bd50 | 2018-11-05 22:44:45 -0500 | [diff] [blame] | 638 | } |
| 639 | func (handler *APIHandler) StreamPacketsOut( |
| 640 | packets voltha.VolthaService_StreamPacketsOutServer, |
| 641 | ) error { |
| 642 | log.Debugw("StreamPacketsOut-request", log.Fields{"packets": packets}) |
| 643 | for { |
| 644 | packet, err := packets.Recv() |
| 645 | |
| 646 | if err == io.EOF { |
| 647 | break |
| 648 | } else if err != nil { |
| 649 | log.Errorw("Failed to receive packet", log.Fields{"error": err}) |
| 650 | } |
| 651 | |
| 652 | handler.forwardPacketOut(packet) |
| 653 | } |
| 654 | |
| 655 | log.Debugw("StreamPacketsOut-request-done", log.Fields{"packets": packets}) |
| 656 | return nil |
| 657 | } |
| 658 | |
| 659 | func (handler *APIHandler) sendPacketIn(deviceId string, packet *openflow_13.OfpPacketIn) { |
| 660 | packetIn := openflow_13.PacketIn{Id: deviceId, PacketIn: packet} |
| 661 | log.Debugw("sendPacketIn", log.Fields{"packetIn": packetIn}) |
Richard Jankowski | dbab94a | 2018-12-06 16:20:25 -0500 | [diff] [blame] | 662 | // Enqueue the packet |
| 663 | if err := handler.packetInQueue.Put(packetIn); err != nil { |
| 664 | log.Errorw("failed-to-enqueue-packet", log.Fields{"error": err}) |
| 665 | } |
Stephane Barbarie | 6e1bd50 | 2018-11-05 22:44:45 -0500 | [diff] [blame] | 666 | } |
| 667 | |
| 668 | func (handler *APIHandler) ReceivePacketsIn( |
| 669 | empty *empty.Empty, |
| 670 | packetsIn voltha.VolthaService_ReceivePacketsInServer, |
| 671 | ) error { |
| 672 | log.Debugw("ReceivePacketsIn-request", log.Fields{"packetsIn": packetsIn}) |
| 673 | |
| 674 | for { |
Richard Jankowski | dbab94a | 2018-12-06 16:20:25 -0500 | [diff] [blame] | 675 | // Dequeue a packet |
| 676 | if packets, err := handler.packetInQueue.Get(1); err == nil { |
| 677 | log.Debugw("dequeued-packet", log.Fields{"packet": packets[0]}) |
| 678 | if packet, ok := packets[0].(openflow_13.PacketIn); ok { |
| 679 | log.Debugw("sending-packet-in", log.Fields{"packet": packet}) |
| 680 | if err := packetsIn.Send(&packet); err != nil { |
| 681 | log.Errorw("failed-to-send-packet", log.Fields{"error": err}) |
| 682 | } |
| 683 | } |
Stephane Barbarie | 6e1bd50 | 2018-11-05 22:44:45 -0500 | [diff] [blame] | 684 | } |
| 685 | } |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 686 | //TODO: FInd an elegant way to get out of the above loop when the Core is stopped |
Stephane Barbarie | 6e1bd50 | 2018-11-05 22:44:45 -0500 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | func (handler *APIHandler) sendChangeEvent(deviceId string, portStatus *openflow_13.OfpPortStatus) { |
| 690 | // TODO: validate the type of portStatus parameter |
| 691 | //if _, ok := portStatus.(*openflow_13.OfpPortStatus); ok { |
| 692 | //} |
| 693 | event := openflow_13.ChangeEvent{Id: deviceId, Event: &openflow_13.ChangeEvent_PortStatus{PortStatus: portStatus}} |
| 694 | log.Debugw("sendChangeEvent", log.Fields{"event": event}) |
| 695 | // TODO: put the packet in the queue |
| 696 | } |
| 697 | |
| 698 | func (handler *APIHandler) ReceiveChangeEvents( |
| 699 | empty *empty.Empty, |
| 700 | changeEvents voltha.VolthaService_ReceiveChangeEventsServer, |
| 701 | ) error { |
| 702 | log.Debugw("ReceiveChangeEvents-request", log.Fields{"changeEvents": changeEvents}) |
| 703 | for { |
| 704 | // TODO: need to retrieve packet from queue |
| 705 | event := &openflow_13.ChangeEvent{} |
| 706 | time.Sleep(time.Duration(5) * time.Second) |
| 707 | err := changeEvents.Send(event) |
| 708 | if err != nil { |
| 709 | log.Errorw("Failed to send change event", log.Fields{"error": err}) |
| 710 | } |
| 711 | } |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 712 | // TODO: put the packet in the queue |
| 713 | } |
Stephane Barbarie | 6e1bd50 | 2018-11-05 22:44:45 -0500 | [diff] [blame] | 714 | |
| 715 | func (handler *APIHandler) Subscribe( |
| 716 | ctx context.Context, |
| 717 | ofAgent *voltha.OfAgentSubscriber, |
| 718 | ) (*voltha.OfAgentSubscriber, error) { |
| 719 | log.Debugw("Subscribe-request", log.Fields{"ofAgent": ofAgent}) |
| 720 | return &voltha.OfAgentSubscriber{OfagentId: ofAgent.OfagentId, VolthaId: ofAgent.VolthaId}, nil |
| 721 | } |