blob: 6af73cd45d148a1273169e1e8f433c4c491c3e9b [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"
khenaidoob9203542018-09-17 22:56:37 -040024 "github.com/opencord/voltha-go/db/model"
khenaidoobf6e7bb2018-08-14 22:27:29 -040025 "github.com/opencord/voltha-go/protos/common"
26 "github.com/opencord/voltha-go/protos/openflow_13"
27 "github.com/opencord/voltha-go/protos/voltha"
khenaidoob9203542018-09-17 22:56:37 -040028 "google.golang.org/grpc/codes"
khenaidoobf6e7bb2018-08-14 22:27:29 -040029 "google.golang.org/grpc/metadata"
khenaidoob9203542018-09-17 22:56:37 -040030 "google.golang.org/grpc/status"
khenaidoobf6e7bb2018-08-14 22:27:29 -040031)
32
33type APIHandler struct {
khenaidoob9203542018-09-17 22:56:37 -040034 deviceMgr *DeviceManager
35 logicalDeviceMgr *LogicalDeviceManager
36 clusterDataProxy *model.Proxy
37 localDataProxy *model.Proxy
khenaidoobf6e7bb2018-08-14 22:27:29 -040038 da.DefaultAPIHandler
39}
40
khenaidoob9203542018-09-17 22:56:37 -040041func NewAPIHandler(deviceMgr *DeviceManager, lDeviceMgr *LogicalDeviceManager, cdProxy *model.Proxy, ldProxy *model.Proxy) *APIHandler {
42 handler := &APIHandler{deviceMgr: deviceMgr,
43 logicalDeviceMgr: lDeviceMgr,
44 clusterDataProxy: cdProxy,
45 localDataProxy: ldProxy}
khenaidoobf6e7bb2018-08-14 22:27:29 -040046 return handler
47}
48func isTestMode(ctx context.Context) bool {
49 md, _ := metadata.FromIncomingContext(ctx)
50 _, exist := md[common.TestModeKeys_api_test.String()]
51 return exist
52}
53
54func (handler *APIHandler) UpdateLogLevel(ctx context.Context, logging *voltha.Logging) (*empty.Empty, error) {
55 log.Debugw("UpdateLogLevel-request", log.Fields{"newloglevel": logging.Level, "intval": int(logging.Level)})
56 if isTestMode(ctx) {
57 out := new(empty.Empty)
khenaidoob9203542018-09-17 22:56:37 -040058 log.SetPackageLogLevel(logging.PackageName, int(logging.Level))
khenaidoobf6e7bb2018-08-14 22:27:29 -040059 return out, nil
60 }
61 return nil, errors.New("Unimplemented")
62
63}
64
khenaidoob9203542018-09-17 22:56:37 -040065func processEnableDevicePort(ctx context.Context, id *voltha.LogicalPortId, ch chan error) {
66 log.Debugw("processEnableDevicePort", log.Fields{"id": id, "test": common.TestModeKeys_api_test.String()})
67 ch <- status.Errorf(100, "%d-%s", 100, "erreur")
68
69}
70
khenaidoobf6e7bb2018-08-14 22:27:29 -040071func (handler *APIHandler) EnableLogicalDevicePort(ctx context.Context, id *voltha.LogicalPortId) (*empty.Empty, error) {
72 log.Debugw("EnableLogicalDevicePort-request", log.Fields{"id": id, "test": common.TestModeKeys_api_test.String()})
73 if isTestMode(ctx) {
74 out := new(empty.Empty)
75 return out, nil
76 }
khenaidoob9203542018-09-17 22:56:37 -040077 ch := make(chan error)
78 go processEnableDevicePort(ctx, id, ch)
79 select {
80 case resp := <-ch:
81 close(ch)
82 return new(empty.Empty), resp
83 case <-ctx.Done():
84 return nil, ctx.Err()
85 }
khenaidoobf6e7bb2018-08-14 22:27:29 -040086}
87
88func (handler *APIHandler) DisableLogicalDevicePort(ctx context.Context, id *voltha.LogicalPortId) (*empty.Empty, error) {
89 log.Debugw("DisableLogicalDevicePort-request", log.Fields{"id": id, "test": common.TestModeKeys_api_test.String()})
90 if isTestMode(ctx) {
91 out := new(empty.Empty)
92 return out, nil
93 }
94 return nil, errors.New("Unimplemented")
95}
96
97func (handler *APIHandler) UpdateLogicalDeviceFlowTable(ctx context.Context, flow *openflow_13.FlowTableUpdate) (*empty.Empty, error) {
98 log.Debugw("UpdateLogicalDeviceFlowTable-request", log.Fields{"flow": flow, "test": common.TestModeKeys_api_test.String()})
99 if isTestMode(ctx) {
100 out := new(empty.Empty)
101 return out, nil
102 }
103 return nil, errors.New("Unimplemented")
104}
105
106func (handler *APIHandler) UpdateLogicalDeviceFlowGroupTable(ctx context.Context, flow *openflow_13.FlowGroupTableUpdate) (*empty.Empty, error) {
107 log.Debugw("UpdateLogicalDeviceFlowGroupTable-request", log.Fields{"flow": flow, "test": common.TestModeKeys_api_test.String()})
108 if isTestMode(ctx) {
109 out := new(empty.Empty)
110 return out, nil
111 }
112 return nil, errors.New("Unimplemented")
113}
114
khenaidoob9203542018-09-17 22:56:37 -0400115// GetDevice must be implemented in the read-only containers - should it also be implemented here?
116func (handler *APIHandler) GetDevice(ctx context.Context, id *voltha.ID) (*voltha.Device, error) {
117 log.Debugw("GetDevice-request", log.Fields{"id": id})
118 return handler.deviceMgr.getDevice(id.Id)
119}
120
121// GetDevice must be implemented in the read-only containers - should it also be implemented here?
122func (handler *APIHandler) ListDevices(ctx context.Context, empty *empty.Empty) (*voltha.Devices, error) {
123 log.Debug("ListDevices")
124 return handler.deviceMgr.ListDevices()
125}
126
127// GetLogicalDevice must be implemented in the read-only containers - should it also be implemented here?
128func (handler *APIHandler) GetLogicalDevice(ctx context.Context, id *voltha.ID) (*voltha.LogicalDevice, error) {
129 log.Debugw("GetLogicalDevice-request", log.Fields{"id": id})
130 return handler.logicalDeviceMgr.getLogicalDevice(id.Id)
131}
132
133// ListLogicalDevices must be implemented in the read-only containers - should it also be implemented here?
134func (handler *APIHandler) ListLogicalDevices(ctx context.Context, empty *empty.Empty) (*voltha.LogicalDevices, error) {
135 log.Debug("ListLogicalDevices")
136 return handler.logicalDeviceMgr.listLogicalDevices()
137}
138
khenaidoobf6e7bb2018-08-14 22:27:29 -0400139func (handler *APIHandler) CreateDevice(ctx context.Context, device *voltha.Device) (*voltha.Device, error) {
khenaidoob9203542018-09-17 22:56:37 -0400140 log.Debugw("createdevice", log.Fields{"device": *device})
khenaidoobf6e7bb2018-08-14 22:27:29 -0400141 if isTestMode(ctx) {
142 return &voltha.Device{Id: device.Id}, nil
143 }
khenaidoob9203542018-09-17 22:56:37 -0400144 ch := make(chan interface{})
145 defer close(ch)
146 go handler.deviceMgr.createDevice(ctx, device, ch)
147 select {
148 case res := <-ch:
149 if res == nil {
150 return &voltha.Device{Id: device.Id}, nil
151 } else if err, ok := res.(error); ok {
152 return &voltha.Device{Id: device.Id}, err
153 } else {
154 log.Warnw("create-device-unexpected-return-type", log.Fields{"result": res})
155 err = status.Errorf(codes.Internal, "%s", res)
156 return &voltha.Device{Id: device.Id}, err
157 }
158 case <-ctx.Done():
159 log.Debug("createdevice-client-timeout")
160 return nil, ctx.Err()
161 }
khenaidoobf6e7bb2018-08-14 22:27:29 -0400162}
163
164func (handler *APIHandler) EnableDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) {
khenaidoob9203542018-09-17 22:56:37 -0400165 log.Debugw("enabledevice", log.Fields{"id": id})
khenaidoobf6e7bb2018-08-14 22:27:29 -0400166 if isTestMode(ctx) {
167 out := new(empty.Empty)
168 return out, nil
169 }
khenaidoob9203542018-09-17 22:56:37 -0400170 ch := make(chan interface{})
171 defer close(ch)
172 go handler.deviceMgr.enableDevice(ctx, id, ch)
173 select {
174 case res := <-ch:
175 if res == nil {
176 return new(empty.Empty), nil
177 } else if err, ok := res.(error); ok {
178 return new(empty.Empty), err
179 } else {
180 log.Warnw("enable-device-unexpected-return-type", log.Fields{"result": res})
181 err = status.Errorf(codes.Internal, "%s", res)
182 return new(empty.Empty), err
183 }
184 case <-ctx.Done():
185 log.Debug("enabledevice-client-timeout")
186 return nil, ctx.Err()
187 }
khenaidoobf6e7bb2018-08-14 22:27:29 -0400188}
189
190func (handler *APIHandler) DisableDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) {
191 log.Debugw("disabledevice-request", log.Fields{"id": id})
192 if isTestMode(ctx) {
193 out := new(empty.Empty)
194 return out, nil
195 }
196 return nil, errors.New("Unimplemented")
197}
198
199func (handler *APIHandler) RebootDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) {
200 log.Debugw("disabledevice-request", log.Fields{"id": id})
201 if isTestMode(ctx) {
202 out := new(empty.Empty)
203 return out, nil
204 }
205 return nil, errors.New("Unimplemented")
206}
207
208func (handler *APIHandler) DeleteDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) {
209 log.Debugw("deletedevice-request", log.Fields{"id": id})
210 if isTestMode(ctx) {
211 out := new(empty.Empty)
212 return out, nil
213 }
214 return nil, errors.New("Unimplemented")
215}
216
217func (handler *APIHandler) DownloadImage(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) {
218 log.Debugw("DownloadImage-request", log.Fields{"img": *img})
219 if isTestMode(ctx) {
220 resp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS}
221 return resp, nil
222 }
223
224 return nil, errors.New("UnImplemented")
225}
226
227func (handler *APIHandler) CancelImageDownload(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) {
228 log.Debugw("CancelImageDownload-request", log.Fields{"img": *img})
229 if isTestMode(ctx) {
230 resp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS}
231 return resp, nil
232 }
233 return nil, errors.New("UnImplemented")
234}
235
236func (handler *APIHandler) ActivateImageUpdate(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) {
237 log.Debugw("ActivateImageUpdate-request", log.Fields{"img": *img})
238 if isTestMode(ctx) {
239 resp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS}
240 return resp, nil
241 }
242 return nil, errors.New("UnImplemented")
243}
244
245func (handler *APIHandler) RevertImageUpdate(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) {
246 log.Debugw("RevertImageUpdate-request", log.Fields{"img": *img})
247 if isTestMode(ctx) {
248 resp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS}
249 return resp, nil
250 }
251 return nil, errors.New("UnImplemented")
252}
253
254func (handler *APIHandler) UpdateDevicePmConfigs(ctx context.Context, configs *voltha.PmConfigs) (*empty.Empty, error) {
255 log.Debugw("UpdateDevicePmConfigs-request", log.Fields{"configs": *configs})
256 if isTestMode(ctx) {
257 out := new(empty.Empty)
258 return out, nil
259 }
260 return nil, errors.New("UnImplemented")
261}
262
263func (handler *APIHandler) CreateAlarmFilter(ctx context.Context, filter *voltha.AlarmFilter) (*voltha.AlarmFilter, error) {
264 log.Debugw("CreateAlarmFilter-request", log.Fields{"filter": *filter})
265 if isTestMode(ctx) {
266 f := &voltha.AlarmFilter{Id: filter.Id}
267 return f, nil
268 }
269 return nil, errors.New("UnImplemented")
270}
271
272func (handler *APIHandler) UpdateAlarmFilter(ctx context.Context, filter *voltha.AlarmFilter) (*voltha.AlarmFilter, error) {
273 log.Debugw("UpdateAlarmFilter-request", log.Fields{"filter": *filter})
274 if isTestMode(ctx) {
275 f := &voltha.AlarmFilter{Id: filter.Id}
276 return f, nil
277 }
278 return nil, errors.New("UnImplemented")
279}
280
281func (handler *APIHandler) DeleteAlarmFilter(ctx context.Context, id *voltha.ID) (*empty.Empty, error) {
282 log.Debugw("DeleteAlarmFilter-request", log.Fields{"id": *id})
283 if isTestMode(ctx) {
284 out := new(empty.Empty)
285 return out, nil
286 }
287 return nil, errors.New("UnImplemented")
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}