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