blob: 57279831d25e6a6caac411482aaa16433159b977 [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"
24 "github.com/opencord/voltha-go/protos/common"
25 "github.com/opencord/voltha-go/protos/voltha"
26 "google.golang.org/grpc/codes"
27 "google.golang.org/grpc/metadata"
28 "google.golang.org/grpc/status"
29)
30
31type APIHandler struct {
32 commonMgr *ModelProxyManager
33 deviceMgr *DeviceManager
34 logicalDeviceMgr *LogicalDeviceManager
35 da.DefaultAPIHandler
36}
37
38func NewAPIHandler(generalMgr *ModelProxyManager, deviceMgr *DeviceManager, lDeviceMgr *LogicalDeviceManager) *APIHandler {
39 handler := &APIHandler{
40 commonMgr: generalMgr,
41 deviceMgr: deviceMgr,
42 logicalDeviceMgr: lDeviceMgr,
43 }
44 return handler
45}
46
47// isTestMode is a helper function to determine a function is invoked for testing only
48func isTestMode(ctx context.Context) bool {
49 md, _ := metadata.FromIncomingContext(ctx)
50 _, exist := md[common.TestModeKeys_api_test.String()]
51 return exist
52}
53
54// waitForNilResponseOnSuccess is a helper function to wait for a response on channel ch where an nil
55// response is expected in a successful scenario
56func waitForNilResponseOnSuccess(ctx context.Context, ch chan interface{}) (*empty.Empty, error) {
57 select {
58 case res := <-ch:
59 if res == nil {
60 return new(empty.Empty), nil
61 } else if err, ok := res.(error); ok {
62 return new(empty.Empty), err
63 } else {
64 log.Warnw("unexpected-return-type", log.Fields{"result": res})
65 err = status.Errorf(codes.Internal, "%s", res)
66 return new(empty.Empty), err
67 }
68 case <-ctx.Done():
69 log.Debug("client-timeout")
70 return nil, ctx.Err()
71 }
72}
73
74// GetVoltha returns the contents of all components (i.e. devices, logical_devices, ...)
75func (handler *APIHandler) GetVoltha(ctx context.Context, empty *empty.Empty) (*voltha.Voltha, error) {
76 log.Debug("GetVoltha")
77 return handler.commonMgr.GetVoltha(ctx)
78}
79
80// ListCoreInstances returns details on the running core containers
81func (handler *APIHandler) ListCoreInstances(ctx context.Context, empty *empty.Empty) (*voltha.CoreInstances, error) {
82 log.Debug("ListCoreInstances")
83 return handler.commonMgr.ListCoreInstances(ctx)
84}
85
86// GetCoreInstance returns the details of a specific core container
87func (handler *APIHandler) GetCoreInstance(ctx context.Context, id *voltha.ID) (*voltha.CoreInstance, error) {
88 log.Debugw("GetCoreInstance", log.Fields{"id": id})
89 return handler.commonMgr.GetCoreInstance(ctx, id.Id)
90}
91
92// ListAdapters returns the contents of all adapters known to the system
93func (handler *APIHandler) ListAdapters(ctx context.Context, empty *empty.Empty) (*voltha.Adapters, error) {
94 log.Debug("ListDevices")
95 return handler.commonMgr.ListAdapters(ctx)
96}
97
98// GetDevice returns the details a specific device
99func (handler *APIHandler) GetDevice(ctx context.Context, id *voltha.ID) (*voltha.Device, error) {
100 log.Debugw("GetDevice-request", log.Fields{"id": id})
101 return handler.deviceMgr.GetDevice(id.Id)
102}
103
104// ListDevices returns the contents of all devices known to the system
105func (handler *APIHandler) ListDevices(ctx context.Context, empty *empty.Empty) (*voltha.Devices, error) {
106 log.Debug("ListDevices")
107 return handler.deviceMgr.ListDevices()
108}
109
110// ListDeviceIds returns the list of device ids managed by a voltha core
111func (handler *APIHandler) ListDeviceIds(ctx context.Context, empty *empty.Empty) (*voltha.IDs, error) {
112 log.Debug("ListDeviceIDs")
113 if isTestMode(ctx) {
114 out := &voltha.IDs{Items: make([]*voltha.ID, 0)}
115 return out, nil
116 }
117 return handler.deviceMgr.ListDeviceIds()
118}
119
120// ListDevicePorts returns the ports details for a specific device entry
121func (handler *APIHandler) ListDevicePorts(ctx context.Context, id *voltha.ID) (*voltha.Ports, error) {
122 log.Debugw("ListDevicePorts", log.Fields{"deviceid": id})
123 return handler.deviceMgr.ListDevicePorts(ctx, id.Id)
124}
125
126// ListDevicePmConfigs returns the PM config details for a specific device entry
127func (handler *APIHandler) ListDevicePmConfigs(ctx context.Context, id *voltha.ID) (*voltha.PmConfigs, error) {
128 log.Debugw("ListDevicePmConfigs", log.Fields{"deviceid": id})
129 return handler.deviceMgr.ListDevicePmConfigs(ctx, id.Id)
130}
131
132// ListDeviceFlows returns the flow details for a specific device entry
133func (handler *APIHandler) ListDeviceFlows(ctx context.Context, id *voltha.ID) (*voltha.Flows, error) {
134 log.Debugw("ListDeviceFlows", log.Fields{"deviceid": id})
135 return handler.deviceMgr.ListDeviceFlows(ctx, id.Id)
136}
137
138// ListDeviceFlowGroups returns the flow group details for a specific device entry
139func (handler *APIHandler) ListDeviceFlowGroups(ctx context.Context, id *voltha.ID) (*voltha.FlowGroups, error) {
140 log.Debugw("ListDeviceFlowGroups", log.Fields{"deviceid": id})
141 return handler.deviceMgr.ListDeviceFlowGroups(ctx, id.Id)
142}
143
144// ListDeviceTypes returns all the device types known to the system
145func (handler *APIHandler) ListDeviceTypes(ctx context.Context, empty *empty.Empty) (*voltha.DeviceTypes, error) {
146 log.Debug("ListDeviceTypes")
147 return handler.commonMgr.ListDeviceTypes(ctx)
148}
149
150// GetDeviceType returns the device type for a specific device entry
151func (handler *APIHandler) GetDeviceType(ctx context.Context, id *voltha.ID) (*voltha.DeviceType, error) {
152 log.Debugw("GetDeviceType", log.Fields{"typeid": id})
153 return handler.commonMgr.GetDeviceType(ctx, id.Id)
154}
155
156// ListDeviceGroups returns all the device groups known to the system
157func (handler *APIHandler) ListDeviceGroups(ctx context.Context, empty *empty.Empty) (*voltha.DeviceGroups, error) {
158 log.Debug("ListDeviceGroups")
159 return handler.commonMgr.ListDeviceGroups(ctx)
160}
161
162// GetDeviceGroup returns a specific device group entry
163func (handler *APIHandler) GetDeviceGroup(ctx context.Context, id *voltha.ID) (*voltha.DeviceGroup, error) {
164 log.Debugw("GetDeviceGroup", log.Fields{"groupid": id})
165 return handler.commonMgr.GetDeviceGroup(ctx, id.Id)
166}
167
168// GetImageDownloadStatus returns the download status for a specific image entry
169func (handler *APIHandler) GetImageDownloadStatus(ctx context.Context, img *voltha.ImageDownload) (*voltha.ImageDownload, error) {
170 log.Debugw("GetImageDownloadStatus", log.Fields{"deviceid": img.GetId(), "imagename": img.GetName()})
171 return handler.deviceMgr.GetImageDownloadStatus(ctx, img.GetId(), img.GetName())
172}
173
174// GetImageDownload return the download details for a specific image entry
175func (handler *APIHandler) GetImageDownload(ctx context.Context, img *voltha.ImageDownload) (*voltha.ImageDownload, error) {
176 log.Debugw("GetImageDownload", log.Fields{"deviceid": img.GetId(), "imagename": img.GetName()})
177 return handler.deviceMgr.GetImageDownload(ctx, img.GetId(), img.GetName())
178}
179
180// ListImageDownloads returns all image downloads known to the system
181func (handler *APIHandler) ListImageDownloads(ctx context.Context, id *voltha.ID) (*voltha.ImageDownloads, error) {
182 log.Debugw("GetImageDownload", log.Fields{"deviceid": id})
183 return handler.deviceMgr.ListImageDownloads(ctx, id.Id)
184}
185
186// GetImages returns all images for a specific device entry
187func (handler *APIHandler) GetImages(ctx context.Context, id *voltha.ID) (*voltha.Images, error) {
188 log.Debugw("GetImages", log.Fields{"deviceid": id})
189 return handler.deviceMgr.GetImages(ctx, id.Id)
190}
191
192// ListAlarmFilters return all alarm filters known to the system
193func (handler *APIHandler) ListAlarmFilters(ctx context.Context, empty *empty.Empty) (*voltha.AlarmFilters, error) {
194 log.Debug("ListAlarmFilters")
195 return handler.commonMgr.ListAlarmFilters(ctx)
196}
197
198// GetAlarmFilter returns a specific alarm filter entry
199func (handler *APIHandler) GetAlarmFilter(ctx context.Context, id *voltha.ID) (*voltha.AlarmFilter, error) {
200 log.Debugw("GetAlarmFilter", log.Fields{"alarmid": id})
201 return handler.commonMgr.GetAlarmFilter(ctx, id.Id)
202}
203
204//ReconcileDevices is a request to a voltha core to managed a list of devices based on their IDs
205func (handler *APIHandler) ReconcileDevices(ctx context.Context, ids *voltha.IDs) (*empty.Empty, error) {
206 log.Debug("ReconcileDevices")
207 if isTestMode(ctx) {
208 out := new(empty.Empty)
209 return out, nil
210 }
211 ch := make(chan interface{})
212 defer close(ch)
213 go handler.deviceMgr.ReconcileDevices(ctx, ids, ch)
214 return waitForNilResponseOnSuccess(ctx, ch)
215}
216
217// GetLogicalDevice returns the details for a specific logical device entry
218func (handler *APIHandler) GetLogicalDevice(ctx context.Context, id *voltha.ID) (*voltha.LogicalDevice, error) {
219 log.Debugw("GetLogicalDevice-request", log.Fields{"id": id})
220 return handler.logicalDeviceMgr.getLogicalDevice(id.Id)
221}
222
223// ListLogicalDevices returns all logical devices known to the system
224func (handler *APIHandler) ListLogicalDevices(ctx context.Context, empty *empty.Empty) (*voltha.LogicalDevices, error) {
225 log.Debug("ListLogicalDevices")
226 return handler.logicalDeviceMgr.listLogicalDevices()
227}
228
229// ListLogicalDevicePorts returns port details for a specific logical device entry
230func (handler *APIHandler) ListLogicalDevicePorts(ctx context.Context, id *voltha.ID) (*voltha.LogicalPorts, error) {
231 log.Debugw("ListLogicalDevicePorts", log.Fields{"logicaldeviceid": id})
232 return handler.logicalDeviceMgr.ListLogicalDevicePorts(ctx, id.Id)
233}
234
235// ListLogicalDeviceFlows returns flow details for a specific logical device entry
236func (handler *APIHandler) ListLogicalDeviceFlows(ctx context.Context, id *voltha.ID) (*voltha.Flows, error) {
237 log.Debugw("ListLogicalDeviceFlows", log.Fields{"logicaldeviceid": id})
238 return handler.logicalDeviceMgr.ListLogicalDeviceFlows(ctx, id.Id)
239}
240
241// ListLogicalDeviceFlowGroups returns flow group details for a specific logical device entry
242func (handler *APIHandler) ListLogicalDeviceFlowGroups(ctx context.Context, id *voltha.ID) (*voltha.FlowGroups, error) {
243 log.Debugw("ListLogicalDeviceFlows", log.Fields{"logicaldeviceid": id})
244 return handler.logicalDeviceMgr.ListLogicalDeviceFlowGroups(ctx, id.Id)
245}
246
247func (handler *APIHandler) SelfTest(ctx context.Context, id *voltha.ID) (*voltha.SelfTestResponse, error) {
248 log.Debugw("SelfTest-request", log.Fields{"id": id})
249 if isTestMode(ctx) {
250 resp := &voltha.SelfTestResponse{Result: voltha.SelfTestResponse_SUCCESS}
251 return resp, nil
252 }
253 return nil, errors.New("UnImplemented")
254}