khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 3 | |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 16 | package core |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 17 | |
| 18 | import ( |
| 19 | "context" |
| 20 | "errors" |
| 21 | "github.com/golang/protobuf/ptypes/empty" |
| 22 | da "github.com/opencord/voltha-go/common/core/northbound/grpc" |
| 23 | "github.com/opencord/voltha-go/common/log" |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 24 | "github.com/opencord/voltha-go/rw_core/utils" |
William Kurkian | daa6bb2 | 2019-03-07 12:26:28 -0500 | [diff] [blame] | 25 | "github.com/opencord/voltha-protos/go/common" |
William Kurkian | daa6bb2 | 2019-03-07 12:26:28 -0500 | [diff] [blame] | 26 | "github.com/opencord/voltha-protos/go/omci" |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 27 | "github.com/opencord/voltha-protos/go/openflow_13" |
William Kurkian | daa6bb2 | 2019-03-07 12:26:28 -0500 | [diff] [blame] | 28 | "github.com/opencord/voltha-protos/go/voltha" |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 29 | "google.golang.org/grpc/codes" |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 30 | "google.golang.org/grpc/metadata" |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 31 | "google.golang.org/grpc/status" |
Stephane Barbarie | 6e1bd50 | 2018-11-05 22:44:45 -0500 | [diff] [blame] | 32 | "io" |
A R Karthick | 881e7ea | 2019-08-19 19:44:02 +0000 | [diff] [blame] | 33 | "sync" |
Stephane Barbarie | 6e1bd50 | 2018-11-05 22:44:45 -0500 | [diff] [blame] | 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 ( |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 38 | IMAGE_DOWNLOAD = iota |
| 39 | CANCEL_IMAGE_DOWNLOAD = iota |
| 40 | ACTIVATE_IMAGE = iota |
| 41 | REVERT_IMAGE = iota |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 42 | ) |
| 43 | |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 44 | type APIHandler struct { |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 45 | deviceMgr *DeviceManager |
| 46 | logicalDeviceMgr *LogicalDeviceManager |
| 47 | adapterMgr *AdapterManager |
A R Karthick | 881e7ea | 2019-08-19 19:44:02 +0000 | [diff] [blame] | 48 | packetInQueue chan openflow_13.PacketIn |
| 49 | changeEventQueue chan openflow_13.ChangeEvent |
| 50 | packetInQueueDone chan bool |
| 51 | changeEventQueueDone chan bool |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 52 | coreInCompetingMode bool |
khenaidoo | b608032 | 2019-01-29 21:47:38 -0500 | [diff] [blame] | 53 | longRunningRequestTimeout int64 |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 54 | defaultRequestTimeout int64 |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 55 | da.DefaultAPIHandler |
khenaidoo | 54e0ddf | 2019-02-27 16:21:33 -0500 | [diff] [blame] | 56 | core *Core |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 57 | } |
| 58 | |
khenaidoo | 54e0ddf | 2019-02-27 16:21:33 -0500 | [diff] [blame] | 59 | func NewAPIHandler(core *Core) *APIHandler { |
Stephane Barbarie | 6e1bd50 | 2018-11-05 22:44:45 -0500 | [diff] [blame] | 60 | handler := &APIHandler{ |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 61 | deviceMgr: core.deviceMgr, |
| 62 | logicalDeviceMgr: core.logicalDeviceMgr, |
| 63 | adapterMgr: core.adapterMgr, |
| 64 | coreInCompetingMode: core.config.InCompetingMode, |
| 65 | longRunningRequestTimeout: core.config.LongRunningRequestTimeout, |
| 66 | defaultRequestTimeout: core.config.DefaultRequestTimeout, |
A R Karthick | 881e7ea | 2019-08-19 19:44:02 +0000 | [diff] [blame] | 67 | packetInQueue: make(chan openflow_13.PacketIn, 100), |
| 68 | changeEventQueue: make(chan openflow_13.ChangeEvent, 100), |
| 69 | packetInQueueDone: make(chan bool, 1), |
| 70 | changeEventQueueDone: make(chan bool, 1), |
| 71 | core: core, |
Stephane Barbarie | 6e1bd50 | 2018-11-05 22:44:45 -0500 | [diff] [blame] | 72 | } |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 73 | return handler |
| 74 | } |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 75 | |
| 76 | // 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] | 77 | func isTestMode(ctx context.Context) bool { |
| 78 | md, _ := metadata.FromIncomingContext(ctx) |
| 79 | _, exist := md[common.TestModeKeys_api_test.String()] |
| 80 | return exist |
| 81 | } |
| 82 | |
Richard Jankowski | d42826e | 2018-11-02 16:06:37 -0400 | [diff] [blame] | 83 | // This function attempts to extract the serial number from the request metadata |
| 84 | // and create a KV transaction for that serial number for the current core. |
| 85 | func (handler *APIHandler) createKvTransaction(ctx context.Context) (*KVTransaction, error) { |
| 86 | var ( |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 87 | err error |
| 88 | ok bool |
| 89 | md metadata.MD |
Richard Jankowski | d42826e | 2018-11-02 16:06:37 -0400 | [diff] [blame] | 90 | serNum []string |
| 91 | ) |
| 92 | if md, ok = metadata.FromIncomingContext(ctx); !ok { |
| 93 | err = errors.New("metadata-not-found") |
| 94 | } else if serNum, ok = md["voltha_serial_number"]; !ok { |
| 95 | err = errors.New("serial-number-not-found") |
| 96 | } |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 97 | if !ok || serNum == nil { |
Richard Jankowski | d42826e | 2018-11-02 16:06:37 -0400 | [diff] [blame] | 98 | log.Error(err) |
| 99 | return nil, err |
| 100 | } |
| 101 | // Create KV transaction |
| 102 | txn := NewKVTransaction(serNum[0]) |
| 103 | return txn, nil |
| 104 | } |
| 105 | |
Richard Jankowski | 2755adf | 2019-01-17 17:16:48 -0500 | [diff] [blame] | 106 | // isOFControllerRequest is a helper function to determine if a request was initiated |
| 107 | // from the OpenFlow controller (or its proxy, e.g. OFAgent) |
Richard Jankowski | 46464e9 | 2019-03-05 11:53:55 -0500 | [diff] [blame] | 108 | func (handler *APIHandler) isOFControllerRequest(ctx context.Context) bool { |
khenaidoo | 9cdc1a6 | 2019-01-24 21:57:40 -0500 | [diff] [blame] | 109 | if md, ok := metadata.FromIncomingContext(ctx); ok { |
| 110 | // Metadata in context |
Richard Jankowski | 46464e9 | 2019-03-05 11:53:55 -0500 | [diff] [blame] | 111 | if _, ok = md[handler.core.config.CoreBindingKey]; ok { |
khenaidoo | 9cdc1a6 | 2019-01-24 21:57:40 -0500 | [diff] [blame] | 112 | // OFAgent field in metadata |
khenaidoo | 3d3b8c2 | 2019-05-22 18:10:39 -0400 | [diff] [blame] | 113 | log.Debug("OFController-request") |
khenaidoo | 9cdc1a6 | 2019-01-24 21:57:40 -0500 | [diff] [blame] | 114 | return true |
| 115 | } |
Richard Jankowski | 2755adf | 2019-01-17 17:16:48 -0500 | [diff] [blame] | 116 | } |
khenaidoo | 3d3b8c2 | 2019-05-22 18:10:39 -0400 | [diff] [blame] | 117 | log.Debug("not-OFController-request") |
khenaidoo | 9cdc1a6 | 2019-01-24 21:57:40 -0500 | [diff] [blame] | 118 | return false |
| 119 | } |
| 120 | |
| 121 | // competeForTransaction is a helper function to determine whether every request needs to compete with another |
| 122 | // Core to execute the request |
| 123 | func (handler *APIHandler) competeForTransaction() bool { |
| 124 | return handler.coreInCompetingMode |
| 125 | } |
| 126 | |
khenaidoo | 631fe54 | 2019-05-31 15:44:43 -0400 | [diff] [blame] | 127 | // acquireRequest handles transaction processing for device creation and list requests, i.e. when there are no |
| 128 | // specific id requested (list scenario) or id present in the request (creation use case). |
| 129 | func (handler *APIHandler) acquireRequest(ctx context.Context, maxTimeout ...int64) (*KVTransaction, error) { |
khenaidoo | 43aa6bd | 2019-05-29 13:35:13 -0400 | [diff] [blame] | 130 | timeout := handler.defaultRequestTimeout |
| 131 | if len(maxTimeout) > 0 { |
| 132 | timeout = maxTimeout[0] |
| 133 | } |
| 134 | log.Debugw("transaction-timeout", log.Fields{"timeout": timeout}) |
| 135 | txn, err := handler.createKvTransaction(ctx) |
| 136 | if txn == nil { |
| 137 | return nil, err |
| 138 | } else if txn.Acquired(timeout) { |
| 139 | return txn, nil |
| 140 | } else { |
| 141 | return nil, errors.New("failed-to-seize-request") |
| 142 | } |
| 143 | } |
| 144 | |
khenaidoo | 6d62c00 | 2019-05-15 21:57:03 -0400 | [diff] [blame] | 145 | // takeRequestOwnership creates a transaction in the dB for this request and handles the logic of transaction |
| 146 | // acquisition. If the device is owned by this Core (in a core-pair) then acquire the transaction with a |
| 147 | // timeout value (in the event of a timeout the other Core in the core-pair will proceed with the transaction). If the |
| 148 | // device is not owned then this Core will just monitor the transaction for potential timeouts. |
Richard Jankowski | 199fd86 | 2019-03-18 14:49:51 -0400 | [diff] [blame] | 149 | func (handler *APIHandler) takeRequestOwnership(ctx context.Context, id interface{}, maxTimeout ...int64) (*KVTransaction, error) { |
khenaidoo | 6d62c00 | 2019-05-15 21:57:03 -0400 | [diff] [blame] | 150 | t := time.Now() |
Richard Jankowski | 199fd86 | 2019-03-18 14:49:51 -0400 | [diff] [blame] | 151 | timeout := handler.defaultRequestTimeout |
| 152 | if len(maxTimeout) > 0 { |
| 153 | timeout = maxTimeout[0] |
| 154 | } |
Richard Jankowski | 199fd86 | 2019-03-18 14:49:51 -0400 | [diff] [blame] | 155 | txn, err := handler.createKvTransaction(ctx) |
| 156 | if txn == nil { |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 157 | return nil, err |
Richard Jankowski | 199fd86 | 2019-03-18 14:49:51 -0400 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | owned := false |
| 161 | if id != nil { |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 162 | owned = handler.core.deviceOwnership.OwnedByMe(id) |
Richard Jankowski | 199fd86 | 2019-03-18 14:49:51 -0400 | [diff] [blame] | 163 | } |
| 164 | if owned { |
| 165 | if txn.Acquired(timeout) { |
khenaidoo | 6d62c00 | 2019-05-15 21:57:03 -0400 | [diff] [blame] | 166 | log.Debugw("acquired-transaction", log.Fields{"transaction-timeout": timeout}) |
Richard Jankowski | 199fd86 | 2019-03-18 14:49:51 -0400 | [diff] [blame] | 167 | return txn, nil |
| 168 | } else { |
| 169 | return nil, errors.New("failed-to-seize-request") |
| 170 | } |
| 171 | } else { |
| 172 | if txn.Monitor(timeout) { |
khenaidoo | 6d62c00 | 2019-05-15 21:57:03 -0400 | [diff] [blame] | 173 | log.Debugw("acquired-transaction-after-timeout", log.Fields{"timeout": timeout, "waited-time": time.Since(t)}) |
Richard Jankowski | 199fd86 | 2019-03-18 14:49:51 -0400 | [diff] [blame] | 174 | return txn, nil |
| 175 | } else { |
khenaidoo | 6d62c00 | 2019-05-15 21:57:03 -0400 | [diff] [blame] | 176 | log.Debugw("transaction-completed-by-other", log.Fields{"timeout": timeout, "waited-time": time.Since(t)}) |
| 177 | return nil, errors.New(string(COMPLETED_BY_OTHER)) |
Richard Jankowski | 199fd86 | 2019-03-18 14:49:51 -0400 | [diff] [blame] | 178 | } |
| 179 | } |
| 180 | } |
| 181 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 182 | // waitForNilResponseOnSuccess is a helper function to wait for a response on channel ch where an nil |
| 183 | // response is expected in a successful scenario |
| 184 | func waitForNilResponseOnSuccess(ctx context.Context, ch chan interface{}) (*empty.Empty, error) { |
| 185 | select { |
| 186 | case res := <-ch: |
| 187 | if res == nil { |
| 188 | return new(empty.Empty), nil |
| 189 | } else if err, ok := res.(error); ok { |
| 190 | return new(empty.Empty), err |
| 191 | } else { |
| 192 | log.Warnw("unexpected-return-type", log.Fields{"result": res}) |
| 193 | err = status.Errorf(codes.Internal, "%s", res) |
| 194 | return new(empty.Empty), err |
| 195 | } |
| 196 | case <-ctx.Done(): |
| 197 | log.Debug("client-timeout") |
| 198 | return nil, ctx.Err() |
| 199 | } |
| 200 | } |
| 201 | |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 202 | func (handler *APIHandler) UpdateLogLevel(ctx context.Context, logging *voltha.Logging) (*empty.Empty, error) { |
khenaidoo | 6f2fbe3 | 2019-01-18 16:16:50 -0500 | [diff] [blame] | 203 | log.Debugw("UpdateLogLevel-request", log.Fields{"package": logging.PackageName, "intval": int(logging.Level)}) |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 204 | out := new(empty.Empty) |
khenaidoo | 6f2fbe3 | 2019-01-18 16:16:50 -0500 | [diff] [blame] | 205 | if logging.PackageName == "" { |
| 206 | log.SetAllLogLevel(int(logging.Level)) |
| 207 | } else { |
| 208 | log.SetPackageLogLevel(logging.PackageName, int(logging.Level)) |
| 209 | } |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 210 | return out, nil |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 211 | } |
| 212 | |
khenaidoo | 43aa6bd | 2019-05-29 13:35:13 -0400 | [diff] [blame] | 213 | func (handler *APIHandler) GetLogicalDevicePort(ctx context.Context, id *voltha.LogicalPortId) (*voltha.LogicalPort, error) { |
| 214 | log.Debugw("GetLogicalDevicePort-request", log.Fields{"id": *id}) |
| 215 | |
| 216 | if handler.competeForTransaction() { |
| 217 | if txn, err := handler.takeRequestOwnership(ctx, &utils.LogicalDeviceID{Id: id.Id}); err != nil { |
| 218 | return &voltha.LogicalPort{}, err |
| 219 | } else { |
| 220 | defer txn.Close() |
| 221 | } |
| 222 | } |
| 223 | return handler.logicalDeviceMgr.getLogicalPort(id) |
| 224 | } |
| 225 | |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 226 | func (handler *APIHandler) EnableLogicalDevicePort(ctx context.Context, id *voltha.LogicalPortId) (*empty.Empty, error) { |
| 227 | log.Debugw("EnableLogicalDevicePort-request", log.Fields{"id": id, "test": common.TestModeKeys_api_test.String()}) |
| 228 | if isTestMode(ctx) { |
| 229 | out := new(empty.Empty) |
| 230 | return out, nil |
| 231 | } |
Richard Jankowski | 2755adf | 2019-01-17 17:16:48 -0500 | [diff] [blame] | 232 | |
khenaidoo | 9cdc1a6 | 2019-01-24 21:57:40 -0500 | [diff] [blame] | 233 | if handler.competeForTransaction() { |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 234 | 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] | 235 | return new(empty.Empty), err |
Richard Jankowski | 2755adf | 2019-01-17 17:16:48 -0500 | [diff] [blame] | 236 | } else { |
khenaidoo | 9cdc1a6 | 2019-01-24 21:57:40 -0500 | [diff] [blame] | 237 | defer txn.Close() |
Richard Jankowski | 2755adf | 2019-01-17 17:16:48 -0500 | [diff] [blame] | 238 | } |
| 239 | } |
khenaidoo | 9cdc1a6 | 2019-01-24 21:57:40 -0500 | [diff] [blame] | 240 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 241 | ch := make(chan interface{}) |
| 242 | defer close(ch) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 243 | go handler.logicalDeviceMgr.enableLogicalPort(ctx, id, ch) |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 244 | return waitForNilResponseOnSuccess(ctx, ch) |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | func (handler *APIHandler) DisableLogicalDevicePort(ctx context.Context, id *voltha.LogicalPortId) (*empty.Empty, error) { |
| 248 | log.Debugw("DisableLogicalDevicePort-request", log.Fields{"id": id, "test": common.TestModeKeys_api_test.String()}) |
| 249 | if isTestMode(ctx) { |
| 250 | out := new(empty.Empty) |
| 251 | return out, nil |
| 252 | } |
Richard Jankowski | 2755adf | 2019-01-17 17:16:48 -0500 | [diff] [blame] | 253 | |
khenaidoo | 9cdc1a6 | 2019-01-24 21:57:40 -0500 | [diff] [blame] | 254 | if handler.competeForTransaction() { |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 255 | 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] | 256 | return new(empty.Empty), err |
Richard Jankowski | 2755adf | 2019-01-17 17:16:48 -0500 | [diff] [blame] | 257 | } else { |
khenaidoo | 9cdc1a6 | 2019-01-24 21:57:40 -0500 | [diff] [blame] | 258 | defer txn.Close() |
Richard Jankowski | 2755adf | 2019-01-17 17:16:48 -0500 | [diff] [blame] | 259 | } |
| 260 | } |
khenaidoo | 9cdc1a6 | 2019-01-24 21:57:40 -0500 | [diff] [blame] | 261 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 262 | ch := make(chan interface{}) |
| 263 | defer close(ch) |
| 264 | go handler.logicalDeviceMgr.disableLogicalPort(ctx, id, ch) |
| 265 | return waitForNilResponseOnSuccess(ctx, ch) |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | func (handler *APIHandler) UpdateLogicalDeviceFlowTable(ctx context.Context, flow *openflow_13.FlowTableUpdate) (*empty.Empty, error) { |
| 269 | log.Debugw("UpdateLogicalDeviceFlowTable-request", log.Fields{"flow": flow, "test": common.TestModeKeys_api_test.String()}) |
| 270 | if isTestMode(ctx) { |
| 271 | out := new(empty.Empty) |
| 272 | return out, nil |
| 273 | } |
Richard Jankowski | 2755adf | 2019-01-17 17:16:48 -0500 | [diff] [blame] | 274 | |
khenaidoo | 9cdc1a6 | 2019-01-24 21:57:40 -0500 | [diff] [blame] | 275 | if handler.competeForTransaction() { |
khenaidoo | 43aa6bd | 2019-05-29 13:35:13 -0400 | [diff] [blame] | 276 | if txn, err := handler.takeRequestOwnership(ctx, &utils.LogicalDeviceID{Id: flow.Id}); err != nil { |
| 277 | return new(empty.Empty), err |
| 278 | } else { |
| 279 | defer txn.Close() |
Richard Jankowski | 2755adf | 2019-01-17 17:16:48 -0500 | [diff] [blame] | 280 | } |
| 281 | } |
khenaidoo | 9cdc1a6 | 2019-01-24 21:57:40 -0500 | [diff] [blame] | 282 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 283 | ch := make(chan interface{}) |
| 284 | defer close(ch) |
| 285 | go handler.logicalDeviceMgr.updateFlowTable(ctx, flow.Id, flow.FlowMod, ch) |
| 286 | return waitForNilResponseOnSuccess(ctx, ch) |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | func (handler *APIHandler) UpdateLogicalDeviceFlowGroupTable(ctx context.Context, flow *openflow_13.FlowGroupTableUpdate) (*empty.Empty, error) { |
| 290 | log.Debugw("UpdateLogicalDeviceFlowGroupTable-request", log.Fields{"flow": flow, "test": common.TestModeKeys_api_test.String()}) |
| 291 | if isTestMode(ctx) { |
| 292 | out := new(empty.Empty) |
| 293 | return out, nil |
| 294 | } |
Richard Jankowski | 2755adf | 2019-01-17 17:16:48 -0500 | [diff] [blame] | 295 | |
khenaidoo | 9cdc1a6 | 2019-01-24 21:57:40 -0500 | [diff] [blame] | 296 | if handler.competeForTransaction() { |
khenaidoo | 43aa6bd | 2019-05-29 13:35:13 -0400 | [diff] [blame] | 297 | if txn, err := handler.takeRequestOwnership(ctx, &utils.LogicalDeviceID{Id: flow.Id}); err != nil { |
| 298 | return new(empty.Empty), err |
| 299 | } else { |
| 300 | defer txn.Close() |
Richard Jankowski | 2755adf | 2019-01-17 17:16:48 -0500 | [diff] [blame] | 301 | } |
| 302 | } |
khenaidoo | 9cdc1a6 | 2019-01-24 21:57:40 -0500 | [diff] [blame] | 303 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 304 | ch := make(chan interface{}) |
| 305 | defer close(ch) |
| 306 | go handler.logicalDeviceMgr.updateGroupTable(ctx, flow.Id, flow.GroupMod, ch) |
| 307 | return waitForNilResponseOnSuccess(ctx, ch) |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 308 | } |
| 309 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 310 | // GetDevice must be implemented in the read-only containers - should it also be implemented here? |
| 311 | func (handler *APIHandler) GetDevice(ctx context.Context, id *voltha.ID) (*voltha.Device, error) { |
| 312 | log.Debugw("GetDevice-request", log.Fields{"id": id}) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 313 | return handler.deviceMgr.GetDevice(id.Id) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | // GetDevice must be implemented in the read-only containers - should it also be implemented here? |
| 317 | func (handler *APIHandler) ListDevices(ctx context.Context, empty *empty.Empty) (*voltha.Devices, error) { |
| 318 | log.Debug("ListDevices") |
| 319 | return handler.deviceMgr.ListDevices() |
| 320 | } |
| 321 | |
khenaidoo | 7ccedd5 | 2018-12-14 16:48:54 -0500 | [diff] [blame] | 322 | // ListDeviceIds returns the list of device ids managed by a voltha core |
| 323 | func (handler *APIHandler) ListDeviceIds(ctx context.Context, empty *empty.Empty) (*voltha.IDs, error) { |
| 324 | log.Debug("ListDeviceIDs") |
| 325 | if isTestMode(ctx) { |
| 326 | out := &voltha.IDs{Items: make([]*voltha.ID, 0)} |
| 327 | return out, nil |
| 328 | } |
| 329 | return handler.deviceMgr.ListDeviceIds() |
| 330 | } |
| 331 | |
| 332 | //ReconcileDevices is a request to a voltha core to managed a list of devices based on their IDs |
| 333 | func (handler *APIHandler) ReconcileDevices(ctx context.Context, ids *voltha.IDs) (*empty.Empty, error) { |
| 334 | log.Debug("ReconcileDevices") |
| 335 | if isTestMode(ctx) { |
| 336 | out := new(empty.Empty) |
| 337 | return out, nil |
| 338 | } |
Richard Jankowski | 2755adf | 2019-01-17 17:16:48 -0500 | [diff] [blame] | 339 | |
khenaidoo | 9cdc1a6 | 2019-01-24 21:57:40 -0500 | [diff] [blame] | 340 | // No need to grab a transaction as this request is core specific |
| 341 | |
khenaidoo | 7ccedd5 | 2018-12-14 16:48:54 -0500 | [diff] [blame] | 342 | ch := make(chan interface{}) |
| 343 | defer close(ch) |
| 344 | go handler.deviceMgr.ReconcileDevices(ctx, ids, ch) |
| 345 | return waitForNilResponseOnSuccess(ctx, ch) |
| 346 | } |
| 347 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 348 | func (handler *APIHandler) GetLogicalDevice(ctx context.Context, id *voltha.ID) (*voltha.LogicalDevice, error) { |
| 349 | log.Debugw("GetLogicalDevice-request", log.Fields{"id": id}) |
khenaidoo | 43aa6bd | 2019-05-29 13:35:13 -0400 | [diff] [blame] | 350 | if handler.competeForTransaction() { |
| 351 | if txn, err := handler.takeRequestOwnership(ctx, &utils.LogicalDeviceID{Id: id.Id}); err != nil { |
| 352 | return &voltha.LogicalDevice{}, err |
| 353 | } else { |
| 354 | defer txn.Close() |
| 355 | } |
| 356 | } |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 357 | return handler.logicalDeviceMgr.getLogicalDevice(id.Id) |
| 358 | } |
| 359 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 360 | func (handler *APIHandler) ListLogicalDevices(ctx context.Context, empty *empty.Empty) (*voltha.LogicalDevices, error) { |
khenaidoo | 43aa6bd | 2019-05-29 13:35:13 -0400 | [diff] [blame] | 361 | log.Debug("ListLogicalDevices-request") |
| 362 | if handler.competeForTransaction() { |
khenaidoo | 631fe54 | 2019-05-31 15:44:43 -0400 | [diff] [blame] | 363 | if txn, err := handler.acquireRequest(ctx); err != nil { |
khenaidoo | 43aa6bd | 2019-05-29 13:35:13 -0400 | [diff] [blame] | 364 | return &voltha.LogicalDevices{}, err |
| 365 | } else { |
| 366 | defer txn.Close() |
| 367 | } |
| 368 | } |
| 369 | if handler.isOFControllerRequest(ctx) { |
| 370 | // Since an OF controller is only interested in the set of logical devices managed by thgis Core then return |
| 371 | // only logical devices managed/monitored by this Core. |
| 372 | return handler.logicalDeviceMgr.listManagedLogicalDevices() |
| 373 | } |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 374 | return handler.logicalDeviceMgr.listLogicalDevices() |
| 375 | } |
| 376 | |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 377 | // ListAdapters returns the contents of all adapters known to the system |
| 378 | func (handler *APIHandler) ListAdapters(ctx context.Context, empty *empty.Empty) (*voltha.Adapters, error) { |
| 379 | log.Debug("ListDevices") |
| 380 | return handler.adapterMgr.listAdapters(ctx) |
| 381 | } |
| 382 | |
khenaidoo | dd23717 | 2019-05-27 16:37:17 -0400 | [diff] [blame] | 383 | func (handler *APIHandler) ListLogicalDeviceFlows(ctx context.Context, id *voltha.ID) (*openflow_13.Flows, error) { |
| 384 | log.Debugw("ListLogicalDeviceFlows", log.Fields{"id": *id}) |
khenaidoo | 43aa6bd | 2019-05-29 13:35:13 -0400 | [diff] [blame] | 385 | if handler.competeForTransaction() { |
| 386 | if txn, err := handler.takeRequestOwnership(ctx, &utils.LogicalDeviceID{Id: id.Id}); err != nil { |
| 387 | return &openflow_13.Flows{}, err |
| 388 | } else { |
| 389 | defer txn.Close() |
| 390 | } |
| 391 | } |
khenaidoo | dd23717 | 2019-05-27 16:37:17 -0400 | [diff] [blame] | 392 | return handler.logicalDeviceMgr.ListLogicalDeviceFlows(ctx, id.Id) |
| 393 | } |
| 394 | |
| 395 | func (handler *APIHandler) ListLogicalDeviceFlowGroups(ctx context.Context, id *voltha.ID) (*openflow_13.FlowGroups, error) { |
| 396 | log.Debugw("ListLogicalDeviceFlowGroups", log.Fields{"id": *id}) |
khenaidoo | 43aa6bd | 2019-05-29 13:35:13 -0400 | [diff] [blame] | 397 | if handler.competeForTransaction() { |
| 398 | if txn, err := handler.takeRequestOwnership(ctx, &utils.LogicalDeviceID{Id: id.Id}); err != nil { |
| 399 | return &openflow_13.FlowGroups{}, err |
| 400 | } else { |
| 401 | defer txn.Close() |
| 402 | } |
| 403 | } |
khenaidoo | dd23717 | 2019-05-27 16:37:17 -0400 | [diff] [blame] | 404 | return handler.logicalDeviceMgr.ListLogicalDeviceFlowGroups(ctx, id.Id) |
| 405 | } |
| 406 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 407 | func (handler *APIHandler) ListLogicalDevicePorts(ctx context.Context, id *voltha.ID) (*voltha.LogicalPorts, error) { |
| 408 | log.Debugw("ListLogicalDevicePorts", log.Fields{"logicaldeviceid": id}) |
khenaidoo | 43aa6bd | 2019-05-29 13:35:13 -0400 | [diff] [blame] | 409 | if handler.competeForTransaction() { |
| 410 | if txn, err := handler.takeRequestOwnership(ctx, &utils.LogicalDeviceID{Id: id.Id}); err != nil { |
| 411 | return &voltha.LogicalPorts{}, err |
| 412 | } else { |
| 413 | defer txn.Close() |
| 414 | } |
| 415 | } |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 416 | return handler.logicalDeviceMgr.ListLogicalDevicePorts(ctx, id.Id) |
| 417 | } |
| 418 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 419 | // CreateDevice creates a new parent device in the data model |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 420 | func (handler *APIHandler) CreateDevice(ctx context.Context, device *voltha.Device) (*voltha.Device, error) { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 421 | log.Debugw("createdevice", log.Fields{"device": *device}) |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 422 | if isTestMode(ctx) { |
| 423 | return &voltha.Device{Id: device.Id}, nil |
| 424 | } |
Richard Jankowski | d42826e | 2018-11-02 16:06:37 -0400 | [diff] [blame] | 425 | |
khenaidoo | 9cdc1a6 | 2019-01-24 21:57:40 -0500 | [diff] [blame] | 426 | if handler.competeForTransaction() { |
khenaidoo | 631fe54 | 2019-05-31 15:44:43 -0400 | [diff] [blame] | 427 | // There are no device Id present in this function. |
| 428 | if txn, err := handler.acquireRequest(ctx); err != nil { |
Richard Jankowski | 2755adf | 2019-01-17 17:16:48 -0500 | [diff] [blame] | 429 | return &voltha.Device{}, err |
Richard Jankowski | 2755adf | 2019-01-17 17:16:48 -0500 | [diff] [blame] | 430 | } else { |
khenaidoo | 9cdc1a6 | 2019-01-24 21:57:40 -0500 | [diff] [blame] | 431 | defer txn.Close() |
Richard Jankowski | 2755adf | 2019-01-17 17:16:48 -0500 | [diff] [blame] | 432 | } |
| 433 | } |
khenaidoo | 9cdc1a6 | 2019-01-24 21:57:40 -0500 | [diff] [blame] | 434 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 435 | ch := make(chan interface{}) |
| 436 | defer close(ch) |
| 437 | go handler.deviceMgr.createDevice(ctx, device, ch) |
| 438 | select { |
| 439 | case res := <-ch: |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 440 | if res != nil { |
| 441 | if err, ok := res.(error); ok { |
| 442 | return &voltha.Device{}, err |
| 443 | } |
| 444 | if d, ok := res.(*voltha.Device); ok { |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 445 | handler.core.deviceOwnership.OwnedByMe(&utils.DeviceID{Id: d.Id}) |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 446 | return d, nil |
| 447 | } |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 448 | } |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 449 | log.Warnw("create-device-unexpected-return-type", log.Fields{"result": res}) |
| 450 | err := status.Errorf(codes.Internal, "%s", res) |
| 451 | return &voltha.Device{}, err |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 452 | case <-ctx.Done(): |
| 453 | log.Debug("createdevice-client-timeout") |
| 454 | return nil, ctx.Err() |
| 455 | } |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 456 | } |
| 457 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 458 | // 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] | 459 | func (handler *APIHandler) EnableDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 460 | log.Debugw("enabledevice", log.Fields{"id": id}) |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 461 | if isTestMode(ctx) { |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 462 | return new(empty.Empty), nil |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 463 | } |
Richard Jankowski | d42826e | 2018-11-02 16:06:37 -0400 | [diff] [blame] | 464 | |
khenaidoo | 9cdc1a6 | 2019-01-24 21:57:40 -0500 | [diff] [blame] | 465 | if handler.competeForTransaction() { |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 466 | 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] | 467 | return new(empty.Empty), err |
Richard Jankowski | 2755adf | 2019-01-17 17:16:48 -0500 | [diff] [blame] | 468 | } else { |
khenaidoo | 9cdc1a6 | 2019-01-24 21:57:40 -0500 | [diff] [blame] | 469 | defer txn.Close() |
Richard Jankowski | 2755adf | 2019-01-17 17:16:48 -0500 | [diff] [blame] | 470 | } |
| 471 | } |
khenaidoo | 9cdc1a6 | 2019-01-24 21:57:40 -0500 | [diff] [blame] | 472 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 473 | ch := make(chan interface{}) |
| 474 | defer close(ch) |
| 475 | go handler.deviceMgr.enableDevice(ctx, id, ch) |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 476 | return waitForNilResponseOnSuccess(ctx, ch) |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 477 | } |
| 478 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 479 | // DisableDevice disables a device along with any child device it may have |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 480 | func (handler *APIHandler) DisableDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) { |
| 481 | log.Debugw("disabledevice-request", log.Fields{"id": id}) |
| 482 | if isTestMode(ctx) { |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 483 | return new(empty.Empty), nil |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 484 | } |
Richard Jankowski | 2755adf | 2019-01-17 17:16:48 -0500 | [diff] [blame] | 485 | |
khenaidoo | 9cdc1a6 | 2019-01-24 21:57:40 -0500 | [diff] [blame] | 486 | if handler.competeForTransaction() { |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 487 | 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] | 488 | return new(empty.Empty), err |
Richard Jankowski | 2755adf | 2019-01-17 17:16:48 -0500 | [diff] [blame] | 489 | } else { |
khenaidoo | 9cdc1a6 | 2019-01-24 21:57:40 -0500 | [diff] [blame] | 490 | defer txn.Close() |
Richard Jankowski | 2755adf | 2019-01-17 17:16:48 -0500 | [diff] [blame] | 491 | } |
| 492 | } |
khenaidoo | 9cdc1a6 | 2019-01-24 21:57:40 -0500 | [diff] [blame] | 493 | |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 494 | ch := make(chan interface{}) |
| 495 | defer close(ch) |
| 496 | go handler.deviceMgr.disableDevice(ctx, id, ch) |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 497 | return waitForNilResponseOnSuccess(ctx, ch) |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 498 | } |
| 499 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 500 | //RebootDevice invoked the reboot API to the corresponding adapter |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 501 | func (handler *APIHandler) RebootDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) { |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 502 | log.Debugw("rebootDevice-request", log.Fields{"id": id}) |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 503 | if isTestMode(ctx) { |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 504 | return new(empty.Empty), nil |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 505 | } |
Richard Jankowski | 2755adf | 2019-01-17 17:16:48 -0500 | [diff] [blame] | 506 | |
khenaidoo | 9cdc1a6 | 2019-01-24 21:57:40 -0500 | [diff] [blame] | 507 | if handler.competeForTransaction() { |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 508 | 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] | 509 | return new(empty.Empty), err |
Richard Jankowski | 2755adf | 2019-01-17 17:16:48 -0500 | [diff] [blame] | 510 | } else { |
khenaidoo | 9cdc1a6 | 2019-01-24 21:57:40 -0500 | [diff] [blame] | 511 | defer txn.Close() |
Richard Jankowski | 2755adf | 2019-01-17 17:16:48 -0500 | [diff] [blame] | 512 | } |
| 513 | } |
khenaidoo | 9cdc1a6 | 2019-01-24 21:57:40 -0500 | [diff] [blame] | 514 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 515 | ch := make(chan interface{}) |
| 516 | defer close(ch) |
| 517 | go handler.deviceMgr.rebootDevice(ctx, id, ch) |
| 518 | return waitForNilResponseOnSuccess(ctx, ch) |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 519 | } |
| 520 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 521 | // DeleteDevice removes a device from the data model |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 522 | func (handler *APIHandler) DeleteDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) { |
| 523 | log.Debugw("deletedevice-request", log.Fields{"id": id}) |
| 524 | if isTestMode(ctx) { |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 525 | return new(empty.Empty), nil |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 526 | } |
Richard Jankowski | 2755adf | 2019-01-17 17:16:48 -0500 | [diff] [blame] | 527 | |
khenaidoo | 9cdc1a6 | 2019-01-24 21:57:40 -0500 | [diff] [blame] | 528 | if handler.competeForTransaction() { |
khenaidoo | 6d62c00 | 2019-05-15 21:57:03 -0400 | [diff] [blame] | 529 | if txn, err := handler.takeRequestOwnership(ctx, &utils.DeviceID{Id: id.Id}); err != nil { |
| 530 | // Remove the device in memory |
| 531 | if err.Error() == (errors.New(string(COMPLETED_BY_OTHER)).Error()) { |
| 532 | handler.deviceMgr.stopManagingDevice(id.Id) |
| 533 | } |
Richard Jankowski | 2755adf | 2019-01-17 17:16:48 -0500 | [diff] [blame] | 534 | return new(empty.Empty), err |
Richard Jankowski | 2755adf | 2019-01-17 17:16:48 -0500 | [diff] [blame] | 535 | } else { |
khenaidoo | 9cdc1a6 | 2019-01-24 21:57:40 -0500 | [diff] [blame] | 536 | defer txn.Close() |
Richard Jankowski | 2755adf | 2019-01-17 17:16:48 -0500 | [diff] [blame] | 537 | } |
| 538 | } |
khenaidoo | 9cdc1a6 | 2019-01-24 21:57:40 -0500 | [diff] [blame] | 539 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 540 | ch := make(chan interface{}) |
| 541 | defer close(ch) |
| 542 | go handler.deviceMgr.deleteDevice(ctx, id, ch) |
| 543 | return waitForNilResponseOnSuccess(ctx, ch) |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 544 | } |
| 545 | |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 546 | // processImageRequest is a helper method to execute an image download request |
| 547 | func (handler *APIHandler) processImageRequest(ctx context.Context, img *voltha.ImageDownload, requestType int) (*common.OperationResp, error) { |
| 548 | log.Debugw("processImageDownload", log.Fields{"img": *img, "requestType": requestType}) |
| 549 | if isTestMode(ctx) { |
| 550 | resp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS} |
| 551 | return resp, nil |
| 552 | } |
| 553 | |
khenaidoo | 9cdc1a6 | 2019-01-24 21:57:40 -0500 | [diff] [blame] | 554 | if handler.competeForTransaction() { |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 555 | if txn, err := handler.takeRequestOwnership(ctx, &utils.DeviceID{Id: img.Id}); err != nil { |
khenaidoo | 9cdc1a6 | 2019-01-24 21:57:40 -0500 | [diff] [blame] | 556 | return &common.OperationResp{}, err |
| 557 | } else { |
| 558 | defer txn.Close() |
| 559 | } |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 560 | } |
| 561 | |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 562 | failedresponse := &common.OperationResp{Code: voltha.OperationResp_OPERATION_FAILURE} |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 563 | |
| 564 | ch := make(chan interface{}) |
| 565 | defer close(ch) |
| 566 | switch requestType { |
| 567 | case IMAGE_DOWNLOAD: |
| 568 | go handler.deviceMgr.downloadImage(ctx, img, ch) |
| 569 | case CANCEL_IMAGE_DOWNLOAD: |
| 570 | go handler.deviceMgr.cancelImageDownload(ctx, img, ch) |
| 571 | case ACTIVATE_IMAGE: |
| 572 | go handler.deviceMgr.activateImage(ctx, img, ch) |
| 573 | case REVERT_IMAGE: |
| 574 | go handler.deviceMgr.revertImage(ctx, img, ch) |
| 575 | default: |
| 576 | log.Warn("invalid-request-type", log.Fields{"requestType": requestType}) |
| 577 | return failedresponse, status.Errorf(codes.InvalidArgument, "%d", requestType) |
| 578 | } |
| 579 | select { |
| 580 | case res := <-ch: |
| 581 | if res != nil { |
| 582 | if err, ok := res.(error); ok { |
| 583 | return failedresponse, err |
| 584 | } |
| 585 | if opResp, ok := res.(*common.OperationResp); ok { |
| 586 | return opResp, nil |
| 587 | } |
| 588 | } |
| 589 | log.Warnw("download-image-unexpected-return-type", log.Fields{"result": res}) |
| 590 | return failedresponse, status.Errorf(codes.Internal, "%s", res) |
| 591 | case <-ctx.Done(): |
| 592 | log.Debug("downloadImage-client-timeout") |
| 593 | return nil, ctx.Err() |
| 594 | } |
| 595 | } |
| 596 | |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 597 | func (handler *APIHandler) DownloadImage(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) { |
| 598 | log.Debugw("DownloadImage-request", log.Fields{"img": *img}) |
| 599 | if isTestMode(ctx) { |
| 600 | resp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS} |
| 601 | return resp, nil |
| 602 | } |
| 603 | |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 604 | return handler.processImageRequest(ctx, img, IMAGE_DOWNLOAD) |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | func (handler *APIHandler) CancelImageDownload(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) { |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 608 | log.Debugw("cancelImageDownload-request", log.Fields{"img": *img}) |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 609 | if isTestMode(ctx) { |
| 610 | resp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS} |
| 611 | return resp, nil |
| 612 | } |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 613 | return handler.processImageRequest(ctx, img, CANCEL_IMAGE_DOWNLOAD) |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | func (handler *APIHandler) ActivateImageUpdate(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) { |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 617 | log.Debugw("activateImageUpdate-request", log.Fields{"img": *img}) |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 618 | if isTestMode(ctx) { |
| 619 | resp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS} |
| 620 | return resp, nil |
| 621 | } |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 622 | |
| 623 | return handler.processImageRequest(ctx, img, ACTIVATE_IMAGE) |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 624 | } |
| 625 | |
| 626 | func (handler *APIHandler) RevertImageUpdate(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) { |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 627 | log.Debugw("revertImageUpdate-request", log.Fields{"img": *img}) |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 628 | if isTestMode(ctx) { |
| 629 | resp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS} |
| 630 | return resp, nil |
| 631 | } |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 632 | |
| 633 | return handler.processImageRequest(ctx, img, REVERT_IMAGE) |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 634 | } |
| 635 | |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 636 | func (handler *APIHandler) GetImageDownloadStatus(ctx context.Context, img *voltha.ImageDownload) (*voltha.ImageDownload, error) { |
| 637 | log.Debugw("getImageDownloadStatus-request", log.Fields{"img": *img}) |
| 638 | if isTestMode(ctx) { |
Stephane Barbarie | df5479f | 2019-01-29 22:13:00 -0500 | [diff] [blame] | 639 | resp := &voltha.ImageDownload{DownloadState: voltha.ImageDownload_DOWNLOAD_SUCCEEDED} |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 640 | return resp, nil |
| 641 | } |
| 642 | |
Stephane Barbarie | df5479f | 2019-01-29 22:13:00 -0500 | [diff] [blame] | 643 | failedresponse := &voltha.ImageDownload{DownloadState: voltha.ImageDownload_DOWNLOAD_UNKNOWN} |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 644 | |
khenaidoo | 9cdc1a6 | 2019-01-24 21:57:40 -0500 | [diff] [blame] | 645 | if handler.competeForTransaction() { |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 646 | if txn, err := handler.takeRequestOwnership(ctx, &utils.DeviceID{Id: img.Id}); err != nil { |
khenaidoo | 9cdc1a6 | 2019-01-24 21:57:40 -0500 | [diff] [blame] | 647 | return failedresponse, err |
| 648 | } else { |
| 649 | defer txn.Close() |
| 650 | } |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 651 | } |
| 652 | |
| 653 | ch := make(chan interface{}) |
| 654 | defer close(ch) |
| 655 | go handler.deviceMgr.getImageDownloadStatus(ctx, img, ch) |
| 656 | |
| 657 | select { |
| 658 | case res := <-ch: |
| 659 | if res != nil { |
| 660 | if err, ok := res.(error); ok { |
| 661 | return failedresponse, err |
| 662 | } |
| 663 | if downloadResp, ok := res.(*voltha.ImageDownload); ok { |
| 664 | return downloadResp, nil |
| 665 | } |
| 666 | } |
| 667 | log.Warnw("download-image-status", log.Fields{"result": res}) |
| 668 | return failedresponse, status.Errorf(codes.Internal, "%s", res) |
| 669 | case <-ctx.Done(): |
| 670 | log.Debug("downloadImage-client-timeout") |
| 671 | return failedresponse, ctx.Err() |
| 672 | } |
| 673 | } |
| 674 | |
| 675 | func (handler *APIHandler) GetImageDownload(ctx context.Context, img *voltha.ImageDownload) (*voltha.ImageDownload, error) { |
| 676 | log.Debugw("GetImageDownload-request", log.Fields{"img": *img}) |
| 677 | if isTestMode(ctx) { |
Stephane Barbarie | df5479f | 2019-01-29 22:13:00 -0500 | [diff] [blame] | 678 | resp := &voltha.ImageDownload{DownloadState: voltha.ImageDownload_DOWNLOAD_SUCCEEDED} |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 679 | return resp, nil |
| 680 | } |
| 681 | |
| 682 | if download, err := handler.deviceMgr.getImageDownload(ctx, img); err != nil { |
Stephane Barbarie | df5479f | 2019-01-29 22:13:00 -0500 | [diff] [blame] | 683 | return &voltha.ImageDownload{DownloadState: voltha.ImageDownload_DOWNLOAD_UNKNOWN}, err |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 684 | } else { |
| 685 | return download, nil |
| 686 | } |
| 687 | } |
| 688 | |
| 689 | func (handler *APIHandler) ListImageDownloads(ctx context.Context, id *voltha.ID) (*voltha.ImageDownloads, error) { |
| 690 | log.Debugw("ListImageDownloads-request", log.Fields{"deviceId": id.Id}) |
| 691 | if isTestMode(ctx) { |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 692 | resp := &voltha.ImageDownloads{Items: []*voltha.ImageDownload{}} |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 693 | return resp, nil |
| 694 | } |
| 695 | |
| 696 | if downloads, err := handler.deviceMgr.listImageDownloads(ctx, id.Id); err != nil { |
| 697 | failedResp := &voltha.ImageDownloads{ |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 698 | Items: []*voltha.ImageDownload{ |
| 699 | {DownloadState: voltha.ImageDownload_DOWNLOAD_UNKNOWN}, |
| 700 | }, |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 701 | } |
| 702 | return failedResp, err |
| 703 | } else { |
| 704 | return downloads, nil |
| 705 | } |
| 706 | } |
| 707 | |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 708 | func (handler *APIHandler) UpdateDevicePmConfigs(ctx context.Context, configs *voltha.PmConfigs) (*empty.Empty, error) { |
| 709 | log.Debugw("UpdateDevicePmConfigs-request", log.Fields{"configs": *configs}) |
| 710 | if isTestMode(ctx) { |
| 711 | out := new(empty.Empty) |
| 712 | return out, nil |
| 713 | } |
khenaidoo | b312747 | 2019-07-24 21:04:55 -0400 | [diff] [blame] | 714 | if handler.competeForTransaction() { |
| 715 | if txn, err := handler.takeRequestOwnership(ctx, &utils.DeviceID{Id: configs.Id}); err != nil { |
| 716 | return new(empty.Empty), err |
| 717 | } else { |
| 718 | defer txn.Close() |
| 719 | } |
| 720 | } |
| 721 | |
| 722 | ch := make(chan interface{}) |
| 723 | defer close(ch) |
| 724 | go handler.deviceMgr.updatePmConfigs(ctx, configs, ch) |
| 725 | return waitForNilResponseOnSuccess(ctx, ch) |
| 726 | } |
| 727 | |
| 728 | func (handler *APIHandler) ListDevicePmConfigs(ctx context.Context, id *voltha.ID) (*voltha.PmConfigs, error) { |
| 729 | log.Debugw("ListDevicePmConfigs-request", log.Fields{"deviceId": *id}) |
| 730 | if handler.competeForTransaction() { |
| 731 | if txn, err := handler.takeRequestOwnership(ctx, &utils.LogicalDeviceID{Id: id.Id}); err != nil { |
| 732 | return &voltha.PmConfigs{}, err |
| 733 | } else { |
| 734 | defer txn.Close() |
| 735 | } |
| 736 | } |
| 737 | return handler.deviceMgr.listPmConfigs(ctx, id.Id) |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 738 | } |
| 739 | |
| 740 | func (handler *APIHandler) CreateAlarmFilter(ctx context.Context, filter *voltha.AlarmFilter) (*voltha.AlarmFilter, error) { |
| 741 | log.Debugw("CreateAlarmFilter-request", log.Fields{"filter": *filter}) |
| 742 | if isTestMode(ctx) { |
| 743 | f := &voltha.AlarmFilter{Id: filter.Id} |
| 744 | return f, nil |
| 745 | } |
| 746 | return nil, errors.New("UnImplemented") |
| 747 | } |
| 748 | |
| 749 | func (handler *APIHandler) UpdateAlarmFilter(ctx context.Context, filter *voltha.AlarmFilter) (*voltha.AlarmFilter, error) { |
| 750 | log.Debugw("UpdateAlarmFilter-request", log.Fields{"filter": *filter}) |
| 751 | if isTestMode(ctx) { |
| 752 | f := &voltha.AlarmFilter{Id: filter.Id} |
| 753 | return f, nil |
| 754 | } |
| 755 | return nil, errors.New("UnImplemented") |
| 756 | } |
| 757 | |
| 758 | func (handler *APIHandler) DeleteAlarmFilter(ctx context.Context, id *voltha.ID) (*empty.Empty, error) { |
| 759 | log.Debugw("DeleteAlarmFilter-request", log.Fields{"id": *id}) |
| 760 | if isTestMode(ctx) { |
| 761 | out := new(empty.Empty) |
| 762 | return out, nil |
| 763 | } |
| 764 | return nil, errors.New("UnImplemented") |
| 765 | } |
| 766 | |
| 767 | func (handler *APIHandler) SelfTest(ctx context.Context, id *voltha.ID) (*voltha.SelfTestResponse, error) { |
| 768 | log.Debugw("SelfTest-request", log.Fields{"id": id}) |
| 769 | if isTestMode(ctx) { |
| 770 | resp := &voltha.SelfTestResponse{Result: voltha.SelfTestResponse_SUCCESS} |
| 771 | return resp, nil |
| 772 | } |
| 773 | return nil, errors.New("UnImplemented") |
| 774 | } |
Stephane Barbarie | 6e1bd50 | 2018-11-05 22:44:45 -0500 | [diff] [blame] | 775 | |
| 776 | func (handler *APIHandler) forwardPacketOut(packet *openflow_13.PacketOut) { |
| 777 | log.Debugw("forwardPacketOut-request", log.Fields{"packet": packet}) |
khenaidoo | 3d3b8c2 | 2019-05-22 18:10:39 -0400 | [diff] [blame] | 778 | //TODO: Update this logic once the OF Controller (OFAgent in this case) can include a transaction Id in its |
| 779 | // request. For performance reason we can let both Cores in a Core-Pair forward the Packet to the adapters and |
| 780 | // let once of the shim layer (kafka proxy or adapter request handler filters out the duplicate packet) |
| 781 | if handler.core.deviceOwnership.OwnedByMe(&utils.LogicalDeviceID{Id: packet.Id}) { |
| 782 | agent := handler.logicalDeviceMgr.getLogicalDeviceAgent(packet.Id) |
| 783 | agent.packetOut(packet.PacketOut) |
| 784 | } |
Stephane Barbarie | 6e1bd50 | 2018-11-05 22:44:45 -0500 | [diff] [blame] | 785 | } |
khenaidoo | 3d3b8c2 | 2019-05-22 18:10:39 -0400 | [diff] [blame] | 786 | |
Stephane Barbarie | 6e1bd50 | 2018-11-05 22:44:45 -0500 | [diff] [blame] | 787 | func (handler *APIHandler) StreamPacketsOut( |
| 788 | packets voltha.VolthaService_StreamPacketsOutServer, |
| 789 | ) error { |
| 790 | log.Debugw("StreamPacketsOut-request", log.Fields{"packets": packets}) |
khenaidoo | 5e25069 | 2019-08-30 14:46:21 -0400 | [diff] [blame^] | 791 | loop: |
Stephane Barbarie | 6e1bd50 | 2018-11-05 22:44:45 -0500 | [diff] [blame] | 792 | for { |
khenaidoo | 5e25069 | 2019-08-30 14:46:21 -0400 | [diff] [blame^] | 793 | select { |
| 794 | case <-packets.Context().Done(): |
| 795 | log.Infow("StreamPacketsOut-context-done", log.Fields{"packets": packets, "error": packets.Context().Err()}) |
| 796 | break loop |
| 797 | default: |
| 798 | } |
| 799 | |
Stephane Barbarie | 6e1bd50 | 2018-11-05 22:44:45 -0500 | [diff] [blame] | 800 | packet, err := packets.Recv() |
| 801 | |
| 802 | if err == io.EOF { |
khenaidoo | 5e25069 | 2019-08-30 14:46:21 -0400 | [diff] [blame^] | 803 | log.Debugw("Received-EOF", log.Fields{"packets": packets}) |
| 804 | break loop |
| 805 | } |
| 806 | |
| 807 | if err != nil { |
| 808 | log.Errorw("Failed to receive packet out", log.Fields{"error": err}) |
| 809 | continue |
Stephane Barbarie | 6e1bd50 | 2018-11-05 22:44:45 -0500 | [diff] [blame] | 810 | } |
| 811 | |
| 812 | handler.forwardPacketOut(packet) |
| 813 | } |
| 814 | |
| 815 | log.Debugw("StreamPacketsOut-request-done", log.Fields{"packets": packets}) |
| 816 | return nil |
| 817 | } |
| 818 | |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 819 | func (handler *APIHandler) sendPacketIn(deviceId string, transationId string, packet *openflow_13.OfpPacketIn) { |
| 820 | // TODO: Augment the OF PacketIn to include the transactionId |
Stephane Barbarie | 6e1bd50 | 2018-11-05 22:44:45 -0500 | [diff] [blame] | 821 | packetIn := openflow_13.PacketIn{Id: deviceId, PacketIn: packet} |
| 822 | log.Debugw("sendPacketIn", log.Fields{"packetIn": packetIn}) |
A R Karthick | 881e7ea | 2019-08-19 19:44:02 +0000 | [diff] [blame] | 823 | handler.packetInQueue <- packetIn |
| 824 | } |
| 825 | |
| 826 | type callTracker struct { |
| 827 | failedPacket interface{} |
| 828 | } |
| 829 | type streamTracker struct { |
| 830 | calls map[string]*callTracker |
| 831 | sync.Mutex |
| 832 | } |
| 833 | |
| 834 | var streamingTracker = &streamTracker{calls: make(map[string]*callTracker)} |
| 835 | |
| 836 | func (handler *APIHandler) getStreamingTracker(method string, done chan<- bool) *callTracker { |
| 837 | streamingTracker.Lock() |
| 838 | defer streamingTracker.Unlock() |
| 839 | if _, ok := streamingTracker.calls[method]; ok { |
| 840 | // bail out the other packet in thread |
| 841 | log.Debugf("%s streaming call already running. Exiting it", method) |
| 842 | done <- true |
| 843 | log.Debugf("Last %s exited. Continuing ...", method) |
| 844 | } else { |
| 845 | streamingTracker.calls[method] = &callTracker{failedPacket: nil} |
Richard Jankowski | dbab94a | 2018-12-06 16:20:25 -0500 | [diff] [blame] | 846 | } |
A R Karthick | 881e7ea | 2019-08-19 19:44:02 +0000 | [diff] [blame] | 847 | return streamingTracker.calls[method] |
| 848 | } |
| 849 | |
| 850 | func (handler *APIHandler) flushFailedPackets(tracker *callTracker) error { |
| 851 | if tracker.failedPacket != nil { |
| 852 | switch tracker.failedPacket.(type) { |
| 853 | case openflow_13.PacketIn: |
| 854 | log.Debug("Enqueueing last failed packetIn") |
| 855 | handler.packetInQueue <- tracker.failedPacket.(openflow_13.PacketIn) |
| 856 | case openflow_13.ChangeEvent: |
| 857 | log.Debug("Enqueueing last failed changeEvent") |
| 858 | handler.changeEventQueue <- tracker.failedPacket.(openflow_13.ChangeEvent) |
| 859 | } |
| 860 | } |
| 861 | return nil |
Stephane Barbarie | 6e1bd50 | 2018-11-05 22:44:45 -0500 | [diff] [blame] | 862 | } |
| 863 | |
| 864 | func (handler *APIHandler) ReceivePacketsIn( |
| 865 | empty *empty.Empty, |
| 866 | packetsIn voltha.VolthaService_ReceivePacketsInServer, |
| 867 | ) error { |
A R Karthick | 881e7ea | 2019-08-19 19:44:02 +0000 | [diff] [blame] | 868 | var streamingTracker = handler.getStreamingTracker("ReceivePacketsIn", handler.packetInQueueDone) |
Stephane Barbarie | 6e1bd50 | 2018-11-05 22:44:45 -0500 | [diff] [blame] | 869 | log.Debugw("ReceivePacketsIn-request", log.Fields{"packetsIn": packetsIn}) |
| 870 | |
A R Karthick | 881e7ea | 2019-08-19 19:44:02 +0000 | [diff] [blame] | 871 | handler.flushFailedPackets(streamingTracker) |
| 872 | |
| 873 | loop: |
Stephane Barbarie | 6e1bd50 | 2018-11-05 22:44:45 -0500 | [diff] [blame] | 874 | for { |
A R Karthick | 881e7ea | 2019-08-19 19:44:02 +0000 | [diff] [blame] | 875 | select { |
| 876 | case packet := <-handler.packetInQueue: |
| 877 | log.Debugw("sending-packet-in", log.Fields{"packet": packet}) |
| 878 | if err := packetsIn.Send(&packet); err != nil { |
| 879 | log.Errorw("failed-to-send-packet", log.Fields{"error": err}) |
| 880 | // save the last failed packet in |
| 881 | streamingTracker.failedPacket = packet |
| 882 | } else { |
| 883 | if streamingTracker.failedPacket != nil { |
| 884 | // reset last failed packet saved to avoid flush |
| 885 | streamingTracker.failedPacket = nil |
Richard Jankowski | dbab94a | 2018-12-06 16:20:25 -0500 | [diff] [blame] | 886 | } |
| 887 | } |
A R Karthick | 881e7ea | 2019-08-19 19:44:02 +0000 | [diff] [blame] | 888 | case <-handler.packetInQueueDone: |
| 889 | log.Debug("Another ReceivePacketsIn running. Bailing out ...") |
| 890 | break loop |
Stephane Barbarie | 6e1bd50 | 2018-11-05 22:44:45 -0500 | [diff] [blame] | 891 | } |
| 892 | } |
A R Karthick | 881e7ea | 2019-08-19 19:44:02 +0000 | [diff] [blame] | 893 | |
| 894 | //TODO: Find an elegant way to get out of the above loop when the Core is stopped |
| 895 | return nil |
Stephane Barbarie | 6e1bd50 | 2018-11-05 22:44:45 -0500 | [diff] [blame] | 896 | } |
| 897 | |
| 898 | func (handler *APIHandler) sendChangeEvent(deviceId string, portStatus *openflow_13.OfpPortStatus) { |
| 899 | // TODO: validate the type of portStatus parameter |
| 900 | //if _, ok := portStatus.(*openflow_13.OfpPortStatus); ok { |
| 901 | //} |
| 902 | event := openflow_13.ChangeEvent{Id: deviceId, Event: &openflow_13.ChangeEvent_PortStatus{PortStatus: portStatus}} |
| 903 | log.Debugw("sendChangeEvent", log.Fields{"event": event}) |
A R Karthick | 881e7ea | 2019-08-19 19:44:02 +0000 | [diff] [blame] | 904 | handler.changeEventQueue <- event |
Stephane Barbarie | 6e1bd50 | 2018-11-05 22:44:45 -0500 | [diff] [blame] | 905 | } |
| 906 | |
| 907 | func (handler *APIHandler) ReceiveChangeEvents( |
| 908 | empty *empty.Empty, |
| 909 | changeEvents voltha.VolthaService_ReceiveChangeEventsServer, |
| 910 | ) error { |
A R Karthick | 881e7ea | 2019-08-19 19:44:02 +0000 | [diff] [blame] | 911 | var streamingTracker = handler.getStreamingTracker("ReceiveChangeEvents", handler.changeEventQueueDone) |
Stephane Barbarie | 6e1bd50 | 2018-11-05 22:44:45 -0500 | [diff] [blame] | 912 | log.Debugw("ReceiveChangeEvents-request", log.Fields{"changeEvents": changeEvents}) |
A R Karthick | 881e7ea | 2019-08-19 19:44:02 +0000 | [diff] [blame] | 913 | |
| 914 | handler.flushFailedPackets(streamingTracker) |
| 915 | |
| 916 | loop: |
Stephane Barbarie | 6e1bd50 | 2018-11-05 22:44:45 -0500 | [diff] [blame] | 917 | for { |
A R Karthick | 881e7ea | 2019-08-19 19:44:02 +0000 | [diff] [blame] | 918 | select { |
Richard Jankowski | 199fd86 | 2019-03-18 14:49:51 -0400 | [diff] [blame] | 919 | // Dequeue a change event |
A R Karthick | 881e7ea | 2019-08-19 19:44:02 +0000 | [diff] [blame] | 920 | case event := <-handler.changeEventQueue: |
| 921 | log.Debugw("sending-change-event", log.Fields{"event": event}) |
| 922 | if err := changeEvents.Send(&event); err != nil { |
| 923 | log.Errorw("failed-to-send-change-event", log.Fields{"error": err}) |
| 924 | // save last failed changeevent |
| 925 | streamingTracker.failedPacket = event |
| 926 | } else { |
| 927 | if streamingTracker.failedPacket != nil { |
| 928 | // reset last failed event saved on success to avoid flushing |
| 929 | streamingTracker.failedPacket = nil |
Richard Jankowski | 199fd86 | 2019-03-18 14:49:51 -0400 | [diff] [blame] | 930 | } |
| 931 | } |
A R Karthick | 881e7ea | 2019-08-19 19:44:02 +0000 | [diff] [blame] | 932 | case <-handler.changeEventQueueDone: |
| 933 | log.Debug("Another ReceiveChangeEvents already running. Bailing out ...") |
| 934 | break loop |
Stephane Barbarie | 6e1bd50 | 2018-11-05 22:44:45 -0500 | [diff] [blame] | 935 | } |
| 936 | } |
A R Karthick | 881e7ea | 2019-08-19 19:44:02 +0000 | [diff] [blame] | 937 | |
| 938 | return nil |
Richard Jankowski | 199fd86 | 2019-03-18 14:49:51 -0400 | [diff] [blame] | 939 | } |
Stephane Barbarie | 6e1bd50 | 2018-11-05 22:44:45 -0500 | [diff] [blame] | 940 | |
| 941 | func (handler *APIHandler) Subscribe( |
| 942 | ctx context.Context, |
| 943 | ofAgent *voltha.OfAgentSubscriber, |
| 944 | ) (*voltha.OfAgentSubscriber, error) { |
| 945 | log.Debugw("Subscribe-request", log.Fields{"ofAgent": ofAgent}) |
| 946 | return &voltha.OfAgentSubscriber{OfagentId: ofAgent.OfagentId, VolthaId: ofAgent.VolthaId}, nil |
| 947 | } |
William Kurkian | daa6bb2 | 2019-03-07 12:26:28 -0500 | [diff] [blame] | 948 | |
| 949 | //@TODO useless stub, what should this actually do? |
| 950 | func (handler *APIHandler) GetAlarmDeviceData( |
| 951 | ctx context.Context, |
| 952 | in *common.ID, |
| 953 | ) (*omci.AlarmDeviceData, error) { |
| 954 | log.Debug("GetAlarmDeviceData-stub") |
| 955 | return nil, nil |
| 956 | } |
| 957 | |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 958 | func (handler *APIHandler) ListLogicalDeviceMeters(ctx context.Context, id *voltha.ID) (*openflow_13.Meters, error) { |
| 959 | |
| 960 | log.Debugw("ListLogicalDeviceMeters", log.Fields{"id": *id}) |
| 961 | if handler.competeForTransaction() { |
| 962 | if txn, err := handler.takeRequestOwnership(ctx, &utils.LogicalDeviceID{Id: id.Id}); err != nil { |
| 963 | return nil, err // TODO: Return empty meter entry |
| 964 | } else { |
| 965 | defer txn.Close() |
| 966 | } |
| 967 | } |
| 968 | return handler.logicalDeviceMgr.ListLogicalDeviceMeters(ctx, id.Id) |
William Kurkian | daa6bb2 | 2019-03-07 12:26:28 -0500 | [diff] [blame] | 969 | } |
| 970 | |
| 971 | //@TODO useless stub, what should this actually do? |
| 972 | func (handler *APIHandler) GetMibDeviceData( |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 973 | ctx context.Context, |
| 974 | in *common.ID, |
William Kurkian | daa6bb2 | 2019-03-07 12:26:28 -0500 | [diff] [blame] | 975 | ) (*omci.MibDeviceData, error) { |
| 976 | log.Debug("GetMibDeviceData-stub") |
| 977 | return nil, nil |
| 978 | } |
| 979 | |
William Kurkian | daa6bb2 | 2019-03-07 12:26:28 -0500 | [diff] [blame] | 980 | func (handler *APIHandler) SimulateAlarm( |
| 981 | ctx context.Context, |
| 982 | in *voltha.SimulateAlarmRequest, |
| 983 | ) (*common.OperationResp, error) { |
serkant.uluderya | 334479d | 2019-04-10 08:26:15 -0700 | [diff] [blame] | 984 | log.Debugw("SimulateAlarm-request", log.Fields{"id": in.Id}) |
| 985 | successResp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS} |
| 986 | if isTestMode(ctx) { |
| 987 | return successResp, nil |
| 988 | } |
| 989 | |
| 990 | if handler.competeForTransaction() { |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 991 | if txn, err := handler.takeRequestOwnership(ctx, &utils.DeviceID{Id: in.Id}, handler.longRunningRequestTimeout); err != nil { |
| 992 | failedresponse := &common.OperationResp{Code: voltha.OperationResp_OPERATION_FAILURE} |
serkant.uluderya | 334479d | 2019-04-10 08:26:15 -0700 | [diff] [blame] | 993 | return failedresponse, err |
| 994 | } else { |
| 995 | defer txn.Close() |
| 996 | } |
| 997 | } |
| 998 | |
| 999 | ch := make(chan interface{}) |
| 1000 | defer close(ch) |
| 1001 | go handler.deviceMgr.simulateAlarm(ctx, in, ch) |
| 1002 | return successResp, nil |
William Kurkian | daa6bb2 | 2019-03-07 12:26:28 -0500 | [diff] [blame] | 1003 | } |
| 1004 | |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 1005 | // This function sends meter mod request to logical device manager and waits for response |
| 1006 | func (handler *APIHandler) UpdateLogicalDeviceMeterTable(ctx context.Context, meter *openflow_13.MeterModUpdate) (*empty.Empty, error) { |
| 1007 | log.Debugw("UpdateLogicalDeviceMeterTable-request", |
| 1008 | log.Fields{"meter": meter, "test": common.TestModeKeys_api_test.String()}) |
| 1009 | if isTestMode(ctx) { |
| 1010 | out := new(empty.Empty) |
| 1011 | return out, nil |
| 1012 | } |
| 1013 | |
| 1014 | if handler.competeForTransaction() { |
| 1015 | if txn, err := handler.takeRequestOwnership(ctx, &utils.LogicalDeviceID{Id: meter.Id}); err != nil { |
| 1016 | return new(empty.Empty), err |
| 1017 | } else { |
| 1018 | defer txn.Close() |
| 1019 | } |
| 1020 | } |
| 1021 | |
| 1022 | ch := make(chan interface{}) |
| 1023 | defer close(ch) |
| 1024 | go handler.logicalDeviceMgr.updateMeterTable(ctx, meter.Id, meter.MeterMod, ch) |
| 1025 | return waitForNilResponseOnSuccess(ctx, ch) |
William Kurkian | daa6bb2 | 2019-03-07 12:26:28 -0500 | [diff] [blame] | 1026 | } |