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