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 | } |
khenaidoo | 7ccedd5 | 2018-12-14 16:48:54 -0500 | [diff] [blame^] | 288 | if device.AdminState != voltha.AdminState_PREPROVISIONED { |
| 289 | // Send the request to an Adapter and wait for a response |
| 290 | if err := agent.adapterProxy.DeleteDevice(ctx, device); err != nil { |
| 291 | log.Debugw("deleteDevice-error", log.Fields{"id": agent.lastData.Id, "error": err}) |
| 292 | agent.lockDevice.Unlock() |
| 293 | return err |
| 294 | } |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 295 | } |
khenaidoo | 7ccedd5 | 2018-12-14 16:48:54 -0500 | [diff] [blame^] | 296 | if removed := agent.clusterDataProxy.Remove("/devices/"+agent.deviceId, ""); removed == nil { |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 297 | agent.lockDevice.Unlock() |
| 298 | return status.Errorf(codes.Internal, "failed-update-device:%s", agent.deviceId) |
| 299 | } |
| 300 | agent.lockDevice.Unlock() |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 301 | } |
| 302 | return nil |
| 303 | } |
| 304 | |
| 305 | // getPorts retrieves the ports information of the device based on the port type. |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 306 | func (agent *DeviceAgent) getPorts(ctx context.Context, portType voltha.Port_PortType) *voltha.Ports { |
| 307 | log.Debugw("getPorts", log.Fields{"id": agent.deviceId, "portType": portType}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 308 | ports := &voltha.Ports{} |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 309 | if device, _ := agent.deviceMgr.GetDevice(agent.deviceId); device != nil { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 310 | for _, port := range device.Ports { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 311 | if port.Type == portType { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 312 | ports.Items = append(ports.Items, port) |
| 313 | } |
| 314 | } |
| 315 | } |
| 316 | return ports |
| 317 | } |
| 318 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 319 | // getSwitchCapability is a helper method that a logical device agent uses to retrieve the switch capability of a |
| 320 | // parent device |
khenaidoo | 7923270 | 2018-12-04 11:00:41 -0500 | [diff] [blame] | 321 | func (agent *DeviceAgent) getSwitchCapability(ctx context.Context) (*ic.SwitchCapability, error) { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 322 | log.Debugw("getSwitchCapability", log.Fields{"deviceId": agent.deviceId}) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 323 | if device, err := agent.deviceMgr.GetDevice(agent.deviceId); device == nil { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 324 | return nil, err |
| 325 | } else { |
khenaidoo | 7923270 | 2018-12-04 11:00:41 -0500 | [diff] [blame] | 326 | var switchCap *ic.SwitchCapability |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 327 | var err error |
| 328 | if switchCap, err = agent.adapterProxy.GetOfpDeviceInfo(ctx, device); err != nil { |
| 329 | log.Debugw("getSwitchCapability-error", log.Fields{"id": device.Id, "error": err}) |
| 330 | return nil, err |
| 331 | } |
| 332 | return switchCap, nil |
| 333 | } |
| 334 | } |
| 335 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 336 | // getPortCapability is a helper method that a logical device agent uses to retrieve the port capability of a |
| 337 | // device |
khenaidoo | 7923270 | 2018-12-04 11:00:41 -0500 | [diff] [blame] | 338 | func (agent *DeviceAgent) getPortCapability(ctx context.Context, portNo uint32) (*ic.PortCapability, error) { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 339 | log.Debugw("getPortCapability", log.Fields{"deviceId": agent.deviceId}) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 340 | if device, err := agent.deviceMgr.GetDevice(agent.deviceId); device == nil { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 341 | return nil, err |
| 342 | } else { |
khenaidoo | 7923270 | 2018-12-04 11:00:41 -0500 | [diff] [blame] | 343 | var portCap *ic.PortCapability |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 344 | var err error |
| 345 | if portCap, err = agent.adapterProxy.GetOfpPortInfo(ctx, device, portNo); err != nil { |
| 346 | log.Debugw("getPortCapability-error", log.Fields{"id": device.Id, "error": err}) |
| 347 | return nil, err |
| 348 | } |
| 349 | return portCap, nil |
| 350 | } |
| 351 | } |
| 352 | |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 353 | func (agent *DeviceAgent) packetOut(outPort uint32, packet *ofp.OfpPacketOut) error { |
| 354 | // Send packet to adapter |
| 355 | if err := agent.adapterProxy.packetOut(agent.deviceType, agent.deviceId, outPort, packet); err != nil { |
| 356 | log.Debugw("packet-out-error", log.Fields{"id": agent.lastData.Id, "error": err}) |
| 357 | return err |
| 358 | } |
| 359 | return nil |
| 360 | } |
| 361 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 362 | // 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] | 363 | func (agent *DeviceAgent) processUpdate(args ...interface{}) interface{} { |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 364 | //// Run this callback in its own go routine |
| 365 | go func(args ...interface{}) interface{} { |
| 366 | var previous *voltha.Device |
| 367 | var current *voltha.Device |
| 368 | var ok bool |
| 369 | if len(args) == 2 { |
| 370 | if previous, ok = args[0].(*voltha.Device); !ok { |
| 371 | log.Errorw("invalid-callback-type", log.Fields{"data": args[0]}) |
| 372 | return nil |
| 373 | } |
| 374 | if current, ok = args[1].(*voltha.Device); !ok { |
| 375 | log.Errorw("invalid-callback-type", log.Fields{"data": args[1]}) |
| 376 | return nil |
| 377 | } |
| 378 | } else { |
| 379 | log.Errorw("too-many-args-in-callback", log.Fields{"len": len(args)}) |
| 380 | return nil |
| 381 | } |
| 382 | // Perform the state transition in it's own go routine |
| 383 | agent.deviceMgr.processTransition(previous, current) |
| 384 | return nil |
| 385 | }(args...) |
| 386 | |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 387 | return nil |
| 388 | } |
| 389 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 390 | func (agent *DeviceAgent) updateDevice(device *voltha.Device) error { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 391 | agent.lockDevice.Lock() |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 392 | defer agent.lockDevice.Unlock() |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 393 | log.Debugw("updateDevice", log.Fields{"deviceId": device.Id}) |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 394 | cloned := proto.Clone(device).(*voltha.Device) |
| 395 | afterUpdate := agent.clusterDataProxy.Update("/devices/"+device.Id, cloned, false, "") |
| 396 | if afterUpdate == nil { |
| 397 | return status.Errorf(codes.Internal, "%s", device.Id) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 398 | } |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 399 | return nil |
| 400 | } |
| 401 | |
| 402 | func (agent *DeviceAgent) updateDeviceWithoutLock(device *voltha.Device) error { |
| 403 | log.Debugw("updateDevice", log.Fields{"deviceId": device.Id}) |
| 404 | cloned := proto.Clone(device).(*voltha.Device) |
| 405 | afterUpdate := agent.clusterDataProxy.Update("/devices/"+device.Id, cloned, false, "") |
| 406 | if afterUpdate == nil { |
| 407 | return status.Errorf(codes.Internal, "%s", device.Id) |
| 408 | } |
| 409 | return nil |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 410 | } |
| 411 | |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 412 | func (agent *DeviceAgent) updateDeviceStatus(operStatus voltha.OperStatus_OperStatus, connStatus voltha.ConnectStatus_ConnectStatus) error { |
| 413 | agent.lockDevice.Lock() |
| 414 | //defer agent.lockDevice.Unlock() |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 415 | // Work only on latest data |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 416 | if storeDevice, err := agent.getDeviceWithoutLock(); err != nil { |
| 417 | agent.lockDevice.Unlock() |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 418 | return status.Errorf(codes.NotFound, "%s", agent.deviceId) |
| 419 | } else { |
| 420 | // clone the device |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 421 | cloned := proto.Clone(storeDevice).(*voltha.Device) |
| 422 | // Ensure the enums passed in are valid - they will be invalid if they are not set when this function is invoked |
| 423 | if s, ok := voltha.ConnectStatus_ConnectStatus_value[connStatus.String()]; ok { |
| 424 | log.Debugw("updateDeviceStatus-conn", log.Fields{"ok": ok, "val": s}) |
| 425 | cloned.ConnectStatus = connStatus |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 426 | } |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 427 | if s, ok := voltha.OperStatus_OperStatus_value[operStatus.String()]; ok { |
| 428 | log.Debugw("updateDeviceStatus-oper", log.Fields{"ok": ok, "val": s}) |
| 429 | cloned.OperStatus = operStatus |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 430 | } |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 431 | 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] | 432 | // Store the device |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 433 | if afterUpdate := agent.clusterDataProxy.Update("/devices/"+agent.deviceId, cloned, false, ""); afterUpdate == nil { |
| 434 | agent.lockDevice.Unlock() |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 435 | return status.Errorf(codes.Internal, "%s", agent.deviceId) |
| 436 | } |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 437 | agent.lockDevice.Unlock() |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 438 | return nil |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | func (agent *DeviceAgent) updatePortState(portType voltha.Port_PortType, portNo uint32, operStatus voltha.OperStatus_OperStatus) error { |
| 443 | agent.lockDevice.Lock() |
| 444 | //defer agent.lockDevice.Unlock() |
| 445 | // Work only on latest data |
| 446 | // TODO: Get list of ports from device directly instead of the entire device |
| 447 | if storeDevice, err := agent.getDeviceWithoutLock(); err != nil { |
| 448 | agent.lockDevice.Unlock() |
| 449 | return status.Errorf(codes.NotFound, "%s", agent.deviceId) |
| 450 | } else { |
| 451 | // clone the device |
| 452 | cloned := proto.Clone(storeDevice).(*voltha.Device) |
| 453 | // Ensure the enums passed in are valid - they will be invalid if they are not set when this function is invoked |
| 454 | if _, ok := voltha.Port_PortType_value[portType.String()]; !ok { |
| 455 | agent.lockDevice.Unlock() |
| 456 | return status.Errorf(codes.InvalidArgument, "%s", portType) |
| 457 | } |
| 458 | for _, port := range cloned.Ports { |
| 459 | if port.Type == portType && port.PortNo == portNo { |
| 460 | port.OperStatus = operStatus |
| 461 | // Set the admin status to ENABLED if the operational status is ACTIVE |
| 462 | // TODO: Set by northbound system? |
| 463 | if operStatus == voltha.OperStatus_ACTIVE { |
| 464 | port.AdminState = voltha.AdminState_ENABLED |
| 465 | } |
| 466 | break |
| 467 | } |
| 468 | } |
| 469 | log.Debugw("portStatusUpdate", log.Fields{"deviceId": cloned.Id}) |
| 470 | // Store the device |
| 471 | if afterUpdate := agent.clusterDataProxy.Update("/devices/"+agent.deviceId, cloned, false, ""); afterUpdate == nil { |
| 472 | agent.lockDevice.Unlock() |
| 473 | return status.Errorf(codes.Internal, "%s", agent.deviceId) |
| 474 | } |
| 475 | agent.lockDevice.Unlock() |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 476 | return nil |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | func (agent *DeviceAgent) updatePmConfigs(pmConfigs *voltha.PmConfigs) error { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 481 | agent.lockDevice.Lock() |
| 482 | defer agent.lockDevice.Unlock() |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 483 | log.Debug("updatePmConfigs") |
| 484 | // Work only on latest data |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 485 | if storeDevice, err := agent.getDeviceWithoutLock(); err != nil { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 486 | return status.Errorf(codes.NotFound, "%s", agent.deviceId) |
| 487 | } else { |
| 488 | // clone the device |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 489 | cloned := proto.Clone(storeDevice).(*voltha.Device) |
| 490 | cloned.PmConfigs = proto.Clone(pmConfigs).(*voltha.PmConfigs) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 491 | // Store the device |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 492 | afterUpdate := agent.clusterDataProxy.Update("/devices/"+agent.deviceId, cloned, false, "") |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 493 | if afterUpdate == nil { |
| 494 | return status.Errorf(codes.Internal, "%s", agent.deviceId) |
| 495 | } |
| 496 | return nil |
| 497 | } |
| 498 | } |
| 499 | |
| 500 | func (agent *DeviceAgent) addPort(port *voltha.Port) error { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 501 | agent.lockDevice.Lock() |
| 502 | defer agent.lockDevice.Unlock() |
| 503 | log.Debugw("addPort", log.Fields{"deviceId": agent.deviceId}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 504 | // Work only on latest data |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 505 | if storeDevice, err := agent.getDeviceWithoutLock(); err != nil { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 506 | return status.Errorf(codes.NotFound, "%s", agent.deviceId) |
| 507 | } else { |
| 508 | // clone the device |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 509 | cloned := proto.Clone(storeDevice).(*voltha.Device) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 510 | if cloned.Ports == nil { |
| 511 | // First port |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 512 | log.Debugw("addPort-first-port-to-add", log.Fields{"deviceId": agent.deviceId}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 513 | cloned.Ports = make([]*voltha.Port, 0) |
| 514 | } |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 515 | cp := proto.Clone(port).(*voltha.Port) |
| 516 | // Set the admin state of the port to ENABLE if the operational state is ACTIVE |
| 517 | // TODO: Set by northbound system? |
| 518 | if cp.OperStatus == voltha.OperStatus_ACTIVE { |
| 519 | cp.AdminState = voltha.AdminState_ENABLED |
| 520 | } |
| 521 | cloned.Ports = append(cloned.Ports, cp) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 522 | // Store the device |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 523 | afterUpdate := agent.clusterDataProxy.Update("/devices/"+agent.deviceId, cloned, false, "") |
| 524 | if afterUpdate == nil { |
| 525 | return status.Errorf(codes.Internal, "%s", agent.deviceId) |
| 526 | } |
| 527 | return nil |
| 528 | } |
| 529 | } |
| 530 | |
| 531 | func (agent *DeviceAgent) addPeerPort(port *voltha.Port_PeerPort) error { |
| 532 | agent.lockDevice.Lock() |
| 533 | defer agent.lockDevice.Unlock() |
| 534 | log.Debug("addPeerPort") |
| 535 | // Work only on latest data |
| 536 | if storeDevice, err := agent.getDeviceWithoutLock(); err != nil { |
| 537 | return status.Errorf(codes.NotFound, "%s", agent.deviceId) |
| 538 | } else { |
| 539 | // clone the device |
| 540 | cloned := proto.Clone(storeDevice).(*voltha.Device) |
| 541 | // Get the peer port on the device based on the port no |
| 542 | for _, peerPort := range cloned.Ports { |
| 543 | if peerPort.PortNo == port.PortNo { // found port |
| 544 | cp := proto.Clone(port).(*voltha.Port_PeerPort) |
| 545 | peerPort.Peers = append(peerPort.Peers, cp) |
| 546 | log.Debugw("found-peer", log.Fields{"portNo": port.PortNo, "deviceId": agent.deviceId}) |
| 547 | break |
| 548 | } |
| 549 | } |
| 550 | // Store the device |
| 551 | afterUpdate := agent.clusterDataProxy.Update("/devices/"+agent.deviceId, cloned, false, "") |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 552 | if afterUpdate == nil { |
| 553 | return status.Errorf(codes.Internal, "%s", agent.deviceId) |
| 554 | } |
| 555 | return nil |
| 556 | } |
| 557 | } |
| 558 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 559 | //flowTableUpdated is the callback after flows have been updated in the model to push them |
| 560 | //to the adapters |
| 561 | func (agent *DeviceAgent) flowTableUpdated(args ...interface{}) interface{} { |
| 562 | log.Debugw("flowTableUpdated-callback", log.Fields{"argsLen": len(args)}) |
| 563 | |
| 564 | agent.lockDevice.Lock() |
| 565 | defer agent.lockDevice.Unlock() |
| 566 | |
| 567 | var previousData *voltha.Flows |
| 568 | var latestData *voltha.Flows |
| 569 | |
| 570 | var ok bool |
| 571 | if previousData, ok = args[0].(*ofp.Flows); !ok { |
| 572 | log.Errorw("invalid-args", log.Fields{"args0": args[0]}) |
| 573 | return nil |
| 574 | } |
| 575 | if latestData, ok = args[1].(*ofp.Flows); !ok { |
| 576 | log.Errorw("invalid-args", log.Fields{"args1": args[1]}) |
| 577 | return nil |
| 578 | } |
| 579 | |
| 580 | // Sanity check - should not happen as this is already handled in logical device agent |
| 581 | if reflect.DeepEqual(previousData.Items, latestData.Items) { |
| 582 | log.Debugw("flow-update-not-required", log.Fields{"previous": previousData.Items, "new": latestData.Items}) |
| 583 | return nil |
| 584 | } |
| 585 | |
| 586 | var device *voltha.Device |
| 587 | var err error |
| 588 | if device, err = agent.getDeviceWithoutLock(); err != nil { |
| 589 | log.Errorw("no-device", log.Fields{"id": agent.deviceId, "error": err}) |
| 590 | return nil |
| 591 | } |
| 592 | groups := device.FlowGroups |
| 593 | |
| 594 | // Send update to adapters |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 595 | // TODO: Check whether the device supports incremental flow changes |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 596 | // Assume false for test |
| 597 | acceptsAddRemoveFlowUpdates := false |
| 598 | if !acceptsAddRemoveFlowUpdates { |
| 599 | if err := agent.adapterProxy.UpdateFlowsBulk(device, latestData, groups); err != nil { |
| 600 | log.Debugw("update-flow-bulk-error", log.Fields{"id": agent.lastData.Id, "error": err}) |
| 601 | return err |
| 602 | } |
| 603 | return nil |
| 604 | } |
| 605 | // Incremental flow changes accepted |
| 606 | var toAdd []*ofp.OfpFlowStats |
| 607 | var toDelete []*ofp.OfpFlowStats |
| 608 | |
| 609 | for _, flow := range latestData.Items { |
| 610 | if fu.FindFlowById(previousData.Items, flow) == -1 { // did not exist before |
| 611 | toAdd = append(toAdd, flow) |
| 612 | } |
| 613 | } |
| 614 | for _, flow := range previousData.Items { |
| 615 | if fu.FindFlowById(latestData.Items, flow) == -1 { // does not exist now |
| 616 | toDelete = append(toDelete, flow) |
| 617 | } |
| 618 | } |
| 619 | flowChanges := &ofp.FlowChanges{ |
| 620 | ToAdd: &voltha.Flows{Items: toAdd}, |
| 621 | ToRemove: &voltha.Flows{Items: toDelete}, |
| 622 | } |
| 623 | // Send an empty group changes as it would be dealt with a call to groupTableUpdated |
| 624 | groupChanges := &ofp.FlowGroupChanges{} |
| 625 | |
| 626 | // Send changes only |
| 627 | if err := agent.adapterProxy.UpdateFlowsIncremental(device, flowChanges, groupChanges); err != nil { |
| 628 | log.Debugw("update-flow-bulk-error", log.Fields{"id": agent.lastData.Id, "error": err}) |
| 629 | return err |
| 630 | } |
| 631 | |
| 632 | return nil |
| 633 | } |
| 634 | |
| 635 | //groupTableUpdated is the callback after group table has been updated in the model to push them |
| 636 | //to the adapters |
| 637 | func (agent *DeviceAgent) groupTableUpdated(args ...interface{}) interface{} { |
| 638 | log.Debugw("groupTableUpdated-callback", log.Fields{"argsLen": len(args)}) |
| 639 | |
| 640 | agent.lockDevice.Lock() |
| 641 | defer agent.lockDevice.Unlock() |
| 642 | |
| 643 | var previousData *voltha.FlowGroups |
| 644 | var latestData *voltha.FlowGroups |
| 645 | |
| 646 | var ok bool |
| 647 | if previousData, ok = args[0].(*ofp.FlowGroups); !ok { |
| 648 | log.Errorw("invalid-args", log.Fields{"args0": args[0]}) |
| 649 | return nil |
| 650 | } |
| 651 | if latestData, ok = args[1].(*ofp.FlowGroups); !ok { |
| 652 | log.Errorw("invalid-args", log.Fields{"args1": args[1]}) |
| 653 | return nil |
| 654 | } |
| 655 | |
| 656 | // Sanity check - should not happen as this is already handled in logical device agent |
| 657 | if reflect.DeepEqual(previousData.Items, latestData.Items) { |
| 658 | log.Debugw("group-table-update-not-required", log.Fields{"previous": previousData.Items, "new": latestData.Items}) |
| 659 | return nil |
| 660 | } |
| 661 | |
| 662 | var device *voltha.Device |
| 663 | var err error |
| 664 | if device, err = agent.getDeviceWithoutLock(); err != nil { |
| 665 | log.Errorw("no-device", log.Fields{"id": agent.deviceId, "error": err}) |
| 666 | return nil |
| 667 | } |
| 668 | flows := device.Flows |
| 669 | |
| 670 | // Send update to adapters |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 671 | // TODO: Check whether the device supports incremental flow changes |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 672 | // Assume false for test |
| 673 | acceptsAddRemoveFlowUpdates := false |
| 674 | if !acceptsAddRemoveFlowUpdates { |
| 675 | if err := agent.adapterProxy.UpdateFlowsBulk(device, flows, latestData); err != nil { |
| 676 | log.Debugw("update-flows-bulk-error", log.Fields{"id": agent.lastData.Id, "error": err}) |
| 677 | return err |
| 678 | } |
| 679 | return nil |
| 680 | } |
| 681 | |
| 682 | // Incremental group changes accepted |
| 683 | var toAdd []*ofp.OfpGroupEntry |
| 684 | var toDelete []*ofp.OfpGroupEntry |
| 685 | var toUpdate []*ofp.OfpGroupEntry |
| 686 | |
| 687 | for _, group := range latestData.Items { |
| 688 | if idx := fu.FindGroup(previousData.Items, group.Desc.GroupId); idx == -1 { // did not exist before |
| 689 | toAdd = append(toAdd, group) |
| 690 | } else { // existed before |
| 691 | if previousData.Items[idx].String() != group.String() { // there is a change |
| 692 | toUpdate = append(toUpdate, group) |
| 693 | } |
| 694 | } |
| 695 | } |
| 696 | for _, group := range previousData.Items { |
| 697 | if fu.FindGroup(latestData.Items, group.Desc.GroupId) == -1 { // does not exist now |
| 698 | toDelete = append(toDelete, group) |
| 699 | } |
| 700 | } |
| 701 | groupChanges := &ofp.FlowGroupChanges{ |
| 702 | ToAdd: &voltha.FlowGroups{Items: toAdd}, |
| 703 | ToRemove: &voltha.FlowGroups{Items: toDelete}, |
| 704 | ToUpdate: &voltha.FlowGroups{Items: toUpdate}, |
| 705 | } |
| 706 | // Send an empty flow changes as it should have been dealt with a call to flowTableUpdated |
| 707 | flowChanges := &ofp.FlowChanges{} |
| 708 | |
| 709 | // Send changes only |
| 710 | if err := agent.adapterProxy.UpdateFlowsIncremental(device, flowChanges, groupChanges); err != nil { |
| 711 | log.Debugw("update-incremental-group-error", log.Fields{"id": agent.lastData.Id, "error": err}) |
| 712 | return err |
| 713 | } |
| 714 | return nil |
| 715 | } |
| 716 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 717 | // TODO: A generic device update by attribute |
| 718 | func (agent *DeviceAgent) updateDeviceAttribute(name string, value interface{}) { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 719 | agent.lockDevice.Lock() |
| 720 | defer agent.lockDevice.Unlock() |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 721 | if value == nil { |
| 722 | return |
| 723 | } |
| 724 | var storeDevice *voltha.Device |
| 725 | var err error |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 726 | if storeDevice, err = agent.getDeviceWithoutLock(); err != nil { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 727 | return |
| 728 | } |
| 729 | updated := false |
| 730 | s := reflect.ValueOf(storeDevice).Elem() |
| 731 | if s.Kind() == reflect.Struct { |
| 732 | // exported field |
| 733 | f := s.FieldByName(name) |
| 734 | if f.IsValid() && f.CanSet() { |
| 735 | switch f.Kind() { |
| 736 | case reflect.String: |
| 737 | f.SetString(value.(string)) |
| 738 | updated = true |
| 739 | case reflect.Uint32: |
| 740 | f.SetUint(uint64(value.(uint32))) |
| 741 | updated = true |
| 742 | case reflect.Bool: |
| 743 | f.SetBool(value.(bool)) |
| 744 | updated = true |
| 745 | } |
| 746 | } |
| 747 | } |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 748 | 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] | 749 | // Save the data |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 750 | cloned := proto.Clone(storeDevice).(*voltha.Device) |
| 751 | if afterUpdate := agent.clusterDataProxy.Update("/devices/"+agent.deviceId, cloned, false, ""); afterUpdate == nil { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 752 | log.Warnw("attribute-update-failed", log.Fields{"attribute": name, "value": value}) |
| 753 | } |
| 754 | return |
| 755 | } |