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