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