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 | */ |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 16 | package core |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 17 | |
| 18 | import ( |
| 19 | "context" |
| 20 | "errors" |
Richard Jankowski | dbab94a | 2018-12-06 16:20:25 -0500 | [diff] [blame] | 21 | "github.com/golang-collections/go-datastructures/queue" |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 22 | "github.com/golang/protobuf/ptypes/empty" |
| 23 | da "github.com/opencord/voltha-go/common/core/northbound/grpc" |
| 24 | "github.com/opencord/voltha-go/common/log" |
| 25 | "github.com/opencord/voltha-go/protos/common" |
| 26 | "github.com/opencord/voltha-go/protos/openflow_13" |
| 27 | "github.com/opencord/voltha-go/protos/voltha" |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 28 | "google.golang.org/grpc/codes" |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 29 | "google.golang.org/grpc/metadata" |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 30 | "google.golang.org/grpc/status" |
Stephane Barbarie | 6e1bd50 | 2018-11-05 22:44:45 -0500 | [diff] [blame] | 31 | "io" |
| 32 | "time" |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 33 | ) |
| 34 | |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 35 | const MAX_RESPONSE_TIME = 500 // milliseconds |
Richard Jankowski | d42826e | 2018-11-02 16:06:37 -0400 | [diff] [blame] | 36 | |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 37 | type APIHandler struct { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 38 | deviceMgr *DeviceManager |
| 39 | logicalDeviceMgr *LogicalDeviceManager |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 40 | packetInQueue *queue.Queue |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 41 | da.DefaultAPIHandler |
| 42 | } |
| 43 | |
khenaidoo | 9a46896 | 2018-09-19 15:33:13 -0400 | [diff] [blame] | 44 | func NewAPIHandler(deviceMgr *DeviceManager, lDeviceMgr *LogicalDeviceManager) *APIHandler { |
Stephane Barbarie | 6e1bd50 | 2018-11-05 22:44:45 -0500 | [diff] [blame] | 45 | handler := &APIHandler{ |
| 46 | deviceMgr: deviceMgr, |
| 47 | logicalDeviceMgr: lDeviceMgr, |
Richard Jankowski | dbab94a | 2018-12-06 16:20:25 -0500 | [diff] [blame] | 48 | // TODO: Figure out what the 'hint' parameter to queue.New does |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 49 | packetInQueue: queue.New(10), |
Stephane Barbarie | 6e1bd50 | 2018-11-05 22:44:45 -0500 | [diff] [blame] | 50 | } |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 51 | return handler |
| 52 | } |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 53 | |
| 54 | // isTestMode is a helper function to determine a function is invoked for testing only |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 55 | func isTestMode(ctx context.Context) bool { |
| 56 | md, _ := metadata.FromIncomingContext(ctx) |
| 57 | _, exist := md[common.TestModeKeys_api_test.String()] |
| 58 | return exist |
| 59 | } |
| 60 | |
Richard Jankowski | d42826e | 2018-11-02 16:06:37 -0400 | [diff] [blame] | 61 | // This function attempts to extract the serial number from the request metadata |
| 62 | // and create a KV transaction for that serial number for the current core. |
| 63 | func (handler *APIHandler) createKvTransaction(ctx context.Context) (*KVTransaction, error) { |
| 64 | var ( |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 65 | err error |
| 66 | ok bool |
| 67 | md metadata.MD |
Richard Jankowski | d42826e | 2018-11-02 16:06:37 -0400 | [diff] [blame] | 68 | serNum []string |
| 69 | ) |
| 70 | if md, ok = metadata.FromIncomingContext(ctx); !ok { |
| 71 | err = errors.New("metadata-not-found") |
| 72 | } else if serNum, ok = md["voltha_serial_number"]; !ok { |
| 73 | err = errors.New("serial-number-not-found") |
| 74 | } |
| 75 | if !ok { |
| 76 | log.Error(err) |
| 77 | return nil, err |
| 78 | } |
| 79 | // Create KV transaction |
| 80 | txn := NewKVTransaction(serNum[0]) |
| 81 | return txn, nil |
| 82 | } |
| 83 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 84 | // waitForNilResponseOnSuccess is a helper function to wait for a response on channel ch where an nil |
| 85 | // response is expected in a successful scenario |
| 86 | func waitForNilResponseOnSuccess(ctx context.Context, ch chan interface{}) (*empty.Empty, error) { |
| 87 | select { |
| 88 | case res := <-ch: |
| 89 | if res == nil { |
| 90 | return new(empty.Empty), nil |
| 91 | } else if err, ok := res.(error); ok { |
| 92 | return new(empty.Empty), err |
| 93 | } else { |
| 94 | log.Warnw("unexpected-return-type", log.Fields{"result": res}) |
| 95 | err = status.Errorf(codes.Internal, "%s", res) |
| 96 | return new(empty.Empty), err |
| 97 | } |
| 98 | case <-ctx.Done(): |
| 99 | log.Debug("client-timeout") |
| 100 | return nil, ctx.Err() |
| 101 | } |
| 102 | } |
| 103 | |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 104 | func (handler *APIHandler) UpdateLogLevel(ctx context.Context, logging *voltha.Logging) (*empty.Empty, error) { |
| 105 | log.Debugw("UpdateLogLevel-request", log.Fields{"newloglevel": logging.Level, "intval": int(logging.Level)}) |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 106 | out := new(empty.Empty) |
| 107 | log.SetPackageLogLevel(logging.PackageName, int(logging.Level)) |
| 108 | return out, nil |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | func (handler *APIHandler) EnableLogicalDevicePort(ctx context.Context, id *voltha.LogicalPortId) (*empty.Empty, error) { |
| 112 | log.Debugw("EnableLogicalDevicePort-request", log.Fields{"id": id, "test": common.TestModeKeys_api_test.String()}) |
| 113 | if isTestMode(ctx) { |
| 114 | out := new(empty.Empty) |
| 115 | return out, nil |
| 116 | } |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 117 | ch := make(chan interface{}) |
| 118 | defer close(ch) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 119 | go handler.logicalDeviceMgr.enableLogicalPort(ctx, id, ch) |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 120 | return waitForNilResponseOnSuccess(ctx, ch) |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | func (handler *APIHandler) DisableLogicalDevicePort(ctx context.Context, id *voltha.LogicalPortId) (*empty.Empty, error) { |
| 124 | log.Debugw("DisableLogicalDevicePort-request", log.Fields{"id": id, "test": common.TestModeKeys_api_test.String()}) |
| 125 | if isTestMode(ctx) { |
| 126 | out := new(empty.Empty) |
| 127 | return out, nil |
| 128 | } |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 129 | ch := make(chan interface{}) |
| 130 | defer close(ch) |
| 131 | go handler.logicalDeviceMgr.disableLogicalPort(ctx, id, ch) |
| 132 | return waitForNilResponseOnSuccess(ctx, ch) |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | func (handler *APIHandler) UpdateLogicalDeviceFlowTable(ctx context.Context, flow *openflow_13.FlowTableUpdate) (*empty.Empty, error) { |
| 136 | log.Debugw("UpdateLogicalDeviceFlowTable-request", log.Fields{"flow": flow, "test": common.TestModeKeys_api_test.String()}) |
| 137 | if isTestMode(ctx) { |
| 138 | out := new(empty.Empty) |
| 139 | return out, nil |
| 140 | } |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 141 | ch := make(chan interface{}) |
| 142 | defer close(ch) |
| 143 | go handler.logicalDeviceMgr.updateFlowTable(ctx, flow.Id, flow.FlowMod, ch) |
| 144 | return waitForNilResponseOnSuccess(ctx, ch) |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | func (handler *APIHandler) UpdateLogicalDeviceFlowGroupTable(ctx context.Context, flow *openflow_13.FlowGroupTableUpdate) (*empty.Empty, error) { |
| 148 | log.Debugw("UpdateLogicalDeviceFlowGroupTable-request", log.Fields{"flow": flow, "test": common.TestModeKeys_api_test.String()}) |
| 149 | if isTestMode(ctx) { |
| 150 | out := new(empty.Empty) |
| 151 | return out, nil |
| 152 | } |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 153 | ch := make(chan interface{}) |
| 154 | defer close(ch) |
| 155 | go handler.logicalDeviceMgr.updateGroupTable(ctx, flow.Id, flow.GroupMod, ch) |
| 156 | return waitForNilResponseOnSuccess(ctx, ch) |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 157 | } |
| 158 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 159 | // GetDevice must be implemented in the read-only containers - should it also be implemented here? |
| 160 | func (handler *APIHandler) GetDevice(ctx context.Context, id *voltha.ID) (*voltha.Device, error) { |
| 161 | log.Debugw("GetDevice-request", log.Fields{"id": id}) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 162 | return handler.deviceMgr.GetDevice(id.Id) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | // GetDevice must be implemented in the read-only containers - should it also be implemented here? |
| 166 | func (handler *APIHandler) ListDevices(ctx context.Context, empty *empty.Empty) (*voltha.Devices, error) { |
| 167 | log.Debug("ListDevices") |
| 168 | return handler.deviceMgr.ListDevices() |
| 169 | } |
| 170 | |
khenaidoo | 7ccedd5 | 2018-12-14 16:48:54 -0500 | [diff] [blame^] | 171 | // ListDeviceIds returns the list of device ids managed by a voltha core |
| 172 | func (handler *APIHandler) ListDeviceIds(ctx context.Context, empty *empty.Empty) (*voltha.IDs, error) { |
| 173 | log.Debug("ListDeviceIDs") |
| 174 | if isTestMode(ctx) { |
| 175 | out := &voltha.IDs{Items: make([]*voltha.ID, 0)} |
| 176 | return out, nil |
| 177 | } |
| 178 | return handler.deviceMgr.ListDeviceIds() |
| 179 | } |
| 180 | |
| 181 | //ReconcileDevices is a request to a voltha core to managed a list of devices based on their IDs |
| 182 | func (handler *APIHandler) ReconcileDevices(ctx context.Context, ids *voltha.IDs) (*empty.Empty, error) { |
| 183 | log.Debug("ReconcileDevices") |
| 184 | if isTestMode(ctx) { |
| 185 | out := new(empty.Empty) |
| 186 | return out, nil |
| 187 | } |
| 188 | ch := make(chan interface{}) |
| 189 | defer close(ch) |
| 190 | go handler.deviceMgr.ReconcileDevices(ctx, ids, ch) |
| 191 | return waitForNilResponseOnSuccess(ctx, ch) |
| 192 | } |
| 193 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 194 | // GetLogicalDevice must be implemented in the read-only containers - should it also be implemented here? |
| 195 | func (handler *APIHandler) GetLogicalDevice(ctx context.Context, id *voltha.ID) (*voltha.LogicalDevice, error) { |
| 196 | log.Debugw("GetLogicalDevice-request", log.Fields{"id": id}) |
| 197 | return handler.logicalDeviceMgr.getLogicalDevice(id.Id) |
| 198 | } |
| 199 | |
| 200 | // ListLogicalDevices must be implemented in the read-only containers - should it also be implemented here? |
| 201 | func (handler *APIHandler) ListLogicalDevices(ctx context.Context, empty *empty.Empty) (*voltha.LogicalDevices, error) { |
| 202 | log.Debug("ListLogicalDevices") |
| 203 | return handler.logicalDeviceMgr.listLogicalDevices() |
| 204 | } |
| 205 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 206 | // ListLogicalDevicePorts must be implemented in the read-only containers - should it also be implemented here? |
| 207 | func (handler *APIHandler) ListLogicalDevicePorts(ctx context.Context, id *voltha.ID) (*voltha.LogicalPorts, error) { |
| 208 | log.Debugw("ListLogicalDevicePorts", log.Fields{"logicaldeviceid": id}) |
| 209 | return handler.logicalDeviceMgr.ListLogicalDevicePorts(ctx, id.Id) |
| 210 | } |
| 211 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 212 | // CreateDevice creates a new parent device in the data model |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 213 | func (handler *APIHandler) CreateDevice(ctx context.Context, device *voltha.Device) (*voltha.Device, error) { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 214 | log.Debugw("createdevice", log.Fields{"device": *device}) |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 215 | if isTestMode(ctx) { |
| 216 | return &voltha.Device{Id: device.Id}, nil |
| 217 | } |
Richard Jankowski | d42826e | 2018-11-02 16:06:37 -0400 | [diff] [blame] | 218 | |
khenaidoo | 9f1fd17 | 2018-11-13 09:16:17 -0500 | [diff] [blame] | 219 | //txn, err := handler.createKvTransaction(ctx) |
| 220 | //if txn == nil { |
| 221 | // return &voltha.Device{}, err |
| 222 | //} else if txn.Acquired(MAX_RESPONSE_TIME) { |
| 223 | // defer txn.Close() // Ensure active core signals "done" to standby |
| 224 | //} else { |
| 225 | // return &voltha.Device{}, nil |
| 226 | //} |
Richard Jankowski | d42826e | 2018-11-02 16:06:37 -0400 | [diff] [blame] | 227 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 228 | ch := make(chan interface{}) |
| 229 | defer close(ch) |
| 230 | go handler.deviceMgr.createDevice(ctx, device, ch) |
| 231 | select { |
| 232 | case res := <-ch: |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 233 | if res != nil { |
| 234 | if err, ok := res.(error); ok { |
| 235 | return &voltha.Device{}, err |
| 236 | } |
| 237 | if d, ok := res.(*voltha.Device); ok { |
| 238 | return d, nil |
| 239 | } |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 240 | } |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 241 | log.Warnw("create-device-unexpected-return-type", log.Fields{"result": res}) |
| 242 | err := status.Errorf(codes.Internal, "%s", res) |
| 243 | return &voltha.Device{}, err |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 244 | case <-ctx.Done(): |
| 245 | log.Debug("createdevice-client-timeout") |
| 246 | return nil, ctx.Err() |
| 247 | } |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 248 | } |
| 249 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 250 | // EnableDevice activates a device by invoking the adopt_device API on the appropriate adapter |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 251 | func (handler *APIHandler) EnableDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 252 | log.Debugw("enabledevice", log.Fields{"id": id}) |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 253 | if isTestMode(ctx) { |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 254 | return new(empty.Empty), nil |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 255 | } |
Richard Jankowski | d42826e | 2018-11-02 16:06:37 -0400 | [diff] [blame] | 256 | |
khenaidoo | 9f1fd17 | 2018-11-13 09:16:17 -0500 | [diff] [blame] | 257 | //txn, err := handler.createKvTransaction(ctx) |
| 258 | //if txn == nil { |
| 259 | // return new(empty.Empty), err |
| 260 | //} else if txn.Acquired(MAX_RESPONSE_TIME) { |
| 261 | // defer txn.Close() // Ensure active core signals "done" to standby |
| 262 | //} else { |
| 263 | // return new(empty.Empty), nil |
| 264 | //} |
Richard Jankowski | d42826e | 2018-11-02 16:06:37 -0400 | [diff] [blame] | 265 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 266 | ch := make(chan interface{}) |
| 267 | defer close(ch) |
| 268 | go handler.deviceMgr.enableDevice(ctx, id, ch) |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 269 | return waitForNilResponseOnSuccess(ctx, ch) |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 270 | } |
| 271 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 272 | // DisableDevice disables a device along with any child device it may have |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 273 | func (handler *APIHandler) DisableDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) { |
| 274 | log.Debugw("disabledevice-request", log.Fields{"id": id}) |
| 275 | if isTestMode(ctx) { |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 276 | return new(empty.Empty), nil |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 277 | } |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 278 | ch := make(chan interface{}) |
| 279 | defer close(ch) |
| 280 | go handler.deviceMgr.disableDevice(ctx, id, ch) |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 281 | return waitForNilResponseOnSuccess(ctx, ch) |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 282 | } |
| 283 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 284 | //RebootDevice invoked the reboot API to the corresponding adapter |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 285 | func (handler *APIHandler) RebootDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) { |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 286 | log.Debugw("rebootDevice-request", log.Fields{"id": id}) |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 287 | if isTestMode(ctx) { |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 288 | return new(empty.Empty), nil |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 289 | } |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 290 | ch := make(chan interface{}) |
| 291 | defer close(ch) |
| 292 | go handler.deviceMgr.rebootDevice(ctx, id, ch) |
| 293 | return waitForNilResponseOnSuccess(ctx, ch) |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 294 | } |
| 295 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 296 | // DeleteDevice removes a device from the data model |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 297 | func (handler *APIHandler) DeleteDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) { |
| 298 | log.Debugw("deletedevice-request", log.Fields{"id": id}) |
| 299 | if isTestMode(ctx) { |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 300 | return new(empty.Empty), nil |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 301 | } |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 302 | ch := make(chan interface{}) |
| 303 | defer close(ch) |
| 304 | go handler.deviceMgr.deleteDevice(ctx, id, ch) |
| 305 | return waitForNilResponseOnSuccess(ctx, ch) |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | func (handler *APIHandler) DownloadImage(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) { |
| 309 | log.Debugw("DownloadImage-request", log.Fields{"img": *img}) |
| 310 | if isTestMode(ctx) { |
| 311 | resp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS} |
| 312 | return resp, nil |
| 313 | } |
| 314 | |
| 315 | return nil, errors.New("UnImplemented") |
| 316 | } |
| 317 | |
| 318 | func (handler *APIHandler) CancelImageDownload(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) { |
| 319 | log.Debugw("CancelImageDownload-request", log.Fields{"img": *img}) |
| 320 | if isTestMode(ctx) { |
| 321 | resp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS} |
| 322 | return resp, nil |
| 323 | } |
| 324 | return nil, errors.New("UnImplemented") |
| 325 | } |
| 326 | |
| 327 | func (handler *APIHandler) ActivateImageUpdate(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) { |
| 328 | log.Debugw("ActivateImageUpdate-request", log.Fields{"img": *img}) |
| 329 | if isTestMode(ctx) { |
| 330 | resp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS} |
| 331 | return resp, nil |
| 332 | } |
| 333 | return nil, errors.New("UnImplemented") |
| 334 | } |
| 335 | |
| 336 | func (handler *APIHandler) RevertImageUpdate(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) { |
| 337 | log.Debugw("RevertImageUpdate-request", log.Fields{"img": *img}) |
| 338 | if isTestMode(ctx) { |
| 339 | resp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS} |
| 340 | return resp, nil |
| 341 | } |
| 342 | return nil, errors.New("UnImplemented") |
| 343 | } |
| 344 | |
| 345 | func (handler *APIHandler) UpdateDevicePmConfigs(ctx context.Context, configs *voltha.PmConfigs) (*empty.Empty, error) { |
| 346 | log.Debugw("UpdateDevicePmConfigs-request", log.Fields{"configs": *configs}) |
| 347 | if isTestMode(ctx) { |
| 348 | out := new(empty.Empty) |
| 349 | return out, nil |
| 350 | } |
| 351 | return nil, errors.New("UnImplemented") |
| 352 | } |
| 353 | |
| 354 | func (handler *APIHandler) CreateAlarmFilter(ctx context.Context, filter *voltha.AlarmFilter) (*voltha.AlarmFilter, error) { |
| 355 | log.Debugw("CreateAlarmFilter-request", log.Fields{"filter": *filter}) |
| 356 | if isTestMode(ctx) { |
| 357 | f := &voltha.AlarmFilter{Id: filter.Id} |
| 358 | return f, nil |
| 359 | } |
| 360 | return nil, errors.New("UnImplemented") |
| 361 | } |
| 362 | |
| 363 | func (handler *APIHandler) UpdateAlarmFilter(ctx context.Context, filter *voltha.AlarmFilter) (*voltha.AlarmFilter, error) { |
| 364 | log.Debugw("UpdateAlarmFilter-request", log.Fields{"filter": *filter}) |
| 365 | if isTestMode(ctx) { |
| 366 | f := &voltha.AlarmFilter{Id: filter.Id} |
| 367 | return f, nil |
| 368 | } |
| 369 | return nil, errors.New("UnImplemented") |
| 370 | } |
| 371 | |
| 372 | func (handler *APIHandler) DeleteAlarmFilter(ctx context.Context, id *voltha.ID) (*empty.Empty, error) { |
| 373 | log.Debugw("DeleteAlarmFilter-request", log.Fields{"id": *id}) |
| 374 | if isTestMode(ctx) { |
| 375 | out := new(empty.Empty) |
| 376 | return out, nil |
| 377 | } |
| 378 | return nil, errors.New("UnImplemented") |
| 379 | } |
| 380 | |
| 381 | func (handler *APIHandler) SelfTest(ctx context.Context, id *voltha.ID) (*voltha.SelfTestResponse, error) { |
| 382 | log.Debugw("SelfTest-request", log.Fields{"id": id}) |
| 383 | if isTestMode(ctx) { |
| 384 | resp := &voltha.SelfTestResponse{Result: voltha.SelfTestResponse_SUCCESS} |
| 385 | return resp, nil |
| 386 | } |
| 387 | return nil, errors.New("UnImplemented") |
| 388 | } |
Stephane Barbarie | 6e1bd50 | 2018-11-05 22:44:45 -0500 | [diff] [blame] | 389 | |
| 390 | func (handler *APIHandler) forwardPacketOut(packet *openflow_13.PacketOut) { |
| 391 | log.Debugw("forwardPacketOut-request", log.Fields{"packet": packet}) |
| 392 | //agent := handler.logicalDeviceMgr.getLogicalDeviceAgent(packet.Id) |
| 393 | //agent.packetOut(packet.PacketOut) |
| 394 | } |
| 395 | func (handler *APIHandler) StreamPacketsOut( |
| 396 | packets voltha.VolthaService_StreamPacketsOutServer, |
| 397 | ) error { |
| 398 | log.Debugw("StreamPacketsOut-request", log.Fields{"packets": packets}) |
| 399 | for { |
| 400 | packet, err := packets.Recv() |
| 401 | |
| 402 | if err == io.EOF { |
| 403 | break |
| 404 | } else if err != nil { |
| 405 | log.Errorw("Failed to receive packet", log.Fields{"error": err}) |
| 406 | } |
| 407 | |
| 408 | handler.forwardPacketOut(packet) |
| 409 | } |
| 410 | |
| 411 | log.Debugw("StreamPacketsOut-request-done", log.Fields{"packets": packets}) |
| 412 | return nil |
| 413 | } |
| 414 | |
| 415 | func (handler *APIHandler) sendPacketIn(deviceId string, packet *openflow_13.OfpPacketIn) { |
| 416 | packetIn := openflow_13.PacketIn{Id: deviceId, PacketIn: packet} |
| 417 | log.Debugw("sendPacketIn", log.Fields{"packetIn": packetIn}) |
Richard Jankowski | dbab94a | 2018-12-06 16:20:25 -0500 | [diff] [blame] | 418 | // Enqueue the packet |
| 419 | if err := handler.packetInQueue.Put(packetIn); err != nil { |
| 420 | log.Errorw("failed-to-enqueue-packet", log.Fields{"error": err}) |
| 421 | } |
Stephane Barbarie | 6e1bd50 | 2018-11-05 22:44:45 -0500 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | func (handler *APIHandler) ReceivePacketsIn( |
| 425 | empty *empty.Empty, |
| 426 | packetsIn voltha.VolthaService_ReceivePacketsInServer, |
| 427 | ) error { |
| 428 | log.Debugw("ReceivePacketsIn-request", log.Fields{"packetsIn": packetsIn}) |
| 429 | |
| 430 | for { |
Richard Jankowski | dbab94a | 2018-12-06 16:20:25 -0500 | [diff] [blame] | 431 | // Dequeue a packet |
| 432 | if packets, err := handler.packetInQueue.Get(1); err == nil { |
| 433 | log.Debugw("dequeued-packet", log.Fields{"packet": packets[0]}) |
| 434 | if packet, ok := packets[0].(openflow_13.PacketIn); ok { |
| 435 | log.Debugw("sending-packet-in", log.Fields{"packet": packet}) |
| 436 | if err := packetsIn.Send(&packet); err != nil { |
| 437 | log.Errorw("failed-to-send-packet", log.Fields{"error": err}) |
| 438 | } |
| 439 | } |
Stephane Barbarie | 6e1bd50 | 2018-11-05 22:44:45 -0500 | [diff] [blame] | 440 | } |
| 441 | } |
| 442 | log.Debugw("ReceivePacketsIn-request-done", log.Fields{"packetsIn": packetsIn}) |
| 443 | return nil |
| 444 | } |
| 445 | |
| 446 | func (handler *APIHandler) sendChangeEvent(deviceId string, portStatus *openflow_13.OfpPortStatus) { |
| 447 | // TODO: validate the type of portStatus parameter |
| 448 | //if _, ok := portStatus.(*openflow_13.OfpPortStatus); ok { |
| 449 | //} |
| 450 | event := openflow_13.ChangeEvent{Id: deviceId, Event: &openflow_13.ChangeEvent_PortStatus{PortStatus: portStatus}} |
| 451 | log.Debugw("sendChangeEvent", log.Fields{"event": event}) |
| 452 | // TODO: put the packet in the queue |
| 453 | } |
| 454 | |
| 455 | func (handler *APIHandler) ReceiveChangeEvents( |
| 456 | empty *empty.Empty, |
| 457 | changeEvents voltha.VolthaService_ReceiveChangeEventsServer, |
| 458 | ) error { |
| 459 | log.Debugw("ReceiveChangeEvents-request", log.Fields{"changeEvents": changeEvents}) |
| 460 | for { |
| 461 | // TODO: need to retrieve packet from queue |
| 462 | event := &openflow_13.ChangeEvent{} |
| 463 | time.Sleep(time.Duration(5) * time.Second) |
| 464 | err := changeEvents.Send(event) |
| 465 | if err != nil { |
| 466 | log.Errorw("Failed to send change event", log.Fields{"error": err}) |
| 467 | } |
| 468 | } |
| 469 | return nil |
| 470 | } |
| 471 | |
| 472 | func (handler *APIHandler) Subscribe( |
| 473 | ctx context.Context, |
| 474 | ofAgent *voltha.OfAgentSubscriber, |
| 475 | ) (*voltha.OfAgentSubscriber, error) { |
| 476 | log.Debugw("Subscribe-request", log.Fields{"ofAgent": ofAgent}) |
| 477 | return &voltha.OfAgentSubscriber{OfagentId: ofAgent.OfagentId, VolthaId: ofAgent.VolthaId}, nil |
| 478 | } |