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