blob: c1225745e7548404a5ca18b12ee7ce7ddb247d33 [file] [log] [blame]
khenaidoob9203542018-09-17 22:56:37 -04001/*
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 */
npujar1d86a522019-11-14 17:11:16 +053016
Kent Hagerman2b216042020-04-03 18:28:56 -040017package device
khenaidoob9203542018-09-17 22:56:37 -040018
19import (
20 "context"
Matteo Scandolo360605d2019-11-05 18:29:17 -080021 "encoding/hex"
Kent Hagerman4f355f52020-03-30 16:01:33 -040022 "errors"
khenaidoo3ab34882019-05-02 21:33:30 -040023 "fmt"
Mahir Gunyeladdb66a2020-04-29 18:08:50 -070024 "reflect"
25 "sync"
26 "time"
27
Himani Chawla2ba1c9c2020-10-07 13:19:03 +053028 "github.com/gogo/protobuf/proto"
khenaidoo442e7c72020-03-10 16:13:48 -040029 "github.com/golang/protobuf/ptypes"
dpaul62686312020-06-23 14:17:36 +053030 "github.com/golang/protobuf/ptypes/empty"
Himani Chawla2ba1c9c2020-10-07 13:19:03 +053031 "github.com/opencord/voltha-go/db/model"
Kent Hagerman2b216042020-04-03 18:28:56 -040032 "github.com/opencord/voltha-go/rw_core/core/adapter"
Mahir Gunyel03de0d32020-06-03 01:36:59 -070033 "github.com/opencord/voltha-go/rw_core/core/device/flow"
34 "github.com/opencord/voltha-go/rw_core/core/device/group"
Kent Hagerman2a07b862020-06-19 15:23:07 -040035 "github.com/opencord/voltha-go/rw_core/core/device/port"
Kent Hagerman2b216042020-04-03 18:28:56 -040036 "github.com/opencord/voltha-go/rw_core/core/device/remote"
Himani Chawla2ba1c9c2020-10-07 13:19:03 +053037 "github.com/opencord/voltha-go/rw_core/core/device/transientstate"
Scott Bakerb671a862019-10-24 10:53:40 -070038 coreutils "github.com/opencord/voltha-go/rw_core/utils"
Himani Chawla2ba1c9c2020-10-07 13:19:03 +053039 "github.com/opencord/voltha-lib-go/v4/pkg/kafka"
Maninderdfadc982020-10-28 14:04:33 +053040 "github.com/opencord/voltha-lib-go/v4/pkg/log"
Salman Siddiqui1cf95042020-11-19 00:42:56 +053041 "github.com/opencord/voltha-protos/v4/go/extension"
Maninderdfadc982020-10-28 14:04:33 +053042 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"
khenaidoob9203542018-09-17 22:56:37 -040045 "google.golang.org/grpc/codes"
46 "google.golang.org/grpc/status"
khenaidoob9203542018-09-17 22:56:37 -040047)
48
Kent Hagerman2b216042020-04-03 18:28:56 -040049// Agent represents device agent attributes
50type Agent struct {
Kent Hagermanf5a67352020-04-30 15:15:26 -040051 deviceID string
52 parentID string
53 deviceType string
Kent Hagerman2a07b862020-06-19 15:23:07 -040054 isRootDevice bool
Kent Hagermanf5a67352020-04-30 15:15:26 -040055 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 Gunyel03de0d32020-06-03 01:36:59 -070066
Himani Chawla2ba1c9c2020-10-07 13:19:03 +053067 flowLoader *flow.Loader
68 groupLoader *group.Loader
69 portLoader *port.Loader
70 transientStateLoader *transientstate.Loader
khenaidoob9203542018-09-17 22:56:37 -040071}
72
Kent Hagerman2b216042020-04-03 18:28:56 -040073//newAgent creates a new device agent. The device will be initialized when start() is called.
Kent Hagerman2a07b862020-06-19 15:23:07 -040074func 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 Barbarie1ab43272018-12-08 21:42:13 -050078 }
Scott Baker80678602019-11-14 16:57:36 -080079
Kent Hagerman2a07b862020-06-19 15:23:07 -040080 return &Agent{
Himani Chawla2ba1c9c2020-10-07 13:19:03 +053081 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 Hagerman2a07b862020-06-19 15:23:07 -040097 }
khenaidoob9203542018-09-17 22:56:37 -040098}
99
khenaidoo442e7c72020-03-10 16:13:48 -0400100// 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 Baker80678602019-11-14 16:57:36 -0800102// was started.
Kent Hagerman2b216042020-04-03 18:28:56 -0400103func (agent *Agent) start(ctx context.Context, deviceToCreate *voltha.Device) (*voltha.Device, error) {
khenaidoo442e7c72020-03-10 16:13:48 -0400104 needToStart := false
105 if agent.startOnce.Do(func() { needToStart = true }); !needToStart {
Kent Hagermancba2f302020-07-28 13:37:36 -0400106 return agent.getDeviceReadOnly(ctx)
khenaidoo442e7c72020-03-10 16:13:48 -0400107 }
108 var startSucceeded bool
109 defer func() {
110 if !startSucceeded {
111 if err := agent.stop(ctx); err != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000112 logger.Errorw(ctx, "failed-to-cleanup-after-unsuccessful-start", log.Fields{"device-id": agent.deviceID, "error": err})
khenaidoo442e7c72020-03-10 16:13:48 -0400113 }
114 }
115 }()
Scott Baker80678602019-11-14 16:57:36 -0800116
khenaidoo442e7c72020-03-10 16:13:48 -0400117 var device *voltha.Device
Scott Baker80678602019-11-14 16:57:36 -0800118 if deviceToCreate == nil {
119 // Load the existing device
Kent Hagerman4f355f52020-03-30 16:01:33 -0400120 device := &voltha.Device{}
Kent Hagermanf5a67352020-04-30 15:15:26 -0400121 have, err := agent.dbProxy.Get(ctx, agent.deviceID, device)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530122 if err != nil {
Thomas Lee Se5a44012019-11-07 20:32:24 +0530123 return nil, err
Kent Hagerman4f355f52020-03-30 16:01:33 -0400124 } else if !have {
125 return nil, status.Errorf(codes.NotFound, "device-%s", agent.deviceID)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530126 }
Kent Hagerman4f355f52020-03-30 16:01:33 -0400127
128 agent.deviceType = device.Adapter
129 agent.device = proto.Clone(device).(*voltha.Device)
Mahir Gunyel03de0d32020-06-03 01:36:59 -0700130 // load the flows and groups from KV to cache
131 agent.flowLoader.Load(ctx)
132 agent.groupLoader.Load(ctx)
Kent Hagerman2a07b862020-06-19 15:23:07 -0400133 agent.portLoader.Load(ctx)
Himani Chawla2ba1c9c2020-10-07 13:19:03 +0530134 agent.transientStateLoader.Load(ctx)
Kent Hagerman4f355f52020-03-30 16:01:33 -0400135
Rohan Agrawal31f21802020-06-12 05:38:46 +0000136 logger.Infow(ctx, "device-loaded-from-dB", log.Fields{"device-id": agent.deviceID})
khenaidoo297cd252019-02-07 22:10:23 -0500137 } else {
Scott Baker80678602019-11-14 16:57:36 -0800138 // Create a new device
Kent Hagermanf5a67352020-04-30 15:15:26 -0400139 // 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 Hagerman2b216042020-04-03 18:28:56 -0400141 // agent.deviceId will also have been set during newAgent().
Scott Baker80678602019-11-14 16:57:36 -0800142 device = (proto.Clone(deviceToCreate)).(*voltha.Device)
npujar1d86a522019-11-14 17:11:16 +0530143 device.Id = agent.deviceID
Scott Baker80678602019-11-14 16:57:36 -0800144 device.AdminState = voltha.AdminState_PREPROVISIONED
Scott Baker80678602019-11-14 16:57:36 -0800145 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
khenaidoo297cd252019-02-07 22:10:23 -0500151 // Add the initial device to the local model
Kent Hagermanf5a67352020-04-30 15:15:26 -0400152 if err := agent.dbProxy.Set(ctx, agent.deviceID, device); err != nil {
Kent Hagerman4f355f52020-03-30 16:01:33 -0400153 return nil, status.Errorf(codes.Aborted, "failed-adding-device-%s: %s", agent.deviceID, err)
khenaidoo297cd252019-02-07 22:10:23 -0500154 }
khenaidoo442e7c72020-03-10 16:13:48 -0400155 agent.device = device
khenaidoob9203542018-09-17 22:56:37 -0400156 }
khenaidoo442e7c72020-03-10 16:13:48 -0400157 startSucceeded = true
Rohan Agrawalcf12f202020-08-03 04:42:01 +0000158 log.EnrichSpan(ctx, log.Fields{"device-id": agent.deviceID})
Rohan Agrawal31f21802020-06-12 05:38:46 +0000159 logger.Debugw(ctx, "device-agent-started", log.Fields{"device-id": agent.deviceID})
khenaidoo442e7c72020-03-10 16:13:48 -0400160
Kent Hagermancba2f302020-07-28 13:37:36 -0400161 return agent.getDeviceReadOnly(ctx)
khenaidoob9203542018-09-17 22:56:37 -0400162}
163
khenaidoo4d4802d2018-10-04 21:59:49 -0400164// stop stops the device agent. Not much to do for now
Kent Hagerman2b216042020-04-03 18:28:56 -0400165func (agent *Agent) stop(ctx context.Context) error {
khenaidoo442e7c72020-03-10 16:13:48 -0400166 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()
khenaidoo49085352020-01-13 19:15:43 -0500174
divyadesaicb8b59d2020-08-18 09:55:47 +0000175 logger.Infow(ctx, "stopping-device-agent", log.Fields{"device-id": agent.deviceID, "parentId": agent.parentID})
Himani Chawla2ba1c9c2020-10-07 13:19:03 +0530176 // Remove the device transient loader
177 if err := agent.deleteTransientState(ctx); err != nil {
178 return err
179 }
khenaidoo0a822f92019-05-08 15:15:57 -0400180 // Remove the device from the KV store
Kent Hagermanf5a67352020-04-30 15:15:26 -0400181 if err := agent.dbProxy.Remove(ctx, agent.deviceID); err != nil {
khenaidoo442e7c72020-03-10 16:13:48 -0400182 return err
Thomas Lee Se5a44012019-11-07 20:32:24 +0530183 }
khenaidoo442e7c72020-03-10 16:13:48 -0400184
khenaidoo442e7c72020-03-10 16:13:48 -0400185 close(agent.exitChannel)
186
187 agent.stopped = true
188
Rohan Agrawal31f21802020-06-12 05:38:46 +0000189 logger.Infow(ctx, "device-agent-stopped", log.Fields{"device-id": agent.deviceID, "parent-id": agent.parentID})
khenaidoo442e7c72020-03-10 16:13:48 -0400190
191 return nil
khenaidoob9203542018-09-17 22:56:37 -0400192}
193
Scott Baker80678602019-11-14 16:57:36 -0800194// Load the most recent state from the KVStore for the device.
Kent Hagerman2b216042020-04-03 18:28:56 -0400195func (agent *Agent) reconcileWithKVStore(ctx context.Context) {
khenaidoo442e7c72020-03-10 16:13:48 -0400196 if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000197 logger.Warnw(ctx, "request-aborted", log.Fields{"device-id": agent.deviceID, "error": err})
khenaidoo442e7c72020-03-10 16:13:48 -0400198 return
199 }
200 defer agent.requestQueue.RequestComplete()
Rohan Agrawal31f21802020-06-12 05:38:46 +0000201 logger.Debug(ctx, "reconciling-device-agent-devicetype")
Scott Baker80678602019-11-14 16:57:36 -0800202 // TODO: context timeout
Kent Hagerman4f355f52020-03-30 16:01:33 -0400203 device := &voltha.Device{}
Kent Hagermanf5a67352020-04-30 15:15:26 -0400204 if have, err := agent.dbProxy.Get(ctx, agent.deviceID, device); err != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000205 logger.Errorw(ctx, "kv-get-failed", log.Fields{"device-id": agent.deviceID, "error": err})
Thomas Lee Se5a44012019-11-07 20:32:24 +0530206 return
Kent Hagerman4f355f52020-03-30 16:01:33 -0400207 } else if !have {
208 return // not found in kv
Thomas Lee Se5a44012019-11-07 20:32:24 +0530209 }
Kent Hagerman4f355f52020-03-30 16:01:33 -0400210
211 agent.deviceType = device.Adapter
212 agent.device = device
Mahir Gunyel03de0d32020-06-03 01:36:59 -0700213 agent.flowLoader.Load(ctx)
214 agent.groupLoader.Load(ctx)
Kent Hagerman2a07b862020-06-19 15:23:07 -0400215 agent.portLoader.Load(ctx)
Himani Chawla2ba1c9c2020-10-07 13:19:03 +0530216 agent.transientStateLoader.Load(ctx)
217
Rohan Agrawal31f21802020-06-12 05:38:46 +0000218 logger.Debugw(ctx, "reconciled-device-agent-devicetype", log.Fields{"device-id": agent.deviceID, "type": agent.deviceType})
Scott Baker80678602019-11-14 16:57:36 -0800219}
220
khenaidoo442e7c72020-03-10 16:13:48 -0400221// 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 Agrawal31f21802020-06-12 05:38:46 +0000223func (agent *Agent) onSuccess(ctx context.Context, rpc string, response interface{}, reqArgs ...interface{}) {
Himani Chawla2ba1c9c2020-10-07 13:19:03 +0530224 logger.Debugw(ctx, "response-successful", log.Fields{"rpc": rpc, "device-id": agent.deviceID})
khenaidoo442e7c72020-03-10 16:13:48 -0400225 // 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 Agrawal31f21802020-06-12 05:38:46 +0000230func (agent *Agent) onFailure(ctx context.Context, rpc string, response interface{}, reqArgs ...interface{}) {
khenaidoo442e7c72020-03-10 16:13:48 -0400231 if res, ok := response.(error); ok {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000232 logger.Errorw(ctx, "rpc-failed", log.Fields{"rpc": rpc, "device-id": agent.deviceID, "error": res, "args": reqArgs})
khenaidoo442e7c72020-03-10 16:13:48 -0400233 } else {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000234 logger.Errorw(ctx, "rpc-failed-invalid-error", log.Fields{"rpc": rpc, "device-id": agent.deviceID, "args": reqArgs})
khenaidoo442e7c72020-03-10 16:13:48 -0400235 }
236 // TODO: Post failure message onto kafka
237}
238
Kent Hagerman2b216042020-04-03 18:28:56 -0400239func (agent *Agent) waitForAdapterResponse(ctx context.Context, cancel context.CancelFunc, rpc string, ch chan *kafka.RpcResponse,
khenaidoo442e7c72020-03-10 16:13:48 -0400240 onSuccess coreutils.ResponseCallback, onFailure coreutils.ResponseCallback, reqArgs ...interface{}) {
241 defer cancel()
242 select {
243 case rpcResponse, ok := <-ch:
244 if !ok {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000245 onFailure(ctx, rpc, status.Errorf(codes.Aborted, "channel-closed"), reqArgs)
khenaidoo442e7c72020-03-10 16:13:48 -0400246 } else if rpcResponse.Err != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000247 onFailure(ctx, rpc, rpcResponse.Err, reqArgs)
khenaidoo442e7c72020-03-10 16:13:48 -0400248 } else {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000249 onSuccess(ctx, rpc, rpcResponse.Reply, reqArgs)
khenaidoo442e7c72020-03-10 16:13:48 -0400250 }
251 case <-ctx.Done():
Rohan Agrawal31f21802020-06-12 05:38:46 +0000252 onFailure(ctx, rpc, ctx.Err(), reqArgs)
khenaidoo442e7c72020-03-10 16:13:48 -0400253 }
254}
255
Himani Chawla2ba1c9c2020-10-07 13:19:03 +0530256// onDeleteSuccess is a common callback for scenarios where we receive a nil response following a delete request
257// to an adapter.
258func (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.
273func (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
286func (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 Hagermancba2f302020-07-28 13:37:36 -0400303// getDeviceReadOnly returns a device which MUST NOT be modified, but is safe to keep forever.
304func (agent *Agent) getDeviceReadOnly(ctx context.Context) (*voltha.Device, error) {
khenaidoo442e7c72020-03-10 16:13:48 -0400305 if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil {
306 return nil, err
307 }
308 defer agent.requestQueue.RequestComplete()
Kent Hagermancba2f302020-07-28 13:37:36 -0400309 return agent.device, nil
khenaidoo92e62c52018-10-03 14:02:54 -0400310}
311
Kent Hagermancba2f302020-07-28 13:37:36 -0400312// getDeviceReadOnlyWithoutLock returns a device which MUST NOT be modified, but is safe to keep forever. This is very efficient.
Kent Hagermanf6db9f12020-07-22 17:16:19 -0400313// The device lock MUST be held by the caller.
Kent Hagermancba2f302020-07-28 13:37:36 -0400314func (agent *Agent) getDeviceReadOnlyWithoutLock() *voltha.Device {
khenaidoo0db4c812020-05-27 15:27:30 -0400315 return agent.device
khenaidoo92e62c52018-10-03 14:02:54 -0400316}
317
Kent Hagermanf6db9f12020-07-22 17:16:19 -0400318// cloneDeviceWithoutLock returns a copy of the device which is safe to modify.
319// The device lock MUST be held by the caller.
320func (agent *Agent) cloneDeviceWithoutLock() *voltha.Device {
321 return proto.Clone(agent.device).(*voltha.Device)
322}
323
khenaidoo3ab34882019-05-02 21:33:30 -0400324// enableDevice activates a preprovisioned or a disable device
Kent Hagerman2b216042020-04-03 18:28:56 -0400325func (agent *Agent) enableDevice(ctx context.Context) error {
khenaidoo442e7c72020-03-10 16:13:48 -0400326 if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil {
327 return err
328 }
Rohan Agrawal31f21802020-06-12 05:38:46 +0000329 logger.Debugw(ctx, "enableDevice", log.Fields{"device-id": agent.deviceID})
khenaidoo21d51152019-02-01 13:48:37 -0500330
Kent Hagermancba2f302020-07-28 13:37:36 -0400331 oldDevice := agent.getDeviceReadOnlyWithoutLock()
Kent Hagermanf6db9f12020-07-22 17:16:19 -0400332 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 Chawla2ba1c9c2020-10-07 13:19:03 +0530337 if agent.isDeletionInProgress() {
Kent Hagermanf6db9f12020-07-22 17:16:19 -0400338 agent.requestQueue.RequestComplete()
Himani Chawla2ba1c9c2020-10-07 13:19:03 +0530339 return status.Errorf(codes.FailedPrecondition, "deviceId:%s, Device deletion is in progress.", agent.deviceID)
Kent Hagermanf6db9f12020-07-22 17:16:19 -0400340 }
npujar1d86a522019-11-14 17:11:16 +0530341 // First figure out which adapter will handle this device type. We do it at this stage as allow devices to be
khenaidoo442e7c72020-03-10 16:13:48 -0400342 // pre-provisioned with the required adapter not registered. At this stage, since we need to communicate
npujar1d86a522019-11-14 17:11:16 +0530343 // with the adapter then we need to know the adapter that will handle this request
Kent Hagermanf6db9f12020-07-22 17:16:19 -0400344 adapterName, err := agent.adapterMgr.GetAdapterType(oldDevice.Type)
npujar1d86a522019-11-14 17:11:16 +0530345 if err != nil {
Kent Hagermanf6db9f12020-07-22 17:16:19 -0400346 agent.requestQueue.RequestComplete()
Matteo Scandolod525ae32020-04-02 17:27:29 -0700347 return err
npujar1d86a522019-11-14 17:11:16 +0530348 }
349
Kent Hagermanf6db9f12020-07-22 17:16:19 -0400350 newDevice := agent.cloneDeviceWithoutLock()
351 newDevice.Adapter = adapterName
npujar1d86a522019-11-14 17:11:16 +0530352
Kent Hagermanf6db9f12020-07-22 17:16:19 -0400353 // 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 {
npujar1d86a522019-11-14 17:11:16 +0530357 return err
358 }
359
khenaidoo442e7c72020-03-10 16:13:48 -0400360 // Adopt the device if it was in pre-provision state. In all other cases, try to re-enable it.
khenaidoo442e7c72020-03-10 16:13:48 -0400361 var ch chan *kafka.RpcResponse
Rohan Agrawalcf12f202020-08-03 04:42:01 +0000362 subCtx, cancel := context.WithTimeout(log.WithSpanFromContext(context.Background(), ctx), agent.defaultTimeout)
Kent Hagermanf6db9f12020-07-22 17:16:19 -0400363 if oldDevice.AdminState == voltha.AdminState_PREPROVISIONED {
364 ch, err = agent.adapterProxy.AdoptDevice(subCtx, newDevice)
khenaidoob9203542018-09-17 22:56:37 -0400365 } else {
Kent Hagermanf6db9f12020-07-22 17:16:19 -0400366 ch, err = agent.adapterProxy.ReEnableDevice(subCtx, newDevice)
khenaidoob9203542018-09-17 22:56:37 -0400367 }
khenaidoo442e7c72020-03-10 16:13:48 -0400368 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)
khenaidoob9203542018-09-17 22:56:37 -0400374 return nil
375}
376
Kent Hagerman2b216042020-04-03 18:28:56 -0400377func (agent *Agent) waitForAdapterFlowResponse(ctx context.Context, cancel context.CancelFunc, ch chan *kafka.RpcResponse, response coreutils.Response) {
khenaidoo442e7c72020-03-10 16:13:48 -0400378 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())
khenaidoo2c6a0992019-04-29 13:46:56 -0400390 }
khenaidoo2c6a0992019-04-29 13:46:56 -0400391}
392
A R Karthick5c28f552019-12-11 22:47:44 -0800393//addFlowsAndGroups adds the "newFlows" and "newGroups" from the existing flows/groups and sends the update to the
394//adapters
Kent Hagerman2b216042020-04-03 18:28:56 -0400395func (agent *Agent) addFlowsAndGroups(ctx context.Context, newFlows []*ofp.OfpFlowStats, newGroups []*ofp.OfpGroupEntry, flowMetadata *voltha.FlowMetadata) error {
Mahir Gunyel03de0d32020-06-03 01:36:59 -0700396 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 Karthick5c28f552019-12-11 22:47:44 -0800400 return err
401 }
Mahir Gunyel03de0d32020-06-03 01:36:59 -0700402 //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 Agrawal31f21802020-06-12 05:38:46 +0000407 logger.Warnw(ctx, "no-adapter-response", log.Fields{"device-id": agent.deviceID, "result": errs})
khenaidoo442e7c72020-03-10 16:13:48 -0400408 return status.Errorf(codes.Aborted, "flow-failure-device-%s", agent.deviceID)
khenaidoo0458db62019-06-20 08:50:36 -0400409 }
khenaidoo0458db62019-06-20 08:50:36 -0400410 return nil
411}
412
A R Karthick5c28f552019-12-11 22:47:44 -0800413//deleteFlowsAndGroups removes the "flowsToDel" and "groupsToDel" from the existing flows/groups and sends the update to the
414//adapters
Kent Hagerman2b216042020-04-03 18:28:56 -0400415func (agent *Agent) deleteFlowsAndGroups(ctx context.Context, flowsToDel []*ofp.OfpFlowStats, groupsToDel []*ofp.OfpGroupEntry, flowMetadata *voltha.FlowMetadata) error {
Mahir Gunyel03de0d32020-06-03 01:36:59 -0700416 var flwResponse, grpResponse coreutils.Response
417 var err error
418 if flwResponse, err = agent.deleteFlowsFromAdapter(ctx, flowsToDel, flowMetadata); err != nil {
A R Karthick5c28f552019-12-11 22:47:44 -0800419 return err
420 }
Mahir Gunyel03de0d32020-06-03 01:36:59 -0700421 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 {
khenaidoo0458db62019-06-20 08:50:36 -0400426 return status.Errorf(codes.Aborted, "errors-%s", res)
427 }
428 return nil
khenaidoo0458db62019-06-20 08:50:36 -0400429}
430
A R Karthick5c28f552019-12-11 22:47:44 -0800431//updateFlowsAndGroups replaces the existing flows and groups with "updatedFlows" and "updatedGroups" respectively. It
432//also sends the updates to the adapters
Kent Hagerman2b216042020-04-03 18:28:56 -0400433func (agent *Agent) updateFlowsAndGroups(ctx context.Context, updatedFlows []*ofp.OfpFlowStats, updatedGroups []*ofp.OfpGroupEntry, flowMetadata *voltha.FlowMetadata) error {
Mahir Gunyel03de0d32020-06-03 01:36:59 -0700434 var flwResponse, grpResponse coreutils.Response
435 var err error
436 if flwResponse, err = agent.updateFlowsToAdapter(ctx, updatedFlows, flowMetadata); err != nil {
A R Karthick5c28f552019-12-11 22:47:44 -0800437 return err
438 }
Mahir Gunyel03de0d32020-06-03 01:36:59 -0700439 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 {
khenaidoo0458db62019-06-20 08:50:36 -0400444 return status.Errorf(codes.Aborted, "errors-%s", res)
445 }
446 return nil
khenaidoo19d7b632018-10-30 10:49:50 -0400447}
448
khenaidoo4d4802d2018-10-04 21:59:49 -0400449//disableDevice disable a device
Kent Hagerman2b216042020-04-03 18:28:56 -0400450func (agent *Agent) disableDevice(ctx context.Context) error {
khenaidoo442e7c72020-03-10 16:13:48 -0400451 if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil {
452 return err
453 }
Rohan Agrawal31f21802020-06-12 05:38:46 +0000454 logger.Debugw(ctx, "disableDevice", log.Fields{"device-id": agent.deviceID})
khenaidoo6e55d9e2019-12-12 18:26:26 -0500455
Kent Hagermanf6db9f12020-07-22 17:16:19 -0400456 cloned := agent.cloneDeviceWithoutLock()
khenaidoo6e55d9e2019-12-12 18:26:26 -0500457
458 if cloned.AdminState == voltha.AdminState_DISABLED {
divyadesaicb8b59d2020-08-18 09:55:47 +0000459 logger.Debugw(ctx, "device-already-disabled", log.Fields{"device-id": agent.deviceID})
Kent Hagermanf6db9f12020-07-22 17:16:19 -0400460 agent.requestQueue.RequestComplete()
npujar1d86a522019-11-14 17:11:16 +0530461 return nil
462 }
Himani Chawla2ba1c9c2020-10-07 13:19:03 +0530463 if cloned.AdminState == voltha.AdminState_PREPROVISIONED {
Kent Hagermanf6db9f12020-07-22 17:16:19 -0400464 agent.requestQueue.RequestComplete()
khenaidoo6e55d9e2019-12-12 18:26:26 -0500465 return status.Errorf(codes.FailedPrecondition, "deviceId:%s, invalid-admin-state:%s", agent.deviceID, cloned.AdminState)
npujar1d86a522019-11-14 17:11:16 +0530466 }
Himani Chawla2ba1c9c2020-10-07 13:19:03 +0530467 if agent.isDeletionInProgress() {
468 agent.requestQueue.RequestComplete()
469 return status.Errorf(codes.FailedPrecondition, "deviceId:%s, Device deletion is in progress.", agent.deviceID)
470 }
npujar1d86a522019-11-14 17:11:16 +0530471 // Update the Admin State and operational state before sending the request out
Kent Hagermanf6db9f12020-07-22 17:16:19 -0400472 cloned.AdminState = voltha.AdminState_DISABLED
473 cloned.OperStatus = voltha.OperStatus_UNKNOWN
474 if err := agent.updateDeviceAndReleaseLock(ctx, cloned); err != nil {
npujar1d86a522019-11-14 17:11:16 +0530475 return err
476 }
khenaidoo442e7c72020-03-10 16:13:48 -0400477
Rohan Agrawalcf12f202020-08-03 04:42:01 +0000478 subCtx, cancel := context.WithTimeout(log.WithSpanFromContext(context.Background(), ctx), agent.defaultTimeout)
Kent Hagermanf6db9f12020-07-22 17:16:19 -0400479 ch, err := agent.adapterProxy.DisableDevice(subCtx, cloned)
khenaidoo442e7c72020-03-10 16:13:48 -0400480 if err != nil {
481 cancel()
npujar1d86a522019-11-14 17:11:16 +0530482 return err
khenaidoo0a822f92019-05-08 15:15:57 -0400483 }
khenaidoo442e7c72020-03-10 16:13:48 -0400484 go agent.waitForAdapterResponse(subCtx, cancel, "disableDevice", ch, agent.onSuccess, agent.onFailure)
khenaidoo0a822f92019-05-08 15:15:57 -0400485
khenaidoo92e62c52018-10-03 14:02:54 -0400486 return nil
487}
488
Kent Hagerman2b216042020-04-03 18:28:56 -0400489func (agent *Agent) rebootDevice(ctx context.Context) error {
khenaidoo442e7c72020-03-10 16:13:48 -0400490 if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil {
npujar1d86a522019-11-14 17:11:16 +0530491 return err
khenaidoo4d4802d2018-10-04 21:59:49 -0400492 }
khenaidoo442e7c72020-03-10 16:13:48 -0400493 defer agent.requestQueue.RequestComplete()
Rohan Agrawal31f21802020-06-12 05:38:46 +0000494 logger.Debugw(ctx, "rebootDevice", log.Fields{"device-id": agent.deviceID})
khenaidoo442e7c72020-03-10 16:13:48 -0400495
Kent Hagermancba2f302020-07-28 13:37:36 -0400496 device := agent.getDeviceReadOnlyWithoutLock()
Himani Chawla2ba1c9c2020-10-07 13:19:03 +0530497 if agent.isDeletionInProgress() {
498 return status.Errorf(codes.FailedPrecondition, "deviceId:%s, Device deletion is in progress.", agent.deviceID)
499 }
Rohan Agrawalcf12f202020-08-03 04:42:01 +0000500 subCtx, cancel := context.WithTimeout(log.WithSpanFromContext(context.Background(), ctx), agent.defaultTimeout)
Kent Hagerman2b216042020-04-03 18:28:56 -0400501 ch, err := agent.adapterProxy.RebootDevice(subCtx, device)
khenaidoo442e7c72020-03-10 16:13:48 -0400502 if err != nil {
503 cancel()
504 return err
505 }
506 go agent.waitForAdapterResponse(subCtx, cancel, "rebootDevice", ch, agent.onSuccess, agent.onFailure)
khenaidoo4d4802d2018-10-04 21:59:49 -0400507 return nil
508}
509
Himani Chawla2ba1c9c2020-10-07 13:19:03 +0530510func (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 Hagerman2b216042020-04-03 18:28:56 -0400543func (agent *Agent) deleteDevice(ctx context.Context) error {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000544 logger.Debugw(ctx, "deleteDevice", log.Fields{"device-id": agent.deviceID})
khenaidoo442e7c72020-03-10 16:13:48 -0400545 if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil {
546 return err
547 }
Himani Chawla2ba1c9c2020-10-07 13:19:03 +0530548 // Get the device Transient state, return err if it is DELETING
549 previousDeviceTransientState := agent.getTransientState()
khenaidoo6e55d9e2019-12-12 18:26:26 -0500550
Himani Chawla2ba1c9c2020-10-07 13:19:03 +0530551 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
khenaidoo442e7c72020-03-10 16:13:48 -0400559
Himani Chawla2ba1c9c2020-10-07 13:19:03 +0530560 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 {
npujar1d86a522019-11-14 17:11:16 +0530566 return err
567 }
khenaidoo442e7c72020-03-10 16:13:48 -0400568 // 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 Chawla2ba1c9c2020-10-07 13:19:03 +0530570 if previousAdminState != ic.AdminState_PREPROVISIONED {
Rohan Agrawalcf12f202020-08-03 04:42:01 +0000571 subCtx, cancel := context.WithTimeout(log.WithSpanFromContext(context.Background(), ctx), agent.defaultTimeout)
Himani Chawla2ba1c9c2020-10-07 13:19:03 +0530572 ch, err := agent.adapterProxy.DeleteDevice(subCtx, device)
khenaidoo442e7c72020-03-10 16:13:48 -0400573 if err != nil {
574 cancel()
Himani Chawla2ba1c9c2020-10-07 13:19:03 +0530575 //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 }
khenaidoo442e7c72020-03-10 16:13:48 -0400579 return err
580 }
Himani Chawla2ba1c9c2020-10-07 13:19:03 +0530581 go agent.waitForAdapterDeleteResponse(subCtx, cancel, "deleteDevice", ch, agent.onDeleteSuccess,
582 agent.onDeleteFailure)
khenaidoo442e7c72020-03-10 16:13:48 -0400583 }
khenaidoo4d4802d2018-10-04 21:59:49 -0400584 return nil
585}
586
Kent Hagerman2b216042020-04-03 18:28:56 -0400587func (agent *Agent) setParentID(ctx context.Context, device *voltha.Device, parentID string) error {
khenaidoo442e7c72020-03-10 16:13:48 -0400588 if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil {
589 return err
590 }
Rohan Agrawal31f21802020-06-12 05:38:46 +0000591 logger.Debugw(ctx, "setParentId", log.Fields{"device-id": device.Id, "parent-id": parentID})
khenaidoo6e55d9e2019-12-12 18:26:26 -0500592
Kent Hagermanf6db9f12020-07-22 17:16:19 -0400593 cloned := agent.cloneDeviceWithoutLock()
npujar1d86a522019-11-14 17:11:16 +0530594 cloned.ParentId = parentID
Kent Hagermanf6db9f12020-07-22 17:16:19 -0400595 return agent.updateDeviceAndReleaseLock(ctx, cloned)
khenaidooad06fd72019-10-28 12:26:05 -0400596}
597
khenaidoo442e7c72020-03-10 16:13:48 -0400598// getSwitchCapability retrieves the switch capability of a parent device
Kent Hagerman2b216042020-04-03 18:28:56 -0400599func (agent *Agent) getSwitchCapability(ctx context.Context) (*ic.SwitchCapability, error) {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000600 logger.Debugw(ctx, "getSwitchCapability", log.Fields{"device-id": agent.deviceID})
khenaidoo442e7c72020-03-10 16:13:48 -0400601
Kent Hagermancba2f302020-07-28 13:37:36 -0400602 device, err := agent.getDeviceReadOnly(ctx)
khenaidoo442e7c72020-03-10 16:13:48 -0400603 if err != nil {
khenaidoob9203542018-09-17 22:56:37 -0400604 return nil, err
khenaidoob9203542018-09-17 22:56:37 -0400605 }
Kent Hagermancba2f302020-07-28 13:37:36 -0400606 ch, err := agent.adapterProxy.GetOfpDeviceInfo(ctx, device)
khenaidoo442e7c72020-03-10 16:13:48 -0400607 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 {
npujar1d86a522019-11-14 17:11:16 +0530622 return nil, err
623 }
624 return switchCap, nil
khenaidoob9203542018-09-17 22:56:37 -0400625}
626
Rohan Agrawal31f21802020-06-12 05:38:46 +0000627func (agent *Agent) onPacketFailure(ctx context.Context, rpc string, response interface{}, args ...interface{}) {
khenaidoo442e7c72020-03-10 16:13:48 -0400628 // 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 Agrawal31f21802020-06-12 05:38:46 +0000639 logger.Warnw(ctx, "packet-out-error", log.Fields{
khenaidoo442e7c72020-03-10 16:13:48 -0400640 "device-id": agent.deviceID,
641 "error": errResp,
642 "packet": hex.EncodeToString(packet),
643 })
644}
645
Kent Hagerman2b216042020-04-03 18:28:56 -0400646func (agent *Agent) packetOut(ctx context.Context, outPort uint32, packet *ofp.OfpPacketOut) error {
Scott Baker80678602019-11-14 16:57:36 -0800647 // 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 == "" {
npujar467fe752020-01-16 20:17:45 +0530650 agent.reconcileWithKVStore(ctx)
Scott Baker80678602019-11-14 16:57:36 -0800651 }
khenaidoofdbad6e2018-11-06 22:26:38 -0500652 // Send packet to adapter
Rohan Agrawalcf12f202020-08-03 04:42:01 +0000653 subCtx, cancel := context.WithTimeout(log.WithSpanFromContext(context.Background(), ctx), agent.defaultTimeout)
Kent Hagerman2b216042020-04-03 18:28:56 -0400654 ch, err := agent.adapterProxy.PacketOut(subCtx, agent.deviceType, agent.deviceID, outPort, packet)
khenaidoo442e7c72020-03-10 16:13:48 -0400655 if err != nil {
656 cancel()
657 return nil
khenaidoofdbad6e2018-11-06 22:26:38 -0500658 }
khenaidoo442e7c72020-03-10 16:13:48 -0400659 go agent.waitForAdapterResponse(subCtx, cancel, "packetOut", ch, agent.onSuccess, agent.onPacketFailure, packet.Data)
khenaidoofdbad6e2018-11-06 22:26:38 -0500660 return nil
661}
662
Kent Hagermanf6db9f12020-07-22 17:16:19 -0400663func (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 Gunyel8e2707d2019-07-25 00:36:21 -0700670 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 Campanella025667e2021-01-14 11:50:07 +0100677 cloned.ImageDownloads = device.ImageDownloads
Kent Hagermanf6db9f12020-07-22 17:16:19 -0400678 return agent.updateDeviceAndReleaseLock(ctx, cloned)
khenaidoo43c82122018-11-22 18:38:28 -0500679}
680
Kent Hagerman2b216042020-04-03 18:28:56 -0400681func (agent *Agent) updateDeviceStatus(ctx context.Context, operStatus voltha.OperStatus_Types, connStatus voltha.ConnectStatus_Types) error {
khenaidoo442e7c72020-03-10 16:13:48 -0400682 if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil {
683 return err
684 }
khenaidoo6e55d9e2019-12-12 18:26:26 -0500685
Kent Hagermanf6db9f12020-07-22 17:16:19 -0400686 cloned := agent.cloneDeviceWithoutLock()
npujar1d86a522019-11-14 17:11:16 +0530687 // Ensure the enums passed in are valid - they will be invalid if they are not set when this function is invoked
Kent Hagermanf6db9f12020-07-22 17:16:19 -0400688 if s, ok := voltha.ConnectStatus_Types_name[int32(connStatus)]; ok {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000689 logger.Debugw(ctx, "updateDeviceStatus-conn", log.Fields{"ok": ok, "val": s})
Kent Hagermanf6db9f12020-07-22 17:16:19 -0400690 cloned.ConnectStatus = connStatus
npujar1d86a522019-11-14 17:11:16 +0530691 }
Kent Hagermanf6db9f12020-07-22 17:16:19 -0400692 if s, ok := voltha.OperStatus_Types_name[int32(operStatus)]; ok {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000693 logger.Debugw(ctx, "updateDeviceStatus-oper", log.Fields{"ok": ok, "val": s})
Kent Hagermanf6db9f12020-07-22 17:16:19 -0400694 cloned.OperStatus = operStatus
npujar1d86a522019-11-14 17:11:16 +0530695 }
divyadesaicb8b59d2020-08-18 09:55:47 +0000696 logger.Debugw(ctx, "updateDeviceStatus", log.Fields{"device-id": cloned.Id, "operStatus": cloned.OperStatus, "connectStatus": cloned.ConnectStatus})
npujar1d86a522019-11-14 17:11:16 +0530697 // Store the device
Kent Hagermanf6db9f12020-07-22 17:16:19 -0400698 return agent.updateDeviceAndReleaseLock(ctx, cloned)
khenaidoo92e62c52018-10-03 14:02:54 -0400699}
700
khenaidoob9203542018-09-17 22:56:37 -0400701// TODO: A generic device update by attribute
Kent Hagerman2b216042020-04-03 18:28:56 -0400702func (agent *Agent) updateDeviceAttribute(ctx context.Context, name string, value interface{}) {
khenaidoob9203542018-09-17 22:56:37 -0400703 if value == nil {
704 return
705 }
khenaidoo6e55d9e2019-12-12 18:26:26 -0500706
Kent Hagermanf6db9f12020-07-22 17:16:19 -0400707 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()
khenaidoob9203542018-09-17 22:56:37 -0400713 updated := false
khenaidoo6e55d9e2019-12-12 18:26:26 -0500714 s := reflect.ValueOf(cloned).Elem()
khenaidoob9203542018-09-17 22:56:37 -0400715 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 }
divyadesaicb8b59d2020-08-18 09:55:47 +0000732 logger.Debugw(ctx, "update-field-status", log.Fields{"device-id": cloned.Id, "name": name, "updated": updated})
khenaidoob9203542018-09-17 22:56:37 -0400733 // Save the data
khenaidoo6e55d9e2019-12-12 18:26:26 -0500734
Kent Hagermanf6db9f12020-07-22 17:16:19 -0400735 if err := agent.updateDeviceAndReleaseLock(ctx, cloned); err != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000736 logger.Warnw(ctx, "attribute-update-failed", log.Fields{"attribute": name, "value": value})
khenaidoob9203542018-09-17 22:56:37 -0400737 }
khenaidoob9203542018-09-17 22:56:37 -0400738}
serkant.uluderya334479d2019-04-10 08:26:15 -0700739
Kent Hagerman45a13e42020-04-13 12:23:50 -0400740func (agent *Agent) simulateAlarm(ctx context.Context, simulateReq *voltha.SimulateAlarmRequest) error {
khenaidoo442e7c72020-03-10 16:13:48 -0400741 if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil {
742 return err
743 }
744 defer agent.requestQueue.RequestComplete()
divyadesaicb8b59d2020-08-18 09:55:47 +0000745 logger.Debugw(ctx, "simulateAlarm", log.Fields{"device-id": agent.deviceID})
khenaidoo6e55d9e2019-12-12 18:26:26 -0500746
Kent Hagermancba2f302020-07-28 13:37:36 -0400747 device := agent.getDeviceReadOnlyWithoutLock()
khenaidoo6e55d9e2019-12-12 18:26:26 -0500748
Rohan Agrawalcf12f202020-08-03 04:42:01 +0000749 subCtx, cancel := context.WithTimeout(log.WithSpanFromContext(context.Background(), ctx), agent.defaultTimeout)
Kent Hagermanf6db9f12020-07-22 17:16:19 -0400750 ch, err := agent.adapterProxy.SimulateAlarm(subCtx, device, simulateReq)
khenaidoo442e7c72020-03-10 16:13:48 -0400751 if err != nil {
752 cancel()
npujar1d86a522019-11-14 17:11:16 +0530753 return err
serkant.uluderya334479d2019-04-10 08:26:15 -0700754 }
khenaidoo442e7c72020-03-10 16:13:48 -0400755 go agent.waitForAdapterResponse(subCtx, cancel, "simulateAlarm", ch, agent.onSuccess, agent.onFailure)
serkant.uluderya334479d2019-04-10 08:26:15 -0700756 return nil
757}
Mahir Gunyelb5851672019-07-24 10:46:26 +0300758
Kent Hagermanf6db9f12020-07-22 17:16:19 -0400759// 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.
761func (agent *Agent) updateDeviceAndReleaseLock(ctx context.Context, device *voltha.Device) error {
762 // fail early if this agent is no longer valid
Kent Hagerman4f355f52020-03-30 16:01:33 -0400763 if agent.stopped {
Kent Hagermanf6db9f12020-07-22 17:16:19 -0400764 agent.requestQueue.RequestComplete()
Himani Chawla2ba1c9c2020-10-07 13:19:03 +0530765 return errors.New("device-agent-stopped")
Thomas Lee Se5a44012019-11-07 20:32:24 +0530766 }
Kent Hagerman4f355f52020-03-30 16:01:33 -0400767
Kent Hagermanf6db9f12020-07-22 17:16:19 -0400768 // update in db
Kent Hagermanf5a67352020-04-30 15:15:26 -0400769 if err := agent.dbProxy.Set(ctx, agent.deviceID, device); err != nil {
Kent Hagermanf6db9f12020-07-22 17:16:19 -0400770 agent.requestQueue.RequestComplete()
Kent Hagerman4f355f52020-03-30 16:01:33 -0400771 return status.Errorf(codes.Internal, "failed-update-device:%s: %s", agent.deviceID, err)
Mahir Gunyelb5851672019-07-24 10:46:26 +0300772 }
divyadesaicb8b59d2020-08-18 09:55:47 +0000773 logger.Debugw(ctx, "updated-device-in-store", log.Fields{"device-id: ": agent.deviceID})
Mahir Gunyelb5851672019-07-24 10:46:26 +0300774
Kent Hagerman6031aad2020-07-29 16:36:33 -0400775 prevDevice := agent.device
Kent Hagermanf6db9f12020-07-22 17:16:19 -0400776 // update the device
khenaidoo0db4c812020-05-27 15:27:30 -0400777 agent.device = device
Kent Hagermanf6db9f12020-07-22 17:16:19 -0400778
779 // release lock before processing transition
780 agent.requestQueue.RequestComplete()
781
Himani Chawla2ba1c9c2020-10-07 13:19:03 +0530782 if err := agent.deviceMgr.stateTransitions.ProcessTransition(log.WithSpanFromContext(context.Background(), ctx),
783 device, prevDevice, voltha.DeviceTransientState_NONE, voltha.DeviceTransientState_NONE); err != nil {
Kent Hagerman6031aad2020-07-29 16:36:33 -0400784 logger.Errorw(ctx, "failed-process-transition", log.Fields{"device-id": device.Id, "previousAdminState": prevDevice.AdminState, "currentAdminState": device.AdminState})
Kent Hagermanf6db9f12020-07-22 17:16:19 -0400785 }
Mahir Gunyelb5851672019-07-24 10:46:26 +0300786 return nil
787}
Mahir Gunyelfdee9212019-10-16 16:52:21 -0700788
Himani Chawla2ba1c9c2020-10-07 13:19:03 +0530789// 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.
791func (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 Hagerman2b216042020-04-03 18:28:56 -0400828func (agent *Agent) updateDeviceReason(ctx context.Context, reason string) error {
khenaidoo442e7c72020-03-10 16:13:48 -0400829 if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil {
830 return err
831 }
divyadesaicb8b59d2020-08-18 09:55:47 +0000832 logger.Debugw(ctx, "updateDeviceReason", log.Fields{"device-id": agent.deviceID, "reason": reason})
khenaidoo6e55d9e2019-12-12 18:26:26 -0500833
Kent Hagermanf6db9f12020-07-22 17:16:19 -0400834 cloned := agent.cloneDeviceWithoutLock()
npujar1d86a522019-11-14 17:11:16 +0530835 cloned.Reason = reason
Kent Hagermanf6db9f12020-07-22 17:16:19 -0400836 return agent.updateDeviceAndReleaseLock(ctx, cloned)
Mahir Gunyelfdee9212019-10-16 16:52:21 -0700837}
kesavandbc2d1622020-01-21 00:42:01 -0500838
Kent Hagerman2b216042020-04-03 18:28:56 -0400839func (agent *Agent) ChildDeviceLost(ctx context.Context, device *voltha.Device) error {
divyadesaicb8b59d2020-08-18 09:55:47 +0000840 logger.Debugw(ctx, "childDeviceLost", log.Fields{"child-device-id": device.Id, "parent-device-id": agent.deviceID})
Chaitrashree G S543df3e2020-02-24 22:36:54 -0500841
Kent Hagerman2a07b862020-06-19 15:23:07 -0400842 // 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 }
khenaidoo442e7c72020-03-10 16:13:48 -0400851 }
Kent Hagerman2a07b862020-06-19 15:23:07 -0400852 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()
khenaidoo442e7c72020-03-10 16:13:48 -0400859 }
Chaitrashree G S543df3e2020-02-24 22:36:54 -0500860 }
861
khenaidoo442e7c72020-03-10 16:13:48 -0400862 //send request to adapter
Rohan Agrawalcf12f202020-08-03 04:42:01 +0000863 subCtx, cancel := context.WithTimeout(log.WithSpanFromContext(context.Background(), ctx), agent.defaultTimeout)
Kent Hagerman2b216042020-04-03 18:28:56 -0400864 ch, err := agent.adapterProxy.ChildDeviceLost(ctx, agent.deviceType, agent.deviceID, device.ParentPortNo, device.ProxyAddress.OnuId)
khenaidoo442e7c72020-03-10 16:13:48 -0400865 if err != nil {
866 cancel()
867 return err
Chaitrashree G S543df3e2020-02-24 22:36:54 -0500868 }
khenaidoo442e7c72020-03-10 16:13:48 -0400869 go agent.waitForAdapterResponse(subCtx, cancel, "childDeviceLost", ch, agent.onSuccess, agent.onFailure)
Chaitrashree G S543df3e2020-02-24 22:36:54 -0500870 return nil
Chaitrashree G S543df3e2020-02-24 22:36:54 -0500871}
onkarkundargi87285252020-01-27 11:34:52 +0530872
Kent Hagerman2b216042020-04-03 18:28:56 -0400873func (agent *Agent) startOmciTest(ctx context.Context, omcitestrequest *voltha.OmciTestRequest) (*voltha.TestResponse, error) {
onkarkundargi87285252020-01-27 11:34:52 +0530874 if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil {
875 return nil, err
876 }
877
Kent Hagermanf6db9f12020-07-22 17:16:19 -0400878 cloned := agent.cloneDeviceWithoutLock()
Matteo Scandolod525ae32020-04-02 17:27:29 -0700879
Kent Hagermanf6db9f12020-07-22 17:16:19 -0400880 if cloned.Adapter == "" {
881 adapterName, err := agent.adapterMgr.GetAdapterType(cloned.Type)
Matteo Scandolod525ae32020-04-02 17:27:29 -0700882 if err != nil {
883 agent.requestQueue.RequestComplete()
884 return nil, err
885 }
Kent Hagermanf6db9f12020-07-22 17:16:19 -0400886 cloned.Adapter = adapterName
onkarkundargi87285252020-01-27 11:34:52 +0530887 }
888
889 // Send request to the adapter
Kent Hagermanf6db9f12020-07-22 17:16:19 -0400890 ch, err := agent.adapterProxy.StartOmciTest(ctx, cloned, omcitestrequest)
onkarkundargi87285252020-01-27 11:34:52 +0530891 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 Agrawal31f21802020-06-12 05:38:46 +0000910 logger.Debugw(ctx, "Omci_test_Request-Success-device-agent", log.Fields{"testResp": testResp})
onkarkundargi87285252020-01-27 11:34:52 +0530911 return testResp, nil
912}
Dinesh Belwalkarc1129f12020-02-27 10:41:33 -0800913
914func (agent *Agent) getExtValue(ctx context.Context, pdevice *voltha.Device, cdevice *voltha.Device, valueparam *voltha.ValueSpecifier) (*voltha.ReturnValues, error) {
Girish Kumar3e8ee212020-08-19 17:50:11 +0000915 logger.Debugw(ctx, "getExtValue", log.Fields{"device-id": agent.deviceID, "onuid": valueparam.Id, "valuetype": valueparam.Value})
Dinesh Belwalkarc1129f12020-02-27 10:41:33 -0800916 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 Agrawal31f21802020-06-12 05:38:46 +0000941 logger.Debugw(ctx, "getExtValue-Success-device-agent", log.Fields{"Resp": Resp})
Dinesh Belwalkarc1129f12020-02-27 10:41:33 -0800942 return Resp, nil
943}
dpaul62686312020-06-23 14:17:36 +0530944
945func (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 Siddiqui1cf95042020-11-19 00:42:56 +0530971
972func (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
1006func (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}