khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame^] | 1 | /* |
| 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 | */ |
| 16 | package grpc |
| 17 | |
| 18 | import ( |
| 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" |
| 27 | "google.golang.org/grpc/metadata" |
| 28 | ) |
| 29 | |
| 30 | type APIHandler struct { |
| 31 | da.DefaultAPIHandler |
| 32 | } |
| 33 | |
| 34 | func NewAPIHandler() *APIHandler { |
| 35 | handler := &APIHandler{} |
| 36 | return handler |
| 37 | } |
| 38 | func isTestMode(ctx context.Context) bool { |
| 39 | md, _ := metadata.FromIncomingContext(ctx) |
| 40 | _, exist := md[common.TestModeKeys_api_test.String()] |
| 41 | return exist |
| 42 | } |
| 43 | |
| 44 | func (handler *APIHandler) UpdateLogLevel(ctx context.Context, logging *voltha.Logging) (*empty.Empty, error) { |
| 45 | log.Debugw("UpdateLogLevel-request", log.Fields{"newloglevel": logging.Level, "intval": int(logging.Level)}) |
| 46 | if isTestMode(ctx) { |
| 47 | out := new(empty.Empty) |
| 48 | log.SetLoglevel(int(logging.Level)) |
| 49 | return out, nil |
| 50 | } |
| 51 | return nil, errors.New("Unimplemented") |
| 52 | |
| 53 | } |
| 54 | |
| 55 | func (handler *APIHandler) EnableLogicalDevicePort(ctx context.Context, id *voltha.LogicalPortId) (*empty.Empty, error) { |
| 56 | log.Debugw("EnableLogicalDevicePort-request", log.Fields{"id": id, "test": common.TestModeKeys_api_test.String()}) |
| 57 | if isTestMode(ctx) { |
| 58 | out := new(empty.Empty) |
| 59 | return out, nil |
| 60 | } |
| 61 | return nil, errors.New("Unimplemented") |
| 62 | } |
| 63 | |
| 64 | func (handler *APIHandler) DisableLogicalDevicePort(ctx context.Context, id *voltha.LogicalPortId) (*empty.Empty, error) { |
| 65 | log.Debugw("DisableLogicalDevicePort-request", log.Fields{"id": id, "test": common.TestModeKeys_api_test.String()}) |
| 66 | if isTestMode(ctx) { |
| 67 | out := new(empty.Empty) |
| 68 | return out, nil |
| 69 | } |
| 70 | return nil, errors.New("Unimplemented") |
| 71 | } |
| 72 | |
| 73 | func (handler *APIHandler) UpdateLogicalDeviceFlowTable(ctx context.Context, flow *openflow_13.FlowTableUpdate) (*empty.Empty, error) { |
| 74 | log.Debugw("UpdateLogicalDeviceFlowTable-request", log.Fields{"flow": flow, "test": common.TestModeKeys_api_test.String()}) |
| 75 | if isTestMode(ctx) { |
| 76 | out := new(empty.Empty) |
| 77 | return out, nil |
| 78 | } |
| 79 | return nil, errors.New("Unimplemented") |
| 80 | } |
| 81 | |
| 82 | func (handler *APIHandler) UpdateLogicalDeviceFlowGroupTable(ctx context.Context, flow *openflow_13.FlowGroupTableUpdate) (*empty.Empty, error) { |
| 83 | log.Debugw("UpdateLogicalDeviceFlowGroupTable-request", log.Fields{"flow": flow, "test": common.TestModeKeys_api_test.String()}) |
| 84 | if isTestMode(ctx) { |
| 85 | out := new(empty.Empty) |
| 86 | return out, nil |
| 87 | } |
| 88 | return nil, errors.New("Unimplemented") |
| 89 | } |
| 90 | |
| 91 | func (handler *APIHandler) CreateDevice(ctx context.Context, device *voltha.Device) (*voltha.Device, error) { |
| 92 | log.Debugw("createdevice-request", log.Fields{"device": *device}) |
| 93 | if isTestMode(ctx) { |
| 94 | return &voltha.Device{Id: device.Id}, nil |
| 95 | } |
| 96 | return nil, errors.New("Unimplemented") |
| 97 | } |
| 98 | |
| 99 | func (handler *APIHandler) EnableDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) { |
| 100 | log.Debugw("enabledevice-request", log.Fields{"id": id}) |
| 101 | if isTestMode(ctx) { |
| 102 | out := new(empty.Empty) |
| 103 | return out, nil |
| 104 | } |
| 105 | return nil, errors.New("Unimplemented") |
| 106 | } |
| 107 | |
| 108 | func (handler *APIHandler) DisableDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) { |
| 109 | log.Debugw("disabledevice-request", log.Fields{"id": id}) |
| 110 | if isTestMode(ctx) { |
| 111 | out := new(empty.Empty) |
| 112 | return out, nil |
| 113 | } |
| 114 | return nil, errors.New("Unimplemented") |
| 115 | } |
| 116 | |
| 117 | func (handler *APIHandler) RebootDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) { |
| 118 | log.Debugw("disabledevice-request", log.Fields{"id": id}) |
| 119 | if isTestMode(ctx) { |
| 120 | out := new(empty.Empty) |
| 121 | return out, nil |
| 122 | } |
| 123 | return nil, errors.New("Unimplemented") |
| 124 | } |
| 125 | |
| 126 | func (handler *APIHandler) DeleteDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) { |
| 127 | log.Debugw("deletedevice-request", log.Fields{"id": id}) |
| 128 | if isTestMode(ctx) { |
| 129 | out := new(empty.Empty) |
| 130 | return out, nil |
| 131 | } |
| 132 | return nil, errors.New("Unimplemented") |
| 133 | } |
| 134 | |
| 135 | func (handler *APIHandler) DownloadImage(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) { |
| 136 | log.Debugw("DownloadImage-request", log.Fields{"img": *img}) |
| 137 | if isTestMode(ctx) { |
| 138 | resp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS} |
| 139 | return resp, nil |
| 140 | } |
| 141 | |
| 142 | return nil, errors.New("UnImplemented") |
| 143 | } |
| 144 | |
| 145 | func (handler *APIHandler) CancelImageDownload(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) { |
| 146 | log.Debugw("CancelImageDownload-request", log.Fields{"img": *img}) |
| 147 | if isTestMode(ctx) { |
| 148 | resp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS} |
| 149 | return resp, nil |
| 150 | } |
| 151 | return nil, errors.New("UnImplemented") |
| 152 | } |
| 153 | |
| 154 | func (handler *APIHandler) ActivateImageUpdate(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) { |
| 155 | log.Debugw("ActivateImageUpdate-request", log.Fields{"img": *img}) |
| 156 | if isTestMode(ctx) { |
| 157 | resp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS} |
| 158 | return resp, nil |
| 159 | } |
| 160 | return nil, errors.New("UnImplemented") |
| 161 | } |
| 162 | |
| 163 | func (handler *APIHandler) RevertImageUpdate(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) { |
| 164 | log.Debugw("RevertImageUpdate-request", log.Fields{"img": *img}) |
| 165 | if isTestMode(ctx) { |
| 166 | resp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS} |
| 167 | return resp, nil |
| 168 | } |
| 169 | return nil, errors.New("UnImplemented") |
| 170 | } |
| 171 | |
| 172 | func (handler *APIHandler) UpdateDevicePmConfigs(ctx context.Context, configs *voltha.PmConfigs) (*empty.Empty, error) { |
| 173 | log.Debugw("UpdateDevicePmConfigs-request", log.Fields{"configs": *configs}) |
| 174 | if isTestMode(ctx) { |
| 175 | out := new(empty.Empty) |
| 176 | return out, nil |
| 177 | } |
| 178 | return nil, errors.New("UnImplemented") |
| 179 | } |
| 180 | |
| 181 | func (handler *APIHandler) CreateAlarmFilter(ctx context.Context, filter *voltha.AlarmFilter) (*voltha.AlarmFilter, error) { |
| 182 | log.Debugw("CreateAlarmFilter-request", log.Fields{"filter": *filter}) |
| 183 | if isTestMode(ctx) { |
| 184 | f := &voltha.AlarmFilter{Id: filter.Id} |
| 185 | return f, nil |
| 186 | } |
| 187 | return nil, errors.New("UnImplemented") |
| 188 | } |
| 189 | |
| 190 | func (handler *APIHandler) UpdateAlarmFilter(ctx context.Context, filter *voltha.AlarmFilter) (*voltha.AlarmFilter, error) { |
| 191 | log.Debugw("UpdateAlarmFilter-request", log.Fields{"filter": *filter}) |
| 192 | if isTestMode(ctx) { |
| 193 | f := &voltha.AlarmFilter{Id: filter.Id} |
| 194 | return f, nil |
| 195 | } |
| 196 | return nil, errors.New("UnImplemented") |
| 197 | } |
| 198 | |
| 199 | func (handler *APIHandler) DeleteAlarmFilter(ctx context.Context, id *voltha.ID) (*empty.Empty, error) { |
| 200 | log.Debugw("DeleteAlarmFilter-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 | |
| 208 | func (handler *APIHandler) SelfTest(ctx context.Context, id *voltha.ID) (*voltha.SelfTestResponse, error) { |
| 209 | log.Debugw("SelfTest-request", log.Fields{"id": id}) |
| 210 | if isTestMode(ctx) { |
| 211 | resp := &voltha.SelfTestResponse{Result: voltha.SelfTestResponse_SUCCESS} |
| 212 | return resp, nil |
| 213 | } |
| 214 | return nil, errors.New("UnImplemented") |
| 215 | } |