blob: 7ed756c2033f34964f7f1a2b3b7874741886e4f7 [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"
khenaidoo1ce37ad2019-03-24 22:07:24 -040025 "github.com/opencord/voltha-go/rw_core/utils"
William Kurkiandaa6bb22019-03-07 12:26:28 -050026 "github.com/opencord/voltha-protos/go/common"
27 "github.com/opencord/voltha-protos/go/openflow_13"
28 "github.com/opencord/voltha-protos/go/omci"
29 "github.com/opencord/voltha-protos/go/voltha"
khenaidoob9203542018-09-17 22:56:37 -040030 "google.golang.org/grpc/codes"
khenaidoobf6e7bb2018-08-14 22:27:29 -040031 "google.golang.org/grpc/metadata"
khenaidoob9203542018-09-17 22:56:37 -040032 "google.golang.org/grpc/status"
Stephane Barbarie6e1bd502018-11-05 22:44:45 -050033 "io"
34 "time"
khenaidoobf6e7bb2018-08-14 22:27:29 -040035)
36
khenaidoof5a5bfa2019-01-23 22:20:29 -050037const (
38 IMAGE_DOWNLOAD = iota
39 CANCEL_IMAGE_DOWNLOAD = iota
40 ACTIVATE_IMAGE = iota
41 REVERT_IMAGE = iota
42)
43
khenaidoo297cd252019-02-07 22:10:23 -050044
khenaidoobf6e7bb2018-08-14 22:27:29 -040045type APIHandler struct {
khenaidoob9203542018-09-17 22:56:37 -040046 deviceMgr *DeviceManager
47 logicalDeviceMgr *LogicalDeviceManager
khenaidoo21d51152019-02-01 13:48:37 -050048 adapterMgr *AdapterManager
khenaidood2b6df92018-12-13 16:37:20 -050049 packetInQueue *queue.Queue
Richard Jankowski199fd862019-03-18 14:49:51 -040050 changeEventQueue *queue.Queue
khenaidoo9cdc1a62019-01-24 21:57:40 -050051 coreInCompetingMode bool
khenaidoob6080322019-01-29 21:47:38 -050052 longRunningRequestTimeout int64
53 defaultRequestTimeout int64
khenaidoobf6e7bb2018-08-14 22:27:29 -040054 da.DefaultAPIHandler
khenaidoo54e0ddf2019-02-27 16:21:33 -050055 core *Core
khenaidoobf6e7bb2018-08-14 22:27:29 -040056}
57
khenaidoo54e0ddf2019-02-27 16:21:33 -050058func NewAPIHandler(core *Core) *APIHandler {
Stephane Barbarie6e1bd502018-11-05 22:44:45 -050059 handler := &APIHandler{
khenaidoo54e0ddf2019-02-27 16:21:33 -050060 deviceMgr: core.deviceMgr,
61 logicalDeviceMgr: core.logicalDeviceMgr,
62 adapterMgr: core.adapterMgr,
63 coreInCompetingMode: core.config.InCompetingMode,
64 longRunningRequestTimeout:core.config.LongRunningRequestTimeout,
65 defaultRequestTimeout:core.config.DefaultRequestTimeout,
Richard Jankowskidbab94a2018-12-06 16:20:25 -050066 // TODO: Figure out what the 'hint' parameter to queue.New does
khenaidood2b6df92018-12-13 16:37:20 -050067 packetInQueue: queue.New(10),
Richard Jankowski199fd862019-03-18 14:49:51 -040068 changeEventQueue: queue.New(10),
khenaidoo54e0ddf2019-02-27 16:21:33 -050069 core: core,
Stephane Barbarie6e1bd502018-11-05 22:44:45 -050070 }
khenaidoobf6e7bb2018-08-14 22:27:29 -040071 return handler
72}
khenaidoo4d4802d2018-10-04 21:59:49 -040073
74// isTestMode is a helper function to determine a function is invoked for testing only
khenaidoobf6e7bb2018-08-14 22:27:29 -040075func isTestMode(ctx context.Context) bool {
76 md, _ := metadata.FromIncomingContext(ctx)
77 _, exist := md[common.TestModeKeys_api_test.String()]
78 return exist
79}
80
Richard Jankowskid42826e2018-11-02 16:06:37 -040081// This function attempts to extract the serial number from the request metadata
82// and create a KV transaction for that serial number for the current core.
83func (handler *APIHandler) createKvTransaction(ctx context.Context) (*KVTransaction, error) {
84 var (
khenaidoo43c82122018-11-22 18:38:28 -050085 err error
86 ok bool
87 md metadata.MD
Richard Jankowskid42826e2018-11-02 16:06:37 -040088 serNum []string
89 )
90 if md, ok = metadata.FromIncomingContext(ctx); !ok {
91 err = errors.New("metadata-not-found")
92 } else if serNum, ok = md["voltha_serial_number"]; !ok {
93 err = errors.New("serial-number-not-found")
94 }
95 if !ok {
96 log.Error(err)
97 return nil, err
98 }
99 // Create KV transaction
100 txn := NewKVTransaction(serNum[0])
101 return txn, nil
102}
103
Richard Jankowski2755adf2019-01-17 17:16:48 -0500104// isOFControllerRequest is a helper function to determine if a request was initiated
105// from the OpenFlow controller (or its proxy, e.g. OFAgent)
Richard Jankowski46464e92019-03-05 11:53:55 -0500106func (handler *APIHandler) isOFControllerRequest(ctx context.Context) bool {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500107 if md, ok := metadata.FromIncomingContext(ctx); ok {
108 // Metadata in context
Richard Jankowski46464e92019-03-05 11:53:55 -0500109 if _, ok = md[handler.core.config.CoreBindingKey]; ok {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500110 // OFAgent field in metadata
111 return true
112 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500113 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500114 return false
115}
116
117// competeForTransaction is a helper function to determine whether every request needs to compete with another
118// Core to execute the request
119func (handler *APIHandler) competeForTransaction() bool {
120 return handler.coreInCompetingMode
121}
122
Richard Jankowski199fd862019-03-18 14:49:51 -0400123// This function handles the creation of new devices
124func (handler *APIHandler) acquireRequest(ctx context.Context, id interface{}, maxTimeout ...int64) (*KVTransaction, error) {
khenaidoob6080322019-01-29 21:47:38 -0500125 timeout := handler.defaultRequestTimeout
khenaidoo9cdc1a62019-01-24 21:57:40 -0500126 if len(maxTimeout) > 0 {
127 timeout = maxTimeout[0]
Richard Jankowski2755adf2019-01-17 17:16:48 -0500128 }
khenaidoob6080322019-01-29 21:47:38 -0500129 log.Debugw("transaction-timeout", log.Fields{"timeout": timeout})
khenaidoo9cdc1a62019-01-24 21:57:40 -0500130 txn, err := handler.createKvTransaction(ctx)
131 if txn == nil {
132 return nil, err
133 } else if txn.Acquired(timeout) {
134 return txn, nil
135 } else {
khenaidoo297cd252019-02-07 22:10:23 -0500136 if id != nil {
137 // The id can either be a device Id or a logical device id.
khenaidoo1ce37ad2019-03-24 22:07:24 -0400138 if dId, ok := id.(*utils.DeviceID); ok {
khenaidoo297cd252019-02-07 22:10:23 -0500139 // Since this core has not processed this request, let's load the device, along with its extended
140 // family (parents and children) in memory. This will keep this core in-sync with its paired core as
141 // much as possible. The watch feature in the core model will ensure that the contents of those objects in
142 // memory are in sync.
143 time.Sleep(2 * time.Second)
khenaidoo1ce37ad2019-03-24 22:07:24 -0400144 go handler.deviceMgr.load(dId.Id)
145 } else if ldId, ok := id.(*utils.LogicalDeviceID); ok {
khenaidoo297cd252019-02-07 22:10:23 -0500146 // This will load the logical device along with its children and grandchildren
khenaidoo1ce37ad2019-03-24 22:07:24 -0400147 go handler.logicalDeviceMgr.load(ldId.Id)
khenaidoo297cd252019-02-07 22:10:23 -0500148 }
149 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500150 return nil, errors.New("failed-to-seize-request")
Richard Jankowski2755adf2019-01-17 17:16:48 -0500151 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500152}
153
Richard Jankowski199fd862019-03-18 14:49:51 -0400154// This function handles the modification or deletion of existing devices
155func (handler *APIHandler) takeRequestOwnership(ctx context.Context, id interface{}, maxTimeout ...int64) (*KVTransaction, error) {
156 timeout := handler.defaultRequestTimeout
157 if len(maxTimeout) > 0 {
158 timeout = maxTimeout[0]
159 }
160 log.Debugw("transaction-timeout", log.Fields{"timeout": timeout})
161 txn, err := handler.createKvTransaction(ctx)
162 if txn == nil {
163 return nil, err
164 }
165
166 owned := false
167 if id != nil {
khenaidoo1ce37ad2019-03-24 22:07:24 -0400168 owned = handler.core.deviceOwnership.OwnedByMe(id)
Richard Jankowski199fd862019-03-18 14:49:51 -0400169 }
170 if owned {
171 if txn.Acquired(timeout) {
172 return txn, nil
173 } else {
174 return nil, errors.New("failed-to-seize-request")
175 }
176 } else {
177 if txn.Monitor(timeout) {
178 return txn, nil
179 } else {
180 return nil, errors.New("device-not-owned")
181 }
182 }
183}
184
khenaidoo4d4802d2018-10-04 21:59:49 -0400185// waitForNilResponseOnSuccess is a helper function to wait for a response on channel ch where an nil
186// response is expected in a successful scenario
187func waitForNilResponseOnSuccess(ctx context.Context, ch chan interface{}) (*empty.Empty, error) {
188 select {
189 case res := <-ch:
190 if res == nil {
191 return new(empty.Empty), nil
192 } else if err, ok := res.(error); ok {
193 return new(empty.Empty), err
194 } else {
195 log.Warnw("unexpected-return-type", log.Fields{"result": res})
196 err = status.Errorf(codes.Internal, "%s", res)
197 return new(empty.Empty), err
198 }
199 case <-ctx.Done():
200 log.Debug("client-timeout")
201 return nil, ctx.Err()
202 }
203}
204
khenaidoobf6e7bb2018-08-14 22:27:29 -0400205func (handler *APIHandler) UpdateLogLevel(ctx context.Context, logging *voltha.Logging) (*empty.Empty, error) {
khenaidoo6f2fbe32019-01-18 16:16:50 -0500206 log.Debugw("UpdateLogLevel-request", log.Fields{"package": logging.PackageName, "intval": int(logging.Level)})
khenaidoo92e62c52018-10-03 14:02:54 -0400207 out := new(empty.Empty)
khenaidoo6f2fbe32019-01-18 16:16:50 -0500208 if logging.PackageName == "" {
209 log.SetAllLogLevel(int(logging.Level))
210 } else {
211 log.SetPackageLogLevel(logging.PackageName, int(logging.Level))
212 }
khenaidoo92e62c52018-10-03 14:02:54 -0400213 return out, nil
khenaidoobf6e7bb2018-08-14 22:27:29 -0400214}
215
khenaidoo54e0ddf2019-02-27 16:21:33 -0500216
217func (handler *APIHandler) UpdateMembership(ctx context.Context, membership *voltha.Membership) (*empty.Empty, error) {
218 log.Debugw("UpdateMembership-request", log.Fields{"membership": membership})
219 out := new(empty.Empty)
khenaidoo6417b6c2019-03-01 18:18:01 -0500220 if err := handler.core.updateCoreMembership(ctx, membership); err != nil {
khenaidoo54e0ddf2019-02-27 16:21:33 -0500221 return out, err
222 }
223 return out, nil
224}
225
khenaidoo6417b6c2019-03-01 18:18:01 -0500226func (handler *APIHandler) GetMembership(ctx context.Context, empty *empty.Empty) (*voltha.Membership, error) {
227 log.Debug("GetMembership-request")
228 if membership := handler.core.getCoreMembership(ctx); membership != nil {
229 return membership, nil
230 }
231 return &voltha.Membership{}, nil
232}
233
234
khenaidoobf6e7bb2018-08-14 22:27:29 -0400235func (handler *APIHandler) EnableLogicalDevicePort(ctx context.Context, id *voltha.LogicalPortId) (*empty.Empty, error) {
236 log.Debugw("EnableLogicalDevicePort-request", log.Fields{"id": id, "test": common.TestModeKeys_api_test.String()})
237 if isTestMode(ctx) {
238 out := new(empty.Empty)
239 return out, nil
240 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500241
khenaidoo9cdc1a62019-01-24 21:57:40 -0500242 if handler.competeForTransaction() {
khenaidoo1ce37ad2019-03-24 22:07:24 -0400243 if txn, err := handler.takeRequestOwnership(ctx, &utils.LogicalDeviceID{Id:id.Id}); err != nil {
Richard Jankowski2755adf2019-01-17 17:16:48 -0500244 return new(empty.Empty), err
Richard Jankowski2755adf2019-01-17 17:16:48 -0500245 } else {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500246 defer txn.Close()
Richard Jankowski2755adf2019-01-17 17:16:48 -0500247 }
248 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500249
khenaidoo4d4802d2018-10-04 21:59:49 -0400250 ch := make(chan interface{})
251 defer close(ch)
khenaidoo19d7b632018-10-30 10:49:50 -0400252 go handler.logicalDeviceMgr.enableLogicalPort(ctx, id, ch)
khenaidoo4d4802d2018-10-04 21:59:49 -0400253 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400254}
255
256func (handler *APIHandler) DisableLogicalDevicePort(ctx context.Context, id *voltha.LogicalPortId) (*empty.Empty, error) {
257 log.Debugw("DisableLogicalDevicePort-request", log.Fields{"id": id, "test": common.TestModeKeys_api_test.String()})
258 if isTestMode(ctx) {
259 out := new(empty.Empty)
260 return out, nil
261 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500262
khenaidoo9cdc1a62019-01-24 21:57:40 -0500263 if handler.competeForTransaction() {
khenaidoo1ce37ad2019-03-24 22:07:24 -0400264 if txn, err := handler.takeRequestOwnership(ctx, &utils.LogicalDeviceID{Id:id.Id}); err != nil {
Richard Jankowski2755adf2019-01-17 17:16:48 -0500265 return new(empty.Empty), err
Richard Jankowski2755adf2019-01-17 17:16:48 -0500266 } else {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500267 defer txn.Close()
Richard Jankowski2755adf2019-01-17 17:16:48 -0500268 }
269 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500270
khenaidoo19d7b632018-10-30 10:49:50 -0400271 ch := make(chan interface{})
272 defer close(ch)
273 go handler.logicalDeviceMgr.disableLogicalPort(ctx, id, ch)
274 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400275}
276
277func (handler *APIHandler) UpdateLogicalDeviceFlowTable(ctx context.Context, flow *openflow_13.FlowTableUpdate) (*empty.Empty, error) {
278 log.Debugw("UpdateLogicalDeviceFlowTable-request", log.Fields{"flow": flow, "test": common.TestModeKeys_api_test.String()})
279 if isTestMode(ctx) {
280 out := new(empty.Empty)
281 return out, nil
282 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500283
khenaidoo9cdc1a62019-01-24 21:57:40 -0500284 if handler.competeForTransaction() {
Richard Jankowski46464e92019-03-05 11:53:55 -0500285 if !handler.isOFControllerRequest(ctx) { // No need to acquire the transaction as request is sent to one core only
khenaidoo1ce37ad2019-03-24 22:07:24 -0400286 if txn, err := handler.takeRequestOwnership(ctx, &utils.LogicalDeviceID{Id:flow.Id}); err != nil {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500287 return new(empty.Empty), err
288 } else {
289 defer txn.Close()
290 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500291 }
292 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500293
khenaidoo19d7b632018-10-30 10:49:50 -0400294 ch := make(chan interface{})
295 defer close(ch)
296 go handler.logicalDeviceMgr.updateFlowTable(ctx, flow.Id, flow.FlowMod, ch)
297 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400298}
299
300func (handler *APIHandler) UpdateLogicalDeviceFlowGroupTable(ctx context.Context, flow *openflow_13.FlowGroupTableUpdate) (*empty.Empty, error) {
301 log.Debugw("UpdateLogicalDeviceFlowGroupTable-request", log.Fields{"flow": flow, "test": common.TestModeKeys_api_test.String()})
302 if isTestMode(ctx) {
303 out := new(empty.Empty)
304 return out, nil
305 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500306
khenaidoo9cdc1a62019-01-24 21:57:40 -0500307 if handler.competeForTransaction() {
Richard Jankowski46464e92019-03-05 11:53:55 -0500308 if !handler.isOFControllerRequest(ctx) { // No need to acquire the transaction as request is sent to one core only
khenaidoo1ce37ad2019-03-24 22:07:24 -0400309 if txn, err := handler.takeRequestOwnership(ctx, &utils.LogicalDeviceID{Id:flow.Id}); err != nil {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500310 return new(empty.Empty), err
311 } else {
312 defer txn.Close()
313 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500314 }
315 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500316
khenaidoo19d7b632018-10-30 10:49:50 -0400317 ch := make(chan interface{})
318 defer close(ch)
319 go handler.logicalDeviceMgr.updateGroupTable(ctx, flow.Id, flow.GroupMod, ch)
320 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400321}
322
khenaidoob9203542018-09-17 22:56:37 -0400323// GetDevice must be implemented in the read-only containers - should it also be implemented here?
324func (handler *APIHandler) GetDevice(ctx context.Context, id *voltha.ID) (*voltha.Device, error) {
325 log.Debugw("GetDevice-request", log.Fields{"id": id})
khenaidoo19d7b632018-10-30 10:49:50 -0400326 return handler.deviceMgr.GetDevice(id.Id)
khenaidoob9203542018-09-17 22:56:37 -0400327}
328
329// GetDevice must be implemented in the read-only containers - should it also be implemented here?
330func (handler *APIHandler) ListDevices(ctx context.Context, empty *empty.Empty) (*voltha.Devices, error) {
331 log.Debug("ListDevices")
332 return handler.deviceMgr.ListDevices()
333}
334
khenaidoo7ccedd52018-12-14 16:48:54 -0500335// ListDeviceIds returns the list of device ids managed by a voltha core
336func (handler *APIHandler) ListDeviceIds(ctx context.Context, empty *empty.Empty) (*voltha.IDs, error) {
337 log.Debug("ListDeviceIDs")
338 if isTestMode(ctx) {
339 out := &voltha.IDs{Items: make([]*voltha.ID, 0)}
340 return out, nil
341 }
342 return handler.deviceMgr.ListDeviceIds()
343}
344
345//ReconcileDevices is a request to a voltha core to managed a list of devices based on their IDs
346func (handler *APIHandler) ReconcileDevices(ctx context.Context, ids *voltha.IDs) (*empty.Empty, error) {
347 log.Debug("ReconcileDevices")
348 if isTestMode(ctx) {
349 out := new(empty.Empty)
350 return out, nil
351 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500352
khenaidoo9cdc1a62019-01-24 21:57:40 -0500353 // No need to grab a transaction as this request is core specific
354
khenaidoo7ccedd52018-12-14 16:48:54 -0500355 ch := make(chan interface{})
356 defer close(ch)
357 go handler.deviceMgr.ReconcileDevices(ctx, ids, ch)
358 return waitForNilResponseOnSuccess(ctx, ch)
359}
360
khenaidoob9203542018-09-17 22:56:37 -0400361// GetLogicalDevice must be implemented in the read-only containers - should it also be implemented here?
362func (handler *APIHandler) GetLogicalDevice(ctx context.Context, id *voltha.ID) (*voltha.LogicalDevice, error) {
363 log.Debugw("GetLogicalDevice-request", log.Fields{"id": id})
364 return handler.logicalDeviceMgr.getLogicalDevice(id.Id)
365}
366
367// ListLogicalDevices must be implemented in the read-only containers - should it also be implemented here?
368func (handler *APIHandler) ListLogicalDevices(ctx context.Context, empty *empty.Empty) (*voltha.LogicalDevices, error) {
369 log.Debug("ListLogicalDevices")
370 return handler.logicalDeviceMgr.listLogicalDevices()
371}
372
khenaidoo21d51152019-02-01 13:48:37 -0500373
374// ListAdapters returns the contents of all adapters known to the system
375func (handler *APIHandler) ListAdapters(ctx context.Context, empty *empty.Empty) (*voltha.Adapters, error) {
376 log.Debug("ListDevices")
377 return handler.adapterMgr.listAdapters(ctx)
378}
379
khenaidoo19d7b632018-10-30 10:49:50 -0400380// ListLogicalDevicePorts must be implemented in the read-only containers - should it also be implemented here?
381func (handler *APIHandler) ListLogicalDevicePorts(ctx context.Context, id *voltha.ID) (*voltha.LogicalPorts, error) {
382 log.Debugw("ListLogicalDevicePorts", log.Fields{"logicaldeviceid": id})
383 return handler.logicalDeviceMgr.ListLogicalDevicePorts(ctx, id.Id)
384}
385
khenaidoo4d4802d2018-10-04 21:59:49 -0400386// CreateDevice creates a new parent device in the data model
khenaidoobf6e7bb2018-08-14 22:27:29 -0400387func (handler *APIHandler) CreateDevice(ctx context.Context, device *voltha.Device) (*voltha.Device, error) {
khenaidoob9203542018-09-17 22:56:37 -0400388 log.Debugw("createdevice", log.Fields{"device": *device})
khenaidoobf6e7bb2018-08-14 22:27:29 -0400389 if isTestMode(ctx) {
390 return &voltha.Device{Id: device.Id}, nil
391 }
Richard Jankowskid42826e2018-11-02 16:06:37 -0400392
khenaidoo9cdc1a62019-01-24 21:57:40 -0500393 if handler.competeForTransaction() {
Richard Jankowski199fd862019-03-18 14:49:51 -0400394 if txn, err := handler.acquireRequest(ctx, nil); err != nil {
Richard Jankowski2755adf2019-01-17 17:16:48 -0500395 return &voltha.Device{}, err
Richard Jankowski2755adf2019-01-17 17:16:48 -0500396 } else {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500397 defer txn.Close()
Richard Jankowski2755adf2019-01-17 17:16:48 -0500398 }
399 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500400
khenaidoob9203542018-09-17 22:56:37 -0400401 ch := make(chan interface{})
402 defer close(ch)
403 go handler.deviceMgr.createDevice(ctx, device, ch)
404 select {
405 case res := <-ch:
khenaidoo92e62c52018-10-03 14:02:54 -0400406 if res != nil {
407 if err, ok := res.(error); ok {
408 return &voltha.Device{}, err
409 }
410 if d, ok := res.(*voltha.Device); ok {
khenaidoo1ce37ad2019-03-24 22:07:24 -0400411 handler.core.deviceOwnership.OwnedByMe(&utils.DeviceID{Id:d.Id})
khenaidoo92e62c52018-10-03 14:02:54 -0400412 return d, nil
413 }
khenaidoob9203542018-09-17 22:56:37 -0400414 }
khenaidoo92e62c52018-10-03 14:02:54 -0400415 log.Warnw("create-device-unexpected-return-type", log.Fields{"result": res})
416 err := status.Errorf(codes.Internal, "%s", res)
417 return &voltha.Device{}, err
khenaidoob9203542018-09-17 22:56:37 -0400418 case <-ctx.Done():
419 log.Debug("createdevice-client-timeout")
420 return nil, ctx.Err()
421 }
khenaidoobf6e7bb2018-08-14 22:27:29 -0400422}
423
khenaidoo4d4802d2018-10-04 21:59:49 -0400424// EnableDevice activates a device by invoking the adopt_device API on the appropriate adapter
khenaidoobf6e7bb2018-08-14 22:27:29 -0400425func (handler *APIHandler) EnableDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) {
khenaidoob9203542018-09-17 22:56:37 -0400426 log.Debugw("enabledevice", log.Fields{"id": id})
khenaidoobf6e7bb2018-08-14 22:27:29 -0400427 if isTestMode(ctx) {
khenaidoo4d4802d2018-10-04 21:59:49 -0400428 return new(empty.Empty), nil
khenaidoobf6e7bb2018-08-14 22:27:29 -0400429 }
Richard Jankowskid42826e2018-11-02 16:06:37 -0400430
khenaidoo9cdc1a62019-01-24 21:57:40 -0500431 if handler.competeForTransaction() {
khenaidoo1ce37ad2019-03-24 22:07:24 -0400432 if txn, err := handler.takeRequestOwnership(ctx, &utils.DeviceID{Id:id.Id}, handler.longRunningRequestTimeout); err != nil {
Richard Jankowski2755adf2019-01-17 17:16:48 -0500433 return new(empty.Empty), err
Richard Jankowski2755adf2019-01-17 17:16:48 -0500434 } else {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500435 defer txn.Close()
Richard Jankowski2755adf2019-01-17 17:16:48 -0500436 }
437 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500438
khenaidoob9203542018-09-17 22:56:37 -0400439 ch := make(chan interface{})
440 defer close(ch)
441 go handler.deviceMgr.enableDevice(ctx, id, ch)
khenaidoo4d4802d2018-10-04 21:59:49 -0400442 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400443}
444
khenaidoo4d4802d2018-10-04 21:59:49 -0400445// DisableDevice disables a device along with any child device it may have
khenaidoobf6e7bb2018-08-14 22:27:29 -0400446func (handler *APIHandler) DisableDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) {
447 log.Debugw("disabledevice-request", log.Fields{"id": id})
448 if isTestMode(ctx) {
khenaidoo4d4802d2018-10-04 21:59:49 -0400449 return new(empty.Empty), nil
khenaidoobf6e7bb2018-08-14 22:27:29 -0400450 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500451
khenaidoo9cdc1a62019-01-24 21:57:40 -0500452 if handler.competeForTransaction() {
khenaidoo1ce37ad2019-03-24 22:07:24 -0400453 if txn, err := handler.takeRequestOwnership(ctx, &utils.DeviceID{Id:id.Id}); err != nil {
Richard Jankowski2755adf2019-01-17 17:16:48 -0500454 return new(empty.Empty), err
Richard Jankowski2755adf2019-01-17 17:16:48 -0500455 } else {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500456 defer txn.Close()
Richard Jankowski2755adf2019-01-17 17:16:48 -0500457 }
458 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500459
khenaidoo92e62c52018-10-03 14:02:54 -0400460 ch := make(chan interface{})
461 defer close(ch)
462 go handler.deviceMgr.disableDevice(ctx, id, ch)
khenaidoo4d4802d2018-10-04 21:59:49 -0400463 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400464}
465
khenaidoo4d4802d2018-10-04 21:59:49 -0400466//RebootDevice invoked the reboot API to the corresponding adapter
khenaidoobf6e7bb2018-08-14 22:27:29 -0400467func (handler *APIHandler) RebootDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) {
khenaidoo4d4802d2018-10-04 21:59:49 -0400468 log.Debugw("rebootDevice-request", log.Fields{"id": id})
khenaidoobf6e7bb2018-08-14 22:27:29 -0400469 if isTestMode(ctx) {
khenaidoo4d4802d2018-10-04 21:59:49 -0400470 return new(empty.Empty), nil
khenaidoobf6e7bb2018-08-14 22:27:29 -0400471 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500472
khenaidoo9cdc1a62019-01-24 21:57:40 -0500473 if handler.competeForTransaction() {
khenaidoo1ce37ad2019-03-24 22:07:24 -0400474 if txn, err := handler.takeRequestOwnership(ctx, &utils.DeviceID{Id:id.Id}); err != nil {
Richard Jankowski2755adf2019-01-17 17:16:48 -0500475 return new(empty.Empty), err
Richard Jankowski2755adf2019-01-17 17:16:48 -0500476 } else {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500477 defer txn.Close()
Richard Jankowski2755adf2019-01-17 17:16:48 -0500478 }
479 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500480
khenaidoo4d4802d2018-10-04 21:59:49 -0400481 ch := make(chan interface{})
482 defer close(ch)
483 go handler.deviceMgr.rebootDevice(ctx, id, ch)
484 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400485}
486
khenaidoo4d4802d2018-10-04 21:59:49 -0400487// DeleteDevice removes a device from the data model
khenaidoobf6e7bb2018-08-14 22:27:29 -0400488func (handler *APIHandler) DeleteDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) {
489 log.Debugw("deletedevice-request", log.Fields{"id": id})
490 if isTestMode(ctx) {
khenaidoo4d4802d2018-10-04 21:59:49 -0400491 return new(empty.Empty), nil
khenaidoobf6e7bb2018-08-14 22:27:29 -0400492 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500493
khenaidoo9cdc1a62019-01-24 21:57:40 -0500494 if handler.competeForTransaction() {
Richard Jankowski199fd862019-03-18 14:49:51 -0400495 if txn, err := handler.takeRequestOwnership(ctx, nil); err != nil {
Richard Jankowski2755adf2019-01-17 17:16:48 -0500496 return new(empty.Empty), err
Richard Jankowski2755adf2019-01-17 17:16:48 -0500497 } else {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500498 defer txn.Close()
Richard Jankowski2755adf2019-01-17 17:16:48 -0500499 }
500 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500501
khenaidoo4d4802d2018-10-04 21:59:49 -0400502 ch := make(chan interface{})
503 defer close(ch)
504 go handler.deviceMgr.deleteDevice(ctx, id, ch)
505 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400506}
507
khenaidoof5a5bfa2019-01-23 22:20:29 -0500508// processImageRequest is a helper method to execute an image download request
509func (handler *APIHandler) processImageRequest(ctx context.Context, img *voltha.ImageDownload, requestType int) (*common.OperationResp, error) {
510 log.Debugw("processImageDownload", log.Fields{"img": *img, "requestType": requestType})
511 if isTestMode(ctx) {
512 resp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS}
513 return resp, nil
514 }
515
khenaidoo9cdc1a62019-01-24 21:57:40 -0500516 if handler.competeForTransaction() {
khenaidoo1ce37ad2019-03-24 22:07:24 -0400517 if txn, err := handler.takeRequestOwnership(ctx, &utils.DeviceID{Id:img.Id}); err != nil {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500518 return &common.OperationResp{}, err
519 } else {
520 defer txn.Close()
521 }
khenaidoof5a5bfa2019-01-23 22:20:29 -0500522 }
523
524 failedresponse := &common.OperationResp{Code:voltha.OperationResp_OPERATION_FAILURE}
525
526 ch := make(chan interface{})
527 defer close(ch)
528 switch requestType {
529 case IMAGE_DOWNLOAD:
530 go handler.deviceMgr.downloadImage(ctx, img, ch)
531 case CANCEL_IMAGE_DOWNLOAD:
532 go handler.deviceMgr.cancelImageDownload(ctx, img, ch)
533 case ACTIVATE_IMAGE:
534 go handler.deviceMgr.activateImage(ctx, img, ch)
535 case REVERT_IMAGE:
536 go handler.deviceMgr.revertImage(ctx, img, ch)
537 default:
538 log.Warn("invalid-request-type", log.Fields{"requestType": requestType})
539 return failedresponse, status.Errorf(codes.InvalidArgument, "%d", requestType)
540 }
541 select {
542 case res := <-ch:
543 if res != nil {
544 if err, ok := res.(error); ok {
545 return failedresponse, err
546 }
547 if opResp, ok := res.(*common.OperationResp); ok {
548 return opResp, nil
549 }
550 }
551 log.Warnw("download-image-unexpected-return-type", log.Fields{"result": res})
552 return failedresponse, status.Errorf(codes.Internal, "%s", res)
553 case <-ctx.Done():
554 log.Debug("downloadImage-client-timeout")
555 return nil, ctx.Err()
556 }
557}
558
khenaidoobf6e7bb2018-08-14 22:27:29 -0400559func (handler *APIHandler) DownloadImage(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) {
560 log.Debugw("DownloadImage-request", log.Fields{"img": *img})
561 if isTestMode(ctx) {
562 resp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS}
563 return resp, nil
564 }
565
khenaidoof5a5bfa2019-01-23 22:20:29 -0500566 return handler.processImageRequest(ctx, img, IMAGE_DOWNLOAD)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400567}
568
569func (handler *APIHandler) CancelImageDownload(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) {
khenaidoof5a5bfa2019-01-23 22:20:29 -0500570 log.Debugw("cancelImageDownload-request", log.Fields{"img": *img})
khenaidoobf6e7bb2018-08-14 22:27:29 -0400571 if isTestMode(ctx) {
572 resp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS}
573 return resp, nil
574 }
khenaidoof5a5bfa2019-01-23 22:20:29 -0500575 return handler.processImageRequest(ctx, img, CANCEL_IMAGE_DOWNLOAD)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400576}
577
578func (handler *APIHandler) ActivateImageUpdate(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) {
khenaidoof5a5bfa2019-01-23 22:20:29 -0500579 log.Debugw("activateImageUpdate-request", log.Fields{"img": *img})
khenaidoobf6e7bb2018-08-14 22:27:29 -0400580 if isTestMode(ctx) {
581 resp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS}
582 return resp, nil
583 }
khenaidoof5a5bfa2019-01-23 22:20:29 -0500584
585 return handler.processImageRequest(ctx, img, ACTIVATE_IMAGE)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400586}
587
588func (handler *APIHandler) RevertImageUpdate(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) {
khenaidoof5a5bfa2019-01-23 22:20:29 -0500589 log.Debugw("revertImageUpdate-request", log.Fields{"img": *img})
khenaidoobf6e7bb2018-08-14 22:27:29 -0400590 if isTestMode(ctx) {
591 resp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS}
592 return resp, nil
593 }
khenaidoof5a5bfa2019-01-23 22:20:29 -0500594
595 return handler.processImageRequest(ctx, img, REVERT_IMAGE)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400596}
597
khenaidoof5a5bfa2019-01-23 22:20:29 -0500598func (handler *APIHandler) GetImageDownloadStatus(ctx context.Context, img *voltha.ImageDownload) (*voltha.ImageDownload, error) {
599 log.Debugw("getImageDownloadStatus-request", log.Fields{"img": *img})
600 if isTestMode(ctx) {
Stephane Barbariedf5479f2019-01-29 22:13:00 -0500601 resp := &voltha.ImageDownload{DownloadState: voltha.ImageDownload_DOWNLOAD_SUCCEEDED}
khenaidoof5a5bfa2019-01-23 22:20:29 -0500602 return resp, nil
603 }
604
Stephane Barbariedf5479f2019-01-29 22:13:00 -0500605 failedresponse := &voltha.ImageDownload{DownloadState: voltha.ImageDownload_DOWNLOAD_UNKNOWN}
khenaidoof5a5bfa2019-01-23 22:20:29 -0500606
khenaidoo9cdc1a62019-01-24 21:57:40 -0500607 if handler.competeForTransaction() {
khenaidoo1ce37ad2019-03-24 22:07:24 -0400608 if txn, err := handler.takeRequestOwnership(ctx, &utils.DeviceID{Id:img.Id}); err != nil {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500609 return failedresponse, err
610 } else {
611 defer txn.Close()
612 }
khenaidoof5a5bfa2019-01-23 22:20:29 -0500613 }
614
615 ch := make(chan interface{})
616 defer close(ch)
617 go handler.deviceMgr.getImageDownloadStatus(ctx, img, ch)
618
619 select {
620 case res := <-ch:
621 if res != nil {
622 if err, ok := res.(error); ok {
623 return failedresponse, err
624 }
625 if downloadResp, ok := res.(*voltha.ImageDownload); ok {
626 return downloadResp, nil
627 }
628 }
629 log.Warnw("download-image-status", log.Fields{"result": res})
630 return failedresponse, status.Errorf(codes.Internal, "%s", res)
631 case <-ctx.Done():
632 log.Debug("downloadImage-client-timeout")
633 return failedresponse, ctx.Err()
634 }
635}
636
637func (handler *APIHandler) GetImageDownload(ctx context.Context, img *voltha.ImageDownload) (*voltha.ImageDownload, error) {
638 log.Debugw("GetImageDownload-request", log.Fields{"img": *img})
639 if isTestMode(ctx) {
Stephane Barbariedf5479f2019-01-29 22:13:00 -0500640 resp := &voltha.ImageDownload{DownloadState: voltha.ImageDownload_DOWNLOAD_SUCCEEDED}
khenaidoof5a5bfa2019-01-23 22:20:29 -0500641 return resp, nil
642 }
643
644 if download, err := handler.deviceMgr.getImageDownload(ctx, img); err != nil {
Stephane Barbariedf5479f2019-01-29 22:13:00 -0500645 return &voltha.ImageDownload{DownloadState: voltha.ImageDownload_DOWNLOAD_UNKNOWN}, err
khenaidoof5a5bfa2019-01-23 22:20:29 -0500646 } else {
647 return download, nil
648 }
649}
650
651func (handler *APIHandler) ListImageDownloads(ctx context.Context, id *voltha.ID) (*voltha.ImageDownloads, error) {
652 log.Debugw("ListImageDownloads-request", log.Fields{"deviceId": id.Id})
653 if isTestMode(ctx) {
654 resp := &voltha.ImageDownloads{Items:[]*voltha.ImageDownload{}}
655 return resp, nil
656 }
657
658 if downloads, err := handler.deviceMgr.listImageDownloads(ctx, id.Id); err != nil {
659 failedResp := &voltha.ImageDownloads{
660 Items:[]*voltha.ImageDownload{
Stephane Barbariedf5479f2019-01-29 22:13:00 -0500661 &voltha.ImageDownload{DownloadState: voltha.ImageDownload_DOWNLOAD_UNKNOWN},
khenaidoof5a5bfa2019-01-23 22:20:29 -0500662 },
663 }
664 return failedResp, err
665 } else {
666 return downloads, nil
667 }
668}
669
670
khenaidoobf6e7bb2018-08-14 22:27:29 -0400671func (handler *APIHandler) UpdateDevicePmConfigs(ctx context.Context, configs *voltha.PmConfigs) (*empty.Empty, error) {
672 log.Debugw("UpdateDevicePmConfigs-request", log.Fields{"configs": *configs})
673 if isTestMode(ctx) {
674 out := new(empty.Empty)
675 return out, nil
676 }
677 return nil, errors.New("UnImplemented")
678}
679
680func (handler *APIHandler) CreateAlarmFilter(ctx context.Context, filter *voltha.AlarmFilter) (*voltha.AlarmFilter, error) {
681 log.Debugw("CreateAlarmFilter-request", log.Fields{"filter": *filter})
682 if isTestMode(ctx) {
683 f := &voltha.AlarmFilter{Id: filter.Id}
684 return f, nil
685 }
686 return nil, errors.New("UnImplemented")
687}
688
689func (handler *APIHandler) UpdateAlarmFilter(ctx context.Context, filter *voltha.AlarmFilter) (*voltha.AlarmFilter, error) {
690 log.Debugw("UpdateAlarmFilter-request", log.Fields{"filter": *filter})
691 if isTestMode(ctx) {
692 f := &voltha.AlarmFilter{Id: filter.Id}
693 return f, nil
694 }
695 return nil, errors.New("UnImplemented")
696}
697
698func (handler *APIHandler) DeleteAlarmFilter(ctx context.Context, id *voltha.ID) (*empty.Empty, error) {
699 log.Debugw("DeleteAlarmFilter-request", log.Fields{"id": *id})
700 if isTestMode(ctx) {
701 out := new(empty.Empty)
702 return out, nil
703 }
704 return nil, errors.New("UnImplemented")
705}
706
707func (handler *APIHandler) SelfTest(ctx context.Context, id *voltha.ID) (*voltha.SelfTestResponse, error) {
708 log.Debugw("SelfTest-request", log.Fields{"id": id})
709 if isTestMode(ctx) {
710 resp := &voltha.SelfTestResponse{Result: voltha.SelfTestResponse_SUCCESS}
711 return resp, nil
712 }
713 return nil, errors.New("UnImplemented")
714}
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500715
716func (handler *APIHandler) forwardPacketOut(packet *openflow_13.PacketOut) {
717 log.Debugw("forwardPacketOut-request", log.Fields{"packet": packet})
Richard Jankowski2755adf2019-01-17 17:16:48 -0500718 agent := handler.logicalDeviceMgr.getLogicalDeviceAgent(packet.Id)
719 agent.packetOut(packet.PacketOut)
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500720}
721func (handler *APIHandler) StreamPacketsOut(
722 packets voltha.VolthaService_StreamPacketsOutServer,
723) error {
724 log.Debugw("StreamPacketsOut-request", log.Fields{"packets": packets})
725 for {
726 packet, err := packets.Recv()
727
728 if err == io.EOF {
729 break
730 } else if err != nil {
731 log.Errorw("Failed to receive packet", log.Fields{"error": err})
732 }
733
734 handler.forwardPacketOut(packet)
735 }
736
737 log.Debugw("StreamPacketsOut-request-done", log.Fields{"packets": packets})
738 return nil
739}
740
khenaidoo297cd252019-02-07 22:10:23 -0500741func (handler *APIHandler) sendPacketIn(deviceId string, transationId string, packet *openflow_13.OfpPacketIn) {
742 // TODO: Augment the OF PacketIn to include the transactionId
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500743 packetIn := openflow_13.PacketIn{Id: deviceId, PacketIn: packet}
744 log.Debugw("sendPacketIn", log.Fields{"packetIn": packetIn})
Richard Jankowskidbab94a2018-12-06 16:20:25 -0500745 // Enqueue the packet
746 if err := handler.packetInQueue.Put(packetIn); err != nil {
747 log.Errorw("failed-to-enqueue-packet", log.Fields{"error": err})
748 }
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500749}
750
751func (handler *APIHandler) ReceivePacketsIn(
752 empty *empty.Empty,
753 packetsIn voltha.VolthaService_ReceivePacketsInServer,
754) error {
755 log.Debugw("ReceivePacketsIn-request", log.Fields{"packetsIn": packetsIn})
756
757 for {
Richard Jankowskidbab94a2018-12-06 16:20:25 -0500758 // Dequeue a packet
759 if packets, err := handler.packetInQueue.Get(1); err == nil {
760 log.Debugw("dequeued-packet", log.Fields{"packet": packets[0]})
761 if packet, ok := packets[0].(openflow_13.PacketIn); ok {
762 log.Debugw("sending-packet-in", log.Fields{"packet": packet})
763 if err := packetsIn.Send(&packet); err != nil {
764 log.Errorw("failed-to-send-packet", log.Fields{"error": err})
765 }
766 }
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500767 }
768 }
khenaidoof5a5bfa2019-01-23 22:20:29 -0500769 //TODO: FInd an elegant way to get out of the above loop when the Core is stopped
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500770}
771
772func (handler *APIHandler) sendChangeEvent(deviceId string, portStatus *openflow_13.OfpPortStatus) {
773 // TODO: validate the type of portStatus parameter
774 //if _, ok := portStatus.(*openflow_13.OfpPortStatus); ok {
775 //}
776 event := openflow_13.ChangeEvent{Id: deviceId, Event: &openflow_13.ChangeEvent_PortStatus{PortStatus: portStatus}}
777 log.Debugw("sendChangeEvent", log.Fields{"event": event})
Richard Jankowski199fd862019-03-18 14:49:51 -0400778 // Enqueue the change event
779 if err := handler.changeEventQueue.Put(event); err != nil {
780 log.Errorw("failed-to-enqueue-change-event", log.Fields{"error": err})
781 }
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500782}
783
784func (handler *APIHandler) ReceiveChangeEvents(
785 empty *empty.Empty,
786 changeEvents voltha.VolthaService_ReceiveChangeEventsServer,
787) error {
788 log.Debugw("ReceiveChangeEvents-request", log.Fields{"changeEvents": changeEvents})
789 for {
Richard Jankowski199fd862019-03-18 14:49:51 -0400790 // Dequeue a change event
791 if events, err := handler.changeEventQueue.Get(1); err == nil {
792 log.Debugw("dequeued-change-event", log.Fields{"event": events[0]})
793 if event, ok := events[0].(openflow_13.ChangeEvent); ok {
794 log.Debugw("sending-change-event", log.Fields{"event": event})
795 if err := changeEvents.Send(&event); err != nil {
796 log.Errorw("failed-to-send-change-event", log.Fields{"error": err})
797 }
798 }
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500799 }
800 }
Richard Jankowski199fd862019-03-18 14:49:51 -0400801}
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500802
803func (handler *APIHandler) Subscribe(
804 ctx context.Context,
805 ofAgent *voltha.OfAgentSubscriber,
806) (*voltha.OfAgentSubscriber, error) {
807 log.Debugw("Subscribe-request", log.Fields{"ofAgent": ofAgent})
808 return &voltha.OfAgentSubscriber{OfagentId: ofAgent.OfagentId, VolthaId: ofAgent.VolthaId}, nil
809}
William Kurkiandaa6bb22019-03-07 12:26:28 -0500810
811//@TODO useless stub, what should this actually do?
812func (handler *APIHandler) GetAlarmDeviceData(
813 ctx context.Context,
814 in *common.ID,
815) (*omci.AlarmDeviceData, error) {
816 log.Debug("GetAlarmDeviceData-stub")
817 return nil, nil
818}
819
820//@TODO useless stub, what should this actually do?
821func (handler *APIHandler) GetMeterStatsOfLogicalDevice(
822 ctx context.Context,
823 in *common.ID,
824) (*openflow_13.MeterStatsReply, error) {
825 log.Debug("GetMeterStatsOfLogicalDevice-stub")
826 return nil, nil
827}
828
829//@TODO useless stub, what should this actually do?
830func (handler *APIHandler) GetMibDeviceData(
831 ctx context.Context,
832 in *common.ID,
833) (*omci.MibDeviceData, error) {
834 log.Debug("GetMibDeviceData-stub")
835 return nil, nil
836}
837
838//@TODO useless stub, what should this actually do?
839func (handler *APIHandler) SimulateAlarm(
840 ctx context.Context,
841 in *voltha.SimulateAlarmRequest,
842) (*common.OperationResp, error) {
843 log.Debug("SimulateAlarm-stub")
844 return nil, nil
845}
846
847//@TODO useless stub, what should this actually do?
848func (handler *APIHandler) UpdateLogicalDeviceMeterTable(
849 ctx context.Context,
850 in *openflow_13.MeterModUpdate,
851) (*empty.Empty, error) {
852 log.Debug("UpdateLogicalDeviceMeterTable-stub")
853 return nil, nil
854}