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