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