blob: 1e5cc5b4d29b9bd1f5b93ccb2181fcadc45d2c46 [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"
Richard Jankowskidbab94a2018-12-06 16:20:25 -050021 "github.com/golang-collections/go-datastructures/queue"
khenaidoobf6e7bb2018-08-14 22:27:29 -040022 "github.com/golang/protobuf/ptypes/empty"
23 da "github.com/opencord/voltha-go/common/core/northbound/grpc"
24 "github.com/opencord/voltha-go/common/log"
25 "github.com/opencord/voltha-go/protos/common"
26 "github.com/opencord/voltha-go/protos/openflow_13"
27 "github.com/opencord/voltha-go/protos/voltha"
khenaidoo1ce37ad2019-03-24 22:07:24 -040028 "github.com/opencord/voltha-go/rw_core/utils"
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"
33 "time"
khenaidoobf6e7bb2018-08-14 22:27:29 -040034)
35
khenaidoof5a5bfa2019-01-23 22:20:29 -050036const (
37 IMAGE_DOWNLOAD = iota
38 CANCEL_IMAGE_DOWNLOAD = iota
39 ACTIVATE_IMAGE = iota
40 REVERT_IMAGE = iota
41)
42
khenaidoo297cd252019-02-07 22:10:23 -050043
khenaidoobf6e7bb2018-08-14 22:27:29 -040044type APIHandler struct {
khenaidoob9203542018-09-17 22:56:37 -040045 deviceMgr *DeviceManager
46 logicalDeviceMgr *LogicalDeviceManager
khenaidoo21d51152019-02-01 13:48:37 -050047 adapterMgr *AdapterManager
khenaidood2b6df92018-12-13 16:37:20 -050048 packetInQueue *queue.Queue
Richard Jankowski199fd862019-03-18 14:49:51 -040049 changeEventQueue *queue.Queue
khenaidoo9cdc1a62019-01-24 21:57:40 -050050 coreInCompetingMode bool
khenaidoob6080322019-01-29 21:47:38 -050051 longRunningRequestTimeout int64
52 defaultRequestTimeout int64
khenaidoobf6e7bb2018-08-14 22:27:29 -040053 da.DefaultAPIHandler
khenaidoo54e0ddf2019-02-27 16:21:33 -050054 core *Core
khenaidoobf6e7bb2018-08-14 22:27:29 -040055}
56
khenaidoo54e0ddf2019-02-27 16:21:33 -050057func NewAPIHandler(core *Core) *APIHandler {
Stephane Barbarie6e1bd502018-11-05 22:44:45 -050058 handler := &APIHandler{
khenaidoo54e0ddf2019-02-27 16:21:33 -050059 deviceMgr: core.deviceMgr,
60 logicalDeviceMgr: core.logicalDeviceMgr,
61 adapterMgr: core.adapterMgr,
62 coreInCompetingMode: core.config.InCompetingMode,
63 longRunningRequestTimeout:core.config.LongRunningRequestTimeout,
64 defaultRequestTimeout:core.config.DefaultRequestTimeout,
Richard Jankowskidbab94a2018-12-06 16:20:25 -050065 // TODO: Figure out what the 'hint' parameter to queue.New does
khenaidood2b6df92018-12-13 16:37:20 -050066 packetInQueue: queue.New(10),
Richard Jankowski199fd862019-03-18 14:49:51 -040067 changeEventQueue: queue.New(10),
khenaidoo54e0ddf2019-02-27 16:21:33 -050068 core: core,
Stephane Barbarie6e1bd502018-11-05 22:44:45 -050069 }
khenaidoobf6e7bb2018-08-14 22:27:29 -040070 return handler
71}
khenaidoo4d4802d2018-10-04 21:59:49 -040072
73// isTestMode is a helper function to determine a function is invoked for testing only
khenaidoobf6e7bb2018-08-14 22:27:29 -040074func isTestMode(ctx context.Context) bool {
75 md, _ := metadata.FromIncomingContext(ctx)
76 _, exist := md[common.TestModeKeys_api_test.String()]
77 return exist
78}
79
Richard Jankowskid42826e2018-11-02 16:06:37 -040080// This function attempts to extract the serial number from the request metadata
81// and create a KV transaction for that serial number for the current core.
82func (handler *APIHandler) createKvTransaction(ctx context.Context) (*KVTransaction, error) {
83 var (
khenaidoo43c82122018-11-22 18:38:28 -050084 err error
85 ok bool
86 md metadata.MD
Richard Jankowskid42826e2018-11-02 16:06:37 -040087 serNum []string
88 )
89 if md, ok = metadata.FromIncomingContext(ctx); !ok {
90 err = errors.New("metadata-not-found")
91 } else if serNum, ok = md["voltha_serial_number"]; !ok {
92 err = errors.New("serial-number-not-found")
93 }
94 if !ok {
95 log.Error(err)
96 return nil, err
97 }
98 // Create KV transaction
99 txn := NewKVTransaction(serNum[0])
100 return txn, nil
101}
102
Richard Jankowski2755adf2019-01-17 17:16:48 -0500103// isOFControllerRequest is a helper function to determine if a request was initiated
104// from the OpenFlow controller (or its proxy, e.g. OFAgent)
Richard Jankowski46464e92019-03-05 11:53:55 -0500105func (handler *APIHandler) isOFControllerRequest(ctx context.Context) bool {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500106 if md, ok := metadata.FromIncomingContext(ctx); ok {
107 // Metadata in context
Richard Jankowski46464e92019-03-05 11:53:55 -0500108 if _, ok = md[handler.core.config.CoreBindingKey]; ok {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500109 // OFAgent field in metadata
110 return true
111 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500112 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500113 return false
114}
115
116// competeForTransaction is a helper function to determine whether every request needs to compete with another
117// Core to execute the request
118func (handler *APIHandler) competeForTransaction() bool {
119 return handler.coreInCompetingMode
120}
121
Richard Jankowski199fd862019-03-18 14:49:51 -0400122// This function handles the creation of new devices
123func (handler *APIHandler) acquireRequest(ctx context.Context, id interface{}, maxTimeout ...int64) (*KVTransaction, error) {
khenaidoob6080322019-01-29 21:47:38 -0500124 timeout := handler.defaultRequestTimeout
khenaidoo9cdc1a62019-01-24 21:57:40 -0500125 if len(maxTimeout) > 0 {
126 timeout = maxTimeout[0]
Richard Jankowski2755adf2019-01-17 17:16:48 -0500127 }
khenaidoob6080322019-01-29 21:47:38 -0500128 log.Debugw("transaction-timeout", log.Fields{"timeout": timeout})
khenaidoo9cdc1a62019-01-24 21:57:40 -0500129 txn, err := handler.createKvTransaction(ctx)
130 if txn == nil {
131 return nil, err
132 } else if txn.Acquired(timeout) {
133 return txn, nil
134 } else {
khenaidoo297cd252019-02-07 22:10:23 -0500135 if id != nil {
136 // The id can either be a device Id or a logical device id.
khenaidoo1ce37ad2019-03-24 22:07:24 -0400137 if dId, ok := id.(*utils.DeviceID); ok {
khenaidoo297cd252019-02-07 22:10:23 -0500138 // Since this core has not processed this request, let's load the device, along with its extended
139 // family (parents and children) in memory. This will keep this core in-sync with its paired core as
140 // much as possible. The watch feature in the core model will ensure that the contents of those objects in
141 // memory are in sync.
142 time.Sleep(2 * time.Second)
khenaidoo1ce37ad2019-03-24 22:07:24 -0400143 go handler.deviceMgr.load(dId.Id)
144 } else if ldId, ok := id.(*utils.LogicalDeviceID); ok {
khenaidoo297cd252019-02-07 22:10:23 -0500145 // This will load the logical device along with its children and grandchildren
khenaidoo1ce37ad2019-03-24 22:07:24 -0400146 go handler.logicalDeviceMgr.load(ldId.Id)
khenaidoo297cd252019-02-07 22:10:23 -0500147 }
148 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500149 return nil, errors.New("failed-to-seize-request")
Richard Jankowski2755adf2019-01-17 17:16:48 -0500150 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500151}
152
Richard Jankowski199fd862019-03-18 14:49:51 -0400153// This function handles the modification or deletion of existing devices
154func (handler *APIHandler) takeRequestOwnership(ctx context.Context, id interface{}, maxTimeout ...int64) (*KVTransaction, error) {
155 timeout := handler.defaultRequestTimeout
156 if len(maxTimeout) > 0 {
157 timeout = maxTimeout[0]
158 }
159 log.Debugw("transaction-timeout", log.Fields{"timeout": timeout})
160 txn, err := handler.createKvTransaction(ctx)
161 if txn == nil {
162 return nil, err
163 }
164
165 owned := false
166 if id != nil {
khenaidoo1ce37ad2019-03-24 22:07:24 -0400167 owned = handler.core.deviceOwnership.OwnedByMe(id)
Richard Jankowski199fd862019-03-18 14:49:51 -0400168 }
169 if owned {
170 if txn.Acquired(timeout) {
171 return txn, nil
172 } else {
173 return nil, errors.New("failed-to-seize-request")
174 }
175 } else {
176 if txn.Monitor(timeout) {
177 return txn, nil
178 } else {
179 return nil, errors.New("device-not-owned")
180 }
181 }
182}
183
khenaidoo4d4802d2018-10-04 21:59:49 -0400184// waitForNilResponseOnSuccess is a helper function to wait for a response on channel ch where an nil
185// response is expected in a successful scenario
186func waitForNilResponseOnSuccess(ctx context.Context, ch chan interface{}) (*empty.Empty, error) {
187 select {
188 case res := <-ch:
189 if res == nil {
190 return new(empty.Empty), nil
191 } else if err, ok := res.(error); ok {
192 return new(empty.Empty), err
193 } else {
194 log.Warnw("unexpected-return-type", log.Fields{"result": res})
195 err = status.Errorf(codes.Internal, "%s", res)
196 return new(empty.Empty), err
197 }
198 case <-ctx.Done():
199 log.Debug("client-timeout")
200 return nil, ctx.Err()
201 }
202}
203
khenaidoobf6e7bb2018-08-14 22:27:29 -0400204func (handler *APIHandler) UpdateLogLevel(ctx context.Context, logging *voltha.Logging) (*empty.Empty, error) {
khenaidoo6f2fbe32019-01-18 16:16:50 -0500205 log.Debugw("UpdateLogLevel-request", log.Fields{"package": logging.PackageName, "intval": int(logging.Level)})
khenaidoo92e62c52018-10-03 14:02:54 -0400206 out := new(empty.Empty)
khenaidoo6f2fbe32019-01-18 16:16:50 -0500207 if logging.PackageName == "" {
208 log.SetAllLogLevel(int(logging.Level))
209 } else {
210 log.SetPackageLogLevel(logging.PackageName, int(logging.Level))
211 }
khenaidoo92e62c52018-10-03 14:02:54 -0400212 return out, nil
khenaidoobf6e7bb2018-08-14 22:27:29 -0400213}
214
khenaidoo54e0ddf2019-02-27 16:21:33 -0500215
216func (handler *APIHandler) UpdateMembership(ctx context.Context, membership *voltha.Membership) (*empty.Empty, error) {
217 log.Debugw("UpdateMembership-request", log.Fields{"membership": membership})
218 out := new(empty.Empty)
khenaidoo6417b6c2019-03-01 18:18:01 -0500219 if err := handler.core.updateCoreMembership(ctx, membership); err != nil {
khenaidoo54e0ddf2019-02-27 16:21:33 -0500220 return out, err
221 }
222 return out, nil
223}
224
khenaidoo6417b6c2019-03-01 18:18:01 -0500225func (handler *APIHandler) GetMembership(ctx context.Context, empty *empty.Empty) (*voltha.Membership, error) {
226 log.Debug("GetMembership-request")
227 if membership := handler.core.getCoreMembership(ctx); membership != nil {
228 return membership, nil
229 }
230 return &voltha.Membership{}, nil
231}
232
233
khenaidoobf6e7bb2018-08-14 22:27:29 -0400234func (handler *APIHandler) EnableLogicalDevicePort(ctx context.Context, id *voltha.LogicalPortId) (*empty.Empty, error) {
235 log.Debugw("EnableLogicalDevicePort-request", log.Fields{"id": id, "test": common.TestModeKeys_api_test.String()})
236 if isTestMode(ctx) {
237 out := new(empty.Empty)
238 return out, nil
239 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500240
khenaidoo9cdc1a62019-01-24 21:57:40 -0500241 if handler.competeForTransaction() {
khenaidoo1ce37ad2019-03-24 22:07:24 -0400242 if txn, err := handler.takeRequestOwnership(ctx, &utils.LogicalDeviceID{Id:id.Id}); err != nil {
Richard Jankowski2755adf2019-01-17 17:16:48 -0500243 return new(empty.Empty), err
Richard Jankowski2755adf2019-01-17 17:16:48 -0500244 } else {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500245 defer txn.Close()
Richard Jankowski2755adf2019-01-17 17:16:48 -0500246 }
247 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500248
khenaidoo4d4802d2018-10-04 21:59:49 -0400249 ch := make(chan interface{})
250 defer close(ch)
khenaidoo19d7b632018-10-30 10:49:50 -0400251 go handler.logicalDeviceMgr.enableLogicalPort(ctx, id, ch)
khenaidoo4d4802d2018-10-04 21:59:49 -0400252 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400253}
254
255func (handler *APIHandler) DisableLogicalDevicePort(ctx context.Context, id *voltha.LogicalPortId) (*empty.Empty, error) {
256 log.Debugw("DisableLogicalDevicePort-request", log.Fields{"id": id, "test": common.TestModeKeys_api_test.String()})
257 if isTestMode(ctx) {
258 out := new(empty.Empty)
259 return out, nil
260 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500261
khenaidoo9cdc1a62019-01-24 21:57:40 -0500262 if handler.competeForTransaction() {
khenaidoo1ce37ad2019-03-24 22:07:24 -0400263 if txn, err := handler.takeRequestOwnership(ctx, &utils.LogicalDeviceID{Id:id.Id}); err != nil {
Richard Jankowski2755adf2019-01-17 17:16:48 -0500264 return new(empty.Empty), err
Richard Jankowski2755adf2019-01-17 17:16:48 -0500265 } else {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500266 defer txn.Close()
Richard Jankowski2755adf2019-01-17 17:16:48 -0500267 }
268 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500269
khenaidoo19d7b632018-10-30 10:49:50 -0400270 ch := make(chan interface{})
271 defer close(ch)
272 go handler.logicalDeviceMgr.disableLogicalPort(ctx, id, ch)
273 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400274}
275
276func (handler *APIHandler) UpdateLogicalDeviceFlowTable(ctx context.Context, flow *openflow_13.FlowTableUpdate) (*empty.Empty, error) {
277 log.Debugw("UpdateLogicalDeviceFlowTable-request", log.Fields{"flow": flow, "test": common.TestModeKeys_api_test.String()})
278 if isTestMode(ctx) {
279 out := new(empty.Empty)
280 return out, nil
281 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500282
khenaidoo9cdc1a62019-01-24 21:57:40 -0500283 if handler.competeForTransaction() {
Richard Jankowski46464e92019-03-05 11:53:55 -0500284 if !handler.isOFControllerRequest(ctx) { // No need to acquire the transaction as request is sent to one core only
khenaidoo1ce37ad2019-03-24 22:07:24 -0400285 if txn, err := handler.takeRequestOwnership(ctx, &utils.LogicalDeviceID{Id:flow.Id}); err != nil {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500286 return new(empty.Empty), err
287 } else {
288 defer txn.Close()
289 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500290 }
291 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500292
khenaidoo19d7b632018-10-30 10:49:50 -0400293 ch := make(chan interface{})
294 defer close(ch)
295 go handler.logicalDeviceMgr.updateFlowTable(ctx, flow.Id, flow.FlowMod, ch)
296 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400297}
298
299func (handler *APIHandler) UpdateLogicalDeviceFlowGroupTable(ctx context.Context, flow *openflow_13.FlowGroupTableUpdate) (*empty.Empty, error) {
300 log.Debugw("UpdateLogicalDeviceFlowGroupTable-request", log.Fields{"flow": flow, "test": common.TestModeKeys_api_test.String()})
301 if isTestMode(ctx) {
302 out := new(empty.Empty)
303 return out, nil
304 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500305
khenaidoo9cdc1a62019-01-24 21:57:40 -0500306 if handler.competeForTransaction() {
Richard Jankowski46464e92019-03-05 11:53:55 -0500307 if !handler.isOFControllerRequest(ctx) { // No need to acquire the transaction as request is sent to one core only
khenaidoo1ce37ad2019-03-24 22:07:24 -0400308 if txn, err := handler.takeRequestOwnership(ctx, &utils.LogicalDeviceID{Id:flow.Id}); err != nil {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500309 return new(empty.Empty), err
310 } else {
311 defer txn.Close()
312 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500313 }
314 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500315
khenaidoo19d7b632018-10-30 10:49:50 -0400316 ch := make(chan interface{})
317 defer close(ch)
318 go handler.logicalDeviceMgr.updateGroupTable(ctx, flow.Id, flow.GroupMod, ch)
319 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400320}
321
khenaidoob9203542018-09-17 22:56:37 -0400322// GetDevice must be implemented in the read-only containers - should it also be implemented here?
323func (handler *APIHandler) GetDevice(ctx context.Context, id *voltha.ID) (*voltha.Device, error) {
324 log.Debugw("GetDevice-request", log.Fields{"id": id})
khenaidoo19d7b632018-10-30 10:49:50 -0400325 return handler.deviceMgr.GetDevice(id.Id)
khenaidoob9203542018-09-17 22:56:37 -0400326}
327
328// GetDevice must be implemented in the read-only containers - should it also be implemented here?
329func (handler *APIHandler) ListDevices(ctx context.Context, empty *empty.Empty) (*voltha.Devices, error) {
330 log.Debug("ListDevices")
331 return handler.deviceMgr.ListDevices()
332}
333
khenaidoo7ccedd52018-12-14 16:48:54 -0500334// ListDeviceIds returns the list of device ids managed by a voltha core
335func (handler *APIHandler) ListDeviceIds(ctx context.Context, empty *empty.Empty) (*voltha.IDs, error) {
336 log.Debug("ListDeviceIDs")
337 if isTestMode(ctx) {
338 out := &voltha.IDs{Items: make([]*voltha.ID, 0)}
339 return out, nil
340 }
341 return handler.deviceMgr.ListDeviceIds()
342}
343
344//ReconcileDevices is a request to a voltha core to managed a list of devices based on their IDs
345func (handler *APIHandler) ReconcileDevices(ctx context.Context, ids *voltha.IDs) (*empty.Empty, error) {
346 log.Debug("ReconcileDevices")
347 if isTestMode(ctx) {
348 out := new(empty.Empty)
349 return out, nil
350 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500351
khenaidoo9cdc1a62019-01-24 21:57:40 -0500352 // No need to grab a transaction as this request is core specific
353
khenaidoo7ccedd52018-12-14 16:48:54 -0500354 ch := make(chan interface{})
355 defer close(ch)
356 go handler.deviceMgr.ReconcileDevices(ctx, ids, ch)
357 return waitForNilResponseOnSuccess(ctx, ch)
358}
359
khenaidoob9203542018-09-17 22:56:37 -0400360// GetLogicalDevice must be implemented in the read-only containers - should it also be implemented here?
361func (handler *APIHandler) GetLogicalDevice(ctx context.Context, id *voltha.ID) (*voltha.LogicalDevice, error) {
362 log.Debugw("GetLogicalDevice-request", log.Fields{"id": id})
363 return handler.logicalDeviceMgr.getLogicalDevice(id.Id)
364}
365
366// ListLogicalDevices must be implemented in the read-only containers - should it also be implemented here?
367func (handler *APIHandler) ListLogicalDevices(ctx context.Context, empty *empty.Empty) (*voltha.LogicalDevices, error) {
368 log.Debug("ListLogicalDevices")
369 return handler.logicalDeviceMgr.listLogicalDevices()
370}
371
khenaidoo21d51152019-02-01 13:48:37 -0500372
373// ListAdapters returns the contents of all adapters known to the system
374func (handler *APIHandler) ListAdapters(ctx context.Context, empty *empty.Empty) (*voltha.Adapters, error) {
375 log.Debug("ListDevices")
376 return handler.adapterMgr.listAdapters(ctx)
377}
378
khenaidoo19d7b632018-10-30 10:49:50 -0400379// ListLogicalDevicePorts must be implemented in the read-only containers - should it also be implemented here?
380func (handler *APIHandler) ListLogicalDevicePorts(ctx context.Context, id *voltha.ID) (*voltha.LogicalPorts, error) {
381 log.Debugw("ListLogicalDevicePorts", log.Fields{"logicaldeviceid": id})
382 return handler.logicalDeviceMgr.ListLogicalDevicePorts(ctx, id.Id)
383}
384
khenaidoo4d4802d2018-10-04 21:59:49 -0400385// CreateDevice creates a new parent device in the data model
khenaidoobf6e7bb2018-08-14 22:27:29 -0400386func (handler *APIHandler) CreateDevice(ctx context.Context, device *voltha.Device) (*voltha.Device, error) {
khenaidoob9203542018-09-17 22:56:37 -0400387 log.Debugw("createdevice", log.Fields{"device": *device})
khenaidoobf6e7bb2018-08-14 22:27:29 -0400388 if isTestMode(ctx) {
389 return &voltha.Device{Id: device.Id}, nil
390 }
Richard Jankowskid42826e2018-11-02 16:06:37 -0400391
khenaidoo9cdc1a62019-01-24 21:57:40 -0500392 if handler.competeForTransaction() {
Richard Jankowski199fd862019-03-18 14:49:51 -0400393 if txn, err := handler.acquireRequest(ctx, nil); err != nil {
Richard Jankowski2755adf2019-01-17 17:16:48 -0500394 return &voltha.Device{}, err
Richard Jankowski2755adf2019-01-17 17:16:48 -0500395 } else {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500396 defer txn.Close()
Richard Jankowski2755adf2019-01-17 17:16:48 -0500397 }
398 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500399
khenaidoob9203542018-09-17 22:56:37 -0400400 ch := make(chan interface{})
401 defer close(ch)
402 go handler.deviceMgr.createDevice(ctx, device, ch)
403 select {
404 case res := <-ch:
khenaidoo92e62c52018-10-03 14:02:54 -0400405 if res != nil {
406 if err, ok := res.(error); ok {
407 return &voltha.Device{}, err
408 }
409 if d, ok := res.(*voltha.Device); ok {
khenaidoo1ce37ad2019-03-24 22:07:24 -0400410 handler.core.deviceOwnership.OwnedByMe(&utils.DeviceID{Id:d.Id})
khenaidoo92e62c52018-10-03 14:02:54 -0400411 return d, nil
412 }
khenaidoob9203542018-09-17 22:56:37 -0400413 }
khenaidoo92e62c52018-10-03 14:02:54 -0400414 log.Warnw("create-device-unexpected-return-type", log.Fields{"result": res})
415 err := status.Errorf(codes.Internal, "%s", res)
416 return &voltha.Device{}, err
khenaidoob9203542018-09-17 22:56:37 -0400417 case <-ctx.Done():
418 log.Debug("createdevice-client-timeout")
419 return nil, ctx.Err()
420 }
khenaidoobf6e7bb2018-08-14 22:27:29 -0400421}
422
khenaidoo4d4802d2018-10-04 21:59:49 -0400423// EnableDevice activates a device by invoking the adopt_device API on the appropriate adapter
khenaidoobf6e7bb2018-08-14 22:27:29 -0400424func (handler *APIHandler) EnableDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) {
khenaidoob9203542018-09-17 22:56:37 -0400425 log.Debugw("enabledevice", log.Fields{"id": id})
khenaidoobf6e7bb2018-08-14 22:27:29 -0400426 if isTestMode(ctx) {
khenaidoo4d4802d2018-10-04 21:59:49 -0400427 return new(empty.Empty), nil
khenaidoobf6e7bb2018-08-14 22:27:29 -0400428 }
Richard Jankowskid42826e2018-11-02 16:06:37 -0400429
khenaidoo9cdc1a62019-01-24 21:57:40 -0500430 if handler.competeForTransaction() {
khenaidoo1ce37ad2019-03-24 22:07:24 -0400431 if txn, err := handler.takeRequestOwnership(ctx, &utils.DeviceID{Id:id.Id}, handler.longRunningRequestTimeout); err != nil {
Richard Jankowski2755adf2019-01-17 17:16:48 -0500432 return new(empty.Empty), err
Richard Jankowski2755adf2019-01-17 17:16:48 -0500433 } else {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500434 defer txn.Close()
Richard Jankowski2755adf2019-01-17 17:16:48 -0500435 }
436 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500437
khenaidoob9203542018-09-17 22:56:37 -0400438 ch := make(chan interface{})
439 defer close(ch)
440 go handler.deviceMgr.enableDevice(ctx, id, ch)
khenaidoo4d4802d2018-10-04 21:59:49 -0400441 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400442}
443
khenaidoo4d4802d2018-10-04 21:59:49 -0400444// DisableDevice disables a device along with any child device it may have
khenaidoobf6e7bb2018-08-14 22:27:29 -0400445func (handler *APIHandler) DisableDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) {
446 log.Debugw("disabledevice-request", log.Fields{"id": id})
447 if isTestMode(ctx) {
khenaidoo4d4802d2018-10-04 21:59:49 -0400448 return new(empty.Empty), nil
khenaidoobf6e7bb2018-08-14 22:27:29 -0400449 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500450
khenaidoo9cdc1a62019-01-24 21:57:40 -0500451 if handler.competeForTransaction() {
khenaidoo1ce37ad2019-03-24 22:07:24 -0400452 if txn, err := handler.takeRequestOwnership(ctx, &utils.DeviceID{Id:id.Id}); err != nil {
Richard Jankowski2755adf2019-01-17 17:16:48 -0500453 return new(empty.Empty), err
Richard Jankowski2755adf2019-01-17 17:16:48 -0500454 } else {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500455 defer txn.Close()
Richard Jankowski2755adf2019-01-17 17:16:48 -0500456 }
457 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500458
khenaidoo92e62c52018-10-03 14:02:54 -0400459 ch := make(chan interface{})
460 defer close(ch)
461 go handler.deviceMgr.disableDevice(ctx, id, ch)
khenaidoo4d4802d2018-10-04 21:59:49 -0400462 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400463}
464
khenaidoo4d4802d2018-10-04 21:59:49 -0400465//RebootDevice invoked the reboot API to the corresponding adapter
khenaidoobf6e7bb2018-08-14 22:27:29 -0400466func (handler *APIHandler) RebootDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) {
khenaidoo4d4802d2018-10-04 21:59:49 -0400467 log.Debugw("rebootDevice-request", log.Fields{"id": id})
khenaidoobf6e7bb2018-08-14 22:27:29 -0400468 if isTestMode(ctx) {
khenaidoo4d4802d2018-10-04 21:59:49 -0400469 return new(empty.Empty), nil
khenaidoobf6e7bb2018-08-14 22:27:29 -0400470 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500471
khenaidoo9cdc1a62019-01-24 21:57:40 -0500472 if handler.competeForTransaction() {
khenaidoo1ce37ad2019-03-24 22:07:24 -0400473 if txn, err := handler.takeRequestOwnership(ctx, &utils.DeviceID{Id:id.Id}); err != nil {
Richard Jankowski2755adf2019-01-17 17:16:48 -0500474 return new(empty.Empty), err
Richard Jankowski2755adf2019-01-17 17:16:48 -0500475 } else {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500476 defer txn.Close()
Richard Jankowski2755adf2019-01-17 17:16:48 -0500477 }
478 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500479
khenaidoo4d4802d2018-10-04 21:59:49 -0400480 ch := make(chan interface{})
481 defer close(ch)
482 go handler.deviceMgr.rebootDevice(ctx, id, ch)
483 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400484}
485
khenaidoo4d4802d2018-10-04 21:59:49 -0400486// DeleteDevice removes a device from the data model
khenaidoobf6e7bb2018-08-14 22:27:29 -0400487func (handler *APIHandler) DeleteDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) {
488 log.Debugw("deletedevice-request", log.Fields{"id": id})
489 if isTestMode(ctx) {
khenaidoo4d4802d2018-10-04 21:59:49 -0400490 return new(empty.Empty), nil
khenaidoobf6e7bb2018-08-14 22:27:29 -0400491 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500492
khenaidoo9cdc1a62019-01-24 21:57:40 -0500493 if handler.competeForTransaction() {
Richard Jankowski199fd862019-03-18 14:49:51 -0400494 if txn, err := handler.takeRequestOwnership(ctx, nil); err != nil {
Richard Jankowski2755adf2019-01-17 17:16:48 -0500495 return new(empty.Empty), err
Richard Jankowski2755adf2019-01-17 17:16:48 -0500496 } else {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500497 defer txn.Close()
Richard Jankowski2755adf2019-01-17 17:16:48 -0500498 }
499 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500500
khenaidoo4d4802d2018-10-04 21:59:49 -0400501 ch := make(chan interface{})
502 defer close(ch)
503 go handler.deviceMgr.deleteDevice(ctx, id, ch)
504 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400505}
506
khenaidoof5a5bfa2019-01-23 22:20:29 -0500507// processImageRequest is a helper method to execute an image download request
508func (handler *APIHandler) processImageRequest(ctx context.Context, img *voltha.ImageDownload, requestType int) (*common.OperationResp, error) {
509 log.Debugw("processImageDownload", log.Fields{"img": *img, "requestType": requestType})
510 if isTestMode(ctx) {
511 resp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS}
512 return resp, nil
513 }
514
khenaidoo9cdc1a62019-01-24 21:57:40 -0500515 if handler.competeForTransaction() {
khenaidoo1ce37ad2019-03-24 22:07:24 -0400516 if txn, err := handler.takeRequestOwnership(ctx, &utils.DeviceID{Id:img.Id}); err != nil {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500517 return &common.OperationResp{}, err
518 } else {
519 defer txn.Close()
520 }
khenaidoof5a5bfa2019-01-23 22:20:29 -0500521 }
522
523 failedresponse := &common.OperationResp{Code:voltha.OperationResp_OPERATION_FAILURE}
524
525 ch := make(chan interface{})
526 defer close(ch)
527 switch requestType {
528 case IMAGE_DOWNLOAD:
529 go handler.deviceMgr.downloadImage(ctx, img, ch)
530 case CANCEL_IMAGE_DOWNLOAD:
531 go handler.deviceMgr.cancelImageDownload(ctx, img, ch)
532 case ACTIVATE_IMAGE:
533 go handler.deviceMgr.activateImage(ctx, img, ch)
534 case REVERT_IMAGE:
535 go handler.deviceMgr.revertImage(ctx, img, ch)
536 default:
537 log.Warn("invalid-request-type", log.Fields{"requestType": requestType})
538 return failedresponse, status.Errorf(codes.InvalidArgument, "%d", requestType)
539 }
540 select {
541 case res := <-ch:
542 if res != nil {
543 if err, ok := res.(error); ok {
544 return failedresponse, err
545 }
546 if opResp, ok := res.(*common.OperationResp); ok {
547 return opResp, nil
548 }
549 }
550 log.Warnw("download-image-unexpected-return-type", log.Fields{"result": res})
551 return failedresponse, status.Errorf(codes.Internal, "%s", res)
552 case <-ctx.Done():
553 log.Debug("downloadImage-client-timeout")
554 return nil, ctx.Err()
555 }
556}
557
khenaidoobf6e7bb2018-08-14 22:27:29 -0400558func (handler *APIHandler) DownloadImage(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) {
559 log.Debugw("DownloadImage-request", log.Fields{"img": *img})
560 if isTestMode(ctx) {
561 resp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS}
562 return resp, nil
563 }
564
khenaidoof5a5bfa2019-01-23 22:20:29 -0500565 return handler.processImageRequest(ctx, img, IMAGE_DOWNLOAD)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400566}
567
568func (handler *APIHandler) CancelImageDownload(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) {
khenaidoof5a5bfa2019-01-23 22:20:29 -0500569 log.Debugw("cancelImageDownload-request", log.Fields{"img": *img})
khenaidoobf6e7bb2018-08-14 22:27:29 -0400570 if isTestMode(ctx) {
571 resp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS}
572 return resp, nil
573 }
khenaidoof5a5bfa2019-01-23 22:20:29 -0500574 return handler.processImageRequest(ctx, img, CANCEL_IMAGE_DOWNLOAD)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400575}
576
577func (handler *APIHandler) ActivateImageUpdate(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) {
khenaidoof5a5bfa2019-01-23 22:20:29 -0500578 log.Debugw("activateImageUpdate-request", log.Fields{"img": *img})
khenaidoobf6e7bb2018-08-14 22:27:29 -0400579 if isTestMode(ctx) {
580 resp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS}
581 return resp, nil
582 }
khenaidoof5a5bfa2019-01-23 22:20:29 -0500583
584 return handler.processImageRequest(ctx, img, ACTIVATE_IMAGE)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400585}
586
587func (handler *APIHandler) RevertImageUpdate(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) {
khenaidoof5a5bfa2019-01-23 22:20:29 -0500588 log.Debugw("revertImageUpdate-request", log.Fields{"img": *img})
khenaidoobf6e7bb2018-08-14 22:27:29 -0400589 if isTestMode(ctx) {
590 resp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS}
591 return resp, nil
592 }
khenaidoof5a5bfa2019-01-23 22:20:29 -0500593
594 return handler.processImageRequest(ctx, img, REVERT_IMAGE)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400595}
596
khenaidoof5a5bfa2019-01-23 22:20:29 -0500597func (handler *APIHandler) GetImageDownloadStatus(ctx context.Context, img *voltha.ImageDownload) (*voltha.ImageDownload, error) {
598 log.Debugw("getImageDownloadStatus-request", log.Fields{"img": *img})
599 if isTestMode(ctx) {
Stephane Barbariedf5479f2019-01-29 22:13:00 -0500600 resp := &voltha.ImageDownload{DownloadState: voltha.ImageDownload_DOWNLOAD_SUCCEEDED}
khenaidoof5a5bfa2019-01-23 22:20:29 -0500601 return resp, nil
602 }
603
Stephane Barbariedf5479f2019-01-29 22:13:00 -0500604 failedresponse := &voltha.ImageDownload{DownloadState: voltha.ImageDownload_DOWNLOAD_UNKNOWN}
khenaidoof5a5bfa2019-01-23 22:20:29 -0500605
khenaidoo9cdc1a62019-01-24 21:57:40 -0500606 if handler.competeForTransaction() {
khenaidoo1ce37ad2019-03-24 22:07:24 -0400607 if txn, err := handler.takeRequestOwnership(ctx, &utils.DeviceID{Id:img.Id}); err != nil {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500608 return failedresponse, err
609 } else {
610 defer txn.Close()
611 }
khenaidoof5a5bfa2019-01-23 22:20:29 -0500612 }
613
614 ch := make(chan interface{})
615 defer close(ch)
616 go handler.deviceMgr.getImageDownloadStatus(ctx, img, ch)
617
618 select {
619 case res := <-ch:
620 if res != nil {
621 if err, ok := res.(error); ok {
622 return failedresponse, err
623 }
624 if downloadResp, ok := res.(*voltha.ImageDownload); ok {
625 return downloadResp, nil
626 }
627 }
628 log.Warnw("download-image-status", log.Fields{"result": res})
629 return failedresponse, status.Errorf(codes.Internal, "%s", res)
630 case <-ctx.Done():
631 log.Debug("downloadImage-client-timeout")
632 return failedresponse, ctx.Err()
633 }
634}
635
636func (handler *APIHandler) GetImageDownload(ctx context.Context, img *voltha.ImageDownload) (*voltha.ImageDownload, error) {
637 log.Debugw("GetImageDownload-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
643 if download, err := handler.deviceMgr.getImageDownload(ctx, img); err != nil {
Stephane Barbariedf5479f2019-01-29 22:13:00 -0500644 return &voltha.ImageDownload{DownloadState: voltha.ImageDownload_DOWNLOAD_UNKNOWN}, err
khenaidoof5a5bfa2019-01-23 22:20:29 -0500645 } else {
646 return download, nil
647 }
648}
649
650func (handler *APIHandler) ListImageDownloads(ctx context.Context, id *voltha.ID) (*voltha.ImageDownloads, error) {
651 log.Debugw("ListImageDownloads-request", log.Fields{"deviceId": id.Id})
652 if isTestMode(ctx) {
653 resp := &voltha.ImageDownloads{Items:[]*voltha.ImageDownload{}}
654 return resp, nil
655 }
656
657 if downloads, err := handler.deviceMgr.listImageDownloads(ctx, id.Id); err != nil {
658 failedResp := &voltha.ImageDownloads{
659 Items:[]*voltha.ImageDownload{
Stephane Barbariedf5479f2019-01-29 22:13:00 -0500660 &voltha.ImageDownload{DownloadState: voltha.ImageDownload_DOWNLOAD_UNKNOWN},
khenaidoof5a5bfa2019-01-23 22:20:29 -0500661 },
662 }
663 return failedResp, err
664 } else {
665 return downloads, nil
666 }
667}
668
669
khenaidoobf6e7bb2018-08-14 22:27:29 -0400670func (handler *APIHandler) UpdateDevicePmConfigs(ctx context.Context, configs *voltha.PmConfigs) (*empty.Empty, error) {
671 log.Debugw("UpdateDevicePmConfigs-request", log.Fields{"configs": *configs})
672 if isTestMode(ctx) {
673 out := new(empty.Empty)
674 return out, nil
675 }
676 return nil, errors.New("UnImplemented")
677}
678
679func (handler *APIHandler) CreateAlarmFilter(ctx context.Context, filter *voltha.AlarmFilter) (*voltha.AlarmFilter, error) {
680 log.Debugw("CreateAlarmFilter-request", log.Fields{"filter": *filter})
681 if isTestMode(ctx) {
682 f := &voltha.AlarmFilter{Id: filter.Id}
683 return f, nil
684 }
685 return nil, errors.New("UnImplemented")
686}
687
688func (handler *APIHandler) UpdateAlarmFilter(ctx context.Context, filter *voltha.AlarmFilter) (*voltha.AlarmFilter, error) {
689 log.Debugw("UpdateAlarmFilter-request", log.Fields{"filter": *filter})
690 if isTestMode(ctx) {
691 f := &voltha.AlarmFilter{Id: filter.Id}
692 return f, nil
693 }
694 return nil, errors.New("UnImplemented")
695}
696
697func (handler *APIHandler) DeleteAlarmFilter(ctx context.Context, id *voltha.ID) (*empty.Empty, error) {
698 log.Debugw("DeleteAlarmFilter-request", log.Fields{"id": *id})
699 if isTestMode(ctx) {
700 out := new(empty.Empty)
701 return out, nil
702 }
703 return nil, errors.New("UnImplemented")
704}
705
706func (handler *APIHandler) SelfTest(ctx context.Context, id *voltha.ID) (*voltha.SelfTestResponse, error) {
707 log.Debugw("SelfTest-request", log.Fields{"id": id})
708 if isTestMode(ctx) {
709 resp := &voltha.SelfTestResponse{Result: voltha.SelfTestResponse_SUCCESS}
710 return resp, nil
711 }
712 return nil, errors.New("UnImplemented")
713}
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500714
715func (handler *APIHandler) forwardPacketOut(packet *openflow_13.PacketOut) {
716 log.Debugw("forwardPacketOut-request", log.Fields{"packet": packet})
Richard Jankowski2755adf2019-01-17 17:16:48 -0500717 agent := handler.logicalDeviceMgr.getLogicalDeviceAgent(packet.Id)
718 agent.packetOut(packet.PacketOut)
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500719}
720func (handler *APIHandler) StreamPacketsOut(
721 packets voltha.VolthaService_StreamPacketsOutServer,
722) error {
723 log.Debugw("StreamPacketsOut-request", log.Fields{"packets": packets})
724 for {
725 packet, err := packets.Recv()
726
727 if err == io.EOF {
728 break
729 } else if err != nil {
730 log.Errorw("Failed to receive packet", log.Fields{"error": err})
731 }
732
733 handler.forwardPacketOut(packet)
734 }
735
736 log.Debugw("StreamPacketsOut-request-done", log.Fields{"packets": packets})
737 return nil
738}
739
khenaidoo297cd252019-02-07 22:10:23 -0500740func (handler *APIHandler) sendPacketIn(deviceId string, transationId string, packet *openflow_13.OfpPacketIn) {
741 // TODO: Augment the OF PacketIn to include the transactionId
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500742 packetIn := openflow_13.PacketIn{Id: deviceId, PacketIn: packet}
743 log.Debugw("sendPacketIn", log.Fields{"packetIn": packetIn})
Richard Jankowskidbab94a2018-12-06 16:20:25 -0500744 // Enqueue the packet
745 if err := handler.packetInQueue.Put(packetIn); err != nil {
746 log.Errorw("failed-to-enqueue-packet", log.Fields{"error": err})
747 }
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500748}
749
750func (handler *APIHandler) ReceivePacketsIn(
751 empty *empty.Empty,
752 packetsIn voltha.VolthaService_ReceivePacketsInServer,
753) error {
754 log.Debugw("ReceivePacketsIn-request", log.Fields{"packetsIn": packetsIn})
755
756 for {
Richard Jankowskidbab94a2018-12-06 16:20:25 -0500757 // Dequeue a packet
758 if packets, err := handler.packetInQueue.Get(1); err == nil {
759 log.Debugw("dequeued-packet", log.Fields{"packet": packets[0]})
760 if packet, ok := packets[0].(openflow_13.PacketIn); ok {
761 log.Debugw("sending-packet-in", log.Fields{"packet": packet})
762 if err := packetsIn.Send(&packet); err != nil {
763 log.Errorw("failed-to-send-packet", log.Fields{"error": err})
764 }
765 }
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500766 }
767 }
khenaidoof5a5bfa2019-01-23 22:20:29 -0500768 //TODO: FInd an elegant way to get out of the above loop when the Core is stopped
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500769}
770
771func (handler *APIHandler) sendChangeEvent(deviceId string, portStatus *openflow_13.OfpPortStatus) {
772 // TODO: validate the type of portStatus parameter
773 //if _, ok := portStatus.(*openflow_13.OfpPortStatus); ok {
774 //}
775 event := openflow_13.ChangeEvent{Id: deviceId, Event: &openflow_13.ChangeEvent_PortStatus{PortStatus: portStatus}}
776 log.Debugw("sendChangeEvent", log.Fields{"event": event})
Richard Jankowski199fd862019-03-18 14:49:51 -0400777 // Enqueue the change event
778 if err := handler.changeEventQueue.Put(event); err != nil {
779 log.Errorw("failed-to-enqueue-change-event", log.Fields{"error": err})
780 }
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500781}
782
783func (handler *APIHandler) ReceiveChangeEvents(
784 empty *empty.Empty,
785 changeEvents voltha.VolthaService_ReceiveChangeEventsServer,
786) error {
787 log.Debugw("ReceiveChangeEvents-request", log.Fields{"changeEvents": changeEvents})
788 for {
Richard Jankowski199fd862019-03-18 14:49:51 -0400789 // Dequeue a change event
790 if events, err := handler.changeEventQueue.Get(1); err == nil {
791 log.Debugw("dequeued-change-event", log.Fields{"event": events[0]})
792 if event, ok := events[0].(openflow_13.ChangeEvent); ok {
793 log.Debugw("sending-change-event", log.Fields{"event": event})
794 if err := changeEvents.Send(&event); err != nil {
795 log.Errorw("failed-to-send-change-event", log.Fields{"error": err})
796 }
797 }
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500798 }
799 }
Richard Jankowski199fd862019-03-18 14:49:51 -0400800}
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500801
802func (handler *APIHandler) Subscribe(
803 ctx context.Context,
804 ofAgent *voltha.OfAgentSubscriber,
805) (*voltha.OfAgentSubscriber, error) {
806 log.Debugw("Subscribe-request", log.Fields{"ofAgent": ofAgent})
807 return &voltha.OfAgentSubscriber{OfagentId: ofAgent.OfagentId, VolthaId: ofAgent.VolthaId}, nil
808}