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 |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 39 | adapterMgr *AdapterManager |
khenaidoo | 9a46896 | 2018-09-19 15:33:13 -0400 | [diff] [blame] | 40 | deviceMgr *DeviceManager |
| 41 | clusterDataProxy *model.Proxy |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 42 | deviceProxy *model.Proxy |
khenaidoo | 9a46896 | 2018-09-19 15:33:13 -0400 | [diff] [blame] | 43 | exitChannel chan int |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 44 | flowProxy *model.Proxy |
| 45 | groupProxy *model.Proxy |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 46 | lockDevice sync.RWMutex |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 47 | } |
| 48 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 49 | //newDeviceAgent creates a new device agent along as creating a unique ID for the device and set the device state to |
| 50 | //preprovisioning |
khenaidoo | 9a46896 | 2018-09-19 15:33:13 -0400 | [diff] [blame] | 51 | func newDeviceAgent(ap *AdapterProxy, device *voltha.Device, deviceMgr *DeviceManager, cdProxy *model.Proxy) *DeviceAgent { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 52 | var agent DeviceAgent |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 53 | agent.adapterProxy = ap |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 54 | cloned := (proto.Clone(device)).(*voltha.Device) |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 55 | if cloned.Id == "" { |
| 56 | cloned.Id = CreateDeviceId() |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 57 | cloned.AdminState = voltha.AdminState_PREPROVISIONED |
| 58 | cloned.FlowGroups = &ofp.FlowGroups{Items: nil} |
| 59 | cloned.Flows = &ofp.Flows{Items: nil} |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 60 | } |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 61 | if !device.GetRoot() && device.ProxyAddress != nil { |
| 62 | // Set the default vlan ID to the one specified by the parent adapter. It can be |
| 63 | // overwritten by the child adapter during a device update request |
| 64 | cloned.Vlan = device.ProxyAddress.ChannelId |
| 65 | } |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 66 | agent.deviceId = cloned.Id |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 67 | agent.deviceType = cloned.Type |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 68 | agent.lastData = cloned |
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 | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 74 | return &agent |
| 75 | } |
| 76 | |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 77 | // start save the device to the data model and registers for callbacks on that device if loadFromdB is false. Otherwise, |
| 78 | // it will load the data from the dB and setup teh necessary callbacks and proxies. |
| 79 | func (agent *DeviceAgent) start(ctx context.Context, loadFromdB bool) error { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 80 | agent.lockDevice.Lock() |
| 81 | defer agent.lockDevice.Unlock() |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 82 | log.Debugw("starting-device-agent", log.Fields{"deviceId": agent.deviceId}) |
| 83 | if loadFromdB { |
| 84 | if device := agent.clusterDataProxy.Get("/devices/"+agent.deviceId, 1, false, ""); device != nil { |
| 85 | if d, ok := device.(*voltha.Device); ok { |
| 86 | agent.lastData = proto.Clone(d).(*voltha.Device) |
khenaidoo | 6d05513 | 2019-02-12 16:51:19 -0500 | [diff] [blame] | 87 | agent.deviceType = agent.lastData.Adapter |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 88 | } |
| 89 | } else { |
| 90 | log.Errorw("failed-to-load-device", log.Fields{"deviceId": agent.deviceId}) |
| 91 | return status.Errorf(codes.NotFound, "device-%s", agent.deviceId) |
| 92 | } |
| 93 | log.Debugw("device-loaded-from-dB", log.Fields{"device": agent.lastData}) |
| 94 | } else { |
| 95 | // Add the initial device to the local model |
| 96 | if added := agent.clusterDataProxy.AddWithID("/devices", agent.deviceId, agent.lastData, ""); added == nil { |
| 97 | log.Errorw("failed-to-add-device", log.Fields{"deviceId": agent.deviceId}) |
| 98 | } |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 99 | } |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 100 | |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 101 | agent.deviceProxy = agent.clusterDataProxy.Root.CreateProxy("/devices/"+agent.deviceId, false) |
| 102 | agent.deviceProxy.RegisterCallback(model.POST_UPDATE, agent.processUpdate) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 103 | |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 104 | agent.flowProxy = agent.clusterDataProxy.Root.CreateProxy( |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 105 | fmt.Sprintf("/devices/%s/flows", agent.deviceId), |
| 106 | false) |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 107 | agent.groupProxy = agent.clusterDataProxy.Root.CreateProxy( |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 108 | fmt.Sprintf("/devices/%s/flow_groups", agent.deviceId), |
| 109 | false) |
| 110 | |
| 111 | agent.flowProxy.RegisterCallback(model.POST_UPDATE, agent.flowTableUpdated) |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 112 | agent.groupProxy.RegisterCallback(model.POST_UPDATE, agent.groupTableUpdated) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 113 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 114 | log.Debug("device-agent-started") |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 115 | return nil |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 116 | } |
| 117 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 118 | // stop stops the device agent. Not much to do for now |
| 119 | func (agent *DeviceAgent) stop(ctx context.Context) { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 120 | agent.lockDevice.Lock() |
| 121 | defer agent.lockDevice.Unlock() |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 122 | log.Debug("stopping-device-agent") |
| 123 | agent.exitChannel <- 1 |
| 124 | log.Debug("device-agent-stopped") |
| 125 | } |
| 126 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 127 | // GetDevice retrieves the latest device information from the data model |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 128 | func (agent *DeviceAgent) getDevice() (*voltha.Device, error) { |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame^] | 129 | agent.lockDevice.RLock() |
| 130 | defer agent.lockDevice.RUnlock() |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 131 | if device := agent.clusterDataProxy.Get("/devices/"+agent.deviceId, 0, false, ""); device != nil { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 132 | if d, ok := device.(*voltha.Device); ok { |
| 133 | cloned := proto.Clone(d).(*voltha.Device) |
| 134 | return cloned, nil |
| 135 | } |
| 136 | } |
| 137 | return nil, status.Errorf(codes.NotFound, "device-%s", agent.deviceId) |
| 138 | } |
| 139 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 140 | // 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] | 141 | // This function is meant so that we do not have duplicate code all over the device agent functions |
| 142 | func (agent *DeviceAgent) getDeviceWithoutLock() (*voltha.Device, error) { |
Stephane Barbarie | 260a563 | 2019-02-26 16:12:49 -0500 | [diff] [blame] | 143 | if device := agent.clusterDataProxy.Get("/devices/"+agent.deviceId, 0, true, ""); device != nil { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 144 | if d, ok := device.(*voltha.Device); ok { |
| 145 | cloned := proto.Clone(d).(*voltha.Device) |
| 146 | return cloned, nil |
| 147 | } |
| 148 | } |
| 149 | return nil, status.Errorf(codes.NotFound, "device-%s", agent.deviceId) |
| 150 | } |
| 151 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 152 | // enableDevice activates a preprovisioned or disable device |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 153 | func (agent *DeviceAgent) enableDevice(ctx context.Context) error { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 154 | agent.lockDevice.Lock() |
| 155 | defer agent.lockDevice.Unlock() |
| 156 | log.Debugw("enableDevice", log.Fields{"id": agent.deviceId}) |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 157 | |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 158 | if device, err := agent.getDeviceWithoutLock(); err != nil { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 159 | return status.Errorf(codes.NotFound, "%s", agent.deviceId) |
| 160 | } else { |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 161 | // First figure out which adapter will handle this device type. We do it at this stage as allow devices to be |
| 162 | // pre-provisionned with the required adapter not registered. At this stage, since we need to communicate |
| 163 | // with the adapter then we need to know the adapter that will handle this request |
| 164 | if adapterName, err := agent.adapterMgr.getAdapterName(device.Type); err != nil { |
| 165 | log.Warnw("no-adapter-registered-for-device-type", log.Fields{"deviceType": device.Type, "deviceAdapter": device.Adapter}) |
| 166 | return err |
| 167 | } else { |
| 168 | device.Adapter = adapterName |
| 169 | } |
| 170 | |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 171 | if device.AdminState == voltha.AdminState_ENABLED { |
| 172 | log.Debugw("device-already-enabled", log.Fields{"id": agent.deviceId}) |
| 173 | //TODO: Needs customized error message |
| 174 | return nil |
| 175 | } |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 176 | //TODO: if parent device is disabled then do not enable device |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 177 | // Verify whether we need to adopt the device the first time |
| 178 | // TODO: A state machine for these state transitions would be better (we just have to handle |
| 179 | // a limited set of states now or it may be an overkill) |
| 180 | if device.AdminState == voltha.AdminState_PREPROVISIONED { |
| 181 | // First send the request to an Adapter and wait for a response |
| 182 | if err := agent.adapterProxy.AdoptDevice(ctx, device); err != nil { |
| 183 | log.Debugw("adoptDevice-error", log.Fields{"id": agent.lastData.Id, "error": err}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 184 | return err |
| 185 | } |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 186 | } else { |
| 187 | // First send the request to an Adapter and wait for a response |
| 188 | if err := agent.adapterProxy.ReEnableDevice(ctx, device); err != nil { |
| 189 | log.Debugw("renableDevice-error", log.Fields{"id": agent.lastData.Id, "error": err}) |
| 190 | return err |
| 191 | } |
| 192 | } |
| 193 | // Received an Ack (no error found above). Now update the device in the model to the expected state |
| 194 | cloned := proto.Clone(device).(*voltha.Device) |
| 195 | cloned.AdminState = voltha.AdminState_ENABLED |
| 196 | cloned.OperStatus = voltha.OperStatus_ACTIVATING |
| 197 | if afterUpdate := agent.clusterDataProxy.Update("/devices/"+agent.deviceId, cloned, false, ""); afterUpdate == nil { |
| 198 | return status.Errorf(codes.Internal, "failed-update-device:%s", agent.deviceId) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 199 | } |
| 200 | } |
| 201 | return nil |
| 202 | } |
| 203 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 204 | func (agent *DeviceAgent) updateFlows(flows []*ofp.OfpFlowStats) error { |
| 205 | agent.lockDevice.Lock() |
| 206 | defer agent.lockDevice.Unlock() |
| 207 | log.Debugw("updateFlows", log.Fields{"deviceId": agent.deviceId, "flows": flows}) |
| 208 | var oldData *voltha.Flows |
| 209 | if storedData, err := agent.getDeviceWithoutLock(); err != nil { |
| 210 | return status.Errorf(codes.NotFound, "%s", agent.deviceId) |
| 211 | } else { |
| 212 | oldData = proto.Clone(storedData.Flows).(*voltha.Flows) |
| 213 | log.Debugw("updateFlows", log.Fields{"deviceId": agent.deviceId, "flows": flows, "old": oldData}) |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 214 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 215 | // store the changed data |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 216 | afterUpdate := agent.flowProxy.Update("/", &ofp.Flows{Items: flows}, false, "") |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 217 | if afterUpdate == nil { |
| 218 | return status.Errorf(codes.Internal, "%s", agent.deviceId) |
| 219 | } |
| 220 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 221 | return nil |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | func (agent *DeviceAgent) updateGroups(groups []*ofp.OfpGroupEntry) error { |
| 226 | agent.lockDevice.Lock() |
| 227 | defer agent.lockDevice.Unlock() |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 228 | log.Debugw("updateGroups", log.Fields{"deviceId": agent.deviceId, "groups": groups}) |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 229 | if _, err := agent.getDeviceWithoutLock(); err != nil { |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 230 | return status.Errorf(codes.NotFound, "%s", agent.deviceId) |
| 231 | } else { |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 232 | // store the changed data |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 233 | afterUpdate := agent.groupProxy.Update("/", &ofp.FlowGroups{Items: groups}, false, "") |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 234 | if afterUpdate == nil { |
| 235 | return status.Errorf(codes.Internal, "%s", agent.deviceId) |
| 236 | } |
| 237 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 238 | return nil |
| 239 | } |
| 240 | } |
| 241 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 242 | //disableDevice disable a device |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 243 | func (agent *DeviceAgent) disableDevice(ctx context.Context) error { |
| 244 | agent.lockDevice.Lock() |
| 245 | //defer agent.lockDevice.Unlock() |
| 246 | log.Debugw("disableDevice", log.Fields{"id": agent.deviceId}) |
| 247 | // Get the most up to date the device info |
| 248 | if device, err := agent.getDeviceWithoutLock(); err != nil { |
| 249 | return status.Errorf(codes.NotFound, "%s", agent.deviceId) |
| 250 | } else { |
| 251 | if device.AdminState == voltha.AdminState_DISABLED { |
| 252 | log.Debugw("device-already-disabled", log.Fields{"id": agent.deviceId}) |
| 253 | //TODO: Needs customized error message |
| 254 | agent.lockDevice.Unlock() |
| 255 | return nil |
| 256 | } |
| 257 | // First send the request to an Adapter and wait for a response |
| 258 | if err := agent.adapterProxy.DisableDevice(ctx, device); err != nil { |
| 259 | log.Debugw("disableDevice-error", log.Fields{"id": agent.lastData.Id, "error": err}) |
| 260 | agent.lockDevice.Unlock() |
| 261 | return err |
| 262 | } |
| 263 | // Received an Ack (no error found above). Now update the device in the model to the expected state |
| 264 | cloned := proto.Clone(device).(*voltha.Device) |
| 265 | cloned.AdminState = voltha.AdminState_DISABLED |
| 266 | // Set the state of all ports on that device to disable |
| 267 | for _, port := range cloned.Ports { |
| 268 | port.AdminState = voltha.AdminState_DISABLED |
| 269 | port.OperStatus = voltha.OperStatus_UNKNOWN |
| 270 | } |
| 271 | if afterUpdate := agent.clusterDataProxy.Update("/devices/"+agent.deviceId, cloned, false, ""); afterUpdate == nil { |
| 272 | agent.lockDevice.Unlock() |
| 273 | return status.Errorf(codes.Internal, "failed-update-device:%s", agent.deviceId) |
| 274 | } |
| 275 | agent.lockDevice.Unlock() |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 276 | } |
| 277 | return nil |
| 278 | } |
| 279 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 280 | func (agent *DeviceAgent) rebootDevice(ctx context.Context) error { |
| 281 | agent.lockDevice.Lock() |
| 282 | defer agent.lockDevice.Unlock() |
| 283 | log.Debugw("rebootDevice", log.Fields{"id": agent.deviceId}) |
| 284 | // Get the most up to date the device info |
| 285 | if device, err := agent.getDeviceWithoutLock(); err != nil { |
| 286 | return status.Errorf(codes.NotFound, "%s", agent.deviceId) |
| 287 | } else { |
| 288 | if device.AdminState != voltha.AdminState_DISABLED { |
| 289 | log.Debugw("device-not-disabled", log.Fields{"id": agent.deviceId}) |
| 290 | //TODO: Needs customized error message |
| 291 | return status.Errorf(codes.FailedPrecondition, "deviceId:%s, expected-admin-state:%s", agent.deviceId, voltha.AdminState_DISABLED) |
| 292 | } |
| 293 | // First send the request to an Adapter and wait for a response |
| 294 | if err := agent.adapterProxy.RebootDevice(ctx, device); err != nil { |
| 295 | log.Debugw("rebootDevice-error", log.Fields{"id": agent.lastData.Id, "error": err}) |
| 296 | return err |
| 297 | } |
| 298 | } |
| 299 | return nil |
| 300 | } |
| 301 | |
| 302 | func (agent *DeviceAgent) deleteDevice(ctx context.Context) error { |
| 303 | agent.lockDevice.Lock() |
| 304 | log.Debugw("deleteDevice", log.Fields{"id": agent.deviceId}) |
| 305 | // Get the most up to date the device info |
| 306 | if device, err := agent.getDeviceWithoutLock(); err != nil { |
| 307 | agent.lockDevice.Unlock() |
| 308 | return status.Errorf(codes.NotFound, "%s", agent.deviceId) |
| 309 | } else { |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 310 | if (device.AdminState != voltha.AdminState_DISABLED) && |
| 311 | (device.AdminState != voltha.AdminState_PREPROVISIONED) { |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 312 | log.Debugw("device-not-disabled", log.Fields{"id": agent.deviceId}) |
| 313 | //TODO: Needs customized error message |
| 314 | agent.lockDevice.Unlock() |
| 315 | return status.Errorf(codes.FailedPrecondition, "deviceId:%s, expected-admin-state:%s", agent.deviceId, voltha.AdminState_DISABLED) |
| 316 | } |
khenaidoo | 7ccedd5 | 2018-12-14 16:48:54 -0500 | [diff] [blame] | 317 | if device.AdminState != voltha.AdminState_PREPROVISIONED { |
| 318 | // Send the request to an Adapter and wait for a response |
| 319 | if err := agent.adapterProxy.DeleteDevice(ctx, device); err != nil { |
| 320 | log.Debugw("deleteDevice-error", log.Fields{"id": agent.lastData.Id, "error": err}) |
| 321 | agent.lockDevice.Unlock() |
| 322 | return err |
| 323 | } |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 324 | } |
khenaidoo | 7ccedd5 | 2018-12-14 16:48:54 -0500 | [diff] [blame] | 325 | if removed := agent.clusterDataProxy.Remove("/devices/"+agent.deviceId, ""); removed == nil { |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 326 | agent.lockDevice.Unlock() |
| 327 | return status.Errorf(codes.Internal, "failed-update-device:%s", agent.deviceId) |
| 328 | } |
| 329 | agent.lockDevice.Unlock() |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 330 | } |
| 331 | return nil |
| 332 | } |
| 333 | |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 334 | func (agent *DeviceAgent) downloadImage(ctx context.Context, img *voltha.ImageDownload) (*voltha.OperationResp, error) { |
| 335 | agent.lockDevice.Lock() |
| 336 | defer agent.lockDevice.Unlock() |
| 337 | log.Debugw("downloadImage", log.Fields{"id": agent.deviceId}) |
| 338 | // Get the most up to date the device info |
| 339 | if device, err := agent.getDeviceWithoutLock(); err != nil { |
| 340 | return nil, status.Errorf(codes.NotFound, "%s", agent.deviceId) |
| 341 | } else { |
| 342 | if device.AdminState != voltha.AdminState_ENABLED { |
| 343 | log.Debugw("device-not-enabled", log.Fields{"id": agent.deviceId}) |
| 344 | return nil, status.Errorf(codes.FailedPrecondition, "deviceId:%s, expected-admin-state:%s", agent.deviceId, voltha.AdminState_ENABLED) |
| 345 | } |
| 346 | // Save the image |
| 347 | clonedImg := proto.Clone(img).(*voltha.ImageDownload) |
Stephane Barbarie | df5479f | 2019-01-29 22:13:00 -0500 | [diff] [blame] | 348 | clonedImg.DownloadState = voltha.ImageDownload_DOWNLOAD_REQUESTED |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 349 | cloned := proto.Clone(device).(*voltha.Device) |
| 350 | if cloned.ImageDownloads == nil { |
| 351 | cloned.ImageDownloads = []*voltha.ImageDownload{clonedImg} |
| 352 | } else { |
| 353 | cloned.ImageDownloads = append(cloned.ImageDownloads, clonedImg) |
| 354 | } |
| 355 | cloned.AdminState = voltha.AdminState_DOWNLOADING_IMAGE |
| 356 | if afterUpdate := agent.clusterDataProxy.Update("/devices/"+agent.deviceId, cloned, false, ""); afterUpdate == nil { |
| 357 | return nil, status.Errorf(codes.Internal, "failed-update-device:%s", agent.deviceId) |
| 358 | } |
| 359 | // Send the request to the adapter |
| 360 | if err := agent.adapterProxy.DownloadImage(ctx, cloned, clonedImg); err != nil { |
| 361 | log.Debugw("downloadImage-error", log.Fields{"id": agent.lastData.Id, "error": err, "image": img.Name}) |
| 362 | return nil, err |
| 363 | } |
| 364 | } |
| 365 | return &voltha.OperationResp{Code: voltha.OperationResp_OPERATION_SUCCESS}, nil |
| 366 | } |
| 367 | |
| 368 | // isImageRegistered is a helper method to figure out if an image is already registered |
| 369 | func isImageRegistered(img *voltha.ImageDownload, device *voltha.Device) bool { |
| 370 | for _, image := range device.ImageDownloads { |
| 371 | if image.Id == img.Id && image.Name == img.Name { |
| 372 | return true |
| 373 | } |
| 374 | } |
| 375 | return false |
| 376 | } |
| 377 | |
| 378 | func (agent *DeviceAgent) cancelImageDownload(ctx context.Context, img *voltha.ImageDownload) (*voltha.OperationResp, error) { |
| 379 | agent.lockDevice.Lock() |
| 380 | defer agent.lockDevice.Unlock() |
| 381 | log.Debugw("cancelImageDownload", log.Fields{"id": agent.deviceId}) |
| 382 | // Get the most up to date the device info |
| 383 | if device, err := agent.getDeviceWithoutLock(); err != nil { |
| 384 | return nil, status.Errorf(codes.NotFound, "%s", agent.deviceId) |
| 385 | } else { |
| 386 | // Verify whether the Image is in the list of image being downloaded |
| 387 | if !isImageRegistered(img, device) { |
| 388 | return nil, status.Errorf(codes.FailedPrecondition, "deviceId:%s, image-not-registered:%s", agent.deviceId, img.Name) |
| 389 | } |
| 390 | |
| 391 | // Update image download state |
| 392 | cloned := proto.Clone(device).(*voltha.Device) |
| 393 | for _, image := range cloned.ImageDownloads { |
| 394 | if image.Id == img.Id && image.Name == img.Name { |
Stephane Barbarie | df5479f | 2019-01-29 22:13:00 -0500 | [diff] [blame] | 395 | image.DownloadState = voltha.ImageDownload_DOWNLOAD_CANCELLED |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 396 | } |
| 397 | } |
| 398 | |
| 399 | //If device is in downloading state, send the request to cancel the download |
| 400 | if device.AdminState == voltha.AdminState_DOWNLOADING_IMAGE { |
| 401 | if err := agent.adapterProxy.CancelImageDownload(ctx, device, img); err != nil { |
| 402 | log.Debugw("cancelImageDownload-error", log.Fields{"id": agent.lastData.Id, "error": err, "image": img.Name}) |
| 403 | return nil, err |
| 404 | } |
| 405 | // Set the device to Enabled |
| 406 | cloned.AdminState = voltha.AdminState_ENABLED |
| 407 | if afterUpdate := agent.clusterDataProxy.Update("/devices/"+agent.deviceId, cloned, false, ""); afterUpdate == nil { |
| 408 | return nil, status.Errorf(codes.Internal, "failed-update-device:%s", agent.deviceId) |
| 409 | } |
| 410 | } |
| 411 | } |
| 412 | return &voltha.OperationResp{Code: voltha.OperationResp_OPERATION_SUCCESS}, nil |
| 413 | } |
| 414 | |
| 415 | func (agent *DeviceAgent) activateImage(ctx context.Context, img *voltha.ImageDownload) (*voltha.OperationResp, error) { |
| 416 | agent.lockDevice.Lock() |
| 417 | defer agent.lockDevice.Unlock() |
| 418 | log.Debugw("activateImage", log.Fields{"id": agent.deviceId}) |
| 419 | // Get the most up to date the device info |
| 420 | if device, err := agent.getDeviceWithoutLock(); err != nil { |
| 421 | return nil, status.Errorf(codes.NotFound, "%s", agent.deviceId) |
| 422 | } else { |
| 423 | // Verify whether the Image is in the list of image being downloaded |
| 424 | if !isImageRegistered(img, device) { |
| 425 | return nil, status.Errorf(codes.FailedPrecondition, "deviceId:%s, image-not-registered:%s", agent.deviceId, img.Name) |
| 426 | } |
| 427 | |
| 428 | if device.AdminState == voltha.AdminState_DOWNLOADING_IMAGE { |
| 429 | return nil, status.Errorf(codes.FailedPrecondition, "deviceId:%s, device-in-downloading-state:%s", agent.deviceId, img.Name) |
| 430 | } |
| 431 | // Update image download state |
| 432 | cloned := proto.Clone(device).(*voltha.Device) |
| 433 | for _, image := range cloned.ImageDownloads { |
| 434 | if image.Id == img.Id && image.Name == img.Name { |
| 435 | image.ImageState = voltha.ImageDownload_IMAGE_ACTIVATING |
| 436 | } |
| 437 | } |
| 438 | // Set the device to downloading_image |
| 439 | cloned.AdminState = voltha.AdminState_DOWNLOADING_IMAGE |
| 440 | if afterUpdate := agent.clusterDataProxy.Update("/devices/"+agent.deviceId, cloned, false, ""); afterUpdate == nil { |
| 441 | return nil, status.Errorf(codes.Internal, "failed-update-device:%s", agent.deviceId) |
| 442 | } |
| 443 | |
| 444 | if err := agent.adapterProxy.ActivateImageUpdate(ctx, device, img); err != nil { |
| 445 | log.Debugw("activateImage-error", log.Fields{"id": agent.lastData.Id, "error": err, "image": img.Name}) |
| 446 | return nil, err |
| 447 | } |
| 448 | // The status of the AdminState will be changed following the update_download_status response from the adapter |
| 449 | // The image name will also be removed from the device list |
| 450 | } |
| 451 | return &voltha.OperationResp{Code: voltha.OperationResp_OPERATION_SUCCESS}, nil} |
| 452 | |
| 453 | |
| 454 | func (agent *DeviceAgent) revertImage(ctx context.Context, img *voltha.ImageDownload) (*voltha.OperationResp, error) { |
| 455 | agent.lockDevice.Lock() |
| 456 | defer agent.lockDevice.Unlock() |
| 457 | log.Debugw("revertImage", log.Fields{"id": agent.deviceId}) |
| 458 | // Get the most up to date the device info |
| 459 | if device, err := agent.getDeviceWithoutLock(); err != nil { |
| 460 | return nil, status.Errorf(codes.NotFound, "%s", agent.deviceId) |
| 461 | } else { |
| 462 | // Verify whether the Image is in the list of image being downloaded |
| 463 | if !isImageRegistered(img, device) { |
| 464 | return nil, status.Errorf(codes.FailedPrecondition, "deviceId:%s, image-not-registered:%s", agent.deviceId, img.Name) |
| 465 | } |
| 466 | |
| 467 | if device.AdminState != voltha.AdminState_ENABLED { |
| 468 | return nil, status.Errorf(codes.FailedPrecondition, "deviceId:%s, device-not-enabled-state:%s", agent.deviceId, img.Name) |
| 469 | } |
| 470 | // Update image download state |
| 471 | cloned := proto.Clone(device).(*voltha.Device) |
| 472 | for _, image := range cloned.ImageDownloads { |
| 473 | if image.Id == img.Id && image.Name == img.Name { |
| 474 | image.ImageState = voltha.ImageDownload_IMAGE_REVERTING |
| 475 | } |
| 476 | } |
| 477 | if afterUpdate := agent.clusterDataProxy.Update("/devices/"+agent.deviceId, cloned, false, ""); afterUpdate == nil { |
| 478 | return nil, status.Errorf(codes.Internal, "failed-update-device:%s", agent.deviceId) |
| 479 | } |
| 480 | |
| 481 | if err := agent.adapterProxy.RevertImageUpdate(ctx, device, img); err != nil { |
| 482 | log.Debugw("revertImage-error", log.Fields{"id": agent.lastData.Id, "error": err, "image": img.Name}) |
| 483 | return nil, err |
| 484 | } |
| 485 | } |
| 486 | return &voltha.OperationResp{Code: voltha.OperationResp_OPERATION_SUCCESS}, nil |
| 487 | } |
| 488 | |
| 489 | |
| 490 | func (agent *DeviceAgent) getImageDownloadStatus(ctx context.Context, img *voltha.ImageDownload) (*voltha.ImageDownload, error) { |
| 491 | agent.lockDevice.Lock() |
| 492 | defer agent.lockDevice.Unlock() |
| 493 | log.Debugw("getImageDownloadStatus", log.Fields{"id": agent.deviceId}) |
| 494 | // Get the most up to date the device info |
| 495 | if device, err := agent.getDeviceWithoutLock(); err != nil { |
| 496 | return nil, status.Errorf(codes.NotFound, "%s", agent.deviceId) |
| 497 | } else { |
| 498 | if resp, err := agent.adapterProxy.GetImageDownloadStatus(ctx, device, img); err != nil { |
| 499 | log.Debugw("getImageDownloadStatus-error", log.Fields{"id": agent.lastData.Id, "error": err, "image": img.Name}) |
| 500 | return nil, err |
| 501 | } else { |
| 502 | return resp, nil |
| 503 | } |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | func (agent *DeviceAgent) updateImageDownload(img *voltha.ImageDownload) error{ |
| 508 | agent.lockDevice.Lock() |
| 509 | defer agent.lockDevice.Unlock() |
| 510 | log.Debugw("updateImageDownload", log.Fields{"id": agent.deviceId}) |
| 511 | // Get the most up to date the device info |
| 512 | if device, err := agent.getDeviceWithoutLock(); err != nil { |
| 513 | return status.Errorf(codes.NotFound, "%s", agent.deviceId) |
| 514 | } else { |
| 515 | // Update the image as well as remove it if the download was cancelled |
| 516 | cloned := proto.Clone(device).(*voltha.Device) |
| 517 | clonedImages := make([]*voltha.ImageDownload, len(cloned.ImageDownloads)) |
| 518 | for _, image := range cloned.ImageDownloads { |
| 519 | if image.Id == img.Id && image.Name == img.Name { |
Stephane Barbarie | df5479f | 2019-01-29 22:13:00 -0500 | [diff] [blame] | 520 | if image.DownloadState != voltha.ImageDownload_DOWNLOAD_CANCELLED { |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 521 | clonedImages = append(clonedImages, img) |
| 522 | } |
| 523 | } |
| 524 | } |
| 525 | cloned.ImageDownloads = clonedImages |
| 526 | // Set the Admin state to enabled if required |
Stephane Barbarie | df5479f | 2019-01-29 22:13:00 -0500 | [diff] [blame] | 527 | if (img.DownloadState != voltha.ImageDownload_DOWNLOAD_REQUESTED && |
| 528 | img.DownloadState != voltha.ImageDownload_DOWNLOAD_STARTED) || |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 529 | (img.ImageState != voltha.ImageDownload_IMAGE_ACTIVATING){ |
| 530 | cloned.AdminState = voltha.AdminState_ENABLED |
| 531 | } |
| 532 | |
| 533 | if afterUpdate := agent.clusterDataProxy.Update("/devices/"+agent.deviceId, cloned, false, ""); afterUpdate == nil { |
| 534 | return status.Errorf(codes.Internal, "failed-update-device:%s", agent.deviceId) |
| 535 | } |
| 536 | } |
| 537 | return nil |
| 538 | } |
| 539 | |
| 540 | func (agent *DeviceAgent) getImageDownload(ctx context.Context, img *voltha.ImageDownload) (*voltha.ImageDownload, error) { |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame^] | 541 | agent.lockDevice.RLock() |
| 542 | defer agent.lockDevice.RUnlock() |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 543 | log.Debugw("getImageDownload", log.Fields{"id": agent.deviceId}) |
| 544 | // Get the most up to date the device info |
| 545 | if device, err := agent.getDeviceWithoutLock(); err != nil { |
| 546 | return nil, status.Errorf(codes.NotFound, "%s", agent.deviceId) |
| 547 | } else { |
| 548 | for _, image := range device.ImageDownloads { |
| 549 | if image.Id == img.Id && image.Name == img.Name { |
| 550 | return image, nil |
| 551 | } |
| 552 | } |
| 553 | return nil, status.Errorf(codes.NotFound, "image-not-found:%s", img.Name) |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | func (agent *DeviceAgent) listImageDownloads(ctx context.Context, deviceId string) (*voltha.ImageDownloads, error) { |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame^] | 558 | agent.lockDevice.RLock() |
| 559 | defer agent.lockDevice.RUnlock() |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 560 | log.Debugw("listImageDownloads", log.Fields{"id": agent.deviceId}) |
| 561 | // Get the most up to date the device info |
| 562 | if device, err := agent.getDeviceWithoutLock(); err != nil { |
| 563 | return nil, status.Errorf(codes.NotFound, "%s", agent.deviceId) |
| 564 | } else { |
| 565 | return &voltha.ImageDownloads{Items:device.ImageDownloads}, nil |
| 566 | } |
| 567 | } |
| 568 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 569 | // getPorts retrieves the ports information of the device based on the port type. |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 570 | func (agent *DeviceAgent) getPorts(ctx context.Context, portType voltha.Port_PortType) *voltha.Ports { |
| 571 | log.Debugw("getPorts", log.Fields{"id": agent.deviceId, "portType": portType}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 572 | ports := &voltha.Ports{} |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 573 | if device, _ := agent.deviceMgr.GetDevice(agent.deviceId); device != nil { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 574 | for _, port := range device.Ports { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 575 | if port.Type == portType { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 576 | ports.Items = append(ports.Items, port) |
| 577 | } |
| 578 | } |
| 579 | } |
| 580 | return ports |
| 581 | } |
| 582 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 583 | // getSwitchCapability is a helper method that a logical device agent uses to retrieve the switch capability of a |
| 584 | // parent device |
khenaidoo | 7923270 | 2018-12-04 11:00:41 -0500 | [diff] [blame] | 585 | func (agent *DeviceAgent) getSwitchCapability(ctx context.Context) (*ic.SwitchCapability, error) { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 586 | log.Debugw("getSwitchCapability", log.Fields{"deviceId": agent.deviceId}) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 587 | if device, err := agent.deviceMgr.GetDevice(agent.deviceId); device == nil { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 588 | return nil, err |
| 589 | } else { |
khenaidoo | 7923270 | 2018-12-04 11:00:41 -0500 | [diff] [blame] | 590 | var switchCap *ic.SwitchCapability |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 591 | var err error |
| 592 | if switchCap, err = agent.adapterProxy.GetOfpDeviceInfo(ctx, device); err != nil { |
| 593 | log.Debugw("getSwitchCapability-error", log.Fields{"id": device.Id, "error": err}) |
| 594 | return nil, err |
| 595 | } |
| 596 | return switchCap, nil |
| 597 | } |
| 598 | } |
| 599 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 600 | // getPortCapability is a helper method that a logical device agent uses to retrieve the port capability of a |
| 601 | // device |
khenaidoo | 7923270 | 2018-12-04 11:00:41 -0500 | [diff] [blame] | 602 | func (agent *DeviceAgent) getPortCapability(ctx context.Context, portNo uint32) (*ic.PortCapability, error) { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 603 | log.Debugw("getPortCapability", log.Fields{"deviceId": agent.deviceId}) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 604 | if device, err := agent.deviceMgr.GetDevice(agent.deviceId); device == nil { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 605 | return nil, err |
| 606 | } else { |
khenaidoo | 7923270 | 2018-12-04 11:00:41 -0500 | [diff] [blame] | 607 | var portCap *ic.PortCapability |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 608 | var err error |
| 609 | if portCap, err = agent.adapterProxy.GetOfpPortInfo(ctx, device, portNo); err != nil { |
| 610 | log.Debugw("getPortCapability-error", log.Fields{"id": device.Id, "error": err}) |
| 611 | return nil, err |
| 612 | } |
| 613 | return portCap, nil |
| 614 | } |
| 615 | } |
| 616 | |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 617 | func (agent *DeviceAgent) packetOut(outPort uint32, packet *ofp.OfpPacketOut) error { |
| 618 | // Send packet to adapter |
| 619 | if err := agent.adapterProxy.packetOut(agent.deviceType, agent.deviceId, outPort, packet); err != nil { |
| 620 | log.Debugw("packet-out-error", log.Fields{"id": agent.lastData.Id, "error": err}) |
| 621 | return err |
| 622 | } |
| 623 | return nil |
| 624 | } |
| 625 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 626 | // 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] | 627 | func (agent *DeviceAgent) processUpdate(args ...interface{}) interface{} { |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 628 | //// Run this callback in its own go routine |
| 629 | go func(args ...interface{}) interface{} { |
| 630 | var previous *voltha.Device |
| 631 | var current *voltha.Device |
| 632 | var ok bool |
| 633 | if len(args) == 2 { |
| 634 | if previous, ok = args[0].(*voltha.Device); !ok { |
| 635 | log.Errorw("invalid-callback-type", log.Fields{"data": args[0]}) |
| 636 | return nil |
| 637 | } |
| 638 | if current, ok = args[1].(*voltha.Device); !ok { |
| 639 | log.Errorw("invalid-callback-type", log.Fields{"data": args[1]}) |
| 640 | return nil |
| 641 | } |
| 642 | } else { |
| 643 | log.Errorw("too-many-args-in-callback", log.Fields{"len": len(args)}) |
| 644 | return nil |
| 645 | } |
| 646 | // Perform the state transition in it's own go routine |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 647 | if err := agent.deviceMgr.processTransition(previous, current); err != nil { |
| 648 | log.Errorw("failed-process-transition", log.Fields{"deviceId": previous.Id, |
| 649 | "previousAdminState": previous.AdminState, "currentAdminState": current.AdminState}) |
| 650 | } |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 651 | return nil |
| 652 | }(args...) |
| 653 | |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 654 | return nil |
| 655 | } |
| 656 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 657 | func (agent *DeviceAgent) updateDevice(device *voltha.Device) error { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 658 | agent.lockDevice.Lock() |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 659 | defer agent.lockDevice.Unlock() |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 660 | log.Debugw("updateDevice", log.Fields{"deviceId": device.Id}) |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 661 | cloned := proto.Clone(device).(*voltha.Device) |
| 662 | afterUpdate := agent.clusterDataProxy.Update("/devices/"+device.Id, cloned, false, "") |
| 663 | if afterUpdate == nil { |
| 664 | return status.Errorf(codes.Internal, "%s", device.Id) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 665 | } |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 666 | return nil |
| 667 | } |
| 668 | |
| 669 | func (agent *DeviceAgent) updateDeviceWithoutLock(device *voltha.Device) error { |
| 670 | log.Debugw("updateDevice", log.Fields{"deviceId": device.Id}) |
| 671 | cloned := proto.Clone(device).(*voltha.Device) |
| 672 | afterUpdate := agent.clusterDataProxy.Update("/devices/"+device.Id, cloned, false, "") |
| 673 | if afterUpdate == nil { |
| 674 | return status.Errorf(codes.Internal, "%s", device.Id) |
| 675 | } |
| 676 | return nil |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 677 | } |
| 678 | |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 679 | func (agent *DeviceAgent) updateDeviceStatus(operStatus voltha.OperStatus_OperStatus, connStatus voltha.ConnectStatus_ConnectStatus) error { |
| 680 | agent.lockDevice.Lock() |
| 681 | //defer agent.lockDevice.Unlock() |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 682 | // Work only on latest data |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 683 | if storeDevice, err := agent.getDeviceWithoutLock(); err != nil { |
| 684 | agent.lockDevice.Unlock() |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 685 | return status.Errorf(codes.NotFound, "%s", agent.deviceId) |
| 686 | } else { |
| 687 | // clone the device |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 688 | cloned := proto.Clone(storeDevice).(*voltha.Device) |
| 689 | // Ensure the enums passed in are valid - they will be invalid if they are not set when this function is invoked |
| 690 | if s, ok := voltha.ConnectStatus_ConnectStatus_value[connStatus.String()]; ok { |
| 691 | log.Debugw("updateDeviceStatus-conn", log.Fields{"ok": ok, "val": s}) |
| 692 | cloned.ConnectStatus = connStatus |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 693 | } |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 694 | if s, ok := voltha.OperStatus_OperStatus_value[operStatus.String()]; ok { |
| 695 | log.Debugw("updateDeviceStatus-oper", log.Fields{"ok": ok, "val": s}) |
| 696 | cloned.OperStatus = operStatus |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 697 | } |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 698 | 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] | 699 | // Store the device |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 700 | if afterUpdate := agent.clusterDataProxy.Update("/devices/"+agent.deviceId, cloned, false, ""); afterUpdate == nil { |
| 701 | agent.lockDevice.Unlock() |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 702 | return status.Errorf(codes.Internal, "%s", agent.deviceId) |
| 703 | } |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 704 | agent.lockDevice.Unlock() |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 705 | return nil |
| 706 | } |
| 707 | } |
| 708 | |
| 709 | func (agent *DeviceAgent) updatePortState(portType voltha.Port_PortType, portNo uint32, operStatus voltha.OperStatus_OperStatus) error { |
| 710 | agent.lockDevice.Lock() |
| 711 | //defer agent.lockDevice.Unlock() |
| 712 | // Work only on latest data |
| 713 | // TODO: Get list of ports from device directly instead of the entire device |
| 714 | if storeDevice, err := agent.getDeviceWithoutLock(); err != nil { |
| 715 | agent.lockDevice.Unlock() |
| 716 | return status.Errorf(codes.NotFound, "%s", agent.deviceId) |
| 717 | } else { |
| 718 | // clone the device |
| 719 | cloned := proto.Clone(storeDevice).(*voltha.Device) |
| 720 | // Ensure the enums passed in are valid - they will be invalid if they are not set when this function is invoked |
| 721 | if _, ok := voltha.Port_PortType_value[portType.String()]; !ok { |
| 722 | agent.lockDevice.Unlock() |
| 723 | return status.Errorf(codes.InvalidArgument, "%s", portType) |
| 724 | } |
| 725 | for _, port := range cloned.Ports { |
| 726 | if port.Type == portType && port.PortNo == portNo { |
| 727 | port.OperStatus = operStatus |
| 728 | // Set the admin status to ENABLED if the operational status is ACTIVE |
| 729 | // TODO: Set by northbound system? |
| 730 | if operStatus == voltha.OperStatus_ACTIVE { |
| 731 | port.AdminState = voltha.AdminState_ENABLED |
| 732 | } |
| 733 | break |
| 734 | } |
| 735 | } |
| 736 | log.Debugw("portStatusUpdate", log.Fields{"deviceId": cloned.Id}) |
| 737 | // Store the device |
| 738 | if afterUpdate := agent.clusterDataProxy.Update("/devices/"+agent.deviceId, cloned, false, ""); afterUpdate == nil { |
| 739 | agent.lockDevice.Unlock() |
| 740 | return status.Errorf(codes.Internal, "%s", agent.deviceId) |
| 741 | } |
| 742 | agent.lockDevice.Unlock() |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 743 | return nil |
| 744 | } |
| 745 | } |
| 746 | |
| 747 | func (agent *DeviceAgent) updatePmConfigs(pmConfigs *voltha.PmConfigs) error { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 748 | agent.lockDevice.Lock() |
| 749 | defer agent.lockDevice.Unlock() |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 750 | log.Debug("updatePmConfigs") |
| 751 | // Work only on latest data |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 752 | if storeDevice, err := agent.getDeviceWithoutLock(); err != nil { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 753 | return status.Errorf(codes.NotFound, "%s", agent.deviceId) |
| 754 | } else { |
| 755 | // clone the device |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 756 | cloned := proto.Clone(storeDevice).(*voltha.Device) |
| 757 | cloned.PmConfigs = proto.Clone(pmConfigs).(*voltha.PmConfigs) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 758 | // Store the device |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 759 | afterUpdate := agent.clusterDataProxy.Update("/devices/"+agent.deviceId, cloned, false, "") |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 760 | if afterUpdate == nil { |
| 761 | return status.Errorf(codes.Internal, "%s", agent.deviceId) |
| 762 | } |
| 763 | return nil |
| 764 | } |
| 765 | } |
| 766 | |
| 767 | func (agent *DeviceAgent) addPort(port *voltha.Port) error { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 768 | agent.lockDevice.Lock() |
| 769 | defer agent.lockDevice.Unlock() |
| 770 | log.Debugw("addPort", log.Fields{"deviceId": agent.deviceId}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 771 | // Work only on latest data |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 772 | if storeDevice, err := agent.getDeviceWithoutLock(); err != nil { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 773 | return status.Errorf(codes.NotFound, "%s", agent.deviceId) |
| 774 | } else { |
| 775 | // clone the device |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 776 | cloned := proto.Clone(storeDevice).(*voltha.Device) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 777 | if cloned.Ports == nil { |
| 778 | // First port |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 779 | log.Debugw("addPort-first-port-to-add", log.Fields{"deviceId": agent.deviceId}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 780 | cloned.Ports = make([]*voltha.Port, 0) |
| 781 | } |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 782 | cp := proto.Clone(port).(*voltha.Port) |
| 783 | // Set the admin state of the port to ENABLE if the operational state is ACTIVE |
| 784 | // TODO: Set by northbound system? |
| 785 | if cp.OperStatus == voltha.OperStatus_ACTIVE { |
| 786 | cp.AdminState = voltha.AdminState_ENABLED |
| 787 | } |
| 788 | cloned.Ports = append(cloned.Ports, cp) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 789 | // Store the device |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 790 | afterUpdate := agent.clusterDataProxy.Update("/devices/"+agent.deviceId, cloned, false, "") |
| 791 | if afterUpdate == nil { |
| 792 | return status.Errorf(codes.Internal, "%s", agent.deviceId) |
| 793 | } |
| 794 | return nil |
| 795 | } |
| 796 | } |
| 797 | |
| 798 | func (agent *DeviceAgent) addPeerPort(port *voltha.Port_PeerPort) error { |
| 799 | agent.lockDevice.Lock() |
| 800 | defer agent.lockDevice.Unlock() |
| 801 | log.Debug("addPeerPort") |
| 802 | // Work only on latest data |
| 803 | if storeDevice, err := agent.getDeviceWithoutLock(); err != nil { |
| 804 | return status.Errorf(codes.NotFound, "%s", agent.deviceId) |
| 805 | } else { |
| 806 | // clone the device |
| 807 | cloned := proto.Clone(storeDevice).(*voltha.Device) |
| 808 | // Get the peer port on the device based on the port no |
| 809 | for _, peerPort := range cloned.Ports { |
| 810 | if peerPort.PortNo == port.PortNo { // found port |
| 811 | cp := proto.Clone(port).(*voltha.Port_PeerPort) |
| 812 | peerPort.Peers = append(peerPort.Peers, cp) |
| 813 | log.Debugw("found-peer", log.Fields{"portNo": port.PortNo, "deviceId": agent.deviceId}) |
| 814 | break |
| 815 | } |
| 816 | } |
| 817 | // Store the device |
| 818 | afterUpdate := agent.clusterDataProxy.Update("/devices/"+agent.deviceId, cloned, false, "") |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 819 | if afterUpdate == nil { |
| 820 | return status.Errorf(codes.Internal, "%s", agent.deviceId) |
| 821 | } |
| 822 | return nil |
| 823 | } |
| 824 | } |
| 825 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 826 | //flowTableUpdated is the callback after flows have been updated in the model to push them |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 827 | //to the adapters |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 828 | func (agent *DeviceAgent) flowTableUpdated(args ...interface{}) interface{} { |
| 829 | log.Debugw("flowTableUpdated-callback", log.Fields{"argsLen": len(args)}) |
| 830 | |
| 831 | agent.lockDevice.Lock() |
| 832 | defer agent.lockDevice.Unlock() |
| 833 | |
| 834 | var previousData *voltha.Flows |
| 835 | var latestData *voltha.Flows |
| 836 | |
| 837 | var ok bool |
| 838 | if previousData, ok = args[0].(*ofp.Flows); !ok { |
| 839 | log.Errorw("invalid-args", log.Fields{"args0": args[0]}) |
| 840 | return nil |
| 841 | } |
| 842 | if latestData, ok = args[1].(*ofp.Flows); !ok { |
| 843 | log.Errorw("invalid-args", log.Fields{"args1": args[1]}) |
| 844 | return nil |
| 845 | } |
| 846 | |
| 847 | // Sanity check - should not happen as this is already handled in logical device agent |
| 848 | if reflect.DeepEqual(previousData.Items, latestData.Items) { |
| 849 | log.Debugw("flow-update-not-required", log.Fields{"previous": previousData.Items, "new": latestData.Items}) |
| 850 | return nil |
| 851 | } |
| 852 | |
| 853 | var device *voltha.Device |
| 854 | var err error |
| 855 | if device, err = agent.getDeviceWithoutLock(); err != nil { |
| 856 | log.Errorw("no-device", log.Fields{"id": agent.deviceId, "error": err}) |
| 857 | return nil |
| 858 | } |
| 859 | groups := device.FlowGroups |
| 860 | |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 861 | // Send update to adapters |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 862 | dType := agent.adapterMgr.getDeviceType(device.Type) |
| 863 | if !dType.AcceptsAddRemoveFlowUpdates { |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 864 | if err := agent.adapterProxy.UpdateFlowsBulk(device, latestData, groups); err != nil { |
| 865 | log.Debugw("update-flow-bulk-error", log.Fields{"id": agent.lastData.Id, "error": err}) |
| 866 | return err |
| 867 | } |
| 868 | return nil |
| 869 | } |
| 870 | // Incremental flow changes accepted |
| 871 | var toAdd []*ofp.OfpFlowStats |
| 872 | var toDelete []*ofp.OfpFlowStats |
| 873 | |
| 874 | for _, flow := range latestData.Items { |
| 875 | if fu.FindFlowById(previousData.Items, flow) == -1 { // did not exist before |
| 876 | toAdd = append(toAdd, flow) |
| 877 | } |
| 878 | } |
| 879 | for _, flow := range previousData.Items { |
| 880 | if fu.FindFlowById(latestData.Items, flow) == -1 { // does not exist now |
| 881 | toDelete = append(toDelete, flow) |
| 882 | } |
| 883 | } |
| 884 | flowChanges := &ofp.FlowChanges{ |
| 885 | ToAdd: &voltha.Flows{Items: toAdd}, |
| 886 | ToRemove: &voltha.Flows{Items: toDelete}, |
| 887 | } |
| 888 | // Send an empty group changes as it would be dealt with a call to groupTableUpdated |
| 889 | groupChanges := &ofp.FlowGroupChanges{} |
| 890 | |
| 891 | // Send changes only |
| 892 | if err := agent.adapterProxy.UpdateFlowsIncremental(device, flowChanges, groupChanges); err != nil { |
| 893 | log.Debugw("update-flow-bulk-error", log.Fields{"id": agent.lastData.Id, "error": err}) |
| 894 | return err |
| 895 | } |
| 896 | |
| 897 | return nil |
| 898 | } |
| 899 | |
| 900 | //groupTableUpdated is the callback after group table has been updated in the model to push them |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 901 | //to the adapters |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 902 | func (agent *DeviceAgent) groupTableUpdated(args ...interface{}) interface{} { |
| 903 | log.Debugw("groupTableUpdated-callback", log.Fields{"argsLen": len(args)}) |
| 904 | |
| 905 | agent.lockDevice.Lock() |
| 906 | defer agent.lockDevice.Unlock() |
| 907 | |
| 908 | var previousData *voltha.FlowGroups |
| 909 | var latestData *voltha.FlowGroups |
| 910 | |
| 911 | var ok bool |
| 912 | if previousData, ok = args[0].(*ofp.FlowGroups); !ok { |
| 913 | log.Errorw("invalid-args", log.Fields{"args0": args[0]}) |
| 914 | return nil |
| 915 | } |
| 916 | if latestData, ok = args[1].(*ofp.FlowGroups); !ok { |
| 917 | log.Errorw("invalid-args", log.Fields{"args1": args[1]}) |
| 918 | return nil |
| 919 | } |
| 920 | |
| 921 | // Sanity check - should not happen as this is already handled in logical device agent |
| 922 | if reflect.DeepEqual(previousData.Items, latestData.Items) { |
| 923 | log.Debugw("group-table-update-not-required", log.Fields{"previous": previousData.Items, "new": latestData.Items}) |
| 924 | return nil |
| 925 | } |
| 926 | |
| 927 | var device *voltha.Device |
| 928 | var err error |
| 929 | if device, err = agent.getDeviceWithoutLock(); err != nil { |
| 930 | log.Errorw("no-device", log.Fields{"id": agent.deviceId, "error": err}) |
| 931 | return nil |
| 932 | } |
| 933 | flows := device.Flows |
| 934 | |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 935 | // Send update to adapters |
| 936 | dType := agent.adapterMgr.getDeviceType(device.Type) |
| 937 | if !dType.AcceptsAddRemoveFlowUpdates { |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 938 | if err := agent.adapterProxy.UpdateFlowsBulk(device, flows, latestData); err != nil { |
| 939 | log.Debugw("update-flows-bulk-error", log.Fields{"id": agent.lastData.Id, "error": err}) |
| 940 | return err |
| 941 | } |
| 942 | return nil |
| 943 | } |
| 944 | |
| 945 | // Incremental group changes accepted |
| 946 | var toAdd []*ofp.OfpGroupEntry |
| 947 | var toDelete []*ofp.OfpGroupEntry |
| 948 | var toUpdate []*ofp.OfpGroupEntry |
| 949 | |
| 950 | for _, group := range latestData.Items { |
| 951 | if idx := fu.FindGroup(previousData.Items, group.Desc.GroupId); idx == -1 { // did not exist before |
| 952 | toAdd = append(toAdd, group) |
| 953 | } else { // existed before |
| 954 | if previousData.Items[idx].String() != group.String() { // there is a change |
| 955 | toUpdate = append(toUpdate, group) |
| 956 | } |
| 957 | } |
| 958 | } |
| 959 | for _, group := range previousData.Items { |
| 960 | if fu.FindGroup(latestData.Items, group.Desc.GroupId) == -1 { // does not exist now |
| 961 | toDelete = append(toDelete, group) |
| 962 | } |
| 963 | } |
| 964 | groupChanges := &ofp.FlowGroupChanges{ |
| 965 | ToAdd: &voltha.FlowGroups{Items: toAdd}, |
| 966 | ToRemove: &voltha.FlowGroups{Items: toDelete}, |
| 967 | ToUpdate: &voltha.FlowGroups{Items: toUpdate}, |
| 968 | } |
| 969 | // Send an empty flow changes as it should have been dealt with a call to flowTableUpdated |
| 970 | flowChanges := &ofp.FlowChanges{} |
| 971 | |
| 972 | // Send changes only |
| 973 | if err := agent.adapterProxy.UpdateFlowsIncremental(device, flowChanges, groupChanges); err != nil { |
| 974 | log.Debugw("update-incremental-group-error", log.Fields{"id": agent.lastData.Id, "error": err}) |
| 975 | return err |
| 976 | } |
| 977 | return nil |
| 978 | } |
| 979 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 980 | // TODO: A generic device update by attribute |
| 981 | func (agent *DeviceAgent) updateDeviceAttribute(name string, value interface{}) { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 982 | agent.lockDevice.Lock() |
| 983 | defer agent.lockDevice.Unlock() |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 984 | if value == nil { |
| 985 | return |
| 986 | } |
| 987 | var storeDevice *voltha.Device |
| 988 | var err error |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 989 | if storeDevice, err = agent.getDeviceWithoutLock(); err != nil { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 990 | return |
| 991 | } |
| 992 | updated := false |
| 993 | s := reflect.ValueOf(storeDevice).Elem() |
| 994 | if s.Kind() == reflect.Struct { |
| 995 | // exported field |
| 996 | f := s.FieldByName(name) |
| 997 | if f.IsValid() && f.CanSet() { |
| 998 | switch f.Kind() { |
| 999 | case reflect.String: |
| 1000 | f.SetString(value.(string)) |
| 1001 | updated = true |
| 1002 | case reflect.Uint32: |
| 1003 | f.SetUint(uint64(value.(uint32))) |
| 1004 | updated = true |
| 1005 | case reflect.Bool: |
| 1006 | f.SetBool(value.(bool)) |
| 1007 | updated = true |
| 1008 | } |
| 1009 | } |
| 1010 | } |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1011 | 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] | 1012 | // Save the data |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1013 | cloned := proto.Clone(storeDevice).(*voltha.Device) |
| 1014 | if afterUpdate := agent.clusterDataProxy.Update("/devices/"+agent.deviceId, cloned, false, ""); afterUpdate == nil { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1015 | log.Warnw("attribute-update-failed", log.Fields{"attribute": name, "value": value}) |
| 1016 | } |
| 1017 | return |
| 1018 | } |