khenaidoo | b920354 | 2018-09-17 22:56:37 -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 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 17 | package device |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 18 | |
| 19 | import ( |
| 20 | "context" |
Matteo Scandolo | 360605d | 2019-11-05 18:29:17 -0800 | [diff] [blame] | 21 | "encoding/hex" |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 22 | "errors" |
khenaidoo | 3ab3488 | 2019-05-02 21:33:30 -0400 | [diff] [blame] | 23 | "fmt" |
Mahir Gunyel | addb66a | 2020-04-29 18:08:50 -0700 | [diff] [blame] | 24 | "reflect" |
| 25 | "sync" |
| 26 | "time" |
| 27 | |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 28 | "github.com/gogo/protobuf/proto" |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 29 | "github.com/golang/protobuf/ptypes" |
dpaul | 6268631 | 2020-06-23 14:17:36 +0530 | [diff] [blame] | 30 | "github.com/golang/protobuf/ptypes/empty" |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 31 | "github.com/opencord/voltha-go/db/model" |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 32 | "github.com/opencord/voltha-go/rw_core/core/adapter" |
Mahir Gunyel | 03de0d3 | 2020-06-03 01:36:59 -0700 | [diff] [blame] | 33 | "github.com/opencord/voltha-go/rw_core/core/device/flow" |
| 34 | "github.com/opencord/voltha-go/rw_core/core/device/group" |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 35 | "github.com/opencord/voltha-go/rw_core/core/device/port" |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 36 | "github.com/opencord/voltha-go/rw_core/core/device/remote" |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 37 | "github.com/opencord/voltha-go/rw_core/core/device/transientstate" |
Scott Baker | b671a86 | 2019-10-24 10:53:40 -0700 | [diff] [blame] | 38 | coreutils "github.com/opencord/voltha-go/rw_core/utils" |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 39 | "github.com/opencord/voltha-lib-go/v4/pkg/kafka" |
Maninder | dfadc98 | 2020-10-28 14:04:33 +0530 | [diff] [blame] | 40 | "github.com/opencord/voltha-lib-go/v4/pkg/log" |
Salman Siddiqui | 1cf9504 | 2020-11-19 00:42:56 +0530 | [diff] [blame] | 41 | "github.com/opencord/voltha-protos/v4/go/extension" |
Maninder | dfadc98 | 2020-10-28 14:04:33 +0530 | [diff] [blame] | 42 | ic "github.com/opencord/voltha-protos/v4/go/inter_container" |
| 43 | ofp "github.com/opencord/voltha-protos/v4/go/openflow_13" |
| 44 | "github.com/opencord/voltha-protos/v4/go/voltha" |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 45 | "google.golang.org/grpc/codes" |
| 46 | "google.golang.org/grpc/status" |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 47 | ) |
| 48 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 49 | // Agent represents device agent attributes |
| 50 | type Agent struct { |
Kent Hagerman | f5a6735 | 2020-04-30 15:15:26 -0400 | [diff] [blame] | 51 | deviceID string |
| 52 | parentID string |
| 53 | deviceType string |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 54 | isRootDevice bool |
Kent Hagerman | f5a6735 | 2020-04-30 15:15:26 -0400 | [diff] [blame] | 55 | adapterProxy *remote.AdapterProxy |
| 56 | adapterMgr *adapter.Manager |
| 57 | deviceMgr *Manager |
| 58 | dbProxy *model.Proxy |
| 59 | exitChannel chan int |
| 60 | device *voltha.Device |
| 61 | requestQueue *coreutils.RequestQueue |
| 62 | defaultTimeout time.Duration |
| 63 | startOnce sync.Once |
| 64 | stopOnce sync.Once |
| 65 | stopped bool |
Mahir Gunyel | 03de0d3 | 2020-06-03 01:36:59 -0700 | [diff] [blame] | 66 | |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 67 | flowLoader *flow.Loader |
| 68 | groupLoader *group.Loader |
| 69 | portLoader *port.Loader |
| 70 | transientStateLoader *transientstate.Loader |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 71 | } |
| 72 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 73 | //newAgent creates a new device agent. The device will be initialized when start() is called. |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 74 | func newAgent(ap *remote.AdapterProxy, device *voltha.Device, deviceMgr *Manager, dbPath *model.Path, deviceProxy *model.Proxy, timeout time.Duration) *Agent { |
| 75 | deviceID := device.Id |
| 76 | if deviceID == "" { |
| 77 | deviceID = coreutils.CreateDeviceID() |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 78 | } |
Scott Baker | 8067860 | 2019-11-14 16:57:36 -0800 | [diff] [blame] | 79 | |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 80 | return &Agent{ |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 81 | deviceID: deviceID, |
| 82 | adapterProxy: ap, |
| 83 | isRootDevice: device.Root, |
| 84 | parentID: device.ParentId, |
| 85 | deviceType: device.Type, |
| 86 | deviceMgr: deviceMgr, |
| 87 | adapterMgr: deviceMgr.adapterMgr, |
| 88 | exitChannel: make(chan int, 1), |
| 89 | dbProxy: deviceProxy, |
| 90 | defaultTimeout: timeout, |
| 91 | device: proto.Clone(device).(*voltha.Device), |
| 92 | requestQueue: coreutils.NewRequestQueue(), |
| 93 | flowLoader: flow.NewLoader(dbPath.SubPath("flows").Proxy(deviceID)), |
| 94 | groupLoader: group.NewLoader(dbPath.SubPath("groups").Proxy(deviceID)), |
| 95 | portLoader: port.NewLoader(dbPath.SubPath("ports").Proxy(deviceID)), |
| 96 | transientStateLoader: transientstate.NewLoader(dbPath.SubPath("core").Proxy("transientstate"), deviceID), |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 97 | } |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 98 | } |
| 99 | |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 100 | // start() saves the device to the data model and registers for callbacks on that device if deviceToCreate!=nil. |
| 101 | // Otherwise, it will load the data from the dB and setup the necessary callbacks and proxies. Returns the device that |
Scott Baker | 8067860 | 2019-11-14 16:57:36 -0800 | [diff] [blame] | 102 | // was started. |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 103 | func (agent *Agent) start(ctx context.Context, deviceToCreate *voltha.Device) (*voltha.Device, error) { |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 104 | needToStart := false |
| 105 | if agent.startOnce.Do(func() { needToStart = true }); !needToStart { |
Kent Hagerman | cba2f30 | 2020-07-28 13:37:36 -0400 | [diff] [blame] | 106 | return agent.getDeviceReadOnly(ctx) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 107 | } |
| 108 | var startSucceeded bool |
| 109 | defer func() { |
| 110 | if !startSucceeded { |
| 111 | if err := agent.stop(ctx); err != nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 112 | logger.Errorw(ctx, "failed-to-cleanup-after-unsuccessful-start", log.Fields{"device-id": agent.deviceID, "error": err}) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 113 | } |
| 114 | } |
| 115 | }() |
Scott Baker | 8067860 | 2019-11-14 16:57:36 -0800 | [diff] [blame] | 116 | |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 117 | var device *voltha.Device |
Scott Baker | 8067860 | 2019-11-14 16:57:36 -0800 | [diff] [blame] | 118 | if deviceToCreate == nil { |
| 119 | // Load the existing device |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 120 | device := &voltha.Device{} |
Kent Hagerman | f5a6735 | 2020-04-30 15:15:26 -0400 | [diff] [blame] | 121 | have, err := agent.dbProxy.Get(ctx, agent.deviceID, device) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 122 | if err != nil { |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 123 | return nil, err |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 124 | } else if !have { |
| 125 | return nil, status.Errorf(codes.NotFound, "device-%s", agent.deviceID) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 126 | } |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 127 | |
| 128 | agent.deviceType = device.Adapter |
| 129 | agent.device = proto.Clone(device).(*voltha.Device) |
Mahir Gunyel | 03de0d3 | 2020-06-03 01:36:59 -0700 | [diff] [blame] | 130 | // load the flows and groups from KV to cache |
| 131 | agent.flowLoader.Load(ctx) |
| 132 | agent.groupLoader.Load(ctx) |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 133 | agent.portLoader.Load(ctx) |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 134 | agent.transientStateLoader.Load(ctx) |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 135 | |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame^] | 136 | logger.Infow(ctx, "device-loaded-from-db", log.Fields{"device-id": agent.deviceID}) |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 137 | } else { |
Scott Baker | 8067860 | 2019-11-14 16:57:36 -0800 | [diff] [blame] | 138 | // Create a new device |
Kent Hagerman | f5a6735 | 2020-04-30 15:15:26 -0400 | [diff] [blame] | 139 | // Assumption is that AdminState, FlowGroups, and Flows are uninitialized since this |
| 140 | // is a new device, so populate them here before passing the device to ldProxy.Set. |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 141 | // agent.deviceId will also have been set during newAgent(). |
Scott Baker | 8067860 | 2019-11-14 16:57:36 -0800 | [diff] [blame] | 142 | device = (proto.Clone(deviceToCreate)).(*voltha.Device) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 143 | device.Id = agent.deviceID |
Scott Baker | 8067860 | 2019-11-14 16:57:36 -0800 | [diff] [blame] | 144 | device.AdminState = voltha.AdminState_PREPROVISIONED |
Scott Baker | 8067860 | 2019-11-14 16:57:36 -0800 | [diff] [blame] | 145 | if !deviceToCreate.GetRoot() && deviceToCreate.ProxyAddress != nil { |
| 146 | // Set the default vlan ID to the one specified by the parent adapter. It can be |
| 147 | // overwritten by the child adapter during a device update request |
| 148 | device.Vlan = deviceToCreate.ProxyAddress.ChannelId |
| 149 | } |
| 150 | |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 151 | // Add the initial device to the local model |
Kent Hagerman | f5a6735 | 2020-04-30 15:15:26 -0400 | [diff] [blame] | 152 | if err := agent.dbProxy.Set(ctx, agent.deviceID, device); err != nil { |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 153 | return nil, status.Errorf(codes.Aborted, "failed-adding-device-%s: %s", agent.deviceID, err) |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 154 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 155 | agent.device = device |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 156 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 157 | startSucceeded = true |
Rohan Agrawal | cf12f20 | 2020-08-03 04:42:01 +0000 | [diff] [blame] | 158 | log.EnrichSpan(ctx, log.Fields{"device-id": agent.deviceID}) |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 159 | logger.Debugw(ctx, "device-agent-started", log.Fields{"device-id": agent.deviceID}) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 160 | |
Kent Hagerman | cba2f30 | 2020-07-28 13:37:36 -0400 | [diff] [blame] | 161 | return agent.getDeviceReadOnly(ctx) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 162 | } |
| 163 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 164 | // stop stops the device agent. Not much to do for now |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 165 | func (agent *Agent) stop(ctx context.Context) error { |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 166 | needToStop := false |
| 167 | if agent.stopOnce.Do(func() { needToStop = true }); !needToStop { |
| 168 | return nil |
| 169 | } |
| 170 | if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
| 171 | return err |
| 172 | } |
| 173 | defer agent.requestQueue.RequestComplete() |
khenaidoo | 4908535 | 2020-01-13 19:15:43 -0500 | [diff] [blame] | 174 | |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame^] | 175 | logger.Infow(ctx, "stopping-device-agent", log.Fields{"device-id": agent.deviceID, "parent-id": agent.parentID}) |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 176 | // Remove the device transient loader |
| 177 | if err := agent.deleteTransientState(ctx); err != nil { |
| 178 | return err |
| 179 | } |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 180 | // Remove the device from the KV store |
Kent Hagerman | f5a6735 | 2020-04-30 15:15:26 -0400 | [diff] [blame] | 181 | if err := agent.dbProxy.Remove(ctx, agent.deviceID); err != nil { |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 182 | return err |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 183 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 184 | |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 185 | close(agent.exitChannel) |
| 186 | |
| 187 | agent.stopped = true |
| 188 | |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 189 | logger.Infow(ctx, "device-agent-stopped", log.Fields{"device-id": agent.deviceID, "parent-id": agent.parentID}) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 190 | |
| 191 | return nil |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 192 | } |
| 193 | |
Scott Baker | 8067860 | 2019-11-14 16:57:36 -0800 | [diff] [blame] | 194 | // Load the most recent state from the KVStore for the device. |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 195 | func (agent *Agent) reconcileWithKVStore(ctx context.Context) { |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 196 | if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 197 | logger.Warnw(ctx, "request-aborted", log.Fields{"device-id": agent.deviceID, "error": err}) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 198 | return |
| 199 | } |
| 200 | defer agent.requestQueue.RequestComplete() |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 201 | logger.Debug(ctx, "reconciling-device-agent-devicetype") |
Scott Baker | 8067860 | 2019-11-14 16:57:36 -0800 | [diff] [blame] | 202 | // TODO: context timeout |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 203 | device := &voltha.Device{} |
Kent Hagerman | f5a6735 | 2020-04-30 15:15:26 -0400 | [diff] [blame] | 204 | if have, err := agent.dbProxy.Get(ctx, agent.deviceID, device); err != nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 205 | logger.Errorw(ctx, "kv-get-failed", log.Fields{"device-id": agent.deviceID, "error": err}) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 206 | return |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 207 | } else if !have { |
| 208 | return // not found in kv |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 209 | } |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 210 | |
| 211 | agent.deviceType = device.Adapter |
| 212 | agent.device = device |
Mahir Gunyel | 03de0d3 | 2020-06-03 01:36:59 -0700 | [diff] [blame] | 213 | agent.flowLoader.Load(ctx) |
| 214 | agent.groupLoader.Load(ctx) |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 215 | agent.portLoader.Load(ctx) |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 216 | agent.transientStateLoader.Load(ctx) |
| 217 | |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 218 | logger.Debugw(ctx, "reconciled-device-agent-devicetype", log.Fields{"device-id": agent.deviceID, "type": agent.deviceType}) |
Scott Baker | 8067860 | 2019-11-14 16:57:36 -0800 | [diff] [blame] | 219 | } |
| 220 | |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 221 | // onSuccess is a common callback for scenarios where we receive a nil response following a request to an adapter |
| 222 | // and the only action required is to publish a successful result on kafka |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 223 | func (agent *Agent) onSuccess(ctx context.Context, rpc string, response interface{}, reqArgs ...interface{}) { |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 224 | logger.Debugw(ctx, "response-successful", log.Fields{"rpc": rpc, "device-id": agent.deviceID}) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 225 | // TODO: Post success message onto kafka |
| 226 | } |
| 227 | |
| 228 | // onFailure is a common callback for scenarios where we receive an error response following a request to an adapter |
| 229 | // and the only action required is to publish the failed result on kafka |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 230 | func (agent *Agent) onFailure(ctx context.Context, rpc string, response interface{}, reqArgs ...interface{}) { |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 231 | if res, ok := response.(error); ok { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 232 | logger.Errorw(ctx, "rpc-failed", log.Fields{"rpc": rpc, "device-id": agent.deviceID, "error": res, "args": reqArgs}) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 233 | } else { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 234 | logger.Errorw(ctx, "rpc-failed-invalid-error", log.Fields{"rpc": rpc, "device-id": agent.deviceID, "args": reqArgs}) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 235 | } |
| 236 | // TODO: Post failure message onto kafka |
| 237 | } |
| 238 | |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame^] | 239 | func (agent *Agent) waitForAdapterForceDeleteResponse(ctx context.Context, cancel context.CancelFunc, rpc string, ch chan *kafka.RpcResponse, |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 240 | onSuccess coreutils.ResponseCallback, onFailure coreutils.ResponseCallback, reqArgs ...interface{}) { |
| 241 | defer cancel() |
| 242 | select { |
| 243 | case rpcResponse, ok := <-ch: |
| 244 | if !ok { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 245 | onFailure(ctx, rpc, status.Errorf(codes.Aborted, "channel-closed"), reqArgs) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 246 | } else if rpcResponse.Err != nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 247 | onFailure(ctx, rpc, rpcResponse.Err, reqArgs) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 248 | } else { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 249 | onSuccess(ctx, rpc, rpcResponse.Reply, reqArgs) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 250 | } |
| 251 | case <-ctx.Done(): |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 252 | onFailure(ctx, rpc, ctx.Err(), reqArgs) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 253 | } |
| 254 | } |
| 255 | |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 256 | // onDeleteSuccess is a common callback for scenarios where we receive a nil response following a delete request |
| 257 | // to an adapter. |
| 258 | func (agent *Agent) onDeleteSuccess(ctx context.Context, rpc string, response interface{}, reqArgs ...interface{}) { |
| 259 | logger.Debugw(ctx, "response-successful", log.Fields{"rpc": rpc, "device-id": agent.deviceID}) |
| 260 | if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
| 261 | logger.Errorw(ctx, "delete-device-failure", log.Fields{"device-id": agent.deviceID, "error": err, "args": reqArgs}) |
| 262 | } |
| 263 | previousDeviceTransientState := agent.getTransientState() |
| 264 | newDevice := agent.cloneDeviceWithoutLock() |
| 265 | if err := agent.updateDeviceWithTransientStateAndReleaseLock(ctx, newDevice, |
| 266 | voltha.DeviceTransientState_DELETING_POST_ADAPTER_RESPONSE, previousDeviceTransientState); err != nil { |
| 267 | logger.Errorw(ctx, "delete-device-failure", log.Fields{"device-id": agent.deviceID, "error": err, "args": reqArgs}) |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | // onDeleteFailure is a common callback for scenarios where we receive an error response following a delete request |
| 272 | // to an adapter and the only action required is to return the error response. |
| 273 | func (agent *Agent) onDeleteFailure(ctx context.Context, rpc string, response interface{}, reqArgs ...interface{}) { |
| 274 | if res, ok := response.(error); ok { |
| 275 | logger.Errorw(ctx, "rpc-failed", log.Fields{"rpc": rpc, "device-id": agent.deviceID, "error": res, "args": reqArgs}) |
| 276 | } else { |
| 277 | logger.Errorw(ctx, "rpc-failed-invalid-error", log.Fields{"rpc": rpc, "device-id": agent.deviceID, "args": reqArgs}) |
| 278 | } |
| 279 | //Only updating of transient state is required, no transition. |
| 280 | if err := agent.updateTransientState(ctx, voltha.DeviceTransientState_DELETE_FAILED); err != nil { |
| 281 | logger.Errorw(ctx, "failed-to-update-transient-state-as-delete-failed", log.Fields{"device-id": agent.deviceID}) |
| 282 | } |
| 283 | |
| 284 | } |
| 285 | |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame^] | 286 | func (agent *Agent) waitForAdapterResponse(ctx context.Context, cancel context.CancelFunc, rpc string, ch chan *kafka.RpcResponse, |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 287 | onSuccess coreutils.ResponseCallback, onFailure coreutils.ResponseCallback, reqArgs ...interface{}) { |
| 288 | defer cancel() |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame^] | 289 | var rpce *voltha.RPCEvent |
| 290 | defer func() { |
| 291 | if rpce != nil { |
| 292 | go agent.deviceMgr.SendRPCEvent(ctx, "RPC_ERROR_RAISE_EVENT", rpce, |
| 293 | voltha.EventCategory_COMMUNICATION, nil, time.Now().UnixNano()) |
| 294 | } |
| 295 | }() |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 296 | select { |
| 297 | case rpcResponse, ok := <-ch: |
| 298 | if !ok { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame^] | 299 | rpce = agent.deviceMgr.NewRPCEvent(ctx, agent.deviceID, "Response Channel Closed", nil) |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 300 | onFailure(ctx, rpc, status.Errorf(codes.Aborted, "channel-closed"), reqArgs) |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame^] | 301 | //add failure |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 302 | } else if rpcResponse.Err != nil { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame^] | 303 | rpce = agent.deviceMgr.NewRPCEvent(ctx, agent.deviceID, rpcResponse.Err.Error(), nil) |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 304 | onFailure(ctx, rpc, rpcResponse.Err, reqArgs) |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame^] | 305 | //add failure |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 306 | } else { |
| 307 | onSuccess(ctx, rpc, rpcResponse.Reply, reqArgs) |
| 308 | } |
| 309 | case <-ctx.Done(): |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame^] | 310 | rpce = agent.deviceMgr.NewRPCEvent(ctx, agent.deviceID, ctx.Err().Error(), nil) |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 311 | onFailure(ctx, rpc, ctx.Err(), reqArgs) |
| 312 | } |
| 313 | } |
| 314 | |
Kent Hagerman | cba2f30 | 2020-07-28 13:37:36 -0400 | [diff] [blame] | 315 | // getDeviceReadOnly returns a device which MUST NOT be modified, but is safe to keep forever. |
| 316 | func (agent *Agent) getDeviceReadOnly(ctx context.Context) (*voltha.Device, error) { |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 317 | if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
| 318 | return nil, err |
| 319 | } |
| 320 | defer agent.requestQueue.RequestComplete() |
Kent Hagerman | cba2f30 | 2020-07-28 13:37:36 -0400 | [diff] [blame] | 321 | return agent.device, nil |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 322 | } |
| 323 | |
Kent Hagerman | cba2f30 | 2020-07-28 13:37:36 -0400 | [diff] [blame] | 324 | // getDeviceReadOnlyWithoutLock returns a device which MUST NOT be modified, but is safe to keep forever. This is very efficient. |
Kent Hagerman | f6db9f1 | 2020-07-22 17:16:19 -0400 | [diff] [blame] | 325 | // The device lock MUST be held by the caller. |
Kent Hagerman | cba2f30 | 2020-07-28 13:37:36 -0400 | [diff] [blame] | 326 | func (agent *Agent) getDeviceReadOnlyWithoutLock() *voltha.Device { |
khenaidoo | 0db4c81 | 2020-05-27 15:27:30 -0400 | [diff] [blame] | 327 | return agent.device |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 328 | } |
| 329 | |
Kent Hagerman | f6db9f1 | 2020-07-22 17:16:19 -0400 | [diff] [blame] | 330 | // cloneDeviceWithoutLock returns a copy of the device which is safe to modify. |
| 331 | // The device lock MUST be held by the caller. |
| 332 | func (agent *Agent) cloneDeviceWithoutLock() *voltha.Device { |
| 333 | return proto.Clone(agent.device).(*voltha.Device) |
| 334 | } |
| 335 | |
khenaidoo | 3ab3488 | 2019-05-02 21:33:30 -0400 | [diff] [blame] | 336 | // enableDevice activates a preprovisioned or a disable device |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 337 | func (agent *Agent) enableDevice(ctx context.Context) error { |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 338 | if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
| 339 | return err |
| 340 | } |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame^] | 341 | logger.Debugw(ctx, "enable-device", log.Fields{"device-id": agent.deviceID}) |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 342 | |
Kent Hagerman | cba2f30 | 2020-07-28 13:37:36 -0400 | [diff] [blame] | 343 | oldDevice := agent.getDeviceReadOnlyWithoutLock() |
Kent Hagerman | f6db9f1 | 2020-07-22 17:16:19 -0400 | [diff] [blame] | 344 | if oldDevice.AdminState == voltha.AdminState_ENABLED { |
| 345 | logger.Warnw(ctx, "device-already-enabled", log.Fields{"device-id": agent.deviceID}) |
| 346 | agent.requestQueue.RequestComplete() |
| 347 | return status.Error(codes.FailedPrecondition, fmt.Sprintf("cannot-enable-an-already-enabled-device: %s", oldDevice.Id)) |
| 348 | } |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 349 | if agent.isDeletionInProgress() { |
Kent Hagerman | f6db9f1 | 2020-07-22 17:16:19 -0400 | [diff] [blame] | 350 | agent.requestQueue.RequestComplete() |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 351 | return status.Errorf(codes.FailedPrecondition, "deviceId:%s, Device deletion is in progress.", agent.deviceID) |
Kent Hagerman | f6db9f1 | 2020-07-22 17:16:19 -0400 | [diff] [blame] | 352 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 353 | // First figure out which adapter will handle this device type. We do it at this stage as allow devices to be |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 354 | // pre-provisioned with the required adapter not registered. At this stage, since we need to communicate |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 355 | // with the adapter then we need to know the adapter that will handle this request |
Kent Hagerman | f6db9f1 | 2020-07-22 17:16:19 -0400 | [diff] [blame] | 356 | adapterName, err := agent.adapterMgr.GetAdapterType(oldDevice.Type) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 357 | if err != nil { |
Kent Hagerman | f6db9f1 | 2020-07-22 17:16:19 -0400 | [diff] [blame] | 358 | agent.requestQueue.RequestComplete() |
Matteo Scandolo | d525ae3 | 2020-04-02 17:27:29 -0700 | [diff] [blame] | 359 | return err |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 360 | } |
| 361 | |
Kent Hagerman | f6db9f1 | 2020-07-22 17:16:19 -0400 | [diff] [blame] | 362 | newDevice := agent.cloneDeviceWithoutLock() |
| 363 | newDevice.Adapter = adapterName |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 364 | |
Kent Hagerman | f6db9f1 | 2020-07-22 17:16:19 -0400 | [diff] [blame] | 365 | // Update the Admin State and set the operational state to activating before sending the request to the Adapters |
| 366 | newDevice.AdminState = voltha.AdminState_ENABLED |
| 367 | newDevice.OperStatus = voltha.OperStatus_ACTIVATING |
| 368 | if err := agent.updateDeviceAndReleaseLock(ctx, newDevice); err != nil { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 369 | return err |
| 370 | } |
| 371 | |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 372 | // Adopt the device if it was in pre-provision state. In all other cases, try to re-enable it. |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 373 | var ch chan *kafka.RpcResponse |
Rohan Agrawal | cf12f20 | 2020-08-03 04:42:01 +0000 | [diff] [blame] | 374 | subCtx, cancel := context.WithTimeout(log.WithSpanFromContext(context.Background(), ctx), agent.defaultTimeout) |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame^] | 375 | subCtx = coreutils.WithRPCMetadataFromContext(subCtx, ctx) |
| 376 | |
Kent Hagerman | f6db9f1 | 2020-07-22 17:16:19 -0400 | [diff] [blame] | 377 | if oldDevice.AdminState == voltha.AdminState_PREPROVISIONED { |
| 378 | ch, err = agent.adapterProxy.AdoptDevice(subCtx, newDevice) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 379 | } else { |
Kent Hagerman | f6db9f1 | 2020-07-22 17:16:19 -0400 | [diff] [blame] | 380 | ch, err = agent.adapterProxy.ReEnableDevice(subCtx, newDevice) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 381 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 382 | if err != nil { |
| 383 | cancel() |
| 384 | return err |
| 385 | } |
| 386 | // Wait for response |
| 387 | go agent.waitForAdapterResponse(subCtx, cancel, "enableDevice", ch, agent.onSuccess, agent.onFailure) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 388 | return nil |
| 389 | } |
| 390 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 391 | func (agent *Agent) waitForAdapterFlowResponse(ctx context.Context, cancel context.CancelFunc, ch chan *kafka.RpcResponse, response coreutils.Response) { |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 392 | defer cancel() |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame^] | 393 | var rpce *voltha.RPCEvent |
| 394 | defer func() { |
| 395 | if rpce != nil { |
| 396 | go agent.deviceMgr.SendRPCEvent(ctx, "RPC_ERROR_RAISE_EVENT", rpce, |
| 397 | voltha.EventCategory_COMMUNICATION, nil, time.Now().UnixNano()) |
| 398 | } |
| 399 | }() |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 400 | select { |
| 401 | case rpcResponse, ok := <-ch: |
| 402 | if !ok { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame^] | 403 | //add failure |
| 404 | rpce = agent.deviceMgr.NewRPCEvent(ctx, agent.deviceID, "Response Channel Closed", nil) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 405 | response.Error(status.Errorf(codes.Aborted, "channel-closed")) |
| 406 | } else if rpcResponse.Err != nil { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame^] | 407 | //add failure |
| 408 | rpce = agent.deviceMgr.NewRPCEvent(ctx, agent.deviceID, rpcResponse.Err.Error(), nil) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 409 | response.Error(rpcResponse.Err) |
| 410 | } else { |
| 411 | response.Done() |
| 412 | } |
| 413 | case <-ctx.Done(): |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame^] | 414 | rpce = agent.deviceMgr.NewRPCEvent(ctx, agent.deviceID, ctx.Err().Error(), nil) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 415 | response.Error(ctx.Err()) |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 416 | } |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 417 | } |
| 418 | |
A R Karthick | 5c28f55 | 2019-12-11 22:47:44 -0800 | [diff] [blame] | 419 | //addFlowsAndGroups adds the "newFlows" and "newGroups" from the existing flows/groups and sends the update to the |
| 420 | //adapters |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 421 | func (agent *Agent) addFlowsAndGroups(ctx context.Context, newFlows []*ofp.OfpFlowStats, newGroups []*ofp.OfpGroupEntry, flowMetadata *voltha.FlowMetadata) error { |
Mahir Gunyel | 03de0d3 | 2020-06-03 01:36:59 -0700 | [diff] [blame] | 422 | var flwResponse, grpResponse coreutils.Response |
| 423 | var err error |
| 424 | //if new flow list is empty then the called function returns quickly |
| 425 | if flwResponse, err = agent.addFlowsToAdapter(ctx, newFlows, flowMetadata); err != nil { |
A R Karthick | 5c28f55 | 2019-12-11 22:47:44 -0800 | [diff] [blame] | 426 | return err |
| 427 | } |
Mahir Gunyel | 03de0d3 | 2020-06-03 01:36:59 -0700 | [diff] [blame] | 428 | //if new group list is empty then the called function returns quickly |
| 429 | if grpResponse, err = agent.addGroupsToAdapter(ctx, newGroups, flowMetadata); err != nil { |
| 430 | return err |
| 431 | } |
| 432 | if errs := coreutils.WaitForNilOrErrorResponses(agent.defaultTimeout, flwResponse, grpResponse); errs != nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 433 | logger.Warnw(ctx, "no-adapter-response", log.Fields{"device-id": agent.deviceID, "result": errs}) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 434 | return status.Errorf(codes.Aborted, "flow-failure-device-%s", agent.deviceID) |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 435 | } |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 436 | return nil |
| 437 | } |
| 438 | |
A R Karthick | 5c28f55 | 2019-12-11 22:47:44 -0800 | [diff] [blame] | 439 | //deleteFlowsAndGroups removes the "flowsToDel" and "groupsToDel" from the existing flows/groups and sends the update to the |
| 440 | //adapters |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 441 | func (agent *Agent) deleteFlowsAndGroups(ctx context.Context, flowsToDel []*ofp.OfpFlowStats, groupsToDel []*ofp.OfpGroupEntry, flowMetadata *voltha.FlowMetadata) error { |
Mahir Gunyel | 03de0d3 | 2020-06-03 01:36:59 -0700 | [diff] [blame] | 442 | var flwResponse, grpResponse coreutils.Response |
| 443 | var err error |
| 444 | if flwResponse, err = agent.deleteFlowsFromAdapter(ctx, flowsToDel, flowMetadata); err != nil { |
A R Karthick | 5c28f55 | 2019-12-11 22:47:44 -0800 | [diff] [blame] | 445 | return err |
| 446 | } |
Mahir Gunyel | 03de0d3 | 2020-06-03 01:36:59 -0700 | [diff] [blame] | 447 | if grpResponse, err = agent.deleteGroupsFromAdapter(ctx, groupsToDel, flowMetadata); err != nil { |
| 448 | return err |
| 449 | } |
| 450 | |
| 451 | if res := coreutils.WaitForNilOrErrorResponses(agent.defaultTimeout, flwResponse, grpResponse); res != nil { |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 452 | return status.Errorf(codes.Aborted, "errors-%s", res) |
| 453 | } |
| 454 | return nil |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 455 | } |
| 456 | |
A R Karthick | 5c28f55 | 2019-12-11 22:47:44 -0800 | [diff] [blame] | 457 | //updateFlowsAndGroups replaces the existing flows and groups with "updatedFlows" and "updatedGroups" respectively. It |
| 458 | //also sends the updates to the adapters |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 459 | func (agent *Agent) updateFlowsAndGroups(ctx context.Context, updatedFlows []*ofp.OfpFlowStats, updatedGroups []*ofp.OfpGroupEntry, flowMetadata *voltha.FlowMetadata) error { |
Mahir Gunyel | 03de0d3 | 2020-06-03 01:36:59 -0700 | [diff] [blame] | 460 | var flwResponse, grpResponse coreutils.Response |
| 461 | var err error |
| 462 | if flwResponse, err = agent.updateFlowsToAdapter(ctx, updatedFlows, flowMetadata); err != nil { |
A R Karthick | 5c28f55 | 2019-12-11 22:47:44 -0800 | [diff] [blame] | 463 | return err |
| 464 | } |
Mahir Gunyel | 03de0d3 | 2020-06-03 01:36:59 -0700 | [diff] [blame] | 465 | if grpResponse, err = agent.updateGroupsToAdapter(ctx, updatedGroups, flowMetadata); err != nil { |
| 466 | return err |
| 467 | } |
| 468 | |
| 469 | if res := coreutils.WaitForNilOrErrorResponses(agent.defaultTimeout, flwResponse, grpResponse); res != nil { |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 470 | return status.Errorf(codes.Aborted, "errors-%s", res) |
| 471 | } |
| 472 | return nil |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 473 | } |
| 474 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 475 | //disableDevice disable a device |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 476 | func (agent *Agent) disableDevice(ctx context.Context) error { |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 477 | if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
| 478 | return err |
| 479 | } |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame^] | 480 | logger.Debugw(ctx, "disable-device", log.Fields{"device-id": agent.deviceID}) |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 481 | |
Kent Hagerman | f6db9f1 | 2020-07-22 17:16:19 -0400 | [diff] [blame] | 482 | cloned := agent.cloneDeviceWithoutLock() |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 483 | |
| 484 | if cloned.AdminState == voltha.AdminState_DISABLED { |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 485 | logger.Debugw(ctx, "device-already-disabled", log.Fields{"device-id": agent.deviceID}) |
Kent Hagerman | f6db9f1 | 2020-07-22 17:16:19 -0400 | [diff] [blame] | 486 | agent.requestQueue.RequestComplete() |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 487 | return nil |
| 488 | } |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 489 | if cloned.AdminState == voltha.AdminState_PREPROVISIONED { |
Kent Hagerman | f6db9f1 | 2020-07-22 17:16:19 -0400 | [diff] [blame] | 490 | agent.requestQueue.RequestComplete() |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 491 | return status.Errorf(codes.FailedPrecondition, "deviceId:%s, invalid-admin-state:%s", agent.deviceID, cloned.AdminState) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 492 | } |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 493 | if agent.isDeletionInProgress() { |
| 494 | agent.requestQueue.RequestComplete() |
| 495 | return status.Errorf(codes.FailedPrecondition, "deviceId:%s, Device deletion is in progress.", agent.deviceID) |
| 496 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 497 | // Update the Admin State and operational state before sending the request out |
Kent Hagerman | f6db9f1 | 2020-07-22 17:16:19 -0400 | [diff] [blame] | 498 | cloned.AdminState = voltha.AdminState_DISABLED |
| 499 | cloned.OperStatus = voltha.OperStatus_UNKNOWN |
| 500 | if err := agent.updateDeviceAndReleaseLock(ctx, cloned); err != nil { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 501 | return err |
| 502 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 503 | |
Rohan Agrawal | cf12f20 | 2020-08-03 04:42:01 +0000 | [diff] [blame] | 504 | subCtx, cancel := context.WithTimeout(log.WithSpanFromContext(context.Background(), ctx), agent.defaultTimeout) |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame^] | 505 | subCtx = coreutils.WithRPCMetadataFromContext(subCtx, ctx) |
| 506 | |
Kent Hagerman | f6db9f1 | 2020-07-22 17:16:19 -0400 | [diff] [blame] | 507 | ch, err := agent.adapterProxy.DisableDevice(subCtx, cloned) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 508 | if err != nil { |
| 509 | cancel() |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 510 | return err |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 511 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 512 | go agent.waitForAdapterResponse(subCtx, cancel, "disableDevice", ch, agent.onSuccess, agent.onFailure) |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 513 | |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 514 | return nil |
| 515 | } |
| 516 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 517 | func (agent *Agent) rebootDevice(ctx context.Context) error { |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 518 | if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 519 | return err |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 520 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 521 | defer agent.requestQueue.RequestComplete() |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame^] | 522 | logger.Debugw(ctx, "reboot-device", log.Fields{"device-id": agent.deviceID}) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 523 | |
Kent Hagerman | cba2f30 | 2020-07-28 13:37:36 -0400 | [diff] [blame] | 524 | device := agent.getDeviceReadOnlyWithoutLock() |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 525 | if agent.isDeletionInProgress() { |
| 526 | return status.Errorf(codes.FailedPrecondition, "deviceId:%s, Device deletion is in progress.", agent.deviceID) |
| 527 | } |
Rohan Agrawal | cf12f20 | 2020-08-03 04:42:01 +0000 | [diff] [blame] | 528 | subCtx, cancel := context.WithTimeout(log.WithSpanFromContext(context.Background(), ctx), agent.defaultTimeout) |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame^] | 529 | subCtx = coreutils.WithRPCMetadataFromContext(subCtx, ctx) |
| 530 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 531 | ch, err := agent.adapterProxy.RebootDevice(subCtx, device) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 532 | if err != nil { |
| 533 | cancel() |
| 534 | return err |
| 535 | } |
| 536 | go agent.waitForAdapterResponse(subCtx, cancel, "rebootDevice", ch, agent.onSuccess, agent.onFailure) |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 537 | return nil |
| 538 | } |
| 539 | |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 540 | func (agent *Agent) deleteDeviceForce(ctx context.Context) error { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame^] | 541 | logger.Debugw(ctx, "delete-device-force", log.Fields{"device-id": agent.deviceID}) |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 542 | if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
| 543 | return err |
| 544 | } |
| 545 | // Get the device Transient state, return err if it is DELETING |
| 546 | previousDeviceTransientState := agent.getTransientState() |
| 547 | |
| 548 | if agent.isStateDeleting(previousDeviceTransientState) { |
| 549 | agent.requestQueue.RequestComplete() |
| 550 | return status.Errorf(codes.FailedPrecondition, "deviceId:%s, Device Deletion is in progress", |
| 551 | agent.deviceID) |
| 552 | } |
| 553 | device := agent.cloneDeviceWithoutLock() |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame^] | 554 | if err := agent.updateDeviceWithTransientStateAndReleaseLock(ctx, device, |
| 555 | voltha.DeviceTransientState_FORCE_DELETING, previousDeviceTransientState); err != nil { |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 556 | return err |
| 557 | } |
| 558 | previousAdminState := device.AdminState |
| 559 | if previousAdminState != ic.AdminState_PREPROVISIONED { |
| 560 | subCtx, cancel := context.WithTimeout(log.WithSpanFromContext(context.Background(), ctx), agent.defaultTimeout) |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame^] | 561 | subCtx = coreutils.WithRPCMetadataFromContext(subCtx, ctx) |
| 562 | |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 563 | ch, err := agent.adapterProxy.DeleteDevice(subCtx, device) |
| 564 | if err != nil { |
| 565 | cancel() |
| 566 | return err |
| 567 | } |
| 568 | // Since it is a case of force delete, nothing needs to be done on adapter responses. |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame^] | 569 | go agent.waitForAdapterForceDeleteResponse(subCtx, cancel, "deleteDeviceForce", ch, agent.onSuccess, |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 570 | agent.onFailure) |
| 571 | } |
| 572 | return nil |
| 573 | } |
| 574 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 575 | func (agent *Agent) deleteDevice(ctx context.Context) error { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame^] | 576 | logger.Debugw(ctx, "delete-device", log.Fields{"device-id": agent.deviceID}) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 577 | if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
| 578 | return err |
| 579 | } |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 580 | // Get the device Transient state, return err if it is DELETING |
| 581 | previousDeviceTransientState := agent.getTransientState() |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 582 | |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 583 | if agent.isStateDeleting(previousDeviceTransientState) { |
| 584 | agent.requestQueue.RequestComplete() |
| 585 | return status.Errorf(codes.FailedPrecondition, "deviceId:%s, Device Deletion is in progress", agent.deviceID) |
| 586 | } |
| 587 | device := agent.cloneDeviceWithoutLock() |
| 588 | previousAdminState := device.AdminState |
| 589 | // Change the device transient state to DELETING_FROM_ADAPTER state till the device is removed from adapters. |
| 590 | currentDeviceTransientState := voltha.DeviceTransientState_DELETING_FROM_ADAPTER |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 591 | |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 592 | if previousAdminState == ic.AdminState_PREPROVISIONED { |
| 593 | // Change the state to DELETING POST ADAPTER RESPONSE directly as adapters have no info of the device. |
| 594 | currentDeviceTransientState = voltha.DeviceTransientState_DELETING_POST_ADAPTER_RESPONSE |
| 595 | } |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame^] | 596 | if err := agent.updateDeviceWithTransientStateAndReleaseLock(ctx, device, |
| 597 | currentDeviceTransientState, previousDeviceTransientState); err != nil { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 598 | return err |
| 599 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 600 | // If the device was in pre-prov state (only parent device are in that state) then do not send the request to the |
| 601 | // adapter |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 602 | if previousAdminState != ic.AdminState_PREPROVISIONED { |
Rohan Agrawal | cf12f20 | 2020-08-03 04:42:01 +0000 | [diff] [blame] | 603 | subCtx, cancel := context.WithTimeout(log.WithSpanFromContext(context.Background(), ctx), agent.defaultTimeout) |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame^] | 604 | subCtx = coreutils.WithRPCMetadataFromContext(subCtx, ctx) |
| 605 | |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 606 | ch, err := agent.adapterProxy.DeleteDevice(subCtx, device) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 607 | if err != nil { |
| 608 | cancel() |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 609 | //updating of transient state is required in error |
| 610 | if err := agent.updateTransientState(ctx, voltha.DeviceTransientState_DELETE_FAILED); err != nil { |
| 611 | logger.Errorw(ctx, "failed-to-update-transient-state-as-delete-failed", log.Fields{"device-id": agent.deviceID}) |
| 612 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 613 | return err |
| 614 | } |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame^] | 615 | go agent.waitForAdapterResponse(subCtx, cancel, "deleteDevice", ch, agent.onDeleteSuccess, |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 616 | agent.onDeleteFailure) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 617 | } |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 618 | return nil |
| 619 | } |
| 620 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 621 | func (agent *Agent) setParentID(ctx context.Context, device *voltha.Device, parentID string) error { |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 622 | if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
| 623 | return err |
| 624 | } |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame^] | 625 | logger.Debugw(ctx, "set-parent-id", log.Fields{"device-id": device.Id, "parent-id": parentID}) |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 626 | |
Kent Hagerman | f6db9f1 | 2020-07-22 17:16:19 -0400 | [diff] [blame] | 627 | cloned := agent.cloneDeviceWithoutLock() |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 628 | cloned.ParentId = parentID |
Kent Hagerman | f6db9f1 | 2020-07-22 17:16:19 -0400 | [diff] [blame] | 629 | return agent.updateDeviceAndReleaseLock(ctx, cloned) |
khenaidoo | ad06fd7 | 2019-10-28 12:26:05 -0400 | [diff] [blame] | 630 | } |
| 631 | |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 632 | // getSwitchCapability retrieves the switch capability of a parent device |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 633 | func (agent *Agent) getSwitchCapability(ctx context.Context) (*ic.SwitchCapability, error) { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame^] | 634 | logger.Debugw(ctx, "get-switch-capability", log.Fields{"device-id": agent.deviceID}) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 635 | |
Kent Hagerman | cba2f30 | 2020-07-28 13:37:36 -0400 | [diff] [blame] | 636 | device, err := agent.getDeviceReadOnly(ctx) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 637 | if err != nil { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 638 | return nil, err |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 639 | } |
Kent Hagerman | cba2f30 | 2020-07-28 13:37:36 -0400 | [diff] [blame] | 640 | ch, err := agent.adapterProxy.GetOfpDeviceInfo(ctx, device) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 641 | if err != nil { |
| 642 | return nil, err |
| 643 | } |
| 644 | |
| 645 | // Wait for adapter response |
| 646 | rpcResponse, ok := <-ch |
| 647 | if !ok { |
| 648 | return nil, status.Errorf(codes.Aborted, "channel-closed") |
| 649 | } |
| 650 | if rpcResponse.Err != nil { |
| 651 | return nil, rpcResponse.Err |
| 652 | } |
| 653 | // Successful response |
| 654 | switchCap := &ic.SwitchCapability{} |
| 655 | if err := ptypes.UnmarshalAny(rpcResponse.Reply, switchCap); err != nil { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 656 | return nil, err |
| 657 | } |
| 658 | return switchCap, nil |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 659 | } |
| 660 | |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 661 | func (agent *Agent) onPacketFailure(ctx context.Context, rpc string, response interface{}, args ...interface{}) { |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 662 | // packet data is encoded in the args param as the first parameter |
| 663 | var packet []byte |
| 664 | if len(args) >= 1 { |
| 665 | if pkt, ok := args[0].([]byte); ok { |
| 666 | packet = pkt |
| 667 | } |
| 668 | } |
| 669 | var errResp error |
| 670 | if err, ok := response.(error); ok { |
| 671 | errResp = err |
| 672 | } |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 673 | logger.Warnw(ctx, "packet-out-error", log.Fields{ |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 674 | "device-id": agent.deviceID, |
| 675 | "error": errResp, |
| 676 | "packet": hex.EncodeToString(packet), |
| 677 | }) |
| 678 | } |
| 679 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 680 | func (agent *Agent) packetOut(ctx context.Context, outPort uint32, packet *ofp.OfpPacketOut) error { |
Scott Baker | 8067860 | 2019-11-14 16:57:36 -0800 | [diff] [blame] | 681 | // If deviceType=="" then we must have taken ownership of this device. |
| 682 | // Fixes VOL-2226 where a core would take ownership and have stale data |
| 683 | if agent.deviceType == "" { |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 684 | agent.reconcileWithKVStore(ctx) |
Scott Baker | 8067860 | 2019-11-14 16:57:36 -0800 | [diff] [blame] | 685 | } |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 686 | // Send packet to adapter |
Rohan Agrawal | cf12f20 | 2020-08-03 04:42:01 +0000 | [diff] [blame] | 687 | subCtx, cancel := context.WithTimeout(log.WithSpanFromContext(context.Background(), ctx), agent.defaultTimeout) |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame^] | 688 | subCtx = coreutils.WithRPCMetadataFromContext(subCtx, ctx) |
| 689 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 690 | ch, err := agent.adapterProxy.PacketOut(subCtx, agent.deviceType, agent.deviceID, outPort, packet) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 691 | if err != nil { |
| 692 | cancel() |
| 693 | return nil |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 694 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 695 | go agent.waitForAdapterResponse(subCtx, cancel, "packetOut", ch, agent.onSuccess, agent.onPacketFailure, packet.Data) |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 696 | return nil |
| 697 | } |
| 698 | |
Kent Hagerman | f6db9f1 | 2020-07-22 17:16:19 -0400 | [diff] [blame] | 699 | func (agent *Agent) updateDeviceUsingAdapterData(ctx context.Context, device *voltha.Device) error { |
| 700 | if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
| 701 | return err |
| 702 | } |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame^] | 703 | logger.Debugw(ctx, "update-device-using-adapter-data", log.Fields{"device-id": device.Id}) |
Kent Hagerman | f6db9f1 | 2020-07-22 17:16:19 -0400 | [diff] [blame] | 704 | |
| 705 | cloned := agent.cloneDeviceWithoutLock() |
Mahir Gunyel | 8e2707d | 2019-07-25 00:36:21 -0700 | [diff] [blame] | 706 | cloned.Root = device.Root |
| 707 | cloned.Vendor = device.Vendor |
| 708 | cloned.Model = device.Model |
| 709 | cloned.SerialNumber = device.SerialNumber |
| 710 | cloned.MacAddress = device.MacAddress |
| 711 | cloned.Vlan = device.Vlan |
| 712 | cloned.Reason = device.Reason |
Andrea Campanella | 025667e | 2021-01-14 11:50:07 +0100 | [diff] [blame] | 713 | cloned.ImageDownloads = device.ImageDownloads |
Kent Hagerman | f6db9f1 | 2020-07-22 17:16:19 -0400 | [diff] [blame] | 714 | return agent.updateDeviceAndReleaseLock(ctx, cloned) |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 715 | } |
| 716 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 717 | func (agent *Agent) updateDeviceStatus(ctx context.Context, operStatus voltha.OperStatus_Types, connStatus voltha.ConnectStatus_Types) error { |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 718 | if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
| 719 | return err |
| 720 | } |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 721 | |
Kent Hagerman | f6db9f1 | 2020-07-22 17:16:19 -0400 | [diff] [blame] | 722 | cloned := agent.cloneDeviceWithoutLock() |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 723 | // Ensure the enums passed in are valid - they will be invalid if they are not set when this function is invoked |
Kent Hagerman | f6db9f1 | 2020-07-22 17:16:19 -0400 | [diff] [blame] | 724 | if s, ok := voltha.ConnectStatus_Types_name[int32(connStatus)]; ok { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame^] | 725 | logger.Debugw(ctx, "update-device-status-conn", log.Fields{"ok": ok, "val": s}) |
Kent Hagerman | f6db9f1 | 2020-07-22 17:16:19 -0400 | [diff] [blame] | 726 | cloned.ConnectStatus = connStatus |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 727 | } |
Kent Hagerman | f6db9f1 | 2020-07-22 17:16:19 -0400 | [diff] [blame] | 728 | if s, ok := voltha.OperStatus_Types_name[int32(operStatus)]; ok { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame^] | 729 | logger.Debugw(ctx, "update-device-status-conn", log.Fields{"ok": ok, "val": s}) |
Kent Hagerman | f6db9f1 | 2020-07-22 17:16:19 -0400 | [diff] [blame] | 730 | cloned.OperStatus = operStatus |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 731 | } |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame^] | 732 | logger.Debugw(ctx, "update-device-status", log.Fields{"device-id": cloned.Id, "oper-status": cloned.OperStatus, "connect-status": cloned.ConnectStatus}) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 733 | // Store the device |
Kent Hagerman | f6db9f1 | 2020-07-22 17:16:19 -0400 | [diff] [blame] | 734 | return agent.updateDeviceAndReleaseLock(ctx, cloned) |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 735 | } |
| 736 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 737 | // TODO: A generic device update by attribute |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 738 | func (agent *Agent) updateDeviceAttribute(ctx context.Context, name string, value interface{}) { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 739 | if value == nil { |
| 740 | return |
| 741 | } |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 742 | |
Kent Hagerman | f6db9f1 | 2020-07-22 17:16:19 -0400 | [diff] [blame] | 743 | if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
| 744 | logger.Warnw(ctx, "request-aborted", log.Fields{"device-id": agent.deviceID, "name": name, "error": err}) |
| 745 | return |
| 746 | } |
| 747 | |
| 748 | cloned := agent.cloneDeviceWithoutLock() |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 749 | updated := false |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 750 | s := reflect.ValueOf(cloned).Elem() |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 751 | if s.Kind() == reflect.Struct { |
| 752 | // exported field |
| 753 | f := s.FieldByName(name) |
| 754 | if f.IsValid() && f.CanSet() { |
| 755 | switch f.Kind() { |
| 756 | case reflect.String: |
| 757 | f.SetString(value.(string)) |
| 758 | updated = true |
| 759 | case reflect.Uint32: |
| 760 | f.SetUint(uint64(value.(uint32))) |
| 761 | updated = true |
| 762 | case reflect.Bool: |
| 763 | f.SetBool(value.(bool)) |
| 764 | updated = true |
| 765 | } |
| 766 | } |
| 767 | } |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 768 | logger.Debugw(ctx, "update-field-status", log.Fields{"device-id": cloned.Id, "name": name, "updated": updated}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 769 | // Save the data |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 770 | |
Kent Hagerman | f6db9f1 | 2020-07-22 17:16:19 -0400 | [diff] [blame] | 771 | if err := agent.updateDeviceAndReleaseLock(ctx, cloned); err != nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 772 | logger.Warnw(ctx, "attribute-update-failed", log.Fields{"attribute": name, "value": value}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 773 | } |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 774 | } |
serkant.uluderya | 334479d | 2019-04-10 08:26:15 -0700 | [diff] [blame] | 775 | |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 776 | func (agent *Agent) simulateAlarm(ctx context.Context, simulateReq *voltha.SimulateAlarmRequest) error { |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 777 | if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
| 778 | return err |
| 779 | } |
| 780 | defer agent.requestQueue.RequestComplete() |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame^] | 781 | logger.Debugw(ctx, "simulate-alarm", log.Fields{"device-id": agent.deviceID}) |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 782 | |
Kent Hagerman | cba2f30 | 2020-07-28 13:37:36 -0400 | [diff] [blame] | 783 | device := agent.getDeviceReadOnlyWithoutLock() |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 784 | |
Rohan Agrawal | cf12f20 | 2020-08-03 04:42:01 +0000 | [diff] [blame] | 785 | subCtx, cancel := context.WithTimeout(log.WithSpanFromContext(context.Background(), ctx), agent.defaultTimeout) |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame^] | 786 | subCtx = coreutils.WithRPCMetadataFromContext(subCtx, ctx) |
| 787 | |
Kent Hagerman | f6db9f1 | 2020-07-22 17:16:19 -0400 | [diff] [blame] | 788 | ch, err := agent.adapterProxy.SimulateAlarm(subCtx, device, simulateReq) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 789 | if err != nil { |
| 790 | cancel() |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 791 | return err |
serkant.uluderya | 334479d | 2019-04-10 08:26:15 -0700 | [diff] [blame] | 792 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 793 | go agent.waitForAdapterResponse(subCtx, cancel, "simulateAlarm", ch, agent.onSuccess, agent.onFailure) |
serkant.uluderya | 334479d | 2019-04-10 08:26:15 -0700 | [diff] [blame] | 794 | return nil |
| 795 | } |
Mahir Gunyel | b585167 | 2019-07-24 10:46:26 +0300 | [diff] [blame] | 796 | |
Kent Hagerman | f6db9f1 | 2020-07-22 17:16:19 -0400 | [diff] [blame] | 797 | // This function updates the device in the DB, releases the device lock, and runs any state transitions. |
| 798 | // The calling function MUST hold the device lock. The caller MUST NOT modify the device after this is called. |
| 799 | func (agent *Agent) updateDeviceAndReleaseLock(ctx context.Context, device *voltha.Device) error { |
| 800 | // fail early if this agent is no longer valid |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 801 | if agent.stopped { |
Kent Hagerman | f6db9f1 | 2020-07-22 17:16:19 -0400 | [diff] [blame] | 802 | agent.requestQueue.RequestComplete() |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 803 | return errors.New("device-agent-stopped") |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 804 | } |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 805 | |
Kent Hagerman | f6db9f1 | 2020-07-22 17:16:19 -0400 | [diff] [blame] | 806 | // update in db |
Kent Hagerman | f5a6735 | 2020-04-30 15:15:26 -0400 | [diff] [blame] | 807 | if err := agent.dbProxy.Set(ctx, agent.deviceID, device); err != nil { |
Kent Hagerman | f6db9f1 | 2020-07-22 17:16:19 -0400 | [diff] [blame] | 808 | agent.requestQueue.RequestComplete() |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 809 | return status.Errorf(codes.Internal, "failed-update-device:%s: %s", agent.deviceID, err) |
Mahir Gunyel | b585167 | 2019-07-24 10:46:26 +0300 | [diff] [blame] | 810 | } |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 811 | logger.Debugw(ctx, "updated-device-in-store", log.Fields{"device-id: ": agent.deviceID}) |
Mahir Gunyel | b585167 | 2019-07-24 10:46:26 +0300 | [diff] [blame] | 812 | |
Kent Hagerman | 6031aad | 2020-07-29 16:36:33 -0400 | [diff] [blame] | 813 | prevDevice := agent.device |
Kent Hagerman | f6db9f1 | 2020-07-22 17:16:19 -0400 | [diff] [blame] | 814 | // update the device |
khenaidoo | 0db4c81 | 2020-05-27 15:27:30 -0400 | [diff] [blame] | 815 | agent.device = device |
Kent Hagerman | f6db9f1 | 2020-07-22 17:16:19 -0400 | [diff] [blame] | 816 | |
| 817 | // release lock before processing transition |
| 818 | agent.requestQueue.RequestComplete() |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame^] | 819 | subCtx := coreutils.WithSpanAndRPCMetadataFromContext(ctx) |
Kent Hagerman | f6db9f1 | 2020-07-22 17:16:19 -0400 | [diff] [blame] | 820 | |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame^] | 821 | if err := agent.deviceMgr.stateTransitions.ProcessTransition(subCtx, |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 822 | device, prevDevice, voltha.DeviceTransientState_NONE, voltha.DeviceTransientState_NONE); err != nil { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame^] | 823 | logger.Errorw(ctx, "failed-process-transition", log.Fields{"device-id": device.Id, "previous-admin-state": prevDevice.AdminState, "current-admin-state": device.AdminState}) |
| 824 | // Sending RPC EVENT here |
| 825 | rpce := agent.deviceMgr.NewRPCEvent(ctx, agent.deviceID, err.Error(), nil) |
| 826 | go agent.deviceMgr.SendRPCEvent(ctx, "RPC_ERROR_RAISE_EVENT", rpce, voltha.EventCategory_COMMUNICATION, |
| 827 | nil, time.Now().UnixNano()) |
| 828 | |
Kent Hagerman | f6db9f1 | 2020-07-22 17:16:19 -0400 | [diff] [blame] | 829 | } |
Mahir Gunyel | b585167 | 2019-07-24 10:46:26 +0300 | [diff] [blame] | 830 | return nil |
| 831 | } |
Mahir Gunyel | fdee921 | 2019-10-16 16:52:21 -0700 | [diff] [blame] | 832 | |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 833 | // This function updates the device transient in the DB through loader, releases the device lock, and runs any state transitions. |
| 834 | // The calling function MUST hold the device lock. The caller MUST NOT modify the device after this is called. |
| 835 | func (agent *Agent) updateDeviceWithTransientStateAndReleaseLock(ctx context.Context, device *voltha.Device, |
| 836 | transientState, prevTransientState voltha.DeviceTransientState_Types) error { |
| 837 | // fail early if this agent is no longer valid |
| 838 | if agent.stopped { |
| 839 | agent.requestQueue.RequestComplete() |
| 840 | return errors.New("device-agent-stopped") |
| 841 | } |
| 842 | //update device TransientState |
| 843 | if err := agent.updateTransientState(ctx, transientState); err != nil { |
| 844 | agent.requestQueue.RequestComplete() |
| 845 | return err |
| 846 | } |
| 847 | // update in db |
| 848 | if err := agent.dbProxy.Set(ctx, agent.deviceID, device); err != nil { |
| 849 | //Reverting TransientState update |
| 850 | err := agent.updateTransientState(ctx, prevTransientState) |
| 851 | logger.Errorw(ctx, "failed-to-revert-transient-state-update-on-error", log.Fields{"device-id": device.Id, |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame^] | 852 | "previous-transient-state": prevTransientState, "current-transient-state": transientState}) |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 853 | agent.requestQueue.RequestComplete() |
| 854 | return status.Errorf(codes.Internal, "failed-update-device:%s: %s", agent.deviceID, err) |
| 855 | } |
| 856 | |
| 857 | logger.Debugw(ctx, "updated-device-in-store", log.Fields{"device-id: ": agent.deviceID}) |
| 858 | |
| 859 | prevDevice := agent.device |
| 860 | // update the device |
| 861 | agent.device = device |
| 862 | |
| 863 | // release lock before processing transition |
| 864 | agent.requestQueue.RequestComplete() |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame^] | 865 | subCtx := coreutils.WithSpanAndRPCMetadataFromContext(ctx) |
| 866 | if err := agent.deviceMgr.stateTransitions.ProcessTransition(subCtx, |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 867 | device, prevDevice, transientState, prevTransientState); err != nil { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame^] | 868 | logger.Errorw(ctx, "failed-process-transition", log.Fields{"device-id": device.Id, "previous-admin-state": prevDevice.AdminState, "current-admin-state": device.AdminState}) |
| 869 | // Sending RPC EVENT here |
| 870 | rpce := agent.deviceMgr.NewRPCEvent(ctx, agent.deviceID, err.Error(), nil) |
| 871 | go agent.deviceMgr.SendRPCEvent(ctx, "RPC_ERROR_RAISE_EVENT", rpce, voltha.EventCategory_COMMUNICATION, |
| 872 | nil, time.Now().UnixNano()) |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 873 | } |
| 874 | return nil |
| 875 | } |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 876 | func (agent *Agent) updateDeviceReason(ctx context.Context, reason string) error { |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 877 | if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
| 878 | return err |
| 879 | } |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame^] | 880 | logger.Debugw(ctx, "update-device-reason", log.Fields{"device-id": agent.deviceID, "reason": reason}) |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 881 | |
Kent Hagerman | f6db9f1 | 2020-07-22 17:16:19 -0400 | [diff] [blame] | 882 | cloned := agent.cloneDeviceWithoutLock() |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 883 | cloned.Reason = reason |
Kent Hagerman | f6db9f1 | 2020-07-22 17:16:19 -0400 | [diff] [blame] | 884 | return agent.updateDeviceAndReleaseLock(ctx, cloned) |
Mahir Gunyel | fdee921 | 2019-10-16 16:52:21 -0700 | [diff] [blame] | 885 | } |
kesavand | bc2d162 | 2020-01-21 00:42:01 -0500 | [diff] [blame] | 886 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 887 | func (agent *Agent) ChildDeviceLost(ctx context.Context, device *voltha.Device) error { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame^] | 888 | logger.Debugw(ctx, "child-device-lost", log.Fields{"child-device-id": device.Id, "parent-device-id": agent.deviceID}) |
Chaitrashree G S | 543df3e | 2020-02-24 22:36:54 -0500 | [diff] [blame] | 889 | |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 890 | // Remove the associated peer ports on the parent device |
| 891 | for portID := range agent.portLoader.ListIDs() { |
| 892 | if portHandle, have := agent.portLoader.Lock(portID); have { |
| 893 | oldPort := portHandle.GetReadOnly() |
| 894 | updatedPeers := make([]*voltha.Port_PeerPort, 0) |
| 895 | for _, peerPort := range oldPort.Peers { |
| 896 | if peerPort.DeviceId != device.Id { |
| 897 | updatedPeers = append(updatedPeers, peerPort) |
| 898 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 899 | } |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 900 | newPort := *oldPort |
| 901 | newPort.Peers = updatedPeers |
| 902 | if err := portHandle.Update(ctx, &newPort); err != nil { |
| 903 | portHandle.Unlock() |
| 904 | return nil |
| 905 | } |
| 906 | portHandle.Unlock() |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 907 | } |
Chaitrashree G S | 543df3e | 2020-02-24 22:36:54 -0500 | [diff] [blame] | 908 | } |
| 909 | |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 910 | //send request to adapter |
Rohan Agrawal | cf12f20 | 2020-08-03 04:42:01 +0000 | [diff] [blame] | 911 | subCtx, cancel := context.WithTimeout(log.WithSpanFromContext(context.Background(), ctx), agent.defaultTimeout) |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame^] | 912 | subCtx = coreutils.WithRPCMetadataFromContext(subCtx, ctx) |
| 913 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 914 | ch, err := agent.adapterProxy.ChildDeviceLost(ctx, agent.deviceType, agent.deviceID, device.ParentPortNo, device.ProxyAddress.OnuId) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 915 | if err != nil { |
| 916 | cancel() |
| 917 | return err |
Chaitrashree G S | 543df3e | 2020-02-24 22:36:54 -0500 | [diff] [blame] | 918 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 919 | go agent.waitForAdapterResponse(subCtx, cancel, "childDeviceLost", ch, agent.onSuccess, agent.onFailure) |
Chaitrashree G S | 543df3e | 2020-02-24 22:36:54 -0500 | [diff] [blame] | 920 | return nil |
Chaitrashree G S | 543df3e | 2020-02-24 22:36:54 -0500 | [diff] [blame] | 921 | } |
onkarkundargi | 8728525 | 2020-01-27 11:34:52 +0530 | [diff] [blame] | 922 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 923 | func (agent *Agent) startOmciTest(ctx context.Context, omcitestrequest *voltha.OmciTestRequest) (*voltha.TestResponse, error) { |
onkarkundargi | 8728525 | 2020-01-27 11:34:52 +0530 | [diff] [blame] | 924 | if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
| 925 | return nil, err |
| 926 | } |
| 927 | |
Kent Hagerman | f6db9f1 | 2020-07-22 17:16:19 -0400 | [diff] [blame] | 928 | cloned := agent.cloneDeviceWithoutLock() |
Matteo Scandolo | d525ae3 | 2020-04-02 17:27:29 -0700 | [diff] [blame] | 929 | |
Kent Hagerman | f6db9f1 | 2020-07-22 17:16:19 -0400 | [diff] [blame] | 930 | if cloned.Adapter == "" { |
| 931 | adapterName, err := agent.adapterMgr.GetAdapterType(cloned.Type) |
Matteo Scandolo | d525ae3 | 2020-04-02 17:27:29 -0700 | [diff] [blame] | 932 | if err != nil { |
| 933 | agent.requestQueue.RequestComplete() |
| 934 | return nil, err |
| 935 | } |
Kent Hagerman | f6db9f1 | 2020-07-22 17:16:19 -0400 | [diff] [blame] | 936 | cloned.Adapter = adapterName |
onkarkundargi | 8728525 | 2020-01-27 11:34:52 +0530 | [diff] [blame] | 937 | } |
| 938 | |
| 939 | // Send request to the adapter |
Kent Hagerman | f6db9f1 | 2020-07-22 17:16:19 -0400 | [diff] [blame] | 940 | ch, err := agent.adapterProxy.StartOmciTest(ctx, cloned, omcitestrequest) |
onkarkundargi | 8728525 | 2020-01-27 11:34:52 +0530 | [diff] [blame] | 941 | agent.requestQueue.RequestComplete() |
| 942 | if err != nil { |
| 943 | return nil, err |
| 944 | } |
| 945 | |
| 946 | // Wait for the adapter response |
| 947 | rpcResponse, ok := <-ch |
| 948 | if !ok { |
| 949 | return nil, status.Errorf(codes.Aborted, "channel-closed-device-id-%s", agent.deviceID) |
| 950 | } |
| 951 | if rpcResponse.Err != nil { |
| 952 | return nil, rpcResponse.Err |
| 953 | } |
| 954 | |
| 955 | // Unmarshal and return the response |
| 956 | testResp := &voltha.TestResponse{} |
| 957 | if err := ptypes.UnmarshalAny(rpcResponse.Reply, testResp); err != nil { |
| 958 | return nil, status.Errorf(codes.InvalidArgument, "%s", err.Error()) |
| 959 | } |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame^] | 960 | logger.Debugw(ctx, "omci_test_request-success-device-agent", log.Fields{"test-resp": testResp}) |
onkarkundargi | 8728525 | 2020-01-27 11:34:52 +0530 | [diff] [blame] | 961 | return testResp, nil |
| 962 | } |
Dinesh Belwalkar | c1129f1 | 2020-02-27 10:41:33 -0800 | [diff] [blame] | 963 | |
| 964 | func (agent *Agent) getExtValue(ctx context.Context, pdevice *voltha.Device, cdevice *voltha.Device, valueparam *voltha.ValueSpecifier) (*voltha.ReturnValues, error) { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame^] | 965 | logger.Debugw(ctx, "get-ext-value", log.Fields{"device-id": agent.deviceID, "onu-id": valueparam.Id, "value-type": valueparam.Value}) |
Dinesh Belwalkar | c1129f1 | 2020-02-27 10:41:33 -0800 | [diff] [blame] | 966 | if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
| 967 | return nil, err |
| 968 | } |
| 969 | |
| 970 | //send request to adapter |
| 971 | ch, err := agent.adapterProxy.GetExtValue(ctx, pdevice, cdevice, valueparam.Id, valueparam.Value) |
| 972 | agent.requestQueue.RequestComplete() |
| 973 | if err != nil { |
| 974 | return nil, err |
| 975 | } |
| 976 | |
| 977 | // Wait for the adapter response |
| 978 | rpcResponse, ok := <-ch |
| 979 | if !ok { |
| 980 | return nil, status.Errorf(codes.Aborted, "channel-closed-device-id-%s", agent.deviceID) |
| 981 | } |
| 982 | if rpcResponse.Err != nil { |
| 983 | return nil, rpcResponse.Err |
| 984 | } |
| 985 | |
| 986 | // Unmarshal and return the response |
| 987 | Resp := &voltha.ReturnValues{} |
| 988 | if err := ptypes.UnmarshalAny(rpcResponse.Reply, Resp); err != nil { |
| 989 | return nil, status.Errorf(codes.InvalidArgument, "%s", err.Error()) |
| 990 | } |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame^] | 991 | logger.Debugw(ctx, "get-ext-value-success-device-agent", log.Fields{"Resp": Resp}) |
Dinesh Belwalkar | c1129f1 | 2020-02-27 10:41:33 -0800 | [diff] [blame] | 992 | return Resp, nil |
| 993 | } |
dpaul | 6268631 | 2020-06-23 14:17:36 +0530 | [diff] [blame] | 994 | |
| 995 | func (agent *Agent) setExtValue(ctx context.Context, device *voltha.Device, value *voltha.ValueSet) (*empty.Empty, error) { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame^] | 996 | logger.Debugw(ctx, "set-ext-value", log.Fields{"device-id": value.Id}) |
dpaul | 6268631 | 2020-06-23 14:17:36 +0530 | [diff] [blame] | 997 | if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
| 998 | return nil, err |
| 999 | } |
| 1000 | |
| 1001 | //send request to adapter |
| 1002 | ch, err := agent.adapterProxy.SetExtValue(ctx, device, value) |
| 1003 | agent.requestQueue.RequestComplete() |
| 1004 | if err != nil { |
| 1005 | return nil, err |
| 1006 | } |
| 1007 | |
| 1008 | // Wait for the adapter response |
| 1009 | rpcResponse, ok := <-ch |
| 1010 | if !ok { |
| 1011 | return nil, status.Errorf(codes.Aborted, "channel-closed-device-id-%s", agent.deviceID) |
| 1012 | } |
| 1013 | if rpcResponse.Err != nil { |
| 1014 | return nil, rpcResponse.Err |
| 1015 | } |
| 1016 | |
| 1017 | // Unmarshal and return the response |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame^] | 1018 | logger.Debug(ctx, "set-ext-value-success-device-agent") |
dpaul | 6268631 | 2020-06-23 14:17:36 +0530 | [diff] [blame] | 1019 | return &empty.Empty{}, nil |
| 1020 | } |
Salman Siddiqui | 1cf9504 | 2020-11-19 00:42:56 +0530 | [diff] [blame] | 1021 | |
| 1022 | func (agent *Agent) getSingleValue(ctx context.Context, request *extension.SingleGetValueRequest) (*extension.SingleGetValueResponse, error) { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame^] | 1023 | logger.Debugw(ctx, "get-single-value", log.Fields{"device-id": request.TargetId}) |
Salman Siddiqui | 1cf9504 | 2020-11-19 00:42:56 +0530 | [diff] [blame] | 1024 | |
| 1025 | if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
| 1026 | return nil, err |
| 1027 | } |
| 1028 | |
| 1029 | cloned := agent.cloneDeviceWithoutLock() |
| 1030 | |
| 1031 | //send request to adapter |
| 1032 | ch, err := agent.adapterProxy.GetSingleValue(ctx, cloned.Adapter, request) |
| 1033 | agent.requestQueue.RequestComplete() |
| 1034 | if err != nil { |
| 1035 | return nil, err |
| 1036 | } |
| 1037 | |
| 1038 | // Wait for the adapter response |
| 1039 | rpcResponse, ok := <-ch |
| 1040 | if !ok { |
| 1041 | return nil, status.Errorf(codes.Aborted, "channel-closed-device-id-%s", agent.deviceID) |
| 1042 | } |
| 1043 | |
| 1044 | if rpcResponse.Err != nil { |
| 1045 | return nil, rpcResponse.Err |
| 1046 | } |
| 1047 | |
| 1048 | resp := &extension.SingleGetValueResponse{} |
| 1049 | if err := ptypes.UnmarshalAny(rpcResponse.Reply, resp); err != nil { |
| 1050 | return nil, status.Errorf(codes.InvalidArgument, "%s", err.Error()) |
| 1051 | } |
| 1052 | |
| 1053 | return resp, nil |
| 1054 | } |
| 1055 | |
| 1056 | func (agent *Agent) setSingleValue(ctx context.Context, request *extension.SingleSetValueRequest) (*extension.SingleSetValueResponse, error) { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame^] | 1057 | logger.Debugw(ctx, "set-single-value", log.Fields{"device-id": request.TargetId}) |
Salman Siddiqui | 1cf9504 | 2020-11-19 00:42:56 +0530 | [diff] [blame] | 1058 | |
| 1059 | if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
| 1060 | return nil, err |
| 1061 | } |
| 1062 | |
| 1063 | cloned := agent.cloneDeviceWithoutLock() |
| 1064 | |
| 1065 | //send request to adapter |
| 1066 | ch, err := agent.adapterProxy.SetSingleValue(ctx, cloned.Adapter, request) |
| 1067 | agent.requestQueue.RequestComplete() |
| 1068 | if err != nil { |
| 1069 | return nil, err |
| 1070 | } |
| 1071 | |
| 1072 | // Wait for the adapter response |
| 1073 | rpcResponse, ok := <-ch |
| 1074 | if !ok { |
| 1075 | return nil, status.Errorf(codes.Aborted, "channel-closed-cloned-id-%s", agent.deviceID) |
| 1076 | } |
| 1077 | |
| 1078 | if rpcResponse.Err != nil { |
| 1079 | return nil, rpcResponse.Err |
| 1080 | } |
| 1081 | |
| 1082 | resp := &extension.SingleSetValueResponse{} |
| 1083 | if err := ptypes.UnmarshalAny(rpcResponse.Reply, resp); err != nil { |
| 1084 | return nil, status.Errorf(codes.InvalidArgument, "%s", err.Error()) |
| 1085 | } |
| 1086 | |
| 1087 | return resp, nil |
| 1088 | } |