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" |
Scott Baker | 807addd | 2019-10-24 15:16:21 -0700 | [diff] [blame] | 30 | fu "github.com/opencord/voltha-lib-go/v2/pkg/flows" |
| 31 | "github.com/opencord/voltha-lib-go/v2/pkg/log" |
Scott Baker | 555307d | 2019-11-04 08:58:01 -0800 | [diff] [blame] | 32 | ic "github.com/opencord/voltha-protos/v2/go/inter_container" |
| 33 | ofp "github.com/opencord/voltha-protos/v2/go/openflow_13" |
| 34 | "github.com/opencord/voltha-protos/v2/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 | } |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 143 | agent.deviceProxy.RegisterCallback(model.POST_UPDATE, 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 | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 153 | log.Debug("stopping-device-agent") |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 154 | |
| 155 | // First unregister any callbacks |
| 156 | agent.deviceProxy.UnregisterCallback(model.POST_UPDATE, agent.processUpdate) |
| 157 | |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 158 | // Remove the device from the KV store |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 159 | removed, err := agent.clusterDataProxy.Remove(ctx, "/devices/"+agent.deviceID, "") |
| 160 | if err != nil { |
| 161 | log.Errorw("Failed-to-remove-device-from-cluster-data-proxy", log.Fields{"error": err}) |
| 162 | return |
| 163 | } |
| 164 | if removed == nil { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 165 | log.Debugw("device-already-removed", log.Fields{"id": agent.deviceID}) |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 166 | } |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 167 | agent.exitChannel <- 1 |
| 168 | log.Debug("device-agent-stopped") |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 169 | |
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. |
| 173 | func (agent *DeviceAgent) reconcileWithKVStore() { |
| 174 | agent.lockDevice.Lock() |
| 175 | defer agent.lockDevice.Unlock() |
| 176 | log.Debug("reconciling-device-agent-devicetype") |
| 177 | // TODO: context timeout |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 178 | device, err := agent.clusterDataProxy.Get(context.Background(), "/devices/"+agent.deviceID, 1, true, "") |
| 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 | |
| 241 | if err := agent.updateDeviceInStoreWithoutLock(cloned, false, ""); err != nil { |
| 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 | |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 261 | func (agent *DeviceAgent) sendBulkFlowsToAdapters(device *voltha.Device, flows *voltha.Flows, groups *voltha.FlowGroups, flowMetadata *voltha.FlowMetadata, response coreutils.Response) { |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 262 | if err := agent.adapterProxy.UpdateFlowsBulk(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 | |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 269 | func (agent *DeviceAgent) sendIncrementalFlowsToAdapters(device *voltha.Device, flows *ofp.FlowChanges, groups *ofp.FlowGroupChanges, flowMetadata *voltha.FlowMetadata, response coreutils.Response) { |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 270 | if err := agent.adapterProxy.UpdateFlowsIncremental(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 | |
A R Karthick | 5c28f55 | 2019-12-11 22:47:44 -0800 | [diff] [blame] | 336 | func (agent *DeviceAgent) addFlowsAndGroupsToAdapter(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) |
| 370 | if !dType.AcceptsAddRemoveFlowUpdates { |
| 371 | |
khenaidoo | b2121e5 | 2019-12-16 17:17:22 -0500 | [diff] [blame] | 372 | 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] | 373 | 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] | 374 | return coreutils.DoneResponse(), nil |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 375 | } |
khenaidoo | b2121e5 | 2019-12-16 17:17:22 -0500 | [diff] [blame] | 376 | go agent.sendBulkFlowsToAdapters(device, &voltha.Flows{Items: updatedAllFlows}, &voltha.FlowGroups{Items: updatedAllGroups}, flowMetadata, response) |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 377 | |
| 378 | } else { |
| 379 | flowChanges := &ofp.FlowChanges{ |
Matt Jeanneret | 518b5a4 | 2019-10-29 10:30:46 -0400 | [diff] [blame] | 380 | ToAdd: &voltha.Flows{Items: newFlows}, |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 381 | ToRemove: &voltha.Flows{Items: flowsToDelete}, |
| 382 | } |
| 383 | groupChanges := &ofp.FlowGroupChanges{ |
Matt Jeanneret | 518b5a4 | 2019-10-29 10:30:46 -0400 | [diff] [blame] | 384 | ToAdd: &voltha.FlowGroups{Items: newGroups}, |
| 385 | ToRemove: &voltha.FlowGroups{Items: groupsToDelete}, |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 386 | ToUpdate: &voltha.FlowGroups{Items: []*ofp.OfpGroupEntry{}}, |
| 387 | } |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 388 | go agent.sendIncrementalFlowsToAdapters(device, flowChanges, groupChanges, flowMetadata, response) |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | // store the changed data |
khenaidoo | b2121e5 | 2019-12-16 17:17:22 -0500 | [diff] [blame] | 392 | device.Flows = &voltha.Flows{Items: updatedAllFlows} |
| 393 | device.FlowGroups = &voltha.FlowGroups{Items: updatedAllGroups} |
Kent Hagerman | 3c51397 | 2019-11-25 13:49:41 -0500 | [diff] [blame] | 394 | if err := agent.updateDeviceWithoutLock(device); err != nil { |
A R Karthick | 5c28f55 | 2019-12-11 22:47:44 -0800 | [diff] [blame] | 395 | return coreutils.DoneResponse(), status.Errorf(codes.Internal, "failure-updating-%s", agent.deviceID) |
Kent Hagerman | 3c51397 | 2019-11-25 13:49:41 -0500 | [diff] [blame] | 396 | } |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 397 | |
A R Karthick | 5c28f55 | 2019-12-11 22:47:44 -0800 | [diff] [blame] | 398 | return response, nil |
| 399 | } |
| 400 | |
| 401 | //addFlowsAndGroups adds the "newFlows" and "newGroups" from the existing flows/groups and sends the update to the |
| 402 | //adapters |
| 403 | func (agent *DeviceAgent) addFlowsAndGroups(newFlows []*ofp.OfpFlowStats, newGroups []*ofp.OfpGroupEntry, flowMetadata *voltha.FlowMetadata) error { |
| 404 | response, err := agent.addFlowsAndGroupsToAdapter(newFlows, newGroups, flowMetadata) |
| 405 | if err != nil { |
| 406 | return err |
| 407 | } |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 408 | if res := coreutils.WaitForNilOrErrorResponses(agent.defaultTimeout, response); res != nil { |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 409 | 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] | 410 | return status.Errorf(codes.Aborted, "errors-%s", res) |
| 411 | } |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 412 | return nil |
| 413 | } |
| 414 | |
A R Karthick | 5c28f55 | 2019-12-11 22:47:44 -0800 | [diff] [blame] | 415 | func (agent *DeviceAgent) deleteFlowsAndGroupsFromAdapter(flowsToDel []*ofp.OfpFlowStats, groupsToDel []*ofp.OfpGroupEntry, flowMetadata *voltha.FlowMetadata) (coreutils.Response, error) { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 416 | log.Debugw("deleteFlowsAndGroups", log.Fields{"deviceId": agent.deviceID, "flows": flowsToDel, "groups": groupsToDel}) |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 417 | |
| 418 | if (len(flowsToDel) | len(groupsToDel)) == 0 { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 419 | 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] | 420 | return coreutils.DoneResponse(), nil |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | agent.lockDevice.Lock() |
| 424 | defer agent.lockDevice.Unlock() |
| 425 | |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 426 | device := agent.getDeviceWithoutLock() |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 427 | |
| 428 | existingFlows := proto.Clone(device.Flows).(*voltha.Flows) |
| 429 | existingGroups := proto.Clone(device.FlowGroups).(*ofp.FlowGroups) |
| 430 | |
| 431 | var flowsToKeep []*ofp.OfpFlowStats |
| 432 | var groupsToKeep []*ofp.OfpGroupEntry |
| 433 | |
| 434 | // Process flows |
| 435 | for _, flow := range existingFlows.Items { |
| 436 | if idx := fu.FindFlows(flowsToDel, flow); idx == -1 { |
| 437 | flowsToKeep = append(flowsToKeep, flow) |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | // Process groups |
| 442 | for _, group := range existingGroups.Items { |
| 443 | if fu.FindGroup(groupsToDel, group.Desc.GroupId) == -1 { // does not exist now |
| 444 | groupsToKeep = append(groupsToKeep, group) |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | log.Debugw("deleteFlowsAndGroups", |
| 449 | log.Fields{ |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 450 | "deviceId": agent.deviceID, |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 451 | "flowsToDel": len(flowsToDel), |
| 452 | "flowsToKeep": len(flowsToKeep), |
| 453 | "groupsToDel": len(groupsToDel), |
| 454 | "groupsToKeep": len(groupsToKeep), |
| 455 | }) |
| 456 | |
| 457 | // Sanity check |
| 458 | if (len(flowsToKeep) | len(flowsToDel) | len(groupsToKeep) | len(groupsToDel)) == 0 { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 459 | 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] | 460 | return coreutils.DoneResponse(), nil |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | // Send update to adapters |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 464 | response := coreutils.NewResponse() |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 465 | dType := agent.adapterMgr.getDeviceType(device.Type) |
| 466 | if !dType.AcceptsAddRemoveFlowUpdates { |
| 467 | 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] | 468 | 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] | 469 | return coreutils.DoneResponse(), nil |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 470 | } |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 471 | go agent.sendBulkFlowsToAdapters(device, &voltha.Flows{Items: flowsToKeep}, &voltha.FlowGroups{Items: groupsToKeep}, flowMetadata, response) |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 472 | } else { |
| 473 | flowChanges := &ofp.FlowChanges{ |
| 474 | ToAdd: &voltha.Flows{Items: []*ofp.OfpFlowStats{}}, |
| 475 | ToRemove: &voltha.Flows{Items: flowsToDel}, |
| 476 | } |
| 477 | groupChanges := &ofp.FlowGroupChanges{ |
| 478 | ToAdd: &voltha.FlowGroups{Items: []*ofp.OfpGroupEntry{}}, |
| 479 | ToRemove: &voltha.FlowGroups{Items: groupsToDel}, |
| 480 | ToUpdate: &voltha.FlowGroups{Items: []*ofp.OfpGroupEntry{}}, |
| 481 | } |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 482 | go agent.sendIncrementalFlowsToAdapters(device, flowChanges, groupChanges, flowMetadata, response) |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | // store the changed data |
| 486 | device.Flows = &voltha.Flows{Items: flowsToKeep} |
| 487 | device.FlowGroups = &voltha.FlowGroups{Items: groupsToKeep} |
Kent Hagerman | 3c51397 | 2019-11-25 13:49:41 -0500 | [diff] [blame] | 488 | if err := agent.updateDeviceWithoutLock(device); err != nil { |
A R Karthick | 5c28f55 | 2019-12-11 22:47:44 -0800 | [diff] [blame] | 489 | return coreutils.DoneResponse(), status.Errorf(codes.Internal, "failure-updating-%s", agent.deviceID) |
Kent Hagerman | 3c51397 | 2019-11-25 13:49:41 -0500 | [diff] [blame] | 490 | } |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 491 | |
A R Karthick | 5c28f55 | 2019-12-11 22:47:44 -0800 | [diff] [blame] | 492 | return response, nil |
| 493 | |
| 494 | } |
| 495 | |
| 496 | //deleteFlowsAndGroups removes the "flowsToDel" and "groupsToDel" from the existing flows/groups and sends the update to the |
| 497 | //adapters |
| 498 | func (agent *DeviceAgent) deleteFlowsAndGroups(flowsToDel []*ofp.OfpFlowStats, groupsToDel []*ofp.OfpGroupEntry, flowMetadata *voltha.FlowMetadata) error { |
| 499 | response, err := agent.deleteFlowsAndGroupsFromAdapter(flowsToDel, groupsToDel, flowMetadata) |
| 500 | if err != nil { |
| 501 | return err |
| 502 | } |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 503 | if res := coreutils.WaitForNilOrErrorResponses(agent.defaultTimeout, response); res != nil { |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 504 | return status.Errorf(codes.Aborted, "errors-%s", res) |
| 505 | } |
| 506 | return nil |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 507 | } |
| 508 | |
A R Karthick | 5c28f55 | 2019-12-11 22:47:44 -0800 | [diff] [blame] | 509 | func (agent *DeviceAgent) updateFlowsAndGroupsToAdapter(updatedFlows []*ofp.OfpFlowStats, updatedGroups []*ofp.OfpGroupEntry, flowMetadata *voltha.FlowMetadata) (coreutils.Response, error) { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 510 | log.Debugw("updateFlowsAndGroups", log.Fields{"deviceId": agent.deviceID, "flows": updatedFlows, "groups": updatedGroups}) |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 511 | |
| 512 | if (len(updatedFlows) | len(updatedGroups)) == 0 { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 513 | 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] | 514 | return coreutils.DoneResponse(), nil |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | agent.lockDevice.Lock() |
| 518 | defer agent.lockDevice.Unlock() |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 519 | |
| 520 | device := agent.getDeviceWithoutLock() |
| 521 | |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 522 | existingFlows := proto.Clone(device.Flows).(*voltha.Flows) |
| 523 | existingGroups := proto.Clone(device.FlowGroups).(*ofp.FlowGroups) |
| 524 | |
| 525 | 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] | 526 | 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] | 527 | return coreutils.DoneResponse(), nil |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | log.Debugw("updating-flows-and-groups", |
| 531 | log.Fields{ |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 532 | "deviceId": agent.deviceID, |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 533 | "updatedFlows": updatedFlows, |
| 534 | "updatedGroups": updatedGroups, |
| 535 | }) |
| 536 | |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 537 | response := coreutils.NewResponse() |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 538 | dType := agent.adapterMgr.getDeviceType(device.Type) |
| 539 | |
| 540 | // Process bulk flow update differently than incremental update |
| 541 | if !dType.AcceptsAddRemoveFlowUpdates { |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 542 | go agent.sendBulkFlowsToAdapters(device, &voltha.Flows{Items: updatedFlows}, &voltha.FlowGroups{Items: updatedGroups}, nil, response) |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 543 | } else { |
| 544 | var flowsToAdd []*ofp.OfpFlowStats |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 545 | var flowsToDelete []*ofp.OfpFlowStats |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 546 | var groupsToAdd []*ofp.OfpGroupEntry |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 547 | var groupsToDelete []*ofp.OfpGroupEntry |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 548 | |
| 549 | // Process flows |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 550 | for _, flow := range updatedFlows { |
| 551 | if idx := fu.FindFlows(existingFlows.Items, flow); idx == -1 { |
| 552 | flowsToAdd = append(flowsToAdd, flow) |
| 553 | } |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 554 | } |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 555 | for _, flow := range existingFlows.Items { |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 556 | if idx := fu.FindFlows(updatedFlows, flow); idx != -1 { |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 557 | flowsToDelete = append(flowsToDelete, flow) |
| 558 | } |
| 559 | } |
| 560 | |
| 561 | // Process groups |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 562 | for _, g := range updatedGroups { |
| 563 | if fu.FindGroup(existingGroups.Items, g.Desc.GroupId) == -1 { // does not exist now |
| 564 | groupsToAdd = append(groupsToAdd, g) |
| 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 _, group := range existingGroups.Items { |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 568 | if fu.FindGroup(updatedGroups, group.Desc.GroupId) != -1 { // does not exist now |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 569 | groupsToDelete = append(groupsToDelete, group) |
| 570 | } |
| 571 | } |
| 572 | |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 573 | log.Debugw("updating-flows-and-groups", |
| 574 | log.Fields{ |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 575 | "deviceId": agent.deviceID, |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 576 | "flowsToAdd": flowsToAdd, |
| 577 | "flowsToDelete": flowsToDelete, |
| 578 | "groupsToAdd": groupsToAdd, |
| 579 | "groupsToDelete": groupsToDelete, |
| 580 | }) |
| 581 | |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 582 | // Sanity check |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 583 | if (len(flowsToAdd) | len(flowsToDelete) | len(groupsToAdd) | len(groupsToDelete) | len(updatedGroups)) == 0 { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 584 | 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] | 585 | return coreutils.DoneResponse(), nil |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 586 | } |
| 587 | |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 588 | flowChanges := &ofp.FlowChanges{ |
| 589 | ToAdd: &voltha.Flows{Items: flowsToAdd}, |
| 590 | ToRemove: &voltha.Flows{Items: flowsToDelete}, |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 591 | } |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 592 | groupChanges := &ofp.FlowGroupChanges{ |
| 593 | ToAdd: &voltha.FlowGroups{Items: groupsToAdd}, |
| 594 | ToRemove: &voltha.FlowGroups{Items: groupsToDelete}, |
| 595 | ToUpdate: &voltha.FlowGroups{Items: updatedGroups}, |
| 596 | } |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 597 | go agent.sendIncrementalFlowsToAdapters(device, flowChanges, groupChanges, flowMetadata, response) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 598 | } |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 599 | |
| 600 | // store the updated data |
| 601 | device.Flows = &voltha.Flows{Items: updatedFlows} |
| 602 | device.FlowGroups = &voltha.FlowGroups{Items: updatedGroups} |
Kent Hagerman | 3c51397 | 2019-11-25 13:49:41 -0500 | [diff] [blame] | 603 | if err := agent.updateDeviceWithoutLock(device); err != nil { |
A R Karthick | 5c28f55 | 2019-12-11 22:47:44 -0800 | [diff] [blame] | 604 | return coreutils.DoneResponse(), status.Errorf(codes.Internal, "failure-updating-%s", agent.deviceID) |
Kent Hagerman | 3c51397 | 2019-11-25 13:49:41 -0500 | [diff] [blame] | 605 | } |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 606 | |
A R Karthick | 5c28f55 | 2019-12-11 22:47:44 -0800 | [diff] [blame] | 607 | return response, nil |
| 608 | } |
| 609 | |
| 610 | //updateFlowsAndGroups replaces the existing flows and groups with "updatedFlows" and "updatedGroups" respectively. It |
| 611 | //also sends the updates to the adapters |
| 612 | func (agent *DeviceAgent) updateFlowsAndGroups(updatedFlows []*ofp.OfpFlowStats, updatedGroups []*ofp.OfpGroupEntry, flowMetadata *voltha.FlowMetadata) error { |
| 613 | response, err := agent.updateFlowsAndGroupsToAdapter(updatedFlows, updatedGroups, flowMetadata) |
| 614 | if err != nil { |
| 615 | return err |
| 616 | } |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 617 | if res := coreutils.WaitForNilOrErrorResponses(agent.defaultTimeout, response); res != nil { |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 618 | return status.Errorf(codes.Aborted, "errors-%s", res) |
| 619 | } |
| 620 | return nil |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 621 | } |
| 622 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 623 | //disableDevice disable a device |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 624 | func (agent *DeviceAgent) disableDevice(ctx context.Context) error { |
khenaidoo | 59ef7be | 2019-06-21 12:40:28 -0400 | [diff] [blame] | 625 | agent.lockDevice.Lock() |
| 626 | defer agent.lockDevice.Unlock() |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 627 | log.Debugw("disableDevice", log.Fields{"id": agent.deviceID}) |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 628 | |
| 629 | cloned := agent.getDeviceWithoutLock() |
| 630 | |
| 631 | if cloned.AdminState == voltha.AdminState_DISABLED { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 632 | log.Debugw("device-already-disabled", log.Fields{"id": agent.deviceID}) |
| 633 | return nil |
| 634 | } |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 635 | if cloned.AdminState == voltha.AdminState_PREPROVISIONED || |
| 636 | cloned.AdminState == voltha.AdminState_DELETED { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 637 | log.Debugw("device-not-enabled", log.Fields{"id": agent.deviceID}) |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 638 | 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] | 639 | } |
khenaidoo | 4554f7c | 2019-05-29 22:13:15 -0400 | [diff] [blame] | 640 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 641 | // Update the Admin State and operational state before sending the request out |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 642 | cloned.AdminState = voltha.AdminState_DISABLED |
| 643 | cloned.OperStatus = voltha.OperStatus_UNKNOWN |
| 644 | if err := agent.updateDeviceInStoreWithoutLock(cloned, false, ""); err != nil { |
| 645 | return err |
| 646 | } |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 647 | if err := agent.adapterProxy.DisableDevice(ctx, proto.Clone(cloned).(*voltha.Device)); err != nil { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 648 | log.Debugw("disableDevice-error", log.Fields{"id": agent.deviceID, "error": err}) |
| 649 | return err |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 650 | } |
| 651 | return nil |
| 652 | } |
| 653 | |
| 654 | func (agent *DeviceAgent) updateAdminState(adminState voltha.AdminState_AdminState) error { |
| 655 | agent.lockDevice.Lock() |
| 656 | defer agent.lockDevice.Unlock() |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 657 | log.Debugw("updateAdminState", log.Fields{"id": agent.deviceID}) |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 658 | |
| 659 | cloned := agent.getDeviceWithoutLock() |
| 660 | |
| 661 | if cloned.AdminState == adminState { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 662 | log.Debugw("no-change-needed", log.Fields{"id": agent.deviceID, "state": adminState}) |
| 663 | return nil |
| 664 | } |
| 665 | // 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] | 666 | cloned.AdminState = adminState |
| 667 | if err := agent.updateDeviceInStoreWithoutLock(cloned, false, ""); err != nil { |
| 668 | return err |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 669 | } |
| 670 | return nil |
| 671 | } |
| 672 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 673 | func (agent *DeviceAgent) rebootDevice(ctx context.Context) error { |
| 674 | agent.lockDevice.Lock() |
| 675 | defer agent.lockDevice.Unlock() |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 676 | log.Debugw("rebootDevice", log.Fields{"id": agent.deviceID}) |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 677 | |
| 678 | device := agent.getDeviceWithoutLock() |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 679 | if err := agent.adapterProxy.RebootDevice(ctx, device); err != nil { |
| 680 | log.Debugw("rebootDevice-error", log.Fields{"id": agent.deviceID, "error": err}) |
| 681 | return err |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 682 | } |
| 683 | return nil |
| 684 | } |
| 685 | |
| 686 | func (agent *DeviceAgent) deleteDevice(ctx context.Context) error { |
| 687 | agent.lockDevice.Lock() |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 688 | defer agent.lockDevice.Unlock() |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 689 | log.Debugw("deleteDevice", log.Fields{"id": agent.deviceID}) |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 690 | |
| 691 | cloned := agent.getDeviceWithoutLock() |
| 692 | if cloned.AdminState == voltha.AdminState_DELETED { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 693 | log.Debugw("device-already-in-deleted-state", log.Fields{"id": agent.deviceID}) |
| 694 | return nil |
| 695 | } |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 696 | if (cloned.AdminState != voltha.AdminState_DISABLED) && |
| 697 | (cloned.AdminState != voltha.AdminState_PREPROVISIONED) { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 698 | log.Debugw("device-not-disabled", log.Fields{"id": agent.deviceID}) |
| 699 | //TODO: Needs customized error message |
| 700 | return status.Errorf(codes.FailedPrecondition, "deviceId:%s, expected-admin-state:%s", agent.deviceID, voltha.AdminState_DISABLED) |
| 701 | } |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 702 | if cloned.AdminState != voltha.AdminState_PREPROVISIONED { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 703 | // 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] | 704 | if err := agent.adapterProxy.DeleteDevice(ctx, cloned); err != nil { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 705 | log.Debugw("deleteDevice-error", log.Fields{"id": agent.deviceID, "error": err}) |
Mahir Gunyel | b585167 | 2019-07-24 10:46:26 +0300 | [diff] [blame] | 706 | return err |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 707 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 708 | } |
| 709 | // Set the state to deleted after we receive an Ack - this will trigger some background process to clean up |
| 710 | // the device as well as its association with the logical device |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 711 | cloned.AdminState = voltha.AdminState_DELETED |
| 712 | if err := agent.updateDeviceInStoreWithoutLock(cloned, false, ""); err != nil { |
| 713 | return err |
| 714 | } |
| 715 | // 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] | 716 | if !cloned.Root { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 717 | go func() { |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 718 | err := agent.deviceMgr.deletePeerPorts(cloned.ParentId, cloned.Id) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 719 | if err != nil { |
| 720 | log.Errorw("unable-to-delete-peer-ports", log.Fields{"error": err}) |
| 721 | } |
| 722 | }() |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 723 | } |
| 724 | return nil |
| 725 | } |
| 726 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 727 | func (agent *DeviceAgent) setParentID(device *voltha.Device, parentID string) error { |
khenaidoo | ad06fd7 | 2019-10-28 12:26:05 -0400 | [diff] [blame] | 728 | agent.lockDevice.Lock() |
| 729 | defer agent.lockDevice.Unlock() |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 730 | log.Debugw("setParentId", log.Fields{"deviceId": device.Id, "parentId": parentID}) |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 731 | |
| 732 | cloned := agent.getDeviceWithoutLock() |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 733 | cloned.ParentId = parentID |
| 734 | // Store the device |
| 735 | if err := agent.updateDeviceInStoreWithoutLock(cloned, false, ""); err != nil { |
| 736 | return err |
| 737 | } |
| 738 | return nil |
khenaidoo | ad06fd7 | 2019-10-28 12:26:05 -0400 | [diff] [blame] | 739 | } |
| 740 | |
khenaidoo | b312747 | 2019-07-24 21:04:55 -0400 | [diff] [blame] | 741 | func (agent *DeviceAgent) updatePmConfigs(ctx context.Context, pmConfigs *voltha.PmConfigs) error { |
| 742 | agent.lockDevice.Lock() |
| 743 | defer agent.lockDevice.Unlock() |
| 744 | log.Debugw("updatePmConfigs", log.Fields{"id": pmConfigs.Id}) |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 745 | |
| 746 | cloned := agent.getDeviceWithoutLock() |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 747 | cloned.PmConfigs = proto.Clone(pmConfigs).(*voltha.PmConfigs) |
| 748 | // Store the device |
| 749 | if err := agent.updateDeviceInStoreWithoutLock(cloned, false, ""); err != nil { |
| 750 | return err |
| 751 | } |
| 752 | // Send the request to the adapter |
| 753 | if err := agent.adapterProxy.UpdatePmConfigs(ctx, cloned, pmConfigs); err != nil { |
| 754 | log.Errorw("update-pm-configs-error", log.Fields{"id": agent.deviceID, "error": err}) |
| 755 | return err |
| 756 | } |
| 757 | return nil |
khenaidoo | b312747 | 2019-07-24 21:04:55 -0400 | [diff] [blame] | 758 | } |
| 759 | |
| 760 | func (agent *DeviceAgent) initPmConfigs(pmConfigs *voltha.PmConfigs) error { |
| 761 | agent.lockDevice.Lock() |
| 762 | defer agent.lockDevice.Unlock() |
| 763 | log.Debugw("initPmConfigs", log.Fields{"id": pmConfigs.Id}) |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 764 | |
| 765 | cloned := agent.getDeviceWithoutLock() |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 766 | cloned.PmConfigs = proto.Clone(pmConfigs).(*voltha.PmConfigs) |
| 767 | // Store the device |
| 768 | updateCtx := context.WithValue(context.Background(), model.RequestTimestamp, time.Now().UnixNano()) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 769 | afterUpdate, err := agent.clusterDataProxy.Update(updateCtx, "/devices/"+agent.deviceID, cloned, false, "") |
| 770 | if err != nil { |
| 771 | return status.Errorf(codes.Internal, "%s", agent.deviceID) |
| 772 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 773 | if afterUpdate == nil { |
| 774 | return status.Errorf(codes.Internal, "%s", agent.deviceID) |
| 775 | } |
| 776 | return nil |
khenaidoo | b312747 | 2019-07-24 21:04:55 -0400 | [diff] [blame] | 777 | } |
| 778 | |
| 779 | func (agent *DeviceAgent) listPmConfigs(ctx context.Context) (*voltha.PmConfigs, error) { |
| 780 | agent.lockDevice.RLock() |
| 781 | defer agent.lockDevice.RUnlock() |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 782 | log.Debugw("listPmConfigs", log.Fields{"id": agent.deviceID}) |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 783 | |
| 784 | return agent.getDeviceWithoutLock().PmConfigs, nil |
khenaidoo | b312747 | 2019-07-24 21:04:55 -0400 | [diff] [blame] | 785 | } |
| 786 | |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 787 | func (agent *DeviceAgent) downloadImage(ctx context.Context, img *voltha.ImageDownload) (*voltha.OperationResp, error) { |
| 788 | agent.lockDevice.Lock() |
| 789 | defer agent.lockDevice.Unlock() |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 790 | log.Debugw("downloadImage", log.Fields{"id": agent.deviceID}) |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 791 | |
| 792 | device := agent.getDeviceWithoutLock() |
| 793 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 794 | if device.AdminState != voltha.AdminState_ENABLED { |
| 795 | log.Debugw("device-not-enabled", log.Fields{"id": agent.deviceID}) |
| 796 | return nil, status.Errorf(codes.FailedPrecondition, "deviceId:%s, expected-admin-state:%s", agent.deviceID, voltha.AdminState_ENABLED) |
| 797 | } |
| 798 | // Save the image |
| 799 | clonedImg := proto.Clone(img).(*voltha.ImageDownload) |
| 800 | clonedImg.DownloadState = voltha.ImageDownload_DOWNLOAD_REQUESTED |
| 801 | cloned := proto.Clone(device).(*voltha.Device) |
| 802 | if cloned.ImageDownloads == nil { |
| 803 | cloned.ImageDownloads = []*voltha.ImageDownload{clonedImg} |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 804 | } else { |
| 805 | if device.AdminState != voltha.AdminState_ENABLED { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 806 | log.Debugw("device-not-enabled", log.Fields{"id": agent.deviceID}) |
| 807 | 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] | 808 | } |
| 809 | // Save the image |
| 810 | clonedImg := proto.Clone(img).(*voltha.ImageDownload) |
Stephane Barbarie | df5479f | 2019-01-29 22:13:00 -0500 | [diff] [blame] | 811 | clonedImg.DownloadState = voltha.ImageDownload_DOWNLOAD_REQUESTED |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 812 | cloned := proto.Clone(device).(*voltha.Device) |
| 813 | if cloned.ImageDownloads == nil { |
| 814 | cloned.ImageDownloads = []*voltha.ImageDownload{clonedImg} |
| 815 | } else { |
| 816 | cloned.ImageDownloads = append(cloned.ImageDownloads, clonedImg) |
| 817 | } |
| 818 | cloned.AdminState = voltha.AdminState_DOWNLOADING_IMAGE |
Mahir Gunyel | b585167 | 2019-07-24 10:46:26 +0300 | [diff] [blame] | 819 | if err := agent.updateDeviceInStoreWithoutLock(cloned, false, ""); err != nil { |
| 820 | return nil, err |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 821 | } |
| 822 | // Send the request to the adapter |
| 823 | if err := agent.adapterProxy.DownloadImage(ctx, cloned, clonedImg); err != nil { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 824 | 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] | 825 | return nil, err |
| 826 | } |
| 827 | } |
| 828 | return &voltha.OperationResp{Code: voltha.OperationResp_OPERATION_SUCCESS}, nil |
| 829 | } |
| 830 | |
| 831 | // isImageRegistered is a helper method to figure out if an image is already registered |
| 832 | func isImageRegistered(img *voltha.ImageDownload, device *voltha.Device) bool { |
| 833 | for _, image := range device.ImageDownloads { |
| 834 | if image.Id == img.Id && image.Name == img.Name { |
| 835 | return true |
| 836 | } |
| 837 | } |
| 838 | return false |
| 839 | } |
| 840 | |
| 841 | func (agent *DeviceAgent) cancelImageDownload(ctx context.Context, img *voltha.ImageDownload) (*voltha.OperationResp, error) { |
| 842 | agent.lockDevice.Lock() |
| 843 | defer agent.lockDevice.Unlock() |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 844 | log.Debugw("cancelImageDownload", log.Fields{"id": agent.deviceID}) |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 845 | |
| 846 | device := agent.getDeviceWithoutLock() |
| 847 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 848 | // Verify whether the Image is in the list of image being downloaded |
| 849 | if !isImageRegistered(img, device) { |
| 850 | return nil, status.Errorf(codes.FailedPrecondition, "deviceId:%s, image-not-registered:%s", agent.deviceID, img.Name) |
| 851 | } |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 852 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 853 | // Update image download state |
| 854 | cloned := proto.Clone(device).(*voltha.Device) |
| 855 | for _, image := range cloned.ImageDownloads { |
| 856 | if image.Id == img.Id && image.Name == img.Name { |
| 857 | image.DownloadState = voltha.ImageDownload_DOWNLOAD_CANCELLED |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 858 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 859 | } |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 860 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 861 | if device.AdminState == voltha.AdminState_DOWNLOADING_IMAGE { |
| 862 | // Set the device to Enabled |
| 863 | cloned.AdminState = voltha.AdminState_ENABLED |
| 864 | if err := agent.updateDeviceInStoreWithoutLock(cloned, false, ""); err != nil { |
| 865 | return nil, err |
| 866 | } |
| 867 | // Send the request to the adapter |
| 868 | if err := agent.adapterProxy.CancelImageDownload(ctx, device, img); err != nil { |
| 869 | log.Debugw("cancelImageDownload-error", log.Fields{"id": agent.deviceID, "error": err, "image": img.Name}) |
| 870 | return nil, err |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 871 | } |
| 872 | } |
| 873 | return &voltha.OperationResp{Code: voltha.OperationResp_OPERATION_SUCCESS}, nil |
serkant.uluderya | 334479d | 2019-04-10 08:26:15 -0700 | [diff] [blame] | 874 | } |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 875 | |
| 876 | func (agent *DeviceAgent) activateImage(ctx context.Context, img *voltha.ImageDownload) (*voltha.OperationResp, error) { |
| 877 | agent.lockDevice.Lock() |
| 878 | defer agent.lockDevice.Unlock() |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 879 | log.Debugw("activateImage", log.Fields{"id": agent.deviceID}) |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 880 | cloned := agent.getDeviceWithoutLock() |
| 881 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 882 | // Verify whether the Image is in the list of image being downloaded |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 883 | if !isImageRegistered(img, cloned) { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 884 | return nil, status.Errorf(codes.FailedPrecondition, "deviceId:%s, image-not-registered:%s", agent.deviceID, img.Name) |
| 885 | } |
| 886 | |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 887 | if cloned.AdminState == voltha.AdminState_DOWNLOADING_IMAGE { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 888 | return nil, status.Errorf(codes.FailedPrecondition, "deviceId:%s, device-in-downloading-state:%s", agent.deviceID, img.Name) |
| 889 | } |
| 890 | // Update image download state |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 891 | for _, image := range cloned.ImageDownloads { |
| 892 | if image.Id == img.Id && image.Name == img.Name { |
| 893 | image.ImageState = voltha.ImageDownload_IMAGE_ACTIVATING |
| 894 | } |
| 895 | } |
| 896 | // Set the device to downloading_image |
| 897 | cloned.AdminState = voltha.AdminState_DOWNLOADING_IMAGE |
| 898 | if err := agent.updateDeviceInStoreWithoutLock(cloned, false, ""); err != nil { |
| 899 | return nil, err |
| 900 | } |
| 901 | |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 902 | 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] | 903 | log.Debugw("activateImage-error", log.Fields{"id": agent.deviceID, "error": err, "image": img.Name}) |
| 904 | return nil, err |
| 905 | } |
| 906 | // The status of the AdminState will be changed following the update_download_status response from the adapter |
| 907 | // The image name will also be removed from the device list |
serkant.uluderya | 334479d | 2019-04-10 08:26:15 -0700 | [diff] [blame] | 908 | return &voltha.OperationResp{Code: voltha.OperationResp_OPERATION_SUCCESS}, nil |
| 909 | } |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 910 | |
| 911 | func (agent *DeviceAgent) revertImage(ctx context.Context, img *voltha.ImageDownload) (*voltha.OperationResp, error) { |
| 912 | agent.lockDevice.Lock() |
| 913 | defer agent.lockDevice.Unlock() |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 914 | log.Debugw("revertImage", log.Fields{"id": agent.deviceID}) |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 915 | |
| 916 | cloned := agent.getDeviceWithoutLock() |
| 917 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 918 | // Verify whether the Image is in the list of image being downloaded |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 919 | if !isImageRegistered(img, cloned) { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 920 | return nil, status.Errorf(codes.FailedPrecondition, "deviceId:%s, image-not-registered:%s", agent.deviceID, img.Name) |
| 921 | } |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 922 | |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 923 | if cloned.AdminState != voltha.AdminState_ENABLED { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 924 | return nil, status.Errorf(codes.FailedPrecondition, "deviceId:%s, device-not-enabled-state:%s", agent.deviceID, img.Name) |
| 925 | } |
| 926 | // Update image download state |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 927 | for _, image := range cloned.ImageDownloads { |
| 928 | if image.Id == img.Id && image.Name == img.Name { |
| 929 | image.ImageState = voltha.ImageDownload_IMAGE_REVERTING |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 930 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 931 | } |
Mahir Gunyel | b585167 | 2019-07-24 10:46:26 +0300 | [diff] [blame] | 932 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 933 | if err := agent.updateDeviceInStoreWithoutLock(cloned, false, ""); err != nil { |
| 934 | return nil, err |
| 935 | } |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 936 | |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 937 | 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] | 938 | log.Debugw("revertImage-error", log.Fields{"id": agent.deviceID, "error": err, "image": img.Name}) |
| 939 | return nil, err |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 940 | } |
| 941 | return &voltha.OperationResp{Code: voltha.OperationResp_OPERATION_SUCCESS}, nil |
serkant.uluderya | 334479d | 2019-04-10 08:26:15 -0700 | [diff] [blame] | 942 | } |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 943 | |
| 944 | func (agent *DeviceAgent) getImageDownloadStatus(ctx context.Context, img *voltha.ImageDownload) (*voltha.ImageDownload, error) { |
| 945 | agent.lockDevice.Lock() |
| 946 | defer agent.lockDevice.Unlock() |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 947 | log.Debugw("getImageDownloadStatus", log.Fields{"id": agent.deviceID}) |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 948 | |
| 949 | cloned := agent.getDeviceWithoutLock() |
| 950 | resp, err := agent.adapterProxy.GetImageDownloadStatus(ctx, cloned, img) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 951 | if err != nil { |
| 952 | log.Debugw("getImageDownloadStatus-error", log.Fields{"id": agent.deviceID, "error": err, "image": img.Name}) |
| 953 | return nil, err |
| 954 | } |
| 955 | return resp, nil |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 956 | } |
| 957 | |
serkant.uluderya | 334479d | 2019-04-10 08:26:15 -0700 | [diff] [blame] | 958 | func (agent *DeviceAgent) updateImageDownload(img *voltha.ImageDownload) error { |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 959 | agent.lockDevice.Lock() |
| 960 | defer agent.lockDevice.Unlock() |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 961 | log.Debugw("updateImageDownload", log.Fields{"id": agent.deviceID}) |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 962 | |
| 963 | cloned := agent.getDeviceWithoutLock() |
| 964 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 965 | // Update the image as well as remove it if the download was cancelled |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 966 | clonedImages := make([]*voltha.ImageDownload, len(cloned.ImageDownloads)) |
| 967 | for _, image := range cloned.ImageDownloads { |
| 968 | if image.Id == img.Id && image.Name == img.Name { |
| 969 | if image.DownloadState != voltha.ImageDownload_DOWNLOAD_CANCELLED { |
| 970 | clonedImages = append(clonedImages, img) |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 971 | } |
| 972 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 973 | } |
| 974 | cloned.ImageDownloads = clonedImages |
| 975 | // Set the Admin state to enabled if required |
| 976 | if (img.DownloadState != voltha.ImageDownload_DOWNLOAD_REQUESTED && |
| 977 | img.DownloadState != voltha.ImageDownload_DOWNLOAD_STARTED) || |
| 978 | (img.ImageState != voltha.ImageDownload_IMAGE_ACTIVATING) { |
| 979 | cloned.AdminState = voltha.AdminState_ENABLED |
| 980 | } |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 981 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 982 | if err := agent.updateDeviceInStoreWithoutLock(cloned, false, ""); err != nil { |
| 983 | return err |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 984 | } |
| 985 | return nil |
| 986 | } |
| 987 | |
| 988 | func (agent *DeviceAgent) getImageDownload(ctx context.Context, img *voltha.ImageDownload) (*voltha.ImageDownload, error) { |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 989 | agent.lockDevice.RLock() |
| 990 | defer agent.lockDevice.RUnlock() |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 991 | log.Debugw("getImageDownload", log.Fields{"id": agent.deviceID}) |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 992 | |
| 993 | cloned := agent.getDeviceWithoutLock() |
| 994 | for _, image := range cloned.ImageDownloads { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 995 | if image.Id == img.Id && image.Name == img.Name { |
| 996 | return image, nil |
| 997 | } |
| 998 | } |
| 999 | return nil, status.Errorf(codes.NotFound, "image-not-found:%s", img.Name) |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 1000 | } |
| 1001 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1002 | func (agent *DeviceAgent) listImageDownloads(ctx context.Context, deviceID string) (*voltha.ImageDownloads, error) { |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 1003 | agent.lockDevice.RLock() |
| 1004 | defer agent.lockDevice.RUnlock() |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1005 | log.Debugw("listImageDownloads", log.Fields{"id": agent.deviceID}) |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1006 | |
| 1007 | return &voltha.ImageDownloads{Items: agent.getDeviceWithoutLock().ImageDownloads}, nil |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 1008 | } |
| 1009 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 1010 | // getPorts retrieves the ports information of the device based on the port type. |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1011 | func (agent *DeviceAgent) getPorts(ctx context.Context, portType voltha.Port_PortType) *voltha.Ports { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1012 | log.Debugw("getPorts", log.Fields{"id": agent.deviceID, "portType": portType}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1013 | ports := &voltha.Ports{} |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1014 | if device, _ := agent.deviceMgr.GetDevice(agent.deviceID); device != nil { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1015 | for _, port := range device.Ports { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1016 | if port.Type == portType { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1017 | ports.Items = append(ports.Items, port) |
| 1018 | } |
| 1019 | } |
| 1020 | } |
| 1021 | return ports |
| 1022 | } |
| 1023 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 1024 | // getSwitchCapability is a helper method that a logical device agent uses to retrieve the switch capability of a |
| 1025 | // parent device |
khenaidoo | 7923270 | 2018-12-04 11:00:41 -0500 | [diff] [blame] | 1026 | func (agent *DeviceAgent) getSwitchCapability(ctx context.Context) (*ic.SwitchCapability, error) { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1027 | log.Debugw("getSwitchCapability", log.Fields{"deviceId": agent.deviceID}) |
| 1028 | device, err := agent.deviceMgr.GetDevice(agent.deviceID) |
| 1029 | if device == nil { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1030 | return nil, err |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1031 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1032 | var switchCap *ic.SwitchCapability |
| 1033 | if switchCap, err = agent.adapterProxy.GetOfpDeviceInfo(ctx, device); err != nil { |
| 1034 | log.Debugw("getSwitchCapability-error", log.Fields{"id": device.Id, "error": err}) |
| 1035 | return nil, err |
| 1036 | } |
| 1037 | return switchCap, nil |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1038 | } |
| 1039 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 1040 | // getPortCapability is a helper method that a logical device agent uses to retrieve the port capability of a |
| 1041 | // device |
khenaidoo | 7923270 | 2018-12-04 11:00:41 -0500 | [diff] [blame] | 1042 | func (agent *DeviceAgent) getPortCapability(ctx context.Context, portNo uint32) (*ic.PortCapability, error) { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1043 | log.Debugw("getPortCapability", log.Fields{"deviceId": agent.deviceID}) |
| 1044 | device, err := agent.deviceMgr.GetDevice(agent.deviceID) |
| 1045 | if device == nil { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1046 | return nil, err |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1047 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1048 | var portCap *ic.PortCapability |
| 1049 | if portCap, err = agent.adapterProxy.GetOfpPortInfo(ctx, device, portNo); err != nil { |
| 1050 | log.Debugw("getPortCapability-error", log.Fields{"id": device.Id, "error": err}) |
| 1051 | return nil, err |
| 1052 | } |
| 1053 | return portCap, nil |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1054 | } |
| 1055 | |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 1056 | func (agent *DeviceAgent) packetOut(outPort uint32, packet *ofp.OfpPacketOut) error { |
Scott Baker | 8067860 | 2019-11-14 16:57:36 -0800 | [diff] [blame] | 1057 | // If deviceType=="" then we must have taken ownership of this device. |
| 1058 | // Fixes VOL-2226 where a core would take ownership and have stale data |
| 1059 | if agent.deviceType == "" { |
| 1060 | agent.reconcileWithKVStore() |
| 1061 | } |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 1062 | // Send packet to adapter |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1063 | if err := agent.adapterProxy.packetOut(agent.deviceType, agent.deviceID, outPort, packet); err != nil { |
Matteo Scandolo | 360605d | 2019-11-05 18:29:17 -0800 | [diff] [blame] | 1064 | log.Debugw("packet-out-error", log.Fields{ |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1065 | "id": agent.deviceID, |
Matteo Scandolo | 360605d | 2019-11-05 18:29:17 -0800 | [diff] [blame] | 1066 | "error": err, |
| 1067 | "packet": hex.EncodeToString(packet.Data), |
| 1068 | }) |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 1069 | return err |
| 1070 | } |
| 1071 | return nil |
| 1072 | } |
| 1073 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 1074 | // processUpdate is a callback invoked whenever there is a change on the device manages by this device agent |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1075 | func (agent *DeviceAgent) processUpdate(args ...interface{}) interface{} { |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 1076 | //// Run this callback in its own go routine |
| 1077 | go func(args ...interface{}) interface{} { |
| 1078 | var previous *voltha.Device |
| 1079 | var current *voltha.Device |
| 1080 | var ok bool |
| 1081 | if len(args) == 2 { |
| 1082 | if previous, ok = args[0].(*voltha.Device); !ok { |
| 1083 | log.Errorw("invalid-callback-type", log.Fields{"data": args[0]}) |
| 1084 | return nil |
| 1085 | } |
| 1086 | if current, ok = args[1].(*voltha.Device); !ok { |
| 1087 | log.Errorw("invalid-callback-type", log.Fields{"data": args[1]}) |
| 1088 | return nil |
| 1089 | } |
| 1090 | } else { |
| 1091 | log.Errorw("too-many-args-in-callback", log.Fields{"len": len(args)}) |
| 1092 | return nil |
| 1093 | } |
| 1094 | // Perform the state transition in it's own go routine |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 1095 | if err := agent.deviceMgr.processTransition(previous, current); err != nil { |
| 1096 | log.Errorw("failed-process-transition", log.Fields{"deviceId": previous.Id, |
| 1097 | "previousAdminState": previous.AdminState, "currentAdminState": current.AdminState}) |
| 1098 | } |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 1099 | return nil |
| 1100 | }(args...) |
| 1101 | |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1102 | return nil |
| 1103 | } |
| 1104 | |
Mahir Gunyel | 8e2707d | 2019-07-25 00:36:21 -0700 | [diff] [blame] | 1105 | // updatePartialDeviceData updates a subset of a device that an Adapter can update. |
| 1106 | // TODO: May need a specific proto to handle only a subset of a device that can be changed by an adapter |
| 1107 | func (agent *DeviceAgent) mergeDeviceInfoFromAdapter(device *voltha.Device) (*voltha.Device, error) { |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1108 | cloned := agent.getDeviceWithoutLock() |
Mahir Gunyel | 8e2707d | 2019-07-25 00:36:21 -0700 | [diff] [blame] | 1109 | cloned.Root = device.Root |
| 1110 | cloned.Vendor = device.Vendor |
| 1111 | cloned.Model = device.Model |
| 1112 | cloned.SerialNumber = device.SerialNumber |
| 1113 | cloned.MacAddress = device.MacAddress |
| 1114 | cloned.Vlan = device.Vlan |
| 1115 | cloned.Reason = device.Reason |
| 1116 | return cloned, nil |
| 1117 | } |
| 1118 | func (agent *DeviceAgent) updateDeviceUsingAdapterData(device *voltha.Device) error { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1119 | agent.lockDevice.Lock() |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 1120 | defer agent.lockDevice.Unlock() |
Mahir Gunyel | 8e2707d | 2019-07-25 00:36:21 -0700 | [diff] [blame] | 1121 | log.Debugw("updateDeviceUsingAdapterData", log.Fields{"deviceId": device.Id}) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1122 | updatedDevice, err := agent.mergeDeviceInfoFromAdapter(device) |
| 1123 | if err != nil { |
Mahir Gunyel | 8e2707d | 2019-07-25 00:36:21 -0700 | [diff] [blame] | 1124 | log.Errorw("failed to update device ", log.Fields{"deviceId": device.Id}) |
| 1125 | return status.Errorf(codes.Internal, "%s", err.Error()) |
Mahir Gunyel | 8e2707d | 2019-07-25 00:36:21 -0700 | [diff] [blame] | 1126 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1127 | cloned := proto.Clone(updatedDevice).(*voltha.Device) |
| 1128 | return agent.updateDeviceInStoreWithoutLock(cloned, false, "") |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 1129 | } |
| 1130 | |
| 1131 | func (agent *DeviceAgent) updateDeviceWithoutLock(device *voltha.Device) error { |
| 1132 | log.Debugw("updateDevice", log.Fields{"deviceId": device.Id}) |
| 1133 | cloned := proto.Clone(device).(*voltha.Device) |
Mahir Gunyel | b585167 | 2019-07-24 10:46:26 +0300 | [diff] [blame] | 1134 | return agent.updateDeviceInStoreWithoutLock(cloned, false, "") |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1135 | } |
| 1136 | |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1137 | func (agent *DeviceAgent) updateDeviceStatus(operStatus voltha.OperStatus_OperStatus, connStatus voltha.ConnectStatus_ConnectStatus) error { |
| 1138 | agent.lockDevice.Lock() |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 1139 | defer agent.lockDevice.Unlock() |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1140 | |
| 1141 | cloned := agent.getDeviceWithoutLock() |
| 1142 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1143 | // Ensure the enums passed in are valid - they will be invalid if they are not set when this function is invoked |
| 1144 | if s, ok := voltha.ConnectStatus_ConnectStatus_value[connStatus.String()]; ok { |
| 1145 | log.Debugw("updateDeviceStatus-conn", log.Fields{"ok": ok, "val": s}) |
| 1146 | cloned.ConnectStatus = connStatus |
| 1147 | } |
| 1148 | if s, ok := voltha.OperStatus_OperStatus_value[operStatus.String()]; ok { |
| 1149 | log.Debugw("updateDeviceStatus-oper", log.Fields{"ok": ok, "val": s}) |
| 1150 | cloned.OperStatus = operStatus |
| 1151 | } |
| 1152 | log.Debugw("updateDeviceStatus", log.Fields{"deviceId": cloned.Id, "operStatus": cloned.OperStatus, "connectStatus": cloned.ConnectStatus}) |
| 1153 | // Store the device |
| 1154 | return agent.updateDeviceInStoreWithoutLock(cloned, false, "") |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1155 | } |
| 1156 | |
khenaidoo | 3ab3488 | 2019-05-02 21:33:30 -0400 | [diff] [blame] | 1157 | func (agent *DeviceAgent) enablePorts() error { |
| 1158 | agent.lockDevice.Lock() |
| 1159 | defer agent.lockDevice.Unlock() |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1160 | |
| 1161 | cloned := agent.getDeviceWithoutLock() |
| 1162 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1163 | for _, port := range cloned.Ports { |
| 1164 | port.AdminState = voltha.AdminState_ENABLED |
| 1165 | port.OperStatus = voltha.OperStatus_ACTIVE |
| 1166 | } |
| 1167 | // Store the device |
| 1168 | return agent.updateDeviceInStoreWithoutLock(cloned, false, "") |
khenaidoo | 3ab3488 | 2019-05-02 21:33:30 -0400 | [diff] [blame] | 1169 | } |
| 1170 | |
| 1171 | func (agent *DeviceAgent) disablePorts() error { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1172 | log.Debugw("disablePorts", log.Fields{"deviceid": agent.deviceID}) |
khenaidoo | 3ab3488 | 2019-05-02 21:33:30 -0400 | [diff] [blame] | 1173 | agent.lockDevice.Lock() |
| 1174 | defer agent.lockDevice.Unlock() |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1175 | cloned := agent.getDeviceWithoutLock() |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1176 | for _, port := range cloned.Ports { |
| 1177 | port.AdminState = voltha.AdminState_DISABLED |
| 1178 | port.OperStatus = voltha.OperStatus_UNKNOWN |
| 1179 | } |
| 1180 | // Store the device |
| 1181 | return agent.updateDeviceInStoreWithoutLock(cloned, false, "") |
khenaidoo | 3ab3488 | 2019-05-02 21:33:30 -0400 | [diff] [blame] | 1182 | } |
| 1183 | |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1184 | func (agent *DeviceAgent) updatePortState(portType voltha.Port_PortType, portNo uint32, operStatus voltha.OperStatus_OperStatus) error { |
| 1185 | agent.lockDevice.Lock() |
khenaidoo | 59ef7be | 2019-06-21 12:40:28 -0400 | [diff] [blame] | 1186 | defer agent.lockDevice.Unlock() |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1187 | // Work only on latest data |
| 1188 | // TODO: Get list of ports from device directly instead of the entire device |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1189 | cloned := agent.getDeviceWithoutLock() |
| 1190 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1191 | // Ensure the enums passed in are valid - they will be invalid if they are not set when this function is invoked |
| 1192 | if _, ok := voltha.Port_PortType_value[portType.String()]; !ok { |
| 1193 | return status.Errorf(codes.InvalidArgument, "%s", portType) |
| 1194 | } |
| 1195 | for _, port := range cloned.Ports { |
| 1196 | if port.Type == portType && port.PortNo == portNo { |
| 1197 | port.OperStatus = operStatus |
| 1198 | // Set the admin status to ENABLED if the operational status is ACTIVE |
| 1199 | // TODO: Set by northbound system? |
| 1200 | if operStatus == voltha.OperStatus_ACTIVE { |
| 1201 | port.AdminState = voltha.AdminState_ENABLED |
| 1202 | } |
| 1203 | break |
| 1204 | } |
| 1205 | } |
| 1206 | log.Debugw("portStatusUpdate", log.Fields{"deviceId": cloned.Id}) |
| 1207 | // Store the device |
| 1208 | return agent.updateDeviceInStoreWithoutLock(cloned, false, "") |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1209 | } |
| 1210 | |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 1211 | func (agent *DeviceAgent) deleteAllPorts() error { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1212 | log.Debugw("deleteAllPorts", log.Fields{"deviceId": agent.deviceID}) |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 1213 | agent.lockDevice.Lock() |
| 1214 | defer agent.lockDevice.Unlock() |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1215 | |
| 1216 | cloned := agent.getDeviceWithoutLock() |
| 1217 | |
| 1218 | if cloned.AdminState != voltha.AdminState_DISABLED && cloned.AdminState != voltha.AdminState_DELETED { |
| 1219 | err := status.Error(codes.FailedPrecondition, fmt.Sprintf("invalid-state-%v", cloned.AdminState)) |
| 1220 | log.Warnw("invalid-state-removing-ports", log.Fields{"state": cloned.AdminState, "error": err}) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1221 | return err |
| 1222 | } |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1223 | if len(cloned.Ports) == 0 { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1224 | log.Debugw("no-ports-present", log.Fields{"deviceId": agent.deviceID}) |
| 1225 | return nil |
| 1226 | } |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1227 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1228 | cloned.Ports = []*voltha.Port{} |
| 1229 | log.Debugw("portStatusUpdate", log.Fields{"deviceId": cloned.Id}) |
| 1230 | // Store the device |
| 1231 | return agent.updateDeviceInStoreWithoutLock(cloned, false, "") |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 1232 | } |
| 1233 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1234 | func (agent *DeviceAgent) addPort(port *voltha.Port) error { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1235 | agent.lockDevice.Lock() |
| 1236 | defer agent.lockDevice.Unlock() |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1237 | log.Debugw("addPort", log.Fields{"deviceId": agent.deviceID}) |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1238 | |
| 1239 | cloned := agent.getDeviceWithoutLock() |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1240 | if cloned.Ports == nil { |
| 1241 | // First port |
| 1242 | log.Debugw("addPort-first-port-to-add", log.Fields{"deviceId": agent.deviceID}) |
| 1243 | cloned.Ports = make([]*voltha.Port, 0) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1244 | } else { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1245 | for _, p := range cloned.Ports { |
| 1246 | if p.Type == port.Type && p.PortNo == port.PortNo { |
| 1247 | log.Debugw("port already exists", log.Fields{"port": *port}) |
| 1248 | return nil |
manikkaraj k | 259a6f7 | 2019-05-06 09:55:44 -0400 | [diff] [blame] | 1249 | } |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1250 | } |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1251 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1252 | cp := proto.Clone(port).(*voltha.Port) |
| 1253 | // Set the admin state of the port to ENABLE if the operational state is ACTIVE |
| 1254 | // TODO: Set by northbound system? |
| 1255 | if cp.OperStatus == voltha.OperStatus_ACTIVE { |
| 1256 | cp.AdminState = voltha.AdminState_ENABLED |
| 1257 | } |
| 1258 | cloned.Ports = append(cloned.Ports, cp) |
| 1259 | // Store the device |
| 1260 | return agent.updateDeviceInStoreWithoutLock(cloned, false, "") |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1261 | } |
| 1262 | |
| 1263 | func (agent *DeviceAgent) addPeerPort(port *voltha.Port_PeerPort) error { |
| 1264 | agent.lockDevice.Lock() |
| 1265 | defer agent.lockDevice.Unlock() |
| 1266 | log.Debug("addPeerPort") |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1267 | |
| 1268 | cloned := agent.getDeviceWithoutLock() |
| 1269 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1270 | // Get the peer port on the device based on the port no |
| 1271 | for _, peerPort := range cloned.Ports { |
| 1272 | if peerPort.PortNo == port.PortNo { // found port |
| 1273 | cp := proto.Clone(port).(*voltha.Port_PeerPort) |
| 1274 | peerPort.Peers = append(peerPort.Peers, cp) |
| 1275 | log.Debugw("found-peer", log.Fields{"portNo": port.PortNo, "deviceId": agent.deviceID}) |
| 1276 | break |
| 1277 | } |
| 1278 | } |
| 1279 | // Store the device |
| 1280 | return agent.updateDeviceInStoreWithoutLock(cloned, false, "") |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1281 | } |
| 1282 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1283 | func (agent *DeviceAgent) deletePeerPorts(deviceID string) error { |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 1284 | agent.lockDevice.Lock() |
| 1285 | defer agent.lockDevice.Unlock() |
| 1286 | log.Debug("deletePeerPorts") |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1287 | |
| 1288 | cloned := agent.getDeviceWithoutLock() |
| 1289 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1290 | var updatedPeers []*voltha.Port_PeerPort |
| 1291 | for _, port := range cloned.Ports { |
| 1292 | updatedPeers = make([]*voltha.Port_PeerPort, 0) |
| 1293 | for _, peerPort := range port.Peers { |
| 1294 | if peerPort.DeviceId != deviceID { |
| 1295 | updatedPeers = append(updatedPeers, peerPort) |
| 1296 | } |
| 1297 | } |
| 1298 | port.Peers = updatedPeers |
| 1299 | } |
| 1300 | |
| 1301 | // Store the device with updated peer ports |
| 1302 | return agent.updateDeviceInStoreWithoutLock(cloned, false, "") |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 1303 | } |
| 1304 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1305 | // TODO: A generic device update by attribute |
| 1306 | func (agent *DeviceAgent) updateDeviceAttribute(name string, value interface{}) { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1307 | agent.lockDevice.Lock() |
| 1308 | defer agent.lockDevice.Unlock() |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1309 | if value == nil { |
| 1310 | return |
| 1311 | } |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1312 | |
| 1313 | cloned := agent.getDeviceWithoutLock() |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1314 | updated := false |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1315 | s := reflect.ValueOf(cloned).Elem() |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1316 | if s.Kind() == reflect.Struct { |
| 1317 | // exported field |
| 1318 | f := s.FieldByName(name) |
| 1319 | if f.IsValid() && f.CanSet() { |
| 1320 | switch f.Kind() { |
| 1321 | case reflect.String: |
| 1322 | f.SetString(value.(string)) |
| 1323 | updated = true |
| 1324 | case reflect.Uint32: |
| 1325 | f.SetUint(uint64(value.(uint32))) |
| 1326 | updated = true |
| 1327 | case reflect.Bool: |
| 1328 | f.SetBool(value.(bool)) |
| 1329 | updated = true |
| 1330 | } |
| 1331 | } |
| 1332 | } |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1333 | 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] | 1334 | // Save the data |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1335 | |
| 1336 | if err := agent.updateDeviceInStoreWithoutLock(cloned, false, ""); err != nil { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1337 | log.Warnw("attribute-update-failed", log.Fields{"attribute": name, "value": value}) |
| 1338 | } |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1339 | } |
serkant.uluderya | 334479d | 2019-04-10 08:26:15 -0700 | [diff] [blame] | 1340 | |
| 1341 | func (agent *DeviceAgent) simulateAlarm(ctx context.Context, simulatereq *voltha.SimulateAlarmRequest) error { |
| 1342 | agent.lockDevice.Lock() |
| 1343 | defer agent.lockDevice.Unlock() |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1344 | log.Debugw("simulateAlarm", log.Fields{"id": agent.deviceID}) |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1345 | |
| 1346 | cloned := agent.getDeviceWithoutLock() |
| 1347 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1348 | // First send the request to an Adapter and wait for a response |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1349 | if err := agent.adapterProxy.SimulateAlarm(ctx, cloned, simulatereq); err != nil { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1350 | log.Debugw("simulateAlarm-error", log.Fields{"id": agent.deviceID, "error": err}) |
| 1351 | return err |
serkant.uluderya | 334479d | 2019-04-10 08:26:15 -0700 | [diff] [blame] | 1352 | } |
| 1353 | return nil |
| 1354 | } |
Mahir Gunyel | b585167 | 2019-07-24 10:46:26 +0300 | [diff] [blame] | 1355 | |
| 1356 | //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. |
| 1357 | // It is an internal helper function. |
| 1358 | func (agent *DeviceAgent) updateDeviceInStoreWithoutLock(device *voltha.Device, strict bool, txid string) error { |
| 1359 | updateCtx := context.WithValue(context.Background(), model.RequestTimestamp, time.Now().UnixNano()) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 1360 | afterUpdate, err := agent.clusterDataProxy.Update(updateCtx, "/devices/"+agent.deviceID, device, strict, txid) |
| 1361 | if err != nil { |
| 1362 | return status.Errorf(codes.Internal, "failed-update-device:%s", agent.deviceID) |
| 1363 | } |
| 1364 | if afterUpdate == nil { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1365 | return status.Errorf(codes.Internal, "failed-update-device:%s", agent.deviceID) |
Mahir Gunyel | b585167 | 2019-07-24 10:46:26 +0300 | [diff] [blame] | 1366 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1367 | log.Debugw("updated-device-in-store", log.Fields{"deviceId: ": agent.deviceID}) |
Mahir Gunyel | b585167 | 2019-07-24 10:46:26 +0300 | [diff] [blame] | 1368 | |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1369 | agent.device = proto.Clone(device).(*voltha.Device) |
| 1370 | |
Mahir Gunyel | b585167 | 2019-07-24 10:46:26 +0300 | [diff] [blame] | 1371 | return nil |
| 1372 | } |
Mahir Gunyel | fdee921 | 2019-10-16 16:52:21 -0700 | [diff] [blame] | 1373 | |
| 1374 | func (agent *DeviceAgent) updateDeviceReason(reason string) error { |
| 1375 | agent.lockDevice.Lock() |
| 1376 | defer agent.lockDevice.Unlock() |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1377 | |
| 1378 | cloned := agent.getDeviceWithoutLock() |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1379 | cloned.Reason = reason |
| 1380 | log.Debugw("updateDeviceReason", log.Fields{"deviceId": cloned.Id, "reason": cloned.Reason}) |
| 1381 | // Store the device |
| 1382 | return agent.updateDeviceInStoreWithoutLock(cloned, false, "") |
Mahir Gunyel | fdee921 | 2019-10-16 16:52:21 -0700 | [diff] [blame] | 1383 | } |