blob: 8180ecd33309ec66e8c3c669fe985e295b3568cb [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
khenaidoo3d3b8c22019-05-22 18:10:39 -0400110 log.Debug("OFController-request")
khenaidoo9cdc1a62019-01-24 21:57:40 -0500111 return true
112 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500113 }
khenaidoo3d3b8c22019-05-22 18:10:39 -0400114 log.Debug("not-OFController-request")
khenaidoo9cdc1a62019-01-24 21:57:40 -0500115 return false
116}
117
118// competeForTransaction is a helper function to determine whether every request needs to compete with another
119// Core to execute the request
120func (handler *APIHandler) competeForTransaction() bool {
121 return handler.coreInCompetingMode
122}
123
khenaidoo631fe542019-05-31 15:44:43 -0400124// acquireRequest handles transaction processing for device creation and list requests, i.e. when there are no
125// specific id requested (list scenario) or id present in the request (creation use case).
126func (handler *APIHandler) acquireRequest(ctx context.Context, maxTimeout ...int64) (*KVTransaction, error) {
khenaidoo43aa6bd2019-05-29 13:35:13 -0400127 timeout := handler.defaultRequestTimeout
128 if len(maxTimeout) > 0 {
129 timeout = maxTimeout[0]
130 }
131 log.Debugw("transaction-timeout", log.Fields{"timeout": timeout})
132 txn, err := handler.createKvTransaction(ctx)
133 if txn == nil {
134 return nil, err
135 } else if txn.Acquired(timeout) {
136 return txn, nil
137 } else {
138 return nil, errors.New("failed-to-seize-request")
139 }
140}
141
khenaidoo6d62c002019-05-15 21:57:03 -0400142// takeRequestOwnership creates a transaction in the dB for this request and handles the logic of transaction
143// acquisition. If the device is owned by this Core (in a core-pair) then acquire the transaction with a
144// timeout value (in the event of a timeout the other Core in the core-pair will proceed with the transaction). If the
145// device is not owned then this Core will just monitor the transaction for potential timeouts.
Richard Jankowski199fd862019-03-18 14:49:51 -0400146func (handler *APIHandler) takeRequestOwnership(ctx context.Context, id interface{}, maxTimeout ...int64) (*KVTransaction, error) {
khenaidoo6d62c002019-05-15 21:57:03 -0400147 t := time.Now()
Richard Jankowski199fd862019-03-18 14:49:51 -0400148 timeout := handler.defaultRequestTimeout
149 if len(maxTimeout) > 0 {
150 timeout = maxTimeout[0]
151 }
Richard Jankowski199fd862019-03-18 14:49:51 -0400152 txn, err := handler.createKvTransaction(ctx)
153 if txn == nil {
khenaidoo2c6a0992019-04-29 13:46:56 -0400154 return nil, err
Richard Jankowski199fd862019-03-18 14:49:51 -0400155 }
156
157 owned := false
158 if id != nil {
khenaidoo1ce37ad2019-03-24 22:07:24 -0400159 owned = handler.core.deviceOwnership.OwnedByMe(id)
Richard Jankowski199fd862019-03-18 14:49:51 -0400160 }
161 if owned {
162 if txn.Acquired(timeout) {
khenaidoo6d62c002019-05-15 21:57:03 -0400163 log.Debugw("acquired-transaction", log.Fields{"transaction-timeout": timeout})
Richard Jankowski199fd862019-03-18 14:49:51 -0400164 return txn, nil
165 } else {
166 return nil, errors.New("failed-to-seize-request")
167 }
168 } else {
169 if txn.Monitor(timeout) {
khenaidoo6d62c002019-05-15 21:57:03 -0400170 log.Debugw("acquired-transaction-after-timeout", log.Fields{"timeout": timeout, "waited-time": time.Since(t)})
Richard Jankowski199fd862019-03-18 14:49:51 -0400171 return txn, nil
172 } else {
khenaidoo6d62c002019-05-15 21:57:03 -0400173 log.Debugw("transaction-completed-by-other", log.Fields{"timeout": timeout, "waited-time": time.Since(t)})
174 return nil, errors.New(string(COMPLETED_BY_OTHER))
Richard Jankowski199fd862019-03-18 14:49:51 -0400175 }
176 }
177}
178
khenaidoo4d4802d2018-10-04 21:59:49 -0400179// waitForNilResponseOnSuccess is a helper function to wait for a response on channel ch where an nil
180// response is expected in a successful scenario
181func waitForNilResponseOnSuccess(ctx context.Context, ch chan interface{}) (*empty.Empty, error) {
182 select {
183 case res := <-ch:
184 if res == nil {
185 return new(empty.Empty), nil
186 } else if err, ok := res.(error); ok {
187 return new(empty.Empty), err
188 } else {
189 log.Warnw("unexpected-return-type", log.Fields{"result": res})
190 err = status.Errorf(codes.Internal, "%s", res)
191 return new(empty.Empty), err
192 }
193 case <-ctx.Done():
194 log.Debug("client-timeout")
195 return nil, ctx.Err()
196 }
197}
198
khenaidoobf6e7bb2018-08-14 22:27:29 -0400199func (handler *APIHandler) UpdateLogLevel(ctx context.Context, logging *voltha.Logging) (*empty.Empty, error) {
khenaidoo6f2fbe32019-01-18 16:16:50 -0500200 log.Debugw("UpdateLogLevel-request", log.Fields{"package": logging.PackageName, "intval": int(logging.Level)})
khenaidoo92e62c52018-10-03 14:02:54 -0400201 out := new(empty.Empty)
khenaidoo6f2fbe32019-01-18 16:16:50 -0500202 if logging.PackageName == "" {
203 log.SetAllLogLevel(int(logging.Level))
204 } else {
205 log.SetPackageLogLevel(logging.PackageName, int(logging.Level))
206 }
khenaidoo92e62c52018-10-03 14:02:54 -0400207 return out, nil
khenaidoobf6e7bb2018-08-14 22:27:29 -0400208}
209
khenaidoo54e0ddf2019-02-27 16:21:33 -0500210func (handler *APIHandler) UpdateMembership(ctx context.Context, membership *voltha.Membership) (*empty.Empty, error) {
211 log.Debugw("UpdateMembership-request", log.Fields{"membership": membership})
212 out := new(empty.Empty)
khenaidoo6417b6c2019-03-01 18:18:01 -0500213 if err := handler.core.updateCoreMembership(ctx, membership); err != nil {
khenaidoo54e0ddf2019-02-27 16:21:33 -0500214 return out, err
215 }
216 return out, nil
217}
218
khenaidoo6417b6c2019-03-01 18:18:01 -0500219func (handler *APIHandler) GetMembership(ctx context.Context, empty *empty.Empty) (*voltha.Membership, error) {
220 log.Debug("GetMembership-request")
221 if membership := handler.core.getCoreMembership(ctx); membership != nil {
222 return membership, nil
223 }
224 return &voltha.Membership{}, nil
225}
226
khenaidoo43aa6bd2019-05-29 13:35:13 -0400227func (handler *APIHandler) GetLogicalDevicePort(ctx context.Context, id *voltha.LogicalPortId) (*voltha.LogicalPort, error) {
228 log.Debugw("GetLogicalDevicePort-request", log.Fields{"id": *id})
229
230 if handler.competeForTransaction() {
231 if txn, err := handler.takeRequestOwnership(ctx, &utils.LogicalDeviceID{Id: id.Id}); err != nil {
232 return &voltha.LogicalPort{}, err
233 } else {
234 defer txn.Close()
235 }
236 }
237 return handler.logicalDeviceMgr.getLogicalPort(id)
238}
239
khenaidoobf6e7bb2018-08-14 22:27:29 -0400240func (handler *APIHandler) EnableLogicalDevicePort(ctx context.Context, id *voltha.LogicalPortId) (*empty.Empty, error) {
241 log.Debugw("EnableLogicalDevicePort-request", log.Fields{"id": id, "test": common.TestModeKeys_api_test.String()})
242 if isTestMode(ctx) {
243 out := new(empty.Empty)
244 return out, nil
245 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500246
khenaidoo9cdc1a62019-01-24 21:57:40 -0500247 if handler.competeForTransaction() {
khenaidoo2c6a0992019-04-29 13:46:56 -0400248 if txn, err := handler.takeRequestOwnership(ctx, &utils.LogicalDeviceID{Id: id.Id}); err != nil {
Richard Jankowski2755adf2019-01-17 17:16:48 -0500249 return new(empty.Empty), err
Richard Jankowski2755adf2019-01-17 17:16:48 -0500250 } else {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500251 defer txn.Close()
Richard Jankowski2755adf2019-01-17 17:16:48 -0500252 }
253 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500254
khenaidoo4d4802d2018-10-04 21:59:49 -0400255 ch := make(chan interface{})
256 defer close(ch)
khenaidoo19d7b632018-10-30 10:49:50 -0400257 go handler.logicalDeviceMgr.enableLogicalPort(ctx, id, ch)
khenaidoo4d4802d2018-10-04 21:59:49 -0400258 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400259}
260
261func (handler *APIHandler) DisableLogicalDevicePort(ctx context.Context, id *voltha.LogicalPortId) (*empty.Empty, error) {
262 log.Debugw("DisableLogicalDevicePort-request", log.Fields{"id": id, "test": common.TestModeKeys_api_test.String()})
263 if isTestMode(ctx) {
264 out := new(empty.Empty)
265 return out, nil
266 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500267
khenaidoo9cdc1a62019-01-24 21:57:40 -0500268 if handler.competeForTransaction() {
khenaidoo2c6a0992019-04-29 13:46:56 -0400269 if txn, err := handler.takeRequestOwnership(ctx, &utils.LogicalDeviceID{Id: id.Id}); err != nil {
Richard Jankowski2755adf2019-01-17 17:16:48 -0500270 return new(empty.Empty), err
Richard Jankowski2755adf2019-01-17 17:16:48 -0500271 } else {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500272 defer txn.Close()
Richard Jankowski2755adf2019-01-17 17:16:48 -0500273 }
274 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500275
khenaidoo19d7b632018-10-30 10:49:50 -0400276 ch := make(chan interface{})
277 defer close(ch)
278 go handler.logicalDeviceMgr.disableLogicalPort(ctx, id, ch)
279 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400280}
281
282func (handler *APIHandler) UpdateLogicalDeviceFlowTable(ctx context.Context, flow *openflow_13.FlowTableUpdate) (*empty.Empty, error) {
283 log.Debugw("UpdateLogicalDeviceFlowTable-request", log.Fields{"flow": flow, "test": common.TestModeKeys_api_test.String()})
284 if isTestMode(ctx) {
285 out := new(empty.Empty)
286 return out, nil
287 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500288
khenaidoo9cdc1a62019-01-24 21:57:40 -0500289 if handler.competeForTransaction() {
khenaidoo43aa6bd2019-05-29 13:35:13 -0400290 if txn, err := handler.takeRequestOwnership(ctx, &utils.LogicalDeviceID{Id: flow.Id}); err != nil {
291 return new(empty.Empty), err
292 } else {
293 defer txn.Close()
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() {
khenaidoo43aa6bd2019-05-29 13:35:13 -0400311 if txn, err := handler.takeRequestOwnership(ctx, &utils.LogicalDeviceID{Id: flow.Id}); err != nil {
312 return new(empty.Empty), err
313 } else {
314 defer txn.Close()
Richard Jankowski2755adf2019-01-17 17:16:48 -0500315 }
316 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500317
khenaidoo19d7b632018-10-30 10:49:50 -0400318 ch := make(chan interface{})
319 defer close(ch)
320 go handler.logicalDeviceMgr.updateGroupTable(ctx, flow.Id, flow.GroupMod, ch)
321 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400322}
323
khenaidoob9203542018-09-17 22:56:37 -0400324// GetDevice must be implemented in the read-only containers - should it also be implemented here?
325func (handler *APIHandler) GetDevice(ctx context.Context, id *voltha.ID) (*voltha.Device, error) {
326 log.Debugw("GetDevice-request", log.Fields{"id": id})
khenaidoo19d7b632018-10-30 10:49:50 -0400327 return handler.deviceMgr.GetDevice(id.Id)
khenaidoob9203542018-09-17 22:56:37 -0400328}
329
330// GetDevice must be implemented in the read-only containers - should it also be implemented here?
331func (handler *APIHandler) ListDevices(ctx context.Context, empty *empty.Empty) (*voltha.Devices, error) {
332 log.Debug("ListDevices")
333 return handler.deviceMgr.ListDevices()
334}
335
khenaidoo7ccedd52018-12-14 16:48:54 -0500336// ListDeviceIds returns the list of device ids managed by a voltha core
337func (handler *APIHandler) ListDeviceIds(ctx context.Context, empty *empty.Empty) (*voltha.IDs, error) {
338 log.Debug("ListDeviceIDs")
339 if isTestMode(ctx) {
340 out := &voltha.IDs{Items: make([]*voltha.ID, 0)}
341 return out, nil
342 }
343 return handler.deviceMgr.ListDeviceIds()
344}
345
346//ReconcileDevices is a request to a voltha core to managed a list of devices based on their IDs
347func (handler *APIHandler) ReconcileDevices(ctx context.Context, ids *voltha.IDs) (*empty.Empty, error) {
348 log.Debug("ReconcileDevices")
349 if isTestMode(ctx) {
350 out := new(empty.Empty)
351 return out, nil
352 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500353
khenaidoo9cdc1a62019-01-24 21:57:40 -0500354 // No need to grab a transaction as this request is core specific
355
khenaidoo7ccedd52018-12-14 16:48:54 -0500356 ch := make(chan interface{})
357 defer close(ch)
358 go handler.deviceMgr.ReconcileDevices(ctx, ids, ch)
359 return waitForNilResponseOnSuccess(ctx, ch)
360}
361
khenaidoob9203542018-09-17 22:56:37 -0400362func (handler *APIHandler) GetLogicalDevice(ctx context.Context, id *voltha.ID) (*voltha.LogicalDevice, error) {
363 log.Debugw("GetLogicalDevice-request", log.Fields{"id": id})
khenaidoo43aa6bd2019-05-29 13:35:13 -0400364 if handler.competeForTransaction() {
365 if txn, err := handler.takeRequestOwnership(ctx, &utils.LogicalDeviceID{Id: id.Id}); err != nil {
366 return &voltha.LogicalDevice{}, err
367 } else {
368 defer txn.Close()
369 }
370 }
khenaidoob9203542018-09-17 22:56:37 -0400371 return handler.logicalDeviceMgr.getLogicalDevice(id.Id)
372}
373
khenaidoob9203542018-09-17 22:56:37 -0400374func (handler *APIHandler) ListLogicalDevices(ctx context.Context, empty *empty.Empty) (*voltha.LogicalDevices, error) {
khenaidoo43aa6bd2019-05-29 13:35:13 -0400375 log.Debug("ListLogicalDevices-request")
376 if handler.competeForTransaction() {
khenaidoo631fe542019-05-31 15:44:43 -0400377 if txn, err := handler.acquireRequest(ctx); err != nil {
khenaidoo43aa6bd2019-05-29 13:35:13 -0400378 return &voltha.LogicalDevices{}, err
379 } else {
380 defer txn.Close()
381 }
382 }
383 if handler.isOFControllerRequest(ctx) {
384 // Since an OF controller is only interested in the set of logical devices managed by thgis Core then return
385 // only logical devices managed/monitored by this Core.
386 return handler.logicalDeviceMgr.listManagedLogicalDevices()
387 }
khenaidoob9203542018-09-17 22:56:37 -0400388 return handler.logicalDeviceMgr.listLogicalDevices()
389}
390
khenaidoo21d51152019-02-01 13:48:37 -0500391// ListAdapters returns the contents of all adapters known to the system
392func (handler *APIHandler) ListAdapters(ctx context.Context, empty *empty.Empty) (*voltha.Adapters, error) {
393 log.Debug("ListDevices")
394 return handler.adapterMgr.listAdapters(ctx)
395}
396
khenaidoodd237172019-05-27 16:37:17 -0400397func (handler *APIHandler) ListLogicalDeviceFlows(ctx context.Context, id *voltha.ID) (*openflow_13.Flows, error) {
398 log.Debugw("ListLogicalDeviceFlows", log.Fields{"id": *id})
khenaidoo43aa6bd2019-05-29 13:35:13 -0400399 if handler.competeForTransaction() {
400 if txn, err := handler.takeRequestOwnership(ctx, &utils.LogicalDeviceID{Id: id.Id}); err != nil {
401 return &openflow_13.Flows{}, err
402 } else {
403 defer txn.Close()
404 }
405 }
khenaidoodd237172019-05-27 16:37:17 -0400406 return handler.logicalDeviceMgr.ListLogicalDeviceFlows(ctx, id.Id)
407}
408
409func (handler *APIHandler) ListLogicalDeviceFlowGroups(ctx context.Context, id *voltha.ID) (*openflow_13.FlowGroups, error) {
410 log.Debugw("ListLogicalDeviceFlowGroups", log.Fields{"id": *id})
khenaidoo43aa6bd2019-05-29 13:35:13 -0400411 if handler.competeForTransaction() {
412 if txn, err := handler.takeRequestOwnership(ctx, &utils.LogicalDeviceID{Id: id.Id}); err != nil {
413 return &openflow_13.FlowGroups{}, err
414 } else {
415 defer txn.Close()
416 }
417 }
khenaidoodd237172019-05-27 16:37:17 -0400418 return handler.logicalDeviceMgr.ListLogicalDeviceFlowGroups(ctx, id.Id)
419}
420
khenaidoo19d7b632018-10-30 10:49:50 -0400421func (handler *APIHandler) ListLogicalDevicePorts(ctx context.Context, id *voltha.ID) (*voltha.LogicalPorts, error) {
422 log.Debugw("ListLogicalDevicePorts", log.Fields{"logicaldeviceid": id})
khenaidoo43aa6bd2019-05-29 13:35:13 -0400423 if handler.competeForTransaction() {
424 if txn, err := handler.takeRequestOwnership(ctx, &utils.LogicalDeviceID{Id: id.Id}); err != nil {
425 return &voltha.LogicalPorts{}, err
426 } else {
427 defer txn.Close()
428 }
429 }
khenaidoo19d7b632018-10-30 10:49:50 -0400430 return handler.logicalDeviceMgr.ListLogicalDevicePorts(ctx, id.Id)
431}
432
khenaidoo4d4802d2018-10-04 21:59:49 -0400433// CreateDevice creates a new parent device in the data model
khenaidoobf6e7bb2018-08-14 22:27:29 -0400434func (handler *APIHandler) CreateDevice(ctx context.Context, device *voltha.Device) (*voltha.Device, error) {
khenaidoob9203542018-09-17 22:56:37 -0400435 log.Debugw("createdevice", log.Fields{"device": *device})
khenaidoobf6e7bb2018-08-14 22:27:29 -0400436 if isTestMode(ctx) {
437 return &voltha.Device{Id: device.Id}, nil
438 }
Richard Jankowskid42826e2018-11-02 16:06:37 -0400439
khenaidoo9cdc1a62019-01-24 21:57:40 -0500440 if handler.competeForTransaction() {
khenaidoo631fe542019-05-31 15:44:43 -0400441 // There are no device Id present in this function.
442 if txn, err := handler.acquireRequest(ctx); err != nil {
Richard Jankowski2755adf2019-01-17 17:16:48 -0500443 return &voltha.Device{}, err
Richard Jankowski2755adf2019-01-17 17:16:48 -0500444 } else {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500445 defer txn.Close()
Richard Jankowski2755adf2019-01-17 17:16:48 -0500446 }
447 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500448
khenaidoob9203542018-09-17 22:56:37 -0400449 ch := make(chan interface{})
450 defer close(ch)
451 go handler.deviceMgr.createDevice(ctx, device, ch)
452 select {
453 case res := <-ch:
khenaidoo92e62c52018-10-03 14:02:54 -0400454 if res != nil {
455 if err, ok := res.(error); ok {
456 return &voltha.Device{}, err
457 }
458 if d, ok := res.(*voltha.Device); ok {
khenaidoo2c6a0992019-04-29 13:46:56 -0400459 handler.core.deviceOwnership.OwnedByMe(&utils.DeviceID{Id: d.Id})
khenaidoo92e62c52018-10-03 14:02:54 -0400460 return d, nil
461 }
khenaidoob9203542018-09-17 22:56:37 -0400462 }
khenaidoo92e62c52018-10-03 14:02:54 -0400463 log.Warnw("create-device-unexpected-return-type", log.Fields{"result": res})
464 err := status.Errorf(codes.Internal, "%s", res)
465 return &voltha.Device{}, err
khenaidoob9203542018-09-17 22:56:37 -0400466 case <-ctx.Done():
467 log.Debug("createdevice-client-timeout")
468 return nil, ctx.Err()
469 }
khenaidoobf6e7bb2018-08-14 22:27:29 -0400470}
471
khenaidoo4d4802d2018-10-04 21:59:49 -0400472// EnableDevice activates a device by invoking the adopt_device API on the appropriate adapter
khenaidoobf6e7bb2018-08-14 22:27:29 -0400473func (handler *APIHandler) EnableDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) {
khenaidoob9203542018-09-17 22:56:37 -0400474 log.Debugw("enabledevice", log.Fields{"id": id})
khenaidoobf6e7bb2018-08-14 22:27:29 -0400475 if isTestMode(ctx) {
khenaidoo4d4802d2018-10-04 21:59:49 -0400476 return new(empty.Empty), nil
khenaidoobf6e7bb2018-08-14 22:27:29 -0400477 }
Richard Jankowskid42826e2018-11-02 16:06:37 -0400478
khenaidoo9cdc1a62019-01-24 21:57:40 -0500479 if handler.competeForTransaction() {
khenaidoo2c6a0992019-04-29 13:46:56 -0400480 if txn, err := handler.takeRequestOwnership(ctx, &utils.DeviceID{Id: id.Id}, handler.longRunningRequestTimeout); err != nil {
Richard Jankowski2755adf2019-01-17 17:16:48 -0500481 return new(empty.Empty), err
Richard Jankowski2755adf2019-01-17 17:16:48 -0500482 } else {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500483 defer txn.Close()
Richard Jankowski2755adf2019-01-17 17:16:48 -0500484 }
485 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500486
khenaidoob9203542018-09-17 22:56:37 -0400487 ch := make(chan interface{})
488 defer close(ch)
489 go handler.deviceMgr.enableDevice(ctx, id, ch)
khenaidoo4d4802d2018-10-04 21:59:49 -0400490 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400491}
492
khenaidoo4d4802d2018-10-04 21:59:49 -0400493// DisableDevice disables a device along with any child device it may have
khenaidoobf6e7bb2018-08-14 22:27:29 -0400494func (handler *APIHandler) DisableDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) {
495 log.Debugw("disabledevice-request", log.Fields{"id": id})
496 if isTestMode(ctx) {
khenaidoo4d4802d2018-10-04 21:59:49 -0400497 return new(empty.Empty), nil
khenaidoobf6e7bb2018-08-14 22:27:29 -0400498 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500499
khenaidoo9cdc1a62019-01-24 21:57:40 -0500500 if handler.competeForTransaction() {
khenaidoo2c6a0992019-04-29 13:46:56 -0400501 if txn, err := handler.takeRequestOwnership(ctx, &utils.DeviceID{Id: id.Id}); err != nil {
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
khenaidoo92e62c52018-10-03 14:02:54 -0400508 ch := make(chan interface{})
509 defer close(ch)
510 go handler.deviceMgr.disableDevice(ctx, id, ch)
khenaidoo4d4802d2018-10-04 21:59:49 -0400511 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400512}
513
khenaidoo4d4802d2018-10-04 21:59:49 -0400514//RebootDevice invoked the reboot API to the corresponding adapter
khenaidoobf6e7bb2018-08-14 22:27:29 -0400515func (handler *APIHandler) RebootDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) {
khenaidoo4d4802d2018-10-04 21:59:49 -0400516 log.Debugw("rebootDevice-request", log.Fields{"id": id})
khenaidoobf6e7bb2018-08-14 22:27:29 -0400517 if isTestMode(ctx) {
khenaidoo4d4802d2018-10-04 21:59:49 -0400518 return new(empty.Empty), nil
khenaidoobf6e7bb2018-08-14 22:27:29 -0400519 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500520
khenaidoo9cdc1a62019-01-24 21:57:40 -0500521 if handler.competeForTransaction() {
khenaidoo2c6a0992019-04-29 13:46:56 -0400522 if txn, err := handler.takeRequestOwnership(ctx, &utils.DeviceID{Id: id.Id}); err != nil {
Richard Jankowski2755adf2019-01-17 17:16:48 -0500523 return new(empty.Empty), err
Richard Jankowski2755adf2019-01-17 17:16:48 -0500524 } else {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500525 defer txn.Close()
Richard Jankowski2755adf2019-01-17 17:16:48 -0500526 }
527 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500528
khenaidoo4d4802d2018-10-04 21:59:49 -0400529 ch := make(chan interface{})
530 defer close(ch)
531 go handler.deviceMgr.rebootDevice(ctx, id, ch)
532 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400533}
534
khenaidoo4d4802d2018-10-04 21:59:49 -0400535// DeleteDevice removes a device from the data model
khenaidoobf6e7bb2018-08-14 22:27:29 -0400536func (handler *APIHandler) DeleteDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) {
537 log.Debugw("deletedevice-request", log.Fields{"id": id})
538 if isTestMode(ctx) {
khenaidoo4d4802d2018-10-04 21:59:49 -0400539 return new(empty.Empty), nil
khenaidoobf6e7bb2018-08-14 22:27:29 -0400540 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500541
khenaidoo9cdc1a62019-01-24 21:57:40 -0500542 if handler.competeForTransaction() {
khenaidoo6d62c002019-05-15 21:57:03 -0400543 if txn, err := handler.takeRequestOwnership(ctx, &utils.DeviceID{Id: id.Id}); err != nil {
544 // Remove the device in memory
545 if err.Error() == (errors.New(string(COMPLETED_BY_OTHER)).Error()) {
546 handler.deviceMgr.stopManagingDevice(id.Id)
547 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500548 return new(empty.Empty), err
Richard Jankowski2755adf2019-01-17 17:16:48 -0500549 } else {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500550 defer txn.Close()
Richard Jankowski2755adf2019-01-17 17:16:48 -0500551 }
552 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500553
khenaidoo4d4802d2018-10-04 21:59:49 -0400554 ch := make(chan interface{})
555 defer close(ch)
556 go handler.deviceMgr.deleteDevice(ctx, id, ch)
557 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400558}
559
khenaidoof5a5bfa2019-01-23 22:20:29 -0500560// processImageRequest is a helper method to execute an image download request
561func (handler *APIHandler) processImageRequest(ctx context.Context, img *voltha.ImageDownload, requestType int) (*common.OperationResp, error) {
562 log.Debugw("processImageDownload", log.Fields{"img": *img, "requestType": requestType})
563 if isTestMode(ctx) {
564 resp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS}
565 return resp, nil
566 }
567
khenaidoo9cdc1a62019-01-24 21:57:40 -0500568 if handler.competeForTransaction() {
khenaidoo2c6a0992019-04-29 13:46:56 -0400569 if txn, err := handler.takeRequestOwnership(ctx, &utils.DeviceID{Id: img.Id}); err != nil {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500570 return &common.OperationResp{}, err
571 } else {
572 defer txn.Close()
573 }
khenaidoof5a5bfa2019-01-23 22:20:29 -0500574 }
575
khenaidoo2c6a0992019-04-29 13:46:56 -0400576 failedresponse := &common.OperationResp{Code: voltha.OperationResp_OPERATION_FAILURE}
khenaidoof5a5bfa2019-01-23 22:20:29 -0500577
578 ch := make(chan interface{})
579 defer close(ch)
580 switch requestType {
581 case IMAGE_DOWNLOAD:
582 go handler.deviceMgr.downloadImage(ctx, img, ch)
583 case CANCEL_IMAGE_DOWNLOAD:
584 go handler.deviceMgr.cancelImageDownload(ctx, img, ch)
585 case ACTIVATE_IMAGE:
586 go handler.deviceMgr.activateImage(ctx, img, ch)
587 case REVERT_IMAGE:
588 go handler.deviceMgr.revertImage(ctx, img, ch)
589 default:
590 log.Warn("invalid-request-type", log.Fields{"requestType": requestType})
591 return failedresponse, status.Errorf(codes.InvalidArgument, "%d", requestType)
592 }
593 select {
594 case res := <-ch:
595 if res != nil {
596 if err, ok := res.(error); ok {
597 return failedresponse, err
598 }
599 if opResp, ok := res.(*common.OperationResp); ok {
600 return opResp, nil
601 }
602 }
603 log.Warnw("download-image-unexpected-return-type", log.Fields{"result": res})
604 return failedresponse, status.Errorf(codes.Internal, "%s", res)
605 case <-ctx.Done():
606 log.Debug("downloadImage-client-timeout")
607 return nil, ctx.Err()
608 }
609}
610
khenaidoobf6e7bb2018-08-14 22:27:29 -0400611func (handler *APIHandler) DownloadImage(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) {
612 log.Debugw("DownloadImage-request", log.Fields{"img": *img})
613 if isTestMode(ctx) {
614 resp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS}
615 return resp, nil
616 }
617
khenaidoof5a5bfa2019-01-23 22:20:29 -0500618 return handler.processImageRequest(ctx, img, IMAGE_DOWNLOAD)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400619}
620
621func (handler *APIHandler) CancelImageDownload(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) {
khenaidoof5a5bfa2019-01-23 22:20:29 -0500622 log.Debugw("cancelImageDownload-request", log.Fields{"img": *img})
khenaidoobf6e7bb2018-08-14 22:27:29 -0400623 if isTestMode(ctx) {
624 resp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS}
625 return resp, nil
626 }
khenaidoof5a5bfa2019-01-23 22:20:29 -0500627 return handler.processImageRequest(ctx, img, CANCEL_IMAGE_DOWNLOAD)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400628}
629
630func (handler *APIHandler) ActivateImageUpdate(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) {
khenaidoof5a5bfa2019-01-23 22:20:29 -0500631 log.Debugw("activateImageUpdate-request", log.Fields{"img": *img})
khenaidoobf6e7bb2018-08-14 22:27:29 -0400632 if isTestMode(ctx) {
633 resp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS}
634 return resp, nil
635 }
khenaidoof5a5bfa2019-01-23 22:20:29 -0500636
637 return handler.processImageRequest(ctx, img, ACTIVATE_IMAGE)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400638}
639
640func (handler *APIHandler) RevertImageUpdate(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) {
khenaidoof5a5bfa2019-01-23 22:20:29 -0500641 log.Debugw("revertImageUpdate-request", log.Fields{"img": *img})
khenaidoobf6e7bb2018-08-14 22:27:29 -0400642 if isTestMode(ctx) {
643 resp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS}
644 return resp, nil
645 }
khenaidoof5a5bfa2019-01-23 22:20:29 -0500646
647 return handler.processImageRequest(ctx, img, REVERT_IMAGE)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400648}
649
khenaidoof5a5bfa2019-01-23 22:20:29 -0500650func (handler *APIHandler) GetImageDownloadStatus(ctx context.Context, img *voltha.ImageDownload) (*voltha.ImageDownload, error) {
651 log.Debugw("getImageDownloadStatus-request", log.Fields{"img": *img})
652 if isTestMode(ctx) {
Stephane Barbariedf5479f2019-01-29 22:13:00 -0500653 resp := &voltha.ImageDownload{DownloadState: voltha.ImageDownload_DOWNLOAD_SUCCEEDED}
khenaidoof5a5bfa2019-01-23 22:20:29 -0500654 return resp, nil
655 }
656
Stephane Barbariedf5479f2019-01-29 22:13:00 -0500657 failedresponse := &voltha.ImageDownload{DownloadState: voltha.ImageDownload_DOWNLOAD_UNKNOWN}
khenaidoof5a5bfa2019-01-23 22:20:29 -0500658
khenaidoo9cdc1a62019-01-24 21:57:40 -0500659 if handler.competeForTransaction() {
khenaidoo2c6a0992019-04-29 13:46:56 -0400660 if txn, err := handler.takeRequestOwnership(ctx, &utils.DeviceID{Id: img.Id}); err != nil {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500661 return failedresponse, err
662 } else {
663 defer txn.Close()
664 }
khenaidoof5a5bfa2019-01-23 22:20:29 -0500665 }
666
667 ch := make(chan interface{})
668 defer close(ch)
669 go handler.deviceMgr.getImageDownloadStatus(ctx, img, ch)
670
671 select {
672 case res := <-ch:
673 if res != nil {
674 if err, ok := res.(error); ok {
675 return failedresponse, err
676 }
677 if downloadResp, ok := res.(*voltha.ImageDownload); ok {
678 return downloadResp, nil
679 }
680 }
681 log.Warnw("download-image-status", log.Fields{"result": res})
682 return failedresponse, status.Errorf(codes.Internal, "%s", res)
683 case <-ctx.Done():
684 log.Debug("downloadImage-client-timeout")
685 return failedresponse, ctx.Err()
686 }
687}
688
689func (handler *APIHandler) GetImageDownload(ctx context.Context, img *voltha.ImageDownload) (*voltha.ImageDownload, error) {
690 log.Debugw("GetImageDownload-request", log.Fields{"img": *img})
691 if isTestMode(ctx) {
Stephane Barbariedf5479f2019-01-29 22:13:00 -0500692 resp := &voltha.ImageDownload{DownloadState: voltha.ImageDownload_DOWNLOAD_SUCCEEDED}
khenaidoof5a5bfa2019-01-23 22:20:29 -0500693 return resp, nil
694 }
695
696 if download, err := handler.deviceMgr.getImageDownload(ctx, img); err != nil {
Stephane Barbariedf5479f2019-01-29 22:13:00 -0500697 return &voltha.ImageDownload{DownloadState: voltha.ImageDownload_DOWNLOAD_UNKNOWN}, err
khenaidoof5a5bfa2019-01-23 22:20:29 -0500698 } else {
699 return download, nil
700 }
701}
702
703func (handler *APIHandler) ListImageDownloads(ctx context.Context, id *voltha.ID) (*voltha.ImageDownloads, error) {
704 log.Debugw("ListImageDownloads-request", log.Fields{"deviceId": id.Id})
705 if isTestMode(ctx) {
khenaidoo2c6a0992019-04-29 13:46:56 -0400706 resp := &voltha.ImageDownloads{Items: []*voltha.ImageDownload{}}
khenaidoof5a5bfa2019-01-23 22:20:29 -0500707 return resp, nil
708 }
709
710 if downloads, err := handler.deviceMgr.listImageDownloads(ctx, id.Id); err != nil {
711 failedResp := &voltha.ImageDownloads{
khenaidoo2c6a0992019-04-29 13:46:56 -0400712 Items: []*voltha.ImageDownload{
713 {DownloadState: voltha.ImageDownload_DOWNLOAD_UNKNOWN},
714 },
khenaidoof5a5bfa2019-01-23 22:20:29 -0500715 }
716 return failedResp, err
717 } else {
718 return downloads, nil
719 }
720}
721
khenaidoobf6e7bb2018-08-14 22:27:29 -0400722func (handler *APIHandler) UpdateDevicePmConfigs(ctx context.Context, configs *voltha.PmConfigs) (*empty.Empty, error) {
723 log.Debugw("UpdateDevicePmConfigs-request", log.Fields{"configs": *configs})
724 if isTestMode(ctx) {
725 out := new(empty.Empty)
726 return out, nil
727 }
khenaidoob3127472019-07-24 21:04:55 -0400728 if handler.competeForTransaction() {
729 if txn, err := handler.takeRequestOwnership(ctx, &utils.DeviceID{Id: configs.Id}); err != nil {
730 return new(empty.Empty), err
731 } else {
732 defer txn.Close()
733 }
734 }
735
736 ch := make(chan interface{})
737 defer close(ch)
738 go handler.deviceMgr.updatePmConfigs(ctx, configs, ch)
739 return waitForNilResponseOnSuccess(ctx, ch)
740}
741
742func (handler *APIHandler) ListDevicePmConfigs(ctx context.Context, id *voltha.ID) (*voltha.PmConfigs, error) {
743 log.Debugw("ListDevicePmConfigs-request", log.Fields{"deviceId": *id})
744 if handler.competeForTransaction() {
745 if txn, err := handler.takeRequestOwnership(ctx, &utils.LogicalDeviceID{Id: id.Id}); err != nil {
746 return &voltha.PmConfigs{}, err
747 } else {
748 defer txn.Close()
749 }
750 }
751 return handler.deviceMgr.listPmConfigs(ctx, id.Id)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400752}
753
754func (handler *APIHandler) CreateAlarmFilter(ctx context.Context, filter *voltha.AlarmFilter) (*voltha.AlarmFilter, error) {
755 log.Debugw("CreateAlarmFilter-request", log.Fields{"filter": *filter})
756 if isTestMode(ctx) {
757 f := &voltha.AlarmFilter{Id: filter.Id}
758 return f, nil
759 }
760 return nil, errors.New("UnImplemented")
761}
762
763func (handler *APIHandler) UpdateAlarmFilter(ctx context.Context, filter *voltha.AlarmFilter) (*voltha.AlarmFilter, error) {
764 log.Debugw("UpdateAlarmFilter-request", log.Fields{"filter": *filter})
765 if isTestMode(ctx) {
766 f := &voltha.AlarmFilter{Id: filter.Id}
767 return f, nil
768 }
769 return nil, errors.New("UnImplemented")
770}
771
772func (handler *APIHandler) DeleteAlarmFilter(ctx context.Context, id *voltha.ID) (*empty.Empty, error) {
773 log.Debugw("DeleteAlarmFilter-request", log.Fields{"id": *id})
774 if isTestMode(ctx) {
775 out := new(empty.Empty)
776 return out, nil
777 }
778 return nil, errors.New("UnImplemented")
779}
780
781func (handler *APIHandler) SelfTest(ctx context.Context, id *voltha.ID) (*voltha.SelfTestResponse, error) {
782 log.Debugw("SelfTest-request", log.Fields{"id": id})
783 if isTestMode(ctx) {
784 resp := &voltha.SelfTestResponse{Result: voltha.SelfTestResponse_SUCCESS}
785 return resp, nil
786 }
787 return nil, errors.New("UnImplemented")
788}
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500789
790func (handler *APIHandler) forwardPacketOut(packet *openflow_13.PacketOut) {
791 log.Debugw("forwardPacketOut-request", log.Fields{"packet": packet})
khenaidoo3d3b8c22019-05-22 18:10:39 -0400792 //TODO: Update this logic once the OF Controller (OFAgent in this case) can include a transaction Id in its
793 // request. For performance reason we can let both Cores in a Core-Pair forward the Packet to the adapters and
794 // let once of the shim layer (kafka proxy or adapter request handler filters out the duplicate packet)
795 if handler.core.deviceOwnership.OwnedByMe(&utils.LogicalDeviceID{Id: packet.Id}) {
796 agent := handler.logicalDeviceMgr.getLogicalDeviceAgent(packet.Id)
797 agent.packetOut(packet.PacketOut)
798 }
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500799}
khenaidoo3d3b8c22019-05-22 18:10:39 -0400800
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500801func (handler *APIHandler) StreamPacketsOut(
802 packets voltha.VolthaService_StreamPacketsOutServer,
803) error {
804 log.Debugw("StreamPacketsOut-request", log.Fields{"packets": packets})
805 for {
806 packet, err := packets.Recv()
807
808 if err == io.EOF {
809 break
810 } else if err != nil {
811 log.Errorw("Failed to receive packet", log.Fields{"error": err})
812 }
813
814 handler.forwardPacketOut(packet)
815 }
816
817 log.Debugw("StreamPacketsOut-request-done", log.Fields{"packets": packets})
818 return nil
819}
820
khenaidoo297cd252019-02-07 22:10:23 -0500821func (handler *APIHandler) sendPacketIn(deviceId string, transationId string, packet *openflow_13.OfpPacketIn) {
822 // TODO: Augment the OF PacketIn to include the transactionId
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500823 packetIn := openflow_13.PacketIn{Id: deviceId, PacketIn: packet}
824 log.Debugw("sendPacketIn", log.Fields{"packetIn": packetIn})
Richard Jankowskidbab94a2018-12-06 16:20:25 -0500825 // Enqueue the packet
826 if err := handler.packetInQueue.Put(packetIn); err != nil {
827 log.Errorw("failed-to-enqueue-packet", log.Fields{"error": err})
828 }
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500829}
830
831func (handler *APIHandler) ReceivePacketsIn(
832 empty *empty.Empty,
833 packetsIn voltha.VolthaService_ReceivePacketsInServer,
834) error {
835 log.Debugw("ReceivePacketsIn-request", log.Fields{"packetsIn": packetsIn})
836
837 for {
Richard Jankowskidbab94a2018-12-06 16:20:25 -0500838 // Dequeue a packet
839 if packets, err := handler.packetInQueue.Get(1); err == nil {
840 log.Debugw("dequeued-packet", log.Fields{"packet": packets[0]})
841 if packet, ok := packets[0].(openflow_13.PacketIn); ok {
842 log.Debugw("sending-packet-in", log.Fields{"packet": packet})
843 if err := packetsIn.Send(&packet); err != nil {
844 log.Errorw("failed-to-send-packet", log.Fields{"error": err})
845 }
846 }
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500847 }
848 }
khenaidoof5a5bfa2019-01-23 22:20:29 -0500849 //TODO: FInd an elegant way to get out of the above loop when the Core is stopped
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500850}
851
852func (handler *APIHandler) sendChangeEvent(deviceId string, portStatus *openflow_13.OfpPortStatus) {
853 // TODO: validate the type of portStatus parameter
854 //if _, ok := portStatus.(*openflow_13.OfpPortStatus); ok {
855 //}
856 event := openflow_13.ChangeEvent{Id: deviceId, Event: &openflow_13.ChangeEvent_PortStatus{PortStatus: portStatus}}
857 log.Debugw("sendChangeEvent", log.Fields{"event": event})
Richard Jankowski199fd862019-03-18 14:49:51 -0400858 // Enqueue the change event
859 if err := handler.changeEventQueue.Put(event); err != nil {
860 log.Errorw("failed-to-enqueue-change-event", log.Fields{"error": err})
861 }
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500862}
863
864func (handler *APIHandler) ReceiveChangeEvents(
865 empty *empty.Empty,
866 changeEvents voltha.VolthaService_ReceiveChangeEventsServer,
867) error {
868 log.Debugw("ReceiveChangeEvents-request", log.Fields{"changeEvents": changeEvents})
869 for {
Richard Jankowski199fd862019-03-18 14:49:51 -0400870 // Dequeue a change event
871 if events, err := handler.changeEventQueue.Get(1); err == nil {
872 log.Debugw("dequeued-change-event", log.Fields{"event": events[0]})
873 if event, ok := events[0].(openflow_13.ChangeEvent); ok {
874 log.Debugw("sending-change-event", log.Fields{"event": event})
875 if err := changeEvents.Send(&event); err != nil {
876 log.Errorw("failed-to-send-change-event", log.Fields{"error": err})
877 }
878 }
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500879 }
880 }
Richard Jankowski199fd862019-03-18 14:49:51 -0400881}
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500882
883func (handler *APIHandler) Subscribe(
884 ctx context.Context,
885 ofAgent *voltha.OfAgentSubscriber,
886) (*voltha.OfAgentSubscriber, error) {
887 log.Debugw("Subscribe-request", log.Fields{"ofAgent": ofAgent})
888 return &voltha.OfAgentSubscriber{OfagentId: ofAgent.OfagentId, VolthaId: ofAgent.VolthaId}, nil
889}
William Kurkiandaa6bb22019-03-07 12:26:28 -0500890
891//@TODO useless stub, what should this actually do?
892func (handler *APIHandler) GetAlarmDeviceData(
893 ctx context.Context,
894 in *common.ID,
895) (*omci.AlarmDeviceData, error) {
896 log.Debug("GetAlarmDeviceData-stub")
897 return nil, nil
898}
899
900//@TODO useless stub, what should this actually do?
901func (handler *APIHandler) GetMeterStatsOfLogicalDevice(
khenaidoo2c6a0992019-04-29 13:46:56 -0400902 ctx context.Context,
William Kurkiandaa6bb22019-03-07 12:26:28 -0500903 in *common.ID,
904) (*openflow_13.MeterStatsReply, error) {
905 log.Debug("GetMeterStatsOfLogicalDevice-stub")
906 return nil, nil
907}
908
909//@TODO useless stub, what should this actually do?
910func (handler *APIHandler) GetMibDeviceData(
khenaidoo2c6a0992019-04-29 13:46:56 -0400911 ctx context.Context,
912 in *common.ID,
William Kurkiandaa6bb22019-03-07 12:26:28 -0500913) (*omci.MibDeviceData, error) {
914 log.Debug("GetMibDeviceData-stub")
915 return nil, nil
916}
917
William Kurkiandaa6bb22019-03-07 12:26:28 -0500918func (handler *APIHandler) SimulateAlarm(
919 ctx context.Context,
920 in *voltha.SimulateAlarmRequest,
921) (*common.OperationResp, error) {
serkant.uluderya334479d2019-04-10 08:26:15 -0700922 log.Debugw("SimulateAlarm-request", log.Fields{"id": in.Id})
923 successResp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS}
924 if isTestMode(ctx) {
925 return successResp, nil
926 }
927
928 if handler.competeForTransaction() {
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400929 if txn, err := handler.takeRequestOwnership(ctx, &utils.DeviceID{Id: in.Id}, handler.longRunningRequestTimeout); err != nil {
930 failedresponse := &common.OperationResp{Code: voltha.OperationResp_OPERATION_FAILURE}
serkant.uluderya334479d2019-04-10 08:26:15 -0700931 return failedresponse, err
932 } else {
933 defer txn.Close()
934 }
935 }
936
937 ch := make(chan interface{})
938 defer close(ch)
939 go handler.deviceMgr.simulateAlarm(ctx, in, ch)
940 return successResp, nil
William Kurkiandaa6bb22019-03-07 12:26:28 -0500941}
942
943//@TODO useless stub, what should this actually do?
944func (handler *APIHandler) UpdateLogicalDeviceMeterTable(
945 ctx context.Context,
946 in *openflow_13.MeterModUpdate,
947) (*empty.Empty, error) {
948 log.Debug("UpdateLogicalDeviceMeterTable-stub")
949 return nil, nil
950}