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 | */ |
| 16 | package core |
| 17 | |
| 18 | import ( |
| 19 | "context" |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 20 | "fmt" |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 21 | "github.com/gogo/protobuf/proto" |
| 22 | "github.com/opencord/voltha-go/common/log" |
| 23 | "github.com/opencord/voltha-go/db/model" |
khenaidoo | 7923270 | 2018-12-04 11:00:41 -0500 | [diff] [blame] | 24 | ic "github.com/opencord/voltha-go/protos/inter_container" |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 25 | ofp "github.com/opencord/voltha-go/protos/openflow_13" |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 26 | "github.com/opencord/voltha-go/protos/voltha" |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 27 | fu "github.com/opencord/voltha-go/rw_core/utils" |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 28 | "google.golang.org/grpc/codes" |
| 29 | "google.golang.org/grpc/status" |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 30 | "reflect" |
| 31 | "sync" |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 32 | ) |
| 33 | |
| 34 | type DeviceAgent struct { |
khenaidoo | 9a46896 | 2018-09-19 15:33:13 -0400 | [diff] [blame] | 35 | deviceId string |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 36 | deviceType string |
khenaidoo | 9a46896 | 2018-09-19 15:33:13 -0400 | [diff] [blame] | 37 | lastData *voltha.Device |
| 38 | adapterProxy *AdapterProxy |
| 39 | deviceMgr *DeviceManager |
| 40 | clusterDataProxy *model.Proxy |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 41 | deviceProxy *model.Proxy |
khenaidoo | 9a46896 | 2018-09-19 15:33:13 -0400 | [diff] [blame] | 42 | exitChannel chan int |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 43 | flowProxy *model.Proxy |
| 44 | groupProxy *model.Proxy |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 45 | lockDevice sync.RWMutex |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 46 | } |
| 47 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 48 | //newDeviceAgent creates a new device agent along as creating a unique ID for the device and set the device state to |
| 49 | //preprovisioning |
khenaidoo | 9a46896 | 2018-09-19 15:33:13 -0400 | [diff] [blame] | 50 | func newDeviceAgent(ap *AdapterProxy, device *voltha.Device, deviceMgr *DeviceManager, cdProxy *model.Proxy) *DeviceAgent { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 51 | var agent DeviceAgent |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 52 | agent.adapterProxy = ap |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 53 | cloned := (proto.Clone(device)).(*voltha.Device) |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame^] | 54 | if cloned.Id == "" { |
| 55 | cloned.Id = CreateDeviceId() |
| 56 | } |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 57 | cloned.AdminState = voltha.AdminState_PREPROVISIONED |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 58 | cloned.FlowGroups = &ofp.FlowGroups{Items: nil} |
| 59 | cloned.Flows = &ofp.Flows{Items: nil} |
| 60 | if !device.GetRoot() && device.ProxyAddress != nil { |
| 61 | // Set the default vlan ID to the one specified by the parent adapter. It can be |
| 62 | // overwritten by the child adapter during a device update request |
| 63 | cloned.Vlan = device.ProxyAddress.ChannelId |
| 64 | } |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 65 | agent.deviceId = cloned.Id |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 66 | agent.deviceType = cloned.Type |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 67 | agent.lastData = cloned |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 68 | agent.deviceMgr = deviceMgr |
| 69 | agent.exitChannel = make(chan int, 1) |
khenaidoo | 9a46896 | 2018-09-19 15:33:13 -0400 | [diff] [blame] | 70 | agent.clusterDataProxy = cdProxy |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 71 | agent.lockDevice = sync.RWMutex{} |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 72 | return &agent |
| 73 | } |
| 74 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 75 | // start save the device to the data model and registers for callbacks on that device |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 76 | func (agent *DeviceAgent) start(ctx context.Context) { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 77 | agent.lockDevice.Lock() |
| 78 | defer agent.lockDevice.Unlock() |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 79 | log.Debugw("starting-device-agent", log.Fields{"device": agent.lastData}) |
| 80 | // Add the initial device to the local model |
khenaidoo | 9a46896 | 2018-09-19 15:33:13 -0400 | [diff] [blame] | 81 | if added := agent.clusterDataProxy.Add("/devices", agent.lastData, ""); added == nil { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 82 | log.Errorw("failed-to-add-device", log.Fields{"deviceId": agent.deviceId}) |
| 83 | } |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 84 | agent.deviceProxy = agent.clusterDataProxy.Root.CreateProxy("/devices/"+agent.deviceId, false) |
| 85 | agent.deviceProxy.RegisterCallback(model.POST_UPDATE, agent.processUpdate) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 86 | |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 87 | agent.flowProxy = agent.clusterDataProxy.Root.CreateProxy( |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 88 | fmt.Sprintf("/devices/%s/flows", agent.deviceId), |
| 89 | false) |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 90 | agent.groupProxy = agent.clusterDataProxy.Root.CreateProxy( |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 91 | fmt.Sprintf("/devices/%s/flow_groups", agent.deviceId), |
| 92 | false) |
| 93 | |
| 94 | agent.flowProxy.RegisterCallback(model.POST_UPDATE, agent.flowTableUpdated) |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 95 | agent.groupProxy.RegisterCallback(model.POST_UPDATE, agent.groupTableUpdated) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 96 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 97 | log.Debug("device-agent-started") |
| 98 | } |
| 99 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 100 | // stop stops the device agent. Not much to do for now |
| 101 | func (agent *DeviceAgent) stop(ctx context.Context) { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 102 | agent.lockDevice.Lock() |
| 103 | defer agent.lockDevice.Unlock() |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 104 | log.Debug("stopping-device-agent") |
| 105 | agent.exitChannel <- 1 |
| 106 | log.Debug("device-agent-stopped") |
| 107 | } |
| 108 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 109 | // GetDevice retrieves the latest device information from the data model |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 110 | func (agent *DeviceAgent) getDevice() (*voltha.Device, error) { |
| 111 | agent.lockDevice.Lock() |
| 112 | defer agent.lockDevice.Unlock() |
| 113 | if device := agent.clusterDataProxy.Get("/devices/"+agent.deviceId, 1, false, ""); device != nil { |
| 114 | if d, ok := device.(*voltha.Device); ok { |
| 115 | cloned := proto.Clone(d).(*voltha.Device) |
| 116 | return cloned, nil |
| 117 | } |
| 118 | } |
| 119 | return nil, status.Errorf(codes.NotFound, "device-%s", agent.deviceId) |
| 120 | } |
| 121 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 122 | // getDeviceWithoutLock is a helper function to be used ONLY by any device agent function AFTER it has acquired the device lock. |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 123 | // This function is meant so that we do not have duplicate code all over the device agent functions |
| 124 | func (agent *DeviceAgent) getDeviceWithoutLock() (*voltha.Device, error) { |
| 125 | if device := agent.clusterDataProxy.Get("/devices/"+agent.deviceId, 1, false, ""); device != nil { |
| 126 | if d, ok := device.(*voltha.Device); ok { |
| 127 | cloned := proto.Clone(d).(*voltha.Device) |
| 128 | return cloned, nil |
| 129 | } |
| 130 | } |
| 131 | return nil, status.Errorf(codes.NotFound, "device-%s", agent.deviceId) |
| 132 | } |
| 133 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 134 | // enableDevice activates a preprovisioned or disable device |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 135 | func (agent *DeviceAgent) enableDevice(ctx context.Context) error { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 136 | agent.lockDevice.Lock() |
| 137 | defer agent.lockDevice.Unlock() |
| 138 | log.Debugw("enableDevice", log.Fields{"id": agent.deviceId}) |
| 139 | if device, err := agent.getDeviceWithoutLock(); err != nil { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 140 | return status.Errorf(codes.NotFound, "%s", agent.deviceId) |
| 141 | } else { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 142 | if device.AdminState == voltha.AdminState_ENABLED { |
| 143 | log.Debugw("device-already-enabled", log.Fields{"id": agent.deviceId}) |
| 144 | //TODO: Needs customized error message |
| 145 | return nil |
| 146 | } |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 147 | //TODO: if parent device is disabled then do not enable device |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 148 | // Verify whether we need to adopt the device the first time |
| 149 | // TODO: A state machine for these state transitions would be better (we just have to handle |
| 150 | // a limited set of states now or it may be an overkill) |
| 151 | if device.AdminState == voltha.AdminState_PREPROVISIONED { |
| 152 | // First send the request to an Adapter and wait for a response |
| 153 | if err := agent.adapterProxy.AdoptDevice(ctx, device); err != nil { |
| 154 | log.Debugw("adoptDevice-error", log.Fields{"id": agent.lastData.Id, "error": err}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 155 | return err |
| 156 | } |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 157 | } else { |
| 158 | // First send the request to an Adapter and wait for a response |
| 159 | if err := agent.adapterProxy.ReEnableDevice(ctx, device); err != nil { |
| 160 | log.Debugw("renableDevice-error", log.Fields{"id": agent.lastData.Id, "error": err}) |
| 161 | return err |
| 162 | } |
| 163 | } |
| 164 | // Received an Ack (no error found above). Now update the device in the model to the expected state |
| 165 | cloned := proto.Clone(device).(*voltha.Device) |
| 166 | cloned.AdminState = voltha.AdminState_ENABLED |
| 167 | cloned.OperStatus = voltha.OperStatus_ACTIVATING |
| 168 | if afterUpdate := agent.clusterDataProxy.Update("/devices/"+agent.deviceId, cloned, false, ""); afterUpdate == nil { |
| 169 | return status.Errorf(codes.Internal, "failed-update-device:%s", agent.deviceId) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 170 | } |
| 171 | } |
| 172 | return nil |
| 173 | } |
| 174 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 175 | func (agent *DeviceAgent) updateFlows(flows []*ofp.OfpFlowStats) error { |
| 176 | agent.lockDevice.Lock() |
| 177 | defer agent.lockDevice.Unlock() |
| 178 | log.Debugw("updateFlows", log.Fields{"deviceId": agent.deviceId, "flows": flows}) |
| 179 | var oldData *voltha.Flows |
| 180 | if storedData, err := agent.getDeviceWithoutLock(); err != nil { |
| 181 | return status.Errorf(codes.NotFound, "%s", agent.deviceId) |
| 182 | } else { |
| 183 | oldData = proto.Clone(storedData.Flows).(*voltha.Flows) |
| 184 | log.Debugw("updateFlows", log.Fields{"deviceId": agent.deviceId, "flows": flows, "old": oldData}) |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame^] | 185 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 186 | // store the changed data |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame^] | 187 | afterUpdate := agent.flowProxy.Update("/", &ofp.Flows{Items: flows}, false, "") |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 188 | if afterUpdate == nil { |
| 189 | return status.Errorf(codes.Internal, "%s", agent.deviceId) |
| 190 | } |
| 191 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 192 | return nil |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | func (agent *DeviceAgent) updateGroups(groups []*ofp.OfpGroupEntry) error { |
| 197 | agent.lockDevice.Lock() |
| 198 | defer agent.lockDevice.Unlock() |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 199 | log.Debugw("updateGroups", log.Fields{"deviceId": agent.deviceId, "groups": groups}) |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame^] | 200 | if _, err := agent.getDeviceWithoutLock(); err != nil { |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 201 | return status.Errorf(codes.NotFound, "%s", agent.deviceId) |
| 202 | } else { |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 203 | // store the changed data |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame^] | 204 | afterUpdate := agent.groupProxy.Update("/", &ofp.FlowGroups{Items: groups}, false, "") |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 205 | if afterUpdate == nil { |
| 206 | return status.Errorf(codes.Internal, "%s", agent.deviceId) |
| 207 | } |
| 208 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 209 | return nil |
| 210 | } |
| 211 | } |
| 212 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 213 | //disableDevice disable a device |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 214 | func (agent *DeviceAgent) disableDevice(ctx context.Context) error { |
| 215 | agent.lockDevice.Lock() |
| 216 | //defer agent.lockDevice.Unlock() |
| 217 | log.Debugw("disableDevice", log.Fields{"id": agent.deviceId}) |
| 218 | // Get the most up to date the device info |
| 219 | if device, err := agent.getDeviceWithoutLock(); err != nil { |
| 220 | return status.Errorf(codes.NotFound, "%s", agent.deviceId) |
| 221 | } else { |
| 222 | if device.AdminState == voltha.AdminState_DISABLED { |
| 223 | log.Debugw("device-already-disabled", log.Fields{"id": agent.deviceId}) |
| 224 | //TODO: Needs customized error message |
| 225 | agent.lockDevice.Unlock() |
| 226 | return nil |
| 227 | } |
| 228 | // First send the request to an Adapter and wait for a response |
| 229 | if err := agent.adapterProxy.DisableDevice(ctx, device); err != nil { |
| 230 | log.Debugw("disableDevice-error", log.Fields{"id": agent.lastData.Id, "error": err}) |
| 231 | agent.lockDevice.Unlock() |
| 232 | return err |
| 233 | } |
| 234 | // Received an Ack (no error found above). Now update the device in the model to the expected state |
| 235 | cloned := proto.Clone(device).(*voltha.Device) |
| 236 | cloned.AdminState = voltha.AdminState_DISABLED |
| 237 | // Set the state of all ports on that device to disable |
| 238 | for _, port := range cloned.Ports { |
| 239 | port.AdminState = voltha.AdminState_DISABLED |
| 240 | port.OperStatus = voltha.OperStatus_UNKNOWN |
| 241 | } |
| 242 | if afterUpdate := agent.clusterDataProxy.Update("/devices/"+agent.deviceId, cloned, false, ""); afterUpdate == nil { |
| 243 | agent.lockDevice.Unlock() |
| 244 | return status.Errorf(codes.Internal, "failed-update-device:%s", agent.deviceId) |
| 245 | } |
| 246 | agent.lockDevice.Unlock() |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 247 | } |
| 248 | return nil |
| 249 | } |
| 250 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 251 | func (agent *DeviceAgent) rebootDevice(ctx context.Context) error { |
| 252 | agent.lockDevice.Lock() |
| 253 | defer agent.lockDevice.Unlock() |
| 254 | log.Debugw("rebootDevice", log.Fields{"id": agent.deviceId}) |
| 255 | // Get the most up to date the device info |
| 256 | if device, err := agent.getDeviceWithoutLock(); err != nil { |
| 257 | return status.Errorf(codes.NotFound, "%s", agent.deviceId) |
| 258 | } else { |
| 259 | if device.AdminState != voltha.AdminState_DISABLED { |
| 260 | log.Debugw("device-not-disabled", log.Fields{"id": agent.deviceId}) |
| 261 | //TODO: Needs customized error message |
| 262 | return status.Errorf(codes.FailedPrecondition, "deviceId:%s, expected-admin-state:%s", agent.deviceId, voltha.AdminState_DISABLED) |
| 263 | } |
| 264 | // First send the request to an Adapter and wait for a response |
| 265 | if err := agent.adapterProxy.RebootDevice(ctx, device); err != nil { |
| 266 | log.Debugw("rebootDevice-error", log.Fields{"id": agent.lastData.Id, "error": err}) |
| 267 | return err |
| 268 | } |
| 269 | } |
| 270 | return nil |
| 271 | } |
| 272 | |
| 273 | func (agent *DeviceAgent) deleteDevice(ctx context.Context) error { |
| 274 | agent.lockDevice.Lock() |
| 275 | log.Debugw("deleteDevice", log.Fields{"id": agent.deviceId}) |
| 276 | // Get the most up to date the device info |
| 277 | if device, err := agent.getDeviceWithoutLock(); err != nil { |
| 278 | agent.lockDevice.Unlock() |
| 279 | return status.Errorf(codes.NotFound, "%s", agent.deviceId) |
| 280 | } else { |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 281 | if (device.AdminState != voltha.AdminState_DISABLED) && |
| 282 | (device.AdminState != voltha.AdminState_PREPROVISIONED) { |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 283 | log.Debugw("device-not-disabled", log.Fields{"id": agent.deviceId}) |
| 284 | //TODO: Needs customized error message |
| 285 | agent.lockDevice.Unlock() |
| 286 | return status.Errorf(codes.FailedPrecondition, "deviceId:%s, expected-admin-state:%s", agent.deviceId, voltha.AdminState_DISABLED) |
| 287 | } |
| 288 | // Send the request to an Adapter and wait for a response |
| 289 | if err := agent.adapterProxy.DeleteDevice(ctx, device); err != nil { |
| 290 | log.Debugw("deleteDevice-error", log.Fields{"id": agent.lastData.Id, "error": err}) |
| 291 | agent.lockDevice.Unlock() |
| 292 | return err |
| 293 | } |
| 294 | // Set the device Admin state to DELETED in order to trigger the callback to delete |
| 295 | // child devices, if any |
| 296 | // Received an Ack (no error found above). Now update the device in the model to the expected state |
| 297 | cloned := proto.Clone(device).(*voltha.Device) |
| 298 | cloned.AdminState = voltha.AdminState_DELETED |
| 299 | if afterUpdate := agent.clusterDataProxy.Update("/devices/"+agent.deviceId, cloned, false, ""); afterUpdate == nil { |
| 300 | agent.lockDevice.Unlock() |
| 301 | return status.Errorf(codes.Internal, "failed-update-device:%s", agent.deviceId) |
| 302 | } |
| 303 | agent.lockDevice.Unlock() |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 304 | } |
| 305 | return nil |
| 306 | } |
| 307 | |
| 308 | // getPorts retrieves the ports information of the device based on the port type. |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 309 | func (agent *DeviceAgent) getPorts(ctx context.Context, portType voltha.Port_PortType) *voltha.Ports { |
| 310 | log.Debugw("getPorts", log.Fields{"id": agent.deviceId, "portType": portType}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 311 | ports := &voltha.Ports{} |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 312 | if device, _ := agent.deviceMgr.GetDevice(agent.deviceId); device != nil { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 313 | for _, port := range device.Ports { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 314 | if port.Type == portType { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 315 | ports.Items = append(ports.Items, port) |
| 316 | } |
| 317 | } |
| 318 | } |
| 319 | return ports |
| 320 | } |
| 321 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 322 | // getSwitchCapability is a helper method that a logical device agent uses to retrieve the switch capability of a |
| 323 | // parent device |
khenaidoo | 7923270 | 2018-12-04 11:00:41 -0500 | [diff] [blame] | 324 | func (agent *DeviceAgent) getSwitchCapability(ctx context.Context) (*ic.SwitchCapability, error) { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 325 | log.Debugw("getSwitchCapability", log.Fields{"deviceId": agent.deviceId}) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 326 | if device, err := agent.deviceMgr.GetDevice(agent.deviceId); device == nil { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 327 | return nil, err |
| 328 | } else { |
khenaidoo | 7923270 | 2018-12-04 11:00:41 -0500 | [diff] [blame] | 329 | var switchCap *ic.SwitchCapability |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 330 | var err error |
| 331 | if switchCap, err = agent.adapterProxy.GetOfpDeviceInfo(ctx, device); err != nil { |
| 332 | log.Debugw("getSwitchCapability-error", log.Fields{"id": device.Id, "error": err}) |
| 333 | return nil, err |
| 334 | } |
| 335 | return switchCap, nil |
| 336 | } |
| 337 | } |
| 338 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 339 | // getPortCapability is a helper method that a logical device agent uses to retrieve the port capability of a |
| 340 | // device |
khenaidoo | 7923270 | 2018-12-04 11:00:41 -0500 | [diff] [blame] | 341 | func (agent *DeviceAgent) getPortCapability(ctx context.Context, portNo uint32) (*ic.PortCapability, error) { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 342 | log.Debugw("getPortCapability", log.Fields{"deviceId": agent.deviceId}) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 343 | if device, err := agent.deviceMgr.GetDevice(agent.deviceId); device == nil { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 344 | return nil, err |
| 345 | } else { |
khenaidoo | 7923270 | 2018-12-04 11:00:41 -0500 | [diff] [blame] | 346 | var portCap *ic.PortCapability |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 347 | var err error |
| 348 | if portCap, err = agent.adapterProxy.GetOfpPortInfo(ctx, device, portNo); err != nil { |
| 349 | log.Debugw("getPortCapability-error", log.Fields{"id": device.Id, "error": err}) |
| 350 | return nil, err |
| 351 | } |
| 352 | return portCap, nil |
| 353 | } |
| 354 | } |
| 355 | |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 356 | func (agent *DeviceAgent) packetOut(outPort uint32, packet *ofp.OfpPacketOut) error { |
| 357 | // Send packet to adapter |
| 358 | if err := agent.adapterProxy.packetOut(agent.deviceType, agent.deviceId, outPort, packet); err != nil { |
| 359 | log.Debugw("packet-out-error", log.Fields{"id": agent.lastData.Id, "error": err}) |
| 360 | return err |
| 361 | } |
| 362 | return nil |
| 363 | } |
| 364 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 365 | // 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] | 366 | func (agent *DeviceAgent) processUpdate(args ...interface{}) interface{} { |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 367 | //// Run this callback in its own go routine |
| 368 | go func(args ...interface{}) interface{} { |
| 369 | var previous *voltha.Device |
| 370 | var current *voltha.Device |
| 371 | var ok bool |
| 372 | if len(args) == 2 { |
| 373 | if previous, ok = args[0].(*voltha.Device); !ok { |
| 374 | log.Errorw("invalid-callback-type", log.Fields{"data": args[0]}) |
| 375 | return nil |
| 376 | } |
| 377 | if current, ok = args[1].(*voltha.Device); !ok { |
| 378 | log.Errorw("invalid-callback-type", log.Fields{"data": args[1]}) |
| 379 | return nil |
| 380 | } |
| 381 | } else { |
| 382 | log.Errorw("too-many-args-in-callback", log.Fields{"len": len(args)}) |
| 383 | return nil |
| 384 | } |
| 385 | // Perform the state transition in it's own go routine |
| 386 | agent.deviceMgr.processTransition(previous, current) |
| 387 | return nil |
| 388 | }(args...) |
| 389 | |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 390 | return nil |
| 391 | } |
| 392 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 393 | func (agent *DeviceAgent) updateDevice(device *voltha.Device) error { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 394 | agent.lockDevice.Lock() |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 395 | defer agent.lockDevice.Unlock() |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 396 | log.Debugw("updateDevice", log.Fields{"deviceId": device.Id}) |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 397 | cloned := proto.Clone(device).(*voltha.Device) |
| 398 | afterUpdate := agent.clusterDataProxy.Update("/devices/"+device.Id, cloned, false, "") |
| 399 | if afterUpdate == nil { |
| 400 | return status.Errorf(codes.Internal, "%s", device.Id) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 401 | } |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 402 | return nil |
| 403 | } |
| 404 | |
| 405 | func (agent *DeviceAgent) updateDeviceWithoutLock(device *voltha.Device) error { |
| 406 | log.Debugw("updateDevice", log.Fields{"deviceId": device.Id}) |
| 407 | cloned := proto.Clone(device).(*voltha.Device) |
| 408 | afterUpdate := agent.clusterDataProxy.Update("/devices/"+device.Id, cloned, false, "") |
| 409 | if afterUpdate == nil { |
| 410 | return status.Errorf(codes.Internal, "%s", device.Id) |
| 411 | } |
| 412 | return nil |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 413 | } |
| 414 | |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 415 | func (agent *DeviceAgent) updateDeviceStatus(operStatus voltha.OperStatus_OperStatus, connStatus voltha.ConnectStatus_ConnectStatus) error { |
| 416 | agent.lockDevice.Lock() |
| 417 | //defer agent.lockDevice.Unlock() |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 418 | // Work only on latest data |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 419 | if storeDevice, err := agent.getDeviceWithoutLock(); err != nil { |
| 420 | agent.lockDevice.Unlock() |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 421 | return status.Errorf(codes.NotFound, "%s", agent.deviceId) |
| 422 | } else { |
| 423 | // clone the device |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 424 | cloned := proto.Clone(storeDevice).(*voltha.Device) |
| 425 | // Ensure the enums passed in are valid - they will be invalid if they are not set when this function is invoked |
| 426 | if s, ok := voltha.ConnectStatus_ConnectStatus_value[connStatus.String()]; ok { |
| 427 | log.Debugw("updateDeviceStatus-conn", log.Fields{"ok": ok, "val": s}) |
| 428 | cloned.ConnectStatus = connStatus |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 429 | } |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 430 | if s, ok := voltha.OperStatus_OperStatus_value[operStatus.String()]; ok { |
| 431 | log.Debugw("updateDeviceStatus-oper", log.Fields{"ok": ok, "val": s}) |
| 432 | cloned.OperStatus = operStatus |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 433 | } |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 434 | log.Debugw("updateDeviceStatus", log.Fields{"deviceId": cloned.Id, "operStatus": cloned.OperStatus, "connectStatus": cloned.ConnectStatus}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 435 | // Store the device |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 436 | if afterUpdate := agent.clusterDataProxy.Update("/devices/"+agent.deviceId, cloned, false, ""); afterUpdate == nil { |
| 437 | agent.lockDevice.Unlock() |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 438 | return status.Errorf(codes.Internal, "%s", agent.deviceId) |
| 439 | } |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 440 | agent.lockDevice.Unlock() |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 441 | return nil |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | func (agent *DeviceAgent) updatePortState(portType voltha.Port_PortType, portNo uint32, operStatus voltha.OperStatus_OperStatus) error { |
| 446 | agent.lockDevice.Lock() |
| 447 | //defer agent.lockDevice.Unlock() |
| 448 | // Work only on latest data |
| 449 | // TODO: Get list of ports from device directly instead of the entire device |
| 450 | if storeDevice, err := agent.getDeviceWithoutLock(); err != nil { |
| 451 | agent.lockDevice.Unlock() |
| 452 | return status.Errorf(codes.NotFound, "%s", agent.deviceId) |
| 453 | } else { |
| 454 | // clone the device |
| 455 | cloned := proto.Clone(storeDevice).(*voltha.Device) |
| 456 | // Ensure the enums passed in are valid - they will be invalid if they are not set when this function is invoked |
| 457 | if _, ok := voltha.Port_PortType_value[portType.String()]; !ok { |
| 458 | agent.lockDevice.Unlock() |
| 459 | return status.Errorf(codes.InvalidArgument, "%s", portType) |
| 460 | } |
| 461 | for _, port := range cloned.Ports { |
| 462 | if port.Type == portType && port.PortNo == portNo { |
| 463 | port.OperStatus = operStatus |
| 464 | // Set the admin status to ENABLED if the operational status is ACTIVE |
| 465 | // TODO: Set by northbound system? |
| 466 | if operStatus == voltha.OperStatus_ACTIVE { |
| 467 | port.AdminState = voltha.AdminState_ENABLED |
| 468 | } |
| 469 | break |
| 470 | } |
| 471 | } |
| 472 | log.Debugw("portStatusUpdate", log.Fields{"deviceId": cloned.Id}) |
| 473 | // Store the device |
| 474 | if afterUpdate := agent.clusterDataProxy.Update("/devices/"+agent.deviceId, cloned, false, ""); afterUpdate == nil { |
| 475 | agent.lockDevice.Unlock() |
| 476 | return status.Errorf(codes.Internal, "%s", agent.deviceId) |
| 477 | } |
| 478 | agent.lockDevice.Unlock() |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 479 | return nil |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | func (agent *DeviceAgent) updatePmConfigs(pmConfigs *voltha.PmConfigs) error { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 484 | agent.lockDevice.Lock() |
| 485 | defer agent.lockDevice.Unlock() |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 486 | log.Debug("updatePmConfigs") |
| 487 | // Work only on latest data |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 488 | if storeDevice, err := agent.getDeviceWithoutLock(); err != nil { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 489 | return status.Errorf(codes.NotFound, "%s", agent.deviceId) |
| 490 | } else { |
| 491 | // clone the device |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 492 | cloned := proto.Clone(storeDevice).(*voltha.Device) |
| 493 | cloned.PmConfigs = proto.Clone(pmConfigs).(*voltha.PmConfigs) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 494 | // Store the device |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 495 | afterUpdate := agent.clusterDataProxy.Update("/devices/"+agent.deviceId, cloned, false, "") |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 496 | if afterUpdate == nil { |
| 497 | return status.Errorf(codes.Internal, "%s", agent.deviceId) |
| 498 | } |
| 499 | return nil |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | func (agent *DeviceAgent) addPort(port *voltha.Port) error { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 504 | agent.lockDevice.Lock() |
| 505 | defer agent.lockDevice.Unlock() |
| 506 | log.Debugw("addPort", log.Fields{"deviceId": agent.deviceId}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 507 | // Work only on latest data |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 508 | if storeDevice, err := agent.getDeviceWithoutLock(); err != nil { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 509 | return status.Errorf(codes.NotFound, "%s", agent.deviceId) |
| 510 | } else { |
| 511 | // clone the device |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 512 | cloned := proto.Clone(storeDevice).(*voltha.Device) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 513 | if cloned.Ports == nil { |
| 514 | // First port |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 515 | log.Debugw("addPort-first-port-to-add", log.Fields{"deviceId": agent.deviceId}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 516 | cloned.Ports = make([]*voltha.Port, 0) |
| 517 | } |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 518 | cp := proto.Clone(port).(*voltha.Port) |
| 519 | // Set the admin state of the port to ENABLE if the operational state is ACTIVE |
| 520 | // TODO: Set by northbound system? |
| 521 | if cp.OperStatus == voltha.OperStatus_ACTIVE { |
| 522 | cp.AdminState = voltha.AdminState_ENABLED |
| 523 | } |
| 524 | cloned.Ports = append(cloned.Ports, cp) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 525 | // Store the device |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 526 | afterUpdate := agent.clusterDataProxy.Update("/devices/"+agent.deviceId, cloned, false, "") |
| 527 | if afterUpdate == nil { |
| 528 | return status.Errorf(codes.Internal, "%s", agent.deviceId) |
| 529 | } |
| 530 | return nil |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | func (agent *DeviceAgent) addPeerPort(port *voltha.Port_PeerPort) error { |
| 535 | agent.lockDevice.Lock() |
| 536 | defer agent.lockDevice.Unlock() |
| 537 | log.Debug("addPeerPort") |
| 538 | // Work only on latest data |
| 539 | if storeDevice, err := agent.getDeviceWithoutLock(); err != nil { |
| 540 | return status.Errorf(codes.NotFound, "%s", agent.deviceId) |
| 541 | } else { |
| 542 | // clone the device |
| 543 | cloned := proto.Clone(storeDevice).(*voltha.Device) |
| 544 | // Get the peer port on the device based on the port no |
| 545 | for _, peerPort := range cloned.Ports { |
| 546 | if peerPort.PortNo == port.PortNo { // found port |
| 547 | cp := proto.Clone(port).(*voltha.Port_PeerPort) |
| 548 | peerPort.Peers = append(peerPort.Peers, cp) |
| 549 | log.Debugw("found-peer", log.Fields{"portNo": port.PortNo, "deviceId": agent.deviceId}) |
| 550 | break |
| 551 | } |
| 552 | } |
| 553 | // Store the device |
| 554 | afterUpdate := agent.clusterDataProxy.Update("/devices/"+agent.deviceId, cloned, false, "") |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 555 | if afterUpdate == nil { |
| 556 | return status.Errorf(codes.Internal, "%s", agent.deviceId) |
| 557 | } |
| 558 | return nil |
| 559 | } |
| 560 | } |
| 561 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 562 | //flowTableUpdated is the callback after flows have been updated in the model to push them |
| 563 | //to the adapters |
| 564 | func (agent *DeviceAgent) flowTableUpdated(args ...interface{}) interface{} { |
| 565 | log.Debugw("flowTableUpdated-callback", log.Fields{"argsLen": len(args)}) |
| 566 | |
| 567 | agent.lockDevice.Lock() |
| 568 | defer agent.lockDevice.Unlock() |
| 569 | |
| 570 | var previousData *voltha.Flows |
| 571 | var latestData *voltha.Flows |
| 572 | |
| 573 | var ok bool |
| 574 | if previousData, ok = args[0].(*ofp.Flows); !ok { |
| 575 | log.Errorw("invalid-args", log.Fields{"args0": args[0]}) |
| 576 | return nil |
| 577 | } |
| 578 | if latestData, ok = args[1].(*ofp.Flows); !ok { |
| 579 | log.Errorw("invalid-args", log.Fields{"args1": args[1]}) |
| 580 | return nil |
| 581 | } |
| 582 | |
| 583 | // Sanity check - should not happen as this is already handled in logical device agent |
| 584 | if reflect.DeepEqual(previousData.Items, latestData.Items) { |
| 585 | log.Debugw("flow-update-not-required", log.Fields{"previous": previousData.Items, "new": latestData.Items}) |
| 586 | return nil |
| 587 | } |
| 588 | |
| 589 | var device *voltha.Device |
| 590 | var err error |
| 591 | if device, err = agent.getDeviceWithoutLock(); err != nil { |
| 592 | log.Errorw("no-device", log.Fields{"id": agent.deviceId, "error": err}) |
| 593 | return nil |
| 594 | } |
| 595 | groups := device.FlowGroups |
| 596 | |
| 597 | // Send update to adapters |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 598 | // TODO: Check whether the device supports incremental flow changes |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 599 | // Assume false for test |
| 600 | acceptsAddRemoveFlowUpdates := false |
| 601 | if !acceptsAddRemoveFlowUpdates { |
| 602 | if err := agent.adapterProxy.UpdateFlowsBulk(device, latestData, groups); err != nil { |
| 603 | log.Debugw("update-flow-bulk-error", log.Fields{"id": agent.lastData.Id, "error": err}) |
| 604 | return err |
| 605 | } |
| 606 | return nil |
| 607 | } |
| 608 | // Incremental flow changes accepted |
| 609 | var toAdd []*ofp.OfpFlowStats |
| 610 | var toDelete []*ofp.OfpFlowStats |
| 611 | |
| 612 | for _, flow := range latestData.Items { |
| 613 | if fu.FindFlowById(previousData.Items, flow) == -1 { // did not exist before |
| 614 | toAdd = append(toAdd, flow) |
| 615 | } |
| 616 | } |
| 617 | for _, flow := range previousData.Items { |
| 618 | if fu.FindFlowById(latestData.Items, flow) == -1 { // does not exist now |
| 619 | toDelete = append(toDelete, flow) |
| 620 | } |
| 621 | } |
| 622 | flowChanges := &ofp.FlowChanges{ |
| 623 | ToAdd: &voltha.Flows{Items: toAdd}, |
| 624 | ToRemove: &voltha.Flows{Items: toDelete}, |
| 625 | } |
| 626 | // Send an empty group changes as it would be dealt with a call to groupTableUpdated |
| 627 | groupChanges := &ofp.FlowGroupChanges{} |
| 628 | |
| 629 | // Send changes only |
| 630 | if err := agent.adapterProxy.UpdateFlowsIncremental(device, flowChanges, groupChanges); err != nil { |
| 631 | log.Debugw("update-flow-bulk-error", log.Fields{"id": agent.lastData.Id, "error": err}) |
| 632 | return err |
| 633 | } |
| 634 | |
| 635 | return nil |
| 636 | } |
| 637 | |
| 638 | //groupTableUpdated is the callback after group table has been updated in the model to push them |
| 639 | //to the adapters |
| 640 | func (agent *DeviceAgent) groupTableUpdated(args ...interface{}) interface{} { |
| 641 | log.Debugw("groupTableUpdated-callback", log.Fields{"argsLen": len(args)}) |
| 642 | |
| 643 | agent.lockDevice.Lock() |
| 644 | defer agent.lockDevice.Unlock() |
| 645 | |
| 646 | var previousData *voltha.FlowGroups |
| 647 | var latestData *voltha.FlowGroups |
| 648 | |
| 649 | var ok bool |
| 650 | if previousData, ok = args[0].(*ofp.FlowGroups); !ok { |
| 651 | log.Errorw("invalid-args", log.Fields{"args0": args[0]}) |
| 652 | return nil |
| 653 | } |
| 654 | if latestData, ok = args[1].(*ofp.FlowGroups); !ok { |
| 655 | log.Errorw("invalid-args", log.Fields{"args1": args[1]}) |
| 656 | return nil |
| 657 | } |
| 658 | |
| 659 | // Sanity check - should not happen as this is already handled in logical device agent |
| 660 | if reflect.DeepEqual(previousData.Items, latestData.Items) { |
| 661 | log.Debugw("group-table-update-not-required", log.Fields{"previous": previousData.Items, "new": latestData.Items}) |
| 662 | return nil |
| 663 | } |
| 664 | |
| 665 | var device *voltha.Device |
| 666 | var err error |
| 667 | if device, err = agent.getDeviceWithoutLock(); err != nil { |
| 668 | log.Errorw("no-device", log.Fields{"id": agent.deviceId, "error": err}) |
| 669 | return nil |
| 670 | } |
| 671 | flows := device.Flows |
| 672 | |
| 673 | // Send update to adapters |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 674 | // TODO: Check whether the device supports incremental flow changes |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 675 | // Assume false for test |
| 676 | acceptsAddRemoveFlowUpdates := false |
| 677 | if !acceptsAddRemoveFlowUpdates { |
| 678 | if err := agent.adapterProxy.UpdateFlowsBulk(device, flows, latestData); err != nil { |
| 679 | log.Debugw("update-flows-bulk-error", log.Fields{"id": agent.lastData.Id, "error": err}) |
| 680 | return err |
| 681 | } |
| 682 | return nil |
| 683 | } |
| 684 | |
| 685 | // Incremental group changes accepted |
| 686 | var toAdd []*ofp.OfpGroupEntry |
| 687 | var toDelete []*ofp.OfpGroupEntry |
| 688 | var toUpdate []*ofp.OfpGroupEntry |
| 689 | |
| 690 | for _, group := range latestData.Items { |
| 691 | if idx := fu.FindGroup(previousData.Items, group.Desc.GroupId); idx == -1 { // did not exist before |
| 692 | toAdd = append(toAdd, group) |
| 693 | } else { // existed before |
| 694 | if previousData.Items[idx].String() != group.String() { // there is a change |
| 695 | toUpdate = append(toUpdate, group) |
| 696 | } |
| 697 | } |
| 698 | } |
| 699 | for _, group := range previousData.Items { |
| 700 | if fu.FindGroup(latestData.Items, group.Desc.GroupId) == -1 { // does not exist now |
| 701 | toDelete = append(toDelete, group) |
| 702 | } |
| 703 | } |
| 704 | groupChanges := &ofp.FlowGroupChanges{ |
| 705 | ToAdd: &voltha.FlowGroups{Items: toAdd}, |
| 706 | ToRemove: &voltha.FlowGroups{Items: toDelete}, |
| 707 | ToUpdate: &voltha.FlowGroups{Items: toUpdate}, |
| 708 | } |
| 709 | // Send an empty flow changes as it should have been dealt with a call to flowTableUpdated |
| 710 | flowChanges := &ofp.FlowChanges{} |
| 711 | |
| 712 | // Send changes only |
| 713 | if err := agent.adapterProxy.UpdateFlowsIncremental(device, flowChanges, groupChanges); err != nil { |
| 714 | log.Debugw("update-incremental-group-error", log.Fields{"id": agent.lastData.Id, "error": err}) |
| 715 | return err |
| 716 | } |
| 717 | return nil |
| 718 | } |
| 719 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 720 | // TODO: A generic device update by attribute |
| 721 | func (agent *DeviceAgent) updateDeviceAttribute(name string, value interface{}) { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 722 | agent.lockDevice.Lock() |
| 723 | defer agent.lockDevice.Unlock() |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 724 | if value == nil { |
| 725 | return |
| 726 | } |
| 727 | var storeDevice *voltha.Device |
| 728 | var err error |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 729 | if storeDevice, err = agent.getDeviceWithoutLock(); err != nil { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 730 | return |
| 731 | } |
| 732 | updated := false |
| 733 | s := reflect.ValueOf(storeDevice).Elem() |
| 734 | if s.Kind() == reflect.Struct { |
| 735 | // exported field |
| 736 | f := s.FieldByName(name) |
| 737 | if f.IsValid() && f.CanSet() { |
| 738 | switch f.Kind() { |
| 739 | case reflect.String: |
| 740 | f.SetString(value.(string)) |
| 741 | updated = true |
| 742 | case reflect.Uint32: |
| 743 | f.SetUint(uint64(value.(uint32))) |
| 744 | updated = true |
| 745 | case reflect.Bool: |
| 746 | f.SetBool(value.(bool)) |
| 747 | updated = true |
| 748 | } |
| 749 | } |
| 750 | } |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 751 | log.Debugw("update-field-status", log.Fields{"deviceId": storeDevice.Id, "name": name, "updated": updated}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 752 | // Save the data |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 753 | cloned := proto.Clone(storeDevice).(*voltha.Device) |
| 754 | if afterUpdate := agent.clusterDataProxy.Update("/devices/"+agent.deviceId, cloned, false, ""); afterUpdate == nil { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 755 | log.Warnw("attribute-update-failed", log.Fields{"attribute": name, "value": value}) |
| 756 | } |
| 757 | return |
| 758 | } |