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 | b0343bf | 2021-05-11 14:14:26 -0700 | [diff] [blame] | 24 | "reflect" |
| 25 | "sync" |
| 26 | "time" |
| 27 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 28 | "github.com/opencord/voltha-protos/v5/go/adapter_services" |
| 29 | "github.com/opencord/voltha-protos/v5/go/core" |
| 30 | |
Maninder | 0aabf0c | 2021-03-17 14:55:14 +0530 | [diff] [blame] | 31 | "github.com/cenkalti/backoff/v3" |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 32 | "github.com/gogo/protobuf/proto" |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 33 | "github.com/golang/protobuf/ptypes/empty" |
Maninder | 0aabf0c | 2021-03-17 14:55:14 +0530 | [diff] [blame] | 34 | "github.com/opencord/voltha-go/rw_core/config" |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 35 | "google.golang.org/grpc/codes" |
| 36 | "google.golang.org/grpc/status" |
Mahir Gunyel | addb66a | 2020-04-29 18:08:50 -0700 | [diff] [blame] | 37 | |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 38 | "github.com/opencord/voltha-go/db/model" |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 39 | "github.com/opencord/voltha-go/rw_core/core/adapter" |
Mahir Gunyel | 03de0d3 | 2020-06-03 01:36:59 -0700 | [diff] [blame] | 40 | "github.com/opencord/voltha-go/rw_core/core/device/flow" |
| 41 | "github.com/opencord/voltha-go/rw_core/core/device/group" |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 42 | "github.com/opencord/voltha-go/rw_core/core/device/port" |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 43 | "github.com/opencord/voltha-go/rw_core/core/device/transientstate" |
Scott Baker | b671a86 | 2019-10-24 10:53:40 -0700 | [diff] [blame] | 44 | coreutils "github.com/opencord/voltha-go/rw_core/utils" |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 45 | "github.com/opencord/voltha-lib-go/v7/pkg/log" |
| 46 | "github.com/opencord/voltha-protos/v5/go/common" |
| 47 | "github.com/opencord/voltha-protos/v5/go/extension" |
| 48 | ic "github.com/opencord/voltha-protos/v5/go/inter_container" |
| 49 | ofp "github.com/opencord/voltha-protos/v5/go/openflow_13" |
| 50 | "github.com/opencord/voltha-protos/v5/go/voltha" |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 51 | ) |
| 52 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 53 | var errReconcileAborted = errors.New("reconcile aborted") |
| 54 | var errContextExpired = errors.New("context expired") |
| 55 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 56 | // Agent represents device agent attributes |
| 57 | type Agent struct { |
Maninder | 0aabf0c | 2021-03-17 14:55:14 +0530 | [diff] [blame] | 58 | deviceID string |
| 59 | parentID string |
| 60 | deviceType string |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 61 | adapterEndpoint string |
Maninder | 0aabf0c | 2021-03-17 14:55:14 +0530 | [diff] [blame] | 62 | isRootDevice bool |
Maninder | 0aabf0c | 2021-03-17 14:55:14 +0530 | [diff] [blame] | 63 | adapterMgr *adapter.Manager |
| 64 | deviceMgr *Manager |
| 65 | dbProxy *model.Proxy |
| 66 | exitChannel chan int |
| 67 | device *voltha.Device |
| 68 | requestQueue *coreutils.RequestQueue |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 69 | internalTimeout time.Duration |
| 70 | rpcTimeout time.Duration |
Maninder | 0aabf0c | 2021-03-17 14:55:14 +0530 | [diff] [blame] | 71 | startOnce sync.Once |
| 72 | stopOnce sync.Once |
| 73 | stopped bool |
| 74 | stopReconciling chan int |
| 75 | stopReconcilingMutex sync.RWMutex |
| 76 | config *config.RWCoreFlags |
Mahir Gunyel | 03de0d3 | 2020-06-03 01:36:59 -0700 | [diff] [blame] | 77 | |
khenaidoo | 7585a96 | 2021-06-10 16:15:38 -0400 | [diff] [blame] | 78 | flowCache *flow.Cache |
| 79 | groupCache *group.Cache |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 80 | portLoader *port.Loader |
| 81 | transientStateLoader *transientstate.Loader |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 82 | } |
| 83 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 84 | //newAgent creates a new device agent. The device will be initialized when start() is called. |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 85 | func newAgent(device *voltha.Device, deviceMgr *Manager, dbPath *model.Path, deviceProxy *model.Proxy, internalTimeout, rpcTimeout time.Duration) *Agent { |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 86 | deviceID := device.Id |
| 87 | if deviceID == "" { |
| 88 | deviceID = coreutils.CreateDeviceID() |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 89 | } |
Scott Baker | 8067860 | 2019-11-14 16:57:36 -0800 | [diff] [blame] | 90 | |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 91 | return &Agent{ |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 92 | deviceID: deviceID, |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 93 | isRootDevice: device.Root, |
| 94 | parentID: device.ParentId, |
| 95 | deviceType: device.Type, |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 96 | adapterEndpoint: device.AdapterEndpoint, |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 97 | deviceMgr: deviceMgr, |
| 98 | adapterMgr: deviceMgr.adapterMgr, |
| 99 | exitChannel: make(chan int, 1), |
| 100 | dbProxy: deviceProxy, |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 101 | internalTimeout: internalTimeout, |
| 102 | rpcTimeout: rpcTimeout, |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 103 | device: proto.Clone(device).(*voltha.Device), |
| 104 | requestQueue: coreutils.NewRequestQueue(), |
Maninder | 0aabf0c | 2021-03-17 14:55:14 +0530 | [diff] [blame] | 105 | config: deviceMgr.config, |
khenaidoo | 7585a96 | 2021-06-10 16:15:38 -0400 | [diff] [blame] | 106 | flowCache: flow.NewCache(), |
| 107 | groupCache: group.NewCache(), |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 108 | portLoader: port.NewLoader(dbPath.SubPath("ports").Proxy(deviceID)), |
| 109 | transientStateLoader: transientstate.NewLoader(dbPath.SubPath("core").Proxy("transientstate"), deviceID), |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 110 | } |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 111 | } |
| 112 | |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 113 | // start() saves the device to the data model and registers for callbacks on that device if deviceToCreate!=nil. |
| 114 | // 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] | 115 | // was started. |
khenaidoo | 7585a96 | 2021-06-10 16:15:38 -0400 | [diff] [blame] | 116 | func (agent *Agent) start(ctx context.Context, deviceExist bool, deviceToCreate *voltha.Device) (*voltha.Device, error) { |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 117 | needToStart := false |
| 118 | if agent.startOnce.Do(func() { needToStart = true }); !needToStart { |
Kent Hagerman | cba2f30 | 2020-07-28 13:37:36 -0400 | [diff] [blame] | 119 | return agent.getDeviceReadOnly(ctx) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 120 | } |
| 121 | var startSucceeded bool |
| 122 | defer func() { |
| 123 | if !startSucceeded { |
| 124 | if err := agent.stop(ctx); err != nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 125 | 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] | 126 | } |
| 127 | } |
| 128 | }() |
khenaidoo | 7585a96 | 2021-06-10 16:15:38 -0400 | [diff] [blame] | 129 | if deviceExist { |
| 130 | device := deviceToCreate |
| 131 | if device == nil { |
| 132 | // Load from dB |
| 133 | device = &voltha.Device{} |
| 134 | have, err := agent.dbProxy.Get(ctx, agent.deviceID, device) |
| 135 | if err != nil { |
| 136 | return nil, err |
| 137 | } else if !have { |
| 138 | return nil, status.Errorf(codes.NotFound, "device-%s", agent.deviceID) |
| 139 | } |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 140 | logger.Infow(ctx, "device-loaded-from-db", log.Fields{"device-id": agent.deviceID, "adapter-endpoint": device.AdapterEndpoint, "type": device.Type}) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 141 | } |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 142 | agent.deviceType = device.Type |
| 143 | agent.adapterEndpoint = device.AdapterEndpoint |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 144 | agent.device = proto.Clone(device).(*voltha.Device) |
khenaidoo | 7585a96 | 2021-06-10 16:15:38 -0400 | [diff] [blame] | 145 | // load the ports from KV to cache |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 146 | agent.portLoader.Load(ctx) |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 147 | agent.transientStateLoader.Load(ctx) |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 148 | } else { |
Scott Baker | 8067860 | 2019-11-14 16:57:36 -0800 | [diff] [blame] | 149 | // Create a new device |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 150 | var desc string |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 151 | var err error |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 152 | prevState := common.AdminState_UNKNOWN |
| 153 | currState := common.AdminState_UNKNOWN |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 154 | requestStatus := &common.OperationResp{Code: common.OperationResp_OPERATION_FAILURE} |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 155 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 156 | defer func() { agent.logDeviceUpdate(ctx, &prevState, &currState, requestStatus, err, desc) }() |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 157 | |
Kent Hagerman | f5a6735 | 2020-04-30 15:15:26 -0400 | [diff] [blame] | 158 | // Assumption is that AdminState, FlowGroups, and Flows are uninitialized since this |
| 159 | // 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] | 160 | // agent.deviceId will also have been set during newAgent(). |
khenaidoo | 7585a96 | 2021-06-10 16:15:38 -0400 | [diff] [blame] | 161 | device := (proto.Clone(deviceToCreate)).(*voltha.Device) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 162 | device.Id = agent.deviceID |
Scott Baker | 8067860 | 2019-11-14 16:57:36 -0800 | [diff] [blame] | 163 | device.AdminState = voltha.AdminState_PREPROVISIONED |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 164 | currState = device.AdminState |
Scott Baker | 8067860 | 2019-11-14 16:57:36 -0800 | [diff] [blame] | 165 | if !deviceToCreate.GetRoot() && deviceToCreate.ProxyAddress != nil { |
| 166 | // Set the default vlan ID to the one specified by the parent adapter. It can be |
| 167 | // overwritten by the child adapter during a device update request |
| 168 | device.Vlan = deviceToCreate.ProxyAddress.ChannelId |
| 169 | } |
| 170 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 171 | // Save the device to the model |
| 172 | if err = agent.dbProxy.Set(ctx, agent.deviceID, device); err != nil { |
| 173 | err = status.Errorf(codes.Aborted, "failed-adding-device-%s: %s", agent.deviceID, err) |
| 174 | return nil, err |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 175 | } |
Mahir Gunyel | b0343bf | 2021-05-11 14:14:26 -0700 | [diff] [blame] | 176 | _ = agent.deviceMgr.Agent.SendDeviceStateChangeEvent(ctx, device.OperStatus, device.ConnectStatus, prevState, device, time.Now().Unix()) |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 177 | requestStatus.Code = common.OperationResp_OPERATION_SUCCESS |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 178 | agent.device = device |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 179 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 180 | startSucceeded = true |
Rohan Agrawal | cf12f20 | 2020-08-03 04:42:01 +0000 | [diff] [blame] | 181 | log.EnrichSpan(ctx, log.Fields{"device-id": agent.deviceID}) |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 182 | logger.Debugw(ctx, "device-agent-started", log.Fields{"device-id": agent.deviceID}) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 183 | |
Kent Hagerman | cba2f30 | 2020-07-28 13:37:36 -0400 | [diff] [blame] | 184 | return agent.getDeviceReadOnly(ctx) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 185 | } |
| 186 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 187 | // stop stops the device agent. Not much to do for now |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 188 | func (agent *Agent) stop(ctx context.Context) error { |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 189 | needToStop := false |
| 190 | if agent.stopOnce.Do(func() { needToStop = true }); !needToStop { |
| 191 | return nil |
| 192 | } |
| 193 | if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
| 194 | return err |
| 195 | } |
| 196 | defer agent.requestQueue.RequestComplete() |
khenaidoo | 4908535 | 2020-01-13 19:15:43 -0500 | [diff] [blame] | 197 | |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 198 | 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] | 199 | // Remove the device transient loader |
| 200 | if err := agent.deleteTransientState(ctx); err != nil { |
| 201 | return err |
| 202 | } |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 203 | // Remove the device from the KV store |
Kent Hagerman | f5a6735 | 2020-04-30 15:15:26 -0400 | [diff] [blame] | 204 | if err := agent.dbProxy.Remove(ctx, agent.deviceID); err != nil { |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 205 | return err |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 206 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 207 | |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 208 | close(agent.exitChannel) |
| 209 | |
| 210 | agent.stopped = true |
| 211 | |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 212 | 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] | 213 | |
| 214 | return nil |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 215 | } |
| 216 | |
Scott Baker | 8067860 | 2019-11-14 16:57:36 -0800 | [diff] [blame] | 217 | // Load the most recent state from the KVStore for the device. |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 218 | func (agent *Agent) reconcileWithKVStore(ctx context.Context) { |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 219 | if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 220 | logger.Warnw(ctx, "request-aborted", log.Fields{"device-id": agent.deviceID, "error": err}) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 221 | return |
| 222 | } |
| 223 | defer agent.requestQueue.RequestComplete() |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 224 | logger.Debug(ctx, "reconciling-device-agent-devicetype") |
Scott Baker | 8067860 | 2019-11-14 16:57:36 -0800 | [diff] [blame] | 225 | // TODO: context timeout |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 226 | device := &voltha.Device{} |
Kent Hagerman | f5a6735 | 2020-04-30 15:15:26 -0400 | [diff] [blame] | 227 | if have, err := agent.dbProxy.Get(ctx, agent.deviceID, device); err != nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 228 | 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] | 229 | return |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 230 | } else if !have { |
| 231 | return // not found in kv |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 232 | } |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 233 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 234 | agent.deviceType = device.Type |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 235 | agent.device = device |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 236 | agent.adapterEndpoint = device.AdapterEndpoint |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 237 | agent.portLoader.Load(ctx) |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 238 | agent.transientStateLoader.Load(ctx) |
| 239 | |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 240 | 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] | 241 | } |
| 242 | |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 243 | // onSuccess is a common callback for scenarios where we receive a nil response following a request to an adapter |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 244 | func (agent *Agent) onSuccess(ctx context.Context, prevState, currState *common.AdminState_Types, deviceUpdateLog bool) { |
| 245 | if deviceUpdateLog { |
| 246 | requestStatus := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS} |
| 247 | desc := "adapter-response" |
| 248 | agent.logDeviceUpdate(ctx, prevState, currState, requestStatus, nil, desc) |
| 249 | return |
| 250 | } |
| 251 | logger.Debugw(ctx, "successful-operation", log.Fields{"device-id": agent.deviceID, "rpc": coreutils.GetRPCMetadataFromContext(ctx)}) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | // onFailure is a common callback for scenarios where we receive an error response following a request to an adapter |
| 255 | // and the only action required is to publish the failed result on kafka |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 256 | func (agent *Agent) onFailure(ctx context.Context, err error, prevState, currState *common.AdminState_Types, deviceUpdateLog bool) { |
| 257 | // Send an event on kafka |
| 258 | rpce := agent.deviceMgr.NewRPCEvent(ctx, agent.deviceID, err.Error(), nil) |
| 259 | go agent.deviceMgr.SendRPCEvent(ctx, "RPC_ERROR_RAISE_EVENT", rpce, |
| 260 | voltha.EventCategory_COMMUNICATION, nil, time.Now().Unix()) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 261 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 262 | // Log the device update event |
| 263 | if deviceUpdateLog { |
| 264 | requestStatus := &common.OperationResp{Code: common.OperationResp_OPERATION_FAILURE} |
| 265 | desc := "adapter-response" |
| 266 | agent.logDeviceUpdate(ctx, prevState, currState, requestStatus, err, desc) |
| 267 | return |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 268 | } |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 269 | logger.Errorw(ctx, "failed-operation", log.Fields{"error": err, "device-id": agent.deviceID, "rpc": coreutils.GetRPCMetadataFromContext(ctx)}) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 270 | } |
| 271 | |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 272 | // onDeleteSuccess is a common callback for scenarios where we receive a nil response following a delete request |
| 273 | // to an adapter. |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 274 | func (agent *Agent) onDeleteSuccess(ctx context.Context, prevState, currState *common.AdminState_Types) { |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 275 | if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 276 | logger.Errorw(ctx, "delete-device-failure", log.Fields{"device-id": agent.deviceID, "error": err}) |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 277 | } |
| 278 | previousDeviceTransientState := agent.getTransientState() |
| 279 | newDevice := agent.cloneDeviceWithoutLock() |
| 280 | if err := agent.updateDeviceWithTransientStateAndReleaseLock(ctx, newDevice, |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 281 | core.DeviceTransientState_DELETING_POST_ADAPTER_RESPONSE, previousDeviceTransientState); err != nil { |
| 282 | logger.Errorw(ctx, "delete-device-failure", log.Fields{"device-id": agent.deviceID, "error": err}) |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 283 | } |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 284 | requestStatus := &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS} |
| 285 | desc := "adapter-response" |
| 286 | agent.logDeviceUpdate(ctx, prevState, currState, requestStatus, nil, desc) |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | // onDeleteFailure is a common callback for scenarios where we receive an error response following a delete request |
| 290 | // to an adapter and the only action required is to return the error response. |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 291 | func (agent *Agent) onDeleteFailure(ctx context.Context, err error, prevState, currState *common.AdminState_Types) { |
| 292 | logger.Errorw(ctx, "rpc-failed", log.Fields{"rpc": coreutils.GetRPCMetadataFromContext(ctx), "device-id": agent.deviceID, "error": err}) |
| 293 | |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 294 | //Only updating of transient state is required, no transition. |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 295 | if er := agent.updateTransientState(ctx, core.DeviceTransientState_DELETE_FAILED); er != nil { |
| 296 | logger.Errorw(ctx, "failed-to-update-transient-state-as-delete-failed", log.Fields{"device-id": agent.deviceID, "error": er}) |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 297 | } |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 298 | rpce := agent.deviceMgr.NewRPCEvent(ctx, agent.deviceID, err.Error(), nil) |
| 299 | go agent.deviceMgr.SendRPCEvent(ctx, "RPC_ERROR_RAISE_EVENT", rpce, |
| 300 | voltha.EventCategory_COMMUNICATION, nil, time.Now().Unix()) |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 301 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 302 | // Log the device update event |
| 303 | requestStatus := &common.OperationResp{Code: common.OperationResp_OPERATION_FAILURE} |
| 304 | desc := "adapter-response" |
| 305 | agent.logDeviceUpdate(ctx, prevState, currState, requestStatus, err, desc) |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 306 | } |
| 307 | |
Kent Hagerman | cba2f30 | 2020-07-28 13:37:36 -0400 | [diff] [blame] | 308 | // getDeviceReadOnly returns a device which MUST NOT be modified, but is safe to keep forever. |
| 309 | func (agent *Agent) getDeviceReadOnly(ctx context.Context) (*voltha.Device, error) { |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 310 | if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
| 311 | return nil, err |
| 312 | } |
| 313 | defer agent.requestQueue.RequestComplete() |
Kent Hagerman | cba2f30 | 2020-07-28 13:37:36 -0400 | [diff] [blame] | 314 | return agent.device, nil |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 315 | } |
| 316 | |
Kent Hagerman | cba2f30 | 2020-07-28 13:37:36 -0400 | [diff] [blame] | 317 | // 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] | 318 | // The device lock MUST be held by the caller. |
Kent Hagerman | cba2f30 | 2020-07-28 13:37:36 -0400 | [diff] [blame] | 319 | func (agent *Agent) getDeviceReadOnlyWithoutLock() *voltha.Device { |
khenaidoo | 0db4c81 | 2020-05-27 15:27:30 -0400 | [diff] [blame] | 320 | return agent.device |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 321 | } |
| 322 | |
Kent Hagerman | f6db9f1 | 2020-07-22 17:16:19 -0400 | [diff] [blame] | 323 | // cloneDeviceWithoutLock returns a copy of the device which is safe to modify. |
| 324 | // The device lock MUST be held by the caller. |
| 325 | func (agent *Agent) cloneDeviceWithoutLock() *voltha.Device { |
| 326 | return proto.Clone(agent.device).(*voltha.Device) |
| 327 | } |
| 328 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 329 | func (agent *Agent) updateDeviceTypeAndEndpoint(ctx context.Context) error { |
| 330 | if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
| 331 | return err |
| 332 | } |
| 333 | changed := false |
| 334 | cloned := agent.cloneDeviceWithoutLock() |
| 335 | if cloned.Type == "" { |
| 336 | adapterType, err := agent.adapterMgr.GetAdapterType(cloned.Type) |
| 337 | if err != nil { |
| 338 | agent.requestQueue.RequestComplete() |
| 339 | return err |
| 340 | } |
| 341 | cloned.Type = adapterType |
| 342 | changed = true |
| 343 | } |
| 344 | |
| 345 | if cloned.AdapterEndpoint == "" { |
| 346 | var err error |
| 347 | if cloned.AdapterEndpoint, err = agent.adapterMgr.GetAdapterEndpoint(ctx, cloned.Id, cloned.Type); err != nil { |
| 348 | agent.requestQueue.RequestComplete() |
| 349 | return err |
| 350 | } |
| 351 | agent.adapterEndpoint = cloned.AdapterEndpoint |
| 352 | changed = true |
| 353 | } |
| 354 | |
| 355 | if changed { |
| 356 | return agent.updateDeviceAndReleaseLock(ctx, cloned) |
| 357 | } |
| 358 | agent.requestQueue.RequestComplete() |
| 359 | return nil |
| 360 | } |
| 361 | |
khenaidoo | 3ab3488 | 2019-05-02 21:33:30 -0400 | [diff] [blame] | 362 | // enableDevice activates a preprovisioned or a disable device |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 363 | func (agent *Agent) enableDevice(ctx context.Context) error { |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 364 | //To preserve and use oldDevice state as prev state in new device |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 365 | var err error |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 366 | var desc string |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 367 | var prevAdminState, currAdminState common.AdminState_Types |
| 368 | requestStatus := &common.OperationResp{Code: common.OperationResp_OPERATION_FAILURE} |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 369 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 370 | defer func() { agent.logDeviceUpdate(ctx, &prevAdminState, &currAdminState, requestStatus, err, desc) }() |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 371 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 372 | if err = agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 373 | return err |
| 374 | } |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 375 | logger.Debugw(ctx, "enable-device", log.Fields{"device-id": agent.deviceID}) |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 376 | |
Kent Hagerman | cba2f30 | 2020-07-28 13:37:36 -0400 | [diff] [blame] | 377 | oldDevice := agent.getDeviceReadOnlyWithoutLock() |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 378 | prevAdminState = oldDevice.AdminState |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 379 | |
Maninder | 2195ccc | 2021-06-23 20:23:01 +0530 | [diff] [blame] | 380 | if !agent.proceedWithRequest(oldDevice) { |
| 381 | agent.requestQueue.RequestComplete() |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 382 | err = status.Errorf(codes.FailedPrecondition, "cannot complete operation as device deletion is in progress or reconciling is in progress/failed: %s", agent.deviceID) |
| 383 | return err |
Maninder | 2195ccc | 2021-06-23 20:23:01 +0530 | [diff] [blame] | 384 | } |
Mahir Gunyel | 92dd121 | 2021-10-22 11:42:56 -0700 | [diff] [blame^] | 385 | //vol-4275 TST meeting 08/04/2021: Let EnableDevice to be called again if device is in FAILED operational state, |
| 386 | //even the admin state is ENABLED. |
| 387 | if oldDevice.AdminState == voltha.AdminState_ENABLED && oldDevice.OperStatus != voltha.OperStatus_FAILED { |
Kent Hagerman | f6db9f1 | 2020-07-22 17:16:19 -0400 | [diff] [blame] | 388 | logger.Warnw(ctx, "device-already-enabled", log.Fields{"device-id": agent.deviceID}) |
| 389 | agent.requestQueue.RequestComplete() |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 390 | err = status.Errorf(codes.FailedPrecondition, fmt.Sprintf("cannot-enable-an-already-enabled-device: %s", oldDevice.Id)) |
Matteo Scandolo | d525ae3 | 2020-04-02 17:27:29 -0700 | [diff] [blame] | 391 | return err |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 392 | } |
| 393 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 394 | // Verify whether there is a device type that supports this device type |
| 395 | _, err = agent.adapterMgr.GetAdapterType(oldDevice.Type) |
| 396 | if err != nil { |
| 397 | agent.requestQueue.RequestComplete() |
| 398 | return err |
| 399 | } |
| 400 | |
| 401 | // Update device adapter endpoint if not set. This is set once by the Core and use as is by the adapters. E.g if this is a |
| 402 | // child device then the parent adapter will use this device's adapter endpoint (set here) to communicate with it. |
Kent Hagerman | f6db9f1 | 2020-07-22 17:16:19 -0400 | [diff] [blame] | 403 | newDevice := agent.cloneDeviceWithoutLock() |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 404 | if newDevice.AdapterEndpoint == "" { |
| 405 | if newDevice.AdapterEndpoint, err = agent.adapterMgr.GetAdapterEndpoint(ctx, newDevice.Id, newDevice.Type); err != nil { |
| 406 | agent.requestQueue.RequestComplete() |
| 407 | return err |
| 408 | } |
| 409 | agent.adapterEndpoint = newDevice.AdapterEndpoint |
| 410 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 411 | |
Kent Hagerman | f6db9f1 | 2020-07-22 17:16:19 -0400 | [diff] [blame] | 412 | // Update the Admin State and set the operational state to activating before sending the request to the Adapters |
| 413 | newDevice.AdminState = voltha.AdminState_ENABLED |
| 414 | newDevice.OperStatus = voltha.OperStatus_ACTIVATING |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 415 | |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 416 | // Adopt the device if it was in pre-provision state. In all other cases, try to re-enable it. |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 417 | client, err := agent.adapterMgr.GetAdapterClient(ctx, agent.adapterEndpoint) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 418 | if err != nil { |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 419 | logger.Errorw(ctx, "grpc-client-nil", |
| 420 | log.Fields{ |
| 421 | "error": err, |
| 422 | "device-id": agent.deviceID, |
| 423 | "device-type": agent.deviceType, |
| 424 | "adapter-endpoint": newDevice.AdapterEndpoint, |
| 425 | }) |
| 426 | agent.requestQueue.RequestComplete() |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 427 | return err |
| 428 | } |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 429 | subCtx, cancel := context.WithTimeout(coreutils.WithAllMetadataFromContext(ctx), agent.rpcTimeout) |
| 430 | requestStatus.Code = common.OperationResp_OPERATION_IN_PROGRESS |
| 431 | go func() { |
| 432 | defer cancel() |
| 433 | var err error |
| 434 | if oldDevice.AdminState == voltha.AdminState_PREPROVISIONED { |
| 435 | _, err = client.AdoptDevice(subCtx, newDevice) |
| 436 | } else { |
| 437 | _, err = client.ReEnableDevice(subCtx, newDevice) |
| 438 | } |
| 439 | if err == nil { |
| 440 | agent.onSuccess(subCtx, nil, nil, true) |
| 441 | } else { |
| 442 | agent.onFailure(subCtx, err, nil, nil, true) |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 443 | } |
| 444 | }() |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 445 | |
| 446 | // Update device |
| 447 | if err = agent.updateDeviceAndReleaseLock(ctx, newDevice); err != nil { |
| 448 | return err |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 449 | } |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 450 | currAdminState = newDevice.AdminState |
| 451 | return nil |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 452 | } |
| 453 | |
A R Karthick | 5c28f55 | 2019-12-11 22:47:44 -0800 | [diff] [blame] | 454 | //addFlowsAndGroups adds the "newFlows" and "newGroups" from the existing flows/groups and sends the update to the |
| 455 | //adapters |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 456 | 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] | 457 | var flwResponse, grpResponse coreutils.Response |
| 458 | var err error |
| 459 | //if new flow list is empty then the called function returns quickly |
| 460 | if flwResponse, err = agent.addFlowsToAdapter(ctx, newFlows, flowMetadata); err != nil { |
A R Karthick | 5c28f55 | 2019-12-11 22:47:44 -0800 | [diff] [blame] | 461 | return err |
| 462 | } |
Mahir Gunyel | 03de0d3 | 2020-06-03 01:36:59 -0700 | [diff] [blame] | 463 | //if new group list is empty then the called function returns quickly |
| 464 | if grpResponse, err = agent.addGroupsToAdapter(ctx, newGroups, flowMetadata); err != nil { |
| 465 | return err |
| 466 | } |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 467 | if errs := coreutils.WaitForNilOrErrorResponses(agent.rpcTimeout, flwResponse, grpResponse); errs != nil { |
| 468 | logger.Warnw(ctx, "adapter-response", log.Fields{"device-id": agent.deviceID, "result": errs}) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 469 | return status.Errorf(codes.Aborted, "flow-failure-device-%s", agent.deviceID) |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 470 | } |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 471 | return nil |
| 472 | } |
| 473 | |
A R Karthick | 5c28f55 | 2019-12-11 22:47:44 -0800 | [diff] [blame] | 474 | //deleteFlowsAndGroups removes the "flowsToDel" and "groupsToDel" from the existing flows/groups and sends the update to the |
| 475 | //adapters |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 476 | 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] | 477 | var flwResponse, grpResponse coreutils.Response |
| 478 | var err error |
| 479 | if flwResponse, err = agent.deleteFlowsFromAdapter(ctx, flowsToDel, flowMetadata); err != nil { |
A R Karthick | 5c28f55 | 2019-12-11 22:47:44 -0800 | [diff] [blame] | 480 | return err |
| 481 | } |
Mahir Gunyel | 03de0d3 | 2020-06-03 01:36:59 -0700 | [diff] [blame] | 482 | if grpResponse, err = agent.deleteGroupsFromAdapter(ctx, groupsToDel, flowMetadata); err != nil { |
| 483 | return err |
| 484 | } |
| 485 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 486 | if res := coreutils.WaitForNilOrErrorResponses(agent.rpcTimeout, flwResponse, grpResponse); res != nil { |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 487 | return status.Errorf(codes.Aborted, "errors-%s", res) |
| 488 | } |
| 489 | return nil |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 490 | } |
| 491 | |
A R Karthick | 5c28f55 | 2019-12-11 22:47:44 -0800 | [diff] [blame] | 492 | //updateFlowsAndGroups replaces the existing flows and groups with "updatedFlows" and "updatedGroups" respectively. It |
| 493 | //also sends the updates to the adapters |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 494 | 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] | 495 | var flwResponse, grpResponse coreutils.Response |
| 496 | var err error |
| 497 | if flwResponse, err = agent.updateFlowsToAdapter(ctx, updatedFlows, flowMetadata); err != nil { |
A R Karthick | 5c28f55 | 2019-12-11 22:47:44 -0800 | [diff] [blame] | 498 | return err |
| 499 | } |
Mahir Gunyel | 03de0d3 | 2020-06-03 01:36:59 -0700 | [diff] [blame] | 500 | if grpResponse, err = agent.updateGroupsToAdapter(ctx, updatedGroups, flowMetadata); err != nil { |
| 501 | return err |
| 502 | } |
| 503 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 504 | if res := coreutils.WaitForNilOrErrorResponses(agent.rpcTimeout, flwResponse, grpResponse); res != nil { |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 505 | return status.Errorf(codes.Aborted, "errors-%s", res) |
| 506 | } |
| 507 | return nil |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 508 | } |
| 509 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 510 | //disableDevice disable a device |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 511 | func (agent *Agent) disableDevice(ctx context.Context) error { |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 512 | var err error |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 513 | var desc string |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 514 | var prevAdminState, currAdminState common.AdminState_Types |
| 515 | requestStatus := &common.OperationResp{Code: common.OperationResp_OPERATION_FAILURE} |
| 516 | defer func() { agent.logDeviceUpdate(ctx, &prevAdminState, &currAdminState, requestStatus, err, desc) }() |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 517 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 518 | if err = agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 519 | return err |
| 520 | } |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 521 | logger.Debugw(ctx, "disable-device", log.Fields{"device-id": agent.deviceID}) |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 522 | |
Kent Hagerman | f6db9f1 | 2020-07-22 17:16:19 -0400 | [diff] [blame] | 523 | cloned := agent.cloneDeviceWithoutLock() |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 524 | prevAdminState = agent.device.AdminState |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 525 | |
Maninder | 2195ccc | 2021-06-23 20:23:01 +0530 | [diff] [blame] | 526 | if !agent.proceedWithRequest(cloned) { |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 527 | err = status.Errorf(codes.FailedPrecondition, "cannot complete operation as device deletion is in progress or reconciling is in progress/failed: %s", agent.deviceID) |
Maninder | 2195ccc | 2021-06-23 20:23:01 +0530 | [diff] [blame] | 528 | agent.requestQueue.RequestComplete() |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 529 | return err |
Maninder | 2195ccc | 2021-06-23 20:23:01 +0530 | [diff] [blame] | 530 | } |
| 531 | |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 532 | if cloned.AdminState == voltha.AdminState_DISABLED { |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 533 | desc = "device-already-disabled" |
Kent Hagerman | f6db9f1 | 2020-07-22 17:16:19 -0400 | [diff] [blame] | 534 | agent.requestQueue.RequestComplete() |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 535 | return nil |
| 536 | } |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 537 | if cloned.AdminState == voltha.AdminState_PREPROVISIONED { |
Kent Hagerman | f6db9f1 | 2020-07-22 17:16:19 -0400 | [diff] [blame] | 538 | agent.requestQueue.RequestComplete() |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 539 | err = status.Errorf(codes.FailedPrecondition, "deviceId:%s, invalid-admin-state:%s", agent.deviceID, cloned.AdminState) |
| 540 | return err |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 541 | } |
Maninder | 0aabf0c | 2021-03-17 14:55:14 +0530 | [diff] [blame] | 542 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 543 | // Update the Admin State and operational state before sending the request out |
Kent Hagerman | f6db9f1 | 2020-07-22 17:16:19 -0400 | [diff] [blame] | 544 | cloned.AdminState = voltha.AdminState_DISABLED |
| 545 | cloned.OperStatus = voltha.OperStatus_UNKNOWN |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 546 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 547 | client, err := agent.adapterMgr.GetAdapterClient(ctx, agent.adapterEndpoint) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 548 | if err != nil { |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 549 | logger.Errorw(ctx, "grpc-client-nil", |
| 550 | log.Fields{ |
| 551 | "error": err, |
| 552 | "device-id": agent.deviceID, |
| 553 | "device-type": agent.deviceType, |
| 554 | "adapter-endpoint": cloned.AdapterEndpoint, |
| 555 | }) |
| 556 | agent.requestQueue.RequestComplete() |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 557 | return err |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 558 | } |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 559 | subCtx, cancel := context.WithTimeout(coreutils.WithAllMetadataFromContext(ctx), agent.rpcTimeout) |
| 560 | requestStatus.Code = common.OperationResp_OPERATION_IN_PROGRESS |
| 561 | go func() { |
| 562 | defer cancel() |
| 563 | _, err := client.DisableDevice(subCtx, cloned) |
| 564 | if err == nil { |
| 565 | agent.onSuccess(subCtx, nil, nil, true) |
| 566 | } else { |
| 567 | agent.onFailure(subCtx, err, nil, nil, true) |
| 568 | } |
| 569 | }() |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 570 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 571 | // Update device |
| 572 | if err = agent.updateDeviceAndReleaseLock(ctx, cloned); err != nil { |
| 573 | return err |
| 574 | } |
| 575 | currAdminState = cloned.AdminState |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 576 | |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 577 | return nil |
| 578 | } |
| 579 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 580 | func (agent *Agent) rebootDevice(ctx context.Context) error { |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 581 | var desc string |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 582 | var err error |
| 583 | requestStatus := &common.OperationResp{Code: common.OperationResp_OPERATION_FAILURE} |
| 584 | defer func() { agent.logDeviceUpdate(ctx, nil, nil, requestStatus, err, desc) }() |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 585 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 586 | if err = agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 587 | desc = err.Error() |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 588 | return err |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 589 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 590 | defer agent.requestQueue.RequestComplete() |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 591 | logger.Debugw(ctx, "reboot-device", log.Fields{"device-id": agent.deviceID}) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 592 | |
Kent Hagerman | cba2f30 | 2020-07-28 13:37:36 -0400 | [diff] [blame] | 593 | device := agent.getDeviceReadOnlyWithoutLock() |
Maninder | 2195ccc | 2021-06-23 20:23:01 +0530 | [diff] [blame] | 594 | |
| 595 | if !agent.proceedWithRequest(device) { |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 596 | err = status.Errorf(codes.FailedPrecondition, "cannot complete operation as device deletion is in progress or reconciling is in progress/failed:%s", agent.deviceID) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 597 | return err |
| 598 | } |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 599 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 600 | client, err := agent.adapterMgr.GetAdapterClient(ctx, agent.adapterEndpoint) |
| 601 | if err != nil { |
| 602 | logger.Errorw(ctx, "grpc-client-nil", |
| 603 | log.Fields{ |
| 604 | "error": err, |
| 605 | "device-id": agent.deviceID, |
| 606 | "device-type": agent.deviceType, |
| 607 | "adapter-endpoint": device.AdapterEndpoint, |
| 608 | }) |
| 609 | return err |
| 610 | } |
| 611 | subCtx, cancel := context.WithTimeout(coreutils.WithAllMetadataFromContext(ctx), agent.rpcTimeout) |
| 612 | requestStatus.Code = common.OperationResp_OPERATION_IN_PROGRESS |
| 613 | go func() { |
| 614 | defer cancel() |
| 615 | _, err := client.RebootDevice(subCtx, device) |
| 616 | if err == nil { |
| 617 | agent.onSuccess(subCtx, nil, nil, true) |
| 618 | } else { |
| 619 | agent.onFailure(subCtx, err, nil, nil, true) |
| 620 | } |
| 621 | }() |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 622 | return nil |
| 623 | } |
| 624 | |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 625 | func (agent *Agent) deleteDeviceForce(ctx context.Context) error { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 626 | logger.Debugw(ctx, "delete-device-force", log.Fields{"device-id": agent.deviceID}) |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 627 | |
| 628 | var desc string |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 629 | var err error |
| 630 | requestStatus := &common.OperationResp{Code: common.OperationResp_OPERATION_FAILURE} |
| 631 | defer func() { agent.logDeviceUpdate(ctx, nil, nil, requestStatus, err, desc) }() |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 632 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 633 | if err = agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 634 | return err |
| 635 | } |
| 636 | // Get the device Transient state, return err if it is DELETING |
| 637 | previousDeviceTransientState := agent.getTransientState() |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 638 | device := agent.cloneDeviceWithoutLock() |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 639 | if !agent.isForceDeletingAllowed(previousDeviceTransientState, device) { |
| 640 | agent.requestQueue.RequestComplete() |
| 641 | err = status.Error(codes.FailedPrecondition, fmt.Sprintf("deviceId:%s, force deletion is in progress", agent.deviceID)) |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 642 | return err |
| 643 | } |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 644 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 645 | previousAdminState := device.AdminState |
| 646 | if previousAdminState != common.AdminState_PREPROVISIONED { |
| 647 | var client adapter_services.AdapterServiceClient |
| 648 | client, err = agent.adapterMgr.GetAdapterClient(ctx, agent.adapterEndpoint) |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 649 | if err != nil { |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 650 | logger.Errorw(ctx, "grpc-client-nil", |
| 651 | log.Fields{ |
| 652 | "error": err, |
| 653 | "device-id": agent.deviceID, |
| 654 | "device-type": agent.deviceType, |
| 655 | "adapter-endpoint": device.AdapterEndpoint, |
| 656 | }) |
| 657 | agent.requestQueue.RequestComplete() |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 658 | return err |
| 659 | } |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 660 | subCtx, cancel := context.WithTimeout(coreutils.WithAllMetadataFromContext(ctx), agent.rpcTimeout) |
| 661 | requestStatus.Code = common.OperationResp_OPERATION_IN_PROGRESS |
| 662 | go func() { |
| 663 | defer cancel() |
| 664 | _, err := client.DeleteDevice(subCtx, device) |
| 665 | if err == nil { |
| 666 | agent.onSuccess(subCtx, nil, nil, true) |
| 667 | } else { |
| 668 | agent.onFailure(subCtx, err, nil, nil, true) |
| 669 | } |
| 670 | }() |
| 671 | } |
| 672 | |
| 673 | // Update device |
| 674 | if err = agent.updateDeviceWithTransientStateAndReleaseLock(ctx, device, |
| 675 | core.DeviceTransientState_FORCE_DELETING, previousDeviceTransientState); err != nil { |
| 676 | return err |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 677 | } |
| 678 | return nil |
| 679 | } |
| 680 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 681 | func (agent *Agent) deleteDevice(ctx context.Context) error { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 682 | logger.Debugw(ctx, "delete-device", log.Fields{"device-id": agent.deviceID}) |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 683 | |
| 684 | var desc string |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 685 | var err error |
| 686 | requestStatus := &common.OperationResp{Code: common.OperationResp_OPERATION_FAILURE} |
| 687 | defer func() { agent.logDeviceUpdate(ctx, nil, nil, requestStatus, err, desc) }() |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 688 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 689 | if err = agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 690 | desc = err.Error() |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 691 | return err |
| 692 | } |
Maninder | 0aabf0c | 2021-03-17 14:55:14 +0530 | [diff] [blame] | 693 | |
Maninder | 2195ccc | 2021-06-23 20:23:01 +0530 | [diff] [blame] | 694 | device := agent.cloneDeviceWithoutLock() |
| 695 | |
| 696 | if !agent.proceedWithRequest(device) { |
Maninder | 0aabf0c | 2021-03-17 14:55:14 +0530 | [diff] [blame] | 697 | agent.requestQueue.RequestComplete() |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 698 | err = status.Errorf(codes.FailedPrecondition, "cannot complete operation as device deletion is in progress or reconciling is in progress/failed: %s", agent.deviceID) |
| 699 | return err |
Maninder | 0aabf0c | 2021-03-17 14:55:14 +0530 | [diff] [blame] | 700 | } |
| 701 | |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 702 | // Get the device Transient state, return err if it is DELETING |
| 703 | previousDeviceTransientState := agent.getTransientState() |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 704 | |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 705 | previousAdminState := device.AdminState |
| 706 | // Change the device transient state to DELETING_FROM_ADAPTER state till the device is removed from adapters. |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 707 | currentDeviceTransientState := core.DeviceTransientState_DELETING_FROM_ADAPTER |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 708 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 709 | if previousAdminState == common.AdminState_PREPROVISIONED { |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 710 | // Change the state to DELETING POST ADAPTER RESPONSE directly as adapters have no info of the device. |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 711 | currentDeviceTransientState = core.DeviceTransientState_DELETING_POST_ADAPTER_RESPONSE |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 712 | } |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 713 | // If the device was in pre-prov state (only parent device are in that state) then do not send the request to the |
| 714 | // adapter |
| 715 | if previousAdminState != common.AdminState_PREPROVISIONED { |
| 716 | var client adapter_services.AdapterServiceClient |
| 717 | client, err = agent.adapterMgr.GetAdapterClient(ctx, agent.adapterEndpoint) |
| 718 | if err != nil { |
| 719 | logger.Errorw(ctx, "grpc-client-nil", |
| 720 | log.Fields{ |
| 721 | "error": err, |
| 722 | "device-id": agent.deviceID, |
| 723 | "device-type": agent.deviceType, |
| 724 | "adapter-endpoint": device.AdapterEndpoint, |
| 725 | }) |
| 726 | agent.requestQueue.RequestComplete() |
| 727 | return err |
| 728 | } |
| 729 | subCtx, cancel := context.WithTimeout(coreutils.WithAllMetadataFromContext(ctx), agent.rpcTimeout) |
| 730 | requestStatus.Code = common.OperationResp_OPERATION_IN_PROGRESS |
| 731 | go func() { |
| 732 | defer cancel() |
| 733 | _, err := client.DeleteDevice(subCtx, device) |
| 734 | if err == nil { |
| 735 | agent.onDeleteSuccess(subCtx, nil, nil) |
| 736 | } else { |
| 737 | agent.onDeleteFailure(subCtx, err, nil, nil) |
| 738 | } |
| 739 | }() |
| 740 | } |
| 741 | |
| 742 | // Update device and release lock |
| 743 | if err = agent.updateDeviceWithTransientStateAndReleaseLock(ctx, device, |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 744 | currentDeviceTransientState, previousDeviceTransientState); err != nil { |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 745 | desc = err.Error() |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 746 | return err |
| 747 | } |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 748 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 749 | return nil |
| 750 | } |
| 751 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 752 | func (agent *Agent) setParentID(ctx context.Context, device *voltha.Device, parentID string) error { |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 753 | if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
| 754 | return err |
| 755 | } |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 756 | 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] | 757 | |
Kent Hagerman | f6db9f1 | 2020-07-22 17:16:19 -0400 | [diff] [blame] | 758 | cloned := agent.cloneDeviceWithoutLock() |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 759 | cloned.ParentId = parentID |
Kent Hagerman | f6db9f1 | 2020-07-22 17:16:19 -0400 | [diff] [blame] | 760 | return agent.updateDeviceAndReleaseLock(ctx, cloned) |
khenaidoo | ad06fd7 | 2019-10-28 12:26:05 -0400 | [diff] [blame] | 761 | } |
| 762 | |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 763 | // getSwitchCapability retrieves the switch capability of a parent device |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 764 | func (agent *Agent) getSwitchCapability(ctx context.Context) (*ic.SwitchCapability, error) { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 765 | logger.Debugw(ctx, "get-switch-capability", log.Fields{"device-id": agent.deviceID}) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 766 | |
Kent Hagerman | cba2f30 | 2020-07-28 13:37:36 -0400 | [diff] [blame] | 767 | device, err := agent.getDeviceReadOnly(ctx) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 768 | if err != nil { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 769 | return nil, err |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 770 | } |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 771 | |
| 772 | // Get the gRPC client |
| 773 | client, err := agent.adapterMgr.GetAdapterClient(ctx, agent.adapterEndpoint) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 774 | if err != nil { |
| 775 | return nil, err |
| 776 | } |
| 777 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 778 | return client.GetOfpDeviceInfo(ctx, device) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 779 | } |
| 780 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 781 | func (agent *Agent) onPacketFailure(ctx context.Context, err error, packet *ofp.OfpPacketOut) { |
| 782 | logger.Errorw(ctx, "packet-out-error", log.Fields{ |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 783 | "device-id": agent.deviceID, |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 784 | "error": err.Error(), |
| 785 | "packet": hex.EncodeToString(packet.Data), |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 786 | }) |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 787 | rpce := agent.deviceMgr.NewRPCEvent(ctx, agent.deviceID, err.Error(), nil) |
| 788 | go agent.deviceMgr.SendRPCEvent(ctx, "RPC_ERROR_RAISE_EVENT", rpce, |
| 789 | voltha.EventCategory_COMMUNICATION, nil, time.Now().Unix()) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 790 | } |
| 791 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 792 | 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] | 793 | if agent.deviceType == "" { |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 794 | agent.reconcileWithKVStore(ctx) |
Scott Baker | 8067860 | 2019-11-14 16:57:36 -0800 | [diff] [blame] | 795 | } |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 796 | // Send packet to adapter |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 797 | client, err := agent.adapterMgr.GetAdapterClient(ctx, agent.adapterEndpoint) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 798 | if err != nil { |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 799 | logger.Errorw(ctx, "grpc-client-nil", |
| 800 | log.Fields{ |
| 801 | "error": err, |
| 802 | "device-id": agent.deviceID, |
| 803 | "device-type": agent.deviceType, |
| 804 | }) |
| 805 | return err |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 806 | } |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 807 | subCtx, cancel := context.WithTimeout(coreutils.WithAllMetadataFromContext(ctx), agent.rpcTimeout) |
| 808 | go func() { |
| 809 | defer cancel() |
| 810 | _, err := client.SendPacketOut(subCtx, &ic.PacketOut{ |
| 811 | DeviceId: agent.deviceID, |
| 812 | EgressPortNo: outPort, |
| 813 | Packet: packet, |
| 814 | }) |
| 815 | if err == nil { |
| 816 | agent.onSuccess(subCtx, nil, nil, false) |
| 817 | } else { |
| 818 | agent.onPacketFailure(subCtx, err, packet) |
| 819 | } |
| 820 | }() |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 821 | return nil |
| 822 | } |
| 823 | |
Kent Hagerman | f6db9f1 | 2020-07-22 17:16:19 -0400 | [diff] [blame] | 824 | func (agent *Agent) updateDeviceUsingAdapterData(ctx context.Context, device *voltha.Device) error { |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 825 | var err error |
| 826 | var desc string |
| 827 | requestStatus := &common.OperationResp{Code: common.OperationResp_OPERATION_FAILURE} |
| 828 | defer func() { agent.logDeviceUpdate(ctx, nil, nil, requestStatus, err, desc) }() |
| 829 | |
| 830 | if err = agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
Kent Hagerman | f6db9f1 | 2020-07-22 17:16:19 -0400 | [diff] [blame] | 831 | return err |
| 832 | } |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 833 | 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] | 834 | |
| 835 | cloned := agent.cloneDeviceWithoutLock() |
Mahir Gunyel | 8e2707d | 2019-07-25 00:36:21 -0700 | [diff] [blame] | 836 | cloned.Root = device.Root |
| 837 | cloned.Vendor = device.Vendor |
| 838 | cloned.Model = device.Model |
| 839 | cloned.SerialNumber = device.SerialNumber |
| 840 | cloned.MacAddress = device.MacAddress |
| 841 | cloned.Vlan = device.Vlan |
| 842 | cloned.Reason = device.Reason |
Andrea Campanella | 025667e | 2021-01-14 11:50:07 +0100 | [diff] [blame] | 843 | cloned.ImageDownloads = device.ImageDownloads |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 844 | cloned.OperStatus = device.OperStatus |
| 845 | cloned.ConnectStatus = device.ConnectStatus |
| 846 | if err = agent.updateDeviceAndReleaseLock(ctx, cloned); err == nil { |
| 847 | requestStatus.Code = common.OperationResp_OPERATION_SUCCESS |
| 848 | } |
| 849 | return err |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 850 | } |
| 851 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 852 | func (agent *Agent) updateDeviceStatus(ctx context.Context, operStatus voltha.OperStatus_Types, connStatus voltha.ConnectStatus_Types) error { |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 853 | var err error |
| 854 | var desc string |
| 855 | opStatus := &common.OperationResp{Code: common.OperationResp_OPERATION_FAILURE} |
| 856 | defer func() { agent.logDeviceUpdate(ctx, nil, nil, opStatus, err, desc) }() |
| 857 | |
| 858 | if err = agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 859 | return err |
| 860 | } |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 861 | |
Kent Hagerman | f6db9f1 | 2020-07-22 17:16:19 -0400 | [diff] [blame] | 862 | cloned := agent.cloneDeviceWithoutLock() |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 863 | // 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] | 864 | if s, ok := voltha.ConnectStatus_Types_name[int32(connStatus)]; ok { |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 865 | logger.Debugw(ctx, "update-device-conn-status", log.Fields{"ok": ok, "val": s}) |
Kent Hagerman | f6db9f1 | 2020-07-22 17:16:19 -0400 | [diff] [blame] | 866 | cloned.ConnectStatus = connStatus |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 867 | } |
Kent Hagerman | f6db9f1 | 2020-07-22 17:16:19 -0400 | [diff] [blame] | 868 | if s, ok := voltha.OperStatus_Types_name[int32(operStatus)]; ok { |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 869 | logger.Debugw(ctx, "update-device-oper-status", log.Fields{"ok": ok, "val": s}) |
Kent Hagerman | f6db9f1 | 2020-07-22 17:16:19 -0400 | [diff] [blame] | 870 | cloned.OperStatus = operStatus |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 871 | } |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 872 | 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] | 873 | // Store the device |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 874 | if err = agent.updateDeviceAndReleaseLock(ctx, cloned); err == nil { |
| 875 | opStatus.Code = common.OperationResp_OPERATION_SUCCESS |
| 876 | } |
| 877 | return err |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 878 | } |
| 879 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 880 | // TODO: A generic device update by attribute |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 881 | func (agent *Agent) updateDeviceAttribute(ctx context.Context, name string, value interface{}) { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 882 | if value == nil { |
| 883 | return |
| 884 | } |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 885 | |
Kent Hagerman | f6db9f1 | 2020-07-22 17:16:19 -0400 | [diff] [blame] | 886 | if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
| 887 | logger.Warnw(ctx, "request-aborted", log.Fields{"device-id": agent.deviceID, "name": name, "error": err}) |
| 888 | return |
| 889 | } |
| 890 | |
| 891 | cloned := agent.cloneDeviceWithoutLock() |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 892 | updated := false |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 893 | s := reflect.ValueOf(cloned).Elem() |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 894 | if s.Kind() == reflect.Struct { |
| 895 | // exported field |
| 896 | f := s.FieldByName(name) |
| 897 | if f.IsValid() && f.CanSet() { |
| 898 | switch f.Kind() { |
| 899 | case reflect.String: |
| 900 | f.SetString(value.(string)) |
| 901 | updated = true |
| 902 | case reflect.Uint32: |
| 903 | f.SetUint(uint64(value.(uint32))) |
| 904 | updated = true |
| 905 | case reflect.Bool: |
| 906 | f.SetBool(value.(bool)) |
| 907 | updated = true |
| 908 | } |
| 909 | } |
| 910 | } |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 911 | 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] | 912 | // Save the data |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 913 | |
Kent Hagerman | f6db9f1 | 2020-07-22 17:16:19 -0400 | [diff] [blame] | 914 | if err := agent.updateDeviceAndReleaseLock(ctx, cloned); err != nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 915 | logger.Warnw(ctx, "attribute-update-failed", log.Fields{"attribute": name, "value": value}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 916 | } |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 917 | } |
serkant.uluderya | 334479d | 2019-04-10 08:26:15 -0700 | [diff] [blame] | 918 | |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 919 | func (agent *Agent) simulateAlarm(ctx context.Context, simulateReq *voltha.SimulateAlarmRequest) error { |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 920 | var err error |
| 921 | var desc string |
| 922 | requestStatus := &common.OperationResp{Code: common.OperationResp_OPERATION_FAILURE} |
| 923 | defer func() { agent.logDeviceUpdate(ctx, nil, nil, requestStatus, err, desc) }() |
| 924 | |
| 925 | if err = agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 926 | return err |
| 927 | } |
| 928 | defer agent.requestQueue.RequestComplete() |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 929 | logger.Debugw(ctx, "simulate-alarm", log.Fields{"device-id": agent.deviceID}) |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 930 | |
Kent Hagerman | cba2f30 | 2020-07-28 13:37:36 -0400 | [diff] [blame] | 931 | device := agent.getDeviceReadOnlyWithoutLock() |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 932 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 933 | client, err := agent.adapterMgr.GetAdapterClient(ctx, agent.adapterEndpoint) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 934 | if err != nil { |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 935 | logger.Errorw(ctx, "grpc-client-nil", |
| 936 | log.Fields{ |
| 937 | "error": err, |
| 938 | "device-id": agent.deviceID, |
| 939 | "device-type": agent.deviceType, |
| 940 | "adapter-endpoint": device.AdapterEndpoint, |
| 941 | }) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 942 | return err |
serkant.uluderya | 334479d | 2019-04-10 08:26:15 -0700 | [diff] [blame] | 943 | } |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 944 | subCtx, cancel := context.WithTimeout(coreutils.WithAllMetadataFromContext(ctx), agent.rpcTimeout) |
| 945 | requestStatus.Code = common.OperationResp_OPERATION_IN_PROGRESS |
| 946 | go func() { |
| 947 | defer cancel() |
| 948 | _, err := client.SimulateAlarm(subCtx, &ic.SimulateAlarmMessage{Device: device, Request: simulateReq}) |
| 949 | if err == nil { |
| 950 | agent.onSuccess(subCtx, nil, nil, false) |
| 951 | } else { |
| 952 | agent.onFailure(subCtx, err, nil, nil, false) |
| 953 | } |
| 954 | }() |
serkant.uluderya | 334479d | 2019-04-10 08:26:15 -0700 | [diff] [blame] | 955 | return nil |
| 956 | } |
Mahir Gunyel | b585167 | 2019-07-24 10:46:26 +0300 | [diff] [blame] | 957 | |
Kent Hagerman | f6db9f1 | 2020-07-22 17:16:19 -0400 | [diff] [blame] | 958 | // This function updates the device in the DB, releases the device lock, and runs any state transitions. |
| 959 | // The calling function MUST hold the device lock. The caller MUST NOT modify the device after this is called. |
| 960 | func (agent *Agent) updateDeviceAndReleaseLock(ctx context.Context, device *voltha.Device) error { |
| 961 | // fail early if this agent is no longer valid |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 962 | if agent.stopped { |
Kent Hagerman | f6db9f1 | 2020-07-22 17:16:19 -0400 | [diff] [blame] | 963 | agent.requestQueue.RequestComplete() |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 964 | return errors.New("device-agent-stopped") |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 965 | } |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 966 | |
Kent Hagerman | f6db9f1 | 2020-07-22 17:16:19 -0400 | [diff] [blame] | 967 | // update in db |
Kent Hagerman | f5a6735 | 2020-04-30 15:15:26 -0400 | [diff] [blame] | 968 | if err := agent.dbProxy.Set(ctx, agent.deviceID, device); err != nil { |
Kent Hagerman | f6db9f1 | 2020-07-22 17:16:19 -0400 | [diff] [blame] | 969 | agent.requestQueue.RequestComplete() |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 970 | 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] | 971 | } |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 972 | 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] | 973 | |
Kent Hagerman | 6031aad | 2020-07-29 16:36:33 -0400 | [diff] [blame] | 974 | prevDevice := agent.device |
Kent Hagerman | f6db9f1 | 2020-07-22 17:16:19 -0400 | [diff] [blame] | 975 | // update the device |
khenaidoo | 0db4c81 | 2020-05-27 15:27:30 -0400 | [diff] [blame] | 976 | agent.device = device |
Mahir Gunyel | b0343bf | 2021-05-11 14:14:26 -0700 | [diff] [blame] | 977 | //If any of the states has chenged, send the change event. |
| 978 | if prevDevice.OperStatus != device.OperStatus || prevDevice.ConnectStatus != device.ConnectStatus || prevDevice.AdminState != device.AdminState { |
| 979 | _ = agent.deviceMgr.Agent.SendDeviceStateChangeEvent(ctx, prevDevice.OperStatus, prevDevice.ConnectStatus, prevDevice.AdminState, device, time.Now().Unix()) |
| 980 | } |
Maninder | 0aabf0c | 2021-03-17 14:55:14 +0530 | [diff] [blame] | 981 | deviceTransientState := agent.getTransientState() |
| 982 | |
Kent Hagerman | f6db9f1 | 2020-07-22 17:16:19 -0400 | [diff] [blame] | 983 | // release lock before processing transition |
| 984 | agent.requestQueue.RequestComplete() |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 985 | subCtx := coreutils.WithSpanAndRPCMetadataFromContext(ctx) |
Kent Hagerman | f6db9f1 | 2020-07-22 17:16:19 -0400 | [diff] [blame] | 986 | |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 987 | if err := agent.deviceMgr.stateTransitions.ProcessTransition(subCtx, |
Maninder | 0aabf0c | 2021-03-17 14:55:14 +0530 | [diff] [blame] | 988 | device, prevDevice, deviceTransientState, deviceTransientState); err != nil { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 989 | logger.Errorw(ctx, "failed-process-transition", log.Fields{"device-id": device.Id, "previous-admin-state": prevDevice.AdminState, "current-admin-state": device.AdminState}) |
| 990 | // Sending RPC EVENT here |
| 991 | rpce := agent.deviceMgr.NewRPCEvent(ctx, agent.deviceID, err.Error(), nil) |
Himani Chawla | 606a4f0 | 2021-03-23 19:45:58 +0530 | [diff] [blame] | 992 | agent.deviceMgr.SendRPCEvent(ctx, "RPC_ERROR_RAISE_EVENT", rpce, voltha.EventCategory_COMMUNICATION, |
| 993 | nil, time.Now().Unix()) |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 994 | |
Kent Hagerman | f6db9f1 | 2020-07-22 17:16:19 -0400 | [diff] [blame] | 995 | } |
Mahir Gunyel | b585167 | 2019-07-24 10:46:26 +0300 | [diff] [blame] | 996 | return nil |
| 997 | } |
Mahir Gunyel | fdee921 | 2019-10-16 16:52:21 -0700 | [diff] [blame] | 998 | |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 999 | // This function updates the device transient in the DB through loader, releases the device lock, and runs any state transitions. |
| 1000 | // The calling function MUST hold the device lock. The caller MUST NOT modify the device after this is called. |
| 1001 | func (agent *Agent) updateDeviceWithTransientStateAndReleaseLock(ctx context.Context, device *voltha.Device, |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 1002 | transientState, prevTransientState core.DeviceTransientState_Types) error { |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 1003 | // fail early if this agent is no longer valid |
| 1004 | if agent.stopped { |
| 1005 | agent.requestQueue.RequestComplete() |
| 1006 | return errors.New("device-agent-stopped") |
| 1007 | } |
| 1008 | //update device TransientState |
| 1009 | if err := agent.updateTransientState(ctx, transientState); err != nil { |
| 1010 | agent.requestQueue.RequestComplete() |
| 1011 | return err |
| 1012 | } |
| 1013 | // update in db |
| 1014 | if err := agent.dbProxy.Set(ctx, agent.deviceID, device); err != nil { |
| 1015 | //Reverting TransientState update |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 1016 | if errTransient := agent.updateTransientState(ctx, prevTransientState); errTransient != nil { |
| 1017 | logger.Errorw(ctx, "failed-to-revert-transient-state-update-on-error", log.Fields{"device-id": device.Id, |
| 1018 | "previous-transient-state": prevTransientState, "current-transient-state": transientState, "error": errTransient}) |
| 1019 | } |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 1020 | agent.requestQueue.RequestComplete() |
| 1021 | return status.Errorf(codes.Internal, "failed-update-device:%s: %s", agent.deviceID, err) |
| 1022 | } |
| 1023 | |
| 1024 | logger.Debugw(ctx, "updated-device-in-store", log.Fields{"device-id: ": agent.deviceID}) |
| 1025 | |
| 1026 | prevDevice := agent.device |
| 1027 | // update the device |
| 1028 | agent.device = device |
Mahir Gunyel | b0343bf | 2021-05-11 14:14:26 -0700 | [diff] [blame] | 1029 | //If any of the states has chenged, send the change event. |
| 1030 | if prevDevice.OperStatus != device.OperStatus || prevDevice.ConnectStatus != device.ConnectStatus || prevDevice.AdminState != device.AdminState { |
| 1031 | _ = agent.deviceMgr.Agent.SendDeviceStateChangeEvent(ctx, prevDevice.OperStatus, prevDevice.ConnectStatus, prevDevice.AdminState, device, time.Now().Unix()) |
| 1032 | } |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 1033 | |
| 1034 | // release lock before processing transition |
| 1035 | agent.requestQueue.RequestComplete() |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 1036 | go func() { |
| 1037 | subCtx := coreutils.WithSpanAndRPCMetadataFromContext(ctx) |
| 1038 | if err := agent.deviceMgr.stateTransitions.ProcessTransition(subCtx, |
| 1039 | device, prevDevice, transientState, prevTransientState); err != nil { |
| 1040 | logger.Errorw(ctx, "failed-process-transition", log.Fields{"device-id": device.Id, "previous-admin-state": prevDevice.AdminState, "current-admin-state": device.AdminState}) |
| 1041 | // Sending RPC EVENT here |
| 1042 | rpce := agent.deviceMgr.NewRPCEvent(ctx, agent.deviceID, err.Error(), nil) |
| 1043 | agent.deviceMgr.SendRPCEvent(ctx, "RPC_ERROR_RAISE_EVENT", rpce, voltha.EventCategory_COMMUNICATION, |
| 1044 | nil, time.Now().Unix()) |
| 1045 | } |
| 1046 | }() |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 1047 | return nil |
| 1048 | } |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 1049 | func (agent *Agent) updateDeviceReason(ctx context.Context, reason string) error { |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 1050 | logger.Debugw(ctx, "update-device-reason", log.Fields{"device-id": agent.deviceID, "reason": reason}) |
| 1051 | |
| 1052 | var err error |
| 1053 | var desc string |
| 1054 | requestStatus := &common.OperationResp{Code: common.OperationResp_OPERATION_FAILURE} |
| 1055 | defer func() { agent.logDeviceUpdate(ctx, nil, nil, requestStatus, err, desc) }() |
| 1056 | |
| 1057 | if err = agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1058 | return err |
| 1059 | } |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 1060 | |
Kent Hagerman | f6db9f1 | 2020-07-22 17:16:19 -0400 | [diff] [blame] | 1061 | cloned := agent.cloneDeviceWithoutLock() |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1062 | cloned.Reason = reason |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 1063 | if err = agent.updateDeviceAndReleaseLock(ctx, cloned); err == nil { |
| 1064 | requestStatus.Code = common.OperationResp_OPERATION_SUCCESS |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 1065 | } |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 1066 | return err |
Mahir Gunyel | fdee921 | 2019-10-16 16:52:21 -0700 | [diff] [blame] | 1067 | } |
kesavand | bc2d162 | 2020-01-21 00:42:01 -0500 | [diff] [blame] | 1068 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 1069 | func (agent *Agent) ChildDeviceLost(ctx context.Context, device *voltha.Device) error { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 1070 | 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] | 1071 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 1072 | var err error |
| 1073 | var desc string |
| 1074 | requestStatus := &common.OperationResp{Code: common.OperationResp_OPERATION_FAILURE} |
| 1075 | defer func() { agent.logDeviceUpdate(ctx, nil, nil, requestStatus, err, desc) }() |
| 1076 | |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 1077 | // Remove the associated peer ports on the parent device |
| 1078 | for portID := range agent.portLoader.ListIDs() { |
| 1079 | if portHandle, have := agent.portLoader.Lock(portID); have { |
| 1080 | oldPort := portHandle.GetReadOnly() |
| 1081 | updatedPeers := make([]*voltha.Port_PeerPort, 0) |
| 1082 | for _, peerPort := range oldPort.Peers { |
| 1083 | if peerPort.DeviceId != device.Id { |
| 1084 | updatedPeers = append(updatedPeers, peerPort) |
| 1085 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1086 | } |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 1087 | newPort := *oldPort |
| 1088 | newPort.Peers = updatedPeers |
| 1089 | if err := portHandle.Update(ctx, &newPort); err != nil { |
| 1090 | portHandle.Unlock() |
| 1091 | return nil |
| 1092 | } |
| 1093 | portHandle.Unlock() |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1094 | } |
Chaitrashree G S | 543df3e | 2020-02-24 22:36:54 -0500 | [diff] [blame] | 1095 | } |
| 1096 | |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1097 | //send request to adapter |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 1098 | client, err := agent.adapterMgr.GetAdapterClient(ctx, agent.adapterEndpoint) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1099 | if err != nil { |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 1100 | logger.Errorw(ctx, "grpc-client-nil", |
| 1101 | log.Fields{ |
| 1102 | "error": err, |
| 1103 | "device-id": agent.deviceID, |
| 1104 | "device-type": agent.deviceType, |
| 1105 | "adapter-endpoint": device.AdapterEndpoint, |
| 1106 | }) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1107 | return err |
Chaitrashree G S | 543df3e | 2020-02-24 22:36:54 -0500 | [diff] [blame] | 1108 | } |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 1109 | subCtx, cancel := context.WithTimeout(coreutils.WithAllMetadataFromContext(ctx), agent.rpcTimeout) |
| 1110 | requestStatus.Code = common.OperationResp_OPERATION_IN_PROGRESS |
| 1111 | go func() { |
| 1112 | defer cancel() |
| 1113 | _, err := client.ChildDeviceLost(subCtx, device) |
| 1114 | if err == nil { |
| 1115 | agent.onSuccess(subCtx, nil, nil, true) |
| 1116 | } else { |
| 1117 | agent.onFailure(subCtx, err, nil, nil, true) |
| 1118 | } |
| 1119 | }() |
Chaitrashree G S | 543df3e | 2020-02-24 22:36:54 -0500 | [diff] [blame] | 1120 | return nil |
Chaitrashree G S | 543df3e | 2020-02-24 22:36:54 -0500 | [diff] [blame] | 1121 | } |
onkarkundargi | 8728525 | 2020-01-27 11:34:52 +0530 | [diff] [blame] | 1122 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 1123 | func (agent *Agent) startOmciTest(ctx context.Context, omcitestrequest *voltha.OmciTestRequest) (*voltha.TestResponse, error) { |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 1124 | var err error |
| 1125 | var desc string |
| 1126 | requestStatus := &common.OperationResp{Code: common.OperationResp_OPERATION_FAILURE} |
| 1127 | defer func() { agent.logDeviceUpdate(ctx, nil, nil, requestStatus, err, desc) }() |
onkarkundargi | 8728525 | 2020-01-27 11:34:52 +0530 | [diff] [blame] | 1128 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 1129 | // OMCI test may be performed on a pre-provisioned device. If a device is in that state both its device type and endpoint |
| 1130 | // may not have been set yet. |
| 1131 | // First check if we need to update the type or endpoint |
| 1132 | cloned, err := agent.getDeviceReadOnly(ctx) |
onkarkundargi | 8728525 | 2020-01-27 11:34:52 +0530 | [diff] [blame] | 1133 | if err != nil { |
| 1134 | return nil, err |
| 1135 | } |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 1136 | if cloned.Type == "" || cloned.AdapterEndpoint == "" { |
| 1137 | if err = agent.updateDeviceTypeAndEndpoint(ctx); err != nil { |
| 1138 | return nil, err |
| 1139 | } |
| 1140 | cloned, err = agent.getDeviceReadOnly(ctx) |
| 1141 | if err != nil { |
| 1142 | return nil, err |
| 1143 | } |
onkarkundargi | 8728525 | 2020-01-27 11:34:52 +0530 | [diff] [blame] | 1144 | } |
| 1145 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 1146 | // Send request to the adapter |
| 1147 | client, err := agent.adapterMgr.GetAdapterClient(ctx, agent.adapterEndpoint) |
| 1148 | if err != nil { |
| 1149 | logger.Errorw(ctx, "grpc-client-nil", |
| 1150 | log.Fields{ |
| 1151 | "error": err, |
| 1152 | "device-id": agent.deviceID, |
| 1153 | "device-type": agent.deviceType, |
| 1154 | "adapter-endpoint": cloned.AdapterEndpoint, |
| 1155 | }) |
| 1156 | return nil, err |
onkarkundargi | 8728525 | 2020-01-27 11:34:52 +0530 | [diff] [blame] | 1157 | } |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 1158 | |
| 1159 | res, err := client.StartOmciTest(ctx, &ic.OMCITest{ |
| 1160 | Device: cloned, |
| 1161 | Request: omcitestrequest, |
| 1162 | }) |
| 1163 | if err == nil { |
| 1164 | requestStatus.Code = common.OperationResp_OPERATION_SUCCESS |
| 1165 | } |
| 1166 | return res, err |
onkarkundargi | 8728525 | 2020-01-27 11:34:52 +0530 | [diff] [blame] | 1167 | } |
Dinesh Belwalkar | c1129f1 | 2020-02-27 10:41:33 -0800 | [diff] [blame] | 1168 | |
| 1169 | 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] | 1170 | logger.Debugw(ctx, "get-ext-value", log.Fields{"device-id": agent.deviceID, "onu-id": valueparam.Id, "value-type": valueparam.Value}) |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 1171 | var err error |
| 1172 | var desc string |
| 1173 | requestStatus := &common.OperationResp{Code: common.OperationResp_OPERATION_FAILURE} |
| 1174 | defer func() { agent.logDeviceUpdate(ctx, nil, nil, requestStatus, err, desc) }() |
| 1175 | |
| 1176 | if err = agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
Dinesh Belwalkar | c1129f1 | 2020-02-27 10:41:33 -0800 | [diff] [blame] | 1177 | return nil, err |
| 1178 | } |
| 1179 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 1180 | //send request to adapter synchronously |
| 1181 | client, err := agent.adapterMgr.GetAdapterClient(ctx, pdevice.AdapterEndpoint) |
Dinesh Belwalkar | c1129f1 | 2020-02-27 10:41:33 -0800 | [diff] [blame] | 1182 | if err != nil { |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 1183 | logger.Errorw(ctx, "grpc-client-nil", |
| 1184 | log.Fields{ |
| 1185 | "error": err, |
| 1186 | "device-id": agent.deviceID, |
| 1187 | "device-type": agent.deviceType, |
| 1188 | "adapter-endpoint": pdevice.AdapterEndpoint, |
| 1189 | }) |
| 1190 | agent.requestQueue.RequestComplete() |
Dinesh Belwalkar | c1129f1 | 2020-02-27 10:41:33 -0800 | [diff] [blame] | 1191 | return nil, err |
| 1192 | } |
| 1193 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 1194 | // Release lock before sending to adapter |
| 1195 | agent.requestQueue.RequestComplete() |
Dinesh Belwalkar | c1129f1 | 2020-02-27 10:41:33 -0800 | [diff] [blame] | 1196 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 1197 | retVal, err := client.GetExtValue(ctx, &ic.GetExtValueMessage{ |
| 1198 | ParentDevice: pdevice, |
| 1199 | ChildDevice: cdevice, |
| 1200 | ValueType: valueparam.Value, |
| 1201 | }) |
| 1202 | if err == nil { |
| 1203 | requestStatus.Code = common.OperationResp_OPERATION_SUCCESS |
Dinesh Belwalkar | c1129f1 | 2020-02-27 10:41:33 -0800 | [diff] [blame] | 1204 | } |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 1205 | return retVal, err |
Dinesh Belwalkar | c1129f1 | 2020-02-27 10:41:33 -0800 | [diff] [blame] | 1206 | } |
dpaul | 6268631 | 2020-06-23 14:17:36 +0530 | [diff] [blame] | 1207 | |
| 1208 | 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] | 1209 | logger.Debugw(ctx, "set-ext-value", log.Fields{"device-id": value.Id}) |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 1210 | |
| 1211 | var err error |
| 1212 | var desc string |
| 1213 | requestStatus := &common.OperationResp{Code: common.OperationResp_OPERATION_FAILURE} |
| 1214 | defer func() { agent.logDeviceUpdate(ctx, nil, nil, requestStatus, err, desc) }() |
| 1215 | |
| 1216 | if err = agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
dpaul | 6268631 | 2020-06-23 14:17:36 +0530 | [diff] [blame] | 1217 | return nil, err |
| 1218 | } |
| 1219 | |
| 1220 | //send request to adapter |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 1221 | //send request to adapter synchronously |
| 1222 | client, err := agent.adapterMgr.GetAdapterClient(ctx, agent.adapterEndpoint) |
dpaul | 6268631 | 2020-06-23 14:17:36 +0530 | [diff] [blame] | 1223 | if err != nil { |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 1224 | logger.Errorw(ctx, "grpc-client-nil", |
| 1225 | log.Fields{ |
| 1226 | "error": err, |
| 1227 | "device-id": agent.deviceID, |
| 1228 | "device-type": agent.deviceType, |
| 1229 | "adapter-endpoint": device.AdapterEndpoint, |
| 1230 | }) |
| 1231 | agent.requestQueue.RequestComplete() |
dpaul | 6268631 | 2020-06-23 14:17:36 +0530 | [diff] [blame] | 1232 | return nil, err |
| 1233 | } |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 1234 | // Release lock before sending request to adapter |
| 1235 | agent.requestQueue.RequestComplete() |
dpaul | 6268631 | 2020-06-23 14:17:36 +0530 | [diff] [blame] | 1236 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 1237 | retVal, err := client.SetExtValue(ctx, &ic.SetExtValueMessage{ |
| 1238 | Device: device, |
| 1239 | Value: value, |
| 1240 | }) |
| 1241 | if err == nil { |
| 1242 | requestStatus.Code = common.OperationResp_OPERATION_SUCCESS |
dpaul | 6268631 | 2020-06-23 14:17:36 +0530 | [diff] [blame] | 1243 | } |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 1244 | return retVal, err |
dpaul | 6268631 | 2020-06-23 14:17:36 +0530 | [diff] [blame] | 1245 | } |
Salman Siddiqui | 1cf9504 | 2020-11-19 00:42:56 +0530 | [diff] [blame] | 1246 | |
| 1247 | 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] | 1248 | logger.Debugw(ctx, "get-single-value", log.Fields{"device-id": request.TargetId}) |
Salman Siddiqui | 1cf9504 | 2020-11-19 00:42:56 +0530 | [diff] [blame] | 1249 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 1250 | var err error |
| 1251 | var desc string |
| 1252 | requestStatus := &common.OperationResp{Code: common.OperationResp_OPERATION_FAILURE} |
| 1253 | defer func() { agent.logDeviceUpdate(ctx, nil, nil, requestStatus, err, desc) }() |
| 1254 | |
| 1255 | if err = agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
Salman Siddiqui | 1cf9504 | 2020-11-19 00:42:56 +0530 | [diff] [blame] | 1256 | return nil, err |
| 1257 | } |
| 1258 | |
| 1259 | cloned := agent.cloneDeviceWithoutLock() |
| 1260 | |
| 1261 | //send request to adapter |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 1262 | client, err := agent.adapterMgr.GetAdapterClient(ctx, agent.adapterEndpoint) |
Salman Siddiqui | 1cf9504 | 2020-11-19 00:42:56 +0530 | [diff] [blame] | 1263 | if err != nil { |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 1264 | logger.Errorw(ctx, "grpc-client-nil", |
| 1265 | log.Fields{ |
| 1266 | "error": err, |
| 1267 | "device-id": cloned.Id, |
| 1268 | "adapter-endpoint": cloned.AdapterEndpoint, |
| 1269 | }) |
| 1270 | agent.requestQueue.RequestComplete() |
Salman Siddiqui | 1cf9504 | 2020-11-19 00:42:56 +0530 | [diff] [blame] | 1271 | return nil, err |
| 1272 | } |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 1273 | // Release lock before sending request to adapter |
| 1274 | agent.requestQueue.RequestComplete() |
Salman Siddiqui | 1cf9504 | 2020-11-19 00:42:56 +0530 | [diff] [blame] | 1275 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 1276 | resp, err := client.GetSingleValue(ctx, request) |
| 1277 | if err == nil { |
| 1278 | requestStatus.Code = common.OperationResp_OPERATION_SUCCESS |
Salman Siddiqui | 1cf9504 | 2020-11-19 00:42:56 +0530 | [diff] [blame] | 1279 | } |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 1280 | return resp, err |
Salman Siddiqui | 1cf9504 | 2020-11-19 00:42:56 +0530 | [diff] [blame] | 1281 | } |
| 1282 | |
| 1283 | 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] | 1284 | logger.Debugw(ctx, "set-single-value", log.Fields{"device-id": request.TargetId}) |
Salman Siddiqui | 1cf9504 | 2020-11-19 00:42:56 +0530 | [diff] [blame] | 1285 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 1286 | var err error |
| 1287 | var desc string |
| 1288 | requestStatus := &common.OperationResp{Code: common.OperationResp_OPERATION_FAILURE} |
| 1289 | defer func() { agent.logDeviceUpdate(ctx, nil, nil, requestStatus, err, desc) }() |
| 1290 | |
Salman Siddiqui | 1cf9504 | 2020-11-19 00:42:56 +0530 | [diff] [blame] | 1291 | if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
| 1292 | return nil, err |
| 1293 | } |
| 1294 | |
| 1295 | cloned := agent.cloneDeviceWithoutLock() |
| 1296 | |
| 1297 | //send request to adapter |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 1298 | client, err := agent.adapterMgr.GetAdapterClient(ctx, agent.adapterEndpoint) |
Salman Siddiqui | 1cf9504 | 2020-11-19 00:42:56 +0530 | [diff] [blame] | 1299 | if err != nil { |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 1300 | logger.Errorw(ctx, "grpc-client-nil", |
| 1301 | log.Fields{ |
| 1302 | "error": err, |
| 1303 | "device-id": agent.deviceID, |
| 1304 | "device-type": agent.deviceType, |
| 1305 | "adapter-endpoint": cloned.AdapterEndpoint, |
| 1306 | }) |
| 1307 | agent.requestQueue.RequestComplete() |
Salman Siddiqui | 1cf9504 | 2020-11-19 00:42:56 +0530 | [diff] [blame] | 1308 | return nil, err |
| 1309 | } |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 1310 | // Release lock before sending request to adapter |
| 1311 | agent.requestQueue.RequestComplete() |
Salman Siddiqui | 1cf9504 | 2020-11-19 00:42:56 +0530 | [diff] [blame] | 1312 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 1313 | resp, err := client.SetSingleValue(ctx, request) |
| 1314 | if err == nil { |
| 1315 | requestStatus.Code = common.OperationResp_OPERATION_SUCCESS |
Salman Siddiqui | 1cf9504 | 2020-11-19 00:42:56 +0530 | [diff] [blame] | 1316 | } |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 1317 | return resp, err |
Salman Siddiqui | 1cf9504 | 2020-11-19 00:42:56 +0530 | [diff] [blame] | 1318 | } |
Maninder | 0aabf0c | 2021-03-17 14:55:14 +0530 | [diff] [blame] | 1319 | |
Maninder | 2195ccc | 2021-06-23 20:23:01 +0530 | [diff] [blame] | 1320 | func (agent *Agent) proceedWithRequest(device *voltha.Device) bool { |
| 1321 | return !agent.isDeletionInProgress() && !agent.isInReconcileState(device) |
Maninder | 0aabf0c | 2021-03-17 14:55:14 +0530 | [diff] [blame] | 1322 | } |
| 1323 | |
| 1324 | func (agent *Agent) stopReconcile() { |
| 1325 | agent.stopReconcilingMutex.Lock() |
| 1326 | if agent.stopReconciling != nil { |
| 1327 | agent.stopReconciling <- 0 |
| 1328 | } |
| 1329 | agent.stopReconcilingMutex.Unlock() |
| 1330 | } |
| 1331 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 1332 | // abortAllProcessing is invoked when an adapter managing this device is restarted |
| 1333 | func (agent *Agent) abortAllProcessing(ctx context.Context) error { |
| 1334 | logger.Infow(ctx, "aborting-current-running-requests", log.Fields{"device-id": agent.deviceID}) |
| 1335 | if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
| 1336 | return err |
| 1337 | } |
| 1338 | defer agent.requestQueue.RequestComplete() |
| 1339 | |
| 1340 | // If any reconciling is in progress just abort it. The adapter is gone. |
| 1341 | agent.stopReconcile() |
| 1342 | |
| 1343 | // Update the Core device transient state accordingly |
| 1344 | var updatedState core.DeviceTransientState_Types |
| 1345 | switch agent.getTransientState() { |
| 1346 | case core.DeviceTransientState_RECONCILE_IN_PROGRESS: |
| 1347 | updatedState = core.DeviceTransientState_NONE |
| 1348 | case core.DeviceTransientState_FORCE_DELETING: |
| 1349 | updatedState = core.DeviceTransientState_DELETE_FAILED |
| 1350 | case core.DeviceTransientState_DELETING_FROM_ADAPTER: |
| 1351 | updatedState = core.DeviceTransientState_DELETE_FAILED |
| 1352 | default: |
| 1353 | updatedState = core.DeviceTransientState_NONE |
| 1354 | } |
| 1355 | if err := agent.updateTransientState(ctx, updatedState); err != nil { |
| 1356 | logger.Errorf(ctx, "transient-state-update-failed", log.Fields{"error": err}) |
| 1357 | return err |
| 1358 | } |
| 1359 | return nil |
| 1360 | } |
| 1361 | |
| 1362 | func (agent *Agent) ReconcileDevice(ctx context.Context) { |
| 1363 | requestStatus := &common.OperationResp{Code: common.OperationResp_OPERATION_FAILURE} |
Maninder | 0aabf0c | 2021-03-17 14:55:14 +0530 | [diff] [blame] | 1364 | var desc string |
Maninder | 0aabf0c | 2021-03-17 14:55:14 +0530 | [diff] [blame] | 1365 | |
| 1366 | if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 1367 | agent.logDeviceUpdate(ctx, nil, nil, requestStatus, err, desc) |
| 1368 | return |
| 1369 | } |
| 1370 | |
| 1371 | device := agent.getDeviceReadOnlyWithoutLock() |
| 1372 | if device.AdminState == voltha.AdminState_PREPROVISIONED { |
| 1373 | agent.requestQueue.RequestComplete() |
| 1374 | logger.Debugw(ctx, "device-in-preprovisioning-state-reconcile-not-needed", log.Fields{"device-id": device.Id}) |
Maninder | 0aabf0c | 2021-03-17 14:55:14 +0530 | [diff] [blame] | 1375 | return |
| 1376 | } |
| 1377 | |
Maninder | 2195ccc | 2021-06-23 20:23:01 +0530 | [diff] [blame] | 1378 | if !agent.proceedWithRequest(device) { |
Maninder | 0aabf0c | 2021-03-17 14:55:14 +0530 | [diff] [blame] | 1379 | agent.requestQueue.RequestComplete() |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 1380 | err := fmt.Errorf("cannot complete operation as device deletion/reconciling is in progress or reconcile failed for device : %s", device.Id) |
| 1381 | logger.Errorw(ctx, "reconcile-failed", log.Fields{"error": err}) |
| 1382 | agent.logDeviceUpdate(ctx, nil, nil, requestStatus, err, desc) |
Maninder | 0aabf0c | 2021-03-17 14:55:14 +0530 | [diff] [blame] | 1383 | return |
| 1384 | } |
| 1385 | |
| 1386 | //set transient state to RECONCILE IN PROGRESS |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 1387 | err := agent.updateTransientState(ctx, core.DeviceTransientState_RECONCILE_IN_PROGRESS) |
Maninder | 0aabf0c | 2021-03-17 14:55:14 +0530 | [diff] [blame] | 1388 | if err != nil { |
| 1389 | agent.requestQueue.RequestComplete() |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 1390 | logger.Errorw(ctx, "setting-transient-state-failed", log.Fields{"error": err}) |
| 1391 | agent.logDeviceUpdate(ctx, nil, nil, requestStatus, nil, desc) |
Maninder | 0aabf0c | 2021-03-17 14:55:14 +0530 | [diff] [blame] | 1392 | return |
| 1393 | } |
| 1394 | |
Maninder | 0aabf0c | 2021-03-17 14:55:14 +0530 | [diff] [blame] | 1395 | reconcilingBackoff := backoff.NewExponentialBackOff() |
| 1396 | reconcilingBackoff.InitialInterval = agent.config.BackoffRetryInitialInterval |
| 1397 | reconcilingBackoff.MaxElapsedTime = agent.config.BackoffRetryMaxElapsedTime |
| 1398 | reconcilingBackoff.MaxInterval = agent.config.BackoffRetryMaxInterval |
| 1399 | |
| 1400 | //making here to keep lifecycle of this channel within the scope of retryReconcile |
| 1401 | agent.stopReconcilingMutex.Lock() |
| 1402 | agent.stopReconciling = make(chan int) |
| 1403 | agent.stopReconcilingMutex.Unlock() |
| 1404 | |
David K. Bainbridge | 482e442 | 2021-06-30 12:23:42 -0700 | [diff] [blame] | 1405 | // defined outside the retry loop so it can be cleaned |
| 1406 | // up when the loop breaks |
| 1407 | var backoffTimer *time.Timer |
| 1408 | |
| 1409 | retry: |
Maninder | 0aabf0c | 2021-03-17 14:55:14 +0530 | [diff] [blame] | 1410 | for { |
David K. Bainbridge | 482e442 | 2021-06-30 12:23:42 -0700 | [diff] [blame] | 1411 | // If the operations state of the device is RECONCILING_FAILED then we do not |
| 1412 | // want to continue to attempt reconciliation. |
| 1413 | deviceRef := agent.getDeviceReadOnlyWithoutLock() |
| 1414 | if deviceRef.OperStatus == common.OperStatus_RECONCILING_FAILED { |
| 1415 | logger.Warnw(ctx, "reconciling-failed-halting-retries", |
| 1416 | log.Fields{"device-id": device.Id}) |
| 1417 | agent.requestQueue.RequestComplete() |
| 1418 | break retry |
| 1419 | } |
| 1420 | |
Maninder | 0aabf0c | 2021-03-17 14:55:14 +0530 | [diff] [blame] | 1421 | // Use an exponential back off to prevent getting into a tight loop |
| 1422 | duration := reconcilingBackoff.NextBackOff() |
| 1423 | //This case should never occur in default case as max elapsed time for backoff is 0(by default) , so it will never return stop |
| 1424 | if duration == backoff.Stop { |
| 1425 | // If we reach a maximum then warn and reset the backoff |
| 1426 | // timer and keep attempting. |
| 1427 | logger.Warnw(ctx, "maximum-reconciling-backoff-reached--resetting-backoff-timer", |
| 1428 | log.Fields{"max-reconciling-backoff": reconcilingBackoff.MaxElapsedTime, |
| 1429 | "device-id": device.Id}) |
| 1430 | reconcilingBackoff.Reset() |
| 1431 | duration = reconcilingBackoff.NextBackOff() |
| 1432 | } |
| 1433 | |
David K. Bainbridge | 482e442 | 2021-06-30 12:23:42 -0700 | [diff] [blame] | 1434 | backoffTimer = time.NewTimer(duration) |
Maninder | 0aabf0c | 2021-03-17 14:55:14 +0530 | [diff] [blame] | 1435 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 1436 | logger.Debugw(ctx, "retrying-reconciling", log.Fields{"deviceID": device.Id, "endpoint": device.AdapterEndpoint}) |
| 1437 | // Release lock before sending request to adapter |
Maninder | 0aabf0c | 2021-03-17 14:55:14 +0530 | [diff] [blame] | 1438 | agent.requestQueue.RequestComplete() |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 1439 | |
| 1440 | // Send a reconcile request to the adapter. |
| 1441 | err := agent.sendReconcileRequestToAdapter(ctx, device) |
| 1442 | if errors.Is(err, errContextExpired) || errors.Is(err, errReconcileAborted) { |
| 1443 | logger.Errorw(ctx, "reconcile-aborted", log.Fields{"error": err}) |
| 1444 | requestStatus = &common.OperationResp{Code: common.OperationResp_OperationReturnCode(common.OperStatus_FAILED)} |
| 1445 | desc = "aborted" |
| 1446 | agent.logDeviceUpdate(ctx, nil, nil, requestStatus, err, desc) |
| 1447 | break retry |
| 1448 | } |
Maninder | 0aabf0c | 2021-03-17 14:55:14 +0530 | [diff] [blame] | 1449 | if err != nil { |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 1450 | agent.logDeviceUpdate(ctx, nil, nil, requestStatus, err, desc) |
Maninder | 0aabf0c | 2021-03-17 14:55:14 +0530 | [diff] [blame] | 1451 | <-backoffTimer.C |
| 1452 | // backoffTimer expired continue |
| 1453 | // Take lock back before retrying |
| 1454 | if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 1455 | agent.logDeviceUpdate(ctx, nil, nil, requestStatus, err, desc) |
David K. Bainbridge | 482e442 | 2021-06-30 12:23:42 -0700 | [diff] [blame] | 1456 | break retry |
Maninder | 0aabf0c | 2021-03-17 14:55:14 +0530 | [diff] [blame] | 1457 | } |
| 1458 | continue |
| 1459 | } |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 1460 | // Success |
| 1461 | requestStatus = &common.OperationResp{Code: common.OperationResp_OPERATION_SUCCESS} |
| 1462 | desc = "adapter-response" |
| 1463 | agent.logDeviceUpdate(ctx, nil, nil, requestStatus, err, desc) |
| 1464 | break retry |
David K. Bainbridge | 482e442 | 2021-06-30 12:23:42 -0700 | [diff] [blame] | 1465 | } |
| 1466 | |
| 1467 | // Retry loop is broken, so stop any timers and drain the channel |
| 1468 | if backoffTimer != nil && !backoffTimer.Stop() { |
| 1469 | |
| 1470 | // As per documentation and stack overflow when a timer is stopped its |
| 1471 | // channel should be drained. The issue is that Stop returns false |
| 1472 | // either if the timer has already been fired "OR" if the timer can be |
| 1473 | // stopped before being fired. This means that in some cases the |
| 1474 | // channel has already be emptied so attempting to read from it means |
| 1475 | // a blocked thread. To get around this use a select so if the |
| 1476 | // channel is already empty the default case hits and we are not |
| 1477 | // blocked. |
| 1478 | select { |
| 1479 | case <-backoffTimer.C: |
| 1480 | default: |
Maninder | 0aabf0c | 2021-03-17 14:55:14 +0530 | [diff] [blame] | 1481 | } |
| 1482 | } |
| 1483 | } |
| 1484 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 1485 | func (agent *Agent) sendReconcileRequestToAdapter(ctx context.Context, device *voltha.Device) error { |
| 1486 | logger.Debugw(ctx, "sending-reconcile-to-adapter", log.Fields{"device-id": device.Id, "endpoint": agent.adapterEndpoint}) |
| 1487 | client, err := agent.adapterMgr.GetAdapterClient(ctx, agent.adapterEndpoint) |
| 1488 | if err != nil { |
| 1489 | return err |
| 1490 | } |
| 1491 | adapterResponse := make(chan error) |
| 1492 | go func() { |
| 1493 | _, err := client.ReconcileDevice(ctx, device) |
| 1494 | adapterResponse <- err |
| 1495 | }() |
Maninder | 0aabf0c | 2021-03-17 14:55:14 +0530 | [diff] [blame] | 1496 | select { |
| 1497 | // wait for response |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 1498 | case err := <-adapterResponse: |
| 1499 | if err != nil { |
| 1500 | return err |
Maninder | 0aabf0c | 2021-03-17 14:55:14 +0530 | [diff] [blame] | 1501 | } |
Maninder | 0aabf0c | 2021-03-17 14:55:14 +0530 | [diff] [blame] | 1502 | //In case of success quit retrying and wait for adapter to reset operation state of device |
| 1503 | agent.stopReconcilingMutex.Lock() |
| 1504 | agent.stopReconciling = nil |
| 1505 | agent.stopReconcilingMutex.Unlock() |
| 1506 | return nil |
| 1507 | |
| 1508 | //if reconciling need to be stopped |
| 1509 | case _, ok := <-agent.stopReconciling: |
| 1510 | agent.stopReconcilingMutex.Lock() |
| 1511 | agent.stopReconciling = nil |
| 1512 | agent.stopReconcilingMutex.Unlock() |
| 1513 | if !ok { |
| 1514 | //channel-closed |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 1515 | return fmt.Errorf("reconcile channel closed:%w", errReconcileAborted) |
Maninder | 0aabf0c | 2021-03-17 14:55:14 +0530 | [diff] [blame] | 1516 | } |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 1517 | return fmt.Errorf("reconciling aborted:%w", errReconcileAborted) |
| 1518 | // Context expired |
| 1519 | case <-ctx.Done(): |
| 1520 | return fmt.Errorf("context expired:%s :%w", ctx.Err(), errContextExpired) |
Maninder | 0aabf0c | 2021-03-17 14:55:14 +0530 | [diff] [blame] | 1521 | } |
Maninder | 0aabf0c | 2021-03-17 14:55:14 +0530 | [diff] [blame] | 1522 | } |
| 1523 | |
| 1524 | func (agent *Agent) reconcilingCleanup(ctx context.Context) error { |
| 1525 | var desc string |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 1526 | var err error |
Maninder | 0aabf0c | 2021-03-17 14:55:14 +0530 | [diff] [blame] | 1527 | operStatus := &common.OperationResp{Code: common.OperationResp_OPERATION_FAILURE} |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 1528 | defer func() { agent.logDeviceUpdate(ctx, nil, nil, operStatus, err, desc) }() |
| 1529 | |
| 1530 | if err = agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
| 1531 | desc = "reconcile-cleanup-failed" |
Maninder | 0aabf0c | 2021-03-17 14:55:14 +0530 | [diff] [blame] | 1532 | return err |
| 1533 | } |
| 1534 | defer agent.requestQueue.RequestComplete() |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 1535 | err = agent.updateTransientState(ctx, core.DeviceTransientState_NONE) |
Maninder | 0aabf0c | 2021-03-17 14:55:14 +0530 | [diff] [blame] | 1536 | if err != nil { |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 1537 | logger.Errorf(ctx, "transient-state-update-failed", log.Fields{"error": err}) |
Maninder | 0aabf0c | 2021-03-17 14:55:14 +0530 | [diff] [blame] | 1538 | return err |
| 1539 | } |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 1540 | operStatus.Code = common.OperationResp_OPERATION_SUCCESS |
Maninder | 0aabf0c | 2021-03-17 14:55:14 +0530 | [diff] [blame] | 1541 | return nil |
| 1542 | } |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 1543 | |
| 1544 | func (agent *Agent) isAdapterConnectionUp(ctx context.Context) bool { |
| 1545 | c, err := agent.adapterMgr.GetAdapterClient(ctx, agent.adapterEndpoint) |
| 1546 | return c != nil && err == nil |
| 1547 | } |
| 1548 | |
| 1549 | func (agent *Agent) canDeviceRequestProceed(ctx context.Context) error { |
| 1550 | if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
| 1551 | return err |
| 1552 | } |
| 1553 | defer agent.requestQueue.RequestComplete() |
| 1554 | if agent.proceedWithRequest(agent.device) { |
| 1555 | return nil |
| 1556 | } |
| 1557 | return fmt.Errorf("device-cannot-process-request-%s", agent.deviceID) |
| 1558 | } |