blob: 9b7fac72e611a6104671a6d3414b46fb5289a2ee [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
khenaidoo43c82122018-11-22 18:38:28 -050035const MAX_RESPONSE_TIME = 500 // milliseconds
Richard Jankowskid42826e2018-11-02 16:06:37 -040036
khenaidoobf6e7bb2018-08-14 22:27:29 -040037type APIHandler struct {
khenaidoob9203542018-09-17 22:56:37 -040038 deviceMgr *DeviceManager
39 logicalDeviceMgr *LogicalDeviceManager
khenaidood2b6df92018-12-13 16:37:20 -050040 packetInQueue *queue.Queue
khenaidoobf6e7bb2018-08-14 22:27:29 -040041 da.DefaultAPIHandler
42}
43
khenaidoo9a468962018-09-19 15:33:13 -040044func NewAPIHandler(deviceMgr *DeviceManager, lDeviceMgr *LogicalDeviceManager) *APIHandler {
Stephane Barbarie6e1bd502018-11-05 22:44:45 -050045 handler := &APIHandler{
46 deviceMgr: deviceMgr,
47 logicalDeviceMgr: lDeviceMgr,
Richard Jankowskidbab94a2018-12-06 16:20:25 -050048 // TODO: Figure out what the 'hint' parameter to queue.New does
khenaidood2b6df92018-12-13 16:37:20 -050049 packetInQueue: queue.New(10),
Stephane Barbarie6e1bd502018-11-05 22:44:45 -050050 }
khenaidoobf6e7bb2018-08-14 22:27:29 -040051 return handler
52}
khenaidoo4d4802d2018-10-04 21:59:49 -040053
54// isTestMode is a helper function to determine a function is invoked for testing only
khenaidoobf6e7bb2018-08-14 22:27:29 -040055func isTestMode(ctx context.Context) bool {
56 md, _ := metadata.FromIncomingContext(ctx)
57 _, exist := md[common.TestModeKeys_api_test.String()]
58 return exist
59}
60
Richard Jankowskid42826e2018-11-02 16:06:37 -040061// This function attempts to extract the serial number from the request metadata
62// and create a KV transaction for that serial number for the current core.
63func (handler *APIHandler) createKvTransaction(ctx context.Context) (*KVTransaction, error) {
64 var (
khenaidoo43c82122018-11-22 18:38:28 -050065 err error
66 ok bool
67 md metadata.MD
Richard Jankowskid42826e2018-11-02 16:06:37 -040068 serNum []string
69 )
70 if md, ok = metadata.FromIncomingContext(ctx); !ok {
71 err = errors.New("metadata-not-found")
72 } else if serNum, ok = md["voltha_serial_number"]; !ok {
73 err = errors.New("serial-number-not-found")
74 }
75 if !ok {
76 log.Error(err)
77 return nil, err
78 }
79 // Create KV transaction
80 txn := NewKVTransaction(serNum[0])
81 return txn, nil
82}
83
khenaidoo4d4802d2018-10-04 21:59:49 -040084// waitForNilResponseOnSuccess is a helper function to wait for a response on channel ch where an nil
85// response is expected in a successful scenario
86func waitForNilResponseOnSuccess(ctx context.Context, ch chan interface{}) (*empty.Empty, error) {
87 select {
88 case res := <-ch:
89 if res == nil {
90 return new(empty.Empty), nil
91 } else if err, ok := res.(error); ok {
92 return new(empty.Empty), err
93 } else {
94 log.Warnw("unexpected-return-type", log.Fields{"result": res})
95 err = status.Errorf(codes.Internal, "%s", res)
96 return new(empty.Empty), err
97 }
98 case <-ctx.Done():
99 log.Debug("client-timeout")
100 return nil, ctx.Err()
101 }
102}
103
khenaidoobf6e7bb2018-08-14 22:27:29 -0400104func (handler *APIHandler) UpdateLogLevel(ctx context.Context, logging *voltha.Logging) (*empty.Empty, error) {
105 log.Debugw("UpdateLogLevel-request", log.Fields{"newloglevel": logging.Level, "intval": int(logging.Level)})
khenaidoo92e62c52018-10-03 14:02:54 -0400106 out := new(empty.Empty)
107 log.SetPackageLogLevel(logging.PackageName, int(logging.Level))
108 return out, nil
khenaidoobf6e7bb2018-08-14 22:27:29 -0400109}
110
111func (handler *APIHandler) EnableLogicalDevicePort(ctx context.Context, id *voltha.LogicalPortId) (*empty.Empty, error) {
112 log.Debugw("EnableLogicalDevicePort-request", log.Fields{"id": id, "test": common.TestModeKeys_api_test.String()})
113 if isTestMode(ctx) {
114 out := new(empty.Empty)
115 return out, nil
116 }
khenaidoo4d4802d2018-10-04 21:59:49 -0400117 ch := make(chan interface{})
118 defer close(ch)
khenaidoo19d7b632018-10-30 10:49:50 -0400119 go handler.logicalDeviceMgr.enableLogicalPort(ctx, id, ch)
khenaidoo4d4802d2018-10-04 21:59:49 -0400120 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400121}
122
123func (handler *APIHandler) DisableLogicalDevicePort(ctx context.Context, id *voltha.LogicalPortId) (*empty.Empty, error) {
124 log.Debugw("DisableLogicalDevicePort-request", log.Fields{"id": id, "test": common.TestModeKeys_api_test.String()})
125 if isTestMode(ctx) {
126 out := new(empty.Empty)
127 return out, nil
128 }
khenaidoo19d7b632018-10-30 10:49:50 -0400129 ch := make(chan interface{})
130 defer close(ch)
131 go handler.logicalDeviceMgr.disableLogicalPort(ctx, id, ch)
132 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400133}
134
135func (handler *APIHandler) UpdateLogicalDeviceFlowTable(ctx context.Context, flow *openflow_13.FlowTableUpdate) (*empty.Empty, error) {
136 log.Debugw("UpdateLogicalDeviceFlowTable-request", log.Fields{"flow": flow, "test": common.TestModeKeys_api_test.String()})
137 if isTestMode(ctx) {
138 out := new(empty.Empty)
139 return out, nil
140 }
khenaidoo19d7b632018-10-30 10:49:50 -0400141 ch := make(chan interface{})
142 defer close(ch)
143 go handler.logicalDeviceMgr.updateFlowTable(ctx, flow.Id, flow.FlowMod, ch)
144 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400145}
146
147func (handler *APIHandler) UpdateLogicalDeviceFlowGroupTable(ctx context.Context, flow *openflow_13.FlowGroupTableUpdate) (*empty.Empty, error) {
148 log.Debugw("UpdateLogicalDeviceFlowGroupTable-request", log.Fields{"flow": flow, "test": common.TestModeKeys_api_test.String()})
149 if isTestMode(ctx) {
150 out := new(empty.Empty)
151 return out, nil
152 }
khenaidoo19d7b632018-10-30 10:49:50 -0400153 ch := make(chan interface{})
154 defer close(ch)
155 go handler.logicalDeviceMgr.updateGroupTable(ctx, flow.Id, flow.GroupMod, ch)
156 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400157}
158
khenaidoob9203542018-09-17 22:56:37 -0400159// GetDevice must be implemented in the read-only containers - should it also be implemented here?
160func (handler *APIHandler) GetDevice(ctx context.Context, id *voltha.ID) (*voltha.Device, error) {
161 log.Debugw("GetDevice-request", log.Fields{"id": id})
khenaidoo19d7b632018-10-30 10:49:50 -0400162 return handler.deviceMgr.GetDevice(id.Id)
khenaidoob9203542018-09-17 22:56:37 -0400163}
164
165// GetDevice must be implemented in the read-only containers - should it also be implemented here?
166func (handler *APIHandler) ListDevices(ctx context.Context, empty *empty.Empty) (*voltha.Devices, error) {
167 log.Debug("ListDevices")
168 return handler.deviceMgr.ListDevices()
169}
170
171// GetLogicalDevice must be implemented in the read-only containers - should it also be implemented here?
172func (handler *APIHandler) GetLogicalDevice(ctx context.Context, id *voltha.ID) (*voltha.LogicalDevice, error) {
173 log.Debugw("GetLogicalDevice-request", log.Fields{"id": id})
174 return handler.logicalDeviceMgr.getLogicalDevice(id.Id)
175}
176
177// ListLogicalDevices must be implemented in the read-only containers - should it also be implemented here?
178func (handler *APIHandler) ListLogicalDevices(ctx context.Context, empty *empty.Empty) (*voltha.LogicalDevices, error) {
179 log.Debug("ListLogicalDevices")
180 return handler.logicalDeviceMgr.listLogicalDevices()
181}
182
khenaidoo19d7b632018-10-30 10:49:50 -0400183// ListLogicalDevicePorts must be implemented in the read-only containers - should it also be implemented here?
184func (handler *APIHandler) ListLogicalDevicePorts(ctx context.Context, id *voltha.ID) (*voltha.LogicalPorts, error) {
185 log.Debugw("ListLogicalDevicePorts", log.Fields{"logicaldeviceid": id})
186 return handler.logicalDeviceMgr.ListLogicalDevicePorts(ctx, id.Id)
187}
188
khenaidoo4d4802d2018-10-04 21:59:49 -0400189// CreateDevice creates a new parent device in the data model
khenaidoobf6e7bb2018-08-14 22:27:29 -0400190func (handler *APIHandler) CreateDevice(ctx context.Context, device *voltha.Device) (*voltha.Device, error) {
khenaidoob9203542018-09-17 22:56:37 -0400191 log.Debugw("createdevice", log.Fields{"device": *device})
khenaidoobf6e7bb2018-08-14 22:27:29 -0400192 if isTestMode(ctx) {
193 return &voltha.Device{Id: device.Id}, nil
194 }
Richard Jankowskid42826e2018-11-02 16:06:37 -0400195
khenaidoo9f1fd172018-11-13 09:16:17 -0500196 //txn, err := handler.createKvTransaction(ctx)
197 //if txn == nil {
198 // return &voltha.Device{}, err
199 //} else if txn.Acquired(MAX_RESPONSE_TIME) {
200 // defer txn.Close() // Ensure active core signals "done" to standby
201 //} else {
202 // return &voltha.Device{}, nil
203 //}
Richard Jankowskid42826e2018-11-02 16:06:37 -0400204
khenaidoob9203542018-09-17 22:56:37 -0400205 ch := make(chan interface{})
206 defer close(ch)
207 go handler.deviceMgr.createDevice(ctx, device, ch)
208 select {
209 case res := <-ch:
khenaidoo92e62c52018-10-03 14:02:54 -0400210 if res != nil {
211 if err, ok := res.(error); ok {
212 return &voltha.Device{}, err
213 }
214 if d, ok := res.(*voltha.Device); ok {
215 return d, nil
216 }
khenaidoob9203542018-09-17 22:56:37 -0400217 }
khenaidoo92e62c52018-10-03 14:02:54 -0400218 log.Warnw("create-device-unexpected-return-type", log.Fields{"result": res})
219 err := status.Errorf(codes.Internal, "%s", res)
220 return &voltha.Device{}, err
khenaidoob9203542018-09-17 22:56:37 -0400221 case <-ctx.Done():
222 log.Debug("createdevice-client-timeout")
223 return nil, ctx.Err()
224 }
khenaidoobf6e7bb2018-08-14 22:27:29 -0400225}
226
khenaidoo4d4802d2018-10-04 21:59:49 -0400227// EnableDevice activates a device by invoking the adopt_device API on the appropriate adapter
khenaidoobf6e7bb2018-08-14 22:27:29 -0400228func (handler *APIHandler) EnableDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) {
khenaidoob9203542018-09-17 22:56:37 -0400229 log.Debugw("enabledevice", log.Fields{"id": id})
khenaidoobf6e7bb2018-08-14 22:27:29 -0400230 if isTestMode(ctx) {
khenaidoo4d4802d2018-10-04 21:59:49 -0400231 return new(empty.Empty), nil
khenaidoobf6e7bb2018-08-14 22:27:29 -0400232 }
Richard Jankowskid42826e2018-11-02 16:06:37 -0400233
khenaidoo9f1fd172018-11-13 09:16:17 -0500234 //txn, err := handler.createKvTransaction(ctx)
235 //if txn == nil {
236 // return new(empty.Empty), err
237 //} else if txn.Acquired(MAX_RESPONSE_TIME) {
238 // defer txn.Close() // Ensure active core signals "done" to standby
239 //} else {
240 // return new(empty.Empty), nil
241 //}
Richard Jankowskid42826e2018-11-02 16:06:37 -0400242
khenaidoob9203542018-09-17 22:56:37 -0400243 ch := make(chan interface{})
244 defer close(ch)
245 go handler.deviceMgr.enableDevice(ctx, id, ch)
khenaidoo4d4802d2018-10-04 21:59:49 -0400246 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400247}
248
khenaidoo4d4802d2018-10-04 21:59:49 -0400249// DisableDevice disables a device along with any child device it may have
khenaidoobf6e7bb2018-08-14 22:27:29 -0400250func (handler *APIHandler) DisableDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) {
251 log.Debugw("disabledevice-request", log.Fields{"id": id})
252 if isTestMode(ctx) {
khenaidoo4d4802d2018-10-04 21:59:49 -0400253 return new(empty.Empty), nil
khenaidoobf6e7bb2018-08-14 22:27:29 -0400254 }
khenaidoo92e62c52018-10-03 14:02:54 -0400255 ch := make(chan interface{})
256 defer close(ch)
257 go handler.deviceMgr.disableDevice(ctx, id, ch)
khenaidoo4d4802d2018-10-04 21:59:49 -0400258 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400259}
260
khenaidoo4d4802d2018-10-04 21:59:49 -0400261//RebootDevice invoked the reboot API to the corresponding adapter
khenaidoobf6e7bb2018-08-14 22:27:29 -0400262func (handler *APIHandler) RebootDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) {
khenaidoo4d4802d2018-10-04 21:59:49 -0400263 log.Debugw("rebootDevice-request", log.Fields{"id": id})
khenaidoobf6e7bb2018-08-14 22:27:29 -0400264 if isTestMode(ctx) {
khenaidoo4d4802d2018-10-04 21:59:49 -0400265 return new(empty.Empty), nil
khenaidoobf6e7bb2018-08-14 22:27:29 -0400266 }
khenaidoo4d4802d2018-10-04 21:59:49 -0400267 ch := make(chan interface{})
268 defer close(ch)
269 go handler.deviceMgr.rebootDevice(ctx, id, ch)
270 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400271}
272
khenaidoo4d4802d2018-10-04 21:59:49 -0400273// DeleteDevice removes a device from the data model
khenaidoobf6e7bb2018-08-14 22:27:29 -0400274func (handler *APIHandler) DeleteDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) {
275 log.Debugw("deletedevice-request", log.Fields{"id": id})
276 if isTestMode(ctx) {
khenaidoo4d4802d2018-10-04 21:59:49 -0400277 return new(empty.Empty), nil
khenaidoobf6e7bb2018-08-14 22:27:29 -0400278 }
khenaidoo4d4802d2018-10-04 21:59:49 -0400279 ch := make(chan interface{})
280 defer close(ch)
281 go handler.deviceMgr.deleteDevice(ctx, id, ch)
282 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400283}
284
285func (handler *APIHandler) DownloadImage(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) {
286 log.Debugw("DownloadImage-request", log.Fields{"img": *img})
287 if isTestMode(ctx) {
288 resp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS}
289 return resp, nil
290 }
291
292 return nil, errors.New("UnImplemented")
293}
294
295func (handler *APIHandler) CancelImageDownload(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) {
296 log.Debugw("CancelImageDownload-request", log.Fields{"img": *img})
297 if isTestMode(ctx) {
298 resp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS}
299 return resp, nil
300 }
301 return nil, errors.New("UnImplemented")
302}
303
304func (handler *APIHandler) ActivateImageUpdate(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) {
305 log.Debugw("ActivateImageUpdate-request", log.Fields{"img": *img})
306 if isTestMode(ctx) {
307 resp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS}
308 return resp, nil
309 }
310 return nil, errors.New("UnImplemented")
311}
312
313func (handler *APIHandler) RevertImageUpdate(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) {
314 log.Debugw("RevertImageUpdate-request", log.Fields{"img": *img})
315 if isTestMode(ctx) {
316 resp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS}
317 return resp, nil
318 }
319 return nil, errors.New("UnImplemented")
320}
321
322func (handler *APIHandler) UpdateDevicePmConfigs(ctx context.Context, configs *voltha.PmConfigs) (*empty.Empty, error) {
323 log.Debugw("UpdateDevicePmConfigs-request", log.Fields{"configs": *configs})
324 if isTestMode(ctx) {
325 out := new(empty.Empty)
326 return out, nil
327 }
328 return nil, errors.New("UnImplemented")
329}
330
331func (handler *APIHandler) CreateAlarmFilter(ctx context.Context, filter *voltha.AlarmFilter) (*voltha.AlarmFilter, error) {
332 log.Debugw("CreateAlarmFilter-request", log.Fields{"filter": *filter})
333 if isTestMode(ctx) {
334 f := &voltha.AlarmFilter{Id: filter.Id}
335 return f, nil
336 }
337 return nil, errors.New("UnImplemented")
338}
339
340func (handler *APIHandler) UpdateAlarmFilter(ctx context.Context, filter *voltha.AlarmFilter) (*voltha.AlarmFilter, error) {
341 log.Debugw("UpdateAlarmFilter-request", log.Fields{"filter": *filter})
342 if isTestMode(ctx) {
343 f := &voltha.AlarmFilter{Id: filter.Id}
344 return f, nil
345 }
346 return nil, errors.New("UnImplemented")
347}
348
349func (handler *APIHandler) DeleteAlarmFilter(ctx context.Context, id *voltha.ID) (*empty.Empty, error) {
350 log.Debugw("DeleteAlarmFilter-request", log.Fields{"id": *id})
351 if isTestMode(ctx) {
352 out := new(empty.Empty)
353 return out, nil
354 }
355 return nil, errors.New("UnImplemented")
356}
357
358func (handler *APIHandler) SelfTest(ctx context.Context, id *voltha.ID) (*voltha.SelfTestResponse, error) {
359 log.Debugw("SelfTest-request", log.Fields{"id": id})
360 if isTestMode(ctx) {
361 resp := &voltha.SelfTestResponse{Result: voltha.SelfTestResponse_SUCCESS}
362 return resp, nil
363 }
364 return nil, errors.New("UnImplemented")
365}
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500366
367func (handler *APIHandler) forwardPacketOut(packet *openflow_13.PacketOut) {
368 log.Debugw("forwardPacketOut-request", log.Fields{"packet": packet})
369 //agent := handler.logicalDeviceMgr.getLogicalDeviceAgent(packet.Id)
370 //agent.packetOut(packet.PacketOut)
371}
372func (handler *APIHandler) StreamPacketsOut(
373 packets voltha.VolthaService_StreamPacketsOutServer,
374) error {
375 log.Debugw("StreamPacketsOut-request", log.Fields{"packets": packets})
376 for {
377 packet, err := packets.Recv()
378
379 if err == io.EOF {
380 break
381 } else if err != nil {
382 log.Errorw("Failed to receive packet", log.Fields{"error": err})
383 }
384
385 handler.forwardPacketOut(packet)
386 }
387
388 log.Debugw("StreamPacketsOut-request-done", log.Fields{"packets": packets})
389 return nil
390}
391
392func (handler *APIHandler) sendPacketIn(deviceId string, packet *openflow_13.OfpPacketIn) {
393 packetIn := openflow_13.PacketIn{Id: deviceId, PacketIn: packet}
394 log.Debugw("sendPacketIn", log.Fields{"packetIn": packetIn})
Richard Jankowskidbab94a2018-12-06 16:20:25 -0500395 // Enqueue the packet
396 if err := handler.packetInQueue.Put(packetIn); err != nil {
397 log.Errorw("failed-to-enqueue-packet", log.Fields{"error": err})
398 }
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500399}
400
401func (handler *APIHandler) ReceivePacketsIn(
402 empty *empty.Empty,
403 packetsIn voltha.VolthaService_ReceivePacketsInServer,
404) error {
405 log.Debugw("ReceivePacketsIn-request", log.Fields{"packetsIn": packetsIn})
406
407 for {
Richard Jankowskidbab94a2018-12-06 16:20:25 -0500408 // Dequeue a packet
409 if packets, err := handler.packetInQueue.Get(1); err == nil {
410 log.Debugw("dequeued-packet", log.Fields{"packet": packets[0]})
411 if packet, ok := packets[0].(openflow_13.PacketIn); ok {
412 log.Debugw("sending-packet-in", log.Fields{"packet": packet})
413 if err := packetsIn.Send(&packet); err != nil {
414 log.Errorw("failed-to-send-packet", log.Fields{"error": err})
415 }
416 }
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500417 }
418 }
419 log.Debugw("ReceivePacketsIn-request-done", log.Fields{"packetsIn": packetsIn})
420 return nil
421}
422
423func (handler *APIHandler) sendChangeEvent(deviceId string, portStatus *openflow_13.OfpPortStatus) {
424 // TODO: validate the type of portStatus parameter
425 //if _, ok := portStatus.(*openflow_13.OfpPortStatus); ok {
426 //}
427 event := openflow_13.ChangeEvent{Id: deviceId, Event: &openflow_13.ChangeEvent_PortStatus{PortStatus: portStatus}}
428 log.Debugw("sendChangeEvent", log.Fields{"event": event})
429 // TODO: put the packet in the queue
430}
431
432func (handler *APIHandler) ReceiveChangeEvents(
433 empty *empty.Empty,
434 changeEvents voltha.VolthaService_ReceiveChangeEventsServer,
435) error {
436 log.Debugw("ReceiveChangeEvents-request", log.Fields{"changeEvents": changeEvents})
437 for {
438 // TODO: need to retrieve packet from queue
439 event := &openflow_13.ChangeEvent{}
440 time.Sleep(time.Duration(5) * time.Second)
441 err := changeEvents.Send(event)
442 if err != nil {
443 log.Errorw("Failed to send change event", log.Fields{"error": err})
444 }
445 }
446 return nil
447}
448
449func (handler *APIHandler) Subscribe(
450 ctx context.Context,
451 ofAgent *voltha.OfAgentSubscriber,
452) (*voltha.OfAgentSubscriber, error) {
453 log.Debugw("Subscribe-request", log.Fields{"ofAgent": ofAgent})
454 return &voltha.OfAgentSubscriber{OfagentId: ofAgent.OfagentId, VolthaId: ofAgent.VolthaId}, nil
455}