khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 3 | |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 16 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 17 | package core |
| 18 | |
| 19 | import ( |
| 20 | "context" |
Matteo Scandolo | 360605d | 2019-11-05 18:29:17 -0800 | [diff] [blame] | 21 | "encoding/hex" |
khenaidoo | 3ab3488 | 2019-05-02 21:33:30 -0400 | [diff] [blame] | 22 | "fmt" |
Chaitrashree G S | a773e99 | 2019-09-09 21:04:15 -0400 | [diff] [blame] | 23 | "reflect" |
| 24 | "sync" |
| 25 | "time" |
| 26 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 27 | "github.com/gogo/protobuf/proto" |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 28 | "github.com/opencord/voltha-go/db/model" |
Scott Baker | b671a86 | 2019-10-24 10:53:40 -0700 | [diff] [blame] | 29 | coreutils "github.com/opencord/voltha-go/rw_core/utils" |
serkant.uluderya | 2ae470f | 2020-01-21 11:13:09 -0800 | [diff] [blame] | 30 | fu "github.com/opencord/voltha-lib-go/v3/pkg/flows" |
| 31 | "github.com/opencord/voltha-lib-go/v3/pkg/log" |
| 32 | ic "github.com/opencord/voltha-protos/v3/go/inter_container" |
| 33 | ofp "github.com/opencord/voltha-protos/v3/go/openflow_13" |
| 34 | "github.com/opencord/voltha-protos/v3/go/voltha" |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 35 | "google.golang.org/grpc/codes" |
| 36 | "google.golang.org/grpc/status" |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 37 | ) |
| 38 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 39 | // DeviceAgent represents device agent attributes |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 40 | type DeviceAgent struct { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 41 | deviceID string |
| 42 | parentID string |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 43 | deviceType string |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 44 | isRootdevice bool |
khenaidoo | 9a46896 | 2018-09-19 15:33:13 -0400 | [diff] [blame] | 45 | adapterProxy *AdapterProxy |
serkant.uluderya | 334479d | 2019-04-10 08:26:15 -0700 | [diff] [blame] | 46 | adapterMgr *AdapterManager |
khenaidoo | 9a46896 | 2018-09-19 15:33:13 -0400 | [diff] [blame] | 47 | deviceMgr *DeviceManager |
| 48 | clusterDataProxy *model.Proxy |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 49 | deviceProxy *model.Proxy |
khenaidoo | 9a46896 | 2018-09-19 15:33:13 -0400 | [diff] [blame] | 50 | exitChannel chan int |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 51 | lockDevice sync.RWMutex |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 52 | device *voltha.Device |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 53 | defaultTimeout int64 |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 54 | } |
| 55 | |
Scott Baker | 8067860 | 2019-11-14 16:57:36 -0800 | [diff] [blame] | 56 | //newDeviceAgent creates a new device agent. The device will be initialized when start() is called. |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 57 | func newDeviceAgent(ap *AdapterProxy, device *voltha.Device, deviceMgr *DeviceManager, cdProxy *model.Proxy, timeout int64) *DeviceAgent { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 58 | var agent DeviceAgent |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 59 | agent.adapterProxy = ap |
Scott Baker | 8067860 | 2019-11-14 16:57:36 -0800 | [diff] [blame] | 60 | if device.Id == "" { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 61 | agent.deviceID = CreateDeviceID() |
Scott Baker | 8067860 | 2019-11-14 16:57:36 -0800 | [diff] [blame] | 62 | } else { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 63 | agent.deviceID = device.Id |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 64 | } |
Scott Baker | 8067860 | 2019-11-14 16:57:36 -0800 | [diff] [blame] | 65 | |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 66 | agent.isRootdevice = device.Root |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 67 | agent.parentID = device.ParentId |
Scott Baker | 8067860 | 2019-11-14 16:57:36 -0800 | [diff] [blame] | 68 | agent.deviceType = device.Type |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 69 | agent.deviceMgr = deviceMgr |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 70 | agent.adapterMgr = deviceMgr.adapterMgr |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 71 | agent.exitChannel = make(chan int, 1) |
khenaidoo | 9a46896 | 2018-09-19 15:33:13 -0400 | [diff] [blame] | 72 | agent.clusterDataProxy = cdProxy |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 73 | agent.lockDevice = sync.RWMutex{} |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 74 | agent.defaultTimeout = timeout |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 75 | agent.device = proto.Clone(device).(*voltha.Device) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 76 | return &agent |
| 77 | } |
| 78 | |
Scott Baker | 8067860 | 2019-11-14 16:57:36 -0800 | [diff] [blame] | 79 | // start() |
| 80 | // save the device to the data model and registers for callbacks on that device if deviceToCreate!=nil. Otherwise, |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 81 | // 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] | 82 | // was started. |
| 83 | func (agent *DeviceAgent) start(ctx context.Context, deviceToCreate *voltha.Device) (*voltha.Device, error) { |
| 84 | var device *voltha.Device |
| 85 | |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 86 | agent.lockDevice.Lock() |
| 87 | defer agent.lockDevice.Unlock() |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 88 | log.Debugw("starting-device-agent", log.Fields{"deviceId": agent.deviceID}) |
Scott Baker | 8067860 | 2019-11-14 16:57:36 -0800 | [diff] [blame] | 89 | if deviceToCreate == nil { |
| 90 | // Load the existing device |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 91 | loadedDevice, err := agent.clusterDataProxy.Get(ctx, "/devices/"+agent.deviceID, 1, true, "") |
| 92 | if err != nil { |
| 93 | log.Errorw("failed-to-get-from-cluster-data-proxy", log.Fields{"error": err}) |
| 94 | return nil, err |
| 95 | } |
| 96 | if loadedDevice != nil { |
Scott Baker | 8067860 | 2019-11-14 16:57:36 -0800 | [diff] [blame] | 97 | var ok bool |
| 98 | if device, ok = loadedDevice.(*voltha.Device); ok { |
| 99 | agent.deviceType = device.Adapter |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 100 | agent.device = proto.Clone(device).(*voltha.Device) |
Scott Baker | 8067860 | 2019-11-14 16:57:36 -0800 | [diff] [blame] | 101 | } else { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 102 | log.Errorw("failed-to-convert-device", log.Fields{"deviceId": agent.deviceID}) |
| 103 | return nil, status.Errorf(codes.NotFound, "device-%s", agent.deviceID) |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 104 | } |
| 105 | } else { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 106 | log.Errorw("failed-to-load-device", log.Fields{"deviceId": agent.deviceID}) |
| 107 | return nil, status.Errorf(codes.NotFound, "device-%s", agent.deviceID) |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 108 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 109 | log.Debugw("device-loaded-from-dB", log.Fields{"deviceId": agent.deviceID}) |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 110 | } else { |
Scott Baker | 8067860 | 2019-11-14 16:57:36 -0800 | [diff] [blame] | 111 | // Create a new device |
| 112 | // Assumption is that AdminState, FlowGroups, and Flows are unitialized since this |
| 113 | // is a new device, so populate them here before passing the device to clusterDataProxy.AddWithId. |
| 114 | // agent.deviceId will also have been set during newDeviceAgent(). |
| 115 | device = (proto.Clone(deviceToCreate)).(*voltha.Device) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 116 | device.Id = agent.deviceID |
Scott Baker | 8067860 | 2019-11-14 16:57:36 -0800 | [diff] [blame] | 117 | device.AdminState = voltha.AdminState_PREPROVISIONED |
| 118 | device.FlowGroups = &ofp.FlowGroups{Items: nil} |
| 119 | device.Flows = &ofp.Flows{Items: nil} |
| 120 | if !deviceToCreate.GetRoot() && deviceToCreate.ProxyAddress != nil { |
| 121 | // Set the default vlan ID to the one specified by the parent adapter. It can be |
| 122 | // overwritten by the child adapter during a device update request |
| 123 | device.Vlan = deviceToCreate.ProxyAddress.ChannelId |
| 124 | } |
| 125 | |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 126 | // Add the initial device to the local model |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 127 | added, err := agent.clusterDataProxy.AddWithID(ctx, "/devices", agent.deviceID, device, "") |
| 128 | if err != nil { |
| 129 | log.Errorw("failed-to-save-devices-to-cluster-proxy", log.Fields{"error": err}) |
| 130 | return nil, err |
| 131 | } |
| 132 | if added == nil { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 133 | log.Errorw("failed-to-add-device", log.Fields{"deviceId": agent.deviceID}) |
| 134 | return nil, status.Errorf(codes.Aborted, "failed-adding-device-%s", agent.deviceID) |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 135 | } |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 136 | agent.device = proto.Clone(device).(*voltha.Device) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 137 | } |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 138 | var err error |
| 139 | if agent.deviceProxy, err = agent.clusterDataProxy.CreateProxy(ctx, "/devices/"+agent.deviceID, false); err != nil { |
| 140 | log.Errorw("failed-to-add-devices-to-cluster-proxy", log.Fields{"error": err}) |
| 141 | return nil, err |
| 142 | } |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 143 | agent.deviceProxy.RegisterCallback(model.PostUpdate, agent.processUpdate) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 144 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 145 | log.Debugw("device-agent-started", log.Fields{"deviceId": agent.deviceID}) |
Scott Baker | 8067860 | 2019-11-14 16:57:36 -0800 | [diff] [blame] | 146 | return device, nil |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 147 | } |
| 148 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 149 | // stop stops the device agent. Not much to do for now |
| 150 | func (agent *DeviceAgent) stop(ctx context.Context) { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 151 | agent.lockDevice.Lock() |
| 152 | defer agent.lockDevice.Unlock() |
khenaidoo | 4908535 | 2020-01-13 19:15:43 -0500 | [diff] [blame] | 153 | |
| 154 | log.Debugw("stopping-device-agent", log.Fields{"deviceId": agent.deviceID, "parentId": agent.parentID}) |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 155 | |
| 156 | // First unregister any callbacks |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 157 | agent.deviceProxy.UnregisterCallback(model.PostUpdate, agent.processUpdate) |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 158 | |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 159 | // Remove the device from the KV store |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 160 | removed, err := agent.clusterDataProxy.Remove(ctx, "/devices/"+agent.deviceID, "") |
| 161 | if err != nil { |
| 162 | log.Errorw("Failed-to-remove-device-from-cluster-data-proxy", log.Fields{"error": err}) |
| 163 | return |
| 164 | } |
| 165 | if removed == nil { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 166 | log.Debugw("device-already-removed", log.Fields{"id": agent.deviceID}) |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 167 | } |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 168 | agent.exitChannel <- 1 |
khenaidoo | 4908535 | 2020-01-13 19:15:43 -0500 | [diff] [blame] | 169 | log.Debugw("device-agent-stopped", log.Fields{"deviceId": agent.deviceID, "parentId": agent.parentID}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 170 | } |
| 171 | |
Scott Baker | 8067860 | 2019-11-14 16:57:36 -0800 | [diff] [blame] | 172 | // Load the most recent state from the KVStore for the device. |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame^] | 173 | func (agent *DeviceAgent) reconcileWithKVStore(ctx context.Context) { |
Scott Baker | 8067860 | 2019-11-14 16:57:36 -0800 | [diff] [blame] | 174 | agent.lockDevice.Lock() |
| 175 | defer agent.lockDevice.Unlock() |
| 176 | log.Debug("reconciling-device-agent-devicetype") |
| 177 | // TODO: context timeout |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame^] | 178 | device, err := agent.clusterDataProxy.Get(ctx, "/devices/"+agent.deviceID, 1, true, "") |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 179 | if err != nil { |
| 180 | log.Errorw("Failed to get device info from cluster data proxy", log.Fields{"error": err}) |
| 181 | return |
| 182 | } |
| 183 | if device != nil { |
Scott Baker | 8067860 | 2019-11-14 16:57:36 -0800 | [diff] [blame] | 184 | if d, ok := device.(*voltha.Device); ok { |
| 185 | agent.deviceType = d.Adapter |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 186 | agent.device = proto.Clone(d).(*voltha.Device) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 187 | log.Debugw("reconciled-device-agent-devicetype", log.Fields{"Id": agent.deviceID, "type": agent.deviceType}) |
Scott Baker | 8067860 | 2019-11-14 16:57:36 -0800 | [diff] [blame] | 188 | } |
| 189 | } |
| 190 | } |
| 191 | |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 192 | // getDevice returns the device data from cache |
| 193 | func (agent *DeviceAgent) getDevice() *voltha.Device { |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 194 | agent.lockDevice.RLock() |
| 195 | defer agent.lockDevice.RUnlock() |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 196 | return proto.Clone(agent.device).(*voltha.Device) |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 197 | } |
| 198 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 199 | // getDeviceWithoutLock is a helper function to be used ONLY by any device agent function AFTER it has acquired the device lock. |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 200 | func (agent *DeviceAgent) getDeviceWithoutLock() *voltha.Device { |
| 201 | return proto.Clone(agent.device).(*voltha.Device) |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 202 | } |
| 203 | |
khenaidoo | 3ab3488 | 2019-05-02 21:33:30 -0400 | [diff] [blame] | 204 | // enableDevice activates a preprovisioned or a disable device |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 205 | func (agent *DeviceAgent) enableDevice(ctx context.Context) error { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 206 | agent.lockDevice.Lock() |
| 207 | defer agent.lockDevice.Unlock() |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 208 | log.Debugw("enableDevice", log.Fields{"id": agent.deviceID}) |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 209 | |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 210 | cloned := agent.getDeviceWithoutLock() |
| 211 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 212 | // First figure out which adapter will handle this device type. We do it at this stage as allow devices to be |
| 213 | // pre-provisionned with the required adapter not registered. At this stage, since we need to communicate |
| 214 | // with the adapter then we need to know the adapter that will handle this request |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 215 | adapterName, err := agent.adapterMgr.getAdapterName(cloned.Type) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 216 | if err != nil { |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 217 | log.Warnw("no-adapter-registered-for-device-type", log.Fields{"deviceType": cloned.Type, "deviceAdapter": cloned.Adapter}) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 218 | return err |
| 219 | } |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 220 | cloned.Adapter = adapterName |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 221 | |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 222 | if cloned.AdminState == voltha.AdminState_ENABLED { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 223 | log.Debugw("device-already-enabled", log.Fields{"id": agent.deviceID}) |
| 224 | return nil |
| 225 | } |
| 226 | |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 227 | if cloned.AdminState == voltha.AdminState_DELETED { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 228 | // 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] | 229 | err = status.Error(codes.FailedPrecondition, fmt.Sprintf("cannot-enable-a-deleted-device: %s ", cloned.Id)) |
| 230 | log.Warnw("invalid-state", log.Fields{"id": agent.deviceID, "state": cloned.AdminState, "error": err}) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 231 | return err |
| 232 | } |
| 233 | |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 234 | previousAdminState := cloned.AdminState |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 235 | |
| 236 | // Update the Admin State and set the operational state to activating before sending the request to the |
| 237 | // Adapters |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 238 | cloned.AdminState = voltha.AdminState_ENABLED |
| 239 | cloned.OperStatus = voltha.OperStatus_ACTIVATING |
| 240 | |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame^] | 241 | if err := agent.updateDeviceInStoreWithoutLock(ctx, cloned, false, ""); err != nil { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 242 | return err |
| 243 | } |
| 244 | |
| 245 | // Adopt the device if it was in preprovision state. In all other cases, try to reenable it. |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 246 | device := proto.Clone(cloned).(*voltha.Device) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 247 | if previousAdminState == voltha.AdminState_PREPROVISIONED { |
| 248 | if err := agent.adapterProxy.AdoptDevice(ctx, device); err != nil { |
| 249 | log.Debugw("adoptDevice-error", log.Fields{"id": agent.deviceID, "error": err}) |
| 250 | return err |
| 251 | } |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 252 | } else { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 253 | if err := agent.adapterProxy.ReEnableDevice(ctx, device); err != nil { |
| 254 | log.Debugw("renableDevice-error", log.Fields{"id": agent.deviceID, "error": err}) |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 255 | return err |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 256 | } |
| 257 | } |
| 258 | return nil |
| 259 | } |
| 260 | |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame^] | 261 | func (agent *DeviceAgent) sendBulkFlowsToAdapters(ctx context.Context, device *voltha.Device, flows *voltha.Flows, groups *voltha.FlowGroups, flowMetadata *voltha.FlowMetadata, response coreutils.Response) { |
| 262 | if err := agent.adapterProxy.UpdateFlowsBulk(ctx, device, flows, groups, flowMetadata); err != nil { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 263 | log.Debugw("update-flow-bulk-error", log.Fields{"id": agent.deviceID, "error": err}) |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 264 | response.Error(err) |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 265 | } |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 266 | response.Done() |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 267 | } |
| 268 | |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame^] | 269 | func (agent *DeviceAgent) sendIncrementalFlowsToAdapters(ctx context.Context, device *voltha.Device, flows *ofp.FlowChanges, groups *ofp.FlowGroupChanges, flowMetadata *voltha.FlowMetadata, response coreutils.Response) { |
| 270 | if err := agent.adapterProxy.UpdateFlowsIncremental(ctx, device, flows, groups, flowMetadata); err != nil { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 271 | log.Debugw("update-flow-incremental-error", log.Fields{"id": agent.deviceID, "error": err}) |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 272 | response.Error(err) |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 273 | } |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 274 | response.Done() |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 275 | } |
| 276 | |
khenaidoo | b2121e5 | 2019-12-16 17:17:22 -0500 | [diff] [blame] | 277 | //deleteFlowWithoutPreservingOrder removes a flow specified by index from the flows slice. This function will |
| 278 | //panic if the index is out of range. |
| 279 | func deleteFlowWithoutPreservingOrder(flows []*ofp.OfpFlowStats, index int) []*ofp.OfpFlowStats { |
| 280 | flows[index] = flows[len(flows)-1] |
| 281 | flows[len(flows)-1] = nil |
| 282 | return flows[:len(flows)-1] |
| 283 | } |
| 284 | |
| 285 | //deleteGroupWithoutPreservingOrder removes a group specified by index from the groups slice. This function will |
| 286 | //panic if the index is out of range. |
| 287 | func deleteGroupWithoutPreservingOrder(groups []*ofp.OfpGroupEntry, index int) []*ofp.OfpGroupEntry { |
| 288 | groups[index] = groups[len(groups)-1] |
| 289 | groups[len(groups)-1] = nil |
| 290 | return groups[:len(groups)-1] |
| 291 | } |
| 292 | |
| 293 | func flowsToUpdateToDelete(newFlows, existingFlows []*ofp.OfpFlowStats) (updatedNewFlows, flowsToDelete, updatedAllFlows []*ofp.OfpFlowStats) { |
| 294 | // Process flows |
| 295 | for _, flow := range existingFlows { |
| 296 | if idx := fu.FindFlows(newFlows, flow); idx == -1 { |
| 297 | updatedAllFlows = append(updatedAllFlows, flow) |
| 298 | } else { |
| 299 | // We have a matching flow (i.e. the following field matches: "TableId", "Priority", "Flags", "Cookie", |
| 300 | // "Match". If this is an exact match (i.e. all other fields matches as well) then this flow will be |
| 301 | // ignored. Otherwise, the previous flow will be deleted and the new one added |
| 302 | if proto.Equal(newFlows[idx], flow) { |
| 303 | // Flow already exist, remove it from the new flows but keep it in the updated flows slice |
| 304 | newFlows = deleteFlowWithoutPreservingOrder(newFlows, idx) |
| 305 | updatedAllFlows = append(updatedAllFlows, flow) |
| 306 | } else { |
| 307 | // Minor change to flow, delete old and add new one |
| 308 | flowsToDelete = append(flowsToDelete, flow) |
| 309 | } |
| 310 | } |
| 311 | } |
| 312 | updatedAllFlows = append(updatedAllFlows, newFlows...) |
| 313 | return newFlows, flowsToDelete, updatedAllFlows |
| 314 | } |
| 315 | |
| 316 | func groupsToUpdateToDelete(newGroups, existingGroups []*ofp.OfpGroupEntry) (updatedNewGroups, groupsToDelete, updatedAllGroups []*ofp.OfpGroupEntry) { |
| 317 | for _, group := range existingGroups { |
| 318 | if idx := fu.FindGroup(newGroups, group.Desc.GroupId); idx == -1 { // does not exist now |
| 319 | updatedAllGroups = append(updatedAllGroups, group) |
| 320 | } else { |
| 321 | // Follow same logic as flows |
| 322 | if proto.Equal(newGroups[idx], group) { |
| 323 | // Group already exist, remove it from the new groups |
| 324 | newGroups = deleteGroupWithoutPreservingOrder(newGroups, idx) |
| 325 | updatedAllGroups = append(updatedAllGroups, group) |
| 326 | } else { |
| 327 | // Minor change to group, delete old and add new one |
| 328 | groupsToDelete = append(groupsToDelete, group) |
| 329 | } |
| 330 | } |
| 331 | } |
| 332 | updatedAllGroups = append(updatedAllGroups, newGroups...) |
| 333 | return newGroups, groupsToDelete, updatedAllGroups |
| 334 | } |
| 335 | |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame^] | 336 | func (agent *DeviceAgent) addFlowsAndGroupsToAdapter(ctx context.Context, newFlows []*ofp.OfpFlowStats, newGroups []*ofp.OfpGroupEntry, flowMetadata *voltha.FlowMetadata) (coreutils.Response, error) { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 337 | log.Debugw("addFlowsAndGroups", log.Fields{"deviceId": agent.deviceID, "flows": newFlows, "groups": newGroups, "flowMetadata": flowMetadata}) |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 338 | |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 339 | if (len(newFlows) | len(newGroups)) == 0 { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 340 | log.Debugw("nothing-to-update", log.Fields{"deviceId": agent.deviceID, "flows": newFlows, "groups": newGroups}) |
A R Karthick | 5c28f55 | 2019-12-11 22:47:44 -0800 | [diff] [blame] | 341 | return coreutils.DoneResponse(), nil |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 342 | } |
| 343 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 344 | agent.lockDevice.Lock() |
| 345 | defer agent.lockDevice.Unlock() |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 346 | |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 347 | device := agent.getDeviceWithoutLock() |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 348 | existingFlows := proto.Clone(device.Flows).(*voltha.Flows) |
| 349 | existingGroups := proto.Clone(device.FlowGroups).(*ofp.FlowGroups) |
| 350 | |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 351 | // Process flows |
khenaidoo | b2121e5 | 2019-12-16 17:17:22 -0500 | [diff] [blame] | 352 | newFlows, flowsToDelete, updatedAllFlows := flowsToUpdateToDelete(newFlows, existingFlows.Items) |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 353 | |
| 354 | // Process groups |
khenaidoo | b2121e5 | 2019-12-16 17:17:22 -0500 | [diff] [blame] | 355 | newGroups, groupsToDelete, updatedAllGroups := groupsToUpdateToDelete(newGroups, existingGroups.Items) |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 356 | |
| 357 | // Sanity check |
khenaidoo | b2121e5 | 2019-12-16 17:17:22 -0500 | [diff] [blame] | 358 | if (len(updatedAllFlows) | len(flowsToDelete) | len(updatedAllGroups) | len(groupsToDelete)) == 0 { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 359 | log.Debugw("nothing-to-update", log.Fields{"deviceId": agent.deviceID, "flows": newFlows, "groups": newGroups}) |
A R Karthick | 5c28f55 | 2019-12-11 22:47:44 -0800 | [diff] [blame] | 360 | return coreutils.DoneResponse(), nil |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | // Send update to adapters |
| 364 | // Create two channels to receive responses from the dB and from the adapters. |
| 365 | // Do not close these channels as this function may exit on timeout before the dB or adapters get a chance |
| 366 | // to send their responses. These channels will be garbage collected once all the responses are |
| 367 | // received |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 368 | response := coreutils.NewResponse() |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 369 | dType := agent.adapterMgr.getDeviceType(device.Type) |
khenaidoo | e7be133 | 2020-01-24 18:58:33 -0500 | [diff] [blame] | 370 | if dType == nil { |
| 371 | log.Errorw("non-existent device type", log.Fields{"deviceType": device.Type}) |
| 372 | return coreutils.DoneResponse(), status.Errorf(codes.FailedPrecondition, "non-existent device type %s", device.Type) |
| 373 | } |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 374 | if !dType.AcceptsAddRemoveFlowUpdates { |
| 375 | |
khenaidoo | b2121e5 | 2019-12-16 17:17:22 -0500 | [diff] [blame] | 376 | if len(updatedAllGroups) != 0 && reflect.DeepEqual(existingGroups.Items, updatedAllGroups) && len(updatedAllFlows) != 0 && reflect.DeepEqual(existingFlows.Items, updatedAllFlows) { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 377 | log.Debugw("nothing-to-update", log.Fields{"deviceId": agent.deviceID, "flows": newFlows, "groups": newGroups}) |
A R Karthick | 5c28f55 | 2019-12-11 22:47:44 -0800 | [diff] [blame] | 378 | return coreutils.DoneResponse(), nil |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 379 | } |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame^] | 380 | go agent.sendBulkFlowsToAdapters(ctx, device, &voltha.Flows{Items: updatedAllFlows}, &voltha.FlowGroups{Items: updatedAllGroups}, flowMetadata, response) |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 381 | |
| 382 | } else { |
| 383 | flowChanges := &ofp.FlowChanges{ |
Matt Jeanneret | 518b5a4 | 2019-10-29 10:30:46 -0400 | [diff] [blame] | 384 | ToAdd: &voltha.Flows{Items: newFlows}, |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 385 | ToRemove: &voltha.Flows{Items: flowsToDelete}, |
| 386 | } |
| 387 | groupChanges := &ofp.FlowGroupChanges{ |
Matt Jeanneret | 518b5a4 | 2019-10-29 10:30:46 -0400 | [diff] [blame] | 388 | ToAdd: &voltha.FlowGroups{Items: newGroups}, |
| 389 | ToRemove: &voltha.FlowGroups{Items: groupsToDelete}, |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 390 | ToUpdate: &voltha.FlowGroups{Items: []*ofp.OfpGroupEntry{}}, |
| 391 | } |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame^] | 392 | go agent.sendIncrementalFlowsToAdapters(ctx, device, flowChanges, groupChanges, flowMetadata, response) |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | // store the changed data |
khenaidoo | b2121e5 | 2019-12-16 17:17:22 -0500 | [diff] [blame] | 396 | device.Flows = &voltha.Flows{Items: updatedAllFlows} |
| 397 | device.FlowGroups = &voltha.FlowGroups{Items: updatedAllGroups} |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame^] | 398 | if err := agent.updateDeviceWithoutLock(ctx, device); err != nil { |
A R Karthick | 5c28f55 | 2019-12-11 22:47:44 -0800 | [diff] [blame] | 399 | return coreutils.DoneResponse(), status.Errorf(codes.Internal, "failure-updating-%s", agent.deviceID) |
Kent Hagerman | 3c51397 | 2019-11-25 13:49:41 -0500 | [diff] [blame] | 400 | } |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 401 | |
A R Karthick | 5c28f55 | 2019-12-11 22:47:44 -0800 | [diff] [blame] | 402 | return response, nil |
| 403 | } |
| 404 | |
| 405 | //addFlowsAndGroups adds the "newFlows" and "newGroups" from the existing flows/groups and sends the update to the |
| 406 | //adapters |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame^] | 407 | func (agent *DeviceAgent) addFlowsAndGroups(ctx context.Context, newFlows []*ofp.OfpFlowStats, newGroups []*ofp.OfpGroupEntry, flowMetadata *voltha.FlowMetadata) error { |
| 408 | response, err := agent.addFlowsAndGroupsToAdapter(ctx, newFlows, newGroups, flowMetadata) |
A R Karthick | 5c28f55 | 2019-12-11 22:47:44 -0800 | [diff] [blame] | 409 | if err != nil { |
| 410 | return err |
| 411 | } |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 412 | if res := coreutils.WaitForNilOrErrorResponses(agent.defaultTimeout, response); res != nil { |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 413 | log.Debugw("Failed to get response from adapter[or] DB", log.Fields{"result": res}) |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 414 | return status.Errorf(codes.Aborted, "errors-%s", res) |
| 415 | } |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 416 | return nil |
| 417 | } |
| 418 | |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame^] | 419 | func (agent *DeviceAgent) deleteFlowsAndGroupsFromAdapter(ctx context.Context, flowsToDel []*ofp.OfpFlowStats, groupsToDel []*ofp.OfpGroupEntry, flowMetadata *voltha.FlowMetadata) (coreutils.Response, error) { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 420 | log.Debugw("deleteFlowsAndGroups", log.Fields{"deviceId": agent.deviceID, "flows": flowsToDel, "groups": groupsToDel}) |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 421 | |
| 422 | if (len(flowsToDel) | len(groupsToDel)) == 0 { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 423 | log.Debugw("nothing-to-update", log.Fields{"deviceId": agent.deviceID, "flows": flowsToDel, "groups": groupsToDel}) |
A R Karthick | 5c28f55 | 2019-12-11 22:47:44 -0800 | [diff] [blame] | 424 | return coreutils.DoneResponse(), nil |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | agent.lockDevice.Lock() |
| 428 | defer agent.lockDevice.Unlock() |
| 429 | |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 430 | device := agent.getDeviceWithoutLock() |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 431 | |
| 432 | existingFlows := proto.Clone(device.Flows).(*voltha.Flows) |
| 433 | existingGroups := proto.Clone(device.FlowGroups).(*ofp.FlowGroups) |
| 434 | |
| 435 | var flowsToKeep []*ofp.OfpFlowStats |
| 436 | var groupsToKeep []*ofp.OfpGroupEntry |
| 437 | |
| 438 | // Process flows |
| 439 | for _, flow := range existingFlows.Items { |
| 440 | if idx := fu.FindFlows(flowsToDel, flow); idx == -1 { |
| 441 | flowsToKeep = append(flowsToKeep, flow) |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | // Process groups |
| 446 | for _, group := range existingGroups.Items { |
| 447 | if fu.FindGroup(groupsToDel, group.Desc.GroupId) == -1 { // does not exist now |
| 448 | groupsToKeep = append(groupsToKeep, group) |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | log.Debugw("deleteFlowsAndGroups", |
| 453 | log.Fields{ |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 454 | "deviceId": agent.deviceID, |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 455 | "flowsToDel": len(flowsToDel), |
| 456 | "flowsToKeep": len(flowsToKeep), |
| 457 | "groupsToDel": len(groupsToDel), |
| 458 | "groupsToKeep": len(groupsToKeep), |
| 459 | }) |
| 460 | |
| 461 | // Sanity check |
| 462 | if (len(flowsToKeep) | len(flowsToDel) | len(groupsToKeep) | len(groupsToDel)) == 0 { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 463 | log.Debugw("nothing-to-update", log.Fields{"deviceId": agent.deviceID, "flowsToDel": flowsToDel, "groupsToDel": groupsToDel}) |
A R Karthick | 5c28f55 | 2019-12-11 22:47:44 -0800 | [diff] [blame] | 464 | return coreutils.DoneResponse(), nil |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | // Send update to adapters |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 468 | response := coreutils.NewResponse() |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 469 | dType := agent.adapterMgr.getDeviceType(device.Type) |
khenaidoo | e7be133 | 2020-01-24 18:58:33 -0500 | [diff] [blame] | 470 | if dType == nil { |
| 471 | log.Errorw("non-existent device type", log.Fields{"deviceType": device.Type}) |
| 472 | return coreutils.DoneResponse(), status.Errorf(codes.FailedPrecondition, "non-existent device type %s", device.Type) |
| 473 | } |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 474 | if !dType.AcceptsAddRemoveFlowUpdates { |
| 475 | if len(groupsToKeep) != 0 && reflect.DeepEqual(existingGroups.Items, groupsToKeep) && len(flowsToKeep) != 0 && reflect.DeepEqual(existingFlows.Items, flowsToKeep) { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 476 | log.Debugw("nothing-to-update", log.Fields{"deviceId": agent.deviceID, "flowsToDel": flowsToDel, "groupsToDel": groupsToDel}) |
A R Karthick | 5c28f55 | 2019-12-11 22:47:44 -0800 | [diff] [blame] | 477 | return coreutils.DoneResponse(), nil |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 478 | } |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame^] | 479 | go agent.sendBulkFlowsToAdapters(ctx, device, &voltha.Flows{Items: flowsToKeep}, &voltha.FlowGroups{Items: groupsToKeep}, flowMetadata, response) |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 480 | } else { |
| 481 | flowChanges := &ofp.FlowChanges{ |
| 482 | ToAdd: &voltha.Flows{Items: []*ofp.OfpFlowStats{}}, |
| 483 | ToRemove: &voltha.Flows{Items: flowsToDel}, |
| 484 | } |
| 485 | groupChanges := &ofp.FlowGroupChanges{ |
| 486 | ToAdd: &voltha.FlowGroups{Items: []*ofp.OfpGroupEntry{}}, |
| 487 | ToRemove: &voltha.FlowGroups{Items: groupsToDel}, |
| 488 | ToUpdate: &voltha.FlowGroups{Items: []*ofp.OfpGroupEntry{}}, |
| 489 | } |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame^] | 490 | go agent.sendIncrementalFlowsToAdapters(ctx, device, flowChanges, groupChanges, flowMetadata, response) |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | // store the changed data |
| 494 | device.Flows = &voltha.Flows{Items: flowsToKeep} |
| 495 | device.FlowGroups = &voltha.FlowGroups{Items: groupsToKeep} |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame^] | 496 | if err := agent.updateDeviceWithoutLock(ctx, device); err != nil { |
A R Karthick | 5c28f55 | 2019-12-11 22:47:44 -0800 | [diff] [blame] | 497 | return coreutils.DoneResponse(), status.Errorf(codes.Internal, "failure-updating-%s", agent.deviceID) |
Kent Hagerman | 3c51397 | 2019-11-25 13:49:41 -0500 | [diff] [blame] | 498 | } |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 499 | |
A R Karthick | 5c28f55 | 2019-12-11 22:47:44 -0800 | [diff] [blame] | 500 | return response, nil |
| 501 | |
| 502 | } |
| 503 | |
| 504 | //deleteFlowsAndGroups removes the "flowsToDel" and "groupsToDel" from the existing flows/groups and sends the update to the |
| 505 | //adapters |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame^] | 506 | func (agent *DeviceAgent) deleteFlowsAndGroups(ctx context.Context, flowsToDel []*ofp.OfpFlowStats, groupsToDel []*ofp.OfpGroupEntry, flowMetadata *voltha.FlowMetadata) error { |
| 507 | response, err := agent.deleteFlowsAndGroupsFromAdapter(ctx, flowsToDel, groupsToDel, flowMetadata) |
A R Karthick | 5c28f55 | 2019-12-11 22:47:44 -0800 | [diff] [blame] | 508 | if err != nil { |
| 509 | return err |
| 510 | } |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 511 | if res := coreutils.WaitForNilOrErrorResponses(agent.defaultTimeout, response); res != nil { |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 512 | return status.Errorf(codes.Aborted, "errors-%s", res) |
| 513 | } |
| 514 | return nil |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 515 | } |
| 516 | |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame^] | 517 | func (agent *DeviceAgent) updateFlowsAndGroupsToAdapter(ctx context.Context, updatedFlows []*ofp.OfpFlowStats, updatedGroups []*ofp.OfpGroupEntry, flowMetadata *voltha.FlowMetadata) (coreutils.Response, error) { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 518 | log.Debugw("updateFlowsAndGroups", log.Fields{"deviceId": agent.deviceID, "flows": updatedFlows, "groups": updatedGroups}) |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 519 | |
| 520 | if (len(updatedFlows) | len(updatedGroups)) == 0 { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 521 | log.Debugw("nothing-to-update", log.Fields{"deviceId": agent.deviceID, "flows": updatedFlows, "groups": updatedGroups}) |
A R Karthick | 5c28f55 | 2019-12-11 22:47:44 -0800 | [diff] [blame] | 522 | return coreutils.DoneResponse(), nil |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | agent.lockDevice.Lock() |
| 526 | defer agent.lockDevice.Unlock() |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 527 | |
| 528 | device := agent.getDeviceWithoutLock() |
| 529 | |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 530 | existingFlows := proto.Clone(device.Flows).(*voltha.Flows) |
| 531 | existingGroups := proto.Clone(device.FlowGroups).(*ofp.FlowGroups) |
| 532 | |
| 533 | if len(updatedGroups) != 0 && reflect.DeepEqual(existingGroups.Items, updatedGroups) && len(updatedFlows) != 0 && reflect.DeepEqual(existingFlows.Items, updatedFlows) { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 534 | log.Debugw("nothing-to-update", log.Fields{"deviceId": agent.deviceID, "flows": updatedFlows, "groups": updatedGroups}) |
A R Karthick | 5c28f55 | 2019-12-11 22:47:44 -0800 | [diff] [blame] | 535 | return coreutils.DoneResponse(), nil |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | log.Debugw("updating-flows-and-groups", |
| 539 | log.Fields{ |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 540 | "deviceId": agent.deviceID, |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 541 | "updatedFlows": updatedFlows, |
| 542 | "updatedGroups": updatedGroups, |
| 543 | }) |
| 544 | |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 545 | response := coreutils.NewResponse() |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 546 | dType := agent.adapterMgr.getDeviceType(device.Type) |
khenaidoo | e7be133 | 2020-01-24 18:58:33 -0500 | [diff] [blame] | 547 | if dType == nil { |
| 548 | log.Errorw("non-existent device type", log.Fields{"deviceType": device.Type}) |
| 549 | return coreutils.DoneResponse(), status.Errorf(codes.FailedPrecondition, "non-existent device type %s", device.Type) |
| 550 | } |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 551 | |
| 552 | // Process bulk flow update differently than incremental update |
| 553 | if !dType.AcceptsAddRemoveFlowUpdates { |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame^] | 554 | go agent.sendBulkFlowsToAdapters(ctx, device, &voltha.Flows{Items: updatedFlows}, &voltha.FlowGroups{Items: updatedGroups}, nil, response) |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 555 | } else { |
| 556 | var flowsToAdd []*ofp.OfpFlowStats |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 557 | var flowsToDelete []*ofp.OfpFlowStats |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 558 | var groupsToAdd []*ofp.OfpGroupEntry |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 559 | var groupsToDelete []*ofp.OfpGroupEntry |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 560 | |
| 561 | // Process flows |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 562 | for _, flow := range updatedFlows { |
| 563 | if idx := fu.FindFlows(existingFlows.Items, flow); idx == -1 { |
| 564 | flowsToAdd = append(flowsToAdd, flow) |
| 565 | } |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 566 | } |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 567 | for _, flow := range existingFlows.Items { |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 568 | if idx := fu.FindFlows(updatedFlows, flow); idx != -1 { |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 569 | flowsToDelete = append(flowsToDelete, flow) |
| 570 | } |
| 571 | } |
| 572 | |
| 573 | // Process groups |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 574 | for _, g := range updatedGroups { |
| 575 | if fu.FindGroup(existingGroups.Items, g.Desc.GroupId) == -1 { // does not exist now |
| 576 | groupsToAdd = append(groupsToAdd, g) |
| 577 | } |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 578 | } |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 579 | for _, group := range existingGroups.Items { |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 580 | if fu.FindGroup(updatedGroups, group.Desc.GroupId) != -1 { // does not exist now |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 581 | groupsToDelete = append(groupsToDelete, group) |
| 582 | } |
| 583 | } |
| 584 | |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 585 | log.Debugw("updating-flows-and-groups", |
| 586 | log.Fields{ |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 587 | "deviceId": agent.deviceID, |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 588 | "flowsToAdd": flowsToAdd, |
| 589 | "flowsToDelete": flowsToDelete, |
| 590 | "groupsToAdd": groupsToAdd, |
| 591 | "groupsToDelete": groupsToDelete, |
| 592 | }) |
| 593 | |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 594 | // Sanity check |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 595 | if (len(flowsToAdd) | len(flowsToDelete) | len(groupsToAdd) | len(groupsToDelete) | len(updatedGroups)) == 0 { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 596 | log.Debugw("nothing-to-update", log.Fields{"deviceId": agent.deviceID, "flows": updatedFlows, "groups": updatedGroups}) |
A R Karthick | 5c28f55 | 2019-12-11 22:47:44 -0800 | [diff] [blame] | 597 | return coreutils.DoneResponse(), nil |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 598 | } |
| 599 | |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 600 | flowChanges := &ofp.FlowChanges{ |
| 601 | ToAdd: &voltha.Flows{Items: flowsToAdd}, |
| 602 | ToRemove: &voltha.Flows{Items: flowsToDelete}, |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 603 | } |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 604 | groupChanges := &ofp.FlowGroupChanges{ |
| 605 | ToAdd: &voltha.FlowGroups{Items: groupsToAdd}, |
| 606 | ToRemove: &voltha.FlowGroups{Items: groupsToDelete}, |
| 607 | ToUpdate: &voltha.FlowGroups{Items: updatedGroups}, |
| 608 | } |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame^] | 609 | go agent.sendIncrementalFlowsToAdapters(ctx, device, flowChanges, groupChanges, flowMetadata, response) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 610 | } |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 611 | |
| 612 | // store the updated data |
| 613 | device.Flows = &voltha.Flows{Items: updatedFlows} |
| 614 | device.FlowGroups = &voltha.FlowGroups{Items: updatedGroups} |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame^] | 615 | if err := agent.updateDeviceWithoutLock(ctx, device); err != nil { |
A R Karthick | 5c28f55 | 2019-12-11 22:47:44 -0800 | [diff] [blame] | 616 | return coreutils.DoneResponse(), status.Errorf(codes.Internal, "failure-updating-%s", agent.deviceID) |
Kent Hagerman | 3c51397 | 2019-11-25 13:49:41 -0500 | [diff] [blame] | 617 | } |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 618 | |
A R Karthick | 5c28f55 | 2019-12-11 22:47:44 -0800 | [diff] [blame] | 619 | return response, nil |
| 620 | } |
| 621 | |
| 622 | //updateFlowsAndGroups replaces the existing flows and groups with "updatedFlows" and "updatedGroups" respectively. It |
| 623 | //also sends the updates to the adapters |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame^] | 624 | func (agent *DeviceAgent) updateFlowsAndGroups(ctx context.Context, updatedFlows []*ofp.OfpFlowStats, updatedGroups []*ofp.OfpGroupEntry, flowMetadata *voltha.FlowMetadata) error { |
| 625 | response, err := agent.updateFlowsAndGroupsToAdapter(ctx, updatedFlows, updatedGroups, flowMetadata) |
A R Karthick | 5c28f55 | 2019-12-11 22:47:44 -0800 | [diff] [blame] | 626 | if err != nil { |
| 627 | return err |
| 628 | } |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 629 | if res := coreutils.WaitForNilOrErrorResponses(agent.defaultTimeout, response); res != nil { |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 630 | return status.Errorf(codes.Aborted, "errors-%s", res) |
| 631 | } |
| 632 | return nil |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 633 | } |
| 634 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 635 | //disableDevice disable a device |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 636 | func (agent *DeviceAgent) disableDevice(ctx context.Context) error { |
khenaidoo | 59ef7be | 2019-06-21 12:40:28 -0400 | [diff] [blame] | 637 | agent.lockDevice.Lock() |
| 638 | defer agent.lockDevice.Unlock() |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 639 | log.Debugw("disableDevice", log.Fields{"id": agent.deviceID}) |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 640 | |
| 641 | cloned := agent.getDeviceWithoutLock() |
| 642 | |
| 643 | if cloned.AdminState == voltha.AdminState_DISABLED { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 644 | log.Debugw("device-already-disabled", log.Fields{"id": agent.deviceID}) |
| 645 | return nil |
| 646 | } |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 647 | if cloned.AdminState == voltha.AdminState_PREPROVISIONED || |
| 648 | cloned.AdminState == voltha.AdminState_DELETED { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 649 | log.Debugw("device-not-enabled", log.Fields{"id": agent.deviceID}) |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 650 | 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] | 651 | } |
khenaidoo | 4554f7c | 2019-05-29 22:13:15 -0400 | [diff] [blame] | 652 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 653 | // Update the Admin State and operational state before sending the request out |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 654 | cloned.AdminState = voltha.AdminState_DISABLED |
| 655 | cloned.OperStatus = voltha.OperStatus_UNKNOWN |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame^] | 656 | if err := agent.updateDeviceInStoreWithoutLock(ctx, cloned, false, ""); err != nil { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 657 | return err |
| 658 | } |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 659 | if err := agent.adapterProxy.DisableDevice(ctx, proto.Clone(cloned).(*voltha.Device)); err != nil { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 660 | log.Debugw("disableDevice-error", log.Fields{"id": agent.deviceID, "error": err}) |
| 661 | return err |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 662 | } |
| 663 | return nil |
| 664 | } |
| 665 | |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame^] | 666 | func (agent *DeviceAgent) updateAdminState(ctx context.Context, adminState voltha.AdminState_Types) error { |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 667 | agent.lockDevice.Lock() |
| 668 | defer agent.lockDevice.Unlock() |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 669 | log.Debugw("updateAdminState", log.Fields{"id": agent.deviceID}) |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 670 | |
| 671 | cloned := agent.getDeviceWithoutLock() |
| 672 | |
| 673 | if cloned.AdminState == adminState { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 674 | log.Debugw("no-change-needed", log.Fields{"id": agent.deviceID, "state": adminState}) |
| 675 | return nil |
| 676 | } |
| 677 | // Received an Ack (no error found above). Now update the device in the model to the expected state |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 678 | cloned.AdminState = adminState |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame^] | 679 | if err := agent.updateDeviceInStoreWithoutLock(ctx, cloned, false, ""); err != nil { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 680 | return err |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 681 | } |
| 682 | return nil |
| 683 | } |
| 684 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 685 | func (agent *DeviceAgent) rebootDevice(ctx context.Context) error { |
| 686 | agent.lockDevice.Lock() |
| 687 | defer agent.lockDevice.Unlock() |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 688 | log.Debugw("rebootDevice", log.Fields{"id": agent.deviceID}) |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 689 | |
| 690 | device := agent.getDeviceWithoutLock() |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 691 | if err := agent.adapterProxy.RebootDevice(ctx, device); err != nil { |
| 692 | log.Debugw("rebootDevice-error", log.Fields{"id": agent.deviceID, "error": err}) |
| 693 | return err |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 694 | } |
| 695 | return nil |
| 696 | } |
| 697 | |
| 698 | func (agent *DeviceAgent) deleteDevice(ctx context.Context) error { |
| 699 | agent.lockDevice.Lock() |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 700 | defer agent.lockDevice.Unlock() |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 701 | log.Debugw("deleteDevice", log.Fields{"id": agent.deviceID}) |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 702 | |
| 703 | cloned := agent.getDeviceWithoutLock() |
| 704 | if cloned.AdminState == voltha.AdminState_DELETED { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 705 | log.Debugw("device-already-in-deleted-state", log.Fields{"id": agent.deviceID}) |
| 706 | return nil |
| 707 | } |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 708 | if (cloned.AdminState != voltha.AdminState_DISABLED) && |
| 709 | (cloned.AdminState != voltha.AdminState_PREPROVISIONED) { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 710 | log.Debugw("device-not-disabled", log.Fields{"id": agent.deviceID}) |
| 711 | //TODO: Needs customized error message |
| 712 | return status.Errorf(codes.FailedPrecondition, "deviceId:%s, expected-admin-state:%s", agent.deviceID, voltha.AdminState_DISABLED) |
| 713 | } |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 714 | if cloned.AdminState != voltha.AdminState_PREPROVISIONED { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 715 | // Send the request to an Adapter only if the device is not in poreporovision state and wait for a response |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 716 | if err := agent.adapterProxy.DeleteDevice(ctx, cloned); err != nil { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 717 | log.Debugw("deleteDevice-error", log.Fields{"id": agent.deviceID, "error": err}) |
Mahir Gunyel | b585167 | 2019-07-24 10:46:26 +0300 | [diff] [blame] | 718 | return err |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 719 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 720 | } |
| 721 | // Set the state to deleted after we receive an Ack - this will trigger some background process to clean up |
| 722 | // the device as well as its association with the logical device |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 723 | cloned.AdminState = voltha.AdminState_DELETED |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame^] | 724 | if err := agent.updateDeviceInStoreWithoutLock(ctx, cloned, false, ""); err != nil { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 725 | return err |
| 726 | } |
| 727 | // If this is a child device then remove the associated peer ports on the parent device |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 728 | if !cloned.Root { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 729 | go func() { |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame^] | 730 | // since the caller does not wait for this for complete, use background context |
| 731 | err := agent.deviceMgr.deletePeerPorts(context.Background(), cloned.ParentId, cloned.Id) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 732 | if err != nil { |
| 733 | log.Errorw("unable-to-delete-peer-ports", log.Fields{"error": err}) |
| 734 | } |
| 735 | }() |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 736 | } |
| 737 | return nil |
| 738 | } |
| 739 | |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame^] | 740 | func (agent *DeviceAgent) setParentID(ctx context.Context, device *voltha.Device, parentID string) error { |
khenaidoo | ad06fd7 | 2019-10-28 12:26:05 -0400 | [diff] [blame] | 741 | agent.lockDevice.Lock() |
| 742 | defer agent.lockDevice.Unlock() |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 743 | log.Debugw("setParentId", log.Fields{"deviceId": device.Id, "parentId": parentID}) |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 744 | |
| 745 | cloned := agent.getDeviceWithoutLock() |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 746 | cloned.ParentId = parentID |
| 747 | // Store the device |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame^] | 748 | if err := agent.updateDeviceInStoreWithoutLock(ctx, cloned, false, ""); err != nil { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 749 | return err |
| 750 | } |
| 751 | return nil |
khenaidoo | ad06fd7 | 2019-10-28 12:26:05 -0400 | [diff] [blame] | 752 | } |
| 753 | |
khenaidoo | b312747 | 2019-07-24 21:04:55 -0400 | [diff] [blame] | 754 | func (agent *DeviceAgent) updatePmConfigs(ctx context.Context, pmConfigs *voltha.PmConfigs) error { |
| 755 | agent.lockDevice.Lock() |
| 756 | defer agent.lockDevice.Unlock() |
| 757 | log.Debugw("updatePmConfigs", log.Fields{"id": pmConfigs.Id}) |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 758 | |
| 759 | cloned := agent.getDeviceWithoutLock() |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 760 | cloned.PmConfigs = proto.Clone(pmConfigs).(*voltha.PmConfigs) |
| 761 | // Store the device |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame^] | 762 | if err := agent.updateDeviceInStoreWithoutLock(ctx, cloned, false, ""); err != nil { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 763 | return err |
| 764 | } |
| 765 | // Send the request to the adapter |
| 766 | if err := agent.adapterProxy.UpdatePmConfigs(ctx, cloned, pmConfigs); err != nil { |
| 767 | log.Errorw("update-pm-configs-error", log.Fields{"id": agent.deviceID, "error": err}) |
| 768 | return err |
| 769 | } |
| 770 | return nil |
khenaidoo | b312747 | 2019-07-24 21:04:55 -0400 | [diff] [blame] | 771 | } |
| 772 | |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame^] | 773 | func (agent *DeviceAgent) initPmConfigs(ctx context.Context, pmConfigs *voltha.PmConfigs) error { |
khenaidoo | b312747 | 2019-07-24 21:04:55 -0400 | [diff] [blame] | 774 | agent.lockDevice.Lock() |
| 775 | defer agent.lockDevice.Unlock() |
| 776 | log.Debugw("initPmConfigs", log.Fields{"id": pmConfigs.Id}) |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 777 | |
| 778 | cloned := agent.getDeviceWithoutLock() |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 779 | cloned.PmConfigs = proto.Clone(pmConfigs).(*voltha.PmConfigs) |
| 780 | // Store the device |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame^] | 781 | updateCtx := context.WithValue(ctx, model.RequestTimestamp, time.Now().UnixNano()) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 782 | afterUpdate, err := agent.clusterDataProxy.Update(updateCtx, "/devices/"+agent.deviceID, cloned, false, "") |
| 783 | if err != nil { |
| 784 | return status.Errorf(codes.Internal, "%s", agent.deviceID) |
| 785 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 786 | if afterUpdate == nil { |
| 787 | return status.Errorf(codes.Internal, "%s", agent.deviceID) |
| 788 | } |
| 789 | return nil |
khenaidoo | b312747 | 2019-07-24 21:04:55 -0400 | [diff] [blame] | 790 | } |
| 791 | |
| 792 | func (agent *DeviceAgent) listPmConfigs(ctx context.Context) (*voltha.PmConfigs, error) { |
| 793 | agent.lockDevice.RLock() |
| 794 | defer agent.lockDevice.RUnlock() |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 795 | log.Debugw("listPmConfigs", log.Fields{"id": agent.deviceID}) |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 796 | |
| 797 | return agent.getDeviceWithoutLock().PmConfigs, nil |
khenaidoo | b312747 | 2019-07-24 21:04:55 -0400 | [diff] [blame] | 798 | } |
| 799 | |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 800 | func (agent *DeviceAgent) downloadImage(ctx context.Context, img *voltha.ImageDownload) (*voltha.OperationResp, error) { |
| 801 | agent.lockDevice.Lock() |
| 802 | defer agent.lockDevice.Unlock() |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 803 | log.Debugw("downloadImage", log.Fields{"id": agent.deviceID}) |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 804 | |
| 805 | device := agent.getDeviceWithoutLock() |
| 806 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 807 | if device.AdminState != voltha.AdminState_ENABLED { |
| 808 | log.Debugw("device-not-enabled", log.Fields{"id": agent.deviceID}) |
| 809 | return nil, status.Errorf(codes.FailedPrecondition, "deviceId:%s, expected-admin-state:%s", agent.deviceID, voltha.AdminState_ENABLED) |
| 810 | } |
| 811 | // Save the image |
| 812 | clonedImg := proto.Clone(img).(*voltha.ImageDownload) |
| 813 | clonedImg.DownloadState = voltha.ImageDownload_DOWNLOAD_REQUESTED |
| 814 | cloned := proto.Clone(device).(*voltha.Device) |
| 815 | if cloned.ImageDownloads == nil { |
| 816 | cloned.ImageDownloads = []*voltha.ImageDownload{clonedImg} |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 817 | } else { |
| 818 | if device.AdminState != voltha.AdminState_ENABLED { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 819 | log.Debugw("device-not-enabled", log.Fields{"id": agent.deviceID}) |
| 820 | 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] | 821 | } |
| 822 | // Save the image |
| 823 | clonedImg := proto.Clone(img).(*voltha.ImageDownload) |
Stephane Barbarie | df5479f | 2019-01-29 22:13:00 -0500 | [diff] [blame] | 824 | clonedImg.DownloadState = voltha.ImageDownload_DOWNLOAD_REQUESTED |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 825 | cloned := proto.Clone(device).(*voltha.Device) |
| 826 | if cloned.ImageDownloads == nil { |
| 827 | cloned.ImageDownloads = []*voltha.ImageDownload{clonedImg} |
| 828 | } else { |
| 829 | cloned.ImageDownloads = append(cloned.ImageDownloads, clonedImg) |
| 830 | } |
| 831 | cloned.AdminState = voltha.AdminState_DOWNLOADING_IMAGE |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame^] | 832 | if err := agent.updateDeviceInStoreWithoutLock(ctx, cloned, false, ""); err != nil { |
Mahir Gunyel | b585167 | 2019-07-24 10:46:26 +0300 | [diff] [blame] | 833 | return nil, err |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 834 | } |
| 835 | // Send the request to the adapter |
| 836 | if err := agent.adapterProxy.DownloadImage(ctx, cloned, clonedImg); err != nil { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 837 | log.Debugw("downloadImage-error", log.Fields{"id": agent.deviceID, "error": err, "image": img.Name}) |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 838 | return nil, err |
| 839 | } |
| 840 | } |
| 841 | return &voltha.OperationResp{Code: voltha.OperationResp_OPERATION_SUCCESS}, nil |
| 842 | } |
| 843 | |
| 844 | // isImageRegistered is a helper method to figure out if an image is already registered |
| 845 | func isImageRegistered(img *voltha.ImageDownload, device *voltha.Device) bool { |
| 846 | for _, image := range device.ImageDownloads { |
| 847 | if image.Id == img.Id && image.Name == img.Name { |
| 848 | return true |
| 849 | } |
| 850 | } |
| 851 | return false |
| 852 | } |
| 853 | |
| 854 | func (agent *DeviceAgent) cancelImageDownload(ctx context.Context, img *voltha.ImageDownload) (*voltha.OperationResp, error) { |
| 855 | agent.lockDevice.Lock() |
| 856 | defer agent.lockDevice.Unlock() |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 857 | log.Debugw("cancelImageDownload", log.Fields{"id": agent.deviceID}) |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 858 | |
| 859 | device := agent.getDeviceWithoutLock() |
| 860 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 861 | // Verify whether the Image is in the list of image being downloaded |
| 862 | if !isImageRegistered(img, device) { |
| 863 | return nil, status.Errorf(codes.FailedPrecondition, "deviceId:%s, image-not-registered:%s", agent.deviceID, img.Name) |
| 864 | } |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 865 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 866 | // Update image download state |
| 867 | cloned := proto.Clone(device).(*voltha.Device) |
| 868 | for _, image := range cloned.ImageDownloads { |
| 869 | if image.Id == img.Id && image.Name == img.Name { |
| 870 | image.DownloadState = voltha.ImageDownload_DOWNLOAD_CANCELLED |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 871 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 872 | } |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 873 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 874 | if device.AdminState == voltha.AdminState_DOWNLOADING_IMAGE { |
| 875 | // Set the device to Enabled |
| 876 | cloned.AdminState = voltha.AdminState_ENABLED |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame^] | 877 | if err := agent.updateDeviceInStoreWithoutLock(ctx, cloned, false, ""); err != nil { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 878 | return nil, err |
| 879 | } |
| 880 | // Send the request to the adapter |
| 881 | if err := agent.adapterProxy.CancelImageDownload(ctx, device, img); err != nil { |
| 882 | log.Debugw("cancelImageDownload-error", log.Fields{"id": agent.deviceID, "error": err, "image": img.Name}) |
| 883 | return nil, err |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 884 | } |
| 885 | } |
| 886 | return &voltha.OperationResp{Code: voltha.OperationResp_OPERATION_SUCCESS}, nil |
serkant.uluderya | 334479d | 2019-04-10 08:26:15 -0700 | [diff] [blame] | 887 | } |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 888 | |
| 889 | func (agent *DeviceAgent) activateImage(ctx context.Context, img *voltha.ImageDownload) (*voltha.OperationResp, error) { |
| 890 | agent.lockDevice.Lock() |
| 891 | defer agent.lockDevice.Unlock() |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 892 | log.Debugw("activateImage", log.Fields{"id": agent.deviceID}) |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 893 | cloned := agent.getDeviceWithoutLock() |
| 894 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 895 | // Verify whether the Image is in the list of image being downloaded |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 896 | if !isImageRegistered(img, cloned) { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 897 | return nil, status.Errorf(codes.FailedPrecondition, "deviceId:%s, image-not-registered:%s", agent.deviceID, img.Name) |
| 898 | } |
| 899 | |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 900 | if cloned.AdminState == voltha.AdminState_DOWNLOADING_IMAGE { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 901 | return nil, status.Errorf(codes.FailedPrecondition, "deviceId:%s, device-in-downloading-state:%s", agent.deviceID, img.Name) |
| 902 | } |
| 903 | // Update image download state |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 904 | for _, image := range cloned.ImageDownloads { |
| 905 | if image.Id == img.Id && image.Name == img.Name { |
| 906 | image.ImageState = voltha.ImageDownload_IMAGE_ACTIVATING |
| 907 | } |
| 908 | } |
| 909 | // Set the device to downloading_image |
| 910 | cloned.AdminState = voltha.AdminState_DOWNLOADING_IMAGE |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame^] | 911 | if err := agent.updateDeviceInStoreWithoutLock(ctx, cloned, false, ""); err != nil { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 912 | return nil, err |
| 913 | } |
| 914 | |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 915 | if err := agent.adapterProxy.ActivateImageUpdate(ctx, proto.Clone(cloned).(*voltha.Device), img); err != nil { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 916 | log.Debugw("activateImage-error", log.Fields{"id": agent.deviceID, "error": err, "image": img.Name}) |
| 917 | return nil, err |
| 918 | } |
| 919 | // The status of the AdminState will be changed following the update_download_status response from the adapter |
| 920 | // The image name will also be removed from the device list |
serkant.uluderya | 334479d | 2019-04-10 08:26:15 -0700 | [diff] [blame] | 921 | return &voltha.OperationResp{Code: voltha.OperationResp_OPERATION_SUCCESS}, nil |
| 922 | } |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 923 | |
| 924 | func (agent *DeviceAgent) revertImage(ctx context.Context, img *voltha.ImageDownload) (*voltha.OperationResp, error) { |
| 925 | agent.lockDevice.Lock() |
| 926 | defer agent.lockDevice.Unlock() |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 927 | log.Debugw("revertImage", log.Fields{"id": agent.deviceID}) |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 928 | |
| 929 | cloned := agent.getDeviceWithoutLock() |
| 930 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 931 | // Verify whether the Image is in the list of image being downloaded |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 932 | if !isImageRegistered(img, cloned) { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 933 | return nil, status.Errorf(codes.FailedPrecondition, "deviceId:%s, image-not-registered:%s", agent.deviceID, img.Name) |
| 934 | } |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 935 | |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 936 | if cloned.AdminState != voltha.AdminState_ENABLED { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 937 | return nil, status.Errorf(codes.FailedPrecondition, "deviceId:%s, device-not-enabled-state:%s", agent.deviceID, img.Name) |
| 938 | } |
| 939 | // Update image download state |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 940 | for _, image := range cloned.ImageDownloads { |
| 941 | if image.Id == img.Id && image.Name == img.Name { |
| 942 | image.ImageState = voltha.ImageDownload_IMAGE_REVERTING |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 943 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 944 | } |
Mahir Gunyel | b585167 | 2019-07-24 10:46:26 +0300 | [diff] [blame] | 945 | |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame^] | 946 | if err := agent.updateDeviceInStoreWithoutLock(ctx, cloned, false, ""); err != nil { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 947 | return nil, err |
| 948 | } |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 949 | |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 950 | if err := agent.adapterProxy.RevertImageUpdate(ctx, proto.Clone(cloned).(*voltha.Device), img); err != nil { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 951 | log.Debugw("revertImage-error", log.Fields{"id": agent.deviceID, "error": err, "image": img.Name}) |
| 952 | return nil, err |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 953 | } |
| 954 | return &voltha.OperationResp{Code: voltha.OperationResp_OPERATION_SUCCESS}, nil |
serkant.uluderya | 334479d | 2019-04-10 08:26:15 -0700 | [diff] [blame] | 955 | } |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 956 | |
| 957 | func (agent *DeviceAgent) getImageDownloadStatus(ctx context.Context, img *voltha.ImageDownload) (*voltha.ImageDownload, error) { |
| 958 | agent.lockDevice.Lock() |
| 959 | defer agent.lockDevice.Unlock() |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 960 | log.Debugw("getImageDownloadStatus", log.Fields{"id": agent.deviceID}) |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 961 | |
| 962 | cloned := agent.getDeviceWithoutLock() |
| 963 | resp, err := agent.adapterProxy.GetImageDownloadStatus(ctx, cloned, img) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 964 | if err != nil { |
| 965 | log.Debugw("getImageDownloadStatus-error", log.Fields{"id": agent.deviceID, "error": err, "image": img.Name}) |
| 966 | return nil, err |
| 967 | } |
| 968 | return resp, nil |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 969 | } |
| 970 | |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame^] | 971 | func (agent *DeviceAgent) updateImageDownload(ctx context.Context, img *voltha.ImageDownload) error { |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 972 | agent.lockDevice.Lock() |
| 973 | defer agent.lockDevice.Unlock() |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 974 | log.Debugw("updateImageDownload", log.Fields{"id": agent.deviceID}) |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 975 | |
| 976 | cloned := agent.getDeviceWithoutLock() |
| 977 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 978 | // Update the image as well as remove it if the download was cancelled |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 979 | clonedImages := make([]*voltha.ImageDownload, len(cloned.ImageDownloads)) |
| 980 | for _, image := range cloned.ImageDownloads { |
| 981 | if image.Id == img.Id && image.Name == img.Name { |
| 982 | if image.DownloadState != voltha.ImageDownload_DOWNLOAD_CANCELLED { |
| 983 | clonedImages = append(clonedImages, img) |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 984 | } |
| 985 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 986 | } |
| 987 | cloned.ImageDownloads = clonedImages |
| 988 | // Set the Admin state to enabled if required |
| 989 | if (img.DownloadState != voltha.ImageDownload_DOWNLOAD_REQUESTED && |
| 990 | img.DownloadState != voltha.ImageDownload_DOWNLOAD_STARTED) || |
| 991 | (img.ImageState != voltha.ImageDownload_IMAGE_ACTIVATING) { |
| 992 | cloned.AdminState = voltha.AdminState_ENABLED |
| 993 | } |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 994 | |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame^] | 995 | if err := agent.updateDeviceInStoreWithoutLock(ctx, cloned, false, ""); err != nil { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 996 | return err |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 997 | } |
| 998 | return nil |
| 999 | } |
| 1000 | |
| 1001 | func (agent *DeviceAgent) getImageDownload(ctx context.Context, img *voltha.ImageDownload) (*voltha.ImageDownload, error) { |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 1002 | agent.lockDevice.RLock() |
| 1003 | defer agent.lockDevice.RUnlock() |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1004 | log.Debugw("getImageDownload", log.Fields{"id": agent.deviceID}) |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1005 | |
| 1006 | cloned := agent.getDeviceWithoutLock() |
| 1007 | for _, image := range cloned.ImageDownloads { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1008 | if image.Id == img.Id && image.Name == img.Name { |
| 1009 | return image, nil |
| 1010 | } |
| 1011 | } |
| 1012 | return nil, status.Errorf(codes.NotFound, "image-not-found:%s", img.Name) |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 1013 | } |
| 1014 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1015 | func (agent *DeviceAgent) listImageDownloads(ctx context.Context, deviceID string) (*voltha.ImageDownloads, error) { |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 1016 | agent.lockDevice.RLock() |
| 1017 | defer agent.lockDevice.RUnlock() |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1018 | log.Debugw("listImageDownloads", log.Fields{"id": agent.deviceID}) |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1019 | |
| 1020 | return &voltha.ImageDownloads{Items: agent.getDeviceWithoutLock().ImageDownloads}, nil |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 1021 | } |
| 1022 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 1023 | // getPorts retrieves the ports information of the device based on the port type. |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1024 | func (agent *DeviceAgent) getPorts(ctx context.Context, portType voltha.Port_PortType) *voltha.Ports { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1025 | log.Debugw("getPorts", log.Fields{"id": agent.deviceID, "portType": portType}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1026 | ports := &voltha.Ports{} |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame^] | 1027 | if device, _ := agent.deviceMgr.GetDevice(ctx, agent.deviceID); device != nil { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1028 | for _, port := range device.Ports { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1029 | if port.Type == portType { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1030 | ports.Items = append(ports.Items, port) |
| 1031 | } |
| 1032 | } |
| 1033 | } |
| 1034 | return ports |
| 1035 | } |
| 1036 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 1037 | // getSwitchCapability is a helper method that a logical device agent uses to retrieve the switch capability of a |
| 1038 | // parent device |
khenaidoo | 7923270 | 2018-12-04 11:00:41 -0500 | [diff] [blame] | 1039 | func (agent *DeviceAgent) getSwitchCapability(ctx context.Context) (*ic.SwitchCapability, error) { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1040 | log.Debugw("getSwitchCapability", log.Fields{"deviceId": agent.deviceID}) |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame^] | 1041 | device, err := agent.deviceMgr.GetDevice(ctx, agent.deviceID) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1042 | if device == nil { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1043 | return nil, err |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1044 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1045 | var switchCap *ic.SwitchCapability |
| 1046 | if switchCap, err = agent.adapterProxy.GetOfpDeviceInfo(ctx, device); err != nil { |
| 1047 | log.Debugw("getSwitchCapability-error", log.Fields{"id": device.Id, "error": err}) |
| 1048 | return nil, err |
| 1049 | } |
| 1050 | return switchCap, nil |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1051 | } |
| 1052 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 1053 | // getPortCapability is a helper method that a logical device agent uses to retrieve the port capability of a |
| 1054 | // device |
khenaidoo | 7923270 | 2018-12-04 11:00:41 -0500 | [diff] [blame] | 1055 | func (agent *DeviceAgent) getPortCapability(ctx context.Context, portNo uint32) (*ic.PortCapability, error) { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1056 | log.Debugw("getPortCapability", log.Fields{"deviceId": agent.deviceID}) |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame^] | 1057 | device, err := agent.deviceMgr.GetDevice(ctx, agent.deviceID) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1058 | if device == nil { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1059 | return nil, err |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1060 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1061 | var portCap *ic.PortCapability |
| 1062 | if portCap, err = agent.adapterProxy.GetOfpPortInfo(ctx, device, portNo); err != nil { |
| 1063 | log.Debugw("getPortCapability-error", log.Fields{"id": device.Id, "error": err}) |
| 1064 | return nil, err |
| 1065 | } |
| 1066 | return portCap, nil |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1067 | } |
| 1068 | |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame^] | 1069 | func (agent *DeviceAgent) packetOut(ctx context.Context, outPort uint32, packet *ofp.OfpPacketOut) error { |
Scott Baker | 8067860 | 2019-11-14 16:57:36 -0800 | [diff] [blame] | 1070 | // If deviceType=="" then we must have taken ownership of this device. |
| 1071 | // Fixes VOL-2226 where a core would take ownership and have stale data |
| 1072 | if agent.deviceType == "" { |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame^] | 1073 | agent.reconcileWithKVStore(ctx) |
Scott Baker | 8067860 | 2019-11-14 16:57:36 -0800 | [diff] [blame] | 1074 | } |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 1075 | // Send packet to adapter |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame^] | 1076 | if err := agent.adapterProxy.packetOut(ctx, agent.deviceType, agent.deviceID, outPort, packet); err != nil { |
Matteo Scandolo | 360605d | 2019-11-05 18:29:17 -0800 | [diff] [blame] | 1077 | log.Debugw("packet-out-error", log.Fields{ |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1078 | "id": agent.deviceID, |
Matteo Scandolo | 360605d | 2019-11-05 18:29:17 -0800 | [diff] [blame] | 1079 | "error": err, |
| 1080 | "packet": hex.EncodeToString(packet.Data), |
| 1081 | }) |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 1082 | return err |
| 1083 | } |
| 1084 | return nil |
| 1085 | } |
| 1086 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 1087 | // processUpdate is a callback invoked whenever there is a change on the device manages by this device agent |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame^] | 1088 | func (agent *DeviceAgent) processUpdate(ctx context.Context, args ...interface{}) interface{} { |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 1089 | //// Run this callback in its own go routine |
| 1090 | go func(args ...interface{}) interface{} { |
| 1091 | var previous *voltha.Device |
| 1092 | var current *voltha.Device |
| 1093 | var ok bool |
| 1094 | if len(args) == 2 { |
| 1095 | if previous, ok = args[0].(*voltha.Device); !ok { |
| 1096 | log.Errorw("invalid-callback-type", log.Fields{"data": args[0]}) |
| 1097 | return nil |
| 1098 | } |
| 1099 | if current, ok = args[1].(*voltha.Device); !ok { |
| 1100 | log.Errorw("invalid-callback-type", log.Fields{"data": args[1]}) |
| 1101 | return nil |
| 1102 | } |
| 1103 | } else { |
| 1104 | log.Errorw("too-many-args-in-callback", log.Fields{"len": len(args)}) |
| 1105 | return nil |
| 1106 | } |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame^] | 1107 | // Perform the state transition in it's own go routine (since the caller doesn't wait for this, use a background context) |
| 1108 | if err := agent.deviceMgr.processTransition(context.Background(), previous, current); err != nil { |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 1109 | log.Errorw("failed-process-transition", log.Fields{"deviceId": previous.Id, |
| 1110 | "previousAdminState": previous.AdminState, "currentAdminState": current.AdminState}) |
| 1111 | } |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 1112 | return nil |
| 1113 | }(args...) |
| 1114 | |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1115 | return nil |
| 1116 | } |
| 1117 | |
Mahir Gunyel | 8e2707d | 2019-07-25 00:36:21 -0700 | [diff] [blame] | 1118 | // updatePartialDeviceData updates a subset of a device that an Adapter can update. |
| 1119 | // TODO: May need a specific proto to handle only a subset of a device that can be changed by an adapter |
| 1120 | func (agent *DeviceAgent) mergeDeviceInfoFromAdapter(device *voltha.Device) (*voltha.Device, error) { |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1121 | cloned := agent.getDeviceWithoutLock() |
Mahir Gunyel | 8e2707d | 2019-07-25 00:36:21 -0700 | [diff] [blame] | 1122 | cloned.Root = device.Root |
| 1123 | cloned.Vendor = device.Vendor |
| 1124 | cloned.Model = device.Model |
| 1125 | cloned.SerialNumber = device.SerialNumber |
| 1126 | cloned.MacAddress = device.MacAddress |
| 1127 | cloned.Vlan = device.Vlan |
| 1128 | cloned.Reason = device.Reason |
| 1129 | return cloned, nil |
| 1130 | } |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame^] | 1131 | func (agent *DeviceAgent) updateDeviceUsingAdapterData(ctx context.Context, device *voltha.Device) error { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1132 | agent.lockDevice.Lock() |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 1133 | defer agent.lockDevice.Unlock() |
Mahir Gunyel | 8e2707d | 2019-07-25 00:36:21 -0700 | [diff] [blame] | 1134 | log.Debugw("updateDeviceUsingAdapterData", log.Fields{"deviceId": device.Id}) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1135 | updatedDevice, err := agent.mergeDeviceInfoFromAdapter(device) |
| 1136 | if err != nil { |
Mahir Gunyel | 8e2707d | 2019-07-25 00:36:21 -0700 | [diff] [blame] | 1137 | log.Errorw("failed to update device ", log.Fields{"deviceId": device.Id}) |
| 1138 | return status.Errorf(codes.Internal, "%s", err.Error()) |
Mahir Gunyel | 8e2707d | 2019-07-25 00:36:21 -0700 | [diff] [blame] | 1139 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1140 | cloned := proto.Clone(updatedDevice).(*voltha.Device) |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame^] | 1141 | return agent.updateDeviceInStoreWithoutLock(ctx, cloned, false, "") |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 1142 | } |
| 1143 | |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame^] | 1144 | func (agent *DeviceAgent) updateDeviceWithoutLock(ctx context.Context, device *voltha.Device) error { |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 1145 | log.Debugw("updateDevice", log.Fields{"deviceId": device.Id}) |
| 1146 | cloned := proto.Clone(device).(*voltha.Device) |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame^] | 1147 | return agent.updateDeviceInStoreWithoutLock(ctx, cloned, false, "") |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1148 | } |
| 1149 | |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame^] | 1150 | func (agent *DeviceAgent) updateDeviceStatus(ctx context.Context, operStatus voltha.OperStatus_Types, connStatus voltha.ConnectStatus_Types) error { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1151 | agent.lockDevice.Lock() |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 1152 | defer agent.lockDevice.Unlock() |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1153 | |
| 1154 | cloned := agent.getDeviceWithoutLock() |
| 1155 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1156 | // 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] | 1157 | if s, ok := voltha.ConnectStatus_Types_value[connStatus.String()]; ok { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1158 | log.Debugw("updateDeviceStatus-conn", log.Fields{"ok": ok, "val": s}) |
| 1159 | cloned.ConnectStatus = connStatus |
| 1160 | } |
serkant.uluderya | 2ae470f | 2020-01-21 11:13:09 -0800 | [diff] [blame] | 1161 | if s, ok := voltha.OperStatus_Types_value[operStatus.String()]; ok { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1162 | log.Debugw("updateDeviceStatus-oper", log.Fields{"ok": ok, "val": s}) |
| 1163 | cloned.OperStatus = operStatus |
| 1164 | } |
| 1165 | log.Debugw("updateDeviceStatus", log.Fields{"deviceId": cloned.Id, "operStatus": cloned.OperStatus, "connectStatus": cloned.ConnectStatus}) |
| 1166 | // Store the device |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame^] | 1167 | return agent.updateDeviceInStoreWithoutLock(ctx, cloned, false, "") |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1168 | } |
| 1169 | |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame^] | 1170 | func (agent *DeviceAgent) enablePorts(ctx context.Context) error { |
khenaidoo | 3ab3488 | 2019-05-02 21:33:30 -0400 | [diff] [blame] | 1171 | agent.lockDevice.Lock() |
| 1172 | defer agent.lockDevice.Unlock() |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1173 | |
| 1174 | cloned := agent.getDeviceWithoutLock() |
| 1175 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1176 | for _, port := range cloned.Ports { |
| 1177 | port.AdminState = voltha.AdminState_ENABLED |
| 1178 | port.OperStatus = voltha.OperStatus_ACTIVE |
| 1179 | } |
| 1180 | // Store the device |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame^] | 1181 | return agent.updateDeviceInStoreWithoutLock(ctx, cloned, false, "") |
khenaidoo | 3ab3488 | 2019-05-02 21:33:30 -0400 | [diff] [blame] | 1182 | } |
| 1183 | |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame^] | 1184 | func (agent *DeviceAgent) disablePorts(ctx context.Context) error { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1185 | log.Debugw("disablePorts", log.Fields{"deviceid": agent.deviceID}) |
khenaidoo | 3ab3488 | 2019-05-02 21:33:30 -0400 | [diff] [blame] | 1186 | agent.lockDevice.Lock() |
| 1187 | defer agent.lockDevice.Unlock() |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1188 | cloned := agent.getDeviceWithoutLock() |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1189 | for _, port := range cloned.Ports { |
| 1190 | port.AdminState = voltha.AdminState_DISABLED |
| 1191 | port.OperStatus = voltha.OperStatus_UNKNOWN |
| 1192 | } |
| 1193 | // Store the device |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame^] | 1194 | return agent.updateDeviceInStoreWithoutLock(ctx, cloned, false, "") |
khenaidoo | 3ab3488 | 2019-05-02 21:33:30 -0400 | [diff] [blame] | 1195 | } |
| 1196 | |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame^] | 1197 | func (agent *DeviceAgent) updatePortState(ctx context.Context, portType voltha.Port_PortType, portNo uint32, operStatus voltha.OperStatus_Types) error { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1198 | agent.lockDevice.Lock() |
khenaidoo | 59ef7be | 2019-06-21 12:40:28 -0400 | [diff] [blame] | 1199 | defer agent.lockDevice.Unlock() |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1200 | // Work only on latest data |
| 1201 | // TODO: Get list of ports from device directly instead of the entire device |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1202 | cloned := agent.getDeviceWithoutLock() |
| 1203 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1204 | // Ensure the enums passed in are valid - they will be invalid if they are not set when this function is invoked |
| 1205 | if _, ok := voltha.Port_PortType_value[portType.String()]; !ok { |
| 1206 | return status.Errorf(codes.InvalidArgument, "%s", portType) |
| 1207 | } |
| 1208 | for _, port := range cloned.Ports { |
| 1209 | if port.Type == portType && port.PortNo == portNo { |
| 1210 | port.OperStatus = operStatus |
| 1211 | // Set the admin status to ENABLED if the operational status is ACTIVE |
| 1212 | // TODO: Set by northbound system? |
| 1213 | if operStatus == voltha.OperStatus_ACTIVE { |
| 1214 | port.AdminState = voltha.AdminState_ENABLED |
| 1215 | } |
| 1216 | break |
| 1217 | } |
| 1218 | } |
| 1219 | log.Debugw("portStatusUpdate", log.Fields{"deviceId": cloned.Id}) |
| 1220 | // Store the device |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame^] | 1221 | return agent.updateDeviceInStoreWithoutLock(ctx, cloned, false, "") |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1222 | } |
| 1223 | |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame^] | 1224 | func (agent *DeviceAgent) deleteAllPorts(ctx context.Context) error { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1225 | log.Debugw("deleteAllPorts", log.Fields{"deviceId": agent.deviceID}) |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 1226 | agent.lockDevice.Lock() |
| 1227 | defer agent.lockDevice.Unlock() |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1228 | |
| 1229 | cloned := agent.getDeviceWithoutLock() |
| 1230 | |
| 1231 | if cloned.AdminState != voltha.AdminState_DISABLED && cloned.AdminState != voltha.AdminState_DELETED { |
| 1232 | err := status.Error(codes.FailedPrecondition, fmt.Sprintf("invalid-state-%v", cloned.AdminState)) |
| 1233 | log.Warnw("invalid-state-removing-ports", log.Fields{"state": cloned.AdminState, "error": err}) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1234 | return err |
| 1235 | } |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1236 | if len(cloned.Ports) == 0 { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1237 | log.Debugw("no-ports-present", log.Fields{"deviceId": agent.deviceID}) |
| 1238 | return nil |
| 1239 | } |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1240 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1241 | cloned.Ports = []*voltha.Port{} |
| 1242 | log.Debugw("portStatusUpdate", log.Fields{"deviceId": cloned.Id}) |
| 1243 | // Store the device |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame^] | 1244 | return agent.updateDeviceInStoreWithoutLock(ctx, cloned, false, "") |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 1245 | } |
| 1246 | |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame^] | 1247 | func (agent *DeviceAgent) addPort(ctx context.Context, port *voltha.Port) error { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1248 | agent.lockDevice.Lock() |
| 1249 | defer agent.lockDevice.Unlock() |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1250 | log.Debugw("addPort", log.Fields{"deviceId": agent.deviceID}) |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1251 | |
| 1252 | cloned := agent.getDeviceWithoutLock() |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1253 | if cloned.Ports == nil { |
| 1254 | // First port |
| 1255 | log.Debugw("addPort-first-port-to-add", log.Fields{"deviceId": agent.deviceID}) |
| 1256 | cloned.Ports = make([]*voltha.Port, 0) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1257 | } else { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1258 | for _, p := range cloned.Ports { |
| 1259 | if p.Type == port.Type && p.PortNo == port.PortNo { |
| 1260 | log.Debugw("port already exists", log.Fields{"port": *port}) |
| 1261 | return nil |
manikkaraj k | 259a6f7 | 2019-05-06 09:55:44 -0400 | [diff] [blame] | 1262 | } |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1263 | } |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1264 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1265 | cp := proto.Clone(port).(*voltha.Port) |
| 1266 | // Set the admin state of the port to ENABLE if the operational state is ACTIVE |
| 1267 | // TODO: Set by northbound system? |
| 1268 | if cp.OperStatus == voltha.OperStatus_ACTIVE { |
| 1269 | cp.AdminState = voltha.AdminState_ENABLED |
| 1270 | } |
| 1271 | cloned.Ports = append(cloned.Ports, cp) |
| 1272 | // Store the device |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame^] | 1273 | return agent.updateDeviceInStoreWithoutLock(ctx, cloned, false, "") |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1274 | } |
| 1275 | |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame^] | 1276 | func (agent *DeviceAgent) addPeerPort(ctx context.Context, port *voltha.Port_PeerPort) error { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1277 | agent.lockDevice.Lock() |
| 1278 | defer agent.lockDevice.Unlock() |
| 1279 | log.Debug("addPeerPort") |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1280 | |
| 1281 | cloned := agent.getDeviceWithoutLock() |
| 1282 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1283 | // Get the peer port on the device based on the port no |
| 1284 | for _, peerPort := range cloned.Ports { |
| 1285 | if peerPort.PortNo == port.PortNo { // found port |
| 1286 | cp := proto.Clone(port).(*voltha.Port_PeerPort) |
| 1287 | peerPort.Peers = append(peerPort.Peers, cp) |
| 1288 | log.Debugw("found-peer", log.Fields{"portNo": port.PortNo, "deviceId": agent.deviceID}) |
| 1289 | break |
| 1290 | } |
| 1291 | } |
| 1292 | // Store the device |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame^] | 1293 | return agent.updateDeviceInStoreWithoutLock(ctx, cloned, false, "") |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1294 | } |
| 1295 | |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame^] | 1296 | func (agent *DeviceAgent) deletePeerPorts(ctx context.Context, deviceID string) error { |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 1297 | agent.lockDevice.Lock() |
| 1298 | defer agent.lockDevice.Unlock() |
| 1299 | log.Debug("deletePeerPorts") |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1300 | |
| 1301 | cloned := agent.getDeviceWithoutLock() |
| 1302 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1303 | var updatedPeers []*voltha.Port_PeerPort |
| 1304 | for _, port := range cloned.Ports { |
| 1305 | updatedPeers = make([]*voltha.Port_PeerPort, 0) |
| 1306 | for _, peerPort := range port.Peers { |
| 1307 | if peerPort.DeviceId != deviceID { |
| 1308 | updatedPeers = append(updatedPeers, peerPort) |
| 1309 | } |
| 1310 | } |
| 1311 | port.Peers = updatedPeers |
| 1312 | } |
| 1313 | |
| 1314 | // Store the device with updated peer ports |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame^] | 1315 | return agent.updateDeviceInStoreWithoutLock(ctx, cloned, false, "") |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 1316 | } |
| 1317 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1318 | // TODO: A generic device update by attribute |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame^] | 1319 | func (agent *DeviceAgent) updateDeviceAttribute(ctx context.Context, name string, value interface{}) { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1320 | agent.lockDevice.Lock() |
| 1321 | defer agent.lockDevice.Unlock() |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1322 | if value == nil { |
| 1323 | return |
| 1324 | } |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1325 | |
| 1326 | cloned := agent.getDeviceWithoutLock() |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1327 | updated := false |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1328 | s := reflect.ValueOf(cloned).Elem() |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1329 | if s.Kind() == reflect.Struct { |
| 1330 | // exported field |
| 1331 | f := s.FieldByName(name) |
| 1332 | if f.IsValid() && f.CanSet() { |
| 1333 | switch f.Kind() { |
| 1334 | case reflect.String: |
| 1335 | f.SetString(value.(string)) |
| 1336 | updated = true |
| 1337 | case reflect.Uint32: |
| 1338 | f.SetUint(uint64(value.(uint32))) |
| 1339 | updated = true |
| 1340 | case reflect.Bool: |
| 1341 | f.SetBool(value.(bool)) |
| 1342 | updated = true |
| 1343 | } |
| 1344 | } |
| 1345 | } |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1346 | log.Debugw("update-field-status", log.Fields{"deviceId": cloned.Id, "name": name, "updated": updated}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1347 | // Save the data |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1348 | |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame^] | 1349 | if err := agent.updateDeviceInStoreWithoutLock(ctx, cloned, false, ""); err != nil { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1350 | log.Warnw("attribute-update-failed", log.Fields{"attribute": name, "value": value}) |
| 1351 | } |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1352 | } |
serkant.uluderya | 334479d | 2019-04-10 08:26:15 -0700 | [diff] [blame] | 1353 | |
| 1354 | func (agent *DeviceAgent) simulateAlarm(ctx context.Context, simulatereq *voltha.SimulateAlarmRequest) error { |
| 1355 | agent.lockDevice.Lock() |
| 1356 | defer agent.lockDevice.Unlock() |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1357 | log.Debugw("simulateAlarm", log.Fields{"id": agent.deviceID}) |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1358 | |
| 1359 | cloned := agent.getDeviceWithoutLock() |
| 1360 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1361 | // First send the request to an Adapter and wait for a response |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1362 | if err := agent.adapterProxy.SimulateAlarm(ctx, cloned, simulatereq); err != nil { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1363 | log.Debugw("simulateAlarm-error", log.Fields{"id": agent.deviceID, "error": err}) |
| 1364 | return err |
serkant.uluderya | 334479d | 2019-04-10 08:26:15 -0700 | [diff] [blame] | 1365 | } |
| 1366 | return nil |
| 1367 | } |
Mahir Gunyel | b585167 | 2019-07-24 10:46:26 +0300 | [diff] [blame] | 1368 | |
| 1369 | //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. |
| 1370 | // It is an internal helper function. |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame^] | 1371 | func (agent *DeviceAgent) updateDeviceInStoreWithoutLock(ctx context.Context, device *voltha.Device, strict bool, txid string) error { |
| 1372 | updateCtx := context.WithValue(ctx, model.RequestTimestamp, time.Now().UnixNano()) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 1373 | afterUpdate, err := agent.clusterDataProxy.Update(updateCtx, "/devices/"+agent.deviceID, device, strict, txid) |
| 1374 | if err != nil { |
| 1375 | return status.Errorf(codes.Internal, "failed-update-device:%s", agent.deviceID) |
| 1376 | } |
| 1377 | if afterUpdate == nil { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1378 | return status.Errorf(codes.Internal, "failed-update-device:%s", agent.deviceID) |
Mahir Gunyel | b585167 | 2019-07-24 10:46:26 +0300 | [diff] [blame] | 1379 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1380 | log.Debugw("updated-device-in-store", log.Fields{"deviceId: ": agent.deviceID}) |
Mahir Gunyel | b585167 | 2019-07-24 10:46:26 +0300 | [diff] [blame] | 1381 | |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1382 | agent.device = proto.Clone(device).(*voltha.Device) |
| 1383 | |
Mahir Gunyel | b585167 | 2019-07-24 10:46:26 +0300 | [diff] [blame] | 1384 | return nil |
| 1385 | } |
Mahir Gunyel | fdee921 | 2019-10-16 16:52:21 -0700 | [diff] [blame] | 1386 | |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame^] | 1387 | func (agent *DeviceAgent) updateDeviceReason(ctx context.Context, reason string) error { |
Mahir Gunyel | fdee921 | 2019-10-16 16:52:21 -0700 | [diff] [blame] | 1388 | agent.lockDevice.Lock() |
| 1389 | defer agent.lockDevice.Unlock() |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1390 | |
| 1391 | cloned := agent.getDeviceWithoutLock() |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1392 | cloned.Reason = reason |
| 1393 | log.Debugw("updateDeviceReason", log.Fields{"deviceId": cloned.Id, "reason": cloned.Reason}) |
| 1394 | // Store the device |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame^] | 1395 | return agent.updateDeviceInStoreWithoutLock(ctx, cloned, false, "") |
Mahir Gunyel | fdee921 | 2019-10-16 16:52:21 -0700 | [diff] [blame] | 1396 | } |