blob: bd283222ae29863f0a106ee5b19f02f49f231c47 [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)})
51 if isTestMode(ctx) {
52 out := new(empty.Empty)
khenaidoob9203542018-09-17 22:56:37 -040053 log.SetPackageLogLevel(logging.PackageName, int(logging.Level))
khenaidoobf6e7bb2018-08-14 22:27:29 -040054 return out, nil
55 }
56 return nil, errors.New("Unimplemented")
57
58}
59
khenaidoob9203542018-09-17 22:56:37 -040060func processEnableDevicePort(ctx context.Context, id *voltha.LogicalPortId, ch chan error) {
61 log.Debugw("processEnableDevicePort", log.Fields{"id": id, "test": common.TestModeKeys_api_test.String()})
62 ch <- status.Errorf(100, "%d-%s", 100, "erreur")
63
64}
65
khenaidoobf6e7bb2018-08-14 22:27:29 -040066func (handler *APIHandler) EnableLogicalDevicePort(ctx context.Context, id *voltha.LogicalPortId) (*empty.Empty, error) {
67 log.Debugw("EnableLogicalDevicePort-request", log.Fields{"id": id, "test": common.TestModeKeys_api_test.String()})
68 if isTestMode(ctx) {
69 out := new(empty.Empty)
70 return out, nil
71 }
khenaidoob9203542018-09-17 22:56:37 -040072 ch := make(chan error)
73 go processEnableDevicePort(ctx, id, ch)
74 select {
75 case resp := <-ch:
76 close(ch)
77 return new(empty.Empty), resp
78 case <-ctx.Done():
79 return nil, ctx.Err()
80 }
khenaidoobf6e7bb2018-08-14 22:27:29 -040081}
82
83func (handler *APIHandler) DisableLogicalDevicePort(ctx context.Context, id *voltha.LogicalPortId) (*empty.Empty, error) {
84 log.Debugw("DisableLogicalDevicePort-request", log.Fields{"id": id, "test": common.TestModeKeys_api_test.String()})
85 if isTestMode(ctx) {
86 out := new(empty.Empty)
87 return out, nil
88 }
89 return nil, errors.New("Unimplemented")
90}
91
92func (handler *APIHandler) UpdateLogicalDeviceFlowTable(ctx context.Context, flow *openflow_13.FlowTableUpdate) (*empty.Empty, error) {
93 log.Debugw("UpdateLogicalDeviceFlowTable-request", log.Fields{"flow": flow, "test": common.TestModeKeys_api_test.String()})
94 if isTestMode(ctx) {
95 out := new(empty.Empty)
96 return out, nil
97 }
98 return nil, errors.New("Unimplemented")
99}
100
101func (handler *APIHandler) UpdateLogicalDeviceFlowGroupTable(ctx context.Context, flow *openflow_13.FlowGroupTableUpdate) (*empty.Empty, error) {
102 log.Debugw("UpdateLogicalDeviceFlowGroupTable-request", log.Fields{"flow": flow, "test": common.TestModeKeys_api_test.String()})
103 if isTestMode(ctx) {
104 out := new(empty.Empty)
105 return out, nil
106 }
107 return nil, errors.New("Unimplemented")
108}
109
khenaidoob9203542018-09-17 22:56:37 -0400110// GetDevice must be implemented in the read-only containers - should it also be implemented here?
111func (handler *APIHandler) GetDevice(ctx context.Context, id *voltha.ID) (*voltha.Device, error) {
112 log.Debugw("GetDevice-request", log.Fields{"id": id})
113 return handler.deviceMgr.getDevice(id.Id)
114}
115
116// GetDevice must be implemented in the read-only containers - should it also be implemented here?
117func (handler *APIHandler) ListDevices(ctx context.Context, empty *empty.Empty) (*voltha.Devices, error) {
118 log.Debug("ListDevices")
119 return handler.deviceMgr.ListDevices()
120}
121
122// GetLogicalDevice must be implemented in the read-only containers - should it also be implemented here?
123func (handler *APIHandler) GetLogicalDevice(ctx context.Context, id *voltha.ID) (*voltha.LogicalDevice, error) {
124 log.Debugw("GetLogicalDevice-request", log.Fields{"id": id})
125 return handler.logicalDeviceMgr.getLogicalDevice(id.Id)
126}
127
128// ListLogicalDevices must be implemented in the read-only containers - should it also be implemented here?
129func (handler *APIHandler) ListLogicalDevices(ctx context.Context, empty *empty.Empty) (*voltha.LogicalDevices, error) {
130 log.Debug("ListLogicalDevices")
131 return handler.logicalDeviceMgr.listLogicalDevices()
132}
133
khenaidoobf6e7bb2018-08-14 22:27:29 -0400134func (handler *APIHandler) CreateDevice(ctx context.Context, device *voltha.Device) (*voltha.Device, error) {
khenaidoob9203542018-09-17 22:56:37 -0400135 log.Debugw("createdevice", log.Fields{"device": *device})
khenaidoobf6e7bb2018-08-14 22:27:29 -0400136 if isTestMode(ctx) {
137 return &voltha.Device{Id: device.Id}, nil
138 }
khenaidoob9203542018-09-17 22:56:37 -0400139 ch := make(chan interface{})
140 defer close(ch)
141 go handler.deviceMgr.createDevice(ctx, device, ch)
142 select {
143 case res := <-ch:
144 if res == nil {
145 return &voltha.Device{Id: device.Id}, nil
146 } else if err, ok := res.(error); ok {
147 return &voltha.Device{Id: device.Id}, err
148 } else {
149 log.Warnw("create-device-unexpected-return-type", log.Fields{"result": res})
150 err = status.Errorf(codes.Internal, "%s", res)
151 return &voltha.Device{Id: device.Id}, err
152 }
153 case <-ctx.Done():
154 log.Debug("createdevice-client-timeout")
155 return nil, ctx.Err()
156 }
khenaidoobf6e7bb2018-08-14 22:27:29 -0400157}
158
159func (handler *APIHandler) EnableDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) {
khenaidoob9203542018-09-17 22:56:37 -0400160 log.Debugw("enabledevice", log.Fields{"id": id})
khenaidoobf6e7bb2018-08-14 22:27:29 -0400161 if isTestMode(ctx) {
162 out := new(empty.Empty)
163 return out, nil
164 }
khenaidoob9203542018-09-17 22:56:37 -0400165 ch := make(chan interface{})
166 defer close(ch)
167 go handler.deviceMgr.enableDevice(ctx, id, ch)
168 select {
169 case res := <-ch:
170 if res == nil {
171 return new(empty.Empty), nil
172 } else if err, ok := res.(error); ok {
173 return new(empty.Empty), err
174 } else {
175 log.Warnw("enable-device-unexpected-return-type", log.Fields{"result": res})
176 err = status.Errorf(codes.Internal, "%s", res)
177 return new(empty.Empty), err
178 }
179 case <-ctx.Done():
180 log.Debug("enabledevice-client-timeout")
181 return nil, ctx.Err()
182 }
khenaidoobf6e7bb2018-08-14 22:27:29 -0400183}
184
185func (handler *APIHandler) DisableDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) {
186 log.Debugw("disabledevice-request", log.Fields{"id": id})
187 if isTestMode(ctx) {
188 out := new(empty.Empty)
189 return out, nil
190 }
191 return nil, errors.New("Unimplemented")
192}
193
194func (handler *APIHandler) RebootDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) {
195 log.Debugw("disabledevice-request", log.Fields{"id": id})
196 if isTestMode(ctx) {
197 out := new(empty.Empty)
198 return out, nil
199 }
200 return nil, errors.New("Unimplemented")
201}
202
203func (handler *APIHandler) DeleteDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) {
204 log.Debugw("deletedevice-request", log.Fields{"id": id})
205 if isTestMode(ctx) {
206 out := new(empty.Empty)
207 return out, nil
208 }
209 return nil, errors.New("Unimplemented")
210}
211
212func (handler *APIHandler) DownloadImage(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) {
213 log.Debugw("DownloadImage-request", log.Fields{"img": *img})
214 if isTestMode(ctx) {
215 resp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS}
216 return resp, nil
217 }
218
219 return nil, errors.New("UnImplemented")
220}
221
222func (handler *APIHandler) CancelImageDownload(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) {
223 log.Debugw("CancelImageDownload-request", log.Fields{"img": *img})
224 if isTestMode(ctx) {
225 resp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS}
226 return resp, nil
227 }
228 return nil, errors.New("UnImplemented")
229}
230
231func (handler *APIHandler) ActivateImageUpdate(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) {
232 log.Debugw("ActivateImageUpdate-request", log.Fields{"img": *img})
233 if isTestMode(ctx) {
234 resp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS}
235 return resp, nil
236 }
237 return nil, errors.New("UnImplemented")
238}
239
240func (handler *APIHandler) RevertImageUpdate(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) {
241 log.Debugw("RevertImageUpdate-request", log.Fields{"img": *img})
242 if isTestMode(ctx) {
243 resp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS}
244 return resp, nil
245 }
246 return nil, errors.New("UnImplemented")
247}
248
249func (handler *APIHandler) UpdateDevicePmConfigs(ctx context.Context, configs *voltha.PmConfigs) (*empty.Empty, error) {
250 log.Debugw("UpdateDevicePmConfigs-request", log.Fields{"configs": *configs})
251 if isTestMode(ctx) {
252 out := new(empty.Empty)
253 return out, nil
254 }
255 return nil, errors.New("UnImplemented")
256}
257
258func (handler *APIHandler) CreateAlarmFilter(ctx context.Context, filter *voltha.AlarmFilter) (*voltha.AlarmFilter, error) {
259 log.Debugw("CreateAlarmFilter-request", log.Fields{"filter": *filter})
260 if isTestMode(ctx) {
261 f := &voltha.AlarmFilter{Id: filter.Id}
262 return f, nil
263 }
264 return nil, errors.New("UnImplemented")
265}
266
267func (handler *APIHandler) UpdateAlarmFilter(ctx context.Context, filter *voltha.AlarmFilter) (*voltha.AlarmFilter, error) {
268 log.Debugw("UpdateAlarmFilter-request", log.Fields{"filter": *filter})
269 if isTestMode(ctx) {
270 f := &voltha.AlarmFilter{Id: filter.Id}
271 return f, nil
272 }
273 return nil, errors.New("UnImplemented")
274}
275
276func (handler *APIHandler) DeleteAlarmFilter(ctx context.Context, id *voltha.ID) (*empty.Empty, error) {
277 log.Debugw("DeleteAlarmFilter-request", log.Fields{"id": *id})
278 if isTestMode(ctx) {
279 out := new(empty.Empty)
280 return out, nil
281 }
282 return nil, errors.New("UnImplemented")
283}
284
285func (handler *APIHandler) SelfTest(ctx context.Context, id *voltha.ID) (*voltha.SelfTestResponse, error) {
286 log.Debugw("SelfTest-request", log.Fields{"id": id})
287 if isTestMode(ctx) {
288 resp := &voltha.SelfTestResponse{Result: voltha.SelfTestResponse_SUCCESS}
289 return resp, nil
290 }
291 return nil, errors.New("UnImplemented")
292}