blob: 30ea5edf7a82553ef7ea48124fd377322635a4b2 [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"
khenaidoo442e7c72020-03-10 16:13:48 -040026 "time"
npujar1d86a522019-11-14 17:11:16 +053027
khenaidoobf6e7bb2018-08-14 22:27:29 -040028 "github.com/golang/protobuf/ptypes/empty"
29 da "github.com/opencord/voltha-go/common/core/northbound/grpc"
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"
khenaidoob9203542018-09-17 22:56:37 -040037 "google.golang.org/grpc/status"
khenaidoobf6e7bb2018-08-14 22:27:29 -040038)
39
npujar1d86a522019-11-14 17:11:16 +053040// Image related constants
khenaidoof5a5bfa2019-01-23 22:20:29 -050041const (
npujar1d86a522019-11-14 17:11:16 +053042 ImageDownload = iota
43 CancelImageDownload = iota
44 ActivateImage = iota
45 RevertImage = iota
khenaidoof5a5bfa2019-01-23 22:20:29 -050046)
47
npujar1d86a522019-11-14 17:11:16 +053048// APIHandler represent attributes of API handler
khenaidoobf6e7bb2018-08-14 22:27:29 -040049type APIHandler struct {
khenaidoo2c6a0992019-04-29 13:46:56 -040050 deviceMgr *DeviceManager
51 logicalDeviceMgr *LogicalDeviceManager
52 adapterMgr *AdapterManager
A R Karthick881e7ea2019-08-19 19:44:02 +000053 packetInQueue chan openflow_13.PacketIn
54 changeEventQueue chan openflow_13.ChangeEvent
55 packetInQueueDone chan bool
56 changeEventQueueDone chan bool
khenaidoo2c6a0992019-04-29 13:46:56 -040057 coreInCompetingMode bool
khenaidoo442e7c72020-03-10 16:13:48 -040058 longRunningRequestTimeout time.Duration
59 defaultRequestTimeout time.Duration
khenaidoobf6e7bb2018-08-14 22:27:29 -040060 da.DefaultAPIHandler
khenaidoo54e0ddf2019-02-27 16:21:33 -050061 core *Core
khenaidoobf6e7bb2018-08-14 22:27:29 -040062}
63
npujar1d86a522019-11-14 17:11:16 +053064// NewAPIHandler creates API handler instance
khenaidoo54e0ddf2019-02-27 16:21:33 -050065func NewAPIHandler(core *Core) *APIHandler {
Stephane Barbarie6e1bd502018-11-05 22:44:45 -050066 handler := &APIHandler{
khenaidoo2c6a0992019-04-29 13:46:56 -040067 deviceMgr: core.deviceMgr,
68 logicalDeviceMgr: core.logicalDeviceMgr,
69 adapterMgr: core.adapterMgr,
70 coreInCompetingMode: core.config.InCompetingMode,
71 longRunningRequestTimeout: core.config.LongRunningRequestTimeout,
72 defaultRequestTimeout: core.config.DefaultRequestTimeout,
A R Karthick881e7ea2019-08-19 19:44:02 +000073 packetInQueue: make(chan openflow_13.PacketIn, 100),
74 changeEventQueue: make(chan openflow_13.ChangeEvent, 100),
75 packetInQueueDone: make(chan bool, 1),
76 changeEventQueueDone: make(chan bool, 1),
77 core: core,
Stephane Barbarie6e1bd502018-11-05 22:44:45 -050078 }
khenaidoobf6e7bb2018-08-14 22:27:29 -040079 return handler
80}
khenaidoo4d4802d2018-10-04 21:59:49 -040081
khenaidoo09771ef2019-10-11 14:25:02 -040082// waitForNilResponseOnSuccess is a helper function to wait for a response on channel monitorCh where an nil
khenaidoo4d4802d2018-10-04 21:59:49 -040083// response is expected in a successful scenario
84func waitForNilResponseOnSuccess(ctx context.Context, ch chan interface{}) (*empty.Empty, error) {
85 select {
86 case res := <-ch:
87 if res == nil {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -050088 return &empty.Empty{}, nil
khenaidoo4d4802d2018-10-04 21:59:49 -040089 } else if err, ok := res.(error); ok {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -050090 return &empty.Empty{}, err
khenaidoo4d4802d2018-10-04 21:59:49 -040091 } else {
Girish Kumarf56a4682020-03-20 20:07:46 +000092 logger.Warnw("unexpected-return-type", log.Fields{"result": res})
khenaidoo4d4802d2018-10-04 21:59:49 -040093 err = status.Errorf(codes.Internal, "%s", res)
Kent Hagermanc2c73ff2019-11-20 16:22:32 -050094 return &empty.Empty{}, err
khenaidoo4d4802d2018-10-04 21:59:49 -040095 }
96 case <-ctx.Done():
Girish Kumarf56a4682020-03-20 20:07:46 +000097 logger.Debug("client-timeout")
khenaidoo4d4802d2018-10-04 21:59:49 -040098 return nil, ctx.Err()
99 }
100}
101
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500102// ListCoreInstances returns details on the running core containers
103func (handler *APIHandler) ListCoreInstances(ctx context.Context, empty *empty.Empty) (*voltha.CoreInstances, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000104 logger.Debug("ListCoreInstances")
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500105 // TODO: unused stub
106 return &voltha.CoreInstances{}, status.Errorf(codes.NotFound, "no-core-instances")
107}
108
109// GetCoreInstance returns the details of a specific core container
110func (handler *APIHandler) GetCoreInstance(ctx context.Context, id *voltha.ID) (*voltha.CoreInstance, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000111 logger.Debugw("GetCoreInstance", log.Fields{"id": id})
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500112 //TODO: unused stub
113 return &voltha.CoreInstance{}, status.Errorf(codes.NotFound, "core-instance-%s", id.Id)
114}
115
npujar1d86a522019-11-14 17:11:16 +0530116// GetLogicalDevicePort returns logical device port details
khenaidoo43aa6bd2019-05-29 13:35:13 -0400117func (handler *APIHandler) GetLogicalDevicePort(ctx context.Context, id *voltha.LogicalPortId) (*voltha.LogicalPort, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000118 logger.Debugw("GetLogicalDevicePort-request", log.Fields{"id": *id})
khenaidoo43aa6bd2019-05-29 13:35:13 -0400119
npujar467fe752020-01-16 20:17:45 +0530120 return handler.logicalDeviceMgr.getLogicalPort(ctx, id)
khenaidoo43aa6bd2019-05-29 13:35:13 -0400121}
122
npujar1d86a522019-11-14 17:11:16 +0530123// EnableLogicalDevicePort enables logical device port
khenaidoobf6e7bb2018-08-14 22:27:29 -0400124func (handler *APIHandler) EnableLogicalDevicePort(ctx context.Context, id *voltha.LogicalPortId) (*empty.Empty, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000125 logger.Debugw("EnableLogicalDevicePort-request", log.Fields{"id": id, "test": common.TestModeKeys_api_test.String()})
khenaidoo9cdc1a62019-01-24 21:57:40 -0500126
khenaidoo4d4802d2018-10-04 21:59:49 -0400127 ch := make(chan interface{})
128 defer close(ch)
khenaidoo19d7b632018-10-30 10:49:50 -0400129 go handler.logicalDeviceMgr.enableLogicalPort(ctx, id, ch)
khenaidoo4d4802d2018-10-04 21:59:49 -0400130 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400131}
132
npujar1d86a522019-11-14 17:11:16 +0530133// DisableLogicalDevicePort disables logical device port
khenaidoobf6e7bb2018-08-14 22:27:29 -0400134func (handler *APIHandler) DisableLogicalDevicePort(ctx context.Context, id *voltha.LogicalPortId) (*empty.Empty, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000135 logger.Debugw("DisableLogicalDevicePort-request", log.Fields{"id": id, "test": common.TestModeKeys_api_test.String()})
khenaidoo9cdc1a62019-01-24 21:57:40 -0500136
khenaidoo19d7b632018-10-30 10:49:50 -0400137 ch := make(chan interface{})
138 defer close(ch)
139 go handler.logicalDeviceMgr.disableLogicalPort(ctx, id, ch)
140 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400141}
142
npujar1d86a522019-11-14 17:11:16 +0530143// UpdateLogicalDeviceFlowTable updates logical device flow table
khenaidoobf6e7bb2018-08-14 22:27:29 -0400144func (handler *APIHandler) UpdateLogicalDeviceFlowTable(ctx context.Context, flow *openflow_13.FlowTableUpdate) (*empty.Empty, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000145 logger.Debugw("UpdateLogicalDeviceFlowTable-request", log.Fields{"flow": flow, "test": common.TestModeKeys_api_test.String()})
khenaidoo9cdc1a62019-01-24 21:57:40 -0500146
khenaidoo19d7b632018-10-30 10:49:50 -0400147 ch := make(chan interface{})
148 defer close(ch)
149 go handler.logicalDeviceMgr.updateFlowTable(ctx, flow.Id, flow.FlowMod, ch)
150 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400151}
152
npujar1d86a522019-11-14 17:11:16 +0530153// UpdateLogicalDeviceFlowGroupTable updates logical device flow group table
khenaidoobf6e7bb2018-08-14 22:27:29 -0400154func (handler *APIHandler) UpdateLogicalDeviceFlowGroupTable(ctx context.Context, flow *openflow_13.FlowGroupTableUpdate) (*empty.Empty, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000155 logger.Debugw("UpdateLogicalDeviceFlowGroupTable-request", log.Fields{"flow": flow, "test": common.TestModeKeys_api_test.String()})
khenaidoo19d7b632018-10-30 10:49:50 -0400156 ch := make(chan interface{})
157 defer close(ch)
158 go handler.logicalDeviceMgr.updateGroupTable(ctx, flow.Id, flow.GroupMod, ch)
159 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400160}
161
khenaidoob9203542018-09-17 22:56:37 -0400162// GetDevice must be implemented in the read-only containers - should it also be implemented here?
163func (handler *APIHandler) GetDevice(ctx context.Context, id *voltha.ID) (*voltha.Device, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000164 logger.Debugw("GetDevice-request", log.Fields{"id": id})
npujar467fe752020-01-16 20:17:45 +0530165 return handler.deviceMgr.GetDevice(ctx, id.Id)
khenaidoob9203542018-09-17 22:56:37 -0400166}
167
168// GetDevice must be implemented in the read-only containers - should it also be implemented here?
npujar1d86a522019-11-14 17:11:16 +0530169
170// ListDevices retrieves the latest devices from the data model
khenaidoob9203542018-09-17 22:56:37 -0400171func (handler *APIHandler) ListDevices(ctx context.Context, empty *empty.Empty) (*voltha.Devices, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000172 logger.Debug("ListDevices")
npujar467fe752020-01-16 20:17:45 +0530173 devices, err := handler.deviceMgr.ListDevices(ctx)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530174 if err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000175 logger.Errorw("Failed to list devices", log.Fields{"error": err})
Thomas Lee Se5a44012019-11-07 20:32:24 +0530176 return nil, err
177 }
178 return devices, nil
khenaidoob9203542018-09-17 22:56:37 -0400179}
180
khenaidoo7ccedd52018-12-14 16:48:54 -0500181// ListDeviceIds returns the list of device ids managed by a voltha core
182func (handler *APIHandler) ListDeviceIds(ctx context.Context, empty *empty.Empty) (*voltha.IDs, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000183 logger.Debug("ListDeviceIDs")
khenaidoo7ccedd52018-12-14 16:48:54 -0500184 return handler.deviceMgr.ListDeviceIds()
185}
186
187//ReconcileDevices is a request to a voltha core to managed a list of devices based on their IDs
188func (handler *APIHandler) ReconcileDevices(ctx context.Context, ids *voltha.IDs) (*empty.Empty, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000189 logger.Debug("ReconcileDevices")
khenaidoo9cdc1a62019-01-24 21:57:40 -0500190
khenaidoo7ccedd52018-12-14 16:48:54 -0500191 ch := make(chan interface{})
192 defer close(ch)
193 go handler.deviceMgr.ReconcileDevices(ctx, ids, ch)
194 return waitForNilResponseOnSuccess(ctx, ch)
195}
196
npujar1d86a522019-11-14 17:11:16 +0530197// GetLogicalDevice provides a cloned most up to date logical device
khenaidoob9203542018-09-17 22:56:37 -0400198func (handler *APIHandler) GetLogicalDevice(ctx context.Context, id *voltha.ID) (*voltha.LogicalDevice, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000199 logger.Debugw("GetLogicalDevice-request", log.Fields{"id": id})
npujar467fe752020-01-16 20:17:45 +0530200 return handler.logicalDeviceMgr.getLogicalDevice(ctx, id.Id)
khenaidoob9203542018-09-17 22:56:37 -0400201}
202
npujar1d86a522019-11-14 17:11:16 +0530203// ListLogicalDevices returns the list of all logical devices
khenaidoob9203542018-09-17 22:56:37 -0400204func (handler *APIHandler) ListLogicalDevices(ctx context.Context, empty *empty.Empty) (*voltha.LogicalDevices, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000205 logger.Debug("ListLogicalDevices-request")
npujar467fe752020-01-16 20:17:45 +0530206 return handler.logicalDeviceMgr.listLogicalDevices(ctx)
khenaidoob9203542018-09-17 22:56:37 -0400207}
208
khenaidoo21d51152019-02-01 13:48:37 -0500209// ListAdapters returns the contents of all adapters known to the system
210func (handler *APIHandler) ListAdapters(ctx context.Context, empty *empty.Empty) (*voltha.Adapters, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000211 logger.Debug("ListAdapters")
khenaidoo21d51152019-02-01 13:48:37 -0500212 return handler.adapterMgr.listAdapters(ctx)
213}
214
npujar1d86a522019-11-14 17:11:16 +0530215// ListLogicalDeviceFlows returns the flows of logical device
khenaidoodd237172019-05-27 16:37:17 -0400216func (handler *APIHandler) ListLogicalDeviceFlows(ctx context.Context, id *voltha.ID) (*openflow_13.Flows, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000217 logger.Debugw("ListLogicalDeviceFlows", log.Fields{"id": *id})
khenaidoodd237172019-05-27 16:37:17 -0400218 return handler.logicalDeviceMgr.ListLogicalDeviceFlows(ctx, id.Id)
219}
220
npujar1d86a522019-11-14 17:11:16 +0530221// ListLogicalDeviceFlowGroups returns logical device flow groups
khenaidoodd237172019-05-27 16:37:17 -0400222func (handler *APIHandler) ListLogicalDeviceFlowGroups(ctx context.Context, id *voltha.ID) (*openflow_13.FlowGroups, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000223 logger.Debugw("ListLogicalDeviceFlowGroups", log.Fields{"id": *id})
khenaidoodd237172019-05-27 16:37:17 -0400224 return handler.logicalDeviceMgr.ListLogicalDeviceFlowGroups(ctx, id.Id)
225}
226
npujar1d86a522019-11-14 17:11:16 +0530227// ListLogicalDevicePorts returns ports of logical device
khenaidoo19d7b632018-10-30 10:49:50 -0400228func (handler *APIHandler) ListLogicalDevicePorts(ctx context.Context, id *voltha.ID) (*voltha.LogicalPorts, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000229 logger.Debugw("ListLogicalDevicePorts", log.Fields{"logicaldeviceid": id})
khenaidoo19d7b632018-10-30 10:49:50 -0400230 return handler.logicalDeviceMgr.ListLogicalDevicePorts(ctx, id.Id)
231}
232
khenaidoo4d4802d2018-10-04 21:59:49 -0400233// CreateDevice creates a new parent device in the data model
khenaidoobf6e7bb2018-08-14 22:27:29 -0400234func (handler *APIHandler) CreateDevice(ctx context.Context, device *voltha.Device) (*voltha.Device, error) {
Thomas Lee S51b5cb82019-10-14 14:49:34 +0530235 if device.MacAddress == "" && device.GetHostAndPort() == "" {
Girish Kumarf56a4682020-03-20 20:07:46 +0000236 logger.Errorf("No Device Info Present")
Thomas Lee Se5a44012019-11-07 20:32:24 +0530237 return &voltha.Device{}, errors.New("no-device-info-present; MAC or HOSTIP&PORT")
Thomas Lee S51b5cb82019-10-14 14:49:34 +0530238 }
Girish Kumarf56a4682020-03-20 20:07:46 +0000239 logger.Debugw("create-device", log.Fields{"device": *device})
khenaidoo9cdc1a62019-01-24 21:57:40 -0500240
khenaidoob9203542018-09-17 22:56:37 -0400241 ch := make(chan interface{})
242 defer close(ch)
243 go handler.deviceMgr.createDevice(ctx, device, ch)
244 select {
245 case res := <-ch:
khenaidoo92e62c52018-10-03 14:02:54 -0400246 if res != nil {
247 if err, ok := res.(error); ok {
Girish Kumarf56a4682020-03-20 20:07:46 +0000248 logger.Errorw("create-device-failed", log.Fields{"error": err})
Thomas Lee Se5a44012019-11-07 20:32:24 +0530249 return nil, err
khenaidoo92e62c52018-10-03 14:02:54 -0400250 }
251 if d, ok := res.(*voltha.Device); ok {
252 return d, nil
253 }
khenaidoob9203542018-09-17 22:56:37 -0400254 }
Girish Kumarf56a4682020-03-20 20:07:46 +0000255 logger.Warnw("create-device-unexpected-return-type", log.Fields{"result": res})
khenaidoo92e62c52018-10-03 14:02:54 -0400256 err := status.Errorf(codes.Internal, "%s", res)
257 return &voltha.Device{}, err
khenaidoob9203542018-09-17 22:56:37 -0400258 case <-ctx.Done():
Girish Kumarf56a4682020-03-20 20:07:46 +0000259 logger.Debug("createdevice-client-timeout")
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500260 return &voltha.Device{}, ctx.Err()
khenaidoob9203542018-09-17 22:56:37 -0400261 }
khenaidoobf6e7bb2018-08-14 22:27:29 -0400262}
263
khenaidoo4d4802d2018-10-04 21:59:49 -0400264// EnableDevice activates a device by invoking the adopt_device API on the appropriate adapter
khenaidoobf6e7bb2018-08-14 22:27:29 -0400265func (handler *APIHandler) EnableDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000266 logger.Debugw("enabledevice", log.Fields{"id": id})
khenaidoo9cdc1a62019-01-24 21:57:40 -0500267
khenaidoob9203542018-09-17 22:56:37 -0400268 ch := make(chan interface{})
269 defer close(ch)
270 go handler.deviceMgr.enableDevice(ctx, id, ch)
khenaidoo4d4802d2018-10-04 21:59:49 -0400271 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400272}
273
khenaidoo4d4802d2018-10-04 21:59:49 -0400274// DisableDevice disables a device along with any child device it may have
khenaidoobf6e7bb2018-08-14 22:27:29 -0400275func (handler *APIHandler) DisableDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000276 logger.Debugw("disabledevice-request", log.Fields{"id": id})
khenaidoo9cdc1a62019-01-24 21:57:40 -0500277
khenaidoo92e62c52018-10-03 14:02:54 -0400278 ch := make(chan interface{})
279 defer close(ch)
280 go handler.deviceMgr.disableDevice(ctx, id, ch)
khenaidoo4d4802d2018-10-04 21:59:49 -0400281 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400282}
283
khenaidoo4d4802d2018-10-04 21:59:49 -0400284//RebootDevice invoked the reboot API to the corresponding adapter
khenaidoobf6e7bb2018-08-14 22:27:29 -0400285func (handler *APIHandler) RebootDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000286 logger.Debugw("rebootDevice-request", log.Fields{"id": id})
khenaidoo9cdc1a62019-01-24 21:57:40 -0500287
khenaidoo4d4802d2018-10-04 21:59:49 -0400288 ch := make(chan interface{})
289 defer close(ch)
290 go handler.deviceMgr.rebootDevice(ctx, id, ch)
291 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400292}
293
khenaidoo4d4802d2018-10-04 21:59:49 -0400294// DeleteDevice removes a device from the data model
khenaidoobf6e7bb2018-08-14 22:27:29 -0400295func (handler *APIHandler) DeleteDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000296 logger.Debugw("deletedevice-request", log.Fields{"id": id})
khenaidoo9cdc1a62019-01-24 21:57:40 -0500297
khenaidoo4d4802d2018-10-04 21:59:49 -0400298 ch := make(chan interface{})
299 defer close(ch)
300 go handler.deviceMgr.deleteDevice(ctx, id, ch)
301 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400302}
303
David Bainbridge4087cc52019-11-13 18:36:03 +0000304// ListDevicePorts returns the ports details for a specific device entry
305func (handler *APIHandler) ListDevicePorts(ctx context.Context, id *voltha.ID) (*voltha.Ports, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000306 logger.Debugw("listdeviceports-request", log.Fields{"id": id})
npujar467fe752020-01-16 20:17:45 +0530307 device, err := handler.deviceMgr.GetDevice(ctx, id.Id)
David Bainbridge4087cc52019-11-13 18:36:03 +0000308 if err != nil {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500309 return &voltha.Ports{}, err
David Bainbridge4087cc52019-11-13 18:36:03 +0000310 }
311 ports := &voltha.Ports{}
npujar1d86a522019-11-14 17:11:16 +0530312 ports.Items = append(ports.Items, device.Ports...)
David Bainbridge4087cc52019-11-13 18:36:03 +0000313 return ports, nil
314}
315
316// ListDeviceFlows returns the flow details for a specific device entry
317func (handler *APIHandler) ListDeviceFlows(ctx context.Context, id *voltha.ID) (*openflow_13.Flows, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000318 logger.Debugw("listdeviceflows-request", log.Fields{"id": id})
David Bainbridge4087cc52019-11-13 18:36:03 +0000319
npujar467fe752020-01-16 20:17:45 +0530320 device, err := handler.deviceMgr.GetDevice(ctx, id.Id)
David Bainbridge4087cc52019-11-13 18:36:03 +0000321 if err != nil {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500322 return &openflow_13.Flows{}, err
David Bainbridge4087cc52019-11-13 18:36:03 +0000323 }
324 flows := &openflow_13.Flows{}
npujar1d86a522019-11-14 17:11:16 +0530325 flows.Items = append(flows.Items, device.Flows.Items...)
David Bainbridge4087cc52019-11-13 18:36:03 +0000326 return flows, nil
327}
328
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500329// ListDeviceFlowGroups returns the flow group details for a specific device entry
330func (handler *APIHandler) ListDeviceFlowGroups(ctx context.Context, id *voltha.ID) (*voltha.FlowGroups, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000331 logger.Debugw("ListDeviceFlowGroups", log.Fields{"deviceid": id})
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500332
npujar467fe752020-01-16 20:17:45 +0530333 if device, _ := handler.deviceMgr.GetDevice(ctx, id.Id); device != nil {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500334 return device.GetFlowGroups(), nil
335 }
336 return &voltha.FlowGroups{}, status.Errorf(codes.NotFound, "device-%s", id.Id)
337}
338
339// ListDeviceGroups returns all the device groups known to the system
340func (handler *APIHandler) ListDeviceGroups(ctx context.Context, empty *empty.Empty) (*voltha.DeviceGroups, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000341 logger.Debug("ListDeviceGroups")
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500342 return &voltha.DeviceGroups{}, errors.New("UnImplemented")
343}
344
345// GetDeviceGroup returns a specific device group entry
346func (handler *APIHandler) GetDeviceGroup(ctx context.Context, id *voltha.ID) (*voltha.DeviceGroup, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000347 logger.Debug("GetDeviceGroup")
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500348 return &voltha.DeviceGroup{}, errors.New("UnImplemented")
349}
350
351// ListDeviceTypes returns all the device types known to the system
352func (handler *APIHandler) ListDeviceTypes(ctx context.Context, _ *empty.Empty) (*voltha.DeviceTypes, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000353 logger.Debug("ListDeviceTypes")
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500354
355 return &voltha.DeviceTypes{Items: handler.adapterMgr.listDeviceTypes()}, nil
356}
357
358// GetDeviceType returns the device type for a specific device entry
359func (handler *APIHandler) GetDeviceType(ctx context.Context, id *voltha.ID) (*voltha.DeviceType, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000360 logger.Debugw("GetDeviceType", log.Fields{"typeid": id})
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500361
362 if deviceType := handler.adapterMgr.getDeviceType(id.Id); deviceType != nil {
363 return deviceType, nil
364 }
365 return &voltha.DeviceType{}, status.Errorf(codes.NotFound, "device_type-%s", id.Id)
366}
367
David Bainbridge4087cc52019-11-13 18:36:03 +0000368// GetVoltha returns the contents of all components (i.e. devices, logical_devices, ...)
369func (handler *APIHandler) GetVoltha(ctx context.Context, empty *empty.Empty) (*voltha.Voltha, error) {
370
Girish Kumarf56a4682020-03-20 20:07:46 +0000371 logger.Debug("GetVoltha")
David Bainbridge4087cc52019-11-13 18:36:03 +0000372 /*
373 * For now, encode all the version information into a JSON object and
374 * pass that back as "version" so the client can get all the
375 * information associated with the version. Long term the API should
376 * better accomidate this, but for now this will work.
377 */
378 data, err := json.Marshal(&version.VersionInfo)
379 info := version.VersionInfo.Version
380 if err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000381 logger.Warnf("Unable to encode version information as JSON: %s", err.Error())
David Bainbridge4087cc52019-11-13 18:36:03 +0000382 } else {
383 info = string(data)
384 }
385
386 return &voltha.Voltha{
387 Version: info,
388 }, nil
389}
390
khenaidoof5a5bfa2019-01-23 22:20:29 -0500391// processImageRequest is a helper method to execute an image download request
392func (handler *APIHandler) processImageRequest(ctx context.Context, img *voltha.ImageDownload, requestType int) (*common.OperationResp, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000393 logger.Debugw("processImageDownload", log.Fields{"img": *img, "requestType": requestType})
khenaidoof5a5bfa2019-01-23 22:20:29 -0500394
khenaidoo2c6a0992019-04-29 13:46:56 -0400395 failedresponse := &common.OperationResp{Code: voltha.OperationResp_OPERATION_FAILURE}
khenaidoof5a5bfa2019-01-23 22:20:29 -0500396
397 ch := make(chan interface{})
398 defer close(ch)
399 switch requestType {
npujar1d86a522019-11-14 17:11:16 +0530400 case ImageDownload:
khenaidoof5a5bfa2019-01-23 22:20:29 -0500401 go handler.deviceMgr.downloadImage(ctx, img, ch)
npujar1d86a522019-11-14 17:11:16 +0530402 case CancelImageDownload:
khenaidoof5a5bfa2019-01-23 22:20:29 -0500403 go handler.deviceMgr.cancelImageDownload(ctx, img, ch)
npujar1d86a522019-11-14 17:11:16 +0530404 case ActivateImage:
khenaidoof5a5bfa2019-01-23 22:20:29 -0500405 go handler.deviceMgr.activateImage(ctx, img, ch)
npujar1d86a522019-11-14 17:11:16 +0530406 case RevertImage:
khenaidoof5a5bfa2019-01-23 22:20:29 -0500407 go handler.deviceMgr.revertImage(ctx, img, ch)
408 default:
Girish Kumarf56a4682020-03-20 20:07:46 +0000409 logger.Warn("invalid-request-type", log.Fields{"requestType": requestType})
khenaidoof5a5bfa2019-01-23 22:20:29 -0500410 return failedresponse, status.Errorf(codes.InvalidArgument, "%d", requestType)
411 }
412 select {
413 case res := <-ch:
414 if res != nil {
415 if err, ok := res.(error); ok {
416 return failedresponse, err
417 }
418 if opResp, ok := res.(*common.OperationResp); ok {
419 return opResp, nil
420 }
421 }
Girish Kumarf56a4682020-03-20 20:07:46 +0000422 logger.Warnw("download-image-unexpected-return-type", log.Fields{"result": res})
khenaidoof5a5bfa2019-01-23 22:20:29 -0500423 return failedresponse, status.Errorf(codes.Internal, "%s", res)
424 case <-ctx.Done():
Girish Kumarf56a4682020-03-20 20:07:46 +0000425 logger.Debug("downloadImage-client-timeout")
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500426 return &common.OperationResp{}, ctx.Err()
khenaidoof5a5bfa2019-01-23 22:20:29 -0500427 }
428}
429
npujar1d86a522019-11-14 17:11:16 +0530430// DownloadImage execute an image download request
khenaidoobf6e7bb2018-08-14 22:27:29 -0400431func (handler *APIHandler) DownloadImage(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000432 logger.Debugw("DownloadImage-request", log.Fields{"img": *img})
khenaidoobf6e7bb2018-08-14 22:27:29 -0400433
npujar1d86a522019-11-14 17:11:16 +0530434 return handler.processImageRequest(ctx, img, ImageDownload)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400435}
436
npujar1d86a522019-11-14 17:11:16 +0530437// CancelImageDownload cancels image download request
khenaidoobf6e7bb2018-08-14 22:27:29 -0400438func (handler *APIHandler) CancelImageDownload(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000439 logger.Debugw("cancelImageDownload-request", log.Fields{"img": *img})
npujar1d86a522019-11-14 17:11:16 +0530440 return handler.processImageRequest(ctx, img, CancelImageDownload)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400441}
442
npujar1d86a522019-11-14 17:11:16 +0530443// ActivateImageUpdate activates image update request
khenaidoobf6e7bb2018-08-14 22:27:29 -0400444func (handler *APIHandler) ActivateImageUpdate(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000445 logger.Debugw("activateImageUpdate-request", log.Fields{"img": *img})
npujar1d86a522019-11-14 17:11:16 +0530446 return handler.processImageRequest(ctx, img, ActivateImage)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400447}
448
npujar1d86a522019-11-14 17:11:16 +0530449// RevertImageUpdate reverts image update
khenaidoobf6e7bb2018-08-14 22:27:29 -0400450func (handler *APIHandler) RevertImageUpdate(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000451 logger.Debugw("revertImageUpdate-request", log.Fields{"img": *img})
npujar1d86a522019-11-14 17:11:16 +0530452 return handler.processImageRequest(ctx, img, RevertImage)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400453}
454
npujar1d86a522019-11-14 17:11:16 +0530455// GetImageDownloadStatus returns status of image download
khenaidoof5a5bfa2019-01-23 22:20:29 -0500456func (handler *APIHandler) GetImageDownloadStatus(ctx context.Context, img *voltha.ImageDownload) (*voltha.ImageDownload, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000457 logger.Debugw("getImageDownloadStatus-request", log.Fields{"img": *img})
khenaidoof5a5bfa2019-01-23 22:20:29 -0500458
Stephane Barbariedf5479f2019-01-29 22:13:00 -0500459 failedresponse := &voltha.ImageDownload{DownloadState: voltha.ImageDownload_DOWNLOAD_UNKNOWN}
khenaidoof5a5bfa2019-01-23 22:20:29 -0500460
khenaidoof5a5bfa2019-01-23 22:20:29 -0500461 ch := make(chan interface{})
462 defer close(ch)
463 go handler.deviceMgr.getImageDownloadStatus(ctx, img, ch)
464
465 select {
466 case res := <-ch:
467 if res != nil {
468 if err, ok := res.(error); ok {
469 return failedresponse, err
470 }
471 if downloadResp, ok := res.(*voltha.ImageDownload); ok {
472 return downloadResp, nil
473 }
474 }
Girish Kumarf56a4682020-03-20 20:07:46 +0000475 logger.Warnw("download-image-status", log.Fields{"result": res})
khenaidoof5a5bfa2019-01-23 22:20:29 -0500476 return failedresponse, status.Errorf(codes.Internal, "%s", res)
477 case <-ctx.Done():
Girish Kumarf56a4682020-03-20 20:07:46 +0000478 logger.Debug("downloadImage-client-timeout")
khenaidoof5a5bfa2019-01-23 22:20:29 -0500479 return failedresponse, ctx.Err()
480 }
481}
482
npujar1d86a522019-11-14 17:11:16 +0530483// GetImageDownload returns image download
khenaidoof5a5bfa2019-01-23 22:20:29 -0500484func (handler *APIHandler) GetImageDownload(ctx context.Context, img *voltha.ImageDownload) (*voltha.ImageDownload, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000485 logger.Debugw("GetImageDownload-request", log.Fields{"img": *img})
khenaidoof5a5bfa2019-01-23 22:20:29 -0500486
npujar1d86a522019-11-14 17:11:16 +0530487 download, err := handler.deviceMgr.getImageDownload(ctx, img)
488 if err != nil {
Stephane Barbariedf5479f2019-01-29 22:13:00 -0500489 return &voltha.ImageDownload{DownloadState: voltha.ImageDownload_DOWNLOAD_UNKNOWN}, err
khenaidoof5a5bfa2019-01-23 22:20:29 -0500490 }
npujar1d86a522019-11-14 17:11:16 +0530491 return download, nil
khenaidoof5a5bfa2019-01-23 22:20:29 -0500492}
493
npujar1d86a522019-11-14 17:11:16 +0530494// ListImageDownloads returns image downloads
khenaidoof5a5bfa2019-01-23 22:20:29 -0500495func (handler *APIHandler) ListImageDownloads(ctx context.Context, id *voltha.ID) (*voltha.ImageDownloads, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000496 logger.Debugw("ListImageDownloads-request", log.Fields{"deviceId": id.Id})
khenaidoof5a5bfa2019-01-23 22:20:29 -0500497
npujar1d86a522019-11-14 17:11:16 +0530498 downloads, err := handler.deviceMgr.listImageDownloads(ctx, id.Id)
499 if err != nil {
khenaidoof5a5bfa2019-01-23 22:20:29 -0500500 failedResp := &voltha.ImageDownloads{
khenaidoo2c6a0992019-04-29 13:46:56 -0400501 Items: []*voltha.ImageDownload{
502 {DownloadState: voltha.ImageDownload_DOWNLOAD_UNKNOWN},
503 },
khenaidoof5a5bfa2019-01-23 22:20:29 -0500504 }
505 return failedResp, err
khenaidoof5a5bfa2019-01-23 22:20:29 -0500506 }
npujar1d86a522019-11-14 17:11:16 +0530507 return downloads, nil
khenaidoof5a5bfa2019-01-23 22:20:29 -0500508}
509
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500510// GetImages returns all images for a specific device entry
511func (handler *APIHandler) GetImages(ctx context.Context, id *voltha.ID) (*voltha.Images, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000512 logger.Debugw("GetImages", log.Fields{"deviceid": id.Id})
npujar467fe752020-01-16 20:17:45 +0530513 device, err := handler.deviceMgr.GetDevice(ctx, id.Id)
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500514 if err != nil {
515 return &voltha.Images{}, err
516 }
517 return device.GetImages(), nil
518}
519
npujar1d86a522019-11-14 17:11:16 +0530520// UpdateDevicePmConfigs updates the PM configs
khenaidoobf6e7bb2018-08-14 22:27:29 -0400521func (handler *APIHandler) UpdateDevicePmConfigs(ctx context.Context, configs *voltha.PmConfigs) (*empty.Empty, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000522 logger.Debugw("UpdateDevicePmConfigs-request", log.Fields{"configs": *configs})
khenaidoob3127472019-07-24 21:04:55 -0400523
524 ch := make(chan interface{})
525 defer close(ch)
526 go handler.deviceMgr.updatePmConfigs(ctx, configs, ch)
527 return waitForNilResponseOnSuccess(ctx, ch)
528}
529
npujar1d86a522019-11-14 17:11:16 +0530530// ListDevicePmConfigs returns pm configs of device
khenaidoob3127472019-07-24 21:04:55 -0400531func (handler *APIHandler) ListDevicePmConfigs(ctx context.Context, id *voltha.ID) (*voltha.PmConfigs, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000532 logger.Debugw("ListDevicePmConfigs-request", log.Fields{"deviceId": *id})
khenaidoob3127472019-07-24 21:04:55 -0400533 return handler.deviceMgr.listPmConfigs(ctx, id.Id)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400534}
535
Devmalya Paulc594bb32019-11-06 07:34:27 +0000536func (handler *APIHandler) CreateEventFilter(ctx context.Context, filter *voltha.EventFilter) (*voltha.EventFilter, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000537 logger.Debugw("CreateEventFilter-request", log.Fields{"filter": *filter})
Devmalya Paulc594bb32019-11-06 07:34:27 +0000538 return nil, errors.New("UnImplemented")
khenaidoobf6e7bb2018-08-14 22:27:29 -0400539}
540
Devmalya Paulc594bb32019-11-06 07:34:27 +0000541func (handler *APIHandler) UpdateEventFilter(ctx context.Context, filter *voltha.EventFilter) (*voltha.EventFilter, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000542 logger.Debugw("UpdateEventFilter-request", log.Fields{"filter": *filter})
Devmalya Paulc594bb32019-11-06 07:34:27 +0000543 return nil, errors.New("UnImplemented")
khenaidoobf6e7bb2018-08-14 22:27:29 -0400544}
545
Devmalya Paulc594bb32019-11-06 07:34:27 +0000546func (handler *APIHandler) DeleteEventFilter(ctx context.Context, filterInfo *voltha.EventFilter) (*empty.Empty, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000547 logger.Debugw("DeleteEventFilter-request", log.Fields{"device-id": filterInfo.DeviceId, "filter-id": filterInfo.Id})
Devmalya Paulc594bb32019-11-06 07:34:27 +0000548 return nil, errors.New("UnImplemented")
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500549}
550
Devmalya Paulc594bb32019-11-06 07:34:27 +0000551// GetEventFilter returns all the filters present for a device
552func (handler *APIHandler) GetEventFilter(ctx context.Context, id *voltha.ID) (*voltha.EventFilters, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000553 logger.Debugw("GetEventFilter-request", log.Fields{"device-id": id})
Devmalya Paulc594bb32019-11-06 07:34:27 +0000554 return nil, errors.New("UnImplemented")
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500555}
556
Devmalya Paulc594bb32019-11-06 07:34:27 +0000557// ListEventFilters returns all the filters known to the system
558func (handler *APIHandler) ListEventFilters(ctx context.Context, empty *empty.Empty) (*voltha.EventFilters, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000559 logger.Debug("ListEventFilter-request")
Devmalya Paulc594bb32019-11-06 07:34:27 +0000560 return nil, errors.New("UnImplemented")
khenaidoobf6e7bb2018-08-14 22:27:29 -0400561}
562
563func (handler *APIHandler) SelfTest(ctx context.Context, id *voltha.ID) (*voltha.SelfTestResponse, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000564 logger.Debugw("SelfTest-request", log.Fields{"id": id})
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500565 return &voltha.SelfTestResponse{}, errors.New("UnImplemented")
khenaidoobf6e7bb2018-08-14 22:27:29 -0400566}
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500567
npujar467fe752020-01-16 20:17:45 +0530568func (handler *APIHandler) forwardPacketOut(ctx context.Context, packet *openflow_13.PacketOut) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000569 logger.Debugw("forwardPacketOut-request", log.Fields{"packet": packet})
khenaidoo3d3b8c22019-05-22 18:10:39 -0400570 //TODO: Update this logic once the OF Controller (OFAgent in this case) can include a transaction Id in its
571 // request. For performance reason we can let both Cores in a Core-Pair forward the Packet to the adapters and
572 // let once of the shim layer (kafka proxy or adapter request handler filters out the duplicate packet)
David Bainbridged1afd662020-03-26 18:27:41 -0700573 if agent := handler.logicalDeviceMgr.getLogicalDeviceAgent(ctx, packet.Id); agent != nil {
574 agent.packetOut(ctx, packet.PacketOut)
575 } else {
576 logger.Errorf("No logical device agent present", log.Fields{"logicaldeviceID": packet.Id})
khenaidoo3d3b8c22019-05-22 18:10:39 -0400577 }
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500578}
khenaidoo3d3b8c22019-05-22 18:10:39 -0400579
npujar1d86a522019-11-14 17:11:16 +0530580// StreamPacketsOut sends packets to adapter
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500581func (handler *APIHandler) StreamPacketsOut(packets voltha.VolthaService_StreamPacketsOutServer) error {
Girish Kumarf56a4682020-03-20 20:07:46 +0000582 logger.Debugw("StreamPacketsOut-request", log.Fields{"packets": packets})
khenaidoo5e250692019-08-30 14:46:21 -0400583loop:
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500584 for {
khenaidoo5e250692019-08-30 14:46:21 -0400585 select {
586 case <-packets.Context().Done():
Girish Kumarf56a4682020-03-20 20:07:46 +0000587 logger.Infow("StreamPacketsOut-context-done", log.Fields{"packets": packets, "error": packets.Context().Err()})
khenaidoo5e250692019-08-30 14:46:21 -0400588 break loop
589 default:
590 }
591
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500592 packet, err := packets.Recv()
593
594 if err == io.EOF {
Girish Kumarf56a4682020-03-20 20:07:46 +0000595 logger.Debugw("Received-EOF", log.Fields{"packets": packets})
khenaidoo5e250692019-08-30 14:46:21 -0400596 break loop
597 }
598
599 if err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000600 logger.Errorw("Failed to receive packet out", log.Fields{"error": err})
khenaidoo5e250692019-08-30 14:46:21 -0400601 continue
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500602 }
603
npujar467fe752020-01-16 20:17:45 +0530604 handler.forwardPacketOut(packets.Context(), packet)
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500605 }
606
Girish Kumarf56a4682020-03-20 20:07:46 +0000607 logger.Debugw("StreamPacketsOut-request-done", log.Fields{"packets": packets})
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500608 return nil
609}
610
npujar1d86a522019-11-14 17:11:16 +0530611func (handler *APIHandler) sendPacketIn(deviceID string, transationID string, packet *openflow_13.OfpPacketIn) {
khenaidoo297cd252019-02-07 22:10:23 -0500612 // TODO: Augment the OF PacketIn to include the transactionId
npujar1d86a522019-11-14 17:11:16 +0530613 packetIn := openflow_13.PacketIn{Id: deviceID, PacketIn: packet}
Girish Kumarf56a4682020-03-20 20:07:46 +0000614 logger.Debugw("sendPacketIn", log.Fields{"packetIn": packetIn})
A R Karthick881e7ea2019-08-19 19:44:02 +0000615 handler.packetInQueue <- packetIn
616}
617
618type callTracker struct {
619 failedPacket interface{}
620}
621type streamTracker struct {
622 calls map[string]*callTracker
623 sync.Mutex
624}
625
626var streamingTracker = &streamTracker{calls: make(map[string]*callTracker)}
627
628func (handler *APIHandler) getStreamingTracker(method string, done chan<- bool) *callTracker {
629 streamingTracker.Lock()
630 defer streamingTracker.Unlock()
631 if _, ok := streamingTracker.calls[method]; ok {
632 // bail out the other packet in thread
Girish Kumarf56a4682020-03-20 20:07:46 +0000633 logger.Debugf("%s streaming call already running. Exiting it", method)
A R Karthick881e7ea2019-08-19 19:44:02 +0000634 done <- true
Girish Kumarf56a4682020-03-20 20:07:46 +0000635 logger.Debugf("Last %s exited. Continuing ...", method)
A R Karthick881e7ea2019-08-19 19:44:02 +0000636 } else {
637 streamingTracker.calls[method] = &callTracker{failedPacket: nil}
Richard Jankowskidbab94a2018-12-06 16:20:25 -0500638 }
A R Karthick881e7ea2019-08-19 19:44:02 +0000639 return streamingTracker.calls[method]
640}
641
642func (handler *APIHandler) flushFailedPackets(tracker *callTracker) error {
643 if tracker.failedPacket != nil {
644 switch tracker.failedPacket.(type) {
645 case openflow_13.PacketIn:
Girish Kumarf56a4682020-03-20 20:07:46 +0000646 logger.Debug("Enqueueing last failed packetIn")
A R Karthick881e7ea2019-08-19 19:44:02 +0000647 handler.packetInQueue <- tracker.failedPacket.(openflow_13.PacketIn)
648 case openflow_13.ChangeEvent:
Girish Kumarf56a4682020-03-20 20:07:46 +0000649 logger.Debug("Enqueueing last failed changeEvent")
A R Karthick881e7ea2019-08-19 19:44:02 +0000650 handler.changeEventQueue <- tracker.failedPacket.(openflow_13.ChangeEvent)
651 }
652 }
653 return nil
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500654}
655
npujar1d86a522019-11-14 17:11:16 +0530656// ReceivePacketsIn receives packets from adapter
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500657func (handler *APIHandler) ReceivePacketsIn(empty *empty.Empty, packetsIn voltha.VolthaService_ReceivePacketsInServer) error {
A R Karthick881e7ea2019-08-19 19:44:02 +0000658 var streamingTracker = handler.getStreamingTracker("ReceivePacketsIn", handler.packetInQueueDone)
Girish Kumarf56a4682020-03-20 20:07:46 +0000659 logger.Debugw("ReceivePacketsIn-request", log.Fields{"packetsIn": packetsIn})
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500660
npujar1d86a522019-11-14 17:11:16 +0530661 err := handler.flushFailedPackets(streamingTracker)
662 if err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000663 logger.Errorw("unable-to-flush-failed-packets", log.Fields{"error": err})
npujar1d86a522019-11-14 17:11:16 +0530664 }
A R Karthick881e7ea2019-08-19 19:44:02 +0000665
666loop:
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500667 for {
A R Karthick881e7ea2019-08-19 19:44:02 +0000668 select {
669 case packet := <-handler.packetInQueue:
Girish Kumarf56a4682020-03-20 20:07:46 +0000670 logger.Debugw("sending-packet-in", log.Fields{
Matteo Scandolo360605d2019-11-05 18:29:17 -0800671 "packet": hex.EncodeToString(packet.PacketIn.Data),
672 })
A R Karthick881e7ea2019-08-19 19:44:02 +0000673 if err := packetsIn.Send(&packet); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000674 logger.Errorw("failed-to-send-packet", log.Fields{"error": err})
A R Karthick881e7ea2019-08-19 19:44:02 +0000675 // save the last failed packet in
676 streamingTracker.failedPacket = packet
677 } else {
678 if streamingTracker.failedPacket != nil {
679 // reset last failed packet saved to avoid flush
680 streamingTracker.failedPacket = nil
Richard Jankowskidbab94a2018-12-06 16:20:25 -0500681 }
682 }
A R Karthick881e7ea2019-08-19 19:44:02 +0000683 case <-handler.packetInQueueDone:
Girish Kumarf56a4682020-03-20 20:07:46 +0000684 logger.Debug("Another ReceivePacketsIn running. Bailing out ...")
A R Karthick881e7ea2019-08-19 19:44:02 +0000685 break loop
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500686 }
687 }
A R Karthick881e7ea2019-08-19 19:44:02 +0000688
689 //TODO: Find an elegant way to get out of the above loop when the Core is stopped
690 return nil
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500691}
692
npujar1d86a522019-11-14 17:11:16 +0530693func (handler *APIHandler) sendChangeEvent(deviceID string, portStatus *openflow_13.OfpPortStatus) {
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500694 // TODO: validate the type of portStatus parameter
695 //if _, ok := portStatus.(*openflow_13.OfpPortStatus); ok {
696 //}
npujar1d86a522019-11-14 17:11:16 +0530697 event := openflow_13.ChangeEvent{Id: deviceID, Event: &openflow_13.ChangeEvent_PortStatus{PortStatus: portStatus}}
Girish Kumarf56a4682020-03-20 20:07:46 +0000698 logger.Debugw("sendChangeEvent", log.Fields{"event": event})
A R Karthick881e7ea2019-08-19 19:44:02 +0000699 handler.changeEventQueue <- event
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500700}
701
npujar1d86a522019-11-14 17:11:16 +0530702// ReceiveChangeEvents receives change in events
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500703func (handler *APIHandler) ReceiveChangeEvents(empty *empty.Empty, changeEvents voltha.VolthaService_ReceiveChangeEventsServer) error {
A R Karthick881e7ea2019-08-19 19:44:02 +0000704 var streamingTracker = handler.getStreamingTracker("ReceiveChangeEvents", handler.changeEventQueueDone)
Girish Kumarf56a4682020-03-20 20:07:46 +0000705 logger.Debugw("ReceiveChangeEvents-request", log.Fields{"changeEvents": changeEvents})
A R Karthick881e7ea2019-08-19 19:44:02 +0000706
npujar1d86a522019-11-14 17:11:16 +0530707 err := handler.flushFailedPackets(streamingTracker)
708 if err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000709 logger.Errorw("unable-to-flush-failed-packets", log.Fields{"error": err})
npujar1d86a522019-11-14 17:11:16 +0530710 }
A R Karthick881e7ea2019-08-19 19:44:02 +0000711
712loop:
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500713 for {
A R Karthick881e7ea2019-08-19 19:44:02 +0000714 select {
Richard Jankowski199fd862019-03-18 14:49:51 -0400715 // Dequeue a change event
A R Karthick881e7ea2019-08-19 19:44:02 +0000716 case event := <-handler.changeEventQueue:
Girish Kumarf56a4682020-03-20 20:07:46 +0000717 logger.Debugw("sending-change-event", log.Fields{"event": event})
A R Karthick881e7ea2019-08-19 19:44:02 +0000718 if err := changeEvents.Send(&event); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000719 logger.Errorw("failed-to-send-change-event", log.Fields{"error": err})
A R Karthick881e7ea2019-08-19 19:44:02 +0000720 // save last failed changeevent
721 streamingTracker.failedPacket = event
722 } else {
723 if streamingTracker.failedPacket != nil {
724 // reset last failed event saved on success to avoid flushing
725 streamingTracker.failedPacket = nil
Richard Jankowski199fd862019-03-18 14:49:51 -0400726 }
727 }
A R Karthick881e7ea2019-08-19 19:44:02 +0000728 case <-handler.changeEventQueueDone:
Girish Kumarf56a4682020-03-20 20:07:46 +0000729 logger.Debug("Another ReceiveChangeEvents already running. Bailing out ...")
A R Karthick881e7ea2019-08-19 19:44:02 +0000730 break loop
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500731 }
732 }
A R Karthick881e7ea2019-08-19 19:44:02 +0000733
734 return nil
Richard Jankowski199fd862019-03-18 14:49:51 -0400735}
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500736
npujar1d86a522019-11-14 17:11:16 +0530737// Subscribe subscribing request of ofagent
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500738func (handler *APIHandler) Subscribe(
739 ctx context.Context,
740 ofAgent *voltha.OfAgentSubscriber,
741) (*voltha.OfAgentSubscriber, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000742 logger.Debugw("Subscribe-request", log.Fields{"ofAgent": ofAgent})
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500743 return &voltha.OfAgentSubscriber{OfagentId: ofAgent.OfagentId, VolthaId: ofAgent.VolthaId}, nil
744}
William Kurkiandaa6bb22019-03-07 12:26:28 -0500745
npujar1d86a522019-11-14 17:11:16 +0530746// GetAlarmDeviceData @TODO useless stub, what should this actually do?
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500747func (handler *APIHandler) GetAlarmDeviceData(ctx context.Context, in *common.ID) (*omci.AlarmDeviceData, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000748 logger.Debug("GetAlarmDeviceData-stub")
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500749 return &omci.AlarmDeviceData{}, errors.New("UnImplemented")
William Kurkiandaa6bb22019-03-07 12:26:28 -0500750}
751
npujar1d86a522019-11-14 17:11:16 +0530752// ListLogicalDeviceMeters returns logical device meters
Manikkaraj kb1a10922019-07-29 12:10:34 -0400753func (handler *APIHandler) ListLogicalDeviceMeters(ctx context.Context, id *voltha.ID) (*openflow_13.Meters, error) {
754
Girish Kumarf56a4682020-03-20 20:07:46 +0000755 logger.Debugw("ListLogicalDeviceMeters", log.Fields{"id": *id})
Manikkaraj kb1a10922019-07-29 12:10:34 -0400756 return handler.logicalDeviceMgr.ListLogicalDeviceMeters(ctx, id.Id)
William Kurkiandaa6bb22019-03-07 12:26:28 -0500757}
758
npujar1d86a522019-11-14 17:11:16 +0530759// GetMeterStatsOfLogicalDevice @TODO useless stub, what should this actually do?
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500760func (handler *APIHandler) GetMeterStatsOfLogicalDevice(ctx context.Context, in *common.ID) (*openflow_13.MeterStatsReply, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000761 logger.Debug("GetMeterStatsOfLogicalDevice")
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500762 return &openflow_13.MeterStatsReply{}, errors.New("UnImplemented")
763}
764
npujar1d86a522019-11-14 17:11:16 +0530765// GetMibDeviceData @TODO useless stub, what should this actually do?
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500766func (handler *APIHandler) GetMibDeviceData(ctx context.Context, in *common.ID) (*omci.MibDeviceData, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000767 logger.Debug("GetMibDeviceData")
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500768 return &omci.MibDeviceData{}, errors.New("UnImplemented")
William Kurkiandaa6bb22019-03-07 12:26:28 -0500769}
770
npujar1d86a522019-11-14 17:11:16 +0530771// SimulateAlarm sends simulate alarm request
William Kurkiandaa6bb22019-03-07 12:26:28 -0500772func (handler *APIHandler) SimulateAlarm(
773 ctx context.Context,
774 in *voltha.SimulateAlarmRequest,
775) (*common.OperationResp, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000776 logger.Debugw("SimulateAlarm-request", log.Fields{"id": in.Id})
serkant.uluderya334479d2019-04-10 08:26:15 -0700777 successResp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS}
serkant.uluderya334479d2019-04-10 08:26:15 -0700778 ch := make(chan interface{})
779 defer close(ch)
780 go handler.deviceMgr.simulateAlarm(ctx, in, ch)
781 return successResp, nil
William Kurkiandaa6bb22019-03-07 12:26:28 -0500782}
783
npujar1d86a522019-11-14 17:11:16 +0530784// UpdateLogicalDeviceMeterTable - This function sends meter mod request to logical device manager and waits for response
Manikkaraj kb1a10922019-07-29 12:10:34 -0400785func (handler *APIHandler) UpdateLogicalDeviceMeterTable(ctx context.Context, meter *openflow_13.MeterModUpdate) (*empty.Empty, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000786 logger.Debugw("UpdateLogicalDeviceMeterTable-request",
Manikkaraj kb1a10922019-07-29 12:10:34 -0400787 log.Fields{"meter": meter, "test": common.TestModeKeys_api_test.String()})
Manikkaraj kb1a10922019-07-29 12:10:34 -0400788 ch := make(chan interface{})
789 defer close(ch)
790 go handler.logicalDeviceMgr.updateMeterTable(ctx, meter.Id, meter.MeterMod, ch)
791 return waitForNilResponseOnSuccess(ctx, ch)
William Kurkiandaa6bb22019-03-07 12:26:28 -0500792}
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500793
npujar1d86a522019-11-14 17:11:16 +0530794// GetMembership returns membership
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500795func (handler *APIHandler) GetMembership(context.Context, *empty.Empty) (*voltha.Membership, error) {
796 return &voltha.Membership{}, errors.New("UnImplemented")
797}
798
npujar1d86a522019-11-14 17:11:16 +0530799// UpdateMembership updates membership
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500800func (handler *APIHandler) UpdateMembership(context.Context, *voltha.Membership) (*empty.Empty, error) {
801 return &empty.Empty{}, errors.New("UnImplemented")
802}
kesavandbc2d1622020-01-21 00:42:01 -0500803
804func (handler *APIHandler) EnablePort(ctx context.Context, port *voltha.Port) (*empty.Empty, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000805 logger.Debugw("EnablePort-request", log.Fields{"device-id": port.DeviceId, "port-no": port.PortNo})
kesavandbc2d1622020-01-21 00:42:01 -0500806 ch := make(chan interface{})
807 defer close(ch)
808 go handler.deviceMgr.enablePort(ctx, port, ch)
809 return waitForNilResponseOnSuccess(ctx, ch)
810}
811
812func (handler *APIHandler) DisablePort(ctx context.Context, port *voltha.Port) (*empty.Empty, error) {
813
Girish Kumarf56a4682020-03-20 20:07:46 +0000814 logger.Debugw("DisablePort-request", log.Fields{"device-id": port.DeviceId, "port-no": port.PortNo})
kesavandbc2d1622020-01-21 00:42:01 -0500815 ch := make(chan interface{})
816 defer close(ch)
817 go handler.deviceMgr.disablePort(ctx, port, ch)
818 return waitForNilResponseOnSuccess(ctx, ch)
819}
onkarkundargi87285252020-01-27 11:34:52 +0530820
821func (handler *APIHandler) StartOmciTestAction(ctx context.Context, omcitestrequest *voltha.OmciTestRequest) (*voltha.TestResponse, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000822 logger.Debugw("Omci_test_Request", log.Fields{"id": omcitestrequest.Id, "uuid": omcitestrequest.Uuid})
onkarkundargi87285252020-01-27 11:34:52 +0530823 return handler.deviceMgr.startOmciTest(ctx, omcitestrequest)
824}