blob: 1c296dd26bbbec7e78e42282cab9de5cbe4c38d3 [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
Kent Hagerman2b216042020-04-03 18:28:56 -040017package api
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"
24 "github.com/golang/protobuf/ptypes/empty"
25 da "github.com/opencord/voltha-go/common/core/northbound/grpc"
Kent Hagerman2b216042020-04-03 18:28:56 -040026 "github.com/opencord/voltha-go/rw_core/core/adapter"
27 "github.com/opencord/voltha-go/rw_core/core/device"
serkant.uluderya2ae470f2020-01-21 11:13:09 -080028 "github.com/opencord/voltha-lib-go/v3/pkg/log"
29 "github.com/opencord/voltha-lib-go/v3/pkg/version"
30 "github.com/opencord/voltha-protos/v3/go/common"
31 "github.com/opencord/voltha-protos/v3/go/omci"
32 "github.com/opencord/voltha-protos/v3/go/openflow_13"
33 "github.com/opencord/voltha-protos/v3/go/voltha"
khenaidoob9203542018-09-17 22:56:37 -040034 "google.golang.org/grpc/codes"
khenaidoob9203542018-09-17 22:56:37 -040035 "google.golang.org/grpc/status"
Kent Hagerman2b216042020-04-03 18:28:56 -040036 "io"
37 "sync"
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
Kent Hagerman2b216042020-04-03 18:28:56 -040048// NBIHandler represent attributes of API handler
49type NBIHandler struct {
50 deviceMgr *device.Manager
51 logicalDeviceMgr *device.LogicalManager
52 adapterMgr *adapter.Manager
53 packetInQueue chan openflow_13.PacketIn
54 changeEventQueue chan openflow_13.ChangeEvent
55 packetInQueueDone chan bool
56 changeEventQueueDone chan bool
khenaidoobf6e7bb2018-08-14 22:27:29 -040057 da.DefaultAPIHandler
58}
59
npujar1d86a522019-11-14 17:11:16 +053060// NewAPIHandler creates API handler instance
Kent Hagerman2b216042020-04-03 18:28:56 -040061func NewAPIHandler(deviceMgr *device.Manager, logicalDeviceMgr *device.LogicalManager, adapterMgr *adapter.Manager) *NBIHandler {
62 return &NBIHandler{
63 deviceMgr: deviceMgr,
64 logicalDeviceMgr: logicalDeviceMgr,
65 adapterMgr: adapterMgr,
66 packetInQueue: make(chan openflow_13.PacketIn, 100),
67 changeEventQueue: make(chan openflow_13.ChangeEvent, 100),
68 packetInQueueDone: make(chan bool, 1),
69 changeEventQueueDone: make(chan bool, 1),
Stephane Barbarie6e1bd502018-11-05 22:44:45 -050070 }
khenaidoobf6e7bb2018-08-14 22:27:29 -040071}
khenaidoo4d4802d2018-10-04 21:59:49 -040072
khenaidoo09771ef2019-10-11 14:25:02 -040073// waitForNilResponseOnSuccess is a helper function to wait for a response on channel monitorCh where an nil
khenaidoo4d4802d2018-10-04 21:59:49 -040074// response is expected in a successful scenario
75func waitForNilResponseOnSuccess(ctx context.Context, ch chan interface{}) (*empty.Empty, error) {
76 select {
77 case res := <-ch:
78 if res == nil {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -050079 return &empty.Empty{}, nil
khenaidoo4d4802d2018-10-04 21:59:49 -040080 } else if err, ok := res.(error); ok {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -050081 return &empty.Empty{}, err
khenaidoo4d4802d2018-10-04 21:59:49 -040082 } else {
Girish Kumarf56a4682020-03-20 20:07:46 +000083 logger.Warnw("unexpected-return-type", log.Fields{"result": res})
khenaidoo4d4802d2018-10-04 21:59:49 -040084 err = status.Errorf(codes.Internal, "%s", res)
Kent Hagermanc2c73ff2019-11-20 16:22:32 -050085 return &empty.Empty{}, err
khenaidoo4d4802d2018-10-04 21:59:49 -040086 }
87 case <-ctx.Done():
Girish Kumarf56a4682020-03-20 20:07:46 +000088 logger.Debug("client-timeout")
khenaidoo4d4802d2018-10-04 21:59:49 -040089 return nil, ctx.Err()
90 }
91}
92
Kent Hagermanc2c73ff2019-11-20 16:22:32 -050093// ListCoreInstances returns details on the running core containers
Kent Hagerman2b216042020-04-03 18:28:56 -040094func (handler *NBIHandler) ListCoreInstances(ctx context.Context, empty *empty.Empty) (*voltha.CoreInstances, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +000095 logger.Debug("ListCoreInstances")
Kent Hagermanc2c73ff2019-11-20 16:22:32 -050096 // TODO: unused stub
97 return &voltha.CoreInstances{}, status.Errorf(codes.NotFound, "no-core-instances")
98}
99
100// GetCoreInstance returns the details of a specific core container
Kent Hagerman2b216042020-04-03 18:28:56 -0400101func (handler *NBIHandler) GetCoreInstance(ctx context.Context, id *voltha.ID) (*voltha.CoreInstance, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000102 logger.Debugw("GetCoreInstance", log.Fields{"id": id})
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500103 //TODO: unused stub
104 return &voltha.CoreInstance{}, status.Errorf(codes.NotFound, "core-instance-%s", id.Id)
105}
106
npujar1d86a522019-11-14 17:11:16 +0530107// GetLogicalDevicePort returns logical device port details
Kent Hagerman2b216042020-04-03 18:28:56 -0400108func (handler *NBIHandler) GetLogicalDevicePort(ctx context.Context, id *voltha.LogicalPortId) (*voltha.LogicalPort, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000109 logger.Debugw("GetLogicalDevicePort-request", log.Fields{"id": *id})
khenaidoo43aa6bd2019-05-29 13:35:13 -0400110
Kent Hagerman2b216042020-04-03 18:28:56 -0400111 return handler.logicalDeviceMgr.GetLogicalPort(ctx, id)
khenaidoo43aa6bd2019-05-29 13:35:13 -0400112}
113
npujar1d86a522019-11-14 17:11:16 +0530114// EnableLogicalDevicePort enables logical device port
Kent Hagerman2b216042020-04-03 18:28:56 -0400115func (handler *NBIHandler) EnableLogicalDevicePort(ctx context.Context, id *voltha.LogicalPortId) (*empty.Empty, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000116 logger.Debugw("EnableLogicalDevicePort-request", log.Fields{"id": id, "test": common.TestModeKeys_api_test.String()})
khenaidoo9cdc1a62019-01-24 21:57:40 -0500117
khenaidoo4d4802d2018-10-04 21:59:49 -0400118 ch := make(chan interface{})
119 defer close(ch)
Kent Hagerman2b216042020-04-03 18:28:56 -0400120 go handler.logicalDeviceMgr.EnableLogicalPort(ctx, id, ch)
khenaidoo4d4802d2018-10-04 21:59:49 -0400121 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400122}
123
npujar1d86a522019-11-14 17:11:16 +0530124// DisableLogicalDevicePort disables logical device port
Kent Hagerman2b216042020-04-03 18:28:56 -0400125func (handler *NBIHandler) DisableLogicalDevicePort(ctx context.Context, id *voltha.LogicalPortId) (*empty.Empty, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000126 logger.Debugw("DisableLogicalDevicePort-request", log.Fields{"id": id, "test": common.TestModeKeys_api_test.String()})
khenaidoo9cdc1a62019-01-24 21:57:40 -0500127
khenaidoo19d7b632018-10-30 10:49:50 -0400128 ch := make(chan interface{})
129 defer close(ch)
Kent Hagerman2b216042020-04-03 18:28:56 -0400130 go handler.logicalDeviceMgr.DisableLogicalPort(ctx, id, ch)
khenaidoo19d7b632018-10-30 10:49:50 -0400131 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400132}
133
npujar1d86a522019-11-14 17:11:16 +0530134// UpdateLogicalDeviceFlowTable updates logical device flow table
Kent Hagerman2b216042020-04-03 18:28:56 -0400135func (handler *NBIHandler) UpdateLogicalDeviceFlowTable(ctx context.Context, flow *openflow_13.FlowTableUpdate) (*empty.Empty, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000136 logger.Debugw("UpdateLogicalDeviceFlowTable-request", log.Fields{"flow": flow, "test": common.TestModeKeys_api_test.String()})
khenaidoo9cdc1a62019-01-24 21:57:40 -0500137
khenaidoo19d7b632018-10-30 10:49:50 -0400138 ch := make(chan interface{})
139 defer close(ch)
Kent Hagerman2b216042020-04-03 18:28:56 -0400140 go handler.logicalDeviceMgr.UpdateFlowTable(ctx, flow.Id, flow.FlowMod, ch)
khenaidoo19d7b632018-10-30 10:49:50 -0400141 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400142}
143
npujar1d86a522019-11-14 17:11:16 +0530144// UpdateLogicalDeviceFlowGroupTable updates logical device flow group table
Kent Hagerman2b216042020-04-03 18:28:56 -0400145func (handler *NBIHandler) UpdateLogicalDeviceFlowGroupTable(ctx context.Context, flow *openflow_13.FlowGroupTableUpdate) (*empty.Empty, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000146 logger.Debugw("UpdateLogicalDeviceFlowGroupTable-request", log.Fields{"flow": flow, "test": common.TestModeKeys_api_test.String()})
khenaidoo19d7b632018-10-30 10:49:50 -0400147 ch := make(chan interface{})
148 defer close(ch)
Kent Hagerman2b216042020-04-03 18:28:56 -0400149 go handler.logicalDeviceMgr.UpdateGroupTable(ctx, flow.Id, flow.GroupMod, ch)
khenaidoo19d7b632018-10-30 10:49:50 -0400150 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400151}
152
khenaidoob9203542018-09-17 22:56:37 -0400153// GetDevice must be implemented in the read-only containers - should it also be implemented here?
Kent Hagerman2b216042020-04-03 18:28:56 -0400154func (handler *NBIHandler) GetDevice(ctx context.Context, id *voltha.ID) (*voltha.Device, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000155 logger.Debugw("GetDevice-request", log.Fields{"id": id})
npujar467fe752020-01-16 20:17:45 +0530156 return handler.deviceMgr.GetDevice(ctx, id.Id)
khenaidoob9203542018-09-17 22:56:37 -0400157}
158
159// GetDevice must be implemented in the read-only containers - should it also be implemented here?
npujar1d86a522019-11-14 17:11:16 +0530160
161// ListDevices retrieves the latest devices from the data model
Kent Hagerman2b216042020-04-03 18:28:56 -0400162func (handler *NBIHandler) ListDevices(ctx context.Context, empty *empty.Empty) (*voltha.Devices, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000163 logger.Debug("ListDevices")
npujar467fe752020-01-16 20:17:45 +0530164 devices, err := handler.deviceMgr.ListDevices(ctx)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530165 if err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000166 logger.Errorw("Failed to list devices", log.Fields{"error": err})
Thomas Lee Se5a44012019-11-07 20:32:24 +0530167 return nil, err
168 }
169 return devices, nil
khenaidoob9203542018-09-17 22:56:37 -0400170}
171
khenaidoo7ccedd52018-12-14 16:48:54 -0500172// ListDeviceIds returns the list of device ids managed by a voltha core
Kent Hagerman2b216042020-04-03 18:28:56 -0400173func (handler *NBIHandler) ListDeviceIds(ctx context.Context, empty *empty.Empty) (*voltha.IDs, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000174 logger.Debug("ListDeviceIDs")
khenaidoo7ccedd52018-12-14 16:48:54 -0500175 return handler.deviceMgr.ListDeviceIds()
176}
177
178//ReconcileDevices is a request to a voltha core to managed a list of devices based on their IDs
Kent Hagerman2b216042020-04-03 18:28:56 -0400179func (handler *NBIHandler) ReconcileDevices(ctx context.Context, ids *voltha.IDs) (*empty.Empty, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000180 logger.Debug("ReconcileDevices")
khenaidoo9cdc1a62019-01-24 21:57:40 -0500181
khenaidoo7ccedd52018-12-14 16:48:54 -0500182 ch := make(chan interface{})
183 defer close(ch)
184 go handler.deviceMgr.ReconcileDevices(ctx, ids, ch)
185 return waitForNilResponseOnSuccess(ctx, ch)
186}
187
npujar1d86a522019-11-14 17:11:16 +0530188// GetLogicalDevice provides a cloned most up to date logical device
Kent Hagerman2b216042020-04-03 18:28:56 -0400189func (handler *NBIHandler) GetLogicalDevice(ctx context.Context, id *voltha.ID) (*voltha.LogicalDevice, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000190 logger.Debugw("GetLogicalDevice-request", log.Fields{"id": id})
Kent Hagerman2b216042020-04-03 18:28:56 -0400191 return handler.logicalDeviceMgr.GetLogicalDevice(ctx, id.Id)
khenaidoob9203542018-09-17 22:56:37 -0400192}
193
npujar1d86a522019-11-14 17:11:16 +0530194// ListLogicalDevices returns the list of all logical devices
Kent Hagerman2b216042020-04-03 18:28:56 -0400195func (handler *NBIHandler) ListLogicalDevices(ctx context.Context, empty *empty.Empty) (*voltha.LogicalDevices, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000196 logger.Debug("ListLogicalDevices-request")
Kent Hagerman2b216042020-04-03 18:28:56 -0400197 return handler.logicalDeviceMgr.ListLogicalDevices(ctx)
khenaidoob9203542018-09-17 22:56:37 -0400198}
199
khenaidoo21d51152019-02-01 13:48:37 -0500200// ListAdapters returns the contents of all adapters known to the system
Kent Hagerman2b216042020-04-03 18:28:56 -0400201func (handler *NBIHandler) ListAdapters(ctx context.Context, empty *empty.Empty) (*voltha.Adapters, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000202 logger.Debug("ListAdapters")
Kent Hagerman2b216042020-04-03 18:28:56 -0400203 return handler.adapterMgr.ListAdapters(ctx)
khenaidoo21d51152019-02-01 13:48:37 -0500204}
205
npujar1d86a522019-11-14 17:11:16 +0530206// ListLogicalDeviceFlows returns the flows of logical device
Kent Hagerman2b216042020-04-03 18:28:56 -0400207func (handler *NBIHandler) ListLogicalDeviceFlows(ctx context.Context, id *voltha.ID) (*openflow_13.Flows, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000208 logger.Debugw("ListLogicalDeviceFlows", log.Fields{"id": *id})
khenaidoodd237172019-05-27 16:37:17 -0400209 return handler.logicalDeviceMgr.ListLogicalDeviceFlows(ctx, id.Id)
210}
211
npujar1d86a522019-11-14 17:11:16 +0530212// ListLogicalDeviceFlowGroups returns logical device flow groups
Kent Hagerman2b216042020-04-03 18:28:56 -0400213func (handler *NBIHandler) ListLogicalDeviceFlowGroups(ctx context.Context, id *voltha.ID) (*openflow_13.FlowGroups, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000214 logger.Debugw("ListLogicalDeviceFlowGroups", log.Fields{"id": *id})
khenaidoodd237172019-05-27 16:37:17 -0400215 return handler.logicalDeviceMgr.ListLogicalDeviceFlowGroups(ctx, id.Id)
216}
217
npujar1d86a522019-11-14 17:11:16 +0530218// ListLogicalDevicePorts returns ports of logical device
Kent Hagerman2b216042020-04-03 18:28:56 -0400219func (handler *NBIHandler) ListLogicalDevicePorts(ctx context.Context, id *voltha.ID) (*voltha.LogicalPorts, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000220 logger.Debugw("ListLogicalDevicePorts", log.Fields{"logicaldeviceid": id})
khenaidoo19d7b632018-10-30 10:49:50 -0400221 return handler.logicalDeviceMgr.ListLogicalDevicePorts(ctx, id.Id)
222}
223
khenaidoo4d4802d2018-10-04 21:59:49 -0400224// CreateDevice creates a new parent device in the data model
Kent Hagerman2b216042020-04-03 18:28:56 -0400225func (handler *NBIHandler) CreateDevice(ctx context.Context, device *voltha.Device) (*voltha.Device, error) {
Thomas Lee S51b5cb82019-10-14 14:49:34 +0530226 if device.MacAddress == "" && device.GetHostAndPort() == "" {
Girish Kumarf56a4682020-03-20 20:07:46 +0000227 logger.Errorf("No Device Info Present")
Thomas Lee Se5a44012019-11-07 20:32:24 +0530228 return &voltha.Device{}, errors.New("no-device-info-present; MAC or HOSTIP&PORT")
Thomas Lee S51b5cb82019-10-14 14:49:34 +0530229 }
Girish Kumarf56a4682020-03-20 20:07:46 +0000230 logger.Debugw("create-device", log.Fields{"device": *device})
khenaidoo9cdc1a62019-01-24 21:57:40 -0500231
khenaidoob9203542018-09-17 22:56:37 -0400232 ch := make(chan interface{})
233 defer close(ch)
Kent Hagerman2b216042020-04-03 18:28:56 -0400234 go handler.deviceMgr.CreateDevice(ctx, device, ch)
khenaidoob9203542018-09-17 22:56:37 -0400235 select {
236 case res := <-ch:
khenaidoo92e62c52018-10-03 14:02:54 -0400237 if res != nil {
238 if err, ok := res.(error); ok {
Girish Kumarf56a4682020-03-20 20:07:46 +0000239 logger.Errorw("create-device-failed", log.Fields{"error": err})
Thomas Lee Se5a44012019-11-07 20:32:24 +0530240 return nil, err
khenaidoo92e62c52018-10-03 14:02:54 -0400241 }
242 if d, ok := res.(*voltha.Device); ok {
243 return d, nil
244 }
khenaidoob9203542018-09-17 22:56:37 -0400245 }
Girish Kumarf56a4682020-03-20 20:07:46 +0000246 logger.Warnw("create-device-unexpected-return-type", log.Fields{"result": res})
khenaidoo92e62c52018-10-03 14:02:54 -0400247 err := status.Errorf(codes.Internal, "%s", res)
248 return &voltha.Device{}, err
khenaidoob9203542018-09-17 22:56:37 -0400249 case <-ctx.Done():
Girish Kumarf56a4682020-03-20 20:07:46 +0000250 logger.Debug("createdevice-client-timeout")
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500251 return &voltha.Device{}, ctx.Err()
khenaidoob9203542018-09-17 22:56:37 -0400252 }
khenaidoobf6e7bb2018-08-14 22:27:29 -0400253}
254
khenaidoo4d4802d2018-10-04 21:59:49 -0400255// EnableDevice activates a device by invoking the adopt_device API on the appropriate adapter
Kent Hagerman2b216042020-04-03 18:28:56 -0400256func (handler *NBIHandler) EnableDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000257 logger.Debugw("enabledevice", log.Fields{"id": id})
khenaidoo9cdc1a62019-01-24 21:57:40 -0500258
khenaidoob9203542018-09-17 22:56:37 -0400259 ch := make(chan interface{})
260 defer close(ch)
Kent Hagerman2b216042020-04-03 18:28:56 -0400261 go handler.deviceMgr.EnableDevice(ctx, id, ch)
khenaidoo4d4802d2018-10-04 21:59:49 -0400262 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400263}
264
khenaidoo4d4802d2018-10-04 21:59:49 -0400265// DisableDevice disables a device along with any child device it may have
Kent Hagerman2b216042020-04-03 18:28:56 -0400266func (handler *NBIHandler) DisableDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000267 logger.Debugw("disabledevice-request", log.Fields{"id": id})
khenaidoo9cdc1a62019-01-24 21:57:40 -0500268
khenaidoo92e62c52018-10-03 14:02:54 -0400269 ch := make(chan interface{})
270 defer close(ch)
Kent Hagerman2b216042020-04-03 18:28:56 -0400271 go handler.deviceMgr.DisableDevice(ctx, id, ch)
khenaidoo4d4802d2018-10-04 21:59:49 -0400272 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400273}
274
khenaidoo4d4802d2018-10-04 21:59:49 -0400275//RebootDevice invoked the reboot API to the corresponding adapter
Kent Hagerman2b216042020-04-03 18:28:56 -0400276func (handler *NBIHandler) RebootDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000277 logger.Debugw("rebootDevice-request", log.Fields{"id": id})
khenaidoo9cdc1a62019-01-24 21:57:40 -0500278
khenaidoo4d4802d2018-10-04 21:59:49 -0400279 ch := make(chan interface{})
280 defer close(ch)
Kent Hagerman2b216042020-04-03 18:28:56 -0400281 go handler.deviceMgr.RebootDevice(ctx, id, ch)
khenaidoo4d4802d2018-10-04 21:59:49 -0400282 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400283}
284
khenaidoo4d4802d2018-10-04 21:59:49 -0400285// DeleteDevice removes a device from the data model
Kent Hagerman2b216042020-04-03 18:28:56 -0400286func (handler *NBIHandler) DeleteDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000287 logger.Debugw("deletedevice-request", log.Fields{"id": id})
khenaidoo9cdc1a62019-01-24 21:57:40 -0500288
khenaidoo4d4802d2018-10-04 21:59:49 -0400289 ch := make(chan interface{})
290 defer close(ch)
Kent Hagerman2b216042020-04-03 18:28:56 -0400291 go handler.deviceMgr.DeleteDevice(ctx, id, ch)
khenaidoo4d4802d2018-10-04 21:59:49 -0400292 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400293}
294
David Bainbridge4087cc52019-11-13 18:36:03 +0000295// ListDevicePorts returns the ports details for a specific device entry
Kent Hagerman2b216042020-04-03 18:28:56 -0400296func (handler *NBIHandler) ListDevicePorts(ctx context.Context, id *voltha.ID) (*voltha.Ports, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000297 logger.Debugw("listdeviceports-request", log.Fields{"id": id})
npujar467fe752020-01-16 20:17:45 +0530298 device, err := handler.deviceMgr.GetDevice(ctx, id.Id)
David Bainbridge4087cc52019-11-13 18:36:03 +0000299 if err != nil {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500300 return &voltha.Ports{}, err
David Bainbridge4087cc52019-11-13 18:36:03 +0000301 }
302 ports := &voltha.Ports{}
npujar1d86a522019-11-14 17:11:16 +0530303 ports.Items = append(ports.Items, device.Ports...)
David Bainbridge4087cc52019-11-13 18:36:03 +0000304 return ports, nil
305}
306
307// ListDeviceFlows returns the flow details for a specific device entry
Kent Hagerman2b216042020-04-03 18:28:56 -0400308func (handler *NBIHandler) ListDeviceFlows(ctx context.Context, id *voltha.ID) (*openflow_13.Flows, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000309 logger.Debugw("listdeviceflows-request", log.Fields{"id": id})
David Bainbridge4087cc52019-11-13 18:36:03 +0000310
npujar467fe752020-01-16 20:17:45 +0530311 device, err := handler.deviceMgr.GetDevice(ctx, id.Id)
David Bainbridge4087cc52019-11-13 18:36:03 +0000312 if err != nil {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500313 return &openflow_13.Flows{}, err
David Bainbridge4087cc52019-11-13 18:36:03 +0000314 }
315 flows := &openflow_13.Flows{}
npujar1d86a522019-11-14 17:11:16 +0530316 flows.Items = append(flows.Items, device.Flows.Items...)
David Bainbridge4087cc52019-11-13 18:36:03 +0000317 return flows, nil
318}
319
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500320// ListDeviceFlowGroups returns the flow group details for a specific device entry
Kent Hagerman2b216042020-04-03 18:28:56 -0400321func (handler *NBIHandler) ListDeviceFlowGroups(ctx context.Context, id *voltha.ID) (*voltha.FlowGroups, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000322 logger.Debugw("ListDeviceFlowGroups", log.Fields{"deviceid": id})
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500323
npujar467fe752020-01-16 20:17:45 +0530324 if device, _ := handler.deviceMgr.GetDevice(ctx, id.Id); device != nil {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500325 return device.GetFlowGroups(), nil
326 }
327 return &voltha.FlowGroups{}, status.Errorf(codes.NotFound, "device-%s", id.Id)
328}
329
330// ListDeviceGroups returns all the device groups known to the system
Kent Hagerman2b216042020-04-03 18:28:56 -0400331func (handler *NBIHandler) ListDeviceGroups(ctx context.Context, empty *empty.Empty) (*voltha.DeviceGroups, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000332 logger.Debug("ListDeviceGroups")
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500333 return &voltha.DeviceGroups{}, errors.New("UnImplemented")
334}
335
336// GetDeviceGroup returns a specific device group entry
Kent Hagerman2b216042020-04-03 18:28:56 -0400337func (handler *NBIHandler) GetDeviceGroup(ctx context.Context, id *voltha.ID) (*voltha.DeviceGroup, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000338 logger.Debug("GetDeviceGroup")
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500339 return &voltha.DeviceGroup{}, errors.New("UnImplemented")
340}
341
342// ListDeviceTypes returns all the device types known to the system
Kent Hagerman2b216042020-04-03 18:28:56 -0400343func (handler *NBIHandler) ListDeviceTypes(ctx context.Context, _ *empty.Empty) (*voltha.DeviceTypes, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000344 logger.Debug("ListDeviceTypes")
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500345
Kent Hagerman2b216042020-04-03 18:28:56 -0400346 return &voltha.DeviceTypes{Items: handler.adapterMgr.ListDeviceTypes()}, nil
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500347}
348
349// GetDeviceType returns the device type for a specific device entry
Kent Hagerman2b216042020-04-03 18:28:56 -0400350func (handler *NBIHandler) GetDeviceType(ctx context.Context, id *voltha.ID) (*voltha.DeviceType, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000351 logger.Debugw("GetDeviceType", log.Fields{"typeid": id})
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500352
Kent Hagerman2b216042020-04-03 18:28:56 -0400353 if deviceType := handler.adapterMgr.GetDeviceType(id.Id); deviceType != nil {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500354 return deviceType, nil
355 }
356 return &voltha.DeviceType{}, status.Errorf(codes.NotFound, "device_type-%s", id.Id)
357}
358
David Bainbridge4087cc52019-11-13 18:36:03 +0000359// GetVoltha returns the contents of all components (i.e. devices, logical_devices, ...)
Kent Hagerman2b216042020-04-03 18:28:56 -0400360func (handler *NBIHandler) GetVoltha(ctx context.Context, empty *empty.Empty) (*voltha.Voltha, error) {
David Bainbridge4087cc52019-11-13 18:36:03 +0000361
Girish Kumarf56a4682020-03-20 20:07:46 +0000362 logger.Debug("GetVoltha")
David Bainbridge4087cc52019-11-13 18:36:03 +0000363 /*
364 * For now, encode all the version information into a JSON object and
365 * pass that back as "version" so the client can get all the
366 * information associated with the version. Long term the API should
367 * better accomidate this, but for now this will work.
368 */
369 data, err := json.Marshal(&version.VersionInfo)
370 info := version.VersionInfo.Version
371 if err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000372 logger.Warnf("Unable to encode version information as JSON: %s", err.Error())
David Bainbridge4087cc52019-11-13 18:36:03 +0000373 } else {
374 info = string(data)
375 }
376
377 return &voltha.Voltha{
378 Version: info,
379 }, nil
380}
381
khenaidoof5a5bfa2019-01-23 22:20:29 -0500382// processImageRequest is a helper method to execute an image download request
Kent Hagerman2b216042020-04-03 18:28:56 -0400383func (handler *NBIHandler) processImageRequest(ctx context.Context, img *voltha.ImageDownload, requestType int) (*common.OperationResp, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000384 logger.Debugw("processImageDownload", log.Fields{"img": *img, "requestType": requestType})
khenaidoof5a5bfa2019-01-23 22:20:29 -0500385
khenaidoo2c6a0992019-04-29 13:46:56 -0400386 failedresponse := &common.OperationResp{Code: voltha.OperationResp_OPERATION_FAILURE}
khenaidoof5a5bfa2019-01-23 22:20:29 -0500387
388 ch := make(chan interface{})
389 defer close(ch)
390 switch requestType {
npujar1d86a522019-11-14 17:11:16 +0530391 case ImageDownload:
Kent Hagerman2b216042020-04-03 18:28:56 -0400392 go handler.deviceMgr.DownloadImage(ctx, img, ch)
npujar1d86a522019-11-14 17:11:16 +0530393 case CancelImageDownload:
Kent Hagerman2b216042020-04-03 18:28:56 -0400394 go handler.deviceMgr.CancelImageDownload(ctx, img, ch)
npujar1d86a522019-11-14 17:11:16 +0530395 case ActivateImage:
Kent Hagerman2b216042020-04-03 18:28:56 -0400396 go handler.deviceMgr.ActivateImage(ctx, img, ch)
npujar1d86a522019-11-14 17:11:16 +0530397 case RevertImage:
Kent Hagerman2b216042020-04-03 18:28:56 -0400398 go handler.deviceMgr.RevertImage(ctx, img, ch)
khenaidoof5a5bfa2019-01-23 22:20:29 -0500399 default:
Girish Kumarf56a4682020-03-20 20:07:46 +0000400 logger.Warn("invalid-request-type", log.Fields{"requestType": requestType})
khenaidoof5a5bfa2019-01-23 22:20:29 -0500401 return failedresponse, status.Errorf(codes.InvalidArgument, "%d", requestType)
402 }
403 select {
404 case res := <-ch:
405 if res != nil {
406 if err, ok := res.(error); ok {
407 return failedresponse, err
408 }
409 if opResp, ok := res.(*common.OperationResp); ok {
410 return opResp, nil
411 }
412 }
Girish Kumarf56a4682020-03-20 20:07:46 +0000413 logger.Warnw("download-image-unexpected-return-type", log.Fields{"result": res})
khenaidoof5a5bfa2019-01-23 22:20:29 -0500414 return failedresponse, status.Errorf(codes.Internal, "%s", res)
415 case <-ctx.Done():
Girish Kumarf56a4682020-03-20 20:07:46 +0000416 logger.Debug("downloadImage-client-timeout")
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500417 return &common.OperationResp{}, ctx.Err()
khenaidoof5a5bfa2019-01-23 22:20:29 -0500418 }
419}
420
npujar1d86a522019-11-14 17:11:16 +0530421// DownloadImage execute an image download request
Kent Hagerman2b216042020-04-03 18:28:56 -0400422func (handler *NBIHandler) DownloadImage(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000423 logger.Debugw("DownloadImage-request", log.Fields{"img": *img})
khenaidoobf6e7bb2018-08-14 22:27:29 -0400424
npujar1d86a522019-11-14 17:11:16 +0530425 return handler.processImageRequest(ctx, img, ImageDownload)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400426}
427
npujar1d86a522019-11-14 17:11:16 +0530428// CancelImageDownload cancels image download request
Kent Hagerman2b216042020-04-03 18:28:56 -0400429func (handler *NBIHandler) CancelImageDownload(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000430 logger.Debugw("cancelImageDownload-request", log.Fields{"img": *img})
npujar1d86a522019-11-14 17:11:16 +0530431 return handler.processImageRequest(ctx, img, CancelImageDownload)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400432}
433
npujar1d86a522019-11-14 17:11:16 +0530434// ActivateImageUpdate activates image update request
Kent Hagerman2b216042020-04-03 18:28:56 -0400435func (handler *NBIHandler) ActivateImageUpdate(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000436 logger.Debugw("activateImageUpdate-request", log.Fields{"img": *img})
npujar1d86a522019-11-14 17:11:16 +0530437 return handler.processImageRequest(ctx, img, ActivateImage)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400438}
439
npujar1d86a522019-11-14 17:11:16 +0530440// RevertImageUpdate reverts image update
Kent Hagerman2b216042020-04-03 18:28:56 -0400441func (handler *NBIHandler) RevertImageUpdate(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000442 logger.Debugw("revertImageUpdate-request", log.Fields{"img": *img})
npujar1d86a522019-11-14 17:11:16 +0530443 return handler.processImageRequest(ctx, img, RevertImage)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400444}
445
npujar1d86a522019-11-14 17:11:16 +0530446// GetImageDownloadStatus returns status of image download
Kent Hagerman2b216042020-04-03 18:28:56 -0400447func (handler *NBIHandler) GetImageDownloadStatus(ctx context.Context, img *voltha.ImageDownload) (*voltha.ImageDownload, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000448 logger.Debugw("getImageDownloadStatus-request", log.Fields{"img": *img})
khenaidoof5a5bfa2019-01-23 22:20:29 -0500449
Stephane Barbariedf5479f2019-01-29 22:13:00 -0500450 failedresponse := &voltha.ImageDownload{DownloadState: voltha.ImageDownload_DOWNLOAD_UNKNOWN}
khenaidoof5a5bfa2019-01-23 22:20:29 -0500451
khenaidoof5a5bfa2019-01-23 22:20:29 -0500452 ch := make(chan interface{})
453 defer close(ch)
Kent Hagerman2b216042020-04-03 18:28:56 -0400454 go handler.deviceMgr.GetImageDownloadStatus(ctx, img, ch)
khenaidoof5a5bfa2019-01-23 22:20:29 -0500455
456 select {
457 case res := <-ch:
458 if res != nil {
459 if err, ok := res.(error); ok {
460 return failedresponse, err
461 }
462 if downloadResp, ok := res.(*voltha.ImageDownload); ok {
463 return downloadResp, nil
464 }
465 }
Girish Kumarf56a4682020-03-20 20:07:46 +0000466 logger.Warnw("download-image-status", log.Fields{"result": res})
khenaidoof5a5bfa2019-01-23 22:20:29 -0500467 return failedresponse, status.Errorf(codes.Internal, "%s", res)
468 case <-ctx.Done():
Girish Kumarf56a4682020-03-20 20:07:46 +0000469 logger.Debug("downloadImage-client-timeout")
khenaidoof5a5bfa2019-01-23 22:20:29 -0500470 return failedresponse, ctx.Err()
471 }
472}
473
npujar1d86a522019-11-14 17:11:16 +0530474// GetImageDownload returns image download
Kent Hagerman2b216042020-04-03 18:28:56 -0400475func (handler *NBIHandler) GetImageDownload(ctx context.Context, img *voltha.ImageDownload) (*voltha.ImageDownload, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000476 logger.Debugw("GetImageDownload-request", log.Fields{"img": *img})
khenaidoof5a5bfa2019-01-23 22:20:29 -0500477
Kent Hagerman2b216042020-04-03 18:28:56 -0400478 download, err := handler.deviceMgr.GetImageDownload(ctx, img)
npujar1d86a522019-11-14 17:11:16 +0530479 if err != nil {
Stephane Barbariedf5479f2019-01-29 22:13:00 -0500480 return &voltha.ImageDownload{DownloadState: voltha.ImageDownload_DOWNLOAD_UNKNOWN}, err
khenaidoof5a5bfa2019-01-23 22:20:29 -0500481 }
npujar1d86a522019-11-14 17:11:16 +0530482 return download, nil
khenaidoof5a5bfa2019-01-23 22:20:29 -0500483}
484
npujar1d86a522019-11-14 17:11:16 +0530485// ListImageDownloads returns image downloads
Kent Hagerman2b216042020-04-03 18:28:56 -0400486func (handler *NBIHandler) ListImageDownloads(ctx context.Context, id *voltha.ID) (*voltha.ImageDownloads, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000487 logger.Debugw("ListImageDownloads-request", log.Fields{"deviceId": id.Id})
khenaidoof5a5bfa2019-01-23 22:20:29 -0500488
Kent Hagerman2b216042020-04-03 18:28:56 -0400489 downloads, err := handler.deviceMgr.ListImageDownloads(ctx, id.Id)
npujar1d86a522019-11-14 17:11:16 +0530490 if err != nil {
khenaidoof5a5bfa2019-01-23 22:20:29 -0500491 failedResp := &voltha.ImageDownloads{
khenaidoo2c6a0992019-04-29 13:46:56 -0400492 Items: []*voltha.ImageDownload{
493 {DownloadState: voltha.ImageDownload_DOWNLOAD_UNKNOWN},
494 },
khenaidoof5a5bfa2019-01-23 22:20:29 -0500495 }
496 return failedResp, err
khenaidoof5a5bfa2019-01-23 22:20:29 -0500497 }
npujar1d86a522019-11-14 17:11:16 +0530498 return downloads, nil
khenaidoof5a5bfa2019-01-23 22:20:29 -0500499}
500
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500501// GetImages returns all images for a specific device entry
Kent Hagerman2b216042020-04-03 18:28:56 -0400502func (handler *NBIHandler) GetImages(ctx context.Context, id *voltha.ID) (*voltha.Images, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000503 logger.Debugw("GetImages", log.Fields{"deviceid": id.Id})
npujar467fe752020-01-16 20:17:45 +0530504 device, err := handler.deviceMgr.GetDevice(ctx, id.Id)
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500505 if err != nil {
506 return &voltha.Images{}, err
507 }
508 return device.GetImages(), nil
509}
510
npujar1d86a522019-11-14 17:11:16 +0530511// UpdateDevicePmConfigs updates the PM configs
Kent Hagerman2b216042020-04-03 18:28:56 -0400512func (handler *NBIHandler) UpdateDevicePmConfigs(ctx context.Context, configs *voltha.PmConfigs) (*empty.Empty, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000513 logger.Debugw("UpdateDevicePmConfigs-request", log.Fields{"configs": *configs})
khenaidoob3127472019-07-24 21:04:55 -0400514
515 ch := make(chan interface{})
516 defer close(ch)
Kent Hagerman2b216042020-04-03 18:28:56 -0400517 go handler.deviceMgr.UpdatePmConfigs(ctx, configs, ch)
khenaidoob3127472019-07-24 21:04:55 -0400518 return waitForNilResponseOnSuccess(ctx, ch)
519}
520
npujar1d86a522019-11-14 17:11:16 +0530521// ListDevicePmConfigs returns pm configs of device
Kent Hagerman2b216042020-04-03 18:28:56 -0400522func (handler *NBIHandler) ListDevicePmConfigs(ctx context.Context, id *voltha.ID) (*voltha.PmConfigs, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000523 logger.Debugw("ListDevicePmConfigs-request", log.Fields{"deviceId": *id})
Kent Hagerman2b216042020-04-03 18:28:56 -0400524 return handler.deviceMgr.ListPmConfigs(ctx, id.Id)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400525}
526
Kent Hagerman2b216042020-04-03 18:28:56 -0400527func (handler *NBIHandler) CreateEventFilter(ctx context.Context, filter *voltha.EventFilter) (*voltha.EventFilter, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000528 logger.Debugw("CreateEventFilter-request", log.Fields{"filter": *filter})
Devmalya Paulc594bb32019-11-06 07:34:27 +0000529 return nil, errors.New("UnImplemented")
khenaidoobf6e7bb2018-08-14 22:27:29 -0400530}
531
Kent Hagerman2b216042020-04-03 18:28:56 -0400532func (handler *NBIHandler) UpdateEventFilter(ctx context.Context, filter *voltha.EventFilter) (*voltha.EventFilter, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000533 logger.Debugw("UpdateEventFilter-request", log.Fields{"filter": *filter})
Devmalya Paulc594bb32019-11-06 07:34:27 +0000534 return nil, errors.New("UnImplemented")
khenaidoobf6e7bb2018-08-14 22:27:29 -0400535}
536
Kent Hagerman2b216042020-04-03 18:28:56 -0400537func (handler *NBIHandler) DeleteEventFilter(ctx context.Context, filterInfo *voltha.EventFilter) (*empty.Empty, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000538 logger.Debugw("DeleteEventFilter-request", log.Fields{"device-id": filterInfo.DeviceId, "filter-id": filterInfo.Id})
Devmalya Paulc594bb32019-11-06 07:34:27 +0000539 return nil, errors.New("UnImplemented")
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500540}
541
Devmalya Paulc594bb32019-11-06 07:34:27 +0000542// GetEventFilter returns all the filters present for a device
Kent Hagerman2b216042020-04-03 18:28:56 -0400543func (handler *NBIHandler) GetEventFilter(ctx context.Context, id *voltha.ID) (*voltha.EventFilters, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000544 logger.Debugw("GetEventFilter-request", log.Fields{"device-id": id})
Devmalya Paulc594bb32019-11-06 07:34:27 +0000545 return nil, errors.New("UnImplemented")
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500546}
547
Devmalya Paulc594bb32019-11-06 07:34:27 +0000548// ListEventFilters returns all the filters known to the system
Kent Hagerman2b216042020-04-03 18:28:56 -0400549func (handler *NBIHandler) ListEventFilters(ctx context.Context, empty *empty.Empty) (*voltha.EventFilters, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000550 logger.Debug("ListEventFilter-request")
Devmalya Paulc594bb32019-11-06 07:34:27 +0000551 return nil, errors.New("UnImplemented")
khenaidoobf6e7bb2018-08-14 22:27:29 -0400552}
553
Kent Hagerman2b216042020-04-03 18:28:56 -0400554func (handler *NBIHandler) SelfTest(ctx context.Context, id *voltha.ID) (*voltha.SelfTestResponse, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000555 logger.Debugw("SelfTest-request", log.Fields{"id": id})
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500556 return &voltha.SelfTestResponse{}, errors.New("UnImplemented")
khenaidoobf6e7bb2018-08-14 22:27:29 -0400557}
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500558
npujar1d86a522019-11-14 17:11:16 +0530559// StreamPacketsOut sends packets to adapter
Kent Hagerman2b216042020-04-03 18:28:56 -0400560func (handler *NBIHandler) StreamPacketsOut(packets voltha.VolthaService_StreamPacketsOutServer) error {
Girish Kumarf56a4682020-03-20 20:07:46 +0000561 logger.Debugw("StreamPacketsOut-request", log.Fields{"packets": packets})
khenaidoo5e250692019-08-30 14:46:21 -0400562loop:
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500563 for {
khenaidoo5e250692019-08-30 14:46:21 -0400564 select {
565 case <-packets.Context().Done():
Girish Kumarf56a4682020-03-20 20:07:46 +0000566 logger.Infow("StreamPacketsOut-context-done", log.Fields{"packets": packets, "error": packets.Context().Err()})
khenaidoo5e250692019-08-30 14:46:21 -0400567 break loop
568 default:
569 }
570
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500571 packet, err := packets.Recv()
572
573 if err == io.EOF {
Girish Kumarf56a4682020-03-20 20:07:46 +0000574 logger.Debugw("Received-EOF", log.Fields{"packets": packets})
khenaidoo5e250692019-08-30 14:46:21 -0400575 break loop
576 }
577
578 if err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000579 logger.Errorw("Failed to receive packet out", log.Fields{"error": err})
khenaidoo5e250692019-08-30 14:46:21 -0400580 continue
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500581 }
582
Kent Hagerman2b216042020-04-03 18:28:56 -0400583 handler.logicalDeviceMgr.PacketOut(packets.Context(), packet)
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500584 }
585
Girish Kumarf56a4682020-03-20 20:07:46 +0000586 logger.Debugw("StreamPacketsOut-request-done", log.Fields{"packets": packets})
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500587 return nil
588}
589
Kent Hagerman2b216042020-04-03 18:28:56 -0400590func (handler *NBIHandler) SendPacketIn(deviceID string, transationID string, packet *openflow_13.OfpPacketIn) {
khenaidoo297cd252019-02-07 22:10:23 -0500591 // TODO: Augment the OF PacketIn to include the transactionId
npujar1d86a522019-11-14 17:11:16 +0530592 packetIn := openflow_13.PacketIn{Id: deviceID, PacketIn: packet}
Kent Hagerman2b216042020-04-03 18:28:56 -0400593 logger.Debugw("SendPacketIn", log.Fields{"packetIn": packetIn})
A R Karthick881e7ea2019-08-19 19:44:02 +0000594 handler.packetInQueue <- packetIn
595}
596
597type callTracker struct {
598 failedPacket interface{}
599}
600type streamTracker struct {
601 calls map[string]*callTracker
602 sync.Mutex
603}
604
605var streamingTracker = &streamTracker{calls: make(map[string]*callTracker)}
606
Kent Hagerman2b216042020-04-03 18:28:56 -0400607func (handler *NBIHandler) getStreamingTracker(method string, done chan<- bool) *callTracker {
A R Karthick881e7ea2019-08-19 19:44:02 +0000608 streamingTracker.Lock()
609 defer streamingTracker.Unlock()
610 if _, ok := streamingTracker.calls[method]; ok {
611 // bail out the other packet in thread
Girish Kumarf56a4682020-03-20 20:07:46 +0000612 logger.Debugf("%s streaming call already running. Exiting it", method)
A R Karthick881e7ea2019-08-19 19:44:02 +0000613 done <- true
Girish Kumarf56a4682020-03-20 20:07:46 +0000614 logger.Debugf("Last %s exited. Continuing ...", method)
A R Karthick881e7ea2019-08-19 19:44:02 +0000615 } else {
616 streamingTracker.calls[method] = &callTracker{failedPacket: nil}
Richard Jankowskidbab94a2018-12-06 16:20:25 -0500617 }
A R Karthick881e7ea2019-08-19 19:44:02 +0000618 return streamingTracker.calls[method]
619}
620
Kent Hagerman2b216042020-04-03 18:28:56 -0400621func (handler *NBIHandler) flushFailedPackets(tracker *callTracker) error {
A R Karthick881e7ea2019-08-19 19:44:02 +0000622 if tracker.failedPacket != nil {
623 switch tracker.failedPacket.(type) {
624 case openflow_13.PacketIn:
Girish Kumarf56a4682020-03-20 20:07:46 +0000625 logger.Debug("Enqueueing last failed packetIn")
A R Karthick881e7ea2019-08-19 19:44:02 +0000626 handler.packetInQueue <- tracker.failedPacket.(openflow_13.PacketIn)
627 case openflow_13.ChangeEvent:
Girish Kumarf56a4682020-03-20 20:07:46 +0000628 logger.Debug("Enqueueing last failed changeEvent")
A R Karthick881e7ea2019-08-19 19:44:02 +0000629 handler.changeEventQueue <- tracker.failedPacket.(openflow_13.ChangeEvent)
630 }
631 }
632 return nil
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500633}
634
npujar1d86a522019-11-14 17:11:16 +0530635// ReceivePacketsIn receives packets from adapter
Kent Hagerman2b216042020-04-03 18:28:56 -0400636func (handler *NBIHandler) ReceivePacketsIn(empty *empty.Empty, packetsIn voltha.VolthaService_ReceivePacketsInServer) error {
A R Karthick881e7ea2019-08-19 19:44:02 +0000637 var streamingTracker = handler.getStreamingTracker("ReceivePacketsIn", handler.packetInQueueDone)
Girish Kumarf56a4682020-03-20 20:07:46 +0000638 logger.Debugw("ReceivePacketsIn-request", log.Fields{"packetsIn": packetsIn})
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500639
npujar1d86a522019-11-14 17:11:16 +0530640 err := handler.flushFailedPackets(streamingTracker)
641 if err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000642 logger.Errorw("unable-to-flush-failed-packets", log.Fields{"error": err})
npujar1d86a522019-11-14 17:11:16 +0530643 }
A R Karthick881e7ea2019-08-19 19:44:02 +0000644
645loop:
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500646 for {
A R Karthick881e7ea2019-08-19 19:44:02 +0000647 select {
648 case packet := <-handler.packetInQueue:
Girish Kumarf56a4682020-03-20 20:07:46 +0000649 logger.Debugw("sending-packet-in", log.Fields{
Matteo Scandolo360605d2019-11-05 18:29:17 -0800650 "packet": hex.EncodeToString(packet.PacketIn.Data),
651 })
A R Karthick881e7ea2019-08-19 19:44:02 +0000652 if err := packetsIn.Send(&packet); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000653 logger.Errorw("failed-to-send-packet", log.Fields{"error": err})
A R Karthick881e7ea2019-08-19 19:44:02 +0000654 // save the last failed packet in
655 streamingTracker.failedPacket = packet
656 } else {
657 if streamingTracker.failedPacket != nil {
658 // reset last failed packet saved to avoid flush
659 streamingTracker.failedPacket = nil
Richard Jankowskidbab94a2018-12-06 16:20:25 -0500660 }
661 }
A R Karthick881e7ea2019-08-19 19:44:02 +0000662 case <-handler.packetInQueueDone:
Girish Kumarf56a4682020-03-20 20:07:46 +0000663 logger.Debug("Another ReceivePacketsIn running. Bailing out ...")
A R Karthick881e7ea2019-08-19 19:44:02 +0000664 break loop
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500665 }
666 }
A R Karthick881e7ea2019-08-19 19:44:02 +0000667
668 //TODO: Find an elegant way to get out of the above loop when the Core is stopped
669 return nil
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500670}
671
Kent Hagerman2b216042020-04-03 18:28:56 -0400672func (handler *NBIHandler) SendChangeEvent(deviceID string, portStatus *openflow_13.OfpPortStatus) {
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500673 // TODO: validate the type of portStatus parameter
674 //if _, ok := portStatus.(*openflow_13.OfpPortStatus); ok {
675 //}
npujar1d86a522019-11-14 17:11:16 +0530676 event := openflow_13.ChangeEvent{Id: deviceID, Event: &openflow_13.ChangeEvent_PortStatus{PortStatus: portStatus}}
Kent Hagerman2b216042020-04-03 18:28:56 -0400677 logger.Debugw("SendChangeEvent", log.Fields{"event": event})
A R Karthick881e7ea2019-08-19 19:44:02 +0000678 handler.changeEventQueue <- event
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500679}
680
npujar1d86a522019-11-14 17:11:16 +0530681// ReceiveChangeEvents receives change in events
Kent Hagerman2b216042020-04-03 18:28:56 -0400682func (handler *NBIHandler) ReceiveChangeEvents(empty *empty.Empty, changeEvents voltha.VolthaService_ReceiveChangeEventsServer) error {
A R Karthick881e7ea2019-08-19 19:44:02 +0000683 var streamingTracker = handler.getStreamingTracker("ReceiveChangeEvents", handler.changeEventQueueDone)
Girish Kumarf56a4682020-03-20 20:07:46 +0000684 logger.Debugw("ReceiveChangeEvents-request", log.Fields{"changeEvents": changeEvents})
A R Karthick881e7ea2019-08-19 19:44:02 +0000685
npujar1d86a522019-11-14 17:11:16 +0530686 err := handler.flushFailedPackets(streamingTracker)
687 if err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000688 logger.Errorw("unable-to-flush-failed-packets", log.Fields{"error": err})
npujar1d86a522019-11-14 17:11:16 +0530689 }
A R Karthick881e7ea2019-08-19 19:44:02 +0000690
691loop:
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500692 for {
A R Karthick881e7ea2019-08-19 19:44:02 +0000693 select {
Richard Jankowski199fd862019-03-18 14:49:51 -0400694 // Dequeue a change event
A R Karthick881e7ea2019-08-19 19:44:02 +0000695 case event := <-handler.changeEventQueue:
Girish Kumarf56a4682020-03-20 20:07:46 +0000696 logger.Debugw("sending-change-event", log.Fields{"event": event})
A R Karthick881e7ea2019-08-19 19:44:02 +0000697 if err := changeEvents.Send(&event); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000698 logger.Errorw("failed-to-send-change-event", log.Fields{"error": err})
A R Karthick881e7ea2019-08-19 19:44:02 +0000699 // save last failed changeevent
700 streamingTracker.failedPacket = event
701 } else {
702 if streamingTracker.failedPacket != nil {
703 // reset last failed event saved on success to avoid flushing
704 streamingTracker.failedPacket = nil
Richard Jankowski199fd862019-03-18 14:49:51 -0400705 }
706 }
A R Karthick881e7ea2019-08-19 19:44:02 +0000707 case <-handler.changeEventQueueDone:
Girish Kumarf56a4682020-03-20 20:07:46 +0000708 logger.Debug("Another ReceiveChangeEvents already running. Bailing out ...")
A R Karthick881e7ea2019-08-19 19:44:02 +0000709 break loop
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500710 }
711 }
A R Karthick881e7ea2019-08-19 19:44:02 +0000712
713 return nil
Richard Jankowski199fd862019-03-18 14:49:51 -0400714}
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500715
Kent Hagerman2b216042020-04-03 18:28:56 -0400716func (handler *NBIHandler) GetChangeEventsQueueForTest() <-chan openflow_13.ChangeEvent {
717 return handler.changeEventQueue
718}
719
npujar1d86a522019-11-14 17:11:16 +0530720// Subscribe subscribing request of ofagent
Kent Hagerman2b216042020-04-03 18:28:56 -0400721func (handler *NBIHandler) Subscribe(
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500722 ctx context.Context,
723 ofAgent *voltha.OfAgentSubscriber,
724) (*voltha.OfAgentSubscriber, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000725 logger.Debugw("Subscribe-request", log.Fields{"ofAgent": ofAgent})
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500726 return &voltha.OfAgentSubscriber{OfagentId: ofAgent.OfagentId, VolthaId: ofAgent.VolthaId}, nil
727}
William Kurkiandaa6bb22019-03-07 12:26:28 -0500728
npujar1d86a522019-11-14 17:11:16 +0530729// GetAlarmDeviceData @TODO useless stub, what should this actually do?
Kent Hagerman2b216042020-04-03 18:28:56 -0400730func (handler *NBIHandler) GetAlarmDeviceData(ctx context.Context, in *common.ID) (*omci.AlarmDeviceData, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000731 logger.Debug("GetAlarmDeviceData-stub")
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500732 return &omci.AlarmDeviceData{}, errors.New("UnImplemented")
William Kurkiandaa6bb22019-03-07 12:26:28 -0500733}
734
npujar1d86a522019-11-14 17:11:16 +0530735// ListLogicalDeviceMeters returns logical device meters
Kent Hagerman2b216042020-04-03 18:28:56 -0400736func (handler *NBIHandler) ListLogicalDeviceMeters(ctx context.Context, id *voltha.ID) (*openflow_13.Meters, error) {
Manikkaraj kb1a10922019-07-29 12:10:34 -0400737
Girish Kumarf56a4682020-03-20 20:07:46 +0000738 logger.Debugw("ListLogicalDeviceMeters", log.Fields{"id": *id})
Manikkaraj kb1a10922019-07-29 12:10:34 -0400739 return handler.logicalDeviceMgr.ListLogicalDeviceMeters(ctx, id.Id)
William Kurkiandaa6bb22019-03-07 12:26:28 -0500740}
741
npujar1d86a522019-11-14 17:11:16 +0530742// GetMeterStatsOfLogicalDevice @TODO useless stub, what should this actually do?
Kent Hagerman2b216042020-04-03 18:28:56 -0400743func (handler *NBIHandler) GetMeterStatsOfLogicalDevice(ctx context.Context, in *common.ID) (*openflow_13.MeterStatsReply, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000744 logger.Debug("GetMeterStatsOfLogicalDevice")
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500745 return &openflow_13.MeterStatsReply{}, errors.New("UnImplemented")
746}
747
npujar1d86a522019-11-14 17:11:16 +0530748// GetMibDeviceData @TODO useless stub, what should this actually do?
Kent Hagerman2b216042020-04-03 18:28:56 -0400749func (handler *NBIHandler) GetMibDeviceData(ctx context.Context, in *common.ID) (*omci.MibDeviceData, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000750 logger.Debug("GetMibDeviceData")
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500751 return &omci.MibDeviceData{}, errors.New("UnImplemented")
William Kurkiandaa6bb22019-03-07 12:26:28 -0500752}
753
npujar1d86a522019-11-14 17:11:16 +0530754// SimulateAlarm sends simulate alarm request
Kent Hagerman2b216042020-04-03 18:28:56 -0400755func (handler *NBIHandler) SimulateAlarm(
William Kurkiandaa6bb22019-03-07 12:26:28 -0500756 ctx context.Context,
757 in *voltha.SimulateAlarmRequest,
758) (*common.OperationResp, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000759 logger.Debugw("SimulateAlarm-request", log.Fields{"id": in.Id})
serkant.uluderya334479d2019-04-10 08:26:15 -0700760 successResp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS}
serkant.uluderya334479d2019-04-10 08:26:15 -0700761 ch := make(chan interface{})
762 defer close(ch)
Kent Hagerman2b216042020-04-03 18:28:56 -0400763 go handler.deviceMgr.SimulateAlarm(ctx, in, ch)
serkant.uluderya334479d2019-04-10 08:26:15 -0700764 return successResp, nil
William Kurkiandaa6bb22019-03-07 12:26:28 -0500765}
766
npujar1d86a522019-11-14 17:11:16 +0530767// UpdateLogicalDeviceMeterTable - This function sends meter mod request to logical device manager and waits for response
Kent Hagerman2b216042020-04-03 18:28:56 -0400768func (handler *NBIHandler) UpdateLogicalDeviceMeterTable(ctx context.Context, meter *openflow_13.MeterModUpdate) (*empty.Empty, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000769 logger.Debugw("UpdateLogicalDeviceMeterTable-request",
Manikkaraj kb1a10922019-07-29 12:10:34 -0400770 log.Fields{"meter": meter, "test": common.TestModeKeys_api_test.String()})
Manikkaraj kb1a10922019-07-29 12:10:34 -0400771 ch := make(chan interface{})
772 defer close(ch)
Kent Hagerman2b216042020-04-03 18:28:56 -0400773 go handler.logicalDeviceMgr.UpdateMeterTable(ctx, meter.Id, meter.MeterMod, ch)
Manikkaraj kb1a10922019-07-29 12:10:34 -0400774 return waitForNilResponseOnSuccess(ctx, ch)
William Kurkiandaa6bb22019-03-07 12:26:28 -0500775}
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500776
npujar1d86a522019-11-14 17:11:16 +0530777// GetMembership returns membership
Kent Hagerman2b216042020-04-03 18:28:56 -0400778func (handler *NBIHandler) GetMembership(context.Context, *empty.Empty) (*voltha.Membership, error) {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500779 return &voltha.Membership{}, errors.New("UnImplemented")
780}
781
npujar1d86a522019-11-14 17:11:16 +0530782// UpdateMembership updates membership
Kent Hagerman2b216042020-04-03 18:28:56 -0400783func (handler *NBIHandler) UpdateMembership(context.Context, *voltha.Membership) (*empty.Empty, error) {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500784 return &empty.Empty{}, errors.New("UnImplemented")
785}
kesavandbc2d1622020-01-21 00:42:01 -0500786
Kent Hagerman2b216042020-04-03 18:28:56 -0400787func (handler *NBIHandler) EnablePort(ctx context.Context, port *voltha.Port) (*empty.Empty, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000788 logger.Debugw("EnablePort-request", log.Fields{"device-id": port.DeviceId, "port-no": port.PortNo})
kesavandbc2d1622020-01-21 00:42:01 -0500789 ch := make(chan interface{})
790 defer close(ch)
Kent Hagerman2b216042020-04-03 18:28:56 -0400791 go handler.deviceMgr.EnablePort(ctx, port, ch)
kesavandbc2d1622020-01-21 00:42:01 -0500792 return waitForNilResponseOnSuccess(ctx, ch)
793}
794
Kent Hagerman2b216042020-04-03 18:28:56 -0400795func (handler *NBIHandler) DisablePort(ctx context.Context, port *voltha.Port) (*empty.Empty, error) {
kesavandbc2d1622020-01-21 00:42:01 -0500796
Girish Kumarf56a4682020-03-20 20:07:46 +0000797 logger.Debugw("DisablePort-request", log.Fields{"device-id": port.DeviceId, "port-no": port.PortNo})
kesavandbc2d1622020-01-21 00:42:01 -0500798 ch := make(chan interface{})
799 defer close(ch)
Kent Hagerman2b216042020-04-03 18:28:56 -0400800 go handler.deviceMgr.DisablePort(ctx, port, ch)
kesavandbc2d1622020-01-21 00:42:01 -0500801 return waitForNilResponseOnSuccess(ctx, ch)
802}
onkarkundargi87285252020-01-27 11:34:52 +0530803
Kent Hagerman2b216042020-04-03 18:28:56 -0400804func (handler *NBIHandler) StartOmciTestAction(ctx context.Context, omcitestrequest *voltha.OmciTestRequest) (*voltha.TestResponse, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000805 logger.Debugw("Omci_test_Request", log.Fields{"id": omcitestrequest.Id, "uuid": omcitestrequest.Uuid})
Kent Hagerman2b216042020-04-03 18:28:56 -0400806 return handler.deviceMgr.StartOmciTest(ctx, omcitestrequest)
onkarkundargi87285252020-01-27 11:34:52 +0530807}