blob: 7bbf5eee34c68041a96a6ab8d9aaaec73a17cf0b [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"
Scott Baker807addd2019-10-24 15:16:21 -070030 "github.com/opencord/voltha-lib-go/v2/pkg/log"
David Bainbridge4087cc52019-11-13 18:36:03 +000031 "github.com/opencord/voltha-lib-go/v2/pkg/version"
Scott Baker555307d2019-11-04 08:58:01 -080032 "github.com/opencord/voltha-protos/v2/go/common"
33 "github.com/opencord/voltha-protos/v2/go/omci"
34 "github.com/opencord/voltha-protos/v2/go/openflow_13"
35 "github.com/opencord/voltha-protos/v2/go/voltha"
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
153 if ownedByMe, err = handler.core.deviceOwnership.OwnedByMe(id); err != nil {
154 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 }
157 acquired, err = txn.Acquired(timeout, ownedByMe)
158 } else {
159 acquired, err = txn.Acquired(timeout)
160 }
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,
218 Level: voltha.LogLevel_LogLevel(level)}
219 logLevels.Items = append(logLevels.Items, logLevel)
220 }
221
222 // now do the default log level
223 logLevel := &voltha.Logging{
224 ComponentName: in.ComponentName,
225 PackageName: "default",
226 Level: voltha.LogLevel_LogLevel(log.GetDefaultLogLevel())}
227 logLevels.Items = append(logLevels.Items, logLevel)
228
229 return logLevels, nil
230}
231
Kent 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 }
npujar1d86a522019-11-14 17:11:16 +0530255 defer txn.Close()
khenaidoo43aa6bd2019-05-29 13:35:13 -0400256 }
257 return handler.logicalDeviceMgr.getLogicalPort(id)
258}
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 }
npujar1d86a522019-11-14 17:11:16 +0530272 defer txn.Close()
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 }
npujar1d86a522019-11-14 17:11:16 +0530293 defer txn.Close()
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 }
npujar1d86a522019-11-14 17:11:16 +0530314 defer txn.Close()
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 }
npujar1d86a522019-11-14 17:11:16 +0530335 defer txn.Close()
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})
khenaidoo19d7b632018-10-30 10:49:50 -0400347 return handler.deviceMgr.GetDevice(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")
355 return handler.deviceMgr.ListDevices()
356}
357
khenaidoo7ccedd52018-12-14 16:48:54 -0500358// ListDeviceIds returns the list of device ids managed by a voltha core
359func (handler *APIHandler) ListDeviceIds(ctx context.Context, empty *empty.Empty) (*voltha.IDs, error) {
360 log.Debug("ListDeviceIDs")
361 if isTestMode(ctx) {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500362 return &voltha.IDs{Items: make([]*voltha.ID, 0)}, nil
khenaidoo7ccedd52018-12-14 16:48:54 -0500363 }
364 return handler.deviceMgr.ListDeviceIds()
365}
366
367//ReconcileDevices is a request to a voltha core to managed a list of devices based on their IDs
368func (handler *APIHandler) ReconcileDevices(ctx context.Context, ids *voltha.IDs) (*empty.Empty, error) {
369 log.Debug("ReconcileDevices")
370 if isTestMode(ctx) {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500371 return &empty.Empty{}, nil
khenaidoo7ccedd52018-12-14 16:48:54 -0500372 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500373
khenaidoo9cdc1a62019-01-24 21:57:40 -0500374 // No need to grab a transaction as this request is core specific
375
khenaidoo7ccedd52018-12-14 16:48:54 -0500376 ch := make(chan interface{})
377 defer close(ch)
378 go handler.deviceMgr.ReconcileDevices(ctx, ids, ch)
379 return waitForNilResponseOnSuccess(ctx, ch)
380}
381
npujar1d86a522019-11-14 17:11:16 +0530382// GetLogicalDevice provides a cloned most up to date logical device
khenaidoob9203542018-09-17 22:56:37 -0400383func (handler *APIHandler) GetLogicalDevice(ctx context.Context, id *voltha.ID) (*voltha.LogicalDevice, error) {
384 log.Debugw("GetLogicalDevice-request", log.Fields{"id": id})
khenaidoo43aa6bd2019-05-29 13:35:13 -0400385 if handler.competeForTransaction() {
npujar1d86a522019-11-14 17:11:16 +0530386 txn, err := handler.takeRequestOwnership(ctx, &utils.LogicalDeviceID{ID: id.Id})
387 if err != nil {
khenaidoo43aa6bd2019-05-29 13:35:13 -0400388 return &voltha.LogicalDevice{}, err
khenaidoo43aa6bd2019-05-29 13:35:13 -0400389 }
npujar1d86a522019-11-14 17:11:16 +0530390 defer txn.Close()
khenaidoo43aa6bd2019-05-29 13:35:13 -0400391 }
khenaidoob9203542018-09-17 22:56:37 -0400392 return handler.logicalDeviceMgr.getLogicalDevice(id.Id)
393}
394
npujar1d86a522019-11-14 17:11:16 +0530395// ListLogicalDevices returns the list of all logical devices
khenaidoob9203542018-09-17 22:56:37 -0400396func (handler *APIHandler) ListLogicalDevices(ctx context.Context, empty *empty.Empty) (*voltha.LogicalDevices, error) {
khenaidoo43aa6bd2019-05-29 13:35:13 -0400397 log.Debug("ListLogicalDevices-request")
398 if handler.competeForTransaction() {
npujar1d86a522019-11-14 17:11:16 +0530399 txn, err := handler.takeRequestOwnership(ctx, nil)
400 if err != nil {
khenaidoo43aa6bd2019-05-29 13:35:13 -0400401 return &voltha.LogicalDevices{}, err
khenaidoo43aa6bd2019-05-29 13:35:13 -0400402 }
npujar1d86a522019-11-14 17:11:16 +0530403 defer txn.Close()
khenaidooa9cc6d02019-11-20 14:56:59 -0500404 if handler.isOFControllerRequest(ctx) {
405 // Since an OF controller is only interested in the set of logical devices managed by thgis Core then return
406 // only logical devices managed/monitored by this Core.
407 return handler.logicalDeviceMgr.listManagedLogicalDevices()
408 }
khenaidoo43aa6bd2019-05-29 13:35:13 -0400409 }
khenaidoob9203542018-09-17 22:56:37 -0400410 return handler.logicalDeviceMgr.listLogicalDevices()
411}
412
khenaidoo21d51152019-02-01 13:48:37 -0500413// ListAdapters returns the contents of all adapters known to the system
414func (handler *APIHandler) ListAdapters(ctx context.Context, empty *empty.Empty) (*voltha.Adapters, error) {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500415 log.Debug("ListAdapters")
khenaidoo21d51152019-02-01 13:48:37 -0500416 return handler.adapterMgr.listAdapters(ctx)
417}
418
npujar1d86a522019-11-14 17:11:16 +0530419// ListLogicalDeviceFlows returns the flows of logical device
khenaidoodd237172019-05-27 16:37:17 -0400420func (handler *APIHandler) ListLogicalDeviceFlows(ctx context.Context, id *voltha.ID) (*openflow_13.Flows, error) {
421 log.Debugw("ListLogicalDeviceFlows", log.Fields{"id": *id})
khenaidoo43aa6bd2019-05-29 13:35:13 -0400422 if handler.competeForTransaction() {
npujar1d86a522019-11-14 17:11:16 +0530423 txn, err := handler.takeRequestOwnership(ctx, &utils.LogicalDeviceID{ID: id.Id})
424 if err != nil {
khenaidoo43aa6bd2019-05-29 13:35:13 -0400425 return &openflow_13.Flows{}, err
khenaidoo43aa6bd2019-05-29 13:35:13 -0400426 }
npujar1d86a522019-11-14 17:11:16 +0530427 defer txn.Close()
khenaidoo43aa6bd2019-05-29 13:35:13 -0400428 }
khenaidoodd237172019-05-27 16:37:17 -0400429 return handler.logicalDeviceMgr.ListLogicalDeviceFlows(ctx, id.Id)
430}
431
npujar1d86a522019-11-14 17:11:16 +0530432// ListLogicalDeviceFlowGroups returns logical device flow groups
khenaidoodd237172019-05-27 16:37:17 -0400433func (handler *APIHandler) ListLogicalDeviceFlowGroups(ctx context.Context, id *voltha.ID) (*openflow_13.FlowGroups, error) {
434 log.Debugw("ListLogicalDeviceFlowGroups", log.Fields{"id": *id})
khenaidoo43aa6bd2019-05-29 13:35:13 -0400435 if handler.competeForTransaction() {
npujar1d86a522019-11-14 17:11:16 +0530436 txn, err := handler.takeRequestOwnership(ctx, &utils.LogicalDeviceID{ID: id.Id})
437 if err != nil {
khenaidoo43aa6bd2019-05-29 13:35:13 -0400438 return &openflow_13.FlowGroups{}, err
khenaidoo43aa6bd2019-05-29 13:35:13 -0400439 }
npujar1d86a522019-11-14 17:11:16 +0530440 defer txn.Close()
khenaidoo43aa6bd2019-05-29 13:35:13 -0400441 }
khenaidoodd237172019-05-27 16:37:17 -0400442 return handler.logicalDeviceMgr.ListLogicalDeviceFlowGroups(ctx, id.Id)
443}
444
npujar1d86a522019-11-14 17:11:16 +0530445// ListLogicalDevicePorts returns ports of logical device
khenaidoo19d7b632018-10-30 10:49:50 -0400446func (handler *APIHandler) ListLogicalDevicePorts(ctx context.Context, id *voltha.ID) (*voltha.LogicalPorts, error) {
447 log.Debugw("ListLogicalDevicePorts", log.Fields{"logicaldeviceid": id})
khenaidoo43aa6bd2019-05-29 13:35:13 -0400448 if handler.competeForTransaction() {
npujar1d86a522019-11-14 17:11:16 +0530449 txn, err := handler.takeRequestOwnership(ctx, &utils.LogicalDeviceID{ID: id.Id})
450 if err != nil {
khenaidoo43aa6bd2019-05-29 13:35:13 -0400451 return &voltha.LogicalPorts{}, err
khenaidoo43aa6bd2019-05-29 13:35:13 -0400452 }
npujar1d86a522019-11-14 17:11:16 +0530453 defer txn.Close()
khenaidoo43aa6bd2019-05-29 13:35:13 -0400454 }
khenaidoo19d7b632018-10-30 10:49:50 -0400455 return handler.logicalDeviceMgr.ListLogicalDevicePorts(ctx, id.Id)
456}
457
khenaidoo4d4802d2018-10-04 21:59:49 -0400458// CreateDevice creates a new parent device in the data model
khenaidoobf6e7bb2018-08-14 22:27:29 -0400459func (handler *APIHandler) CreateDevice(ctx context.Context, device *voltha.Device) (*voltha.Device, error) {
Thomas Lee S51b5cb82019-10-14 14:49:34 +0530460 if device.MacAddress == "" && device.GetHostAndPort() == "" {
461 log.Errorf("No Device Info Present")
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500462 return &voltha.Device{}, errors.New("No Device Info Present; MAC or HOSTIP&PORT")
Thomas Lee S51b5cb82019-10-14 14:49:34 +0530463 }
464 log.Debugw("create-device", log.Fields{"device": *device})
khenaidoobf6e7bb2018-08-14 22:27:29 -0400465 if isTestMode(ctx) {
466 return &voltha.Device{Id: device.Id}, nil
467 }
Richard Jankowskid42826e2018-11-02 16:06:37 -0400468
khenaidoo9cdc1a62019-01-24 21:57:40 -0500469 if handler.competeForTransaction() {
khenaidoo631fe542019-05-31 15:44:43 -0400470 // There are no device Id present in this function.
npujar1d86a522019-11-14 17:11:16 +0530471 txn, err := handler.takeRequestOwnership(ctx, nil)
472 if err != nil {
Richard Jankowski2755adf2019-01-17 17:16:48 -0500473 return &voltha.Device{}, err
Richard Jankowski2755adf2019-01-17 17:16:48 -0500474 }
npujar1d86a522019-11-14 17:11:16 +0530475 defer txn.Close()
Richard Jankowski2755adf2019-01-17 17:16:48 -0500476 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500477
khenaidoob9203542018-09-17 22:56:37 -0400478 ch := make(chan interface{})
479 defer close(ch)
480 go handler.deviceMgr.createDevice(ctx, device, ch)
481 select {
482 case res := <-ch:
khenaidoo92e62c52018-10-03 14:02:54 -0400483 if res != nil {
484 if err, ok := res.(error); ok {
485 return &voltha.Device{}, err
486 }
487 if d, ok := res.(*voltha.Device); ok {
npujar1d86a522019-11-14 17:11:16 +0530488 _, err := handler.core.deviceOwnership.OwnedByMe(&utils.DeviceID{ID: d.Id})
489 if err != nil {
490 log.Errorw("unable-to-find-core-instance-active-owns-this-device", log.Fields{"error": err})
491 }
khenaidoo92e62c52018-10-03 14:02:54 -0400492 return d, nil
493 }
khenaidoob9203542018-09-17 22:56:37 -0400494 }
khenaidoo92e62c52018-10-03 14:02:54 -0400495 log.Warnw("create-device-unexpected-return-type", log.Fields{"result": res})
496 err := status.Errorf(codes.Internal, "%s", res)
497 return &voltha.Device{}, err
khenaidoob9203542018-09-17 22:56:37 -0400498 case <-ctx.Done():
499 log.Debug("createdevice-client-timeout")
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500500 return &voltha.Device{}, ctx.Err()
khenaidoob9203542018-09-17 22:56:37 -0400501 }
khenaidoobf6e7bb2018-08-14 22:27:29 -0400502}
503
khenaidoo4d4802d2018-10-04 21:59:49 -0400504// EnableDevice activates a device by invoking the adopt_device API on the appropriate adapter
khenaidoobf6e7bb2018-08-14 22:27:29 -0400505func (handler *APIHandler) EnableDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) {
khenaidoob9203542018-09-17 22:56:37 -0400506 log.Debugw("enabledevice", log.Fields{"id": id})
khenaidoobf6e7bb2018-08-14 22:27:29 -0400507 if isTestMode(ctx) {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500508 return &empty.Empty{}, nil
khenaidoobf6e7bb2018-08-14 22:27:29 -0400509 }
Richard Jankowskid42826e2018-11-02 16:06:37 -0400510
khenaidoo9cdc1a62019-01-24 21:57:40 -0500511 if handler.competeForTransaction() {
npujar1d86a522019-11-14 17:11:16 +0530512 txn, err := handler.takeRequestOwnership(ctx, &utils.DeviceID{ID: id.Id}, handler.longRunningRequestTimeout)
513 if err != nil {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500514 return &empty.Empty{}, err
Richard Jankowski2755adf2019-01-17 17:16:48 -0500515 }
npujar1d86a522019-11-14 17:11:16 +0530516 defer txn.Close()
Richard Jankowski2755adf2019-01-17 17:16:48 -0500517 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500518
khenaidoob9203542018-09-17 22:56:37 -0400519 ch := make(chan interface{})
520 defer close(ch)
521 go handler.deviceMgr.enableDevice(ctx, id, ch)
khenaidoo4d4802d2018-10-04 21:59:49 -0400522 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400523}
524
khenaidoo4d4802d2018-10-04 21:59:49 -0400525// DisableDevice disables a device along with any child device it may have
khenaidoobf6e7bb2018-08-14 22:27:29 -0400526func (handler *APIHandler) DisableDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) {
527 log.Debugw("disabledevice-request", log.Fields{"id": id})
528 if isTestMode(ctx) {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500529 return &empty.Empty{}, nil
khenaidoobf6e7bb2018-08-14 22:27:29 -0400530 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500531
khenaidoo9cdc1a62019-01-24 21:57:40 -0500532 if handler.competeForTransaction() {
npujar1d86a522019-11-14 17:11:16 +0530533 txn, err := handler.takeRequestOwnership(ctx, &utils.DeviceID{ID: id.Id})
534 if err != nil {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500535 return &empty.Empty{}, err
Richard Jankowski2755adf2019-01-17 17:16:48 -0500536 }
npujar1d86a522019-11-14 17:11:16 +0530537 defer txn.Close()
Richard Jankowski2755adf2019-01-17 17:16:48 -0500538 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500539
khenaidoo92e62c52018-10-03 14:02:54 -0400540 ch := make(chan interface{})
541 defer close(ch)
542 go handler.deviceMgr.disableDevice(ctx, id, ch)
khenaidoo4d4802d2018-10-04 21:59:49 -0400543 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400544}
545
khenaidoo4d4802d2018-10-04 21:59:49 -0400546//RebootDevice invoked the reboot API to the corresponding adapter
khenaidoobf6e7bb2018-08-14 22:27:29 -0400547func (handler *APIHandler) RebootDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) {
khenaidoo4d4802d2018-10-04 21:59:49 -0400548 log.Debugw("rebootDevice-request", log.Fields{"id": id})
khenaidoobf6e7bb2018-08-14 22:27:29 -0400549 if isTestMode(ctx) {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500550 return &empty.Empty{}, nil
khenaidoobf6e7bb2018-08-14 22:27:29 -0400551 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500552
khenaidoo9cdc1a62019-01-24 21:57:40 -0500553 if handler.competeForTransaction() {
npujar1d86a522019-11-14 17:11:16 +0530554 txn, err := handler.takeRequestOwnership(ctx, &utils.DeviceID{ID: id.Id})
555 if err != nil {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500556 return &empty.Empty{}, err
Richard Jankowski2755adf2019-01-17 17:16:48 -0500557 }
npujar1d86a522019-11-14 17:11:16 +0530558 defer txn.Close()
Richard Jankowski2755adf2019-01-17 17:16:48 -0500559 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500560
khenaidoo4d4802d2018-10-04 21:59:49 -0400561 ch := make(chan interface{})
562 defer close(ch)
563 go handler.deviceMgr.rebootDevice(ctx, id, ch)
564 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400565}
566
khenaidoo4d4802d2018-10-04 21:59:49 -0400567// DeleteDevice removes a device from the data model
khenaidoobf6e7bb2018-08-14 22:27:29 -0400568func (handler *APIHandler) DeleteDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) {
569 log.Debugw("deletedevice-request", log.Fields{"id": id})
570 if isTestMode(ctx) {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500571 return &empty.Empty{}, nil
khenaidoobf6e7bb2018-08-14 22:27:29 -0400572 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500573
khenaidoo9cdc1a62019-01-24 21:57:40 -0500574 if handler.competeForTransaction() {
npujar1d86a522019-11-14 17:11:16 +0530575 txn, err := handler.takeRequestOwnership(ctx, &utils.DeviceID{ID: id.Id})
576 if err != nil {
khenaidoo09771ef2019-10-11 14:25:02 -0400577 if err == errorTransactionNotAcquired {
npujar1d86a522019-11-14 17:11:16 +0530578 if ownedByMe, err := handler.core.deviceOwnership.OwnedByMe(&utils.DeviceID{ID: id.Id}); !ownedByMe && err == nil {
khenaidoo09771ef2019-10-11 14:25:02 -0400579 // Remove the device in memory
580 handler.deviceMgr.stopManagingDevice(id.Id)
581 }
khenaidoo6d62c002019-05-15 21:57:03 -0400582 }
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500583 return &empty.Empty{}, err
Richard Jankowski2755adf2019-01-17 17:16:48 -0500584 }
npujar1d86a522019-11-14 17:11:16 +0530585 defer txn.Close()
Richard Jankowski2755adf2019-01-17 17:16:48 -0500586 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500587
khenaidoo4d4802d2018-10-04 21:59:49 -0400588 ch := make(chan interface{})
589 defer close(ch)
590 go handler.deviceMgr.deleteDevice(ctx, id, ch)
591 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400592}
593
David Bainbridge4087cc52019-11-13 18:36:03 +0000594// ListDevicePorts returns the ports details for a specific device entry
595func (handler *APIHandler) ListDevicePorts(ctx context.Context, id *voltha.ID) (*voltha.Ports, error) {
596 log.Debugw("listdeviceports-request", log.Fields{"id": id})
597 if handler.competeForTransaction() {
npujar1d86a522019-11-14 17:11:16 +0530598 txn, err := handler.takeRequestOwnership(ctx, &utils.DeviceID{ID: id.Id})
599 if err != nil {
David Bainbridge4087cc52019-11-13 18:36:03 +0000600 return &voltha.Ports{}, err
David Bainbridge4087cc52019-11-13 18:36:03 +0000601 }
npujar1d86a522019-11-14 17:11:16 +0530602 defer txn.Close()
David Bainbridge4087cc52019-11-13 18:36:03 +0000603 }
604
605 device, err := handler.deviceMgr.GetDevice(id.Id)
606 if err != nil {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500607 return &voltha.Ports{}, err
David Bainbridge4087cc52019-11-13 18:36:03 +0000608 }
609 ports := &voltha.Ports{}
npujar1d86a522019-11-14 17:11:16 +0530610 ports.Items = append(ports.Items, device.Ports...)
David Bainbridge4087cc52019-11-13 18:36:03 +0000611 return ports, nil
612}
613
614// ListDeviceFlows returns the flow details for a specific device entry
615func (handler *APIHandler) ListDeviceFlows(ctx context.Context, id *voltha.ID) (*openflow_13.Flows, error) {
616 log.Debugw("listdeviceflows-request", log.Fields{"id": id})
617 if handler.competeForTransaction() {
npujar1d86a522019-11-14 17:11:16 +0530618 txn, err := handler.takeRequestOwnership(ctx, &utils.DeviceID{ID: id.Id})
619 if err != nil {
David Bainbridge4087cc52019-11-13 18:36:03 +0000620 return &openflow_13.Flows{}, err
David Bainbridge4087cc52019-11-13 18:36:03 +0000621 }
npujar1d86a522019-11-14 17:11:16 +0530622 defer txn.Close()
David Bainbridge4087cc52019-11-13 18:36:03 +0000623 }
624
625 device, err := handler.deviceMgr.GetDevice(id.Id)
626 if err != nil {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500627 return &openflow_13.Flows{}, err
David Bainbridge4087cc52019-11-13 18:36:03 +0000628 }
629 flows := &openflow_13.Flows{}
npujar1d86a522019-11-14 17:11:16 +0530630 flows.Items = append(flows.Items, device.Flows.Items...)
David Bainbridge4087cc52019-11-13 18:36:03 +0000631 return flows, nil
632}
633
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500634// ListDeviceFlowGroups returns the flow group details for a specific device entry
635func (handler *APIHandler) ListDeviceFlowGroups(ctx context.Context, id *voltha.ID) (*voltha.FlowGroups, error) {
636 log.Debugw("ListDeviceFlowGroups", log.Fields{"deviceid": id})
637
638 if device, _ := handler.deviceMgr.GetDevice(id.Id); device != nil {
639 return device.GetFlowGroups(), nil
640 }
641 return &voltha.FlowGroups{}, status.Errorf(codes.NotFound, "device-%s", id.Id)
642}
643
644// ListDeviceGroups returns all the device groups known to the system
645func (handler *APIHandler) ListDeviceGroups(ctx context.Context, empty *empty.Empty) (*voltha.DeviceGroups, error) {
646 log.Debug("ListDeviceGroups")
647 return &voltha.DeviceGroups{}, errors.New("UnImplemented")
648}
649
650// GetDeviceGroup returns a specific device group entry
651func (handler *APIHandler) GetDeviceGroup(ctx context.Context, id *voltha.ID) (*voltha.DeviceGroup, error) {
652 log.Debug("GetDeviceGroup")
653 return &voltha.DeviceGroup{}, errors.New("UnImplemented")
654}
655
656// ListDeviceTypes returns all the device types known to the system
657func (handler *APIHandler) ListDeviceTypes(ctx context.Context, _ *empty.Empty) (*voltha.DeviceTypes, error) {
658 log.Debug("ListDeviceTypes")
659
660 return &voltha.DeviceTypes{Items: handler.adapterMgr.listDeviceTypes()}, nil
661}
662
663// GetDeviceType returns the device type for a specific device entry
664func (handler *APIHandler) GetDeviceType(ctx context.Context, id *voltha.ID) (*voltha.DeviceType, error) {
665 log.Debugw("GetDeviceType", log.Fields{"typeid": id})
666
667 if deviceType := handler.adapterMgr.getDeviceType(id.Id); deviceType != nil {
668 return deviceType, nil
669 }
670 return &voltha.DeviceType{}, status.Errorf(codes.NotFound, "device_type-%s", id.Id)
671}
672
David Bainbridge4087cc52019-11-13 18:36:03 +0000673// GetVoltha returns the contents of all components (i.e. devices, logical_devices, ...)
674func (handler *APIHandler) GetVoltha(ctx context.Context, empty *empty.Empty) (*voltha.Voltha, error) {
675
676 log.Debug("GetVoltha")
677 /*
678 * For now, encode all the version information into a JSON object and
679 * pass that back as "version" so the client can get all the
680 * information associated with the version. Long term the API should
681 * better accomidate this, but for now this will work.
682 */
683 data, err := json.Marshal(&version.VersionInfo)
684 info := version.VersionInfo.Version
685 if err != nil {
686 log.Warnf("Unable to encode version information as JSON: %s", err.Error())
687 } else {
688 info = string(data)
689 }
690
691 return &voltha.Voltha{
692 Version: info,
693 }, nil
694}
695
khenaidoof5a5bfa2019-01-23 22:20:29 -0500696// processImageRequest is a helper method to execute an image download request
697func (handler *APIHandler) processImageRequest(ctx context.Context, img *voltha.ImageDownload, requestType int) (*common.OperationResp, error) {
698 log.Debugw("processImageDownload", log.Fields{"img": *img, "requestType": requestType})
699 if isTestMode(ctx) {
700 resp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS}
701 return resp, nil
702 }
703
khenaidoo9cdc1a62019-01-24 21:57:40 -0500704 if handler.competeForTransaction() {
npujar1d86a522019-11-14 17:11:16 +0530705 txn, err := handler.takeRequestOwnership(ctx, &utils.DeviceID{ID: img.Id})
706 if err != nil {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500707 return &common.OperationResp{}, err
khenaidoo9cdc1a62019-01-24 21:57:40 -0500708 }
npujar1d86a522019-11-14 17:11:16 +0530709 defer txn.Close()
khenaidoof5a5bfa2019-01-23 22:20:29 -0500710 }
711
khenaidoo2c6a0992019-04-29 13:46:56 -0400712 failedresponse := &common.OperationResp{Code: voltha.OperationResp_OPERATION_FAILURE}
khenaidoof5a5bfa2019-01-23 22:20:29 -0500713
714 ch := make(chan interface{})
715 defer close(ch)
716 switch requestType {
npujar1d86a522019-11-14 17:11:16 +0530717 case ImageDownload:
khenaidoof5a5bfa2019-01-23 22:20:29 -0500718 go handler.deviceMgr.downloadImage(ctx, img, ch)
npujar1d86a522019-11-14 17:11:16 +0530719 case CancelImageDownload:
khenaidoof5a5bfa2019-01-23 22:20:29 -0500720 go handler.deviceMgr.cancelImageDownload(ctx, img, ch)
npujar1d86a522019-11-14 17:11:16 +0530721 case ActivateImage:
khenaidoof5a5bfa2019-01-23 22:20:29 -0500722 go handler.deviceMgr.activateImage(ctx, img, ch)
npujar1d86a522019-11-14 17:11:16 +0530723 case RevertImage:
khenaidoof5a5bfa2019-01-23 22:20:29 -0500724 go handler.deviceMgr.revertImage(ctx, img, ch)
725 default:
726 log.Warn("invalid-request-type", log.Fields{"requestType": requestType})
727 return failedresponse, status.Errorf(codes.InvalidArgument, "%d", requestType)
728 }
729 select {
730 case res := <-ch:
731 if res != nil {
732 if err, ok := res.(error); ok {
733 return failedresponse, err
734 }
735 if opResp, ok := res.(*common.OperationResp); ok {
736 return opResp, nil
737 }
738 }
739 log.Warnw("download-image-unexpected-return-type", log.Fields{"result": res})
740 return failedresponse, status.Errorf(codes.Internal, "%s", res)
741 case <-ctx.Done():
742 log.Debug("downloadImage-client-timeout")
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500743 return &common.OperationResp{}, ctx.Err()
khenaidoof5a5bfa2019-01-23 22:20:29 -0500744 }
745}
746
npujar1d86a522019-11-14 17:11:16 +0530747// DownloadImage execute an image download request
khenaidoobf6e7bb2018-08-14 22:27:29 -0400748func (handler *APIHandler) DownloadImage(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) {
749 log.Debugw("DownloadImage-request", log.Fields{"img": *img})
750 if isTestMode(ctx) {
751 resp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS}
752 return resp, nil
753 }
754
npujar1d86a522019-11-14 17:11:16 +0530755 return handler.processImageRequest(ctx, img, ImageDownload)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400756}
757
npujar1d86a522019-11-14 17:11:16 +0530758// CancelImageDownload cancels image download request
khenaidoobf6e7bb2018-08-14 22:27:29 -0400759func (handler *APIHandler) CancelImageDownload(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) {
khenaidoof5a5bfa2019-01-23 22:20:29 -0500760 log.Debugw("cancelImageDownload-request", log.Fields{"img": *img})
khenaidoobf6e7bb2018-08-14 22:27:29 -0400761 if isTestMode(ctx) {
762 resp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS}
763 return resp, nil
764 }
npujar1d86a522019-11-14 17:11:16 +0530765 return handler.processImageRequest(ctx, img, CancelImageDownload)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400766}
767
npujar1d86a522019-11-14 17:11:16 +0530768// ActivateImageUpdate activates image update request
khenaidoobf6e7bb2018-08-14 22:27:29 -0400769func (handler *APIHandler) ActivateImageUpdate(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) {
khenaidoof5a5bfa2019-01-23 22:20:29 -0500770 log.Debugw("activateImageUpdate-request", log.Fields{"img": *img})
khenaidoobf6e7bb2018-08-14 22:27:29 -0400771 if isTestMode(ctx) {
772 resp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS}
773 return resp, nil
774 }
khenaidoof5a5bfa2019-01-23 22:20:29 -0500775
npujar1d86a522019-11-14 17:11:16 +0530776 return handler.processImageRequest(ctx, img, ActivateImage)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400777}
778
npujar1d86a522019-11-14 17:11:16 +0530779// RevertImageUpdate reverts image update
khenaidoobf6e7bb2018-08-14 22:27:29 -0400780func (handler *APIHandler) RevertImageUpdate(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) {
khenaidoof5a5bfa2019-01-23 22:20:29 -0500781 log.Debugw("revertImageUpdate-request", log.Fields{"img": *img})
khenaidoobf6e7bb2018-08-14 22:27:29 -0400782 if isTestMode(ctx) {
783 resp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS}
784 return resp, nil
785 }
khenaidoof5a5bfa2019-01-23 22:20:29 -0500786
npujar1d86a522019-11-14 17:11:16 +0530787 return handler.processImageRequest(ctx, img, RevertImage)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400788}
789
npujar1d86a522019-11-14 17:11:16 +0530790// GetImageDownloadStatus returns status of image download
khenaidoof5a5bfa2019-01-23 22:20:29 -0500791func (handler *APIHandler) GetImageDownloadStatus(ctx context.Context, img *voltha.ImageDownload) (*voltha.ImageDownload, error) {
792 log.Debugw("getImageDownloadStatus-request", log.Fields{"img": *img})
793 if isTestMode(ctx) {
Stephane Barbariedf5479f2019-01-29 22:13:00 -0500794 resp := &voltha.ImageDownload{DownloadState: voltha.ImageDownload_DOWNLOAD_SUCCEEDED}
khenaidoof5a5bfa2019-01-23 22:20:29 -0500795 return resp, nil
796 }
797
Stephane Barbariedf5479f2019-01-29 22:13:00 -0500798 failedresponse := &voltha.ImageDownload{DownloadState: voltha.ImageDownload_DOWNLOAD_UNKNOWN}
khenaidoof5a5bfa2019-01-23 22:20:29 -0500799
khenaidoo9cdc1a62019-01-24 21:57:40 -0500800 if handler.competeForTransaction() {
npujar1d86a522019-11-14 17:11:16 +0530801 txn, err := handler.takeRequestOwnership(ctx, &utils.DeviceID{ID: img.Id})
802 if err != nil {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500803 return failedresponse, err
khenaidoo9cdc1a62019-01-24 21:57:40 -0500804 }
npujar1d86a522019-11-14 17:11:16 +0530805 defer txn.Close()
khenaidoof5a5bfa2019-01-23 22:20:29 -0500806 }
807
808 ch := make(chan interface{})
809 defer close(ch)
810 go handler.deviceMgr.getImageDownloadStatus(ctx, img, ch)
811
812 select {
813 case res := <-ch:
814 if res != nil {
815 if err, ok := res.(error); ok {
816 return failedresponse, err
817 }
818 if downloadResp, ok := res.(*voltha.ImageDownload); ok {
819 return downloadResp, nil
820 }
821 }
822 log.Warnw("download-image-status", log.Fields{"result": res})
823 return failedresponse, status.Errorf(codes.Internal, "%s", res)
824 case <-ctx.Done():
825 log.Debug("downloadImage-client-timeout")
826 return failedresponse, ctx.Err()
827 }
828}
829
npujar1d86a522019-11-14 17:11:16 +0530830// GetImageDownload returns image download
khenaidoof5a5bfa2019-01-23 22:20:29 -0500831func (handler *APIHandler) GetImageDownload(ctx context.Context, img *voltha.ImageDownload) (*voltha.ImageDownload, error) {
832 log.Debugw("GetImageDownload-request", log.Fields{"img": *img})
833 if isTestMode(ctx) {
Stephane Barbariedf5479f2019-01-29 22:13:00 -0500834 resp := &voltha.ImageDownload{DownloadState: voltha.ImageDownload_DOWNLOAD_SUCCEEDED}
khenaidoof5a5bfa2019-01-23 22:20:29 -0500835 return resp, nil
836 }
837
npujar1d86a522019-11-14 17:11:16 +0530838 download, err := handler.deviceMgr.getImageDownload(ctx, img)
839 if err != nil {
Stephane Barbariedf5479f2019-01-29 22:13:00 -0500840 return &voltha.ImageDownload{DownloadState: voltha.ImageDownload_DOWNLOAD_UNKNOWN}, err
khenaidoof5a5bfa2019-01-23 22:20:29 -0500841 }
npujar1d86a522019-11-14 17:11:16 +0530842 return download, nil
khenaidoof5a5bfa2019-01-23 22:20:29 -0500843}
844
npujar1d86a522019-11-14 17:11:16 +0530845// ListImageDownloads returns image downloads
khenaidoof5a5bfa2019-01-23 22:20:29 -0500846func (handler *APIHandler) ListImageDownloads(ctx context.Context, id *voltha.ID) (*voltha.ImageDownloads, error) {
847 log.Debugw("ListImageDownloads-request", log.Fields{"deviceId": id.Id})
848 if isTestMode(ctx) {
khenaidoo2c6a0992019-04-29 13:46:56 -0400849 resp := &voltha.ImageDownloads{Items: []*voltha.ImageDownload{}}
khenaidoof5a5bfa2019-01-23 22:20:29 -0500850 return resp, nil
851 }
852
npujar1d86a522019-11-14 17:11:16 +0530853 downloads, err := handler.deviceMgr.listImageDownloads(ctx, id.Id)
854 if err != nil {
khenaidoof5a5bfa2019-01-23 22:20:29 -0500855 failedResp := &voltha.ImageDownloads{
khenaidoo2c6a0992019-04-29 13:46:56 -0400856 Items: []*voltha.ImageDownload{
857 {DownloadState: voltha.ImageDownload_DOWNLOAD_UNKNOWN},
858 },
khenaidoof5a5bfa2019-01-23 22:20:29 -0500859 }
860 return failedResp, err
khenaidoof5a5bfa2019-01-23 22:20:29 -0500861 }
npujar1d86a522019-11-14 17:11:16 +0530862 return downloads, nil
khenaidoof5a5bfa2019-01-23 22:20:29 -0500863}
864
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500865// GetImages returns all images for a specific device entry
866func (handler *APIHandler) GetImages(ctx context.Context, id *voltha.ID) (*voltha.Images, error) {
867 log.Debugw("GetImages", log.Fields{"deviceid": id.Id})
868 device, err := handler.deviceMgr.GetDevice(id.Id)
869 if err != nil {
870 return &voltha.Images{}, err
871 }
872 return device.GetImages(), nil
873}
874
npujar1d86a522019-11-14 17:11:16 +0530875// UpdateDevicePmConfigs updates the PM configs
khenaidoobf6e7bb2018-08-14 22:27:29 -0400876func (handler *APIHandler) UpdateDevicePmConfigs(ctx context.Context, configs *voltha.PmConfigs) (*empty.Empty, error) {
877 log.Debugw("UpdateDevicePmConfigs-request", log.Fields{"configs": *configs})
878 if isTestMode(ctx) {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500879 return &empty.Empty{}, nil
khenaidoobf6e7bb2018-08-14 22:27:29 -0400880 }
khenaidoob3127472019-07-24 21:04:55 -0400881 if handler.competeForTransaction() {
npujar1d86a522019-11-14 17:11:16 +0530882 txn, err := handler.takeRequestOwnership(ctx, &utils.DeviceID{ID: configs.Id})
883 if err != nil {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500884 return &empty.Empty{}, err
khenaidoob3127472019-07-24 21:04:55 -0400885 }
npujar1d86a522019-11-14 17:11:16 +0530886 defer txn.Close()
khenaidoob3127472019-07-24 21:04:55 -0400887 }
888
889 ch := make(chan interface{})
890 defer close(ch)
891 go handler.deviceMgr.updatePmConfigs(ctx, configs, ch)
892 return waitForNilResponseOnSuccess(ctx, ch)
893}
894
npujar1d86a522019-11-14 17:11:16 +0530895// ListDevicePmConfigs returns pm configs of device
khenaidoob3127472019-07-24 21:04:55 -0400896func (handler *APIHandler) ListDevicePmConfigs(ctx context.Context, id *voltha.ID) (*voltha.PmConfigs, error) {
897 log.Debugw("ListDevicePmConfigs-request", log.Fields{"deviceId": *id})
898 if handler.competeForTransaction() {
npujar1d86a522019-11-14 17:11:16 +0530899 txn, err := handler.takeRequestOwnership(ctx, &utils.LogicalDeviceID{ID: id.Id})
900 if err != nil {
khenaidoob3127472019-07-24 21:04:55 -0400901 return &voltha.PmConfigs{}, err
khenaidoob3127472019-07-24 21:04:55 -0400902 }
npujar1d86a522019-11-14 17:11:16 +0530903 defer txn.Close()
khenaidoob3127472019-07-24 21:04:55 -0400904 }
905 return handler.deviceMgr.listPmConfigs(ctx, id.Id)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400906}
907
npujar1d86a522019-11-14 17:11:16 +0530908// CreateAlarmFilter creates alarm filter
khenaidoobf6e7bb2018-08-14 22:27:29 -0400909func (handler *APIHandler) CreateAlarmFilter(ctx context.Context, filter *voltha.AlarmFilter) (*voltha.AlarmFilter, error) {
910 log.Debugw("CreateAlarmFilter-request", log.Fields{"filter": *filter})
911 if isTestMode(ctx) {
912 f := &voltha.AlarmFilter{Id: filter.Id}
913 return f, nil
914 }
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500915 return &voltha.AlarmFilter{}, errors.New("UnImplemented")
khenaidoobf6e7bb2018-08-14 22:27:29 -0400916}
917
npujar1d86a522019-11-14 17:11:16 +0530918// UpdateAlarmFilter updates alarm filter
khenaidoobf6e7bb2018-08-14 22:27:29 -0400919func (handler *APIHandler) UpdateAlarmFilter(ctx context.Context, filter *voltha.AlarmFilter) (*voltha.AlarmFilter, error) {
920 log.Debugw("UpdateAlarmFilter-request", log.Fields{"filter": *filter})
921 if isTestMode(ctx) {
922 f := &voltha.AlarmFilter{Id: filter.Id}
923 return f, nil
924 }
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500925 return &voltha.AlarmFilter{}, errors.New("UnImplemented")
khenaidoobf6e7bb2018-08-14 22:27:29 -0400926}
927
npujar1d86a522019-11-14 17:11:16 +0530928// DeleteAlarmFilter deletes alarm filter
khenaidoobf6e7bb2018-08-14 22:27:29 -0400929func (handler *APIHandler) DeleteAlarmFilter(ctx context.Context, id *voltha.ID) (*empty.Empty, error) {
930 log.Debugw("DeleteAlarmFilter-request", log.Fields{"id": *id})
931 if isTestMode(ctx) {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500932 return &empty.Empty{}, nil
khenaidoobf6e7bb2018-08-14 22:27:29 -0400933 }
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500934 return &empty.Empty{}, errors.New("UnImplemented")
935}
936
npujar1d86a522019-11-14 17:11:16 +0530937// ListAlarmFilters returns alarm filters
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500938func (handler *APIHandler) ListAlarmFilters(ctx context.Context, empty *empty.Empty) (*voltha.AlarmFilters, error) {
939 log.Debug("ListAlarmFilters")
940 return &voltha.AlarmFilters{}, errors.New("UnImplemented")
941}
942
npujar1d86a522019-11-14 17:11:16 +0530943// GetAlarmFilter returns alarm filter
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500944func (handler *APIHandler) GetAlarmFilter(ctx context.Context, id *voltha.ID) (*voltha.AlarmFilter, error) {
945 log.Debug("GetAlarmFilter")
946 return &voltha.AlarmFilter{}, errors.New("UnImplemented")
khenaidoobf6e7bb2018-08-14 22:27:29 -0400947}
948
npujar1d86a522019-11-14 17:11:16 +0530949// SelfTest executes self test
khenaidoobf6e7bb2018-08-14 22:27:29 -0400950func (handler *APIHandler) SelfTest(ctx context.Context, id *voltha.ID) (*voltha.SelfTestResponse, error) {
951 log.Debugw("SelfTest-request", log.Fields{"id": id})
952 if isTestMode(ctx) {
953 resp := &voltha.SelfTestResponse{Result: voltha.SelfTestResponse_SUCCESS}
954 return resp, nil
955 }
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500956 return &voltha.SelfTestResponse{}, errors.New("UnImplemented")
khenaidoobf6e7bb2018-08-14 22:27:29 -0400957}
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500958
959func (handler *APIHandler) forwardPacketOut(packet *openflow_13.PacketOut) {
960 log.Debugw("forwardPacketOut-request", log.Fields{"packet": packet})
khenaidoo3d3b8c22019-05-22 18:10:39 -0400961 //TODO: Update this logic once the OF Controller (OFAgent in this case) can include a transaction Id in its
962 // request. For performance reason we can let both Cores in a Core-Pair forward the Packet to the adapters and
963 // let once of the shim layer (kafka proxy or adapter request handler filters out the duplicate packet)
npujar1d86a522019-11-14 17:11:16 +0530964 if ownedByMe, err := handler.core.deviceOwnership.OwnedByMe(&utils.LogicalDeviceID{ID: packet.Id}); ownedByMe && err == nil {
khenaidoo3d3b8c22019-05-22 18:10:39 -0400965 agent := handler.logicalDeviceMgr.getLogicalDeviceAgent(packet.Id)
966 agent.packetOut(packet.PacketOut)
967 }
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500968}
khenaidoo3d3b8c22019-05-22 18:10:39 -0400969
npujar1d86a522019-11-14 17:11:16 +0530970// StreamPacketsOut sends packets to adapter
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500971func (handler *APIHandler) StreamPacketsOut(packets voltha.VolthaService_StreamPacketsOutServer) error {
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500972 log.Debugw("StreamPacketsOut-request", log.Fields{"packets": packets})
khenaidoo5e250692019-08-30 14:46:21 -0400973loop:
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500974 for {
khenaidoo5e250692019-08-30 14:46:21 -0400975 select {
976 case <-packets.Context().Done():
977 log.Infow("StreamPacketsOut-context-done", log.Fields{"packets": packets, "error": packets.Context().Err()})
978 break loop
979 default:
980 }
981
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500982 packet, err := packets.Recv()
983
984 if err == io.EOF {
khenaidoo5e250692019-08-30 14:46:21 -0400985 log.Debugw("Received-EOF", log.Fields{"packets": packets})
986 break loop
987 }
988
989 if err != nil {
990 log.Errorw("Failed to receive packet out", log.Fields{"error": err})
991 continue
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500992 }
993
994 handler.forwardPacketOut(packet)
995 }
996
997 log.Debugw("StreamPacketsOut-request-done", log.Fields{"packets": packets})
998 return nil
999}
1000
npujar1d86a522019-11-14 17:11:16 +05301001func (handler *APIHandler) sendPacketIn(deviceID string, transationID string, packet *openflow_13.OfpPacketIn) {
khenaidoo297cd252019-02-07 22:10:23 -05001002 // TODO: Augment the OF PacketIn to include the transactionId
npujar1d86a522019-11-14 17:11:16 +05301003 packetIn := openflow_13.PacketIn{Id: deviceID, PacketIn: packet}
Stephane Barbarie6e1bd502018-11-05 22:44:45 -05001004 log.Debugw("sendPacketIn", log.Fields{"packetIn": packetIn})
A R Karthick881e7ea2019-08-19 19:44:02 +00001005 handler.packetInQueue <- packetIn
1006}
1007
1008type callTracker struct {
1009 failedPacket interface{}
1010}
1011type streamTracker struct {
1012 calls map[string]*callTracker
1013 sync.Mutex
1014}
1015
1016var streamingTracker = &streamTracker{calls: make(map[string]*callTracker)}
1017
1018func (handler *APIHandler) getStreamingTracker(method string, done chan<- bool) *callTracker {
1019 streamingTracker.Lock()
1020 defer streamingTracker.Unlock()
1021 if _, ok := streamingTracker.calls[method]; ok {
1022 // bail out the other packet in thread
1023 log.Debugf("%s streaming call already running. Exiting it", method)
1024 done <- true
1025 log.Debugf("Last %s exited. Continuing ...", method)
1026 } else {
1027 streamingTracker.calls[method] = &callTracker{failedPacket: nil}
Richard Jankowskidbab94a2018-12-06 16:20:25 -05001028 }
A R Karthick881e7ea2019-08-19 19:44:02 +00001029 return streamingTracker.calls[method]
1030}
1031
1032func (handler *APIHandler) flushFailedPackets(tracker *callTracker) error {
1033 if tracker.failedPacket != nil {
1034 switch tracker.failedPacket.(type) {
1035 case openflow_13.PacketIn:
1036 log.Debug("Enqueueing last failed packetIn")
1037 handler.packetInQueue <- tracker.failedPacket.(openflow_13.PacketIn)
1038 case openflow_13.ChangeEvent:
1039 log.Debug("Enqueueing last failed changeEvent")
1040 handler.changeEventQueue <- tracker.failedPacket.(openflow_13.ChangeEvent)
1041 }
1042 }
1043 return nil
Stephane Barbarie6e1bd502018-11-05 22:44:45 -05001044}
1045
npujar1d86a522019-11-14 17:11:16 +05301046// ReceivePacketsIn receives packets from adapter
Kent Hagermanc2c73ff2019-11-20 16:22:32 -05001047func (handler *APIHandler) ReceivePacketsIn(empty *empty.Empty, packetsIn voltha.VolthaService_ReceivePacketsInServer) error {
A R Karthick881e7ea2019-08-19 19:44:02 +00001048 var streamingTracker = handler.getStreamingTracker("ReceivePacketsIn", handler.packetInQueueDone)
Stephane Barbarie6e1bd502018-11-05 22:44:45 -05001049 log.Debugw("ReceivePacketsIn-request", log.Fields{"packetsIn": packetsIn})
1050
npujar1d86a522019-11-14 17:11:16 +05301051 err := handler.flushFailedPackets(streamingTracker)
1052 if err != nil {
1053 log.Errorw("unable-to-flush-failed-packets", log.Fields{"error": err})
1054 }
A R Karthick881e7ea2019-08-19 19:44:02 +00001055
1056loop:
Stephane Barbarie6e1bd502018-11-05 22:44:45 -05001057 for {
A R Karthick881e7ea2019-08-19 19:44:02 +00001058 select {
1059 case packet := <-handler.packetInQueue:
Matteo Scandolo360605d2019-11-05 18:29:17 -08001060 log.Debugw("sending-packet-in", log.Fields{
1061 "packet": hex.EncodeToString(packet.PacketIn.Data),
1062 })
A R Karthick881e7ea2019-08-19 19:44:02 +00001063 if err := packetsIn.Send(&packet); err != nil {
1064 log.Errorw("failed-to-send-packet", log.Fields{"error": err})
1065 // save the last failed packet in
1066 streamingTracker.failedPacket = packet
1067 } else {
1068 if streamingTracker.failedPacket != nil {
1069 // reset last failed packet saved to avoid flush
1070 streamingTracker.failedPacket = nil
Richard Jankowskidbab94a2018-12-06 16:20:25 -05001071 }
1072 }
A R Karthick881e7ea2019-08-19 19:44:02 +00001073 case <-handler.packetInQueueDone:
1074 log.Debug("Another ReceivePacketsIn running. Bailing out ...")
1075 break loop
Stephane Barbarie6e1bd502018-11-05 22:44:45 -05001076 }
1077 }
A R Karthick881e7ea2019-08-19 19:44:02 +00001078
1079 //TODO: Find an elegant way to get out of the above loop when the Core is stopped
1080 return nil
Stephane Barbarie6e1bd502018-11-05 22:44:45 -05001081}
1082
npujar1d86a522019-11-14 17:11:16 +05301083func (handler *APIHandler) sendChangeEvent(deviceID string, portStatus *openflow_13.OfpPortStatus) {
Stephane Barbarie6e1bd502018-11-05 22:44:45 -05001084 // TODO: validate the type of portStatus parameter
1085 //if _, ok := portStatus.(*openflow_13.OfpPortStatus); ok {
1086 //}
npujar1d86a522019-11-14 17:11:16 +05301087 event := openflow_13.ChangeEvent{Id: deviceID, Event: &openflow_13.ChangeEvent_PortStatus{PortStatus: portStatus}}
Stephane Barbarie6e1bd502018-11-05 22:44:45 -05001088 log.Debugw("sendChangeEvent", log.Fields{"event": event})
A R Karthick881e7ea2019-08-19 19:44:02 +00001089 handler.changeEventQueue <- event
Stephane Barbarie6e1bd502018-11-05 22:44:45 -05001090}
1091
npujar1d86a522019-11-14 17:11:16 +05301092// ReceiveChangeEvents receives change in events
Kent Hagermanc2c73ff2019-11-20 16:22:32 -05001093func (handler *APIHandler) ReceiveChangeEvents(empty *empty.Empty, changeEvents voltha.VolthaService_ReceiveChangeEventsServer) error {
A R Karthick881e7ea2019-08-19 19:44:02 +00001094 var streamingTracker = handler.getStreamingTracker("ReceiveChangeEvents", handler.changeEventQueueDone)
Stephane Barbarie6e1bd502018-11-05 22:44:45 -05001095 log.Debugw("ReceiveChangeEvents-request", log.Fields{"changeEvents": changeEvents})
A R Karthick881e7ea2019-08-19 19:44:02 +00001096
npujar1d86a522019-11-14 17:11:16 +05301097 err := handler.flushFailedPackets(streamingTracker)
1098 if err != nil {
1099 log.Errorw("unable-to-flush-failed-packets", log.Fields{"error": err})
1100 }
A R Karthick881e7ea2019-08-19 19:44:02 +00001101
1102loop:
Stephane Barbarie6e1bd502018-11-05 22:44:45 -05001103 for {
A R Karthick881e7ea2019-08-19 19:44:02 +00001104 select {
Richard Jankowski199fd862019-03-18 14:49:51 -04001105 // Dequeue a change event
A R Karthick881e7ea2019-08-19 19:44:02 +00001106 case event := <-handler.changeEventQueue:
1107 log.Debugw("sending-change-event", log.Fields{"event": event})
1108 if err := changeEvents.Send(&event); err != nil {
1109 log.Errorw("failed-to-send-change-event", log.Fields{"error": err})
1110 // save last failed changeevent
1111 streamingTracker.failedPacket = event
1112 } else {
1113 if streamingTracker.failedPacket != nil {
1114 // reset last failed event saved on success to avoid flushing
1115 streamingTracker.failedPacket = nil
Richard Jankowski199fd862019-03-18 14:49:51 -04001116 }
1117 }
A R Karthick881e7ea2019-08-19 19:44:02 +00001118 case <-handler.changeEventQueueDone:
1119 log.Debug("Another ReceiveChangeEvents already running. Bailing out ...")
1120 break loop
Stephane Barbarie6e1bd502018-11-05 22:44:45 -05001121 }
1122 }
A R Karthick881e7ea2019-08-19 19:44:02 +00001123
1124 return nil
Richard Jankowski199fd862019-03-18 14:49:51 -04001125}
Stephane Barbarie6e1bd502018-11-05 22:44:45 -05001126
npujar1d86a522019-11-14 17:11:16 +05301127// Subscribe subscribing request of ofagent
Stephane Barbarie6e1bd502018-11-05 22:44:45 -05001128func (handler *APIHandler) Subscribe(
1129 ctx context.Context,
1130 ofAgent *voltha.OfAgentSubscriber,
1131) (*voltha.OfAgentSubscriber, error) {
1132 log.Debugw("Subscribe-request", log.Fields{"ofAgent": ofAgent})
1133 return &voltha.OfAgentSubscriber{OfagentId: ofAgent.OfagentId, VolthaId: ofAgent.VolthaId}, nil
1134}
William Kurkiandaa6bb22019-03-07 12:26:28 -05001135
npujar1d86a522019-11-14 17:11:16 +05301136// GetAlarmDeviceData @TODO useless stub, what should this actually do?
Kent Hagermanc2c73ff2019-11-20 16:22:32 -05001137func (handler *APIHandler) GetAlarmDeviceData(ctx context.Context, in *common.ID) (*omci.AlarmDeviceData, error) {
William Kurkiandaa6bb22019-03-07 12:26:28 -05001138 log.Debug("GetAlarmDeviceData-stub")
Kent Hagermanc2c73ff2019-11-20 16:22:32 -05001139 return &omci.AlarmDeviceData{}, errors.New("UnImplemented")
William Kurkiandaa6bb22019-03-07 12:26:28 -05001140}
1141
npujar1d86a522019-11-14 17:11:16 +05301142// ListLogicalDeviceMeters returns logical device meters
Manikkaraj kb1a10922019-07-29 12:10:34 -04001143func (handler *APIHandler) ListLogicalDeviceMeters(ctx context.Context, id *voltha.ID) (*openflow_13.Meters, error) {
1144
1145 log.Debugw("ListLogicalDeviceMeters", log.Fields{"id": *id})
1146 if handler.competeForTransaction() {
npujar1d86a522019-11-14 17:11:16 +05301147 txn, err := handler.takeRequestOwnership(ctx, &utils.LogicalDeviceID{ID: id.Id})
1148 if err != nil {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -05001149 return &openflow_13.Meters{}, err // TODO: Return empty meter entry
Manikkaraj kb1a10922019-07-29 12:10:34 -04001150 }
npujar1d86a522019-11-14 17:11:16 +05301151 defer txn.Close()
Manikkaraj kb1a10922019-07-29 12:10:34 -04001152 }
1153 return handler.logicalDeviceMgr.ListLogicalDeviceMeters(ctx, id.Id)
William Kurkiandaa6bb22019-03-07 12:26:28 -05001154}
1155
npujar1d86a522019-11-14 17:11:16 +05301156// GetMeterStatsOfLogicalDevice @TODO useless stub, what should this actually do?
Kent Hagermanc2c73ff2019-11-20 16:22:32 -05001157func (handler *APIHandler) GetMeterStatsOfLogicalDevice(ctx context.Context, in *common.ID) (*openflow_13.MeterStatsReply, error) {
1158 log.Debug("GetMeterStatsOfLogicalDevice")
1159 return &openflow_13.MeterStatsReply{}, errors.New("UnImplemented")
1160}
1161
npujar1d86a522019-11-14 17:11:16 +05301162// GetMibDeviceData @TODO useless stub, what should this actually do?
Kent Hagermanc2c73ff2019-11-20 16:22:32 -05001163func (handler *APIHandler) GetMibDeviceData(ctx context.Context, in *common.ID) (*omci.MibDeviceData, error) {
1164 log.Debug("GetMibDeviceData")
1165 return &omci.MibDeviceData{}, errors.New("UnImplemented")
William Kurkiandaa6bb22019-03-07 12:26:28 -05001166}
1167
npujar1d86a522019-11-14 17:11:16 +05301168// SimulateAlarm sends simulate alarm request
William Kurkiandaa6bb22019-03-07 12:26:28 -05001169func (handler *APIHandler) SimulateAlarm(
1170 ctx context.Context,
1171 in *voltha.SimulateAlarmRequest,
1172) (*common.OperationResp, error) {
serkant.uluderya334479d2019-04-10 08:26:15 -07001173 log.Debugw("SimulateAlarm-request", log.Fields{"id": in.Id})
1174 successResp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS}
1175 if isTestMode(ctx) {
1176 return successResp, nil
1177 }
1178
1179 if handler.competeForTransaction() {
npujar1d86a522019-11-14 17:11:16 +05301180 txn, err := handler.takeRequestOwnership(ctx, &utils.DeviceID{ID: in.Id}, handler.longRunningRequestTimeout)
1181 if err != nil {
Kent Hagerman0ab4cb22019-04-24 13:13:35 -04001182 failedresponse := &common.OperationResp{Code: voltha.OperationResp_OPERATION_FAILURE}
serkant.uluderya334479d2019-04-10 08:26:15 -07001183 return failedresponse, err
serkant.uluderya334479d2019-04-10 08:26:15 -07001184 }
npujar1d86a522019-11-14 17:11:16 +05301185 defer txn.Close()
serkant.uluderya334479d2019-04-10 08:26:15 -07001186 }
1187
1188 ch := make(chan interface{})
1189 defer close(ch)
1190 go handler.deviceMgr.simulateAlarm(ctx, in, ch)
1191 return successResp, nil
William Kurkiandaa6bb22019-03-07 12:26:28 -05001192}
1193
npujar1d86a522019-11-14 17:11:16 +05301194// UpdateLogicalDeviceMeterTable - This function sends meter mod request to logical device manager and waits for response
Manikkaraj kb1a10922019-07-29 12:10:34 -04001195func (handler *APIHandler) UpdateLogicalDeviceMeterTable(ctx context.Context, meter *openflow_13.MeterModUpdate) (*empty.Empty, error) {
1196 log.Debugw("UpdateLogicalDeviceMeterTable-request",
1197 log.Fields{"meter": meter, "test": common.TestModeKeys_api_test.String()})
1198 if isTestMode(ctx) {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -05001199 return &empty.Empty{}, nil
Manikkaraj kb1a10922019-07-29 12:10:34 -04001200 }
1201
1202 if handler.competeForTransaction() {
npujar1d86a522019-11-14 17:11:16 +05301203 txn, err := handler.takeRequestOwnership(ctx, &utils.LogicalDeviceID{ID: meter.Id})
1204 if err != nil {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -05001205 return &empty.Empty{}, err
Manikkaraj kb1a10922019-07-29 12:10:34 -04001206 }
npujar1d86a522019-11-14 17:11:16 +05301207 defer txn.Close()
Manikkaraj kb1a10922019-07-29 12:10:34 -04001208 }
1209
1210 ch := make(chan interface{})
1211 defer close(ch)
1212 go handler.logicalDeviceMgr.updateMeterTable(ctx, meter.Id, meter.MeterMod, ch)
1213 return waitForNilResponseOnSuccess(ctx, ch)
William Kurkiandaa6bb22019-03-07 12:26:28 -05001214}
Kent Hagermanc2c73ff2019-11-20 16:22:32 -05001215
npujar1d86a522019-11-14 17:11:16 +05301216// GetMembership returns membership
Kent Hagermanc2c73ff2019-11-20 16:22:32 -05001217func (handler *APIHandler) GetMembership(context.Context, *empty.Empty) (*voltha.Membership, error) {
1218 return &voltha.Membership{}, errors.New("UnImplemented")
1219}
1220
npujar1d86a522019-11-14 17:11:16 +05301221// UpdateMembership updates membership
Kent Hagermanc2c73ff2019-11-20 16:22:32 -05001222func (handler *APIHandler) UpdateMembership(context.Context, *voltha.Membership) (*empty.Empty, error) {
1223 return &empty.Empty{}, errors.New("UnImplemented")
1224}