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