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