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