blob: 8fdc03c1df7d25a92f719cd4c7746d58778706e9 [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"
serkant.uluderya2ae470f2020-01-21 11:13:09 -080025 "github.com/opencord/voltha-lib-go/v3/pkg/log"
26 "github.com/opencord/voltha-protos/v3/go/common"
27 "github.com/opencord/voltha-protos/v3/go/omci"
28 "github.com/opencord/voltha-protos/v3/go/openflow_13"
29 "github.com/opencord/voltha-protos/v3/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,
serkant.uluderya2ae470f2020-01-21 11:13:09 -0800109 Level: voltha.LogLevel_Types(level)}
Scott Baker5f401472019-08-22 08:32:26 -0700110 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",
serkant.uluderya2ae470f2020-01-21 11:13:09 -0800117 Level: voltha.LogLevel_Types(log.GetDefaultLogLevel())}
Scott Baker5f401472019-08-22 08:32:26 -0700118 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")
Thomas Lee Se5a44012019-11-07 20:32:24 +0530156 devices, err := handler.deviceMgr.ListDevices()
157 if err != nil {
158 log.Errorw("failed-to-list-devices", log.Fields{"error": err})
159 return nil, err
160 }
161 return devices, nil
Stephane Barbariea75791c2019-01-24 10:58:06 -0500162}
163
164// ListDeviceIds returns the list of device ids managed by a voltha core
165func (handler *APIHandler) ListDeviceIds(ctx context.Context, empty *empty.Empty) (*voltha.IDs, error) {
166 log.Debug("ListDeviceIDs")
167 if isTestMode(ctx) {
168 out := &voltha.IDs{Items: make([]*voltha.ID, 0)}
169 return out, nil
170 }
npujar03b018e2019-11-13 15:29:36 +0530171 return handler.deviceMgr.ListDeviceIDs()
Stephane Barbariea75791c2019-01-24 10:58:06 -0500172}
173
174// ListDevicePorts returns the ports details for a specific device entry
175func (handler *APIHandler) ListDevicePorts(ctx context.Context, id *voltha.ID) (*voltha.Ports, error) {
176 log.Debugw("ListDevicePorts", log.Fields{"deviceid": id})
177 return handler.deviceMgr.ListDevicePorts(ctx, id.Id)
178}
179
180// ListDevicePmConfigs returns the PM config details for a specific device entry
181func (handler *APIHandler) ListDevicePmConfigs(ctx context.Context, id *voltha.ID) (*voltha.PmConfigs, error) {
182 log.Debugw("ListDevicePmConfigs", log.Fields{"deviceid": id})
183 return handler.deviceMgr.ListDevicePmConfigs(ctx, id.Id)
184}
185
186// ListDeviceFlows returns the flow details for a specific device entry
187func (handler *APIHandler) ListDeviceFlows(ctx context.Context, id *voltha.ID) (*voltha.Flows, error) {
188 log.Debugw("ListDeviceFlows", log.Fields{"deviceid": id})
189 return handler.deviceMgr.ListDeviceFlows(ctx, id.Id)
190}
191
192// ListDeviceFlowGroups returns the flow group details for a specific device entry
193func (handler *APIHandler) ListDeviceFlowGroups(ctx context.Context, id *voltha.ID) (*voltha.FlowGroups, error) {
194 log.Debugw("ListDeviceFlowGroups", log.Fields{"deviceid": id})
195 return handler.deviceMgr.ListDeviceFlowGroups(ctx, id.Id)
196}
197
198// ListDeviceTypes returns all the device types known to the system
199func (handler *APIHandler) ListDeviceTypes(ctx context.Context, empty *empty.Empty) (*voltha.DeviceTypes, error) {
200 log.Debug("ListDeviceTypes")
201 return handler.commonMgr.ListDeviceTypes(ctx)
202}
203
204// GetDeviceType returns the device type for a specific device entry
205func (handler *APIHandler) GetDeviceType(ctx context.Context, id *voltha.ID) (*voltha.DeviceType, error) {
206 log.Debugw("GetDeviceType", log.Fields{"typeid": id})
207 return handler.commonMgr.GetDeviceType(ctx, id.Id)
208}
209
210// ListDeviceGroups returns all the device groups known to the system
211func (handler *APIHandler) ListDeviceGroups(ctx context.Context, empty *empty.Empty) (*voltha.DeviceGroups, error) {
212 log.Debug("ListDeviceGroups")
213 return handler.commonMgr.ListDeviceGroups(ctx)
214}
215
216// GetDeviceGroup returns a specific device group entry
217func (handler *APIHandler) GetDeviceGroup(ctx context.Context, id *voltha.ID) (*voltha.DeviceGroup, error) {
218 log.Debugw("GetDeviceGroup", log.Fields{"groupid": id})
219 return handler.commonMgr.GetDeviceGroup(ctx, id.Id)
220}
221
222// GetImageDownloadStatus returns the download status for a specific image entry
223func (handler *APIHandler) GetImageDownloadStatus(ctx context.Context, img *voltha.ImageDownload) (*voltha.ImageDownload, error) {
224 log.Debugw("GetImageDownloadStatus", log.Fields{"deviceid": img.GetId(), "imagename": img.GetName()})
225 return handler.deviceMgr.GetImageDownloadStatus(ctx, img.GetId(), img.GetName())
226}
227
228// GetImageDownload return the download details for a specific image entry
229func (handler *APIHandler) GetImageDownload(ctx context.Context, img *voltha.ImageDownload) (*voltha.ImageDownload, error) {
230 log.Debugw("GetImageDownload", log.Fields{"deviceid": img.GetId(), "imagename": img.GetName()})
231 return handler.deviceMgr.GetImageDownload(ctx, img.GetId(), img.GetName())
232}
233
234// ListImageDownloads returns all image downloads known to the system
235func (handler *APIHandler) ListImageDownloads(ctx context.Context, id *voltha.ID) (*voltha.ImageDownloads, error) {
236 log.Debugw("GetImageDownload", log.Fields{"deviceid": id})
237 return handler.deviceMgr.ListImageDownloads(ctx, id.Id)
238}
239
240// GetImages returns all images for a specific device entry
241func (handler *APIHandler) GetImages(ctx context.Context, id *voltha.ID) (*voltha.Images, error) {
242 log.Debugw("GetImages", log.Fields{"deviceid": id})
243 return handler.deviceMgr.GetImages(ctx, id.Id)
244}
245
Devmalya Paulc594bb32019-11-06 07:34:27 +0000246// ListEventFilters return all event filters known to the system
247func (handler *APIHandler) ListEventFilters(ctx context.Context, empty *empty.Empty) (*voltha.EventFilters, error) {
248 log.Debug("ListEventFilters")
249 return handler.commonMgr.ListEventFilters(ctx)
Stephane Barbariea75791c2019-01-24 10:58:06 -0500250}
251
Devmalya Paulc594bb32019-11-06 07:34:27 +0000252// GetEventFilter returns a filter for a specific device
253func (handler *APIHandler) GetEventFilter(ctx context.Context, id *voltha.ID) (*voltha.EventFilters, error) {
254 log.Debugw("GetEventFilter", log.Fields{"deviceid": id})
255 return handler.commonMgr.GetEventFilter(ctx, id.Id)
Stephane Barbariea75791c2019-01-24 10:58:06 -0500256}
257
258//ReconcileDevices is a request to a voltha core to managed a list of devices based on their IDs
259func (handler *APIHandler) ReconcileDevices(ctx context.Context, ids *voltha.IDs) (*empty.Empty, error) {
260 log.Debug("ReconcileDevices")
261 if isTestMode(ctx) {
262 out := new(empty.Empty)
263 return out, nil
264 }
265 ch := make(chan interface{})
266 defer close(ch)
267 go handler.deviceMgr.ReconcileDevices(ctx, ids, ch)
268 return waitForNilResponseOnSuccess(ctx, ch)
269}
270
271// GetLogicalDevice returns the details for a specific logical device entry
272func (handler *APIHandler) GetLogicalDevice(ctx context.Context, id *voltha.ID) (*voltha.LogicalDevice, error) {
273 log.Debugw("GetLogicalDevice-request", log.Fields{"id": id})
274 return handler.logicalDeviceMgr.getLogicalDevice(id.Id)
275}
276
277// ListLogicalDevices returns all logical devices known to the system
278func (handler *APIHandler) ListLogicalDevices(ctx context.Context, empty *empty.Empty) (*voltha.LogicalDevices, error) {
279 log.Debug("ListLogicalDevices")
280 return handler.logicalDeviceMgr.listLogicalDevices()
281}
282
283// ListLogicalDevicePorts returns port details for a specific logical device entry
284func (handler *APIHandler) ListLogicalDevicePorts(ctx context.Context, id *voltha.ID) (*voltha.LogicalPorts, error) {
285 log.Debugw("ListLogicalDevicePorts", log.Fields{"logicaldeviceid": id})
286 return handler.logicalDeviceMgr.ListLogicalDevicePorts(ctx, id.Id)
287}
288
289// ListLogicalDeviceFlows returns flow details for a specific logical device entry
290func (handler *APIHandler) ListLogicalDeviceFlows(ctx context.Context, id *voltha.ID) (*voltha.Flows, error) {
291 log.Debugw("ListLogicalDeviceFlows", log.Fields{"logicaldeviceid": id})
292 return handler.logicalDeviceMgr.ListLogicalDeviceFlows(ctx, id.Id)
293}
294
295// ListLogicalDeviceFlowGroups returns flow group details for a specific logical device entry
296func (handler *APIHandler) ListLogicalDeviceFlowGroups(ctx context.Context, id *voltha.ID) (*voltha.FlowGroups, error) {
297 log.Debugw("ListLogicalDeviceFlows", log.Fields{"logicaldeviceid": id})
298 return handler.logicalDeviceMgr.ListLogicalDeviceFlowGroups(ctx, id.Id)
299}
300
npujar03b018e2019-11-13 15:29:36 +0530301// SelfTest - TODO
Stephane Barbariea75791c2019-01-24 10:58:06 -0500302func (handler *APIHandler) SelfTest(ctx context.Context, id *voltha.ID) (*voltha.SelfTestResponse, error) {
303 log.Debugw("SelfTest-request", log.Fields{"id": id})
304 if isTestMode(ctx) {
305 resp := &voltha.SelfTestResponse{Result: voltha.SelfTestResponse_SUCCESS}
306 return resp, nil
307 }
308 return nil, errors.New("UnImplemented")
309}
William Kurkiandaa6bb22019-03-07 12:26:28 -0500310
npujar03b018e2019-11-13 15:29:36 +0530311// GetAlarmDeviceData - @TODO useless stub, what should this actually do?
William Kurkiandaa6bb22019-03-07 12:26:28 -0500312func (handler *APIHandler) GetAlarmDeviceData(
313 ctx context.Context,
314 in *common.ID,
315) (*omci.AlarmDeviceData, error) {
316 log.Debug("GetAlarmDeviceData-stub")
317 return nil, nil
318}
319
npujar03b018e2019-11-13 15:29:36 +0530320// GetMeterStatsOfLogicalDevice - @TODO useless stub, what should this actually do?
William Kurkiandaa6bb22019-03-07 12:26:28 -0500321func (handler *APIHandler) GetMeterStatsOfLogicalDevice(
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400322 ctx context.Context,
William Kurkiandaa6bb22019-03-07 12:26:28 -0500323 in *common.ID,
324) (*openflow_13.MeterStatsReply, error) {
325 log.Debug("GetMeterStatsOfLogicalDevice-stub")
326 return nil, nil
327}
328
npujar03b018e2019-11-13 15:29:36 +0530329// GetMibDeviceData - @TODO useless stub, what should this actually do?
William Kurkiandaa6bb22019-03-07 12:26:28 -0500330func (handler *APIHandler) GetMibDeviceData(
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400331 ctx context.Context,
332 in *common.ID,
William Kurkiandaa6bb22019-03-07 12:26:28 -0500333) (*omci.MibDeviceData, error) {
334 log.Debug("GetMibDeviceData-stub")
335 return nil, nil
336}
337
npujar03b018e2019-11-13 15:29:36 +0530338// SimulateAlarm - @TODO useless stub, what should this actually do?
William Kurkiandaa6bb22019-03-07 12:26:28 -0500339func (handler *APIHandler) SimulateAlarm(
340 ctx context.Context,
341 in *voltha.SimulateAlarmRequest,
342) (*common.OperationResp, error) {
343 log.Debug("SimulateAlarm-stub")
344 return nil, nil
345}