blob: fd54a505fcf4903a56e4aa417cf3cc692663b036 [file] [log] [blame]
khenaidoobf6e7bb2018-08-14 22:27:29 -04001/*
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 */
npujar1d86a522019-11-14 17:11:16 +053016
khenaidoob9203542018-09-17 22:56:37 -040017package core
khenaidoobf6e7bb2018-08-14 22:27:29 -040018
19import (
20 "context"
Matteo Scandolo360605d2019-11-05 18:29:17 -080021 "encoding/hex"
David Bainbridge4087cc52019-11-13 18:36:03 +000022 "encoding/json"
khenaidoobf6e7bb2018-08-14 22:27:29 -040023 "errors"
npujar1d86a522019-11-14 17:11:16 +053024 "io"
25 "sync"
26
khenaidoobf6e7bb2018-08-14 22:27:29 -040027 "github.com/golang/protobuf/ptypes/empty"
28 da "github.com/opencord/voltha-go/common/core/northbound/grpc"
khenaidoo1ce37ad2019-03-24 22:07:24 -040029 "github.com/opencord/voltha-go/rw_core/utils"
serkant.uluderya2ae470f2020-01-21 11:13:09 -080030 "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"
khenaidoob9203542018-09-17 22:56:37 -040036 "google.golang.org/grpc/codes"
khenaidoobf6e7bb2018-08-14 22:27:29 -040037 "google.golang.org/grpc/metadata"
khenaidoob9203542018-09-17 22:56:37 -040038 "google.golang.org/grpc/status"
khenaidoobf6e7bb2018-08-14 22:27:29 -040039)
40
npujar1d86a522019-11-14 17:11:16 +053041var errorIDNotFound = status.Error(codes.NotFound, "id-not-found")
khenaidoof684e1b2019-10-28 19:00:37 -040042
npujar1d86a522019-11-14 17:11:16 +053043// Image related constants
khenaidoof5a5bfa2019-01-23 22:20:29 -050044const (
npujar1d86a522019-11-14 17:11:16 +053045 ImageDownload = iota
46 CancelImageDownload = iota
47 ActivateImage = iota
48 RevertImage = iota
khenaidoof5a5bfa2019-01-23 22:20:29 -050049)
50
npujar1d86a522019-11-14 17:11:16 +053051// APIHandler represent attributes of API handler
khenaidoobf6e7bb2018-08-14 22:27:29 -040052type APIHandler struct {
khenaidoo2c6a0992019-04-29 13:46:56 -040053 deviceMgr *DeviceManager
54 logicalDeviceMgr *LogicalDeviceManager
55 adapterMgr *AdapterManager
A R Karthick881e7ea2019-08-19 19:44:02 +000056 packetInQueue chan openflow_13.PacketIn
57 changeEventQueue chan openflow_13.ChangeEvent
58 packetInQueueDone chan bool
59 changeEventQueueDone chan bool
khenaidoo2c6a0992019-04-29 13:46:56 -040060 coreInCompetingMode bool
khenaidoob6080322019-01-29 21:47:38 -050061 longRunningRequestTimeout int64
khenaidoo2c6a0992019-04-29 13:46:56 -040062 defaultRequestTimeout int64
khenaidoobf6e7bb2018-08-14 22:27:29 -040063 da.DefaultAPIHandler
khenaidoo54e0ddf2019-02-27 16:21:33 -050064 core *Core
khenaidoobf6e7bb2018-08-14 22:27:29 -040065}
66
npujar1d86a522019-11-14 17:11:16 +053067// NewAPIHandler creates API handler instance
khenaidoo54e0ddf2019-02-27 16:21:33 -050068func NewAPIHandler(core *Core) *APIHandler {
Stephane Barbarie6e1bd502018-11-05 22:44:45 -050069 handler := &APIHandler{
khenaidoo2c6a0992019-04-29 13:46:56 -040070 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 Karthick881e7ea2019-08-19 19:44:02 +000076 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 Barbarie6e1bd502018-11-05 22:44:45 -050081 }
khenaidoobf6e7bb2018-08-14 22:27:29 -040082 return handler
83}
khenaidoo4d4802d2018-10-04 21:59:49 -040084
85// isTestMode is a helper function to determine a function is invoked for testing only
khenaidoobf6e7bb2018-08-14 22:27:29 -040086func isTestMode(ctx context.Context) bool {
87 md, _ := metadata.FromIncomingContext(ctx)
88 _, exist := md[common.TestModeKeys_api_test.String()]
89 return exist
90}
91
Richard Jankowskid42826e2018-11-02 16:06:37 -040092// 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.
94func (handler *APIHandler) createKvTransaction(ctx context.Context) (*KVTransaction, error) {
95 var (
khenaidoo43c82122018-11-22 18:38:28 -050096 err error
97 ok bool
98 md metadata.MD
Richard Jankowskid42826e2018-11-02 16:06:37 -040099 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 }
khenaidoo2c6a0992019-04-29 13:46:56 -0400106 if !ok || serNum == nil {
Richard Jankowskid42826e2018-11-02 16:06:37 -0400107 log.Error(err)
108 return nil, err
109 }
110 // Create KV transaction
111 txn := NewKVTransaction(serNum[0])
112 return txn, nil
113}
114
Richard Jankowski2755adf2019-01-17 17:16:48 -0500115// isOFControllerRequest is a helper function to determine if a request was initiated
116// from the OpenFlow controller (or its proxy, e.g. OFAgent)
Richard Jankowski46464e92019-03-05 11:53:55 -0500117func (handler *APIHandler) isOFControllerRequest(ctx context.Context) bool {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500118 if md, ok := metadata.FromIncomingContext(ctx); ok {
119 // Metadata in context
Richard Jankowski46464e92019-03-05 11:53:55 -0500120 if _, ok = md[handler.core.config.CoreBindingKey]; ok {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500121 // OFAgent field in metadata
khenaidoo3d3b8c22019-05-22 18:10:39 -0400122 log.Debug("OFController-request")
khenaidoo9cdc1a62019-01-24 21:57:40 -0500123 return true
124 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500125 }
khenaidoo3d3b8c22019-05-22 18:10:39 -0400126 log.Debug("not-OFController-request")
khenaidoo9cdc1a62019-01-24 21:57:40 -0500127 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
132func (handler *APIHandler) competeForTransaction() bool {
133 return handler.coreInCompetingMode
134}
135
khenaidoo09771ef2019-10-11 14:25:02 -0400136// 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.
141func (handler *APIHandler) takeRequestOwnership(ctx context.Context, id interface{}, maxTimeout ...int64) (*KVTransaction, error) {
khenaidoo43aa6bd2019-05-29 13:35:13 -0400142 timeout := handler.defaultRequestTimeout
143 if len(maxTimeout) > 0 {
144 timeout = maxTimeout[0]
145 }
khenaidoo43aa6bd2019-05-29 13:35:13 -0400146 txn, err := handler.createKvTransaction(ctx)
147 if txn == nil {
148 return nil, err
khenaidoo09771ef2019-10-11 14:25:02 -0400149 }
150 var acquired bool
151 if id != nil {
152 var ownedByMe bool
npujar467fe752020-01-16 20:17:45 +0530153 if ownedByMe, err = handler.core.deviceOwnership.OwnedByMe(ctx, id); err != nil {
khenaidoo09771ef2019-10-11 14:25:02 -0400154 log.Warnw("getting-ownership-failed", log.Fields{"deviceId": id, "error": err})
npujar1d86a522019-11-14 17:11:16 +0530155 return nil, errorIDNotFound
khenaidoo09771ef2019-10-11 14:25:02 -0400156 }
npujar467fe752020-01-16 20:17:45 +0530157 acquired, err = txn.Acquired(ctx, timeout, ownedByMe)
khenaidoo09771ef2019-10-11 14:25:02 -0400158 } else {
npujar467fe752020-01-16 20:17:45 +0530159 acquired, err = txn.Acquired(ctx, timeout)
khenaidoo09771ef2019-10-11 14:25:02 -0400160 }
161 if err == nil && acquired {
npujar1d86a522019-11-14 17:11:16 +0530162 log.Debugw("transaction-acquired", log.Fields{"transactionId": txn.txnID})
khenaidoo43aa6bd2019-05-29 13:35:13 -0400163 return txn, nil
khenaidoo43aa6bd2019-05-29 13:35:13 -0400164 }
npujar1d86a522019-11-14 17:11:16 +0530165 log.Debugw("transaction-not-acquired", log.Fields{"transactionId": txn.txnID, "error": err})
166 return nil, errorTransactionNotAcquired
khenaidoo43aa6bd2019-05-29 13:35:13 -0400167}
168
khenaidoo09771ef2019-10-11 14:25:02 -0400169// waitForNilResponseOnSuccess is a helper function to wait for a response on channel monitorCh where an nil
khenaidoo4d4802d2018-10-04 21:59:49 -0400170// response is expected in a successful scenario
171func waitForNilResponseOnSuccess(ctx context.Context, ch chan interface{}) (*empty.Empty, error) {
172 select {
173 case res := <-ch:
174 if res == nil {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500175 return &empty.Empty{}, nil
khenaidoo4d4802d2018-10-04 21:59:49 -0400176 } else if err, ok := res.(error); ok {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500177 return &empty.Empty{}, err
khenaidoo4d4802d2018-10-04 21:59:49 -0400178 } else {
179 log.Warnw("unexpected-return-type", log.Fields{"result": res})
180 err = status.Errorf(codes.Internal, "%s", res)
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500181 return &empty.Empty{}, err
khenaidoo4d4802d2018-10-04 21:59:49 -0400182 }
183 case <-ctx.Done():
184 log.Debug("client-timeout")
185 return nil, ctx.Err()
186 }
187}
188
npujar1d86a522019-11-14 17:11:16 +0530189// UpdateLogLevel dynamically sets the log level of a given package to level
khenaidoobf6e7bb2018-08-14 22:27:29 -0400190func (handler *APIHandler) UpdateLogLevel(ctx context.Context, logging *voltha.Logging) (*empty.Empty, error) {
khenaidoo6f2fbe32019-01-18 16:16:50 -0500191 log.Debugw("UpdateLogLevel-request", log.Fields{"package": logging.PackageName, "intval": int(logging.Level)})
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500192
khenaidoo6f2fbe32019-01-18 16:16:50 -0500193 if logging.PackageName == "" {
194 log.SetAllLogLevel(int(logging.Level))
Scott Baker5f401472019-08-22 08:32:26 -0700195 log.SetDefaultLogLevel(int(logging.Level))
196 } else if logging.PackageName == "default" {
197 log.SetDefaultLogLevel(int(logging.Level))
khenaidoo6f2fbe32019-01-18 16:16:50 -0500198 } else {
199 log.SetPackageLogLevel(logging.PackageName, int(logging.Level))
200 }
Scott Baker5f401472019-08-22 08:32:26 -0700201
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500202 return &empty.Empty{}, nil
khenaidoobf6e7bb2018-08-14 22:27:29 -0400203}
204
npujar1d86a522019-11-14 17:11:16 +0530205// GetLogLevels returns log levels of packages
206func (APIHandler) GetLogLevels(ctx context.Context, in *voltha.LoggingComponent) (*voltha.Loggings, error) {
Scott Baker5f401472019-08-22 08:32:26 -0700207 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 Hagermanc2c73ff2019-11-20 16:22:32 -0500213 return &voltha.Loggings{}, err
Scott Baker5f401472019-08-22 08:32:26 -0700214 }
215 logLevel := &voltha.Logging{
216 ComponentName: in.ComponentName,
217 PackageName: packageName,
serkant.uluderya2ae470f2020-01-21 11:13:09 -0800218 Level: voltha.LogLevel_Types(level)}
Scott Baker5f401472019-08-22 08:32:26 -0700219 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",
serkant.uluderya2ae470f2020-01-21 11:13:09 -0800226 Level: voltha.LogLevel_Types(log.GetDefaultLogLevel())}
Scott Baker5f401472019-08-22 08:32:26 -0700227 logLevels.Items = append(logLevels.Items, logLevel)
228
229 return logLevels, nil
230}
231
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500232// ListCoreInstances returns details on the running core containers
233func (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
240func (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
npujar1d86a522019-11-14 17:11:16 +0530246// GetLogicalDevicePort returns logical device port details
khenaidoo43aa6bd2019-05-29 13:35:13 -0400247func (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() {
npujar1d86a522019-11-14 17:11:16 +0530251 txn, err := handler.takeRequestOwnership(ctx, &utils.LogicalDeviceID{ID: id.Id})
252 if err != nil {
khenaidoo43aa6bd2019-05-29 13:35:13 -0400253 return &voltha.LogicalPort{}, err
khenaidoo43aa6bd2019-05-29 13:35:13 -0400254 }
npujar467fe752020-01-16 20:17:45 +0530255 defer txn.Close(ctx)
khenaidoo43aa6bd2019-05-29 13:35:13 -0400256 }
npujar467fe752020-01-16 20:17:45 +0530257 return handler.logicalDeviceMgr.getLogicalPort(ctx, id)
khenaidoo43aa6bd2019-05-29 13:35:13 -0400258}
259
npujar1d86a522019-11-14 17:11:16 +0530260// EnableLogicalDevicePort enables logical device port
khenaidoobf6e7bb2018-08-14 22:27:29 -0400261func (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 Hagermanc2c73ff2019-11-20 16:22:32 -0500264 return &empty.Empty{}, nil
khenaidoobf6e7bb2018-08-14 22:27:29 -0400265 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500266
khenaidoo9cdc1a62019-01-24 21:57:40 -0500267 if handler.competeForTransaction() {
npujar1d86a522019-11-14 17:11:16 +0530268 txn, err := handler.takeRequestOwnership(ctx, &utils.LogicalDeviceID{ID: id.Id})
269 if err != nil {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500270 return &empty.Empty{}, err
Richard Jankowski2755adf2019-01-17 17:16:48 -0500271 }
npujar467fe752020-01-16 20:17:45 +0530272 defer txn.Close(ctx)
Richard Jankowski2755adf2019-01-17 17:16:48 -0500273 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500274
khenaidoo4d4802d2018-10-04 21:59:49 -0400275 ch := make(chan interface{})
276 defer close(ch)
khenaidoo19d7b632018-10-30 10:49:50 -0400277 go handler.logicalDeviceMgr.enableLogicalPort(ctx, id, ch)
khenaidoo4d4802d2018-10-04 21:59:49 -0400278 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400279}
280
npujar1d86a522019-11-14 17:11:16 +0530281// DisableLogicalDevicePort disables logical device port
khenaidoobf6e7bb2018-08-14 22:27:29 -0400282func (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 Hagermanc2c73ff2019-11-20 16:22:32 -0500285 return &empty.Empty{}, nil
khenaidoobf6e7bb2018-08-14 22:27:29 -0400286 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500287
khenaidoo9cdc1a62019-01-24 21:57:40 -0500288 if handler.competeForTransaction() {
npujar1d86a522019-11-14 17:11:16 +0530289 txn, err := handler.takeRequestOwnership(ctx, &utils.LogicalDeviceID{ID: id.Id})
290 if err != nil {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500291 return &empty.Empty{}, err
Richard Jankowski2755adf2019-01-17 17:16:48 -0500292 }
npujar467fe752020-01-16 20:17:45 +0530293 defer txn.Close(ctx)
Richard Jankowski2755adf2019-01-17 17:16:48 -0500294 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500295
khenaidoo19d7b632018-10-30 10:49:50 -0400296 ch := make(chan interface{})
297 defer close(ch)
298 go handler.logicalDeviceMgr.disableLogicalPort(ctx, id, ch)
299 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400300}
301
npujar1d86a522019-11-14 17:11:16 +0530302// UpdateLogicalDeviceFlowTable updates logical device flow table
khenaidoobf6e7bb2018-08-14 22:27:29 -0400303func (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 Hagermanc2c73ff2019-11-20 16:22:32 -0500306 return &empty.Empty{}, nil
khenaidoobf6e7bb2018-08-14 22:27:29 -0400307 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500308
khenaidoo9cdc1a62019-01-24 21:57:40 -0500309 if handler.competeForTransaction() {
npujar1d86a522019-11-14 17:11:16 +0530310 txn, err := handler.takeRequestOwnership(ctx, &utils.LogicalDeviceID{ID: flow.Id})
311 if err != nil {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500312 return &empty.Empty{}, err
Richard Jankowski2755adf2019-01-17 17:16:48 -0500313 }
npujar467fe752020-01-16 20:17:45 +0530314 defer txn.Close(ctx)
Richard Jankowski2755adf2019-01-17 17:16:48 -0500315 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500316
khenaidoo19d7b632018-10-30 10:49:50 -0400317 ch := make(chan interface{})
318 defer close(ch)
319 go handler.logicalDeviceMgr.updateFlowTable(ctx, flow.Id, flow.FlowMod, ch)
320 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400321}
322
npujar1d86a522019-11-14 17:11:16 +0530323// UpdateLogicalDeviceFlowGroupTable updates logical device flow group table
khenaidoobf6e7bb2018-08-14 22:27:29 -0400324func (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 Hagermanc2c73ff2019-11-20 16:22:32 -0500327 return &empty.Empty{}, nil
khenaidoobf6e7bb2018-08-14 22:27:29 -0400328 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500329
khenaidoo9cdc1a62019-01-24 21:57:40 -0500330 if handler.competeForTransaction() {
npujar1d86a522019-11-14 17:11:16 +0530331 txn, err := handler.takeRequestOwnership(ctx, &utils.LogicalDeviceID{ID: flow.Id})
332 if err != nil {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500333 return &empty.Empty{}, err
Richard Jankowski2755adf2019-01-17 17:16:48 -0500334 }
npujar467fe752020-01-16 20:17:45 +0530335 defer txn.Close(ctx)
Richard Jankowski2755adf2019-01-17 17:16:48 -0500336 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500337
khenaidoo19d7b632018-10-30 10:49:50 -0400338 ch := make(chan interface{})
339 defer close(ch)
340 go handler.logicalDeviceMgr.updateGroupTable(ctx, flow.Id, flow.GroupMod, ch)
341 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400342}
343
khenaidoob9203542018-09-17 22:56:37 -0400344// GetDevice must be implemented in the read-only containers - should it also be implemented here?
345func (handler *APIHandler) GetDevice(ctx context.Context, id *voltha.ID) (*voltha.Device, error) {
346 log.Debugw("GetDevice-request", log.Fields{"id": id})
npujar467fe752020-01-16 20:17:45 +0530347 return handler.deviceMgr.GetDevice(ctx, id.Id)
khenaidoob9203542018-09-17 22:56:37 -0400348}
349
350// GetDevice must be implemented in the read-only containers - should it also be implemented here?
npujar1d86a522019-11-14 17:11:16 +0530351
352// ListDevices retrieves the latest devices from the data model
khenaidoob9203542018-09-17 22:56:37 -0400353func (handler *APIHandler) ListDevices(ctx context.Context, empty *empty.Empty) (*voltha.Devices, error) {
354 log.Debug("ListDevices")
npujar467fe752020-01-16 20:17:45 +0530355 devices, err := handler.deviceMgr.ListDevices(ctx)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530356 if err != nil {
357 log.Errorw("Failed to list devices", log.Fields{"error": err})
358 return nil, err
359 }
360 return devices, nil
khenaidoob9203542018-09-17 22:56:37 -0400361}
362
khenaidoo7ccedd52018-12-14 16:48:54 -0500363// ListDeviceIds returns the list of device ids managed by a voltha core
364func (handler *APIHandler) ListDeviceIds(ctx context.Context, empty *empty.Empty) (*voltha.IDs, error) {
365 log.Debug("ListDeviceIDs")
366 if isTestMode(ctx) {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500367 return &voltha.IDs{Items: make([]*voltha.ID, 0)}, nil
khenaidoo7ccedd52018-12-14 16:48:54 -0500368 }
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
373func (handler *APIHandler) ReconcileDevices(ctx context.Context, ids *voltha.IDs) (*empty.Empty, error) {
374 log.Debug("ReconcileDevices")
375 if isTestMode(ctx) {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500376 return &empty.Empty{}, nil
khenaidoo7ccedd52018-12-14 16:48:54 -0500377 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500378
khenaidoo9cdc1a62019-01-24 21:57:40 -0500379 // No need to grab a transaction as this request is core specific
380
khenaidoo7ccedd52018-12-14 16:48:54 -0500381 ch := make(chan interface{})
382 defer close(ch)
383 go handler.deviceMgr.ReconcileDevices(ctx, ids, ch)
384 return waitForNilResponseOnSuccess(ctx, ch)
385}
386
npujar1d86a522019-11-14 17:11:16 +0530387// GetLogicalDevice provides a cloned most up to date logical device
khenaidoob9203542018-09-17 22:56:37 -0400388func (handler *APIHandler) GetLogicalDevice(ctx context.Context, id *voltha.ID) (*voltha.LogicalDevice, error) {
389 log.Debugw("GetLogicalDevice-request", log.Fields{"id": id})
khenaidoo43aa6bd2019-05-29 13:35:13 -0400390 if handler.competeForTransaction() {
npujar1d86a522019-11-14 17:11:16 +0530391 txn, err := handler.takeRequestOwnership(ctx, &utils.LogicalDeviceID{ID: id.Id})
392 if err != nil {
khenaidoo43aa6bd2019-05-29 13:35:13 -0400393 return &voltha.LogicalDevice{}, err
khenaidoo43aa6bd2019-05-29 13:35:13 -0400394 }
npujar467fe752020-01-16 20:17:45 +0530395 defer txn.Close(ctx)
khenaidoo43aa6bd2019-05-29 13:35:13 -0400396 }
npujar467fe752020-01-16 20:17:45 +0530397 return handler.logicalDeviceMgr.getLogicalDevice(ctx, id.Id)
khenaidoob9203542018-09-17 22:56:37 -0400398}
399
npujar1d86a522019-11-14 17:11:16 +0530400// ListLogicalDevices returns the list of all logical devices
khenaidoob9203542018-09-17 22:56:37 -0400401func (handler *APIHandler) ListLogicalDevices(ctx context.Context, empty *empty.Empty) (*voltha.LogicalDevices, error) {
khenaidoo43aa6bd2019-05-29 13:35:13 -0400402 log.Debug("ListLogicalDevices-request")
403 if handler.competeForTransaction() {
npujar1d86a522019-11-14 17:11:16 +0530404 txn, err := handler.takeRequestOwnership(ctx, nil)
405 if err != nil {
khenaidoo43aa6bd2019-05-29 13:35:13 -0400406 return &voltha.LogicalDevices{}, err
khenaidoo43aa6bd2019-05-29 13:35:13 -0400407 }
npujar467fe752020-01-16 20:17:45 +0530408 defer txn.Close(ctx)
khenaidooa9cc6d02019-11-20 14:56:59 -0500409 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 }
khenaidoo43aa6bd2019-05-29 13:35:13 -0400414 }
npujar467fe752020-01-16 20:17:45 +0530415 return handler.logicalDeviceMgr.listLogicalDevices(ctx)
khenaidoob9203542018-09-17 22:56:37 -0400416}
417
khenaidoo21d51152019-02-01 13:48:37 -0500418// ListAdapters returns the contents of all adapters known to the system
419func (handler *APIHandler) ListAdapters(ctx context.Context, empty *empty.Empty) (*voltha.Adapters, error) {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500420 log.Debug("ListAdapters")
khenaidoo21d51152019-02-01 13:48:37 -0500421 return handler.adapterMgr.listAdapters(ctx)
422}
423
npujar1d86a522019-11-14 17:11:16 +0530424// ListLogicalDeviceFlows returns the flows of logical device
khenaidoodd237172019-05-27 16:37:17 -0400425func (handler *APIHandler) ListLogicalDeviceFlows(ctx context.Context, id *voltha.ID) (*openflow_13.Flows, error) {
426 log.Debugw("ListLogicalDeviceFlows", log.Fields{"id": *id})
khenaidoo43aa6bd2019-05-29 13:35:13 -0400427 if handler.competeForTransaction() {
npujar1d86a522019-11-14 17:11:16 +0530428 txn, err := handler.takeRequestOwnership(ctx, &utils.LogicalDeviceID{ID: id.Id})
429 if err != nil {
khenaidoo43aa6bd2019-05-29 13:35:13 -0400430 return &openflow_13.Flows{}, err
khenaidoo43aa6bd2019-05-29 13:35:13 -0400431 }
npujar467fe752020-01-16 20:17:45 +0530432 defer txn.Close(ctx)
khenaidoo43aa6bd2019-05-29 13:35:13 -0400433 }
khenaidoodd237172019-05-27 16:37:17 -0400434 return handler.logicalDeviceMgr.ListLogicalDeviceFlows(ctx, id.Id)
435}
436
npujar1d86a522019-11-14 17:11:16 +0530437// ListLogicalDeviceFlowGroups returns logical device flow groups
khenaidoodd237172019-05-27 16:37:17 -0400438func (handler *APIHandler) ListLogicalDeviceFlowGroups(ctx context.Context, id *voltha.ID) (*openflow_13.FlowGroups, error) {
439 log.Debugw("ListLogicalDeviceFlowGroups", log.Fields{"id": *id})
khenaidoo43aa6bd2019-05-29 13:35:13 -0400440 if handler.competeForTransaction() {
npujar1d86a522019-11-14 17:11:16 +0530441 txn, err := handler.takeRequestOwnership(ctx, &utils.LogicalDeviceID{ID: id.Id})
442 if err != nil {
khenaidoo43aa6bd2019-05-29 13:35:13 -0400443 return &openflow_13.FlowGroups{}, err
khenaidoo43aa6bd2019-05-29 13:35:13 -0400444 }
npujar467fe752020-01-16 20:17:45 +0530445 defer txn.Close(ctx)
khenaidoo43aa6bd2019-05-29 13:35:13 -0400446 }
khenaidoodd237172019-05-27 16:37:17 -0400447 return handler.logicalDeviceMgr.ListLogicalDeviceFlowGroups(ctx, id.Id)
448}
449
npujar1d86a522019-11-14 17:11:16 +0530450// ListLogicalDevicePorts returns ports of logical device
khenaidoo19d7b632018-10-30 10:49:50 -0400451func (handler *APIHandler) ListLogicalDevicePorts(ctx context.Context, id *voltha.ID) (*voltha.LogicalPorts, error) {
452 log.Debugw("ListLogicalDevicePorts", log.Fields{"logicaldeviceid": id})
khenaidoo43aa6bd2019-05-29 13:35:13 -0400453 if handler.competeForTransaction() {
npujar1d86a522019-11-14 17:11:16 +0530454 txn, err := handler.takeRequestOwnership(ctx, &utils.LogicalDeviceID{ID: id.Id})
455 if err != nil {
khenaidoo43aa6bd2019-05-29 13:35:13 -0400456 return &voltha.LogicalPorts{}, err
khenaidoo43aa6bd2019-05-29 13:35:13 -0400457 }
npujar467fe752020-01-16 20:17:45 +0530458 defer txn.Close(ctx)
khenaidoo43aa6bd2019-05-29 13:35:13 -0400459 }
khenaidoo19d7b632018-10-30 10:49:50 -0400460 return handler.logicalDeviceMgr.ListLogicalDevicePorts(ctx, id.Id)
461}
462
khenaidoo4d4802d2018-10-04 21:59:49 -0400463// CreateDevice creates a new parent device in the data model
khenaidoobf6e7bb2018-08-14 22:27:29 -0400464func (handler *APIHandler) CreateDevice(ctx context.Context, device *voltha.Device) (*voltha.Device, error) {
Thomas Lee S51b5cb82019-10-14 14:49:34 +0530465 if device.MacAddress == "" && device.GetHostAndPort() == "" {
466 log.Errorf("No Device Info Present")
Thomas Lee Se5a44012019-11-07 20:32:24 +0530467 return &voltha.Device{}, errors.New("no-device-info-present; MAC or HOSTIP&PORT")
Thomas Lee S51b5cb82019-10-14 14:49:34 +0530468 }
469 log.Debugw("create-device", log.Fields{"device": *device})
khenaidoobf6e7bb2018-08-14 22:27:29 -0400470 if isTestMode(ctx) {
471 return &voltha.Device{Id: device.Id}, nil
472 }
Richard Jankowskid42826e2018-11-02 16:06:37 -0400473
khenaidoo9cdc1a62019-01-24 21:57:40 -0500474 if handler.competeForTransaction() {
khenaidoo631fe542019-05-31 15:44:43 -0400475 // There are no device Id present in this function.
npujar1d86a522019-11-14 17:11:16 +0530476 txn, err := handler.takeRequestOwnership(ctx, nil)
477 if err != nil {
Richard Jankowski2755adf2019-01-17 17:16:48 -0500478 return &voltha.Device{}, err
Richard Jankowski2755adf2019-01-17 17:16:48 -0500479 }
npujar467fe752020-01-16 20:17:45 +0530480 defer txn.Close(ctx)
Richard Jankowski2755adf2019-01-17 17:16:48 -0500481 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500482
khenaidoob9203542018-09-17 22:56:37 -0400483 ch := make(chan interface{})
484 defer close(ch)
485 go handler.deviceMgr.createDevice(ctx, device, ch)
486 select {
487 case res := <-ch:
khenaidoo92e62c52018-10-03 14:02:54 -0400488 if res != nil {
489 if err, ok := res.(error); ok {
Thomas Lee Se5a44012019-11-07 20:32:24 +0530490 log.Errorw("create-device-failed", log.Fields{"error": err})
491 return nil, err
khenaidoo92e62c52018-10-03 14:02:54 -0400492 }
493 if d, ok := res.(*voltha.Device); ok {
npujar467fe752020-01-16 20:17:45 +0530494 _, err := handler.core.deviceOwnership.OwnedByMe(ctx, &utils.DeviceID{ID: d.Id})
npujar1d86a522019-11-14 17:11:16 +0530495 if err != nil {
496 log.Errorw("unable-to-find-core-instance-active-owns-this-device", log.Fields{"error": err})
497 }
khenaidoo92e62c52018-10-03 14:02:54 -0400498 return d, nil
499 }
khenaidoob9203542018-09-17 22:56:37 -0400500 }
khenaidoo92e62c52018-10-03 14:02:54 -0400501 log.Warnw("create-device-unexpected-return-type", log.Fields{"result": res})
502 err := status.Errorf(codes.Internal, "%s", res)
503 return &voltha.Device{}, err
khenaidoob9203542018-09-17 22:56:37 -0400504 case <-ctx.Done():
505 log.Debug("createdevice-client-timeout")
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500506 return &voltha.Device{}, ctx.Err()
khenaidoob9203542018-09-17 22:56:37 -0400507 }
khenaidoobf6e7bb2018-08-14 22:27:29 -0400508}
509
khenaidoo4d4802d2018-10-04 21:59:49 -0400510// EnableDevice activates a device by invoking the adopt_device API on the appropriate adapter
khenaidoobf6e7bb2018-08-14 22:27:29 -0400511func (handler *APIHandler) EnableDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) {
khenaidoob9203542018-09-17 22:56:37 -0400512 log.Debugw("enabledevice", log.Fields{"id": id})
khenaidoobf6e7bb2018-08-14 22:27:29 -0400513 if isTestMode(ctx) {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500514 return &empty.Empty{}, nil
khenaidoobf6e7bb2018-08-14 22:27:29 -0400515 }
Richard Jankowskid42826e2018-11-02 16:06:37 -0400516
khenaidoo9cdc1a62019-01-24 21:57:40 -0500517 if handler.competeForTransaction() {
npujar1d86a522019-11-14 17:11:16 +0530518 txn, err := handler.takeRequestOwnership(ctx, &utils.DeviceID{ID: id.Id}, handler.longRunningRequestTimeout)
519 if err != nil {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500520 return &empty.Empty{}, err
Richard Jankowski2755adf2019-01-17 17:16:48 -0500521 }
npujar467fe752020-01-16 20:17:45 +0530522 defer txn.Close(ctx)
Richard Jankowski2755adf2019-01-17 17:16:48 -0500523 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500524
khenaidoob9203542018-09-17 22:56:37 -0400525 ch := make(chan interface{})
526 defer close(ch)
527 go handler.deviceMgr.enableDevice(ctx, id, ch)
khenaidoo4d4802d2018-10-04 21:59:49 -0400528 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400529}
530
khenaidoo4d4802d2018-10-04 21:59:49 -0400531// DisableDevice disables a device along with any child device it may have
khenaidoobf6e7bb2018-08-14 22:27:29 -0400532func (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 Hagermanc2c73ff2019-11-20 16:22:32 -0500535 return &empty.Empty{}, nil
khenaidoobf6e7bb2018-08-14 22:27:29 -0400536 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500537
khenaidoo9cdc1a62019-01-24 21:57:40 -0500538 if handler.competeForTransaction() {
npujar1d86a522019-11-14 17:11:16 +0530539 txn, err := handler.takeRequestOwnership(ctx, &utils.DeviceID{ID: id.Id})
540 if err != nil {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500541 return &empty.Empty{}, err
Richard Jankowski2755adf2019-01-17 17:16:48 -0500542 }
npujar467fe752020-01-16 20:17:45 +0530543 defer txn.Close(ctx)
Richard Jankowski2755adf2019-01-17 17:16:48 -0500544 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500545
khenaidoo92e62c52018-10-03 14:02:54 -0400546 ch := make(chan interface{})
547 defer close(ch)
548 go handler.deviceMgr.disableDevice(ctx, id, ch)
khenaidoo4d4802d2018-10-04 21:59:49 -0400549 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400550}
551
khenaidoo4d4802d2018-10-04 21:59:49 -0400552//RebootDevice invoked the reboot API to the corresponding adapter
khenaidoobf6e7bb2018-08-14 22:27:29 -0400553func (handler *APIHandler) RebootDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) {
khenaidoo4d4802d2018-10-04 21:59:49 -0400554 log.Debugw("rebootDevice-request", log.Fields{"id": id})
khenaidoobf6e7bb2018-08-14 22:27:29 -0400555 if isTestMode(ctx) {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500556 return &empty.Empty{}, nil
khenaidoobf6e7bb2018-08-14 22:27:29 -0400557 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500558
khenaidoo9cdc1a62019-01-24 21:57:40 -0500559 if handler.competeForTransaction() {
npujar1d86a522019-11-14 17:11:16 +0530560 txn, err := handler.takeRequestOwnership(ctx, &utils.DeviceID{ID: id.Id})
561 if err != nil {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500562 return &empty.Empty{}, err
Richard Jankowski2755adf2019-01-17 17:16:48 -0500563 }
npujar467fe752020-01-16 20:17:45 +0530564 defer txn.Close(ctx)
Richard Jankowski2755adf2019-01-17 17:16:48 -0500565 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500566
khenaidoo4d4802d2018-10-04 21:59:49 -0400567 ch := make(chan interface{})
568 defer close(ch)
569 go handler.deviceMgr.rebootDevice(ctx, id, ch)
570 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400571}
572
khenaidoo4d4802d2018-10-04 21:59:49 -0400573// DeleteDevice removes a device from the data model
khenaidoobf6e7bb2018-08-14 22:27:29 -0400574func (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 Hagermanc2c73ff2019-11-20 16:22:32 -0500577 return &empty.Empty{}, nil
khenaidoobf6e7bb2018-08-14 22:27:29 -0400578 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500579
khenaidoo9cdc1a62019-01-24 21:57:40 -0500580 if handler.competeForTransaction() {
npujar1d86a522019-11-14 17:11:16 +0530581 txn, err := handler.takeRequestOwnership(ctx, &utils.DeviceID{ID: id.Id})
582 if err != nil {
khenaidoo09771ef2019-10-11 14:25:02 -0400583 if err == errorTransactionNotAcquired {
npujar467fe752020-01-16 20:17:45 +0530584 if ownedByMe, err := handler.core.deviceOwnership.OwnedByMe(ctx, &utils.DeviceID{ID: id.Id}); !ownedByMe && err == nil {
khenaidoo09771ef2019-10-11 14:25:02 -0400585 // Remove the device in memory
npujar467fe752020-01-16 20:17:45 +0530586 handler.deviceMgr.stopManagingDevice(ctx, id.Id)
khenaidoo09771ef2019-10-11 14:25:02 -0400587 }
khenaidoo6d62c002019-05-15 21:57:03 -0400588 }
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500589 return &empty.Empty{}, err
Richard Jankowski2755adf2019-01-17 17:16:48 -0500590 }
npujar467fe752020-01-16 20:17:45 +0530591 defer txn.Close(ctx)
Richard Jankowski2755adf2019-01-17 17:16:48 -0500592 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500593
khenaidoo4d4802d2018-10-04 21:59:49 -0400594 ch := make(chan interface{})
595 defer close(ch)
596 go handler.deviceMgr.deleteDevice(ctx, id, ch)
597 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400598}
599
David Bainbridge4087cc52019-11-13 18:36:03 +0000600// ListDevicePorts returns the ports details for a specific device entry
601func (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() {
npujar1d86a522019-11-14 17:11:16 +0530604 txn, err := handler.takeRequestOwnership(ctx, &utils.DeviceID{ID: id.Id})
605 if err != nil {
David Bainbridge4087cc52019-11-13 18:36:03 +0000606 return &voltha.Ports{}, err
David Bainbridge4087cc52019-11-13 18:36:03 +0000607 }
npujar467fe752020-01-16 20:17:45 +0530608 defer txn.Close(ctx)
David Bainbridge4087cc52019-11-13 18:36:03 +0000609 }
610
npujar467fe752020-01-16 20:17:45 +0530611 device, err := handler.deviceMgr.GetDevice(ctx, id.Id)
David Bainbridge4087cc52019-11-13 18:36:03 +0000612 if err != nil {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500613 return &voltha.Ports{}, err
David Bainbridge4087cc52019-11-13 18:36:03 +0000614 }
615 ports := &voltha.Ports{}
npujar1d86a522019-11-14 17:11:16 +0530616 ports.Items = append(ports.Items, device.Ports...)
David Bainbridge4087cc52019-11-13 18:36:03 +0000617 return ports, nil
618}
619
620// ListDeviceFlows returns the flow details for a specific device entry
621func (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() {
npujar1d86a522019-11-14 17:11:16 +0530624 txn, err := handler.takeRequestOwnership(ctx, &utils.DeviceID{ID: id.Id})
625 if err != nil {
David Bainbridge4087cc52019-11-13 18:36:03 +0000626 return &openflow_13.Flows{}, err
David Bainbridge4087cc52019-11-13 18:36:03 +0000627 }
npujar467fe752020-01-16 20:17:45 +0530628 defer txn.Close(ctx)
David Bainbridge4087cc52019-11-13 18:36:03 +0000629 }
630
npujar467fe752020-01-16 20:17:45 +0530631 device, err := handler.deviceMgr.GetDevice(ctx, id.Id)
David Bainbridge4087cc52019-11-13 18:36:03 +0000632 if err != nil {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500633 return &openflow_13.Flows{}, err
David Bainbridge4087cc52019-11-13 18:36:03 +0000634 }
635 flows := &openflow_13.Flows{}
npujar1d86a522019-11-14 17:11:16 +0530636 flows.Items = append(flows.Items, device.Flows.Items...)
David Bainbridge4087cc52019-11-13 18:36:03 +0000637 return flows, nil
638}
639
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500640// ListDeviceFlowGroups returns the flow group details for a specific device entry
641func (handler *APIHandler) ListDeviceFlowGroups(ctx context.Context, id *voltha.ID) (*voltha.FlowGroups, error) {
642 log.Debugw("ListDeviceFlowGroups", log.Fields{"deviceid": id})
643
npujar467fe752020-01-16 20:17:45 +0530644 if device, _ := handler.deviceMgr.GetDevice(ctx, id.Id); device != nil {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500645 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
651func (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
657func (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
663func (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
670func (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 Bainbridge4087cc52019-11-13 18:36:03 +0000679// GetVoltha returns the contents of all components (i.e. devices, logical_devices, ...)
680func (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
khenaidoof5a5bfa2019-01-23 22:20:29 -0500702// processImageRequest is a helper method to execute an image download request
703func (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
khenaidoo9cdc1a62019-01-24 21:57:40 -0500710 if handler.competeForTransaction() {
npujar1d86a522019-11-14 17:11:16 +0530711 txn, err := handler.takeRequestOwnership(ctx, &utils.DeviceID{ID: img.Id})
712 if err != nil {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500713 return &common.OperationResp{}, err
khenaidoo9cdc1a62019-01-24 21:57:40 -0500714 }
npujar467fe752020-01-16 20:17:45 +0530715 defer txn.Close(ctx)
khenaidoof5a5bfa2019-01-23 22:20:29 -0500716 }
717
khenaidoo2c6a0992019-04-29 13:46:56 -0400718 failedresponse := &common.OperationResp{Code: voltha.OperationResp_OPERATION_FAILURE}
khenaidoof5a5bfa2019-01-23 22:20:29 -0500719
720 ch := make(chan interface{})
721 defer close(ch)
722 switch requestType {
npujar1d86a522019-11-14 17:11:16 +0530723 case ImageDownload:
khenaidoof5a5bfa2019-01-23 22:20:29 -0500724 go handler.deviceMgr.downloadImage(ctx, img, ch)
npujar1d86a522019-11-14 17:11:16 +0530725 case CancelImageDownload:
khenaidoof5a5bfa2019-01-23 22:20:29 -0500726 go handler.deviceMgr.cancelImageDownload(ctx, img, ch)
npujar1d86a522019-11-14 17:11:16 +0530727 case ActivateImage:
khenaidoof5a5bfa2019-01-23 22:20:29 -0500728 go handler.deviceMgr.activateImage(ctx, img, ch)
npujar1d86a522019-11-14 17:11:16 +0530729 case RevertImage:
khenaidoof5a5bfa2019-01-23 22:20:29 -0500730 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 Hagermanc2c73ff2019-11-20 16:22:32 -0500749 return &common.OperationResp{}, ctx.Err()
khenaidoof5a5bfa2019-01-23 22:20:29 -0500750 }
751}
752
npujar1d86a522019-11-14 17:11:16 +0530753// DownloadImage execute an image download request
khenaidoobf6e7bb2018-08-14 22:27:29 -0400754func (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
npujar1d86a522019-11-14 17:11:16 +0530761 return handler.processImageRequest(ctx, img, ImageDownload)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400762}
763
npujar1d86a522019-11-14 17:11:16 +0530764// CancelImageDownload cancels image download request
khenaidoobf6e7bb2018-08-14 22:27:29 -0400765func (handler *APIHandler) CancelImageDownload(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) {
khenaidoof5a5bfa2019-01-23 22:20:29 -0500766 log.Debugw("cancelImageDownload-request", log.Fields{"img": *img})
khenaidoobf6e7bb2018-08-14 22:27:29 -0400767 if isTestMode(ctx) {
768 resp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS}
769 return resp, nil
770 }
npujar1d86a522019-11-14 17:11:16 +0530771 return handler.processImageRequest(ctx, img, CancelImageDownload)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400772}
773
npujar1d86a522019-11-14 17:11:16 +0530774// ActivateImageUpdate activates image update request
khenaidoobf6e7bb2018-08-14 22:27:29 -0400775func (handler *APIHandler) ActivateImageUpdate(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) {
khenaidoof5a5bfa2019-01-23 22:20:29 -0500776 log.Debugw("activateImageUpdate-request", log.Fields{"img": *img})
khenaidoobf6e7bb2018-08-14 22:27:29 -0400777 if isTestMode(ctx) {
778 resp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS}
779 return resp, nil
780 }
khenaidoof5a5bfa2019-01-23 22:20:29 -0500781
npujar1d86a522019-11-14 17:11:16 +0530782 return handler.processImageRequest(ctx, img, ActivateImage)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400783}
784
npujar1d86a522019-11-14 17:11:16 +0530785// RevertImageUpdate reverts image update
khenaidoobf6e7bb2018-08-14 22:27:29 -0400786func (handler *APIHandler) RevertImageUpdate(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) {
khenaidoof5a5bfa2019-01-23 22:20:29 -0500787 log.Debugw("revertImageUpdate-request", log.Fields{"img": *img})
khenaidoobf6e7bb2018-08-14 22:27:29 -0400788 if isTestMode(ctx) {
789 resp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS}
790 return resp, nil
791 }
khenaidoof5a5bfa2019-01-23 22:20:29 -0500792
npujar1d86a522019-11-14 17:11:16 +0530793 return handler.processImageRequest(ctx, img, RevertImage)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400794}
795
npujar1d86a522019-11-14 17:11:16 +0530796// GetImageDownloadStatus returns status of image download
khenaidoof5a5bfa2019-01-23 22:20:29 -0500797func (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 Barbariedf5479f2019-01-29 22:13:00 -0500800 resp := &voltha.ImageDownload{DownloadState: voltha.ImageDownload_DOWNLOAD_SUCCEEDED}
khenaidoof5a5bfa2019-01-23 22:20:29 -0500801 return resp, nil
802 }
803
Stephane Barbariedf5479f2019-01-29 22:13:00 -0500804 failedresponse := &voltha.ImageDownload{DownloadState: voltha.ImageDownload_DOWNLOAD_UNKNOWN}
khenaidoof5a5bfa2019-01-23 22:20:29 -0500805
khenaidoo9cdc1a62019-01-24 21:57:40 -0500806 if handler.competeForTransaction() {
npujar1d86a522019-11-14 17:11:16 +0530807 txn, err := handler.takeRequestOwnership(ctx, &utils.DeviceID{ID: img.Id})
808 if err != nil {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500809 return failedresponse, err
khenaidoo9cdc1a62019-01-24 21:57:40 -0500810 }
npujar467fe752020-01-16 20:17:45 +0530811 defer txn.Close(ctx)
khenaidoof5a5bfa2019-01-23 22:20:29 -0500812 }
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
npujar1d86a522019-11-14 17:11:16 +0530836// GetImageDownload returns image download
khenaidoof5a5bfa2019-01-23 22:20:29 -0500837func (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 Barbariedf5479f2019-01-29 22:13:00 -0500840 resp := &voltha.ImageDownload{DownloadState: voltha.ImageDownload_DOWNLOAD_SUCCEEDED}
khenaidoof5a5bfa2019-01-23 22:20:29 -0500841 return resp, nil
842 }
843
npujar1d86a522019-11-14 17:11:16 +0530844 download, err := handler.deviceMgr.getImageDownload(ctx, img)
845 if err != nil {
Stephane Barbariedf5479f2019-01-29 22:13:00 -0500846 return &voltha.ImageDownload{DownloadState: voltha.ImageDownload_DOWNLOAD_UNKNOWN}, err
khenaidoof5a5bfa2019-01-23 22:20:29 -0500847 }
npujar1d86a522019-11-14 17:11:16 +0530848 return download, nil
khenaidoof5a5bfa2019-01-23 22:20:29 -0500849}
850
npujar1d86a522019-11-14 17:11:16 +0530851// ListImageDownloads returns image downloads
khenaidoof5a5bfa2019-01-23 22:20:29 -0500852func (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) {
khenaidoo2c6a0992019-04-29 13:46:56 -0400855 resp := &voltha.ImageDownloads{Items: []*voltha.ImageDownload{}}
khenaidoof5a5bfa2019-01-23 22:20:29 -0500856 return resp, nil
857 }
858
npujar1d86a522019-11-14 17:11:16 +0530859 downloads, err := handler.deviceMgr.listImageDownloads(ctx, id.Id)
860 if err != nil {
khenaidoof5a5bfa2019-01-23 22:20:29 -0500861 failedResp := &voltha.ImageDownloads{
khenaidoo2c6a0992019-04-29 13:46:56 -0400862 Items: []*voltha.ImageDownload{
863 {DownloadState: voltha.ImageDownload_DOWNLOAD_UNKNOWN},
864 },
khenaidoof5a5bfa2019-01-23 22:20:29 -0500865 }
866 return failedResp, err
khenaidoof5a5bfa2019-01-23 22:20:29 -0500867 }
npujar1d86a522019-11-14 17:11:16 +0530868 return downloads, nil
khenaidoof5a5bfa2019-01-23 22:20:29 -0500869}
870
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500871// GetImages returns all images for a specific device entry
872func (handler *APIHandler) GetImages(ctx context.Context, id *voltha.ID) (*voltha.Images, error) {
873 log.Debugw("GetImages", log.Fields{"deviceid": id.Id})
npujar467fe752020-01-16 20:17:45 +0530874 device, err := handler.deviceMgr.GetDevice(ctx, id.Id)
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500875 if err != nil {
876 return &voltha.Images{}, err
877 }
878 return device.GetImages(), nil
879}
880
npujar1d86a522019-11-14 17:11:16 +0530881// UpdateDevicePmConfigs updates the PM configs
khenaidoobf6e7bb2018-08-14 22:27:29 -0400882func (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 Hagermanc2c73ff2019-11-20 16:22:32 -0500885 return &empty.Empty{}, nil
khenaidoobf6e7bb2018-08-14 22:27:29 -0400886 }
khenaidoob3127472019-07-24 21:04:55 -0400887 if handler.competeForTransaction() {
npujar1d86a522019-11-14 17:11:16 +0530888 txn, err := handler.takeRequestOwnership(ctx, &utils.DeviceID{ID: configs.Id})
889 if err != nil {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500890 return &empty.Empty{}, err
khenaidoob3127472019-07-24 21:04:55 -0400891 }
npujar467fe752020-01-16 20:17:45 +0530892 defer txn.Close(ctx)
khenaidoob3127472019-07-24 21:04:55 -0400893 }
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
npujar1d86a522019-11-14 17:11:16 +0530901// ListDevicePmConfigs returns pm configs of device
khenaidoob3127472019-07-24 21:04:55 -0400902func (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() {
npujar1d86a522019-11-14 17:11:16 +0530905 txn, err := handler.takeRequestOwnership(ctx, &utils.LogicalDeviceID{ID: id.Id})
906 if err != nil {
khenaidoob3127472019-07-24 21:04:55 -0400907 return &voltha.PmConfigs{}, err
khenaidoob3127472019-07-24 21:04:55 -0400908 }
npujar467fe752020-01-16 20:17:45 +0530909 defer txn.Close(ctx)
khenaidoob3127472019-07-24 21:04:55 -0400910 }
911 return handler.deviceMgr.listPmConfigs(ctx, id.Id)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400912}
913
Devmalya Paulc594bb32019-11-06 07:34:27 +0000914func (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")
khenaidoobf6e7bb2018-08-14 22:27:29 -0400917}
918
Devmalya Paulc594bb32019-11-06 07:34:27 +0000919func (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")
khenaidoobf6e7bb2018-08-14 22:27:29 -0400922}
923
Devmalya Paulc594bb32019-11-06 07:34:27 +0000924func (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 Hagermanc2c73ff2019-11-20 16:22:32 -0500927}
928
Devmalya Paulc594bb32019-11-06 07:34:27 +0000929// GetEventFilter returns all the filters present for a device
930func (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 Hagermanc2c73ff2019-11-20 16:22:32 -0500933}
934
Devmalya Paulc594bb32019-11-06 07:34:27 +0000935// ListEventFilters returns all the filters known to the system
936func (handler *APIHandler) ListEventFilters(ctx context.Context, empty *empty.Empty) (*voltha.EventFilters, error) {
937 log.Debug("ListEventFilter-request")
938 return nil, errors.New("UnImplemented")
khenaidoobf6e7bb2018-08-14 22:27:29 -0400939}
940
941func (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 Hagermanc2c73ff2019-11-20 16:22:32 -0500947 return &voltha.SelfTestResponse{}, errors.New("UnImplemented")
khenaidoobf6e7bb2018-08-14 22:27:29 -0400948}
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500949
npujar467fe752020-01-16 20:17:45 +0530950func (handler *APIHandler) forwardPacketOut(ctx context.Context, packet *openflow_13.PacketOut) {
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500951 log.Debugw("forwardPacketOut-request", log.Fields{"packet": packet})
khenaidoo3d3b8c22019-05-22 18:10:39 -0400952 //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)
npujar467fe752020-01-16 20:17:45 +0530955 if ownedByMe, err := handler.core.deviceOwnership.OwnedByMe(ctx, &utils.LogicalDeviceID{ID: packet.Id}); ownedByMe && err == nil {
956 if agent := handler.logicalDeviceMgr.getLogicalDeviceAgent(ctx, packet.Id); agent != nil {
957 agent.packetOut(ctx, packet.PacketOut)
khenaidoo93d5a3d2020-01-15 12:37:05 -0500958 } else {
959 log.Errorf("No logical device agent present", log.Fields{"logicaldeviceID": packet.Id})
960 }
khenaidoo3d3b8c22019-05-22 18:10:39 -0400961 }
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500962}
khenaidoo3d3b8c22019-05-22 18:10:39 -0400963
npujar1d86a522019-11-14 17:11:16 +0530964// StreamPacketsOut sends packets to adapter
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500965func (handler *APIHandler) StreamPacketsOut(packets voltha.VolthaService_StreamPacketsOutServer) error {
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500966 log.Debugw("StreamPacketsOut-request", log.Fields{"packets": packets})
khenaidoo5e250692019-08-30 14:46:21 -0400967loop:
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500968 for {
khenaidoo5e250692019-08-30 14:46:21 -0400969 select {
970 case <-packets.Context().Done():
971 log.Infow("StreamPacketsOut-context-done", log.Fields{"packets": packets, "error": packets.Context().Err()})
972 break loop
973 default:
974 }
975
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500976 packet, err := packets.Recv()
977
978 if err == io.EOF {
khenaidoo5e250692019-08-30 14:46:21 -0400979 log.Debugw("Received-EOF", log.Fields{"packets": packets})
980 break loop
981 }
982
983 if err != nil {
984 log.Errorw("Failed to receive packet out", log.Fields{"error": err})
985 continue
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500986 }
987
npujar467fe752020-01-16 20:17:45 +0530988 handler.forwardPacketOut(packets.Context(), packet)
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500989 }
990
991 log.Debugw("StreamPacketsOut-request-done", log.Fields{"packets": packets})
992 return nil
993}
994
npujar1d86a522019-11-14 17:11:16 +0530995func (handler *APIHandler) sendPacketIn(deviceID string, transationID string, packet *openflow_13.OfpPacketIn) {
khenaidoo297cd252019-02-07 22:10:23 -0500996 // TODO: Augment the OF PacketIn to include the transactionId
npujar1d86a522019-11-14 17:11:16 +0530997 packetIn := openflow_13.PacketIn{Id: deviceID, PacketIn: packet}
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500998 log.Debugw("sendPacketIn", log.Fields{"packetIn": packetIn})
A R Karthick881e7ea2019-08-19 19:44:02 +0000999 handler.packetInQueue <- packetIn
1000}
1001
1002type callTracker struct {
1003 failedPacket interface{}
1004}
1005type streamTracker struct {
1006 calls map[string]*callTracker
1007 sync.Mutex
1008}
1009
1010var streamingTracker = &streamTracker{calls: make(map[string]*callTracker)}
1011
1012func (handler *APIHandler) getStreamingTracker(method string, done chan<- bool) *callTracker {
1013 streamingTracker.Lock()
1014 defer streamingTracker.Unlock()
1015 if _, ok := streamingTracker.calls[method]; ok {
1016 // bail out the other packet in thread
1017 log.Debugf("%s streaming call already running. Exiting it", method)
1018 done <- true
1019 log.Debugf("Last %s exited. Continuing ...", method)
1020 } else {
1021 streamingTracker.calls[method] = &callTracker{failedPacket: nil}
Richard Jankowskidbab94a2018-12-06 16:20:25 -05001022 }
A R Karthick881e7ea2019-08-19 19:44:02 +00001023 return streamingTracker.calls[method]
1024}
1025
1026func (handler *APIHandler) flushFailedPackets(tracker *callTracker) error {
1027 if tracker.failedPacket != nil {
1028 switch tracker.failedPacket.(type) {
1029 case openflow_13.PacketIn:
1030 log.Debug("Enqueueing last failed packetIn")
1031 handler.packetInQueue <- tracker.failedPacket.(openflow_13.PacketIn)
1032 case openflow_13.ChangeEvent:
1033 log.Debug("Enqueueing last failed changeEvent")
1034 handler.changeEventQueue <- tracker.failedPacket.(openflow_13.ChangeEvent)
1035 }
1036 }
1037 return nil
Stephane Barbarie6e1bd502018-11-05 22:44:45 -05001038}
1039
npujar1d86a522019-11-14 17:11:16 +05301040// ReceivePacketsIn receives packets from adapter
Kent Hagermanc2c73ff2019-11-20 16:22:32 -05001041func (handler *APIHandler) ReceivePacketsIn(empty *empty.Empty, packetsIn voltha.VolthaService_ReceivePacketsInServer) error {
A R Karthick881e7ea2019-08-19 19:44:02 +00001042 var streamingTracker = handler.getStreamingTracker("ReceivePacketsIn", handler.packetInQueueDone)
Stephane Barbarie6e1bd502018-11-05 22:44:45 -05001043 log.Debugw("ReceivePacketsIn-request", log.Fields{"packetsIn": packetsIn})
1044
npujar1d86a522019-11-14 17:11:16 +05301045 err := handler.flushFailedPackets(streamingTracker)
1046 if err != nil {
1047 log.Errorw("unable-to-flush-failed-packets", log.Fields{"error": err})
1048 }
A R Karthick881e7ea2019-08-19 19:44:02 +00001049
1050loop:
Stephane Barbarie6e1bd502018-11-05 22:44:45 -05001051 for {
A R Karthick881e7ea2019-08-19 19:44:02 +00001052 select {
1053 case packet := <-handler.packetInQueue:
Matteo Scandolo360605d2019-11-05 18:29:17 -08001054 log.Debugw("sending-packet-in", log.Fields{
1055 "packet": hex.EncodeToString(packet.PacketIn.Data),
1056 })
A R Karthick881e7ea2019-08-19 19:44:02 +00001057 if err := packetsIn.Send(&packet); err != nil {
1058 log.Errorw("failed-to-send-packet", log.Fields{"error": err})
1059 // save the last failed packet in
1060 streamingTracker.failedPacket = packet
1061 } else {
1062 if streamingTracker.failedPacket != nil {
1063 // reset last failed packet saved to avoid flush
1064 streamingTracker.failedPacket = nil
Richard Jankowskidbab94a2018-12-06 16:20:25 -05001065 }
1066 }
A R Karthick881e7ea2019-08-19 19:44:02 +00001067 case <-handler.packetInQueueDone:
1068 log.Debug("Another ReceivePacketsIn running. Bailing out ...")
1069 break loop
Stephane Barbarie6e1bd502018-11-05 22:44:45 -05001070 }
1071 }
A R Karthick881e7ea2019-08-19 19:44:02 +00001072
1073 //TODO: Find an elegant way to get out of the above loop when the Core is stopped
1074 return nil
Stephane Barbarie6e1bd502018-11-05 22:44:45 -05001075}
1076
npujar1d86a522019-11-14 17:11:16 +05301077func (handler *APIHandler) sendChangeEvent(deviceID string, portStatus *openflow_13.OfpPortStatus) {
Stephane Barbarie6e1bd502018-11-05 22:44:45 -05001078 // TODO: validate the type of portStatus parameter
1079 //if _, ok := portStatus.(*openflow_13.OfpPortStatus); ok {
1080 //}
npujar1d86a522019-11-14 17:11:16 +05301081 event := openflow_13.ChangeEvent{Id: deviceID, Event: &openflow_13.ChangeEvent_PortStatus{PortStatus: portStatus}}
Stephane Barbarie6e1bd502018-11-05 22:44:45 -05001082 log.Debugw("sendChangeEvent", log.Fields{"event": event})
A R Karthick881e7ea2019-08-19 19:44:02 +00001083 handler.changeEventQueue <- event
Stephane Barbarie6e1bd502018-11-05 22:44:45 -05001084}
1085
npujar1d86a522019-11-14 17:11:16 +05301086// ReceiveChangeEvents receives change in events
Kent Hagermanc2c73ff2019-11-20 16:22:32 -05001087func (handler *APIHandler) ReceiveChangeEvents(empty *empty.Empty, changeEvents voltha.VolthaService_ReceiveChangeEventsServer) error {
A R Karthick881e7ea2019-08-19 19:44:02 +00001088 var streamingTracker = handler.getStreamingTracker("ReceiveChangeEvents", handler.changeEventQueueDone)
Stephane Barbarie6e1bd502018-11-05 22:44:45 -05001089 log.Debugw("ReceiveChangeEvents-request", log.Fields{"changeEvents": changeEvents})
A R Karthick881e7ea2019-08-19 19:44:02 +00001090
npujar1d86a522019-11-14 17:11:16 +05301091 err := handler.flushFailedPackets(streamingTracker)
1092 if err != nil {
1093 log.Errorw("unable-to-flush-failed-packets", log.Fields{"error": err})
1094 }
A R Karthick881e7ea2019-08-19 19:44:02 +00001095
1096loop:
Stephane Barbarie6e1bd502018-11-05 22:44:45 -05001097 for {
A R Karthick881e7ea2019-08-19 19:44:02 +00001098 select {
Richard Jankowski199fd862019-03-18 14:49:51 -04001099 // Dequeue a change event
A R Karthick881e7ea2019-08-19 19:44:02 +00001100 case event := <-handler.changeEventQueue:
1101 log.Debugw("sending-change-event", log.Fields{"event": event})
1102 if err := changeEvents.Send(&event); err != nil {
1103 log.Errorw("failed-to-send-change-event", log.Fields{"error": err})
1104 // save last failed changeevent
1105 streamingTracker.failedPacket = event
1106 } else {
1107 if streamingTracker.failedPacket != nil {
1108 // reset last failed event saved on success to avoid flushing
1109 streamingTracker.failedPacket = nil
Richard Jankowski199fd862019-03-18 14:49:51 -04001110 }
1111 }
A R Karthick881e7ea2019-08-19 19:44:02 +00001112 case <-handler.changeEventQueueDone:
1113 log.Debug("Another ReceiveChangeEvents already running. Bailing out ...")
1114 break loop
Stephane Barbarie6e1bd502018-11-05 22:44:45 -05001115 }
1116 }
A R Karthick881e7ea2019-08-19 19:44:02 +00001117
1118 return nil
Richard Jankowski199fd862019-03-18 14:49:51 -04001119}
Stephane Barbarie6e1bd502018-11-05 22:44:45 -05001120
npujar1d86a522019-11-14 17:11:16 +05301121// Subscribe subscribing request of ofagent
Stephane Barbarie6e1bd502018-11-05 22:44:45 -05001122func (handler *APIHandler) Subscribe(
1123 ctx context.Context,
1124 ofAgent *voltha.OfAgentSubscriber,
1125) (*voltha.OfAgentSubscriber, error) {
1126 log.Debugw("Subscribe-request", log.Fields{"ofAgent": ofAgent})
1127 return &voltha.OfAgentSubscriber{OfagentId: ofAgent.OfagentId, VolthaId: ofAgent.VolthaId}, nil
1128}
William Kurkiandaa6bb22019-03-07 12:26:28 -05001129
npujar1d86a522019-11-14 17:11:16 +05301130// GetAlarmDeviceData @TODO useless stub, what should this actually do?
Kent Hagermanc2c73ff2019-11-20 16:22:32 -05001131func (handler *APIHandler) GetAlarmDeviceData(ctx context.Context, in *common.ID) (*omci.AlarmDeviceData, error) {
William Kurkiandaa6bb22019-03-07 12:26:28 -05001132 log.Debug("GetAlarmDeviceData-stub")
Kent Hagermanc2c73ff2019-11-20 16:22:32 -05001133 return &omci.AlarmDeviceData{}, errors.New("UnImplemented")
William Kurkiandaa6bb22019-03-07 12:26:28 -05001134}
1135
npujar1d86a522019-11-14 17:11:16 +05301136// ListLogicalDeviceMeters returns logical device meters
Manikkaraj kb1a10922019-07-29 12:10:34 -04001137func (handler *APIHandler) ListLogicalDeviceMeters(ctx context.Context, id *voltha.ID) (*openflow_13.Meters, error) {
1138
1139 log.Debugw("ListLogicalDeviceMeters", log.Fields{"id": *id})
1140 if handler.competeForTransaction() {
npujar1d86a522019-11-14 17:11:16 +05301141 txn, err := handler.takeRequestOwnership(ctx, &utils.LogicalDeviceID{ID: id.Id})
1142 if err != nil {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -05001143 return &openflow_13.Meters{}, err // TODO: Return empty meter entry
Manikkaraj kb1a10922019-07-29 12:10:34 -04001144 }
npujar467fe752020-01-16 20:17:45 +05301145 defer txn.Close(ctx)
Manikkaraj kb1a10922019-07-29 12:10:34 -04001146 }
1147 return handler.logicalDeviceMgr.ListLogicalDeviceMeters(ctx, id.Id)
William Kurkiandaa6bb22019-03-07 12:26:28 -05001148}
1149
npujar1d86a522019-11-14 17:11:16 +05301150// GetMeterStatsOfLogicalDevice @TODO useless stub, what should this actually do?
Kent Hagermanc2c73ff2019-11-20 16:22:32 -05001151func (handler *APIHandler) GetMeterStatsOfLogicalDevice(ctx context.Context, in *common.ID) (*openflow_13.MeterStatsReply, error) {
1152 log.Debug("GetMeterStatsOfLogicalDevice")
1153 return &openflow_13.MeterStatsReply{}, errors.New("UnImplemented")
1154}
1155
npujar1d86a522019-11-14 17:11:16 +05301156// GetMibDeviceData @TODO useless stub, what should this actually do?
Kent Hagermanc2c73ff2019-11-20 16:22:32 -05001157func (handler *APIHandler) GetMibDeviceData(ctx context.Context, in *common.ID) (*omci.MibDeviceData, error) {
1158 log.Debug("GetMibDeviceData")
1159 return &omci.MibDeviceData{}, errors.New("UnImplemented")
William Kurkiandaa6bb22019-03-07 12:26:28 -05001160}
1161
npujar1d86a522019-11-14 17:11:16 +05301162// SimulateAlarm sends simulate alarm request
William Kurkiandaa6bb22019-03-07 12:26:28 -05001163func (handler *APIHandler) SimulateAlarm(
1164 ctx context.Context,
1165 in *voltha.SimulateAlarmRequest,
1166) (*common.OperationResp, error) {
serkant.uluderya334479d2019-04-10 08:26:15 -07001167 log.Debugw("SimulateAlarm-request", log.Fields{"id": in.Id})
1168 successResp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS}
1169 if isTestMode(ctx) {
1170 return successResp, nil
1171 }
1172
1173 if handler.competeForTransaction() {
npujar1d86a522019-11-14 17:11:16 +05301174 txn, err := handler.takeRequestOwnership(ctx, &utils.DeviceID{ID: in.Id}, handler.longRunningRequestTimeout)
1175 if err != nil {
Kent Hagerman0ab4cb22019-04-24 13:13:35 -04001176 failedresponse := &common.OperationResp{Code: voltha.OperationResp_OPERATION_FAILURE}
serkant.uluderya334479d2019-04-10 08:26:15 -07001177 return failedresponse, err
serkant.uluderya334479d2019-04-10 08:26:15 -07001178 }
npujar467fe752020-01-16 20:17:45 +05301179 defer txn.Close(ctx)
serkant.uluderya334479d2019-04-10 08:26:15 -07001180 }
1181
1182 ch := make(chan interface{})
1183 defer close(ch)
1184 go handler.deviceMgr.simulateAlarm(ctx, in, ch)
1185 return successResp, nil
William Kurkiandaa6bb22019-03-07 12:26:28 -05001186}
1187
npujar1d86a522019-11-14 17:11:16 +05301188// UpdateLogicalDeviceMeterTable - This function sends meter mod request to logical device manager and waits for response
Manikkaraj kb1a10922019-07-29 12:10:34 -04001189func (handler *APIHandler) UpdateLogicalDeviceMeterTable(ctx context.Context, meter *openflow_13.MeterModUpdate) (*empty.Empty, error) {
1190 log.Debugw("UpdateLogicalDeviceMeterTable-request",
1191 log.Fields{"meter": meter, "test": common.TestModeKeys_api_test.String()})
1192 if isTestMode(ctx) {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -05001193 return &empty.Empty{}, nil
Manikkaraj kb1a10922019-07-29 12:10:34 -04001194 }
1195
1196 if handler.competeForTransaction() {
npujar1d86a522019-11-14 17:11:16 +05301197 txn, err := handler.takeRequestOwnership(ctx, &utils.LogicalDeviceID{ID: meter.Id})
1198 if err != nil {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -05001199 return &empty.Empty{}, err
Manikkaraj kb1a10922019-07-29 12:10:34 -04001200 }
npujar467fe752020-01-16 20:17:45 +05301201 defer txn.Close(ctx)
Manikkaraj kb1a10922019-07-29 12:10:34 -04001202 }
1203
1204 ch := make(chan interface{})
1205 defer close(ch)
1206 go handler.logicalDeviceMgr.updateMeterTable(ctx, meter.Id, meter.MeterMod, ch)
1207 return waitForNilResponseOnSuccess(ctx, ch)
William Kurkiandaa6bb22019-03-07 12:26:28 -05001208}
Kent Hagermanc2c73ff2019-11-20 16:22:32 -05001209
npujar1d86a522019-11-14 17:11:16 +05301210// GetMembership returns membership
Kent Hagermanc2c73ff2019-11-20 16:22:32 -05001211func (handler *APIHandler) GetMembership(context.Context, *empty.Empty) (*voltha.Membership, error) {
1212 return &voltha.Membership{}, errors.New("UnImplemented")
1213}
1214
npujar1d86a522019-11-14 17:11:16 +05301215// UpdateMembership updates membership
Kent Hagermanc2c73ff2019-11-20 16:22:32 -05001216func (handler *APIHandler) UpdateMembership(context.Context, *voltha.Membership) (*empty.Empty, error) {
1217 return &empty.Empty{}, errors.New("UnImplemented")
1218}