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