blob: 2c3b35e452b9654aa1951c0fe2080adc78f7fb79 [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
khenaidoo6d62c002019-05-15 21:57:03 -0400153// takeRequestOwnership creates a transaction in the dB for this request and handles the logic of transaction
154// acquisition. If the device is owned by this Core (in a core-pair) then acquire the transaction with a
155// timeout value (in the event of a timeout the other Core in the core-pair will proceed with the transaction). If the
156// device is not owned then this Core will just monitor the transaction for potential timeouts.
Richard Jankowski199fd862019-03-18 14:49:51 -0400157func (handler *APIHandler) takeRequestOwnership(ctx context.Context, id interface{}, maxTimeout ...int64) (*KVTransaction, error) {
khenaidoo6d62c002019-05-15 21:57:03 -0400158 t := time.Now()
Richard Jankowski199fd862019-03-18 14:49:51 -0400159 timeout := handler.defaultRequestTimeout
160 if len(maxTimeout) > 0 {
161 timeout = maxTimeout[0]
162 }
Richard Jankowski199fd862019-03-18 14:49:51 -0400163 txn, err := handler.createKvTransaction(ctx)
164 if txn == nil {
khenaidoo2c6a0992019-04-29 13:46:56 -0400165 return nil, err
Richard Jankowski199fd862019-03-18 14:49:51 -0400166 }
167
168 owned := false
169 if id != nil {
khenaidoo1ce37ad2019-03-24 22:07:24 -0400170 owned = handler.core.deviceOwnership.OwnedByMe(id)
Richard Jankowski199fd862019-03-18 14:49:51 -0400171 }
172 if owned {
173 if txn.Acquired(timeout) {
khenaidoo6d62c002019-05-15 21:57:03 -0400174 log.Debugw("acquired-transaction", log.Fields{"transaction-timeout": timeout})
Richard Jankowski199fd862019-03-18 14:49:51 -0400175 return txn, nil
176 } else {
177 return nil, errors.New("failed-to-seize-request")
178 }
179 } else {
180 if txn.Monitor(timeout) {
khenaidoo6d62c002019-05-15 21:57:03 -0400181 log.Debugw("acquired-transaction-after-timeout", log.Fields{"timeout": timeout, "waited-time": time.Since(t)})
Richard Jankowski199fd862019-03-18 14:49:51 -0400182 return txn, nil
183 } else {
khenaidoo6d62c002019-05-15 21:57:03 -0400184 log.Debugw("transaction-completed-by-other", log.Fields{"timeout": timeout, "waited-time": time.Since(t)})
185 return nil, errors.New(string(COMPLETED_BY_OTHER))
Richard Jankowski199fd862019-03-18 14:49:51 -0400186 }
187 }
188}
189
khenaidoo4d4802d2018-10-04 21:59:49 -0400190// waitForNilResponseOnSuccess is a helper function to wait for a response on channel ch where an nil
191// response is expected in a successful scenario
192func waitForNilResponseOnSuccess(ctx context.Context, ch chan interface{}) (*empty.Empty, error) {
193 select {
194 case res := <-ch:
195 if res == nil {
196 return new(empty.Empty), nil
197 } else if err, ok := res.(error); ok {
198 return new(empty.Empty), err
199 } else {
200 log.Warnw("unexpected-return-type", log.Fields{"result": res})
201 err = status.Errorf(codes.Internal, "%s", res)
202 return new(empty.Empty), err
203 }
204 case <-ctx.Done():
205 log.Debug("client-timeout")
206 return nil, ctx.Err()
207 }
208}
209
khenaidoobf6e7bb2018-08-14 22:27:29 -0400210func (handler *APIHandler) UpdateLogLevel(ctx context.Context, logging *voltha.Logging) (*empty.Empty, error) {
khenaidoo6f2fbe32019-01-18 16:16:50 -0500211 log.Debugw("UpdateLogLevel-request", log.Fields{"package": logging.PackageName, "intval": int(logging.Level)})
khenaidoo92e62c52018-10-03 14:02:54 -0400212 out := new(empty.Empty)
khenaidoo6f2fbe32019-01-18 16:16:50 -0500213 if logging.PackageName == "" {
214 log.SetAllLogLevel(int(logging.Level))
215 } else {
216 log.SetPackageLogLevel(logging.PackageName, int(logging.Level))
217 }
khenaidoo92e62c52018-10-03 14:02:54 -0400218 return out, nil
khenaidoobf6e7bb2018-08-14 22:27:29 -0400219}
220
khenaidoo54e0ddf2019-02-27 16:21:33 -0500221func (handler *APIHandler) UpdateMembership(ctx context.Context, membership *voltha.Membership) (*empty.Empty, error) {
222 log.Debugw("UpdateMembership-request", log.Fields{"membership": membership})
223 out := new(empty.Empty)
khenaidoo6417b6c2019-03-01 18:18:01 -0500224 if err := handler.core.updateCoreMembership(ctx, membership); err != nil {
khenaidoo54e0ddf2019-02-27 16:21:33 -0500225 return out, err
226 }
227 return out, nil
228}
229
khenaidoo6417b6c2019-03-01 18:18:01 -0500230func (handler *APIHandler) GetMembership(ctx context.Context, empty *empty.Empty) (*voltha.Membership, error) {
231 log.Debug("GetMembership-request")
232 if membership := handler.core.getCoreMembership(ctx); membership != nil {
233 return membership, nil
234 }
235 return &voltha.Membership{}, nil
236}
237
khenaidoobf6e7bb2018-08-14 22:27:29 -0400238func (handler *APIHandler) EnableLogicalDevicePort(ctx context.Context, id *voltha.LogicalPortId) (*empty.Empty, error) {
239 log.Debugw("EnableLogicalDevicePort-request", log.Fields{"id": id, "test": common.TestModeKeys_api_test.String()})
240 if isTestMode(ctx) {
241 out := new(empty.Empty)
242 return out, nil
243 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500244
khenaidoo9cdc1a62019-01-24 21:57:40 -0500245 if handler.competeForTransaction() {
khenaidoo2c6a0992019-04-29 13:46:56 -0400246 if txn, err := handler.takeRequestOwnership(ctx, &utils.LogicalDeviceID{Id: id.Id}); err != nil {
Richard Jankowski2755adf2019-01-17 17:16:48 -0500247 return new(empty.Empty), err
Richard Jankowski2755adf2019-01-17 17:16:48 -0500248 } else {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500249 defer txn.Close()
Richard Jankowski2755adf2019-01-17 17:16:48 -0500250 }
251 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500252
khenaidoo4d4802d2018-10-04 21:59:49 -0400253 ch := make(chan interface{})
254 defer close(ch)
khenaidoo19d7b632018-10-30 10:49:50 -0400255 go handler.logicalDeviceMgr.enableLogicalPort(ctx, id, ch)
khenaidoo4d4802d2018-10-04 21:59:49 -0400256 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400257}
258
259func (handler *APIHandler) DisableLogicalDevicePort(ctx context.Context, id *voltha.LogicalPortId) (*empty.Empty, error) {
260 log.Debugw("DisableLogicalDevicePort-request", log.Fields{"id": id, "test": common.TestModeKeys_api_test.String()})
261 if isTestMode(ctx) {
262 out := new(empty.Empty)
263 return out, nil
264 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500265
khenaidoo9cdc1a62019-01-24 21:57:40 -0500266 if handler.competeForTransaction() {
khenaidoo2c6a0992019-04-29 13:46:56 -0400267 if txn, err := handler.takeRequestOwnership(ctx, &utils.LogicalDeviceID{Id: id.Id}); err != nil {
Richard Jankowski2755adf2019-01-17 17:16:48 -0500268 return new(empty.Empty), err
Richard Jankowski2755adf2019-01-17 17:16:48 -0500269 } else {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500270 defer txn.Close()
Richard Jankowski2755adf2019-01-17 17:16:48 -0500271 }
272 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500273
khenaidoo19d7b632018-10-30 10:49:50 -0400274 ch := make(chan interface{})
275 defer close(ch)
276 go handler.logicalDeviceMgr.disableLogicalPort(ctx, id, ch)
277 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400278}
279
280func (handler *APIHandler) UpdateLogicalDeviceFlowTable(ctx context.Context, flow *openflow_13.FlowTableUpdate) (*empty.Empty, error) {
281 log.Debugw("UpdateLogicalDeviceFlowTable-request", log.Fields{"flow": flow, "test": common.TestModeKeys_api_test.String()})
282 if isTestMode(ctx) {
283 out := new(empty.Empty)
284 return out, nil
285 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500286
khenaidoo9cdc1a62019-01-24 21:57:40 -0500287 if handler.competeForTransaction() {
Richard Jankowski46464e92019-03-05 11:53:55 -0500288 if !handler.isOFControllerRequest(ctx) { // No need to acquire the transaction as request is sent to one core only
khenaidoo2c6a0992019-04-29 13:46:56 -0400289 if txn, err := handler.takeRequestOwnership(ctx, &utils.LogicalDeviceID{Id: flow.Id}); err != nil {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500290 return new(empty.Empty), err
291 } else {
292 defer txn.Close()
293 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500294 }
295 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500296
khenaidoo19d7b632018-10-30 10:49:50 -0400297 ch := make(chan interface{})
298 defer close(ch)
299 go handler.logicalDeviceMgr.updateFlowTable(ctx, flow.Id, flow.FlowMod, ch)
300 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400301}
302
303func (handler *APIHandler) UpdateLogicalDeviceFlowGroupTable(ctx context.Context, flow *openflow_13.FlowGroupTableUpdate) (*empty.Empty, error) {
304 log.Debugw("UpdateLogicalDeviceFlowGroupTable-request", log.Fields{"flow": flow, "test": common.TestModeKeys_api_test.String()})
305 if isTestMode(ctx) {
306 out := new(empty.Empty)
307 return out, nil
308 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500309
khenaidoo9cdc1a62019-01-24 21:57:40 -0500310 if handler.competeForTransaction() {
Richard Jankowski46464e92019-03-05 11:53:55 -0500311 if !handler.isOFControllerRequest(ctx) { // No need to acquire the transaction as request is sent to one core only
khenaidoo2c6a0992019-04-29 13:46:56 -0400312 if txn, err := handler.takeRequestOwnership(ctx, &utils.LogicalDeviceID{Id: flow.Id}); err != nil {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500313 return new(empty.Empty), err
314 } else {
315 defer txn.Close()
316 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500317 }
318 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500319
khenaidoo19d7b632018-10-30 10:49:50 -0400320 ch := make(chan interface{})
321 defer close(ch)
322 go handler.logicalDeviceMgr.updateGroupTable(ctx, flow.Id, flow.GroupMod, ch)
323 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400324}
325
khenaidoob9203542018-09-17 22:56:37 -0400326// GetDevice must be implemented in the read-only containers - should it also be implemented here?
327func (handler *APIHandler) GetDevice(ctx context.Context, id *voltha.ID) (*voltha.Device, error) {
328 log.Debugw("GetDevice-request", log.Fields{"id": id})
khenaidoo19d7b632018-10-30 10:49:50 -0400329 return handler.deviceMgr.GetDevice(id.Id)
khenaidoob9203542018-09-17 22:56:37 -0400330}
331
332// GetDevice must be implemented in the read-only containers - should it also be implemented here?
333func (handler *APIHandler) ListDevices(ctx context.Context, empty *empty.Empty) (*voltha.Devices, error) {
334 log.Debug("ListDevices")
335 return handler.deviceMgr.ListDevices()
336}
337
khenaidoo7ccedd52018-12-14 16:48:54 -0500338// ListDeviceIds returns the list of device ids managed by a voltha core
339func (handler *APIHandler) ListDeviceIds(ctx context.Context, empty *empty.Empty) (*voltha.IDs, error) {
340 log.Debug("ListDeviceIDs")
341 if isTestMode(ctx) {
342 out := &voltha.IDs{Items: make([]*voltha.ID, 0)}
343 return out, nil
344 }
345 return handler.deviceMgr.ListDeviceIds()
346}
347
348//ReconcileDevices is a request to a voltha core to managed a list of devices based on their IDs
349func (handler *APIHandler) ReconcileDevices(ctx context.Context, ids *voltha.IDs) (*empty.Empty, error) {
350 log.Debug("ReconcileDevices")
351 if isTestMode(ctx) {
352 out := new(empty.Empty)
353 return out, nil
354 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500355
khenaidoo9cdc1a62019-01-24 21:57:40 -0500356 // No need to grab a transaction as this request is core specific
357
khenaidoo7ccedd52018-12-14 16:48:54 -0500358 ch := make(chan interface{})
359 defer close(ch)
360 go handler.deviceMgr.ReconcileDevices(ctx, ids, ch)
361 return waitForNilResponseOnSuccess(ctx, ch)
362}
363
khenaidoob9203542018-09-17 22:56:37 -0400364// GetLogicalDevice must be implemented in the read-only containers - should it also be implemented here?
365func (handler *APIHandler) GetLogicalDevice(ctx context.Context, id *voltha.ID) (*voltha.LogicalDevice, error) {
366 log.Debugw("GetLogicalDevice-request", log.Fields{"id": id})
367 return handler.logicalDeviceMgr.getLogicalDevice(id.Id)
368}
369
370// ListLogicalDevices must be implemented in the read-only containers - should it also be implemented here?
371func (handler *APIHandler) ListLogicalDevices(ctx context.Context, empty *empty.Empty) (*voltha.LogicalDevices, error) {
372 log.Debug("ListLogicalDevices")
373 return handler.logicalDeviceMgr.listLogicalDevices()
374}
375
khenaidoo21d51152019-02-01 13:48:37 -0500376// ListAdapters returns the contents of all adapters known to the system
377func (handler *APIHandler) ListAdapters(ctx context.Context, empty *empty.Empty) (*voltha.Adapters, error) {
378 log.Debug("ListDevices")
379 return handler.adapterMgr.listAdapters(ctx)
380}
381
khenaidoo19d7b632018-10-30 10:49:50 -0400382// ListLogicalDevicePorts must be implemented in the read-only containers - should it also be implemented here?
383func (handler *APIHandler) ListLogicalDevicePorts(ctx context.Context, id *voltha.ID) (*voltha.LogicalPorts, error) {
384 log.Debugw("ListLogicalDevicePorts", log.Fields{"logicaldeviceid": id})
385 return handler.logicalDeviceMgr.ListLogicalDevicePorts(ctx, id.Id)
386}
387
khenaidoo4d4802d2018-10-04 21:59:49 -0400388// CreateDevice creates a new parent device in the data model
khenaidoobf6e7bb2018-08-14 22:27:29 -0400389func (handler *APIHandler) CreateDevice(ctx context.Context, device *voltha.Device) (*voltha.Device, error) {
khenaidoob9203542018-09-17 22:56:37 -0400390 log.Debugw("createdevice", log.Fields{"device": *device})
khenaidoobf6e7bb2018-08-14 22:27:29 -0400391 if isTestMode(ctx) {
392 return &voltha.Device{Id: device.Id}, nil
393 }
Richard Jankowskid42826e2018-11-02 16:06:37 -0400394
khenaidoo9cdc1a62019-01-24 21:57:40 -0500395 if handler.competeForTransaction() {
Richard Jankowski199fd862019-03-18 14:49:51 -0400396 if txn, err := handler.acquireRequest(ctx, nil); err != nil {
Richard Jankowski2755adf2019-01-17 17:16:48 -0500397 return &voltha.Device{}, err
Richard Jankowski2755adf2019-01-17 17:16:48 -0500398 } else {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500399 defer txn.Close()
Richard Jankowski2755adf2019-01-17 17:16:48 -0500400 }
401 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500402
khenaidoob9203542018-09-17 22:56:37 -0400403 ch := make(chan interface{})
404 defer close(ch)
405 go handler.deviceMgr.createDevice(ctx, device, ch)
406 select {
407 case res := <-ch:
khenaidoo92e62c52018-10-03 14:02:54 -0400408 if res != nil {
409 if err, ok := res.(error); ok {
410 return &voltha.Device{}, err
411 }
412 if d, ok := res.(*voltha.Device); ok {
khenaidoo2c6a0992019-04-29 13:46:56 -0400413 handler.core.deviceOwnership.OwnedByMe(&utils.DeviceID{Id: d.Id})
khenaidoo92e62c52018-10-03 14:02:54 -0400414 return d, nil
415 }
khenaidoob9203542018-09-17 22:56:37 -0400416 }
khenaidoo92e62c52018-10-03 14:02:54 -0400417 log.Warnw("create-device-unexpected-return-type", log.Fields{"result": res})
418 err := status.Errorf(codes.Internal, "%s", res)
419 return &voltha.Device{}, err
khenaidoob9203542018-09-17 22:56:37 -0400420 case <-ctx.Done():
421 log.Debug("createdevice-client-timeout")
422 return nil, ctx.Err()
423 }
khenaidoobf6e7bb2018-08-14 22:27:29 -0400424}
425
khenaidoo4d4802d2018-10-04 21:59:49 -0400426// EnableDevice activates a device by invoking the adopt_device API on the appropriate adapter
khenaidoobf6e7bb2018-08-14 22:27:29 -0400427func (handler *APIHandler) EnableDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) {
khenaidoob9203542018-09-17 22:56:37 -0400428 log.Debugw("enabledevice", log.Fields{"id": id})
khenaidoobf6e7bb2018-08-14 22:27:29 -0400429 if isTestMode(ctx) {
khenaidoo4d4802d2018-10-04 21:59:49 -0400430 return new(empty.Empty), nil
khenaidoobf6e7bb2018-08-14 22:27:29 -0400431 }
Richard Jankowskid42826e2018-11-02 16:06:37 -0400432
khenaidoo9cdc1a62019-01-24 21:57:40 -0500433 if handler.competeForTransaction() {
khenaidoo2c6a0992019-04-29 13:46:56 -0400434 if txn, err := handler.takeRequestOwnership(ctx, &utils.DeviceID{Id: id.Id}, handler.longRunningRequestTimeout); err != nil {
Richard Jankowski2755adf2019-01-17 17:16:48 -0500435 return new(empty.Empty), err
Richard Jankowski2755adf2019-01-17 17:16:48 -0500436 } else {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500437 defer txn.Close()
Richard Jankowski2755adf2019-01-17 17:16:48 -0500438 }
439 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500440
khenaidoob9203542018-09-17 22:56:37 -0400441 ch := make(chan interface{})
442 defer close(ch)
443 go handler.deviceMgr.enableDevice(ctx, id, ch)
khenaidoo4d4802d2018-10-04 21:59:49 -0400444 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400445}
446
khenaidoo4d4802d2018-10-04 21:59:49 -0400447// DisableDevice disables a device along with any child device it may have
khenaidoobf6e7bb2018-08-14 22:27:29 -0400448func (handler *APIHandler) DisableDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) {
449 log.Debugw("disabledevice-request", log.Fields{"id": id})
450 if isTestMode(ctx) {
khenaidoo4d4802d2018-10-04 21:59:49 -0400451 return new(empty.Empty), nil
khenaidoobf6e7bb2018-08-14 22:27:29 -0400452 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500453
khenaidoo9cdc1a62019-01-24 21:57:40 -0500454 if handler.competeForTransaction() {
khenaidoo2c6a0992019-04-29 13:46:56 -0400455 if txn, err := handler.takeRequestOwnership(ctx, &utils.DeviceID{Id: id.Id}); err != nil {
Richard Jankowski2755adf2019-01-17 17:16:48 -0500456 return new(empty.Empty), err
Richard Jankowski2755adf2019-01-17 17:16:48 -0500457 } else {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500458 defer txn.Close()
Richard Jankowski2755adf2019-01-17 17:16:48 -0500459 }
460 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500461
khenaidoo92e62c52018-10-03 14:02:54 -0400462 ch := make(chan interface{})
463 defer close(ch)
464 go handler.deviceMgr.disableDevice(ctx, id, ch)
khenaidoo4d4802d2018-10-04 21:59:49 -0400465 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400466}
467
khenaidoo4d4802d2018-10-04 21:59:49 -0400468//RebootDevice invoked the reboot API to the corresponding adapter
khenaidoobf6e7bb2018-08-14 22:27:29 -0400469func (handler *APIHandler) RebootDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) {
khenaidoo4d4802d2018-10-04 21:59:49 -0400470 log.Debugw("rebootDevice-request", log.Fields{"id": id})
khenaidoobf6e7bb2018-08-14 22:27:29 -0400471 if isTestMode(ctx) {
khenaidoo4d4802d2018-10-04 21:59:49 -0400472 return new(empty.Empty), nil
khenaidoobf6e7bb2018-08-14 22:27:29 -0400473 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500474
khenaidoo9cdc1a62019-01-24 21:57:40 -0500475 if handler.competeForTransaction() {
khenaidoo2c6a0992019-04-29 13:46:56 -0400476 if txn, err := handler.takeRequestOwnership(ctx, &utils.DeviceID{Id: id.Id}); err != nil {
Richard Jankowski2755adf2019-01-17 17:16:48 -0500477 return new(empty.Empty), err
Richard Jankowski2755adf2019-01-17 17:16:48 -0500478 } else {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500479 defer txn.Close()
Richard Jankowski2755adf2019-01-17 17:16:48 -0500480 }
481 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500482
khenaidoo4d4802d2018-10-04 21:59:49 -0400483 ch := make(chan interface{})
484 defer close(ch)
485 go handler.deviceMgr.rebootDevice(ctx, id, ch)
486 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400487}
488
khenaidoo4d4802d2018-10-04 21:59:49 -0400489// DeleteDevice removes a device from the data model
khenaidoobf6e7bb2018-08-14 22:27:29 -0400490func (handler *APIHandler) DeleteDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) {
491 log.Debugw("deletedevice-request", log.Fields{"id": id})
492 if isTestMode(ctx) {
khenaidoo4d4802d2018-10-04 21:59:49 -0400493 return new(empty.Empty), nil
khenaidoobf6e7bb2018-08-14 22:27:29 -0400494 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500495
khenaidoo9cdc1a62019-01-24 21:57:40 -0500496 if handler.competeForTransaction() {
khenaidoo6d62c002019-05-15 21:57:03 -0400497 if txn, err := handler.takeRequestOwnership(ctx, &utils.DeviceID{Id: id.Id}); err != nil {
498 // Remove the device in memory
499 if err.Error() == (errors.New(string(COMPLETED_BY_OTHER)).Error()) {
500 handler.deviceMgr.stopManagingDevice(id.Id)
501 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500502 return new(empty.Empty), err
Richard Jankowski2755adf2019-01-17 17:16:48 -0500503 } else {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500504 defer txn.Close()
Richard Jankowski2755adf2019-01-17 17:16:48 -0500505 }
506 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500507
khenaidoo4d4802d2018-10-04 21:59:49 -0400508 ch := make(chan interface{})
509 defer close(ch)
510 go handler.deviceMgr.deleteDevice(ctx, id, ch)
511 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400512}
513
khenaidoof5a5bfa2019-01-23 22:20:29 -0500514// processImageRequest is a helper method to execute an image download request
515func (handler *APIHandler) processImageRequest(ctx context.Context, img *voltha.ImageDownload, requestType int) (*common.OperationResp, error) {
516 log.Debugw("processImageDownload", log.Fields{"img": *img, "requestType": requestType})
517 if isTestMode(ctx) {
518 resp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS}
519 return resp, nil
520 }
521
khenaidoo9cdc1a62019-01-24 21:57:40 -0500522 if handler.competeForTransaction() {
khenaidoo2c6a0992019-04-29 13:46:56 -0400523 if txn, err := handler.takeRequestOwnership(ctx, &utils.DeviceID{Id: img.Id}); err != nil {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500524 return &common.OperationResp{}, err
525 } else {
526 defer txn.Close()
527 }
khenaidoof5a5bfa2019-01-23 22:20:29 -0500528 }
529
khenaidoo2c6a0992019-04-29 13:46:56 -0400530 failedresponse := &common.OperationResp{Code: voltha.OperationResp_OPERATION_FAILURE}
khenaidoof5a5bfa2019-01-23 22:20:29 -0500531
532 ch := make(chan interface{})
533 defer close(ch)
534 switch requestType {
535 case IMAGE_DOWNLOAD:
536 go handler.deviceMgr.downloadImage(ctx, img, ch)
537 case CANCEL_IMAGE_DOWNLOAD:
538 go handler.deviceMgr.cancelImageDownload(ctx, img, ch)
539 case ACTIVATE_IMAGE:
540 go handler.deviceMgr.activateImage(ctx, img, ch)
541 case REVERT_IMAGE:
542 go handler.deviceMgr.revertImage(ctx, img, ch)
543 default:
544 log.Warn("invalid-request-type", log.Fields{"requestType": requestType})
545 return failedresponse, status.Errorf(codes.InvalidArgument, "%d", requestType)
546 }
547 select {
548 case res := <-ch:
549 if res != nil {
550 if err, ok := res.(error); ok {
551 return failedresponse, err
552 }
553 if opResp, ok := res.(*common.OperationResp); ok {
554 return opResp, nil
555 }
556 }
557 log.Warnw("download-image-unexpected-return-type", log.Fields{"result": res})
558 return failedresponse, status.Errorf(codes.Internal, "%s", res)
559 case <-ctx.Done():
560 log.Debug("downloadImage-client-timeout")
561 return nil, ctx.Err()
562 }
563}
564
khenaidoobf6e7bb2018-08-14 22:27:29 -0400565func (handler *APIHandler) DownloadImage(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) {
566 log.Debugw("DownloadImage-request", log.Fields{"img": *img})
567 if isTestMode(ctx) {
568 resp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS}
569 return resp, nil
570 }
571
khenaidoof5a5bfa2019-01-23 22:20:29 -0500572 return handler.processImageRequest(ctx, img, IMAGE_DOWNLOAD)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400573}
574
575func (handler *APIHandler) CancelImageDownload(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) {
khenaidoof5a5bfa2019-01-23 22:20:29 -0500576 log.Debugw("cancelImageDownload-request", log.Fields{"img": *img})
khenaidoobf6e7bb2018-08-14 22:27:29 -0400577 if isTestMode(ctx) {
578 resp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS}
579 return resp, nil
580 }
khenaidoof5a5bfa2019-01-23 22:20:29 -0500581 return handler.processImageRequest(ctx, img, CANCEL_IMAGE_DOWNLOAD)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400582}
583
584func (handler *APIHandler) ActivateImageUpdate(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) {
khenaidoof5a5bfa2019-01-23 22:20:29 -0500585 log.Debugw("activateImageUpdate-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, ACTIVATE_IMAGE)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400592}
593
594func (handler *APIHandler) RevertImageUpdate(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) {
khenaidoof5a5bfa2019-01-23 22:20:29 -0500595 log.Debugw("revertImageUpdate-request", log.Fields{"img": *img})
khenaidoobf6e7bb2018-08-14 22:27:29 -0400596 if isTestMode(ctx) {
597 resp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS}
598 return resp, nil
599 }
khenaidoof5a5bfa2019-01-23 22:20:29 -0500600
601 return handler.processImageRequest(ctx, img, REVERT_IMAGE)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400602}
603
khenaidoof5a5bfa2019-01-23 22:20:29 -0500604func (handler *APIHandler) GetImageDownloadStatus(ctx context.Context, img *voltha.ImageDownload) (*voltha.ImageDownload, error) {
605 log.Debugw("getImageDownloadStatus-request", log.Fields{"img": *img})
606 if isTestMode(ctx) {
Stephane Barbariedf5479f2019-01-29 22:13:00 -0500607 resp := &voltha.ImageDownload{DownloadState: voltha.ImageDownload_DOWNLOAD_SUCCEEDED}
khenaidoof5a5bfa2019-01-23 22:20:29 -0500608 return resp, nil
609 }
610
Stephane Barbariedf5479f2019-01-29 22:13:00 -0500611 failedresponse := &voltha.ImageDownload{DownloadState: voltha.ImageDownload_DOWNLOAD_UNKNOWN}
khenaidoof5a5bfa2019-01-23 22:20:29 -0500612
khenaidoo9cdc1a62019-01-24 21:57:40 -0500613 if handler.competeForTransaction() {
khenaidoo2c6a0992019-04-29 13:46:56 -0400614 if txn, err := handler.takeRequestOwnership(ctx, &utils.DeviceID{Id: img.Id}); err != nil {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500615 return failedresponse, err
616 } else {
617 defer txn.Close()
618 }
khenaidoof5a5bfa2019-01-23 22:20:29 -0500619 }
620
621 ch := make(chan interface{})
622 defer close(ch)
623 go handler.deviceMgr.getImageDownloadStatus(ctx, img, ch)
624
625 select {
626 case res := <-ch:
627 if res != nil {
628 if err, ok := res.(error); ok {
629 return failedresponse, err
630 }
631 if downloadResp, ok := res.(*voltha.ImageDownload); ok {
632 return downloadResp, nil
633 }
634 }
635 log.Warnw("download-image-status", log.Fields{"result": res})
636 return failedresponse, status.Errorf(codes.Internal, "%s", res)
637 case <-ctx.Done():
638 log.Debug("downloadImage-client-timeout")
639 return failedresponse, ctx.Err()
640 }
641}
642
643func (handler *APIHandler) GetImageDownload(ctx context.Context, img *voltha.ImageDownload) (*voltha.ImageDownload, error) {
644 log.Debugw("GetImageDownload-request", log.Fields{"img": *img})
645 if isTestMode(ctx) {
Stephane Barbariedf5479f2019-01-29 22:13:00 -0500646 resp := &voltha.ImageDownload{DownloadState: voltha.ImageDownload_DOWNLOAD_SUCCEEDED}
khenaidoof5a5bfa2019-01-23 22:20:29 -0500647 return resp, nil
648 }
649
650 if download, err := handler.deviceMgr.getImageDownload(ctx, img); err != nil {
Stephane Barbariedf5479f2019-01-29 22:13:00 -0500651 return &voltha.ImageDownload{DownloadState: voltha.ImageDownload_DOWNLOAD_UNKNOWN}, err
khenaidoof5a5bfa2019-01-23 22:20:29 -0500652 } else {
653 return download, nil
654 }
655}
656
657func (handler *APIHandler) ListImageDownloads(ctx context.Context, id *voltha.ID) (*voltha.ImageDownloads, error) {
658 log.Debugw("ListImageDownloads-request", log.Fields{"deviceId": id.Id})
659 if isTestMode(ctx) {
khenaidoo2c6a0992019-04-29 13:46:56 -0400660 resp := &voltha.ImageDownloads{Items: []*voltha.ImageDownload{}}
khenaidoof5a5bfa2019-01-23 22:20:29 -0500661 return resp, nil
662 }
663
664 if downloads, err := handler.deviceMgr.listImageDownloads(ctx, id.Id); err != nil {
665 failedResp := &voltha.ImageDownloads{
khenaidoo2c6a0992019-04-29 13:46:56 -0400666 Items: []*voltha.ImageDownload{
667 {DownloadState: voltha.ImageDownload_DOWNLOAD_UNKNOWN},
668 },
khenaidoof5a5bfa2019-01-23 22:20:29 -0500669 }
670 return failedResp, err
671 } else {
672 return downloads, nil
673 }
674}
675
khenaidoobf6e7bb2018-08-14 22:27:29 -0400676func (handler *APIHandler) UpdateDevicePmConfigs(ctx context.Context, configs *voltha.PmConfigs) (*empty.Empty, error) {
677 log.Debugw("UpdateDevicePmConfigs-request", log.Fields{"configs": *configs})
678 if isTestMode(ctx) {
679 out := new(empty.Empty)
680 return out, nil
681 }
682 return nil, errors.New("UnImplemented")
683}
684
685func (handler *APIHandler) CreateAlarmFilter(ctx context.Context, filter *voltha.AlarmFilter) (*voltha.AlarmFilter, error) {
686 log.Debugw("CreateAlarmFilter-request", log.Fields{"filter": *filter})
687 if isTestMode(ctx) {
688 f := &voltha.AlarmFilter{Id: filter.Id}
689 return f, nil
690 }
691 return nil, errors.New("UnImplemented")
692}
693
694func (handler *APIHandler) UpdateAlarmFilter(ctx context.Context, filter *voltha.AlarmFilter) (*voltha.AlarmFilter, error) {
695 log.Debugw("UpdateAlarmFilter-request", log.Fields{"filter": *filter})
696 if isTestMode(ctx) {
697 f := &voltha.AlarmFilter{Id: filter.Id}
698 return f, nil
699 }
700 return nil, errors.New("UnImplemented")
701}
702
703func (handler *APIHandler) DeleteAlarmFilter(ctx context.Context, id *voltha.ID) (*empty.Empty, error) {
704 log.Debugw("DeleteAlarmFilter-request", log.Fields{"id": *id})
705 if isTestMode(ctx) {
706 out := new(empty.Empty)
707 return out, nil
708 }
709 return nil, errors.New("UnImplemented")
710}
711
712func (handler *APIHandler) SelfTest(ctx context.Context, id *voltha.ID) (*voltha.SelfTestResponse, error) {
713 log.Debugw("SelfTest-request", log.Fields{"id": id})
714 if isTestMode(ctx) {
715 resp := &voltha.SelfTestResponse{Result: voltha.SelfTestResponse_SUCCESS}
716 return resp, nil
717 }
718 return nil, errors.New("UnImplemented")
719}
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500720
721func (handler *APIHandler) forwardPacketOut(packet *openflow_13.PacketOut) {
722 log.Debugw("forwardPacketOut-request", log.Fields{"packet": packet})
Richard Jankowski2755adf2019-01-17 17:16:48 -0500723 agent := handler.logicalDeviceMgr.getLogicalDeviceAgent(packet.Id)
724 agent.packetOut(packet.PacketOut)
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500725}
726func (handler *APIHandler) StreamPacketsOut(
727 packets voltha.VolthaService_StreamPacketsOutServer,
728) error {
729 log.Debugw("StreamPacketsOut-request", log.Fields{"packets": packets})
730 for {
731 packet, err := packets.Recv()
732
733 if err == io.EOF {
734 break
735 } else if err != nil {
736 log.Errorw("Failed to receive packet", log.Fields{"error": err})
737 }
738
739 handler.forwardPacketOut(packet)
740 }
741
742 log.Debugw("StreamPacketsOut-request-done", log.Fields{"packets": packets})
743 return nil
744}
745
khenaidoo297cd252019-02-07 22:10:23 -0500746func (handler *APIHandler) sendPacketIn(deviceId string, transationId string, packet *openflow_13.OfpPacketIn) {
747 // TODO: Augment the OF PacketIn to include the transactionId
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500748 packetIn := openflow_13.PacketIn{Id: deviceId, PacketIn: packet}
749 log.Debugw("sendPacketIn", log.Fields{"packetIn": packetIn})
Richard Jankowskidbab94a2018-12-06 16:20:25 -0500750 // Enqueue the packet
751 if err := handler.packetInQueue.Put(packetIn); err != nil {
752 log.Errorw("failed-to-enqueue-packet", log.Fields{"error": err})
753 }
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500754}
755
756func (handler *APIHandler) ReceivePacketsIn(
757 empty *empty.Empty,
758 packetsIn voltha.VolthaService_ReceivePacketsInServer,
759) error {
760 log.Debugw("ReceivePacketsIn-request", log.Fields{"packetsIn": packetsIn})
761
762 for {
Richard Jankowskidbab94a2018-12-06 16:20:25 -0500763 // Dequeue a packet
764 if packets, err := handler.packetInQueue.Get(1); err == nil {
765 log.Debugw("dequeued-packet", log.Fields{"packet": packets[0]})
766 if packet, ok := packets[0].(openflow_13.PacketIn); ok {
767 log.Debugw("sending-packet-in", log.Fields{"packet": packet})
768 if err := packetsIn.Send(&packet); err != nil {
769 log.Errorw("failed-to-send-packet", log.Fields{"error": err})
770 }
771 }
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500772 }
773 }
khenaidoof5a5bfa2019-01-23 22:20:29 -0500774 //TODO: FInd an elegant way to get out of the above loop when the Core is stopped
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500775}
776
777func (handler *APIHandler) sendChangeEvent(deviceId string, portStatus *openflow_13.OfpPortStatus) {
778 // TODO: validate the type of portStatus parameter
779 //if _, ok := portStatus.(*openflow_13.OfpPortStatus); ok {
780 //}
781 event := openflow_13.ChangeEvent{Id: deviceId, Event: &openflow_13.ChangeEvent_PortStatus{PortStatus: portStatus}}
782 log.Debugw("sendChangeEvent", log.Fields{"event": event})
Richard Jankowski199fd862019-03-18 14:49:51 -0400783 // Enqueue the change event
784 if err := handler.changeEventQueue.Put(event); err != nil {
785 log.Errorw("failed-to-enqueue-change-event", log.Fields{"error": err})
786 }
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500787}
788
789func (handler *APIHandler) ReceiveChangeEvents(
790 empty *empty.Empty,
791 changeEvents voltha.VolthaService_ReceiveChangeEventsServer,
792) error {
793 log.Debugw("ReceiveChangeEvents-request", log.Fields{"changeEvents": changeEvents})
794 for {
Richard Jankowski199fd862019-03-18 14:49:51 -0400795 // Dequeue a change event
796 if events, err := handler.changeEventQueue.Get(1); err == nil {
797 log.Debugw("dequeued-change-event", log.Fields{"event": events[0]})
798 if event, ok := events[0].(openflow_13.ChangeEvent); ok {
799 log.Debugw("sending-change-event", log.Fields{"event": event})
800 if err := changeEvents.Send(&event); err != nil {
801 log.Errorw("failed-to-send-change-event", log.Fields{"error": err})
802 }
803 }
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500804 }
805 }
Richard Jankowski199fd862019-03-18 14:49:51 -0400806}
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500807
808func (handler *APIHandler) Subscribe(
809 ctx context.Context,
810 ofAgent *voltha.OfAgentSubscriber,
811) (*voltha.OfAgentSubscriber, error) {
812 log.Debugw("Subscribe-request", log.Fields{"ofAgent": ofAgent})
813 return &voltha.OfAgentSubscriber{OfagentId: ofAgent.OfagentId, VolthaId: ofAgent.VolthaId}, nil
814}
William Kurkiandaa6bb22019-03-07 12:26:28 -0500815
816//@TODO useless stub, what should this actually do?
817func (handler *APIHandler) GetAlarmDeviceData(
818 ctx context.Context,
819 in *common.ID,
820) (*omci.AlarmDeviceData, error) {
821 log.Debug("GetAlarmDeviceData-stub")
822 return nil, nil
823}
824
825//@TODO useless stub, what should this actually do?
826func (handler *APIHandler) GetMeterStatsOfLogicalDevice(
khenaidoo2c6a0992019-04-29 13:46:56 -0400827 ctx context.Context,
William Kurkiandaa6bb22019-03-07 12:26:28 -0500828 in *common.ID,
829) (*openflow_13.MeterStatsReply, error) {
830 log.Debug("GetMeterStatsOfLogicalDevice-stub")
831 return nil, nil
832}
833
834//@TODO useless stub, what should this actually do?
835func (handler *APIHandler) GetMibDeviceData(
khenaidoo2c6a0992019-04-29 13:46:56 -0400836 ctx context.Context,
837 in *common.ID,
William Kurkiandaa6bb22019-03-07 12:26:28 -0500838) (*omci.MibDeviceData, error) {
839 log.Debug("GetMibDeviceData-stub")
840 return nil, nil
841}
842
William Kurkiandaa6bb22019-03-07 12:26:28 -0500843func (handler *APIHandler) SimulateAlarm(
844 ctx context.Context,
845 in *voltha.SimulateAlarmRequest,
846) (*common.OperationResp, error) {
serkant.uluderya334479d2019-04-10 08:26:15 -0700847 log.Debugw("SimulateAlarm-request", log.Fields{"id": in.Id})
848 successResp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS}
849 if isTestMode(ctx) {
850 return successResp, nil
851 }
852
853 if handler.competeForTransaction() {
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400854 if txn, err := handler.takeRequestOwnership(ctx, &utils.DeviceID{Id: in.Id}, handler.longRunningRequestTimeout); err != nil {
855 failedresponse := &common.OperationResp{Code: voltha.OperationResp_OPERATION_FAILURE}
serkant.uluderya334479d2019-04-10 08:26:15 -0700856 return failedresponse, err
857 } else {
858 defer txn.Close()
859 }
860 }
861
862 ch := make(chan interface{})
863 defer close(ch)
864 go handler.deviceMgr.simulateAlarm(ctx, in, ch)
865 return successResp, nil
William Kurkiandaa6bb22019-03-07 12:26:28 -0500866}
867
868//@TODO useless stub, what should this actually do?
869func (handler *APIHandler) UpdateLogicalDeviceMeterTable(
870 ctx context.Context,
871 in *openflow_13.MeterModUpdate,
872) (*empty.Empty, error) {
873 log.Debug("UpdateLogicalDeviceMeterTable-stub")
874 return nil, nil
875}