blob: d7834eb9911d2205d530e410f16a2e4bac04feca [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"
khenaidoob9203542018-09-17 22:56:37 -040028 "google.golang.org/grpc/codes"
khenaidoobf6e7bb2018-08-14 22:27:29 -040029 "google.golang.org/grpc/metadata"
khenaidoob9203542018-09-17 22:56:37 -040030 "google.golang.org/grpc/status"
Stephane Barbarie6e1bd502018-11-05 22:44:45 -050031 "io"
32 "time"
khenaidoobf6e7bb2018-08-14 22:27:29 -040033)
34
khenaidoof5a5bfa2019-01-23 22:20:29 -050035const (
36 IMAGE_DOWNLOAD = iota
37 CANCEL_IMAGE_DOWNLOAD = iota
38 ACTIVATE_IMAGE = iota
39 REVERT_IMAGE = iota
40)
41
khenaidoo297cd252019-02-07 22:10:23 -050042
43type deviceID struct {
44 id string
45}
46
47type logicalDeviceID struct {
48 id string
49}
50
khenaidoobf6e7bb2018-08-14 22:27:29 -040051type APIHandler struct {
khenaidoob9203542018-09-17 22:56:37 -040052 deviceMgr *DeviceManager
53 logicalDeviceMgr *LogicalDeviceManager
khenaidoo21d51152019-02-01 13:48:37 -050054 adapterMgr *AdapterManager
khenaidood2b6df92018-12-13 16:37:20 -050055 packetInQueue *queue.Queue
Richard Jankowski199fd862019-03-18 14:49:51 -040056 changeEventQueue *queue.Queue
khenaidoo9cdc1a62019-01-24 21:57:40 -050057 coreInCompetingMode bool
khenaidoob6080322019-01-29 21:47:38 -050058 longRunningRequestTimeout int64
59 defaultRequestTimeout int64
khenaidoobf6e7bb2018-08-14 22:27:29 -040060 da.DefaultAPIHandler
khenaidoo54e0ddf2019-02-27 16:21:33 -050061 core *Core
khenaidoobf6e7bb2018-08-14 22:27:29 -040062}
63
khenaidoo54e0ddf2019-02-27 16:21:33 -050064//func NewAPIHandler(deviceMgr *DeviceManager, lDeviceMgr *LogicalDeviceManager, adapterMgr *AdapterManager, inCompetingMode bool, longRunningRequestTimeout int64, defaultRequestTimeout int64 ) *APIHandler {
65// handler := &APIHandler{
66// deviceMgr: deviceMgr,
67// logicalDeviceMgr: lDeviceMgr,
68// adapterMgr:adapterMgr,
69// coreInCompetingMode:inCompetingMode,
70// longRunningRequestTimeout:longRunningRequestTimeout,
71// defaultRequestTimeout:defaultRequestTimeout,
72// // TODO: Figure out what the 'hint' parameter to queue.New does
73// packetInQueue: queue.New(10),
74// }
75// return handler
76//}
77
78func NewAPIHandler(core *Core) *APIHandler {
Stephane Barbarie6e1bd502018-11-05 22:44:45 -050079 handler := &APIHandler{
khenaidoo54e0ddf2019-02-27 16:21:33 -050080 deviceMgr: core.deviceMgr,
81 logicalDeviceMgr: core.logicalDeviceMgr,
82 adapterMgr: core.adapterMgr,
83 coreInCompetingMode: core.config.InCompetingMode,
84 longRunningRequestTimeout:core.config.LongRunningRequestTimeout,
85 defaultRequestTimeout:core.config.DefaultRequestTimeout,
Richard Jankowskidbab94a2018-12-06 16:20:25 -050086 // TODO: Figure out what the 'hint' parameter to queue.New does
khenaidood2b6df92018-12-13 16:37:20 -050087 packetInQueue: queue.New(10),
Richard Jankowski199fd862019-03-18 14:49:51 -040088 changeEventQueue: queue.New(10),
khenaidoo54e0ddf2019-02-27 16:21:33 -050089 core: core,
Stephane Barbarie6e1bd502018-11-05 22:44:45 -050090 }
khenaidoobf6e7bb2018-08-14 22:27:29 -040091 return handler
92}
khenaidoo4d4802d2018-10-04 21:59:49 -040093
94// isTestMode is a helper function to determine a function is invoked for testing only
khenaidoobf6e7bb2018-08-14 22:27:29 -040095func isTestMode(ctx context.Context) bool {
96 md, _ := metadata.FromIncomingContext(ctx)
97 _, exist := md[common.TestModeKeys_api_test.String()]
98 return exist
99}
100
Richard Jankowskid42826e2018-11-02 16:06:37 -0400101// This function attempts to extract the serial number from the request metadata
102// and create a KV transaction for that serial number for the current core.
103func (handler *APIHandler) createKvTransaction(ctx context.Context) (*KVTransaction, error) {
104 var (
khenaidoo43c82122018-11-22 18:38:28 -0500105 err error
106 ok bool
107 md metadata.MD
Richard Jankowskid42826e2018-11-02 16:06:37 -0400108 serNum []string
109 )
110 if md, ok = metadata.FromIncomingContext(ctx); !ok {
111 err = errors.New("metadata-not-found")
112 } else if serNum, ok = md["voltha_serial_number"]; !ok {
113 err = errors.New("serial-number-not-found")
114 }
115 if !ok {
116 log.Error(err)
117 return nil, err
118 }
119 // Create KV transaction
120 txn := NewKVTransaction(serNum[0])
121 return txn, nil
122}
123
Richard Jankowski2755adf2019-01-17 17:16:48 -0500124// isOFControllerRequest is a helper function to determine if a request was initiated
125// from the OpenFlow controller (or its proxy, e.g. OFAgent)
Richard Jankowski46464e92019-03-05 11:53:55 -0500126func (handler *APIHandler) isOFControllerRequest(ctx context.Context) bool {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500127 if md, ok := metadata.FromIncomingContext(ctx); ok {
128 // Metadata in context
Richard Jankowski46464e92019-03-05 11:53:55 -0500129 if _, ok = md[handler.core.config.CoreBindingKey]; ok {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500130 // OFAgent field in metadata
131 return true
132 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500133 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500134 return false
135}
136
137// competeForTransaction is a helper function to determine whether every request needs to compete with another
138// Core to execute the request
139func (handler *APIHandler) competeForTransaction() bool {
140 return handler.coreInCompetingMode
141}
142
Richard Jankowski199fd862019-03-18 14:49:51 -0400143// This function handles the creation of new devices
144func (handler *APIHandler) acquireRequest(ctx context.Context, id interface{}, maxTimeout ...int64) (*KVTransaction, error) {
khenaidoob6080322019-01-29 21:47:38 -0500145 timeout := handler.defaultRequestTimeout
khenaidoo9cdc1a62019-01-24 21:57:40 -0500146 if len(maxTimeout) > 0 {
147 timeout = maxTimeout[0]
Richard Jankowski2755adf2019-01-17 17:16:48 -0500148 }
khenaidoob6080322019-01-29 21:47:38 -0500149 log.Debugw("transaction-timeout", log.Fields{"timeout": timeout})
khenaidoo9cdc1a62019-01-24 21:57:40 -0500150 txn, err := handler.createKvTransaction(ctx)
151 if txn == nil {
152 return nil, err
153 } else if txn.Acquired(timeout) {
154 return txn, nil
155 } else {
khenaidoo297cd252019-02-07 22:10:23 -0500156 if id != nil {
157 // The id can either be a device Id or a logical device id.
158 if dId, ok := id.(*deviceID); ok {
159 // Since this core has not processed this request, let's load the device, along with its extended
160 // family (parents and children) in memory. This will keep this core in-sync with its paired core as
161 // much as possible. The watch feature in the core model will ensure that the contents of those objects in
162 // memory are in sync.
163 time.Sleep(2 * time.Second)
164 go handler.deviceMgr.load(dId.id)
165 } else if ldId, ok := id.(*logicalDeviceID); ok {
166 // This will load the logical device along with its children and grandchildren
167 go handler.logicalDeviceMgr.load(ldId.id)
168 }
169 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500170 return nil, errors.New("failed-to-seize-request")
Richard Jankowski2755adf2019-01-17 17:16:48 -0500171 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500172}
173
Richard Jankowski199fd862019-03-18 14:49:51 -0400174// This function handles the modification or deletion of existing devices
175func (handler *APIHandler) takeRequestOwnership(ctx context.Context, id interface{}, maxTimeout ...int64) (*KVTransaction, error) {
176 timeout := handler.defaultRequestTimeout
177 if len(maxTimeout) > 0 {
178 timeout = maxTimeout[0]
179 }
180 log.Debugw("transaction-timeout", log.Fields{"timeout": timeout})
181 txn, err := handler.createKvTransaction(ctx)
182 if txn == nil {
183 return nil, err
184 }
185
186 owned := false
187 if id != nil {
188 if devId, ok := id.(*deviceID); ok {
189 owned = handler.core.deviceOwnership.OwnedByMe(devId.id)
190 } else if lDevId, ok := id.(*logicalDeviceID); ok {
191 owned = handler.core.deviceOwnership.OwnedByMe(lDevId.id)
192 }
193 }
194 if owned {
195 if txn.Acquired(timeout) {
196 return txn, nil
197 } else {
198 return nil, errors.New("failed-to-seize-request")
199 }
200 } else {
201 if txn.Monitor(timeout) {
202 return txn, nil
203 } else {
204 return nil, errors.New("device-not-owned")
205 }
206 }
207}
208
khenaidoo4d4802d2018-10-04 21:59:49 -0400209// waitForNilResponseOnSuccess is a helper function to wait for a response on channel ch where an nil
210// response is expected in a successful scenario
211func waitForNilResponseOnSuccess(ctx context.Context, ch chan interface{}) (*empty.Empty, error) {
212 select {
213 case res := <-ch:
214 if res == nil {
215 return new(empty.Empty), nil
216 } else if err, ok := res.(error); ok {
217 return new(empty.Empty), err
218 } else {
219 log.Warnw("unexpected-return-type", log.Fields{"result": res})
220 err = status.Errorf(codes.Internal, "%s", res)
221 return new(empty.Empty), err
222 }
223 case <-ctx.Done():
224 log.Debug("client-timeout")
225 return nil, ctx.Err()
226 }
227}
228
khenaidoobf6e7bb2018-08-14 22:27:29 -0400229func (handler *APIHandler) UpdateLogLevel(ctx context.Context, logging *voltha.Logging) (*empty.Empty, error) {
khenaidoo6f2fbe32019-01-18 16:16:50 -0500230 log.Debugw("UpdateLogLevel-request", log.Fields{"package": logging.PackageName, "intval": int(logging.Level)})
khenaidoo92e62c52018-10-03 14:02:54 -0400231 out := new(empty.Empty)
khenaidoo6f2fbe32019-01-18 16:16:50 -0500232 if logging.PackageName == "" {
233 log.SetAllLogLevel(int(logging.Level))
234 } else {
235 log.SetPackageLogLevel(logging.PackageName, int(logging.Level))
236 }
khenaidoo92e62c52018-10-03 14:02:54 -0400237 return out, nil
khenaidoobf6e7bb2018-08-14 22:27:29 -0400238}
239
khenaidoo54e0ddf2019-02-27 16:21:33 -0500240
241func (handler *APIHandler) UpdateMembership(ctx context.Context, membership *voltha.Membership) (*empty.Empty, error) {
242 log.Debugw("UpdateMembership-request", log.Fields{"membership": membership})
243 out := new(empty.Empty)
khenaidoo6417b6c2019-03-01 18:18:01 -0500244 if err := handler.core.updateCoreMembership(ctx, membership); err != nil {
khenaidoo54e0ddf2019-02-27 16:21:33 -0500245 return out, err
246 }
247 return out, nil
248}
249
khenaidoo6417b6c2019-03-01 18:18:01 -0500250func (handler *APIHandler) GetMembership(ctx context.Context, empty *empty.Empty) (*voltha.Membership, error) {
251 log.Debug("GetMembership-request")
252 if membership := handler.core.getCoreMembership(ctx); membership != nil {
253 return membership, nil
254 }
255 return &voltha.Membership{}, nil
256}
257
258
khenaidoobf6e7bb2018-08-14 22:27:29 -0400259func (handler *APIHandler) EnableLogicalDevicePort(ctx context.Context, id *voltha.LogicalPortId) (*empty.Empty, error) {
260 log.Debugw("EnableLogicalDevicePort-request", log.Fields{"id": id, "test": common.TestModeKeys_api_test.String()})
261 if isTestMode(ctx) {
262 out := new(empty.Empty)
263 return out, nil
264 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500265
khenaidoo9cdc1a62019-01-24 21:57:40 -0500266 if handler.competeForTransaction() {
Richard Jankowski199fd862019-03-18 14:49:51 -0400267 if txn, err := handler.takeRequestOwnership(ctx, &logicalDeviceID{id:id.Id}); err != nil {
Richard Jankowski2755adf2019-01-17 17:16:48 -0500268 return new(empty.Empty), err
Richard Jankowski2755adf2019-01-17 17:16:48 -0500269 } else {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500270 defer txn.Close()
Richard Jankowski2755adf2019-01-17 17:16:48 -0500271 }
272 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500273
khenaidoo4d4802d2018-10-04 21:59:49 -0400274 ch := make(chan interface{})
275 defer close(ch)
khenaidoo19d7b632018-10-30 10:49:50 -0400276 go handler.logicalDeviceMgr.enableLogicalPort(ctx, id, ch)
khenaidoo4d4802d2018-10-04 21:59:49 -0400277 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400278}
279
280func (handler *APIHandler) DisableLogicalDevicePort(ctx context.Context, id *voltha.LogicalPortId) (*empty.Empty, error) {
281 log.Debugw("DisableLogicalDevicePort-request", log.Fields{"id": id, "test": common.TestModeKeys_api_test.String()})
282 if isTestMode(ctx) {
283 out := new(empty.Empty)
284 return out, nil
285 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500286
khenaidoo9cdc1a62019-01-24 21:57:40 -0500287 if handler.competeForTransaction() {
Richard Jankowski199fd862019-03-18 14:49:51 -0400288 if txn, err := handler.takeRequestOwnership(ctx, &logicalDeviceID{id:id.Id}); err != nil {
Richard Jankowski2755adf2019-01-17 17:16:48 -0500289 return new(empty.Empty), err
Richard Jankowski2755adf2019-01-17 17:16:48 -0500290 } else {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500291 defer txn.Close()
Richard Jankowski2755adf2019-01-17 17:16:48 -0500292 }
293 }
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.disableLogicalPort(ctx, id, ch)
298 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400299}
300
301func (handler *APIHandler) UpdateLogicalDeviceFlowTable(ctx context.Context, flow *openflow_13.FlowTableUpdate) (*empty.Empty, error) {
302 log.Debugw("UpdateLogicalDeviceFlowTable-request", log.Fields{"flow": flow, "test": common.TestModeKeys_api_test.String()})
303 if isTestMode(ctx) {
304 out := new(empty.Empty)
305 return out, nil
306 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500307
khenaidoo9cdc1a62019-01-24 21:57:40 -0500308 if handler.competeForTransaction() {
Richard Jankowski46464e92019-03-05 11:53:55 -0500309 if !handler.isOFControllerRequest(ctx) { // No need to acquire the transaction as request is sent to one core only
Richard Jankowski199fd862019-03-18 14:49:51 -0400310 if txn, err := handler.takeRequestOwnership(ctx, &logicalDeviceID{id:flow.Id}); err != nil {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500311 return new(empty.Empty), err
312 } else {
313 defer txn.Close()
314 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500315 }
316 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500317
khenaidoo19d7b632018-10-30 10:49:50 -0400318 ch := make(chan interface{})
319 defer close(ch)
320 go handler.logicalDeviceMgr.updateFlowTable(ctx, flow.Id, flow.FlowMod, ch)
321 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400322}
323
324func (handler *APIHandler) UpdateLogicalDeviceFlowGroupTable(ctx context.Context, flow *openflow_13.FlowGroupTableUpdate) (*empty.Empty, error) {
325 log.Debugw("UpdateLogicalDeviceFlowGroupTable-request", log.Fields{"flow": flow, "test": common.TestModeKeys_api_test.String()})
326 if isTestMode(ctx) {
327 out := new(empty.Empty)
328 return out, nil
329 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500330
khenaidoo9cdc1a62019-01-24 21:57:40 -0500331 if handler.competeForTransaction() {
Richard Jankowski46464e92019-03-05 11:53:55 -0500332 if !handler.isOFControllerRequest(ctx) { // No need to acquire the transaction as request is sent to one core only
Richard Jankowski199fd862019-03-18 14:49:51 -0400333 if txn, err := handler.takeRequestOwnership(ctx, &logicalDeviceID{id:flow.Id}); err != nil {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500334 return new(empty.Empty), err
335 } else {
336 defer txn.Close()
337 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500338 }
339 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500340
khenaidoo19d7b632018-10-30 10:49:50 -0400341 ch := make(chan interface{})
342 defer close(ch)
343 go handler.logicalDeviceMgr.updateGroupTable(ctx, flow.Id, flow.GroupMod, ch)
344 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400345}
346
khenaidoob9203542018-09-17 22:56:37 -0400347// GetDevice must be implemented in the read-only containers - should it also be implemented here?
348func (handler *APIHandler) GetDevice(ctx context.Context, id *voltha.ID) (*voltha.Device, error) {
349 log.Debugw("GetDevice-request", log.Fields{"id": id})
khenaidoo19d7b632018-10-30 10:49:50 -0400350 return handler.deviceMgr.GetDevice(id.Id)
khenaidoob9203542018-09-17 22:56:37 -0400351}
352
353// GetDevice must be implemented in the read-only containers - should it also be implemented here?
354func (handler *APIHandler) ListDevices(ctx context.Context, empty *empty.Empty) (*voltha.Devices, error) {
355 log.Debug("ListDevices")
356 return handler.deviceMgr.ListDevices()
357}
358
khenaidoo7ccedd52018-12-14 16:48:54 -0500359// ListDeviceIds returns the list of device ids managed by a voltha core
360func (handler *APIHandler) ListDeviceIds(ctx context.Context, empty *empty.Empty) (*voltha.IDs, error) {
361 log.Debug("ListDeviceIDs")
362 if isTestMode(ctx) {
363 out := &voltha.IDs{Items: make([]*voltha.ID, 0)}
364 return out, nil
365 }
366 return handler.deviceMgr.ListDeviceIds()
367}
368
369//ReconcileDevices is a request to a voltha core to managed a list of devices based on their IDs
370func (handler *APIHandler) ReconcileDevices(ctx context.Context, ids *voltha.IDs) (*empty.Empty, error) {
371 log.Debug("ReconcileDevices")
372 if isTestMode(ctx) {
373 out := new(empty.Empty)
374 return out, nil
375 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500376
khenaidoo9cdc1a62019-01-24 21:57:40 -0500377 // No need to grab a transaction as this request is core specific
378
khenaidoo7ccedd52018-12-14 16:48:54 -0500379 ch := make(chan interface{})
380 defer close(ch)
381 go handler.deviceMgr.ReconcileDevices(ctx, ids, ch)
382 return waitForNilResponseOnSuccess(ctx, ch)
383}
384
khenaidoob9203542018-09-17 22:56:37 -0400385// GetLogicalDevice must be implemented in the read-only containers - should it also be implemented here?
386func (handler *APIHandler) GetLogicalDevice(ctx context.Context, id *voltha.ID) (*voltha.LogicalDevice, error) {
387 log.Debugw("GetLogicalDevice-request", log.Fields{"id": id})
388 return handler.logicalDeviceMgr.getLogicalDevice(id.Id)
389}
390
391// ListLogicalDevices must be implemented in the read-only containers - should it also be implemented here?
392func (handler *APIHandler) ListLogicalDevices(ctx context.Context, empty *empty.Empty) (*voltha.LogicalDevices, error) {
393 log.Debug("ListLogicalDevices")
394 return handler.logicalDeviceMgr.listLogicalDevices()
395}
396
khenaidoo21d51152019-02-01 13:48:37 -0500397
398// ListAdapters returns the contents of all adapters known to the system
399func (handler *APIHandler) ListAdapters(ctx context.Context, empty *empty.Empty) (*voltha.Adapters, error) {
400 log.Debug("ListDevices")
401 return handler.adapterMgr.listAdapters(ctx)
402}
403
khenaidoo19d7b632018-10-30 10:49:50 -0400404// ListLogicalDevicePorts must be implemented in the read-only containers - should it also be implemented here?
405func (handler *APIHandler) ListLogicalDevicePorts(ctx context.Context, id *voltha.ID) (*voltha.LogicalPorts, error) {
406 log.Debugw("ListLogicalDevicePorts", log.Fields{"logicaldeviceid": id})
407 return handler.logicalDeviceMgr.ListLogicalDevicePorts(ctx, id.Id)
408}
409
khenaidoo4d4802d2018-10-04 21:59:49 -0400410// CreateDevice creates a new parent device in the data model
khenaidoobf6e7bb2018-08-14 22:27:29 -0400411func (handler *APIHandler) CreateDevice(ctx context.Context, device *voltha.Device) (*voltha.Device, error) {
khenaidoob9203542018-09-17 22:56:37 -0400412 log.Debugw("createdevice", log.Fields{"device": *device})
khenaidoobf6e7bb2018-08-14 22:27:29 -0400413 if isTestMode(ctx) {
414 return &voltha.Device{Id: device.Id}, nil
415 }
Richard Jankowskid42826e2018-11-02 16:06:37 -0400416
khenaidoo9cdc1a62019-01-24 21:57:40 -0500417 if handler.competeForTransaction() {
Richard Jankowski199fd862019-03-18 14:49:51 -0400418 if txn, err := handler.acquireRequest(ctx, nil); err != nil {
Richard Jankowski2755adf2019-01-17 17:16:48 -0500419 return &voltha.Device{}, err
Richard Jankowski2755adf2019-01-17 17:16:48 -0500420 } else {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500421 defer txn.Close()
Richard Jankowski2755adf2019-01-17 17:16:48 -0500422 }
423 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500424
khenaidoob9203542018-09-17 22:56:37 -0400425 ch := make(chan interface{})
426 defer close(ch)
427 go handler.deviceMgr.createDevice(ctx, device, ch)
428 select {
429 case res := <-ch:
khenaidoo92e62c52018-10-03 14:02:54 -0400430 if res != nil {
431 if err, ok := res.(error); ok {
432 return &voltha.Device{}, err
433 }
434 if d, ok := res.(*voltha.Device); ok {
Richard Jankowski199fd862019-03-18 14:49:51 -0400435 handler.core.deviceOwnership.OwnedByMe(d.Id)
khenaidoo92e62c52018-10-03 14:02:54 -0400436 return d, nil
437 }
khenaidoob9203542018-09-17 22:56:37 -0400438 }
khenaidoo92e62c52018-10-03 14:02:54 -0400439 log.Warnw("create-device-unexpected-return-type", log.Fields{"result": res})
440 err := status.Errorf(codes.Internal, "%s", res)
441 return &voltha.Device{}, err
khenaidoob9203542018-09-17 22:56:37 -0400442 case <-ctx.Done():
443 log.Debug("createdevice-client-timeout")
444 return nil, ctx.Err()
445 }
khenaidoobf6e7bb2018-08-14 22:27:29 -0400446}
447
khenaidoo4d4802d2018-10-04 21:59:49 -0400448// EnableDevice activates a device by invoking the adopt_device API on the appropriate adapter
khenaidoobf6e7bb2018-08-14 22:27:29 -0400449func (handler *APIHandler) EnableDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) {
khenaidoob9203542018-09-17 22:56:37 -0400450 log.Debugw("enabledevice", log.Fields{"id": id})
khenaidoobf6e7bb2018-08-14 22:27:29 -0400451 if isTestMode(ctx) {
khenaidoo4d4802d2018-10-04 21:59:49 -0400452 return new(empty.Empty), nil
khenaidoobf6e7bb2018-08-14 22:27:29 -0400453 }
Richard Jankowskid42826e2018-11-02 16:06:37 -0400454
khenaidoo9cdc1a62019-01-24 21:57:40 -0500455 if handler.competeForTransaction() {
Richard Jankowski199fd862019-03-18 14:49:51 -0400456 if txn, err := handler.takeRequestOwnership(ctx, &deviceID{id:id.Id}, handler.longRunningRequestTimeout); err != nil {
Richard Jankowski2755adf2019-01-17 17:16:48 -0500457 return new(empty.Empty), err
Richard Jankowski2755adf2019-01-17 17:16:48 -0500458 } else {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500459 defer txn.Close()
Richard Jankowski2755adf2019-01-17 17:16:48 -0500460 }
461 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500462
khenaidoob9203542018-09-17 22:56:37 -0400463 ch := make(chan interface{})
464 defer close(ch)
465 go handler.deviceMgr.enableDevice(ctx, id, ch)
khenaidoo4d4802d2018-10-04 21:59:49 -0400466 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400467}
468
khenaidoo4d4802d2018-10-04 21:59:49 -0400469// DisableDevice disables a device along with any child device it may have
khenaidoobf6e7bb2018-08-14 22:27:29 -0400470func (handler *APIHandler) DisableDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) {
471 log.Debugw("disabledevice-request", log.Fields{"id": id})
472 if isTestMode(ctx) {
khenaidoo4d4802d2018-10-04 21:59:49 -0400473 return new(empty.Empty), nil
khenaidoobf6e7bb2018-08-14 22:27:29 -0400474 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500475
khenaidoo9cdc1a62019-01-24 21:57:40 -0500476 if handler.competeForTransaction() {
Richard Jankowski199fd862019-03-18 14:49:51 -0400477 if txn, err := handler.takeRequestOwnership(ctx, &deviceID{id:id.Id}); err != nil {
Richard Jankowski2755adf2019-01-17 17:16:48 -0500478 return new(empty.Empty), err
Richard Jankowski2755adf2019-01-17 17:16:48 -0500479 } else {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500480 defer txn.Close()
Richard Jankowski2755adf2019-01-17 17:16:48 -0500481 }
482 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500483
khenaidoo92e62c52018-10-03 14:02:54 -0400484 ch := make(chan interface{})
485 defer close(ch)
486 go handler.deviceMgr.disableDevice(ctx, id, ch)
khenaidoo4d4802d2018-10-04 21:59:49 -0400487 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400488}
489
khenaidoo4d4802d2018-10-04 21:59:49 -0400490//RebootDevice invoked the reboot API to the corresponding adapter
khenaidoobf6e7bb2018-08-14 22:27:29 -0400491func (handler *APIHandler) RebootDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) {
khenaidoo4d4802d2018-10-04 21:59:49 -0400492 log.Debugw("rebootDevice-request", log.Fields{"id": id})
khenaidoobf6e7bb2018-08-14 22:27:29 -0400493 if isTestMode(ctx) {
khenaidoo4d4802d2018-10-04 21:59:49 -0400494 return new(empty.Empty), nil
khenaidoobf6e7bb2018-08-14 22:27:29 -0400495 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500496
khenaidoo9cdc1a62019-01-24 21:57:40 -0500497 if handler.competeForTransaction() {
Richard Jankowski199fd862019-03-18 14:49:51 -0400498 if txn, err := handler.takeRequestOwnership(ctx, &deviceID{id:id.Id}); err != nil {
Richard Jankowski2755adf2019-01-17 17:16:48 -0500499 return new(empty.Empty), err
Richard Jankowski2755adf2019-01-17 17:16:48 -0500500 } else {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500501 defer txn.Close()
Richard Jankowski2755adf2019-01-17 17:16:48 -0500502 }
503 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500504
khenaidoo4d4802d2018-10-04 21:59:49 -0400505 ch := make(chan interface{})
506 defer close(ch)
507 go handler.deviceMgr.rebootDevice(ctx, id, ch)
508 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400509}
510
khenaidoo4d4802d2018-10-04 21:59:49 -0400511// DeleteDevice removes a device from the data model
khenaidoobf6e7bb2018-08-14 22:27:29 -0400512func (handler *APIHandler) DeleteDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) {
513 log.Debugw("deletedevice-request", log.Fields{"id": id})
514 if isTestMode(ctx) {
khenaidoo4d4802d2018-10-04 21:59:49 -0400515 return new(empty.Empty), nil
khenaidoobf6e7bb2018-08-14 22:27:29 -0400516 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500517
khenaidoo9cdc1a62019-01-24 21:57:40 -0500518 if handler.competeForTransaction() {
Richard Jankowski199fd862019-03-18 14:49:51 -0400519 if txn, err := handler.takeRequestOwnership(ctx, nil); err != nil {
Richard Jankowski2755adf2019-01-17 17:16:48 -0500520 return new(empty.Empty), err
Richard Jankowski2755adf2019-01-17 17:16:48 -0500521 } else {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500522 defer txn.Close()
Richard Jankowski2755adf2019-01-17 17:16:48 -0500523 }
524 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500525
khenaidoo4d4802d2018-10-04 21:59:49 -0400526 ch := make(chan interface{})
527 defer close(ch)
528 go handler.deviceMgr.deleteDevice(ctx, id, ch)
529 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400530}
531
khenaidoof5a5bfa2019-01-23 22:20:29 -0500532// processImageRequest is a helper method to execute an image download request
533func (handler *APIHandler) processImageRequest(ctx context.Context, img *voltha.ImageDownload, requestType int) (*common.OperationResp, error) {
534 log.Debugw("processImageDownload", log.Fields{"img": *img, "requestType": requestType})
535 if isTestMode(ctx) {
536 resp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS}
537 return resp, nil
538 }
539
khenaidoo9cdc1a62019-01-24 21:57:40 -0500540 if handler.competeForTransaction() {
Richard Jankowski199fd862019-03-18 14:49:51 -0400541 if txn, err := handler.takeRequestOwnership(ctx, &deviceID{id:img.Id}); err != nil {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500542 return &common.OperationResp{}, err
543 } else {
544 defer txn.Close()
545 }
khenaidoof5a5bfa2019-01-23 22:20:29 -0500546 }
547
548 failedresponse := &common.OperationResp{Code:voltha.OperationResp_OPERATION_FAILURE}
549
550 ch := make(chan interface{})
551 defer close(ch)
552 switch requestType {
553 case IMAGE_DOWNLOAD:
554 go handler.deviceMgr.downloadImage(ctx, img, ch)
555 case CANCEL_IMAGE_DOWNLOAD:
556 go handler.deviceMgr.cancelImageDownload(ctx, img, ch)
557 case ACTIVATE_IMAGE:
558 go handler.deviceMgr.activateImage(ctx, img, ch)
559 case REVERT_IMAGE:
560 go handler.deviceMgr.revertImage(ctx, img, ch)
561 default:
562 log.Warn("invalid-request-type", log.Fields{"requestType": requestType})
563 return failedresponse, status.Errorf(codes.InvalidArgument, "%d", requestType)
564 }
565 select {
566 case res := <-ch:
567 if res != nil {
568 if err, ok := res.(error); ok {
569 return failedresponse, err
570 }
571 if opResp, ok := res.(*common.OperationResp); ok {
572 return opResp, nil
573 }
574 }
575 log.Warnw("download-image-unexpected-return-type", log.Fields{"result": res})
576 return failedresponse, status.Errorf(codes.Internal, "%s", res)
577 case <-ctx.Done():
578 log.Debug("downloadImage-client-timeout")
579 return nil, ctx.Err()
580 }
581}
582
khenaidoobf6e7bb2018-08-14 22:27:29 -0400583func (handler *APIHandler) DownloadImage(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) {
584 log.Debugw("DownloadImage-request", log.Fields{"img": *img})
585 if isTestMode(ctx) {
586 resp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS}
587 return resp, nil
588 }
589
khenaidoof5a5bfa2019-01-23 22:20:29 -0500590 return handler.processImageRequest(ctx, img, IMAGE_DOWNLOAD)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400591}
592
593func (handler *APIHandler) CancelImageDownload(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) {
khenaidoof5a5bfa2019-01-23 22:20:29 -0500594 log.Debugw("cancelImageDownload-request", log.Fields{"img": *img})
khenaidoobf6e7bb2018-08-14 22:27:29 -0400595 if isTestMode(ctx) {
596 resp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS}
597 return resp, nil
598 }
khenaidoof5a5bfa2019-01-23 22:20:29 -0500599 return handler.processImageRequest(ctx, img, CANCEL_IMAGE_DOWNLOAD)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400600}
601
602func (handler *APIHandler) ActivateImageUpdate(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) {
khenaidoof5a5bfa2019-01-23 22:20:29 -0500603 log.Debugw("activateImageUpdate-request", log.Fields{"img": *img})
khenaidoobf6e7bb2018-08-14 22:27:29 -0400604 if isTestMode(ctx) {
605 resp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS}
606 return resp, nil
607 }
khenaidoof5a5bfa2019-01-23 22:20:29 -0500608
609 return handler.processImageRequest(ctx, img, ACTIVATE_IMAGE)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400610}
611
612func (handler *APIHandler) RevertImageUpdate(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) {
khenaidoof5a5bfa2019-01-23 22:20:29 -0500613 log.Debugw("revertImageUpdate-request", log.Fields{"img": *img})
khenaidoobf6e7bb2018-08-14 22:27:29 -0400614 if isTestMode(ctx) {
615 resp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS}
616 return resp, nil
617 }
khenaidoof5a5bfa2019-01-23 22:20:29 -0500618
619 return handler.processImageRequest(ctx, img, REVERT_IMAGE)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400620}
621
khenaidoof5a5bfa2019-01-23 22:20:29 -0500622func (handler *APIHandler) GetImageDownloadStatus(ctx context.Context, img *voltha.ImageDownload) (*voltha.ImageDownload, error) {
623 log.Debugw("getImageDownloadStatus-request", log.Fields{"img": *img})
624 if isTestMode(ctx) {
Stephane Barbariedf5479f2019-01-29 22:13:00 -0500625 resp := &voltha.ImageDownload{DownloadState: voltha.ImageDownload_DOWNLOAD_SUCCEEDED}
khenaidoof5a5bfa2019-01-23 22:20:29 -0500626 return resp, nil
627 }
628
Stephane Barbariedf5479f2019-01-29 22:13:00 -0500629 failedresponse := &voltha.ImageDownload{DownloadState: voltha.ImageDownload_DOWNLOAD_UNKNOWN}
khenaidoof5a5bfa2019-01-23 22:20:29 -0500630
khenaidoo9cdc1a62019-01-24 21:57:40 -0500631 if handler.competeForTransaction() {
Richard Jankowski199fd862019-03-18 14:49:51 -0400632 if txn, err := handler.takeRequestOwnership(ctx, &deviceID{id:img.Id}); err != nil {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500633 return failedresponse, err
634 } else {
635 defer txn.Close()
636 }
khenaidoof5a5bfa2019-01-23 22:20:29 -0500637 }
638
639 ch := make(chan interface{})
640 defer close(ch)
641 go handler.deviceMgr.getImageDownloadStatus(ctx, img, ch)
642
643 select {
644 case res := <-ch:
645 if res != nil {
646 if err, ok := res.(error); ok {
647 return failedresponse, err
648 }
649 if downloadResp, ok := res.(*voltha.ImageDownload); ok {
650 return downloadResp, nil
651 }
652 }
653 log.Warnw("download-image-status", log.Fields{"result": res})
654 return failedresponse, status.Errorf(codes.Internal, "%s", res)
655 case <-ctx.Done():
656 log.Debug("downloadImage-client-timeout")
657 return failedresponse, ctx.Err()
658 }
659}
660
661func (handler *APIHandler) GetImageDownload(ctx context.Context, img *voltha.ImageDownload) (*voltha.ImageDownload, error) {
662 log.Debugw("GetImageDownload-request", log.Fields{"img": *img})
663 if isTestMode(ctx) {
Stephane Barbariedf5479f2019-01-29 22:13:00 -0500664 resp := &voltha.ImageDownload{DownloadState: voltha.ImageDownload_DOWNLOAD_SUCCEEDED}
khenaidoof5a5bfa2019-01-23 22:20:29 -0500665 return resp, nil
666 }
667
668 if download, err := handler.deviceMgr.getImageDownload(ctx, img); err != nil {
Stephane Barbariedf5479f2019-01-29 22:13:00 -0500669 return &voltha.ImageDownload{DownloadState: voltha.ImageDownload_DOWNLOAD_UNKNOWN}, err
khenaidoof5a5bfa2019-01-23 22:20:29 -0500670 } else {
671 return download, nil
672 }
673}
674
675func (handler *APIHandler) ListImageDownloads(ctx context.Context, id *voltha.ID) (*voltha.ImageDownloads, error) {
676 log.Debugw("ListImageDownloads-request", log.Fields{"deviceId": id.Id})
677 if isTestMode(ctx) {
678 resp := &voltha.ImageDownloads{Items:[]*voltha.ImageDownload{}}
679 return resp, nil
680 }
681
682 if downloads, err := handler.deviceMgr.listImageDownloads(ctx, id.Id); err != nil {
683 failedResp := &voltha.ImageDownloads{
684 Items:[]*voltha.ImageDownload{
Stephane Barbariedf5479f2019-01-29 22:13:00 -0500685 &voltha.ImageDownload{DownloadState: voltha.ImageDownload_DOWNLOAD_UNKNOWN},
khenaidoof5a5bfa2019-01-23 22:20:29 -0500686 },
687 }
688 return failedResp, err
689 } else {
690 return downloads, nil
691 }
692}
693
694
khenaidoobf6e7bb2018-08-14 22:27:29 -0400695func (handler *APIHandler) UpdateDevicePmConfigs(ctx context.Context, configs *voltha.PmConfigs) (*empty.Empty, error) {
696 log.Debugw("UpdateDevicePmConfigs-request", log.Fields{"configs": *configs})
697 if isTestMode(ctx) {
698 out := new(empty.Empty)
699 return out, nil
700 }
701 return nil, errors.New("UnImplemented")
702}
703
704func (handler *APIHandler) CreateAlarmFilter(ctx context.Context, filter *voltha.AlarmFilter) (*voltha.AlarmFilter, error) {
705 log.Debugw("CreateAlarmFilter-request", log.Fields{"filter": *filter})
706 if isTestMode(ctx) {
707 f := &voltha.AlarmFilter{Id: filter.Id}
708 return f, nil
709 }
710 return nil, errors.New("UnImplemented")
711}
712
713func (handler *APIHandler) UpdateAlarmFilter(ctx context.Context, filter *voltha.AlarmFilter) (*voltha.AlarmFilter, error) {
714 log.Debugw("UpdateAlarmFilter-request", log.Fields{"filter": *filter})
715 if isTestMode(ctx) {
716 f := &voltha.AlarmFilter{Id: filter.Id}
717 return f, nil
718 }
719 return nil, errors.New("UnImplemented")
720}
721
722func (handler *APIHandler) DeleteAlarmFilter(ctx context.Context, id *voltha.ID) (*empty.Empty, error) {
723 log.Debugw("DeleteAlarmFilter-request", log.Fields{"id": *id})
724 if isTestMode(ctx) {
725 out := new(empty.Empty)
726 return out, nil
727 }
728 return nil, errors.New("UnImplemented")
729}
730
731func (handler *APIHandler) SelfTest(ctx context.Context, id *voltha.ID) (*voltha.SelfTestResponse, error) {
732 log.Debugw("SelfTest-request", log.Fields{"id": id})
733 if isTestMode(ctx) {
734 resp := &voltha.SelfTestResponse{Result: voltha.SelfTestResponse_SUCCESS}
735 return resp, nil
736 }
737 return nil, errors.New("UnImplemented")
738}
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500739
740func (handler *APIHandler) forwardPacketOut(packet *openflow_13.PacketOut) {
741 log.Debugw("forwardPacketOut-request", log.Fields{"packet": packet})
Richard Jankowski2755adf2019-01-17 17:16:48 -0500742 agent := handler.logicalDeviceMgr.getLogicalDeviceAgent(packet.Id)
743 agent.packetOut(packet.PacketOut)
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500744}
745func (handler *APIHandler) StreamPacketsOut(
746 packets voltha.VolthaService_StreamPacketsOutServer,
747) error {
748 log.Debugw("StreamPacketsOut-request", log.Fields{"packets": packets})
749 for {
750 packet, err := packets.Recv()
751
752 if err == io.EOF {
753 break
754 } else if err != nil {
755 log.Errorw("Failed to receive packet", log.Fields{"error": err})
756 }
757
758 handler.forwardPacketOut(packet)
759 }
760
761 log.Debugw("StreamPacketsOut-request-done", log.Fields{"packets": packets})
762 return nil
763}
764
khenaidoo297cd252019-02-07 22:10:23 -0500765func (handler *APIHandler) sendPacketIn(deviceId string, transationId string, packet *openflow_13.OfpPacketIn) {
766 // TODO: Augment the OF PacketIn to include the transactionId
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500767 packetIn := openflow_13.PacketIn{Id: deviceId, PacketIn: packet}
768 log.Debugw("sendPacketIn", log.Fields{"packetIn": packetIn})
Richard Jankowskidbab94a2018-12-06 16:20:25 -0500769 // Enqueue the packet
770 if err := handler.packetInQueue.Put(packetIn); err != nil {
771 log.Errorw("failed-to-enqueue-packet", log.Fields{"error": err})
772 }
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500773}
774
775func (handler *APIHandler) ReceivePacketsIn(
776 empty *empty.Empty,
777 packetsIn voltha.VolthaService_ReceivePacketsInServer,
778) error {
779 log.Debugw("ReceivePacketsIn-request", log.Fields{"packetsIn": packetsIn})
780
781 for {
Richard Jankowskidbab94a2018-12-06 16:20:25 -0500782 // Dequeue a packet
783 if packets, err := handler.packetInQueue.Get(1); err == nil {
784 log.Debugw("dequeued-packet", log.Fields{"packet": packets[0]})
785 if packet, ok := packets[0].(openflow_13.PacketIn); ok {
786 log.Debugw("sending-packet-in", log.Fields{"packet": packet})
787 if err := packetsIn.Send(&packet); err != nil {
788 log.Errorw("failed-to-send-packet", log.Fields{"error": err})
789 }
790 }
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500791 }
792 }
khenaidoof5a5bfa2019-01-23 22:20:29 -0500793 //TODO: FInd an elegant way to get out of the above loop when the Core is stopped
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500794}
795
796func (handler *APIHandler) sendChangeEvent(deviceId string, portStatus *openflow_13.OfpPortStatus) {
797 // TODO: validate the type of portStatus parameter
798 //if _, ok := portStatus.(*openflow_13.OfpPortStatus); ok {
799 //}
800 event := openflow_13.ChangeEvent{Id: deviceId, Event: &openflow_13.ChangeEvent_PortStatus{PortStatus: portStatus}}
801 log.Debugw("sendChangeEvent", log.Fields{"event": event})
Richard Jankowski199fd862019-03-18 14:49:51 -0400802 // Enqueue the change event
803 if err := handler.changeEventQueue.Put(event); err != nil {
804 log.Errorw("failed-to-enqueue-change-event", log.Fields{"error": err})
805 }
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500806}
807
808func (handler *APIHandler) ReceiveChangeEvents(
809 empty *empty.Empty,
810 changeEvents voltha.VolthaService_ReceiveChangeEventsServer,
811) error {
812 log.Debugw("ReceiveChangeEvents-request", log.Fields{"changeEvents": changeEvents})
813 for {
Richard Jankowski199fd862019-03-18 14:49:51 -0400814 // Dequeue a change event
815 if events, err := handler.changeEventQueue.Get(1); err == nil {
816 log.Debugw("dequeued-change-event", log.Fields{"event": events[0]})
817 if event, ok := events[0].(openflow_13.ChangeEvent); ok {
818 log.Debugw("sending-change-event", log.Fields{"event": event})
819 if err := changeEvents.Send(&event); err != nil {
820 log.Errorw("failed-to-send-change-event", log.Fields{"error": err})
821 }
822 }
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500823 }
824 }
Richard Jankowski199fd862019-03-18 14:49:51 -0400825}
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500826
827func (handler *APIHandler) Subscribe(
828 ctx context.Context,
829 ofAgent *voltha.OfAgentSubscriber,
830) (*voltha.OfAgentSubscriber, error) {
831 log.Debugw("Subscribe-request", log.Fields{"ofAgent": ofAgent})
832 return &voltha.OfAgentSubscriber{OfagentId: ofAgent.OfagentId, VolthaId: ofAgent.VolthaId}, nil
833}