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