blob: 0196f35217d252b46ae0254fa469c9c818092624 [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"
khenaidoo1ce37ad2019-03-24 22:07:24 -040030 "github.com/opencord/voltha-go/rw_core/utils"
serkant.uluderya2ae470f2020-01-21 11:13:09 -080031 "github.com/opencord/voltha-lib-go/v3/pkg/log"
32 "github.com/opencord/voltha-lib-go/v3/pkg/version"
33 "github.com/opencord/voltha-protos/v3/go/common"
34 "github.com/opencord/voltha-protos/v3/go/omci"
35 "github.com/opencord/voltha-protos/v3/go/openflow_13"
36 "github.com/opencord/voltha-protos/v3/go/voltha"
khenaidoob9203542018-09-17 22:56:37 -040037 "google.golang.org/grpc/codes"
khenaidoobf6e7bb2018-08-14 22:27:29 -040038 "google.golang.org/grpc/metadata"
khenaidoob9203542018-09-17 22:56:37 -040039 "google.golang.org/grpc/status"
khenaidoobf6e7bb2018-08-14 22:27:29 -040040)
41
npujar1d86a522019-11-14 17:11:16 +053042var errorIDNotFound = status.Error(codes.NotFound, "id-not-found")
khenaidoof684e1b2019-10-28 19:00:37 -040043
npujar1d86a522019-11-14 17:11:16 +053044// Image related constants
khenaidoof5a5bfa2019-01-23 22:20:29 -050045const (
npujar1d86a522019-11-14 17:11:16 +053046 ImageDownload = iota
47 CancelImageDownload = iota
48 ActivateImage = iota
49 RevertImage = iota
khenaidoof5a5bfa2019-01-23 22:20:29 -050050)
51
npujar1d86a522019-11-14 17:11:16 +053052// APIHandler represent attributes of API handler
khenaidoobf6e7bb2018-08-14 22:27:29 -040053type APIHandler struct {
khenaidoo2c6a0992019-04-29 13:46:56 -040054 deviceMgr *DeviceManager
55 logicalDeviceMgr *LogicalDeviceManager
56 adapterMgr *AdapterManager
A R Karthick881e7ea2019-08-19 19:44:02 +000057 packetInQueue chan openflow_13.PacketIn
58 changeEventQueue chan openflow_13.ChangeEvent
59 packetInQueueDone chan bool
60 changeEventQueueDone chan bool
khenaidoo2c6a0992019-04-29 13:46:56 -040061 coreInCompetingMode bool
khenaidoo442e7c72020-03-10 16:13:48 -040062 longRunningRequestTimeout time.Duration
63 defaultRequestTimeout time.Duration
khenaidoobf6e7bb2018-08-14 22:27:29 -040064 da.DefaultAPIHandler
khenaidoo54e0ddf2019-02-27 16:21:33 -050065 core *Core
khenaidoobf6e7bb2018-08-14 22:27:29 -040066}
67
npujar1d86a522019-11-14 17:11:16 +053068// NewAPIHandler creates API handler instance
khenaidoo54e0ddf2019-02-27 16:21:33 -050069func NewAPIHandler(core *Core) *APIHandler {
Stephane Barbarie6e1bd502018-11-05 22:44:45 -050070 handler := &APIHandler{
khenaidoo2c6a0992019-04-29 13:46:56 -040071 deviceMgr: core.deviceMgr,
72 logicalDeviceMgr: core.logicalDeviceMgr,
73 adapterMgr: core.adapterMgr,
74 coreInCompetingMode: core.config.InCompetingMode,
75 longRunningRequestTimeout: core.config.LongRunningRequestTimeout,
76 defaultRequestTimeout: core.config.DefaultRequestTimeout,
A R Karthick881e7ea2019-08-19 19:44:02 +000077 packetInQueue: make(chan openflow_13.PacketIn, 100),
78 changeEventQueue: make(chan openflow_13.ChangeEvent, 100),
79 packetInQueueDone: make(chan bool, 1),
80 changeEventQueueDone: make(chan bool, 1),
81 core: core,
Stephane Barbarie6e1bd502018-11-05 22:44:45 -050082 }
khenaidoobf6e7bb2018-08-14 22:27:29 -040083 return handler
84}
khenaidoo4d4802d2018-10-04 21:59:49 -040085
86// isTestMode is a helper function to determine a function is invoked for testing only
khenaidoobf6e7bb2018-08-14 22:27:29 -040087func isTestMode(ctx context.Context) bool {
88 md, _ := metadata.FromIncomingContext(ctx)
89 _, exist := md[common.TestModeKeys_api_test.String()]
90 return exist
91}
92
Richard Jankowskid42826e2018-11-02 16:06:37 -040093// This function attempts to extract the serial number from the request metadata
94// and create a KV transaction for that serial number for the current core.
95func (handler *APIHandler) createKvTransaction(ctx context.Context) (*KVTransaction, error) {
96 var (
khenaidoo43c82122018-11-22 18:38:28 -050097 err error
98 ok bool
99 md metadata.MD
Richard Jankowskid42826e2018-11-02 16:06:37 -0400100 serNum []string
101 )
102 if md, ok = metadata.FromIncomingContext(ctx); !ok {
103 err = errors.New("metadata-not-found")
104 } else if serNum, ok = md["voltha_serial_number"]; !ok {
105 err = errors.New("serial-number-not-found")
106 }
khenaidoo2c6a0992019-04-29 13:46:56 -0400107 if !ok || serNum == nil {
Richard Jankowskid42826e2018-11-02 16:06:37 -0400108 log.Error(err)
109 return nil, err
110 }
111 // Create KV transaction
112 txn := NewKVTransaction(serNum[0])
113 return txn, nil
114}
115
Richard Jankowski2755adf2019-01-17 17:16:48 -0500116// isOFControllerRequest is a helper function to determine if a request was initiated
117// from the OpenFlow controller (or its proxy, e.g. OFAgent)
Richard Jankowski46464e92019-03-05 11:53:55 -0500118func (handler *APIHandler) isOFControllerRequest(ctx context.Context) bool {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500119 if md, ok := metadata.FromIncomingContext(ctx); ok {
120 // Metadata in context
Richard Jankowski46464e92019-03-05 11:53:55 -0500121 if _, ok = md[handler.core.config.CoreBindingKey]; ok {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500122 // OFAgent field in metadata
khenaidoo3d3b8c22019-05-22 18:10:39 -0400123 log.Debug("OFController-request")
khenaidoo9cdc1a62019-01-24 21:57:40 -0500124 return true
125 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500126 }
khenaidoo3d3b8c22019-05-22 18:10:39 -0400127 log.Debug("not-OFController-request")
khenaidoo9cdc1a62019-01-24 21:57:40 -0500128 return false
129}
130
131// competeForTransaction is a helper function to determine whether every request needs to compete with another
132// Core to execute the request
133func (handler *APIHandler) competeForTransaction() bool {
134 return handler.coreInCompetingMode
135}
136
khenaidoo09771ef2019-10-11 14:25:02 -0400137// takeRequestOwnership creates a transaction in the dB for this request and handles the logic of transaction
138// acquisition. If the device is owned by this Core (in a core-pair) then acquire the transaction with a
139// timeout value (in the event this Core dies the transaction times out in the dB causing the other Core in the
140// core-pair to proceed with the it). If the device is not owned then this Core will just monitor the transaction
141// for potential timeouts.
khenaidoo442e7c72020-03-10 16:13:48 -0400142func (handler *APIHandler) takeRequestOwnership(ctx context.Context, id interface{}, maxTimeout ...time.Duration) (*KVTransaction, error) {
khenaidoo43aa6bd2019-05-29 13:35:13 -0400143 timeout := handler.defaultRequestTimeout
144 if len(maxTimeout) > 0 {
145 timeout = maxTimeout[0]
146 }
khenaidoo43aa6bd2019-05-29 13:35:13 -0400147 txn, err := handler.createKvTransaction(ctx)
148 if txn == nil {
149 return nil, err
khenaidoo09771ef2019-10-11 14:25:02 -0400150 }
151 var acquired bool
152 if id != nil {
153 var ownedByMe bool
npujar467fe752020-01-16 20:17:45 +0530154 if ownedByMe, err = handler.core.deviceOwnership.OwnedByMe(ctx, id); err != nil {
khenaidoo09771ef2019-10-11 14:25:02 -0400155 log.Warnw("getting-ownership-failed", log.Fields{"deviceId": id, "error": err})
npujar1d86a522019-11-14 17:11:16 +0530156 return nil, errorIDNotFound
khenaidoo09771ef2019-10-11 14:25:02 -0400157 }
khenaidoo442e7c72020-03-10 16:13:48 -0400158 acquired, err = txn.Acquired(ctx, timeout.Milliseconds(), ownedByMe)
khenaidoo09771ef2019-10-11 14:25:02 -0400159 } else {
khenaidoo442e7c72020-03-10 16:13:48 -0400160 acquired, err = txn.Acquired(ctx, timeout.Milliseconds())
khenaidoo09771ef2019-10-11 14:25:02 -0400161 }
162 if err == nil && acquired {
npujar1d86a522019-11-14 17:11:16 +0530163 log.Debugw("transaction-acquired", log.Fields{"transactionId": txn.txnID})
khenaidoo43aa6bd2019-05-29 13:35:13 -0400164 return txn, nil
khenaidoo43aa6bd2019-05-29 13:35:13 -0400165 }
npujar1d86a522019-11-14 17:11:16 +0530166 log.Debugw("transaction-not-acquired", log.Fields{"transactionId": txn.txnID, "error": err})
167 return nil, errorTransactionNotAcquired
khenaidoo43aa6bd2019-05-29 13:35:13 -0400168}
169
khenaidoo09771ef2019-10-11 14:25:02 -0400170// waitForNilResponseOnSuccess is a helper function to wait for a response on channel monitorCh where an nil
khenaidoo4d4802d2018-10-04 21:59:49 -0400171// response is expected in a successful scenario
172func waitForNilResponseOnSuccess(ctx context.Context, ch chan interface{}) (*empty.Empty, error) {
173 select {
174 case res := <-ch:
175 if res == nil {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500176 return &empty.Empty{}, nil
khenaidoo4d4802d2018-10-04 21:59:49 -0400177 } else if err, ok := res.(error); ok {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500178 return &empty.Empty{}, err
khenaidoo4d4802d2018-10-04 21:59:49 -0400179 } else {
180 log.Warnw("unexpected-return-type", log.Fields{"result": res})
181 err = status.Errorf(codes.Internal, "%s", res)
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500182 return &empty.Empty{}, err
khenaidoo4d4802d2018-10-04 21:59:49 -0400183 }
184 case <-ctx.Done():
185 log.Debug("client-timeout")
186 return nil, ctx.Err()
187 }
188}
189
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500190// ListCoreInstances returns details on the running core containers
191func (handler *APIHandler) ListCoreInstances(ctx context.Context, empty *empty.Empty) (*voltha.CoreInstances, error) {
192 log.Debug("ListCoreInstances")
193 // TODO: unused stub
194 return &voltha.CoreInstances{}, status.Errorf(codes.NotFound, "no-core-instances")
195}
196
197// GetCoreInstance returns the details of a specific core container
198func (handler *APIHandler) GetCoreInstance(ctx context.Context, id *voltha.ID) (*voltha.CoreInstance, error) {
199 log.Debugw("GetCoreInstance", log.Fields{"id": id})
200 //TODO: unused stub
201 return &voltha.CoreInstance{}, status.Errorf(codes.NotFound, "core-instance-%s", id.Id)
202}
203
npujar1d86a522019-11-14 17:11:16 +0530204// GetLogicalDevicePort returns logical device port details
khenaidoo43aa6bd2019-05-29 13:35:13 -0400205func (handler *APIHandler) GetLogicalDevicePort(ctx context.Context, id *voltha.LogicalPortId) (*voltha.LogicalPort, error) {
206 log.Debugw("GetLogicalDevicePort-request", log.Fields{"id": *id})
207
208 if handler.competeForTransaction() {
npujar1d86a522019-11-14 17:11:16 +0530209 txn, err := handler.takeRequestOwnership(ctx, &utils.LogicalDeviceID{ID: id.Id})
210 if err != nil {
khenaidoo43aa6bd2019-05-29 13:35:13 -0400211 return &voltha.LogicalPort{}, err
khenaidoo43aa6bd2019-05-29 13:35:13 -0400212 }
npujar467fe752020-01-16 20:17:45 +0530213 defer txn.Close(ctx)
khenaidoo43aa6bd2019-05-29 13:35:13 -0400214 }
npujar467fe752020-01-16 20:17:45 +0530215 return handler.logicalDeviceMgr.getLogicalPort(ctx, id)
khenaidoo43aa6bd2019-05-29 13:35:13 -0400216}
217
npujar1d86a522019-11-14 17:11:16 +0530218// EnableLogicalDevicePort enables logical device port
khenaidoobf6e7bb2018-08-14 22:27:29 -0400219func (handler *APIHandler) EnableLogicalDevicePort(ctx context.Context, id *voltha.LogicalPortId) (*empty.Empty, error) {
220 log.Debugw("EnableLogicalDevicePort-request", log.Fields{"id": id, "test": common.TestModeKeys_api_test.String()})
221 if isTestMode(ctx) {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500222 return &empty.Empty{}, nil
khenaidoobf6e7bb2018-08-14 22:27:29 -0400223 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500224
khenaidoo9cdc1a62019-01-24 21:57:40 -0500225 if handler.competeForTransaction() {
npujar1d86a522019-11-14 17:11:16 +0530226 txn, err := handler.takeRequestOwnership(ctx, &utils.LogicalDeviceID{ID: id.Id})
227 if err != nil {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500228 return &empty.Empty{}, err
Richard Jankowski2755adf2019-01-17 17:16:48 -0500229 }
npujar467fe752020-01-16 20:17:45 +0530230 defer txn.Close(ctx)
Richard Jankowski2755adf2019-01-17 17:16:48 -0500231 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500232
khenaidoo4d4802d2018-10-04 21:59:49 -0400233 ch := make(chan interface{})
234 defer close(ch)
khenaidoo19d7b632018-10-30 10:49:50 -0400235 go handler.logicalDeviceMgr.enableLogicalPort(ctx, id, ch)
khenaidoo4d4802d2018-10-04 21:59:49 -0400236 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400237}
238
npujar1d86a522019-11-14 17:11:16 +0530239// DisableLogicalDevicePort disables logical device port
khenaidoobf6e7bb2018-08-14 22:27:29 -0400240func (handler *APIHandler) DisableLogicalDevicePort(ctx context.Context, id *voltha.LogicalPortId) (*empty.Empty, error) {
241 log.Debugw("DisableLogicalDevicePort-request", log.Fields{"id": id, "test": common.TestModeKeys_api_test.String()})
242 if isTestMode(ctx) {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500243 return &empty.Empty{}, nil
khenaidoobf6e7bb2018-08-14 22:27:29 -0400244 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500245
khenaidoo9cdc1a62019-01-24 21:57:40 -0500246 if handler.competeForTransaction() {
npujar1d86a522019-11-14 17:11:16 +0530247 txn, err := handler.takeRequestOwnership(ctx, &utils.LogicalDeviceID{ID: id.Id})
248 if err != nil {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500249 return &empty.Empty{}, err
Richard Jankowski2755adf2019-01-17 17:16:48 -0500250 }
npujar467fe752020-01-16 20:17:45 +0530251 defer txn.Close(ctx)
Richard Jankowski2755adf2019-01-17 17:16:48 -0500252 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500253
khenaidoo19d7b632018-10-30 10:49:50 -0400254 ch := make(chan interface{})
255 defer close(ch)
256 go handler.logicalDeviceMgr.disableLogicalPort(ctx, id, ch)
257 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400258}
259
npujar1d86a522019-11-14 17:11:16 +0530260// UpdateLogicalDeviceFlowTable updates logical device flow table
khenaidoobf6e7bb2018-08-14 22:27:29 -0400261func (handler *APIHandler) UpdateLogicalDeviceFlowTable(ctx context.Context, flow *openflow_13.FlowTableUpdate) (*empty.Empty, error) {
262 log.Debugw("UpdateLogicalDeviceFlowTable-request", log.Fields{"flow": flow, "test": common.TestModeKeys_api_test.String()})
263 if isTestMode(ctx) {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500264 return &empty.Empty{}, nil
khenaidoobf6e7bb2018-08-14 22:27:29 -0400265 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500266
khenaidoo9cdc1a62019-01-24 21:57:40 -0500267 if handler.competeForTransaction() {
npujar1d86a522019-11-14 17:11:16 +0530268 txn, err := handler.takeRequestOwnership(ctx, &utils.LogicalDeviceID{ID: flow.Id})
269 if err != nil {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500270 return &empty.Empty{}, err
Richard Jankowski2755adf2019-01-17 17:16:48 -0500271 }
npujar467fe752020-01-16 20:17:45 +0530272 defer txn.Close(ctx)
Richard Jankowski2755adf2019-01-17 17:16:48 -0500273 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500274
khenaidoo19d7b632018-10-30 10:49:50 -0400275 ch := make(chan interface{})
276 defer close(ch)
277 go handler.logicalDeviceMgr.updateFlowTable(ctx, flow.Id, flow.FlowMod, ch)
278 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400279}
280
npujar1d86a522019-11-14 17:11:16 +0530281// UpdateLogicalDeviceFlowGroupTable updates logical device flow group table
khenaidoobf6e7bb2018-08-14 22:27:29 -0400282func (handler *APIHandler) UpdateLogicalDeviceFlowGroupTable(ctx context.Context, flow *openflow_13.FlowGroupTableUpdate) (*empty.Empty, error) {
283 log.Debugw("UpdateLogicalDeviceFlowGroupTable-request", log.Fields{"flow": flow, "test": common.TestModeKeys_api_test.String()})
284 if isTestMode(ctx) {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500285 return &empty.Empty{}, nil
khenaidoobf6e7bb2018-08-14 22:27:29 -0400286 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500287
khenaidoo9cdc1a62019-01-24 21:57:40 -0500288 if handler.competeForTransaction() {
npujar1d86a522019-11-14 17:11:16 +0530289 txn, err := handler.takeRequestOwnership(ctx, &utils.LogicalDeviceID{ID: flow.Id})
290 if err != nil {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500291 return &empty.Empty{}, err
Richard Jankowski2755adf2019-01-17 17:16:48 -0500292 }
npujar467fe752020-01-16 20:17:45 +0530293 defer txn.Close(ctx)
Richard Jankowski2755adf2019-01-17 17:16:48 -0500294 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500295
khenaidoo19d7b632018-10-30 10:49:50 -0400296 ch := make(chan interface{})
297 defer close(ch)
298 go handler.logicalDeviceMgr.updateGroupTable(ctx, flow.Id, flow.GroupMod, ch)
299 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400300}
301
khenaidoob9203542018-09-17 22:56:37 -0400302// GetDevice must be implemented in the read-only containers - should it also be implemented here?
303func (handler *APIHandler) GetDevice(ctx context.Context, id *voltha.ID) (*voltha.Device, error) {
304 log.Debugw("GetDevice-request", log.Fields{"id": id})
npujar467fe752020-01-16 20:17:45 +0530305 return handler.deviceMgr.GetDevice(ctx, id.Id)
khenaidoob9203542018-09-17 22:56:37 -0400306}
307
308// GetDevice must be implemented in the read-only containers - should it also be implemented here?
npujar1d86a522019-11-14 17:11:16 +0530309
310// ListDevices retrieves the latest devices from the data model
khenaidoob9203542018-09-17 22:56:37 -0400311func (handler *APIHandler) ListDevices(ctx context.Context, empty *empty.Empty) (*voltha.Devices, error) {
312 log.Debug("ListDevices")
npujar467fe752020-01-16 20:17:45 +0530313 devices, err := handler.deviceMgr.ListDevices(ctx)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530314 if err != nil {
315 log.Errorw("Failed to list devices", log.Fields{"error": err})
316 return nil, err
317 }
318 return devices, nil
khenaidoob9203542018-09-17 22:56:37 -0400319}
320
khenaidoo7ccedd52018-12-14 16:48:54 -0500321// ListDeviceIds returns the list of device ids managed by a voltha core
322func (handler *APIHandler) ListDeviceIds(ctx context.Context, empty *empty.Empty) (*voltha.IDs, error) {
323 log.Debug("ListDeviceIDs")
324 if isTestMode(ctx) {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500325 return &voltha.IDs{Items: make([]*voltha.ID, 0)}, nil
khenaidoo7ccedd52018-12-14 16:48:54 -0500326 }
327 return handler.deviceMgr.ListDeviceIds()
328}
329
330//ReconcileDevices is a request to a voltha core to managed a list of devices based on their IDs
331func (handler *APIHandler) ReconcileDevices(ctx context.Context, ids *voltha.IDs) (*empty.Empty, error) {
332 log.Debug("ReconcileDevices")
333 if isTestMode(ctx) {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500334 return &empty.Empty{}, nil
khenaidoo7ccedd52018-12-14 16:48:54 -0500335 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500336
khenaidoo9cdc1a62019-01-24 21:57:40 -0500337 // No need to grab a transaction as this request is core specific
338
khenaidoo7ccedd52018-12-14 16:48:54 -0500339 ch := make(chan interface{})
340 defer close(ch)
341 go handler.deviceMgr.ReconcileDevices(ctx, ids, ch)
342 return waitForNilResponseOnSuccess(ctx, ch)
343}
344
npujar1d86a522019-11-14 17:11:16 +0530345// GetLogicalDevice provides a cloned most up to date logical device
khenaidoob9203542018-09-17 22:56:37 -0400346func (handler *APIHandler) GetLogicalDevice(ctx context.Context, id *voltha.ID) (*voltha.LogicalDevice, error) {
347 log.Debugw("GetLogicalDevice-request", log.Fields{"id": id})
khenaidoo43aa6bd2019-05-29 13:35:13 -0400348 if handler.competeForTransaction() {
npujar1d86a522019-11-14 17:11:16 +0530349 txn, err := handler.takeRequestOwnership(ctx, &utils.LogicalDeviceID{ID: id.Id})
350 if err != nil {
khenaidoo43aa6bd2019-05-29 13:35:13 -0400351 return &voltha.LogicalDevice{}, err
khenaidoo43aa6bd2019-05-29 13:35:13 -0400352 }
npujar467fe752020-01-16 20:17:45 +0530353 defer txn.Close(ctx)
khenaidoo43aa6bd2019-05-29 13:35:13 -0400354 }
npujar467fe752020-01-16 20:17:45 +0530355 return handler.logicalDeviceMgr.getLogicalDevice(ctx, id.Id)
khenaidoob9203542018-09-17 22:56:37 -0400356}
357
npujar1d86a522019-11-14 17:11:16 +0530358// ListLogicalDevices returns the list of all logical devices
khenaidoob9203542018-09-17 22:56:37 -0400359func (handler *APIHandler) ListLogicalDevices(ctx context.Context, empty *empty.Empty) (*voltha.LogicalDevices, error) {
khenaidoo43aa6bd2019-05-29 13:35:13 -0400360 log.Debug("ListLogicalDevices-request")
361 if handler.competeForTransaction() {
npujar1d86a522019-11-14 17:11:16 +0530362 txn, err := handler.takeRequestOwnership(ctx, nil)
363 if err != nil {
khenaidoo43aa6bd2019-05-29 13:35:13 -0400364 return &voltha.LogicalDevices{}, err
khenaidoo43aa6bd2019-05-29 13:35:13 -0400365 }
npujar467fe752020-01-16 20:17:45 +0530366 defer txn.Close(ctx)
khenaidooa9cc6d02019-11-20 14:56:59 -0500367 if handler.isOFControllerRequest(ctx) {
368 // Since an OF controller is only interested in the set of logical devices managed by thgis Core then return
369 // only logical devices managed/monitored by this Core.
khenaidoo442e7c72020-03-10 16:13:48 -0400370 return handler.logicalDeviceMgr.listManagedLogicalDevices(ctx)
khenaidooa9cc6d02019-11-20 14:56:59 -0500371 }
khenaidoo43aa6bd2019-05-29 13:35:13 -0400372 }
npujar467fe752020-01-16 20:17:45 +0530373 return handler.logicalDeviceMgr.listLogicalDevices(ctx)
khenaidoob9203542018-09-17 22:56:37 -0400374}
375
khenaidoo21d51152019-02-01 13:48:37 -0500376// ListAdapters returns the contents of all adapters known to the system
377func (handler *APIHandler) ListAdapters(ctx context.Context, empty *empty.Empty) (*voltha.Adapters, error) {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500378 log.Debug("ListAdapters")
khenaidoo21d51152019-02-01 13:48:37 -0500379 return handler.adapterMgr.listAdapters(ctx)
380}
381
npujar1d86a522019-11-14 17:11:16 +0530382// ListLogicalDeviceFlows returns the flows of logical device
khenaidoodd237172019-05-27 16:37:17 -0400383func (handler *APIHandler) ListLogicalDeviceFlows(ctx context.Context, id *voltha.ID) (*openflow_13.Flows, error) {
384 log.Debugw("ListLogicalDeviceFlows", log.Fields{"id": *id})
khenaidoo43aa6bd2019-05-29 13:35:13 -0400385 if handler.competeForTransaction() {
npujar1d86a522019-11-14 17:11:16 +0530386 txn, err := handler.takeRequestOwnership(ctx, &utils.LogicalDeviceID{ID: id.Id})
387 if err != nil {
khenaidoo43aa6bd2019-05-29 13:35:13 -0400388 return &openflow_13.Flows{}, err
khenaidoo43aa6bd2019-05-29 13:35:13 -0400389 }
npujar467fe752020-01-16 20:17:45 +0530390 defer txn.Close(ctx)
khenaidoo43aa6bd2019-05-29 13:35:13 -0400391 }
khenaidoodd237172019-05-27 16:37:17 -0400392 return handler.logicalDeviceMgr.ListLogicalDeviceFlows(ctx, id.Id)
393}
394
npujar1d86a522019-11-14 17:11:16 +0530395// ListLogicalDeviceFlowGroups returns logical device flow groups
khenaidoodd237172019-05-27 16:37:17 -0400396func (handler *APIHandler) ListLogicalDeviceFlowGroups(ctx context.Context, id *voltha.ID) (*openflow_13.FlowGroups, error) {
397 log.Debugw("ListLogicalDeviceFlowGroups", log.Fields{"id": *id})
khenaidoo43aa6bd2019-05-29 13:35:13 -0400398 if handler.competeForTransaction() {
npujar1d86a522019-11-14 17:11:16 +0530399 txn, err := handler.takeRequestOwnership(ctx, &utils.LogicalDeviceID{ID: id.Id})
400 if err != nil {
khenaidoo43aa6bd2019-05-29 13:35:13 -0400401 return &openflow_13.FlowGroups{}, err
khenaidoo43aa6bd2019-05-29 13:35:13 -0400402 }
npujar467fe752020-01-16 20:17:45 +0530403 defer txn.Close(ctx)
khenaidoo43aa6bd2019-05-29 13:35:13 -0400404 }
khenaidoodd237172019-05-27 16:37:17 -0400405 return handler.logicalDeviceMgr.ListLogicalDeviceFlowGroups(ctx, id.Id)
406}
407
npujar1d86a522019-11-14 17:11:16 +0530408// ListLogicalDevicePorts returns ports of logical device
khenaidoo19d7b632018-10-30 10:49:50 -0400409func (handler *APIHandler) ListLogicalDevicePorts(ctx context.Context, id *voltha.ID) (*voltha.LogicalPorts, error) {
410 log.Debugw("ListLogicalDevicePorts", log.Fields{"logicaldeviceid": id})
khenaidoo43aa6bd2019-05-29 13:35:13 -0400411 if handler.competeForTransaction() {
npujar1d86a522019-11-14 17:11:16 +0530412 txn, err := handler.takeRequestOwnership(ctx, &utils.LogicalDeviceID{ID: id.Id})
413 if err != nil {
khenaidoo43aa6bd2019-05-29 13:35:13 -0400414 return &voltha.LogicalPorts{}, err
khenaidoo43aa6bd2019-05-29 13:35:13 -0400415 }
npujar467fe752020-01-16 20:17:45 +0530416 defer txn.Close(ctx)
khenaidoo43aa6bd2019-05-29 13:35:13 -0400417 }
khenaidoo19d7b632018-10-30 10:49:50 -0400418 return handler.logicalDeviceMgr.ListLogicalDevicePorts(ctx, id.Id)
419}
420
khenaidoo4d4802d2018-10-04 21:59:49 -0400421// CreateDevice creates a new parent device in the data model
khenaidoobf6e7bb2018-08-14 22:27:29 -0400422func (handler *APIHandler) CreateDevice(ctx context.Context, device *voltha.Device) (*voltha.Device, error) {
Thomas Lee S51b5cb82019-10-14 14:49:34 +0530423 if device.MacAddress == "" && device.GetHostAndPort() == "" {
424 log.Errorf("No Device Info Present")
Thomas Lee Se5a44012019-11-07 20:32:24 +0530425 return &voltha.Device{}, errors.New("no-device-info-present; MAC or HOSTIP&PORT")
Thomas Lee S51b5cb82019-10-14 14:49:34 +0530426 }
427 log.Debugw("create-device", log.Fields{"device": *device})
khenaidoobf6e7bb2018-08-14 22:27:29 -0400428 if isTestMode(ctx) {
429 return &voltha.Device{Id: device.Id}, nil
430 }
Richard Jankowskid42826e2018-11-02 16:06:37 -0400431
khenaidoo9cdc1a62019-01-24 21:57:40 -0500432 if handler.competeForTransaction() {
khenaidoo631fe542019-05-31 15:44:43 -0400433 // There are no device Id present in this function.
npujar1d86a522019-11-14 17:11:16 +0530434 txn, err := handler.takeRequestOwnership(ctx, nil)
435 if err != nil {
Richard Jankowski2755adf2019-01-17 17:16:48 -0500436 return &voltha.Device{}, err
Richard Jankowski2755adf2019-01-17 17:16:48 -0500437 }
npujar467fe752020-01-16 20:17:45 +0530438 defer txn.Close(ctx)
Richard Jankowski2755adf2019-01-17 17:16:48 -0500439 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500440
khenaidoob9203542018-09-17 22:56:37 -0400441 ch := make(chan interface{})
442 defer close(ch)
443 go handler.deviceMgr.createDevice(ctx, device, ch)
444 select {
445 case res := <-ch:
khenaidoo92e62c52018-10-03 14:02:54 -0400446 if res != nil {
447 if err, ok := res.(error); ok {
Thomas Lee Se5a44012019-11-07 20:32:24 +0530448 log.Errorw("create-device-failed", log.Fields{"error": err})
449 return nil, err
khenaidoo92e62c52018-10-03 14:02:54 -0400450 }
451 if d, ok := res.(*voltha.Device); ok {
npujar467fe752020-01-16 20:17:45 +0530452 _, err := handler.core.deviceOwnership.OwnedByMe(ctx, &utils.DeviceID{ID: d.Id})
npujar1d86a522019-11-14 17:11:16 +0530453 if err != nil {
454 log.Errorw("unable-to-find-core-instance-active-owns-this-device", log.Fields{"error": err})
455 }
khenaidoo92e62c52018-10-03 14:02:54 -0400456 return d, nil
457 }
khenaidoob9203542018-09-17 22:56:37 -0400458 }
khenaidoo92e62c52018-10-03 14:02:54 -0400459 log.Warnw("create-device-unexpected-return-type", log.Fields{"result": res})
460 err := status.Errorf(codes.Internal, "%s", res)
461 return &voltha.Device{}, err
khenaidoob9203542018-09-17 22:56:37 -0400462 case <-ctx.Done():
463 log.Debug("createdevice-client-timeout")
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500464 return &voltha.Device{}, ctx.Err()
khenaidoob9203542018-09-17 22:56:37 -0400465 }
khenaidoobf6e7bb2018-08-14 22:27:29 -0400466}
467
khenaidoo4d4802d2018-10-04 21:59:49 -0400468// EnableDevice activates a device by invoking the adopt_device API on the appropriate adapter
khenaidoobf6e7bb2018-08-14 22:27:29 -0400469func (handler *APIHandler) EnableDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) {
khenaidoob9203542018-09-17 22:56:37 -0400470 log.Debugw("enabledevice", log.Fields{"id": id})
khenaidoobf6e7bb2018-08-14 22:27:29 -0400471 if isTestMode(ctx) {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500472 return &empty.Empty{}, nil
khenaidoobf6e7bb2018-08-14 22:27:29 -0400473 }
Richard Jankowskid42826e2018-11-02 16:06:37 -0400474
khenaidoo9cdc1a62019-01-24 21:57:40 -0500475 if handler.competeForTransaction() {
npujar1d86a522019-11-14 17:11:16 +0530476 txn, err := handler.takeRequestOwnership(ctx, &utils.DeviceID{ID: id.Id}, handler.longRunningRequestTimeout)
477 if err != nil {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500478 return &empty.Empty{}, err
Richard Jankowski2755adf2019-01-17 17:16:48 -0500479 }
npujar467fe752020-01-16 20:17:45 +0530480 defer txn.Close(ctx)
Richard Jankowski2755adf2019-01-17 17:16:48 -0500481 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500482
khenaidoob9203542018-09-17 22:56:37 -0400483 ch := make(chan interface{})
484 defer close(ch)
485 go handler.deviceMgr.enableDevice(ctx, id, ch)
khenaidoo4d4802d2018-10-04 21:59:49 -0400486 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400487}
488
khenaidoo4d4802d2018-10-04 21:59:49 -0400489// DisableDevice disables a device along with any child device it may have
khenaidoobf6e7bb2018-08-14 22:27:29 -0400490func (handler *APIHandler) DisableDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) {
491 log.Debugw("disabledevice-request", log.Fields{"id": id})
492 if isTestMode(ctx) {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500493 return &empty.Empty{}, nil
khenaidoobf6e7bb2018-08-14 22:27:29 -0400494 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500495
khenaidoo9cdc1a62019-01-24 21:57:40 -0500496 if handler.competeForTransaction() {
npujar1d86a522019-11-14 17:11:16 +0530497 txn, err := handler.takeRequestOwnership(ctx, &utils.DeviceID{ID: id.Id})
498 if err != nil {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500499 return &empty.Empty{}, err
Richard Jankowski2755adf2019-01-17 17:16:48 -0500500 }
npujar467fe752020-01-16 20:17:45 +0530501 defer txn.Close(ctx)
Richard Jankowski2755adf2019-01-17 17:16:48 -0500502 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500503
khenaidoo92e62c52018-10-03 14:02:54 -0400504 ch := make(chan interface{})
505 defer close(ch)
506 go handler.deviceMgr.disableDevice(ctx, id, ch)
khenaidoo4d4802d2018-10-04 21:59:49 -0400507 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400508}
509
khenaidoo4d4802d2018-10-04 21:59:49 -0400510//RebootDevice invoked the reboot API to the corresponding adapter
khenaidoobf6e7bb2018-08-14 22:27:29 -0400511func (handler *APIHandler) RebootDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) {
khenaidoo4d4802d2018-10-04 21:59:49 -0400512 log.Debugw("rebootDevice-request", log.Fields{"id": id})
khenaidoobf6e7bb2018-08-14 22:27:29 -0400513 if isTestMode(ctx) {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500514 return &empty.Empty{}, nil
khenaidoobf6e7bb2018-08-14 22:27:29 -0400515 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500516
khenaidoo9cdc1a62019-01-24 21:57:40 -0500517 if handler.competeForTransaction() {
npujar1d86a522019-11-14 17:11:16 +0530518 txn, err := handler.takeRequestOwnership(ctx, &utils.DeviceID{ID: id.Id})
519 if err != nil {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500520 return &empty.Empty{}, err
Richard Jankowski2755adf2019-01-17 17:16:48 -0500521 }
npujar467fe752020-01-16 20:17:45 +0530522 defer txn.Close(ctx)
Richard Jankowski2755adf2019-01-17 17:16:48 -0500523 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500524
khenaidoo4d4802d2018-10-04 21:59:49 -0400525 ch := make(chan interface{})
526 defer close(ch)
527 go handler.deviceMgr.rebootDevice(ctx, id, ch)
528 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400529}
530
khenaidoo4d4802d2018-10-04 21:59:49 -0400531// DeleteDevice removes a device from the data model
khenaidoobf6e7bb2018-08-14 22:27:29 -0400532func (handler *APIHandler) DeleteDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) {
533 log.Debugw("deletedevice-request", log.Fields{"id": id})
534 if isTestMode(ctx) {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500535 return &empty.Empty{}, nil
khenaidoobf6e7bb2018-08-14 22:27:29 -0400536 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500537
khenaidoo9cdc1a62019-01-24 21:57:40 -0500538 if handler.competeForTransaction() {
npujar1d86a522019-11-14 17:11:16 +0530539 txn, err := handler.takeRequestOwnership(ctx, &utils.DeviceID{ID: id.Id})
540 if err != nil {
khenaidoo09771ef2019-10-11 14:25:02 -0400541 if err == errorTransactionNotAcquired {
npujar467fe752020-01-16 20:17:45 +0530542 if ownedByMe, err := handler.core.deviceOwnership.OwnedByMe(ctx, &utils.DeviceID{ID: id.Id}); !ownedByMe && err == nil {
khenaidoo09771ef2019-10-11 14:25:02 -0400543 // Remove the device in memory
npujar467fe752020-01-16 20:17:45 +0530544 handler.deviceMgr.stopManagingDevice(ctx, id.Id)
khenaidoo09771ef2019-10-11 14:25:02 -0400545 }
khenaidoo6d62c002019-05-15 21:57:03 -0400546 }
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500547 return &empty.Empty{}, err
Richard Jankowski2755adf2019-01-17 17:16:48 -0500548 }
npujar467fe752020-01-16 20:17:45 +0530549 defer txn.Close(ctx)
Richard Jankowski2755adf2019-01-17 17:16:48 -0500550 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500551
khenaidoo4d4802d2018-10-04 21:59:49 -0400552 ch := make(chan interface{})
553 defer close(ch)
554 go handler.deviceMgr.deleteDevice(ctx, id, ch)
555 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400556}
557
David Bainbridge4087cc52019-11-13 18:36:03 +0000558// ListDevicePorts returns the ports details for a specific device entry
559func (handler *APIHandler) ListDevicePorts(ctx context.Context, id *voltha.ID) (*voltha.Ports, error) {
560 log.Debugw("listdeviceports-request", log.Fields{"id": id})
561 if handler.competeForTransaction() {
npujar1d86a522019-11-14 17:11:16 +0530562 txn, err := handler.takeRequestOwnership(ctx, &utils.DeviceID{ID: id.Id})
563 if err != nil {
David Bainbridge4087cc52019-11-13 18:36:03 +0000564 return &voltha.Ports{}, err
David Bainbridge4087cc52019-11-13 18:36:03 +0000565 }
npujar467fe752020-01-16 20:17:45 +0530566 defer txn.Close(ctx)
David Bainbridge4087cc52019-11-13 18:36:03 +0000567 }
568
npujar467fe752020-01-16 20:17:45 +0530569 device, err := handler.deviceMgr.GetDevice(ctx, id.Id)
David Bainbridge4087cc52019-11-13 18:36:03 +0000570 if err != nil {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500571 return &voltha.Ports{}, err
David Bainbridge4087cc52019-11-13 18:36:03 +0000572 }
573 ports := &voltha.Ports{}
npujar1d86a522019-11-14 17:11:16 +0530574 ports.Items = append(ports.Items, device.Ports...)
David Bainbridge4087cc52019-11-13 18:36:03 +0000575 return ports, nil
576}
577
578// ListDeviceFlows returns the flow details for a specific device entry
579func (handler *APIHandler) ListDeviceFlows(ctx context.Context, id *voltha.ID) (*openflow_13.Flows, error) {
580 log.Debugw("listdeviceflows-request", log.Fields{"id": id})
581 if handler.competeForTransaction() {
npujar1d86a522019-11-14 17:11:16 +0530582 txn, err := handler.takeRequestOwnership(ctx, &utils.DeviceID{ID: id.Id})
583 if err != nil {
David Bainbridge4087cc52019-11-13 18:36:03 +0000584 return &openflow_13.Flows{}, err
David Bainbridge4087cc52019-11-13 18:36:03 +0000585 }
npujar467fe752020-01-16 20:17:45 +0530586 defer txn.Close(ctx)
David Bainbridge4087cc52019-11-13 18:36:03 +0000587 }
588
npujar467fe752020-01-16 20:17:45 +0530589 device, err := handler.deviceMgr.GetDevice(ctx, id.Id)
David Bainbridge4087cc52019-11-13 18:36:03 +0000590 if err != nil {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500591 return &openflow_13.Flows{}, err
David Bainbridge4087cc52019-11-13 18:36:03 +0000592 }
593 flows := &openflow_13.Flows{}
npujar1d86a522019-11-14 17:11:16 +0530594 flows.Items = append(flows.Items, device.Flows.Items...)
David Bainbridge4087cc52019-11-13 18:36:03 +0000595 return flows, nil
596}
597
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500598// ListDeviceFlowGroups returns the flow group details for a specific device entry
599func (handler *APIHandler) ListDeviceFlowGroups(ctx context.Context, id *voltha.ID) (*voltha.FlowGroups, error) {
600 log.Debugw("ListDeviceFlowGroups", log.Fields{"deviceid": id})
601
npujar467fe752020-01-16 20:17:45 +0530602 if device, _ := handler.deviceMgr.GetDevice(ctx, id.Id); device != nil {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500603 return device.GetFlowGroups(), nil
604 }
605 return &voltha.FlowGroups{}, status.Errorf(codes.NotFound, "device-%s", id.Id)
606}
607
608// ListDeviceGroups returns all the device groups known to the system
609func (handler *APIHandler) ListDeviceGroups(ctx context.Context, empty *empty.Empty) (*voltha.DeviceGroups, error) {
610 log.Debug("ListDeviceGroups")
611 return &voltha.DeviceGroups{}, errors.New("UnImplemented")
612}
613
614// GetDeviceGroup returns a specific device group entry
615func (handler *APIHandler) GetDeviceGroup(ctx context.Context, id *voltha.ID) (*voltha.DeviceGroup, error) {
616 log.Debug("GetDeviceGroup")
617 return &voltha.DeviceGroup{}, errors.New("UnImplemented")
618}
619
620// ListDeviceTypes returns all the device types known to the system
621func (handler *APIHandler) ListDeviceTypes(ctx context.Context, _ *empty.Empty) (*voltha.DeviceTypes, error) {
622 log.Debug("ListDeviceTypes")
623
624 return &voltha.DeviceTypes{Items: handler.adapterMgr.listDeviceTypes()}, nil
625}
626
627// GetDeviceType returns the device type for a specific device entry
628func (handler *APIHandler) GetDeviceType(ctx context.Context, id *voltha.ID) (*voltha.DeviceType, error) {
629 log.Debugw("GetDeviceType", log.Fields{"typeid": id})
630
631 if deviceType := handler.adapterMgr.getDeviceType(id.Id); deviceType != nil {
632 return deviceType, nil
633 }
634 return &voltha.DeviceType{}, status.Errorf(codes.NotFound, "device_type-%s", id.Id)
635}
636
David Bainbridge4087cc52019-11-13 18:36:03 +0000637// GetVoltha returns the contents of all components (i.e. devices, logical_devices, ...)
638func (handler *APIHandler) GetVoltha(ctx context.Context, empty *empty.Empty) (*voltha.Voltha, error) {
639
640 log.Debug("GetVoltha")
641 /*
642 * For now, encode all the version information into a JSON object and
643 * pass that back as "version" so the client can get all the
644 * information associated with the version. Long term the API should
645 * better accomidate this, but for now this will work.
646 */
647 data, err := json.Marshal(&version.VersionInfo)
648 info := version.VersionInfo.Version
649 if err != nil {
650 log.Warnf("Unable to encode version information as JSON: %s", err.Error())
651 } else {
652 info = string(data)
653 }
654
655 return &voltha.Voltha{
656 Version: info,
657 }, nil
658}
659
khenaidoof5a5bfa2019-01-23 22:20:29 -0500660// processImageRequest is a helper method to execute an image download request
661func (handler *APIHandler) processImageRequest(ctx context.Context, img *voltha.ImageDownload, requestType int) (*common.OperationResp, error) {
662 log.Debugw("processImageDownload", log.Fields{"img": *img, "requestType": requestType})
663 if isTestMode(ctx) {
664 resp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS}
665 return resp, nil
666 }
667
khenaidoo9cdc1a62019-01-24 21:57:40 -0500668 if handler.competeForTransaction() {
npujar1d86a522019-11-14 17:11:16 +0530669 txn, err := handler.takeRequestOwnership(ctx, &utils.DeviceID{ID: img.Id})
670 if err != nil {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500671 return &common.OperationResp{}, err
khenaidoo9cdc1a62019-01-24 21:57:40 -0500672 }
npujar467fe752020-01-16 20:17:45 +0530673 defer txn.Close(ctx)
khenaidoof5a5bfa2019-01-23 22:20:29 -0500674 }
675
khenaidoo2c6a0992019-04-29 13:46:56 -0400676 failedresponse := &common.OperationResp{Code: voltha.OperationResp_OPERATION_FAILURE}
khenaidoof5a5bfa2019-01-23 22:20:29 -0500677
678 ch := make(chan interface{})
679 defer close(ch)
680 switch requestType {
npujar1d86a522019-11-14 17:11:16 +0530681 case ImageDownload:
khenaidoof5a5bfa2019-01-23 22:20:29 -0500682 go handler.deviceMgr.downloadImage(ctx, img, ch)
npujar1d86a522019-11-14 17:11:16 +0530683 case CancelImageDownload:
khenaidoof5a5bfa2019-01-23 22:20:29 -0500684 go handler.deviceMgr.cancelImageDownload(ctx, img, ch)
npujar1d86a522019-11-14 17:11:16 +0530685 case ActivateImage:
khenaidoof5a5bfa2019-01-23 22:20:29 -0500686 go handler.deviceMgr.activateImage(ctx, img, ch)
npujar1d86a522019-11-14 17:11:16 +0530687 case RevertImage:
khenaidoof5a5bfa2019-01-23 22:20:29 -0500688 go handler.deviceMgr.revertImage(ctx, img, ch)
689 default:
690 log.Warn("invalid-request-type", log.Fields{"requestType": requestType})
691 return failedresponse, status.Errorf(codes.InvalidArgument, "%d", requestType)
692 }
693 select {
694 case res := <-ch:
695 if res != nil {
696 if err, ok := res.(error); ok {
697 return failedresponse, err
698 }
699 if opResp, ok := res.(*common.OperationResp); ok {
700 return opResp, nil
701 }
702 }
703 log.Warnw("download-image-unexpected-return-type", log.Fields{"result": res})
704 return failedresponse, status.Errorf(codes.Internal, "%s", res)
705 case <-ctx.Done():
706 log.Debug("downloadImage-client-timeout")
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500707 return &common.OperationResp{}, ctx.Err()
khenaidoof5a5bfa2019-01-23 22:20:29 -0500708 }
709}
710
npujar1d86a522019-11-14 17:11:16 +0530711// DownloadImage execute an image download request
khenaidoobf6e7bb2018-08-14 22:27:29 -0400712func (handler *APIHandler) DownloadImage(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) {
713 log.Debugw("DownloadImage-request", log.Fields{"img": *img})
714 if isTestMode(ctx) {
715 resp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS}
716 return resp, nil
717 }
718
npujar1d86a522019-11-14 17:11:16 +0530719 return handler.processImageRequest(ctx, img, ImageDownload)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400720}
721
npujar1d86a522019-11-14 17:11:16 +0530722// CancelImageDownload cancels image download request
khenaidoobf6e7bb2018-08-14 22:27:29 -0400723func (handler *APIHandler) CancelImageDownload(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) {
khenaidoof5a5bfa2019-01-23 22:20:29 -0500724 log.Debugw("cancelImageDownload-request", log.Fields{"img": *img})
khenaidoobf6e7bb2018-08-14 22:27:29 -0400725 if isTestMode(ctx) {
726 resp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS}
727 return resp, nil
728 }
npujar1d86a522019-11-14 17:11:16 +0530729 return handler.processImageRequest(ctx, img, CancelImageDownload)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400730}
731
npujar1d86a522019-11-14 17:11:16 +0530732// ActivateImageUpdate activates image update request
khenaidoobf6e7bb2018-08-14 22:27:29 -0400733func (handler *APIHandler) ActivateImageUpdate(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) {
khenaidoof5a5bfa2019-01-23 22:20:29 -0500734 log.Debugw("activateImageUpdate-request", log.Fields{"img": *img})
khenaidoobf6e7bb2018-08-14 22:27:29 -0400735 if isTestMode(ctx) {
736 resp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS}
737 return resp, nil
738 }
khenaidoof5a5bfa2019-01-23 22:20:29 -0500739
npujar1d86a522019-11-14 17:11:16 +0530740 return handler.processImageRequest(ctx, img, ActivateImage)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400741}
742
npujar1d86a522019-11-14 17:11:16 +0530743// RevertImageUpdate reverts image update
khenaidoobf6e7bb2018-08-14 22:27:29 -0400744func (handler *APIHandler) RevertImageUpdate(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) {
khenaidoof5a5bfa2019-01-23 22:20:29 -0500745 log.Debugw("revertImageUpdate-request", log.Fields{"img": *img})
khenaidoobf6e7bb2018-08-14 22:27:29 -0400746 if isTestMode(ctx) {
747 resp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS}
748 return resp, nil
749 }
khenaidoof5a5bfa2019-01-23 22:20:29 -0500750
npujar1d86a522019-11-14 17:11:16 +0530751 return handler.processImageRequest(ctx, img, RevertImage)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400752}
753
npujar1d86a522019-11-14 17:11:16 +0530754// GetImageDownloadStatus returns status of image download
khenaidoof5a5bfa2019-01-23 22:20:29 -0500755func (handler *APIHandler) GetImageDownloadStatus(ctx context.Context, img *voltha.ImageDownload) (*voltha.ImageDownload, error) {
756 log.Debugw("getImageDownloadStatus-request", log.Fields{"img": *img})
757 if isTestMode(ctx) {
Stephane Barbariedf5479f2019-01-29 22:13:00 -0500758 resp := &voltha.ImageDownload{DownloadState: voltha.ImageDownload_DOWNLOAD_SUCCEEDED}
khenaidoof5a5bfa2019-01-23 22:20:29 -0500759 return resp, nil
760 }
761
Stephane Barbariedf5479f2019-01-29 22:13:00 -0500762 failedresponse := &voltha.ImageDownload{DownloadState: voltha.ImageDownload_DOWNLOAD_UNKNOWN}
khenaidoof5a5bfa2019-01-23 22:20:29 -0500763
khenaidoo9cdc1a62019-01-24 21:57:40 -0500764 if handler.competeForTransaction() {
npujar1d86a522019-11-14 17:11:16 +0530765 txn, err := handler.takeRequestOwnership(ctx, &utils.DeviceID{ID: img.Id})
766 if err != nil {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500767 return failedresponse, err
khenaidoo9cdc1a62019-01-24 21:57:40 -0500768 }
npujar467fe752020-01-16 20:17:45 +0530769 defer txn.Close(ctx)
khenaidoof5a5bfa2019-01-23 22:20:29 -0500770 }
771
772 ch := make(chan interface{})
773 defer close(ch)
774 go handler.deviceMgr.getImageDownloadStatus(ctx, img, ch)
775
776 select {
777 case res := <-ch:
778 if res != nil {
779 if err, ok := res.(error); ok {
780 return failedresponse, err
781 }
782 if downloadResp, ok := res.(*voltha.ImageDownload); ok {
783 return downloadResp, nil
784 }
785 }
786 log.Warnw("download-image-status", log.Fields{"result": res})
787 return failedresponse, status.Errorf(codes.Internal, "%s", res)
788 case <-ctx.Done():
789 log.Debug("downloadImage-client-timeout")
790 return failedresponse, ctx.Err()
791 }
792}
793
npujar1d86a522019-11-14 17:11:16 +0530794// GetImageDownload returns image download
khenaidoof5a5bfa2019-01-23 22:20:29 -0500795func (handler *APIHandler) GetImageDownload(ctx context.Context, img *voltha.ImageDownload) (*voltha.ImageDownload, error) {
796 log.Debugw("GetImageDownload-request", log.Fields{"img": *img})
797 if isTestMode(ctx) {
Stephane Barbariedf5479f2019-01-29 22:13:00 -0500798 resp := &voltha.ImageDownload{DownloadState: voltha.ImageDownload_DOWNLOAD_SUCCEEDED}
khenaidoof5a5bfa2019-01-23 22:20:29 -0500799 return resp, nil
800 }
801
npujar1d86a522019-11-14 17:11:16 +0530802 download, err := handler.deviceMgr.getImageDownload(ctx, img)
803 if err != nil {
Stephane Barbariedf5479f2019-01-29 22:13:00 -0500804 return &voltha.ImageDownload{DownloadState: voltha.ImageDownload_DOWNLOAD_UNKNOWN}, err
khenaidoof5a5bfa2019-01-23 22:20:29 -0500805 }
npujar1d86a522019-11-14 17:11:16 +0530806 return download, nil
khenaidoof5a5bfa2019-01-23 22:20:29 -0500807}
808
npujar1d86a522019-11-14 17:11:16 +0530809// ListImageDownloads returns image downloads
khenaidoof5a5bfa2019-01-23 22:20:29 -0500810func (handler *APIHandler) ListImageDownloads(ctx context.Context, id *voltha.ID) (*voltha.ImageDownloads, error) {
811 log.Debugw("ListImageDownloads-request", log.Fields{"deviceId": id.Id})
812 if isTestMode(ctx) {
khenaidoo2c6a0992019-04-29 13:46:56 -0400813 resp := &voltha.ImageDownloads{Items: []*voltha.ImageDownload{}}
khenaidoof5a5bfa2019-01-23 22:20:29 -0500814 return resp, nil
815 }
816
npujar1d86a522019-11-14 17:11:16 +0530817 downloads, err := handler.deviceMgr.listImageDownloads(ctx, id.Id)
818 if err != nil {
khenaidoof5a5bfa2019-01-23 22:20:29 -0500819 failedResp := &voltha.ImageDownloads{
khenaidoo2c6a0992019-04-29 13:46:56 -0400820 Items: []*voltha.ImageDownload{
821 {DownloadState: voltha.ImageDownload_DOWNLOAD_UNKNOWN},
822 },
khenaidoof5a5bfa2019-01-23 22:20:29 -0500823 }
824 return failedResp, err
khenaidoof5a5bfa2019-01-23 22:20:29 -0500825 }
npujar1d86a522019-11-14 17:11:16 +0530826 return downloads, nil
khenaidoof5a5bfa2019-01-23 22:20:29 -0500827}
828
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500829// GetImages returns all images for a specific device entry
830func (handler *APIHandler) GetImages(ctx context.Context, id *voltha.ID) (*voltha.Images, error) {
831 log.Debugw("GetImages", log.Fields{"deviceid": id.Id})
npujar467fe752020-01-16 20:17:45 +0530832 device, err := handler.deviceMgr.GetDevice(ctx, id.Id)
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500833 if err != nil {
834 return &voltha.Images{}, err
835 }
836 return device.GetImages(), nil
837}
838
npujar1d86a522019-11-14 17:11:16 +0530839// UpdateDevicePmConfigs updates the PM configs
khenaidoobf6e7bb2018-08-14 22:27:29 -0400840func (handler *APIHandler) UpdateDevicePmConfigs(ctx context.Context, configs *voltha.PmConfigs) (*empty.Empty, error) {
841 log.Debugw("UpdateDevicePmConfigs-request", log.Fields{"configs": *configs})
842 if isTestMode(ctx) {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500843 return &empty.Empty{}, nil
khenaidoobf6e7bb2018-08-14 22:27:29 -0400844 }
khenaidoob3127472019-07-24 21:04:55 -0400845 if handler.competeForTransaction() {
npujar1d86a522019-11-14 17:11:16 +0530846 txn, err := handler.takeRequestOwnership(ctx, &utils.DeviceID{ID: configs.Id})
847 if err != nil {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500848 return &empty.Empty{}, err
khenaidoob3127472019-07-24 21:04:55 -0400849 }
npujar467fe752020-01-16 20:17:45 +0530850 defer txn.Close(ctx)
khenaidoob3127472019-07-24 21:04:55 -0400851 }
852
853 ch := make(chan interface{})
854 defer close(ch)
855 go handler.deviceMgr.updatePmConfigs(ctx, configs, ch)
856 return waitForNilResponseOnSuccess(ctx, ch)
857}
858
npujar1d86a522019-11-14 17:11:16 +0530859// ListDevicePmConfigs returns pm configs of device
khenaidoob3127472019-07-24 21:04:55 -0400860func (handler *APIHandler) ListDevicePmConfigs(ctx context.Context, id *voltha.ID) (*voltha.PmConfigs, error) {
861 log.Debugw("ListDevicePmConfigs-request", log.Fields{"deviceId": *id})
862 if handler.competeForTransaction() {
npujar1d86a522019-11-14 17:11:16 +0530863 txn, err := handler.takeRequestOwnership(ctx, &utils.LogicalDeviceID{ID: id.Id})
864 if err != nil {
khenaidoob3127472019-07-24 21:04:55 -0400865 return &voltha.PmConfigs{}, err
khenaidoob3127472019-07-24 21:04:55 -0400866 }
npujar467fe752020-01-16 20:17:45 +0530867 defer txn.Close(ctx)
khenaidoob3127472019-07-24 21:04:55 -0400868 }
869 return handler.deviceMgr.listPmConfigs(ctx, id.Id)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400870}
871
Devmalya Paulc594bb32019-11-06 07:34:27 +0000872func (handler *APIHandler) CreateEventFilter(ctx context.Context, filter *voltha.EventFilter) (*voltha.EventFilter, error) {
873 log.Debugw("CreateEventFilter-request", log.Fields{"filter": *filter})
874 return nil, errors.New("UnImplemented")
khenaidoobf6e7bb2018-08-14 22:27:29 -0400875}
876
Devmalya Paulc594bb32019-11-06 07:34:27 +0000877func (handler *APIHandler) UpdateEventFilter(ctx context.Context, filter *voltha.EventFilter) (*voltha.EventFilter, error) {
878 log.Debugw("UpdateEventFilter-request", log.Fields{"filter": *filter})
879 return nil, errors.New("UnImplemented")
khenaidoobf6e7bb2018-08-14 22:27:29 -0400880}
881
Devmalya Paulc594bb32019-11-06 07:34:27 +0000882func (handler *APIHandler) DeleteEventFilter(ctx context.Context, filterInfo *voltha.EventFilter) (*empty.Empty, error) {
883 log.Debugw("DeleteEventFilter-request", log.Fields{"device-id": filterInfo.DeviceId, "filter-id": filterInfo.Id})
884 return nil, errors.New("UnImplemented")
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500885}
886
Devmalya Paulc594bb32019-11-06 07:34:27 +0000887// GetEventFilter returns all the filters present for a device
888func (handler *APIHandler) GetEventFilter(ctx context.Context, id *voltha.ID) (*voltha.EventFilters, error) {
889 log.Debugw("GetEventFilter-request", log.Fields{"device-id": id})
890 return nil, errors.New("UnImplemented")
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500891}
892
Devmalya Paulc594bb32019-11-06 07:34:27 +0000893// ListEventFilters returns all the filters known to the system
894func (handler *APIHandler) ListEventFilters(ctx context.Context, empty *empty.Empty) (*voltha.EventFilters, error) {
895 log.Debug("ListEventFilter-request")
896 return nil, errors.New("UnImplemented")
khenaidoobf6e7bb2018-08-14 22:27:29 -0400897}
898
899func (handler *APIHandler) SelfTest(ctx context.Context, id *voltha.ID) (*voltha.SelfTestResponse, error) {
900 log.Debugw("SelfTest-request", log.Fields{"id": id})
901 if isTestMode(ctx) {
902 resp := &voltha.SelfTestResponse{Result: voltha.SelfTestResponse_SUCCESS}
903 return resp, nil
904 }
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500905 return &voltha.SelfTestResponse{}, errors.New("UnImplemented")
khenaidoobf6e7bb2018-08-14 22:27:29 -0400906}
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500907
npujar467fe752020-01-16 20:17:45 +0530908func (handler *APIHandler) forwardPacketOut(ctx context.Context, packet *openflow_13.PacketOut) {
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500909 log.Debugw("forwardPacketOut-request", log.Fields{"packet": packet})
khenaidoo3d3b8c22019-05-22 18:10:39 -0400910 //TODO: Update this logic once the OF Controller (OFAgent in this case) can include a transaction Id in its
911 // request. For performance reason we can let both Cores in a Core-Pair forward the Packet to the adapters and
912 // let once of the shim layer (kafka proxy or adapter request handler filters out the duplicate packet)
npujar467fe752020-01-16 20:17:45 +0530913 if ownedByMe, err := handler.core.deviceOwnership.OwnedByMe(ctx, &utils.LogicalDeviceID{ID: packet.Id}); ownedByMe && err == nil {
914 if agent := handler.logicalDeviceMgr.getLogicalDeviceAgent(ctx, packet.Id); agent != nil {
915 agent.packetOut(ctx, packet.PacketOut)
khenaidoo93d5a3d2020-01-15 12:37:05 -0500916 } else {
917 log.Errorf("No logical device agent present", log.Fields{"logicaldeviceID": packet.Id})
918 }
khenaidoo3d3b8c22019-05-22 18:10:39 -0400919 }
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500920}
khenaidoo3d3b8c22019-05-22 18:10:39 -0400921
npujar1d86a522019-11-14 17:11:16 +0530922// StreamPacketsOut sends packets to adapter
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500923func (handler *APIHandler) StreamPacketsOut(packets voltha.VolthaService_StreamPacketsOutServer) error {
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500924 log.Debugw("StreamPacketsOut-request", log.Fields{"packets": packets})
khenaidoo5e250692019-08-30 14:46:21 -0400925loop:
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500926 for {
khenaidoo5e250692019-08-30 14:46:21 -0400927 select {
928 case <-packets.Context().Done():
929 log.Infow("StreamPacketsOut-context-done", log.Fields{"packets": packets, "error": packets.Context().Err()})
930 break loop
931 default:
932 }
933
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500934 packet, err := packets.Recv()
935
936 if err == io.EOF {
khenaidoo5e250692019-08-30 14:46:21 -0400937 log.Debugw("Received-EOF", log.Fields{"packets": packets})
938 break loop
939 }
940
941 if err != nil {
942 log.Errorw("Failed to receive packet out", log.Fields{"error": err})
943 continue
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500944 }
945
npujar467fe752020-01-16 20:17:45 +0530946 handler.forwardPacketOut(packets.Context(), packet)
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500947 }
948
949 log.Debugw("StreamPacketsOut-request-done", log.Fields{"packets": packets})
950 return nil
951}
952
npujar1d86a522019-11-14 17:11:16 +0530953func (handler *APIHandler) sendPacketIn(deviceID string, transationID string, packet *openflow_13.OfpPacketIn) {
khenaidoo297cd252019-02-07 22:10:23 -0500954 // TODO: Augment the OF PacketIn to include the transactionId
npujar1d86a522019-11-14 17:11:16 +0530955 packetIn := openflow_13.PacketIn{Id: deviceID, PacketIn: packet}
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500956 log.Debugw("sendPacketIn", log.Fields{"packetIn": packetIn})
A R Karthick881e7ea2019-08-19 19:44:02 +0000957 handler.packetInQueue <- packetIn
958}
959
960type callTracker struct {
961 failedPacket interface{}
962}
963type streamTracker struct {
964 calls map[string]*callTracker
965 sync.Mutex
966}
967
968var streamingTracker = &streamTracker{calls: make(map[string]*callTracker)}
969
970func (handler *APIHandler) getStreamingTracker(method string, done chan<- bool) *callTracker {
971 streamingTracker.Lock()
972 defer streamingTracker.Unlock()
973 if _, ok := streamingTracker.calls[method]; ok {
974 // bail out the other packet in thread
975 log.Debugf("%s streaming call already running. Exiting it", method)
976 done <- true
977 log.Debugf("Last %s exited. Continuing ...", method)
978 } else {
979 streamingTracker.calls[method] = &callTracker{failedPacket: nil}
Richard Jankowskidbab94a2018-12-06 16:20:25 -0500980 }
A R Karthick881e7ea2019-08-19 19:44:02 +0000981 return streamingTracker.calls[method]
982}
983
984func (handler *APIHandler) flushFailedPackets(tracker *callTracker) error {
985 if tracker.failedPacket != nil {
986 switch tracker.failedPacket.(type) {
987 case openflow_13.PacketIn:
988 log.Debug("Enqueueing last failed packetIn")
989 handler.packetInQueue <- tracker.failedPacket.(openflow_13.PacketIn)
990 case openflow_13.ChangeEvent:
991 log.Debug("Enqueueing last failed changeEvent")
992 handler.changeEventQueue <- tracker.failedPacket.(openflow_13.ChangeEvent)
993 }
994 }
995 return nil
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500996}
997
npujar1d86a522019-11-14 17:11:16 +0530998// ReceivePacketsIn receives packets from adapter
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500999func (handler *APIHandler) ReceivePacketsIn(empty *empty.Empty, packetsIn voltha.VolthaService_ReceivePacketsInServer) error {
A R Karthick881e7ea2019-08-19 19:44:02 +00001000 var streamingTracker = handler.getStreamingTracker("ReceivePacketsIn", handler.packetInQueueDone)
Stephane Barbarie6e1bd502018-11-05 22:44:45 -05001001 log.Debugw("ReceivePacketsIn-request", log.Fields{"packetsIn": packetsIn})
1002
npujar1d86a522019-11-14 17:11:16 +05301003 err := handler.flushFailedPackets(streamingTracker)
1004 if err != nil {
1005 log.Errorw("unable-to-flush-failed-packets", log.Fields{"error": err})
1006 }
A R Karthick881e7ea2019-08-19 19:44:02 +00001007
1008loop:
Stephane Barbarie6e1bd502018-11-05 22:44:45 -05001009 for {
A R Karthick881e7ea2019-08-19 19:44:02 +00001010 select {
1011 case packet := <-handler.packetInQueue:
Matteo Scandolo360605d2019-11-05 18:29:17 -08001012 log.Debugw("sending-packet-in", log.Fields{
1013 "packet": hex.EncodeToString(packet.PacketIn.Data),
1014 })
A R Karthick881e7ea2019-08-19 19:44:02 +00001015 if err := packetsIn.Send(&packet); err != nil {
1016 log.Errorw("failed-to-send-packet", log.Fields{"error": err})
1017 // save the last failed packet in
1018 streamingTracker.failedPacket = packet
1019 } else {
1020 if streamingTracker.failedPacket != nil {
1021 // reset last failed packet saved to avoid flush
1022 streamingTracker.failedPacket = nil
Richard Jankowskidbab94a2018-12-06 16:20:25 -05001023 }
1024 }
A R Karthick881e7ea2019-08-19 19:44:02 +00001025 case <-handler.packetInQueueDone:
1026 log.Debug("Another ReceivePacketsIn running. Bailing out ...")
1027 break loop
Stephane Barbarie6e1bd502018-11-05 22:44:45 -05001028 }
1029 }
A R Karthick881e7ea2019-08-19 19:44:02 +00001030
1031 //TODO: Find an elegant way to get out of the above loop when the Core is stopped
1032 return nil
Stephane Barbarie6e1bd502018-11-05 22:44:45 -05001033}
1034
npujar1d86a522019-11-14 17:11:16 +05301035func (handler *APIHandler) sendChangeEvent(deviceID string, portStatus *openflow_13.OfpPortStatus) {
Stephane Barbarie6e1bd502018-11-05 22:44:45 -05001036 // TODO: validate the type of portStatus parameter
1037 //if _, ok := portStatus.(*openflow_13.OfpPortStatus); ok {
1038 //}
npujar1d86a522019-11-14 17:11:16 +05301039 event := openflow_13.ChangeEvent{Id: deviceID, Event: &openflow_13.ChangeEvent_PortStatus{PortStatus: portStatus}}
Stephane Barbarie6e1bd502018-11-05 22:44:45 -05001040 log.Debugw("sendChangeEvent", log.Fields{"event": event})
A R Karthick881e7ea2019-08-19 19:44:02 +00001041 handler.changeEventQueue <- event
Stephane Barbarie6e1bd502018-11-05 22:44:45 -05001042}
1043
npujar1d86a522019-11-14 17:11:16 +05301044// ReceiveChangeEvents receives change in events
Kent Hagermanc2c73ff2019-11-20 16:22:32 -05001045func (handler *APIHandler) ReceiveChangeEvents(empty *empty.Empty, changeEvents voltha.VolthaService_ReceiveChangeEventsServer) error {
A R Karthick881e7ea2019-08-19 19:44:02 +00001046 var streamingTracker = handler.getStreamingTracker("ReceiveChangeEvents", handler.changeEventQueueDone)
Stephane Barbarie6e1bd502018-11-05 22:44:45 -05001047 log.Debugw("ReceiveChangeEvents-request", log.Fields{"changeEvents": changeEvents})
A R Karthick881e7ea2019-08-19 19:44:02 +00001048
npujar1d86a522019-11-14 17:11:16 +05301049 err := handler.flushFailedPackets(streamingTracker)
1050 if err != nil {
1051 log.Errorw("unable-to-flush-failed-packets", log.Fields{"error": err})
1052 }
A R Karthick881e7ea2019-08-19 19:44:02 +00001053
1054loop:
Stephane Barbarie6e1bd502018-11-05 22:44:45 -05001055 for {
A R Karthick881e7ea2019-08-19 19:44:02 +00001056 select {
Richard Jankowski199fd862019-03-18 14:49:51 -04001057 // Dequeue a change event
A R Karthick881e7ea2019-08-19 19:44:02 +00001058 case event := <-handler.changeEventQueue:
1059 log.Debugw("sending-change-event", log.Fields{"event": event})
1060 if err := changeEvents.Send(&event); err != nil {
1061 log.Errorw("failed-to-send-change-event", log.Fields{"error": err})
1062 // save last failed changeevent
1063 streamingTracker.failedPacket = event
1064 } else {
1065 if streamingTracker.failedPacket != nil {
1066 // reset last failed event saved on success to avoid flushing
1067 streamingTracker.failedPacket = nil
Richard Jankowski199fd862019-03-18 14:49:51 -04001068 }
1069 }
A R Karthick881e7ea2019-08-19 19:44:02 +00001070 case <-handler.changeEventQueueDone:
1071 log.Debug("Another ReceiveChangeEvents already running. Bailing out ...")
1072 break loop
Stephane Barbarie6e1bd502018-11-05 22:44:45 -05001073 }
1074 }
A R Karthick881e7ea2019-08-19 19:44:02 +00001075
1076 return nil
Richard Jankowski199fd862019-03-18 14:49:51 -04001077}
Stephane Barbarie6e1bd502018-11-05 22:44:45 -05001078
npujar1d86a522019-11-14 17:11:16 +05301079// Subscribe subscribing request of ofagent
Stephane Barbarie6e1bd502018-11-05 22:44:45 -05001080func (handler *APIHandler) Subscribe(
1081 ctx context.Context,
1082 ofAgent *voltha.OfAgentSubscriber,
1083) (*voltha.OfAgentSubscriber, error) {
1084 log.Debugw("Subscribe-request", log.Fields{"ofAgent": ofAgent})
1085 return &voltha.OfAgentSubscriber{OfagentId: ofAgent.OfagentId, VolthaId: ofAgent.VolthaId}, nil
1086}
William Kurkiandaa6bb22019-03-07 12:26:28 -05001087
npujar1d86a522019-11-14 17:11:16 +05301088// GetAlarmDeviceData @TODO useless stub, what should this actually do?
Kent Hagermanc2c73ff2019-11-20 16:22:32 -05001089func (handler *APIHandler) GetAlarmDeviceData(ctx context.Context, in *common.ID) (*omci.AlarmDeviceData, error) {
William Kurkiandaa6bb22019-03-07 12:26:28 -05001090 log.Debug("GetAlarmDeviceData-stub")
Kent Hagermanc2c73ff2019-11-20 16:22:32 -05001091 return &omci.AlarmDeviceData{}, errors.New("UnImplemented")
William Kurkiandaa6bb22019-03-07 12:26:28 -05001092}
1093
npujar1d86a522019-11-14 17:11:16 +05301094// ListLogicalDeviceMeters returns logical device meters
Manikkaraj kb1a10922019-07-29 12:10:34 -04001095func (handler *APIHandler) ListLogicalDeviceMeters(ctx context.Context, id *voltha.ID) (*openflow_13.Meters, error) {
1096
1097 log.Debugw("ListLogicalDeviceMeters", log.Fields{"id": *id})
1098 if handler.competeForTransaction() {
npujar1d86a522019-11-14 17:11:16 +05301099 txn, err := handler.takeRequestOwnership(ctx, &utils.LogicalDeviceID{ID: id.Id})
1100 if err != nil {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -05001101 return &openflow_13.Meters{}, err // TODO: Return empty meter entry
Manikkaraj kb1a10922019-07-29 12:10:34 -04001102 }
npujar467fe752020-01-16 20:17:45 +05301103 defer txn.Close(ctx)
Manikkaraj kb1a10922019-07-29 12:10:34 -04001104 }
1105 return handler.logicalDeviceMgr.ListLogicalDeviceMeters(ctx, id.Id)
William Kurkiandaa6bb22019-03-07 12:26:28 -05001106}
1107
npujar1d86a522019-11-14 17:11:16 +05301108// GetMeterStatsOfLogicalDevice @TODO useless stub, what should this actually do?
Kent Hagermanc2c73ff2019-11-20 16:22:32 -05001109func (handler *APIHandler) GetMeterStatsOfLogicalDevice(ctx context.Context, in *common.ID) (*openflow_13.MeterStatsReply, error) {
1110 log.Debug("GetMeterStatsOfLogicalDevice")
1111 return &openflow_13.MeterStatsReply{}, errors.New("UnImplemented")
1112}
1113
npujar1d86a522019-11-14 17:11:16 +05301114// GetMibDeviceData @TODO useless stub, what should this actually do?
Kent Hagermanc2c73ff2019-11-20 16:22:32 -05001115func (handler *APIHandler) GetMibDeviceData(ctx context.Context, in *common.ID) (*omci.MibDeviceData, error) {
1116 log.Debug("GetMibDeviceData")
1117 return &omci.MibDeviceData{}, errors.New("UnImplemented")
William Kurkiandaa6bb22019-03-07 12:26:28 -05001118}
1119
npujar1d86a522019-11-14 17:11:16 +05301120// SimulateAlarm sends simulate alarm request
William Kurkiandaa6bb22019-03-07 12:26:28 -05001121func (handler *APIHandler) SimulateAlarm(
1122 ctx context.Context,
1123 in *voltha.SimulateAlarmRequest,
1124) (*common.OperationResp, error) {
serkant.uluderya334479d2019-04-10 08:26:15 -07001125 log.Debugw("SimulateAlarm-request", log.Fields{"id": in.Id})
1126 successResp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS}
1127 if isTestMode(ctx) {
1128 return successResp, nil
1129 }
1130
1131 if handler.competeForTransaction() {
npujar1d86a522019-11-14 17:11:16 +05301132 txn, err := handler.takeRequestOwnership(ctx, &utils.DeviceID{ID: in.Id}, handler.longRunningRequestTimeout)
1133 if err != nil {
Kent Hagerman0ab4cb22019-04-24 13:13:35 -04001134 failedresponse := &common.OperationResp{Code: voltha.OperationResp_OPERATION_FAILURE}
serkant.uluderya334479d2019-04-10 08:26:15 -07001135 return failedresponse, err
serkant.uluderya334479d2019-04-10 08:26:15 -07001136 }
npujar467fe752020-01-16 20:17:45 +05301137 defer txn.Close(ctx)
serkant.uluderya334479d2019-04-10 08:26:15 -07001138 }
1139
1140 ch := make(chan interface{})
1141 defer close(ch)
1142 go handler.deviceMgr.simulateAlarm(ctx, in, ch)
1143 return successResp, nil
William Kurkiandaa6bb22019-03-07 12:26:28 -05001144}
1145
npujar1d86a522019-11-14 17:11:16 +05301146// UpdateLogicalDeviceMeterTable - This function sends meter mod request to logical device manager and waits for response
Manikkaraj kb1a10922019-07-29 12:10:34 -04001147func (handler *APIHandler) UpdateLogicalDeviceMeterTable(ctx context.Context, meter *openflow_13.MeterModUpdate) (*empty.Empty, error) {
1148 log.Debugw("UpdateLogicalDeviceMeterTable-request",
1149 log.Fields{"meter": meter, "test": common.TestModeKeys_api_test.String()})
1150 if isTestMode(ctx) {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -05001151 return &empty.Empty{}, nil
Manikkaraj kb1a10922019-07-29 12:10:34 -04001152 }
1153
1154 if handler.competeForTransaction() {
npujar1d86a522019-11-14 17:11:16 +05301155 txn, err := handler.takeRequestOwnership(ctx, &utils.LogicalDeviceID{ID: meter.Id})
1156 if err != nil {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -05001157 return &empty.Empty{}, err
Manikkaraj kb1a10922019-07-29 12:10:34 -04001158 }
npujar467fe752020-01-16 20:17:45 +05301159 defer txn.Close(ctx)
Manikkaraj kb1a10922019-07-29 12:10:34 -04001160 }
1161
1162 ch := make(chan interface{})
1163 defer close(ch)
1164 go handler.logicalDeviceMgr.updateMeterTable(ctx, meter.Id, meter.MeterMod, ch)
1165 return waitForNilResponseOnSuccess(ctx, ch)
William Kurkiandaa6bb22019-03-07 12:26:28 -05001166}
Kent Hagermanc2c73ff2019-11-20 16:22:32 -05001167
npujar1d86a522019-11-14 17:11:16 +05301168// GetMembership returns membership
Kent Hagermanc2c73ff2019-11-20 16:22:32 -05001169func (handler *APIHandler) GetMembership(context.Context, *empty.Empty) (*voltha.Membership, error) {
1170 return &voltha.Membership{}, errors.New("UnImplemented")
1171}
1172
npujar1d86a522019-11-14 17:11:16 +05301173// UpdateMembership updates membership
Kent Hagermanc2c73ff2019-11-20 16:22:32 -05001174func (handler *APIHandler) UpdateMembership(context.Context, *voltha.Membership) (*empty.Empty, error) {
1175 return &empty.Empty{}, errors.New("UnImplemented")
1176}
kesavandbc2d1622020-01-21 00:42:01 -05001177
1178func (handler *APIHandler) EnablePort(ctx context.Context, port *voltha.Port) (*empty.Empty, error) {
1179 log.Debugw("EnablePort-request", log.Fields{"device-id": port.DeviceId, "port-no": port.PortNo})
1180 if isTestMode(ctx) {
1181 return &empty.Empty{}, nil
1182 }
1183
1184 if handler.competeForTransaction() {
1185 txn, err := handler.takeRequestOwnership(ctx, &utils.DeviceID{ID: port.DeviceId})
1186 if err != nil {
1187 return nil, err
1188 }
1189 defer txn.Close(ctx)
1190 }
1191
1192 ch := make(chan interface{})
1193 defer close(ch)
1194 go handler.deviceMgr.enablePort(ctx, port, ch)
1195 return waitForNilResponseOnSuccess(ctx, ch)
1196}
1197
1198func (handler *APIHandler) DisablePort(ctx context.Context, port *voltha.Port) (*empty.Empty, error) {
1199
1200 log.Debugw("DisablePort-request", log.Fields{"device-id": port.DeviceId, "port-no": port.PortNo})
1201 if isTestMode(ctx) {
1202 return &empty.Empty{}, nil
1203 }
1204
1205 if handler.competeForTransaction() {
1206 txn, err := handler.takeRequestOwnership(ctx, &utils.DeviceID{ID: port.DeviceId})
1207 if err != nil {
1208 return nil, err
1209 }
1210 defer txn.Close(ctx)
1211 }
1212
1213 ch := make(chan interface{})
1214 defer close(ch)
1215 go handler.deviceMgr.disablePort(ctx, port, ch)
1216 return waitForNilResponseOnSuccess(ctx, ch)
1217}