blob: ee8d73602ad21c6c3f45218082c10570297b1465 [file] [log] [blame]
Stephane Barbariea75791c2019-01-24 10:58:06 -05001/*
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 */
npujar03b018e2019-11-13 15:29:36 +053016
Stephane Barbariea75791c2019-01-24 10:58:06 -050017package core
18
19import (
20 "context"
21 "errors"
npujar03b018e2019-11-13 15:29:36 +053022
Stephane Barbariea75791c2019-01-24 10:58:06 -050023 "github.com/golang/protobuf/ptypes/empty"
24 da "github.com/opencord/voltha-go/common/core/northbound/grpc"
Scott Baker807addd2019-10-24 15:16:21 -070025 "github.com/opencord/voltha-lib-go/v2/pkg/log"
Scott Baker555307d2019-11-04 08:58:01 -080026 "github.com/opencord/voltha-protos/v2/go/common"
27 "github.com/opencord/voltha-protos/v2/go/omci"
28 "github.com/opencord/voltha-protos/v2/go/openflow_13"
29 "github.com/opencord/voltha-protos/v2/go/voltha"
Stephane Barbariea75791c2019-01-24 10:58:06 -050030 "google.golang.org/grpc/codes"
31 "google.golang.org/grpc/metadata"
32 "google.golang.org/grpc/status"
33)
34
npujar03b018e2019-11-13 15:29:36 +053035// APIHandler represents API handler related information
Stephane Barbariea75791c2019-01-24 10:58:06 -050036type APIHandler struct {
37 commonMgr *ModelProxyManager
38 deviceMgr *DeviceManager
39 logicalDeviceMgr *LogicalDeviceManager
40 da.DefaultAPIHandler
41}
42
npujar03b018e2019-11-13 15:29:36 +053043// NewAPIHandler creates API handler
Stephane Barbariea75791c2019-01-24 10:58:06 -050044func NewAPIHandler(generalMgr *ModelProxyManager, deviceMgr *DeviceManager, lDeviceMgr *LogicalDeviceManager) *APIHandler {
45 handler := &APIHandler{
46 commonMgr: generalMgr,
47 deviceMgr: deviceMgr,
48 logicalDeviceMgr: lDeviceMgr,
49 }
50 return handler
51}
52
53// isTestMode is a helper function to determine a function is invoked for testing only
54func isTestMode(ctx context.Context) bool {
55 md, _ := metadata.FromIncomingContext(ctx)
56 _, exist := md[common.TestModeKeys_api_test.String()]
57 return exist
58}
59
60// waitForNilResponseOnSuccess is a helper function to wait for a response on channel ch where an nil
61// response is expected in a successful scenario
62func waitForNilResponseOnSuccess(ctx context.Context, ch chan interface{}) (*empty.Empty, error) {
63 select {
64 case res := <-ch:
65 if res == nil {
66 return new(empty.Empty), nil
67 } else if err, ok := res.(error); ok {
68 return new(empty.Empty), err
69 } else {
70 log.Warnw("unexpected-return-type", log.Fields{"result": res})
71 err = status.Errorf(codes.Internal, "%s", res)
72 return new(empty.Empty), err
73 }
74 case <-ctx.Done():
75 log.Debug("client-timeout")
76 return nil, ctx.Err()
77 }
78}
79
npujar03b018e2019-11-13 15:29:36 +053080// UpdateLogLevel updates log level to the requested level in specific packaged if mentioned
81func (*APIHandler) UpdateLogLevel(ctx context.Context, logging *voltha.Logging) (*empty.Empty, error) {
Scott Baker5f401472019-08-22 08:32:26 -070082 log.Debugw("UpdateLogLevel-request", log.Fields{"package": logging.PackageName, "intval": int(logging.Level)})
83 out := new(empty.Empty)
84 if logging.PackageName == "" {
85 log.SetAllLogLevel(int(logging.Level))
86 log.SetDefaultLogLevel(int(logging.Level))
87 } else if logging.PackageName == "default" {
88 log.SetDefaultLogLevel(int(logging.Level))
89 } else {
90 log.SetPackageLogLevel(logging.PackageName, int(logging.Level))
91 }
92
93 return out, nil
94}
95
npujar03b018e2019-11-13 15:29:36 +053096// GetLogLevels returns log levels for requested packages
97func (APIHandler) GetLogLevels(ctx context.Context, in *voltha.LoggingComponent) (*voltha.Loggings, error) {
Scott Baker5f401472019-08-22 08:32:26 -070098 logLevels := &voltha.Loggings{}
99
100 // do the per-package log levels
101 for _, packageName := range log.GetPackageNames() {
102 level, err := log.GetPackageLogLevel(packageName)
103 if err != nil {
104 return nil, err
105 }
106 logLevel := &voltha.Logging{
107 ComponentName: in.ComponentName,
108 PackageName: packageName,
109 Level: voltha.LogLevel_LogLevel(level)}
110 logLevels.Items = append(logLevels.Items, logLevel)
111 }
112
113 // now do the default log level
114 logLevel := &voltha.Logging{
115 ComponentName: in.ComponentName,
116 PackageName: "default",
117 Level: voltha.LogLevel_LogLevel(log.GetDefaultLogLevel())}
118 logLevels.Items = append(logLevels.Items, logLevel)
119
120 return logLevels, nil
121}
122
Stephane Barbariea75791c2019-01-24 10:58:06 -0500123// GetVoltha returns the contents of all components (i.e. devices, logical_devices, ...)
124func (handler *APIHandler) GetVoltha(ctx context.Context, empty *empty.Empty) (*voltha.Voltha, error) {
125 log.Debug("GetVoltha")
126 return handler.commonMgr.GetVoltha(ctx)
127}
128
129// ListCoreInstances returns details on the running core containers
130func (handler *APIHandler) ListCoreInstances(ctx context.Context, empty *empty.Empty) (*voltha.CoreInstances, error) {
131 log.Debug("ListCoreInstances")
132 return handler.commonMgr.ListCoreInstances(ctx)
133}
134
135// GetCoreInstance returns the details of a specific core container
136func (handler *APIHandler) GetCoreInstance(ctx context.Context, id *voltha.ID) (*voltha.CoreInstance, error) {
137 log.Debugw("GetCoreInstance", log.Fields{"id": id})
138 return handler.commonMgr.GetCoreInstance(ctx, id.Id)
139}
140
141// ListAdapters returns the contents of all adapters known to the system
142func (handler *APIHandler) ListAdapters(ctx context.Context, empty *empty.Empty) (*voltha.Adapters, error) {
143 log.Debug("ListDevices")
144 return handler.commonMgr.ListAdapters(ctx)
145}
146
147// GetDevice returns the details a specific device
148func (handler *APIHandler) GetDevice(ctx context.Context, id *voltha.ID) (*voltha.Device, error) {
149 log.Debugw("GetDevice-request", log.Fields{"id": id})
150 return handler.deviceMgr.GetDevice(id.Id)
151}
152
153// ListDevices returns the contents of all devices known to the system
154func (handler *APIHandler) ListDevices(ctx context.Context, empty *empty.Empty) (*voltha.Devices, error) {
155 log.Debug("ListDevices")
156 return handler.deviceMgr.ListDevices()
157}
158
159// ListDeviceIds returns the list of device ids managed by a voltha core
160func (handler *APIHandler) ListDeviceIds(ctx context.Context, empty *empty.Empty) (*voltha.IDs, error) {
161 log.Debug("ListDeviceIDs")
162 if isTestMode(ctx) {
163 out := &voltha.IDs{Items: make([]*voltha.ID, 0)}
164 return out, nil
165 }
npujar03b018e2019-11-13 15:29:36 +0530166 return handler.deviceMgr.ListDeviceIDs()
Stephane Barbariea75791c2019-01-24 10:58:06 -0500167}
168
169// ListDevicePorts returns the ports details for a specific device entry
170func (handler *APIHandler) ListDevicePorts(ctx context.Context, id *voltha.ID) (*voltha.Ports, error) {
171 log.Debugw("ListDevicePorts", log.Fields{"deviceid": id})
172 return handler.deviceMgr.ListDevicePorts(ctx, id.Id)
173}
174
175// ListDevicePmConfigs returns the PM config details for a specific device entry
176func (handler *APIHandler) ListDevicePmConfigs(ctx context.Context, id *voltha.ID) (*voltha.PmConfigs, error) {
177 log.Debugw("ListDevicePmConfigs", log.Fields{"deviceid": id})
178 return handler.deviceMgr.ListDevicePmConfigs(ctx, id.Id)
179}
180
181// ListDeviceFlows returns the flow details for a specific device entry
182func (handler *APIHandler) ListDeviceFlows(ctx context.Context, id *voltha.ID) (*voltha.Flows, error) {
183 log.Debugw("ListDeviceFlows", log.Fields{"deviceid": id})
184 return handler.deviceMgr.ListDeviceFlows(ctx, id.Id)
185}
186
187// ListDeviceFlowGroups returns the flow group details for a specific device entry
188func (handler *APIHandler) ListDeviceFlowGroups(ctx context.Context, id *voltha.ID) (*voltha.FlowGroups, error) {
189 log.Debugw("ListDeviceFlowGroups", log.Fields{"deviceid": id})
190 return handler.deviceMgr.ListDeviceFlowGroups(ctx, id.Id)
191}
192
193// ListDeviceTypes returns all the device types known to the system
194func (handler *APIHandler) ListDeviceTypes(ctx context.Context, empty *empty.Empty) (*voltha.DeviceTypes, error) {
195 log.Debug("ListDeviceTypes")
196 return handler.commonMgr.ListDeviceTypes(ctx)
197}
198
199// GetDeviceType returns the device type for a specific device entry
200func (handler *APIHandler) GetDeviceType(ctx context.Context, id *voltha.ID) (*voltha.DeviceType, error) {
201 log.Debugw("GetDeviceType", log.Fields{"typeid": id})
202 return handler.commonMgr.GetDeviceType(ctx, id.Id)
203}
204
205// ListDeviceGroups returns all the device groups known to the system
206func (handler *APIHandler) ListDeviceGroups(ctx context.Context, empty *empty.Empty) (*voltha.DeviceGroups, error) {
207 log.Debug("ListDeviceGroups")
208 return handler.commonMgr.ListDeviceGroups(ctx)
209}
210
211// GetDeviceGroup returns a specific device group entry
212func (handler *APIHandler) GetDeviceGroup(ctx context.Context, id *voltha.ID) (*voltha.DeviceGroup, error) {
213 log.Debugw("GetDeviceGroup", log.Fields{"groupid": id})
214 return handler.commonMgr.GetDeviceGroup(ctx, id.Id)
215}
216
217// GetImageDownloadStatus returns the download status for a specific image entry
218func (handler *APIHandler) GetImageDownloadStatus(ctx context.Context, img *voltha.ImageDownload) (*voltha.ImageDownload, error) {
219 log.Debugw("GetImageDownloadStatus", log.Fields{"deviceid": img.GetId(), "imagename": img.GetName()})
220 return handler.deviceMgr.GetImageDownloadStatus(ctx, img.GetId(), img.GetName())
221}
222
223// GetImageDownload return the download details for a specific image entry
224func (handler *APIHandler) GetImageDownload(ctx context.Context, img *voltha.ImageDownload) (*voltha.ImageDownload, error) {
225 log.Debugw("GetImageDownload", log.Fields{"deviceid": img.GetId(), "imagename": img.GetName()})
226 return handler.deviceMgr.GetImageDownload(ctx, img.GetId(), img.GetName())
227}
228
229// ListImageDownloads returns all image downloads known to the system
230func (handler *APIHandler) ListImageDownloads(ctx context.Context, id *voltha.ID) (*voltha.ImageDownloads, error) {
231 log.Debugw("GetImageDownload", log.Fields{"deviceid": id})
232 return handler.deviceMgr.ListImageDownloads(ctx, id.Id)
233}
234
235// GetImages returns all images for a specific device entry
236func (handler *APIHandler) GetImages(ctx context.Context, id *voltha.ID) (*voltha.Images, error) {
237 log.Debugw("GetImages", log.Fields{"deviceid": id})
238 return handler.deviceMgr.GetImages(ctx, id.Id)
239}
240
241// ListAlarmFilters return all alarm filters known to the system
242func (handler *APIHandler) ListAlarmFilters(ctx context.Context, empty *empty.Empty) (*voltha.AlarmFilters, error) {
243 log.Debug("ListAlarmFilters")
244 return handler.commonMgr.ListAlarmFilters(ctx)
245}
246
247// GetAlarmFilter returns a specific alarm filter entry
248func (handler *APIHandler) GetAlarmFilter(ctx context.Context, id *voltha.ID) (*voltha.AlarmFilter, error) {
249 log.Debugw("GetAlarmFilter", log.Fields{"alarmid": id})
250 return handler.commonMgr.GetAlarmFilter(ctx, id.Id)
251}
252
253//ReconcileDevices is a request to a voltha core to managed a list of devices based on their IDs
254func (handler *APIHandler) ReconcileDevices(ctx context.Context, ids *voltha.IDs) (*empty.Empty, error) {
255 log.Debug("ReconcileDevices")
256 if isTestMode(ctx) {
257 out := new(empty.Empty)
258 return out, nil
259 }
260 ch := make(chan interface{})
261 defer close(ch)
262 go handler.deviceMgr.ReconcileDevices(ctx, ids, ch)
263 return waitForNilResponseOnSuccess(ctx, ch)
264}
265
266// GetLogicalDevice returns the details for a specific logical device entry
267func (handler *APIHandler) GetLogicalDevice(ctx context.Context, id *voltha.ID) (*voltha.LogicalDevice, error) {
268 log.Debugw("GetLogicalDevice-request", log.Fields{"id": id})
269 return handler.logicalDeviceMgr.getLogicalDevice(id.Id)
270}
271
272// ListLogicalDevices returns all logical devices known to the system
273func (handler *APIHandler) ListLogicalDevices(ctx context.Context, empty *empty.Empty) (*voltha.LogicalDevices, error) {
274 log.Debug("ListLogicalDevices")
275 return handler.logicalDeviceMgr.listLogicalDevices()
276}
277
278// ListLogicalDevicePorts returns port details for a specific logical device entry
279func (handler *APIHandler) ListLogicalDevicePorts(ctx context.Context, id *voltha.ID) (*voltha.LogicalPorts, error) {
280 log.Debugw("ListLogicalDevicePorts", log.Fields{"logicaldeviceid": id})
281 return handler.logicalDeviceMgr.ListLogicalDevicePorts(ctx, id.Id)
282}
283
284// ListLogicalDeviceFlows returns flow details for a specific logical device entry
285func (handler *APIHandler) ListLogicalDeviceFlows(ctx context.Context, id *voltha.ID) (*voltha.Flows, error) {
286 log.Debugw("ListLogicalDeviceFlows", log.Fields{"logicaldeviceid": id})
287 return handler.logicalDeviceMgr.ListLogicalDeviceFlows(ctx, id.Id)
288}
289
290// ListLogicalDeviceFlowGroups returns flow group details for a specific logical device entry
291func (handler *APIHandler) ListLogicalDeviceFlowGroups(ctx context.Context, id *voltha.ID) (*voltha.FlowGroups, error) {
292 log.Debugw("ListLogicalDeviceFlows", log.Fields{"logicaldeviceid": id})
293 return handler.logicalDeviceMgr.ListLogicalDeviceFlowGroups(ctx, id.Id)
294}
295
npujar03b018e2019-11-13 15:29:36 +0530296// SelfTest - TODO
Stephane Barbariea75791c2019-01-24 10:58:06 -0500297func (handler *APIHandler) SelfTest(ctx context.Context, id *voltha.ID) (*voltha.SelfTestResponse, error) {
298 log.Debugw("SelfTest-request", log.Fields{"id": id})
299 if isTestMode(ctx) {
300 resp := &voltha.SelfTestResponse{Result: voltha.SelfTestResponse_SUCCESS}
301 return resp, nil
302 }
303 return nil, errors.New("UnImplemented")
304}
William Kurkiandaa6bb22019-03-07 12:26:28 -0500305
npujar03b018e2019-11-13 15:29:36 +0530306// GetAlarmDeviceData - @TODO useless stub, what should this actually do?
William Kurkiandaa6bb22019-03-07 12:26:28 -0500307func (handler *APIHandler) GetAlarmDeviceData(
308 ctx context.Context,
309 in *common.ID,
310) (*omci.AlarmDeviceData, error) {
311 log.Debug("GetAlarmDeviceData-stub")
312 return nil, nil
313}
314
npujar03b018e2019-11-13 15:29:36 +0530315// GetMeterStatsOfLogicalDevice - @TODO useless stub, what should this actually do?
William Kurkiandaa6bb22019-03-07 12:26:28 -0500316func (handler *APIHandler) GetMeterStatsOfLogicalDevice(
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400317 ctx context.Context,
William Kurkiandaa6bb22019-03-07 12:26:28 -0500318 in *common.ID,
319) (*openflow_13.MeterStatsReply, error) {
320 log.Debug("GetMeterStatsOfLogicalDevice-stub")
321 return nil, nil
322}
323
npujar03b018e2019-11-13 15:29:36 +0530324// GetMibDeviceData - @TODO useless stub, what should this actually do?
William Kurkiandaa6bb22019-03-07 12:26:28 -0500325func (handler *APIHandler) GetMibDeviceData(
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400326 ctx context.Context,
327 in *common.ID,
William Kurkiandaa6bb22019-03-07 12:26:28 -0500328) (*omci.MibDeviceData, error) {
329 log.Debug("GetMibDeviceData-stub")
330 return nil, nil
331}
332
npujar03b018e2019-11-13 15:29:36 +0530333// SimulateAlarm - @TODO useless stub, what should this actually do?
William Kurkiandaa6bb22019-03-07 12:26:28 -0500334func (handler *APIHandler) SimulateAlarm(
335 ctx context.Context,
336 in *voltha.SimulateAlarmRequest,
337) (*common.OperationResp, error) {
338 log.Debug("SimulateAlarm-stub")
339 return nil, nil
340}