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