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