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