blob: f2c16e749093bc236f7bda7c48e99d43894190f0 [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 */
khenaidoob9203542018-09-17 22:56:37 -040016package core
khenaidoobf6e7bb2018-08-14 22:27:29 -040017
18import (
19 "context"
20 "errors"
21 "github.com/golang/protobuf/ptypes/empty"
22 da "github.com/opencord/voltha-go/common/core/northbound/grpc"
23 "github.com/opencord/voltha-go/common/log"
khenaidoo1ce37ad2019-03-24 22:07:24 -040024 "github.com/opencord/voltha-go/rw_core/utils"
William Kurkiandaa6bb22019-03-07 12:26:28 -050025 "github.com/opencord/voltha-protos/go/common"
William Kurkiandaa6bb22019-03-07 12:26:28 -050026 "github.com/opencord/voltha-protos/go/omci"
khenaidoo2c6a0992019-04-29 13:46:56 -040027 "github.com/opencord/voltha-protos/go/openflow_13"
William Kurkiandaa6bb22019-03-07 12:26:28 -050028 "github.com/opencord/voltha-protos/go/voltha"
khenaidoob9203542018-09-17 22:56:37 -040029 "google.golang.org/grpc/codes"
khenaidoobf6e7bb2018-08-14 22:27:29 -040030 "google.golang.org/grpc/metadata"
khenaidoob9203542018-09-17 22:56:37 -040031 "google.golang.org/grpc/status"
Stephane Barbarie6e1bd502018-11-05 22:44:45 -050032 "io"
A R Karthick881e7ea2019-08-19 19:44:02 +000033 "sync"
Stephane Barbarie6e1bd502018-11-05 22:44:45 -050034 "time"
khenaidoobf6e7bb2018-08-14 22:27:29 -040035)
36
khenaidoof5a5bfa2019-01-23 22:20:29 -050037const (
khenaidoo2c6a0992019-04-29 13:46:56 -040038 IMAGE_DOWNLOAD = iota
39 CANCEL_IMAGE_DOWNLOAD = iota
40 ACTIVATE_IMAGE = iota
41 REVERT_IMAGE = iota
khenaidoof5a5bfa2019-01-23 22:20:29 -050042)
43
khenaidoobf6e7bb2018-08-14 22:27:29 -040044type APIHandler struct {
khenaidoo2c6a0992019-04-29 13:46:56 -040045 deviceMgr *DeviceManager
46 logicalDeviceMgr *LogicalDeviceManager
47 adapterMgr *AdapterManager
A R Karthick881e7ea2019-08-19 19:44:02 +000048 packetInQueue chan openflow_13.PacketIn
49 changeEventQueue chan openflow_13.ChangeEvent
50 packetInQueueDone chan bool
51 changeEventQueueDone chan bool
khenaidoo2c6a0992019-04-29 13:46:56 -040052 coreInCompetingMode bool
khenaidoob6080322019-01-29 21:47:38 -050053 longRunningRequestTimeout int64
khenaidoo2c6a0992019-04-29 13:46:56 -040054 defaultRequestTimeout int64
khenaidoobf6e7bb2018-08-14 22:27:29 -040055 da.DefaultAPIHandler
khenaidoo54e0ddf2019-02-27 16:21:33 -050056 core *Core
khenaidoobf6e7bb2018-08-14 22:27:29 -040057}
58
khenaidoo54e0ddf2019-02-27 16:21:33 -050059func NewAPIHandler(core *Core) *APIHandler {
Stephane Barbarie6e1bd502018-11-05 22:44:45 -050060 handler := &APIHandler{
khenaidoo2c6a0992019-04-29 13:46:56 -040061 deviceMgr: core.deviceMgr,
62 logicalDeviceMgr: core.logicalDeviceMgr,
63 adapterMgr: core.adapterMgr,
64 coreInCompetingMode: core.config.InCompetingMode,
65 longRunningRequestTimeout: core.config.LongRunningRequestTimeout,
66 defaultRequestTimeout: core.config.DefaultRequestTimeout,
A R Karthick881e7ea2019-08-19 19:44:02 +000067 packetInQueue: make(chan openflow_13.PacketIn, 100),
68 changeEventQueue: make(chan openflow_13.ChangeEvent, 100),
69 packetInQueueDone: make(chan bool, 1),
70 changeEventQueueDone: make(chan bool, 1),
71 core: core,
Stephane Barbarie6e1bd502018-11-05 22:44:45 -050072 }
khenaidoobf6e7bb2018-08-14 22:27:29 -040073 return handler
74}
khenaidoo4d4802d2018-10-04 21:59:49 -040075
76// isTestMode is a helper function to determine a function is invoked for testing only
khenaidoobf6e7bb2018-08-14 22:27:29 -040077func isTestMode(ctx context.Context) bool {
78 md, _ := metadata.FromIncomingContext(ctx)
79 _, exist := md[common.TestModeKeys_api_test.String()]
80 return exist
81}
82
Richard Jankowskid42826e2018-11-02 16:06:37 -040083// This function attempts to extract the serial number from the request metadata
84// and create a KV transaction for that serial number for the current core.
85func (handler *APIHandler) createKvTransaction(ctx context.Context) (*KVTransaction, error) {
86 var (
khenaidoo43c82122018-11-22 18:38:28 -050087 err error
88 ok bool
89 md metadata.MD
Richard Jankowskid42826e2018-11-02 16:06:37 -040090 serNum []string
91 )
92 if md, ok = metadata.FromIncomingContext(ctx); !ok {
93 err = errors.New("metadata-not-found")
94 } else if serNum, ok = md["voltha_serial_number"]; !ok {
95 err = errors.New("serial-number-not-found")
96 }
khenaidoo2c6a0992019-04-29 13:46:56 -040097 if !ok || serNum == nil {
Richard Jankowskid42826e2018-11-02 16:06:37 -040098 log.Error(err)
99 return nil, err
100 }
101 // Create KV transaction
102 txn := NewKVTransaction(serNum[0])
103 return txn, nil
104}
105
Richard Jankowski2755adf2019-01-17 17:16:48 -0500106// isOFControllerRequest is a helper function to determine if a request was initiated
107// from the OpenFlow controller (or its proxy, e.g. OFAgent)
Richard Jankowski46464e92019-03-05 11:53:55 -0500108func (handler *APIHandler) isOFControllerRequest(ctx context.Context) bool {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500109 if md, ok := metadata.FromIncomingContext(ctx); ok {
110 // Metadata in context
Richard Jankowski46464e92019-03-05 11:53:55 -0500111 if _, ok = md[handler.core.config.CoreBindingKey]; ok {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500112 // OFAgent field in metadata
khenaidoo3d3b8c22019-05-22 18:10:39 -0400113 log.Debug("OFController-request")
khenaidoo9cdc1a62019-01-24 21:57:40 -0500114 return true
115 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500116 }
khenaidoo3d3b8c22019-05-22 18:10:39 -0400117 log.Debug("not-OFController-request")
khenaidoo9cdc1a62019-01-24 21:57:40 -0500118 return false
119}
120
121// competeForTransaction is a helper function to determine whether every request needs to compete with another
122// Core to execute the request
123func (handler *APIHandler) competeForTransaction() bool {
124 return handler.coreInCompetingMode
125}
126
khenaidoo631fe542019-05-31 15:44:43 -0400127// acquireRequest handles transaction processing for device creation and list requests, i.e. when there are no
128// specific id requested (list scenario) or id present in the request (creation use case).
129func (handler *APIHandler) acquireRequest(ctx context.Context, maxTimeout ...int64) (*KVTransaction, error) {
khenaidoo43aa6bd2019-05-29 13:35:13 -0400130 timeout := handler.defaultRequestTimeout
131 if len(maxTimeout) > 0 {
132 timeout = maxTimeout[0]
133 }
134 log.Debugw("transaction-timeout", log.Fields{"timeout": timeout})
135 txn, err := handler.createKvTransaction(ctx)
136 if txn == nil {
137 return nil, err
138 } else if txn.Acquired(timeout) {
139 return txn, nil
140 } else {
141 return nil, errors.New("failed-to-seize-request")
142 }
143}
144
khenaidoo6d62c002019-05-15 21:57:03 -0400145// takeRequestOwnership creates a transaction in the dB for this request and handles the logic of transaction
146// acquisition. If the device is owned by this Core (in a core-pair) then acquire the transaction with a
147// timeout value (in the event of a timeout the other Core in the core-pair will proceed with the transaction). If the
148// device is not owned then this Core will just monitor the transaction for potential timeouts.
Richard Jankowski199fd862019-03-18 14:49:51 -0400149func (handler *APIHandler) takeRequestOwnership(ctx context.Context, id interface{}, maxTimeout ...int64) (*KVTransaction, error) {
khenaidoo6d62c002019-05-15 21:57:03 -0400150 t := time.Now()
Richard Jankowski199fd862019-03-18 14:49:51 -0400151 timeout := handler.defaultRequestTimeout
152 if len(maxTimeout) > 0 {
153 timeout = maxTimeout[0]
154 }
Richard Jankowski199fd862019-03-18 14:49:51 -0400155 txn, err := handler.createKvTransaction(ctx)
156 if txn == nil {
khenaidoo2c6a0992019-04-29 13:46:56 -0400157 return nil, err
Richard Jankowski199fd862019-03-18 14:49:51 -0400158 }
159
160 owned := false
161 if id != nil {
khenaidoo1ce37ad2019-03-24 22:07:24 -0400162 owned = handler.core.deviceOwnership.OwnedByMe(id)
Richard Jankowski199fd862019-03-18 14:49:51 -0400163 }
164 if owned {
165 if txn.Acquired(timeout) {
khenaidoo6d62c002019-05-15 21:57:03 -0400166 log.Debugw("acquired-transaction", log.Fields{"transaction-timeout": timeout})
Richard Jankowski199fd862019-03-18 14:49:51 -0400167 return txn, nil
168 } else {
169 return nil, errors.New("failed-to-seize-request")
170 }
171 } else {
172 if txn.Monitor(timeout) {
khenaidoo6d62c002019-05-15 21:57:03 -0400173 log.Debugw("acquired-transaction-after-timeout", log.Fields{"timeout": timeout, "waited-time": time.Since(t)})
Richard Jankowski199fd862019-03-18 14:49:51 -0400174 return txn, nil
175 } else {
khenaidoo6d62c002019-05-15 21:57:03 -0400176 log.Debugw("transaction-completed-by-other", log.Fields{"timeout": timeout, "waited-time": time.Since(t)})
177 return nil, errors.New(string(COMPLETED_BY_OTHER))
Richard Jankowski199fd862019-03-18 14:49:51 -0400178 }
179 }
180}
181
khenaidoo4d4802d2018-10-04 21:59:49 -0400182// waitForNilResponseOnSuccess is a helper function to wait for a response on channel ch where an nil
183// response is expected in a successful scenario
184func waitForNilResponseOnSuccess(ctx context.Context, ch chan interface{}) (*empty.Empty, error) {
185 select {
186 case res := <-ch:
187 if res == nil {
188 return new(empty.Empty), nil
189 } else if err, ok := res.(error); ok {
190 return new(empty.Empty), err
191 } else {
192 log.Warnw("unexpected-return-type", log.Fields{"result": res})
193 err = status.Errorf(codes.Internal, "%s", res)
194 return new(empty.Empty), err
195 }
196 case <-ctx.Done():
197 log.Debug("client-timeout")
198 return nil, ctx.Err()
199 }
200}
201
khenaidoobf6e7bb2018-08-14 22:27:29 -0400202func (handler *APIHandler) UpdateLogLevel(ctx context.Context, logging *voltha.Logging) (*empty.Empty, error) {
khenaidoo6f2fbe32019-01-18 16:16:50 -0500203 log.Debugw("UpdateLogLevel-request", log.Fields{"package": logging.PackageName, "intval": int(logging.Level)})
khenaidoo92e62c52018-10-03 14:02:54 -0400204 out := new(empty.Empty)
khenaidoo6f2fbe32019-01-18 16:16:50 -0500205 if logging.PackageName == "" {
206 log.SetAllLogLevel(int(logging.Level))
207 } else {
208 log.SetPackageLogLevel(logging.PackageName, int(logging.Level))
209 }
khenaidoo92e62c52018-10-03 14:02:54 -0400210 return out, nil
khenaidoobf6e7bb2018-08-14 22:27:29 -0400211}
212
khenaidoo43aa6bd2019-05-29 13:35:13 -0400213func (handler *APIHandler) GetLogicalDevicePort(ctx context.Context, id *voltha.LogicalPortId) (*voltha.LogicalPort, error) {
214 log.Debugw("GetLogicalDevicePort-request", log.Fields{"id": *id})
215
216 if handler.competeForTransaction() {
217 if txn, err := handler.takeRequestOwnership(ctx, &utils.LogicalDeviceID{Id: id.Id}); err != nil {
218 return &voltha.LogicalPort{}, err
219 } else {
220 defer txn.Close()
221 }
222 }
223 return handler.logicalDeviceMgr.getLogicalPort(id)
224}
225
khenaidoobf6e7bb2018-08-14 22:27:29 -0400226func (handler *APIHandler) EnableLogicalDevicePort(ctx context.Context, id *voltha.LogicalPortId) (*empty.Empty, error) {
227 log.Debugw("EnableLogicalDevicePort-request", log.Fields{"id": id, "test": common.TestModeKeys_api_test.String()})
228 if isTestMode(ctx) {
229 out := new(empty.Empty)
230 return out, nil
231 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500232
khenaidoo9cdc1a62019-01-24 21:57:40 -0500233 if handler.competeForTransaction() {
khenaidoo2c6a0992019-04-29 13:46:56 -0400234 if txn, err := handler.takeRequestOwnership(ctx, &utils.LogicalDeviceID{Id: id.Id}); err != nil {
Richard Jankowski2755adf2019-01-17 17:16:48 -0500235 return new(empty.Empty), err
Richard Jankowski2755adf2019-01-17 17:16:48 -0500236 } else {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500237 defer txn.Close()
Richard Jankowski2755adf2019-01-17 17:16:48 -0500238 }
239 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500240
khenaidoo4d4802d2018-10-04 21:59:49 -0400241 ch := make(chan interface{})
242 defer close(ch)
khenaidoo19d7b632018-10-30 10:49:50 -0400243 go handler.logicalDeviceMgr.enableLogicalPort(ctx, id, ch)
khenaidoo4d4802d2018-10-04 21:59:49 -0400244 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400245}
246
247func (handler *APIHandler) DisableLogicalDevicePort(ctx context.Context, id *voltha.LogicalPortId) (*empty.Empty, error) {
248 log.Debugw("DisableLogicalDevicePort-request", log.Fields{"id": id, "test": common.TestModeKeys_api_test.String()})
249 if isTestMode(ctx) {
250 out := new(empty.Empty)
251 return out, nil
252 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500253
khenaidoo9cdc1a62019-01-24 21:57:40 -0500254 if handler.competeForTransaction() {
khenaidoo2c6a0992019-04-29 13:46:56 -0400255 if txn, err := handler.takeRequestOwnership(ctx, &utils.LogicalDeviceID{Id: id.Id}); err != nil {
Richard Jankowski2755adf2019-01-17 17:16:48 -0500256 return new(empty.Empty), err
Richard Jankowski2755adf2019-01-17 17:16:48 -0500257 } else {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500258 defer txn.Close()
Richard Jankowski2755adf2019-01-17 17:16:48 -0500259 }
260 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500261
khenaidoo19d7b632018-10-30 10:49:50 -0400262 ch := make(chan interface{})
263 defer close(ch)
264 go handler.logicalDeviceMgr.disableLogicalPort(ctx, id, ch)
265 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400266}
267
268func (handler *APIHandler) UpdateLogicalDeviceFlowTable(ctx context.Context, flow *openflow_13.FlowTableUpdate) (*empty.Empty, error) {
269 log.Debugw("UpdateLogicalDeviceFlowTable-request", log.Fields{"flow": flow, "test": common.TestModeKeys_api_test.String()})
270 if isTestMode(ctx) {
271 out := new(empty.Empty)
272 return out, nil
273 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500274
khenaidoo9cdc1a62019-01-24 21:57:40 -0500275 if handler.competeForTransaction() {
khenaidoo43aa6bd2019-05-29 13:35:13 -0400276 if txn, err := handler.takeRequestOwnership(ctx, &utils.LogicalDeviceID{Id: flow.Id}); err != nil {
277 return new(empty.Empty), err
278 } else {
279 defer txn.Close()
Richard Jankowski2755adf2019-01-17 17:16:48 -0500280 }
281 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500282
khenaidoo19d7b632018-10-30 10:49:50 -0400283 ch := make(chan interface{})
284 defer close(ch)
285 go handler.logicalDeviceMgr.updateFlowTable(ctx, flow.Id, flow.FlowMod, ch)
286 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400287}
288
289func (handler *APIHandler) UpdateLogicalDeviceFlowGroupTable(ctx context.Context, flow *openflow_13.FlowGroupTableUpdate) (*empty.Empty, error) {
290 log.Debugw("UpdateLogicalDeviceFlowGroupTable-request", log.Fields{"flow": flow, "test": common.TestModeKeys_api_test.String()})
291 if isTestMode(ctx) {
292 out := new(empty.Empty)
293 return out, nil
294 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500295
khenaidoo9cdc1a62019-01-24 21:57:40 -0500296 if handler.competeForTransaction() {
khenaidoo43aa6bd2019-05-29 13:35:13 -0400297 if txn, err := handler.takeRequestOwnership(ctx, &utils.LogicalDeviceID{Id: flow.Id}); err != nil {
298 return new(empty.Empty), err
299 } else {
300 defer txn.Close()
Richard Jankowski2755adf2019-01-17 17:16:48 -0500301 }
302 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500303
khenaidoo19d7b632018-10-30 10:49:50 -0400304 ch := make(chan interface{})
305 defer close(ch)
306 go handler.logicalDeviceMgr.updateGroupTable(ctx, flow.Id, flow.GroupMod, ch)
307 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400308}
309
khenaidoob9203542018-09-17 22:56:37 -0400310// GetDevice must be implemented in the read-only containers - should it also be implemented here?
311func (handler *APIHandler) GetDevice(ctx context.Context, id *voltha.ID) (*voltha.Device, error) {
312 log.Debugw("GetDevice-request", log.Fields{"id": id})
khenaidoo19d7b632018-10-30 10:49:50 -0400313 return handler.deviceMgr.GetDevice(id.Id)
khenaidoob9203542018-09-17 22:56:37 -0400314}
315
316// GetDevice must be implemented in the read-only containers - should it also be implemented here?
317func (handler *APIHandler) ListDevices(ctx context.Context, empty *empty.Empty) (*voltha.Devices, error) {
318 log.Debug("ListDevices")
319 return handler.deviceMgr.ListDevices()
320}
321
khenaidoo7ccedd52018-12-14 16:48:54 -0500322// ListDeviceIds returns the list of device ids managed by a voltha core
323func (handler *APIHandler) ListDeviceIds(ctx context.Context, empty *empty.Empty) (*voltha.IDs, error) {
324 log.Debug("ListDeviceIDs")
325 if isTestMode(ctx) {
326 out := &voltha.IDs{Items: make([]*voltha.ID, 0)}
327 return out, nil
328 }
329 return handler.deviceMgr.ListDeviceIds()
330}
331
332//ReconcileDevices is a request to a voltha core to managed a list of devices based on their IDs
333func (handler *APIHandler) ReconcileDevices(ctx context.Context, ids *voltha.IDs) (*empty.Empty, error) {
334 log.Debug("ReconcileDevices")
335 if isTestMode(ctx) {
336 out := new(empty.Empty)
337 return out, nil
338 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500339
khenaidoo9cdc1a62019-01-24 21:57:40 -0500340 // No need to grab a transaction as this request is core specific
341
khenaidoo7ccedd52018-12-14 16:48:54 -0500342 ch := make(chan interface{})
343 defer close(ch)
344 go handler.deviceMgr.ReconcileDevices(ctx, ids, ch)
345 return waitForNilResponseOnSuccess(ctx, ch)
346}
347
khenaidoob9203542018-09-17 22:56:37 -0400348func (handler *APIHandler) GetLogicalDevice(ctx context.Context, id *voltha.ID) (*voltha.LogicalDevice, error) {
349 log.Debugw("GetLogicalDevice-request", log.Fields{"id": id})
khenaidoo43aa6bd2019-05-29 13:35:13 -0400350 if handler.competeForTransaction() {
351 if txn, err := handler.takeRequestOwnership(ctx, &utils.LogicalDeviceID{Id: id.Id}); err != nil {
352 return &voltha.LogicalDevice{}, err
353 } else {
354 defer txn.Close()
355 }
356 }
khenaidoob9203542018-09-17 22:56:37 -0400357 return handler.logicalDeviceMgr.getLogicalDevice(id.Id)
358}
359
khenaidoob9203542018-09-17 22:56:37 -0400360func (handler *APIHandler) ListLogicalDevices(ctx context.Context, empty *empty.Empty) (*voltha.LogicalDevices, error) {
khenaidoo43aa6bd2019-05-29 13:35:13 -0400361 log.Debug("ListLogicalDevices-request")
362 if handler.competeForTransaction() {
khenaidoo631fe542019-05-31 15:44:43 -0400363 if txn, err := handler.acquireRequest(ctx); err != nil {
khenaidoo43aa6bd2019-05-29 13:35:13 -0400364 return &voltha.LogicalDevices{}, err
365 } else {
366 defer txn.Close()
367 }
368 }
369 if handler.isOFControllerRequest(ctx) {
370 // Since an OF controller is only interested in the set of logical devices managed by thgis Core then return
371 // only logical devices managed/monitored by this Core.
372 return handler.logicalDeviceMgr.listManagedLogicalDevices()
373 }
khenaidoob9203542018-09-17 22:56:37 -0400374 return handler.logicalDeviceMgr.listLogicalDevices()
375}
376
khenaidoo21d51152019-02-01 13:48:37 -0500377// ListAdapters returns the contents of all adapters known to the system
378func (handler *APIHandler) ListAdapters(ctx context.Context, empty *empty.Empty) (*voltha.Adapters, error) {
379 log.Debug("ListDevices")
380 return handler.adapterMgr.listAdapters(ctx)
381}
382
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() {
386 if txn, err := handler.takeRequestOwnership(ctx, &utils.LogicalDeviceID{Id: id.Id}); err != nil {
387 return &openflow_13.Flows{}, err
388 } else {
389 defer txn.Close()
390 }
391 }
khenaidoodd237172019-05-27 16:37:17 -0400392 return handler.logicalDeviceMgr.ListLogicalDeviceFlows(ctx, id.Id)
393}
394
395func (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() {
398 if txn, err := handler.takeRequestOwnership(ctx, &utils.LogicalDeviceID{Id: id.Id}); err != nil {
399 return &openflow_13.FlowGroups{}, err
400 } else {
401 defer txn.Close()
402 }
403 }
khenaidoodd237172019-05-27 16:37:17 -0400404 return handler.logicalDeviceMgr.ListLogicalDeviceFlowGroups(ctx, id.Id)
405}
406
khenaidoo19d7b632018-10-30 10:49:50 -0400407func (handler *APIHandler) ListLogicalDevicePorts(ctx context.Context, id *voltha.ID) (*voltha.LogicalPorts, error) {
408 log.Debugw("ListLogicalDevicePorts", log.Fields{"logicaldeviceid": id})
khenaidoo43aa6bd2019-05-29 13:35:13 -0400409 if handler.competeForTransaction() {
410 if txn, err := handler.takeRequestOwnership(ctx, &utils.LogicalDeviceID{Id: id.Id}); err != nil {
411 return &voltha.LogicalPorts{}, err
412 } else {
413 defer txn.Close()
414 }
415 }
khenaidoo19d7b632018-10-30 10:49:50 -0400416 return handler.logicalDeviceMgr.ListLogicalDevicePorts(ctx, id.Id)
417}
418
khenaidoo4d4802d2018-10-04 21:59:49 -0400419// CreateDevice creates a new parent device in the data model
khenaidoobf6e7bb2018-08-14 22:27:29 -0400420func (handler *APIHandler) CreateDevice(ctx context.Context, device *voltha.Device) (*voltha.Device, error) {
khenaidoob9203542018-09-17 22:56:37 -0400421 log.Debugw("createdevice", log.Fields{"device": *device})
khenaidoobf6e7bb2018-08-14 22:27:29 -0400422 if isTestMode(ctx) {
423 return &voltha.Device{Id: device.Id}, nil
424 }
Richard Jankowskid42826e2018-11-02 16:06:37 -0400425
khenaidoo9cdc1a62019-01-24 21:57:40 -0500426 if handler.competeForTransaction() {
khenaidoo631fe542019-05-31 15:44:43 -0400427 // There are no device Id present in this function.
428 if txn, err := handler.acquireRequest(ctx); err != nil {
Richard Jankowski2755adf2019-01-17 17:16:48 -0500429 return &voltha.Device{}, err
Richard Jankowski2755adf2019-01-17 17:16:48 -0500430 } else {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500431 defer txn.Close()
Richard Jankowski2755adf2019-01-17 17:16:48 -0500432 }
433 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500434
khenaidoob9203542018-09-17 22:56:37 -0400435 ch := make(chan interface{})
436 defer close(ch)
437 go handler.deviceMgr.createDevice(ctx, device, ch)
438 select {
439 case res := <-ch:
khenaidoo92e62c52018-10-03 14:02:54 -0400440 if res != nil {
441 if err, ok := res.(error); ok {
442 return &voltha.Device{}, err
443 }
444 if d, ok := res.(*voltha.Device); ok {
khenaidoo2c6a0992019-04-29 13:46:56 -0400445 handler.core.deviceOwnership.OwnedByMe(&utils.DeviceID{Id: d.Id})
khenaidoo92e62c52018-10-03 14:02:54 -0400446 return d, nil
447 }
khenaidoob9203542018-09-17 22:56:37 -0400448 }
khenaidoo92e62c52018-10-03 14:02:54 -0400449 log.Warnw("create-device-unexpected-return-type", log.Fields{"result": res})
450 err := status.Errorf(codes.Internal, "%s", res)
451 return &voltha.Device{}, err
khenaidoob9203542018-09-17 22:56:37 -0400452 case <-ctx.Done():
453 log.Debug("createdevice-client-timeout")
454 return nil, ctx.Err()
455 }
khenaidoobf6e7bb2018-08-14 22:27:29 -0400456}
457
khenaidoo4d4802d2018-10-04 21:59:49 -0400458// EnableDevice activates a device by invoking the adopt_device API on the appropriate adapter
khenaidoobf6e7bb2018-08-14 22:27:29 -0400459func (handler *APIHandler) EnableDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) {
khenaidoob9203542018-09-17 22:56:37 -0400460 log.Debugw("enabledevice", log.Fields{"id": id})
khenaidoobf6e7bb2018-08-14 22:27:29 -0400461 if isTestMode(ctx) {
khenaidoo4d4802d2018-10-04 21:59:49 -0400462 return new(empty.Empty), nil
khenaidoobf6e7bb2018-08-14 22:27:29 -0400463 }
Richard Jankowskid42826e2018-11-02 16:06:37 -0400464
khenaidoo9cdc1a62019-01-24 21:57:40 -0500465 if handler.competeForTransaction() {
khenaidoo2c6a0992019-04-29 13:46:56 -0400466 if txn, err := handler.takeRequestOwnership(ctx, &utils.DeviceID{Id: id.Id}, handler.longRunningRequestTimeout); err != nil {
Richard Jankowski2755adf2019-01-17 17:16:48 -0500467 return new(empty.Empty), err
Richard Jankowski2755adf2019-01-17 17:16:48 -0500468 } else {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500469 defer txn.Close()
Richard Jankowski2755adf2019-01-17 17:16:48 -0500470 }
471 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500472
khenaidoob9203542018-09-17 22:56:37 -0400473 ch := make(chan interface{})
474 defer close(ch)
475 go handler.deviceMgr.enableDevice(ctx, id, ch)
khenaidoo4d4802d2018-10-04 21:59:49 -0400476 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400477}
478
khenaidoo4d4802d2018-10-04 21:59:49 -0400479// DisableDevice disables a device along with any child device it may have
khenaidoobf6e7bb2018-08-14 22:27:29 -0400480func (handler *APIHandler) DisableDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) {
481 log.Debugw("disabledevice-request", log.Fields{"id": id})
482 if isTestMode(ctx) {
khenaidoo4d4802d2018-10-04 21:59:49 -0400483 return new(empty.Empty), nil
khenaidoobf6e7bb2018-08-14 22:27:29 -0400484 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500485
khenaidoo9cdc1a62019-01-24 21:57:40 -0500486 if handler.competeForTransaction() {
khenaidoo2c6a0992019-04-29 13:46:56 -0400487 if txn, err := handler.takeRequestOwnership(ctx, &utils.DeviceID{Id: id.Id}); err != nil {
Richard Jankowski2755adf2019-01-17 17:16:48 -0500488 return new(empty.Empty), err
Richard Jankowski2755adf2019-01-17 17:16:48 -0500489 } else {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500490 defer txn.Close()
Richard Jankowski2755adf2019-01-17 17:16:48 -0500491 }
492 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500493
khenaidoo92e62c52018-10-03 14:02:54 -0400494 ch := make(chan interface{})
495 defer close(ch)
496 go handler.deviceMgr.disableDevice(ctx, id, ch)
khenaidoo4d4802d2018-10-04 21:59:49 -0400497 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400498}
499
khenaidoo4d4802d2018-10-04 21:59:49 -0400500//RebootDevice invoked the reboot API to the corresponding adapter
khenaidoobf6e7bb2018-08-14 22:27:29 -0400501func (handler *APIHandler) RebootDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) {
khenaidoo4d4802d2018-10-04 21:59:49 -0400502 log.Debugw("rebootDevice-request", log.Fields{"id": id})
khenaidoobf6e7bb2018-08-14 22:27:29 -0400503 if isTestMode(ctx) {
khenaidoo4d4802d2018-10-04 21:59:49 -0400504 return new(empty.Empty), nil
khenaidoobf6e7bb2018-08-14 22:27:29 -0400505 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500506
khenaidoo9cdc1a62019-01-24 21:57:40 -0500507 if handler.competeForTransaction() {
khenaidoo2c6a0992019-04-29 13:46:56 -0400508 if txn, err := handler.takeRequestOwnership(ctx, &utils.DeviceID{Id: id.Id}); err != nil {
Richard Jankowski2755adf2019-01-17 17:16:48 -0500509 return new(empty.Empty), err
Richard Jankowski2755adf2019-01-17 17:16:48 -0500510 } else {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500511 defer txn.Close()
Richard Jankowski2755adf2019-01-17 17:16:48 -0500512 }
513 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500514
khenaidoo4d4802d2018-10-04 21:59:49 -0400515 ch := make(chan interface{})
516 defer close(ch)
517 go handler.deviceMgr.rebootDevice(ctx, id, ch)
518 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400519}
520
khenaidoo4d4802d2018-10-04 21:59:49 -0400521// DeleteDevice removes a device from the data model
khenaidoobf6e7bb2018-08-14 22:27:29 -0400522func (handler *APIHandler) DeleteDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) {
523 log.Debugw("deletedevice-request", log.Fields{"id": id})
524 if isTestMode(ctx) {
khenaidoo4d4802d2018-10-04 21:59:49 -0400525 return new(empty.Empty), nil
khenaidoobf6e7bb2018-08-14 22:27:29 -0400526 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500527
khenaidoo9cdc1a62019-01-24 21:57:40 -0500528 if handler.competeForTransaction() {
khenaidoo6d62c002019-05-15 21:57:03 -0400529 if txn, err := handler.takeRequestOwnership(ctx, &utils.DeviceID{Id: id.Id}); err != nil {
530 // Remove the device in memory
531 if err.Error() == (errors.New(string(COMPLETED_BY_OTHER)).Error()) {
532 handler.deviceMgr.stopManagingDevice(id.Id)
533 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500534 return new(empty.Empty), err
Richard Jankowski2755adf2019-01-17 17:16:48 -0500535 } else {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500536 defer txn.Close()
Richard Jankowski2755adf2019-01-17 17:16:48 -0500537 }
538 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500539
khenaidoo4d4802d2018-10-04 21:59:49 -0400540 ch := make(chan interface{})
541 defer close(ch)
542 go handler.deviceMgr.deleteDevice(ctx, id, ch)
543 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400544}
545
khenaidoof5a5bfa2019-01-23 22:20:29 -0500546// processImageRequest is a helper method to execute an image download request
547func (handler *APIHandler) processImageRequest(ctx context.Context, img *voltha.ImageDownload, requestType int) (*common.OperationResp, error) {
548 log.Debugw("processImageDownload", log.Fields{"img": *img, "requestType": requestType})
549 if isTestMode(ctx) {
550 resp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS}
551 return resp, nil
552 }
553
khenaidoo9cdc1a62019-01-24 21:57:40 -0500554 if handler.competeForTransaction() {
khenaidoo2c6a0992019-04-29 13:46:56 -0400555 if txn, err := handler.takeRequestOwnership(ctx, &utils.DeviceID{Id: img.Id}); err != nil {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500556 return &common.OperationResp{}, err
557 } else {
558 defer txn.Close()
559 }
khenaidoof5a5bfa2019-01-23 22:20:29 -0500560 }
561
khenaidoo2c6a0992019-04-29 13:46:56 -0400562 failedresponse := &common.OperationResp{Code: voltha.OperationResp_OPERATION_FAILURE}
khenaidoof5a5bfa2019-01-23 22:20:29 -0500563
564 ch := make(chan interface{})
565 defer close(ch)
566 switch requestType {
567 case IMAGE_DOWNLOAD:
568 go handler.deviceMgr.downloadImage(ctx, img, ch)
569 case CANCEL_IMAGE_DOWNLOAD:
570 go handler.deviceMgr.cancelImageDownload(ctx, img, ch)
571 case ACTIVATE_IMAGE:
572 go handler.deviceMgr.activateImage(ctx, img, ch)
573 case REVERT_IMAGE:
574 go handler.deviceMgr.revertImage(ctx, img, ch)
575 default:
576 log.Warn("invalid-request-type", log.Fields{"requestType": requestType})
577 return failedresponse, status.Errorf(codes.InvalidArgument, "%d", requestType)
578 }
579 select {
580 case res := <-ch:
581 if res != nil {
582 if err, ok := res.(error); ok {
583 return failedresponse, err
584 }
585 if opResp, ok := res.(*common.OperationResp); ok {
586 return opResp, nil
587 }
588 }
589 log.Warnw("download-image-unexpected-return-type", log.Fields{"result": res})
590 return failedresponse, status.Errorf(codes.Internal, "%s", res)
591 case <-ctx.Done():
592 log.Debug("downloadImage-client-timeout")
593 return nil, ctx.Err()
594 }
595}
596
khenaidoobf6e7bb2018-08-14 22:27:29 -0400597func (handler *APIHandler) DownloadImage(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) {
598 log.Debugw("DownloadImage-request", log.Fields{"img": *img})
599 if isTestMode(ctx) {
600 resp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS}
601 return resp, nil
602 }
603
khenaidoof5a5bfa2019-01-23 22:20:29 -0500604 return handler.processImageRequest(ctx, img, IMAGE_DOWNLOAD)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400605}
606
607func (handler *APIHandler) CancelImageDownload(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) {
khenaidoof5a5bfa2019-01-23 22:20:29 -0500608 log.Debugw("cancelImageDownload-request", log.Fields{"img": *img})
khenaidoobf6e7bb2018-08-14 22:27:29 -0400609 if isTestMode(ctx) {
610 resp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS}
611 return resp, nil
612 }
khenaidoof5a5bfa2019-01-23 22:20:29 -0500613 return handler.processImageRequest(ctx, img, CANCEL_IMAGE_DOWNLOAD)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400614}
615
616func (handler *APIHandler) ActivateImageUpdate(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) {
khenaidoof5a5bfa2019-01-23 22:20:29 -0500617 log.Debugw("activateImageUpdate-request", log.Fields{"img": *img})
khenaidoobf6e7bb2018-08-14 22:27:29 -0400618 if isTestMode(ctx) {
619 resp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS}
620 return resp, nil
621 }
khenaidoof5a5bfa2019-01-23 22:20:29 -0500622
623 return handler.processImageRequest(ctx, img, ACTIVATE_IMAGE)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400624}
625
626func (handler *APIHandler) RevertImageUpdate(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) {
khenaidoof5a5bfa2019-01-23 22:20:29 -0500627 log.Debugw("revertImageUpdate-request", log.Fields{"img": *img})
khenaidoobf6e7bb2018-08-14 22:27:29 -0400628 if isTestMode(ctx) {
629 resp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS}
630 return resp, nil
631 }
khenaidoof5a5bfa2019-01-23 22:20:29 -0500632
633 return handler.processImageRequest(ctx, img, REVERT_IMAGE)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400634}
635
khenaidoof5a5bfa2019-01-23 22:20:29 -0500636func (handler *APIHandler) GetImageDownloadStatus(ctx context.Context, img *voltha.ImageDownload) (*voltha.ImageDownload, error) {
637 log.Debugw("getImageDownloadStatus-request", log.Fields{"img": *img})
638 if isTestMode(ctx) {
Stephane Barbariedf5479f2019-01-29 22:13:00 -0500639 resp := &voltha.ImageDownload{DownloadState: voltha.ImageDownload_DOWNLOAD_SUCCEEDED}
khenaidoof5a5bfa2019-01-23 22:20:29 -0500640 return resp, nil
641 }
642
Stephane Barbariedf5479f2019-01-29 22:13:00 -0500643 failedresponse := &voltha.ImageDownload{DownloadState: voltha.ImageDownload_DOWNLOAD_UNKNOWN}
khenaidoof5a5bfa2019-01-23 22:20:29 -0500644
khenaidoo9cdc1a62019-01-24 21:57:40 -0500645 if handler.competeForTransaction() {
khenaidoo2c6a0992019-04-29 13:46:56 -0400646 if txn, err := handler.takeRequestOwnership(ctx, &utils.DeviceID{Id: img.Id}); err != nil {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500647 return failedresponse, err
648 } else {
649 defer txn.Close()
650 }
khenaidoof5a5bfa2019-01-23 22:20:29 -0500651 }
652
653 ch := make(chan interface{})
654 defer close(ch)
655 go handler.deviceMgr.getImageDownloadStatus(ctx, img, ch)
656
657 select {
658 case res := <-ch:
659 if res != nil {
660 if err, ok := res.(error); ok {
661 return failedresponse, err
662 }
663 if downloadResp, ok := res.(*voltha.ImageDownload); ok {
664 return downloadResp, nil
665 }
666 }
667 log.Warnw("download-image-status", log.Fields{"result": res})
668 return failedresponse, status.Errorf(codes.Internal, "%s", res)
669 case <-ctx.Done():
670 log.Debug("downloadImage-client-timeout")
671 return failedresponse, ctx.Err()
672 }
673}
674
675func (handler *APIHandler) GetImageDownload(ctx context.Context, img *voltha.ImageDownload) (*voltha.ImageDownload, error) {
676 log.Debugw("GetImageDownload-request", log.Fields{"img": *img})
677 if isTestMode(ctx) {
Stephane Barbariedf5479f2019-01-29 22:13:00 -0500678 resp := &voltha.ImageDownload{DownloadState: voltha.ImageDownload_DOWNLOAD_SUCCEEDED}
khenaidoof5a5bfa2019-01-23 22:20:29 -0500679 return resp, nil
680 }
681
682 if download, err := handler.deviceMgr.getImageDownload(ctx, img); err != nil {
Stephane Barbariedf5479f2019-01-29 22:13:00 -0500683 return &voltha.ImageDownload{DownloadState: voltha.ImageDownload_DOWNLOAD_UNKNOWN}, err
khenaidoof5a5bfa2019-01-23 22:20:29 -0500684 } else {
685 return download, nil
686 }
687}
688
689func (handler *APIHandler) ListImageDownloads(ctx context.Context, id *voltha.ID) (*voltha.ImageDownloads, error) {
690 log.Debugw("ListImageDownloads-request", log.Fields{"deviceId": id.Id})
691 if isTestMode(ctx) {
khenaidoo2c6a0992019-04-29 13:46:56 -0400692 resp := &voltha.ImageDownloads{Items: []*voltha.ImageDownload{}}
khenaidoof5a5bfa2019-01-23 22:20:29 -0500693 return resp, nil
694 }
695
696 if downloads, err := handler.deviceMgr.listImageDownloads(ctx, id.Id); err != nil {
697 failedResp := &voltha.ImageDownloads{
khenaidoo2c6a0992019-04-29 13:46:56 -0400698 Items: []*voltha.ImageDownload{
699 {DownloadState: voltha.ImageDownload_DOWNLOAD_UNKNOWN},
700 },
khenaidoof5a5bfa2019-01-23 22:20:29 -0500701 }
702 return failedResp, err
703 } else {
704 return downloads, nil
705 }
706}
707
khenaidoobf6e7bb2018-08-14 22:27:29 -0400708func (handler *APIHandler) UpdateDevicePmConfigs(ctx context.Context, configs *voltha.PmConfigs) (*empty.Empty, error) {
709 log.Debugw("UpdateDevicePmConfigs-request", log.Fields{"configs": *configs})
710 if isTestMode(ctx) {
711 out := new(empty.Empty)
712 return out, nil
713 }
khenaidoob3127472019-07-24 21:04:55 -0400714 if handler.competeForTransaction() {
715 if txn, err := handler.takeRequestOwnership(ctx, &utils.DeviceID{Id: configs.Id}); err != nil {
716 return new(empty.Empty), err
717 } else {
718 defer txn.Close()
719 }
720 }
721
722 ch := make(chan interface{})
723 defer close(ch)
724 go handler.deviceMgr.updatePmConfigs(ctx, configs, ch)
725 return waitForNilResponseOnSuccess(ctx, ch)
726}
727
728func (handler *APIHandler) ListDevicePmConfigs(ctx context.Context, id *voltha.ID) (*voltha.PmConfigs, error) {
729 log.Debugw("ListDevicePmConfigs-request", log.Fields{"deviceId": *id})
730 if handler.competeForTransaction() {
731 if txn, err := handler.takeRequestOwnership(ctx, &utils.LogicalDeviceID{Id: id.Id}); err != nil {
732 return &voltha.PmConfigs{}, err
733 } else {
734 defer txn.Close()
735 }
736 }
737 return handler.deviceMgr.listPmConfigs(ctx, id.Id)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400738}
739
740func (handler *APIHandler) CreateAlarmFilter(ctx context.Context, filter *voltha.AlarmFilter) (*voltha.AlarmFilter, error) {
741 log.Debugw("CreateAlarmFilter-request", log.Fields{"filter": *filter})
742 if isTestMode(ctx) {
743 f := &voltha.AlarmFilter{Id: filter.Id}
744 return f, nil
745 }
746 return nil, errors.New("UnImplemented")
747}
748
749func (handler *APIHandler) UpdateAlarmFilter(ctx context.Context, filter *voltha.AlarmFilter) (*voltha.AlarmFilter, error) {
750 log.Debugw("UpdateAlarmFilter-request", log.Fields{"filter": *filter})
751 if isTestMode(ctx) {
752 f := &voltha.AlarmFilter{Id: filter.Id}
753 return f, nil
754 }
755 return nil, errors.New("UnImplemented")
756}
757
758func (handler *APIHandler) DeleteAlarmFilter(ctx context.Context, id *voltha.ID) (*empty.Empty, error) {
759 log.Debugw("DeleteAlarmFilter-request", log.Fields{"id": *id})
760 if isTestMode(ctx) {
761 out := new(empty.Empty)
762 return out, nil
763 }
764 return nil, errors.New("UnImplemented")
765}
766
767func (handler *APIHandler) SelfTest(ctx context.Context, id *voltha.ID) (*voltha.SelfTestResponse, error) {
768 log.Debugw("SelfTest-request", log.Fields{"id": id})
769 if isTestMode(ctx) {
770 resp := &voltha.SelfTestResponse{Result: voltha.SelfTestResponse_SUCCESS}
771 return resp, nil
772 }
773 return nil, errors.New("UnImplemented")
774}
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500775
776func (handler *APIHandler) forwardPacketOut(packet *openflow_13.PacketOut) {
777 log.Debugw("forwardPacketOut-request", log.Fields{"packet": packet})
khenaidoo3d3b8c22019-05-22 18:10:39 -0400778 //TODO: Update this logic once the OF Controller (OFAgent in this case) can include a transaction Id in its
779 // request. For performance reason we can let both Cores in a Core-Pair forward the Packet to the adapters and
780 // let once of the shim layer (kafka proxy or adapter request handler filters out the duplicate packet)
781 if handler.core.deviceOwnership.OwnedByMe(&utils.LogicalDeviceID{Id: packet.Id}) {
782 agent := handler.logicalDeviceMgr.getLogicalDeviceAgent(packet.Id)
783 agent.packetOut(packet.PacketOut)
784 }
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500785}
khenaidoo3d3b8c22019-05-22 18:10:39 -0400786
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500787func (handler *APIHandler) StreamPacketsOut(
788 packets voltha.VolthaService_StreamPacketsOutServer,
789) error {
790 log.Debugw("StreamPacketsOut-request", log.Fields{"packets": packets})
791 for {
792 packet, err := packets.Recv()
793
794 if err == io.EOF {
795 break
796 } else if err != nil {
797 log.Errorw("Failed to receive packet", log.Fields{"error": err})
798 }
799
800 handler.forwardPacketOut(packet)
801 }
802
803 log.Debugw("StreamPacketsOut-request-done", log.Fields{"packets": packets})
804 return nil
805}
806
khenaidoo297cd252019-02-07 22:10:23 -0500807func (handler *APIHandler) sendPacketIn(deviceId string, transationId string, packet *openflow_13.OfpPacketIn) {
808 // TODO: Augment the OF PacketIn to include the transactionId
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500809 packetIn := openflow_13.PacketIn{Id: deviceId, PacketIn: packet}
810 log.Debugw("sendPacketIn", log.Fields{"packetIn": packetIn})
A R Karthick881e7ea2019-08-19 19:44:02 +0000811 handler.packetInQueue <- packetIn
812}
813
814type callTracker struct {
815 failedPacket interface{}
816}
817type streamTracker struct {
818 calls map[string]*callTracker
819 sync.Mutex
820}
821
822var streamingTracker = &streamTracker{calls: make(map[string]*callTracker)}
823
824func (handler *APIHandler) getStreamingTracker(method string, done chan<- bool) *callTracker {
825 streamingTracker.Lock()
826 defer streamingTracker.Unlock()
827 if _, ok := streamingTracker.calls[method]; ok {
828 // bail out the other packet in thread
829 log.Debugf("%s streaming call already running. Exiting it", method)
830 done <- true
831 log.Debugf("Last %s exited. Continuing ...", method)
832 } else {
833 streamingTracker.calls[method] = &callTracker{failedPacket: nil}
Richard Jankowskidbab94a2018-12-06 16:20:25 -0500834 }
A R Karthick881e7ea2019-08-19 19:44:02 +0000835 return streamingTracker.calls[method]
836}
837
838func (handler *APIHandler) flushFailedPackets(tracker *callTracker) error {
839 if tracker.failedPacket != nil {
840 switch tracker.failedPacket.(type) {
841 case openflow_13.PacketIn:
842 log.Debug("Enqueueing last failed packetIn")
843 handler.packetInQueue <- tracker.failedPacket.(openflow_13.PacketIn)
844 case openflow_13.ChangeEvent:
845 log.Debug("Enqueueing last failed changeEvent")
846 handler.changeEventQueue <- tracker.failedPacket.(openflow_13.ChangeEvent)
847 }
848 }
849 return nil
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500850}
851
852func (handler *APIHandler) ReceivePacketsIn(
853 empty *empty.Empty,
854 packetsIn voltha.VolthaService_ReceivePacketsInServer,
855) error {
A R Karthick881e7ea2019-08-19 19:44:02 +0000856 var streamingTracker = handler.getStreamingTracker("ReceivePacketsIn", handler.packetInQueueDone)
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500857 log.Debugw("ReceivePacketsIn-request", log.Fields{"packetsIn": packetsIn})
858
A R Karthick881e7ea2019-08-19 19:44:02 +0000859 handler.flushFailedPackets(streamingTracker)
860
861loop:
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500862 for {
A R Karthick881e7ea2019-08-19 19:44:02 +0000863 select {
864 case packet := <-handler.packetInQueue:
865 log.Debugw("sending-packet-in", log.Fields{"packet": packet})
866 if err := packetsIn.Send(&packet); err != nil {
867 log.Errorw("failed-to-send-packet", log.Fields{"error": err})
868 // save the last failed packet in
869 streamingTracker.failedPacket = packet
870 } else {
871 if streamingTracker.failedPacket != nil {
872 // reset last failed packet saved to avoid flush
873 streamingTracker.failedPacket = nil
Richard Jankowskidbab94a2018-12-06 16:20:25 -0500874 }
875 }
A R Karthick881e7ea2019-08-19 19:44:02 +0000876 case <-handler.packetInQueueDone:
877 log.Debug("Another ReceivePacketsIn running. Bailing out ...")
878 break loop
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500879 }
880 }
A R Karthick881e7ea2019-08-19 19:44:02 +0000881
882 //TODO: Find an elegant way to get out of the above loop when the Core is stopped
883 return nil
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500884}
885
886func (handler *APIHandler) sendChangeEvent(deviceId string, portStatus *openflow_13.OfpPortStatus) {
887 // TODO: validate the type of portStatus parameter
888 //if _, ok := portStatus.(*openflow_13.OfpPortStatus); ok {
889 //}
890 event := openflow_13.ChangeEvent{Id: deviceId, Event: &openflow_13.ChangeEvent_PortStatus{PortStatus: portStatus}}
891 log.Debugw("sendChangeEvent", log.Fields{"event": event})
A R Karthick881e7ea2019-08-19 19:44:02 +0000892 handler.changeEventQueue <- event
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500893}
894
895func (handler *APIHandler) ReceiveChangeEvents(
896 empty *empty.Empty,
897 changeEvents voltha.VolthaService_ReceiveChangeEventsServer,
898) error {
A R Karthick881e7ea2019-08-19 19:44:02 +0000899 var streamingTracker = handler.getStreamingTracker("ReceiveChangeEvents", handler.changeEventQueueDone)
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500900 log.Debugw("ReceiveChangeEvents-request", log.Fields{"changeEvents": changeEvents})
A R Karthick881e7ea2019-08-19 19:44:02 +0000901
902 handler.flushFailedPackets(streamingTracker)
903
904loop:
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500905 for {
A R Karthick881e7ea2019-08-19 19:44:02 +0000906 select {
Richard Jankowski199fd862019-03-18 14:49:51 -0400907 // Dequeue a change event
A R Karthick881e7ea2019-08-19 19:44:02 +0000908 case event := <-handler.changeEventQueue:
909 log.Debugw("sending-change-event", log.Fields{"event": event})
910 if err := changeEvents.Send(&event); err != nil {
911 log.Errorw("failed-to-send-change-event", log.Fields{"error": err})
912 // save last failed changeevent
913 streamingTracker.failedPacket = event
914 } else {
915 if streamingTracker.failedPacket != nil {
916 // reset last failed event saved on success to avoid flushing
917 streamingTracker.failedPacket = nil
Richard Jankowski199fd862019-03-18 14:49:51 -0400918 }
919 }
A R Karthick881e7ea2019-08-19 19:44:02 +0000920 case <-handler.changeEventQueueDone:
921 log.Debug("Another ReceiveChangeEvents already running. Bailing out ...")
922 break loop
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500923 }
924 }
A R Karthick881e7ea2019-08-19 19:44:02 +0000925
926 return nil
Richard Jankowski199fd862019-03-18 14:49:51 -0400927}
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500928
929func (handler *APIHandler) Subscribe(
930 ctx context.Context,
931 ofAgent *voltha.OfAgentSubscriber,
932) (*voltha.OfAgentSubscriber, error) {
933 log.Debugw("Subscribe-request", log.Fields{"ofAgent": ofAgent})
934 return &voltha.OfAgentSubscriber{OfagentId: ofAgent.OfagentId, VolthaId: ofAgent.VolthaId}, nil
935}
William Kurkiandaa6bb22019-03-07 12:26:28 -0500936
937//@TODO useless stub, what should this actually do?
938func (handler *APIHandler) GetAlarmDeviceData(
939 ctx context.Context,
940 in *common.ID,
941) (*omci.AlarmDeviceData, error) {
942 log.Debug("GetAlarmDeviceData-stub")
943 return nil, nil
944}
945
946//@TODO useless stub, what should this actually do?
947func (handler *APIHandler) GetMeterStatsOfLogicalDevice(
khenaidoo2c6a0992019-04-29 13:46:56 -0400948 ctx context.Context,
William Kurkiandaa6bb22019-03-07 12:26:28 -0500949 in *common.ID,
950) (*openflow_13.MeterStatsReply, error) {
951 log.Debug("GetMeterStatsOfLogicalDevice-stub")
952 return nil, nil
953}
954
955//@TODO useless stub, what should this actually do?
956func (handler *APIHandler) GetMibDeviceData(
khenaidoo2c6a0992019-04-29 13:46:56 -0400957 ctx context.Context,
958 in *common.ID,
William Kurkiandaa6bb22019-03-07 12:26:28 -0500959) (*omci.MibDeviceData, error) {
960 log.Debug("GetMibDeviceData-stub")
961 return nil, nil
962}
963
William Kurkiandaa6bb22019-03-07 12:26:28 -0500964func (handler *APIHandler) SimulateAlarm(
965 ctx context.Context,
966 in *voltha.SimulateAlarmRequest,
967) (*common.OperationResp, error) {
serkant.uluderya334479d2019-04-10 08:26:15 -0700968 log.Debugw("SimulateAlarm-request", log.Fields{"id": in.Id})
969 successResp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS}
970 if isTestMode(ctx) {
971 return successResp, nil
972 }
973
974 if handler.competeForTransaction() {
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400975 if txn, err := handler.takeRequestOwnership(ctx, &utils.DeviceID{Id: in.Id}, handler.longRunningRequestTimeout); err != nil {
976 failedresponse := &common.OperationResp{Code: voltha.OperationResp_OPERATION_FAILURE}
serkant.uluderya334479d2019-04-10 08:26:15 -0700977 return failedresponse, err
978 } else {
979 defer txn.Close()
980 }
981 }
982
983 ch := make(chan interface{})
984 defer close(ch)
985 go handler.deviceMgr.simulateAlarm(ctx, in, ch)
986 return successResp, nil
William Kurkiandaa6bb22019-03-07 12:26:28 -0500987}
988
989//@TODO useless stub, what should this actually do?
990func (handler *APIHandler) UpdateLogicalDeviceMeterTable(
991 ctx context.Context,
992 in *openflow_13.MeterModUpdate,
993) (*empty.Empty, error) {
994 log.Debug("UpdateLogicalDeviceMeterTable-stub")
995 return nil, nil
996}