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 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 17 | package core |
| 18 | |
| 19 | import ( |
| 20 | "context" |
Matteo Scandolo | 360605d | 2019-11-05 18:29:17 -0800 | [diff] [blame] | 21 | "encoding/hex" |
khenaidoo | 3ab3488 | 2019-05-02 21:33:30 -0400 | [diff] [blame] | 22 | "fmt" |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 23 | "github.com/golang/protobuf/ptypes" |
| 24 | "github.com/opencord/voltha-lib-go/v3/pkg/kafka" |
Chaitrashree G S | a773e99 | 2019-09-09 21:04:15 -0400 | [diff] [blame] | 25 | "reflect" |
| 26 | "sync" |
| 27 | "time" |
| 28 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 29 | "github.com/gogo/protobuf/proto" |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 30 | "github.com/opencord/voltha-go/db/model" |
Scott Baker | b671a86 | 2019-10-24 10:53:40 -0700 | [diff] [blame] | 31 | coreutils "github.com/opencord/voltha-go/rw_core/utils" |
serkant.uluderya | 2ae470f | 2020-01-21 11:13:09 -0800 | [diff] [blame] | 32 | fu "github.com/opencord/voltha-lib-go/v3/pkg/flows" |
| 33 | "github.com/opencord/voltha-lib-go/v3/pkg/log" |
| 34 | ic "github.com/opencord/voltha-protos/v3/go/inter_container" |
| 35 | ofp "github.com/opencord/voltha-protos/v3/go/openflow_13" |
| 36 | "github.com/opencord/voltha-protos/v3/go/voltha" |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 37 | "google.golang.org/grpc/codes" |
| 38 | "google.golang.org/grpc/status" |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 39 | ) |
| 40 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 41 | // DeviceAgent represents device agent attributes |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 42 | type DeviceAgent struct { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 43 | deviceID string |
| 44 | parentID string |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 45 | deviceType string |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 46 | isRootdevice bool |
khenaidoo | 9a46896 | 2018-09-19 15:33:13 -0400 | [diff] [blame] | 47 | adapterProxy *AdapterProxy |
serkant.uluderya | 334479d | 2019-04-10 08:26:15 -0700 | [diff] [blame] | 48 | adapterMgr *AdapterManager |
khenaidoo | 9a46896 | 2018-09-19 15:33:13 -0400 | [diff] [blame] | 49 | deviceMgr *DeviceManager |
| 50 | clusterDataProxy *model.Proxy |
| 51 | exitChannel chan int |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 52 | device *voltha.Device |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 53 | requestQueue *coreutils.RequestQueue |
| 54 | defaultTimeout time.Duration |
| 55 | startOnce sync.Once |
| 56 | stopOnce sync.Once |
| 57 | stopped bool |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 58 | } |
| 59 | |
Scott Baker | 8067860 | 2019-11-14 16:57:36 -0800 | [diff] [blame] | 60 | //newDeviceAgent creates a new device agent. The device will be initialized when start() is called. |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 61 | func newDeviceAgent(ap *AdapterProxy, device *voltha.Device, deviceMgr *DeviceManager, cdProxy *model.Proxy, timeout time.Duration) *DeviceAgent { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 62 | var agent DeviceAgent |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 63 | agent.adapterProxy = ap |
Scott Baker | 8067860 | 2019-11-14 16:57:36 -0800 | [diff] [blame] | 64 | if device.Id == "" { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 65 | agent.deviceID = CreateDeviceID() |
Scott Baker | 8067860 | 2019-11-14 16:57:36 -0800 | [diff] [blame] | 66 | } else { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 67 | agent.deviceID = device.Id |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 68 | } |
Scott Baker | 8067860 | 2019-11-14 16:57:36 -0800 | [diff] [blame] | 69 | |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 70 | agent.isRootdevice = device.Root |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 71 | agent.parentID = device.ParentId |
Scott Baker | 8067860 | 2019-11-14 16:57:36 -0800 | [diff] [blame] | 72 | agent.deviceType = device.Type |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 73 | agent.deviceMgr = deviceMgr |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 74 | agent.adapterMgr = deviceMgr.adapterMgr |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 75 | agent.exitChannel = make(chan int, 1) |
khenaidoo | 9a46896 | 2018-09-19 15:33:13 -0400 | [diff] [blame] | 76 | agent.clusterDataProxy = cdProxy |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 77 | agent.defaultTimeout = timeout |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 78 | agent.device = proto.Clone(device).(*voltha.Device) |
Kent Hagerman | 730cbdf | 2020-03-31 12:22:08 -0400 | [diff] [blame] | 79 | agent.requestQueue = coreutils.NewRequestQueue() |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 80 | return &agent |
| 81 | } |
| 82 | |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 83 | // start() saves the device to the data model and registers for callbacks on that device if deviceToCreate!=nil. |
| 84 | // 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] | 85 | // was started. |
| 86 | func (agent *DeviceAgent) start(ctx context.Context, deviceToCreate *voltha.Device) (*voltha.Device, error) { |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 87 | needToStart := false |
| 88 | if agent.startOnce.Do(func() { needToStart = true }); !needToStart { |
| 89 | return agent.getDevice(ctx) |
| 90 | } |
| 91 | var startSucceeded bool |
| 92 | defer func() { |
| 93 | if !startSucceeded { |
| 94 | if err := agent.stop(ctx); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 95 | logger.Errorw("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] | 96 | } |
| 97 | } |
| 98 | }() |
Scott Baker | 8067860 | 2019-11-14 16:57:36 -0800 | [diff] [blame] | 99 | |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 100 | var device *voltha.Device |
Scott Baker | 8067860 | 2019-11-14 16:57:36 -0800 | [diff] [blame] | 101 | if deviceToCreate == nil { |
| 102 | // Load the existing device |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 103 | loadedDevice, err := agent.clusterDataProxy.Get(ctx, "/devices/"+agent.deviceID, 1, true, "") |
| 104 | if err != nil { |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 105 | return nil, err |
| 106 | } |
| 107 | if loadedDevice != nil { |
Scott Baker | 8067860 | 2019-11-14 16:57:36 -0800 | [diff] [blame] | 108 | var ok bool |
| 109 | if device, ok = loadedDevice.(*voltha.Device); ok { |
| 110 | agent.deviceType = device.Adapter |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 111 | agent.device = proto.Clone(device).(*voltha.Device) |
Scott Baker | 8067860 | 2019-11-14 16:57:36 -0800 | [diff] [blame] | 112 | } else { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 113 | return nil, status.Errorf(codes.NotFound, "device-%s", agent.deviceID) |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 114 | } |
| 115 | } else { |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 116 | return nil, status.Errorf(codes.NotFound, "device-%s-loading-failed", agent.deviceID) |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 117 | } |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 118 | logger.Infow("device-loaded-from-dB", log.Fields{"device-id": agent.deviceID}) |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 119 | } else { |
Scott Baker | 8067860 | 2019-11-14 16:57:36 -0800 | [diff] [blame] | 120 | // Create a new device |
| 121 | // Assumption is that AdminState, FlowGroups, and Flows are unitialized since this |
| 122 | // is a new device, so populate them here before passing the device to clusterDataProxy.AddWithId. |
| 123 | // agent.deviceId will also have been set during newDeviceAgent(). |
| 124 | device = (proto.Clone(deviceToCreate)).(*voltha.Device) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 125 | device.Id = agent.deviceID |
Scott Baker | 8067860 | 2019-11-14 16:57:36 -0800 | [diff] [blame] | 126 | device.AdminState = voltha.AdminState_PREPROVISIONED |
| 127 | device.FlowGroups = &ofp.FlowGroups{Items: nil} |
| 128 | device.Flows = &ofp.Flows{Items: nil} |
| 129 | if !deviceToCreate.GetRoot() && deviceToCreate.ProxyAddress != nil { |
| 130 | // Set the default vlan ID to the one specified by the parent adapter. It can be |
| 131 | // overwritten by the child adapter during a device update request |
| 132 | device.Vlan = deviceToCreate.ProxyAddress.ChannelId |
| 133 | } |
| 134 | |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 135 | // Add the initial device to the local model |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 136 | added, err := agent.clusterDataProxy.AddWithID(ctx, "/devices", agent.deviceID, device, "") |
| 137 | if err != nil { |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 138 | return nil, err |
| 139 | } |
| 140 | if added == nil { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 141 | return nil, status.Errorf(codes.Aborted, "failed-adding-device-%s", agent.deviceID) |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 142 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 143 | agent.device = device |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 144 | } |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 145 | |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 146 | startSucceeded = true |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 147 | logger.Debugw("device-agent-started", log.Fields{"device-id": agent.deviceID}) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 148 | |
| 149 | return agent.getDevice(ctx) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 150 | } |
| 151 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 152 | // stop stops the device agent. Not much to do for now |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 153 | func (agent *DeviceAgent) stop(ctx context.Context) error { |
| 154 | needToStop := false |
| 155 | if agent.stopOnce.Do(func() { needToStop = true }); !needToStop { |
| 156 | return nil |
| 157 | } |
| 158 | if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
| 159 | return err |
| 160 | } |
| 161 | defer agent.requestQueue.RequestComplete() |
khenaidoo | 4908535 | 2020-01-13 19:15:43 -0500 | [diff] [blame] | 162 | |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 163 | logger.Infow("stopping-device-agent", log.Fields{"deviceId": agent.deviceID, "parentId": agent.parentID}) |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 164 | |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 165 | // Remove the device from the KV store |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 166 | removed, err := agent.clusterDataProxy.Remove(ctx, "/devices/"+agent.deviceID, "") |
| 167 | if err != nil { |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 168 | return err |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 169 | } |
| 170 | if removed == nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 171 | logger.Debugw("device-already-removed", log.Fields{"device-id": agent.deviceID}) |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 172 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 173 | |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 174 | close(agent.exitChannel) |
| 175 | |
| 176 | agent.stopped = true |
| 177 | |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 178 | logger.Infow("device-agent-stopped", log.Fields{"device-id": agent.deviceID, "parent-id": agent.parentID}) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 179 | |
| 180 | return nil |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 181 | } |
| 182 | |
Scott Baker | 8067860 | 2019-11-14 16:57:36 -0800 | [diff] [blame] | 183 | // Load the most recent state from the KVStore for the device. |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 184 | func (agent *DeviceAgent) reconcileWithKVStore(ctx context.Context) { |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 185 | if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 186 | logger.Warnw("request-aborted", log.Fields{"device-id": agent.deviceID, "error": err}) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 187 | return |
| 188 | } |
| 189 | defer agent.requestQueue.RequestComplete() |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 190 | logger.Debug("reconciling-device-agent-devicetype") |
Scott Baker | 8067860 | 2019-11-14 16:57:36 -0800 | [diff] [blame] | 191 | // TODO: context timeout |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 192 | device, err := agent.clusterDataProxy.Get(ctx, "/devices/"+agent.deviceID, 1, true, "") |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 193 | if err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 194 | logger.Errorw("kv-get-failed", log.Fields{"device-id": agent.deviceID, "error": err}) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 195 | return |
| 196 | } |
| 197 | if device != nil { |
Scott Baker | 8067860 | 2019-11-14 16:57:36 -0800 | [diff] [blame] | 198 | if d, ok := device.(*voltha.Device); ok { |
| 199 | agent.deviceType = d.Adapter |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 200 | agent.device = proto.Clone(d).(*voltha.Device) |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 201 | logger.Debugw("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] | 202 | } |
| 203 | } |
| 204 | } |
| 205 | |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 206 | // onSuccess is a common callback for scenarios where we receive a nil response following a request to an adapter |
| 207 | // and the only action required is to publish a successful result on kafka |
| 208 | func (agent *DeviceAgent) onSuccess(rpc string, response interface{}, reqArgs ...interface{}) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 209 | logger.Debugw("response successful", log.Fields{"rpc": rpc, "device-id": agent.deviceID}) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 210 | // TODO: Post success message onto kafka |
| 211 | } |
| 212 | |
| 213 | // onFailure is a common callback for scenarios where we receive an error response following a request to an adapter |
| 214 | // and the only action required is to publish the failed result on kafka |
| 215 | func (agent *DeviceAgent) onFailure(rpc string, response interface{}, reqArgs ...interface{}) { |
| 216 | if res, ok := response.(error); ok { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 217 | logger.Errorw("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] | 218 | } else { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 219 | logger.Errorw("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] | 220 | } |
| 221 | // TODO: Post failure message onto kafka |
| 222 | } |
| 223 | |
| 224 | func (agent *DeviceAgent) waitForAdapterResponse(ctx context.Context, cancel context.CancelFunc, rpc string, ch chan *kafka.RpcResponse, |
| 225 | onSuccess coreutils.ResponseCallback, onFailure coreutils.ResponseCallback, reqArgs ...interface{}) { |
| 226 | defer cancel() |
| 227 | select { |
| 228 | case rpcResponse, ok := <-ch: |
| 229 | if !ok { |
| 230 | onFailure(rpc, status.Errorf(codes.Aborted, "channel-closed"), reqArgs) |
| 231 | } else if rpcResponse.Err != nil { |
| 232 | onFailure(rpc, rpcResponse.Err, reqArgs) |
| 233 | } else { |
| 234 | onSuccess(rpc, rpcResponse.Reply, reqArgs) |
| 235 | } |
| 236 | case <-ctx.Done(): |
| 237 | onFailure(rpc, ctx.Err(), reqArgs) |
| 238 | } |
| 239 | } |
| 240 | |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 241 | // getDevice returns the device data from cache |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 242 | func (agent *DeviceAgent) getDevice(ctx context.Context) (*voltha.Device, error) { |
| 243 | if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
| 244 | return nil, err |
| 245 | } |
| 246 | defer agent.requestQueue.RequestComplete() |
| 247 | return proto.Clone(agent.device).(*voltha.Device), nil |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 248 | } |
| 249 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 250 | // getDeviceWithoutLock is a helper function to be used ONLY by any device agent function AFTER it has acquired the device lock. |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 251 | func (agent *DeviceAgent) getDeviceWithoutLock() *voltha.Device { |
| 252 | return proto.Clone(agent.device).(*voltha.Device) |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 253 | } |
| 254 | |
khenaidoo | 3ab3488 | 2019-05-02 21:33:30 -0400 | [diff] [blame] | 255 | // enableDevice activates a preprovisioned or a disable device |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 256 | func (agent *DeviceAgent) enableDevice(ctx context.Context) error { |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 257 | if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
| 258 | return err |
| 259 | } |
| 260 | defer agent.requestQueue.RequestComplete() |
| 261 | |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 262 | logger.Debugw("enableDevice", log.Fields{"device-id": agent.deviceID}) |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 263 | |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 264 | cloned := agent.getDeviceWithoutLock() |
| 265 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 266 | // 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] | 267 | // 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] | 268 | // with the adapter then we need to know the adapter that will handle this request |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 269 | adapterName, err := agent.adapterMgr.getAdapterName(cloned.Type) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 270 | if err != nil { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 271 | return err |
| 272 | } |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 273 | cloned.Adapter = adapterName |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 274 | |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 275 | if cloned.AdminState == voltha.AdminState_ENABLED { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 276 | logger.Debugw("device-already-enabled", log.Fields{"device-id": agent.deviceID}) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 277 | return nil |
| 278 | } |
| 279 | |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 280 | if cloned.AdminState == voltha.AdminState_DELETED { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 281 | // This is a temporary state when a device is deleted before it gets removed from the model. |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 282 | err = status.Error(codes.FailedPrecondition, fmt.Sprintf("cannot-enable-a-deleted-device: %s ", cloned.Id)) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 283 | return err |
| 284 | } |
| 285 | |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 286 | previousAdminState := cloned.AdminState |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 287 | |
| 288 | // Update the Admin State and set the operational state to activating before sending the request to the |
| 289 | // Adapters |
Kent Hagerman | d9cc2e9 | 2019-11-04 13:28:15 -0500 | [diff] [blame^] | 290 | if err := agent.updateDeviceStateInStoreWithoutLock(ctx, cloned, voltha.AdminState_ENABLED, cloned.ConnectStatus, voltha.OperStatus_ACTIVATING); err != nil { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 291 | return err |
| 292 | } |
| 293 | |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 294 | // Adopt the device if it was in pre-provision state. In all other cases, try to re-enable it. |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 295 | device := proto.Clone(cloned).(*voltha.Device) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 296 | var ch chan *kafka.RpcResponse |
| 297 | subCtx, cancel := context.WithTimeout(context.Background(), agent.defaultTimeout) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 298 | if previousAdminState == voltha.AdminState_PREPROVISIONED { |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 299 | ch, err = agent.adapterProxy.adoptDevice(subCtx, device) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 300 | } else { |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 301 | ch, err = agent.adapterProxy.reEnableDevice(subCtx, device) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 302 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 303 | if err != nil { |
| 304 | cancel() |
| 305 | return err |
| 306 | } |
| 307 | // Wait for response |
| 308 | go agent.waitForAdapterResponse(subCtx, cancel, "enableDevice", ch, agent.onSuccess, agent.onFailure) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 309 | return nil |
| 310 | } |
| 311 | |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 312 | func (agent *DeviceAgent) waitForAdapterFlowResponse(ctx context.Context, cancel context.CancelFunc, ch chan *kafka.RpcResponse, response coreutils.Response) { |
| 313 | defer cancel() |
| 314 | select { |
| 315 | case rpcResponse, ok := <-ch: |
| 316 | if !ok { |
| 317 | response.Error(status.Errorf(codes.Aborted, "channel-closed")) |
| 318 | } else if rpcResponse.Err != nil { |
| 319 | response.Error(rpcResponse.Err) |
| 320 | } else { |
| 321 | response.Done() |
| 322 | } |
| 323 | case <-ctx.Done(): |
| 324 | response.Error(ctx.Err()) |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 325 | } |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 326 | } |
| 327 | |
khenaidoo | b2121e5 | 2019-12-16 17:17:22 -0500 | [diff] [blame] | 328 | //deleteFlowWithoutPreservingOrder removes a flow specified by index from the flows slice. This function will |
| 329 | //panic if the index is out of range. |
| 330 | func deleteFlowWithoutPreservingOrder(flows []*ofp.OfpFlowStats, index int) []*ofp.OfpFlowStats { |
| 331 | flows[index] = flows[len(flows)-1] |
| 332 | flows[len(flows)-1] = nil |
| 333 | return flows[:len(flows)-1] |
| 334 | } |
| 335 | |
| 336 | //deleteGroupWithoutPreservingOrder removes a group specified by index from the groups slice. This function will |
| 337 | //panic if the index is out of range. |
| 338 | func deleteGroupWithoutPreservingOrder(groups []*ofp.OfpGroupEntry, index int) []*ofp.OfpGroupEntry { |
| 339 | groups[index] = groups[len(groups)-1] |
| 340 | groups[len(groups)-1] = nil |
| 341 | return groups[:len(groups)-1] |
| 342 | } |
| 343 | |
| 344 | func flowsToUpdateToDelete(newFlows, existingFlows []*ofp.OfpFlowStats) (updatedNewFlows, flowsToDelete, updatedAllFlows []*ofp.OfpFlowStats) { |
| 345 | // Process flows |
| 346 | for _, flow := range existingFlows { |
| 347 | if idx := fu.FindFlows(newFlows, flow); idx == -1 { |
| 348 | updatedAllFlows = append(updatedAllFlows, flow) |
| 349 | } else { |
| 350 | // We have a matching flow (i.e. the following field matches: "TableId", "Priority", "Flags", "Cookie", |
| 351 | // "Match". If this is an exact match (i.e. all other fields matches as well) then this flow will be |
| 352 | // ignored. Otherwise, the previous flow will be deleted and the new one added |
| 353 | if proto.Equal(newFlows[idx], flow) { |
| 354 | // Flow already exist, remove it from the new flows but keep it in the updated flows slice |
| 355 | newFlows = deleteFlowWithoutPreservingOrder(newFlows, idx) |
| 356 | updatedAllFlows = append(updatedAllFlows, flow) |
| 357 | } else { |
| 358 | // Minor change to flow, delete old and add new one |
| 359 | flowsToDelete = append(flowsToDelete, flow) |
| 360 | } |
| 361 | } |
| 362 | } |
| 363 | updatedAllFlows = append(updatedAllFlows, newFlows...) |
| 364 | return newFlows, flowsToDelete, updatedAllFlows |
| 365 | } |
| 366 | |
| 367 | func groupsToUpdateToDelete(newGroups, existingGroups []*ofp.OfpGroupEntry) (updatedNewGroups, groupsToDelete, updatedAllGroups []*ofp.OfpGroupEntry) { |
| 368 | for _, group := range existingGroups { |
| 369 | if idx := fu.FindGroup(newGroups, group.Desc.GroupId); idx == -1 { // does not exist now |
| 370 | updatedAllGroups = append(updatedAllGroups, group) |
| 371 | } else { |
| 372 | // Follow same logic as flows |
| 373 | if proto.Equal(newGroups[idx], group) { |
| 374 | // Group already exist, remove it from the new groups |
| 375 | newGroups = deleteGroupWithoutPreservingOrder(newGroups, idx) |
| 376 | updatedAllGroups = append(updatedAllGroups, group) |
| 377 | } else { |
| 378 | // Minor change to group, delete old and add new one |
| 379 | groupsToDelete = append(groupsToDelete, group) |
| 380 | } |
| 381 | } |
| 382 | } |
| 383 | updatedAllGroups = append(updatedAllGroups, newGroups...) |
| 384 | return newGroups, groupsToDelete, updatedAllGroups |
| 385 | } |
| 386 | |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 387 | func (agent *DeviceAgent) addFlowsAndGroupsToAdapter(ctx context.Context, newFlows []*ofp.OfpFlowStats, newGroups []*ofp.OfpGroupEntry, flowMetadata *voltha.FlowMetadata) (coreutils.Response, error) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 388 | logger.Debugw("add-flows-groups-to-adapters", log.Fields{"device-id": agent.deviceID, "flows": newFlows, "groups": newGroups, "flow-metadata": flowMetadata}) |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 389 | |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 390 | if (len(newFlows) | len(newGroups)) == 0 { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 391 | logger.Debugw("nothing-to-update", log.Fields{"device-id": agent.deviceID, "flows": newFlows, "groups": newGroups}) |
A R Karthick | 5c28f55 | 2019-12-11 22:47:44 -0800 | [diff] [blame] | 392 | return coreutils.DoneResponse(), nil |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 393 | } |
| 394 | |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 395 | if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
| 396 | return coreutils.DoneResponse(), err |
| 397 | } |
| 398 | defer agent.requestQueue.RequestComplete() |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 399 | |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 400 | device := agent.getDeviceWithoutLock() |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 401 | dType := agent.adapterMgr.getDeviceType(device.Type) |
| 402 | if dType == nil { |
| 403 | return coreutils.DoneResponse(), status.Errorf(codes.FailedPrecondition, "non-existent-device-type-%s", device.Type) |
| 404 | } |
| 405 | |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 406 | existingFlows := proto.Clone(device.Flows).(*voltha.Flows) |
| 407 | existingGroups := proto.Clone(device.FlowGroups).(*ofp.FlowGroups) |
| 408 | |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 409 | // Process flows |
khenaidoo | b2121e5 | 2019-12-16 17:17:22 -0500 | [diff] [blame] | 410 | newFlows, flowsToDelete, updatedAllFlows := flowsToUpdateToDelete(newFlows, existingFlows.Items) |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 411 | |
| 412 | // Process groups |
khenaidoo | b2121e5 | 2019-12-16 17:17:22 -0500 | [diff] [blame] | 413 | newGroups, groupsToDelete, updatedAllGroups := groupsToUpdateToDelete(newGroups, existingGroups.Items) |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 414 | |
| 415 | // Sanity check |
khenaidoo | b2121e5 | 2019-12-16 17:17:22 -0500 | [diff] [blame] | 416 | if (len(updatedAllFlows) | len(flowsToDelete) | len(updatedAllGroups) | len(groupsToDelete)) == 0 { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 417 | logger.Debugw("nothing-to-update", log.Fields{"device-id": agent.deviceID, "flows": newFlows, "groups": newGroups}) |
A R Karthick | 5c28f55 | 2019-12-11 22:47:44 -0800 | [diff] [blame] | 418 | return coreutils.DoneResponse(), nil |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 419 | } |
| 420 | |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 421 | // store the changed data |
| 422 | device.Flows = &voltha.Flows{Items: updatedAllFlows} |
| 423 | device.FlowGroups = &voltha.FlowGroups{Items: updatedAllGroups} |
| 424 | if err := agent.updateDeviceWithoutLock(ctx, device); err != nil { |
| 425 | return coreutils.DoneResponse(), status.Errorf(codes.Internal, "failure-updating-device-%s", agent.deviceID) |
khenaidoo | e7be133 | 2020-01-24 18:58:33 -0500 | [diff] [blame] | 426 | } |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 427 | |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 428 | // Send update to adapters |
| 429 | subCtx, cancel := context.WithTimeout(context.Background(), agent.defaultTimeout) |
| 430 | response := coreutils.NewResponse() |
| 431 | if !dType.AcceptsAddRemoveFlowUpdates { |
khenaidoo | b2121e5 | 2019-12-16 17:17:22 -0500 | [diff] [blame] | 432 | if len(updatedAllGroups) != 0 && reflect.DeepEqual(existingGroups.Items, updatedAllGroups) && len(updatedAllFlows) != 0 && reflect.DeepEqual(existingFlows.Items, updatedAllFlows) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 433 | logger.Debugw("nothing-to-update", log.Fields{"device-id": agent.deviceID, "flows": newFlows, "groups": newGroups}) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 434 | cancel() |
A R Karthick | 5c28f55 | 2019-12-11 22:47:44 -0800 | [diff] [blame] | 435 | return coreutils.DoneResponse(), nil |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 436 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 437 | rpcResponse, err := agent.adapterProxy.updateFlowsBulk(subCtx, device, &voltha.Flows{Items: updatedAllFlows}, &voltha.FlowGroups{Items: updatedAllGroups}, flowMetadata) |
| 438 | if err != nil { |
| 439 | cancel() |
| 440 | return coreutils.DoneResponse(), err |
| 441 | } |
| 442 | go agent.waitForAdapterFlowResponse(subCtx, cancel, rpcResponse, response) |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 443 | } else { |
| 444 | flowChanges := &ofp.FlowChanges{ |
Matt Jeanneret | 518b5a4 | 2019-10-29 10:30:46 -0400 | [diff] [blame] | 445 | ToAdd: &voltha.Flows{Items: newFlows}, |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 446 | ToRemove: &voltha.Flows{Items: flowsToDelete}, |
| 447 | } |
| 448 | groupChanges := &ofp.FlowGroupChanges{ |
Matt Jeanneret | 518b5a4 | 2019-10-29 10:30:46 -0400 | [diff] [blame] | 449 | ToAdd: &voltha.FlowGroups{Items: newGroups}, |
| 450 | ToRemove: &voltha.FlowGroups{Items: groupsToDelete}, |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 451 | ToUpdate: &voltha.FlowGroups{Items: []*ofp.OfpGroupEntry{}}, |
| 452 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 453 | rpcResponse, err := agent.adapterProxy.updateFlowsIncremental(subCtx, device, flowChanges, groupChanges, flowMetadata) |
| 454 | if err != nil { |
| 455 | cancel() |
| 456 | return coreutils.DoneResponse(), err |
| 457 | } |
| 458 | go agent.waitForAdapterFlowResponse(subCtx, cancel, rpcResponse, response) |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 459 | } |
A R Karthick | 5c28f55 | 2019-12-11 22:47:44 -0800 | [diff] [blame] | 460 | return response, nil |
| 461 | } |
| 462 | |
| 463 | //addFlowsAndGroups adds the "newFlows" and "newGroups" from the existing flows/groups and sends the update to the |
| 464 | //adapters |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 465 | func (agent *DeviceAgent) addFlowsAndGroups(ctx context.Context, newFlows []*ofp.OfpFlowStats, newGroups []*ofp.OfpGroupEntry, flowMetadata *voltha.FlowMetadata) error { |
| 466 | response, err := agent.addFlowsAndGroupsToAdapter(ctx, newFlows, newGroups, flowMetadata) |
A R Karthick | 5c28f55 | 2019-12-11 22:47:44 -0800 | [diff] [blame] | 467 | if err != nil { |
| 468 | return err |
| 469 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 470 | if errs := coreutils.WaitForNilOrErrorResponses(agent.defaultTimeout, response); errs != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 471 | logger.Warnw("no-adapter-response", log.Fields{"device-id": agent.deviceID, "result": errs}) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 472 | return status.Errorf(codes.Aborted, "flow-failure-device-%s", agent.deviceID) |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 473 | } |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 474 | return nil |
| 475 | } |
| 476 | |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 477 | func (agent *DeviceAgent) deleteFlowsAndGroupsFromAdapter(ctx context.Context, flowsToDel []*ofp.OfpFlowStats, groupsToDel []*ofp.OfpGroupEntry, flowMetadata *voltha.FlowMetadata) (coreutils.Response, error) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 478 | logger.Debugw("delete-flows-groups-from-adapter", log.Fields{"device-id": agent.deviceID, "flows": flowsToDel, "groups": groupsToDel}) |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 479 | |
| 480 | if (len(flowsToDel) | len(groupsToDel)) == 0 { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 481 | logger.Debugw("nothing-to-update", log.Fields{"device-id": agent.deviceID, "flows": flowsToDel, "groups": groupsToDel}) |
A R Karthick | 5c28f55 | 2019-12-11 22:47:44 -0800 | [diff] [blame] | 482 | return coreutils.DoneResponse(), nil |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 483 | } |
| 484 | |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 485 | if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
| 486 | return coreutils.DoneResponse(), err |
| 487 | } |
| 488 | defer agent.requestQueue.RequestComplete() |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 489 | |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 490 | device := agent.getDeviceWithoutLock() |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 491 | dType := agent.adapterMgr.getDeviceType(device.Type) |
| 492 | if dType == nil { |
| 493 | return coreutils.DoneResponse(), status.Errorf(codes.FailedPrecondition, "non-existent-device-type-%s", device.Type) |
| 494 | } |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 495 | |
| 496 | existingFlows := proto.Clone(device.Flows).(*voltha.Flows) |
| 497 | existingGroups := proto.Clone(device.FlowGroups).(*ofp.FlowGroups) |
| 498 | |
| 499 | var flowsToKeep []*ofp.OfpFlowStats |
| 500 | var groupsToKeep []*ofp.OfpGroupEntry |
| 501 | |
| 502 | // Process flows |
| 503 | for _, flow := range existingFlows.Items { |
| 504 | if idx := fu.FindFlows(flowsToDel, flow); idx == -1 { |
| 505 | flowsToKeep = append(flowsToKeep, flow) |
| 506 | } |
| 507 | } |
| 508 | |
| 509 | // Process groups |
| 510 | for _, group := range existingGroups.Items { |
| 511 | if fu.FindGroup(groupsToDel, group.Desc.GroupId) == -1 { // does not exist now |
| 512 | groupsToKeep = append(groupsToKeep, group) |
| 513 | } |
| 514 | } |
| 515 | |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 516 | logger.Debugw("deleteFlowsAndGroups", |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 517 | log.Fields{ |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 518 | "device-id": agent.deviceID, |
| 519 | "flows-to-del": len(flowsToDel), |
| 520 | "flows-to-keep": len(flowsToKeep), |
| 521 | "groups-to-del": len(groupsToDel), |
| 522 | "groups-to-keep": len(groupsToKeep), |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 523 | }) |
| 524 | |
| 525 | // Sanity check |
| 526 | if (len(flowsToKeep) | len(flowsToDel) | len(groupsToKeep) | len(groupsToDel)) == 0 { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 527 | logger.Debugw("nothing-to-update", log.Fields{"device-id": agent.deviceID, "flows-to-del": flowsToDel, "groups-to-del": groupsToDel}) |
A R Karthick | 5c28f55 | 2019-12-11 22:47:44 -0800 | [diff] [blame] | 528 | return coreutils.DoneResponse(), nil |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 529 | } |
| 530 | |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 531 | // store the changed data |
| 532 | device.Flows = &voltha.Flows{Items: flowsToKeep} |
| 533 | device.FlowGroups = &voltha.FlowGroups{Items: groupsToKeep} |
| 534 | if err := agent.updateDeviceWithoutLock(ctx, device); err != nil { |
| 535 | return coreutils.DoneResponse(), status.Errorf(codes.Internal, "failure-updating-%s", agent.deviceID) |
khenaidoo | e7be133 | 2020-01-24 18:58:33 -0500 | [diff] [blame] | 536 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 537 | |
| 538 | // Send update to adapters |
| 539 | subCtx, cancel := context.WithTimeout(context.Background(), agent.defaultTimeout) |
| 540 | response := coreutils.NewResponse() |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 541 | if !dType.AcceptsAddRemoveFlowUpdates { |
| 542 | if len(groupsToKeep) != 0 && reflect.DeepEqual(existingGroups.Items, groupsToKeep) && len(flowsToKeep) != 0 && reflect.DeepEqual(existingFlows.Items, flowsToKeep) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 543 | logger.Debugw("nothing-to-update", log.Fields{"deviceId": agent.deviceID, "flowsToDel": flowsToDel, "groupsToDel": groupsToDel}) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 544 | cancel() |
A R Karthick | 5c28f55 | 2019-12-11 22:47:44 -0800 | [diff] [blame] | 545 | return coreutils.DoneResponse(), nil |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 546 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 547 | rpcResponse, err := agent.adapterProxy.updateFlowsBulk(subCtx, device, &voltha.Flows{Items: flowsToKeep}, &voltha.FlowGroups{Items: groupsToKeep}, flowMetadata) |
| 548 | if err != nil { |
| 549 | cancel() |
| 550 | return coreutils.DoneResponse(), err |
| 551 | } |
| 552 | go agent.waitForAdapterFlowResponse(subCtx, cancel, rpcResponse, response) |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 553 | } else { |
| 554 | flowChanges := &ofp.FlowChanges{ |
| 555 | ToAdd: &voltha.Flows{Items: []*ofp.OfpFlowStats{}}, |
| 556 | ToRemove: &voltha.Flows{Items: flowsToDel}, |
| 557 | } |
| 558 | groupChanges := &ofp.FlowGroupChanges{ |
| 559 | ToAdd: &voltha.FlowGroups{Items: []*ofp.OfpGroupEntry{}}, |
| 560 | ToRemove: &voltha.FlowGroups{Items: groupsToDel}, |
| 561 | ToUpdate: &voltha.FlowGroups{Items: []*ofp.OfpGroupEntry{}}, |
| 562 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 563 | rpcResponse, err := agent.adapterProxy.updateFlowsIncremental(subCtx, device, flowChanges, groupChanges, flowMetadata) |
| 564 | if err != nil { |
| 565 | cancel() |
| 566 | return coreutils.DoneResponse(), err |
| 567 | } |
| 568 | go agent.waitForAdapterFlowResponse(subCtx, cancel, rpcResponse, response) |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 569 | } |
A R Karthick | 5c28f55 | 2019-12-11 22:47:44 -0800 | [diff] [blame] | 570 | return response, nil |
A R Karthick | 5c28f55 | 2019-12-11 22:47:44 -0800 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | //deleteFlowsAndGroups removes the "flowsToDel" and "groupsToDel" from the existing flows/groups and sends the update to the |
| 574 | //adapters |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 575 | func (agent *DeviceAgent) deleteFlowsAndGroups(ctx context.Context, flowsToDel []*ofp.OfpFlowStats, groupsToDel []*ofp.OfpGroupEntry, flowMetadata *voltha.FlowMetadata) error { |
| 576 | response, err := agent.deleteFlowsAndGroupsFromAdapter(ctx, flowsToDel, groupsToDel, flowMetadata) |
A R Karthick | 5c28f55 | 2019-12-11 22:47:44 -0800 | [diff] [blame] | 577 | if err != nil { |
| 578 | return err |
| 579 | } |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 580 | if res := coreutils.WaitForNilOrErrorResponses(agent.defaultTimeout, response); res != nil { |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 581 | return status.Errorf(codes.Aborted, "errors-%s", res) |
| 582 | } |
| 583 | return nil |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 584 | } |
| 585 | |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 586 | func (agent *DeviceAgent) updateFlowsAndGroupsToAdapter(ctx context.Context, updatedFlows []*ofp.OfpFlowStats, updatedGroups []*ofp.OfpGroupEntry, flowMetadata *voltha.FlowMetadata) (coreutils.Response, error) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 587 | logger.Debugw("updateFlowsAndGroups", log.Fields{"device-id": agent.deviceID, "flows": updatedFlows, "groups": updatedGroups}) |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 588 | |
| 589 | if (len(updatedFlows) | len(updatedGroups)) == 0 { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 590 | logger.Debugw("nothing-to-update", log.Fields{"device-id": agent.deviceID, "flows": updatedFlows, "groups": updatedGroups}) |
A R Karthick | 5c28f55 | 2019-12-11 22:47:44 -0800 | [diff] [blame] | 591 | return coreutils.DoneResponse(), nil |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 592 | } |
| 593 | |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 594 | if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
| 595 | return coreutils.DoneResponse(), err |
| 596 | } |
| 597 | defer agent.requestQueue.RequestComplete() |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 598 | |
| 599 | device := agent.getDeviceWithoutLock() |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 600 | if device.OperStatus != voltha.OperStatus_ACTIVE || device.ConnectStatus != voltha.ConnectStatus_REACHABLE || device.AdminState != voltha.AdminState_ENABLED { |
| 601 | return coreutils.DoneResponse(), status.Errorf(codes.FailedPrecondition, "invalid device states") |
| 602 | } |
| 603 | dType := agent.adapterMgr.getDeviceType(device.Type) |
| 604 | if dType == nil { |
| 605 | return coreutils.DoneResponse(), status.Errorf(codes.FailedPrecondition, "non-existent-device-type-%s", device.Type) |
| 606 | } |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 607 | |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 608 | existingFlows := proto.Clone(device.Flows).(*voltha.Flows) |
| 609 | existingGroups := proto.Clone(device.FlowGroups).(*ofp.FlowGroups) |
| 610 | |
| 611 | if len(updatedGroups) != 0 && reflect.DeepEqual(existingGroups.Items, updatedGroups) && len(updatedFlows) != 0 && reflect.DeepEqual(existingFlows.Items, updatedFlows) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 612 | logger.Debugw("nothing-to-update", log.Fields{"device-id": agent.deviceID, "flows": updatedFlows, "groups": updatedGroups}) |
A R Karthick | 5c28f55 | 2019-12-11 22:47:44 -0800 | [diff] [blame] | 613 | return coreutils.DoneResponse(), nil |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 614 | } |
| 615 | |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 616 | logger.Debugw("updating-flows-and-groups", |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 617 | log.Fields{ |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 618 | "device-id": agent.deviceID, |
| 619 | "updated-flows": updatedFlows, |
| 620 | "updated-groups": updatedGroups, |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 621 | }) |
| 622 | |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 623 | // store the updated data |
| 624 | device.Flows = &voltha.Flows{Items: updatedFlows} |
| 625 | device.FlowGroups = &voltha.FlowGroups{Items: updatedGroups} |
| 626 | if err := agent.updateDeviceWithoutLock(ctx, device); err != nil { |
| 627 | return coreutils.DoneResponse(), status.Errorf(codes.Internal, "failure-updating-%s", agent.deviceID) |
khenaidoo | e7be133 | 2020-01-24 18:58:33 -0500 | [diff] [blame] | 628 | } |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 629 | |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 630 | subCtx, cancel := context.WithTimeout(context.Background(), agent.defaultTimeout) |
| 631 | response := coreutils.NewResponse() |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 632 | // Process bulk flow update differently than incremental update |
| 633 | if !dType.AcceptsAddRemoveFlowUpdates { |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 634 | rpcResponse, err := agent.adapterProxy.updateFlowsBulk(subCtx, device, &voltha.Flows{Items: updatedFlows}, &voltha.FlowGroups{Items: updatedGroups}, nil) |
| 635 | if err != nil { |
| 636 | cancel() |
| 637 | return coreutils.DoneResponse(), err |
| 638 | } |
| 639 | go agent.waitForAdapterFlowResponse(subCtx, cancel, rpcResponse, response) |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 640 | } else { |
| 641 | var flowsToAdd []*ofp.OfpFlowStats |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 642 | var flowsToDelete []*ofp.OfpFlowStats |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 643 | var groupsToAdd []*ofp.OfpGroupEntry |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 644 | var groupsToDelete []*ofp.OfpGroupEntry |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 645 | |
| 646 | // Process flows |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 647 | for _, flow := range updatedFlows { |
| 648 | if idx := fu.FindFlows(existingFlows.Items, flow); idx == -1 { |
| 649 | flowsToAdd = append(flowsToAdd, flow) |
| 650 | } |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 651 | } |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 652 | for _, flow := range existingFlows.Items { |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 653 | if idx := fu.FindFlows(updatedFlows, flow); idx != -1 { |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 654 | flowsToDelete = append(flowsToDelete, flow) |
| 655 | } |
| 656 | } |
| 657 | |
| 658 | // Process groups |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 659 | for _, g := range updatedGroups { |
| 660 | if fu.FindGroup(existingGroups.Items, g.Desc.GroupId) == -1 { // does not exist now |
| 661 | groupsToAdd = append(groupsToAdd, g) |
| 662 | } |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 663 | } |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 664 | for _, group := range existingGroups.Items { |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 665 | if fu.FindGroup(updatedGroups, group.Desc.GroupId) != -1 { // does not exist now |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 666 | groupsToDelete = append(groupsToDelete, group) |
| 667 | } |
| 668 | } |
| 669 | |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 670 | logger.Debugw("updating-flows-and-groups", |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 671 | log.Fields{ |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 672 | "device-id": agent.deviceID, |
| 673 | "flows-to-add": flowsToAdd, |
| 674 | "flows-to-delete": flowsToDelete, |
| 675 | "groups-to-add": groupsToAdd, |
| 676 | "groups-to-delete": groupsToDelete, |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 677 | }) |
| 678 | |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 679 | // Sanity check |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 680 | if (len(flowsToAdd) | len(flowsToDelete) | len(groupsToAdd) | len(groupsToDelete) | len(updatedGroups)) == 0 { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 681 | logger.Debugw("nothing-to-update", log.Fields{"device-id": agent.deviceID, "flows": updatedFlows, "groups": updatedGroups}) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 682 | cancel() |
A R Karthick | 5c28f55 | 2019-12-11 22:47:44 -0800 | [diff] [blame] | 683 | return coreutils.DoneResponse(), nil |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 684 | } |
| 685 | |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 686 | flowChanges := &ofp.FlowChanges{ |
| 687 | ToAdd: &voltha.Flows{Items: flowsToAdd}, |
| 688 | ToRemove: &voltha.Flows{Items: flowsToDelete}, |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 689 | } |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 690 | groupChanges := &ofp.FlowGroupChanges{ |
| 691 | ToAdd: &voltha.FlowGroups{Items: groupsToAdd}, |
| 692 | ToRemove: &voltha.FlowGroups{Items: groupsToDelete}, |
| 693 | ToUpdate: &voltha.FlowGroups{Items: updatedGroups}, |
| 694 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 695 | rpcResponse, err := agent.adapterProxy.updateFlowsIncremental(subCtx, device, flowChanges, groupChanges, flowMetadata) |
| 696 | if err != nil { |
| 697 | cancel() |
| 698 | return coreutils.DoneResponse(), err |
| 699 | } |
| 700 | go agent.waitForAdapterFlowResponse(subCtx, cancel, rpcResponse, response) |
Kent Hagerman | 3c51397 | 2019-11-25 13:49:41 -0500 | [diff] [blame] | 701 | } |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 702 | |
A R Karthick | 5c28f55 | 2019-12-11 22:47:44 -0800 | [diff] [blame] | 703 | return response, nil |
| 704 | } |
| 705 | |
| 706 | //updateFlowsAndGroups replaces the existing flows and groups with "updatedFlows" and "updatedGroups" respectively. It |
| 707 | //also sends the updates to the adapters |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 708 | func (agent *DeviceAgent) updateFlowsAndGroups(ctx context.Context, updatedFlows []*ofp.OfpFlowStats, updatedGroups []*ofp.OfpGroupEntry, flowMetadata *voltha.FlowMetadata) error { |
| 709 | response, err := agent.updateFlowsAndGroupsToAdapter(ctx, updatedFlows, updatedGroups, flowMetadata) |
A R Karthick | 5c28f55 | 2019-12-11 22:47:44 -0800 | [diff] [blame] | 710 | if err != nil { |
| 711 | return err |
| 712 | } |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 713 | if res := coreutils.WaitForNilOrErrorResponses(agent.defaultTimeout, response); res != nil { |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 714 | return status.Errorf(codes.Aborted, "errors-%s", res) |
| 715 | } |
| 716 | return nil |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 717 | } |
| 718 | |
Girish Gowdra | 408cd96 | 2020-03-11 14:31:31 -0700 | [diff] [blame] | 719 | //deleteAllFlows deletes all flows in the device table |
| 720 | func (agent *DeviceAgent) deleteAllFlows(ctx context.Context) error { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 721 | logger.Debugw("deleteAllFlows", log.Fields{"deviceId": agent.deviceID}) |
Girish Gowdra | 408cd96 | 2020-03-11 14:31:31 -0700 | [diff] [blame] | 722 | if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
| 723 | return err |
| 724 | } |
| 725 | defer agent.requestQueue.RequestComplete() |
| 726 | |
| 727 | device := agent.getDeviceWithoutLock() |
| 728 | // purge all flows on the device by setting it to nil |
| 729 | device.Flows = &ofp.Flows{Items: nil} |
| 730 | if err := agent.updateDeviceWithoutLock(ctx, device); err != nil { |
| 731 | // The caller logs the error |
| 732 | return err |
| 733 | } |
| 734 | return nil |
| 735 | } |
| 736 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 737 | //disableDevice disable a device |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 738 | func (agent *DeviceAgent) disableDevice(ctx context.Context) 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() |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 743 | logger.Debugw("disableDevice", log.Fields{"device-id": agent.deviceID}) |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 744 | |
| 745 | cloned := agent.getDeviceWithoutLock() |
| 746 | |
| 747 | if cloned.AdminState == voltha.AdminState_DISABLED { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 748 | logger.Debugw("device-already-disabled", log.Fields{"id": agent.deviceID}) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 749 | return nil |
| 750 | } |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 751 | if cloned.AdminState == voltha.AdminState_PREPROVISIONED || |
| 752 | cloned.AdminState == voltha.AdminState_DELETED { |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 753 | 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] | 754 | } |
khenaidoo | 4554f7c | 2019-05-29 22:13:15 -0400 | [diff] [blame] | 755 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 756 | // Update the Admin State and operational state before sending the request out |
Kent Hagerman | d9cc2e9 | 2019-11-04 13:28:15 -0500 | [diff] [blame^] | 757 | if err := agent.updateDeviceStateInStoreWithoutLock(ctx, cloned, voltha.AdminState_DISABLED, cloned.ConnectStatus, voltha.OperStatus_UNKNOWN); err != nil { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 758 | return err |
| 759 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 760 | |
| 761 | subCtx, cancel := context.WithTimeout(context.Background(), agent.defaultTimeout) |
| 762 | ch, err := agent.adapterProxy.disableDevice(subCtx, proto.Clone(cloned).(*voltha.Device)) |
| 763 | if err != nil { |
| 764 | cancel() |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 765 | return err |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 766 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 767 | go agent.waitForAdapterResponse(subCtx, cancel, "disableDevice", ch, agent.onSuccess, agent.onFailure) |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 768 | |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 769 | return nil |
| 770 | } |
| 771 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 772 | func (agent *DeviceAgent) rebootDevice(ctx context.Context) error { |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 773 | if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 774 | return err |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 775 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 776 | defer agent.requestQueue.RequestComplete() |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 777 | logger.Debugw("rebootDevice", log.Fields{"device-id": agent.deviceID}) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 778 | |
| 779 | device := agent.getDeviceWithoutLock() |
| 780 | subCtx, cancel := context.WithTimeout(context.Background(), agent.defaultTimeout) |
| 781 | ch, err := agent.adapterProxy.rebootDevice(subCtx, device) |
| 782 | if err != nil { |
| 783 | cancel() |
| 784 | return err |
| 785 | } |
| 786 | go agent.waitForAdapterResponse(subCtx, cancel, "rebootDevice", ch, agent.onSuccess, agent.onFailure) |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 787 | return nil |
| 788 | } |
| 789 | |
| 790 | func (agent *DeviceAgent) deleteDevice(ctx context.Context) error { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 791 | logger.Debugw("deleteDevice", log.Fields{"device-id": agent.deviceID}) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 792 | if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
| 793 | return err |
| 794 | } |
| 795 | defer agent.requestQueue.RequestComplete() |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 796 | |
| 797 | cloned := agent.getDeviceWithoutLock() |
Chaitrashree G S | 543df3e | 2020-02-24 22:36:54 -0500 | [diff] [blame] | 798 | |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 799 | previousState := cloned.AdminState |
| 800 | |
| 801 | // No check is required when deleting a device. Changing the state to DELETE will trigger the removal of this |
| 802 | // device by the state machine |
Kent Hagerman | d9cc2e9 | 2019-11-04 13:28:15 -0500 | [diff] [blame^] | 803 | if err := agent.updateDeviceStateInStoreWithoutLock(ctx, cloned, voltha.AdminState_DELETED, cloned.ConnectStatus, cloned.OperStatus); err != nil { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 804 | return err |
| 805 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 806 | |
| 807 | // If the device was in pre-prov state (only parent device are in that state) then do not send the request to the |
| 808 | // adapter |
| 809 | if previousState != ic.AdminState_PREPROVISIONED { |
| 810 | subCtx, cancel := context.WithTimeout(context.Background(), agent.defaultTimeout) |
| 811 | ch, err := agent.adapterProxy.deleteDevice(subCtx, cloned) |
| 812 | if err != nil { |
| 813 | cancel() |
| 814 | return err |
| 815 | } |
| 816 | go agent.waitForAdapterResponse(subCtx, cancel, "deleteDevice", ch, agent.onSuccess, agent.onFailure) |
| 817 | } |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 818 | return nil |
| 819 | } |
| 820 | |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 821 | func (agent *DeviceAgent) setParentID(ctx context.Context, device *voltha.Device, parentID string) error { |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 822 | if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
| 823 | return err |
| 824 | } |
| 825 | defer agent.requestQueue.RequestComplete() |
| 826 | |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 827 | logger.Debugw("setParentId", log.Fields{"device-id": device.Id, "parent-id": parentID}) |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 828 | |
| 829 | cloned := agent.getDeviceWithoutLock() |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 830 | cloned.ParentId = parentID |
| 831 | // Store the device |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 832 | if err := agent.updateDeviceInStoreWithoutLock(ctx, cloned, false, ""); err != nil { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 833 | return err |
| 834 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 835 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 836 | return nil |
khenaidoo | ad06fd7 | 2019-10-28 12:26:05 -0400 | [diff] [blame] | 837 | } |
| 838 | |
khenaidoo | b312747 | 2019-07-24 21:04:55 -0400 | [diff] [blame] | 839 | func (agent *DeviceAgent) updatePmConfigs(ctx context.Context, pmConfigs *voltha.PmConfigs) error { |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 840 | if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
| 841 | return err |
| 842 | } |
| 843 | defer agent.requestQueue.RequestComplete() |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 844 | logger.Debugw("updatePmConfigs", log.Fields{"device-id": pmConfigs.Id}) |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 845 | |
| 846 | cloned := agent.getDeviceWithoutLock() |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 847 | cloned.PmConfigs = proto.Clone(pmConfigs).(*voltha.PmConfigs) |
| 848 | // Store the device |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 849 | if err := agent.updateDeviceInStoreWithoutLock(ctx, cloned, false, ""); err != nil { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 850 | return err |
| 851 | } |
| 852 | // Send the request to the adapter |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 853 | subCtx, cancel := context.WithTimeout(context.Background(), agent.defaultTimeout) |
| 854 | ch, err := agent.adapterProxy.updatePmConfigs(subCtx, cloned, pmConfigs) |
| 855 | if err != nil { |
| 856 | cancel() |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 857 | return err |
| 858 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 859 | go agent.waitForAdapterResponse(subCtx, cancel, "updatePmConfigs", ch, agent.onSuccess, agent.onFailure) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 860 | return nil |
khenaidoo | b312747 | 2019-07-24 21:04:55 -0400 | [diff] [blame] | 861 | } |
| 862 | |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 863 | func (agent *DeviceAgent) initPmConfigs(ctx context.Context, pmConfigs *voltha.PmConfigs) error { |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 864 | if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
| 865 | return err |
| 866 | } |
| 867 | defer agent.requestQueue.RequestComplete() |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 868 | logger.Debugw("initPmConfigs", log.Fields{"device-id": pmConfigs.Id}) |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 869 | |
| 870 | cloned := agent.getDeviceWithoutLock() |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 871 | cloned.PmConfigs = proto.Clone(pmConfigs).(*voltha.PmConfigs) |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 872 | updateCtx := context.WithValue(ctx, model.RequestTimestamp, time.Now().UnixNano()) |
Kent Hagerman | d9cc2e9 | 2019-11-04 13:28:15 -0500 | [diff] [blame^] | 873 | return agent.updateDeviceInStoreWithoutLock(updateCtx, cloned, false, "") |
khenaidoo | b312747 | 2019-07-24 21:04:55 -0400 | [diff] [blame] | 874 | } |
| 875 | |
| 876 | func (agent *DeviceAgent) listPmConfigs(ctx context.Context) (*voltha.PmConfigs, error) { |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 877 | if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
| 878 | return nil, err |
| 879 | } |
| 880 | defer agent.requestQueue.RequestComplete() |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 881 | logger.Debugw("listPmConfigs", log.Fields{"device-id": agent.deviceID}) |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 882 | |
| 883 | return agent.getDeviceWithoutLock().PmConfigs, nil |
khenaidoo | b312747 | 2019-07-24 21:04:55 -0400 | [diff] [blame] | 884 | } |
| 885 | |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 886 | func (agent *DeviceAgent) downloadImage(ctx context.Context, img *voltha.ImageDownload) (*voltha.OperationResp, error) { |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 887 | if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
| 888 | return nil, err |
| 889 | } |
| 890 | defer agent.requestQueue.RequestComplete() |
| 891 | |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 892 | logger.Debugw("downloadImage", log.Fields{"device-id": agent.deviceID}) |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 893 | |
| 894 | device := agent.getDeviceWithoutLock() |
| 895 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 896 | if device.AdminState != voltha.AdminState_ENABLED { |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 897 | return nil, status.Errorf(codes.FailedPrecondition, "device-id:%s, expected-admin-state:%s", agent.deviceID, voltha.AdminState_ENABLED) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 898 | } |
| 899 | // Save the image |
| 900 | clonedImg := proto.Clone(img).(*voltha.ImageDownload) |
| 901 | clonedImg.DownloadState = voltha.ImageDownload_DOWNLOAD_REQUESTED |
| 902 | cloned := proto.Clone(device).(*voltha.Device) |
| 903 | if cloned.ImageDownloads == nil { |
| 904 | cloned.ImageDownloads = []*voltha.ImageDownload{clonedImg} |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 905 | } else { |
| 906 | if device.AdminState != voltha.AdminState_ENABLED { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 907 | logger.Debugw("device-not-enabled", log.Fields{"id": agent.deviceID}) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 908 | return nil, status.Errorf(codes.FailedPrecondition, "deviceId:%s, expected-admin-state:%s", agent.deviceID, voltha.AdminState_ENABLED) |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 909 | } |
| 910 | // Save the image |
| 911 | clonedImg := proto.Clone(img).(*voltha.ImageDownload) |
Stephane Barbarie | df5479f | 2019-01-29 22:13:00 -0500 | [diff] [blame] | 912 | clonedImg.DownloadState = voltha.ImageDownload_DOWNLOAD_REQUESTED |
Kent Hagerman | d9cc2e9 | 2019-11-04 13:28:15 -0500 | [diff] [blame^] | 913 | if device.ImageDownloads == nil { |
| 914 | device.ImageDownloads = []*voltha.ImageDownload{clonedImg} |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 915 | } else { |
Kent Hagerman | d9cc2e9 | 2019-11-04 13:28:15 -0500 | [diff] [blame^] | 916 | device.ImageDownloads = append(device.ImageDownloads, clonedImg) |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 917 | } |
Kent Hagerman | d9cc2e9 | 2019-11-04 13:28:15 -0500 | [diff] [blame^] | 918 | if err := agent.updateDeviceStateInStoreWithoutLock(ctx, cloned, voltha.AdminState_DOWNLOADING_IMAGE, device.ConnectStatus, device.OperStatus); err != nil { |
Mahir Gunyel | b585167 | 2019-07-24 10:46:26 +0300 | [diff] [blame] | 919 | return nil, err |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 920 | } |
Kent Hagerman | d9cc2e9 | 2019-11-04 13:28:15 -0500 | [diff] [blame^] | 921 | |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 922 | // Send the request to the adapter |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 923 | subCtx, cancel := context.WithTimeout(context.Background(), agent.defaultTimeout) |
| 924 | ch, err := agent.adapterProxy.downloadImage(ctx, cloned, clonedImg) |
| 925 | if err != nil { |
| 926 | cancel() |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 927 | return nil, err |
| 928 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 929 | go agent.waitForAdapterResponse(subCtx, cancel, "downloadImage", ch, agent.onSuccess, agent.onFailure) |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 930 | } |
| 931 | return &voltha.OperationResp{Code: voltha.OperationResp_OPERATION_SUCCESS}, nil |
| 932 | } |
| 933 | |
| 934 | // isImageRegistered is a helper method to figure out if an image is already registered |
| 935 | func isImageRegistered(img *voltha.ImageDownload, device *voltha.Device) bool { |
| 936 | for _, image := range device.ImageDownloads { |
| 937 | if image.Id == img.Id && image.Name == img.Name { |
| 938 | return true |
| 939 | } |
| 940 | } |
| 941 | return false |
| 942 | } |
| 943 | |
| 944 | func (agent *DeviceAgent) cancelImageDownload(ctx context.Context, img *voltha.ImageDownload) (*voltha.OperationResp, error) { |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 945 | if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
| 946 | return nil, err |
| 947 | } |
| 948 | defer agent.requestQueue.RequestComplete() |
| 949 | |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 950 | logger.Debugw("cancelImageDownload", log.Fields{"device-id": agent.deviceID}) |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 951 | |
| 952 | device := agent.getDeviceWithoutLock() |
| 953 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 954 | // Verify whether the Image is in the list of image being downloaded |
| 955 | if !isImageRegistered(img, device) { |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 956 | return nil, status.Errorf(codes.FailedPrecondition, "device-id:%s, image-not-registered:%s", agent.deviceID, img.Name) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 957 | } |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 958 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 959 | // Update image download state |
Kent Hagerman | d9cc2e9 | 2019-11-04 13:28:15 -0500 | [diff] [blame^] | 960 | for _, image := range device.ImageDownloads { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 961 | if image.Id == img.Id && image.Name == img.Name { |
| 962 | image.DownloadState = voltha.ImageDownload_DOWNLOAD_CANCELLED |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 963 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 964 | } |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 965 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 966 | if device.AdminState == voltha.AdminState_DOWNLOADING_IMAGE { |
| 967 | // Set the device to Enabled |
Kent Hagerman | d9cc2e9 | 2019-11-04 13:28:15 -0500 | [diff] [blame^] | 968 | if err := agent.updateDeviceStateInStoreWithoutLock(ctx, device, voltha.AdminState_ENABLED, device.ConnectStatus, device.OperStatus); err != nil { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 969 | return nil, err |
| 970 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 971 | subCtx, cancel := context.WithTimeout(context.Background(), agent.defaultTimeout) |
| 972 | ch, err := agent.adapterProxy.cancelImageDownload(subCtx, device, img) |
| 973 | if err != nil { |
| 974 | cancel() |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 975 | return nil, err |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 976 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 977 | go agent.waitForAdapterResponse(subCtx, cancel, "cancelImageDownload", ch, agent.onSuccess, agent.onFailure) |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 978 | } |
| 979 | return &voltha.OperationResp{Code: voltha.OperationResp_OPERATION_SUCCESS}, nil |
serkant.uluderya | 334479d | 2019-04-10 08:26:15 -0700 | [diff] [blame] | 980 | } |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 981 | |
| 982 | func (agent *DeviceAgent) activateImage(ctx context.Context, img *voltha.ImageDownload) (*voltha.OperationResp, error) { |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 983 | if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
| 984 | return nil, err |
| 985 | } |
| 986 | defer agent.requestQueue.RequestComplete() |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 987 | logger.Debugw("activateImage", log.Fields{"device-id": agent.deviceID}) |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 988 | cloned := agent.getDeviceWithoutLock() |
| 989 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 990 | // Verify whether the Image is in the list of image being downloaded |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 991 | if !isImageRegistered(img, cloned) { |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 992 | return nil, status.Errorf(codes.FailedPrecondition, "device-id:%s, image-not-registered:%s", agent.deviceID, img.Name) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 993 | } |
| 994 | |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 995 | if cloned.AdminState == voltha.AdminState_DOWNLOADING_IMAGE { |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 996 | return nil, status.Errorf(codes.FailedPrecondition, "device-id:%s, device-in-downloading-state:%s", agent.deviceID, img.Name) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 997 | } |
| 998 | // Update image download state |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 999 | for _, image := range cloned.ImageDownloads { |
| 1000 | if image.Id == img.Id && image.Name == img.Name { |
| 1001 | image.ImageState = voltha.ImageDownload_IMAGE_ACTIVATING |
| 1002 | } |
| 1003 | } |
| 1004 | // Set the device to downloading_image |
Kent Hagerman | d9cc2e9 | 2019-11-04 13:28:15 -0500 | [diff] [blame^] | 1005 | if err := agent.updateDeviceStateInStoreWithoutLock(ctx, cloned, voltha.AdminState_DOWNLOADING_IMAGE, cloned.ConnectStatus, cloned.OperStatus); err != nil { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1006 | return nil, err |
| 1007 | } |
| 1008 | |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1009 | subCtx, cancel := context.WithTimeout(context.Background(), agent.defaultTimeout) |
| 1010 | ch, err := agent.adapterProxy.activateImageUpdate(subCtx, proto.Clone(cloned).(*voltha.Device), img) |
| 1011 | if err != nil { |
| 1012 | cancel() |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1013 | return nil, err |
| 1014 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1015 | go agent.waitForAdapterResponse(subCtx, cancel, "activateImageUpdate", ch, agent.onSuccess, agent.onFailure) |
| 1016 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1017 | // The status of the AdminState will be changed following the update_download_status response from the adapter |
| 1018 | // The image name will also be removed from the device list |
serkant.uluderya | 334479d | 2019-04-10 08:26:15 -0700 | [diff] [blame] | 1019 | return &voltha.OperationResp{Code: voltha.OperationResp_OPERATION_SUCCESS}, nil |
| 1020 | } |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 1021 | |
| 1022 | func (agent *DeviceAgent) revertImage(ctx context.Context, img *voltha.ImageDownload) (*voltha.OperationResp, error) { |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1023 | if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
| 1024 | return nil, err |
| 1025 | } |
| 1026 | defer agent.requestQueue.RequestComplete() |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1027 | logger.Debugw("revertImage", log.Fields{"device-id": agent.deviceID}) |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1028 | |
| 1029 | cloned := agent.getDeviceWithoutLock() |
| 1030 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1031 | // Verify whether the Image is in the list of image being downloaded |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1032 | if !isImageRegistered(img, cloned) { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1033 | return nil, status.Errorf(codes.FailedPrecondition, "deviceId:%s, image-not-registered:%s", agent.deviceID, img.Name) |
| 1034 | } |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 1035 | |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1036 | if cloned.AdminState != voltha.AdminState_ENABLED { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1037 | return nil, status.Errorf(codes.FailedPrecondition, "deviceId:%s, device-not-enabled-state:%s", agent.deviceID, img.Name) |
| 1038 | } |
| 1039 | // Update image download state |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1040 | for _, image := range cloned.ImageDownloads { |
| 1041 | if image.Id == img.Id && image.Name == img.Name { |
| 1042 | image.ImageState = voltha.ImageDownload_IMAGE_REVERTING |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 1043 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1044 | } |
Mahir Gunyel | b585167 | 2019-07-24 10:46:26 +0300 | [diff] [blame] | 1045 | |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1046 | if err := agent.updateDeviceInStoreWithoutLock(ctx, cloned, false, ""); err != nil { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1047 | return nil, err |
| 1048 | } |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 1049 | |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1050 | subCtx, cancel := context.WithTimeout(context.Background(), agent.defaultTimeout) |
| 1051 | ch, err := agent.adapterProxy.revertImageUpdate(subCtx, proto.Clone(cloned).(*voltha.Device), img) |
| 1052 | if err != nil { |
| 1053 | cancel() |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1054 | return nil, err |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 1055 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1056 | go agent.waitForAdapterResponse(subCtx, cancel, "revertImageUpdate", ch, agent.onSuccess, agent.onFailure) |
| 1057 | |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 1058 | return &voltha.OperationResp{Code: voltha.OperationResp_OPERATION_SUCCESS}, nil |
serkant.uluderya | 334479d | 2019-04-10 08:26:15 -0700 | [diff] [blame] | 1059 | } |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 1060 | |
| 1061 | func (agent *DeviceAgent) getImageDownloadStatus(ctx context.Context, img *voltha.ImageDownload) (*voltha.ImageDownload, error) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1062 | logger.Debugw("getImageDownloadStatus", log.Fields{"device-id": agent.deviceID}) |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1063 | |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1064 | if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1065 | return nil, err |
| 1066 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1067 | device := agent.getDeviceWithoutLock() |
| 1068 | ch, err := agent.adapterProxy.getImageDownloadStatus(ctx, device, img) |
| 1069 | agent.requestQueue.RequestComplete() |
| 1070 | if err != nil { |
| 1071 | return nil, err |
| 1072 | } |
| 1073 | // Wait for the adapter response |
| 1074 | rpcResponse, ok := <-ch |
| 1075 | if !ok { |
| 1076 | return nil, status.Errorf(codes.Aborted, "channel-closed-device-id-%s", agent.deviceID) |
| 1077 | } |
| 1078 | if rpcResponse.Err != nil { |
| 1079 | return nil, rpcResponse.Err |
| 1080 | } |
| 1081 | // Successful response |
| 1082 | imgDownload := &voltha.ImageDownload{} |
| 1083 | if err := ptypes.UnmarshalAny(rpcResponse.Reply, imgDownload); err != nil { |
| 1084 | return nil, status.Errorf(codes.InvalidArgument, "%s", err.Error()) |
| 1085 | } |
| 1086 | return imgDownload, nil |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 1087 | } |
| 1088 | |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1089 | func (agent *DeviceAgent) updateImageDownload(ctx context.Context, img *voltha.ImageDownload) error { |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1090 | if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
| 1091 | return err |
| 1092 | } |
| 1093 | defer agent.requestQueue.RequestComplete() |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1094 | logger.Debugw("updating-image-download", log.Fields{"device-id": agent.deviceID, "img": img}) |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1095 | |
| 1096 | cloned := agent.getDeviceWithoutLock() |
| 1097 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1098 | // Update the image as well as remove it if the download was cancelled |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1099 | clonedImages := make([]*voltha.ImageDownload, len(cloned.ImageDownloads)) |
| 1100 | for _, image := range cloned.ImageDownloads { |
| 1101 | if image.Id == img.Id && image.Name == img.Name { |
| 1102 | if image.DownloadState != voltha.ImageDownload_DOWNLOAD_CANCELLED { |
| 1103 | clonedImages = append(clonedImages, img) |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 1104 | } |
| 1105 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1106 | } |
| 1107 | cloned.ImageDownloads = clonedImages |
| 1108 | // Set the Admin state to enabled if required |
| 1109 | if (img.DownloadState != voltha.ImageDownload_DOWNLOAD_REQUESTED && |
| 1110 | img.DownloadState != voltha.ImageDownload_DOWNLOAD_STARTED) || |
| 1111 | (img.ImageState != voltha.ImageDownload_IMAGE_ACTIVATING) { |
Kent Hagerman | d9cc2e9 | 2019-11-04 13:28:15 -0500 | [diff] [blame^] | 1112 | return agent.updateDeviceStateInStoreWithoutLock(ctx, cloned, voltha.AdminState_ENABLED, cloned.ConnectStatus, cloned.OperStatus) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1113 | } |
Kent Hagerman | d9cc2e9 | 2019-11-04 13:28:15 -0500 | [diff] [blame^] | 1114 | return agent.updateDeviceInStoreWithoutLock(ctx, cloned, false, "") |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 1115 | } |
| 1116 | |
| 1117 | func (agent *DeviceAgent) getImageDownload(ctx context.Context, img *voltha.ImageDownload) (*voltha.ImageDownload, error) { |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1118 | if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
| 1119 | return nil, err |
| 1120 | } |
| 1121 | defer agent.requestQueue.RequestComplete() |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1122 | logger.Debugw("getImageDownload", log.Fields{"device-id": agent.deviceID}) |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1123 | |
| 1124 | cloned := agent.getDeviceWithoutLock() |
| 1125 | for _, image := range cloned.ImageDownloads { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1126 | if image.Id == img.Id && image.Name == img.Name { |
| 1127 | return image, nil |
| 1128 | } |
| 1129 | } |
| 1130 | return nil, status.Errorf(codes.NotFound, "image-not-found:%s", img.Name) |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 1131 | } |
| 1132 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1133 | func (agent *DeviceAgent) listImageDownloads(ctx context.Context, deviceID string) (*voltha.ImageDownloads, error) { |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1134 | if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
| 1135 | return nil, err |
| 1136 | } |
| 1137 | defer agent.requestQueue.RequestComplete() |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1138 | logger.Debugw("listImageDownloads", log.Fields{"device-id": agent.deviceID}) |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1139 | |
| 1140 | return &voltha.ImageDownloads{Items: agent.getDeviceWithoutLock().ImageDownloads}, nil |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 1141 | } |
| 1142 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 1143 | // getPorts retrieves the ports information of the device based on the port type. |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1144 | func (agent *DeviceAgent) getPorts(ctx context.Context, portType voltha.Port_PortType) *voltha.Ports { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1145 | logger.Debugw("getPorts", log.Fields{"device-id": agent.deviceID, "port-type": portType}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1146 | ports := &voltha.Ports{} |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1147 | if device, _ := agent.deviceMgr.GetDevice(ctx, agent.deviceID); device != nil { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1148 | for _, port := range device.Ports { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1149 | if port.Type == portType { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1150 | ports.Items = append(ports.Items, port) |
| 1151 | } |
| 1152 | } |
| 1153 | } |
| 1154 | return ports |
| 1155 | } |
| 1156 | |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1157 | // getSwitchCapability retrieves the switch capability of a parent device |
khenaidoo | 7923270 | 2018-12-04 11:00:41 -0500 | [diff] [blame] | 1158 | func (agent *DeviceAgent) getSwitchCapability(ctx context.Context) (*ic.SwitchCapability, error) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1159 | logger.Debugw("getSwitchCapability", log.Fields{"device-id": agent.deviceID}) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1160 | |
| 1161 | cloned, err := agent.getDevice(ctx) |
| 1162 | if err != nil { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1163 | return nil, err |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1164 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1165 | ch, err := agent.adapterProxy.getOfpDeviceInfo(ctx, cloned) |
| 1166 | if err != nil { |
| 1167 | return nil, err |
| 1168 | } |
| 1169 | |
| 1170 | // Wait for adapter response |
| 1171 | rpcResponse, ok := <-ch |
| 1172 | if !ok { |
| 1173 | return nil, status.Errorf(codes.Aborted, "channel-closed") |
| 1174 | } |
| 1175 | if rpcResponse.Err != nil { |
| 1176 | return nil, rpcResponse.Err |
| 1177 | } |
| 1178 | // Successful response |
| 1179 | switchCap := &ic.SwitchCapability{} |
| 1180 | if err := ptypes.UnmarshalAny(rpcResponse.Reply, switchCap); err != nil { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1181 | return nil, err |
| 1182 | } |
| 1183 | return switchCap, nil |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1184 | } |
| 1185 | |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1186 | // getPortCapability retrieves the port capability of a device |
khenaidoo | 7923270 | 2018-12-04 11:00:41 -0500 | [diff] [blame] | 1187 | func (agent *DeviceAgent) getPortCapability(ctx context.Context, portNo uint32) (*ic.PortCapability, error) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1188 | logger.Debugw("getPortCapability", log.Fields{"device-id": agent.deviceID}) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1189 | device, err := agent.getDevice(ctx) |
| 1190 | if err != nil { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1191 | return nil, err |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1192 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1193 | ch, err := agent.adapterProxy.getOfpPortInfo(ctx, device, portNo) |
| 1194 | if err != nil { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1195 | return nil, err |
| 1196 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1197 | // Wait for adapter response |
| 1198 | rpcResponse, ok := <-ch |
| 1199 | if !ok { |
| 1200 | return nil, status.Errorf(codes.Aborted, "channel-closed") |
| 1201 | } |
| 1202 | if rpcResponse.Err != nil { |
| 1203 | return nil, rpcResponse.Err |
| 1204 | } |
| 1205 | // Successful response |
| 1206 | portCap := &ic.PortCapability{} |
| 1207 | if err := ptypes.UnmarshalAny(rpcResponse.Reply, portCap); err != nil { |
| 1208 | return nil, status.Errorf(codes.InvalidArgument, "%s", err.Error()) |
| 1209 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1210 | return portCap, nil |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1211 | } |
| 1212 | |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1213 | func (agent *DeviceAgent) onPacketFailure(rpc string, response interface{}, args ...interface{}) { |
| 1214 | // packet data is encoded in the args param as the first parameter |
| 1215 | var packet []byte |
| 1216 | if len(args) >= 1 { |
| 1217 | if pkt, ok := args[0].([]byte); ok { |
| 1218 | packet = pkt |
| 1219 | } |
| 1220 | } |
| 1221 | var errResp error |
| 1222 | if err, ok := response.(error); ok { |
| 1223 | errResp = err |
| 1224 | } |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1225 | logger.Warnw("packet-out-error", log.Fields{ |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1226 | "device-id": agent.deviceID, |
| 1227 | "error": errResp, |
| 1228 | "packet": hex.EncodeToString(packet), |
| 1229 | }) |
| 1230 | } |
| 1231 | |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1232 | func (agent *DeviceAgent) packetOut(ctx context.Context, outPort uint32, packet *ofp.OfpPacketOut) error { |
Scott Baker | 8067860 | 2019-11-14 16:57:36 -0800 | [diff] [blame] | 1233 | // If deviceType=="" then we must have taken ownership of this device. |
| 1234 | // Fixes VOL-2226 where a core would take ownership and have stale data |
| 1235 | if agent.deviceType == "" { |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1236 | agent.reconcileWithKVStore(ctx) |
Scott Baker | 8067860 | 2019-11-14 16:57:36 -0800 | [diff] [blame] | 1237 | } |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 1238 | // Send packet to adapter |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1239 | subCtx, cancel := context.WithTimeout(context.Background(), agent.defaultTimeout) |
| 1240 | ch, err := agent.adapterProxy.packetOut(subCtx, agent.deviceType, agent.deviceID, outPort, packet) |
| 1241 | if err != nil { |
| 1242 | cancel() |
| 1243 | return nil |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 1244 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1245 | go agent.waitForAdapterResponse(subCtx, cancel, "packetOut", ch, agent.onSuccess, agent.onPacketFailure, packet.Data) |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 1246 | return nil |
| 1247 | } |
| 1248 | |
Mahir Gunyel | 8e2707d | 2019-07-25 00:36:21 -0700 | [diff] [blame] | 1249 | // updatePartialDeviceData updates a subset of a device that an Adapter can update. |
| 1250 | // TODO: May need a specific proto to handle only a subset of a device that can be changed by an adapter |
| 1251 | func (agent *DeviceAgent) mergeDeviceInfoFromAdapter(device *voltha.Device) (*voltha.Device, error) { |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1252 | cloned := agent.getDeviceWithoutLock() |
Mahir Gunyel | 8e2707d | 2019-07-25 00:36:21 -0700 | [diff] [blame] | 1253 | cloned.Root = device.Root |
| 1254 | cloned.Vendor = device.Vendor |
| 1255 | cloned.Model = device.Model |
| 1256 | cloned.SerialNumber = device.SerialNumber |
| 1257 | cloned.MacAddress = device.MacAddress |
| 1258 | cloned.Vlan = device.Vlan |
| 1259 | cloned.Reason = device.Reason |
| 1260 | return cloned, nil |
| 1261 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1262 | |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1263 | func (agent *DeviceAgent) updateDeviceUsingAdapterData(ctx context.Context, device *voltha.Device) error { |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1264 | if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
| 1265 | return err |
| 1266 | } |
| 1267 | defer agent.requestQueue.RequestComplete() |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1268 | logger.Debugw("updateDeviceUsingAdapterData", log.Fields{"device-id": device.Id}) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1269 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1270 | updatedDevice, err := agent.mergeDeviceInfoFromAdapter(device) |
| 1271 | if err != nil { |
Mahir Gunyel | 8e2707d | 2019-07-25 00:36:21 -0700 | [diff] [blame] | 1272 | return status.Errorf(codes.Internal, "%s", err.Error()) |
Mahir Gunyel | 8e2707d | 2019-07-25 00:36:21 -0700 | [diff] [blame] | 1273 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1274 | cloned := proto.Clone(updatedDevice).(*voltha.Device) |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1275 | return agent.updateDeviceInStoreWithoutLock(ctx, cloned, false, "") |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 1276 | } |
| 1277 | |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1278 | func (agent *DeviceAgent) updateDeviceWithoutLock(ctx context.Context, device *voltha.Device) error { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1279 | logger.Debugw("updateDevice", log.Fields{"deviceId": device.Id}) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1280 | //cloned := proto.Clone(device).(*voltha.Device) |
| 1281 | cloned := device |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1282 | return agent.updateDeviceInStoreWithoutLock(ctx, cloned, false, "") |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1283 | } |
| 1284 | |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1285 | func (agent *DeviceAgent) updateDeviceStatus(ctx context.Context, operStatus voltha.OperStatus_Types, connStatus voltha.ConnectStatus_Types) error { |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1286 | if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
| 1287 | return err |
| 1288 | } |
| 1289 | defer agent.requestQueue.RequestComplete() |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1290 | |
| 1291 | cloned := agent.getDeviceWithoutLock() |
| 1292 | |
Kent Hagerman | d9cc2e9 | 2019-11-04 13:28:15 -0500 | [diff] [blame^] | 1293 | newConnStatus, newOperStatus := cloned.ConnectStatus, cloned.OperStatus |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1294 | // Ensure the enums passed in are valid - they will be invalid if they are not set when this function is invoked |
serkant.uluderya | 2ae470f | 2020-01-21 11:13:09 -0800 | [diff] [blame] | 1295 | if s, ok := voltha.ConnectStatus_Types_value[connStatus.String()]; ok { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1296 | logger.Debugw("updateDeviceStatus-conn", log.Fields{"ok": ok, "val": s}) |
Kent Hagerman | d9cc2e9 | 2019-11-04 13:28:15 -0500 | [diff] [blame^] | 1297 | newConnStatus = connStatus |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1298 | } |
serkant.uluderya | 2ae470f | 2020-01-21 11:13:09 -0800 | [diff] [blame] | 1299 | if s, ok := voltha.OperStatus_Types_value[operStatus.String()]; ok { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1300 | logger.Debugw("updateDeviceStatus-oper", log.Fields{"ok": ok, "val": s}) |
Kent Hagerman | d9cc2e9 | 2019-11-04 13:28:15 -0500 | [diff] [blame^] | 1301 | newOperStatus = operStatus |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1302 | } |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1303 | logger.Debugw("updateDeviceStatus", log.Fields{"deviceId": cloned.Id, "operStatus": cloned.OperStatus, "connectStatus": cloned.ConnectStatus}) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1304 | // Store the device |
Kent Hagerman | d9cc2e9 | 2019-11-04 13:28:15 -0500 | [diff] [blame^] | 1305 | return agent.updateDeviceStateInStoreWithoutLock(ctx, cloned, cloned.AdminState, newConnStatus, newOperStatus) |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1306 | } |
| 1307 | |
kesavand | bc2d162 | 2020-01-21 00:42:01 -0500 | [diff] [blame] | 1308 | func (agent *DeviceAgent) updatePortsOperState(ctx context.Context, operStatus voltha.OperStatus_Types) error { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1309 | logger.Debugw("updatePortsOperState", log.Fields{"device-id": agent.deviceID}) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1310 | if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
| 1311 | return err |
| 1312 | } |
| 1313 | defer agent.requestQueue.RequestComplete() |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1314 | cloned := agent.getDeviceWithoutLock() |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1315 | for _, port := range cloned.Ports { |
kesavand | bc2d162 | 2020-01-21 00:42:01 -0500 | [diff] [blame] | 1316 | port.OperStatus = operStatus |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1317 | } |
| 1318 | // Store the device |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1319 | return agent.updateDeviceInStoreWithoutLock(ctx, cloned, false, "") |
khenaidoo | 3ab3488 | 2019-05-02 21:33:30 -0400 | [diff] [blame] | 1320 | } |
| 1321 | |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1322 | func (agent *DeviceAgent) updatePortState(ctx context.Context, portType voltha.Port_PortType, portNo uint32, operStatus voltha.OperStatus_Types) error { |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1323 | if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
| 1324 | return err |
| 1325 | } |
| 1326 | defer agent.requestQueue.RequestComplete() |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1327 | // Work only on latest data |
| 1328 | // TODO: Get list of ports from device directly instead of the entire device |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1329 | cloned := agent.getDeviceWithoutLock() |
| 1330 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1331 | // Ensure the enums passed in are valid - they will be invalid if they are not set when this function is invoked |
| 1332 | if _, ok := voltha.Port_PortType_value[portType.String()]; !ok { |
| 1333 | return status.Errorf(codes.InvalidArgument, "%s", portType) |
| 1334 | } |
| 1335 | for _, port := range cloned.Ports { |
| 1336 | if port.Type == portType && port.PortNo == portNo { |
| 1337 | port.OperStatus = operStatus |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1338 | } |
| 1339 | } |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1340 | logger.Debugw("portStatusUpdate", log.Fields{"deviceId": cloned.Id}) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1341 | // Store the device |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1342 | return agent.updateDeviceInStoreWithoutLock(ctx, cloned, false, "") |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1343 | } |
| 1344 | |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1345 | func (agent *DeviceAgent) deleteAllPorts(ctx context.Context) error { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1346 | logger.Debugw("deleteAllPorts", log.Fields{"deviceId": agent.deviceID}) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1347 | if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
| 1348 | return err |
| 1349 | } |
| 1350 | defer agent.requestQueue.RequestComplete() |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1351 | |
| 1352 | cloned := agent.getDeviceWithoutLock() |
| 1353 | |
| 1354 | if cloned.AdminState != voltha.AdminState_DISABLED && cloned.AdminState != voltha.AdminState_DELETED { |
| 1355 | err := status.Error(codes.FailedPrecondition, fmt.Sprintf("invalid-state-%v", cloned.AdminState)) |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1356 | logger.Warnw("invalid-state-removing-ports", log.Fields{"state": cloned.AdminState, "error": err}) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1357 | return err |
| 1358 | } |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1359 | if len(cloned.Ports) == 0 { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1360 | logger.Debugw("no-ports-present", log.Fields{"deviceId": agent.deviceID}) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1361 | return nil |
| 1362 | } |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1363 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1364 | cloned.Ports = []*voltha.Port{} |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1365 | logger.Debugw("portStatusUpdate", log.Fields{"deviceId": cloned.Id}) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1366 | // Store the device |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1367 | return agent.updateDeviceInStoreWithoutLock(ctx, cloned, false, "") |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 1368 | } |
| 1369 | |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1370 | func (agent *DeviceAgent) addPort(ctx context.Context, port *voltha.Port) error { |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1371 | if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
| 1372 | return err |
| 1373 | } |
| 1374 | defer agent.requestQueue.RequestComplete() |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1375 | logger.Debugw("addPort", log.Fields{"deviceId": agent.deviceID}) |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1376 | |
| 1377 | cloned := agent.getDeviceWithoutLock() |
khenaidoo | 80b987d | 2020-02-20 10:52:52 -0500 | [diff] [blame] | 1378 | updatePort := false |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1379 | if cloned.Ports == nil { |
| 1380 | // First port |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1381 | logger.Debugw("addPort-first-port-to-add", log.Fields{"deviceId": agent.deviceID}) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1382 | cloned.Ports = make([]*voltha.Port, 0) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1383 | } else { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1384 | for _, p := range cloned.Ports { |
| 1385 | if p.Type == port.Type && p.PortNo == port.PortNo { |
khenaidoo | 80b987d | 2020-02-20 10:52:52 -0500 | [diff] [blame] | 1386 | if p.Label == "" && p.Type == voltha.Port_PON_OLT { |
| 1387 | //Creation of OLT PON port is being processed after a default PON port was created. Just update it. |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1388 | logger.Infow("update-pon-port-created-by-default", log.Fields{"default-port": p, "port-to-add": port}) |
khenaidoo | 80b987d | 2020-02-20 10:52:52 -0500 | [diff] [blame] | 1389 | p.Label = port.Label |
| 1390 | p.OperStatus = port.OperStatus |
| 1391 | updatePort = true |
| 1392 | break |
| 1393 | } |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1394 | logger.Debugw("port already exists", log.Fields{"port": port}) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1395 | return nil |
manikkaraj k | 259a6f7 | 2019-05-06 09:55:44 -0400 | [diff] [blame] | 1396 | } |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1397 | } |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1398 | } |
khenaidoo | 80b987d | 2020-02-20 10:52:52 -0500 | [diff] [blame] | 1399 | if !updatePort { |
| 1400 | cp := proto.Clone(port).(*voltha.Port) |
| 1401 | // Set the admin state of the port to ENABLE |
| 1402 | cp.AdminState = voltha.AdminState_ENABLED |
| 1403 | cloned.Ports = append(cloned.Ports, cp) |
| 1404 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1405 | // Store the device |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1406 | return agent.updateDeviceInStoreWithoutLock(ctx, cloned, false, "") |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1407 | } |
| 1408 | |
khenaidoo | 80b987d | 2020-02-20 10:52:52 -0500 | [diff] [blame] | 1409 | func (agent *DeviceAgent) addPeerPort(ctx context.Context, peerPort *voltha.Port_PeerPort) error { |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1410 | if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
| 1411 | return err |
| 1412 | } |
| 1413 | defer agent.requestQueue.RequestComplete() |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1414 | logger.Debugw("adding-peer-peerPort", log.Fields{"device-id": agent.deviceID, "peer-peerPort": peerPort}) |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1415 | |
| 1416 | cloned := agent.getDeviceWithoutLock() |
| 1417 | |
khenaidoo | 80b987d | 2020-02-20 10:52:52 -0500 | [diff] [blame] | 1418 | // Get the peer port on the device based on the peerPort no |
| 1419 | found := false |
| 1420 | for _, port := range cloned.Ports { |
| 1421 | if port.PortNo == peerPort.PortNo { // found peerPort |
| 1422 | cp := proto.Clone(peerPort).(*voltha.Port_PeerPort) |
| 1423 | port.Peers = append(port.Peers, cp) |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1424 | logger.Debugw("found-peer", log.Fields{"device-id": agent.deviceID, "portNo": peerPort.PortNo, "deviceId": agent.deviceID}) |
khenaidoo | 80b987d | 2020-02-20 10:52:52 -0500 | [diff] [blame] | 1425 | found = true |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1426 | break |
| 1427 | } |
| 1428 | } |
khenaidoo | 80b987d | 2020-02-20 10:52:52 -0500 | [diff] [blame] | 1429 | if !found && agent.isRootdevice { |
| 1430 | // An ONU PON port has been created before the corresponding creation of the OLT PON port. Create the OLT PON port |
| 1431 | // with default values which will be updated once the OLT PON port creation is processed. |
| 1432 | ponPort := &voltha.Port{ |
| 1433 | PortNo: peerPort.PortNo, |
| 1434 | Type: voltha.Port_PON_OLT, |
| 1435 | AdminState: voltha.AdminState_ENABLED, |
| 1436 | DeviceId: agent.deviceID, |
| 1437 | Peers: []*voltha.Port_PeerPort{proto.Clone(peerPort).(*voltha.Port_PeerPort)}, |
| 1438 | } |
| 1439 | cloned.Ports = append(cloned.Ports, ponPort) |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1440 | logger.Infow("adding-default-pon-port", log.Fields{"device-id": agent.deviceID, "peer": peerPort, "pon-port": ponPort}) |
khenaidoo | 80b987d | 2020-02-20 10:52:52 -0500 | [diff] [blame] | 1441 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1442 | // Store the device |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1443 | return agent.updateDeviceInStoreWithoutLock(ctx, cloned, false, "") |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1444 | } |
| 1445 | |
| 1446 | // TODO: A generic device update by attribute |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1447 | func (agent *DeviceAgent) updateDeviceAttribute(ctx context.Context, name string, value interface{}) { |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1448 | if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1449 | logger.Warnw("request-aborted", log.Fields{"device-id": agent.deviceID, "name": name, "error": err}) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1450 | return |
| 1451 | } |
| 1452 | defer agent.requestQueue.RequestComplete() |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1453 | if value == nil { |
| 1454 | return |
| 1455 | } |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1456 | |
| 1457 | cloned := agent.getDeviceWithoutLock() |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1458 | updated := false |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1459 | s := reflect.ValueOf(cloned).Elem() |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1460 | if s.Kind() == reflect.Struct { |
| 1461 | // exported field |
| 1462 | f := s.FieldByName(name) |
| 1463 | if f.IsValid() && f.CanSet() { |
| 1464 | switch f.Kind() { |
| 1465 | case reflect.String: |
| 1466 | f.SetString(value.(string)) |
| 1467 | updated = true |
| 1468 | case reflect.Uint32: |
| 1469 | f.SetUint(uint64(value.(uint32))) |
| 1470 | updated = true |
| 1471 | case reflect.Bool: |
| 1472 | f.SetBool(value.(bool)) |
| 1473 | updated = true |
| 1474 | } |
| 1475 | } |
| 1476 | } |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1477 | logger.Debugw("update-field-status", log.Fields{"deviceId": cloned.Id, "name": name, "updated": updated}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1478 | // Save the data |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1479 | |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1480 | if err := agent.updateDeviceInStoreWithoutLock(ctx, cloned, false, ""); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1481 | logger.Warnw("attribute-update-failed", log.Fields{"attribute": name, "value": value}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1482 | } |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1483 | } |
serkant.uluderya | 334479d | 2019-04-10 08:26:15 -0700 | [diff] [blame] | 1484 | |
| 1485 | func (agent *DeviceAgent) simulateAlarm(ctx context.Context, simulatereq *voltha.SimulateAlarmRequest) error { |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1486 | if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
| 1487 | return err |
| 1488 | } |
| 1489 | defer agent.requestQueue.RequestComplete() |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1490 | logger.Debugw("simulateAlarm", log.Fields{"id": agent.deviceID}) |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1491 | |
| 1492 | cloned := agent.getDeviceWithoutLock() |
| 1493 | |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1494 | subCtx, cancel := context.WithTimeout(context.Background(), agent.defaultTimeout) |
| 1495 | ch, err := agent.adapterProxy.simulateAlarm(subCtx, cloned, simulatereq) |
| 1496 | if err != nil { |
| 1497 | cancel() |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1498 | return err |
serkant.uluderya | 334479d | 2019-04-10 08:26:15 -0700 | [diff] [blame] | 1499 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1500 | go agent.waitForAdapterResponse(subCtx, cancel, "simulateAlarm", ch, agent.onSuccess, agent.onFailure) |
serkant.uluderya | 334479d | 2019-04-10 08:26:15 -0700 | [diff] [blame] | 1501 | return nil |
| 1502 | } |
Mahir Gunyel | b585167 | 2019-07-24 10:46:26 +0300 | [diff] [blame] | 1503 | |
Kent Hagerman | d9cc2e9 | 2019-11-04 13:28:15 -0500 | [diff] [blame^] | 1504 | func (agent *DeviceAgent) updateDeviceStateInStoreWithoutLock( |
| 1505 | ctx context.Context, |
| 1506 | device *voltha.Device, |
| 1507 | adminState voltha.AdminState_Types, |
| 1508 | connectStatus voltha.ConnectStatus_Types, |
| 1509 | operStatus voltha.OperStatus_Types, |
| 1510 | ) error { |
| 1511 | previousState := getDeviceStates(device) |
| 1512 | device.AdminState, device.ConnectStatus, device.OperStatus = adminState, connectStatus, operStatus |
| 1513 | |
| 1514 | if err := agent.updateDeviceInStoreWithoutLock(ctx, device, false, ""); err != nil { |
| 1515 | return err |
| 1516 | } |
| 1517 | |
| 1518 | // process state transition in its own thread |
| 1519 | go func() { |
| 1520 | if err := agent.deviceMgr.processTransition(context.Background(), device, previousState); err != nil { |
| 1521 | log.Errorw("failed-process-transition", log.Fields{"deviceId": device.Id, "previousAdminState": previousState.Admin, "currentAdminState": device.AdminState}) |
| 1522 | } |
| 1523 | }() |
| 1524 | return nil |
| 1525 | } |
| 1526 | |
Mahir Gunyel | b585167 | 2019-07-24 10:46:26 +0300 | [diff] [blame] | 1527 | //This is an update operation to model without Lock.This function must never be invoked by another function unless the latter holds a lock on the device. |
| 1528 | // It is an internal helper function. |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1529 | func (agent *DeviceAgent) updateDeviceInStoreWithoutLock(ctx context.Context, device *voltha.Device, strict bool, txid string) error { |
| 1530 | updateCtx := context.WithValue(ctx, model.RequestTimestamp, time.Now().UnixNano()) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 1531 | afterUpdate, err := agent.clusterDataProxy.Update(updateCtx, "/devices/"+agent.deviceID, device, strict, txid) |
| 1532 | if err != nil { |
Kent Hagerman | d9cc2e9 | 2019-11-04 13:28:15 -0500 | [diff] [blame^] | 1533 | return status.Errorf(codes.Internal, "failed-update-device:%s: %s", agent.deviceID, err) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 1534 | } |
| 1535 | if afterUpdate == nil { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1536 | return status.Errorf(codes.Internal, "failed-update-device:%s", agent.deviceID) |
Mahir Gunyel | b585167 | 2019-07-24 10:46:26 +0300 | [diff] [blame] | 1537 | } |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1538 | logger.Debugw("updated-device-in-store", log.Fields{"deviceId: ": agent.deviceID}) |
Mahir Gunyel | b585167 | 2019-07-24 10:46:26 +0300 | [diff] [blame] | 1539 | |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1540 | agent.device = proto.Clone(device).(*voltha.Device) |
Mahir Gunyel | b585167 | 2019-07-24 10:46:26 +0300 | [diff] [blame] | 1541 | return nil |
| 1542 | } |
Mahir Gunyel | fdee921 | 2019-10-16 16:52:21 -0700 | [diff] [blame] | 1543 | |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1544 | func (agent *DeviceAgent) updateDeviceReason(ctx context.Context, reason string) error { |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1545 | if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
| 1546 | return err |
| 1547 | } |
| 1548 | defer agent.requestQueue.RequestComplete() |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1549 | |
| 1550 | cloned := agent.getDeviceWithoutLock() |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1551 | cloned.Reason = reason |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1552 | logger.Debugw("updateDeviceReason", log.Fields{"deviceId": cloned.Id, "reason": cloned.Reason}) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1553 | // Store the device |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1554 | return agent.updateDeviceInStoreWithoutLock(ctx, cloned, false, "") |
Mahir Gunyel | fdee921 | 2019-10-16 16:52:21 -0700 | [diff] [blame] | 1555 | } |
kesavand | bc2d162 | 2020-01-21 00:42:01 -0500 | [diff] [blame] | 1556 | |
| 1557 | func (agent *DeviceAgent) disablePort(ctx context.Context, Port *voltha.Port) error { |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1558 | if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
| 1559 | return err |
| 1560 | } |
| 1561 | defer agent.requestQueue.RequestComplete() |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1562 | logger.Debugw("disablePort", log.Fields{"device-id": agent.deviceID, "port-no": Port.PortNo}) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1563 | var cp *voltha.Port |
kesavand | bc2d162 | 2020-01-21 00:42:01 -0500 | [diff] [blame] | 1564 | // Get the most up to date the device info |
| 1565 | device := agent.getDeviceWithoutLock() |
| 1566 | for _, port := range device.Ports { |
| 1567 | if port.PortNo == Port.PortNo { |
| 1568 | port.AdminState = voltha.AdminState_DISABLED |
| 1569 | cp = proto.Clone(port).(*voltha.Port) |
| 1570 | break |
| 1571 | |
| 1572 | } |
| 1573 | } |
| 1574 | if cp == nil { |
| 1575 | return status.Errorf(codes.InvalidArgument, "%v", Port.PortNo) |
| 1576 | } |
| 1577 | |
| 1578 | if cp.Type != voltha.Port_PON_OLT { |
| 1579 | return status.Errorf(codes.InvalidArgument, "Disabling of Port Type %v unimplemented", cp.Type) |
| 1580 | } |
| 1581 | // Store the device |
| 1582 | if err := agent.updateDeviceInStoreWithoutLock(ctx, device, false, ""); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1583 | logger.Debugw("updateDeviceInStoreWithoutLock error ", log.Fields{"device-id": agent.deviceID, "port-no": Port.PortNo, "error": err}) |
kesavand | bc2d162 | 2020-01-21 00:42:01 -0500 | [diff] [blame] | 1584 | return err |
| 1585 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1586 | |
kesavand | bc2d162 | 2020-01-21 00:42:01 -0500 | [diff] [blame] | 1587 | //send request to adapter |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1588 | subCtx, cancel := context.WithTimeout(context.Background(), agent.defaultTimeout) |
| 1589 | ch, err := agent.adapterProxy.disablePort(ctx, device, cp) |
| 1590 | if err != nil { |
| 1591 | cancel() |
kesavand | bc2d162 | 2020-01-21 00:42:01 -0500 | [diff] [blame] | 1592 | return err |
| 1593 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1594 | go agent.waitForAdapterResponse(subCtx, cancel, "disablePort", ch, agent.onSuccess, agent.onFailure) |
kesavand | bc2d162 | 2020-01-21 00:42:01 -0500 | [diff] [blame] | 1595 | return nil |
| 1596 | } |
| 1597 | |
| 1598 | func (agent *DeviceAgent) enablePort(ctx context.Context, Port *voltha.Port) error { |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1599 | if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
| 1600 | return err |
| 1601 | } |
| 1602 | defer agent.requestQueue.RequestComplete() |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1603 | logger.Debugw("enablePort", log.Fields{"device-id": agent.deviceID, "port-no": Port.PortNo}) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1604 | |
| 1605 | var cp *voltha.Port |
kesavand | bc2d162 | 2020-01-21 00:42:01 -0500 | [diff] [blame] | 1606 | // Get the most up to date the device info |
| 1607 | device := agent.getDeviceWithoutLock() |
| 1608 | for _, port := range device.Ports { |
| 1609 | if port.PortNo == Port.PortNo { |
| 1610 | port.AdminState = voltha.AdminState_ENABLED |
| 1611 | cp = proto.Clone(port).(*voltha.Port) |
| 1612 | break |
| 1613 | } |
| 1614 | } |
| 1615 | |
| 1616 | if cp == nil { |
| 1617 | return status.Errorf(codes.InvalidArgument, "%v", Port.PortNo) |
| 1618 | } |
| 1619 | |
| 1620 | if cp.Type != voltha.Port_PON_OLT { |
| 1621 | return status.Errorf(codes.InvalidArgument, "Enabling of Port Type %v unimplemented", cp.Type) |
| 1622 | } |
| 1623 | // Store the device |
| 1624 | if err := agent.updateDeviceInStoreWithoutLock(ctx, device, false, ""); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1625 | logger.Debugw("updateDeviceInStoreWithoutLock error ", log.Fields{"device-id": agent.deviceID, "port-no": Port.PortNo, "error": err}) |
kesavand | bc2d162 | 2020-01-21 00:42:01 -0500 | [diff] [blame] | 1626 | return err |
| 1627 | } |
| 1628 | //send request to adapter |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1629 | subCtx, cancel := context.WithTimeout(context.Background(), agent.defaultTimeout) |
| 1630 | ch, err := agent.adapterProxy.enablePort(ctx, device, cp) |
| 1631 | if err != nil { |
| 1632 | cancel() |
kesavand | bc2d162 | 2020-01-21 00:42:01 -0500 | [diff] [blame] | 1633 | return err |
| 1634 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1635 | go agent.waitForAdapterResponse(subCtx, cancel, "enablePort", ch, agent.onSuccess, agent.onFailure) |
kesavand | bc2d162 | 2020-01-21 00:42:01 -0500 | [diff] [blame] | 1636 | return nil |
| 1637 | } |
Chaitrashree G S | 543df3e | 2020-02-24 22:36:54 -0500 | [diff] [blame] | 1638 | |
| 1639 | func (agent *DeviceAgent) ChildDeviceLost(ctx context.Context, device *voltha.Device) error { |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1640 | if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
| 1641 | return err |
| 1642 | } |
| 1643 | defer agent.requestQueue.RequestComplete() |
Chaitrashree G S | 543df3e | 2020-02-24 22:36:54 -0500 | [diff] [blame] | 1644 | |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1645 | logger.Debugw("childDeviceLost", log.Fields{"child-device-id": device.Id, "parent-device-ud": agent.deviceID}) |
Chaitrashree G S | 543df3e | 2020-02-24 22:36:54 -0500 | [diff] [blame] | 1646 | |
| 1647 | //Remove the associated peer ports on the parent device |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1648 | parentDevice := agent.getDeviceWithoutLock() |
| 1649 | var updatedPeers []*voltha.Port_PeerPort |
| 1650 | for _, port := range parentDevice.Ports { |
| 1651 | updatedPeers = make([]*voltha.Port_PeerPort, 0) |
| 1652 | for _, peerPort := range port.Peers { |
| 1653 | if peerPort.DeviceId != device.Id { |
| 1654 | updatedPeers = append(updatedPeers, peerPort) |
| 1655 | } |
| 1656 | } |
| 1657 | port.Peers = updatedPeers |
| 1658 | } |
| 1659 | if err := agent.updateDeviceInStoreWithoutLock(ctx, parentDevice, false, ""); err != nil { |
| 1660 | return err |
Chaitrashree G S | 543df3e | 2020-02-24 22:36:54 -0500 | [diff] [blame] | 1661 | } |
| 1662 | |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1663 | //send request to adapter |
| 1664 | subCtx, cancel := context.WithTimeout(context.Background(), agent.defaultTimeout) |
| 1665 | ch, err := agent.adapterProxy.childDeviceLost(ctx, agent.deviceType, agent.deviceID, device.ParentPortNo, device.ProxyAddress.OnuId) |
| 1666 | if err != nil { |
| 1667 | cancel() |
| 1668 | return err |
Chaitrashree G S | 543df3e | 2020-02-24 22:36:54 -0500 | [diff] [blame] | 1669 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1670 | go agent.waitForAdapterResponse(subCtx, cancel, "childDeviceLost", ch, agent.onSuccess, agent.onFailure) |
Chaitrashree G S | 543df3e | 2020-02-24 22:36:54 -0500 | [diff] [blame] | 1671 | return nil |
Chaitrashree G S | 543df3e | 2020-02-24 22:36:54 -0500 | [diff] [blame] | 1672 | } |
onkarkundargi | 8728525 | 2020-01-27 11:34:52 +0530 | [diff] [blame] | 1673 | |
| 1674 | func (agent *DeviceAgent) startOmciTest(ctx context.Context, omcitestrequest *voltha.OmciTestRequest) (*voltha.TestResponse, error) { |
| 1675 | if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
| 1676 | return nil, err |
| 1677 | } |
| 1678 | |
| 1679 | device := agent.getDeviceWithoutLock() |
| 1680 | adapterName, err := agent.adapterMgr.getAdapterName(device.Type) |
| 1681 | if err != nil { |
| 1682 | agent.requestQueue.RequestComplete() |
| 1683 | return nil, err |
| 1684 | } |
| 1685 | |
| 1686 | // Send request to the adapter |
| 1687 | device.Adapter = adapterName |
| 1688 | ch, err := agent.adapterProxy.startOmciTest(ctx, device, omcitestrequest) |
| 1689 | agent.requestQueue.RequestComplete() |
| 1690 | if err != nil { |
| 1691 | return nil, err |
| 1692 | } |
| 1693 | |
| 1694 | // Wait for the adapter response |
| 1695 | rpcResponse, ok := <-ch |
| 1696 | if !ok { |
| 1697 | return nil, status.Errorf(codes.Aborted, "channel-closed-device-id-%s", agent.deviceID) |
| 1698 | } |
| 1699 | if rpcResponse.Err != nil { |
| 1700 | return nil, rpcResponse.Err |
| 1701 | } |
| 1702 | |
| 1703 | // Unmarshal and return the response |
| 1704 | testResp := &voltha.TestResponse{} |
| 1705 | if err := ptypes.UnmarshalAny(rpcResponse.Reply, testResp); err != nil { |
| 1706 | return nil, status.Errorf(codes.InvalidArgument, "%s", err.Error()) |
| 1707 | } |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1708 | logger.Debugw("Omci_test_Request-Success-device-agent", log.Fields{"testResp": testResp}) |
onkarkundargi | 8728525 | 2020-01-27 11:34:52 +0530 | [diff] [blame] | 1709 | return testResp, nil |
| 1710 | } |