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