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