blob: 8f7b328054bcaf53a1b404a5d88aa15b13c9bb6d [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
khenaidoo43aa6bd2019-05-29 13:35:13 -0400124// acquireRequestForList handles transaction processing for list requests, i.e. when there are no specific id requested.
125func (handler *APIHandler) acquireRequestForList(ctx context.Context, maxTimeout ...int64) (*KVTransaction, error) {
126 timeout := handler.defaultRequestTimeout
127 if len(maxTimeout) > 0 {
128 timeout = maxTimeout[0]
129 }
130 log.Debugw("transaction-timeout", log.Fields{"timeout": timeout})
131 txn, err := handler.createKvTransaction(ctx)
132 if txn == nil {
133 return nil, err
134 } else if txn.Acquired(timeout) {
135 return txn, nil
136 } else {
137 return nil, errors.New("failed-to-seize-request")
138 }
139}
140
141// acquireRequest handles transaction processing for creation of new devices
Richard Jankowski199fd862019-03-18 14:49:51 -0400142func (handler *APIHandler) acquireRequest(ctx context.Context, id interface{}, maxTimeout ...int64) (*KVTransaction, error) {
khenaidoob6080322019-01-29 21:47:38 -0500143 timeout := handler.defaultRequestTimeout
khenaidoo9cdc1a62019-01-24 21:57:40 -0500144 if len(maxTimeout) > 0 {
145 timeout = maxTimeout[0]
Richard Jankowski2755adf2019-01-17 17:16:48 -0500146 }
khenaidoob6080322019-01-29 21:47:38 -0500147 log.Debugw("transaction-timeout", log.Fields{"timeout": timeout})
khenaidoo9cdc1a62019-01-24 21:57:40 -0500148 txn, err := handler.createKvTransaction(ctx)
149 if txn == nil {
khenaidoo2c6a0992019-04-29 13:46:56 -0400150 return nil, err
khenaidoo9cdc1a62019-01-24 21:57:40 -0500151 } else if txn.Acquired(timeout) {
152 return txn, nil
153 } else {
khenaidoo297cd252019-02-07 22:10:23 -0500154 if id != nil {
155 // The id can either be a device Id or a logical device id.
khenaidoo1ce37ad2019-03-24 22:07:24 -0400156 if dId, ok := id.(*utils.DeviceID); ok {
khenaidoo297cd252019-02-07 22:10:23 -0500157 // Since this core has not processed this request, let's load the device, along with its extended
158 // family (parents and children) in memory. This will keep this core in-sync with its paired core as
159 // much as possible. The watch feature in the core model will ensure that the contents of those objects in
160 // memory are in sync.
161 time.Sleep(2 * time.Second)
khenaidoo1ce37ad2019-03-24 22:07:24 -0400162 go handler.deviceMgr.load(dId.Id)
163 } else if ldId, ok := id.(*utils.LogicalDeviceID); ok {
khenaidoo297cd252019-02-07 22:10:23 -0500164 // This will load the logical device along with its children and grandchildren
khenaidoo1ce37ad2019-03-24 22:07:24 -0400165 go handler.logicalDeviceMgr.load(ldId.Id)
khenaidoo297cd252019-02-07 22:10:23 -0500166 }
167 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500168 return nil, errors.New("failed-to-seize-request")
Richard Jankowski2755adf2019-01-17 17:16:48 -0500169 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500170}
171
khenaidoo6d62c002019-05-15 21:57:03 -0400172// takeRequestOwnership creates a transaction in the dB for this request and handles the logic of transaction
173// acquisition. If the device is owned by this Core (in a core-pair) then acquire the transaction with a
174// timeout value (in the event of a timeout the other Core in the core-pair will proceed with the transaction). If the
175// device is not owned then this Core will just monitor the transaction for potential timeouts.
Richard Jankowski199fd862019-03-18 14:49:51 -0400176func (handler *APIHandler) takeRequestOwnership(ctx context.Context, id interface{}, maxTimeout ...int64) (*KVTransaction, error) {
khenaidoo6d62c002019-05-15 21:57:03 -0400177 t := time.Now()
Richard Jankowski199fd862019-03-18 14:49:51 -0400178 timeout := handler.defaultRequestTimeout
179 if len(maxTimeout) > 0 {
180 timeout = maxTimeout[0]
181 }
Richard Jankowski199fd862019-03-18 14:49:51 -0400182 txn, err := handler.createKvTransaction(ctx)
183 if txn == nil {
khenaidoo2c6a0992019-04-29 13:46:56 -0400184 return nil, err
Richard Jankowski199fd862019-03-18 14:49:51 -0400185 }
186
187 owned := false
188 if id != nil {
khenaidoo1ce37ad2019-03-24 22:07:24 -0400189 owned = handler.core.deviceOwnership.OwnedByMe(id)
Richard Jankowski199fd862019-03-18 14:49:51 -0400190 }
191 if owned {
192 if txn.Acquired(timeout) {
khenaidoo6d62c002019-05-15 21:57:03 -0400193 log.Debugw("acquired-transaction", log.Fields{"transaction-timeout": timeout})
Richard Jankowski199fd862019-03-18 14:49:51 -0400194 return txn, nil
195 } else {
196 return nil, errors.New("failed-to-seize-request")
197 }
198 } else {
199 if txn.Monitor(timeout) {
khenaidoo6d62c002019-05-15 21:57:03 -0400200 log.Debugw("acquired-transaction-after-timeout", log.Fields{"timeout": timeout, "waited-time": time.Since(t)})
Richard Jankowski199fd862019-03-18 14:49:51 -0400201 return txn, nil
202 } else {
khenaidoo6d62c002019-05-15 21:57:03 -0400203 log.Debugw("transaction-completed-by-other", log.Fields{"timeout": timeout, "waited-time": time.Since(t)})
204 return nil, errors.New(string(COMPLETED_BY_OTHER))
Richard Jankowski199fd862019-03-18 14:49:51 -0400205 }
206 }
207}
208
khenaidoo4d4802d2018-10-04 21:59:49 -0400209// waitForNilResponseOnSuccess is a helper function to wait for a response on channel ch where an nil
210// response is expected in a successful scenario
211func waitForNilResponseOnSuccess(ctx context.Context, ch chan interface{}) (*empty.Empty, error) {
212 select {
213 case res := <-ch:
214 if res == nil {
215 return new(empty.Empty), nil
216 } else if err, ok := res.(error); ok {
217 return new(empty.Empty), err
218 } else {
219 log.Warnw("unexpected-return-type", log.Fields{"result": res})
220 err = status.Errorf(codes.Internal, "%s", res)
221 return new(empty.Empty), err
222 }
223 case <-ctx.Done():
224 log.Debug("client-timeout")
225 return nil, ctx.Err()
226 }
227}
228
khenaidoobf6e7bb2018-08-14 22:27:29 -0400229func (handler *APIHandler) UpdateLogLevel(ctx context.Context, logging *voltha.Logging) (*empty.Empty, error) {
khenaidoo6f2fbe32019-01-18 16:16:50 -0500230 log.Debugw("UpdateLogLevel-request", log.Fields{"package": logging.PackageName, "intval": int(logging.Level)})
khenaidoo92e62c52018-10-03 14:02:54 -0400231 out := new(empty.Empty)
khenaidoo6f2fbe32019-01-18 16:16:50 -0500232 if logging.PackageName == "" {
233 log.SetAllLogLevel(int(logging.Level))
234 } else {
235 log.SetPackageLogLevel(logging.PackageName, int(logging.Level))
236 }
khenaidoo92e62c52018-10-03 14:02:54 -0400237 return out, nil
khenaidoobf6e7bb2018-08-14 22:27:29 -0400238}
239
khenaidoo54e0ddf2019-02-27 16:21:33 -0500240func (handler *APIHandler) UpdateMembership(ctx context.Context, membership *voltha.Membership) (*empty.Empty, error) {
241 log.Debugw("UpdateMembership-request", log.Fields{"membership": membership})
242 out := new(empty.Empty)
khenaidoo6417b6c2019-03-01 18:18:01 -0500243 if err := handler.core.updateCoreMembership(ctx, membership); err != nil {
khenaidoo54e0ddf2019-02-27 16:21:33 -0500244 return out, err
245 }
246 return out, nil
247}
248
khenaidoo6417b6c2019-03-01 18:18:01 -0500249func (handler *APIHandler) GetMembership(ctx context.Context, empty *empty.Empty) (*voltha.Membership, error) {
250 log.Debug("GetMembership-request")
251 if membership := handler.core.getCoreMembership(ctx); membership != nil {
252 return membership, nil
253 }
254 return &voltha.Membership{}, nil
255}
256
khenaidoo43aa6bd2019-05-29 13:35:13 -0400257func (handler *APIHandler) GetLogicalDevicePort(ctx context.Context, id *voltha.LogicalPortId) (*voltha.LogicalPort, error) {
258 log.Debugw("GetLogicalDevicePort-request", log.Fields{"id": *id})
259
260 if handler.competeForTransaction() {
261 if txn, err := handler.takeRequestOwnership(ctx, &utils.LogicalDeviceID{Id: id.Id}); err != nil {
262 return &voltha.LogicalPort{}, err
263 } else {
264 defer txn.Close()
265 }
266 }
267 return handler.logicalDeviceMgr.getLogicalPort(id)
268}
269
khenaidoobf6e7bb2018-08-14 22:27:29 -0400270func (handler *APIHandler) EnableLogicalDevicePort(ctx context.Context, id *voltha.LogicalPortId) (*empty.Empty, error) {
271 log.Debugw("EnableLogicalDevicePort-request", log.Fields{"id": id, "test": common.TestModeKeys_api_test.String()})
272 if isTestMode(ctx) {
273 out := new(empty.Empty)
274 return out, nil
275 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500276
khenaidoo9cdc1a62019-01-24 21:57:40 -0500277 if handler.competeForTransaction() {
khenaidoo2c6a0992019-04-29 13:46:56 -0400278 if txn, err := handler.takeRequestOwnership(ctx, &utils.LogicalDeviceID{Id: id.Id}); err != nil {
Richard Jankowski2755adf2019-01-17 17:16:48 -0500279 return new(empty.Empty), err
Richard Jankowski2755adf2019-01-17 17:16:48 -0500280 } else {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500281 defer txn.Close()
Richard Jankowski2755adf2019-01-17 17:16:48 -0500282 }
283 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500284
khenaidoo4d4802d2018-10-04 21:59:49 -0400285 ch := make(chan interface{})
286 defer close(ch)
khenaidoo19d7b632018-10-30 10:49:50 -0400287 go handler.logicalDeviceMgr.enableLogicalPort(ctx, id, ch)
khenaidoo4d4802d2018-10-04 21:59:49 -0400288 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400289}
290
291func (handler *APIHandler) DisableLogicalDevicePort(ctx context.Context, id *voltha.LogicalPortId) (*empty.Empty, error) {
292 log.Debugw("DisableLogicalDevicePort-request", log.Fields{"id": id, "test": common.TestModeKeys_api_test.String()})
293 if isTestMode(ctx) {
294 out := new(empty.Empty)
295 return out, nil
296 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500297
khenaidoo9cdc1a62019-01-24 21:57:40 -0500298 if handler.competeForTransaction() {
khenaidoo2c6a0992019-04-29 13:46:56 -0400299 if txn, err := handler.takeRequestOwnership(ctx, &utils.LogicalDeviceID{Id: id.Id}); err != nil {
Richard Jankowski2755adf2019-01-17 17:16:48 -0500300 return new(empty.Empty), err
Richard Jankowski2755adf2019-01-17 17:16:48 -0500301 } else {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500302 defer txn.Close()
Richard Jankowski2755adf2019-01-17 17:16:48 -0500303 }
304 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500305
khenaidoo19d7b632018-10-30 10:49:50 -0400306 ch := make(chan interface{})
307 defer close(ch)
308 go handler.logicalDeviceMgr.disableLogicalPort(ctx, id, ch)
309 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400310}
311
312func (handler *APIHandler) UpdateLogicalDeviceFlowTable(ctx context.Context, flow *openflow_13.FlowTableUpdate) (*empty.Empty, error) {
313 log.Debugw("UpdateLogicalDeviceFlowTable-request", log.Fields{"flow": flow, "test": common.TestModeKeys_api_test.String()})
314 if isTestMode(ctx) {
315 out := new(empty.Empty)
316 return out, nil
317 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500318
khenaidoo9cdc1a62019-01-24 21:57:40 -0500319 if handler.competeForTransaction() {
khenaidoo43aa6bd2019-05-29 13:35:13 -0400320 if txn, err := handler.takeRequestOwnership(ctx, &utils.LogicalDeviceID{Id: flow.Id}); err != nil {
321 return new(empty.Empty), err
322 } else {
323 defer txn.Close()
Richard Jankowski2755adf2019-01-17 17:16:48 -0500324 }
325 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500326
khenaidoo19d7b632018-10-30 10:49:50 -0400327 ch := make(chan interface{})
328 defer close(ch)
329 go handler.logicalDeviceMgr.updateFlowTable(ctx, flow.Id, flow.FlowMod, ch)
330 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400331}
332
333func (handler *APIHandler) UpdateLogicalDeviceFlowGroupTable(ctx context.Context, flow *openflow_13.FlowGroupTableUpdate) (*empty.Empty, error) {
334 log.Debugw("UpdateLogicalDeviceFlowGroupTable-request", log.Fields{"flow": flow, "test": common.TestModeKeys_api_test.String()})
335 if isTestMode(ctx) {
336 out := new(empty.Empty)
337 return out, nil
338 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500339
khenaidoo9cdc1a62019-01-24 21:57:40 -0500340 if handler.competeForTransaction() {
khenaidoo43aa6bd2019-05-29 13:35:13 -0400341 if txn, err := handler.takeRequestOwnership(ctx, &utils.LogicalDeviceID{Id: flow.Id}); err != nil {
342 return new(empty.Empty), err
343 } else {
344 defer txn.Close()
Richard Jankowski2755adf2019-01-17 17:16:48 -0500345 }
346 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500347
khenaidoo19d7b632018-10-30 10:49:50 -0400348 ch := make(chan interface{})
349 defer close(ch)
350 go handler.logicalDeviceMgr.updateGroupTable(ctx, flow.Id, flow.GroupMod, ch)
351 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400352}
353
khenaidoob9203542018-09-17 22:56:37 -0400354// GetDevice must be implemented in the read-only containers - should it also be implemented here?
355func (handler *APIHandler) GetDevice(ctx context.Context, id *voltha.ID) (*voltha.Device, error) {
356 log.Debugw("GetDevice-request", log.Fields{"id": id})
khenaidoo19d7b632018-10-30 10:49:50 -0400357 return handler.deviceMgr.GetDevice(id.Id)
khenaidoob9203542018-09-17 22:56:37 -0400358}
359
360// GetDevice must be implemented in the read-only containers - should it also be implemented here?
361func (handler *APIHandler) ListDevices(ctx context.Context, empty *empty.Empty) (*voltha.Devices, error) {
362 log.Debug("ListDevices")
363 return handler.deviceMgr.ListDevices()
364}
365
khenaidoo7ccedd52018-12-14 16:48:54 -0500366// ListDeviceIds returns the list of device ids managed by a voltha core
367func (handler *APIHandler) ListDeviceIds(ctx context.Context, empty *empty.Empty) (*voltha.IDs, error) {
368 log.Debug("ListDeviceIDs")
369 if isTestMode(ctx) {
370 out := &voltha.IDs{Items: make([]*voltha.ID, 0)}
371 return out, nil
372 }
373 return handler.deviceMgr.ListDeviceIds()
374}
375
376//ReconcileDevices is a request to a voltha core to managed a list of devices based on their IDs
377func (handler *APIHandler) ReconcileDevices(ctx context.Context, ids *voltha.IDs) (*empty.Empty, error) {
378 log.Debug("ReconcileDevices")
379 if isTestMode(ctx) {
380 out := new(empty.Empty)
381 return out, nil
382 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500383
khenaidoo9cdc1a62019-01-24 21:57:40 -0500384 // No need to grab a transaction as this request is core specific
385
khenaidoo7ccedd52018-12-14 16:48:54 -0500386 ch := make(chan interface{})
387 defer close(ch)
388 go handler.deviceMgr.ReconcileDevices(ctx, ids, ch)
389 return waitForNilResponseOnSuccess(ctx, ch)
390}
391
khenaidoob9203542018-09-17 22:56:37 -0400392func (handler *APIHandler) GetLogicalDevice(ctx context.Context, id *voltha.ID) (*voltha.LogicalDevice, error) {
393 log.Debugw("GetLogicalDevice-request", log.Fields{"id": id})
khenaidoo43aa6bd2019-05-29 13:35:13 -0400394 if handler.competeForTransaction() {
395 if txn, err := handler.takeRequestOwnership(ctx, &utils.LogicalDeviceID{Id: id.Id}); err != nil {
396 return &voltha.LogicalDevice{}, err
397 } else {
398 defer txn.Close()
399 }
400 }
khenaidoob9203542018-09-17 22:56:37 -0400401 return handler.logicalDeviceMgr.getLogicalDevice(id.Id)
402}
403
khenaidoob9203542018-09-17 22:56:37 -0400404func (handler *APIHandler) ListLogicalDevices(ctx context.Context, empty *empty.Empty) (*voltha.LogicalDevices, error) {
khenaidoo43aa6bd2019-05-29 13:35:13 -0400405 log.Debug("ListLogicalDevices-request")
406 if handler.competeForTransaction() {
407 if txn, err := handler.acquireRequestForList(ctx); err != nil {
408 return &voltha.LogicalDevices{}, err
409 } else {
410 defer txn.Close()
411 }
412 }
413 if handler.isOFControllerRequest(ctx) {
414 // Since an OF controller is only interested in the set of logical devices managed by thgis Core then return
415 // only logical devices managed/monitored by this Core.
416 return handler.logicalDeviceMgr.listManagedLogicalDevices()
417 }
khenaidoob9203542018-09-17 22:56:37 -0400418 return handler.logicalDeviceMgr.listLogicalDevices()
419}
420
khenaidoo21d51152019-02-01 13:48:37 -0500421// ListAdapters returns the contents of all adapters known to the system
422func (handler *APIHandler) ListAdapters(ctx context.Context, empty *empty.Empty) (*voltha.Adapters, error) {
423 log.Debug("ListDevices")
424 return handler.adapterMgr.listAdapters(ctx)
425}
426
khenaidoodd237172019-05-27 16:37:17 -0400427func (handler *APIHandler) ListLogicalDeviceFlows(ctx context.Context, id *voltha.ID) (*openflow_13.Flows, error) {
428 log.Debugw("ListLogicalDeviceFlows", log.Fields{"id": *id})
khenaidoo43aa6bd2019-05-29 13:35:13 -0400429 if handler.competeForTransaction() {
430 if txn, err := handler.takeRequestOwnership(ctx, &utils.LogicalDeviceID{Id: id.Id}); err != nil {
431 return &openflow_13.Flows{}, err
432 } else {
433 defer txn.Close()
434 }
435 }
khenaidoodd237172019-05-27 16:37:17 -0400436 return handler.logicalDeviceMgr.ListLogicalDeviceFlows(ctx, id.Id)
437}
438
439func (handler *APIHandler) ListLogicalDeviceFlowGroups(ctx context.Context, id *voltha.ID) (*openflow_13.FlowGroups, error) {
440 log.Debugw("ListLogicalDeviceFlowGroups", log.Fields{"id": *id})
khenaidoo43aa6bd2019-05-29 13:35:13 -0400441 if handler.competeForTransaction() {
442 if txn, err := handler.takeRequestOwnership(ctx, &utils.LogicalDeviceID{Id: id.Id}); err != nil {
443 return &openflow_13.FlowGroups{}, err
444 } else {
445 defer txn.Close()
446 }
447 }
khenaidoodd237172019-05-27 16:37:17 -0400448 return handler.logicalDeviceMgr.ListLogicalDeviceFlowGroups(ctx, id.Id)
449}
450
khenaidoo19d7b632018-10-30 10:49:50 -0400451func (handler *APIHandler) ListLogicalDevicePorts(ctx context.Context, id *voltha.ID) (*voltha.LogicalPorts, error) {
452 log.Debugw("ListLogicalDevicePorts", log.Fields{"logicaldeviceid": id})
khenaidoo43aa6bd2019-05-29 13:35:13 -0400453 if handler.competeForTransaction() {
454 if txn, err := handler.takeRequestOwnership(ctx, &utils.LogicalDeviceID{Id: id.Id}); err != nil {
455 return &voltha.LogicalPorts{}, err
456 } else {
457 defer txn.Close()
458 }
459 }
khenaidoo19d7b632018-10-30 10:49:50 -0400460 return handler.logicalDeviceMgr.ListLogicalDevicePorts(ctx, id.Id)
461}
462
khenaidoo4d4802d2018-10-04 21:59:49 -0400463// CreateDevice creates a new parent device in the data model
khenaidoobf6e7bb2018-08-14 22:27:29 -0400464func (handler *APIHandler) CreateDevice(ctx context.Context, device *voltha.Device) (*voltha.Device, error) {
khenaidoob9203542018-09-17 22:56:37 -0400465 log.Debugw("createdevice", log.Fields{"device": *device})
khenaidoobf6e7bb2018-08-14 22:27:29 -0400466 if isTestMode(ctx) {
467 return &voltha.Device{Id: device.Id}, nil
468 }
Richard Jankowskid42826e2018-11-02 16:06:37 -0400469
khenaidoo9cdc1a62019-01-24 21:57:40 -0500470 if handler.competeForTransaction() {
khenaidoo43aa6bd2019-05-29 13:35:13 -0400471 if txn, err := handler.acquireRequest(ctx, &utils.DeviceID{Id: device.Id}); err != nil {
Richard Jankowski2755adf2019-01-17 17:16:48 -0500472 return &voltha.Device{}, err
Richard Jankowski2755adf2019-01-17 17:16:48 -0500473 } else {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500474 defer txn.Close()
Richard Jankowski2755adf2019-01-17 17:16:48 -0500475 }
476 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500477
khenaidoob9203542018-09-17 22:56:37 -0400478 ch := make(chan interface{})
479 defer close(ch)
480 go handler.deviceMgr.createDevice(ctx, device, ch)
481 select {
482 case res := <-ch:
khenaidoo92e62c52018-10-03 14:02:54 -0400483 if res != nil {
484 if err, ok := res.(error); ok {
485 return &voltha.Device{}, err
486 }
487 if d, ok := res.(*voltha.Device); ok {
khenaidoo2c6a0992019-04-29 13:46:56 -0400488 handler.core.deviceOwnership.OwnedByMe(&utils.DeviceID{Id: d.Id})
khenaidoo92e62c52018-10-03 14:02:54 -0400489 return d, nil
490 }
khenaidoob9203542018-09-17 22:56:37 -0400491 }
khenaidoo92e62c52018-10-03 14:02:54 -0400492 log.Warnw("create-device-unexpected-return-type", log.Fields{"result": res})
493 err := status.Errorf(codes.Internal, "%s", res)
494 return &voltha.Device{}, err
khenaidoob9203542018-09-17 22:56:37 -0400495 case <-ctx.Done():
496 log.Debug("createdevice-client-timeout")
497 return nil, ctx.Err()
498 }
khenaidoobf6e7bb2018-08-14 22:27:29 -0400499}
500
khenaidoo4d4802d2018-10-04 21:59:49 -0400501// EnableDevice activates a device by invoking the adopt_device API on the appropriate adapter
khenaidoobf6e7bb2018-08-14 22:27:29 -0400502func (handler *APIHandler) EnableDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) {
khenaidoob9203542018-09-17 22:56:37 -0400503 log.Debugw("enabledevice", log.Fields{"id": id})
khenaidoobf6e7bb2018-08-14 22:27:29 -0400504 if isTestMode(ctx) {
khenaidoo4d4802d2018-10-04 21:59:49 -0400505 return new(empty.Empty), nil
khenaidoobf6e7bb2018-08-14 22:27:29 -0400506 }
Richard Jankowskid42826e2018-11-02 16:06:37 -0400507
khenaidoo9cdc1a62019-01-24 21:57:40 -0500508 if handler.competeForTransaction() {
khenaidoo2c6a0992019-04-29 13:46:56 -0400509 if txn, err := handler.takeRequestOwnership(ctx, &utils.DeviceID{Id: id.Id}, handler.longRunningRequestTimeout); err != nil {
Richard Jankowski2755adf2019-01-17 17:16:48 -0500510 return new(empty.Empty), err
Richard Jankowski2755adf2019-01-17 17:16:48 -0500511 } else {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500512 defer txn.Close()
Richard Jankowski2755adf2019-01-17 17:16:48 -0500513 }
514 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500515
khenaidoob9203542018-09-17 22:56:37 -0400516 ch := make(chan interface{})
517 defer close(ch)
518 go handler.deviceMgr.enableDevice(ctx, id, ch)
khenaidoo4d4802d2018-10-04 21:59:49 -0400519 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400520}
521
khenaidoo4d4802d2018-10-04 21:59:49 -0400522// DisableDevice disables a device along with any child device it may have
khenaidoobf6e7bb2018-08-14 22:27:29 -0400523func (handler *APIHandler) DisableDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) {
524 log.Debugw("disabledevice-request", log.Fields{"id": id})
525 if isTestMode(ctx) {
khenaidoo4d4802d2018-10-04 21:59:49 -0400526 return new(empty.Empty), nil
khenaidoobf6e7bb2018-08-14 22:27:29 -0400527 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500528
khenaidoo9cdc1a62019-01-24 21:57:40 -0500529 if handler.competeForTransaction() {
khenaidoo2c6a0992019-04-29 13:46:56 -0400530 if txn, err := handler.takeRequestOwnership(ctx, &utils.DeviceID{Id: id.Id}); err != nil {
Richard Jankowski2755adf2019-01-17 17:16:48 -0500531 return new(empty.Empty), err
Richard Jankowski2755adf2019-01-17 17:16:48 -0500532 } else {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500533 defer txn.Close()
Richard Jankowski2755adf2019-01-17 17:16:48 -0500534 }
535 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500536
khenaidoo92e62c52018-10-03 14:02:54 -0400537 ch := make(chan interface{})
538 defer close(ch)
539 go handler.deviceMgr.disableDevice(ctx, id, ch)
khenaidoo4d4802d2018-10-04 21:59:49 -0400540 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400541}
542
khenaidoo4d4802d2018-10-04 21:59:49 -0400543//RebootDevice invoked the reboot API to the corresponding adapter
khenaidoobf6e7bb2018-08-14 22:27:29 -0400544func (handler *APIHandler) RebootDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) {
khenaidoo4d4802d2018-10-04 21:59:49 -0400545 log.Debugw("rebootDevice-request", log.Fields{"id": id})
khenaidoobf6e7bb2018-08-14 22:27:29 -0400546 if isTestMode(ctx) {
khenaidoo4d4802d2018-10-04 21:59:49 -0400547 return new(empty.Empty), nil
khenaidoobf6e7bb2018-08-14 22:27:29 -0400548 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500549
khenaidoo9cdc1a62019-01-24 21:57:40 -0500550 if handler.competeForTransaction() {
khenaidoo2c6a0992019-04-29 13:46:56 -0400551 if txn, err := handler.takeRequestOwnership(ctx, &utils.DeviceID{Id: id.Id}); err != nil {
Richard Jankowski2755adf2019-01-17 17:16:48 -0500552 return new(empty.Empty), err
Richard Jankowski2755adf2019-01-17 17:16:48 -0500553 } else {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500554 defer txn.Close()
Richard Jankowski2755adf2019-01-17 17:16:48 -0500555 }
556 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500557
khenaidoo4d4802d2018-10-04 21:59:49 -0400558 ch := make(chan interface{})
559 defer close(ch)
560 go handler.deviceMgr.rebootDevice(ctx, id, ch)
561 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400562}
563
khenaidoo4d4802d2018-10-04 21:59:49 -0400564// DeleteDevice removes a device from the data model
khenaidoobf6e7bb2018-08-14 22:27:29 -0400565func (handler *APIHandler) DeleteDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) {
566 log.Debugw("deletedevice-request", log.Fields{"id": id})
567 if isTestMode(ctx) {
khenaidoo4d4802d2018-10-04 21:59:49 -0400568 return new(empty.Empty), nil
khenaidoobf6e7bb2018-08-14 22:27:29 -0400569 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500570
khenaidoo9cdc1a62019-01-24 21:57:40 -0500571 if handler.competeForTransaction() {
khenaidoo6d62c002019-05-15 21:57:03 -0400572 if txn, err := handler.takeRequestOwnership(ctx, &utils.DeviceID{Id: id.Id}); err != nil {
573 // Remove the device in memory
574 if err.Error() == (errors.New(string(COMPLETED_BY_OTHER)).Error()) {
575 handler.deviceMgr.stopManagingDevice(id.Id)
576 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500577 return new(empty.Empty), err
Richard Jankowski2755adf2019-01-17 17:16:48 -0500578 } else {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500579 defer txn.Close()
Richard Jankowski2755adf2019-01-17 17:16:48 -0500580 }
581 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500582
khenaidoo4d4802d2018-10-04 21:59:49 -0400583 ch := make(chan interface{})
584 defer close(ch)
585 go handler.deviceMgr.deleteDevice(ctx, id, ch)
586 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400587}
588
khenaidoof5a5bfa2019-01-23 22:20:29 -0500589// processImageRequest is a helper method to execute an image download request
590func (handler *APIHandler) processImageRequest(ctx context.Context, img *voltha.ImageDownload, requestType int) (*common.OperationResp, error) {
591 log.Debugw("processImageDownload", log.Fields{"img": *img, "requestType": requestType})
592 if isTestMode(ctx) {
593 resp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS}
594 return resp, nil
595 }
596
khenaidoo9cdc1a62019-01-24 21:57:40 -0500597 if handler.competeForTransaction() {
khenaidoo2c6a0992019-04-29 13:46:56 -0400598 if txn, err := handler.takeRequestOwnership(ctx, &utils.DeviceID{Id: img.Id}); err != nil {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500599 return &common.OperationResp{}, err
600 } else {
601 defer txn.Close()
602 }
khenaidoof5a5bfa2019-01-23 22:20:29 -0500603 }
604
khenaidoo2c6a0992019-04-29 13:46:56 -0400605 failedresponse := &common.OperationResp{Code: voltha.OperationResp_OPERATION_FAILURE}
khenaidoof5a5bfa2019-01-23 22:20:29 -0500606
607 ch := make(chan interface{})
608 defer close(ch)
609 switch requestType {
610 case IMAGE_DOWNLOAD:
611 go handler.deviceMgr.downloadImage(ctx, img, ch)
612 case CANCEL_IMAGE_DOWNLOAD:
613 go handler.deviceMgr.cancelImageDownload(ctx, img, ch)
614 case ACTIVATE_IMAGE:
615 go handler.deviceMgr.activateImage(ctx, img, ch)
616 case REVERT_IMAGE:
617 go handler.deviceMgr.revertImage(ctx, img, ch)
618 default:
619 log.Warn("invalid-request-type", log.Fields{"requestType": requestType})
620 return failedresponse, status.Errorf(codes.InvalidArgument, "%d", requestType)
621 }
622 select {
623 case res := <-ch:
624 if res != nil {
625 if err, ok := res.(error); ok {
626 return failedresponse, err
627 }
628 if opResp, ok := res.(*common.OperationResp); ok {
629 return opResp, nil
630 }
631 }
632 log.Warnw("download-image-unexpected-return-type", log.Fields{"result": res})
633 return failedresponse, status.Errorf(codes.Internal, "%s", res)
634 case <-ctx.Done():
635 log.Debug("downloadImage-client-timeout")
636 return nil, ctx.Err()
637 }
638}
639
khenaidoobf6e7bb2018-08-14 22:27:29 -0400640func (handler *APIHandler) DownloadImage(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) {
641 log.Debugw("DownloadImage-request", log.Fields{"img": *img})
642 if isTestMode(ctx) {
643 resp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS}
644 return resp, nil
645 }
646
khenaidoof5a5bfa2019-01-23 22:20:29 -0500647 return handler.processImageRequest(ctx, img, IMAGE_DOWNLOAD)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400648}
649
650func (handler *APIHandler) CancelImageDownload(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) {
khenaidoof5a5bfa2019-01-23 22:20:29 -0500651 log.Debugw("cancelImageDownload-request", log.Fields{"img": *img})
khenaidoobf6e7bb2018-08-14 22:27:29 -0400652 if isTestMode(ctx) {
653 resp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS}
654 return resp, nil
655 }
khenaidoof5a5bfa2019-01-23 22:20:29 -0500656 return handler.processImageRequest(ctx, img, CANCEL_IMAGE_DOWNLOAD)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400657}
658
659func (handler *APIHandler) ActivateImageUpdate(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) {
khenaidoof5a5bfa2019-01-23 22:20:29 -0500660 log.Debugw("activateImageUpdate-request", log.Fields{"img": *img})
khenaidoobf6e7bb2018-08-14 22:27:29 -0400661 if isTestMode(ctx) {
662 resp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS}
663 return resp, nil
664 }
khenaidoof5a5bfa2019-01-23 22:20:29 -0500665
666 return handler.processImageRequest(ctx, img, ACTIVATE_IMAGE)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400667}
668
669func (handler *APIHandler) RevertImageUpdate(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) {
khenaidoof5a5bfa2019-01-23 22:20:29 -0500670 log.Debugw("revertImageUpdate-request", log.Fields{"img": *img})
khenaidoobf6e7bb2018-08-14 22:27:29 -0400671 if isTestMode(ctx) {
672 resp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS}
673 return resp, nil
674 }
khenaidoof5a5bfa2019-01-23 22:20:29 -0500675
676 return handler.processImageRequest(ctx, img, REVERT_IMAGE)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400677}
678
khenaidoof5a5bfa2019-01-23 22:20:29 -0500679func (handler *APIHandler) GetImageDownloadStatus(ctx context.Context, img *voltha.ImageDownload) (*voltha.ImageDownload, error) {
680 log.Debugw("getImageDownloadStatus-request", log.Fields{"img": *img})
681 if isTestMode(ctx) {
Stephane Barbariedf5479f2019-01-29 22:13:00 -0500682 resp := &voltha.ImageDownload{DownloadState: voltha.ImageDownload_DOWNLOAD_SUCCEEDED}
khenaidoof5a5bfa2019-01-23 22:20:29 -0500683 return resp, nil
684 }
685
Stephane Barbariedf5479f2019-01-29 22:13:00 -0500686 failedresponse := &voltha.ImageDownload{DownloadState: voltha.ImageDownload_DOWNLOAD_UNKNOWN}
khenaidoof5a5bfa2019-01-23 22:20:29 -0500687
khenaidoo9cdc1a62019-01-24 21:57:40 -0500688 if handler.competeForTransaction() {
khenaidoo2c6a0992019-04-29 13:46:56 -0400689 if txn, err := handler.takeRequestOwnership(ctx, &utils.DeviceID{Id: img.Id}); err != nil {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500690 return failedresponse, err
691 } else {
692 defer txn.Close()
693 }
khenaidoof5a5bfa2019-01-23 22:20:29 -0500694 }
695
696 ch := make(chan interface{})
697 defer close(ch)
698 go handler.deviceMgr.getImageDownloadStatus(ctx, img, ch)
699
700 select {
701 case res := <-ch:
702 if res != nil {
703 if err, ok := res.(error); ok {
704 return failedresponse, err
705 }
706 if downloadResp, ok := res.(*voltha.ImageDownload); ok {
707 return downloadResp, nil
708 }
709 }
710 log.Warnw("download-image-status", log.Fields{"result": res})
711 return failedresponse, status.Errorf(codes.Internal, "%s", res)
712 case <-ctx.Done():
713 log.Debug("downloadImage-client-timeout")
714 return failedresponse, ctx.Err()
715 }
716}
717
718func (handler *APIHandler) GetImageDownload(ctx context.Context, img *voltha.ImageDownload) (*voltha.ImageDownload, error) {
719 log.Debugw("GetImageDownload-request", log.Fields{"img": *img})
720 if isTestMode(ctx) {
Stephane Barbariedf5479f2019-01-29 22:13:00 -0500721 resp := &voltha.ImageDownload{DownloadState: voltha.ImageDownload_DOWNLOAD_SUCCEEDED}
khenaidoof5a5bfa2019-01-23 22:20:29 -0500722 return resp, nil
723 }
724
725 if download, err := handler.deviceMgr.getImageDownload(ctx, img); err != nil {
Stephane Barbariedf5479f2019-01-29 22:13:00 -0500726 return &voltha.ImageDownload{DownloadState: voltha.ImageDownload_DOWNLOAD_UNKNOWN}, err
khenaidoof5a5bfa2019-01-23 22:20:29 -0500727 } else {
728 return download, nil
729 }
730}
731
732func (handler *APIHandler) ListImageDownloads(ctx context.Context, id *voltha.ID) (*voltha.ImageDownloads, error) {
733 log.Debugw("ListImageDownloads-request", log.Fields{"deviceId": id.Id})
734 if isTestMode(ctx) {
khenaidoo2c6a0992019-04-29 13:46:56 -0400735 resp := &voltha.ImageDownloads{Items: []*voltha.ImageDownload{}}
khenaidoof5a5bfa2019-01-23 22:20:29 -0500736 return resp, nil
737 }
738
739 if downloads, err := handler.deviceMgr.listImageDownloads(ctx, id.Id); err != nil {
740 failedResp := &voltha.ImageDownloads{
khenaidoo2c6a0992019-04-29 13:46:56 -0400741 Items: []*voltha.ImageDownload{
742 {DownloadState: voltha.ImageDownload_DOWNLOAD_UNKNOWN},
743 },
khenaidoof5a5bfa2019-01-23 22:20:29 -0500744 }
745 return failedResp, err
746 } else {
747 return downloads, nil
748 }
749}
750
khenaidoobf6e7bb2018-08-14 22:27:29 -0400751func (handler *APIHandler) UpdateDevicePmConfigs(ctx context.Context, configs *voltha.PmConfigs) (*empty.Empty, error) {
752 log.Debugw("UpdateDevicePmConfigs-request", log.Fields{"configs": *configs})
753 if isTestMode(ctx) {
754 out := new(empty.Empty)
755 return out, nil
756 }
757 return nil, errors.New("UnImplemented")
758}
759
760func (handler *APIHandler) CreateAlarmFilter(ctx context.Context, filter *voltha.AlarmFilter) (*voltha.AlarmFilter, error) {
761 log.Debugw("CreateAlarmFilter-request", log.Fields{"filter": *filter})
762 if isTestMode(ctx) {
763 f := &voltha.AlarmFilter{Id: filter.Id}
764 return f, nil
765 }
766 return nil, errors.New("UnImplemented")
767}
768
769func (handler *APIHandler) UpdateAlarmFilter(ctx context.Context, filter *voltha.AlarmFilter) (*voltha.AlarmFilter, error) {
770 log.Debugw("UpdateAlarmFilter-request", log.Fields{"filter": *filter})
771 if isTestMode(ctx) {
772 f := &voltha.AlarmFilter{Id: filter.Id}
773 return f, nil
774 }
775 return nil, errors.New("UnImplemented")
776}
777
778func (handler *APIHandler) DeleteAlarmFilter(ctx context.Context, id *voltha.ID) (*empty.Empty, error) {
779 log.Debugw("DeleteAlarmFilter-request", log.Fields{"id": *id})
780 if isTestMode(ctx) {
781 out := new(empty.Empty)
782 return out, nil
783 }
784 return nil, errors.New("UnImplemented")
785}
786
787func (handler *APIHandler) SelfTest(ctx context.Context, id *voltha.ID) (*voltha.SelfTestResponse, error) {
788 log.Debugw("SelfTest-request", log.Fields{"id": id})
789 if isTestMode(ctx) {
790 resp := &voltha.SelfTestResponse{Result: voltha.SelfTestResponse_SUCCESS}
791 return resp, nil
792 }
793 return nil, errors.New("UnImplemented")
794}
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500795
796func (handler *APIHandler) forwardPacketOut(packet *openflow_13.PacketOut) {
797 log.Debugw("forwardPacketOut-request", log.Fields{"packet": packet})
khenaidoo3d3b8c22019-05-22 18:10:39 -0400798 //TODO: Update this logic once the OF Controller (OFAgent in this case) can include a transaction Id in its
799 // request. For performance reason we can let both Cores in a Core-Pair forward the Packet to the adapters and
800 // let once of the shim layer (kafka proxy or adapter request handler filters out the duplicate packet)
801 if handler.core.deviceOwnership.OwnedByMe(&utils.LogicalDeviceID{Id: packet.Id}) {
802 agent := handler.logicalDeviceMgr.getLogicalDeviceAgent(packet.Id)
803 agent.packetOut(packet.PacketOut)
804 }
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500805}
khenaidoo3d3b8c22019-05-22 18:10:39 -0400806
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500807func (handler *APIHandler) StreamPacketsOut(
808 packets voltha.VolthaService_StreamPacketsOutServer,
809) error {
810 log.Debugw("StreamPacketsOut-request", log.Fields{"packets": packets})
811 for {
812 packet, err := packets.Recv()
813
814 if err == io.EOF {
815 break
816 } else if err != nil {
817 log.Errorw("Failed to receive packet", log.Fields{"error": err})
818 }
819
820 handler.forwardPacketOut(packet)
821 }
822
823 log.Debugw("StreamPacketsOut-request-done", log.Fields{"packets": packets})
824 return nil
825}
826
khenaidoo297cd252019-02-07 22:10:23 -0500827func (handler *APIHandler) sendPacketIn(deviceId string, transationId string, packet *openflow_13.OfpPacketIn) {
828 // TODO: Augment the OF PacketIn to include the transactionId
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500829 packetIn := openflow_13.PacketIn{Id: deviceId, PacketIn: packet}
830 log.Debugw("sendPacketIn", log.Fields{"packetIn": packetIn})
Richard Jankowskidbab94a2018-12-06 16:20:25 -0500831 // Enqueue the packet
832 if err := handler.packetInQueue.Put(packetIn); err != nil {
833 log.Errorw("failed-to-enqueue-packet", log.Fields{"error": err})
834 }
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500835}
836
837func (handler *APIHandler) ReceivePacketsIn(
838 empty *empty.Empty,
839 packetsIn voltha.VolthaService_ReceivePacketsInServer,
840) error {
841 log.Debugw("ReceivePacketsIn-request", log.Fields{"packetsIn": packetsIn})
842
843 for {
Richard Jankowskidbab94a2018-12-06 16:20:25 -0500844 // Dequeue a packet
845 if packets, err := handler.packetInQueue.Get(1); err == nil {
846 log.Debugw("dequeued-packet", log.Fields{"packet": packets[0]})
847 if packet, ok := packets[0].(openflow_13.PacketIn); ok {
848 log.Debugw("sending-packet-in", log.Fields{"packet": packet})
849 if err := packetsIn.Send(&packet); err != nil {
850 log.Errorw("failed-to-send-packet", log.Fields{"error": err})
851 }
852 }
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500853 }
854 }
khenaidoof5a5bfa2019-01-23 22:20:29 -0500855 //TODO: FInd an elegant way to get out of the above loop when the Core is stopped
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500856}
857
858func (handler *APIHandler) sendChangeEvent(deviceId string, portStatus *openflow_13.OfpPortStatus) {
859 // TODO: validate the type of portStatus parameter
860 //if _, ok := portStatus.(*openflow_13.OfpPortStatus); ok {
861 //}
862 event := openflow_13.ChangeEvent{Id: deviceId, Event: &openflow_13.ChangeEvent_PortStatus{PortStatus: portStatus}}
863 log.Debugw("sendChangeEvent", log.Fields{"event": event})
Richard Jankowski199fd862019-03-18 14:49:51 -0400864 // Enqueue the change event
865 if err := handler.changeEventQueue.Put(event); err != nil {
866 log.Errorw("failed-to-enqueue-change-event", log.Fields{"error": err})
867 }
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500868}
869
870func (handler *APIHandler) ReceiveChangeEvents(
871 empty *empty.Empty,
872 changeEvents voltha.VolthaService_ReceiveChangeEventsServer,
873) error {
874 log.Debugw("ReceiveChangeEvents-request", log.Fields{"changeEvents": changeEvents})
875 for {
Richard Jankowski199fd862019-03-18 14:49:51 -0400876 // Dequeue a change event
877 if events, err := handler.changeEventQueue.Get(1); err == nil {
878 log.Debugw("dequeued-change-event", log.Fields{"event": events[0]})
879 if event, ok := events[0].(openflow_13.ChangeEvent); ok {
880 log.Debugw("sending-change-event", log.Fields{"event": event})
881 if err := changeEvents.Send(&event); err != nil {
882 log.Errorw("failed-to-send-change-event", log.Fields{"error": err})
883 }
884 }
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500885 }
886 }
Richard Jankowski199fd862019-03-18 14:49:51 -0400887}
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500888
889func (handler *APIHandler) Subscribe(
890 ctx context.Context,
891 ofAgent *voltha.OfAgentSubscriber,
892) (*voltha.OfAgentSubscriber, error) {
893 log.Debugw("Subscribe-request", log.Fields{"ofAgent": ofAgent})
894 return &voltha.OfAgentSubscriber{OfagentId: ofAgent.OfagentId, VolthaId: ofAgent.VolthaId}, nil
895}
William Kurkiandaa6bb22019-03-07 12:26:28 -0500896
897//@TODO useless stub, what should this actually do?
898func (handler *APIHandler) GetAlarmDeviceData(
899 ctx context.Context,
900 in *common.ID,
901) (*omci.AlarmDeviceData, error) {
902 log.Debug("GetAlarmDeviceData-stub")
903 return nil, nil
904}
905
906//@TODO useless stub, what should this actually do?
907func (handler *APIHandler) GetMeterStatsOfLogicalDevice(
khenaidoo2c6a0992019-04-29 13:46:56 -0400908 ctx context.Context,
William Kurkiandaa6bb22019-03-07 12:26:28 -0500909 in *common.ID,
910) (*openflow_13.MeterStatsReply, error) {
911 log.Debug("GetMeterStatsOfLogicalDevice-stub")
912 return nil, nil
913}
914
915//@TODO useless stub, what should this actually do?
916func (handler *APIHandler) GetMibDeviceData(
khenaidoo2c6a0992019-04-29 13:46:56 -0400917 ctx context.Context,
918 in *common.ID,
William Kurkiandaa6bb22019-03-07 12:26:28 -0500919) (*omci.MibDeviceData, error) {
920 log.Debug("GetMibDeviceData-stub")
921 return nil, nil
922}
923
William Kurkiandaa6bb22019-03-07 12:26:28 -0500924func (handler *APIHandler) SimulateAlarm(
925 ctx context.Context,
926 in *voltha.SimulateAlarmRequest,
927) (*common.OperationResp, error) {
serkant.uluderya334479d2019-04-10 08:26:15 -0700928 log.Debugw("SimulateAlarm-request", log.Fields{"id": in.Id})
929 successResp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS}
930 if isTestMode(ctx) {
931 return successResp, nil
932 }
933
934 if handler.competeForTransaction() {
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400935 if txn, err := handler.takeRequestOwnership(ctx, &utils.DeviceID{Id: in.Id}, handler.longRunningRequestTimeout); err != nil {
936 failedresponse := &common.OperationResp{Code: voltha.OperationResp_OPERATION_FAILURE}
serkant.uluderya334479d2019-04-10 08:26:15 -0700937 return failedresponse, err
938 } else {
939 defer txn.Close()
940 }
941 }
942
943 ch := make(chan interface{})
944 defer close(ch)
945 go handler.deviceMgr.simulateAlarm(ctx, in, ch)
946 return successResp, nil
William Kurkiandaa6bb22019-03-07 12:26:28 -0500947}
948
949//@TODO useless stub, what should this actually do?
950func (handler *APIHandler) UpdateLogicalDeviceMeterTable(
951 ctx context.Context,
952 in *openflow_13.MeterModUpdate,
953) (*empty.Empty, error) {
954 log.Debug("UpdateLogicalDeviceMeterTable-stub")
955 return nil, nil
956}