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