Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 1 | package org.onlab.onos.store.device.impl; |
| 2 | |
| 3 | import com.google.common.collect.FluentIterable; |
| 4 | import com.google.common.collect.ImmutableList; |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 5 | import com.google.common.collect.Maps; |
| 6 | import com.google.common.collect.Sets; |
Madan Jampani | 47c9373 | 2014-10-06 20:46:08 -0700 | [diff] [blame] | 7 | |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 8 | import org.apache.commons.lang3.concurrent.ConcurrentException; |
| 9 | import org.apache.commons.lang3.concurrent.ConcurrentInitializer; |
| 10 | import org.apache.felix.scr.annotations.Activate; |
| 11 | import org.apache.felix.scr.annotations.Component; |
| 12 | import org.apache.felix.scr.annotations.Deactivate; |
| 13 | import org.apache.felix.scr.annotations.Reference; |
| 14 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 15 | import org.apache.felix.scr.annotations.Service; |
Yuta HIGUCHI | 39ede6a | 2014-10-03 15:23:33 -0700 | [diff] [blame] | 16 | import org.onlab.onos.net.AnnotationsUtil; |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 17 | import org.onlab.onos.net.DefaultAnnotations; |
| 18 | import org.onlab.onos.net.DefaultDevice; |
| 19 | import org.onlab.onos.net.DefaultPort; |
| 20 | import org.onlab.onos.net.Device; |
| 21 | import org.onlab.onos.net.Device.Type; |
| 22 | import org.onlab.onos.net.DeviceId; |
| 23 | import org.onlab.onos.net.Port; |
| 24 | import org.onlab.onos.net.PortNumber; |
| 25 | import org.onlab.onos.net.SparseAnnotations; |
| 26 | import org.onlab.onos.net.device.DefaultDeviceDescription; |
| 27 | import org.onlab.onos.net.device.DefaultPortDescription; |
| 28 | import org.onlab.onos.net.device.DeviceDescription; |
| 29 | import org.onlab.onos.net.device.DeviceEvent; |
| 30 | import org.onlab.onos.net.device.DeviceStore; |
| 31 | import org.onlab.onos.net.device.DeviceStoreDelegate; |
| 32 | import org.onlab.onos.net.device.PortDescription; |
| 33 | import org.onlab.onos.net.provider.ProviderId; |
| 34 | import org.onlab.onos.store.AbstractStore; |
| 35 | import org.onlab.onos.store.ClockService; |
| 36 | import org.onlab.onos.store.Timestamp; |
Madan Jampani | 47c9373 | 2014-10-06 20:46:08 -0700 | [diff] [blame] | 37 | import org.onlab.onos.store.cluster.messaging.ClusterCommunicationService; |
| 38 | import org.onlab.onos.store.cluster.messaging.ClusterMessage; |
| 39 | import org.onlab.onos.store.cluster.messaging.ClusterMessageHandler; |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 40 | import org.onlab.onos.store.common.impl.Timestamped; |
Yuta HIGUCHI | 0d6a5e6 | 2014-10-03 15:54:09 -0700 | [diff] [blame] | 41 | import org.onlab.util.NewConcurrentHashMap; |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 42 | import org.slf4j.Logger; |
| 43 | |
Madan Jampani | 47c9373 | 2014-10-06 20:46:08 -0700 | [diff] [blame] | 44 | import java.io.IOException; |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 45 | import java.util.ArrayList; |
| 46 | import java.util.Collections; |
| 47 | import java.util.HashSet; |
| 48 | import java.util.Iterator; |
| 49 | import java.util.List; |
| 50 | import java.util.Map; |
| 51 | import java.util.Map.Entry; |
| 52 | import java.util.Objects; |
| 53 | import java.util.Set; |
| 54 | import java.util.concurrent.ConcurrentHashMap; |
| 55 | import java.util.concurrent.ConcurrentMap; |
| 56 | import java.util.concurrent.atomic.AtomicReference; |
| 57 | |
| 58 | import static com.google.common.base.Preconditions.checkArgument; |
| 59 | import static com.google.common.base.Preconditions.checkNotNull; |
| 60 | import static com.google.common.base.Predicates.notNull; |
| 61 | import static org.onlab.onos.net.device.DeviceEvent.Type.*; |
| 62 | import static org.slf4j.LoggerFactory.getLogger; |
| 63 | import static org.apache.commons.lang3.concurrent.ConcurrentUtils.createIfAbsentUnchecked; |
| 64 | import static org.onlab.onos.net.DefaultAnnotations.merge; |
Yuta HIGUCHI | 885be1d | 2014-10-04 21:47:26 -0700 | [diff] [blame] | 65 | import static org.onlab.onos.net.DefaultAnnotations.union; |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 66 | import static com.google.common.base.Verify.verify; |
| 67 | |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 68 | // TODO: give me a better name |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 69 | /** |
| 70 | * Manages inventory of infrastructure devices using gossip protocol to distribute |
| 71 | * information. |
| 72 | */ |
| 73 | @Component(immediate = true) |
| 74 | @Service |
| 75 | public class GossipDeviceStore |
| 76 | extends AbstractStore<DeviceEvent, DeviceStoreDelegate> |
| 77 | implements DeviceStore { |
| 78 | |
| 79 | private final Logger log = getLogger(getClass()); |
| 80 | |
| 81 | public static final String DEVICE_NOT_FOUND = "Device with ID %s not found"; |
| 82 | |
| 83 | // TODO: Check if inner Map can be replaced with plain Map |
| 84 | // innerMap is used to lock a Device, thus instance should never be replaced. |
| 85 | // collection of Description given from various providers |
| 86 | private final ConcurrentMap<DeviceId, |
| 87 | ConcurrentMap<ProviderId, DeviceDescriptions>> |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 88 | deviceDescs = Maps.newConcurrentMap(); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 89 | |
| 90 | // cache of Device and Ports generated by compositing descriptions from providers |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 91 | private final ConcurrentMap<DeviceId, Device> devices = Maps.newConcurrentMap(); |
| 92 | private final ConcurrentMap<DeviceId, ConcurrentMap<PortNumber, Port>> devicePorts = Maps.newConcurrentMap(); |
| 93 | |
| 94 | // to be updated under Device lock |
| 95 | private final Map<DeviceId, Timestamp> offline = Maps.newHashMap(); |
| 96 | private final Map<DeviceId, Timestamp> removalRequest = Maps.newHashMap(); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 97 | |
| 98 | // available(=UP) devices |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 99 | private final Set<DeviceId> availableDevices = Sets.newConcurrentHashSet(); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 100 | |
| 101 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 102 | protected ClockService clockService; |
| 103 | |
Madan Jampani | 47c9373 | 2014-10-06 20:46:08 -0700 | [diff] [blame] | 104 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 105 | protected ClusterCommunicationService clusterCommunicator; |
| 106 | |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 107 | @Activate |
| 108 | public void activate() { |
Madan Jampani | 2206e01 | 2014-10-06 21:04:20 -0700 | [diff] [blame^] | 109 | clusterCommunicator.addSubscriber( |
| 110 | GossipDeviceStoreMessageSubjects.DEVICE_UPDATE, new InternalDeviceEventListener()); |
| 111 | clusterCommunicator.addSubscriber( |
| 112 | GossipDeviceStoreMessageSubjects.PORT_UPDATE, new InternalPortEventListener()); |
| 113 | clusterCommunicator.addSubscriber( |
| 114 | GossipDeviceStoreMessageSubjects.PORT_STATUS_UPDATE, new InternalPortStatusEventListener()); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 115 | log.info("Started"); |
| 116 | } |
| 117 | |
| 118 | @Deactivate |
| 119 | public void deactivate() { |
| 120 | deviceDescs.clear(); |
| 121 | devices.clear(); |
| 122 | devicePorts.clear(); |
| 123 | availableDevices.clear(); |
| 124 | log.info("Stopped"); |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public int getDeviceCount() { |
| 129 | return devices.size(); |
| 130 | } |
| 131 | |
| 132 | @Override |
| 133 | public Iterable<Device> getDevices() { |
| 134 | return Collections.unmodifiableCollection(devices.values()); |
| 135 | } |
| 136 | |
| 137 | @Override |
| 138 | public Device getDevice(DeviceId deviceId) { |
| 139 | return devices.get(deviceId); |
| 140 | } |
| 141 | |
| 142 | @Override |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 143 | public synchronized DeviceEvent createOrUpdateDevice(ProviderId providerId, |
| 144 | DeviceId deviceId, |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 145 | DeviceDescription deviceDescription) { |
| 146 | Timestamp newTimestamp = clockService.getTimestamp(deviceId); |
| 147 | final Timestamped<DeviceDescription> deltaDesc = new Timestamped<>(deviceDescription, newTimestamp); |
| 148 | DeviceEvent event = createOrUpdateDeviceInternal(providerId, deviceId, deltaDesc); |
| 149 | if (event != null) { |
Madan Jampani | 47c9373 | 2014-10-06 20:46:08 -0700 | [diff] [blame] | 150 | log.info("Notifying peers of a device update topology event for providerId: {} and deviceId: {}", |
| 151 | providerId, deviceId); |
| 152 | try { |
| 153 | notifyPeers(new InternalDeviceEvent(providerId, deviceId, deltaDesc)); |
| 154 | } catch (IOException e) { |
| 155 | log.error("Failed to notify peers of a device update topology event or providerId: " |
| 156 | + providerId + " and deviceId: " + deviceId, e); |
| 157 | } |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 158 | } |
| 159 | return event; |
| 160 | } |
| 161 | |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 162 | private DeviceEvent createOrUpdateDeviceInternal(ProviderId providerId, |
| 163 | DeviceId deviceId, |
| 164 | Timestamped<DeviceDescription> deltaDesc) { |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 165 | |
| 166 | // Collection of DeviceDescriptions for a Device |
| 167 | ConcurrentMap<ProviderId, DeviceDescriptions> providerDescs |
Yuta HIGUCHI | 0d6a5e6 | 2014-10-03 15:54:09 -0700 | [diff] [blame] | 168 | = getDeviceDescriptions(deviceId); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 169 | |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 170 | synchronized (providerDescs) { |
| 171 | // locking per device |
| 172 | |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 173 | if (isDeviceRemoved(deviceId, deltaDesc.timestamp())) { |
| 174 | log.debug("Ignoring outdated event: {}", deltaDesc); |
| 175 | return null; |
| 176 | } |
| 177 | |
| 178 | DeviceDescriptions descs |
| 179 | = createIfAbsentUnchecked(providerDescs, providerId, |
| 180 | new InitDeviceDescs(deltaDesc)); |
| 181 | |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 182 | final Device oldDevice = devices.get(deviceId); |
| 183 | final Device newDevice; |
| 184 | |
| 185 | if (deltaDesc == descs.getDeviceDesc() || |
| 186 | deltaDesc.isNewer(descs.getDeviceDesc())) { |
| 187 | // on new device or valid update |
| 188 | descs.putDeviceDesc(deltaDesc); |
| 189 | newDevice = composeDevice(deviceId, providerDescs); |
| 190 | } else { |
| 191 | // outdated event, ignored. |
| 192 | return null; |
| 193 | } |
| 194 | if (oldDevice == null) { |
| 195 | // ADD |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 196 | return createDevice(providerId, newDevice, deltaDesc.timestamp()); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 197 | } else { |
| 198 | // UPDATE or ignore (no change or stale) |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 199 | return updateDevice(providerId, oldDevice, newDevice, deltaDesc.timestamp()); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 200 | } |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | // Creates the device and returns the appropriate event if necessary. |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 205 | // Guarded by deviceDescs value (=Device lock) |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 206 | private DeviceEvent createDevice(ProviderId providerId, |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 207 | Device newDevice, Timestamp timestamp) { |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 208 | |
| 209 | // update composed device cache |
| 210 | Device oldDevice = devices.putIfAbsent(newDevice.id(), newDevice); |
| 211 | verify(oldDevice == null, |
| 212 | "Unexpected Device in cache. PID:%s [old=%s, new=%s]", |
| 213 | providerId, oldDevice, newDevice); |
| 214 | |
| 215 | if (!providerId.isAncillary()) { |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 216 | markOnline(newDevice.id(), timestamp); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | return new DeviceEvent(DeviceEvent.Type.DEVICE_ADDED, newDevice, null); |
| 220 | } |
| 221 | |
| 222 | // Updates the device and returns the appropriate event if necessary. |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 223 | // Guarded by deviceDescs value (=Device lock) |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 224 | private DeviceEvent updateDevice(ProviderId providerId, |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 225 | Device oldDevice, |
| 226 | Device newDevice, Timestamp newTimestamp) { |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 227 | |
| 228 | // We allow only certain attributes to trigger update |
| 229 | if (!Objects.equals(oldDevice.hwVersion(), newDevice.hwVersion()) || |
| 230 | !Objects.equals(oldDevice.swVersion(), newDevice.swVersion()) || |
Yuta HIGUCHI | 39ede6a | 2014-10-03 15:23:33 -0700 | [diff] [blame] | 231 | !AnnotationsUtil.isEqual(oldDevice.annotations(), newDevice.annotations())) { |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 232 | |
| 233 | boolean replaced = devices.replace(newDevice.id(), oldDevice, newDevice); |
| 234 | if (!replaced) { |
| 235 | verify(replaced, |
| 236 | "Replacing devices cache failed. PID:%s [expected:%s, found:%s, new=%s]", |
| 237 | providerId, oldDevice, devices.get(newDevice.id()) |
| 238 | , newDevice); |
| 239 | } |
| 240 | if (!providerId.isAncillary()) { |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 241 | markOnline(newDevice.id(), newTimestamp); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 242 | } |
| 243 | return new DeviceEvent(DeviceEvent.Type.DEVICE_UPDATED, newDevice, null); |
| 244 | } |
| 245 | |
| 246 | // Otherwise merely attempt to change availability if primary provider |
| 247 | if (!providerId.isAncillary()) { |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 248 | boolean added = markOnline(newDevice.id(), newTimestamp); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 249 | return !added ? null : |
| 250 | new DeviceEvent(DEVICE_AVAILABILITY_CHANGED, newDevice, null); |
| 251 | } |
| 252 | return null; |
| 253 | } |
| 254 | |
| 255 | @Override |
| 256 | public DeviceEvent markOffline(DeviceId deviceId) { |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 257 | Timestamp timestamp = clockService.getTimestamp(deviceId); |
| 258 | return markOfflineInternal(deviceId, timestamp); |
| 259 | } |
| 260 | |
| 261 | private DeviceEvent markOfflineInternal(DeviceId deviceId, Timestamp timestamp) { |
| 262 | |
| 263 | Map<ProviderId, DeviceDescriptions> providerDescs |
Yuta HIGUCHI | 0d6a5e6 | 2014-10-03 15:54:09 -0700 | [diff] [blame] | 264 | = getDeviceDescriptions(deviceId); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 265 | |
| 266 | // locking device |
| 267 | synchronized (providerDescs) { |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 268 | |
| 269 | // accept off-line if given timestamp is newer than |
| 270 | // the latest Timestamp from Primary provider |
| 271 | DeviceDescriptions primDescs = getPrimaryDescriptions(providerDescs); |
| 272 | Timestamp lastTimestamp = primDescs.getLatestTimestamp(); |
| 273 | if (timestamp.compareTo(lastTimestamp) <= 0) { |
| 274 | // outdated event ignore |
| 275 | return null; |
| 276 | } |
| 277 | |
| 278 | offline.put(deviceId, timestamp); |
| 279 | |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 280 | Device device = devices.get(deviceId); |
| 281 | if (device == null) { |
| 282 | return null; |
| 283 | } |
| 284 | boolean removed = availableDevices.remove(deviceId); |
| 285 | if (removed) { |
| 286 | // TODO: broadcast ... DOWN only? |
| 287 | return new DeviceEvent(DEVICE_AVAILABILITY_CHANGED, device, null); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 288 | } |
| 289 | return null; |
| 290 | } |
| 291 | } |
| 292 | |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 293 | /** |
| 294 | * Marks the device as available if the given timestamp is not outdated, |
| 295 | * compared to the time the device has been marked offline. |
| 296 | * |
| 297 | * @param deviceId identifier of the device |
| 298 | * @param timestamp of the event triggering this change. |
| 299 | * @return true if availability change request was accepted and changed the state |
| 300 | */ |
| 301 | // Guarded by deviceDescs value (=Device lock) |
| 302 | private boolean markOnline(DeviceId deviceId, Timestamp timestamp) { |
| 303 | // accept on-line if given timestamp is newer than |
| 304 | // the latest offline request Timestamp |
| 305 | Timestamp offlineTimestamp = offline.get(deviceId); |
| 306 | if (offlineTimestamp == null || |
| 307 | offlineTimestamp.compareTo(timestamp) < 0) { |
| 308 | |
| 309 | offline.remove(deviceId); |
| 310 | return availableDevices.add(deviceId); |
| 311 | } |
| 312 | return false; |
| 313 | } |
| 314 | |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 315 | @Override |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 316 | public synchronized List<DeviceEvent> updatePorts(ProviderId providerId, |
| 317 | DeviceId deviceId, |
| 318 | List<PortDescription> portDescriptions) { |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 319 | Timestamp newTimestamp = clockService.getTimestamp(deviceId); |
| 320 | |
Madan Jampani | 47c9373 | 2014-10-06 20:46:08 -0700 | [diff] [blame] | 321 | Timestamped<List<PortDescription>> timestampedPortDescriptions = |
| 322 | new Timestamped<>(portDescriptions, newTimestamp); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 323 | |
Madan Jampani | 47c9373 | 2014-10-06 20:46:08 -0700 | [diff] [blame] | 324 | List<DeviceEvent> events = updatePortsInternal(providerId, deviceId, timestampedPortDescriptions); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 325 | if (!events.isEmpty()) { |
Madan Jampani | 47c9373 | 2014-10-06 20:46:08 -0700 | [diff] [blame] | 326 | log.info("Notifying peers of a port update topology event for providerId: {} and deviceId: {}", |
| 327 | providerId, deviceId); |
| 328 | try { |
| 329 | notifyPeers(new InternalPortEvent(providerId, deviceId, timestampedPortDescriptions)); |
| 330 | } catch (IOException e) { |
| 331 | log.error("Failed to notify peers of a port update topology event or providerId: " |
| 332 | + providerId + " and deviceId: " + deviceId, e); |
| 333 | } |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 334 | } |
| 335 | return events; |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 336 | } |
| 337 | |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 338 | private List<DeviceEvent> updatePortsInternal(ProviderId providerId, |
| 339 | DeviceId deviceId, |
| 340 | Timestamped<List<PortDescription>> portDescriptions) { |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 341 | |
| 342 | Device device = devices.get(deviceId); |
| 343 | checkArgument(device != null, DEVICE_NOT_FOUND, deviceId); |
| 344 | |
| 345 | ConcurrentMap<ProviderId, DeviceDescriptions> descsMap = deviceDescs.get(deviceId); |
| 346 | checkArgument(descsMap != null, DEVICE_NOT_FOUND, deviceId); |
| 347 | |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 348 | List<DeviceEvent> events = new ArrayList<>(); |
| 349 | synchronized (descsMap) { |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 350 | |
| 351 | if (isDeviceRemoved(deviceId, portDescriptions.timestamp())) { |
| 352 | log.debug("Ignoring outdated events: {}", portDescriptions); |
| 353 | return null; |
| 354 | } |
| 355 | |
| 356 | DeviceDescriptions descs = descsMap.get(providerId); |
| 357 | // every provider must provide DeviceDescription. |
| 358 | checkArgument(descs != null, |
| 359 | "Device description for Device ID %s from Provider %s was not found", |
| 360 | deviceId, providerId); |
| 361 | |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 362 | Map<PortNumber, Port> ports = getPortMap(deviceId); |
| 363 | |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 364 | final Timestamp newTimestamp = portDescriptions.timestamp(); |
| 365 | |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 366 | // Add new ports |
| 367 | Set<PortNumber> processed = new HashSet<>(); |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 368 | for (PortDescription portDescription : portDescriptions.value()) { |
| 369 | final PortNumber number = portDescription.portNumber(); |
| 370 | processed.add(number); |
| 371 | |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 372 | final Port oldPort = ports.get(number); |
| 373 | final Port newPort; |
| 374 | |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 375 | |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 376 | final Timestamped<PortDescription> existingPortDesc = descs.getPortDesc(number); |
| 377 | if (existingPortDesc == null || |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 378 | newTimestamp.compareTo(existingPortDesc.timestamp()) >= 0) { |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 379 | // on new port or valid update |
| 380 | // update description |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 381 | descs.putPortDesc(new Timestamped<>(portDescription, |
| 382 | portDescriptions.timestamp())); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 383 | newPort = composePort(device, number, descsMap); |
| 384 | } else { |
| 385 | // outdated event, ignored. |
| 386 | continue; |
| 387 | } |
| 388 | |
| 389 | events.add(oldPort == null ? |
| 390 | createPort(device, newPort, ports) : |
| 391 | updatePort(device, oldPort, newPort, ports)); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | events.addAll(pruneOldPorts(device, ports, processed)); |
| 395 | } |
| 396 | return FluentIterable.from(events).filter(notNull()).toList(); |
| 397 | } |
| 398 | |
| 399 | // Creates a new port based on the port description adds it to the map and |
| 400 | // Returns corresponding event. |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 401 | // Guarded by deviceDescs value (=Device lock) |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 402 | private DeviceEvent createPort(Device device, Port newPort, |
| 403 | Map<PortNumber, Port> ports) { |
| 404 | ports.put(newPort.number(), newPort); |
| 405 | return new DeviceEvent(PORT_ADDED, device, newPort); |
| 406 | } |
| 407 | |
| 408 | // Checks if the specified port requires update and if so, it replaces the |
| 409 | // existing entry in the map and returns corresponding event. |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 410 | // Guarded by deviceDescs value (=Device lock) |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 411 | private DeviceEvent updatePort(Device device, Port oldPort, |
| 412 | Port newPort, |
| 413 | Map<PortNumber, Port> ports) { |
| 414 | if (oldPort.isEnabled() != newPort.isEnabled() || |
Yuta HIGUCHI | 39ede6a | 2014-10-03 15:23:33 -0700 | [diff] [blame] | 415 | !AnnotationsUtil.isEqual(oldPort.annotations(), newPort.annotations())) { |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 416 | |
| 417 | ports.put(oldPort.number(), newPort); |
| 418 | return new DeviceEvent(PORT_UPDATED, device, newPort); |
| 419 | } |
| 420 | return null; |
| 421 | } |
| 422 | |
| 423 | // Prunes the specified list of ports based on which ports are in the |
| 424 | // processed list and returns list of corresponding events. |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 425 | // Guarded by deviceDescs value (=Device lock) |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 426 | private List<DeviceEvent> pruneOldPorts(Device device, |
| 427 | Map<PortNumber, Port> ports, |
| 428 | Set<PortNumber> processed) { |
| 429 | List<DeviceEvent> events = new ArrayList<>(); |
| 430 | Iterator<PortNumber> iterator = ports.keySet().iterator(); |
| 431 | while (iterator.hasNext()) { |
| 432 | PortNumber portNumber = iterator.next(); |
| 433 | if (!processed.contains(portNumber)) { |
| 434 | events.add(new DeviceEvent(PORT_REMOVED, device, |
| 435 | ports.get(portNumber))); |
| 436 | iterator.remove(); |
| 437 | } |
| 438 | } |
| 439 | return events; |
| 440 | } |
| 441 | |
| 442 | // Gets the map of ports for the specified device; if one does not already |
| 443 | // exist, it creates and registers a new one. |
| 444 | private ConcurrentMap<PortNumber, Port> getPortMap(DeviceId deviceId) { |
| 445 | return createIfAbsentUnchecked(devicePorts, deviceId, |
Yuta HIGUCHI | 0d6a5e6 | 2014-10-03 15:54:09 -0700 | [diff] [blame] | 446 | NewConcurrentHashMap.<PortNumber, Port>ifNeeded()); |
| 447 | } |
| 448 | |
| 449 | private ConcurrentMap<ProviderId, DeviceDescriptions> getDeviceDescriptions( |
| 450 | DeviceId deviceId) { |
| 451 | return createIfAbsentUnchecked(deviceDescs, deviceId, |
| 452 | NewConcurrentHashMap.<ProviderId, DeviceDescriptions>ifNeeded()); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | @Override |
| 456 | public synchronized DeviceEvent updatePortStatus(ProviderId providerId, DeviceId deviceId, |
| 457 | PortDescription portDescription) { |
| 458 | Timestamp newTimestamp = clockService.getTimestamp(deviceId); |
| 459 | final Timestamped<PortDescription> deltaDesc = new Timestamped<>(portDescription, newTimestamp); |
| 460 | DeviceEvent event = updatePortStatusInternal(providerId, deviceId, deltaDesc); |
| 461 | if (event != null) { |
Madan Jampani | 47c9373 | 2014-10-06 20:46:08 -0700 | [diff] [blame] | 462 | log.info("Notifying peers of a port status update topology event for providerId: {} and deviceId: {}", |
| 463 | providerId, deviceId); |
| 464 | try { |
| 465 | notifyPeers(new InternalPortStatusEvent(providerId, deviceId, deltaDesc)); |
| 466 | } catch (IOException e) { |
| 467 | log.error("Failed to notify peers of a port status update topology event or providerId: " |
| 468 | + providerId + " and deviceId: " + deviceId, e); |
| 469 | } |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 470 | } |
| 471 | return event; |
| 472 | } |
| 473 | |
| 474 | private DeviceEvent updatePortStatusInternal(ProviderId providerId, DeviceId deviceId, |
| 475 | Timestamped<PortDescription> deltaDesc) { |
| 476 | |
| 477 | Device device = devices.get(deviceId); |
| 478 | checkArgument(device != null, DEVICE_NOT_FOUND, deviceId); |
| 479 | |
| 480 | ConcurrentMap<ProviderId, DeviceDescriptions> descsMap = deviceDescs.get(deviceId); |
| 481 | checkArgument(descsMap != null, DEVICE_NOT_FOUND, deviceId); |
| 482 | |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 483 | synchronized (descsMap) { |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 484 | |
| 485 | if (isDeviceRemoved(deviceId, deltaDesc.timestamp())) { |
| 486 | log.debug("Ignoring outdated event: {}", deltaDesc); |
| 487 | return null; |
| 488 | } |
| 489 | |
| 490 | DeviceDescriptions descs = descsMap.get(providerId); |
| 491 | // assuming all providers must to give DeviceDescription |
| 492 | checkArgument(descs != null, |
| 493 | "Device description for Device ID %s from Provider %s was not found", |
| 494 | deviceId, providerId); |
| 495 | |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 496 | ConcurrentMap<PortNumber, Port> ports = getPortMap(deviceId); |
| 497 | final PortNumber number = deltaDesc.value().portNumber(); |
| 498 | final Port oldPort = ports.get(number); |
| 499 | final Port newPort; |
| 500 | |
| 501 | final Timestamped<PortDescription> existingPortDesc = descs.getPortDesc(number); |
| 502 | if (existingPortDesc == null || |
| 503 | deltaDesc == existingPortDesc || |
| 504 | deltaDesc.isNewer(existingPortDesc)) { |
| 505 | // on new port or valid update |
| 506 | // update description |
| 507 | descs.putPortDesc(deltaDesc); |
| 508 | newPort = composePort(device, number, descsMap); |
| 509 | } else { |
| 510 | // outdated event, ignored. |
| 511 | return null; |
| 512 | } |
| 513 | |
| 514 | if (oldPort == null) { |
| 515 | return createPort(device, newPort, ports); |
| 516 | } else { |
| 517 | return updatePort(device, oldPort, newPort, ports); |
| 518 | } |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | @Override |
| 523 | public List<Port> getPorts(DeviceId deviceId) { |
| 524 | Map<PortNumber, Port> ports = devicePorts.get(deviceId); |
| 525 | if (ports == null) { |
| 526 | return Collections.emptyList(); |
| 527 | } |
| 528 | return ImmutableList.copyOf(ports.values()); |
| 529 | } |
| 530 | |
| 531 | @Override |
| 532 | public Port getPort(DeviceId deviceId, PortNumber portNumber) { |
| 533 | Map<PortNumber, Port> ports = devicePorts.get(deviceId); |
| 534 | return ports == null ? null : ports.get(portNumber); |
| 535 | } |
| 536 | |
| 537 | @Override |
| 538 | public boolean isAvailable(DeviceId deviceId) { |
| 539 | return availableDevices.contains(deviceId); |
| 540 | } |
| 541 | |
| 542 | @Override |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 543 | public synchronized DeviceEvent removeDevice(DeviceId deviceId) { |
| 544 | Timestamp timestamp = clockService.getTimestamp(deviceId); |
| 545 | DeviceEvent event = removeDeviceInternal(deviceId, timestamp); |
| 546 | // TODO: broadcast removal event |
| 547 | return event; |
| 548 | } |
| 549 | |
| 550 | private DeviceEvent removeDeviceInternal(DeviceId deviceId, |
| 551 | Timestamp timestamp) { |
| 552 | |
| 553 | Map<ProviderId, DeviceDescriptions> descs = getDeviceDescriptions(deviceId); |
Yuta HIGUCHI | 0d6a5e6 | 2014-10-03 15:54:09 -0700 | [diff] [blame] | 554 | synchronized (descs) { |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 555 | // accept removal request if given timestamp is newer than |
| 556 | // the latest Timestamp from Primary provider |
| 557 | DeviceDescriptions primDescs = getPrimaryDescriptions(descs); |
| 558 | Timestamp lastTimestamp = primDescs.getLatestTimestamp(); |
| 559 | if (timestamp.compareTo(lastTimestamp) <= 0) { |
| 560 | // outdated event ignore |
| 561 | return null; |
| 562 | } |
| 563 | removalRequest.put(deviceId, timestamp); |
| 564 | |
Yuta HIGUCHI | 0d6a5e6 | 2014-10-03 15:54:09 -0700 | [diff] [blame] | 565 | Device device = devices.remove(deviceId); |
| 566 | // should DEVICE_REMOVED carry removed ports? |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 567 | Map<PortNumber, Port> ports = devicePorts.get(deviceId); |
| 568 | if (ports != null) { |
| 569 | ports.clear(); |
| 570 | } |
| 571 | markOfflineInternal(deviceId, timestamp); |
Yuta HIGUCHI | 0d6a5e6 | 2014-10-03 15:54:09 -0700 | [diff] [blame] | 572 | descs.clear(); |
| 573 | return device == null ? null : |
| 574 | new DeviceEvent(DEVICE_REMOVED, device, null); |
| 575 | } |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 576 | } |
| 577 | |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 578 | private boolean isDeviceRemoved(DeviceId deviceId, Timestamp timestampToCheck) { |
| 579 | Timestamp removalTimestamp = removalRequest.get(deviceId); |
| 580 | if (removalTimestamp != null && |
| 581 | removalTimestamp.compareTo(timestampToCheck) >= 0) { |
| 582 | // removalRequest is more recent |
| 583 | return true; |
| 584 | } |
| 585 | return false; |
| 586 | } |
| 587 | |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 588 | /** |
| 589 | * Returns a Device, merging description given from multiple Providers. |
| 590 | * |
| 591 | * @param deviceId device identifier |
| 592 | * @param providerDescs Collection of Descriptions from multiple providers |
| 593 | * @return Device instance |
| 594 | */ |
| 595 | private Device composeDevice(DeviceId deviceId, |
| 596 | ConcurrentMap<ProviderId, DeviceDescriptions> providerDescs) { |
| 597 | |
| 598 | checkArgument(!providerDescs.isEmpty(), "No Device descriptions supplied"); |
| 599 | |
| 600 | ProviderId primary = pickPrimaryPID(providerDescs); |
| 601 | |
| 602 | DeviceDescriptions desc = providerDescs.get(primary); |
| 603 | |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 604 | final DeviceDescription base = desc.getDeviceDesc().value(); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 605 | Type type = base.type(); |
| 606 | String manufacturer = base.manufacturer(); |
| 607 | String hwVersion = base.hwVersion(); |
| 608 | String swVersion = base.swVersion(); |
| 609 | String serialNumber = base.serialNumber(); |
| 610 | DefaultAnnotations annotations = DefaultAnnotations.builder().build(); |
| 611 | annotations = merge(annotations, base.annotations()); |
| 612 | |
| 613 | for (Entry<ProviderId, DeviceDescriptions> e : providerDescs.entrySet()) { |
| 614 | if (e.getKey().equals(primary)) { |
| 615 | continue; |
| 616 | } |
| 617 | // TODO: should keep track of Description timestamp |
| 618 | // and only merge conflicting keys when timestamp is newer |
| 619 | // Currently assuming there will never be a key conflict between |
| 620 | // providers |
| 621 | |
| 622 | // annotation merging. not so efficient, should revisit later |
| 623 | annotations = merge(annotations, e.getValue().getDeviceDesc().value().annotations()); |
| 624 | } |
| 625 | |
| 626 | return new DefaultDevice(primary, deviceId , type, manufacturer, |
| 627 | hwVersion, swVersion, serialNumber, annotations); |
| 628 | } |
| 629 | |
| 630 | /** |
| 631 | * Returns a Port, merging description given from multiple Providers. |
| 632 | * |
| 633 | * @param device device the port is on |
| 634 | * @param number port number |
| 635 | * @param providerDescs Collection of Descriptions from multiple providers |
| 636 | * @return Port instance |
| 637 | */ |
| 638 | private Port composePort(Device device, PortNumber number, |
| 639 | ConcurrentMap<ProviderId, DeviceDescriptions> providerDescs) { |
| 640 | |
| 641 | ProviderId primary = pickPrimaryPID(providerDescs); |
| 642 | DeviceDescriptions primDescs = providerDescs.get(primary); |
| 643 | // if no primary, assume not enabled |
| 644 | // TODO: revisit this default port enabled/disabled behavior |
| 645 | boolean isEnabled = false; |
| 646 | DefaultAnnotations annotations = DefaultAnnotations.builder().build(); |
| 647 | |
| 648 | final Timestamped<PortDescription> portDesc = primDescs.getPortDesc(number); |
| 649 | if (portDesc != null) { |
| 650 | isEnabled = portDesc.value().isEnabled(); |
| 651 | annotations = merge(annotations, portDesc.value().annotations()); |
| 652 | } |
| 653 | |
| 654 | for (Entry<ProviderId, DeviceDescriptions> e : providerDescs.entrySet()) { |
| 655 | if (e.getKey().equals(primary)) { |
| 656 | continue; |
| 657 | } |
| 658 | // TODO: should keep track of Description timestamp |
| 659 | // and only merge conflicting keys when timestamp is newer |
| 660 | // Currently assuming there will never be a key conflict between |
| 661 | // providers |
| 662 | |
| 663 | // annotation merging. not so efficient, should revisit later |
| 664 | final Timestamped<PortDescription> otherPortDesc = e.getValue().getPortDesc(number); |
| 665 | if (otherPortDesc != null) { |
| 666 | annotations = merge(annotations, otherPortDesc.value().annotations()); |
| 667 | } |
| 668 | } |
| 669 | |
| 670 | return new DefaultPort(device, number, isEnabled, annotations); |
| 671 | } |
| 672 | |
| 673 | /** |
| 674 | * @return primary ProviderID, or randomly chosen one if none exists |
| 675 | */ |
| 676 | private ProviderId pickPrimaryPID( |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 677 | Map<ProviderId, DeviceDescriptions> providerDescs) { |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 678 | ProviderId fallBackPrimary = null; |
| 679 | for (Entry<ProviderId, DeviceDescriptions> e : providerDescs.entrySet()) { |
| 680 | if (!e.getKey().isAncillary()) { |
| 681 | return e.getKey(); |
| 682 | } else if (fallBackPrimary == null) { |
| 683 | // pick randomly as a fallback in case there is no primary |
| 684 | fallBackPrimary = e.getKey(); |
| 685 | } |
| 686 | } |
| 687 | return fallBackPrimary; |
| 688 | } |
| 689 | |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 690 | private DeviceDescriptions getPrimaryDescriptions( |
| 691 | Map<ProviderId, DeviceDescriptions> providerDescs) { |
| 692 | ProviderId pid = pickPrimaryPID(providerDescs); |
| 693 | return providerDescs.get(pid); |
| 694 | } |
| 695 | |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 696 | public static final class InitDeviceDescs |
| 697 | implements ConcurrentInitializer<DeviceDescriptions> { |
| 698 | |
| 699 | private final Timestamped<DeviceDescription> deviceDesc; |
| 700 | |
| 701 | public InitDeviceDescs(Timestamped<DeviceDescription> deviceDesc) { |
| 702 | this.deviceDesc = checkNotNull(deviceDesc); |
| 703 | } |
| 704 | @Override |
| 705 | public DeviceDescriptions get() throws ConcurrentException { |
| 706 | return new DeviceDescriptions(deviceDesc); |
| 707 | } |
| 708 | } |
| 709 | |
| 710 | |
| 711 | /** |
| 712 | * Collection of Description of a Device and it's Ports given from a Provider. |
| 713 | */ |
| 714 | public static class DeviceDescriptions { |
| 715 | |
| 716 | private final AtomicReference<Timestamped<DeviceDescription>> deviceDesc; |
| 717 | private final ConcurrentMap<PortNumber, Timestamped<PortDescription>> portDescs; |
| 718 | |
| 719 | public DeviceDescriptions(Timestamped<DeviceDescription> desc) { |
| 720 | this.deviceDesc = new AtomicReference<>(checkNotNull(desc)); |
| 721 | this.portDescs = new ConcurrentHashMap<>(); |
| 722 | } |
| 723 | |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 724 | Timestamp getLatestTimestamp() { |
| 725 | Timestamp latest = deviceDesc.get().timestamp(); |
| 726 | for (Timestamped<PortDescription> desc : portDescs.values()) { |
| 727 | if (desc.timestamp().compareTo(latest) > 0) { |
| 728 | latest = desc.timestamp(); |
| 729 | } |
| 730 | } |
| 731 | return latest; |
| 732 | } |
| 733 | |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 734 | public Timestamped<DeviceDescription> getDeviceDesc() { |
| 735 | return deviceDesc.get(); |
| 736 | } |
| 737 | |
| 738 | public Timestamped<PortDescription> getPortDesc(PortNumber number) { |
| 739 | return portDescs.get(number); |
| 740 | } |
| 741 | |
| 742 | /** |
| 743 | * Puts DeviceDescription, merging annotations as necessary. |
| 744 | * |
| 745 | * @param newDesc new DeviceDescription |
| 746 | * @return previous DeviceDescription |
| 747 | */ |
| 748 | public synchronized Timestamped<DeviceDescription> putDeviceDesc(Timestamped<DeviceDescription> newDesc) { |
| 749 | Timestamped<DeviceDescription> oldOne = deviceDesc.get(); |
| 750 | Timestamped<DeviceDescription> newOne = newDesc; |
| 751 | if (oldOne != null) { |
Yuta HIGUCHI | 885be1d | 2014-10-04 21:47:26 -0700 | [diff] [blame] | 752 | SparseAnnotations merged = union(oldOne.value().annotations(), |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 753 | newDesc.value().annotations()); |
| 754 | newOne = new Timestamped<DeviceDescription>( |
| 755 | new DefaultDeviceDescription(newDesc.value(), merged), |
| 756 | newDesc.timestamp()); |
| 757 | } |
| 758 | return deviceDesc.getAndSet(newOne); |
| 759 | } |
| 760 | |
| 761 | /** |
| 762 | * Puts PortDescription, merging annotations as necessary. |
| 763 | * |
| 764 | * @param newDesc new PortDescription |
| 765 | * @return previous PortDescription |
| 766 | */ |
| 767 | public synchronized Timestamped<PortDescription> putPortDesc(Timestamped<PortDescription> newDesc) { |
| 768 | Timestamped<PortDescription> oldOne = portDescs.get(newDesc.value().portNumber()); |
| 769 | Timestamped<PortDescription> newOne = newDesc; |
| 770 | if (oldOne != null) { |
Yuta HIGUCHI | 885be1d | 2014-10-04 21:47:26 -0700 | [diff] [blame] | 771 | SparseAnnotations merged = union(oldOne.value().annotations(), |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 772 | newDesc.value().annotations()); |
| 773 | newOne = new Timestamped<PortDescription>( |
| 774 | new DefaultPortDescription(newDesc.value(), merged), |
| 775 | newDesc.timestamp()); |
| 776 | } |
| 777 | return portDescs.put(newOne.value().portNumber(), newOne); |
| 778 | } |
| 779 | } |
Madan Jampani | 47c9373 | 2014-10-06 20:46:08 -0700 | [diff] [blame] | 780 | |
| 781 | private void notifyPeers(InternalDeviceEvent event) throws IOException { |
Madan Jampani | 2206e01 | 2014-10-06 21:04:20 -0700 | [diff] [blame^] | 782 | ClusterMessage message = new ClusterMessage(null, GossipDeviceStoreMessageSubjects.DEVICE_UPDATE, event); |
Madan Jampani | 47c9373 | 2014-10-06 20:46:08 -0700 | [diff] [blame] | 783 | clusterCommunicator.broadcast(message); |
| 784 | } |
| 785 | |
| 786 | private void notifyPeers(InternalPortEvent event) throws IOException { |
Madan Jampani | 2206e01 | 2014-10-06 21:04:20 -0700 | [diff] [blame^] | 787 | ClusterMessage message = new ClusterMessage(null, GossipDeviceStoreMessageSubjects.PORT_UPDATE, event); |
Madan Jampani | 47c9373 | 2014-10-06 20:46:08 -0700 | [diff] [blame] | 788 | clusterCommunicator.broadcast(message); |
| 789 | } |
| 790 | |
| 791 | private void notifyPeers(InternalPortStatusEvent event) throws IOException { |
Madan Jampani | 2206e01 | 2014-10-06 21:04:20 -0700 | [diff] [blame^] | 792 | ClusterMessage message = new ClusterMessage(null, GossipDeviceStoreMessageSubjects.PORT_STATUS_UPDATE, event); |
Madan Jampani | 47c9373 | 2014-10-06 20:46:08 -0700 | [diff] [blame] | 793 | clusterCommunicator.broadcast(message); |
| 794 | } |
| 795 | |
| 796 | private class InternalDeviceEventListener implements ClusterMessageHandler { |
| 797 | @Override |
| 798 | public void handle(ClusterMessage message) { |
| 799 | log.info("Received device update event from peer: {}", message.sender()); |
| 800 | InternalDeviceEvent event = (InternalDeviceEvent) message.payload(); |
| 801 | ProviderId providerId = event.providerId(); |
| 802 | DeviceId deviceId = event.deviceId(); |
| 803 | Timestamped<DeviceDescription> deviceDescription = event.deviceDescription(); |
| 804 | createOrUpdateDeviceInternal(providerId, deviceId, deviceDescription); |
| 805 | } |
| 806 | } |
| 807 | |
| 808 | private class InternalPortEventListener implements ClusterMessageHandler { |
| 809 | @Override |
| 810 | public void handle(ClusterMessage message) { |
| 811 | |
| 812 | log.info("Received port update event from peer: {}", message.sender()); |
| 813 | InternalPortEvent event = (InternalPortEvent) message.payload(); |
| 814 | |
| 815 | ProviderId providerId = event.providerId(); |
| 816 | DeviceId deviceId = event.deviceId(); |
| 817 | Timestamped<List<PortDescription>> portDescriptions = event.portDescriptions(); |
| 818 | |
| 819 | updatePortsInternal(providerId, deviceId, portDescriptions); |
| 820 | } |
| 821 | } |
| 822 | |
| 823 | private class InternalPortStatusEventListener implements ClusterMessageHandler { |
| 824 | @Override |
| 825 | public void handle(ClusterMessage message) { |
| 826 | |
| 827 | log.info("Received port status update event from peer: {}", message.sender()); |
| 828 | InternalPortStatusEvent event = (InternalPortStatusEvent) message.payload(); |
| 829 | |
| 830 | ProviderId providerId = event.providerId(); |
| 831 | DeviceId deviceId = event.deviceId(); |
| 832 | Timestamped<PortDescription> portDescription = event.portDescription(); |
| 833 | |
| 834 | updatePortStatusInternal(providerId, deviceId, portDescription); |
| 835 | } |
| 836 | } |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 837 | } |