blob: 1ad076657c8ded757d6a8d0a024dba3fe60a47f8 [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
Richard Jankowski199fd862019-03-18 14:49:51 -0400124// This function handles the creation of new devices
125func (handler *APIHandler) acquireRequest(ctx context.Context, id interface{}, maxTimeout ...int64) (*KVTransaction, error) {
khenaidoob6080322019-01-29 21:47:38 -0500126 timeout := handler.defaultRequestTimeout
khenaidoo9cdc1a62019-01-24 21:57:40 -0500127 if len(maxTimeout) > 0 {
128 timeout = maxTimeout[0]
Richard Jankowski2755adf2019-01-17 17:16:48 -0500129 }
khenaidoob6080322019-01-29 21:47:38 -0500130 log.Debugw("transaction-timeout", log.Fields{"timeout": timeout})
khenaidoo9cdc1a62019-01-24 21:57:40 -0500131 txn, err := handler.createKvTransaction(ctx)
132 if txn == nil {
khenaidoo2c6a0992019-04-29 13:46:56 -0400133 return nil, err
khenaidoo9cdc1a62019-01-24 21:57:40 -0500134 } else if txn.Acquired(timeout) {
135 return txn, nil
136 } else {
khenaidoo297cd252019-02-07 22:10:23 -0500137 if id != nil {
138 // The id can either be a device Id or a logical device id.
khenaidoo1ce37ad2019-03-24 22:07:24 -0400139 if dId, ok := id.(*utils.DeviceID); ok {
khenaidoo297cd252019-02-07 22:10:23 -0500140 // Since this core has not processed this request, let's load the device, along with its extended
141 // family (parents and children) in memory. This will keep this core in-sync with its paired core as
142 // much as possible. The watch feature in the core model will ensure that the contents of those objects in
143 // memory are in sync.
144 time.Sleep(2 * time.Second)
khenaidoo1ce37ad2019-03-24 22:07:24 -0400145 go handler.deviceMgr.load(dId.Id)
146 } else if ldId, ok := id.(*utils.LogicalDeviceID); ok {
khenaidoo297cd252019-02-07 22:10:23 -0500147 // This will load the logical device along with its children and grandchildren
khenaidoo1ce37ad2019-03-24 22:07:24 -0400148 go handler.logicalDeviceMgr.load(ldId.Id)
khenaidoo297cd252019-02-07 22:10:23 -0500149 }
150 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500151 return nil, errors.New("failed-to-seize-request")
Richard Jankowski2755adf2019-01-17 17:16:48 -0500152 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500153}
154
khenaidoo6d62c002019-05-15 21:57:03 -0400155// takeRequestOwnership creates a transaction in the dB for this request and handles the logic of transaction
156// acquisition. If the device is owned by this Core (in a core-pair) then acquire the transaction with a
157// timeout value (in the event of a timeout the other Core in the core-pair will proceed with the transaction). If the
158// device is not owned then this Core will just monitor the transaction for potential timeouts.
Richard Jankowski199fd862019-03-18 14:49:51 -0400159func (handler *APIHandler) takeRequestOwnership(ctx context.Context, id interface{}, maxTimeout ...int64) (*KVTransaction, error) {
khenaidoo6d62c002019-05-15 21:57:03 -0400160 t := time.Now()
Richard Jankowski199fd862019-03-18 14:49:51 -0400161 timeout := handler.defaultRequestTimeout
162 if len(maxTimeout) > 0 {
163 timeout = maxTimeout[0]
164 }
Richard Jankowski199fd862019-03-18 14:49:51 -0400165 txn, err := handler.createKvTransaction(ctx)
166 if txn == nil {
khenaidoo2c6a0992019-04-29 13:46:56 -0400167 return nil, err
Richard Jankowski199fd862019-03-18 14:49:51 -0400168 }
169
170 owned := false
171 if id != nil {
khenaidoo1ce37ad2019-03-24 22:07:24 -0400172 owned = handler.core.deviceOwnership.OwnedByMe(id)
Richard Jankowski199fd862019-03-18 14:49:51 -0400173 }
174 if owned {
175 if txn.Acquired(timeout) {
khenaidoo6d62c002019-05-15 21:57:03 -0400176 log.Debugw("acquired-transaction", log.Fields{"transaction-timeout": timeout})
Richard Jankowski199fd862019-03-18 14:49:51 -0400177 return txn, nil
178 } else {
179 return nil, errors.New("failed-to-seize-request")
180 }
181 } else {
182 if txn.Monitor(timeout) {
khenaidoo6d62c002019-05-15 21:57:03 -0400183 log.Debugw("acquired-transaction-after-timeout", log.Fields{"timeout": timeout, "waited-time": time.Since(t)})
Richard Jankowski199fd862019-03-18 14:49:51 -0400184 return txn, nil
185 } else {
khenaidoo6d62c002019-05-15 21:57:03 -0400186 log.Debugw("transaction-completed-by-other", log.Fields{"timeout": timeout, "waited-time": time.Since(t)})
187 return nil, errors.New(string(COMPLETED_BY_OTHER))
Richard Jankowski199fd862019-03-18 14:49:51 -0400188 }
189 }
190}
191
khenaidoo4d4802d2018-10-04 21:59:49 -0400192// waitForNilResponseOnSuccess is a helper function to wait for a response on channel ch where an nil
193// response is expected in a successful scenario
194func waitForNilResponseOnSuccess(ctx context.Context, ch chan interface{}) (*empty.Empty, error) {
195 select {
196 case res := <-ch:
197 if res == nil {
198 return new(empty.Empty), nil
199 } else if err, ok := res.(error); ok {
200 return new(empty.Empty), err
201 } else {
202 log.Warnw("unexpected-return-type", log.Fields{"result": res})
203 err = status.Errorf(codes.Internal, "%s", res)
204 return new(empty.Empty), err
205 }
206 case <-ctx.Done():
207 log.Debug("client-timeout")
208 return nil, ctx.Err()
209 }
210}
211
khenaidoobf6e7bb2018-08-14 22:27:29 -0400212func (handler *APIHandler) UpdateLogLevel(ctx context.Context, logging *voltha.Logging) (*empty.Empty, error) {
khenaidoo6f2fbe32019-01-18 16:16:50 -0500213 log.Debugw("UpdateLogLevel-request", log.Fields{"package": logging.PackageName, "intval": int(logging.Level)})
khenaidoo92e62c52018-10-03 14:02:54 -0400214 out := new(empty.Empty)
khenaidoo6f2fbe32019-01-18 16:16:50 -0500215 if logging.PackageName == "" {
216 log.SetAllLogLevel(int(logging.Level))
217 } else {
218 log.SetPackageLogLevel(logging.PackageName, int(logging.Level))
219 }
khenaidoo92e62c52018-10-03 14:02:54 -0400220 return out, nil
khenaidoobf6e7bb2018-08-14 22:27:29 -0400221}
222
khenaidoo54e0ddf2019-02-27 16:21:33 -0500223func (handler *APIHandler) UpdateMembership(ctx context.Context, membership *voltha.Membership) (*empty.Empty, error) {
224 log.Debugw("UpdateMembership-request", log.Fields{"membership": membership})
225 out := new(empty.Empty)
khenaidoo6417b6c2019-03-01 18:18:01 -0500226 if err := handler.core.updateCoreMembership(ctx, membership); err != nil {
khenaidoo54e0ddf2019-02-27 16:21:33 -0500227 return out, err
228 }
229 return out, nil
230}
231
khenaidoo6417b6c2019-03-01 18:18:01 -0500232func (handler *APIHandler) GetMembership(ctx context.Context, empty *empty.Empty) (*voltha.Membership, error) {
233 log.Debug("GetMembership-request")
234 if membership := handler.core.getCoreMembership(ctx); membership != nil {
235 return membership, nil
236 }
237 return &voltha.Membership{}, nil
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
khenaidoo3d3b8c22019-05-22 18:10:39 -0400289 // TODO: Update this logic when the OF Controller (OFAgent in this case) is able to send a transaction Id in its
290 // request (the api-router binds the OfAgent to two Cores in a pair and let the traffic flows transparently)
khenaidoo9cdc1a62019-01-24 21:57:40 -0500291 if handler.competeForTransaction() {
khenaidoo3d3b8c22019-05-22 18:10:39 -0400292 if !handler.isOFControllerRequest(ctx) {
khenaidoo2c6a0992019-04-29 13:46:56 -0400293 if txn, err := handler.takeRequestOwnership(ctx, &utils.LogicalDeviceID{Id: flow.Id}); err != nil {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500294 return new(empty.Empty), err
295 } else {
296 defer txn.Close()
297 }
khenaidoo3d3b8c22019-05-22 18:10:39 -0400298 } else if !handler.core.deviceOwnership.OwnedByMe(&utils.LogicalDeviceID{Id: flow.Id}) {
299 return new(empty.Empty), nil
Richard Jankowski2755adf2019-01-17 17:16:48 -0500300 }
301 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500302
khenaidoo19d7b632018-10-30 10:49:50 -0400303 ch := make(chan interface{})
304 defer close(ch)
305 go handler.logicalDeviceMgr.updateFlowTable(ctx, flow.Id, flow.FlowMod, ch)
306 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400307}
308
309func (handler *APIHandler) UpdateLogicalDeviceFlowGroupTable(ctx context.Context, flow *openflow_13.FlowGroupTableUpdate) (*empty.Empty, error) {
310 log.Debugw("UpdateLogicalDeviceFlowGroupTable-request", log.Fields{"flow": flow, "test": common.TestModeKeys_api_test.String()})
311 if isTestMode(ctx) {
312 out := new(empty.Empty)
313 return out, nil
314 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500315
khenaidoo3d3b8c22019-05-22 18:10:39 -0400316 // TODO: Update this logic when the OF Controller (OFAgent in this case) is able to send a transaction Id in its
317 // request (the api-router binds the OfAgent to two Cores in a pair and let the traffic flows transparently)
khenaidoo9cdc1a62019-01-24 21:57:40 -0500318 if handler.competeForTransaction() {
Richard Jankowski46464e92019-03-05 11:53:55 -0500319 if !handler.isOFControllerRequest(ctx) { // No need to acquire the transaction as request is sent to one core only
khenaidoo2c6a0992019-04-29 13:46:56 -0400320 if txn, err := handler.takeRequestOwnership(ctx, &utils.LogicalDeviceID{Id: flow.Id}); err != nil {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500321 return new(empty.Empty), err
322 } else {
323 defer txn.Close()
324 }
khenaidoo3d3b8c22019-05-22 18:10:39 -0400325 } else if !handler.core.deviceOwnership.OwnedByMe(&utils.LogicalDeviceID{Id: flow.Id}) {
326 return new(empty.Empty), nil
Richard Jankowski2755adf2019-01-17 17:16:48 -0500327 }
328 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500329
khenaidoo19d7b632018-10-30 10:49:50 -0400330 ch := make(chan interface{})
331 defer close(ch)
332 go handler.logicalDeviceMgr.updateGroupTable(ctx, flow.Id, flow.GroupMod, ch)
333 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400334}
335
khenaidoob9203542018-09-17 22:56:37 -0400336// GetDevice must be implemented in the read-only containers - should it also be implemented here?
337func (handler *APIHandler) GetDevice(ctx context.Context, id *voltha.ID) (*voltha.Device, error) {
338 log.Debugw("GetDevice-request", log.Fields{"id": id})
khenaidoo19d7b632018-10-30 10:49:50 -0400339 return handler.deviceMgr.GetDevice(id.Id)
khenaidoob9203542018-09-17 22:56:37 -0400340}
341
342// GetDevice must be implemented in the read-only containers - should it also be implemented here?
343func (handler *APIHandler) ListDevices(ctx context.Context, empty *empty.Empty) (*voltha.Devices, error) {
344 log.Debug("ListDevices")
345 return handler.deviceMgr.ListDevices()
346}
347
khenaidoo7ccedd52018-12-14 16:48:54 -0500348// ListDeviceIds returns the list of device ids managed by a voltha core
349func (handler *APIHandler) ListDeviceIds(ctx context.Context, empty *empty.Empty) (*voltha.IDs, error) {
350 log.Debug("ListDeviceIDs")
351 if isTestMode(ctx) {
352 out := &voltha.IDs{Items: make([]*voltha.ID, 0)}
353 return out, nil
354 }
355 return handler.deviceMgr.ListDeviceIds()
356}
357
358//ReconcileDevices is a request to a voltha core to managed a list of devices based on their IDs
359func (handler *APIHandler) ReconcileDevices(ctx context.Context, ids *voltha.IDs) (*empty.Empty, error) {
360 log.Debug("ReconcileDevices")
361 if isTestMode(ctx) {
362 out := new(empty.Empty)
363 return out, nil
364 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500365
khenaidoo9cdc1a62019-01-24 21:57:40 -0500366 // No need to grab a transaction as this request is core specific
367
khenaidoo7ccedd52018-12-14 16:48:54 -0500368 ch := make(chan interface{})
369 defer close(ch)
370 go handler.deviceMgr.ReconcileDevices(ctx, ids, ch)
371 return waitForNilResponseOnSuccess(ctx, ch)
372}
373
khenaidoob9203542018-09-17 22:56:37 -0400374// GetLogicalDevice must be implemented in the read-only containers - should it also be implemented here?
375func (handler *APIHandler) GetLogicalDevice(ctx context.Context, id *voltha.ID) (*voltha.LogicalDevice, error) {
376 log.Debugw("GetLogicalDevice-request", log.Fields{"id": id})
377 return handler.logicalDeviceMgr.getLogicalDevice(id.Id)
378}
379
380// ListLogicalDevices must be implemented in the read-only containers - should it also be implemented here?
381func (handler *APIHandler) ListLogicalDevices(ctx context.Context, empty *empty.Empty) (*voltha.LogicalDevices, error) {
382 log.Debug("ListLogicalDevices")
383 return handler.logicalDeviceMgr.listLogicalDevices()
384}
385
khenaidoo21d51152019-02-01 13:48:37 -0500386// ListAdapters returns the contents of all adapters known to the system
387func (handler *APIHandler) ListAdapters(ctx context.Context, empty *empty.Empty) (*voltha.Adapters, error) {
388 log.Debug("ListDevices")
389 return handler.adapterMgr.listAdapters(ctx)
390}
391
khenaidoodd237172019-05-27 16:37:17 -0400392func (handler *APIHandler) ListLogicalDeviceFlows(ctx context.Context, id *voltha.ID) (*openflow_13.Flows, error) {
393 log.Debugw("ListLogicalDeviceFlows", log.Fields{"id": *id})
394 return handler.logicalDeviceMgr.ListLogicalDeviceFlows(ctx, id.Id)
395}
396
397func (handler *APIHandler) ListLogicalDeviceFlowGroups(ctx context.Context, id *voltha.ID) (*openflow_13.FlowGroups, error) {
398 log.Debugw("ListLogicalDeviceFlowGroups", log.Fields{"id": *id})
399 return handler.logicalDeviceMgr.ListLogicalDeviceFlowGroups(ctx, id.Id)
400}
401
khenaidoo19d7b632018-10-30 10:49:50 -0400402// ListLogicalDevicePorts must be implemented in the read-only containers - should it also be implemented here?
403func (handler *APIHandler) ListLogicalDevicePorts(ctx context.Context, id *voltha.ID) (*voltha.LogicalPorts, error) {
404 log.Debugw("ListLogicalDevicePorts", log.Fields{"logicaldeviceid": id})
405 return handler.logicalDeviceMgr.ListLogicalDevicePorts(ctx, id.Id)
406}
407
khenaidoo4d4802d2018-10-04 21:59:49 -0400408// CreateDevice creates a new parent device in the data model
khenaidoobf6e7bb2018-08-14 22:27:29 -0400409func (handler *APIHandler) CreateDevice(ctx context.Context, device *voltha.Device) (*voltha.Device, error) {
khenaidoob9203542018-09-17 22:56:37 -0400410 log.Debugw("createdevice", log.Fields{"device": *device})
khenaidoobf6e7bb2018-08-14 22:27:29 -0400411 if isTestMode(ctx) {
412 return &voltha.Device{Id: device.Id}, nil
413 }
Richard Jankowskid42826e2018-11-02 16:06:37 -0400414
khenaidoo9cdc1a62019-01-24 21:57:40 -0500415 if handler.competeForTransaction() {
Richard Jankowski199fd862019-03-18 14:49:51 -0400416 if txn, err := handler.acquireRequest(ctx, nil); err != nil {
Richard Jankowski2755adf2019-01-17 17:16:48 -0500417 return &voltha.Device{}, err
Richard Jankowski2755adf2019-01-17 17:16:48 -0500418 } else {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500419 defer txn.Close()
Richard Jankowski2755adf2019-01-17 17:16:48 -0500420 }
421 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500422
khenaidoob9203542018-09-17 22:56:37 -0400423 ch := make(chan interface{})
424 defer close(ch)
425 go handler.deviceMgr.createDevice(ctx, device, ch)
426 select {
427 case res := <-ch:
khenaidoo92e62c52018-10-03 14:02:54 -0400428 if res != nil {
429 if err, ok := res.(error); ok {
430 return &voltha.Device{}, err
431 }
432 if d, ok := res.(*voltha.Device); ok {
khenaidoo2c6a0992019-04-29 13:46:56 -0400433 handler.core.deviceOwnership.OwnedByMe(&utils.DeviceID{Id: d.Id})
khenaidoo92e62c52018-10-03 14:02:54 -0400434 return d, nil
435 }
khenaidoob9203542018-09-17 22:56:37 -0400436 }
khenaidoo92e62c52018-10-03 14:02:54 -0400437 log.Warnw("create-device-unexpected-return-type", log.Fields{"result": res})
438 err := status.Errorf(codes.Internal, "%s", res)
439 return &voltha.Device{}, err
khenaidoob9203542018-09-17 22:56:37 -0400440 case <-ctx.Done():
441 log.Debug("createdevice-client-timeout")
442 return nil, ctx.Err()
443 }
khenaidoobf6e7bb2018-08-14 22:27:29 -0400444}
445
khenaidoo4d4802d2018-10-04 21:59:49 -0400446// EnableDevice activates a device by invoking the adopt_device API on the appropriate adapter
khenaidoobf6e7bb2018-08-14 22:27:29 -0400447func (handler *APIHandler) EnableDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) {
khenaidoob9203542018-09-17 22:56:37 -0400448 log.Debugw("enabledevice", log.Fields{"id": id})
khenaidoobf6e7bb2018-08-14 22:27:29 -0400449 if isTestMode(ctx) {
khenaidoo4d4802d2018-10-04 21:59:49 -0400450 return new(empty.Empty), nil
khenaidoobf6e7bb2018-08-14 22:27:29 -0400451 }
Richard Jankowskid42826e2018-11-02 16:06:37 -0400452
khenaidoo9cdc1a62019-01-24 21:57:40 -0500453 if handler.competeForTransaction() {
khenaidoo2c6a0992019-04-29 13:46:56 -0400454 if txn, err := handler.takeRequestOwnership(ctx, &utils.DeviceID{Id: id.Id}, handler.longRunningRequestTimeout); err != nil {
Richard Jankowski2755adf2019-01-17 17:16:48 -0500455 return new(empty.Empty), err
Richard Jankowski2755adf2019-01-17 17:16:48 -0500456 } else {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500457 defer txn.Close()
Richard Jankowski2755adf2019-01-17 17:16:48 -0500458 }
459 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500460
khenaidoob9203542018-09-17 22:56:37 -0400461 ch := make(chan interface{})
462 defer close(ch)
463 go handler.deviceMgr.enableDevice(ctx, id, ch)
khenaidoo4d4802d2018-10-04 21:59:49 -0400464 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400465}
466
khenaidoo4d4802d2018-10-04 21:59:49 -0400467// DisableDevice disables a device along with any child device it may have
khenaidoobf6e7bb2018-08-14 22:27:29 -0400468func (handler *APIHandler) DisableDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) {
469 log.Debugw("disabledevice-request", log.Fields{"id": id})
470 if isTestMode(ctx) {
khenaidoo4d4802d2018-10-04 21:59:49 -0400471 return new(empty.Empty), nil
khenaidoobf6e7bb2018-08-14 22:27:29 -0400472 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500473
khenaidoo9cdc1a62019-01-24 21:57:40 -0500474 if handler.competeForTransaction() {
khenaidoo2c6a0992019-04-29 13:46:56 -0400475 if txn, err := handler.takeRequestOwnership(ctx, &utils.DeviceID{Id: id.Id}); err != nil {
Richard Jankowski2755adf2019-01-17 17:16:48 -0500476 return new(empty.Empty), err
Richard Jankowski2755adf2019-01-17 17:16:48 -0500477 } else {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500478 defer txn.Close()
Richard Jankowski2755adf2019-01-17 17:16:48 -0500479 }
480 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500481
khenaidoo92e62c52018-10-03 14:02:54 -0400482 ch := make(chan interface{})
483 defer close(ch)
484 go handler.deviceMgr.disableDevice(ctx, id, ch)
khenaidoo4d4802d2018-10-04 21:59:49 -0400485 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400486}
487
khenaidoo4d4802d2018-10-04 21:59:49 -0400488//RebootDevice invoked the reboot API to the corresponding adapter
khenaidoobf6e7bb2018-08-14 22:27:29 -0400489func (handler *APIHandler) RebootDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) {
khenaidoo4d4802d2018-10-04 21:59:49 -0400490 log.Debugw("rebootDevice-request", log.Fields{"id": id})
khenaidoobf6e7bb2018-08-14 22:27:29 -0400491 if isTestMode(ctx) {
khenaidoo4d4802d2018-10-04 21:59:49 -0400492 return new(empty.Empty), nil
khenaidoobf6e7bb2018-08-14 22:27:29 -0400493 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500494
khenaidoo9cdc1a62019-01-24 21:57:40 -0500495 if handler.competeForTransaction() {
khenaidoo2c6a0992019-04-29 13:46:56 -0400496 if txn, err := handler.takeRequestOwnership(ctx, &utils.DeviceID{Id: id.Id}); err != nil {
Richard Jankowski2755adf2019-01-17 17:16:48 -0500497 return new(empty.Empty), err
Richard Jankowski2755adf2019-01-17 17:16:48 -0500498 } else {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500499 defer txn.Close()
Richard Jankowski2755adf2019-01-17 17:16:48 -0500500 }
501 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500502
khenaidoo4d4802d2018-10-04 21:59:49 -0400503 ch := make(chan interface{})
504 defer close(ch)
505 go handler.deviceMgr.rebootDevice(ctx, id, ch)
506 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400507}
508
khenaidoo4d4802d2018-10-04 21:59:49 -0400509// DeleteDevice removes a device from the data model
khenaidoobf6e7bb2018-08-14 22:27:29 -0400510func (handler *APIHandler) DeleteDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) {
511 log.Debugw("deletedevice-request", log.Fields{"id": id})
512 if isTestMode(ctx) {
khenaidoo4d4802d2018-10-04 21:59:49 -0400513 return new(empty.Empty), nil
khenaidoobf6e7bb2018-08-14 22:27:29 -0400514 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500515
khenaidoo9cdc1a62019-01-24 21:57:40 -0500516 if handler.competeForTransaction() {
khenaidoo6d62c002019-05-15 21:57:03 -0400517 if txn, err := handler.takeRequestOwnership(ctx, &utils.DeviceID{Id: id.Id}); err != nil {
518 // Remove the device in memory
519 if err.Error() == (errors.New(string(COMPLETED_BY_OTHER)).Error()) {
520 handler.deviceMgr.stopManagingDevice(id.Id)
521 }
Richard Jankowski2755adf2019-01-17 17:16:48 -0500522 return new(empty.Empty), err
Richard Jankowski2755adf2019-01-17 17:16:48 -0500523 } else {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500524 defer txn.Close()
Richard Jankowski2755adf2019-01-17 17:16:48 -0500525 }
526 }
khenaidoo9cdc1a62019-01-24 21:57:40 -0500527
khenaidoo4d4802d2018-10-04 21:59:49 -0400528 ch := make(chan interface{})
529 defer close(ch)
530 go handler.deviceMgr.deleteDevice(ctx, id, ch)
531 return waitForNilResponseOnSuccess(ctx, ch)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400532}
533
khenaidoof5a5bfa2019-01-23 22:20:29 -0500534// processImageRequest is a helper method to execute an image download request
535func (handler *APIHandler) processImageRequest(ctx context.Context, img *voltha.ImageDownload, requestType int) (*common.OperationResp, error) {
536 log.Debugw("processImageDownload", log.Fields{"img": *img, "requestType": requestType})
537 if isTestMode(ctx) {
538 resp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS}
539 return resp, nil
540 }
541
khenaidoo9cdc1a62019-01-24 21:57:40 -0500542 if handler.competeForTransaction() {
khenaidoo2c6a0992019-04-29 13:46:56 -0400543 if txn, err := handler.takeRequestOwnership(ctx, &utils.DeviceID{Id: img.Id}); err != nil {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500544 return &common.OperationResp{}, err
545 } else {
546 defer txn.Close()
547 }
khenaidoof5a5bfa2019-01-23 22:20:29 -0500548 }
549
khenaidoo2c6a0992019-04-29 13:46:56 -0400550 failedresponse := &common.OperationResp{Code: voltha.OperationResp_OPERATION_FAILURE}
khenaidoof5a5bfa2019-01-23 22:20:29 -0500551
552 ch := make(chan interface{})
553 defer close(ch)
554 switch requestType {
555 case IMAGE_DOWNLOAD:
556 go handler.deviceMgr.downloadImage(ctx, img, ch)
557 case CANCEL_IMAGE_DOWNLOAD:
558 go handler.deviceMgr.cancelImageDownload(ctx, img, ch)
559 case ACTIVATE_IMAGE:
560 go handler.deviceMgr.activateImage(ctx, img, ch)
561 case REVERT_IMAGE:
562 go handler.deviceMgr.revertImage(ctx, img, ch)
563 default:
564 log.Warn("invalid-request-type", log.Fields{"requestType": requestType})
565 return failedresponse, status.Errorf(codes.InvalidArgument, "%d", requestType)
566 }
567 select {
568 case res := <-ch:
569 if res != nil {
570 if err, ok := res.(error); ok {
571 return failedresponse, err
572 }
573 if opResp, ok := res.(*common.OperationResp); ok {
574 return opResp, nil
575 }
576 }
577 log.Warnw("download-image-unexpected-return-type", log.Fields{"result": res})
578 return failedresponse, status.Errorf(codes.Internal, "%s", res)
579 case <-ctx.Done():
580 log.Debug("downloadImage-client-timeout")
581 return nil, ctx.Err()
582 }
583}
584
khenaidoobf6e7bb2018-08-14 22:27:29 -0400585func (handler *APIHandler) DownloadImage(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) {
586 log.Debugw("DownloadImage-request", log.Fields{"img": *img})
587 if isTestMode(ctx) {
588 resp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS}
589 return resp, nil
590 }
591
khenaidoof5a5bfa2019-01-23 22:20:29 -0500592 return handler.processImageRequest(ctx, img, IMAGE_DOWNLOAD)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400593}
594
595func (handler *APIHandler) CancelImageDownload(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) {
khenaidoof5a5bfa2019-01-23 22:20:29 -0500596 log.Debugw("cancelImageDownload-request", log.Fields{"img": *img})
khenaidoobf6e7bb2018-08-14 22:27:29 -0400597 if isTestMode(ctx) {
598 resp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS}
599 return resp, nil
600 }
khenaidoof5a5bfa2019-01-23 22:20:29 -0500601 return handler.processImageRequest(ctx, img, CANCEL_IMAGE_DOWNLOAD)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400602}
603
604func (handler *APIHandler) ActivateImageUpdate(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) {
khenaidoof5a5bfa2019-01-23 22:20:29 -0500605 log.Debugw("activateImageUpdate-request", log.Fields{"img": *img})
khenaidoobf6e7bb2018-08-14 22:27:29 -0400606 if isTestMode(ctx) {
607 resp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS}
608 return resp, nil
609 }
khenaidoof5a5bfa2019-01-23 22:20:29 -0500610
611 return handler.processImageRequest(ctx, img, ACTIVATE_IMAGE)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400612}
613
614func (handler *APIHandler) RevertImageUpdate(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) {
khenaidoof5a5bfa2019-01-23 22:20:29 -0500615 log.Debugw("revertImageUpdate-request", log.Fields{"img": *img})
khenaidoobf6e7bb2018-08-14 22:27:29 -0400616 if isTestMode(ctx) {
617 resp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS}
618 return resp, nil
619 }
khenaidoof5a5bfa2019-01-23 22:20:29 -0500620
621 return handler.processImageRequest(ctx, img, REVERT_IMAGE)
khenaidoobf6e7bb2018-08-14 22:27:29 -0400622}
623
khenaidoof5a5bfa2019-01-23 22:20:29 -0500624func (handler *APIHandler) GetImageDownloadStatus(ctx context.Context, img *voltha.ImageDownload) (*voltha.ImageDownload, error) {
625 log.Debugw("getImageDownloadStatus-request", log.Fields{"img": *img})
626 if isTestMode(ctx) {
Stephane Barbariedf5479f2019-01-29 22:13:00 -0500627 resp := &voltha.ImageDownload{DownloadState: voltha.ImageDownload_DOWNLOAD_SUCCEEDED}
khenaidoof5a5bfa2019-01-23 22:20:29 -0500628 return resp, nil
629 }
630
Stephane Barbariedf5479f2019-01-29 22:13:00 -0500631 failedresponse := &voltha.ImageDownload{DownloadState: voltha.ImageDownload_DOWNLOAD_UNKNOWN}
khenaidoof5a5bfa2019-01-23 22:20:29 -0500632
khenaidoo9cdc1a62019-01-24 21:57:40 -0500633 if handler.competeForTransaction() {
khenaidoo2c6a0992019-04-29 13:46:56 -0400634 if txn, err := handler.takeRequestOwnership(ctx, &utils.DeviceID{Id: img.Id}); err != nil {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500635 return failedresponse, err
636 } else {
637 defer txn.Close()
638 }
khenaidoof5a5bfa2019-01-23 22:20:29 -0500639 }
640
641 ch := make(chan interface{})
642 defer close(ch)
643 go handler.deviceMgr.getImageDownloadStatus(ctx, img, ch)
644
645 select {
646 case res := <-ch:
647 if res != nil {
648 if err, ok := res.(error); ok {
649 return failedresponse, err
650 }
651 if downloadResp, ok := res.(*voltha.ImageDownload); ok {
652 return downloadResp, nil
653 }
654 }
655 log.Warnw("download-image-status", log.Fields{"result": res})
656 return failedresponse, status.Errorf(codes.Internal, "%s", res)
657 case <-ctx.Done():
658 log.Debug("downloadImage-client-timeout")
659 return failedresponse, ctx.Err()
660 }
661}
662
663func (handler *APIHandler) GetImageDownload(ctx context.Context, img *voltha.ImageDownload) (*voltha.ImageDownload, error) {
664 log.Debugw("GetImageDownload-request", log.Fields{"img": *img})
665 if isTestMode(ctx) {
Stephane Barbariedf5479f2019-01-29 22:13:00 -0500666 resp := &voltha.ImageDownload{DownloadState: voltha.ImageDownload_DOWNLOAD_SUCCEEDED}
khenaidoof5a5bfa2019-01-23 22:20:29 -0500667 return resp, nil
668 }
669
670 if download, err := handler.deviceMgr.getImageDownload(ctx, img); err != nil {
Stephane Barbariedf5479f2019-01-29 22:13:00 -0500671 return &voltha.ImageDownload{DownloadState: voltha.ImageDownload_DOWNLOAD_UNKNOWN}, err
khenaidoof5a5bfa2019-01-23 22:20:29 -0500672 } else {
673 return download, nil
674 }
675}
676
677func (handler *APIHandler) ListImageDownloads(ctx context.Context, id *voltha.ID) (*voltha.ImageDownloads, error) {
678 log.Debugw("ListImageDownloads-request", log.Fields{"deviceId": id.Id})
679 if isTestMode(ctx) {
khenaidoo2c6a0992019-04-29 13:46:56 -0400680 resp := &voltha.ImageDownloads{Items: []*voltha.ImageDownload{}}
khenaidoof5a5bfa2019-01-23 22:20:29 -0500681 return resp, nil
682 }
683
684 if downloads, err := handler.deviceMgr.listImageDownloads(ctx, id.Id); err != nil {
685 failedResp := &voltha.ImageDownloads{
khenaidoo2c6a0992019-04-29 13:46:56 -0400686 Items: []*voltha.ImageDownload{
687 {DownloadState: voltha.ImageDownload_DOWNLOAD_UNKNOWN},
688 },
khenaidoof5a5bfa2019-01-23 22:20:29 -0500689 }
690 return failedResp, err
691 } else {
692 return downloads, nil
693 }
694}
695
khenaidoobf6e7bb2018-08-14 22:27:29 -0400696func (handler *APIHandler) UpdateDevicePmConfigs(ctx context.Context, configs *voltha.PmConfigs) (*empty.Empty, error) {
697 log.Debugw("UpdateDevicePmConfigs-request", log.Fields{"configs": *configs})
698 if isTestMode(ctx) {
699 out := new(empty.Empty)
700 return out, nil
701 }
702 return nil, errors.New("UnImplemented")
703}
704
705func (handler *APIHandler) CreateAlarmFilter(ctx context.Context, filter *voltha.AlarmFilter) (*voltha.AlarmFilter, error) {
706 log.Debugw("CreateAlarmFilter-request", log.Fields{"filter": *filter})
707 if isTestMode(ctx) {
708 f := &voltha.AlarmFilter{Id: filter.Id}
709 return f, nil
710 }
711 return nil, errors.New("UnImplemented")
712}
713
714func (handler *APIHandler) UpdateAlarmFilter(ctx context.Context, filter *voltha.AlarmFilter) (*voltha.AlarmFilter, error) {
715 log.Debugw("UpdateAlarmFilter-request", log.Fields{"filter": *filter})
716 if isTestMode(ctx) {
717 f := &voltha.AlarmFilter{Id: filter.Id}
718 return f, nil
719 }
720 return nil, errors.New("UnImplemented")
721}
722
723func (handler *APIHandler) DeleteAlarmFilter(ctx context.Context, id *voltha.ID) (*empty.Empty, error) {
724 log.Debugw("DeleteAlarmFilter-request", log.Fields{"id": *id})
725 if isTestMode(ctx) {
726 out := new(empty.Empty)
727 return out, nil
728 }
729 return nil, errors.New("UnImplemented")
730}
731
732func (handler *APIHandler) SelfTest(ctx context.Context, id *voltha.ID) (*voltha.SelfTestResponse, error) {
733 log.Debugw("SelfTest-request", log.Fields{"id": id})
734 if isTestMode(ctx) {
735 resp := &voltha.SelfTestResponse{Result: voltha.SelfTestResponse_SUCCESS}
736 return resp, nil
737 }
738 return nil, errors.New("UnImplemented")
739}
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500740
741func (handler *APIHandler) forwardPacketOut(packet *openflow_13.PacketOut) {
742 log.Debugw("forwardPacketOut-request", log.Fields{"packet": packet})
khenaidoo3d3b8c22019-05-22 18:10:39 -0400743 //TODO: Update this logic once the OF Controller (OFAgent in this case) can include a transaction Id in its
744 // request. For performance reason we can let both Cores in a Core-Pair forward the Packet to the adapters and
745 // let once of the shim layer (kafka proxy or adapter request handler filters out the duplicate packet)
746 if handler.core.deviceOwnership.OwnedByMe(&utils.LogicalDeviceID{Id: packet.Id}) {
747 agent := handler.logicalDeviceMgr.getLogicalDeviceAgent(packet.Id)
748 agent.packetOut(packet.PacketOut)
749 }
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500750}
khenaidoo3d3b8c22019-05-22 18:10:39 -0400751
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500752func (handler *APIHandler) StreamPacketsOut(
753 packets voltha.VolthaService_StreamPacketsOutServer,
754) error {
755 log.Debugw("StreamPacketsOut-request", log.Fields{"packets": packets})
756 for {
757 packet, err := packets.Recv()
758
759 if err == io.EOF {
760 break
761 } else if err != nil {
762 log.Errorw("Failed to receive packet", log.Fields{"error": err})
763 }
764
765 handler.forwardPacketOut(packet)
766 }
767
768 log.Debugw("StreamPacketsOut-request-done", log.Fields{"packets": packets})
769 return nil
770}
771
khenaidoo297cd252019-02-07 22:10:23 -0500772func (handler *APIHandler) sendPacketIn(deviceId string, transationId string, packet *openflow_13.OfpPacketIn) {
773 // TODO: Augment the OF PacketIn to include the transactionId
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500774 packetIn := openflow_13.PacketIn{Id: deviceId, PacketIn: packet}
775 log.Debugw("sendPacketIn", log.Fields{"packetIn": packetIn})
Richard Jankowskidbab94a2018-12-06 16:20:25 -0500776 // Enqueue the packet
777 if err := handler.packetInQueue.Put(packetIn); err != nil {
778 log.Errorw("failed-to-enqueue-packet", log.Fields{"error": err})
779 }
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500780}
781
782func (handler *APIHandler) ReceivePacketsIn(
783 empty *empty.Empty,
784 packetsIn voltha.VolthaService_ReceivePacketsInServer,
785) error {
786 log.Debugw("ReceivePacketsIn-request", log.Fields{"packetsIn": packetsIn})
787
788 for {
Richard Jankowskidbab94a2018-12-06 16:20:25 -0500789 // Dequeue a packet
790 if packets, err := handler.packetInQueue.Get(1); err == nil {
791 log.Debugw("dequeued-packet", log.Fields{"packet": packets[0]})
792 if packet, ok := packets[0].(openflow_13.PacketIn); ok {
793 log.Debugw("sending-packet-in", log.Fields{"packet": packet})
794 if err := packetsIn.Send(&packet); err != nil {
795 log.Errorw("failed-to-send-packet", log.Fields{"error": err})
796 }
797 }
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500798 }
799 }
khenaidoof5a5bfa2019-01-23 22:20:29 -0500800 //TODO: FInd an elegant way to get out of the above loop when the Core is stopped
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500801}
802
803func (handler *APIHandler) sendChangeEvent(deviceId string, portStatus *openflow_13.OfpPortStatus) {
804 // TODO: validate the type of portStatus parameter
805 //if _, ok := portStatus.(*openflow_13.OfpPortStatus); ok {
806 //}
807 event := openflow_13.ChangeEvent{Id: deviceId, Event: &openflow_13.ChangeEvent_PortStatus{PortStatus: portStatus}}
808 log.Debugw("sendChangeEvent", log.Fields{"event": event})
Richard Jankowski199fd862019-03-18 14:49:51 -0400809 // Enqueue the change event
810 if err := handler.changeEventQueue.Put(event); err != nil {
811 log.Errorw("failed-to-enqueue-change-event", log.Fields{"error": err})
812 }
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500813}
814
815func (handler *APIHandler) ReceiveChangeEvents(
816 empty *empty.Empty,
817 changeEvents voltha.VolthaService_ReceiveChangeEventsServer,
818) error {
819 log.Debugw("ReceiveChangeEvents-request", log.Fields{"changeEvents": changeEvents})
820 for {
Richard Jankowski199fd862019-03-18 14:49:51 -0400821 // Dequeue a change event
822 if events, err := handler.changeEventQueue.Get(1); err == nil {
823 log.Debugw("dequeued-change-event", log.Fields{"event": events[0]})
824 if event, ok := events[0].(openflow_13.ChangeEvent); ok {
825 log.Debugw("sending-change-event", log.Fields{"event": event})
826 if err := changeEvents.Send(&event); err != nil {
827 log.Errorw("failed-to-send-change-event", log.Fields{"error": err})
828 }
829 }
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500830 }
831 }
Richard Jankowski199fd862019-03-18 14:49:51 -0400832}
Stephane Barbarie6e1bd502018-11-05 22:44:45 -0500833
834func (handler *APIHandler) Subscribe(
835 ctx context.Context,
836 ofAgent *voltha.OfAgentSubscriber,
837) (*voltha.OfAgentSubscriber, error) {
838 log.Debugw("Subscribe-request", log.Fields{"ofAgent": ofAgent})
839 return &voltha.OfAgentSubscriber{OfagentId: ofAgent.OfagentId, VolthaId: ofAgent.VolthaId}, nil
840}
William Kurkiandaa6bb22019-03-07 12:26:28 -0500841
842//@TODO useless stub, what should this actually do?
843func (handler *APIHandler) GetAlarmDeviceData(
844 ctx context.Context,
845 in *common.ID,
846) (*omci.AlarmDeviceData, error) {
847 log.Debug("GetAlarmDeviceData-stub")
848 return nil, nil
849}
850
851//@TODO useless stub, what should this actually do?
852func (handler *APIHandler) GetMeterStatsOfLogicalDevice(
khenaidoo2c6a0992019-04-29 13:46:56 -0400853 ctx context.Context,
William Kurkiandaa6bb22019-03-07 12:26:28 -0500854 in *common.ID,
855) (*openflow_13.MeterStatsReply, error) {
856 log.Debug("GetMeterStatsOfLogicalDevice-stub")
857 return nil, nil
858}
859
860//@TODO useless stub, what should this actually do?
861func (handler *APIHandler) GetMibDeviceData(
khenaidoo2c6a0992019-04-29 13:46:56 -0400862 ctx context.Context,
863 in *common.ID,
William Kurkiandaa6bb22019-03-07 12:26:28 -0500864) (*omci.MibDeviceData, error) {
865 log.Debug("GetMibDeviceData-stub")
866 return nil, nil
867}
868
William Kurkiandaa6bb22019-03-07 12:26:28 -0500869func (handler *APIHandler) SimulateAlarm(
870 ctx context.Context,
871 in *voltha.SimulateAlarmRequest,
872) (*common.OperationResp, error) {
serkant.uluderya334479d2019-04-10 08:26:15 -0700873 log.Debugw("SimulateAlarm-request", log.Fields{"id": in.Id})
874 successResp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS}
875 if isTestMode(ctx) {
876 return successResp, nil
877 }
878
879 if handler.competeForTransaction() {
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400880 if txn, err := handler.takeRequestOwnership(ctx, &utils.DeviceID{Id: in.Id}, handler.longRunningRequestTimeout); err != nil {
881 failedresponse := &common.OperationResp{Code: voltha.OperationResp_OPERATION_FAILURE}
serkant.uluderya334479d2019-04-10 08:26:15 -0700882 return failedresponse, err
883 } else {
884 defer txn.Close()
885 }
886 }
887
888 ch := make(chan interface{})
889 defer close(ch)
890 go handler.deviceMgr.simulateAlarm(ctx, in, ch)
891 return successResp, nil
William Kurkiandaa6bb22019-03-07 12:26:28 -0500892}
893
894//@TODO useless stub, what should this actually do?
895func (handler *APIHandler) UpdateLogicalDeviceMeterTable(
896 ctx context.Context,
897 in *openflow_13.MeterModUpdate,
898) (*empty.Empty, error) {
899 log.Debug("UpdateLogicalDeviceMeterTable-stub")
900 return nil, nil
901}