blob: 0f97cd3f4ab0002c0f3fe0efebe68647a22e7d6b [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"
William Kurkiandaa6bb22019-03-07 12:26:28 -050027 "github.com/opencord/voltha-protos/go/omci"
khenaidoo2c6a0992019-04-29 13:46:56 -040028 "github.com/opencord/voltha-protos/go/openflow_13"
William Kurkiandaa6bb22019-03-07 12:26:28 -050029 "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 (
khenaidoo2c6a0992019-04-29 13:46:56 -040038 IMAGE_DOWNLOAD = iota
39 CANCEL_IMAGE_DOWNLOAD = iota
40 ACTIVATE_IMAGE = iota
41 REVERT_IMAGE = iota
khenaidoof5a5bfa2019-01-23 22:20:29 -050042)
43
khenaidoobf6e7bb2018-08-14 22:27:29 -040044type APIHandler struct {
khenaidoo2c6a0992019-04-29 13:46:56 -040045 deviceMgr *DeviceManager
46 logicalDeviceMgr *LogicalDeviceManager
47 adapterMgr *AdapterManager
48 packetInQueue *queue.Queue
49 changeEventQueue *queue.Queue
50 coreInCompetingMode bool
khenaidoob6080322019-01-29 21:47:38 -050051 longRunningRequestTimeout int64
khenaidoo2c6a0992019-04-29 13:46:56 -040052 defaultRequestTimeout int64
khenaidoobf6e7bb2018-08-14 22:27:29 -040053 da.DefaultAPIHandler
khenaidoo54e0ddf2019-02-27 16:21:33 -050054 core *Core
khenaidoobf6e7bb2018-08-14 22:27:29 -040055}
56
khenaidoo54e0ddf2019-02-27 16:21:33 -050057func NewAPIHandler(core *Core) *APIHandler {
Stephane Barbarie6e1bd502018-11-05 22:44:45 -050058 handler := &APIHandler{
khenaidoo2c6a0992019-04-29 13:46:56 -040059 deviceMgr: core.deviceMgr,
60 logicalDeviceMgr: core.logicalDeviceMgr,
61 adapterMgr: core.adapterMgr,
62 coreInCompetingMode: core.config.InCompetingMode,
63 longRunningRequestTimeout: core.config.LongRunningRequestTimeout,
64 defaultRequestTimeout: core.config.DefaultRequestTimeout,
Richard Jankowskidbab94a2018-12-06 16:20:25 -050065 // TODO: Figure out what the 'hint' parameter to queue.New does
khenaidoo2c6a0992019-04-29 13:46:56 -040066 packetInQueue: queue.New(10),
Richard Jankowski199fd862019-03-18 14:49:51 -040067 changeEventQueue: queue.New(10),
khenaidoo2c6a0992019-04-29 13:46:56 -040068 core: core,
Stephane Barbarie6e1bd502018-11-05 22:44:45 -050069 }
khenaidoobf6e7bb2018-08-14 22:27:29 -040070 return handler
71}
khenaidoo4d4802d2018-10-04 21:59:49 -040072
73// isTestMode is a helper function to determine a function is invoked for testing only
khenaidoobf6e7bb2018-08-14 22:27:29 -040074func isTestMode(ctx context.Context) bool {
75 md, _ := metadata.FromIncomingContext(ctx)
76 _, exist := md[common.TestModeKeys_api_test.String()]
77 return exist
78}
79
Richard Jankowskid42826e2018-11-02 16:06:37 -040080// This function attempts to extract the serial number from the request metadata
81// and create a KV transaction for that serial number for the current core.
82func (handler *APIHandler) createKvTransaction(ctx context.Context) (*KVTransaction, error) {
83 var (
khenaidoo43c82122018-11-22 18:38:28 -050084 err error
85 ok bool
86 md metadata.MD
Richard Jankowskid42826e2018-11-02 16:06:37 -040087 serNum []string
88 )
89 if md, ok = metadata.FromIncomingContext(ctx); !ok {
90 err = errors.New("metadata-not-found")
91 } else if serNum, ok = md["voltha_serial_number"]; !ok {
92 err = errors.New("serial-number-not-found")
93 }
khenaidoo2c6a0992019-04-29 13:46:56 -040094 if !ok || serNum == nil {
Richard Jankowskid42826e2018-11-02 16:06:37 -040095 log.Error(err)
96 return nil, err
97 }
98 // Create KV transaction
99 txn := NewKVTransaction(serNum[0])
100 return txn, nil
101}
102
Richard Jankowski2755adf2019-01-17 17:16:48 -0500103// isOFControllerRequest is a helper function to determine if a request was initiated
104// from the OpenFlow controller (or its proxy, e.g. OFAgent)
Richard Jankowski46464e92019-03-05 11:53:55 -0500105func (handler *APIHandler) isOFControllerRequest(ctx context.Context) bool {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500106 if md, ok := metadata.FromIncomingContext(ctx); ok {
107 // Metadata in context
Richard Jankowski46464e92019-03-05 11:53:55 -0500108 if _, ok = md[handler.core.config.CoreBindingKey]; ok {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500109 // OFAgent field in metadata
110 return true
111 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500112 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500113 return false
114}
115
116// competeForTransaction is a helper function to determine whether every request needs to compete with another
117// Core to execute the request
118func (handler *APIHandler) competeForTransaction() bool {
119 return handler.coreInCompetingMode
120}
121
Richard Jankowski199fd862019-03-18 14:49:51 -0400122// This function handles the creation of new devices
123func (handler *APIHandler) acquireRequest(ctx context.Context, id interface{}, maxTimeout ...int64) (*KVTransaction, error) {
khenaidoob6080322019-01-29 21:47:38 -0500124 timeout := handler.defaultRequestTimeout
khenaidoo9cdc1a62019-01-24 21:57:40 -0500125 if len(maxTimeout) > 0 {
126 timeout = maxTimeout[0]
Richard Jankowski2755adf2019-01-17 17:16:48 -0500127 }
khenaidoob6080322019-01-29 21:47:38 -0500128 log.Debugw("transaction-timeout", log.Fields{"timeout": timeout})
khenaidoo9cdc1a62019-01-24 21:57:40 -0500129 txn, err := handler.createKvTransaction(ctx)
130 if txn == nil {
khenaidoo2c6a0992019-04-29 13:46:56 -0400131 return nil, err
khenaidoo9cdc1a62019-01-24 21:57:40 -0500132 } else if txn.Acquired(timeout) {
133 return txn, nil
134 } else {
khenaidoo297cd252019-02-07 22:10:23 -0500135 if id != nil {
136 // The id can either be a device Id or a logical device id.
khenaidoo1ce37ad2019-03-24 22:07:24 -0400137 if dId, ok := id.(*utils.DeviceID); ok {
khenaidoo297cd252019-02-07 22:10:23 -0500138 // Since this core has not processed this request, let's load the device, along with its extended
139 // family (parents and children) in memory. This will keep this core in-sync with its paired core as
140 // much as possible. The watch feature in the core model will ensure that the contents of those objects in
141 // memory are in sync.
142 time.Sleep(2 * time.Second)
khenaidoo1ce37ad2019-03-24 22:07:24 -0400143 go handler.deviceMgr.load(dId.Id)
144 } else if ldId, ok := id.(*utils.LogicalDeviceID); ok {
khenaidoo297cd252019-02-07 22:10:23 -0500145 // This will load the logical device along with its children and grandchildren
khenaidoo1ce37ad2019-03-24 22:07:24 -0400146 go handler.logicalDeviceMgr.load(ldId.Id)
khenaidoo297cd252019-02-07 22:10:23 -0500147 }
148 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500149 return nil, errors.New("failed-to-seize-request")
Richard Jankowski2755adf2019-01-17 17:16:48 -0500150 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500151}
152
Richard Jankowski199fd862019-03-18 14:49:51 -0400153// This function handles the modification or deletion of existing devices
154func (handler *APIHandler) takeRequestOwnership(ctx context.Context, id interface{}, maxTimeout ...int64) (*KVTransaction, error) {
155 timeout := handler.defaultRequestTimeout
156 if len(maxTimeout) > 0 {
157 timeout = maxTimeout[0]
158 }
159 log.Debugw("transaction-timeout", log.Fields{"timeout": timeout})
160 txn, err := handler.createKvTransaction(ctx)
161 if txn == nil {
khenaidoo2c6a0992019-04-29 13:46:56 -0400162 return nil, err
Richard Jankowski199fd862019-03-18 14:49:51 -0400163 }
164
165 owned := false
166 if id != nil {
khenaidoo1ce37ad2019-03-24 22:07:24 -0400167 owned = handler.core.deviceOwnership.OwnedByMe(id)
Richard Jankowski199fd862019-03-18 14:49:51 -0400168 }
169 if owned {
170 if txn.Acquired(timeout) {
171 return txn, nil
172 } else {
173 return nil, errors.New("failed-to-seize-request")
174 }
175 } else {
176 if txn.Monitor(timeout) {
177 return txn, nil
178 } else {
179 return nil, errors.New("device-not-owned")
180 }
181 }
182}
183
khenaidoo4d4802d2018-10-04 21:59:49 -0400184// waitForNilResponseOnSuccess is a helper function to wait for a response on channel ch where an nil
185// response is expected in a successful scenario
186func waitForNilResponseOnSuccess(ctx context.Context, ch chan interface{}) (*empty.Empty, error) {
187 select {
188 case res := <-ch:
189 if res == nil {
190 return new(empty.Empty), nil
191 } else if err, ok := res.(error); ok {
192 return new(empty.Empty), err
193 } else {
194 log.Warnw("unexpected-return-type", log.Fields{"result": res})
195 err = status.Errorf(codes.Internal, "%s", res)
196 return new(empty.Empty), err
197 }
198 case <-ctx.Done():
199 log.Debug("client-timeout")
200 return nil, ctx.Err()
201 }
202}
203
khenaidoobf6e7bb2018-08-14 22:27:29 -0400204func (handler *APIHandler) UpdateLogLevel(ctx context.Context, logging *voltha.Logging) (*empty.Empty, error) {
khenaidoo6f2fbe32019-01-18 16:16:50 -0500205 log.Debugw("UpdateLogLevel-request", log.Fields{"package": logging.PackageName, "intval": int(logging.Level)})
khenaidoo92e62c52018-10-03 14:02:54 -0400206 out := new(empty.Empty)
khenaidoo6f2fbe32019-01-18 16:16:50 -0500207 if logging.PackageName == "" {
208 log.SetAllLogLevel(int(logging.Level))
209 } else {
210 log.SetPackageLogLevel(logging.PackageName, int(logging.Level))
211 }
khenaidoo92e62c52018-10-03 14:02:54 -0400212 return out, nil
khenaidoobf6e7bb2018-08-14 22:27:29 -0400213}
214
khenaidoo54e0ddf2019-02-27 16:21:33 -0500215func (handler *APIHandler) UpdateMembership(ctx context.Context, membership *voltha.Membership) (*empty.Empty, error) {
216 log.Debugw("UpdateMembership-request", log.Fields{"membership": membership})
217 out := new(empty.Empty)
khenaidoo6417b6c2019-03-01 18:18:01 -0500218 if err := handler.core.updateCoreMembership(ctx, membership); err != nil {
khenaidoo54e0ddf2019-02-27 16:21:33 -0500219 return out, err
220 }
221 return out, nil
222}
223
khenaidoo6417b6c2019-03-01 18:18:01 -0500224func (handler *APIHandler) GetMembership(ctx context.Context, empty *empty.Empty) (*voltha.Membership, error) {
225 log.Debug("GetMembership-request")
226 if membership := handler.core.getCoreMembership(ctx); membership != nil {
227 return membership, nil
228 }
229 return &voltha.Membership{}, nil
230}
231
khenaidoobf6e7bb2018-08-14 22:27:29 -0400232func (handler *APIHandler) EnableLogicalDevicePort(ctx context.Context, id *voltha.LogicalPortId) (*empty.Empty, error) {
233 log.Debugw("EnableLogicalDevicePort-request", log.Fields{"id": id, "test": common.TestModeKeys_api_test.String()})
234 if isTestMode(ctx) {
235 out := new(empty.Empty)
236 return out, nil
237 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500238
khenaidoo9cdc1a62019-01-24 21:57:40 -0500239 if handler.competeForTransaction() {
khenaidoo2c6a0992019-04-29 13:46:56 -0400240 if txn, err := handler.takeRequestOwnership(ctx, &utils.LogicalDeviceID{Id: id.Id}); err != nil {
Richard Jankowski2755adf2019-01-17 17:16:48 -0500241 return new(empty.Empty), err
Richard Jankowski2755adf2019-01-17 17:16:48 -0500242 } else {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500243 defer txn.Close()
Richard Jankowski2755adf2019-01-17 17:16:48 -0500244 }
245 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500246
khenaidoo4d4802d2018-10-04 21:59:49 -0400247 ch := make(chan interface{})
248 defer close(ch)
khenaidoo19d7b632018-10-30 10:49:50 -0400249 go handler.logicalDeviceMgr.enableLogicalPort(ctx, id, ch)
khenaidoo4d4802d2018-10-04 21:59:49 -0400250 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400251}
252
253func (handler *APIHandler) DisableLogicalDevicePort(ctx context.Context, id *voltha.LogicalPortId) (*empty.Empty, error) {
254 log.Debugw("DisableLogicalDevicePort-request", log.Fields{"id": id, "test": common.TestModeKeys_api_test.String()})
255 if isTestMode(ctx) {
256 out := new(empty.Empty)
257 return out, nil
258 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500259
khenaidoo9cdc1a62019-01-24 21:57:40 -0500260 if handler.competeForTransaction() {
khenaidoo2c6a0992019-04-29 13:46:56 -0400261 if txn, err := handler.takeRequestOwnership(ctx, &utils.LogicalDeviceID{Id: id.Id}); err != nil {
Richard Jankowski2755adf2019-01-17 17:16:48 -0500262 return new(empty.Empty), err
Richard Jankowski2755adf2019-01-17 17:16:48 -0500263 } else {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500264 defer txn.Close()
Richard Jankowski2755adf2019-01-17 17:16:48 -0500265 }
266 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500267
khenaidoo19d7b632018-10-30 10:49:50 -0400268 ch := make(chan interface{})
269 defer close(ch)
270 go handler.logicalDeviceMgr.disableLogicalPort(ctx, id, ch)
271 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400272}
273
274func (handler *APIHandler) UpdateLogicalDeviceFlowTable(ctx context.Context, flow *openflow_13.FlowTableUpdate) (*empty.Empty, error) {
275 log.Debugw("UpdateLogicalDeviceFlowTable-request", log.Fields{"flow": flow, "test": common.TestModeKeys_api_test.String()})
276 if isTestMode(ctx) {
277 out := new(empty.Empty)
278 return out, nil
279 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500280
khenaidoo9cdc1a62019-01-24 21:57:40 -0500281 if handler.competeForTransaction() {
Richard Jankowski46464e92019-03-05 11:53:55 -0500282 if !handler.isOFControllerRequest(ctx) { // No need to acquire the transaction as request is sent to one core only
khenaidoo2c6a0992019-04-29 13:46:56 -0400283 if txn, err := handler.takeRequestOwnership(ctx, &utils.LogicalDeviceID{Id: flow.Id}); err != nil {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500284 return new(empty.Empty), err
285 } else {
286 defer txn.Close()
287 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500288 }
289 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500290
khenaidoo19d7b632018-10-30 10:49:50 -0400291 ch := make(chan interface{})
292 defer close(ch)
293 go handler.logicalDeviceMgr.updateFlowTable(ctx, flow.Id, flow.FlowMod, ch)
294 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400295}
296
297func (handler *APIHandler) UpdateLogicalDeviceFlowGroupTable(ctx context.Context, flow *openflow_13.FlowGroupTableUpdate) (*empty.Empty, error) {
298 log.Debugw("UpdateLogicalDeviceFlowGroupTable-request", log.Fields{"flow": flow, "test": common.TestModeKeys_api_test.String()})
299 if isTestMode(ctx) {
300 out := new(empty.Empty)
301 return out, nil
302 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500303
khenaidoo9cdc1a62019-01-24 21:57:40 -0500304 if handler.competeForTransaction() {
Richard Jankowski46464e92019-03-05 11:53:55 -0500305 if !handler.isOFControllerRequest(ctx) { // No need to acquire the transaction as request is sent to one core only
khenaidoo2c6a0992019-04-29 13:46:56 -0400306 if txn, err := handler.takeRequestOwnership(ctx, &utils.LogicalDeviceID{Id: flow.Id}); err != nil {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500307 return new(empty.Empty), err
308 } else {
309 defer txn.Close()
310 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500311 }
312 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500313
khenaidoo19d7b632018-10-30 10:49:50 -0400314 ch := make(chan interface{})
315 defer close(ch)
316 go handler.logicalDeviceMgr.updateGroupTable(ctx, flow.Id, flow.GroupMod, ch)
317 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400318}
319
khenaidoob9203542018-09-17 22:56:37 -0400320// GetDevice must be implemented in the read-only containers - should it also be implemented here?
321func (handler *APIHandler) GetDevice(ctx context.Context, id *voltha.ID) (*voltha.Device, error) {
322 log.Debugw("GetDevice-request", log.Fields{"id": id})
khenaidoo19d7b632018-10-30 10:49:50 -0400323 return handler.deviceMgr.GetDevice(id.Id)
khenaidoob9203542018-09-17 22:56:37 -0400324}
325
326// GetDevice must be implemented in the read-only containers - should it also be implemented here?
327func (handler *APIHandler) ListDevices(ctx context.Context, empty *empty.Empty) (*voltha.Devices, error) {
328 log.Debug("ListDevices")
329 return handler.deviceMgr.ListDevices()
330}
331
khenaidoo7ccedd52018-12-14 16:48:54 -0500332// ListDeviceIds returns the list of device ids managed by a voltha core
333func (handler *APIHandler) ListDeviceIds(ctx context.Context, empty *empty.Empty) (*voltha.IDs, error) {
334 log.Debug("ListDeviceIDs")
335 if isTestMode(ctx) {
336 out := &voltha.IDs{Items: make([]*voltha.ID, 0)}
337 return out, nil
338 }
339 return handler.deviceMgr.ListDeviceIds()
340}
341
342//ReconcileDevices is a request to a voltha core to managed a list of devices based on their IDs
343func (handler *APIHandler) ReconcileDevices(ctx context.Context, ids *voltha.IDs) (*empty.Empty, error) {
344 log.Debug("ReconcileDevices")
345 if isTestMode(ctx) {
346 out := new(empty.Empty)
347 return out, nil
348 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500349
khenaidoo9cdc1a62019-01-24 21:57:40 -0500350 // No need to grab a transaction as this request is core specific
351
khenaidoo7ccedd52018-12-14 16:48:54 -0500352 ch := make(chan interface{})
353 defer close(ch)
354 go handler.deviceMgr.ReconcileDevices(ctx, ids, ch)
355 return waitForNilResponseOnSuccess(ctx, ch)
356}
357
khenaidoob9203542018-09-17 22:56:37 -0400358// GetLogicalDevice must be implemented in the read-only containers - should it also be implemented here?
359func (handler *APIHandler) GetLogicalDevice(ctx context.Context, id *voltha.ID) (*voltha.LogicalDevice, error) {
360 log.Debugw("GetLogicalDevice-request", log.Fields{"id": id})
361 return handler.logicalDeviceMgr.getLogicalDevice(id.Id)
362}
363
364// ListLogicalDevices must be implemented in the read-only containers - should it also be implemented here?
365func (handler *APIHandler) ListLogicalDevices(ctx context.Context, empty *empty.Empty) (*voltha.LogicalDevices, error) {
366 log.Debug("ListLogicalDevices")
367 return handler.logicalDeviceMgr.listLogicalDevices()
368}
369
khenaidoo21d51152019-02-01 13:48:37 -0500370// ListAdapters returns the contents of all adapters known to the system
371func (handler *APIHandler) ListAdapters(ctx context.Context, empty *empty.Empty) (*voltha.Adapters, error) {
372 log.Debug("ListDevices")
373 return handler.adapterMgr.listAdapters(ctx)
374}
375
khenaidoo19d7b632018-10-30 10:49:50 -0400376// ListLogicalDevicePorts must be implemented in the read-only containers - should it also be implemented here?
377func (handler *APIHandler) ListLogicalDevicePorts(ctx context.Context, id *voltha.ID) (*voltha.LogicalPorts, error) {
378 log.Debugw("ListLogicalDevicePorts", log.Fields{"logicaldeviceid": id})
379 return handler.logicalDeviceMgr.ListLogicalDevicePorts(ctx, id.Id)
380}
381
khenaidoo4d4802d2018-10-04 21:59:49 -0400382// CreateDevice creates a new parent device in the data model
khenaidoobf6e7bb2018-08-14 22:27:29 -0400383func (handler *APIHandler) CreateDevice(ctx context.Context, device *voltha.Device) (*voltha.Device, error) {
khenaidoob9203542018-09-17 22:56:37 -0400384 log.Debugw("createdevice", log.Fields{"device": *device})
khenaidoobf6e7bb2018-08-14 22:27:29 -0400385 if isTestMode(ctx) {
386 return &voltha.Device{Id: device.Id}, nil
387 }
Richard Jankowskid42826e2018-11-02 16:06:37 -0400388
khenaidoo9cdc1a62019-01-24 21:57:40 -0500389 if handler.competeForTransaction() {
Richard Jankowski199fd862019-03-18 14:49:51 -0400390 if txn, err := handler.acquireRequest(ctx, nil); err != nil {
Richard Jankowski2755adf2019-01-17 17:16:48 -0500391 return &voltha.Device{}, err
Richard Jankowski2755adf2019-01-17 17:16:48 -0500392 } else {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500393 defer txn.Close()
Richard Jankowski2755adf2019-01-17 17:16:48 -0500394 }
395 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500396
khenaidoob9203542018-09-17 22:56:37 -0400397 ch := make(chan interface{})
398 defer close(ch)
399 go handler.deviceMgr.createDevice(ctx, device, ch)
400 select {
401 case res := <-ch:
khenaidoo92e62c52018-10-03 14:02:54 -0400402 if res != nil {
403 if err, ok := res.(error); ok {
404 return &voltha.Device{}, err
405 }
406 if d, ok := res.(*voltha.Device); ok {
khenaidoo2c6a0992019-04-29 13:46:56 -0400407 handler.core.deviceOwnership.OwnedByMe(&utils.DeviceID{Id: d.Id})
khenaidoo92e62c52018-10-03 14:02:54 -0400408 return d, nil
409 }
khenaidoob9203542018-09-17 22:56:37 -0400410 }
khenaidoo92e62c52018-10-03 14:02:54 -0400411 log.Warnw("create-device-unexpected-return-type", log.Fields{"result": res})
412 err := status.Errorf(codes.Internal, "%s", res)
413 return &voltha.Device{}, err
khenaidoob9203542018-09-17 22:56:37 -0400414 case <-ctx.Done():
415 log.Debug("createdevice-client-timeout")
416 return nil, ctx.Err()
417 }
khenaidoobf6e7bb2018-08-14 22:27:29 -0400418}
419
khenaidoo4d4802d2018-10-04 21:59:49 -0400420// EnableDevice activates a device by invoking the adopt_device API on the appropriate adapter
khenaidoobf6e7bb2018-08-14 22:27:29 -0400421func (handler *APIHandler) EnableDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) {
khenaidoob9203542018-09-17 22:56:37 -0400422 log.Debugw("enabledevice", log.Fields{"id": id})
khenaidoobf6e7bb2018-08-14 22:27:29 -0400423 if isTestMode(ctx) {
khenaidoo4d4802d2018-10-04 21:59:49 -0400424 return new(empty.Empty), nil
khenaidoobf6e7bb2018-08-14 22:27:29 -0400425 }
Richard Jankowskid42826e2018-11-02 16:06:37 -0400426
khenaidoo9cdc1a62019-01-24 21:57:40 -0500427 if handler.competeForTransaction() {
khenaidoo2c6a0992019-04-29 13:46:56 -0400428 if txn, err := handler.takeRequestOwnership(ctx, &utils.DeviceID{Id: id.Id}, handler.longRunningRequestTimeout); err != nil {
Richard Jankowski2755adf2019-01-17 17:16:48 -0500429 return new(empty.Empty), err
Richard Jankowski2755adf2019-01-17 17:16:48 -0500430 } else {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500431 defer txn.Close()
Richard Jankowski2755adf2019-01-17 17:16:48 -0500432 }
433 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500434
khenaidoob9203542018-09-17 22:56:37 -0400435 ch := make(chan interface{})
436 defer close(ch)
437 go handler.deviceMgr.enableDevice(ctx, id, ch)
khenaidoo4d4802d2018-10-04 21:59:49 -0400438 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400439}
440
khenaidoo4d4802d2018-10-04 21:59:49 -0400441// DisableDevice disables a device along with any child device it may have
khenaidoobf6e7bb2018-08-14 22:27:29 -0400442func (handler *APIHandler) DisableDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) {
443 log.Debugw("disabledevice-request", log.Fields{"id": id})
444 if isTestMode(ctx) {
khenaidoo4d4802d2018-10-04 21:59:49 -0400445 return new(empty.Empty), nil
khenaidoobf6e7bb2018-08-14 22:27:29 -0400446 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500447
khenaidoo9cdc1a62019-01-24 21:57:40 -0500448 if handler.competeForTransaction() {
khenaidoo2c6a0992019-04-29 13:46:56 -0400449 if txn, err := handler.takeRequestOwnership(ctx, &utils.DeviceID{Id: id.Id}); err != nil {
Richard Jankowski2755adf2019-01-17 17:16:48 -0500450 return new(empty.Empty), err
Richard Jankowski2755adf2019-01-17 17:16:48 -0500451 } else {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500452 defer txn.Close()
Richard Jankowski2755adf2019-01-17 17:16:48 -0500453 }
454 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500455
khenaidoo92e62c52018-10-03 14:02:54 -0400456 ch := make(chan interface{})
457 defer close(ch)
458 go handler.deviceMgr.disableDevice(ctx, id, ch)
khenaidoo4d4802d2018-10-04 21:59:49 -0400459 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400460}
461
khenaidoo4d4802d2018-10-04 21:59:49 -0400462//RebootDevice invoked the reboot API to the corresponding adapter
khenaidoobf6e7bb2018-08-14 22:27:29 -0400463func (handler *APIHandler) RebootDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) {
khenaidoo4d4802d2018-10-04 21:59:49 -0400464 log.Debugw("rebootDevice-request", log.Fields{"id": id})
khenaidoobf6e7bb2018-08-14 22:27:29 -0400465 if isTestMode(ctx) {
khenaidoo4d4802d2018-10-04 21:59:49 -0400466 return new(empty.Empty), nil
khenaidoobf6e7bb2018-08-14 22:27:29 -0400467 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500468
khenaidoo9cdc1a62019-01-24 21:57:40 -0500469 if handler.competeForTransaction() {
khenaidoo2c6a0992019-04-29 13:46:56 -0400470 if txn, err := handler.takeRequestOwnership(ctx, &utils.DeviceID{Id: id.Id}); err != nil {
Richard Jankowski2755adf2019-01-17 17:16:48 -0500471 return new(empty.Empty), err
Richard Jankowski2755adf2019-01-17 17:16:48 -0500472 } else {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500473 defer txn.Close()
Richard Jankowski2755adf2019-01-17 17:16:48 -0500474 }
475 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500476
khenaidoo4d4802d2018-10-04 21:59:49 -0400477 ch := make(chan interface{})
478 defer close(ch)
479 go handler.deviceMgr.rebootDevice(ctx, id, ch)
480 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400481}
482
khenaidoo4d4802d2018-10-04 21:59:49 -0400483// DeleteDevice removes a device from the data model
khenaidoobf6e7bb2018-08-14 22:27:29 -0400484func (handler *APIHandler) DeleteDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) {
485 log.Debugw("deletedevice-request", log.Fields{"id": id})
486 if isTestMode(ctx) {
khenaidoo4d4802d2018-10-04 21:59:49 -0400487 return new(empty.Empty), nil
khenaidoobf6e7bb2018-08-14 22:27:29 -0400488 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500489
khenaidoo9cdc1a62019-01-24 21:57:40 -0500490 if handler.competeForTransaction() {
Richard Jankowski199fd862019-03-18 14:49:51 -0400491 if txn, err := handler.takeRequestOwnership(ctx, nil); err != nil {
Richard Jankowski2755adf2019-01-17 17:16:48 -0500492 return new(empty.Empty), err
Richard Jankowski2755adf2019-01-17 17:16:48 -0500493 } else {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500494 defer txn.Close()
Richard Jankowski2755adf2019-01-17 17:16:48 -0500495 }
496 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500497
khenaidoo4d4802d2018-10-04 21:59:49 -0400498 ch := make(chan interface{})
499 defer close(ch)
500 go handler.deviceMgr.deleteDevice(ctx, id, ch)
501 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400502}
503
khenaidoof5a5bfa2019-01-23 22:20:29 -0500504// processImageRequest is a helper method to execute an image download request
505func (handler *APIHandler) processImageRequest(ctx context.Context, img *voltha.ImageDownload, requestType int) (*common.OperationResp, error) {
506 log.Debugw("processImageDownload", log.Fields{"img": *img, "requestType": requestType})
507 if isTestMode(ctx) {
508 resp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS}
509 return resp, nil
510 }
511
khenaidoo9cdc1a62019-01-24 21:57:40 -0500512 if handler.competeForTransaction() {
khenaidoo2c6a0992019-04-29 13:46:56 -0400513 if txn, err := handler.takeRequestOwnership(ctx, &utils.DeviceID{Id: img.Id}); err != nil {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500514 return &common.OperationResp{}, err
515 } else {
516 defer txn.Close()
517 }
khenaidoof5a5bfa2019-01-23 22:20:29 -0500518 }
519
khenaidoo2c6a0992019-04-29 13:46:56 -0400520 failedresponse := &common.OperationResp{Code: voltha.OperationResp_OPERATION_FAILURE}
khenaidoof5a5bfa2019-01-23 22:20:29 -0500521
522 ch := make(chan interface{})
523 defer close(ch)
524 switch requestType {
525 case IMAGE_DOWNLOAD:
526 go handler.deviceMgr.downloadImage(ctx, img, ch)
527 case CANCEL_IMAGE_DOWNLOAD:
528 go handler.deviceMgr.cancelImageDownload(ctx, img, ch)
529 case ACTIVATE_IMAGE:
530 go handler.deviceMgr.activateImage(ctx, img, ch)
531 case REVERT_IMAGE:
532 go handler.deviceMgr.revertImage(ctx, img, ch)
533 default:
534 log.Warn("invalid-request-type", log.Fields{"requestType": requestType})
535 return failedresponse, status.Errorf(codes.InvalidArgument, "%d", requestType)
536 }
537 select {
538 case res := <-ch:
539 if res != nil {
540 if err, ok := res.(error); ok {
541 return failedresponse, err
542 }
543 if opResp, ok := res.(*common.OperationResp); ok {
544 return opResp, nil
545 }
546 }
547 log.Warnw("download-image-unexpected-return-type", log.Fields{"result": res})
548 return failedresponse, status.Errorf(codes.Internal, "%s", res)
549 case <-ctx.Done():
550 log.Debug("downloadImage-client-timeout")
551 return nil, ctx.Err()
552 }
553}
554
khenaidoobf6e7bb2018-08-14 22:27:29 -0400555func (handler *APIHandler) DownloadImage(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) {
556 log.Debugw("DownloadImage-request", log.Fields{"img": *img})
557 if isTestMode(ctx) {
558 resp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS}
559 return resp, nil
560 }
561
khenaidoof5a5bfa2019-01-23 22:20:29 -0500562 return handler.processImageRequest(ctx, img, IMAGE_DOWNLOAD)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400563}
564
565func (handler *APIHandler) CancelImageDownload(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) {
khenaidoof5a5bfa2019-01-23 22:20:29 -0500566 log.Debugw("cancelImageDownload-request", log.Fields{"img": *img})
khenaidoobf6e7bb2018-08-14 22:27:29 -0400567 if isTestMode(ctx) {
568 resp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS}
569 return resp, nil
570 }
khenaidoof5a5bfa2019-01-23 22:20:29 -0500571 return handler.processImageRequest(ctx, img, CANCEL_IMAGE_DOWNLOAD)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400572}
573
574func (handler *APIHandler) ActivateImageUpdate(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) {
khenaidoof5a5bfa2019-01-23 22:20:29 -0500575 log.Debugw("activateImageUpdate-request", log.Fields{"img": *img})
khenaidoobf6e7bb2018-08-14 22:27:29 -0400576 if isTestMode(ctx) {
577 resp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS}
578 return resp, nil
579 }
khenaidoof5a5bfa2019-01-23 22:20:29 -0500580
581 return handler.processImageRequest(ctx, img, ACTIVATE_IMAGE)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400582}
583
584func (handler *APIHandler) RevertImageUpdate(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) {
khenaidoof5a5bfa2019-01-23 22:20:29 -0500585 log.Debugw("revertImageUpdate-request", log.Fields{"img": *img})
khenaidoobf6e7bb2018-08-14 22:27:29 -0400586 if isTestMode(ctx) {
587 resp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS}
588 return resp, nil
589 }
khenaidoof5a5bfa2019-01-23 22:20:29 -0500590
591 return handler.processImageRequest(ctx, img, REVERT_IMAGE)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400592}
593
khenaidoof5a5bfa2019-01-23 22:20:29 -0500594func (handler *APIHandler) GetImageDownloadStatus(ctx context.Context, img *voltha.ImageDownload) (*voltha.ImageDownload, error) {
595 log.Debugw("getImageDownloadStatus-request", log.Fields{"img": *img})
596 if isTestMode(ctx) {
Stephane Barbariedf5479f2019-01-29 22:13:00 -0500597 resp := &voltha.ImageDownload{DownloadState: voltha.ImageDownload_DOWNLOAD_SUCCEEDED}
khenaidoof5a5bfa2019-01-23 22:20:29 -0500598 return resp, nil
599 }
600
Stephane Barbariedf5479f2019-01-29 22:13:00 -0500601 failedresponse := &voltha.ImageDownload{DownloadState: voltha.ImageDownload_DOWNLOAD_UNKNOWN}
khenaidoof5a5bfa2019-01-23 22:20:29 -0500602
khenaidoo9cdc1a62019-01-24 21:57:40 -0500603 if handler.competeForTransaction() {
khenaidoo2c6a0992019-04-29 13:46:56 -0400604 if txn, err := handler.takeRequestOwnership(ctx, &utils.DeviceID{Id: img.Id}); err != nil {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500605 return failedresponse, err
606 } else {
607 defer txn.Close()
608 }
khenaidoof5a5bfa2019-01-23 22:20:29 -0500609 }
610
611 ch := make(chan interface{})
612 defer close(ch)
613 go handler.deviceMgr.getImageDownloadStatus(ctx, img, ch)
614
615 select {
616 case res := <-ch:
617 if res != nil {
618 if err, ok := res.(error); ok {
619 return failedresponse, err
620 }
621 if downloadResp, ok := res.(*voltha.ImageDownload); ok {
622 return downloadResp, nil
623 }
624 }
625 log.Warnw("download-image-status", log.Fields{"result": res})
626 return failedresponse, status.Errorf(codes.Internal, "%s", res)
627 case <-ctx.Done():
628 log.Debug("downloadImage-client-timeout")
629 return failedresponse, ctx.Err()
630 }
631}
632
633func (handler *APIHandler) GetImageDownload(ctx context.Context, img *voltha.ImageDownload) (*voltha.ImageDownload, error) {
634 log.Debugw("GetImageDownload-request", log.Fields{"img": *img})
635 if isTestMode(ctx) {
Stephane Barbariedf5479f2019-01-29 22:13:00 -0500636 resp := &voltha.ImageDownload{DownloadState: voltha.ImageDownload_DOWNLOAD_SUCCEEDED}
khenaidoof5a5bfa2019-01-23 22:20:29 -0500637 return resp, nil
638 }
639
640 if download, err := handler.deviceMgr.getImageDownload(ctx, img); err != nil {
Stephane Barbariedf5479f2019-01-29 22:13:00 -0500641 return &voltha.ImageDownload{DownloadState: voltha.ImageDownload_DOWNLOAD_UNKNOWN}, err
khenaidoof5a5bfa2019-01-23 22:20:29 -0500642 } else {
643 return download, nil
644 }
645}
646
647func (handler *APIHandler) ListImageDownloads(ctx context.Context, id *voltha.ID) (*voltha.ImageDownloads, error) {
648 log.Debugw("ListImageDownloads-request", log.Fields{"deviceId": id.Id})
649 if isTestMode(ctx) {
khenaidoo2c6a0992019-04-29 13:46:56 -0400650 resp := &voltha.ImageDownloads{Items: []*voltha.ImageDownload{}}
khenaidoof5a5bfa2019-01-23 22:20:29 -0500651 return resp, nil
652 }
653
654 if downloads, err := handler.deviceMgr.listImageDownloads(ctx, id.Id); err != nil {
655 failedResp := &voltha.ImageDownloads{
khenaidoo2c6a0992019-04-29 13:46:56 -0400656 Items: []*voltha.ImageDownload{
657 {DownloadState: voltha.ImageDownload_DOWNLOAD_UNKNOWN},
658 },
khenaidoof5a5bfa2019-01-23 22:20:29 -0500659 }
660 return failedResp, err
661 } else {
662 return downloads, nil
663 }
664}
665
khenaidoobf6e7bb2018-08-14 22:27:29 -0400666func (handler *APIHandler) UpdateDevicePmConfigs(ctx context.Context, configs *voltha.PmConfigs) (*empty.Empty, error) {
667 log.Debugw("UpdateDevicePmConfigs-request", log.Fields{"configs": *configs})
668 if isTestMode(ctx) {
669 out := new(empty.Empty)
670 return out, nil
671 }
672 return nil, errors.New("UnImplemented")
673}
674
675func (handler *APIHandler) CreateAlarmFilter(ctx context.Context, filter *voltha.AlarmFilter) (*voltha.AlarmFilter, error) {
676 log.Debugw("CreateAlarmFilter-request", log.Fields{"filter": *filter})
677 if isTestMode(ctx) {
678 f := &voltha.AlarmFilter{Id: filter.Id}
679 return f, nil
680 }
681 return nil, errors.New("UnImplemented")
682}
683
684func (handler *APIHandler) UpdateAlarmFilter(ctx context.Context, filter *voltha.AlarmFilter) (*voltha.AlarmFilter, error) {
685 log.Debugw("UpdateAlarmFilter-request", log.Fields{"filter": *filter})
686 if isTestMode(ctx) {
687 f := &voltha.AlarmFilter{Id: filter.Id}
688 return f, nil
689 }
690 return nil, errors.New("UnImplemented")
691}
692
693func (handler *APIHandler) DeleteAlarmFilter(ctx context.Context, id *voltha.ID) (*empty.Empty, error) {
694 log.Debugw("DeleteAlarmFilter-request", log.Fields{"id": *id})
695 if isTestMode(ctx) {
696 out := new(empty.Empty)
697 return out, nil
698 }
699 return nil, errors.New("UnImplemented")
700}
701
702func (handler *APIHandler) SelfTest(ctx context.Context, id *voltha.ID) (*voltha.SelfTestResponse, error) {
703 log.Debugw("SelfTest-request", log.Fields{"id": id})
704 if isTestMode(ctx) {
705 resp := &voltha.SelfTestResponse{Result: voltha.SelfTestResponse_SUCCESS}
706 return resp, nil
707 }
708 return nil, errors.New("UnImplemented")
709}
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500710
711func (handler *APIHandler) forwardPacketOut(packet *openflow_13.PacketOut) {
712 log.Debugw("forwardPacketOut-request", log.Fields{"packet": packet})
Richard Jankowski2755adf2019-01-17 17:16:48 -0500713 agent := handler.logicalDeviceMgr.getLogicalDeviceAgent(packet.Id)
714 agent.packetOut(packet.PacketOut)
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500715}
716func (handler *APIHandler) StreamPacketsOut(
717 packets voltha.VolthaService_StreamPacketsOutServer,
718) error {
719 log.Debugw("StreamPacketsOut-request", log.Fields{"packets": packets})
720 for {
721 packet, err := packets.Recv()
722
723 if err == io.EOF {
724 break
725 } else if err != nil {
726 log.Errorw("Failed to receive packet", log.Fields{"error": err})
727 }
728
729 handler.forwardPacketOut(packet)
730 }
731
732 log.Debugw("StreamPacketsOut-request-done", log.Fields{"packets": packets})
733 return nil
734}
735
khenaidoo297cd252019-02-07 22:10:23 -0500736func (handler *APIHandler) sendPacketIn(deviceId string, transationId string, packet *openflow_13.OfpPacketIn) {
737 // TODO: Augment the OF PacketIn to include the transactionId
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500738 packetIn := openflow_13.PacketIn{Id: deviceId, PacketIn: packet}
739 log.Debugw("sendPacketIn", log.Fields{"packetIn": packetIn})
Richard Jankowskidbab94a2018-12-06 16:20:25 -0500740 // Enqueue the packet
741 if err := handler.packetInQueue.Put(packetIn); err != nil {
742 log.Errorw("failed-to-enqueue-packet", log.Fields{"error": err})
743 }
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500744}
745
746func (handler *APIHandler) ReceivePacketsIn(
747 empty *empty.Empty,
748 packetsIn voltha.VolthaService_ReceivePacketsInServer,
749) error {
750 log.Debugw("ReceivePacketsIn-request", log.Fields{"packetsIn": packetsIn})
751
752 for {
Richard Jankowskidbab94a2018-12-06 16:20:25 -0500753 // Dequeue a packet
754 if packets, err := handler.packetInQueue.Get(1); err == nil {
755 log.Debugw("dequeued-packet", log.Fields{"packet": packets[0]})
756 if packet, ok := packets[0].(openflow_13.PacketIn); ok {
757 log.Debugw("sending-packet-in", log.Fields{"packet": packet})
758 if err := packetsIn.Send(&packet); err != nil {
759 log.Errorw("failed-to-send-packet", log.Fields{"error": err})
760 }
761 }
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500762 }
763 }
khenaidoof5a5bfa2019-01-23 22:20:29 -0500764 //TODO: FInd an elegant way to get out of the above loop when the Core is stopped
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500765}
766
767func (handler *APIHandler) sendChangeEvent(deviceId string, portStatus *openflow_13.OfpPortStatus) {
768 // TODO: validate the type of portStatus parameter
769 //if _, ok := portStatus.(*openflow_13.OfpPortStatus); ok {
770 //}
771 event := openflow_13.ChangeEvent{Id: deviceId, Event: &openflow_13.ChangeEvent_PortStatus{PortStatus: portStatus}}
772 log.Debugw("sendChangeEvent", log.Fields{"event": event})
Richard Jankowski199fd862019-03-18 14:49:51 -0400773 // Enqueue the change event
774 if err := handler.changeEventQueue.Put(event); err != nil {
775 log.Errorw("failed-to-enqueue-change-event", log.Fields{"error": err})
776 }
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500777}
778
779func (handler *APIHandler) ReceiveChangeEvents(
780 empty *empty.Empty,
781 changeEvents voltha.VolthaService_ReceiveChangeEventsServer,
782) error {
783 log.Debugw("ReceiveChangeEvents-request", log.Fields{"changeEvents": changeEvents})
784 for {
Richard Jankowski199fd862019-03-18 14:49:51 -0400785 // Dequeue a change event
786 if events, err := handler.changeEventQueue.Get(1); err == nil {
787 log.Debugw("dequeued-change-event", log.Fields{"event": events[0]})
788 if event, ok := events[0].(openflow_13.ChangeEvent); ok {
789 log.Debugw("sending-change-event", log.Fields{"event": event})
790 if err := changeEvents.Send(&event); err != nil {
791 log.Errorw("failed-to-send-change-event", log.Fields{"error": err})
792 }
793 }
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500794 }
795 }
Richard Jankowski199fd862019-03-18 14:49:51 -0400796}
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500797
798func (handler *APIHandler) Subscribe(
799 ctx context.Context,
800 ofAgent *voltha.OfAgentSubscriber,
801) (*voltha.OfAgentSubscriber, error) {
802 log.Debugw("Subscribe-request", log.Fields{"ofAgent": ofAgent})
803 return &voltha.OfAgentSubscriber{OfagentId: ofAgent.OfagentId, VolthaId: ofAgent.VolthaId}, nil
804}
William Kurkiandaa6bb22019-03-07 12:26:28 -0500805
806//@TODO useless stub, what should this actually do?
807func (handler *APIHandler) GetAlarmDeviceData(
808 ctx context.Context,
809 in *common.ID,
810) (*omci.AlarmDeviceData, error) {
811 log.Debug("GetAlarmDeviceData-stub")
812 return nil, nil
813}
814
815//@TODO useless stub, what should this actually do?
816func (handler *APIHandler) GetMeterStatsOfLogicalDevice(
khenaidoo2c6a0992019-04-29 13:46:56 -0400817 ctx context.Context,
William Kurkiandaa6bb22019-03-07 12:26:28 -0500818 in *common.ID,
819) (*openflow_13.MeterStatsReply, error) {
820 log.Debug("GetMeterStatsOfLogicalDevice-stub")
821 return nil, nil
822}
823
824//@TODO useless stub, what should this actually do?
825func (handler *APIHandler) GetMibDeviceData(
khenaidoo2c6a0992019-04-29 13:46:56 -0400826 ctx context.Context,
827 in *common.ID,
William Kurkiandaa6bb22019-03-07 12:26:28 -0500828) (*omci.MibDeviceData, error) {
829 log.Debug("GetMibDeviceData-stub")
830 return nil, nil
831}
832
William Kurkiandaa6bb22019-03-07 12:26:28 -0500833func (handler *APIHandler) SimulateAlarm(
834 ctx context.Context,
835 in *voltha.SimulateAlarmRequest,
836) (*common.OperationResp, error) {
serkant.uluderya334479d2019-04-10 08:26:15 -0700837 log.Debugw("SimulateAlarm-request", log.Fields{"id": in.Id})
838 successResp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS}
839 if isTestMode(ctx) {
840 return successResp, nil
841 }
842
843 if handler.competeForTransaction() {
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400844 if txn, err := handler.takeRequestOwnership(ctx, &utils.DeviceID{Id: in.Id}, handler.longRunningRequestTimeout); err != nil {
845 failedresponse := &common.OperationResp{Code: voltha.OperationResp_OPERATION_FAILURE}
serkant.uluderya334479d2019-04-10 08:26:15 -0700846 return failedresponse, err
847 } else {
848 defer txn.Close()
849 }
850 }
851
852 ch := make(chan interface{})
853 defer close(ch)
854 go handler.deviceMgr.simulateAlarm(ctx, in, ch)
855 return successResp, nil
William Kurkiandaa6bb22019-03-07 12:26:28 -0500856}
857
858//@TODO useless stub, what should this actually do?
859func (handler *APIHandler) UpdateLogicalDeviceMeterTable(
860 ctx context.Context,
861 in *openflow_13.MeterModUpdate,
862) (*empty.Empty, error) {
863 log.Debug("UpdateLogicalDeviceMeterTable-stub")
864 return nil, nil
865}