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