blob: eaf8facd933a9308d76bd02511175094a911f3e0 [file] [log] [blame]
khenaidoobf6e7bb2018-08-14 22:27:29 -04001/*
2 * Copyright 2018-present Open Networking Foundation
3
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7
8 * http://www.apache.org/licenses/LICENSE-2.0
9
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
npujar1d86a522019-11-14 17:11:16 +053016
khenaidoob9203542018-09-17 22:56:37 -040017package core
khenaidoobf6e7bb2018-08-14 22:27:29 -040018
19import (
20 "context"
Matteo Scandolo360605d2019-11-05 18:29:17 -080021 "encoding/hex"
David Bainbridge4087cc52019-11-13 18:36:03 +000022 "encoding/json"
khenaidoobf6e7bb2018-08-14 22:27:29 -040023 "errors"
npujar1d86a522019-11-14 17:11:16 +053024 "io"
25 "sync"
26
khenaidoobf6e7bb2018-08-14 22:27:29 -040027 "github.com/golang/protobuf/ptypes/empty"
28 da "github.com/opencord/voltha-go/common/core/northbound/grpc"
khenaidoo1ce37ad2019-03-24 22:07:24 -040029 "github.com/opencord/voltha-go/rw_core/utils"
serkant.uluderya2ae470f2020-01-21 11:13:09 -080030 "github.com/opencord/voltha-lib-go/v3/pkg/log"
31 "github.com/opencord/voltha-lib-go/v3/pkg/version"
32 "github.com/opencord/voltha-protos/v3/go/common"
33 "github.com/opencord/voltha-protos/v3/go/omci"
34 "github.com/opencord/voltha-protos/v3/go/openflow_13"
35 "github.com/opencord/voltha-protos/v3/go/voltha"
khenaidoob9203542018-09-17 22:56:37 -040036 "google.golang.org/grpc/codes"
khenaidoobf6e7bb2018-08-14 22:27:29 -040037 "google.golang.org/grpc/metadata"
khenaidoob9203542018-09-17 22:56:37 -040038 "google.golang.org/grpc/status"
khenaidoobf6e7bb2018-08-14 22:27:29 -040039)
40
npujar1d86a522019-11-14 17:11:16 +053041var errorIDNotFound = status.Error(codes.NotFound, "id-not-found")
khenaidoof684e1b2019-10-28 19:00:37 -040042
npujar1d86a522019-11-14 17:11:16 +053043// Image related constants
khenaidoof5a5bfa2019-01-23 22:20:29 -050044const (
npujar1d86a522019-11-14 17:11:16 +053045 ImageDownload = iota
46 CancelImageDownload = iota
47 ActivateImage = iota
48 RevertImage = iota
khenaidoof5a5bfa2019-01-23 22:20:29 -050049)
50
npujar1d86a522019-11-14 17:11:16 +053051// APIHandler represent attributes of API handler
khenaidoobf6e7bb2018-08-14 22:27:29 -040052type APIHandler struct {
khenaidoo2c6a0992019-04-29 13:46:56 -040053 deviceMgr *DeviceManager
54 logicalDeviceMgr *LogicalDeviceManager
55 adapterMgr *AdapterManager
A R Karthick881e7ea2019-08-19 19:44:02 +000056 packetInQueue chan openflow_13.PacketIn
57 changeEventQueue chan openflow_13.ChangeEvent
58 packetInQueueDone chan bool
59 changeEventQueueDone chan bool
khenaidoo2c6a0992019-04-29 13:46:56 -040060 coreInCompetingMode bool
khenaidoob6080322019-01-29 21:47:38 -050061 longRunningRequestTimeout int64
khenaidoo2c6a0992019-04-29 13:46:56 -040062 defaultRequestTimeout int64
khenaidoobf6e7bb2018-08-14 22:27:29 -040063 da.DefaultAPIHandler
khenaidoo54e0ddf2019-02-27 16:21:33 -050064 core *Core
khenaidoobf6e7bb2018-08-14 22:27:29 -040065}
66
npujar1d86a522019-11-14 17:11:16 +053067// NewAPIHandler creates API handler instance
khenaidoo54e0ddf2019-02-27 16:21:33 -050068func NewAPIHandler(core *Core) *APIHandler {
Stephane Barbarie6e1bd502018-11-05 22:44:45 -050069 handler := &APIHandler{
khenaidoo2c6a0992019-04-29 13:46:56 -040070 deviceMgr: core.deviceMgr,
71 logicalDeviceMgr: core.logicalDeviceMgr,
72 adapterMgr: core.adapterMgr,
73 coreInCompetingMode: core.config.InCompetingMode,
74 longRunningRequestTimeout: core.config.LongRunningRequestTimeout,
75 defaultRequestTimeout: core.config.DefaultRequestTimeout,
A R Karthick881e7ea2019-08-19 19:44:02 +000076 packetInQueue: make(chan openflow_13.PacketIn, 100),
77 changeEventQueue: make(chan openflow_13.ChangeEvent, 100),
78 packetInQueueDone: make(chan bool, 1),
79 changeEventQueueDone: make(chan bool, 1),
80 core: core,
Stephane Barbarie6e1bd502018-11-05 22:44:45 -050081 }
khenaidoobf6e7bb2018-08-14 22:27:29 -040082 return handler
83}
khenaidoo4d4802d2018-10-04 21:59:49 -040084
85// isTestMode is a helper function to determine a function is invoked for testing only
khenaidoobf6e7bb2018-08-14 22:27:29 -040086func isTestMode(ctx context.Context) bool {
87 md, _ := metadata.FromIncomingContext(ctx)
88 _, exist := md[common.TestModeKeys_api_test.String()]
89 return exist
90}
91
Richard Jankowskid42826e2018-11-02 16:06:37 -040092// This function attempts to extract the serial number from the request metadata
93// and create a KV transaction for that serial number for the current core.
94func (handler *APIHandler) createKvTransaction(ctx context.Context) (*KVTransaction, error) {
95 var (
khenaidoo43c82122018-11-22 18:38:28 -050096 err error
97 ok bool
98 md metadata.MD
Richard Jankowskid42826e2018-11-02 16:06:37 -040099 serNum []string
100 )
101 if md, ok = metadata.FromIncomingContext(ctx); !ok {
102 err = errors.New("metadata-not-found")
103 } else if serNum, ok = md["voltha_serial_number"]; !ok {
104 err = errors.New("serial-number-not-found")
105 }
khenaidoo2c6a0992019-04-29 13:46:56 -0400106 if !ok || serNum == nil {
Richard Jankowskid42826e2018-11-02 16:06:37 -0400107 log.Error(err)
108 return nil, err
109 }
110 // Create KV transaction
111 txn := NewKVTransaction(serNum[0])
112 return txn, nil
113}
114
Richard Jankowski2755adf2019-01-17 17:16:48 -0500115// isOFControllerRequest is a helper function to determine if a request was initiated
116// from the OpenFlow controller (or its proxy, e.g. OFAgent)
Richard Jankowski46464e92019-03-05 11:53:55 -0500117func (handler *APIHandler) isOFControllerRequest(ctx context.Context) bool {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500118 if md, ok := metadata.FromIncomingContext(ctx); ok {
119 // Metadata in context
Richard Jankowski46464e92019-03-05 11:53:55 -0500120 if _, ok = md[handler.core.config.CoreBindingKey]; ok {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500121 // OFAgent field in metadata
khenaidoo3d3b8c22019-05-22 18:10:39 -0400122 log.Debug("OFController-request")
khenaidoo9cdc1a62019-01-24 21:57:40 -0500123 return true
124 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500125 }
khenaidoo3d3b8c22019-05-22 18:10:39 -0400126 log.Debug("not-OFController-request")
khenaidoo9cdc1a62019-01-24 21:57:40 -0500127 return false
128}
129
130// competeForTransaction is a helper function to determine whether every request needs to compete with another
131// Core to execute the request
132func (handler *APIHandler) competeForTransaction() bool {
133 return handler.coreInCompetingMode
134}
135
khenaidoo09771ef2019-10-11 14:25:02 -0400136// takeRequestOwnership creates a transaction in the dB for this request and handles the logic of transaction
137// acquisition. If the device is owned by this Core (in a core-pair) then acquire the transaction with a
138// timeout value (in the event this Core dies the transaction times out in the dB causing the other Core in the
139// core-pair to proceed with the it). If the device is not owned then this Core will just monitor the transaction
140// for potential timeouts.
141func (handler *APIHandler) takeRequestOwnership(ctx context.Context, id interface{}, maxTimeout ...int64) (*KVTransaction, error) {
khenaidoo43aa6bd2019-05-29 13:35:13 -0400142 timeout := handler.defaultRequestTimeout
143 if len(maxTimeout) > 0 {
144 timeout = maxTimeout[0]
145 }
khenaidoo43aa6bd2019-05-29 13:35:13 -0400146 txn, err := handler.createKvTransaction(ctx)
147 if txn == nil {
148 return nil, err
khenaidoo09771ef2019-10-11 14:25:02 -0400149 }
150 var acquired bool
151 if id != nil {
152 var ownedByMe bool
npujar467fe752020-01-16 20:17:45 +0530153 if ownedByMe, err = handler.core.deviceOwnership.OwnedByMe(ctx, id); err != nil {
khenaidoo09771ef2019-10-11 14:25:02 -0400154 log.Warnw("getting-ownership-failed", log.Fields{"deviceId": id, "error": err})
npujar1d86a522019-11-14 17:11:16 +0530155 return nil, errorIDNotFound
khenaidoo09771ef2019-10-11 14:25:02 -0400156 }
npujar467fe752020-01-16 20:17:45 +0530157 acquired, err = txn.Acquired(ctx, timeout, ownedByMe)
khenaidoo09771ef2019-10-11 14:25:02 -0400158 } else {
npujar467fe752020-01-16 20:17:45 +0530159 acquired, err = txn.Acquired(ctx, timeout)
khenaidoo09771ef2019-10-11 14:25:02 -0400160 }
161 if err == nil && acquired {
npujar1d86a522019-11-14 17:11:16 +0530162 log.Debugw("transaction-acquired", log.Fields{"transactionId": txn.txnID})
khenaidoo43aa6bd2019-05-29 13:35:13 -0400163 return txn, nil
khenaidoo43aa6bd2019-05-29 13:35:13 -0400164 }
npujar1d86a522019-11-14 17:11:16 +0530165 log.Debugw("transaction-not-acquired", log.Fields{"transactionId": txn.txnID, "error": err})
166 return nil, errorTransactionNotAcquired
khenaidoo43aa6bd2019-05-29 13:35:13 -0400167}
168
khenaidoo09771ef2019-10-11 14:25:02 -0400169// waitForNilResponseOnSuccess is a helper function to wait for a response on channel monitorCh where an nil
khenaidoo4d4802d2018-10-04 21:59:49 -0400170// response is expected in a successful scenario
171func waitForNilResponseOnSuccess(ctx context.Context, ch chan interface{}) (*empty.Empty, error) {
172 select {
173 case res := <-ch:
174 if res == nil {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500175 return &empty.Empty{}, nil
khenaidoo4d4802d2018-10-04 21:59:49 -0400176 } else if err, ok := res.(error); ok {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500177 return &empty.Empty{}, err
khenaidoo4d4802d2018-10-04 21:59:49 -0400178 } else {
179 log.Warnw("unexpected-return-type", log.Fields{"result": res})
180 err = status.Errorf(codes.Internal, "%s", res)
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500181 return &empty.Empty{}, err
khenaidoo4d4802d2018-10-04 21:59:49 -0400182 }
183 case <-ctx.Done():
184 log.Debug("client-timeout")
185 return nil, ctx.Err()
186 }
187}
188
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500189// ListCoreInstances returns details on the running core containers
190func (handler *APIHandler) ListCoreInstances(ctx context.Context, empty *empty.Empty) (*voltha.CoreInstances, error) {
191 log.Debug("ListCoreInstances")
192 // TODO: unused stub
193 return &voltha.CoreInstances{}, status.Errorf(codes.NotFound, "no-core-instances")
194}
195
196// GetCoreInstance returns the details of a specific core container
197func (handler *APIHandler) GetCoreInstance(ctx context.Context, id *voltha.ID) (*voltha.CoreInstance, error) {
198 log.Debugw("GetCoreInstance", log.Fields{"id": id})
199 //TODO: unused stub
200 return &voltha.CoreInstance{}, status.Errorf(codes.NotFound, "core-instance-%s", id.Id)
201}
202
npujar1d86a522019-11-14 17:11:16 +0530203// GetLogicalDevicePort returns logical device port details
khenaidoo43aa6bd2019-05-29 13:35:13 -0400204func (handler *APIHandler) GetLogicalDevicePort(ctx context.Context, id *voltha.LogicalPortId) (*voltha.LogicalPort, error) {
205 log.Debugw("GetLogicalDevicePort-request", log.Fields{"id": *id})
206
207 if handler.competeForTransaction() {
npujar1d86a522019-11-14 17:11:16 +0530208 txn, err := handler.takeRequestOwnership(ctx, &utils.LogicalDeviceID{ID: id.Id})
209 if err != nil {
khenaidoo43aa6bd2019-05-29 13:35:13 -0400210 return &voltha.LogicalPort{}, err
khenaidoo43aa6bd2019-05-29 13:35:13 -0400211 }
npujar467fe752020-01-16 20:17:45 +0530212 defer txn.Close(ctx)
khenaidoo43aa6bd2019-05-29 13:35:13 -0400213 }
npujar467fe752020-01-16 20:17:45 +0530214 return handler.logicalDeviceMgr.getLogicalPort(ctx, id)
khenaidoo43aa6bd2019-05-29 13:35:13 -0400215}
216
npujar1d86a522019-11-14 17:11:16 +0530217// EnableLogicalDevicePort enables logical device port
khenaidoobf6e7bb2018-08-14 22:27:29 -0400218func (handler *APIHandler) EnableLogicalDevicePort(ctx context.Context, id *voltha.LogicalPortId) (*empty.Empty, error) {
219 log.Debugw("EnableLogicalDevicePort-request", log.Fields{"id": id, "test": common.TestModeKeys_api_test.String()})
220 if isTestMode(ctx) {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500221 return &empty.Empty{}, nil
khenaidoobf6e7bb2018-08-14 22:27:29 -0400222 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500223
khenaidoo9cdc1a62019-01-24 21:57:40 -0500224 if handler.competeForTransaction() {
npujar1d86a522019-11-14 17:11:16 +0530225 txn, err := handler.takeRequestOwnership(ctx, &utils.LogicalDeviceID{ID: id.Id})
226 if err != nil {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500227 return &empty.Empty{}, err
Richard Jankowski2755adf2019-01-17 17:16:48 -0500228 }
npujar467fe752020-01-16 20:17:45 +0530229 defer txn.Close(ctx)
Richard Jankowski2755adf2019-01-17 17:16:48 -0500230 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500231
khenaidoo4d4802d2018-10-04 21:59:49 -0400232 ch := make(chan interface{})
233 defer close(ch)
khenaidoo19d7b632018-10-30 10:49:50 -0400234 go handler.logicalDeviceMgr.enableLogicalPort(ctx, id, ch)
khenaidoo4d4802d2018-10-04 21:59:49 -0400235 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400236}
237
npujar1d86a522019-11-14 17:11:16 +0530238// DisableLogicalDevicePort disables logical device port
khenaidoobf6e7bb2018-08-14 22:27:29 -0400239func (handler *APIHandler) DisableLogicalDevicePort(ctx context.Context, id *voltha.LogicalPortId) (*empty.Empty, error) {
240 log.Debugw("DisableLogicalDevicePort-request", log.Fields{"id": id, "test": common.TestModeKeys_api_test.String()})
241 if isTestMode(ctx) {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500242 return &empty.Empty{}, nil
khenaidoobf6e7bb2018-08-14 22:27:29 -0400243 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500244
khenaidoo9cdc1a62019-01-24 21:57:40 -0500245 if handler.competeForTransaction() {
npujar1d86a522019-11-14 17:11:16 +0530246 txn, err := handler.takeRequestOwnership(ctx, &utils.LogicalDeviceID{ID: id.Id})
247 if err != nil {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500248 return &empty.Empty{}, err
Richard Jankowski2755adf2019-01-17 17:16:48 -0500249 }
npujar467fe752020-01-16 20:17:45 +0530250 defer txn.Close(ctx)
Richard Jankowski2755adf2019-01-17 17:16:48 -0500251 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500252
khenaidoo19d7b632018-10-30 10:49:50 -0400253 ch := make(chan interface{})
254 defer close(ch)
255 go handler.logicalDeviceMgr.disableLogicalPort(ctx, id, ch)
256 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400257}
258
npujar1d86a522019-11-14 17:11:16 +0530259// UpdateLogicalDeviceFlowTable updates logical device flow table
khenaidoobf6e7bb2018-08-14 22:27:29 -0400260func (handler *APIHandler) UpdateLogicalDeviceFlowTable(ctx context.Context, flow *openflow_13.FlowTableUpdate) (*empty.Empty, error) {
261 log.Debugw("UpdateLogicalDeviceFlowTable-request", log.Fields{"flow": flow, "test": common.TestModeKeys_api_test.String()})
262 if isTestMode(ctx) {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500263 return &empty.Empty{}, nil
khenaidoobf6e7bb2018-08-14 22:27:29 -0400264 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500265
khenaidoo9cdc1a62019-01-24 21:57:40 -0500266 if handler.competeForTransaction() {
npujar1d86a522019-11-14 17:11:16 +0530267 txn, err := handler.takeRequestOwnership(ctx, &utils.LogicalDeviceID{ID: flow.Id})
268 if err != nil {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500269 return &empty.Empty{}, err
Richard Jankowski2755adf2019-01-17 17:16:48 -0500270 }
npujar467fe752020-01-16 20:17:45 +0530271 defer txn.Close(ctx)
Richard Jankowski2755adf2019-01-17 17:16:48 -0500272 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500273
khenaidoo19d7b632018-10-30 10:49:50 -0400274 ch := make(chan interface{})
275 defer close(ch)
276 go handler.logicalDeviceMgr.updateFlowTable(ctx, flow.Id, flow.FlowMod, ch)
277 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400278}
279
npujar1d86a522019-11-14 17:11:16 +0530280// UpdateLogicalDeviceFlowGroupTable updates logical device flow group table
khenaidoobf6e7bb2018-08-14 22:27:29 -0400281func (handler *APIHandler) UpdateLogicalDeviceFlowGroupTable(ctx context.Context, flow *openflow_13.FlowGroupTableUpdate) (*empty.Empty, error) {
282 log.Debugw("UpdateLogicalDeviceFlowGroupTable-request", log.Fields{"flow": flow, "test": common.TestModeKeys_api_test.String()})
283 if isTestMode(ctx) {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500284 return &empty.Empty{}, nil
khenaidoobf6e7bb2018-08-14 22:27:29 -0400285 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500286
khenaidoo9cdc1a62019-01-24 21:57:40 -0500287 if handler.competeForTransaction() {
npujar1d86a522019-11-14 17:11:16 +0530288 txn, err := handler.takeRequestOwnership(ctx, &utils.LogicalDeviceID{ID: flow.Id})
289 if err != nil {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500290 return &empty.Empty{}, err
Richard Jankowski2755adf2019-01-17 17:16:48 -0500291 }
npujar467fe752020-01-16 20:17:45 +0530292 defer txn.Close(ctx)
Richard Jankowski2755adf2019-01-17 17:16:48 -0500293 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500294
khenaidoo19d7b632018-10-30 10:49:50 -0400295 ch := make(chan interface{})
296 defer close(ch)
297 go handler.logicalDeviceMgr.updateGroupTable(ctx, flow.Id, flow.GroupMod, ch)
298 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400299}
300
khenaidoob9203542018-09-17 22:56:37 -0400301// GetDevice must be implemented in the read-only containers - should it also be implemented here?
302func (handler *APIHandler) GetDevice(ctx context.Context, id *voltha.ID) (*voltha.Device, error) {
303 log.Debugw("GetDevice-request", log.Fields{"id": id})
npujar467fe752020-01-16 20:17:45 +0530304 return handler.deviceMgr.GetDevice(ctx, id.Id)
khenaidoob9203542018-09-17 22:56:37 -0400305}
306
307// GetDevice must be implemented in the read-only containers - should it also be implemented here?
npujar1d86a522019-11-14 17:11:16 +0530308
309// ListDevices retrieves the latest devices from the data model
khenaidoob9203542018-09-17 22:56:37 -0400310func (handler *APIHandler) ListDevices(ctx context.Context, empty *empty.Empty) (*voltha.Devices, error) {
311 log.Debug("ListDevices")
npujar467fe752020-01-16 20:17:45 +0530312 devices, err := handler.deviceMgr.ListDevices(ctx)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530313 if err != nil {
314 log.Errorw("Failed to list devices", log.Fields{"error": err})
315 return nil, err
316 }
317 return devices, nil
khenaidoob9203542018-09-17 22:56:37 -0400318}
319
khenaidoo7ccedd52018-12-14 16:48:54 -0500320// ListDeviceIds returns the list of device ids managed by a voltha core
321func (handler *APIHandler) ListDeviceIds(ctx context.Context, empty *empty.Empty) (*voltha.IDs, error) {
322 log.Debug("ListDeviceIDs")
323 if isTestMode(ctx) {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500324 return &voltha.IDs{Items: make([]*voltha.ID, 0)}, nil
khenaidoo7ccedd52018-12-14 16:48:54 -0500325 }
326 return handler.deviceMgr.ListDeviceIds()
327}
328
329//ReconcileDevices is a request to a voltha core to managed a list of devices based on their IDs
330func (handler *APIHandler) ReconcileDevices(ctx context.Context, ids *voltha.IDs) (*empty.Empty, error) {
331 log.Debug("ReconcileDevices")
332 if isTestMode(ctx) {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500333 return &empty.Empty{}, nil
khenaidoo7ccedd52018-12-14 16:48:54 -0500334 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500335
khenaidoo9cdc1a62019-01-24 21:57:40 -0500336 // No need to grab a transaction as this request is core specific
337
khenaidoo7ccedd52018-12-14 16:48:54 -0500338 ch := make(chan interface{})
339 defer close(ch)
340 go handler.deviceMgr.ReconcileDevices(ctx, ids, ch)
341 return waitForNilResponseOnSuccess(ctx, ch)
342}
343
npujar1d86a522019-11-14 17:11:16 +0530344// GetLogicalDevice provides a cloned most up to date logical device
khenaidoob9203542018-09-17 22:56:37 -0400345func (handler *APIHandler) GetLogicalDevice(ctx context.Context, id *voltha.ID) (*voltha.LogicalDevice, error) {
346 log.Debugw("GetLogicalDevice-request", log.Fields{"id": id})
khenaidoo43aa6bd2019-05-29 13:35:13 -0400347 if handler.competeForTransaction() {
npujar1d86a522019-11-14 17:11:16 +0530348 txn, err := handler.takeRequestOwnership(ctx, &utils.LogicalDeviceID{ID: id.Id})
349 if err != nil {
khenaidoo43aa6bd2019-05-29 13:35:13 -0400350 return &voltha.LogicalDevice{}, err
khenaidoo43aa6bd2019-05-29 13:35:13 -0400351 }
npujar467fe752020-01-16 20:17:45 +0530352 defer txn.Close(ctx)
khenaidoo43aa6bd2019-05-29 13:35:13 -0400353 }
npujar467fe752020-01-16 20:17:45 +0530354 return handler.logicalDeviceMgr.getLogicalDevice(ctx, id.Id)
khenaidoob9203542018-09-17 22:56:37 -0400355}
356
npujar1d86a522019-11-14 17:11:16 +0530357// ListLogicalDevices returns the list of all logical devices
khenaidoob9203542018-09-17 22:56:37 -0400358func (handler *APIHandler) ListLogicalDevices(ctx context.Context, empty *empty.Empty) (*voltha.LogicalDevices, error) {
khenaidoo43aa6bd2019-05-29 13:35:13 -0400359 log.Debug("ListLogicalDevices-request")
360 if handler.competeForTransaction() {
npujar1d86a522019-11-14 17:11:16 +0530361 txn, err := handler.takeRequestOwnership(ctx, nil)
362 if err != nil {
khenaidoo43aa6bd2019-05-29 13:35:13 -0400363 return &voltha.LogicalDevices{}, err
khenaidoo43aa6bd2019-05-29 13:35:13 -0400364 }
npujar467fe752020-01-16 20:17:45 +0530365 defer txn.Close(ctx)
khenaidooa9cc6d02019-11-20 14:56:59 -0500366 if handler.isOFControllerRequest(ctx) {
367 // Since an OF controller is only interested in the set of logical devices managed by thgis Core then return
368 // only logical devices managed/monitored by this Core.
369 return handler.logicalDeviceMgr.listManagedLogicalDevices()
370 }
khenaidoo43aa6bd2019-05-29 13:35:13 -0400371 }
npujar467fe752020-01-16 20:17:45 +0530372 return handler.logicalDeviceMgr.listLogicalDevices(ctx)
khenaidoob9203542018-09-17 22:56:37 -0400373}
374
khenaidoo21d51152019-02-01 13:48:37 -0500375// ListAdapters returns the contents of all adapters known to the system
376func (handler *APIHandler) ListAdapters(ctx context.Context, empty *empty.Empty) (*voltha.Adapters, error) {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500377 log.Debug("ListAdapters")
khenaidoo21d51152019-02-01 13:48:37 -0500378 return handler.adapterMgr.listAdapters(ctx)
379}
380
npujar1d86a522019-11-14 17:11:16 +0530381// ListLogicalDeviceFlows returns the flows of logical device
khenaidoodd237172019-05-27 16:37:17 -0400382func (handler *APIHandler) ListLogicalDeviceFlows(ctx context.Context, id *voltha.ID) (*openflow_13.Flows, error) {
383 log.Debugw("ListLogicalDeviceFlows", log.Fields{"id": *id})
khenaidoo43aa6bd2019-05-29 13:35:13 -0400384 if handler.competeForTransaction() {
npujar1d86a522019-11-14 17:11:16 +0530385 txn, err := handler.takeRequestOwnership(ctx, &utils.LogicalDeviceID{ID: id.Id})
386 if err != nil {
khenaidoo43aa6bd2019-05-29 13:35:13 -0400387 return &openflow_13.Flows{}, err
khenaidoo43aa6bd2019-05-29 13:35:13 -0400388 }
npujar467fe752020-01-16 20:17:45 +0530389 defer txn.Close(ctx)
khenaidoo43aa6bd2019-05-29 13:35:13 -0400390 }
khenaidoodd237172019-05-27 16:37:17 -0400391 return handler.logicalDeviceMgr.ListLogicalDeviceFlows(ctx, id.Id)
392}
393
npujar1d86a522019-11-14 17:11:16 +0530394// ListLogicalDeviceFlowGroups returns logical device flow groups
khenaidoodd237172019-05-27 16:37:17 -0400395func (handler *APIHandler) ListLogicalDeviceFlowGroups(ctx context.Context, id *voltha.ID) (*openflow_13.FlowGroups, error) {
396 log.Debugw("ListLogicalDeviceFlowGroups", log.Fields{"id": *id})
khenaidoo43aa6bd2019-05-29 13:35:13 -0400397 if handler.competeForTransaction() {
npujar1d86a522019-11-14 17:11:16 +0530398 txn, err := handler.takeRequestOwnership(ctx, &utils.LogicalDeviceID{ID: id.Id})
399 if err != nil {
khenaidoo43aa6bd2019-05-29 13:35:13 -0400400 return &openflow_13.FlowGroups{}, err
khenaidoo43aa6bd2019-05-29 13:35:13 -0400401 }
npujar467fe752020-01-16 20:17:45 +0530402 defer txn.Close(ctx)
khenaidoo43aa6bd2019-05-29 13:35:13 -0400403 }
khenaidoodd237172019-05-27 16:37:17 -0400404 return handler.logicalDeviceMgr.ListLogicalDeviceFlowGroups(ctx, id.Id)
405}
406
npujar1d86a522019-11-14 17:11:16 +0530407// ListLogicalDevicePorts returns ports of logical device
khenaidoo19d7b632018-10-30 10:49:50 -0400408func (handler *APIHandler) ListLogicalDevicePorts(ctx context.Context, id *voltha.ID) (*voltha.LogicalPorts, error) {
409 log.Debugw("ListLogicalDevicePorts", log.Fields{"logicaldeviceid": id})
khenaidoo43aa6bd2019-05-29 13:35:13 -0400410 if handler.competeForTransaction() {
npujar1d86a522019-11-14 17:11:16 +0530411 txn, err := handler.takeRequestOwnership(ctx, &utils.LogicalDeviceID{ID: id.Id})
412 if err != nil {
khenaidoo43aa6bd2019-05-29 13:35:13 -0400413 return &voltha.LogicalPorts{}, err
khenaidoo43aa6bd2019-05-29 13:35:13 -0400414 }
npujar467fe752020-01-16 20:17:45 +0530415 defer txn.Close(ctx)
khenaidoo43aa6bd2019-05-29 13:35:13 -0400416 }
khenaidoo19d7b632018-10-30 10:49:50 -0400417 return handler.logicalDeviceMgr.ListLogicalDevicePorts(ctx, id.Id)
418}
419
khenaidoo4d4802d2018-10-04 21:59:49 -0400420// CreateDevice creates a new parent device in the data model
khenaidoobf6e7bb2018-08-14 22:27:29 -0400421func (handler *APIHandler) CreateDevice(ctx context.Context, device *voltha.Device) (*voltha.Device, error) {
Thomas Lee S51b5cb82019-10-14 14:49:34 +0530422 if device.MacAddress == "" && device.GetHostAndPort() == "" {
423 log.Errorf("No Device Info Present")
Thomas Lee Se5a44012019-11-07 20:32:24 +0530424 return &voltha.Device{}, errors.New("no-device-info-present; MAC or HOSTIP&PORT")
Thomas Lee S51b5cb82019-10-14 14:49:34 +0530425 }
426 log.Debugw("create-device", log.Fields{"device": *device})
khenaidoobf6e7bb2018-08-14 22:27:29 -0400427 if isTestMode(ctx) {
428 return &voltha.Device{Id: device.Id}, nil
429 }
Richard Jankowskid42826e2018-11-02 16:06:37 -0400430
khenaidoo9cdc1a62019-01-24 21:57:40 -0500431 if handler.competeForTransaction() {
khenaidoo631fe542019-05-31 15:44:43 -0400432 // There are no device Id present in this function.
npujar1d86a522019-11-14 17:11:16 +0530433 txn, err := handler.takeRequestOwnership(ctx, nil)
434 if err != nil {
Richard Jankowski2755adf2019-01-17 17:16:48 -0500435 return &voltha.Device{}, err
Richard Jankowski2755adf2019-01-17 17:16:48 -0500436 }
npujar467fe752020-01-16 20:17:45 +0530437 defer txn.Close(ctx)
Richard Jankowski2755adf2019-01-17 17:16:48 -0500438 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500439
khenaidoob9203542018-09-17 22:56:37 -0400440 ch := make(chan interface{})
441 defer close(ch)
442 go handler.deviceMgr.createDevice(ctx, device, ch)
443 select {
444 case res := <-ch:
khenaidoo92e62c52018-10-03 14:02:54 -0400445 if res != nil {
446 if err, ok := res.(error); ok {
Thomas Lee Se5a44012019-11-07 20:32:24 +0530447 log.Errorw("create-device-failed", log.Fields{"error": err})
448 return nil, err
khenaidoo92e62c52018-10-03 14:02:54 -0400449 }
450 if d, ok := res.(*voltha.Device); ok {
npujar467fe752020-01-16 20:17:45 +0530451 _, err := handler.core.deviceOwnership.OwnedByMe(ctx, &utils.DeviceID{ID: d.Id})
npujar1d86a522019-11-14 17:11:16 +0530452 if err != nil {
453 log.Errorw("unable-to-find-core-instance-active-owns-this-device", log.Fields{"error": err})
454 }
khenaidoo92e62c52018-10-03 14:02:54 -0400455 return d, nil
456 }
khenaidoob9203542018-09-17 22:56:37 -0400457 }
khenaidoo92e62c52018-10-03 14:02:54 -0400458 log.Warnw("create-device-unexpected-return-type", log.Fields{"result": res})
459 err := status.Errorf(codes.Internal, "%s", res)
460 return &voltha.Device{}, err
khenaidoob9203542018-09-17 22:56:37 -0400461 case <-ctx.Done():
462 log.Debug("createdevice-client-timeout")
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500463 return &voltha.Device{}, ctx.Err()
khenaidoob9203542018-09-17 22:56:37 -0400464 }
khenaidoobf6e7bb2018-08-14 22:27:29 -0400465}
466
khenaidoo4d4802d2018-10-04 21:59:49 -0400467// EnableDevice activates a device by invoking the adopt_device API on the appropriate adapter
khenaidoobf6e7bb2018-08-14 22:27:29 -0400468func (handler *APIHandler) EnableDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) {
khenaidoob9203542018-09-17 22:56:37 -0400469 log.Debugw("enabledevice", log.Fields{"id": id})
khenaidoobf6e7bb2018-08-14 22:27:29 -0400470 if isTestMode(ctx) {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500471 return &empty.Empty{}, nil
khenaidoobf6e7bb2018-08-14 22:27:29 -0400472 }
Richard Jankowskid42826e2018-11-02 16:06:37 -0400473
khenaidoo9cdc1a62019-01-24 21:57:40 -0500474 if handler.competeForTransaction() {
npujar1d86a522019-11-14 17:11:16 +0530475 txn, err := handler.takeRequestOwnership(ctx, &utils.DeviceID{ID: id.Id}, handler.longRunningRequestTimeout)
476 if err != nil {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500477 return &empty.Empty{}, err
Richard Jankowski2755adf2019-01-17 17:16:48 -0500478 }
npujar467fe752020-01-16 20:17:45 +0530479 defer txn.Close(ctx)
Richard Jankowski2755adf2019-01-17 17:16:48 -0500480 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500481
khenaidoob9203542018-09-17 22:56:37 -0400482 ch := make(chan interface{})
483 defer close(ch)
484 go handler.deviceMgr.enableDevice(ctx, id, ch)
khenaidoo4d4802d2018-10-04 21:59:49 -0400485 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400486}
487
khenaidoo4d4802d2018-10-04 21:59:49 -0400488// DisableDevice disables a device along with any child device it may have
khenaidoobf6e7bb2018-08-14 22:27:29 -0400489func (handler *APIHandler) DisableDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) {
490 log.Debugw("disabledevice-request", log.Fields{"id": id})
491 if isTestMode(ctx) {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500492 return &empty.Empty{}, nil
khenaidoobf6e7bb2018-08-14 22:27:29 -0400493 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500494
khenaidoo9cdc1a62019-01-24 21:57:40 -0500495 if handler.competeForTransaction() {
npujar1d86a522019-11-14 17:11:16 +0530496 txn, err := handler.takeRequestOwnership(ctx, &utils.DeviceID{ID: id.Id})
497 if err != nil {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500498 return &empty.Empty{}, err
Richard Jankowski2755adf2019-01-17 17:16:48 -0500499 }
npujar467fe752020-01-16 20:17:45 +0530500 defer txn.Close(ctx)
Richard Jankowski2755adf2019-01-17 17:16:48 -0500501 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500502
khenaidoo92e62c52018-10-03 14:02:54 -0400503 ch := make(chan interface{})
504 defer close(ch)
505 go handler.deviceMgr.disableDevice(ctx, id, ch)
khenaidoo4d4802d2018-10-04 21:59:49 -0400506 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400507}
508
khenaidoo4d4802d2018-10-04 21:59:49 -0400509//RebootDevice invoked the reboot API to the corresponding adapter
khenaidoobf6e7bb2018-08-14 22:27:29 -0400510func (handler *APIHandler) RebootDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) {
khenaidoo4d4802d2018-10-04 21:59:49 -0400511 log.Debugw("rebootDevice-request", log.Fields{"id": id})
khenaidoobf6e7bb2018-08-14 22:27:29 -0400512 if isTestMode(ctx) {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500513 return &empty.Empty{}, nil
khenaidoobf6e7bb2018-08-14 22:27:29 -0400514 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500515
khenaidoo9cdc1a62019-01-24 21:57:40 -0500516 if handler.competeForTransaction() {
npujar1d86a522019-11-14 17:11:16 +0530517 txn, err := handler.takeRequestOwnership(ctx, &utils.DeviceID{ID: id.Id})
518 if err != nil {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500519 return &empty.Empty{}, err
Richard Jankowski2755adf2019-01-17 17:16:48 -0500520 }
npujar467fe752020-01-16 20:17:45 +0530521 defer txn.Close(ctx)
Richard Jankowski2755adf2019-01-17 17:16:48 -0500522 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500523
khenaidoo4d4802d2018-10-04 21:59:49 -0400524 ch := make(chan interface{})
525 defer close(ch)
526 go handler.deviceMgr.rebootDevice(ctx, id, ch)
527 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400528}
529
khenaidoo4d4802d2018-10-04 21:59:49 -0400530// DeleteDevice removes a device from the data model
khenaidoobf6e7bb2018-08-14 22:27:29 -0400531func (handler *APIHandler) DeleteDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) {
532 log.Debugw("deletedevice-request", log.Fields{"id": id})
533 if isTestMode(ctx) {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500534 return &empty.Empty{}, nil
khenaidoobf6e7bb2018-08-14 22:27:29 -0400535 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500536
khenaidoo9cdc1a62019-01-24 21:57:40 -0500537 if handler.competeForTransaction() {
npujar1d86a522019-11-14 17:11:16 +0530538 txn, err := handler.takeRequestOwnership(ctx, &utils.DeviceID{ID: id.Id})
539 if err != nil {
khenaidoo09771ef2019-10-11 14:25:02 -0400540 if err == errorTransactionNotAcquired {
npujar467fe752020-01-16 20:17:45 +0530541 if ownedByMe, err := handler.core.deviceOwnership.OwnedByMe(ctx, &utils.DeviceID{ID: id.Id}); !ownedByMe && err == nil {
khenaidoo09771ef2019-10-11 14:25:02 -0400542 // Remove the device in memory
npujar467fe752020-01-16 20:17:45 +0530543 handler.deviceMgr.stopManagingDevice(ctx, id.Id)
khenaidoo09771ef2019-10-11 14:25:02 -0400544 }
khenaidoo6d62c002019-05-15 21:57:03 -0400545 }
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500546 return &empty.Empty{}, err
Richard Jankowski2755adf2019-01-17 17:16:48 -0500547 }
npujar467fe752020-01-16 20:17:45 +0530548 defer txn.Close(ctx)
Richard Jankowski2755adf2019-01-17 17:16:48 -0500549 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500550
khenaidoo4d4802d2018-10-04 21:59:49 -0400551 ch := make(chan interface{})
552 defer close(ch)
553 go handler.deviceMgr.deleteDevice(ctx, id, ch)
554 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400555}
556
David Bainbridge4087cc52019-11-13 18:36:03 +0000557// ListDevicePorts returns the ports details for a specific device entry
558func (handler *APIHandler) ListDevicePorts(ctx context.Context, id *voltha.ID) (*voltha.Ports, error) {
559 log.Debugw("listdeviceports-request", log.Fields{"id": id})
560 if handler.competeForTransaction() {
npujar1d86a522019-11-14 17:11:16 +0530561 txn, err := handler.takeRequestOwnership(ctx, &utils.DeviceID{ID: id.Id})
562 if err != nil {
David Bainbridge4087cc52019-11-13 18:36:03 +0000563 return &voltha.Ports{}, err
David Bainbridge4087cc52019-11-13 18:36:03 +0000564 }
npujar467fe752020-01-16 20:17:45 +0530565 defer txn.Close(ctx)
David Bainbridge4087cc52019-11-13 18:36:03 +0000566 }
567
npujar467fe752020-01-16 20:17:45 +0530568 device, err := handler.deviceMgr.GetDevice(ctx, id.Id)
David Bainbridge4087cc52019-11-13 18:36:03 +0000569 if err != nil {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500570 return &voltha.Ports{}, err
David Bainbridge4087cc52019-11-13 18:36:03 +0000571 }
572 ports := &voltha.Ports{}
npujar1d86a522019-11-14 17:11:16 +0530573 ports.Items = append(ports.Items, device.Ports...)
David Bainbridge4087cc52019-11-13 18:36:03 +0000574 return ports, nil
575}
576
577// ListDeviceFlows returns the flow details for a specific device entry
578func (handler *APIHandler) ListDeviceFlows(ctx context.Context, id *voltha.ID) (*openflow_13.Flows, error) {
579 log.Debugw("listdeviceflows-request", log.Fields{"id": id})
580 if handler.competeForTransaction() {
npujar1d86a522019-11-14 17:11:16 +0530581 txn, err := handler.takeRequestOwnership(ctx, &utils.DeviceID{ID: id.Id})
582 if err != nil {
David Bainbridge4087cc52019-11-13 18:36:03 +0000583 return &openflow_13.Flows{}, err
David Bainbridge4087cc52019-11-13 18:36:03 +0000584 }
npujar467fe752020-01-16 20:17:45 +0530585 defer txn.Close(ctx)
David Bainbridge4087cc52019-11-13 18:36:03 +0000586 }
587
npujar467fe752020-01-16 20:17:45 +0530588 device, err := handler.deviceMgr.GetDevice(ctx, id.Id)
David Bainbridge4087cc52019-11-13 18:36:03 +0000589 if err != nil {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500590 return &openflow_13.Flows{}, err
David Bainbridge4087cc52019-11-13 18:36:03 +0000591 }
592 flows := &openflow_13.Flows{}
npujar1d86a522019-11-14 17:11:16 +0530593 flows.Items = append(flows.Items, device.Flows.Items...)
David Bainbridge4087cc52019-11-13 18:36:03 +0000594 return flows, nil
595}
596
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500597// ListDeviceFlowGroups returns the flow group details for a specific device entry
598func (handler *APIHandler) ListDeviceFlowGroups(ctx context.Context, id *voltha.ID) (*voltha.FlowGroups, error) {
599 log.Debugw("ListDeviceFlowGroups", log.Fields{"deviceid": id})
600
npujar467fe752020-01-16 20:17:45 +0530601 if device, _ := handler.deviceMgr.GetDevice(ctx, id.Id); device != nil {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500602 return device.GetFlowGroups(), nil
603 }
604 return &voltha.FlowGroups{}, status.Errorf(codes.NotFound, "device-%s", id.Id)
605}
606
607// ListDeviceGroups returns all the device groups known to the system
608func (handler *APIHandler) ListDeviceGroups(ctx context.Context, empty *empty.Empty) (*voltha.DeviceGroups, error) {
609 log.Debug("ListDeviceGroups")
610 return &voltha.DeviceGroups{}, errors.New("UnImplemented")
611}
612
613// GetDeviceGroup returns a specific device group entry
614func (handler *APIHandler) GetDeviceGroup(ctx context.Context, id *voltha.ID) (*voltha.DeviceGroup, error) {
615 log.Debug("GetDeviceGroup")
616 return &voltha.DeviceGroup{}, errors.New("UnImplemented")
617}
618
619// ListDeviceTypes returns all the device types known to the system
620func (handler *APIHandler) ListDeviceTypes(ctx context.Context, _ *empty.Empty) (*voltha.DeviceTypes, error) {
621 log.Debug("ListDeviceTypes")
622
623 return &voltha.DeviceTypes{Items: handler.adapterMgr.listDeviceTypes()}, nil
624}
625
626// GetDeviceType returns the device type for a specific device entry
627func (handler *APIHandler) GetDeviceType(ctx context.Context, id *voltha.ID) (*voltha.DeviceType, error) {
628 log.Debugw("GetDeviceType", log.Fields{"typeid": id})
629
630 if deviceType := handler.adapterMgr.getDeviceType(id.Id); deviceType != nil {
631 return deviceType, nil
632 }
633 return &voltha.DeviceType{}, status.Errorf(codes.NotFound, "device_type-%s", id.Id)
634}
635
David Bainbridge4087cc52019-11-13 18:36:03 +0000636// GetVoltha returns the contents of all components (i.e. devices, logical_devices, ...)
637func (handler *APIHandler) GetVoltha(ctx context.Context, empty *empty.Empty) (*voltha.Voltha, error) {
638
639 log.Debug("GetVoltha")
640 /*
641 * For now, encode all the version information into a JSON object and
642 * pass that back as "version" so the client can get all the
643 * information associated with the version. Long term the API should
644 * better accomidate this, but for now this will work.
645 */
646 data, err := json.Marshal(&version.VersionInfo)
647 info := version.VersionInfo.Version
648 if err != nil {
649 log.Warnf("Unable to encode version information as JSON: %s", err.Error())
650 } else {
651 info = string(data)
652 }
653
654 return &voltha.Voltha{
655 Version: info,
656 }, nil
657}
658
khenaidoof5a5bfa2019-01-23 22:20:29 -0500659// processImageRequest is a helper method to execute an image download request
660func (handler *APIHandler) processImageRequest(ctx context.Context, img *voltha.ImageDownload, requestType int) (*common.OperationResp, error) {
661 log.Debugw("processImageDownload", log.Fields{"img": *img, "requestType": requestType})
662 if isTestMode(ctx) {
663 resp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS}
664 return resp, nil
665 }
666
khenaidoo9cdc1a62019-01-24 21:57:40 -0500667 if handler.competeForTransaction() {
npujar1d86a522019-11-14 17:11:16 +0530668 txn, err := handler.takeRequestOwnership(ctx, &utils.DeviceID{ID: img.Id})
669 if err != nil {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500670 return &common.OperationResp{}, err
khenaidoo9cdc1a62019-01-24 21:57:40 -0500671 }
npujar467fe752020-01-16 20:17:45 +0530672 defer txn.Close(ctx)
khenaidoof5a5bfa2019-01-23 22:20:29 -0500673 }
674
khenaidoo2c6a0992019-04-29 13:46:56 -0400675 failedresponse := &common.OperationResp{Code: voltha.OperationResp_OPERATION_FAILURE}
khenaidoof5a5bfa2019-01-23 22:20:29 -0500676
677 ch := make(chan interface{})
678 defer close(ch)
679 switch requestType {
npujar1d86a522019-11-14 17:11:16 +0530680 case ImageDownload:
khenaidoof5a5bfa2019-01-23 22:20:29 -0500681 go handler.deviceMgr.downloadImage(ctx, img, ch)
npujar1d86a522019-11-14 17:11:16 +0530682 case CancelImageDownload:
khenaidoof5a5bfa2019-01-23 22:20:29 -0500683 go handler.deviceMgr.cancelImageDownload(ctx, img, ch)
npujar1d86a522019-11-14 17:11:16 +0530684 case ActivateImage:
khenaidoof5a5bfa2019-01-23 22:20:29 -0500685 go handler.deviceMgr.activateImage(ctx, img, ch)
npujar1d86a522019-11-14 17:11:16 +0530686 case RevertImage:
khenaidoof5a5bfa2019-01-23 22:20:29 -0500687 go handler.deviceMgr.revertImage(ctx, img, ch)
688 default:
689 log.Warn("invalid-request-type", log.Fields{"requestType": requestType})
690 return failedresponse, status.Errorf(codes.InvalidArgument, "%d", requestType)
691 }
692 select {
693 case res := <-ch:
694 if res != nil {
695 if err, ok := res.(error); ok {
696 return failedresponse, err
697 }
698 if opResp, ok := res.(*common.OperationResp); ok {
699 return opResp, nil
700 }
701 }
702 log.Warnw("download-image-unexpected-return-type", log.Fields{"result": res})
703 return failedresponse, status.Errorf(codes.Internal, "%s", res)
704 case <-ctx.Done():
705 log.Debug("downloadImage-client-timeout")
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500706 return &common.OperationResp{}, ctx.Err()
khenaidoof5a5bfa2019-01-23 22:20:29 -0500707 }
708}
709
npujar1d86a522019-11-14 17:11:16 +0530710// DownloadImage execute an image download request
khenaidoobf6e7bb2018-08-14 22:27:29 -0400711func (handler *APIHandler) DownloadImage(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) {
712 log.Debugw("DownloadImage-request", log.Fields{"img": *img})
713 if isTestMode(ctx) {
714 resp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS}
715 return resp, nil
716 }
717
npujar1d86a522019-11-14 17:11:16 +0530718 return handler.processImageRequest(ctx, img, ImageDownload)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400719}
720
npujar1d86a522019-11-14 17:11:16 +0530721// CancelImageDownload cancels image download request
khenaidoobf6e7bb2018-08-14 22:27:29 -0400722func (handler *APIHandler) CancelImageDownload(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) {
khenaidoof5a5bfa2019-01-23 22:20:29 -0500723 log.Debugw("cancelImageDownload-request", log.Fields{"img": *img})
khenaidoobf6e7bb2018-08-14 22:27:29 -0400724 if isTestMode(ctx) {
725 resp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS}
726 return resp, nil
727 }
npujar1d86a522019-11-14 17:11:16 +0530728 return handler.processImageRequest(ctx, img, CancelImageDownload)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400729}
730
npujar1d86a522019-11-14 17:11:16 +0530731// ActivateImageUpdate activates image update request
khenaidoobf6e7bb2018-08-14 22:27:29 -0400732func (handler *APIHandler) ActivateImageUpdate(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) {
khenaidoof5a5bfa2019-01-23 22:20:29 -0500733 log.Debugw("activateImageUpdate-request", log.Fields{"img": *img})
khenaidoobf6e7bb2018-08-14 22:27:29 -0400734 if isTestMode(ctx) {
735 resp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS}
736 return resp, nil
737 }
khenaidoof5a5bfa2019-01-23 22:20:29 -0500738
npujar1d86a522019-11-14 17:11:16 +0530739 return handler.processImageRequest(ctx, img, ActivateImage)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400740}
741
npujar1d86a522019-11-14 17:11:16 +0530742// RevertImageUpdate reverts image update
khenaidoobf6e7bb2018-08-14 22:27:29 -0400743func (handler *APIHandler) RevertImageUpdate(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) {
khenaidoof5a5bfa2019-01-23 22:20:29 -0500744 log.Debugw("revertImageUpdate-request", log.Fields{"img": *img})
khenaidoobf6e7bb2018-08-14 22:27:29 -0400745 if isTestMode(ctx) {
746 resp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS}
747 return resp, nil
748 }
khenaidoof5a5bfa2019-01-23 22:20:29 -0500749
npujar1d86a522019-11-14 17:11:16 +0530750 return handler.processImageRequest(ctx, img, RevertImage)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400751}
752
npujar1d86a522019-11-14 17:11:16 +0530753// GetImageDownloadStatus returns status of image download
khenaidoof5a5bfa2019-01-23 22:20:29 -0500754func (handler *APIHandler) GetImageDownloadStatus(ctx context.Context, img *voltha.ImageDownload) (*voltha.ImageDownload, error) {
755 log.Debugw("getImageDownloadStatus-request", log.Fields{"img": *img})
756 if isTestMode(ctx) {
Stephane Barbariedf5479f2019-01-29 22:13:00 -0500757 resp := &voltha.ImageDownload{DownloadState: voltha.ImageDownload_DOWNLOAD_SUCCEEDED}
khenaidoof5a5bfa2019-01-23 22:20:29 -0500758 return resp, nil
759 }
760
Stephane Barbariedf5479f2019-01-29 22:13:00 -0500761 failedresponse := &voltha.ImageDownload{DownloadState: voltha.ImageDownload_DOWNLOAD_UNKNOWN}
khenaidoof5a5bfa2019-01-23 22:20:29 -0500762
khenaidoo9cdc1a62019-01-24 21:57:40 -0500763 if handler.competeForTransaction() {
npujar1d86a522019-11-14 17:11:16 +0530764 txn, err := handler.takeRequestOwnership(ctx, &utils.DeviceID{ID: img.Id})
765 if err != nil {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500766 return failedresponse, err
khenaidoo9cdc1a62019-01-24 21:57:40 -0500767 }
npujar467fe752020-01-16 20:17:45 +0530768 defer txn.Close(ctx)
khenaidoof5a5bfa2019-01-23 22:20:29 -0500769 }
770
771 ch := make(chan interface{})
772 defer close(ch)
773 go handler.deviceMgr.getImageDownloadStatus(ctx, img, ch)
774
775 select {
776 case res := <-ch:
777 if res != nil {
778 if err, ok := res.(error); ok {
779 return failedresponse, err
780 }
781 if downloadResp, ok := res.(*voltha.ImageDownload); ok {
782 return downloadResp, nil
783 }
784 }
785 log.Warnw("download-image-status", log.Fields{"result": res})
786 return failedresponse, status.Errorf(codes.Internal, "%s", res)
787 case <-ctx.Done():
788 log.Debug("downloadImage-client-timeout")
789 return failedresponse, ctx.Err()
790 }
791}
792
npujar1d86a522019-11-14 17:11:16 +0530793// GetImageDownload returns image download
khenaidoof5a5bfa2019-01-23 22:20:29 -0500794func (handler *APIHandler) GetImageDownload(ctx context.Context, img *voltha.ImageDownload) (*voltha.ImageDownload, error) {
795 log.Debugw("GetImageDownload-request", log.Fields{"img": *img})
796 if isTestMode(ctx) {
Stephane Barbariedf5479f2019-01-29 22:13:00 -0500797 resp := &voltha.ImageDownload{DownloadState: voltha.ImageDownload_DOWNLOAD_SUCCEEDED}
khenaidoof5a5bfa2019-01-23 22:20:29 -0500798 return resp, nil
799 }
800
npujar1d86a522019-11-14 17:11:16 +0530801 download, err := handler.deviceMgr.getImageDownload(ctx, img)
802 if err != nil {
Stephane Barbariedf5479f2019-01-29 22:13:00 -0500803 return &voltha.ImageDownload{DownloadState: voltha.ImageDownload_DOWNLOAD_UNKNOWN}, err
khenaidoof5a5bfa2019-01-23 22:20:29 -0500804 }
npujar1d86a522019-11-14 17:11:16 +0530805 return download, nil
khenaidoof5a5bfa2019-01-23 22:20:29 -0500806}
807
npujar1d86a522019-11-14 17:11:16 +0530808// ListImageDownloads returns image downloads
khenaidoof5a5bfa2019-01-23 22:20:29 -0500809func (handler *APIHandler) ListImageDownloads(ctx context.Context, id *voltha.ID) (*voltha.ImageDownloads, error) {
810 log.Debugw("ListImageDownloads-request", log.Fields{"deviceId": id.Id})
811 if isTestMode(ctx) {
khenaidoo2c6a0992019-04-29 13:46:56 -0400812 resp := &voltha.ImageDownloads{Items: []*voltha.ImageDownload{}}
khenaidoof5a5bfa2019-01-23 22:20:29 -0500813 return resp, nil
814 }
815
npujar1d86a522019-11-14 17:11:16 +0530816 downloads, err := handler.deviceMgr.listImageDownloads(ctx, id.Id)
817 if err != nil {
khenaidoof5a5bfa2019-01-23 22:20:29 -0500818 failedResp := &voltha.ImageDownloads{
khenaidoo2c6a0992019-04-29 13:46:56 -0400819 Items: []*voltha.ImageDownload{
820 {DownloadState: voltha.ImageDownload_DOWNLOAD_UNKNOWN},
821 },
khenaidoof5a5bfa2019-01-23 22:20:29 -0500822 }
823 return failedResp, err
khenaidoof5a5bfa2019-01-23 22:20:29 -0500824 }
npujar1d86a522019-11-14 17:11:16 +0530825 return downloads, nil
khenaidoof5a5bfa2019-01-23 22:20:29 -0500826}
827
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500828// GetImages returns all images for a specific device entry
829func (handler *APIHandler) GetImages(ctx context.Context, id *voltha.ID) (*voltha.Images, error) {
830 log.Debugw("GetImages", log.Fields{"deviceid": id.Id})
npujar467fe752020-01-16 20:17:45 +0530831 device, err := handler.deviceMgr.GetDevice(ctx, id.Id)
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500832 if err != nil {
833 return &voltha.Images{}, err
834 }
835 return device.GetImages(), nil
836}
837
npujar1d86a522019-11-14 17:11:16 +0530838// UpdateDevicePmConfigs updates the PM configs
khenaidoobf6e7bb2018-08-14 22:27:29 -0400839func (handler *APIHandler) UpdateDevicePmConfigs(ctx context.Context, configs *voltha.PmConfigs) (*empty.Empty, error) {
840 log.Debugw("UpdateDevicePmConfigs-request", log.Fields{"configs": *configs})
841 if isTestMode(ctx) {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500842 return &empty.Empty{}, nil
khenaidoobf6e7bb2018-08-14 22:27:29 -0400843 }
khenaidoob3127472019-07-24 21:04:55 -0400844 if handler.competeForTransaction() {
npujar1d86a522019-11-14 17:11:16 +0530845 txn, err := handler.takeRequestOwnership(ctx, &utils.DeviceID{ID: configs.Id})
846 if err != nil {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500847 return &empty.Empty{}, err
khenaidoob3127472019-07-24 21:04:55 -0400848 }
npujar467fe752020-01-16 20:17:45 +0530849 defer txn.Close(ctx)
khenaidoob3127472019-07-24 21:04:55 -0400850 }
851
852 ch := make(chan interface{})
853 defer close(ch)
854 go handler.deviceMgr.updatePmConfigs(ctx, configs, ch)
855 return waitForNilResponseOnSuccess(ctx, ch)
856}
857
npujar1d86a522019-11-14 17:11:16 +0530858// ListDevicePmConfigs returns pm configs of device
khenaidoob3127472019-07-24 21:04:55 -0400859func (handler *APIHandler) ListDevicePmConfigs(ctx context.Context, id *voltha.ID) (*voltha.PmConfigs, error) {
860 log.Debugw("ListDevicePmConfigs-request", log.Fields{"deviceId": *id})
861 if handler.competeForTransaction() {
npujar1d86a522019-11-14 17:11:16 +0530862 txn, err := handler.takeRequestOwnership(ctx, &utils.LogicalDeviceID{ID: id.Id})
863 if err != nil {
khenaidoob3127472019-07-24 21:04:55 -0400864 return &voltha.PmConfigs{}, err
khenaidoob3127472019-07-24 21:04:55 -0400865 }
npujar467fe752020-01-16 20:17:45 +0530866 defer txn.Close(ctx)
khenaidoob3127472019-07-24 21:04:55 -0400867 }
868 return handler.deviceMgr.listPmConfigs(ctx, id.Id)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400869}
870
Devmalya Paulc594bb32019-11-06 07:34:27 +0000871func (handler *APIHandler) CreateEventFilter(ctx context.Context, filter *voltha.EventFilter) (*voltha.EventFilter, error) {
872 log.Debugw("CreateEventFilter-request", log.Fields{"filter": *filter})
873 return nil, errors.New("UnImplemented")
khenaidoobf6e7bb2018-08-14 22:27:29 -0400874}
875
Devmalya Paulc594bb32019-11-06 07:34:27 +0000876func (handler *APIHandler) UpdateEventFilter(ctx context.Context, filter *voltha.EventFilter) (*voltha.EventFilter, error) {
877 log.Debugw("UpdateEventFilter-request", log.Fields{"filter": *filter})
878 return nil, errors.New("UnImplemented")
khenaidoobf6e7bb2018-08-14 22:27:29 -0400879}
880
Devmalya Paulc594bb32019-11-06 07:34:27 +0000881func (handler *APIHandler) DeleteEventFilter(ctx context.Context, filterInfo *voltha.EventFilter) (*empty.Empty, error) {
882 log.Debugw("DeleteEventFilter-request", log.Fields{"device-id": filterInfo.DeviceId, "filter-id": filterInfo.Id})
883 return nil, errors.New("UnImplemented")
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500884}
885
Devmalya Paulc594bb32019-11-06 07:34:27 +0000886// GetEventFilter returns all the filters present for a device
887func (handler *APIHandler) GetEventFilter(ctx context.Context, id *voltha.ID) (*voltha.EventFilters, error) {
888 log.Debugw("GetEventFilter-request", log.Fields{"device-id": id})
889 return nil, errors.New("UnImplemented")
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500890}
891
Devmalya Paulc594bb32019-11-06 07:34:27 +0000892// ListEventFilters returns all the filters known to the system
893func (handler *APIHandler) ListEventFilters(ctx context.Context, empty *empty.Empty) (*voltha.EventFilters, error) {
894 log.Debug("ListEventFilter-request")
895 return nil, errors.New("UnImplemented")
khenaidoobf6e7bb2018-08-14 22:27:29 -0400896}
897
898func (handler *APIHandler) SelfTest(ctx context.Context, id *voltha.ID) (*voltha.SelfTestResponse, error) {
899 log.Debugw("SelfTest-request", log.Fields{"id": id})
900 if isTestMode(ctx) {
901 resp := &voltha.SelfTestResponse{Result: voltha.SelfTestResponse_SUCCESS}
902 return resp, nil
903 }
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500904 return &voltha.SelfTestResponse{}, errors.New("UnImplemented")
khenaidoobf6e7bb2018-08-14 22:27:29 -0400905}
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500906
npujar467fe752020-01-16 20:17:45 +0530907func (handler *APIHandler) forwardPacketOut(ctx context.Context, packet *openflow_13.PacketOut) {
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500908 log.Debugw("forwardPacketOut-request", log.Fields{"packet": packet})
khenaidoo3d3b8c22019-05-22 18:10:39 -0400909 //TODO: Update this logic once the OF Controller (OFAgent in this case) can include a transaction Id in its
910 // request. For performance reason we can let both Cores in a Core-Pair forward the Packet to the adapters and
911 // let once of the shim layer (kafka proxy or adapter request handler filters out the duplicate packet)
npujar467fe752020-01-16 20:17:45 +0530912 if ownedByMe, err := handler.core.deviceOwnership.OwnedByMe(ctx, &utils.LogicalDeviceID{ID: packet.Id}); ownedByMe && err == nil {
913 if agent := handler.logicalDeviceMgr.getLogicalDeviceAgent(ctx, packet.Id); agent != nil {
914 agent.packetOut(ctx, packet.PacketOut)
khenaidoo93d5a3d2020-01-15 12:37:05 -0500915 } else {
916 log.Errorf("No logical device agent present", log.Fields{"logicaldeviceID": packet.Id})
917 }
khenaidoo3d3b8c22019-05-22 18:10:39 -0400918 }
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500919}
khenaidoo3d3b8c22019-05-22 18:10:39 -0400920
npujar1d86a522019-11-14 17:11:16 +0530921// StreamPacketsOut sends packets to adapter
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500922func (handler *APIHandler) StreamPacketsOut(packets voltha.VolthaService_StreamPacketsOutServer) error {
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500923 log.Debugw("StreamPacketsOut-request", log.Fields{"packets": packets})
khenaidoo5e250692019-08-30 14:46:21 -0400924loop:
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500925 for {
khenaidoo5e250692019-08-30 14:46:21 -0400926 select {
927 case <-packets.Context().Done():
928 log.Infow("StreamPacketsOut-context-done", log.Fields{"packets": packets, "error": packets.Context().Err()})
929 break loop
930 default:
931 }
932
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500933 packet, err := packets.Recv()
934
935 if err == io.EOF {
khenaidoo5e250692019-08-30 14:46:21 -0400936 log.Debugw("Received-EOF", log.Fields{"packets": packets})
937 break loop
938 }
939
940 if err != nil {
941 log.Errorw("Failed to receive packet out", log.Fields{"error": err})
942 continue
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500943 }
944
npujar467fe752020-01-16 20:17:45 +0530945 handler.forwardPacketOut(packets.Context(), packet)
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500946 }
947
948 log.Debugw("StreamPacketsOut-request-done", log.Fields{"packets": packets})
949 return nil
950}
951
npujar1d86a522019-11-14 17:11:16 +0530952func (handler *APIHandler) sendPacketIn(deviceID string, transationID string, packet *openflow_13.OfpPacketIn) {
khenaidoo297cd252019-02-07 22:10:23 -0500953 // TODO: Augment the OF PacketIn to include the transactionId
npujar1d86a522019-11-14 17:11:16 +0530954 packetIn := openflow_13.PacketIn{Id: deviceID, PacketIn: packet}
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500955 log.Debugw("sendPacketIn", log.Fields{"packetIn": packetIn})
A R Karthick881e7ea2019-08-19 19:44:02 +0000956 handler.packetInQueue <- packetIn
957}
958
959type callTracker struct {
960 failedPacket interface{}
961}
962type streamTracker struct {
963 calls map[string]*callTracker
964 sync.Mutex
965}
966
967var streamingTracker = &streamTracker{calls: make(map[string]*callTracker)}
968
969func (handler *APIHandler) getStreamingTracker(method string, done chan<- bool) *callTracker {
970 streamingTracker.Lock()
971 defer streamingTracker.Unlock()
972 if _, ok := streamingTracker.calls[method]; ok {
973 // bail out the other packet in thread
974 log.Debugf("%s streaming call already running. Exiting it", method)
975 done <- true
976 log.Debugf("Last %s exited. Continuing ...", method)
977 } else {
978 streamingTracker.calls[method] = &callTracker{failedPacket: nil}
Richard Jankowskidbab94a2018-12-06 16:20:25 -0500979 }
A R Karthick881e7ea2019-08-19 19:44:02 +0000980 return streamingTracker.calls[method]
981}
982
983func (handler *APIHandler) flushFailedPackets(tracker *callTracker) error {
984 if tracker.failedPacket != nil {
985 switch tracker.failedPacket.(type) {
986 case openflow_13.PacketIn:
987 log.Debug("Enqueueing last failed packetIn")
988 handler.packetInQueue <- tracker.failedPacket.(openflow_13.PacketIn)
989 case openflow_13.ChangeEvent:
990 log.Debug("Enqueueing last failed changeEvent")
991 handler.changeEventQueue <- tracker.failedPacket.(openflow_13.ChangeEvent)
992 }
993 }
994 return nil
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500995}
996
npujar1d86a522019-11-14 17:11:16 +0530997// ReceivePacketsIn receives packets from adapter
Kent Hagermanc2c73ff2019-11-20 16:22:32 -0500998func (handler *APIHandler) ReceivePacketsIn(empty *empty.Empty, packetsIn voltha.VolthaService_ReceivePacketsInServer) error {
A R Karthick881e7ea2019-08-19 19:44:02 +0000999 var streamingTracker = handler.getStreamingTracker("ReceivePacketsIn", handler.packetInQueueDone)
Stephane Barbarie6e1bd502018-11-05 22:44:45 -05001000 log.Debugw("ReceivePacketsIn-request", log.Fields{"packetsIn": packetsIn})
1001
npujar1d86a522019-11-14 17:11:16 +05301002 err := handler.flushFailedPackets(streamingTracker)
1003 if err != nil {
1004 log.Errorw("unable-to-flush-failed-packets", log.Fields{"error": err})
1005 }
A R Karthick881e7ea2019-08-19 19:44:02 +00001006
1007loop:
Stephane Barbarie6e1bd502018-11-05 22:44:45 -05001008 for {
A R Karthick881e7ea2019-08-19 19:44:02 +00001009 select {
1010 case packet := <-handler.packetInQueue:
Matteo Scandolo360605d2019-11-05 18:29:17 -08001011 log.Debugw("sending-packet-in", log.Fields{
1012 "packet": hex.EncodeToString(packet.PacketIn.Data),
1013 })
A R Karthick881e7ea2019-08-19 19:44:02 +00001014 if err := packetsIn.Send(&packet); err != nil {
1015 log.Errorw("failed-to-send-packet", log.Fields{"error": err})
1016 // save the last failed packet in
1017 streamingTracker.failedPacket = packet
1018 } else {
1019 if streamingTracker.failedPacket != nil {
1020 // reset last failed packet saved to avoid flush
1021 streamingTracker.failedPacket = nil
Richard Jankowskidbab94a2018-12-06 16:20:25 -05001022 }
1023 }
A R Karthick881e7ea2019-08-19 19:44:02 +00001024 case <-handler.packetInQueueDone:
1025 log.Debug("Another ReceivePacketsIn running. Bailing out ...")
1026 break loop
Stephane Barbarie6e1bd502018-11-05 22:44:45 -05001027 }
1028 }
A R Karthick881e7ea2019-08-19 19:44:02 +00001029
1030 //TODO: Find an elegant way to get out of the above loop when the Core is stopped
1031 return nil
Stephane Barbarie6e1bd502018-11-05 22:44:45 -05001032}
1033
npujar1d86a522019-11-14 17:11:16 +05301034func (handler *APIHandler) sendChangeEvent(deviceID string, portStatus *openflow_13.OfpPortStatus) {
Stephane Barbarie6e1bd502018-11-05 22:44:45 -05001035 // TODO: validate the type of portStatus parameter
1036 //if _, ok := portStatus.(*openflow_13.OfpPortStatus); ok {
1037 //}
npujar1d86a522019-11-14 17:11:16 +05301038 event := openflow_13.ChangeEvent{Id: deviceID, Event: &openflow_13.ChangeEvent_PortStatus{PortStatus: portStatus}}
Stephane Barbarie6e1bd502018-11-05 22:44:45 -05001039 log.Debugw("sendChangeEvent", log.Fields{"event": event})
A R Karthick881e7ea2019-08-19 19:44:02 +00001040 handler.changeEventQueue <- event
Stephane Barbarie6e1bd502018-11-05 22:44:45 -05001041}
1042
npujar1d86a522019-11-14 17:11:16 +05301043// ReceiveChangeEvents receives change in events
Kent Hagermanc2c73ff2019-11-20 16:22:32 -05001044func (handler *APIHandler) ReceiveChangeEvents(empty *empty.Empty, changeEvents voltha.VolthaService_ReceiveChangeEventsServer) error {
A R Karthick881e7ea2019-08-19 19:44:02 +00001045 var streamingTracker = handler.getStreamingTracker("ReceiveChangeEvents", handler.changeEventQueueDone)
Stephane Barbarie6e1bd502018-11-05 22:44:45 -05001046 log.Debugw("ReceiveChangeEvents-request", log.Fields{"changeEvents": changeEvents})
A R Karthick881e7ea2019-08-19 19:44:02 +00001047
npujar1d86a522019-11-14 17:11:16 +05301048 err := handler.flushFailedPackets(streamingTracker)
1049 if err != nil {
1050 log.Errorw("unable-to-flush-failed-packets", log.Fields{"error": err})
1051 }
A R Karthick881e7ea2019-08-19 19:44:02 +00001052
1053loop:
Stephane Barbarie6e1bd502018-11-05 22:44:45 -05001054 for {
A R Karthick881e7ea2019-08-19 19:44:02 +00001055 select {
Richard Jankowski199fd862019-03-18 14:49:51 -04001056 // Dequeue a change event
A R Karthick881e7ea2019-08-19 19:44:02 +00001057 case event := <-handler.changeEventQueue:
1058 log.Debugw("sending-change-event", log.Fields{"event": event})
1059 if err := changeEvents.Send(&event); err != nil {
1060 log.Errorw("failed-to-send-change-event", log.Fields{"error": err})
1061 // save last failed changeevent
1062 streamingTracker.failedPacket = event
1063 } else {
1064 if streamingTracker.failedPacket != nil {
1065 // reset last failed event saved on success to avoid flushing
1066 streamingTracker.failedPacket = nil
Richard Jankowski199fd862019-03-18 14:49:51 -04001067 }
1068 }
A R Karthick881e7ea2019-08-19 19:44:02 +00001069 case <-handler.changeEventQueueDone:
1070 log.Debug("Another ReceiveChangeEvents already running. Bailing out ...")
1071 break loop
Stephane Barbarie6e1bd502018-11-05 22:44:45 -05001072 }
1073 }
A R Karthick881e7ea2019-08-19 19:44:02 +00001074
1075 return nil
Richard Jankowski199fd862019-03-18 14:49:51 -04001076}
Stephane Barbarie6e1bd502018-11-05 22:44:45 -05001077
npujar1d86a522019-11-14 17:11:16 +05301078// Subscribe subscribing request of ofagent
Stephane Barbarie6e1bd502018-11-05 22:44:45 -05001079func (handler *APIHandler) Subscribe(
1080 ctx context.Context,
1081 ofAgent *voltha.OfAgentSubscriber,
1082) (*voltha.OfAgentSubscriber, error) {
1083 log.Debugw("Subscribe-request", log.Fields{"ofAgent": ofAgent})
1084 return &voltha.OfAgentSubscriber{OfagentId: ofAgent.OfagentId, VolthaId: ofAgent.VolthaId}, nil
1085}
William Kurkiandaa6bb22019-03-07 12:26:28 -05001086
npujar1d86a522019-11-14 17:11:16 +05301087// GetAlarmDeviceData @TODO useless stub, what should this actually do?
Kent Hagermanc2c73ff2019-11-20 16:22:32 -05001088func (handler *APIHandler) GetAlarmDeviceData(ctx context.Context, in *common.ID) (*omci.AlarmDeviceData, error) {
William Kurkiandaa6bb22019-03-07 12:26:28 -05001089 log.Debug("GetAlarmDeviceData-stub")
Kent Hagermanc2c73ff2019-11-20 16:22:32 -05001090 return &omci.AlarmDeviceData{}, errors.New("UnImplemented")
William Kurkiandaa6bb22019-03-07 12:26:28 -05001091}
1092
npujar1d86a522019-11-14 17:11:16 +05301093// ListLogicalDeviceMeters returns logical device meters
Manikkaraj kb1a10922019-07-29 12:10:34 -04001094func (handler *APIHandler) ListLogicalDeviceMeters(ctx context.Context, id *voltha.ID) (*openflow_13.Meters, error) {
1095
1096 log.Debugw("ListLogicalDeviceMeters", log.Fields{"id": *id})
1097 if handler.competeForTransaction() {
npujar1d86a522019-11-14 17:11:16 +05301098 txn, err := handler.takeRequestOwnership(ctx, &utils.LogicalDeviceID{ID: id.Id})
1099 if err != nil {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -05001100 return &openflow_13.Meters{}, err // TODO: Return empty meter entry
Manikkaraj kb1a10922019-07-29 12:10:34 -04001101 }
npujar467fe752020-01-16 20:17:45 +05301102 defer txn.Close(ctx)
Manikkaraj kb1a10922019-07-29 12:10:34 -04001103 }
1104 return handler.logicalDeviceMgr.ListLogicalDeviceMeters(ctx, id.Id)
William Kurkiandaa6bb22019-03-07 12:26:28 -05001105}
1106
npujar1d86a522019-11-14 17:11:16 +05301107// GetMeterStatsOfLogicalDevice @TODO useless stub, what should this actually do?
Kent Hagermanc2c73ff2019-11-20 16:22:32 -05001108func (handler *APIHandler) GetMeterStatsOfLogicalDevice(ctx context.Context, in *common.ID) (*openflow_13.MeterStatsReply, error) {
1109 log.Debug("GetMeterStatsOfLogicalDevice")
1110 return &openflow_13.MeterStatsReply{}, errors.New("UnImplemented")
1111}
1112
npujar1d86a522019-11-14 17:11:16 +05301113// GetMibDeviceData @TODO useless stub, what should this actually do?
Kent Hagermanc2c73ff2019-11-20 16:22:32 -05001114func (handler *APIHandler) GetMibDeviceData(ctx context.Context, in *common.ID) (*omci.MibDeviceData, error) {
1115 log.Debug("GetMibDeviceData")
1116 return &omci.MibDeviceData{}, errors.New("UnImplemented")
William Kurkiandaa6bb22019-03-07 12:26:28 -05001117}
1118
npujar1d86a522019-11-14 17:11:16 +05301119// SimulateAlarm sends simulate alarm request
William Kurkiandaa6bb22019-03-07 12:26:28 -05001120func (handler *APIHandler) SimulateAlarm(
1121 ctx context.Context,
1122 in *voltha.SimulateAlarmRequest,
1123) (*common.OperationResp, error) {
serkant.uluderya334479d2019-04-10 08:26:15 -07001124 log.Debugw("SimulateAlarm-request", log.Fields{"id": in.Id})
1125 successResp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS}
1126 if isTestMode(ctx) {
1127 return successResp, nil
1128 }
1129
1130 if handler.competeForTransaction() {
npujar1d86a522019-11-14 17:11:16 +05301131 txn, err := handler.takeRequestOwnership(ctx, &utils.DeviceID{ID: in.Id}, handler.longRunningRequestTimeout)
1132 if err != nil {
Kent Hagerman0ab4cb22019-04-24 13:13:35 -04001133 failedresponse := &common.OperationResp{Code: voltha.OperationResp_OPERATION_FAILURE}
serkant.uluderya334479d2019-04-10 08:26:15 -07001134 return failedresponse, err
serkant.uluderya334479d2019-04-10 08:26:15 -07001135 }
npujar467fe752020-01-16 20:17:45 +05301136 defer txn.Close(ctx)
serkant.uluderya334479d2019-04-10 08:26:15 -07001137 }
1138
1139 ch := make(chan interface{})
1140 defer close(ch)
1141 go handler.deviceMgr.simulateAlarm(ctx, in, ch)
1142 return successResp, nil
William Kurkiandaa6bb22019-03-07 12:26:28 -05001143}
1144
npujar1d86a522019-11-14 17:11:16 +05301145// UpdateLogicalDeviceMeterTable - This function sends meter mod request to logical device manager and waits for response
Manikkaraj kb1a10922019-07-29 12:10:34 -04001146func (handler *APIHandler) UpdateLogicalDeviceMeterTable(ctx context.Context, meter *openflow_13.MeterModUpdate) (*empty.Empty, error) {
1147 log.Debugw("UpdateLogicalDeviceMeterTable-request",
1148 log.Fields{"meter": meter, "test": common.TestModeKeys_api_test.String()})
1149 if isTestMode(ctx) {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -05001150 return &empty.Empty{}, nil
Manikkaraj kb1a10922019-07-29 12:10:34 -04001151 }
1152
1153 if handler.competeForTransaction() {
npujar1d86a522019-11-14 17:11:16 +05301154 txn, err := handler.takeRequestOwnership(ctx, &utils.LogicalDeviceID{ID: meter.Id})
1155 if err != nil {
Kent Hagermanc2c73ff2019-11-20 16:22:32 -05001156 return &empty.Empty{}, err
Manikkaraj kb1a10922019-07-29 12:10:34 -04001157 }
npujar467fe752020-01-16 20:17:45 +05301158 defer txn.Close(ctx)
Manikkaraj kb1a10922019-07-29 12:10:34 -04001159 }
1160
1161 ch := make(chan interface{})
1162 defer close(ch)
1163 go handler.logicalDeviceMgr.updateMeterTable(ctx, meter.Id, meter.MeterMod, ch)
1164 return waitForNilResponseOnSuccess(ctx, ch)
William Kurkiandaa6bb22019-03-07 12:26:28 -05001165}
Kent Hagermanc2c73ff2019-11-20 16:22:32 -05001166
npujar1d86a522019-11-14 17:11:16 +05301167// GetMembership returns membership
Kent Hagermanc2c73ff2019-11-20 16:22:32 -05001168func (handler *APIHandler) GetMembership(context.Context, *empty.Empty) (*voltha.Membership, error) {
1169 return &voltha.Membership{}, errors.New("UnImplemented")
1170}
1171
npujar1d86a522019-11-14 17:11:16 +05301172// UpdateMembership updates membership
Kent Hagermanc2c73ff2019-11-20 16:22:32 -05001173func (handler *APIHandler) UpdateMembership(context.Context, *voltha.Membership) (*empty.Empty, error) {
1174 return &empty.Empty{}, errors.New("UnImplemented")
1175}
kesavandbc2d1622020-01-21 00:42:01 -05001176
1177func (handler *APIHandler) EnablePort(ctx context.Context, port *voltha.Port) (*empty.Empty, error) {
1178 log.Debugw("EnablePort-request", log.Fields{"device-id": port.DeviceId, "port-no": port.PortNo})
1179 if isTestMode(ctx) {
1180 return &empty.Empty{}, nil
1181 }
1182
1183 if handler.competeForTransaction() {
1184 txn, err := handler.takeRequestOwnership(ctx, &utils.DeviceID{ID: port.DeviceId})
1185 if err != nil {
1186 return nil, err
1187 }
1188 defer txn.Close(ctx)
1189 }
1190
1191 ch := make(chan interface{})
1192 defer close(ch)
1193 go handler.deviceMgr.enablePort(ctx, port, ch)
1194 return waitForNilResponseOnSuccess(ctx, ch)
1195}
1196
1197func (handler *APIHandler) DisablePort(ctx context.Context, port *voltha.Port) (*empty.Empty, error) {
1198
1199 log.Debugw("DisablePort-request", log.Fields{"device-id": port.DeviceId, "port-no": port.PortNo})
1200 if isTestMode(ctx) {
1201 return &empty.Empty{}, nil
1202 }
1203
1204 if handler.competeForTransaction() {
1205 txn, err := handler.takeRequestOwnership(ctx, &utils.DeviceID{ID: port.DeviceId})
1206 if err != nil {
1207 return nil, err
1208 }
1209 defer txn.Close(ctx)
1210 }
1211
1212 ch := make(chan interface{})
1213 defer close(ch)
1214 go handler.deviceMgr.disablePort(ctx, port, ch)
1215 return waitForNilResponseOnSuccess(ctx, ch)
1216}