blob: 6d50e9e9babfcee3ab6af6f59976742245eee230 [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
76// GetVoltha returns the contents of all components (i.e. devices, logical_devices, ...)
77func (handler *APIHandler) GetVoltha(ctx context.Context, empty *empty.Empty) (*voltha.Voltha, error) {
78 log.Debug("GetVoltha")
79 return handler.commonMgr.GetVoltha(ctx)
80}
81
82// ListCoreInstances returns details on the running core containers
83func (handler *APIHandler) ListCoreInstances(ctx context.Context, empty *empty.Empty) (*voltha.CoreInstances, error) {
84 log.Debug("ListCoreInstances")
85 return handler.commonMgr.ListCoreInstances(ctx)
86}
87
88// GetCoreInstance returns the details of a specific core container
89func (handler *APIHandler) GetCoreInstance(ctx context.Context, id *voltha.ID) (*voltha.CoreInstance, error) {
90 log.Debugw("GetCoreInstance", log.Fields{"id": id})
91 return handler.commonMgr.GetCoreInstance(ctx, id.Id)
92}
93
94// ListAdapters returns the contents of all adapters known to the system
95func (handler *APIHandler) ListAdapters(ctx context.Context, empty *empty.Empty) (*voltha.Adapters, error) {
96 log.Debug("ListDevices")
97 return handler.commonMgr.ListAdapters(ctx)
98}
99
100// GetDevice returns the details a specific device
101func (handler *APIHandler) GetDevice(ctx context.Context, id *voltha.ID) (*voltha.Device, error) {
102 log.Debugw("GetDevice-request", log.Fields{"id": id})
103 return handler.deviceMgr.GetDevice(id.Id)
104}
105
106// ListDevices returns the contents of all devices known to the system
107func (handler *APIHandler) ListDevices(ctx context.Context, empty *empty.Empty) (*voltha.Devices, error) {
108 log.Debug("ListDevices")
109 return handler.deviceMgr.ListDevices()
110}
111
112// ListDeviceIds returns the list of device ids managed by a voltha core
113func (handler *APIHandler) ListDeviceIds(ctx context.Context, empty *empty.Empty) (*voltha.IDs, error) {
114 log.Debug("ListDeviceIDs")
115 if isTestMode(ctx) {
116 out := &voltha.IDs{Items: make([]*voltha.ID, 0)}
117 return out, nil
118 }
119 return handler.deviceMgr.ListDeviceIds()
120}
121
122// ListDevicePorts returns the ports details for a specific device entry
123func (handler *APIHandler) ListDevicePorts(ctx context.Context, id *voltha.ID) (*voltha.Ports, error) {
124 log.Debugw("ListDevicePorts", log.Fields{"deviceid": id})
125 return handler.deviceMgr.ListDevicePorts(ctx, id.Id)
126}
127
128// ListDevicePmConfigs returns the PM config details for a specific device entry
129func (handler *APIHandler) ListDevicePmConfigs(ctx context.Context, id *voltha.ID) (*voltha.PmConfigs, error) {
130 log.Debugw("ListDevicePmConfigs", log.Fields{"deviceid": id})
131 return handler.deviceMgr.ListDevicePmConfigs(ctx, id.Id)
132}
133
134// ListDeviceFlows returns the flow details for a specific device entry
135func (handler *APIHandler) ListDeviceFlows(ctx context.Context, id *voltha.ID) (*voltha.Flows, error) {
136 log.Debugw("ListDeviceFlows", log.Fields{"deviceid": id})
137 return handler.deviceMgr.ListDeviceFlows(ctx, id.Id)
138}
139
140// ListDeviceFlowGroups returns the flow group details for a specific device entry
141func (handler *APIHandler) ListDeviceFlowGroups(ctx context.Context, id *voltha.ID) (*voltha.FlowGroups, error) {
142 log.Debugw("ListDeviceFlowGroups", log.Fields{"deviceid": id})
143 return handler.deviceMgr.ListDeviceFlowGroups(ctx, id.Id)
144}
145
146// ListDeviceTypes returns all the device types known to the system
147func (handler *APIHandler) ListDeviceTypes(ctx context.Context, empty *empty.Empty) (*voltha.DeviceTypes, error) {
148 log.Debug("ListDeviceTypes")
149 return handler.commonMgr.ListDeviceTypes(ctx)
150}
151
152// GetDeviceType returns the device type for a specific device entry
153func (handler *APIHandler) GetDeviceType(ctx context.Context, id *voltha.ID) (*voltha.DeviceType, error) {
154 log.Debugw("GetDeviceType", log.Fields{"typeid": id})
155 return handler.commonMgr.GetDeviceType(ctx, id.Id)
156}
157
158// ListDeviceGroups returns all the device groups known to the system
159func (handler *APIHandler) ListDeviceGroups(ctx context.Context, empty *empty.Empty) (*voltha.DeviceGroups, error) {
160 log.Debug("ListDeviceGroups")
161 return handler.commonMgr.ListDeviceGroups(ctx)
162}
163
164// GetDeviceGroup returns a specific device group entry
165func (handler *APIHandler) GetDeviceGroup(ctx context.Context, id *voltha.ID) (*voltha.DeviceGroup, error) {
166 log.Debugw("GetDeviceGroup", log.Fields{"groupid": id})
167 return handler.commonMgr.GetDeviceGroup(ctx, id.Id)
168}
169
170// GetImageDownloadStatus returns the download status for a specific image entry
171func (handler *APIHandler) GetImageDownloadStatus(ctx context.Context, img *voltha.ImageDownload) (*voltha.ImageDownload, error) {
172 log.Debugw("GetImageDownloadStatus", log.Fields{"deviceid": img.GetId(), "imagename": img.GetName()})
173 return handler.deviceMgr.GetImageDownloadStatus(ctx, img.GetId(), img.GetName())
174}
175
176// GetImageDownload return the download details for a specific image entry
177func (handler *APIHandler) GetImageDownload(ctx context.Context, img *voltha.ImageDownload) (*voltha.ImageDownload, error) {
178 log.Debugw("GetImageDownload", log.Fields{"deviceid": img.GetId(), "imagename": img.GetName()})
179 return handler.deviceMgr.GetImageDownload(ctx, img.GetId(), img.GetName())
180}
181
182// ListImageDownloads returns all image downloads known to the system
183func (handler *APIHandler) ListImageDownloads(ctx context.Context, id *voltha.ID) (*voltha.ImageDownloads, error) {
184 log.Debugw("GetImageDownload", log.Fields{"deviceid": id})
185 return handler.deviceMgr.ListImageDownloads(ctx, id.Id)
186}
187
188// GetImages returns all images for a specific device entry
189func (handler *APIHandler) GetImages(ctx context.Context, id *voltha.ID) (*voltha.Images, error) {
190 log.Debugw("GetImages", log.Fields{"deviceid": id})
191 return handler.deviceMgr.GetImages(ctx, id.Id)
192}
193
194// ListAlarmFilters return all alarm filters known to the system
195func (handler *APIHandler) ListAlarmFilters(ctx context.Context, empty *empty.Empty) (*voltha.AlarmFilters, error) {
196 log.Debug("ListAlarmFilters")
197 return handler.commonMgr.ListAlarmFilters(ctx)
198}
199
200// GetAlarmFilter returns a specific alarm filter entry
201func (handler *APIHandler) GetAlarmFilter(ctx context.Context, id *voltha.ID) (*voltha.AlarmFilter, error) {
202 log.Debugw("GetAlarmFilter", log.Fields{"alarmid": id})
203 return handler.commonMgr.GetAlarmFilter(ctx, id.Id)
204}
205
206//ReconcileDevices is a request to a voltha core to managed a list of devices based on their IDs
207func (handler *APIHandler) ReconcileDevices(ctx context.Context, ids *voltha.IDs) (*empty.Empty, error) {
208 log.Debug("ReconcileDevices")
209 if isTestMode(ctx) {
210 out := new(empty.Empty)
211 return out, nil
212 }
213 ch := make(chan interface{})
214 defer close(ch)
215 go handler.deviceMgr.ReconcileDevices(ctx, ids, ch)
216 return waitForNilResponseOnSuccess(ctx, ch)
217}
218
219// GetLogicalDevice returns the details for a specific logical device entry
220func (handler *APIHandler) GetLogicalDevice(ctx context.Context, id *voltha.ID) (*voltha.LogicalDevice, error) {
221 log.Debugw("GetLogicalDevice-request", log.Fields{"id": id})
222 return handler.logicalDeviceMgr.getLogicalDevice(id.Id)
223}
224
225// ListLogicalDevices returns all logical devices known to the system
226func (handler *APIHandler) ListLogicalDevices(ctx context.Context, empty *empty.Empty) (*voltha.LogicalDevices, error) {
227 log.Debug("ListLogicalDevices")
228 return handler.logicalDeviceMgr.listLogicalDevices()
229}
230
231// ListLogicalDevicePorts returns port details for a specific logical device entry
232func (handler *APIHandler) ListLogicalDevicePorts(ctx context.Context, id *voltha.ID) (*voltha.LogicalPorts, error) {
233 log.Debugw("ListLogicalDevicePorts", log.Fields{"logicaldeviceid": id})
234 return handler.logicalDeviceMgr.ListLogicalDevicePorts(ctx, id.Id)
235}
236
237// ListLogicalDeviceFlows returns flow details for a specific logical device entry
238func (handler *APIHandler) ListLogicalDeviceFlows(ctx context.Context, id *voltha.ID) (*voltha.Flows, error) {
239 log.Debugw("ListLogicalDeviceFlows", log.Fields{"logicaldeviceid": id})
240 return handler.logicalDeviceMgr.ListLogicalDeviceFlows(ctx, id.Id)
241}
242
243// ListLogicalDeviceFlowGroups returns flow group details for a specific logical device entry
244func (handler *APIHandler) ListLogicalDeviceFlowGroups(ctx context.Context, id *voltha.ID) (*voltha.FlowGroups, error) {
245 log.Debugw("ListLogicalDeviceFlows", log.Fields{"logicaldeviceid": id})
246 return handler.logicalDeviceMgr.ListLogicalDeviceFlowGroups(ctx, id.Id)
247}
248
249func (handler *APIHandler) SelfTest(ctx context.Context, id *voltha.ID) (*voltha.SelfTestResponse, error) {
250 log.Debugw("SelfTest-request", log.Fields{"id": id})
251 if isTestMode(ctx) {
252 resp := &voltha.SelfTestResponse{Result: voltha.SelfTestResponse_SUCCESS}
253 return resp, nil
254 }
255 return nil, errors.New("UnImplemented")
256}
William Kurkiandaa6bb22019-03-07 12:26:28 -0500257
258//@TODO useless stub, what should this actually do?
259func (handler *APIHandler) GetAlarmDeviceData(
260 ctx context.Context,
261 in *common.ID,
262) (*omci.AlarmDeviceData, error) {
263 log.Debug("GetAlarmDeviceData-stub")
264 return nil, nil
265}
266
267//@TODO useless stub, what should this actually do?
268func (handler *APIHandler) GetMeterStatsOfLogicalDevice(
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400269 ctx context.Context,
William Kurkiandaa6bb22019-03-07 12:26:28 -0500270 in *common.ID,
271) (*openflow_13.MeterStatsReply, error) {
272 log.Debug("GetMeterStatsOfLogicalDevice-stub")
273 return nil, nil
274}
275
276//@TODO useless stub, what should this actually do?
277func (handler *APIHandler) GetMibDeviceData(
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400278 ctx context.Context,
279 in *common.ID,
William Kurkiandaa6bb22019-03-07 12:26:28 -0500280) (*omci.MibDeviceData, error) {
281 log.Debug("GetMibDeviceData-stub")
282 return nil, nil
283}
284
285//@TODO useless stub, what should this actually do?
286func (handler *APIHandler) SimulateAlarm(
287 ctx context.Context,
288 in *voltha.SimulateAlarmRequest,
289) (*common.OperationResp, error) {
290 log.Debug("SimulateAlarm-stub")
291 return nil, nil
292}
293
294//@TODO useless stub, what should this actually do?
295func (handler *APIHandler) UpdateLogicalDeviceMeterTable(
296 ctx context.Context,
297 in *openflow_13.MeterModUpdate,
298) (*empty.Empty, error) {
299 log.Debug("UpdateLogicalDeviceMeterTable-stub")
300 return nil, nil
301}