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