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 | 3ab3488 | 2019-05-02 21:33:30 -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" |
serkant.uluderya | 334479d | 2019-04-10 08:26:15 -0700 | [diff] [blame] | 24 | fu "github.com/opencord/voltha-go/rw_core/utils" |
William Kurkian | daa6bb2 | 2019-03-07 12:26:28 -0500 | [diff] [blame] | 25 | ic "github.com/opencord/voltha-protos/go/inter_container" |
| 26 | ofp "github.com/opencord/voltha-protos/go/openflow_13" |
| 27 | "github.com/opencord/voltha-protos/go/voltha" |
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" |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 32 | "time" |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 33 | ) |
| 34 | |
| 35 | type DeviceAgent struct { |
khenaidoo | 9a46896 | 2018-09-19 15:33:13 -0400 | [diff] [blame] | 36 | deviceId string |
khenaidoo | 6d62c00 | 2019-05-15 21:57:03 -0400 | [diff] [blame] | 37 | parentId string |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 38 | deviceType string |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 39 | isRootdevice bool |
khenaidoo | 9a46896 | 2018-09-19 15:33:13 -0400 | [diff] [blame] | 40 | lastData *voltha.Device |
| 41 | adapterProxy *AdapterProxy |
serkant.uluderya | 334479d | 2019-04-10 08:26:15 -0700 | [diff] [blame] | 42 | adapterMgr *AdapterManager |
khenaidoo | 9a46896 | 2018-09-19 15:33:13 -0400 | [diff] [blame] | 43 | deviceMgr *DeviceManager |
| 44 | clusterDataProxy *model.Proxy |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 45 | deviceProxy *model.Proxy |
khenaidoo | 9a46896 | 2018-09-19 15:33:13 -0400 | [diff] [blame] | 46 | exitChannel chan int |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 47 | lockDevice sync.RWMutex |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 48 | defaultTimeout int64 |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 49 | } |
| 50 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 51 | //newDeviceAgent creates a new device agent along as creating a unique ID for the device and set the device state to |
| 52 | //preprovisioning |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 53 | 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] | 54 | var agent DeviceAgent |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 55 | agent.adapterProxy = ap |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 56 | cloned := (proto.Clone(device)).(*voltha.Device) |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 57 | if cloned.Id == "" { |
| 58 | cloned.Id = CreateDeviceId() |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 59 | cloned.AdminState = voltha.AdminState_PREPROVISIONED |
| 60 | cloned.FlowGroups = &ofp.FlowGroups{Items: nil} |
| 61 | cloned.Flows = &ofp.Flows{Items: nil} |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 62 | } |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 63 | if !device.GetRoot() && device.ProxyAddress != nil { |
| 64 | // Set the default vlan ID to the one specified by the parent adapter. It can be |
| 65 | // overwritten by the child adapter during a device update request |
| 66 | cloned.Vlan = device.ProxyAddress.ChannelId |
| 67 | } |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 68 | agent.isRootdevice = device.Root |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 69 | agent.deviceId = cloned.Id |
khenaidoo | 6d62c00 | 2019-05-15 21:57:03 -0400 | [diff] [blame] | 70 | agent.parentId = device.ParentId |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 71 | agent.deviceType = cloned.Type |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 72 | agent.lastData = cloned |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 73 | agent.deviceMgr = deviceMgr |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 74 | agent.adapterMgr = deviceMgr.adapterMgr |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 75 | agent.exitChannel = make(chan int, 1) |
khenaidoo | 9a46896 | 2018-09-19 15:33:13 -0400 | [diff] [blame] | 76 | agent.clusterDataProxy = cdProxy |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 77 | agent.lockDevice = sync.RWMutex{} |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 78 | agent.defaultTimeout = timeout |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 79 | return &agent |
| 80 | } |
| 81 | |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 82 | // start save the device to the data model and registers for callbacks on that device if loadFromdB is false. Otherwise, |
| 83 | // it will load the data from the dB and setup teh necessary callbacks and proxies. |
| 84 | func (agent *DeviceAgent) start(ctx context.Context, loadFromdB bool) error { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 85 | agent.lockDevice.Lock() |
| 86 | defer agent.lockDevice.Unlock() |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 87 | log.Debugw("starting-device-agent", log.Fields{"deviceId": agent.deviceId}) |
| 88 | if loadFromdB { |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 89 | if device := agent.clusterDataProxy.Get(ctx, "/devices/"+agent.deviceId, 1, false, ""); device != nil { |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 90 | if d, ok := device.(*voltha.Device); ok { |
| 91 | agent.lastData = proto.Clone(d).(*voltha.Device) |
khenaidoo | 6d05513 | 2019-02-12 16:51:19 -0500 | [diff] [blame] | 92 | agent.deviceType = agent.lastData.Adapter |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 93 | } |
| 94 | } else { |
| 95 | log.Errorw("failed-to-load-device", log.Fields{"deviceId": agent.deviceId}) |
| 96 | return status.Errorf(codes.NotFound, "device-%s", agent.deviceId) |
| 97 | } |
| 98 | log.Debugw("device-loaded-from-dB", log.Fields{"device": agent.lastData}) |
| 99 | } else { |
| 100 | // Add the initial device to the local model |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 101 | if added := agent.clusterDataProxy.AddWithID(ctx, "/devices", agent.deviceId, agent.lastData, ""); added == nil { |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 102 | log.Errorw("failed-to-add-device", log.Fields{"deviceId": agent.deviceId}) |
| 103 | } |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 104 | } |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 105 | |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 106 | agent.deviceProxy = agent.clusterDataProxy.CreateProxy(ctx, "/devices/"+agent.deviceId, false) |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 107 | agent.deviceProxy.RegisterCallback(model.POST_UPDATE, agent.processUpdate) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 108 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 109 | log.Debug("device-agent-started") |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 110 | return nil |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 111 | } |
| 112 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 113 | // stop stops the device agent. Not much to do for now |
| 114 | func (agent *DeviceAgent) stop(ctx context.Context) { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 115 | agent.lockDevice.Lock() |
| 116 | defer agent.lockDevice.Unlock() |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 117 | log.Debug("stopping-device-agent") |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 118 | // Remove the device from the KV store |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 119 | if removed := agent.clusterDataProxy.Remove(ctx, "/devices/"+agent.deviceId, ""); removed == nil { |
khenaidoo | 4554f7c | 2019-05-29 22:13:15 -0400 | [diff] [blame] | 120 | log.Debugw("device-already-removed", log.Fields{"id": agent.deviceId}) |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 121 | } |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 122 | agent.exitChannel <- 1 |
| 123 | log.Debug("device-agent-stopped") |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 124 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 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() |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 131 | if device := agent.clusterDataProxy.Get(context.Background(), "/devices/"+agent.deviceId, 0, true, ""); 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 | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 143 | if device := agent.clusterDataProxy.Get(context.Background(), "/devices/"+agent.deviceId, 0, false, ""); 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 | 3ab3488 | 2019-05-02 21:33:30 -0400 | [diff] [blame] | 152 | // enableDevice activates a preprovisioned or a 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}) |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 173 | return nil |
| 174 | } |
khenaidoo | 59ef7be | 2019-06-21 12:40:28 -0400 | [diff] [blame] | 175 | |
| 176 | if device.AdminState == voltha.AdminState_DELETED { |
| 177 | // This is a temporary state when a device is deleted before it gets removed from the model. |
| 178 | err = status.Error(codes.FailedPrecondition, fmt.Sprintf("cannot-enable-a-deleted-device: %s ", device.Id)) |
| 179 | log.Warnw("invalid-state", log.Fields{"id": agent.deviceId, "state": device.AdminState, "error": err}) |
| 180 | return err |
khenaidoo | 3ab3488 | 2019-05-02 21:33:30 -0400 | [diff] [blame] | 181 | } |
khenaidoo | 59ef7be | 2019-06-21 12:40:28 -0400 | [diff] [blame] | 182 | |
| 183 | previousAdminState := device.AdminState |
| 184 | |
| 185 | // Update the Admin State and set the operational state to activating before sending the request to the |
| 186 | // Adapters |
| 187 | cloned := proto.Clone(device).(*voltha.Device) |
| 188 | cloned.AdminState = voltha.AdminState_ENABLED |
| 189 | cloned.OperStatus = voltha.OperStatus_ACTIVATING |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 190 | |
Mahir Gunyel | b585167 | 2019-07-24 10:46:26 +0300 | [diff] [blame^] | 191 | if err := agent.updateDeviceInStoreWithoutLock(cloned, false, ""); err != nil { |
| 192 | return err |
khenaidoo | 59ef7be | 2019-06-21 12:40:28 -0400 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | // Adopt the device if it was in preprovision state. In all other cases, try to reenable it. |
| 196 | if previousAdminState == voltha.AdminState_PREPROVISIONED { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 197 | if err := agent.adapterProxy.AdoptDevice(ctx, device); err != nil { |
| 198 | log.Debugw("adoptDevice-error", log.Fields{"id": agent.lastData.Id, "error": err}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 199 | return err |
| 200 | } |
khenaidoo | 59ef7be | 2019-06-21 12:40:28 -0400 | [diff] [blame] | 201 | } else { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 202 | if err := agent.adapterProxy.ReEnableDevice(ctx, device); err != nil { |
| 203 | log.Debugw("renableDevice-error", log.Fields{"id": agent.lastData.Id, "error": err}) |
| 204 | return err |
| 205 | } |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 206 | } |
| 207 | } |
| 208 | return nil |
| 209 | } |
| 210 | |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 211 | func (agent *DeviceAgent) updateDeviceWithoutLockAsync(device *voltha.Device, ch chan interface{}) { |
| 212 | if err := agent.updateDeviceWithoutLock(device); err != nil { |
| 213 | ch <- status.Errorf(codes.Internal, "failure-updating-%s", agent.deviceId) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 214 | } |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 215 | ch <- nil |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 216 | } |
| 217 | |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 218 | func (agent *DeviceAgent) sendBulkFlowsToAdapters(device *voltha.Device, flows *voltha.Flows, groups *voltha.FlowGroups, ch chan interface{}) { |
| 219 | if err := agent.adapterProxy.UpdateFlowsBulk(device, flows, groups); err != nil { |
| 220 | log.Debugw("update-flow-bulk-error", log.Fields{"id": agent.lastData.Id, "error": err}) |
| 221 | ch <- err |
| 222 | } |
| 223 | ch <- nil |
| 224 | } |
| 225 | |
| 226 | func (agent *DeviceAgent) sendIncrementalFlowsToAdapters(device *voltha.Device, flows *ofp.FlowChanges, groups *ofp.FlowGroupChanges, ch chan interface{}) { |
| 227 | if err := agent.adapterProxy.UpdateFlowsIncremental(device, flows, groups); err != nil { |
| 228 | log.Debugw("update-flow-incremental-error", log.Fields{"id": agent.lastData.Id, "error": err}) |
| 229 | ch <- err |
| 230 | } |
| 231 | ch <- nil |
| 232 | } |
| 233 | |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 234 | //addFlowsAndGroups adds the "newFlows" and "newGroups" from the existing flows/groups and sends the update to the |
| 235 | //adapters |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 236 | func (agent *DeviceAgent) addFlowsAndGroups(newFlows []*ofp.OfpFlowStats, newGroups []*ofp.OfpGroupEntry) error { |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 237 | log.Debugw("addFlowsAndGroups", log.Fields{"deviceId": agent.deviceId, "flows": newFlows, "groups": newGroups}) |
| 238 | |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 239 | if (len(newFlows) | len(newGroups)) == 0 { |
| 240 | log.Debugw("nothing-to-update", log.Fields{"deviceId": agent.deviceId, "flows": newFlows, "groups": newGroups}) |
| 241 | return nil |
| 242 | } |
| 243 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 244 | agent.lockDevice.Lock() |
| 245 | defer agent.lockDevice.Unlock() |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 246 | |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 247 | var device *voltha.Device |
| 248 | var err error |
| 249 | if device, err = agent.getDeviceWithoutLock(); err != nil { |
| 250 | return status.Errorf(codes.NotFound, "%s", agent.deviceId) |
| 251 | } |
| 252 | |
| 253 | existingFlows := proto.Clone(device.Flows).(*voltha.Flows) |
| 254 | existingGroups := proto.Clone(device.FlowGroups).(*ofp.FlowGroups) |
| 255 | |
| 256 | var updatedFlows []*ofp.OfpFlowStats |
| 257 | var flowsToDelete []*ofp.OfpFlowStats |
| 258 | var groupsToDelete []*ofp.OfpGroupEntry |
| 259 | var updatedGroups []*ofp.OfpGroupEntry |
| 260 | |
| 261 | // Process flows |
| 262 | for _, flow := range newFlows { |
| 263 | updatedFlows = append(updatedFlows, flow) |
| 264 | } |
| 265 | for _, flow := range existingFlows.Items { |
| 266 | if idx := fu.FindFlows(newFlows, flow); idx == -1 { |
| 267 | updatedFlows = append(updatedFlows, flow) |
| 268 | } else { |
| 269 | flowsToDelete = append(flowsToDelete, flow) |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | // Process groups |
| 274 | for _, g := range newGroups { |
| 275 | updatedGroups = append(updatedGroups, g) |
| 276 | } |
| 277 | for _, group := range existingGroups.Items { |
| 278 | if fu.FindGroup(newGroups, group.Desc.GroupId) == -1 { // does not exist now |
| 279 | updatedGroups = append(updatedGroups, group) |
| 280 | } else { |
| 281 | groupsToDelete = append(groupsToDelete, group) |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | // Sanity check |
| 286 | if (len(updatedFlows) | len(flowsToDelete) | len(updatedGroups) | len(groupsToDelete)) == 0 { |
| 287 | log.Debugw("nothing-to-update", log.Fields{"deviceId": agent.deviceId, "flows": newFlows, "groups": newGroups}) |
| 288 | return nil |
| 289 | } |
| 290 | |
| 291 | // Send update to adapters |
| 292 | // Create two channels to receive responses from the dB and from the adapters. |
| 293 | // Do not close these channels as this function may exit on timeout before the dB or adapters get a chance |
| 294 | // to send their responses. These channels will be garbage collected once all the responses are |
| 295 | // received |
| 296 | chAdapters := make(chan interface{}) |
| 297 | chdB := make(chan interface{}) |
| 298 | dType := agent.adapterMgr.getDeviceType(device.Type) |
| 299 | if !dType.AcceptsAddRemoveFlowUpdates { |
| 300 | |
| 301 | if len(updatedGroups) != 0 && reflect.DeepEqual(existingGroups.Items, updatedGroups) && len(updatedFlows) != 0 && reflect.DeepEqual(existingFlows.Items, updatedFlows) { |
| 302 | log.Debugw("nothing-to-update", log.Fields{"deviceId": agent.deviceId, "flows": newFlows, "groups": newGroups}) |
| 303 | return nil |
| 304 | } |
| 305 | go agent.sendBulkFlowsToAdapters(device, &voltha.Flows{Items: updatedFlows}, &voltha.FlowGroups{Items: updatedGroups}, chAdapters) |
| 306 | |
| 307 | } else { |
| 308 | flowChanges := &ofp.FlowChanges{ |
| 309 | ToAdd: &voltha.Flows{Items: newFlows}, |
| 310 | ToRemove: &voltha.Flows{Items: flowsToDelete}, |
| 311 | } |
| 312 | groupChanges := &ofp.FlowGroupChanges{ |
| 313 | ToAdd: &voltha.FlowGroups{Items: newGroups}, |
| 314 | ToRemove: &voltha.FlowGroups{Items: groupsToDelete}, |
| 315 | ToUpdate: &voltha.FlowGroups{Items: []*ofp.OfpGroupEntry{}}, |
| 316 | } |
| 317 | go agent.sendIncrementalFlowsToAdapters(device, flowChanges, groupChanges, chAdapters) |
| 318 | } |
| 319 | |
| 320 | // store the changed data |
| 321 | device.Flows = &voltha.Flows{Items: updatedFlows} |
| 322 | device.FlowGroups = &voltha.FlowGroups{Items: updatedGroups} |
| 323 | go agent.updateDeviceWithoutLockAsync(device, chdB) |
| 324 | |
| 325 | if res := fu.WaitForNilOrErrorResponses(agent.defaultTimeout, chAdapters, chdB); res != nil { |
| 326 | return status.Errorf(codes.Aborted, "errors-%s", res) |
| 327 | } |
| 328 | |
| 329 | return nil |
| 330 | } |
| 331 | |
| 332 | //deleteFlowsAndGroups removes the "flowsToDel" and "groupsToDel" from the existing flows/groups and sends the update to the |
| 333 | //adapters |
| 334 | func (agent *DeviceAgent) deleteFlowsAndGroups(flowsToDel []*ofp.OfpFlowStats, groupsToDel []*ofp.OfpGroupEntry) error { |
| 335 | log.Debugw("deleteFlowsAndGroups", log.Fields{"deviceId": agent.deviceId, "flows": flowsToDel, "groups": groupsToDel}) |
| 336 | |
| 337 | if (len(flowsToDel) | len(groupsToDel)) == 0 { |
| 338 | log.Debugw("nothing-to-update", log.Fields{"deviceId": agent.deviceId, "flows": flowsToDel, "groups": groupsToDel}) |
| 339 | return nil |
| 340 | } |
| 341 | |
| 342 | agent.lockDevice.Lock() |
| 343 | defer agent.lockDevice.Unlock() |
| 344 | |
| 345 | var device *voltha.Device |
| 346 | var err error |
| 347 | |
| 348 | if device, err = agent.getDeviceWithoutLock(); err != nil { |
| 349 | return status.Errorf(codes.NotFound, "%s", agent.deviceId) |
| 350 | } |
| 351 | |
| 352 | existingFlows := proto.Clone(device.Flows).(*voltha.Flows) |
| 353 | existingGroups := proto.Clone(device.FlowGroups).(*ofp.FlowGroups) |
| 354 | |
| 355 | var flowsToKeep []*ofp.OfpFlowStats |
| 356 | var groupsToKeep []*ofp.OfpGroupEntry |
| 357 | |
| 358 | // Process flows |
| 359 | for _, flow := range existingFlows.Items { |
| 360 | if idx := fu.FindFlows(flowsToDel, flow); idx == -1 { |
| 361 | flowsToKeep = append(flowsToKeep, flow) |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | // Process groups |
| 366 | for _, group := range existingGroups.Items { |
| 367 | if fu.FindGroup(groupsToDel, group.Desc.GroupId) == -1 { // does not exist now |
| 368 | groupsToKeep = append(groupsToKeep, group) |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | log.Debugw("deleteFlowsAndGroups", |
| 373 | log.Fields{ |
| 374 | "deviceId": agent.deviceId, |
| 375 | "flowsToDel": len(flowsToDel), |
| 376 | "flowsToKeep": len(flowsToKeep), |
| 377 | "groupsToDel": len(groupsToDel), |
| 378 | "groupsToKeep": len(groupsToKeep), |
| 379 | }) |
| 380 | |
| 381 | // Sanity check |
| 382 | if (len(flowsToKeep) | len(flowsToDel) | len(groupsToKeep) | len(groupsToDel)) == 0 { |
| 383 | log.Debugw("nothing-to-update", log.Fields{"deviceId": agent.deviceId, "flowsToDel": flowsToDel, "groupsToDel": groupsToDel}) |
| 384 | return nil |
| 385 | } |
| 386 | |
| 387 | // Send update to adapters |
| 388 | chAdapters := make(chan interface{}) |
| 389 | chdB := make(chan interface{}) |
| 390 | dType := agent.adapterMgr.getDeviceType(device.Type) |
| 391 | if !dType.AcceptsAddRemoveFlowUpdates { |
| 392 | if len(groupsToKeep) != 0 && reflect.DeepEqual(existingGroups.Items, groupsToKeep) && len(flowsToKeep) != 0 && reflect.DeepEqual(existingFlows.Items, flowsToKeep) { |
| 393 | log.Debugw("nothing-to-update", log.Fields{"deviceId": agent.deviceId, "flowsToDel": flowsToDel, "groupsToDel": groupsToDel}) |
| 394 | return nil |
| 395 | } |
| 396 | go agent.sendBulkFlowsToAdapters(device, &voltha.Flows{Items: flowsToKeep}, &voltha.FlowGroups{Items: groupsToKeep}, chAdapters) |
| 397 | } else { |
| 398 | flowChanges := &ofp.FlowChanges{ |
| 399 | ToAdd: &voltha.Flows{Items: []*ofp.OfpFlowStats{}}, |
| 400 | ToRemove: &voltha.Flows{Items: flowsToDel}, |
| 401 | } |
| 402 | groupChanges := &ofp.FlowGroupChanges{ |
| 403 | ToAdd: &voltha.FlowGroups{Items: []*ofp.OfpGroupEntry{}}, |
| 404 | ToRemove: &voltha.FlowGroups{Items: groupsToDel}, |
| 405 | ToUpdate: &voltha.FlowGroups{Items: []*ofp.OfpGroupEntry{}}, |
| 406 | } |
| 407 | go agent.sendIncrementalFlowsToAdapters(device, flowChanges, groupChanges, chAdapters) |
| 408 | } |
| 409 | |
| 410 | // store the changed data |
| 411 | device.Flows = &voltha.Flows{Items: flowsToKeep} |
| 412 | device.FlowGroups = &voltha.FlowGroups{Items: groupsToKeep} |
| 413 | go agent.updateDeviceWithoutLockAsync(device, chdB) |
| 414 | |
| 415 | if res := fu.WaitForNilOrErrorResponses(agent.defaultTimeout, chAdapters, chdB); res != nil { |
| 416 | return status.Errorf(codes.Aborted, "errors-%s", res) |
| 417 | } |
| 418 | return nil |
| 419 | |
| 420 | } |
| 421 | |
| 422 | //updateFlowsAndGroups replaces the existing flows and groups with "updatedFlows" and "updatedGroups" respectively. It |
| 423 | //also sends the updates to the adapters |
| 424 | func (agent *DeviceAgent) updateFlowsAndGroups(updatedFlows []*ofp.OfpFlowStats, updatedGroups []*ofp.OfpGroupEntry) error { |
| 425 | log.Debugw("updateFlowsAndGroups", log.Fields{"deviceId": agent.deviceId, "flows": updatedFlows, "groups": updatedGroups}) |
| 426 | |
| 427 | if (len(updatedFlows) | len(updatedGroups)) == 0 { |
| 428 | log.Debugw("nothing-to-update", log.Fields{"deviceId": agent.deviceId, "flows": updatedFlows, "groups": updatedGroups}) |
| 429 | return nil |
| 430 | } |
| 431 | |
| 432 | agent.lockDevice.Lock() |
| 433 | defer agent.lockDevice.Unlock() |
| 434 | var device *voltha.Device |
| 435 | var err error |
| 436 | if device, err = agent.getDeviceWithoutLock(); err != nil { |
| 437 | return status.Errorf(codes.NotFound, "%s", agent.deviceId) |
| 438 | } |
| 439 | existingFlows := proto.Clone(device.Flows).(*voltha.Flows) |
| 440 | existingGroups := proto.Clone(device.FlowGroups).(*ofp.FlowGroups) |
| 441 | |
| 442 | if len(updatedGroups) != 0 && reflect.DeepEqual(existingGroups.Items, updatedGroups) && len(updatedFlows) != 0 && reflect.DeepEqual(existingFlows.Items, updatedFlows) { |
| 443 | log.Debugw("nothing-to-update", log.Fields{"deviceId": agent.deviceId, "flows": updatedFlows, "groups": updatedGroups}) |
| 444 | return nil |
| 445 | } |
| 446 | |
| 447 | log.Debugw("updating-flows-and-groups", |
| 448 | log.Fields{ |
| 449 | "deviceId": agent.deviceId, |
| 450 | "updatedFlows": updatedFlows, |
| 451 | "updatedGroups": updatedGroups, |
| 452 | }) |
| 453 | |
| 454 | chAdapters := make(chan interface{}) |
| 455 | chdB := make(chan interface{}) |
| 456 | dType := agent.adapterMgr.getDeviceType(device.Type) |
| 457 | |
| 458 | // Process bulk flow update differently than incremental update |
| 459 | if !dType.AcceptsAddRemoveFlowUpdates { |
| 460 | go agent.sendBulkFlowsToAdapters(device, &voltha.Flows{Items: updatedFlows}, &voltha.FlowGroups{Items: updatedGroups}, chAdapters) |
| 461 | } else { |
| 462 | var flowsToAdd []*ofp.OfpFlowStats |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 463 | var flowsToDelete []*ofp.OfpFlowStats |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 464 | var groupsToAdd []*ofp.OfpGroupEntry |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 465 | var groupsToDelete []*ofp.OfpGroupEntry |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 466 | |
| 467 | // Process flows |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 468 | for _, flow := range updatedFlows { |
| 469 | if idx := fu.FindFlows(existingFlows.Items, flow); idx == -1 { |
| 470 | flowsToAdd = append(flowsToAdd, flow) |
| 471 | } |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 472 | } |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 473 | for _, flow := range existingFlows.Items { |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 474 | if idx := fu.FindFlows(updatedFlows, flow); idx != -1 { |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 475 | flowsToDelete = append(flowsToDelete, flow) |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | // Process groups |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 480 | for _, g := range updatedGroups { |
| 481 | if fu.FindGroup(existingGroups.Items, g.Desc.GroupId) == -1 { // does not exist now |
| 482 | groupsToAdd = append(groupsToAdd, g) |
| 483 | } |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 484 | } |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 485 | for _, group := range existingGroups.Items { |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 486 | if fu.FindGroup(updatedGroups, group.Desc.GroupId) != -1 { // does not exist now |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 487 | groupsToDelete = append(groupsToDelete, group) |
| 488 | } |
| 489 | } |
| 490 | |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 491 | log.Debugw("updating-flows-and-groups", |
| 492 | log.Fields{ |
| 493 | "deviceId": agent.deviceId, |
| 494 | "flowsToAdd": flowsToAdd, |
| 495 | "flowsToDelete": flowsToDelete, |
| 496 | "groupsToAdd": groupsToAdd, |
| 497 | "groupsToDelete": groupsToDelete, |
| 498 | }) |
| 499 | |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 500 | // Sanity check |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 501 | if (len(flowsToAdd) | len(flowsToDelete) | len(groupsToAdd) | len(groupsToDelete) | len(updatedGroups)) == 0 { |
| 502 | log.Debugw("nothing-to-update", log.Fields{"deviceId": agent.deviceId, "flows": updatedFlows, "groups": updatedGroups}) |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 503 | return nil |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 504 | } |
| 505 | |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 506 | flowChanges := &ofp.FlowChanges{ |
| 507 | ToAdd: &voltha.Flows{Items: flowsToAdd}, |
| 508 | ToRemove: &voltha.Flows{Items: flowsToDelete}, |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 509 | } |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 510 | groupChanges := &ofp.FlowGroupChanges{ |
| 511 | ToAdd: &voltha.FlowGroups{Items: groupsToAdd}, |
| 512 | ToRemove: &voltha.FlowGroups{Items: groupsToDelete}, |
| 513 | ToUpdate: &voltha.FlowGroups{Items: updatedGroups}, |
| 514 | } |
| 515 | go agent.sendIncrementalFlowsToAdapters(device, flowChanges, groupChanges, chAdapters) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 516 | } |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 517 | |
| 518 | // store the updated data |
| 519 | device.Flows = &voltha.Flows{Items: updatedFlows} |
| 520 | device.FlowGroups = &voltha.FlowGroups{Items: updatedGroups} |
| 521 | go agent.updateDeviceWithoutLockAsync(device, chdB) |
| 522 | |
| 523 | if res := fu.WaitForNilOrErrorResponses(agent.defaultTimeout, chAdapters, chdB); res != nil { |
| 524 | return status.Errorf(codes.Aborted, "errors-%s", res) |
| 525 | } |
| 526 | return nil |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 527 | } |
| 528 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 529 | //disableDevice disable a device |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 530 | func (agent *DeviceAgent) disableDevice(ctx context.Context) error { |
khenaidoo | 59ef7be | 2019-06-21 12:40:28 -0400 | [diff] [blame] | 531 | agent.lockDevice.Lock() |
| 532 | defer agent.lockDevice.Unlock() |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 533 | log.Debugw("disableDevice", log.Fields{"id": agent.deviceId}) |
| 534 | // Get the most up to date the device info |
| 535 | if device, err := agent.getDeviceWithoutLock(); err != nil { |
| 536 | return status.Errorf(codes.NotFound, "%s", agent.deviceId) |
| 537 | } else { |
| 538 | if device.AdminState == voltha.AdminState_DISABLED { |
| 539 | log.Debugw("device-already-disabled", log.Fields{"id": agent.deviceId}) |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 540 | return nil |
| 541 | } |
khenaidoo | 4554f7c | 2019-05-29 22:13:15 -0400 | [diff] [blame] | 542 | if device.AdminState == voltha.AdminState_PREPROVISIONED || |
| 543 | device.AdminState == voltha.AdminState_DELETED { |
| 544 | log.Debugw("device-not-enabled", log.Fields{"id": agent.deviceId}) |
| 545 | return status.Errorf(codes.FailedPrecondition, "deviceId:%s, invalid-admin-state:%s", agent.deviceId, device.AdminState) |
| 546 | } |
| 547 | |
khenaidoo | 59ef7be | 2019-06-21 12:40:28 -0400 | [diff] [blame] | 548 | // Update the Admin State and operational state before sending the request out |
| 549 | cloned := proto.Clone(device).(*voltha.Device) |
| 550 | cloned.AdminState = voltha.AdminState_DISABLED |
| 551 | cloned.OperStatus = voltha.OperStatus_UNKNOWN |
Mahir Gunyel | b585167 | 2019-07-24 10:46:26 +0300 | [diff] [blame^] | 552 | if err := agent.updateDeviceInStoreWithoutLock(cloned, false, ""); err != nil { |
| 553 | return err |
khenaidoo | 59ef7be | 2019-06-21 12:40:28 -0400 | [diff] [blame] | 554 | } |
| 555 | |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 556 | if err := agent.adapterProxy.DisableDevice(ctx, device); err != nil { |
| 557 | log.Debugw("disableDevice-error", log.Fields{"id": agent.lastData.Id, "error": err}) |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 558 | return err |
| 559 | } |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 560 | } |
| 561 | return nil |
| 562 | } |
| 563 | |
| 564 | func (agent *DeviceAgent) updateAdminState(adminState voltha.AdminState_AdminState) error { |
| 565 | agent.lockDevice.Lock() |
| 566 | defer agent.lockDevice.Unlock() |
| 567 | log.Debugw("updateAdminState", log.Fields{"id": agent.deviceId}) |
| 568 | // Get the most up to date the device info |
| 569 | if device, err := agent.getDeviceWithoutLock(); err != nil { |
| 570 | return status.Errorf(codes.NotFound, "%s", agent.deviceId) |
| 571 | } else { |
| 572 | if device.AdminState == adminState { |
| 573 | log.Debugw("no-change-needed", log.Fields{"id": agent.deviceId, "state": adminState}) |
| 574 | return nil |
| 575 | } |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 576 | // Received an Ack (no error found above). Now update the device in the model to the expected state |
| 577 | cloned := proto.Clone(device).(*voltha.Device) |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 578 | cloned.AdminState = adminState |
Mahir Gunyel | b585167 | 2019-07-24 10:46:26 +0300 | [diff] [blame^] | 579 | if err := agent.updateDeviceInStoreWithoutLock(cloned, false, ""); err != nil { |
| 580 | return err |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 581 | } |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 582 | } |
| 583 | return nil |
| 584 | } |
| 585 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 586 | func (agent *DeviceAgent) rebootDevice(ctx context.Context) error { |
| 587 | agent.lockDevice.Lock() |
| 588 | defer agent.lockDevice.Unlock() |
| 589 | log.Debugw("rebootDevice", log.Fields{"id": agent.deviceId}) |
| 590 | // Get the most up to date the device info |
| 591 | if device, err := agent.getDeviceWithoutLock(); err != nil { |
| 592 | return status.Errorf(codes.NotFound, "%s", agent.deviceId) |
| 593 | } else { |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 594 | if err := agent.adapterProxy.RebootDevice(ctx, device); err != nil { |
| 595 | log.Debugw("rebootDevice-error", log.Fields{"id": agent.lastData.Id, "error": err}) |
| 596 | return err |
| 597 | } |
| 598 | } |
| 599 | return nil |
| 600 | } |
| 601 | |
| 602 | func (agent *DeviceAgent) deleteDevice(ctx context.Context) error { |
| 603 | agent.lockDevice.Lock() |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 604 | defer agent.lockDevice.Unlock() |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 605 | log.Debugw("deleteDevice", log.Fields{"id": agent.deviceId}) |
| 606 | // Get the most up to date the device info |
| 607 | if device, err := agent.getDeviceWithoutLock(); err != nil { |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 608 | return status.Errorf(codes.NotFound, "%s", agent.deviceId) |
| 609 | } else { |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 610 | if device.AdminState == voltha.AdminState_DELETED { |
| 611 | log.Debugw("device-already-in-deleted-state", log.Fields{"id": agent.deviceId}) |
| 612 | return nil |
| 613 | } |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 614 | if (device.AdminState != voltha.AdminState_DISABLED) && |
| 615 | (device.AdminState != voltha.AdminState_PREPROVISIONED) { |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 616 | log.Debugw("device-not-disabled", log.Fields{"id": agent.deviceId}) |
| 617 | //TODO: Needs customized error message |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 618 | return status.Errorf(codes.FailedPrecondition, "deviceId:%s, expected-admin-state:%s", agent.deviceId, voltha.AdminState_DISABLED) |
| 619 | } |
khenaidoo | 4554f7c | 2019-05-29 22:13:15 -0400 | [diff] [blame] | 620 | if device.AdminState != voltha.AdminState_PREPROVISIONED { |
| 621 | // Send the request to an Adapter only if the device is not in poreporovision state and wait for a response |
| 622 | if err := agent.adapterProxy.DeleteDevice(ctx, device); err != nil { |
| 623 | log.Debugw("deleteDevice-error", log.Fields{"id": agent.lastData.Id, "error": err}) |
| 624 | return err |
| 625 | } |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 626 | } |
khenaidoo | 59ef7be | 2019-06-21 12:40:28 -0400 | [diff] [blame] | 627 | // Set the state to deleted after we recieve an Ack - this will trigger some background process to clean up |
| 628 | // the device as well as its association with the logical device |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 629 | cloned := proto.Clone(device).(*voltha.Device) |
| 630 | cloned.AdminState = voltha.AdminState_DELETED |
Mahir Gunyel | b585167 | 2019-07-24 10:46:26 +0300 | [diff] [blame^] | 631 | if err := agent.updateDeviceInStoreWithoutLock(cloned, false, ""); err != nil { |
| 632 | return err |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 633 | } |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 634 | // If this is a child device then remove the associated peer ports on the parent device |
| 635 | if !device.Root { |
| 636 | go agent.deviceMgr.deletePeerPorts(device.ParentId, device.Id) |
| 637 | } |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 638 | } |
| 639 | return nil |
| 640 | } |
| 641 | |
khenaidoo | b312747 | 2019-07-24 21:04:55 -0400 | [diff] [blame] | 642 | func (agent *DeviceAgent) updatePmConfigs(ctx context.Context, pmConfigs *voltha.PmConfigs) error { |
| 643 | agent.lockDevice.Lock() |
| 644 | defer agent.lockDevice.Unlock() |
| 645 | log.Debugw("updatePmConfigs", log.Fields{"id": pmConfigs.Id}) |
| 646 | // Work only on latest data |
| 647 | if storeDevice, err := agent.getDeviceWithoutLock(); err != nil { |
| 648 | return status.Errorf(codes.NotFound, "%s", agent.deviceId) |
| 649 | } else { |
| 650 | // clone the device |
| 651 | cloned := proto.Clone(storeDevice).(*voltha.Device) |
| 652 | cloned.PmConfigs = proto.Clone(pmConfigs).(*voltha.PmConfigs) |
| 653 | // Store the device |
Mahir Gunyel | b585167 | 2019-07-24 10:46:26 +0300 | [diff] [blame^] | 654 | if err := agent.updateDeviceInStoreWithoutLock(cloned, false, ""); err != nil { |
| 655 | return err |
khenaidoo | b312747 | 2019-07-24 21:04:55 -0400 | [diff] [blame] | 656 | } |
| 657 | // Send the request to the adapter |
| 658 | if err := agent.adapterProxy.UpdatePmConfigs(ctx, cloned, pmConfigs); err != nil { |
| 659 | log.Errorw("update-pm-configs-error", log.Fields{"id": agent.lastData.Id, "error": err}) |
| 660 | return err |
| 661 | } |
| 662 | return nil |
| 663 | } |
| 664 | } |
| 665 | |
| 666 | func (agent *DeviceAgent) initPmConfigs(pmConfigs *voltha.PmConfigs) error { |
| 667 | agent.lockDevice.Lock() |
| 668 | defer agent.lockDevice.Unlock() |
| 669 | log.Debugw("initPmConfigs", log.Fields{"id": pmConfigs.Id}) |
| 670 | // Work only on latest data |
| 671 | if storeDevice, err := agent.getDeviceWithoutLock(); err != nil { |
| 672 | return status.Errorf(codes.NotFound, "%s", agent.deviceId) |
| 673 | } else { |
| 674 | // clone the device |
| 675 | cloned := proto.Clone(storeDevice).(*voltha.Device) |
| 676 | cloned.PmConfigs = proto.Clone(pmConfigs).(*voltha.PmConfigs) |
| 677 | // Store the device |
| 678 | updateCtx := context.WithValue(context.Background(), model.RequestTimestamp, time.Now().UnixNano()) |
| 679 | afterUpdate := agent.clusterDataProxy.Update(updateCtx, "/devices/"+agent.deviceId, cloned, false, "") |
| 680 | if afterUpdate == nil { |
| 681 | return status.Errorf(codes.Internal, "%s", agent.deviceId) |
| 682 | } |
| 683 | return nil |
| 684 | } |
| 685 | } |
| 686 | |
| 687 | func (agent *DeviceAgent) listPmConfigs(ctx context.Context) (*voltha.PmConfigs, error) { |
| 688 | agent.lockDevice.RLock() |
| 689 | defer agent.lockDevice.RUnlock() |
| 690 | log.Debugw("listPmConfigs", log.Fields{"id": agent.deviceId}) |
| 691 | // Get the most up to date the device info |
| 692 | if device, err := agent.getDeviceWithoutLock(); err != nil { |
| 693 | return nil, status.Errorf(codes.NotFound, "%s", agent.deviceId) |
| 694 | } else { |
| 695 | cloned := proto.Clone(device).(*voltha.Device) |
| 696 | return cloned.PmConfigs, nil |
| 697 | } |
| 698 | } |
| 699 | |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 700 | func (agent *DeviceAgent) downloadImage(ctx context.Context, img *voltha.ImageDownload) (*voltha.OperationResp, error) { |
| 701 | agent.lockDevice.Lock() |
| 702 | defer agent.lockDevice.Unlock() |
| 703 | log.Debugw("downloadImage", log.Fields{"id": agent.deviceId}) |
| 704 | // Get the most up to date the device info |
| 705 | if device, err := agent.getDeviceWithoutLock(); err != nil { |
| 706 | return nil, status.Errorf(codes.NotFound, "%s", agent.deviceId) |
| 707 | } else { |
| 708 | if device.AdminState != voltha.AdminState_ENABLED { |
| 709 | log.Debugw("device-not-enabled", log.Fields{"id": agent.deviceId}) |
| 710 | return nil, status.Errorf(codes.FailedPrecondition, "deviceId:%s, expected-admin-state:%s", agent.deviceId, voltha.AdminState_ENABLED) |
| 711 | } |
| 712 | // Save the image |
| 713 | clonedImg := proto.Clone(img).(*voltha.ImageDownload) |
Stephane Barbarie | df5479f | 2019-01-29 22:13:00 -0500 | [diff] [blame] | 714 | clonedImg.DownloadState = voltha.ImageDownload_DOWNLOAD_REQUESTED |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 715 | cloned := proto.Clone(device).(*voltha.Device) |
| 716 | if cloned.ImageDownloads == nil { |
| 717 | cloned.ImageDownloads = []*voltha.ImageDownload{clonedImg} |
| 718 | } else { |
| 719 | cloned.ImageDownloads = append(cloned.ImageDownloads, clonedImg) |
| 720 | } |
| 721 | cloned.AdminState = voltha.AdminState_DOWNLOADING_IMAGE |
Mahir Gunyel | b585167 | 2019-07-24 10:46:26 +0300 | [diff] [blame^] | 722 | if err := agent.updateDeviceInStoreWithoutLock(cloned, false, ""); err != nil { |
| 723 | return nil, err |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 724 | } |
| 725 | // Send the request to the adapter |
| 726 | if err := agent.adapterProxy.DownloadImage(ctx, cloned, clonedImg); err != nil { |
| 727 | log.Debugw("downloadImage-error", log.Fields{"id": agent.lastData.Id, "error": err, "image": img.Name}) |
| 728 | return nil, err |
| 729 | } |
| 730 | } |
| 731 | return &voltha.OperationResp{Code: voltha.OperationResp_OPERATION_SUCCESS}, nil |
| 732 | } |
| 733 | |
| 734 | // isImageRegistered is a helper method to figure out if an image is already registered |
| 735 | func isImageRegistered(img *voltha.ImageDownload, device *voltha.Device) bool { |
| 736 | for _, image := range device.ImageDownloads { |
| 737 | if image.Id == img.Id && image.Name == img.Name { |
| 738 | return true |
| 739 | } |
| 740 | } |
| 741 | return false |
| 742 | } |
| 743 | |
| 744 | func (agent *DeviceAgent) cancelImageDownload(ctx context.Context, img *voltha.ImageDownload) (*voltha.OperationResp, error) { |
| 745 | agent.lockDevice.Lock() |
| 746 | defer agent.lockDevice.Unlock() |
| 747 | log.Debugw("cancelImageDownload", log.Fields{"id": agent.deviceId}) |
| 748 | // Get the most up to date the device info |
| 749 | if device, err := agent.getDeviceWithoutLock(); err != nil { |
| 750 | return nil, status.Errorf(codes.NotFound, "%s", agent.deviceId) |
| 751 | } else { |
| 752 | // Verify whether the Image is in the list of image being downloaded |
| 753 | if !isImageRegistered(img, device) { |
| 754 | return nil, status.Errorf(codes.FailedPrecondition, "deviceId:%s, image-not-registered:%s", agent.deviceId, img.Name) |
| 755 | } |
| 756 | |
| 757 | // Update image download state |
| 758 | cloned := proto.Clone(device).(*voltha.Device) |
| 759 | for _, image := range cloned.ImageDownloads { |
| 760 | if image.Id == img.Id && image.Name == img.Name { |
Stephane Barbarie | df5479f | 2019-01-29 22:13:00 -0500 | [diff] [blame] | 761 | image.DownloadState = voltha.ImageDownload_DOWNLOAD_CANCELLED |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 762 | } |
| 763 | } |
| 764 | |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 765 | if device.AdminState == voltha.AdminState_DOWNLOADING_IMAGE { |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 766 | // Set the device to Enabled |
| 767 | cloned.AdminState = voltha.AdminState_ENABLED |
Mahir Gunyel | b585167 | 2019-07-24 10:46:26 +0300 | [diff] [blame^] | 768 | if err := agent.updateDeviceInStoreWithoutLock(cloned, false, ""); err != nil { |
| 769 | return nil, err |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 770 | } |
khenaidoo | 59ef7be | 2019-06-21 12:40:28 -0400 | [diff] [blame] | 771 | // Send the request to teh adapter |
| 772 | if err := agent.adapterProxy.CancelImageDownload(ctx, device, img); err != nil { |
| 773 | log.Debugw("cancelImageDownload-error", log.Fields{"id": agent.lastData.Id, "error": err, "image": img.Name}) |
| 774 | return nil, err |
| 775 | } |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 776 | } |
| 777 | } |
| 778 | return &voltha.OperationResp{Code: voltha.OperationResp_OPERATION_SUCCESS}, nil |
serkant.uluderya | 334479d | 2019-04-10 08:26:15 -0700 | [diff] [blame] | 779 | } |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 780 | |
| 781 | func (agent *DeviceAgent) activateImage(ctx context.Context, img *voltha.ImageDownload) (*voltha.OperationResp, error) { |
| 782 | agent.lockDevice.Lock() |
| 783 | defer agent.lockDevice.Unlock() |
| 784 | log.Debugw("activateImage", log.Fields{"id": agent.deviceId}) |
| 785 | // Get the most up to date the device info |
| 786 | if device, err := agent.getDeviceWithoutLock(); err != nil { |
| 787 | return nil, status.Errorf(codes.NotFound, "%s", agent.deviceId) |
| 788 | } else { |
| 789 | // Verify whether the Image is in the list of image being downloaded |
| 790 | if !isImageRegistered(img, device) { |
| 791 | return nil, status.Errorf(codes.FailedPrecondition, "deviceId:%s, image-not-registered:%s", agent.deviceId, img.Name) |
| 792 | } |
| 793 | |
| 794 | if device.AdminState == voltha.AdminState_DOWNLOADING_IMAGE { |
| 795 | return nil, status.Errorf(codes.FailedPrecondition, "deviceId:%s, device-in-downloading-state:%s", agent.deviceId, img.Name) |
| 796 | } |
| 797 | // Update image download state |
| 798 | cloned := proto.Clone(device).(*voltha.Device) |
| 799 | for _, image := range cloned.ImageDownloads { |
| 800 | if image.Id == img.Id && image.Name == img.Name { |
| 801 | image.ImageState = voltha.ImageDownload_IMAGE_ACTIVATING |
| 802 | } |
| 803 | } |
| 804 | // Set the device to downloading_image |
| 805 | cloned.AdminState = voltha.AdminState_DOWNLOADING_IMAGE |
Mahir Gunyel | b585167 | 2019-07-24 10:46:26 +0300 | [diff] [blame^] | 806 | if err := agent.updateDeviceInStoreWithoutLock(cloned, false, ""); err != nil { |
| 807 | return nil, err |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 808 | } |
| 809 | |
| 810 | if err := agent.adapterProxy.ActivateImageUpdate(ctx, device, img); err != nil { |
| 811 | log.Debugw("activateImage-error", log.Fields{"id": agent.lastData.Id, "error": err, "image": img.Name}) |
| 812 | return nil, err |
| 813 | } |
| 814 | // The status of the AdminState will be changed following the update_download_status response from the adapter |
| 815 | // The image name will also be removed from the device list |
| 816 | } |
serkant.uluderya | 334479d | 2019-04-10 08:26:15 -0700 | [diff] [blame] | 817 | return &voltha.OperationResp{Code: voltha.OperationResp_OPERATION_SUCCESS}, nil |
| 818 | } |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 819 | |
| 820 | func (agent *DeviceAgent) revertImage(ctx context.Context, img *voltha.ImageDownload) (*voltha.OperationResp, error) { |
| 821 | agent.lockDevice.Lock() |
| 822 | defer agent.lockDevice.Unlock() |
| 823 | log.Debugw("revertImage", log.Fields{"id": agent.deviceId}) |
| 824 | // Get the most up to date the device info |
| 825 | if device, err := agent.getDeviceWithoutLock(); err != nil { |
| 826 | return nil, status.Errorf(codes.NotFound, "%s", agent.deviceId) |
| 827 | } else { |
| 828 | // Verify whether the Image is in the list of image being downloaded |
| 829 | if !isImageRegistered(img, device) { |
| 830 | return nil, status.Errorf(codes.FailedPrecondition, "deviceId:%s, image-not-registered:%s", agent.deviceId, img.Name) |
| 831 | } |
| 832 | |
| 833 | if device.AdminState != voltha.AdminState_ENABLED { |
| 834 | return nil, status.Errorf(codes.FailedPrecondition, "deviceId:%s, device-not-enabled-state:%s", agent.deviceId, img.Name) |
| 835 | } |
| 836 | // Update image download state |
| 837 | cloned := proto.Clone(device).(*voltha.Device) |
| 838 | for _, image := range cloned.ImageDownloads { |
| 839 | if image.Id == img.Id && image.Name == img.Name { |
| 840 | image.ImageState = voltha.ImageDownload_IMAGE_REVERTING |
| 841 | } |
| 842 | } |
Mahir Gunyel | b585167 | 2019-07-24 10:46:26 +0300 | [diff] [blame^] | 843 | |
| 844 | if err := agent.updateDeviceInStoreWithoutLock(cloned, false, ""); err != nil { |
| 845 | return nil, err |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 846 | } |
| 847 | |
| 848 | if err := agent.adapterProxy.RevertImageUpdate(ctx, device, img); err != nil { |
| 849 | log.Debugw("revertImage-error", log.Fields{"id": agent.lastData.Id, "error": err, "image": img.Name}) |
| 850 | return nil, err |
| 851 | } |
| 852 | } |
| 853 | return &voltha.OperationResp{Code: voltha.OperationResp_OPERATION_SUCCESS}, nil |
serkant.uluderya | 334479d | 2019-04-10 08:26:15 -0700 | [diff] [blame] | 854 | } |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 855 | |
| 856 | func (agent *DeviceAgent) getImageDownloadStatus(ctx context.Context, img *voltha.ImageDownload) (*voltha.ImageDownload, error) { |
| 857 | agent.lockDevice.Lock() |
| 858 | defer agent.lockDevice.Unlock() |
| 859 | log.Debugw("getImageDownloadStatus", log.Fields{"id": agent.deviceId}) |
| 860 | // Get the most up to date the device info |
| 861 | if device, err := agent.getDeviceWithoutLock(); err != nil { |
| 862 | return nil, status.Errorf(codes.NotFound, "%s", agent.deviceId) |
| 863 | } else { |
| 864 | if resp, err := agent.adapterProxy.GetImageDownloadStatus(ctx, device, img); err != nil { |
| 865 | log.Debugw("getImageDownloadStatus-error", log.Fields{"id": agent.lastData.Id, "error": err, "image": img.Name}) |
| 866 | return nil, err |
| 867 | } else { |
| 868 | return resp, nil |
| 869 | } |
| 870 | } |
| 871 | } |
| 872 | |
serkant.uluderya | 334479d | 2019-04-10 08:26:15 -0700 | [diff] [blame] | 873 | func (agent *DeviceAgent) updateImageDownload(img *voltha.ImageDownload) error { |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 874 | agent.lockDevice.Lock() |
| 875 | defer agent.lockDevice.Unlock() |
| 876 | log.Debugw("updateImageDownload", log.Fields{"id": agent.deviceId}) |
| 877 | // Get the most up to date the device info |
| 878 | if device, err := agent.getDeviceWithoutLock(); err != nil { |
| 879 | return status.Errorf(codes.NotFound, "%s", agent.deviceId) |
| 880 | } else { |
| 881 | // Update the image as well as remove it if the download was cancelled |
| 882 | cloned := proto.Clone(device).(*voltha.Device) |
| 883 | clonedImages := make([]*voltha.ImageDownload, len(cloned.ImageDownloads)) |
| 884 | for _, image := range cloned.ImageDownloads { |
| 885 | if image.Id == img.Id && image.Name == img.Name { |
Stephane Barbarie | df5479f | 2019-01-29 22:13:00 -0500 | [diff] [blame] | 886 | if image.DownloadState != voltha.ImageDownload_DOWNLOAD_CANCELLED { |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 887 | clonedImages = append(clonedImages, img) |
| 888 | } |
| 889 | } |
| 890 | } |
| 891 | cloned.ImageDownloads = clonedImages |
| 892 | // Set the Admin state to enabled if required |
Stephane Barbarie | df5479f | 2019-01-29 22:13:00 -0500 | [diff] [blame] | 893 | if (img.DownloadState != voltha.ImageDownload_DOWNLOAD_REQUESTED && |
| 894 | img.DownloadState != voltha.ImageDownload_DOWNLOAD_STARTED) || |
serkant.uluderya | 334479d | 2019-04-10 08:26:15 -0700 | [diff] [blame] | 895 | (img.ImageState != voltha.ImageDownload_IMAGE_ACTIVATING) { |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 896 | cloned.AdminState = voltha.AdminState_ENABLED |
| 897 | } |
| 898 | |
Mahir Gunyel | b585167 | 2019-07-24 10:46:26 +0300 | [diff] [blame^] | 899 | if err := agent.updateDeviceInStoreWithoutLock(cloned, false, ""); err != nil { |
| 900 | return err |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 901 | } |
| 902 | } |
| 903 | return nil |
| 904 | } |
| 905 | |
| 906 | func (agent *DeviceAgent) getImageDownload(ctx context.Context, img *voltha.ImageDownload) (*voltha.ImageDownload, error) { |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 907 | agent.lockDevice.RLock() |
| 908 | defer agent.lockDevice.RUnlock() |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 909 | log.Debugw("getImageDownload", log.Fields{"id": agent.deviceId}) |
| 910 | // Get the most up to date the device info |
| 911 | if device, err := agent.getDeviceWithoutLock(); err != nil { |
| 912 | return nil, status.Errorf(codes.NotFound, "%s", agent.deviceId) |
| 913 | } else { |
| 914 | for _, image := range device.ImageDownloads { |
| 915 | if image.Id == img.Id && image.Name == img.Name { |
| 916 | return image, nil |
| 917 | } |
| 918 | } |
| 919 | return nil, status.Errorf(codes.NotFound, "image-not-found:%s", img.Name) |
| 920 | } |
| 921 | } |
| 922 | |
| 923 | func (agent *DeviceAgent) listImageDownloads(ctx context.Context, deviceId string) (*voltha.ImageDownloads, error) { |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 924 | agent.lockDevice.RLock() |
| 925 | defer agent.lockDevice.RUnlock() |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 926 | log.Debugw("listImageDownloads", log.Fields{"id": agent.deviceId}) |
| 927 | // Get the most up to date the device info |
| 928 | if device, err := agent.getDeviceWithoutLock(); err != nil { |
| 929 | return nil, status.Errorf(codes.NotFound, "%s", agent.deviceId) |
| 930 | } else { |
serkant.uluderya | 334479d | 2019-04-10 08:26:15 -0700 | [diff] [blame] | 931 | return &voltha.ImageDownloads{Items: device.ImageDownloads}, nil |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 932 | } |
| 933 | } |
| 934 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 935 | // getPorts retrieves the ports information of the device based on the port type. |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 936 | func (agent *DeviceAgent) getPorts(ctx context.Context, portType voltha.Port_PortType) *voltha.Ports { |
| 937 | log.Debugw("getPorts", log.Fields{"id": agent.deviceId, "portType": portType}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 938 | ports := &voltha.Ports{} |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 939 | if device, _ := agent.deviceMgr.GetDevice(agent.deviceId); device != nil { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 940 | for _, port := range device.Ports { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 941 | if port.Type == portType { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 942 | ports.Items = append(ports.Items, port) |
| 943 | } |
| 944 | } |
| 945 | } |
| 946 | return ports |
| 947 | } |
| 948 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 949 | // getSwitchCapability is a helper method that a logical device agent uses to retrieve the switch capability of a |
| 950 | // parent device |
khenaidoo | 7923270 | 2018-12-04 11:00:41 -0500 | [diff] [blame] | 951 | func (agent *DeviceAgent) getSwitchCapability(ctx context.Context) (*ic.SwitchCapability, error) { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 952 | log.Debugw("getSwitchCapability", log.Fields{"deviceId": agent.deviceId}) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 953 | if device, err := agent.deviceMgr.GetDevice(agent.deviceId); device == nil { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 954 | return nil, err |
| 955 | } else { |
khenaidoo | 7923270 | 2018-12-04 11:00:41 -0500 | [diff] [blame] | 956 | var switchCap *ic.SwitchCapability |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 957 | var err error |
| 958 | if switchCap, err = agent.adapterProxy.GetOfpDeviceInfo(ctx, device); err != nil { |
| 959 | log.Debugw("getSwitchCapability-error", log.Fields{"id": device.Id, "error": err}) |
| 960 | return nil, err |
| 961 | } |
| 962 | return switchCap, nil |
| 963 | } |
| 964 | } |
| 965 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 966 | // getPortCapability is a helper method that a logical device agent uses to retrieve the port capability of a |
| 967 | // device |
khenaidoo | 7923270 | 2018-12-04 11:00:41 -0500 | [diff] [blame] | 968 | func (agent *DeviceAgent) getPortCapability(ctx context.Context, portNo uint32) (*ic.PortCapability, error) { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 969 | log.Debugw("getPortCapability", log.Fields{"deviceId": agent.deviceId}) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 970 | if device, err := agent.deviceMgr.GetDevice(agent.deviceId); device == nil { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 971 | return nil, err |
| 972 | } else { |
khenaidoo | 7923270 | 2018-12-04 11:00:41 -0500 | [diff] [blame] | 973 | var portCap *ic.PortCapability |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 974 | var err error |
| 975 | if portCap, err = agent.adapterProxy.GetOfpPortInfo(ctx, device, portNo); err != nil { |
| 976 | log.Debugw("getPortCapability-error", log.Fields{"id": device.Id, "error": err}) |
| 977 | return nil, err |
| 978 | } |
| 979 | return portCap, nil |
| 980 | } |
| 981 | } |
| 982 | |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 983 | func (agent *DeviceAgent) packetOut(outPort uint32, packet *ofp.OfpPacketOut) error { |
| 984 | // Send packet to adapter |
| 985 | if err := agent.adapterProxy.packetOut(agent.deviceType, agent.deviceId, outPort, packet); err != nil { |
| 986 | log.Debugw("packet-out-error", log.Fields{"id": agent.lastData.Id, "error": err}) |
| 987 | return err |
| 988 | } |
| 989 | return nil |
| 990 | } |
| 991 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 992 | // 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] | 993 | func (agent *DeviceAgent) processUpdate(args ...interface{}) interface{} { |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 994 | //// Run this callback in its own go routine |
| 995 | go func(args ...interface{}) interface{} { |
| 996 | var previous *voltha.Device |
| 997 | var current *voltha.Device |
| 998 | var ok bool |
| 999 | if len(args) == 2 { |
| 1000 | if previous, ok = args[0].(*voltha.Device); !ok { |
| 1001 | log.Errorw("invalid-callback-type", log.Fields{"data": args[0]}) |
| 1002 | return nil |
| 1003 | } |
| 1004 | if current, ok = args[1].(*voltha.Device); !ok { |
| 1005 | log.Errorw("invalid-callback-type", log.Fields{"data": args[1]}) |
| 1006 | return nil |
| 1007 | } |
| 1008 | } else { |
| 1009 | log.Errorw("too-many-args-in-callback", log.Fields{"len": len(args)}) |
| 1010 | return nil |
| 1011 | } |
| 1012 | // Perform the state transition in it's own go routine |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 1013 | if err := agent.deviceMgr.processTransition(previous, current); err != nil { |
| 1014 | log.Errorw("failed-process-transition", log.Fields{"deviceId": previous.Id, |
| 1015 | "previousAdminState": previous.AdminState, "currentAdminState": current.AdminState}) |
| 1016 | } |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 1017 | return nil |
| 1018 | }(args...) |
| 1019 | |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1020 | return nil |
| 1021 | } |
| 1022 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1023 | func (agent *DeviceAgent) updateDevice(device *voltha.Device) error { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1024 | agent.lockDevice.Lock() |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 1025 | defer agent.lockDevice.Unlock() |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1026 | log.Debugw("updateDevice", log.Fields{"deviceId": device.Id}) |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 1027 | cloned := proto.Clone(device).(*voltha.Device) |
Mahir Gunyel | b585167 | 2019-07-24 10:46:26 +0300 | [diff] [blame^] | 1028 | return agent.updateDeviceInStoreWithoutLock(cloned, false, "") |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 1029 | } |
| 1030 | |
| 1031 | func (agent *DeviceAgent) updateDeviceWithoutLock(device *voltha.Device) error { |
| 1032 | log.Debugw("updateDevice", log.Fields{"deviceId": device.Id}) |
| 1033 | cloned := proto.Clone(device).(*voltha.Device) |
Mahir Gunyel | b585167 | 2019-07-24 10:46:26 +0300 | [diff] [blame^] | 1034 | return agent.updateDeviceInStoreWithoutLock(cloned, false, "") |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1035 | } |
| 1036 | |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1037 | func (agent *DeviceAgent) updateDeviceStatus(operStatus voltha.OperStatus_OperStatus, connStatus voltha.ConnectStatus_ConnectStatus) error { |
| 1038 | agent.lockDevice.Lock() |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 1039 | defer agent.lockDevice.Unlock() |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1040 | // Work only on latest data |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1041 | if storeDevice, err := agent.getDeviceWithoutLock(); err != nil { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1042 | return status.Errorf(codes.NotFound, "%s", agent.deviceId) |
| 1043 | } else { |
| 1044 | // clone the device |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1045 | cloned := proto.Clone(storeDevice).(*voltha.Device) |
| 1046 | // Ensure the enums passed in are valid - they will be invalid if they are not set when this function is invoked |
| 1047 | if s, ok := voltha.ConnectStatus_ConnectStatus_value[connStatus.String()]; ok { |
| 1048 | log.Debugw("updateDeviceStatus-conn", log.Fields{"ok": ok, "val": s}) |
| 1049 | cloned.ConnectStatus = connStatus |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1050 | } |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1051 | if s, ok := voltha.OperStatus_OperStatus_value[operStatus.String()]; ok { |
| 1052 | log.Debugw("updateDeviceStatus-oper", log.Fields{"ok": ok, "val": s}) |
| 1053 | cloned.OperStatus = operStatus |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1054 | } |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1055 | 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] | 1056 | // Store the device |
Mahir Gunyel | b585167 | 2019-07-24 10:46:26 +0300 | [diff] [blame^] | 1057 | return agent.updateDeviceInStoreWithoutLock(cloned, false, "") |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1058 | } |
| 1059 | } |
| 1060 | |
khenaidoo | 3ab3488 | 2019-05-02 21:33:30 -0400 | [diff] [blame] | 1061 | func (agent *DeviceAgent) enablePorts() error { |
| 1062 | agent.lockDevice.Lock() |
| 1063 | defer agent.lockDevice.Unlock() |
| 1064 | if storeDevice, err := agent.getDeviceWithoutLock(); err != nil { |
| 1065 | return status.Errorf(codes.NotFound, "%s", agent.deviceId) |
| 1066 | } else { |
| 1067 | // clone the device |
| 1068 | cloned := proto.Clone(storeDevice).(*voltha.Device) |
| 1069 | for _, port := range cloned.Ports { |
| 1070 | port.AdminState = voltha.AdminState_ENABLED |
| 1071 | port.OperStatus = voltha.OperStatus_ACTIVE |
| 1072 | } |
| 1073 | // Store the device |
Mahir Gunyel | b585167 | 2019-07-24 10:46:26 +0300 | [diff] [blame^] | 1074 | return agent.updateDeviceInStoreWithoutLock(cloned, false, "") |
khenaidoo | 3ab3488 | 2019-05-02 21:33:30 -0400 | [diff] [blame] | 1075 | } |
| 1076 | } |
| 1077 | |
| 1078 | func (agent *DeviceAgent) disablePorts() error { |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 1079 | log.Debugw("disablePorts", log.Fields{"deviceid": agent.deviceId}) |
khenaidoo | 3ab3488 | 2019-05-02 21:33:30 -0400 | [diff] [blame] | 1080 | agent.lockDevice.Lock() |
| 1081 | defer agent.lockDevice.Unlock() |
| 1082 | if storeDevice, err := agent.getDeviceWithoutLock(); err != nil { |
| 1083 | return status.Errorf(codes.NotFound, "%s", agent.deviceId) |
| 1084 | } else { |
| 1085 | // clone the device |
| 1086 | cloned := proto.Clone(storeDevice).(*voltha.Device) |
| 1087 | for _, port := range cloned.Ports { |
| 1088 | port.AdminState = voltha.AdminState_DISABLED |
| 1089 | port.OperStatus = voltha.OperStatus_UNKNOWN |
| 1090 | } |
| 1091 | // Store the device |
Mahir Gunyel | b585167 | 2019-07-24 10:46:26 +0300 | [diff] [blame^] | 1092 | return agent.updateDeviceInStoreWithoutLock(cloned, false, "") |
khenaidoo | 3ab3488 | 2019-05-02 21:33:30 -0400 | [diff] [blame] | 1093 | } |
| 1094 | } |
| 1095 | |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1096 | func (agent *DeviceAgent) updatePortState(portType voltha.Port_PortType, portNo uint32, operStatus voltha.OperStatus_OperStatus) error { |
| 1097 | agent.lockDevice.Lock() |
khenaidoo | 59ef7be | 2019-06-21 12:40:28 -0400 | [diff] [blame] | 1098 | defer agent.lockDevice.Unlock() |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1099 | // Work only on latest data |
| 1100 | // TODO: Get list of ports from device directly instead of the entire device |
| 1101 | if storeDevice, err := agent.getDeviceWithoutLock(); err != nil { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1102 | return status.Errorf(codes.NotFound, "%s", agent.deviceId) |
| 1103 | } else { |
| 1104 | // clone the device |
| 1105 | cloned := proto.Clone(storeDevice).(*voltha.Device) |
| 1106 | // Ensure the enums passed in are valid - they will be invalid if they are not set when this function is invoked |
| 1107 | if _, ok := voltha.Port_PortType_value[portType.String()]; !ok { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1108 | return status.Errorf(codes.InvalidArgument, "%s", portType) |
| 1109 | } |
| 1110 | for _, port := range cloned.Ports { |
| 1111 | if port.Type == portType && port.PortNo == portNo { |
| 1112 | port.OperStatus = operStatus |
| 1113 | // Set the admin status to ENABLED if the operational status is ACTIVE |
| 1114 | // TODO: Set by northbound system? |
| 1115 | if operStatus == voltha.OperStatus_ACTIVE { |
| 1116 | port.AdminState = voltha.AdminState_ENABLED |
| 1117 | } |
| 1118 | break |
| 1119 | } |
| 1120 | } |
| 1121 | log.Debugw("portStatusUpdate", log.Fields{"deviceId": cloned.Id}) |
| 1122 | // Store the device |
Mahir Gunyel | b585167 | 2019-07-24 10:46:26 +0300 | [diff] [blame^] | 1123 | return agent.updateDeviceInStoreWithoutLock(cloned, false, "") |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1124 | } |
| 1125 | } |
| 1126 | |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 1127 | func (agent *DeviceAgent) deleteAllPorts() error { |
| 1128 | log.Debugw("deleteAllPorts", log.Fields{"deviceId": agent.deviceId}) |
| 1129 | agent.lockDevice.Lock() |
| 1130 | defer agent.lockDevice.Unlock() |
| 1131 | // Work only on latest data |
| 1132 | if storeDevice, err := agent.getDeviceWithoutLock(); err != nil { |
| 1133 | return status.Errorf(codes.NotFound, "%s", agent.deviceId) |
| 1134 | } else { |
| 1135 | if storeDevice.AdminState != voltha.AdminState_DISABLED && storeDevice.AdminState != voltha.AdminState_DELETED { |
| 1136 | err = status.Error(codes.FailedPrecondition, fmt.Sprintf("invalid-state-%v", storeDevice.AdminState)) |
| 1137 | log.Warnw("invalid-state-removing-ports", log.Fields{"state": storeDevice.AdminState, "error": err}) |
| 1138 | return err |
| 1139 | } |
| 1140 | if len(storeDevice.Ports) == 0 { |
| 1141 | log.Debugw("no-ports-present", log.Fields{"deviceId": agent.deviceId}) |
| 1142 | return nil |
| 1143 | } |
| 1144 | // clone the device & set the fields to empty |
| 1145 | cloned := proto.Clone(storeDevice).(*voltha.Device) |
| 1146 | cloned.Ports = []*voltha.Port{} |
| 1147 | log.Debugw("portStatusUpdate", log.Fields{"deviceId": cloned.Id}) |
| 1148 | // Store the device |
Mahir Gunyel | b585167 | 2019-07-24 10:46:26 +0300 | [diff] [blame^] | 1149 | return agent.updateDeviceInStoreWithoutLock(cloned, false, "") |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 1150 | } |
| 1151 | } |
| 1152 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1153 | func (agent *DeviceAgent) addPort(port *voltha.Port) error { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1154 | agent.lockDevice.Lock() |
| 1155 | defer agent.lockDevice.Unlock() |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 1156 | log.Debugw("addPort", log.Fields{"deviceId": agent.deviceId}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1157 | // Work only on latest data |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1158 | if storeDevice, err := agent.getDeviceWithoutLock(); err != nil { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1159 | return status.Errorf(codes.NotFound, "%s", agent.deviceId) |
| 1160 | } else { |
| 1161 | // clone the device |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1162 | cloned := proto.Clone(storeDevice).(*voltha.Device) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1163 | if cloned.Ports == nil { |
| 1164 | // First port |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 1165 | log.Debugw("addPort-first-port-to-add", log.Fields{"deviceId": agent.deviceId}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1166 | cloned.Ports = make([]*voltha.Port, 0) |
manikkaraj k | 259a6f7 | 2019-05-06 09:55:44 -0400 | [diff] [blame] | 1167 | } else { |
| 1168 | for _, p := range cloned.Ports { |
| 1169 | if p.Type == port.Type && p.PortNo == port.PortNo { |
| 1170 | log.Debugw("port already exists", log.Fields{"port": *port}) |
| 1171 | return nil |
| 1172 | } |
| 1173 | } |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1174 | } |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1175 | cp := proto.Clone(port).(*voltha.Port) |
| 1176 | // Set the admin state of the port to ENABLE if the operational state is ACTIVE |
| 1177 | // TODO: Set by northbound system? |
| 1178 | if cp.OperStatus == voltha.OperStatus_ACTIVE { |
| 1179 | cp.AdminState = voltha.AdminState_ENABLED |
| 1180 | } |
| 1181 | cloned.Ports = append(cloned.Ports, cp) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1182 | // Store the device |
Mahir Gunyel | b585167 | 2019-07-24 10:46:26 +0300 | [diff] [blame^] | 1183 | return agent.updateDeviceInStoreWithoutLock(cloned, false, "") |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1184 | } |
| 1185 | } |
| 1186 | |
| 1187 | func (agent *DeviceAgent) addPeerPort(port *voltha.Port_PeerPort) error { |
| 1188 | agent.lockDevice.Lock() |
| 1189 | defer agent.lockDevice.Unlock() |
| 1190 | log.Debug("addPeerPort") |
| 1191 | // Work only on latest data |
| 1192 | if storeDevice, err := agent.getDeviceWithoutLock(); err != nil { |
| 1193 | return status.Errorf(codes.NotFound, "%s", agent.deviceId) |
| 1194 | } else { |
| 1195 | // clone the device |
| 1196 | cloned := proto.Clone(storeDevice).(*voltha.Device) |
| 1197 | // Get the peer port on the device based on the port no |
| 1198 | for _, peerPort := range cloned.Ports { |
| 1199 | if peerPort.PortNo == port.PortNo { // found port |
| 1200 | cp := proto.Clone(port).(*voltha.Port_PeerPort) |
| 1201 | peerPort.Peers = append(peerPort.Peers, cp) |
| 1202 | log.Debugw("found-peer", log.Fields{"portNo": port.PortNo, "deviceId": agent.deviceId}) |
| 1203 | break |
| 1204 | } |
| 1205 | } |
| 1206 | // Store the device |
Mahir Gunyel | b585167 | 2019-07-24 10:46:26 +0300 | [diff] [blame^] | 1207 | return agent.updateDeviceInStoreWithoutLock(cloned, false, "") |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1208 | } |
| 1209 | } |
| 1210 | |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 1211 | func (agent *DeviceAgent) deletePeerPorts(deviceId string) error { |
| 1212 | agent.lockDevice.Lock() |
| 1213 | defer agent.lockDevice.Unlock() |
| 1214 | log.Debug("deletePeerPorts") |
| 1215 | // Work only on latest data |
| 1216 | if storeDevice, err := agent.getDeviceWithoutLock(); err != nil { |
| 1217 | return status.Errorf(codes.NotFound, "%s", agent.deviceId) |
| 1218 | } else { |
| 1219 | // clone the device |
| 1220 | cloned := proto.Clone(storeDevice).(*voltha.Device) |
| 1221 | var updatedPeers []*voltha.Port_PeerPort |
| 1222 | for _, port := range cloned.Ports { |
| 1223 | updatedPeers = make([]*voltha.Port_PeerPort, 0) |
| 1224 | for _, peerPort := range port.Peers { |
| 1225 | if peerPort.DeviceId != deviceId { |
| 1226 | updatedPeers = append(updatedPeers, peerPort) |
| 1227 | } |
| 1228 | } |
| 1229 | port.Peers = updatedPeers |
| 1230 | } |
| 1231 | |
| 1232 | // Store the device with updated peer ports |
Mahir Gunyel | b585167 | 2019-07-24 10:46:26 +0300 | [diff] [blame^] | 1233 | return agent.updateDeviceInStoreWithoutLock(cloned, false, "") |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 1234 | } |
| 1235 | } |
| 1236 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1237 | // TODO: A generic device update by attribute |
| 1238 | func (agent *DeviceAgent) updateDeviceAttribute(name string, value interface{}) { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1239 | agent.lockDevice.Lock() |
| 1240 | defer agent.lockDevice.Unlock() |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1241 | if value == nil { |
| 1242 | return |
| 1243 | } |
| 1244 | var storeDevice *voltha.Device |
| 1245 | var err error |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1246 | if storeDevice, err = agent.getDeviceWithoutLock(); err != nil { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1247 | return |
| 1248 | } |
| 1249 | updated := false |
| 1250 | s := reflect.ValueOf(storeDevice).Elem() |
| 1251 | if s.Kind() == reflect.Struct { |
| 1252 | // exported field |
| 1253 | f := s.FieldByName(name) |
| 1254 | if f.IsValid() && f.CanSet() { |
| 1255 | switch f.Kind() { |
| 1256 | case reflect.String: |
| 1257 | f.SetString(value.(string)) |
| 1258 | updated = true |
| 1259 | case reflect.Uint32: |
| 1260 | f.SetUint(uint64(value.(uint32))) |
| 1261 | updated = true |
| 1262 | case reflect.Bool: |
| 1263 | f.SetBool(value.(bool)) |
| 1264 | updated = true |
| 1265 | } |
| 1266 | } |
| 1267 | } |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1268 | 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] | 1269 | // Save the data |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1270 | cloned := proto.Clone(storeDevice).(*voltha.Device) |
Mahir Gunyel | b585167 | 2019-07-24 10:46:26 +0300 | [diff] [blame^] | 1271 | if err = agent.updateDeviceInStoreWithoutLock(cloned, false, ""); err != nil { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1272 | log.Warnw("attribute-update-failed", log.Fields{"attribute": name, "value": value}) |
| 1273 | } |
| 1274 | return |
| 1275 | } |
serkant.uluderya | 334479d | 2019-04-10 08:26:15 -0700 | [diff] [blame] | 1276 | |
| 1277 | func (agent *DeviceAgent) simulateAlarm(ctx context.Context, simulatereq *voltha.SimulateAlarmRequest) error { |
| 1278 | agent.lockDevice.Lock() |
| 1279 | defer agent.lockDevice.Unlock() |
| 1280 | log.Debugw("simulateAlarm", log.Fields{"id": agent.deviceId}) |
| 1281 | // Get the most up to date the device info |
| 1282 | if device, err := agent.getDeviceWithoutLock(); err != nil { |
| 1283 | return status.Errorf(codes.NotFound, "%s", agent.deviceId) |
| 1284 | } else { |
| 1285 | // First send the request to an Adapter and wait for a response |
| 1286 | if err := agent.adapterProxy.SimulateAlarm(ctx, device, simulatereq); err != nil { |
| 1287 | log.Debugw("simulateAlarm-error", log.Fields{"id": agent.lastData.Id, "error": err}) |
| 1288 | return err |
| 1289 | } |
| 1290 | } |
| 1291 | return nil |
| 1292 | } |
Mahir Gunyel | b585167 | 2019-07-24 10:46:26 +0300 | [diff] [blame^] | 1293 | |
| 1294 | //This is an update operation to model without Lock.This function must never be invoked by another function unless the latter holds a lock on the device. |
| 1295 | // It is an internal helper function. |
| 1296 | func (agent *DeviceAgent) updateDeviceInStoreWithoutLock(device *voltha.Device, strict bool, txid string) error { |
| 1297 | updateCtx := context.WithValue(context.Background(), model.RequestTimestamp, time.Now().UnixNano()) |
| 1298 | if afterUpdate := agent.clusterDataProxy.Update(updateCtx, "/devices/"+agent.deviceId, device, strict, txid); afterUpdate == nil { |
| 1299 | return status.Errorf(codes.Internal, "failed-update-device:%s", agent.deviceId) |
| 1300 | } |
| 1301 | log.Debugw("updated-device-in-store", log.Fields{"deviceId: ": agent.deviceId}) |
| 1302 | |
| 1303 | return nil |
| 1304 | } |