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