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