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 | */ |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 16 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 17 | package core |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 18 | |
| 19 | import ( |
| 20 | "context" |
Matteo Scandolo | 360605d | 2019-11-05 18:29:17 -0800 | [diff] [blame] | 21 | "encoding/hex" |
David Bainbridge | 4087cc5 | 2019-11-13 18:36:03 +0000 | [diff] [blame] | 22 | "encoding/json" |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 23 | "errors" |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 24 | "io" |
| 25 | "sync" |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 26 | "time" |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 27 | |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 28 | "github.com/golang/protobuf/ptypes/empty" |
| 29 | da "github.com/opencord/voltha-go/common/core/northbound/grpc" |
serkant.uluderya | 2ae470f | 2020-01-21 11:13:09 -0800 | [diff] [blame] | 30 | "github.com/opencord/voltha-lib-go/v3/pkg/log" |
| 31 | "github.com/opencord/voltha-lib-go/v3/pkg/version" |
| 32 | "github.com/opencord/voltha-protos/v3/go/common" |
| 33 | "github.com/opencord/voltha-protos/v3/go/omci" |
| 34 | "github.com/opencord/voltha-protos/v3/go/openflow_13" |
| 35 | "github.com/opencord/voltha-protos/v3/go/voltha" |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 36 | "google.golang.org/grpc/codes" |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 37 | "google.golang.org/grpc/status" |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 38 | ) |
| 39 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 40 | // Image related constants |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 41 | const ( |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 42 | ImageDownload = iota |
| 43 | CancelImageDownload = iota |
| 44 | ActivateImage = iota |
| 45 | RevertImage = iota |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 46 | ) |
| 47 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 48 | // APIHandler represent attributes of API handler |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 49 | type APIHandler struct { |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 50 | deviceMgr *DeviceManager |
| 51 | logicalDeviceMgr *LogicalDeviceManager |
| 52 | adapterMgr *AdapterManager |
A R Karthick | 881e7ea | 2019-08-19 19:44:02 +0000 | [diff] [blame] | 53 | packetInQueue chan openflow_13.PacketIn |
| 54 | changeEventQueue chan openflow_13.ChangeEvent |
| 55 | packetInQueueDone chan bool |
| 56 | changeEventQueueDone chan bool |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 57 | coreInCompetingMode bool |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 58 | longRunningRequestTimeout time.Duration |
| 59 | defaultRequestTimeout time.Duration |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 60 | da.DefaultAPIHandler |
khenaidoo | 54e0ddf | 2019-02-27 16:21:33 -0500 | [diff] [blame] | 61 | core *Core |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 62 | } |
| 63 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 64 | // NewAPIHandler creates API handler instance |
khenaidoo | 54e0ddf | 2019-02-27 16:21:33 -0500 | [diff] [blame] | 65 | func NewAPIHandler(core *Core) *APIHandler { |
Stephane Barbarie | 6e1bd50 | 2018-11-05 22:44:45 -0500 | [diff] [blame] | 66 | handler := &APIHandler{ |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 67 | deviceMgr: core.deviceMgr, |
| 68 | logicalDeviceMgr: core.logicalDeviceMgr, |
| 69 | adapterMgr: core.adapterMgr, |
| 70 | coreInCompetingMode: core.config.InCompetingMode, |
| 71 | longRunningRequestTimeout: core.config.LongRunningRequestTimeout, |
| 72 | defaultRequestTimeout: core.config.DefaultRequestTimeout, |
A R Karthick | 881e7ea | 2019-08-19 19:44:02 +0000 | [diff] [blame] | 73 | packetInQueue: make(chan openflow_13.PacketIn, 100), |
| 74 | changeEventQueue: make(chan openflow_13.ChangeEvent, 100), |
| 75 | packetInQueueDone: make(chan bool, 1), |
| 76 | changeEventQueueDone: make(chan bool, 1), |
| 77 | core: core, |
Stephane Barbarie | 6e1bd50 | 2018-11-05 22:44:45 -0500 | [diff] [blame] | 78 | } |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 79 | return handler |
| 80 | } |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 81 | |
khenaidoo | 09771ef | 2019-10-11 14:25:02 -0400 | [diff] [blame] | 82 | // waitForNilResponseOnSuccess is a helper function to wait for a response on channel monitorCh where an nil |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 83 | // response is expected in a successful scenario |
| 84 | func waitForNilResponseOnSuccess(ctx context.Context, ch chan interface{}) (*empty.Empty, error) { |
| 85 | select { |
| 86 | case res := <-ch: |
| 87 | if res == nil { |
Kent Hagerman | c2c73ff | 2019-11-20 16:22:32 -0500 | [diff] [blame] | 88 | return &empty.Empty{}, nil |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 89 | } else if err, ok := res.(error); ok { |
Kent Hagerman | c2c73ff | 2019-11-20 16:22:32 -0500 | [diff] [blame] | 90 | return &empty.Empty{}, err |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 91 | } else { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 92 | logger.Warnw("unexpected-return-type", log.Fields{"result": res}) |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 93 | err = status.Errorf(codes.Internal, "%s", res) |
Kent Hagerman | c2c73ff | 2019-11-20 16:22:32 -0500 | [diff] [blame] | 94 | return &empty.Empty{}, err |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 95 | } |
| 96 | case <-ctx.Done(): |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 97 | logger.Debug("client-timeout") |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 98 | return nil, ctx.Err() |
| 99 | } |
| 100 | } |
| 101 | |
Kent Hagerman | c2c73ff | 2019-11-20 16:22:32 -0500 | [diff] [blame] | 102 | // ListCoreInstances returns details on the running core containers |
| 103 | func (handler *APIHandler) ListCoreInstances(ctx context.Context, empty *empty.Empty) (*voltha.CoreInstances, error) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 104 | logger.Debug("ListCoreInstances") |
Kent Hagerman | c2c73ff | 2019-11-20 16:22:32 -0500 | [diff] [blame] | 105 | // TODO: unused stub |
| 106 | return &voltha.CoreInstances{}, status.Errorf(codes.NotFound, "no-core-instances") |
| 107 | } |
| 108 | |
| 109 | // GetCoreInstance returns the details of a specific core container |
| 110 | func (handler *APIHandler) GetCoreInstance(ctx context.Context, id *voltha.ID) (*voltha.CoreInstance, error) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 111 | logger.Debugw("GetCoreInstance", log.Fields{"id": id}) |
Kent Hagerman | c2c73ff | 2019-11-20 16:22:32 -0500 | [diff] [blame] | 112 | //TODO: unused stub |
| 113 | return &voltha.CoreInstance{}, status.Errorf(codes.NotFound, "core-instance-%s", id.Id) |
| 114 | } |
| 115 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 116 | // GetLogicalDevicePort returns logical device port details |
khenaidoo | 43aa6bd | 2019-05-29 13:35:13 -0400 | [diff] [blame] | 117 | func (handler *APIHandler) GetLogicalDevicePort(ctx context.Context, id *voltha.LogicalPortId) (*voltha.LogicalPort, error) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 118 | logger.Debugw("GetLogicalDevicePort-request", log.Fields{"id": *id}) |
khenaidoo | 43aa6bd | 2019-05-29 13:35:13 -0400 | [diff] [blame] | 119 | |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 120 | return handler.logicalDeviceMgr.getLogicalPort(ctx, id) |
khenaidoo | 43aa6bd | 2019-05-29 13:35:13 -0400 | [diff] [blame] | 121 | } |
| 122 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 123 | // EnableLogicalDevicePort enables logical device port |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 124 | func (handler *APIHandler) EnableLogicalDevicePort(ctx context.Context, id *voltha.LogicalPortId) (*empty.Empty, error) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 125 | logger.Debugw("EnableLogicalDevicePort-request", log.Fields{"id": id, "test": common.TestModeKeys_api_test.String()}) |
khenaidoo | 9cdc1a6 | 2019-01-24 21:57:40 -0500 | [diff] [blame] | 126 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 127 | ch := make(chan interface{}) |
| 128 | defer close(ch) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 129 | go handler.logicalDeviceMgr.enableLogicalPort(ctx, id, ch) |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 130 | return waitForNilResponseOnSuccess(ctx, ch) |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 131 | } |
| 132 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 133 | // DisableLogicalDevicePort disables logical device port |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 134 | func (handler *APIHandler) DisableLogicalDevicePort(ctx context.Context, id *voltha.LogicalPortId) (*empty.Empty, error) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 135 | logger.Debugw("DisableLogicalDevicePort-request", log.Fields{"id": id, "test": common.TestModeKeys_api_test.String()}) |
khenaidoo | 9cdc1a6 | 2019-01-24 21:57:40 -0500 | [diff] [blame] | 136 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 137 | ch := make(chan interface{}) |
| 138 | defer close(ch) |
| 139 | go handler.logicalDeviceMgr.disableLogicalPort(ctx, id, ch) |
| 140 | return waitForNilResponseOnSuccess(ctx, ch) |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 141 | } |
| 142 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 143 | // UpdateLogicalDeviceFlowTable updates logical device flow table |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 144 | func (handler *APIHandler) UpdateLogicalDeviceFlowTable(ctx context.Context, flow *openflow_13.FlowTableUpdate) (*empty.Empty, error) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 145 | logger.Debugw("UpdateLogicalDeviceFlowTable-request", log.Fields{"flow": flow, "test": common.TestModeKeys_api_test.String()}) |
khenaidoo | 9cdc1a6 | 2019-01-24 21:57:40 -0500 | [diff] [blame] | 146 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 147 | ch := make(chan interface{}) |
| 148 | defer close(ch) |
| 149 | go handler.logicalDeviceMgr.updateFlowTable(ctx, flow.Id, flow.FlowMod, ch) |
| 150 | return waitForNilResponseOnSuccess(ctx, ch) |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 151 | } |
| 152 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 153 | // UpdateLogicalDeviceFlowGroupTable updates logical device flow group table |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 154 | func (handler *APIHandler) UpdateLogicalDeviceFlowGroupTable(ctx context.Context, flow *openflow_13.FlowGroupTableUpdate) (*empty.Empty, error) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 155 | logger.Debugw("UpdateLogicalDeviceFlowGroupTable-request", log.Fields{"flow": flow, "test": common.TestModeKeys_api_test.String()}) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 156 | ch := make(chan interface{}) |
| 157 | defer close(ch) |
| 158 | go handler.logicalDeviceMgr.updateGroupTable(ctx, flow.Id, flow.GroupMod, ch) |
| 159 | return waitForNilResponseOnSuccess(ctx, ch) |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 160 | } |
| 161 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 162 | // GetDevice must be implemented in the read-only containers - should it also be implemented here? |
| 163 | func (handler *APIHandler) GetDevice(ctx context.Context, id *voltha.ID) (*voltha.Device, error) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 164 | logger.Debugw("GetDevice-request", log.Fields{"id": id}) |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 165 | return handler.deviceMgr.GetDevice(ctx, id.Id) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | // GetDevice must be implemented in the read-only containers - should it also be implemented here? |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 169 | |
| 170 | // ListDevices retrieves the latest devices from the data model |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 171 | func (handler *APIHandler) ListDevices(ctx context.Context, empty *empty.Empty) (*voltha.Devices, error) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 172 | logger.Debug("ListDevices") |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 173 | devices, err := handler.deviceMgr.ListDevices(ctx) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 174 | if err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 175 | logger.Errorw("Failed to list devices", log.Fields{"error": err}) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 176 | return nil, err |
| 177 | } |
| 178 | return devices, nil |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 179 | } |
| 180 | |
khenaidoo | 7ccedd5 | 2018-12-14 16:48:54 -0500 | [diff] [blame] | 181 | // ListDeviceIds returns the list of device ids managed by a voltha core |
| 182 | func (handler *APIHandler) ListDeviceIds(ctx context.Context, empty *empty.Empty) (*voltha.IDs, error) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 183 | logger.Debug("ListDeviceIDs") |
khenaidoo | 7ccedd5 | 2018-12-14 16:48:54 -0500 | [diff] [blame] | 184 | return handler.deviceMgr.ListDeviceIds() |
| 185 | } |
| 186 | |
| 187 | //ReconcileDevices is a request to a voltha core to managed a list of devices based on their IDs |
| 188 | func (handler *APIHandler) ReconcileDevices(ctx context.Context, ids *voltha.IDs) (*empty.Empty, error) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 189 | logger.Debug("ReconcileDevices") |
khenaidoo | 9cdc1a6 | 2019-01-24 21:57:40 -0500 | [diff] [blame] | 190 | |
khenaidoo | 7ccedd5 | 2018-12-14 16:48:54 -0500 | [diff] [blame] | 191 | ch := make(chan interface{}) |
| 192 | defer close(ch) |
| 193 | go handler.deviceMgr.ReconcileDevices(ctx, ids, ch) |
| 194 | return waitForNilResponseOnSuccess(ctx, ch) |
| 195 | } |
| 196 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 197 | // GetLogicalDevice provides a cloned most up to date logical device |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 198 | func (handler *APIHandler) GetLogicalDevice(ctx context.Context, id *voltha.ID) (*voltha.LogicalDevice, error) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 199 | logger.Debugw("GetLogicalDevice-request", log.Fields{"id": id}) |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 200 | return handler.logicalDeviceMgr.getLogicalDevice(ctx, id.Id) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 201 | } |
| 202 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 203 | // ListLogicalDevices returns the list of all logical devices |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 204 | func (handler *APIHandler) ListLogicalDevices(ctx context.Context, empty *empty.Empty) (*voltha.LogicalDevices, error) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 205 | logger.Debug("ListLogicalDevices-request") |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 206 | return handler.logicalDeviceMgr.listLogicalDevices(ctx) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 207 | } |
| 208 | |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 209 | // ListAdapters returns the contents of all adapters known to the system |
| 210 | func (handler *APIHandler) ListAdapters(ctx context.Context, empty *empty.Empty) (*voltha.Adapters, error) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 211 | logger.Debug("ListAdapters") |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 212 | return handler.adapterMgr.listAdapters(ctx) |
| 213 | } |
| 214 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 215 | // ListLogicalDeviceFlows returns the flows of logical device |
khenaidoo | dd23717 | 2019-05-27 16:37:17 -0400 | [diff] [blame] | 216 | func (handler *APIHandler) ListLogicalDeviceFlows(ctx context.Context, id *voltha.ID) (*openflow_13.Flows, error) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 217 | logger.Debugw("ListLogicalDeviceFlows", log.Fields{"id": *id}) |
khenaidoo | dd23717 | 2019-05-27 16:37:17 -0400 | [diff] [blame] | 218 | return handler.logicalDeviceMgr.ListLogicalDeviceFlows(ctx, id.Id) |
| 219 | } |
| 220 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 221 | // ListLogicalDeviceFlowGroups returns logical device flow groups |
khenaidoo | dd23717 | 2019-05-27 16:37:17 -0400 | [diff] [blame] | 222 | func (handler *APIHandler) ListLogicalDeviceFlowGroups(ctx context.Context, id *voltha.ID) (*openflow_13.FlowGroups, error) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 223 | logger.Debugw("ListLogicalDeviceFlowGroups", log.Fields{"id": *id}) |
khenaidoo | dd23717 | 2019-05-27 16:37:17 -0400 | [diff] [blame] | 224 | return handler.logicalDeviceMgr.ListLogicalDeviceFlowGroups(ctx, id.Id) |
| 225 | } |
| 226 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 227 | // ListLogicalDevicePorts returns ports of logical device |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 228 | func (handler *APIHandler) ListLogicalDevicePorts(ctx context.Context, id *voltha.ID) (*voltha.LogicalPorts, error) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 229 | logger.Debugw("ListLogicalDevicePorts", log.Fields{"logicaldeviceid": id}) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 230 | return handler.logicalDeviceMgr.ListLogicalDevicePorts(ctx, id.Id) |
| 231 | } |
| 232 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 233 | // CreateDevice creates a new parent device in the data model |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 234 | func (handler *APIHandler) CreateDevice(ctx context.Context, device *voltha.Device) (*voltha.Device, error) { |
Thomas Lee S | 51b5cb8 | 2019-10-14 14:49:34 +0530 | [diff] [blame] | 235 | if device.MacAddress == "" && device.GetHostAndPort() == "" { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 236 | logger.Errorf("No Device Info Present") |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 237 | return &voltha.Device{}, errors.New("no-device-info-present; MAC or HOSTIP&PORT") |
Thomas Lee S | 51b5cb8 | 2019-10-14 14:49:34 +0530 | [diff] [blame] | 238 | } |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 239 | logger.Debugw("create-device", log.Fields{"device": *device}) |
khenaidoo | 9cdc1a6 | 2019-01-24 21:57:40 -0500 | [diff] [blame] | 240 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 241 | ch := make(chan interface{}) |
| 242 | defer close(ch) |
| 243 | go handler.deviceMgr.createDevice(ctx, device, ch) |
| 244 | select { |
| 245 | case res := <-ch: |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 246 | if res != nil { |
| 247 | if err, ok := res.(error); ok { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 248 | logger.Errorw("create-device-failed", log.Fields{"error": err}) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 249 | return nil, err |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 250 | } |
| 251 | if d, ok := res.(*voltha.Device); ok { |
| 252 | return d, nil |
| 253 | } |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 254 | } |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 255 | logger.Warnw("create-device-unexpected-return-type", log.Fields{"result": res}) |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 256 | err := status.Errorf(codes.Internal, "%s", res) |
| 257 | return &voltha.Device{}, err |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 258 | case <-ctx.Done(): |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 259 | logger.Debug("createdevice-client-timeout") |
Kent Hagerman | c2c73ff | 2019-11-20 16:22:32 -0500 | [diff] [blame] | 260 | return &voltha.Device{}, ctx.Err() |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 261 | } |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 262 | } |
| 263 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 264 | // 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] | 265 | func (handler *APIHandler) EnableDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 266 | logger.Debugw("enabledevice", log.Fields{"id": id}) |
khenaidoo | 9cdc1a6 | 2019-01-24 21:57:40 -0500 | [diff] [blame] | 267 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 268 | ch := make(chan interface{}) |
| 269 | defer close(ch) |
| 270 | go handler.deviceMgr.enableDevice(ctx, id, ch) |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 271 | return waitForNilResponseOnSuccess(ctx, ch) |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 272 | } |
| 273 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 274 | // DisableDevice disables a device along with any child device it may have |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 275 | func (handler *APIHandler) DisableDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 276 | logger.Debugw("disabledevice-request", log.Fields{"id": id}) |
khenaidoo | 9cdc1a6 | 2019-01-24 21:57:40 -0500 | [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) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 286 | logger.Debugw("rebootDevice-request", log.Fields{"id": id}) |
khenaidoo | 9cdc1a6 | 2019-01-24 21:57:40 -0500 | [diff] [blame] | 287 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 288 | ch := make(chan interface{}) |
| 289 | defer close(ch) |
| 290 | go handler.deviceMgr.rebootDevice(ctx, id, ch) |
| 291 | return waitForNilResponseOnSuccess(ctx, ch) |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 292 | } |
| 293 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 294 | // DeleteDevice removes a device from the data model |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 295 | func (handler *APIHandler) DeleteDevice(ctx context.Context, id *voltha.ID) (*empty.Empty, error) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 296 | logger.Debugw("deletedevice-request", log.Fields{"id": id}) |
khenaidoo | 9cdc1a6 | 2019-01-24 21:57:40 -0500 | [diff] [blame] | 297 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 298 | ch := make(chan interface{}) |
| 299 | defer close(ch) |
| 300 | go handler.deviceMgr.deleteDevice(ctx, id, ch) |
| 301 | return waitForNilResponseOnSuccess(ctx, ch) |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 302 | } |
| 303 | |
David Bainbridge | 4087cc5 | 2019-11-13 18:36:03 +0000 | [diff] [blame] | 304 | // ListDevicePorts returns the ports details for a specific device entry |
| 305 | func (handler *APIHandler) ListDevicePorts(ctx context.Context, id *voltha.ID) (*voltha.Ports, error) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 306 | logger.Debugw("listdeviceports-request", log.Fields{"id": id}) |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 307 | device, err := handler.deviceMgr.GetDevice(ctx, id.Id) |
David Bainbridge | 4087cc5 | 2019-11-13 18:36:03 +0000 | [diff] [blame] | 308 | if err != nil { |
Kent Hagerman | c2c73ff | 2019-11-20 16:22:32 -0500 | [diff] [blame] | 309 | return &voltha.Ports{}, err |
David Bainbridge | 4087cc5 | 2019-11-13 18:36:03 +0000 | [diff] [blame] | 310 | } |
| 311 | ports := &voltha.Ports{} |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 312 | ports.Items = append(ports.Items, device.Ports...) |
David Bainbridge | 4087cc5 | 2019-11-13 18:36:03 +0000 | [diff] [blame] | 313 | return ports, nil |
| 314 | } |
| 315 | |
| 316 | // ListDeviceFlows returns the flow details for a specific device entry |
| 317 | func (handler *APIHandler) ListDeviceFlows(ctx context.Context, id *voltha.ID) (*openflow_13.Flows, error) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 318 | logger.Debugw("listdeviceflows-request", log.Fields{"id": id}) |
David Bainbridge | 4087cc5 | 2019-11-13 18:36:03 +0000 | [diff] [blame] | 319 | |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 320 | device, err := handler.deviceMgr.GetDevice(ctx, id.Id) |
David Bainbridge | 4087cc5 | 2019-11-13 18:36:03 +0000 | [diff] [blame] | 321 | if err != nil { |
Kent Hagerman | c2c73ff | 2019-11-20 16:22:32 -0500 | [diff] [blame] | 322 | return &openflow_13.Flows{}, err |
David Bainbridge | 4087cc5 | 2019-11-13 18:36:03 +0000 | [diff] [blame] | 323 | } |
| 324 | flows := &openflow_13.Flows{} |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 325 | flows.Items = append(flows.Items, device.Flows.Items...) |
David Bainbridge | 4087cc5 | 2019-11-13 18:36:03 +0000 | [diff] [blame] | 326 | return flows, nil |
| 327 | } |
| 328 | |
Kent Hagerman | c2c73ff | 2019-11-20 16:22:32 -0500 | [diff] [blame] | 329 | // ListDeviceFlowGroups returns the flow group details for a specific device entry |
| 330 | func (handler *APIHandler) ListDeviceFlowGroups(ctx context.Context, id *voltha.ID) (*voltha.FlowGroups, error) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 331 | logger.Debugw("ListDeviceFlowGroups", log.Fields{"deviceid": id}) |
Kent Hagerman | c2c73ff | 2019-11-20 16:22:32 -0500 | [diff] [blame] | 332 | |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 333 | if device, _ := handler.deviceMgr.GetDevice(ctx, id.Id); device != nil { |
Kent Hagerman | c2c73ff | 2019-11-20 16:22:32 -0500 | [diff] [blame] | 334 | return device.GetFlowGroups(), nil |
| 335 | } |
| 336 | return &voltha.FlowGroups{}, status.Errorf(codes.NotFound, "device-%s", id.Id) |
| 337 | } |
| 338 | |
| 339 | // ListDeviceGroups returns all the device groups known to the system |
| 340 | func (handler *APIHandler) ListDeviceGroups(ctx context.Context, empty *empty.Empty) (*voltha.DeviceGroups, error) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 341 | logger.Debug("ListDeviceGroups") |
Kent Hagerman | c2c73ff | 2019-11-20 16:22:32 -0500 | [diff] [blame] | 342 | return &voltha.DeviceGroups{}, errors.New("UnImplemented") |
| 343 | } |
| 344 | |
| 345 | // GetDeviceGroup returns a specific device group entry |
| 346 | func (handler *APIHandler) GetDeviceGroup(ctx context.Context, id *voltha.ID) (*voltha.DeviceGroup, error) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 347 | logger.Debug("GetDeviceGroup") |
Kent Hagerman | c2c73ff | 2019-11-20 16:22:32 -0500 | [diff] [blame] | 348 | return &voltha.DeviceGroup{}, errors.New("UnImplemented") |
| 349 | } |
| 350 | |
| 351 | // ListDeviceTypes returns all the device types known to the system |
| 352 | func (handler *APIHandler) ListDeviceTypes(ctx context.Context, _ *empty.Empty) (*voltha.DeviceTypes, error) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 353 | logger.Debug("ListDeviceTypes") |
Kent Hagerman | c2c73ff | 2019-11-20 16:22:32 -0500 | [diff] [blame] | 354 | |
| 355 | return &voltha.DeviceTypes{Items: handler.adapterMgr.listDeviceTypes()}, nil |
| 356 | } |
| 357 | |
| 358 | // GetDeviceType returns the device type for a specific device entry |
| 359 | func (handler *APIHandler) GetDeviceType(ctx context.Context, id *voltha.ID) (*voltha.DeviceType, error) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 360 | logger.Debugw("GetDeviceType", log.Fields{"typeid": id}) |
Kent Hagerman | c2c73ff | 2019-11-20 16:22:32 -0500 | [diff] [blame] | 361 | |
| 362 | if deviceType := handler.adapterMgr.getDeviceType(id.Id); deviceType != nil { |
| 363 | return deviceType, nil |
| 364 | } |
| 365 | return &voltha.DeviceType{}, status.Errorf(codes.NotFound, "device_type-%s", id.Id) |
| 366 | } |
| 367 | |
David Bainbridge | 4087cc5 | 2019-11-13 18:36:03 +0000 | [diff] [blame] | 368 | // GetVoltha returns the contents of all components (i.e. devices, logical_devices, ...) |
| 369 | func (handler *APIHandler) GetVoltha(ctx context.Context, empty *empty.Empty) (*voltha.Voltha, error) { |
| 370 | |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 371 | logger.Debug("GetVoltha") |
David Bainbridge | 4087cc5 | 2019-11-13 18:36:03 +0000 | [diff] [blame] | 372 | /* |
| 373 | * For now, encode all the version information into a JSON object and |
| 374 | * pass that back as "version" so the client can get all the |
| 375 | * information associated with the version. Long term the API should |
| 376 | * better accomidate this, but for now this will work. |
| 377 | */ |
| 378 | data, err := json.Marshal(&version.VersionInfo) |
| 379 | info := version.VersionInfo.Version |
| 380 | if err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 381 | logger.Warnf("Unable to encode version information as JSON: %s", err.Error()) |
David Bainbridge | 4087cc5 | 2019-11-13 18:36:03 +0000 | [diff] [blame] | 382 | } else { |
| 383 | info = string(data) |
| 384 | } |
| 385 | |
| 386 | return &voltha.Voltha{ |
| 387 | Version: info, |
| 388 | }, nil |
| 389 | } |
| 390 | |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 391 | // processImageRequest is a helper method to execute an image download request |
| 392 | func (handler *APIHandler) processImageRequest(ctx context.Context, img *voltha.ImageDownload, requestType int) (*common.OperationResp, error) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 393 | logger.Debugw("processImageDownload", log.Fields{"img": *img, "requestType": requestType}) |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 394 | |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 395 | failedresponse := &common.OperationResp{Code: voltha.OperationResp_OPERATION_FAILURE} |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 396 | |
| 397 | ch := make(chan interface{}) |
| 398 | defer close(ch) |
| 399 | switch requestType { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 400 | case ImageDownload: |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 401 | go handler.deviceMgr.downloadImage(ctx, img, ch) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 402 | case CancelImageDownload: |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 403 | go handler.deviceMgr.cancelImageDownload(ctx, img, ch) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 404 | case ActivateImage: |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 405 | go handler.deviceMgr.activateImage(ctx, img, ch) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 406 | case RevertImage: |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 407 | go handler.deviceMgr.revertImage(ctx, img, ch) |
| 408 | default: |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 409 | logger.Warn("invalid-request-type", log.Fields{"requestType": requestType}) |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 410 | return failedresponse, status.Errorf(codes.InvalidArgument, "%d", requestType) |
| 411 | } |
| 412 | select { |
| 413 | case res := <-ch: |
| 414 | if res != nil { |
| 415 | if err, ok := res.(error); ok { |
| 416 | return failedresponse, err |
| 417 | } |
| 418 | if opResp, ok := res.(*common.OperationResp); ok { |
| 419 | return opResp, nil |
| 420 | } |
| 421 | } |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 422 | logger.Warnw("download-image-unexpected-return-type", log.Fields{"result": res}) |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 423 | return failedresponse, status.Errorf(codes.Internal, "%s", res) |
| 424 | case <-ctx.Done(): |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 425 | logger.Debug("downloadImage-client-timeout") |
Kent Hagerman | c2c73ff | 2019-11-20 16:22:32 -0500 | [diff] [blame] | 426 | return &common.OperationResp{}, ctx.Err() |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 427 | } |
| 428 | } |
| 429 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 430 | // DownloadImage execute an image download request |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 431 | func (handler *APIHandler) DownloadImage(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 432 | logger.Debugw("DownloadImage-request", log.Fields{"img": *img}) |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 433 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 434 | return handler.processImageRequest(ctx, img, ImageDownload) |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 435 | } |
| 436 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 437 | // CancelImageDownload cancels image download request |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 438 | func (handler *APIHandler) CancelImageDownload(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 439 | logger.Debugw("cancelImageDownload-request", log.Fields{"img": *img}) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 440 | return handler.processImageRequest(ctx, img, CancelImageDownload) |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 441 | } |
| 442 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 443 | // ActivateImageUpdate activates image update request |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 444 | func (handler *APIHandler) ActivateImageUpdate(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 445 | logger.Debugw("activateImageUpdate-request", log.Fields{"img": *img}) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 446 | return handler.processImageRequest(ctx, img, ActivateImage) |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 447 | } |
| 448 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 449 | // RevertImageUpdate reverts image update |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 450 | func (handler *APIHandler) RevertImageUpdate(ctx context.Context, img *voltha.ImageDownload) (*common.OperationResp, error) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 451 | logger.Debugw("revertImageUpdate-request", log.Fields{"img": *img}) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 452 | return handler.processImageRequest(ctx, img, RevertImage) |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 453 | } |
| 454 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 455 | // GetImageDownloadStatus returns status of image download |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 456 | func (handler *APIHandler) GetImageDownloadStatus(ctx context.Context, img *voltha.ImageDownload) (*voltha.ImageDownload, error) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 457 | logger.Debugw("getImageDownloadStatus-request", log.Fields{"img": *img}) |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 458 | |
Stephane Barbarie | df5479f | 2019-01-29 22:13:00 -0500 | [diff] [blame] | 459 | failedresponse := &voltha.ImageDownload{DownloadState: voltha.ImageDownload_DOWNLOAD_UNKNOWN} |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 460 | |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 461 | ch := make(chan interface{}) |
| 462 | defer close(ch) |
| 463 | go handler.deviceMgr.getImageDownloadStatus(ctx, img, ch) |
| 464 | |
| 465 | select { |
| 466 | case res := <-ch: |
| 467 | if res != nil { |
| 468 | if err, ok := res.(error); ok { |
| 469 | return failedresponse, err |
| 470 | } |
| 471 | if downloadResp, ok := res.(*voltha.ImageDownload); ok { |
| 472 | return downloadResp, nil |
| 473 | } |
| 474 | } |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 475 | logger.Warnw("download-image-status", log.Fields{"result": res}) |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 476 | return failedresponse, status.Errorf(codes.Internal, "%s", res) |
| 477 | case <-ctx.Done(): |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 478 | logger.Debug("downloadImage-client-timeout") |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 479 | return failedresponse, ctx.Err() |
| 480 | } |
| 481 | } |
| 482 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 483 | // GetImageDownload returns image download |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 484 | func (handler *APIHandler) GetImageDownload(ctx context.Context, img *voltha.ImageDownload) (*voltha.ImageDownload, error) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 485 | logger.Debugw("GetImageDownload-request", log.Fields{"img": *img}) |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 486 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 487 | download, err := handler.deviceMgr.getImageDownload(ctx, img) |
| 488 | if err != nil { |
Stephane Barbarie | df5479f | 2019-01-29 22:13:00 -0500 | [diff] [blame] | 489 | return &voltha.ImageDownload{DownloadState: voltha.ImageDownload_DOWNLOAD_UNKNOWN}, err |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 490 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 491 | return download, nil |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 492 | } |
| 493 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 494 | // ListImageDownloads returns image downloads |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 495 | func (handler *APIHandler) ListImageDownloads(ctx context.Context, id *voltha.ID) (*voltha.ImageDownloads, error) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 496 | logger.Debugw("ListImageDownloads-request", log.Fields{"deviceId": id.Id}) |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 497 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 498 | downloads, err := handler.deviceMgr.listImageDownloads(ctx, id.Id) |
| 499 | if err != nil { |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 500 | failedResp := &voltha.ImageDownloads{ |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 501 | Items: []*voltha.ImageDownload{ |
| 502 | {DownloadState: voltha.ImageDownload_DOWNLOAD_UNKNOWN}, |
| 503 | }, |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 504 | } |
| 505 | return failedResp, err |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 506 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 507 | return downloads, nil |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 508 | } |
| 509 | |
Kent Hagerman | c2c73ff | 2019-11-20 16:22:32 -0500 | [diff] [blame] | 510 | // GetImages returns all images for a specific device entry |
| 511 | func (handler *APIHandler) GetImages(ctx context.Context, id *voltha.ID) (*voltha.Images, error) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 512 | logger.Debugw("GetImages", log.Fields{"deviceid": id.Id}) |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 513 | device, err := handler.deviceMgr.GetDevice(ctx, id.Id) |
Kent Hagerman | c2c73ff | 2019-11-20 16:22:32 -0500 | [diff] [blame] | 514 | if err != nil { |
| 515 | return &voltha.Images{}, err |
| 516 | } |
| 517 | return device.GetImages(), nil |
| 518 | } |
| 519 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 520 | // UpdateDevicePmConfigs updates the PM configs |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 521 | func (handler *APIHandler) UpdateDevicePmConfigs(ctx context.Context, configs *voltha.PmConfigs) (*empty.Empty, error) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 522 | logger.Debugw("UpdateDevicePmConfigs-request", log.Fields{"configs": *configs}) |
khenaidoo | b312747 | 2019-07-24 21:04:55 -0400 | [diff] [blame] | 523 | |
| 524 | ch := make(chan interface{}) |
| 525 | defer close(ch) |
| 526 | go handler.deviceMgr.updatePmConfigs(ctx, configs, ch) |
| 527 | return waitForNilResponseOnSuccess(ctx, ch) |
| 528 | } |
| 529 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 530 | // ListDevicePmConfigs returns pm configs of device |
khenaidoo | b312747 | 2019-07-24 21:04:55 -0400 | [diff] [blame] | 531 | func (handler *APIHandler) ListDevicePmConfigs(ctx context.Context, id *voltha.ID) (*voltha.PmConfigs, error) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 532 | logger.Debugw("ListDevicePmConfigs-request", log.Fields{"deviceId": *id}) |
khenaidoo | b312747 | 2019-07-24 21:04:55 -0400 | [diff] [blame] | 533 | return handler.deviceMgr.listPmConfigs(ctx, id.Id) |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 534 | } |
| 535 | |
Devmalya Paul | c594bb3 | 2019-11-06 07:34:27 +0000 | [diff] [blame] | 536 | func (handler *APIHandler) CreateEventFilter(ctx context.Context, filter *voltha.EventFilter) (*voltha.EventFilter, error) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 537 | logger.Debugw("CreateEventFilter-request", log.Fields{"filter": *filter}) |
Devmalya Paul | c594bb3 | 2019-11-06 07:34:27 +0000 | [diff] [blame] | 538 | return nil, errors.New("UnImplemented") |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 539 | } |
| 540 | |
Devmalya Paul | c594bb3 | 2019-11-06 07:34:27 +0000 | [diff] [blame] | 541 | func (handler *APIHandler) UpdateEventFilter(ctx context.Context, filter *voltha.EventFilter) (*voltha.EventFilter, error) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 542 | logger.Debugw("UpdateEventFilter-request", log.Fields{"filter": *filter}) |
Devmalya Paul | c594bb3 | 2019-11-06 07:34:27 +0000 | [diff] [blame] | 543 | return nil, errors.New("UnImplemented") |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 544 | } |
| 545 | |
Devmalya Paul | c594bb3 | 2019-11-06 07:34:27 +0000 | [diff] [blame] | 546 | func (handler *APIHandler) DeleteEventFilter(ctx context.Context, filterInfo *voltha.EventFilter) (*empty.Empty, error) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 547 | logger.Debugw("DeleteEventFilter-request", log.Fields{"device-id": filterInfo.DeviceId, "filter-id": filterInfo.Id}) |
Devmalya Paul | c594bb3 | 2019-11-06 07:34:27 +0000 | [diff] [blame] | 548 | return nil, errors.New("UnImplemented") |
Kent Hagerman | c2c73ff | 2019-11-20 16:22:32 -0500 | [diff] [blame] | 549 | } |
| 550 | |
Devmalya Paul | c594bb3 | 2019-11-06 07:34:27 +0000 | [diff] [blame] | 551 | // GetEventFilter returns all the filters present for a device |
| 552 | func (handler *APIHandler) GetEventFilter(ctx context.Context, id *voltha.ID) (*voltha.EventFilters, error) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 553 | logger.Debugw("GetEventFilter-request", log.Fields{"device-id": id}) |
Devmalya Paul | c594bb3 | 2019-11-06 07:34:27 +0000 | [diff] [blame] | 554 | return nil, errors.New("UnImplemented") |
Kent Hagerman | c2c73ff | 2019-11-20 16:22:32 -0500 | [diff] [blame] | 555 | } |
| 556 | |
Devmalya Paul | c594bb3 | 2019-11-06 07:34:27 +0000 | [diff] [blame] | 557 | // ListEventFilters returns all the filters known to the system |
| 558 | func (handler *APIHandler) ListEventFilters(ctx context.Context, empty *empty.Empty) (*voltha.EventFilters, error) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 559 | logger.Debug("ListEventFilter-request") |
Devmalya Paul | c594bb3 | 2019-11-06 07:34:27 +0000 | [diff] [blame] | 560 | return nil, errors.New("UnImplemented") |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | func (handler *APIHandler) SelfTest(ctx context.Context, id *voltha.ID) (*voltha.SelfTestResponse, error) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 564 | logger.Debugw("SelfTest-request", log.Fields{"id": id}) |
Kent Hagerman | c2c73ff | 2019-11-20 16:22:32 -0500 | [diff] [blame] | 565 | return &voltha.SelfTestResponse{}, errors.New("UnImplemented") |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 566 | } |
Stephane Barbarie | 6e1bd50 | 2018-11-05 22:44:45 -0500 | [diff] [blame] | 567 | |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 568 | func (handler *APIHandler) forwardPacketOut(ctx context.Context, packet *openflow_13.PacketOut) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 569 | logger.Debugw("forwardPacketOut-request", log.Fields{"packet": packet}) |
khenaidoo | 3d3b8c2 | 2019-05-22 18:10:39 -0400 | [diff] [blame] | 570 | //TODO: Update this logic once the OF Controller (OFAgent in this case) can include a transaction Id in its |
| 571 | // request. For performance reason we can let both Cores in a Core-Pair forward the Packet to the adapters and |
| 572 | // let once of the shim layer (kafka proxy or adapter request handler filters out the duplicate packet) |
David Bainbridge | d1afd66 | 2020-03-26 18:27:41 -0700 | [diff] [blame] | 573 | if agent := handler.logicalDeviceMgr.getLogicalDeviceAgent(ctx, packet.Id); agent != nil { |
| 574 | agent.packetOut(ctx, packet.PacketOut) |
| 575 | } else { |
| 576 | logger.Errorf("No logical device agent present", log.Fields{"logicaldeviceID": packet.Id}) |
khenaidoo | 3d3b8c2 | 2019-05-22 18:10:39 -0400 | [diff] [blame] | 577 | } |
Stephane Barbarie | 6e1bd50 | 2018-11-05 22:44:45 -0500 | [diff] [blame] | 578 | } |
khenaidoo | 3d3b8c2 | 2019-05-22 18:10:39 -0400 | [diff] [blame] | 579 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 580 | // StreamPacketsOut sends packets to adapter |
Kent Hagerman | c2c73ff | 2019-11-20 16:22:32 -0500 | [diff] [blame] | 581 | func (handler *APIHandler) StreamPacketsOut(packets voltha.VolthaService_StreamPacketsOutServer) error { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 582 | logger.Debugw("StreamPacketsOut-request", log.Fields{"packets": packets}) |
khenaidoo | 5e25069 | 2019-08-30 14:46:21 -0400 | [diff] [blame] | 583 | loop: |
Stephane Barbarie | 6e1bd50 | 2018-11-05 22:44:45 -0500 | [diff] [blame] | 584 | for { |
khenaidoo | 5e25069 | 2019-08-30 14:46:21 -0400 | [diff] [blame] | 585 | select { |
| 586 | case <-packets.Context().Done(): |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 587 | logger.Infow("StreamPacketsOut-context-done", log.Fields{"packets": packets, "error": packets.Context().Err()}) |
khenaidoo | 5e25069 | 2019-08-30 14:46:21 -0400 | [diff] [blame] | 588 | break loop |
| 589 | default: |
| 590 | } |
| 591 | |
Stephane Barbarie | 6e1bd50 | 2018-11-05 22:44:45 -0500 | [diff] [blame] | 592 | packet, err := packets.Recv() |
| 593 | |
| 594 | if err == io.EOF { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 595 | logger.Debugw("Received-EOF", log.Fields{"packets": packets}) |
khenaidoo | 5e25069 | 2019-08-30 14:46:21 -0400 | [diff] [blame] | 596 | break loop |
| 597 | } |
| 598 | |
| 599 | if err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 600 | logger.Errorw("Failed to receive packet out", log.Fields{"error": err}) |
khenaidoo | 5e25069 | 2019-08-30 14:46:21 -0400 | [diff] [blame] | 601 | continue |
Stephane Barbarie | 6e1bd50 | 2018-11-05 22:44:45 -0500 | [diff] [blame] | 602 | } |
| 603 | |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 604 | handler.forwardPacketOut(packets.Context(), packet) |
Stephane Barbarie | 6e1bd50 | 2018-11-05 22:44:45 -0500 | [diff] [blame] | 605 | } |
| 606 | |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 607 | logger.Debugw("StreamPacketsOut-request-done", log.Fields{"packets": packets}) |
Stephane Barbarie | 6e1bd50 | 2018-11-05 22:44:45 -0500 | [diff] [blame] | 608 | return nil |
| 609 | } |
| 610 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 611 | func (handler *APIHandler) sendPacketIn(deviceID string, transationID string, packet *openflow_13.OfpPacketIn) { |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 612 | // TODO: Augment the OF PacketIn to include the transactionId |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 613 | packetIn := openflow_13.PacketIn{Id: deviceID, PacketIn: packet} |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 614 | logger.Debugw("sendPacketIn", log.Fields{"packetIn": packetIn}) |
A R Karthick | 881e7ea | 2019-08-19 19:44:02 +0000 | [diff] [blame] | 615 | handler.packetInQueue <- packetIn |
| 616 | } |
| 617 | |
| 618 | type callTracker struct { |
| 619 | failedPacket interface{} |
| 620 | } |
| 621 | type streamTracker struct { |
| 622 | calls map[string]*callTracker |
| 623 | sync.Mutex |
| 624 | } |
| 625 | |
| 626 | var streamingTracker = &streamTracker{calls: make(map[string]*callTracker)} |
| 627 | |
| 628 | func (handler *APIHandler) getStreamingTracker(method string, done chan<- bool) *callTracker { |
| 629 | streamingTracker.Lock() |
| 630 | defer streamingTracker.Unlock() |
| 631 | if _, ok := streamingTracker.calls[method]; ok { |
| 632 | // bail out the other packet in thread |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 633 | logger.Debugf("%s streaming call already running. Exiting it", method) |
A R Karthick | 881e7ea | 2019-08-19 19:44:02 +0000 | [diff] [blame] | 634 | done <- true |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 635 | logger.Debugf("Last %s exited. Continuing ...", method) |
A R Karthick | 881e7ea | 2019-08-19 19:44:02 +0000 | [diff] [blame] | 636 | } else { |
| 637 | streamingTracker.calls[method] = &callTracker{failedPacket: nil} |
Richard Jankowski | dbab94a | 2018-12-06 16:20:25 -0500 | [diff] [blame] | 638 | } |
A R Karthick | 881e7ea | 2019-08-19 19:44:02 +0000 | [diff] [blame] | 639 | return streamingTracker.calls[method] |
| 640 | } |
| 641 | |
| 642 | func (handler *APIHandler) flushFailedPackets(tracker *callTracker) error { |
| 643 | if tracker.failedPacket != nil { |
| 644 | switch tracker.failedPacket.(type) { |
| 645 | case openflow_13.PacketIn: |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 646 | logger.Debug("Enqueueing last failed packetIn") |
A R Karthick | 881e7ea | 2019-08-19 19:44:02 +0000 | [diff] [blame] | 647 | handler.packetInQueue <- tracker.failedPacket.(openflow_13.PacketIn) |
| 648 | case openflow_13.ChangeEvent: |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 649 | logger.Debug("Enqueueing last failed changeEvent") |
A R Karthick | 881e7ea | 2019-08-19 19:44:02 +0000 | [diff] [blame] | 650 | handler.changeEventQueue <- tracker.failedPacket.(openflow_13.ChangeEvent) |
| 651 | } |
| 652 | } |
| 653 | return nil |
Stephane Barbarie | 6e1bd50 | 2018-11-05 22:44:45 -0500 | [diff] [blame] | 654 | } |
| 655 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 656 | // ReceivePacketsIn receives packets from adapter |
Kent Hagerman | c2c73ff | 2019-11-20 16:22:32 -0500 | [diff] [blame] | 657 | func (handler *APIHandler) ReceivePacketsIn(empty *empty.Empty, packetsIn voltha.VolthaService_ReceivePacketsInServer) error { |
A R Karthick | 881e7ea | 2019-08-19 19:44:02 +0000 | [diff] [blame] | 658 | var streamingTracker = handler.getStreamingTracker("ReceivePacketsIn", handler.packetInQueueDone) |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 659 | logger.Debugw("ReceivePacketsIn-request", log.Fields{"packetsIn": packetsIn}) |
Stephane Barbarie | 6e1bd50 | 2018-11-05 22:44:45 -0500 | [diff] [blame] | 660 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 661 | err := handler.flushFailedPackets(streamingTracker) |
| 662 | if err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 663 | logger.Errorw("unable-to-flush-failed-packets", log.Fields{"error": err}) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 664 | } |
A R Karthick | 881e7ea | 2019-08-19 19:44:02 +0000 | [diff] [blame] | 665 | |
| 666 | loop: |
Stephane Barbarie | 6e1bd50 | 2018-11-05 22:44:45 -0500 | [diff] [blame] | 667 | for { |
A R Karthick | 881e7ea | 2019-08-19 19:44:02 +0000 | [diff] [blame] | 668 | select { |
| 669 | case packet := <-handler.packetInQueue: |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 670 | logger.Debugw("sending-packet-in", log.Fields{ |
Matteo Scandolo | 360605d | 2019-11-05 18:29:17 -0800 | [diff] [blame] | 671 | "packet": hex.EncodeToString(packet.PacketIn.Data), |
| 672 | }) |
A R Karthick | 881e7ea | 2019-08-19 19:44:02 +0000 | [diff] [blame] | 673 | if err := packetsIn.Send(&packet); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 674 | logger.Errorw("failed-to-send-packet", log.Fields{"error": err}) |
A R Karthick | 881e7ea | 2019-08-19 19:44:02 +0000 | [diff] [blame] | 675 | // save the last failed packet in |
| 676 | streamingTracker.failedPacket = packet |
| 677 | } else { |
| 678 | if streamingTracker.failedPacket != nil { |
| 679 | // reset last failed packet saved to avoid flush |
| 680 | streamingTracker.failedPacket = nil |
Richard Jankowski | dbab94a | 2018-12-06 16:20:25 -0500 | [diff] [blame] | 681 | } |
| 682 | } |
A R Karthick | 881e7ea | 2019-08-19 19:44:02 +0000 | [diff] [blame] | 683 | case <-handler.packetInQueueDone: |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 684 | logger.Debug("Another ReceivePacketsIn running. Bailing out ...") |
A R Karthick | 881e7ea | 2019-08-19 19:44:02 +0000 | [diff] [blame] | 685 | break loop |
Stephane Barbarie | 6e1bd50 | 2018-11-05 22:44:45 -0500 | [diff] [blame] | 686 | } |
| 687 | } |
A R Karthick | 881e7ea | 2019-08-19 19:44:02 +0000 | [diff] [blame] | 688 | |
| 689 | //TODO: Find an elegant way to get out of the above loop when the Core is stopped |
| 690 | return nil |
Stephane Barbarie | 6e1bd50 | 2018-11-05 22:44:45 -0500 | [diff] [blame] | 691 | } |
| 692 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 693 | func (handler *APIHandler) sendChangeEvent(deviceID string, portStatus *openflow_13.OfpPortStatus) { |
Stephane Barbarie | 6e1bd50 | 2018-11-05 22:44:45 -0500 | [diff] [blame] | 694 | // TODO: validate the type of portStatus parameter |
| 695 | //if _, ok := portStatus.(*openflow_13.OfpPortStatus); ok { |
| 696 | //} |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 697 | event := openflow_13.ChangeEvent{Id: deviceID, Event: &openflow_13.ChangeEvent_PortStatus{PortStatus: portStatus}} |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 698 | logger.Debugw("sendChangeEvent", log.Fields{"event": event}) |
A R Karthick | 881e7ea | 2019-08-19 19:44:02 +0000 | [diff] [blame] | 699 | handler.changeEventQueue <- event |
Stephane Barbarie | 6e1bd50 | 2018-11-05 22:44:45 -0500 | [diff] [blame] | 700 | } |
| 701 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 702 | // ReceiveChangeEvents receives change in events |
Kent Hagerman | c2c73ff | 2019-11-20 16:22:32 -0500 | [diff] [blame] | 703 | func (handler *APIHandler) ReceiveChangeEvents(empty *empty.Empty, changeEvents voltha.VolthaService_ReceiveChangeEventsServer) error { |
A R Karthick | 881e7ea | 2019-08-19 19:44:02 +0000 | [diff] [blame] | 704 | var streamingTracker = handler.getStreamingTracker("ReceiveChangeEvents", handler.changeEventQueueDone) |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 705 | logger.Debugw("ReceiveChangeEvents-request", log.Fields{"changeEvents": changeEvents}) |
A R Karthick | 881e7ea | 2019-08-19 19:44:02 +0000 | [diff] [blame] | 706 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 707 | err := handler.flushFailedPackets(streamingTracker) |
| 708 | if err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 709 | logger.Errorw("unable-to-flush-failed-packets", log.Fields{"error": err}) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 710 | } |
A R Karthick | 881e7ea | 2019-08-19 19:44:02 +0000 | [diff] [blame] | 711 | |
| 712 | loop: |
Stephane Barbarie | 6e1bd50 | 2018-11-05 22:44:45 -0500 | [diff] [blame] | 713 | for { |
A R Karthick | 881e7ea | 2019-08-19 19:44:02 +0000 | [diff] [blame] | 714 | select { |
Richard Jankowski | 199fd86 | 2019-03-18 14:49:51 -0400 | [diff] [blame] | 715 | // Dequeue a change event |
A R Karthick | 881e7ea | 2019-08-19 19:44:02 +0000 | [diff] [blame] | 716 | case event := <-handler.changeEventQueue: |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 717 | logger.Debugw("sending-change-event", log.Fields{"event": event}) |
A R Karthick | 881e7ea | 2019-08-19 19:44:02 +0000 | [diff] [blame] | 718 | if err := changeEvents.Send(&event); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 719 | logger.Errorw("failed-to-send-change-event", log.Fields{"error": err}) |
A R Karthick | 881e7ea | 2019-08-19 19:44:02 +0000 | [diff] [blame] | 720 | // save last failed changeevent |
| 721 | streamingTracker.failedPacket = event |
| 722 | } else { |
| 723 | if streamingTracker.failedPacket != nil { |
| 724 | // reset last failed event saved on success to avoid flushing |
| 725 | streamingTracker.failedPacket = nil |
Richard Jankowski | 199fd86 | 2019-03-18 14:49:51 -0400 | [diff] [blame] | 726 | } |
| 727 | } |
A R Karthick | 881e7ea | 2019-08-19 19:44:02 +0000 | [diff] [blame] | 728 | case <-handler.changeEventQueueDone: |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 729 | logger.Debug("Another ReceiveChangeEvents already running. Bailing out ...") |
A R Karthick | 881e7ea | 2019-08-19 19:44:02 +0000 | [diff] [blame] | 730 | break loop |
Stephane Barbarie | 6e1bd50 | 2018-11-05 22:44:45 -0500 | [diff] [blame] | 731 | } |
| 732 | } |
A R Karthick | 881e7ea | 2019-08-19 19:44:02 +0000 | [diff] [blame] | 733 | |
| 734 | return nil |
Richard Jankowski | 199fd86 | 2019-03-18 14:49:51 -0400 | [diff] [blame] | 735 | } |
Stephane Barbarie | 6e1bd50 | 2018-11-05 22:44:45 -0500 | [diff] [blame] | 736 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 737 | // Subscribe subscribing request of ofagent |
Stephane Barbarie | 6e1bd50 | 2018-11-05 22:44:45 -0500 | [diff] [blame] | 738 | func (handler *APIHandler) Subscribe( |
| 739 | ctx context.Context, |
| 740 | ofAgent *voltha.OfAgentSubscriber, |
| 741 | ) (*voltha.OfAgentSubscriber, error) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 742 | logger.Debugw("Subscribe-request", log.Fields{"ofAgent": ofAgent}) |
Stephane Barbarie | 6e1bd50 | 2018-11-05 22:44:45 -0500 | [diff] [blame] | 743 | return &voltha.OfAgentSubscriber{OfagentId: ofAgent.OfagentId, VolthaId: ofAgent.VolthaId}, nil |
| 744 | } |
William Kurkian | daa6bb2 | 2019-03-07 12:26:28 -0500 | [diff] [blame] | 745 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 746 | // GetAlarmDeviceData @TODO useless stub, what should this actually do? |
Kent Hagerman | c2c73ff | 2019-11-20 16:22:32 -0500 | [diff] [blame] | 747 | func (handler *APIHandler) GetAlarmDeviceData(ctx context.Context, in *common.ID) (*omci.AlarmDeviceData, error) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 748 | logger.Debug("GetAlarmDeviceData-stub") |
Kent Hagerman | c2c73ff | 2019-11-20 16:22:32 -0500 | [diff] [blame] | 749 | return &omci.AlarmDeviceData{}, errors.New("UnImplemented") |
William Kurkian | daa6bb2 | 2019-03-07 12:26:28 -0500 | [diff] [blame] | 750 | } |
| 751 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 752 | // ListLogicalDeviceMeters returns logical device meters |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 753 | func (handler *APIHandler) ListLogicalDeviceMeters(ctx context.Context, id *voltha.ID) (*openflow_13.Meters, error) { |
| 754 | |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 755 | logger.Debugw("ListLogicalDeviceMeters", log.Fields{"id": *id}) |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 756 | return handler.logicalDeviceMgr.ListLogicalDeviceMeters(ctx, id.Id) |
William Kurkian | daa6bb2 | 2019-03-07 12:26:28 -0500 | [diff] [blame] | 757 | } |
| 758 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 759 | // GetMeterStatsOfLogicalDevice @TODO useless stub, what should this actually do? |
Kent Hagerman | c2c73ff | 2019-11-20 16:22:32 -0500 | [diff] [blame] | 760 | func (handler *APIHandler) GetMeterStatsOfLogicalDevice(ctx context.Context, in *common.ID) (*openflow_13.MeterStatsReply, error) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 761 | logger.Debug("GetMeterStatsOfLogicalDevice") |
Kent Hagerman | c2c73ff | 2019-11-20 16:22:32 -0500 | [diff] [blame] | 762 | return &openflow_13.MeterStatsReply{}, errors.New("UnImplemented") |
| 763 | } |
| 764 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 765 | // GetMibDeviceData @TODO useless stub, what should this actually do? |
Kent Hagerman | c2c73ff | 2019-11-20 16:22:32 -0500 | [diff] [blame] | 766 | func (handler *APIHandler) GetMibDeviceData(ctx context.Context, in *common.ID) (*omci.MibDeviceData, error) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 767 | logger.Debug("GetMibDeviceData") |
Kent Hagerman | c2c73ff | 2019-11-20 16:22:32 -0500 | [diff] [blame] | 768 | return &omci.MibDeviceData{}, errors.New("UnImplemented") |
William Kurkian | daa6bb2 | 2019-03-07 12:26:28 -0500 | [diff] [blame] | 769 | } |
| 770 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 771 | // SimulateAlarm sends simulate alarm request |
William Kurkian | daa6bb2 | 2019-03-07 12:26:28 -0500 | [diff] [blame] | 772 | func (handler *APIHandler) SimulateAlarm( |
| 773 | ctx context.Context, |
| 774 | in *voltha.SimulateAlarmRequest, |
| 775 | ) (*common.OperationResp, error) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 776 | logger.Debugw("SimulateAlarm-request", log.Fields{"id": in.Id}) |
serkant.uluderya | 334479d | 2019-04-10 08:26:15 -0700 | [diff] [blame] | 777 | successResp := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS} |
serkant.uluderya | 334479d | 2019-04-10 08:26:15 -0700 | [diff] [blame] | 778 | ch := make(chan interface{}) |
| 779 | defer close(ch) |
| 780 | go handler.deviceMgr.simulateAlarm(ctx, in, ch) |
| 781 | return successResp, nil |
William Kurkian | daa6bb2 | 2019-03-07 12:26:28 -0500 | [diff] [blame] | 782 | } |
| 783 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 784 | // UpdateLogicalDeviceMeterTable - This function sends meter mod request to logical device manager and waits for response |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 785 | func (handler *APIHandler) UpdateLogicalDeviceMeterTable(ctx context.Context, meter *openflow_13.MeterModUpdate) (*empty.Empty, error) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 786 | logger.Debugw("UpdateLogicalDeviceMeterTable-request", |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 787 | log.Fields{"meter": meter, "test": common.TestModeKeys_api_test.String()}) |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 788 | ch := make(chan interface{}) |
| 789 | defer close(ch) |
| 790 | go handler.logicalDeviceMgr.updateMeterTable(ctx, meter.Id, meter.MeterMod, ch) |
| 791 | return waitForNilResponseOnSuccess(ctx, ch) |
William Kurkian | daa6bb2 | 2019-03-07 12:26:28 -0500 | [diff] [blame] | 792 | } |
Kent Hagerman | c2c73ff | 2019-11-20 16:22:32 -0500 | [diff] [blame] | 793 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 794 | // GetMembership returns membership |
Kent Hagerman | c2c73ff | 2019-11-20 16:22:32 -0500 | [diff] [blame] | 795 | func (handler *APIHandler) GetMembership(context.Context, *empty.Empty) (*voltha.Membership, error) { |
| 796 | return &voltha.Membership{}, errors.New("UnImplemented") |
| 797 | } |
| 798 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 799 | // UpdateMembership updates membership |
Kent Hagerman | c2c73ff | 2019-11-20 16:22:32 -0500 | [diff] [blame] | 800 | func (handler *APIHandler) UpdateMembership(context.Context, *voltha.Membership) (*empty.Empty, error) { |
| 801 | return &empty.Empty{}, errors.New("UnImplemented") |
| 802 | } |
kesavand | bc2d162 | 2020-01-21 00:42:01 -0500 | [diff] [blame] | 803 | |
| 804 | func (handler *APIHandler) EnablePort(ctx context.Context, port *voltha.Port) (*empty.Empty, error) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 805 | logger.Debugw("EnablePort-request", log.Fields{"device-id": port.DeviceId, "port-no": port.PortNo}) |
kesavand | bc2d162 | 2020-01-21 00:42:01 -0500 | [diff] [blame] | 806 | ch := make(chan interface{}) |
| 807 | defer close(ch) |
| 808 | go handler.deviceMgr.enablePort(ctx, port, ch) |
| 809 | return waitForNilResponseOnSuccess(ctx, ch) |
| 810 | } |
| 811 | |
| 812 | func (handler *APIHandler) DisablePort(ctx context.Context, port *voltha.Port) (*empty.Empty, error) { |
| 813 | |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 814 | logger.Debugw("DisablePort-request", log.Fields{"device-id": port.DeviceId, "port-no": port.PortNo}) |
kesavand | bc2d162 | 2020-01-21 00:42:01 -0500 | [diff] [blame] | 815 | ch := make(chan interface{}) |
| 816 | defer close(ch) |
| 817 | go handler.deviceMgr.disablePort(ctx, port, ch) |
| 818 | return waitForNilResponseOnSuccess(ctx, ch) |
| 819 | } |
onkarkundargi | 8728525 | 2020-01-27 11:34:52 +0530 | [diff] [blame] | 820 | |
| 821 | func (handler *APIHandler) StartOmciTestAction(ctx context.Context, omcitestrequest *voltha.OmciTestRequest) (*voltha.TestResponse, error) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 822 | logger.Debugw("Omci_test_Request", log.Fields{"id": omcitestrequest.Id, "uuid": omcitestrequest.Uuid}) |
onkarkundargi | 8728525 | 2020-01-27 11:34:52 +0530 | [diff] [blame] | 823 | return handler.deviceMgr.startOmciTest(ctx, omcitestrequest) |
| 824 | } |