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