blob: d446438b401ce44f8016eb43c9731c2b0f124496 [file] [log] [blame]
khenaidoobf6e7bb2018-08-14 22:27:29 -04001/*
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 */
khenaidoob9203542018-09-17 22:56:37 -040016package core
khenaidoobf6e7bb2018-08-14 22:27:29 -040017
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/openflow_13"
26 "github.com/opencord/voltha-go/protos/voltha"
khenaidoob9203542018-09-17 22:56:37 -040027 "google.golang.org/grpc/codes"
khenaidoobf6e7bb2018-08-14 22:27:29 -040028 "google.golang.org/grpc/metadata"
khenaidoob9203542018-09-17 22:56:37 -040029 "google.golang.org/grpc/status"
khenaidoobf6e7bb2018-08-14 22:27:29 -040030)
31
32type APIHandler struct {
khenaidoob9203542018-09-17 22:56:37 -040033 deviceMgr *DeviceManager
34 logicalDeviceMgr *LogicalDeviceManager
khenaidoobf6e7bb2018-08-14 22:27:29 -040035 da.DefaultAPIHandler
36}
37
khenaidoo9a468962018-09-19 15:33:13 -040038func NewAPIHandler(deviceMgr *DeviceManager, lDeviceMgr *LogicalDeviceManager) *APIHandler {
khenaidoob9203542018-09-17 22:56:37 -040039 handler := &APIHandler{deviceMgr: deviceMgr,
khenaidoo9a468962018-09-19 15:33:13 -040040 logicalDeviceMgr: lDeviceMgr}
khenaidoobf6e7bb2018-08-14 22:27:29 -040041 return handler
42}
43func isTestMode(ctx context.Context) bool {
44 md, _ := metadata.FromIncomingContext(ctx)
45 _, exist := md[common.TestModeKeys_api_test.String()]
46 return exist
47}
48
49func (handler *APIHandler) UpdateLogLevel(ctx context.Context, logging *voltha.Logging) (*empty.Empty, error) {
50 log.Debugw("UpdateLogLevel-request", log.Fields{"newloglevel": logging.Level, "intval": int(logging.Level)})
khenaidoo92e62c52018-10-03 14:02:54 -040051 out := new(empty.Empty)
52 log.SetPackageLogLevel(logging.PackageName, int(logging.Level))
53 return out, nil
khenaidoobf6e7bb2018-08-14 22:27:29 -040054}
55
khenaidoob9203542018-09-17 22:56:37 -040056func processEnableDevicePort(ctx context.Context, id *voltha.LogicalPortId, ch chan error) {
57 log.Debugw("processEnableDevicePort", log.Fields{"id": id, "test": common.TestModeKeys_api_test.String()})
58 ch <- status.Errorf(100, "%d-%s", 100, "erreur")
khenaidoob9203542018-09-17 22:56:37 -040059}
60
khenaidoobf6e7bb2018-08-14 22:27:29 -040061func (handler *APIHandler) EnableLogicalDevicePort(ctx context.Context, id *voltha.LogicalPortId) (*empty.Empty, error) {
62 log.Debugw("EnableLogicalDevicePort-request", log.Fields{"id": id, "test": common.TestModeKeys_api_test.String()})
63 if isTestMode(ctx) {
64 out := new(empty.Empty)
65 return out, nil
66 }
khenaidoob9203542018-09-17 22:56:37 -040067 ch := make(chan error)
68 go processEnableDevicePort(ctx, id, ch)
69 select {
70 case resp := <-ch:
71 close(ch)
72 return new(empty.Empty), resp
73 case <-ctx.Done():
74 return nil, ctx.Err()
75 }
khenaidoobf6e7bb2018-08-14 22:27:29 -040076}
77
78func (handler *APIHandler) DisableLogicalDevicePort(ctx context.Context, id *voltha.LogicalPortId) (*empty.Empty, error) {
79 log.Debugw("DisableLogicalDevicePort-request", log.Fields{"id": id, "test": common.TestModeKeys_api_test.String()})
80 if isTestMode(ctx) {
81 out := new(empty.Empty)
82 return out, nil
83 }
84 return nil, errors.New("Unimplemented")
85}
86
87func (handler *APIHandler) UpdateLogicalDeviceFlowTable(ctx context.Context, flow *openflow_13.FlowTableUpdate) (*empty.Empty, error) {
88 log.Debugw("UpdateLogicalDeviceFlowTable-request", log.Fields{"flow": flow, "test": common.TestModeKeys_api_test.String()})
89 if isTestMode(ctx) {
90 out := new(empty.Empty)
91 return out, nil
92 }
93 return nil, errors.New("Unimplemented")
94}
95
96func (handler *APIHandler) UpdateLogicalDeviceFlowGroupTable(ctx context.Context, flow *openflow_13.FlowGroupTableUpdate) (*empty.Empty, error) {
97 log.Debugw("UpdateLogicalDeviceFlowGroupTable-request", log.Fields{"flow": flow, "test": common.TestModeKeys_api_test.String()})
98 if isTestMode(ctx) {
99 out := new(empty.Empty)
100 return out, nil
101 }
102 return nil, errors.New("Unimplemented")
103}
104
khenaidoob9203542018-09-17 22:56:37 -0400105// GetDevice must be implemented in the read-only containers - should it also be implemented here?
106func (handler *APIHandler) GetDevice(ctx context.Context, id *voltha.ID) (*voltha.Device, error) {
107 log.Debugw("GetDevice-request", log.Fields{"id": id})
108 return handler.deviceMgr.getDevice(id.Id)
109}
110
111// GetDevice must be implemented in the read-only containers - should it also be implemented here?
112func (handler *APIHandler) ListDevices(ctx context.Context, empty *empty.Empty) (*voltha.Devices, error) {
113 log.Debug("ListDevices")
114 return handler.deviceMgr.ListDevices()
115}
116
117// GetLogicalDevice must be implemented in the read-only containers - should it also be implemented here?
118func (handler *APIHandler) GetLogicalDevice(ctx context.Context, id *voltha.ID) (*voltha.LogicalDevice, error) {
119 log.Debugw("GetLogicalDevice-request", log.Fields{"id": id})
120 return handler.logicalDeviceMgr.getLogicalDevice(id.Id)
121}
122
123// ListLogicalDevices must be implemented in the read-only containers - should it also be implemented here?
124func (handler *APIHandler) ListLogicalDevices(ctx context.Context, empty *empty.Empty) (*voltha.LogicalDevices, error) {
125 log.Debug("ListLogicalDevices")
126 return handler.logicalDeviceMgr.listLogicalDevices()
127}
128
khenaidoobf6e7bb2018-08-14 22:27:29 -0400129func (handler *APIHandler) CreateDevice(ctx context.Context, device *voltha.Device) (*voltha.Device, error) {
khenaidoob9203542018-09-17 22:56:37 -0400130 log.Debugw("createdevice", log.Fields{"device": *device})
khenaidoobf6e7bb2018-08-14 22:27:29 -0400131 if isTestMode(ctx) {
132 return &voltha.Device{Id: device.Id}, nil
133 }
khenaidoob9203542018-09-17 22:56:37 -0400134 ch := make(chan interface{})
135 defer close(ch)
136 go handler.deviceMgr.createDevice(ctx, device, ch)
137 select {
138 case res := <-ch:
khenaidoo92e62c52018-10-03 14:02:54 -0400139 if res != nil {
140 if err, ok := res.(error); ok {
141 return &voltha.Device{}, err
142 }
143 if d, ok := res.(*voltha.Device); ok {
144 return d, nil
145 }
khenaidoob9203542018-09-17 22:56:37 -0400146 }
khenaidoo92e62c52018-10-03 14:02:54 -0400147 log.Warnw("create-device-unexpected-return-type", log.Fields{"result": res})
148 err := status.Errorf(codes.Internal, "%s", res)
149 return &voltha.Device{}, err
khenaidoob9203542018-09-17 22:56:37 -0400150 case <-ctx.Done():
151 log.Debug("createdevice-client-timeout")
152 return nil, ctx.Err()
153 }
khenaidoobf6e7bb2018-08-14 22:27:29 -0400154}
155
156func (handler *APIHandler) EnableDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) {
khenaidoob9203542018-09-17 22:56:37 -0400157 log.Debugw("enabledevice", log.Fields{"id": id})
khenaidoobf6e7bb2018-08-14 22:27:29 -0400158 if isTestMode(ctx) {
159 out := new(empty.Empty)
160 return out, nil
161 }
khenaidoob9203542018-09-17 22:56:37 -0400162 ch := make(chan interface{})
163 defer close(ch)
164 go handler.deviceMgr.enableDevice(ctx, id, ch)
165 select {
166 case res := <-ch:
167 if res == nil {
168 return new(empty.Empty), nil
169 } else if err, ok := res.(error); ok {
170 return new(empty.Empty), err
171 } else {
172 log.Warnw("enable-device-unexpected-return-type", log.Fields{"result": res})
173 err = status.Errorf(codes.Internal, "%s", res)
174 return new(empty.Empty), err
175 }
176 case <-ctx.Done():
177 log.Debug("enabledevice-client-timeout")
178 return nil, ctx.Err()
179 }
khenaidoobf6e7bb2018-08-14 22:27:29 -0400180}
181
182func (handler *APIHandler) DisableDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) {
183 log.Debugw("disabledevice-request", log.Fields{"id": id})
184 if isTestMode(ctx) {
185 out := new(empty.Empty)
186 return out, nil
187 }
khenaidoo92e62c52018-10-03 14:02:54 -0400188 ch := make(chan interface{})
189 defer close(ch)
190 go handler.deviceMgr.disableDevice(ctx, id, ch)
191 select {
192 case res := <-ch:
193 if res == nil {
194 return new(empty.Empty), nil
195 } else if err, ok := res.(error); ok {
196 return new(empty.Empty), err
197 } else {
198 log.Warnw("disable-device-unexpected-return-type", log.Fields{"result": res})
199 err = status.Errorf(codes.Internal, "%s", res)
200 return new(empty.Empty), err
201 }
202 case <-ctx.Done():
203 log.Debug("enabledevice-client-timeout")
204 return nil, ctx.Err()
205 }
khenaidoobf6e7bb2018-08-14 22:27:29 -0400206 return nil, errors.New("Unimplemented")
207}
208
209func (handler *APIHandler) RebootDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) {
210 log.Debugw("disabledevice-request", log.Fields{"id": id})
211 if isTestMode(ctx) {
212 out := new(empty.Empty)
213 return out, nil
214 }
215 return nil, errors.New("Unimplemented")
216}
217
218func (handler *APIHandler) DeleteDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) {
219 log.Debugw("deletedevice-request", log.Fields{"id": id})
220 if isTestMode(ctx) {
221 out := new(empty.Empty)
222 return out, nil
223 }
224 return nil, errors.New("Unimplemented")
225}
226
227func (handler *APIHandler) DownloadImage(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) {
228 log.Debugw("DownloadImage-request", log.Fields{"img": *img})
229 if isTestMode(ctx) {
230 resp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS}
231 return resp, nil
232 }
233
234 return nil, errors.New("UnImplemented")
235}
236
237func (handler *APIHandler) CancelImageDownload(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) {
238 log.Debugw("CancelImageDownload-request", log.Fields{"img": *img})
239 if isTestMode(ctx) {
240 resp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS}
241 return resp, nil
242 }
243 return nil, errors.New("UnImplemented")
244}
245
246func (handler *APIHandler) ActivateImageUpdate(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) {
247 log.Debugw("ActivateImageUpdate-request", log.Fields{"img": *img})
248 if isTestMode(ctx) {
249 resp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS}
250 return resp, nil
251 }
252 return nil, errors.New("UnImplemented")
253}
254
255func (handler *APIHandler) RevertImageUpdate(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) {
256 log.Debugw("RevertImageUpdate-request", log.Fields{"img": *img})
257 if isTestMode(ctx) {
258 resp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS}
259 return resp, nil
260 }
261 return nil, errors.New("UnImplemented")
262}
263
264func (handler *APIHandler) UpdateDevicePmConfigs(ctx context.Context, configs *voltha.PmConfigs) (*empty.Empty, error) {
265 log.Debugw("UpdateDevicePmConfigs-request", log.Fields{"configs": *configs})
266 if isTestMode(ctx) {
267 out := new(empty.Empty)
268 return out, nil
269 }
270 return nil, errors.New("UnImplemented")
271}
272
273func (handler *APIHandler) CreateAlarmFilter(ctx context.Context, filter *voltha.AlarmFilter) (*voltha.AlarmFilter, error) {
274 log.Debugw("CreateAlarmFilter-request", log.Fields{"filter": *filter})
275 if isTestMode(ctx) {
276 f := &voltha.AlarmFilter{Id: filter.Id}
277 return f, nil
278 }
279 return nil, errors.New("UnImplemented")
280}
281
282func (handler *APIHandler) UpdateAlarmFilter(ctx context.Context, filter *voltha.AlarmFilter) (*voltha.AlarmFilter, error) {
283 log.Debugw("UpdateAlarmFilter-request", log.Fields{"filter": *filter})
284 if isTestMode(ctx) {
285 f := &voltha.AlarmFilter{Id: filter.Id}
286 return f, nil
287 }
288 return nil, errors.New("UnImplemented")
289}
290
291func (handler *APIHandler) DeleteAlarmFilter(ctx context.Context, id *voltha.ID) (*empty.Empty, error) {
292 log.Debugw("DeleteAlarmFilter-request", log.Fields{"id": *id})
293 if isTestMode(ctx) {
294 out := new(empty.Empty)
295 return out, nil
296 }
297 return nil, errors.New("UnImplemented")
298}
299
300func (handler *APIHandler) SelfTest(ctx context.Context, id *voltha.ID) (*voltha.SelfTestResponse, error) {
301 log.Debugw("SelfTest-request", log.Fields{"id": id})
302 if isTestMode(ctx) {
303 resp := &voltha.SelfTestResponse{Result: voltha.SelfTestResponse_SUCCESS}
304 return resp, nil
305 }
306 return nil, errors.New("UnImplemented")
307}