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