blob: 0cb9a14f2e1b6e0c05fb76dcc6983628cfdb0fe9 [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
Richard Jankowski2755adf2019-01-17 17:16:48 -050035//TODO: Move this Tag into the proto file
36const OF_CONTROLLER_TAG= "voltha_backend_name"
37
khenaidoo43c82122018-11-22 18:38:28 -050038const MAX_RESPONSE_TIME = 500 // milliseconds
Richard Jankowskid42826e2018-11-02 16:06:37 -040039
khenaidoobf6e7bb2018-08-14 22:27:29 -040040type APIHandler struct {
khenaidoob9203542018-09-17 22:56:37 -040041 deviceMgr *DeviceManager
42 logicalDeviceMgr *LogicalDeviceManager
khenaidood2b6df92018-12-13 16:37:20 -050043 packetInQueue *queue.Queue
khenaidoobf6e7bb2018-08-14 22:27:29 -040044 da.DefaultAPIHandler
45}
46
khenaidoo9a468962018-09-19 15:33:13 -040047func NewAPIHandler(deviceMgr *DeviceManager, lDeviceMgr *LogicalDeviceManager) *APIHandler {
Stephane Barbarie6e1bd502018-11-05 22:44:45 -050048 handler := &APIHandler{
49 deviceMgr: deviceMgr,
50 logicalDeviceMgr: lDeviceMgr,
Richard Jankowskidbab94a2018-12-06 16:20:25 -050051 // TODO: Figure out what the 'hint' parameter to queue.New does
khenaidood2b6df92018-12-13 16:37:20 -050052 packetInQueue: queue.New(10),
Stephane Barbarie6e1bd502018-11-05 22:44:45 -050053 }
khenaidoobf6e7bb2018-08-14 22:27:29 -040054 return handler
55}
khenaidoo4d4802d2018-10-04 21:59:49 -040056
57// isTestMode is a helper function to determine a function is invoked for testing only
khenaidoobf6e7bb2018-08-14 22:27:29 -040058func isTestMode(ctx context.Context) bool {
59 md, _ := metadata.FromIncomingContext(ctx)
60 _, exist := md[common.TestModeKeys_api_test.String()]
61 return exist
62}
63
Richard Jankowskid42826e2018-11-02 16:06:37 -040064// This function attempts to extract the serial number from the request metadata
65// and create a KV transaction for that serial number for the current core.
66func (handler *APIHandler) createKvTransaction(ctx context.Context) (*KVTransaction, error) {
67 var (
khenaidoo43c82122018-11-22 18:38:28 -050068 err error
69 ok bool
70 md metadata.MD
Richard Jankowskid42826e2018-11-02 16:06:37 -040071 serNum []string
72 )
73 if md, ok = metadata.FromIncomingContext(ctx); !ok {
74 err = errors.New("metadata-not-found")
75 } else if serNum, ok = md["voltha_serial_number"]; !ok {
76 err = errors.New("serial-number-not-found")
77 }
78 if !ok {
79 log.Error(err)
80 return nil, err
81 }
82 // Create KV transaction
83 txn := NewKVTransaction(serNum[0])
84 return txn, nil
85}
86
Richard Jankowski2755adf2019-01-17 17:16:48 -050087// isOFControllerRequest is a helper function to determine if a request was initiated
88// from the OpenFlow controller (or its proxy, e.g. OFAgent)
89func isOFControllerRequest(ctx context.Context) bool {
90 var (
91 ok bool
92 md metadata.MD
93 value []string
94 )
95 if md, ok = metadata.FromIncomingContext(ctx); !ok {
96 // No metadata
97 return false
98 }
99 if value, ok = md[OF_CONTROLLER_TAG]; !ok {
100 // No OFAgent field in metadata
101 return false
102 }
103 if value[0] == "" {
104 // OFAgent has not set a field value
105 return false
106 }
107 return true
108}
109
khenaidoo4d4802d2018-10-04 21:59:49 -0400110// waitForNilResponseOnSuccess is a helper function to wait for a response on channel ch where an nil
111// response is expected in a successful scenario
112func waitForNilResponseOnSuccess(ctx context.Context, ch chan interface{}) (*empty.Empty, error) {
113 select {
114 case res := <-ch:
115 if res == nil {
116 return new(empty.Empty), nil
117 } else if err, ok := res.(error); ok {
118 return new(empty.Empty), err
119 } else {
120 log.Warnw("unexpected-return-type", log.Fields{"result": res})
121 err = status.Errorf(codes.Internal, "%s", res)
122 return new(empty.Empty), err
123 }
124 case <-ctx.Done():
125 log.Debug("client-timeout")
126 return nil, ctx.Err()
127 }
128}
129
khenaidoobf6e7bb2018-08-14 22:27:29 -0400130func (handler *APIHandler) UpdateLogLevel(ctx context.Context, logging *voltha.Logging) (*empty.Empty, error) {
131 log.Debugw("UpdateLogLevel-request", log.Fields{"newloglevel": logging.Level, "intval": int(logging.Level)})
khenaidoo92e62c52018-10-03 14:02:54 -0400132 out := new(empty.Empty)
133 log.SetPackageLogLevel(logging.PackageName, int(logging.Level))
134 return out, nil
khenaidoobf6e7bb2018-08-14 22:27:29 -0400135}
136
137func (handler *APIHandler) EnableLogicalDevicePort(ctx context.Context, id *voltha.LogicalPortId) (*empty.Empty, error) {
138 log.Debugw("EnableLogicalDevicePort-request", log.Fields{"id": id, "test": common.TestModeKeys_api_test.String()})
139 if isTestMode(ctx) {
140 out := new(empty.Empty)
141 return out, nil
142 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500143
144 if isOFControllerRequest(ctx) {
145 txn, err := handler.createKvTransaction(ctx)
146 if txn == nil {
147 return new(empty.Empty), err
148 } else if txn.Acquired(MAX_RESPONSE_TIME) {
149 defer txn.Close() // Ensure active core signals "done" to standby
150 } else {
151 return new(empty.Empty), errors.New("failed-to-seize-request")
152 }
153 }
khenaidoo4d4802d2018-10-04 21:59:49 -0400154 ch := make(chan interface{})
155 defer close(ch)
khenaidoo19d7b632018-10-30 10:49:50 -0400156 go handler.logicalDeviceMgr.enableLogicalPort(ctx, id, ch)
khenaidoo4d4802d2018-10-04 21:59:49 -0400157 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400158}
159
160func (handler *APIHandler) DisableLogicalDevicePort(ctx context.Context, id *voltha.LogicalPortId) (*empty.Empty, error) {
161 log.Debugw("DisableLogicalDevicePort-request", log.Fields{"id": id, "test": common.TestModeKeys_api_test.String()})
162 if isTestMode(ctx) {
163 out := new(empty.Empty)
164 return out, nil
165 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500166
167 if isOFControllerRequest(ctx) {
168 txn, err := handler.createKvTransaction(ctx)
169 if txn == nil {
170 return new(empty.Empty), err
171 } else if txn.Acquired(MAX_RESPONSE_TIME) {
172 defer txn.Close() // Ensure active core signals "done" to standby
173 } else {
174 return new(empty.Empty), errors.New("failed-to-seize-request")
175 }
176 }
khenaidoo19d7b632018-10-30 10:49:50 -0400177 ch := make(chan interface{})
178 defer close(ch)
179 go handler.logicalDeviceMgr.disableLogicalPort(ctx, id, ch)
180 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400181}
182
183func (handler *APIHandler) UpdateLogicalDeviceFlowTable(ctx context.Context, flow *openflow_13.FlowTableUpdate) (*empty.Empty, error) {
184 log.Debugw("UpdateLogicalDeviceFlowTable-request", log.Fields{"flow": flow, "test": common.TestModeKeys_api_test.String()})
185 if isTestMode(ctx) {
186 out := new(empty.Empty)
187 return out, nil
188 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500189
190 if isOFControllerRequest(ctx) {
191 txn, err := handler.createKvTransaction(ctx)
192 if txn == nil {
193 return new(empty.Empty), err
194 } else if txn.Acquired(MAX_RESPONSE_TIME) {
195 defer txn.Close() // Ensure active core signals "done" to standby
196 } else {
197 return new(empty.Empty), errors.New("failed-to-seize-request")
198 }
199 }
khenaidoo19d7b632018-10-30 10:49:50 -0400200 ch := make(chan interface{})
201 defer close(ch)
202 go handler.logicalDeviceMgr.updateFlowTable(ctx, flow.Id, flow.FlowMod, ch)
203 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400204}
205
206func (handler *APIHandler) UpdateLogicalDeviceFlowGroupTable(ctx context.Context, flow *openflow_13.FlowGroupTableUpdate) (*empty.Empty, error) {
207 log.Debugw("UpdateLogicalDeviceFlowGroupTable-request", log.Fields{"flow": flow, "test": common.TestModeKeys_api_test.String()})
208 if isTestMode(ctx) {
209 out := new(empty.Empty)
210 return out, nil
211 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500212
213 if isOFControllerRequest(ctx) {
214 txn, err := handler.createKvTransaction(ctx)
215 if txn == nil {
216 return new(empty.Empty), err
217 } else if txn.Acquired(MAX_RESPONSE_TIME) {
218 defer txn.Close() // Ensure active core signals "done" to standby
219 } else {
220 return new(empty.Empty), errors.New("failed-to-seize-request")
221 }
222 }
khenaidoo19d7b632018-10-30 10:49:50 -0400223 ch := make(chan interface{})
224 defer close(ch)
225 go handler.logicalDeviceMgr.updateGroupTable(ctx, flow.Id, flow.GroupMod, ch)
226 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400227}
228
khenaidoob9203542018-09-17 22:56:37 -0400229// GetDevice must be implemented in the read-only containers - should it also be implemented here?
230func (handler *APIHandler) GetDevice(ctx context.Context, id *voltha.ID) (*voltha.Device, error) {
231 log.Debugw("GetDevice-request", log.Fields{"id": id})
khenaidoo19d7b632018-10-30 10:49:50 -0400232 return handler.deviceMgr.GetDevice(id.Id)
khenaidoob9203542018-09-17 22:56:37 -0400233}
234
235// GetDevice must be implemented in the read-only containers - should it also be implemented here?
236func (handler *APIHandler) ListDevices(ctx context.Context, empty *empty.Empty) (*voltha.Devices, error) {
237 log.Debug("ListDevices")
238 return handler.deviceMgr.ListDevices()
239}
240
khenaidoo7ccedd52018-12-14 16:48:54 -0500241// ListDeviceIds returns the list of device ids managed by a voltha core
242func (handler *APIHandler) ListDeviceIds(ctx context.Context, empty *empty.Empty) (*voltha.IDs, error) {
243 log.Debug("ListDeviceIDs")
244 if isTestMode(ctx) {
245 out := &voltha.IDs{Items: make([]*voltha.ID, 0)}
246 return out, nil
247 }
248 return handler.deviceMgr.ListDeviceIds()
249}
250
251//ReconcileDevices is a request to a voltha core to managed a list of devices based on their IDs
252func (handler *APIHandler) ReconcileDevices(ctx context.Context, ids *voltha.IDs) (*empty.Empty, error) {
253 log.Debug("ReconcileDevices")
254 if isTestMode(ctx) {
255 out := new(empty.Empty)
256 return out, nil
257 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500258
259 if isOFControllerRequest(ctx) {
260 txn, err := handler.createKvTransaction(ctx)
261 if txn == nil {
262 return new(empty.Empty), err
263 } else if txn.Acquired(MAX_RESPONSE_TIME) {
264 defer txn.Close() // Ensure active core signals "done" to standby
265 } else {
266 return new(empty.Empty), errors.New("failed-to-seize-request")
267 }
268 }
khenaidoo7ccedd52018-12-14 16:48:54 -0500269 ch := make(chan interface{})
270 defer close(ch)
271 go handler.deviceMgr.ReconcileDevices(ctx, ids, ch)
272 return waitForNilResponseOnSuccess(ctx, ch)
273}
274
khenaidoob9203542018-09-17 22:56:37 -0400275// GetLogicalDevice must be implemented in the read-only containers - should it also be implemented here?
276func (handler *APIHandler) GetLogicalDevice(ctx context.Context, id *voltha.ID) (*voltha.LogicalDevice, error) {
277 log.Debugw("GetLogicalDevice-request", log.Fields{"id": id})
278 return handler.logicalDeviceMgr.getLogicalDevice(id.Id)
279}
280
281// ListLogicalDevices must be implemented in the read-only containers - should it also be implemented here?
282func (handler *APIHandler) ListLogicalDevices(ctx context.Context, empty *empty.Empty) (*voltha.LogicalDevices, error) {
283 log.Debug("ListLogicalDevices")
284 return handler.logicalDeviceMgr.listLogicalDevices()
285}
286
khenaidoo19d7b632018-10-30 10:49:50 -0400287// ListLogicalDevicePorts must be implemented in the read-only containers - should it also be implemented here?
288func (handler *APIHandler) ListLogicalDevicePorts(ctx context.Context, id *voltha.ID) (*voltha.LogicalPorts, error) {
289 log.Debugw("ListLogicalDevicePorts", log.Fields{"logicaldeviceid": id})
290 return handler.logicalDeviceMgr.ListLogicalDevicePorts(ctx, id.Id)
291}
292
khenaidoo4d4802d2018-10-04 21:59:49 -0400293// CreateDevice creates a new parent device in the data model
khenaidoobf6e7bb2018-08-14 22:27:29 -0400294func (handler *APIHandler) CreateDevice(ctx context.Context, device *voltha.Device) (*voltha.Device, error) {
khenaidoob9203542018-09-17 22:56:37 -0400295 log.Debugw("createdevice", log.Fields{"device": *device})
khenaidoobf6e7bb2018-08-14 22:27:29 -0400296 if isTestMode(ctx) {
297 return &voltha.Device{Id: device.Id}, nil
298 }
Richard Jankowskid42826e2018-11-02 16:06:37 -0400299
Richard Jankowski2755adf2019-01-17 17:16:48 -0500300 if isOFControllerRequest(ctx) {
301 txn, err := handler.createKvTransaction(ctx)
302 if txn == nil {
303 return &voltha.Device{}, err
304 } else if txn.Acquired(MAX_RESPONSE_TIME) {
305 defer txn.Close() // Ensure active core signals "done" to standby
306 } else {
307 return &voltha.Device{}, errors.New("failed-to-seize-request")
308 }
309 }
khenaidoob9203542018-09-17 22:56:37 -0400310 ch := make(chan interface{})
311 defer close(ch)
312 go handler.deviceMgr.createDevice(ctx, device, ch)
313 select {
314 case res := <-ch:
khenaidoo92e62c52018-10-03 14:02:54 -0400315 if res != nil {
316 if err, ok := res.(error); ok {
317 return &voltha.Device{}, err
318 }
319 if d, ok := res.(*voltha.Device); ok {
320 return d, nil
321 }
khenaidoob9203542018-09-17 22:56:37 -0400322 }
khenaidoo92e62c52018-10-03 14:02:54 -0400323 log.Warnw("create-device-unexpected-return-type", log.Fields{"result": res})
324 err := status.Errorf(codes.Internal, "%s", res)
325 return &voltha.Device{}, err
khenaidoob9203542018-09-17 22:56:37 -0400326 case <-ctx.Done():
327 log.Debug("createdevice-client-timeout")
328 return nil, ctx.Err()
329 }
khenaidoobf6e7bb2018-08-14 22:27:29 -0400330}
331
khenaidoo4d4802d2018-10-04 21:59:49 -0400332// EnableDevice activates a device by invoking the adopt_device API on the appropriate adapter
khenaidoobf6e7bb2018-08-14 22:27:29 -0400333func (handler *APIHandler) EnableDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) {
khenaidoob9203542018-09-17 22:56:37 -0400334 log.Debugw("enabledevice", log.Fields{"id": id})
khenaidoobf6e7bb2018-08-14 22:27:29 -0400335 if isTestMode(ctx) {
khenaidoo4d4802d2018-10-04 21:59:49 -0400336 return new(empty.Empty), nil
khenaidoobf6e7bb2018-08-14 22:27:29 -0400337 }
Richard Jankowskid42826e2018-11-02 16:06:37 -0400338
Richard Jankowski2755adf2019-01-17 17:16:48 -0500339 if isOFControllerRequest(ctx) {
340 txn, err := handler.createKvTransaction(ctx)
341 if txn == nil {
342 return new(empty.Empty), err
343 } else if txn.Acquired(MAX_RESPONSE_TIME) {
344 defer txn.Close() // Ensure active core signals "done" to standby
345 } else {
346 return new(empty.Empty), errors.New("failed-to-seize-request")
347 }
348 }
khenaidoob9203542018-09-17 22:56:37 -0400349 ch := make(chan interface{})
350 defer close(ch)
351 go handler.deviceMgr.enableDevice(ctx, id, ch)
khenaidoo4d4802d2018-10-04 21:59:49 -0400352 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400353}
354
khenaidoo4d4802d2018-10-04 21:59:49 -0400355// DisableDevice disables a device along with any child device it may have
khenaidoobf6e7bb2018-08-14 22:27:29 -0400356func (handler *APIHandler) DisableDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) {
357 log.Debugw("disabledevice-request", log.Fields{"id": id})
358 if isTestMode(ctx) {
khenaidoo4d4802d2018-10-04 21:59:49 -0400359 return new(empty.Empty), nil
khenaidoobf6e7bb2018-08-14 22:27:29 -0400360 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500361
362 if isOFControllerRequest(ctx) {
363 txn, err := handler.createKvTransaction(ctx)
364 if txn == nil {
365 return new(empty.Empty), err
366 } else if txn.Acquired(MAX_RESPONSE_TIME) {
367 defer txn.Close() // Ensure active core signals "done" to standby
368 } else {
369 return new(empty.Empty), errors.New("failed-to-seize-request")
370 }
371 }
khenaidoo92e62c52018-10-03 14:02:54 -0400372 ch := make(chan interface{})
373 defer close(ch)
374 go handler.deviceMgr.disableDevice(ctx, id, ch)
khenaidoo4d4802d2018-10-04 21:59:49 -0400375 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400376}
377
khenaidoo4d4802d2018-10-04 21:59:49 -0400378//RebootDevice invoked the reboot API to the corresponding adapter
khenaidoobf6e7bb2018-08-14 22:27:29 -0400379func (handler *APIHandler) RebootDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) {
khenaidoo4d4802d2018-10-04 21:59:49 -0400380 log.Debugw("rebootDevice-request", log.Fields{"id": id})
khenaidoobf6e7bb2018-08-14 22:27:29 -0400381 if isTestMode(ctx) {
khenaidoo4d4802d2018-10-04 21:59:49 -0400382 return new(empty.Empty), nil
khenaidoobf6e7bb2018-08-14 22:27:29 -0400383 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500384
385 if isOFControllerRequest(ctx) {
386 txn, err := handler.createKvTransaction(ctx)
387 if txn == nil {
388 return new(empty.Empty), err
389 } else if txn.Acquired(MAX_RESPONSE_TIME) {
390 defer txn.Close() // Ensure active core signals "done" to standby
391 } else {
392 return new(empty.Empty), errors.New("failed-to-seize-request")
393 }
394 }
khenaidoo4d4802d2018-10-04 21:59:49 -0400395 ch := make(chan interface{})
396 defer close(ch)
397 go handler.deviceMgr.rebootDevice(ctx, id, ch)
398 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400399}
400
khenaidoo4d4802d2018-10-04 21:59:49 -0400401// DeleteDevice removes a device from the data model
khenaidoobf6e7bb2018-08-14 22:27:29 -0400402func (handler *APIHandler) DeleteDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) {
403 log.Debugw("deletedevice-request", log.Fields{"id": id})
404 if isTestMode(ctx) {
khenaidoo4d4802d2018-10-04 21:59:49 -0400405 return new(empty.Empty), nil
khenaidoobf6e7bb2018-08-14 22:27:29 -0400406 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500407
408 if isOFControllerRequest(ctx) {
409 txn, err := handler.createKvTransaction(ctx)
410 if txn == nil {
411 return new(empty.Empty), err
412 } else if txn.Acquired(MAX_RESPONSE_TIME) {
413 defer txn.Close() // Ensure active core signals "done" to standby
414 } else {
415 return new(empty.Empty), errors.New("failed-to-seize-request")
416 }
417 }
khenaidoo4d4802d2018-10-04 21:59:49 -0400418 ch := make(chan interface{})
419 defer close(ch)
420 go handler.deviceMgr.deleteDevice(ctx, id, ch)
421 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400422}
423
424func (handler *APIHandler) DownloadImage(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) {
425 log.Debugw("DownloadImage-request", log.Fields{"img": *img})
426 if isTestMode(ctx) {
427 resp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS}
428 return resp, nil
429 }
430
431 return nil, errors.New("UnImplemented")
432}
433
434func (handler *APIHandler) CancelImageDownload(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) {
435 log.Debugw("CancelImageDownload-request", log.Fields{"img": *img})
436 if isTestMode(ctx) {
437 resp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS}
438 return resp, nil
439 }
440 return nil, errors.New("UnImplemented")
441}
442
443func (handler *APIHandler) ActivateImageUpdate(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) {
444 log.Debugw("ActivateImageUpdate-request", log.Fields{"img": *img})
445 if isTestMode(ctx) {
446 resp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS}
447 return resp, nil
448 }
449 return nil, errors.New("UnImplemented")
450}
451
452func (handler *APIHandler) RevertImageUpdate(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) {
453 log.Debugw("RevertImageUpdate-request", log.Fields{"img": *img})
454 if isTestMode(ctx) {
455 resp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS}
456 return resp, nil
457 }
458 return nil, errors.New("UnImplemented")
459}
460
461func (handler *APIHandler) UpdateDevicePmConfigs(ctx context.Context, configs *voltha.PmConfigs) (*empty.Empty, error) {
462 log.Debugw("UpdateDevicePmConfigs-request", log.Fields{"configs": *configs})
463 if isTestMode(ctx) {
464 out := new(empty.Empty)
465 return out, nil
466 }
467 return nil, errors.New("UnImplemented")
468}
469
470func (handler *APIHandler) CreateAlarmFilter(ctx context.Context, filter *voltha.AlarmFilter) (*voltha.AlarmFilter, error) {
471 log.Debugw("CreateAlarmFilter-request", log.Fields{"filter": *filter})
472 if isTestMode(ctx) {
473 f := &voltha.AlarmFilter{Id: filter.Id}
474 return f, nil
475 }
476 return nil, errors.New("UnImplemented")
477}
478
479func (handler *APIHandler) UpdateAlarmFilter(ctx context.Context, filter *voltha.AlarmFilter) (*voltha.AlarmFilter, error) {
480 log.Debugw("UpdateAlarmFilter-request", log.Fields{"filter": *filter})
481 if isTestMode(ctx) {
482 f := &voltha.AlarmFilter{Id: filter.Id}
483 return f, nil
484 }
485 return nil, errors.New("UnImplemented")
486}
487
488func (handler *APIHandler) DeleteAlarmFilter(ctx context.Context, id *voltha.ID) (*empty.Empty, error) {
489 log.Debugw("DeleteAlarmFilter-request", log.Fields{"id": *id})
490 if isTestMode(ctx) {
491 out := new(empty.Empty)
492 return out, nil
493 }
494 return nil, errors.New("UnImplemented")
495}
496
497func (handler *APIHandler) SelfTest(ctx context.Context, id *voltha.ID) (*voltha.SelfTestResponse, error) {
498 log.Debugw("SelfTest-request", log.Fields{"id": id})
499 if isTestMode(ctx) {
500 resp := &voltha.SelfTestResponse{Result: voltha.SelfTestResponse_SUCCESS}
501 return resp, nil
502 }
503 return nil, errors.New("UnImplemented")
504}
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500505
506func (handler *APIHandler) forwardPacketOut(packet *openflow_13.PacketOut) {
507 log.Debugw("forwardPacketOut-request", log.Fields{"packet": packet})
Richard Jankowski2755adf2019-01-17 17:16:48 -0500508 agent := handler.logicalDeviceMgr.getLogicalDeviceAgent(packet.Id)
509 agent.packetOut(packet.PacketOut)
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500510}
511func (handler *APIHandler) StreamPacketsOut(
512 packets voltha.VolthaService_StreamPacketsOutServer,
513) error {
514 log.Debugw("StreamPacketsOut-request", log.Fields{"packets": packets})
515 for {
516 packet, err := packets.Recv()
517
518 if err == io.EOF {
519 break
520 } else if err != nil {
521 log.Errorw("Failed to receive packet", log.Fields{"error": err})
522 }
523
524 handler.forwardPacketOut(packet)
525 }
526
527 log.Debugw("StreamPacketsOut-request-done", log.Fields{"packets": packets})
528 return nil
529}
530
531func (handler *APIHandler) sendPacketIn(deviceId string, packet *openflow_13.OfpPacketIn) {
532 packetIn := openflow_13.PacketIn{Id: deviceId, PacketIn: packet}
533 log.Debugw("sendPacketIn", log.Fields{"packetIn": packetIn})
Richard Jankowskidbab94a2018-12-06 16:20:25 -0500534 // Enqueue the packet
535 if err := handler.packetInQueue.Put(packetIn); err != nil {
536 log.Errorw("failed-to-enqueue-packet", log.Fields{"error": err})
537 }
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500538}
539
540func (handler *APIHandler) ReceivePacketsIn(
541 empty *empty.Empty,
542 packetsIn voltha.VolthaService_ReceivePacketsInServer,
543) error {
544 log.Debugw("ReceivePacketsIn-request", log.Fields{"packetsIn": packetsIn})
545
546 for {
Richard Jankowskidbab94a2018-12-06 16:20:25 -0500547 // Dequeue a packet
548 if packets, err := handler.packetInQueue.Get(1); err == nil {
549 log.Debugw("dequeued-packet", log.Fields{"packet": packets[0]})
550 if packet, ok := packets[0].(openflow_13.PacketIn); ok {
551 log.Debugw("sending-packet-in", log.Fields{"packet": packet})
552 if err := packetsIn.Send(&packet); err != nil {
553 log.Errorw("failed-to-send-packet", log.Fields{"error": err})
554 }
555 }
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500556 }
557 }
558 log.Debugw("ReceivePacketsIn-request-done", log.Fields{"packetsIn": packetsIn})
559 return nil
560}
561
562func (handler *APIHandler) sendChangeEvent(deviceId string, portStatus *openflow_13.OfpPortStatus) {
563 // TODO: validate the type of portStatus parameter
564 //if _, ok := portStatus.(*openflow_13.OfpPortStatus); ok {
565 //}
566 event := openflow_13.ChangeEvent{Id: deviceId, Event: &openflow_13.ChangeEvent_PortStatus{PortStatus: portStatus}}
567 log.Debugw("sendChangeEvent", log.Fields{"event": event})
568 // TODO: put the packet in the queue
569}
570
571func (handler *APIHandler) ReceiveChangeEvents(
572 empty *empty.Empty,
573 changeEvents voltha.VolthaService_ReceiveChangeEventsServer,
574) error {
575 log.Debugw("ReceiveChangeEvents-request", log.Fields{"changeEvents": changeEvents})
576 for {
577 // TODO: need to retrieve packet from queue
578 event := &openflow_13.ChangeEvent{}
579 time.Sleep(time.Duration(5) * time.Second)
580 err := changeEvents.Send(event)
581 if err != nil {
582 log.Errorw("Failed to send change event", log.Fields{"error": err})
583 }
584 }
585 return nil
586}
587
588func (handler *APIHandler) Subscribe(
589 ctx context.Context,
590 ofAgent *voltha.OfAgentSubscriber,
591) (*voltha.OfAgentSubscriber, error) {
592 log.Debugw("Subscribe-request", log.Fields{"ofAgent": ofAgent})
593 return &voltha.OfAgentSubscriber{OfagentId: ofAgent.OfagentId, VolthaId: ofAgent.VolthaId}, nil
594}