Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 1 | package org.onlab.onos.store.device.impl; |
| 2 | |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 3 | import com.google.common.base.Function; |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 4 | import com.google.common.collect.FluentIterable; |
| 5 | import com.google.common.collect.ImmutableList; |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 6 | import com.google.common.collect.Maps; |
| 7 | import com.google.common.collect.Sets; |
Madan Jampani | 47c9373 | 2014-10-06 20:46:08 -0700 | [diff] [blame] | 8 | |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 9 | import org.apache.commons.lang3.RandomUtils; |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 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; |
Madan Jampani | 53e44e6 | 2014-10-07 12:39:51 -0700 | [diff] [blame] | 16 | import org.onlab.onos.cluster.ClusterService; |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 17 | import org.onlab.onos.cluster.ControllerNode; |
| 18 | import org.onlab.onos.cluster.NodeId; |
Yuta HIGUCHI | 39ede6a | 2014-10-03 15:23:33 -0700 | [diff] [blame] | 19 | import org.onlab.onos.net.AnnotationsUtil; |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 20 | import org.onlab.onos.net.DefaultAnnotations; |
| 21 | import org.onlab.onos.net.DefaultDevice; |
| 22 | import org.onlab.onos.net.DefaultPort; |
| 23 | import org.onlab.onos.net.Device; |
| 24 | import org.onlab.onos.net.Device.Type; |
| 25 | import org.onlab.onos.net.DeviceId; |
| 26 | import org.onlab.onos.net.Port; |
| 27 | import org.onlab.onos.net.PortNumber; |
Yuta HIGUCHI | 093e83e | 2014-10-10 22:26:11 -0700 | [diff] [blame] | 28 | import org.onlab.onos.net.device.DeviceClockService; |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 29 | import org.onlab.onos.net.device.DeviceDescription; |
| 30 | import org.onlab.onos.net.device.DeviceEvent; |
| 31 | import org.onlab.onos.net.device.DeviceStore; |
| 32 | import org.onlab.onos.net.device.DeviceStoreDelegate; |
| 33 | import org.onlab.onos.net.device.PortDescription; |
| 34 | import org.onlab.onos.net.provider.ProviderId; |
| 35 | import org.onlab.onos.store.AbstractStore; |
Yuta HIGUCHI | d40483d | 2014-10-09 15:20:30 -0700 | [diff] [blame] | 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 | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 40 | import org.onlab.onos.store.cluster.messaging.MessageSubject; |
Yuta HIGUCHI | fa891c9 | 2014-10-09 15:21:40 -0700 | [diff] [blame] | 41 | import org.onlab.onos.store.common.impl.Timestamped; |
Madan Jampani | 53e44e6 | 2014-10-07 12:39:51 -0700 | [diff] [blame] | 42 | import org.onlab.onos.store.serializers.KryoSerializer; |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 43 | import org.onlab.onos.store.serializers.DistributedStoreSerializers; |
Madan Jampani | 53e44e6 | 2014-10-07 12:39:51 -0700 | [diff] [blame] | 44 | import org.onlab.util.KryoPool; |
Yuta HIGUCHI | 0d6a5e6 | 2014-10-03 15:54:09 -0700 | [diff] [blame] | 45 | import org.onlab.util.NewConcurrentHashMap; |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 46 | import org.slf4j.Logger; |
| 47 | |
Madan Jampani | 47c9373 | 2014-10-06 20:46:08 -0700 | [diff] [blame] | 48 | import java.io.IOException; |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 49 | import java.util.ArrayList; |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 50 | import java.util.Collection; |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 51 | import java.util.Collections; |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 52 | import java.util.HashMap; |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 53 | import java.util.HashSet; |
| 54 | import java.util.Iterator; |
| 55 | import java.util.List; |
| 56 | import java.util.Map; |
| 57 | import java.util.Map.Entry; |
| 58 | import java.util.Objects; |
| 59 | import java.util.Set; |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 60 | import java.util.concurrent.ConcurrentMap; |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 61 | import java.util.concurrent.ScheduledExecutorService; |
| 62 | import java.util.concurrent.TimeUnit; |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 63 | |
| 64 | import static com.google.common.base.Preconditions.checkArgument; |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 65 | import static com.google.common.base.Predicates.notNull; |
Yuta HIGUCHI | dc7374c | 2014-10-10 11:11:09 -0700 | [diff] [blame] | 66 | import static org.onlab.onos.cluster.ControllerNodeToNodeId.toNodeId; |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 67 | import static org.onlab.onos.net.device.DeviceEvent.Type.*; |
| 68 | import static org.slf4j.LoggerFactory.getLogger; |
| 69 | import static org.apache.commons.lang3.concurrent.ConcurrentUtils.createIfAbsentUnchecked; |
| 70 | import static org.onlab.onos.net.DefaultAnnotations.merge; |
| 71 | import static com.google.common.base.Verify.verify; |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 72 | import static org.onlab.util.Tools.namedThreads; |
| 73 | import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor; |
| 74 | import static org.onlab.onos.store.device.impl.GossipDeviceStoreMessageSubjects.DEVICE_ADVERTISE; |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 75 | |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 76 | // TODO: give me a better name |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 77 | /** |
| 78 | * Manages inventory of infrastructure devices using gossip protocol to distribute |
| 79 | * information. |
| 80 | */ |
| 81 | @Component(immediate = true) |
| 82 | @Service |
| 83 | public class GossipDeviceStore |
| 84 | extends AbstractStore<DeviceEvent, DeviceStoreDelegate> |
| 85 | implements DeviceStore { |
| 86 | |
| 87 | private final Logger log = getLogger(getClass()); |
| 88 | |
| 89 | public static final String DEVICE_NOT_FOUND = "Device with ID %s not found"; |
| 90 | |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 91 | // TODO: Check if inner Map can be replaced with plain Map. |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 92 | // innerMap is used to lock a Device, thus instance should never be replaced. |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 93 | |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 94 | // collection of Description given from various providers |
| 95 | private final ConcurrentMap<DeviceId, |
| 96 | ConcurrentMap<ProviderId, DeviceDescriptions>> |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 97 | deviceDescs = Maps.newConcurrentMap(); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 98 | |
| 99 | // cache of Device and Ports generated by compositing descriptions from providers |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 100 | private final ConcurrentMap<DeviceId, Device> devices = Maps.newConcurrentMap(); |
| 101 | private final ConcurrentMap<DeviceId, ConcurrentMap<PortNumber, Port>> devicePorts = Maps.newConcurrentMap(); |
| 102 | |
| 103 | // to be updated under Device lock |
| 104 | private final Map<DeviceId, Timestamp> offline = Maps.newHashMap(); |
| 105 | private final Map<DeviceId, Timestamp> removalRequest = Maps.newHashMap(); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 106 | |
| 107 | // available(=UP) devices |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 108 | private final Set<DeviceId> availableDevices = Sets.newConcurrentHashSet(); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 109 | |
| 110 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Yuta HIGUCHI | 093e83e | 2014-10-10 22:26:11 -0700 | [diff] [blame] | 111 | protected DeviceClockService deviceClockService; |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 112 | |
Madan Jampani | 47c9373 | 2014-10-06 20:46:08 -0700 | [diff] [blame] | 113 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 114 | protected ClusterCommunicationService clusterCommunicator; |
| 115 | |
Madan Jampani | 53e44e6 | 2014-10-07 12:39:51 -0700 | [diff] [blame] | 116 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 117 | protected ClusterService clusterService; |
| 118 | |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 119 | protected static final KryoSerializer SERIALIZER = new KryoSerializer() { |
Yuta HIGUCHI | 24b2e2a | 2014-10-07 15:53:57 -0700 | [diff] [blame] | 120 | @Override |
Madan Jampani | 53e44e6 | 2014-10-07 12:39:51 -0700 | [diff] [blame] | 121 | protected void setupKryoPool() { |
| 122 | serializerPool = KryoPool.newBuilder() |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 123 | .register(DistributedStoreSerializers.COMMON) |
| 124 | |
Madan Jampani | fef9b3a | 2014-10-07 18:38:17 -0700 | [diff] [blame] | 125 | .register(InternalDeviceEvent.class, new InternalDeviceEventSerializer()) |
Madan Jampani | 2532253 | 2014-10-08 11:20:38 -0700 | [diff] [blame] | 126 | .register(InternalDeviceOfflineEvent.class, new InternalDeviceOfflineEventSerializer()) |
Madan Jampani | 3fc72ed | 2014-10-08 12:50:27 -0700 | [diff] [blame] | 127 | .register(InternalDeviceRemovedEvent.class) |
Madan Jampani | fef9b3a | 2014-10-07 18:38:17 -0700 | [diff] [blame] | 128 | .register(InternalPortEvent.class, new InternalPortEventSerializer()) |
| 129 | .register(InternalPortStatusEvent.class, new InternalPortStatusEventSerializer()) |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 130 | .register(DeviceAntiEntropyAdvertisement.class) |
| 131 | .register(DeviceFragmentId.class) |
| 132 | .register(PortFragmentId.class) |
Madan Jampani | 53e44e6 | 2014-10-07 12:39:51 -0700 | [diff] [blame] | 133 | .build() |
| 134 | .populate(1); |
| 135 | } |
Madan Jampani | 53e44e6 | 2014-10-07 12:39:51 -0700 | [diff] [blame] | 136 | }; |
| 137 | |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 138 | private ScheduledExecutorService executor; |
| 139 | |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 140 | @Activate |
| 141 | public void activate() { |
Madan Jampani | 2206e01 | 2014-10-06 21:04:20 -0700 | [diff] [blame] | 142 | clusterCommunicator.addSubscriber( |
| 143 | GossipDeviceStoreMessageSubjects.DEVICE_UPDATE, new InternalDeviceEventListener()); |
| 144 | clusterCommunicator.addSubscriber( |
Madan Jampani | 2532253 | 2014-10-08 11:20:38 -0700 | [diff] [blame] | 145 | GossipDeviceStoreMessageSubjects.DEVICE_OFFLINE, new InternalDeviceOfflineEventListener()); |
| 146 | clusterCommunicator.addSubscriber( |
Madan Jampani | 3fc72ed | 2014-10-08 12:50:27 -0700 | [diff] [blame] | 147 | GossipDeviceStoreMessageSubjects.DEVICE_REMOVED, new InternalDeviceRemovedEventListener()); |
| 148 | clusterCommunicator.addSubscriber( |
Madan Jampani | 2206e01 | 2014-10-06 21:04:20 -0700 | [diff] [blame] | 149 | GossipDeviceStoreMessageSubjects.PORT_UPDATE, new InternalPortEventListener()); |
| 150 | clusterCommunicator.addSubscriber( |
| 151 | GossipDeviceStoreMessageSubjects.PORT_STATUS_UPDATE, new InternalPortStatusEventListener()); |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 152 | clusterCommunicator.addSubscriber( |
| 153 | GossipDeviceStoreMessageSubjects.DEVICE_ADVERTISE, new InternalDeviceAdvertisementListener()); |
| 154 | |
| 155 | executor = |
| 156 | newSingleThreadScheduledExecutor(namedThreads("anti-entropy-%d")); |
| 157 | |
| 158 | // TODO: Make these configurable |
| 159 | long initialDelaySec = 5; |
| 160 | long periodSec = 5; |
| 161 | // start anti-entropy thread |
| 162 | executor.scheduleAtFixedRate(new SendAdvertisementTask(), |
| 163 | initialDelaySec, periodSec, TimeUnit.SECONDS); |
| 164 | |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 165 | log.info("Started"); |
| 166 | } |
| 167 | |
| 168 | @Deactivate |
| 169 | public void deactivate() { |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 170 | |
| 171 | executor.shutdownNow(); |
| 172 | try { |
| 173 | boolean timedout = executor.awaitTermination(5, TimeUnit.SECONDS); |
| 174 | if (timedout) { |
| 175 | log.error("Timeout during executor shutdown"); |
| 176 | } |
| 177 | } catch (InterruptedException e) { |
| 178 | log.error("Error during executor shutdown", e); |
| 179 | } |
| 180 | |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 181 | deviceDescs.clear(); |
| 182 | devices.clear(); |
| 183 | devicePorts.clear(); |
| 184 | availableDevices.clear(); |
| 185 | log.info("Stopped"); |
| 186 | } |
| 187 | |
| 188 | @Override |
| 189 | public int getDeviceCount() { |
| 190 | return devices.size(); |
| 191 | } |
| 192 | |
| 193 | @Override |
| 194 | public Iterable<Device> getDevices() { |
| 195 | return Collections.unmodifiableCollection(devices.values()); |
| 196 | } |
| 197 | |
| 198 | @Override |
| 199 | public Device getDevice(DeviceId deviceId) { |
| 200 | return devices.get(deviceId); |
| 201 | } |
| 202 | |
| 203 | @Override |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 204 | public synchronized DeviceEvent createOrUpdateDevice(ProviderId providerId, |
| 205 | DeviceId deviceId, |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 206 | DeviceDescription deviceDescription) { |
Yuta HIGUCHI | 093e83e | 2014-10-10 22:26:11 -0700 | [diff] [blame] | 207 | final Timestamp newTimestamp = deviceClockService.getTimestamp(deviceId); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 208 | final Timestamped<DeviceDescription> deltaDesc = new Timestamped<>(deviceDescription, newTimestamp); |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 209 | final DeviceEvent event; |
| 210 | final Timestamped<DeviceDescription> mergedDesc; |
| 211 | synchronized (getDeviceDescriptions(deviceId)) { |
| 212 | event = createOrUpdateDeviceInternal(providerId, deviceId, deltaDesc); |
| 213 | mergedDesc = getDeviceDescriptions(deviceId).get(providerId).getDeviceDesc(); |
| 214 | } |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 215 | if (event != null) { |
Madan Jampani | 47c9373 | 2014-10-06 20:46:08 -0700 | [diff] [blame] | 216 | log.info("Notifying peers of a device update topology event for providerId: {} and deviceId: {}", |
| 217 | providerId, deviceId); |
| 218 | try { |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 219 | notifyPeers(new InternalDeviceEvent(providerId, deviceId, mergedDesc)); |
Madan Jampani | 47c9373 | 2014-10-06 20:46:08 -0700 | [diff] [blame] | 220 | } catch (IOException e) { |
Madan Jampani | 2532253 | 2014-10-08 11:20:38 -0700 | [diff] [blame] | 221 | log.error("Failed to notify peers of a device update topology event for providerId: " |
Madan Jampani | 47c9373 | 2014-10-06 20:46:08 -0700 | [diff] [blame] | 222 | + providerId + " and deviceId: " + deviceId, e); |
| 223 | } |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 224 | } |
| 225 | return event; |
| 226 | } |
| 227 | |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 228 | private DeviceEvent createOrUpdateDeviceInternal(ProviderId providerId, |
| 229 | DeviceId deviceId, |
| 230 | Timestamped<DeviceDescription> deltaDesc) { |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 231 | |
| 232 | // Collection of DeviceDescriptions for a Device |
| 233 | ConcurrentMap<ProviderId, DeviceDescriptions> providerDescs |
Yuta HIGUCHI | 0d6a5e6 | 2014-10-03 15:54:09 -0700 | [diff] [blame] | 234 | = getDeviceDescriptions(deviceId); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 235 | |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 236 | synchronized (providerDescs) { |
| 237 | // locking per device |
| 238 | |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 239 | if (isDeviceRemoved(deviceId, deltaDesc.timestamp())) { |
| 240 | log.debug("Ignoring outdated event: {}", deltaDesc); |
| 241 | return null; |
| 242 | } |
| 243 | |
| 244 | DeviceDescriptions descs |
| 245 | = createIfAbsentUnchecked(providerDescs, providerId, |
| 246 | new InitDeviceDescs(deltaDesc)); |
| 247 | |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 248 | final Device oldDevice = devices.get(deviceId); |
| 249 | final Device newDevice; |
| 250 | |
| 251 | if (deltaDesc == descs.getDeviceDesc() || |
| 252 | deltaDesc.isNewer(descs.getDeviceDesc())) { |
| 253 | // on new device or valid update |
| 254 | descs.putDeviceDesc(deltaDesc); |
| 255 | newDevice = composeDevice(deviceId, providerDescs); |
| 256 | } else { |
| 257 | // outdated event, ignored. |
| 258 | return null; |
| 259 | } |
| 260 | if (oldDevice == null) { |
| 261 | // ADD |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 262 | return createDevice(providerId, newDevice, deltaDesc.timestamp()); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 263 | } else { |
| 264 | // UPDATE or ignore (no change or stale) |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 265 | return updateDevice(providerId, oldDevice, newDevice, deltaDesc.timestamp()); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 266 | } |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | // Creates the device and returns the appropriate event if necessary. |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 271 | // Guarded by deviceDescs value (=Device lock) |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 272 | private DeviceEvent createDevice(ProviderId providerId, |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 273 | Device newDevice, Timestamp timestamp) { |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 274 | |
| 275 | // update composed device cache |
| 276 | Device oldDevice = devices.putIfAbsent(newDevice.id(), newDevice); |
| 277 | verify(oldDevice == null, |
| 278 | "Unexpected Device in cache. PID:%s [old=%s, new=%s]", |
| 279 | providerId, oldDevice, newDevice); |
| 280 | |
| 281 | if (!providerId.isAncillary()) { |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 282 | markOnline(newDevice.id(), timestamp); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | return new DeviceEvent(DeviceEvent.Type.DEVICE_ADDED, newDevice, null); |
| 286 | } |
| 287 | |
| 288 | // Updates the device and returns the appropriate event if necessary. |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 289 | // Guarded by deviceDescs value (=Device lock) |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 290 | private DeviceEvent updateDevice(ProviderId providerId, |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 291 | Device oldDevice, |
| 292 | Device newDevice, Timestamp newTimestamp) { |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 293 | |
| 294 | // We allow only certain attributes to trigger update |
| 295 | if (!Objects.equals(oldDevice.hwVersion(), newDevice.hwVersion()) || |
| 296 | !Objects.equals(oldDevice.swVersion(), newDevice.swVersion()) || |
Yuta HIGUCHI | 39ede6a | 2014-10-03 15:23:33 -0700 | [diff] [blame] | 297 | !AnnotationsUtil.isEqual(oldDevice.annotations(), newDevice.annotations())) { |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 298 | |
| 299 | boolean replaced = devices.replace(newDevice.id(), oldDevice, newDevice); |
| 300 | if (!replaced) { |
| 301 | verify(replaced, |
| 302 | "Replacing devices cache failed. PID:%s [expected:%s, found:%s, new=%s]", |
| 303 | providerId, oldDevice, devices.get(newDevice.id()) |
| 304 | , newDevice); |
| 305 | } |
| 306 | if (!providerId.isAncillary()) { |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 307 | markOnline(newDevice.id(), newTimestamp); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 308 | } |
| 309 | return new DeviceEvent(DeviceEvent.Type.DEVICE_UPDATED, newDevice, null); |
| 310 | } |
| 311 | |
| 312 | // Otherwise merely attempt to change availability if primary provider |
| 313 | if (!providerId.isAncillary()) { |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 314 | boolean added = markOnline(newDevice.id(), newTimestamp); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 315 | return !added ? null : |
| 316 | new DeviceEvent(DEVICE_AVAILABILITY_CHANGED, newDevice, null); |
| 317 | } |
| 318 | return null; |
| 319 | } |
| 320 | |
| 321 | @Override |
| 322 | public DeviceEvent markOffline(DeviceId deviceId) { |
Yuta HIGUCHI | 093e83e | 2014-10-10 22:26:11 -0700 | [diff] [blame] | 323 | final Timestamp timestamp = deviceClockService.getTimestamp(deviceId); |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 324 | final DeviceEvent event = markOfflineInternal(deviceId, timestamp); |
Madan Jampani | 2532253 | 2014-10-08 11:20:38 -0700 | [diff] [blame] | 325 | if (event != null) { |
| 326 | log.info("Notifying peers of a device offline topology event for deviceId: {}", |
| 327 | deviceId); |
| 328 | try { |
| 329 | notifyPeers(new InternalDeviceOfflineEvent(deviceId, timestamp)); |
| 330 | } catch (IOException e) { |
| 331 | log.error("Failed to notify peers of a device offline topology event for deviceId: {}", |
| 332 | deviceId); |
| 333 | } |
| 334 | } |
| 335 | return event; |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | private DeviceEvent markOfflineInternal(DeviceId deviceId, Timestamp timestamp) { |
| 339 | |
| 340 | Map<ProviderId, DeviceDescriptions> providerDescs |
Yuta HIGUCHI | 0d6a5e6 | 2014-10-03 15:54:09 -0700 | [diff] [blame] | 341 | = getDeviceDescriptions(deviceId); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 342 | |
| 343 | // locking device |
| 344 | synchronized (providerDescs) { |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 345 | |
| 346 | // accept off-line if given timestamp is newer than |
| 347 | // the latest Timestamp from Primary provider |
| 348 | DeviceDescriptions primDescs = getPrimaryDescriptions(providerDescs); |
| 349 | Timestamp lastTimestamp = primDescs.getLatestTimestamp(); |
| 350 | if (timestamp.compareTo(lastTimestamp) <= 0) { |
| 351 | // outdated event ignore |
| 352 | return null; |
| 353 | } |
| 354 | |
| 355 | offline.put(deviceId, timestamp); |
| 356 | |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 357 | Device device = devices.get(deviceId); |
| 358 | if (device == null) { |
| 359 | return null; |
| 360 | } |
| 361 | boolean removed = availableDevices.remove(deviceId); |
| 362 | if (removed) { |
| 363 | // TODO: broadcast ... DOWN only? |
| 364 | return new DeviceEvent(DEVICE_AVAILABILITY_CHANGED, device, null); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 365 | } |
| 366 | return null; |
| 367 | } |
| 368 | } |
| 369 | |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 370 | /** |
| 371 | * Marks the device as available if the given timestamp is not outdated, |
| 372 | * compared to the time the device has been marked offline. |
| 373 | * |
| 374 | * @param deviceId identifier of the device |
| 375 | * @param timestamp of the event triggering this change. |
| 376 | * @return true if availability change request was accepted and changed the state |
| 377 | */ |
| 378 | // Guarded by deviceDescs value (=Device lock) |
| 379 | private boolean markOnline(DeviceId deviceId, Timestamp timestamp) { |
| 380 | // accept on-line if given timestamp is newer than |
| 381 | // the latest offline request Timestamp |
| 382 | Timestamp offlineTimestamp = offline.get(deviceId); |
| 383 | if (offlineTimestamp == null || |
| 384 | offlineTimestamp.compareTo(timestamp) < 0) { |
| 385 | |
| 386 | offline.remove(deviceId); |
| 387 | return availableDevices.add(deviceId); |
| 388 | } |
| 389 | return false; |
| 390 | } |
| 391 | |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 392 | @Override |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 393 | public synchronized List<DeviceEvent> updatePorts(ProviderId providerId, |
| 394 | DeviceId deviceId, |
| 395 | List<PortDescription> portDescriptions) { |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 396 | |
Yuta HIGUCHI | 093e83e | 2014-10-10 22:26:11 -0700 | [diff] [blame] | 397 | final Timestamp newTimestamp = deviceClockService.getTimestamp(deviceId); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 398 | |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 399 | final Timestamped<List<PortDescription>> timestampedInput |
| 400 | = new Timestamped<>(portDescriptions, newTimestamp); |
| 401 | final List<DeviceEvent> events; |
| 402 | final Timestamped<List<PortDescription>> merged; |
| 403 | |
| 404 | synchronized (getDeviceDescriptions(deviceId)) { |
| 405 | events = updatePortsInternal(providerId, deviceId, timestampedInput); |
| 406 | final DeviceDescriptions descs = getDeviceDescriptions(deviceId).get(providerId); |
| 407 | List<PortDescription> mergedList = |
| 408 | FluentIterable.from(portDescriptions) |
| 409 | .transform(new Function<PortDescription, PortDescription>() { |
| 410 | @Override |
| 411 | public PortDescription apply(PortDescription input) { |
| 412 | // lookup merged port description |
| 413 | return descs.getPortDesc(input.portNumber()).value(); |
| 414 | } |
| 415 | }).toList(); |
| 416 | merged = new Timestamped<List<PortDescription>>(mergedList, newTimestamp); |
| 417 | } |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 418 | if (!events.isEmpty()) { |
Madan Jampani | 47c9373 | 2014-10-06 20:46:08 -0700 | [diff] [blame] | 419 | log.info("Notifying peers of a port update topology event for providerId: {} and deviceId: {}", |
| 420 | providerId, deviceId); |
| 421 | try { |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 422 | notifyPeers(new InternalPortEvent(providerId, deviceId, merged)); |
Madan Jampani | 47c9373 | 2014-10-06 20:46:08 -0700 | [diff] [blame] | 423 | } catch (IOException e) { |
| 424 | log.error("Failed to notify peers of a port update topology event or providerId: " |
| 425 | + providerId + " and deviceId: " + deviceId, e); |
| 426 | } |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 427 | } |
| 428 | return events; |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 429 | } |
| 430 | |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 431 | private List<DeviceEvent> updatePortsInternal(ProviderId providerId, |
| 432 | DeviceId deviceId, |
| 433 | Timestamped<List<PortDescription>> portDescriptions) { |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 434 | |
| 435 | Device device = devices.get(deviceId); |
| 436 | checkArgument(device != null, DEVICE_NOT_FOUND, deviceId); |
| 437 | |
| 438 | ConcurrentMap<ProviderId, DeviceDescriptions> descsMap = deviceDescs.get(deviceId); |
| 439 | checkArgument(descsMap != null, DEVICE_NOT_FOUND, deviceId); |
| 440 | |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 441 | List<DeviceEvent> events = new ArrayList<>(); |
| 442 | synchronized (descsMap) { |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 443 | |
| 444 | if (isDeviceRemoved(deviceId, portDescriptions.timestamp())) { |
| 445 | log.debug("Ignoring outdated events: {}", portDescriptions); |
| 446 | return null; |
| 447 | } |
| 448 | |
| 449 | DeviceDescriptions descs = descsMap.get(providerId); |
| 450 | // every provider must provide DeviceDescription. |
| 451 | checkArgument(descs != null, |
| 452 | "Device description for Device ID %s from Provider %s was not found", |
| 453 | deviceId, providerId); |
| 454 | |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 455 | Map<PortNumber, Port> ports = getPortMap(deviceId); |
| 456 | |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 457 | final Timestamp newTimestamp = portDescriptions.timestamp(); |
| 458 | |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 459 | // Add new ports |
| 460 | Set<PortNumber> processed = new HashSet<>(); |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 461 | for (PortDescription portDescription : portDescriptions.value()) { |
| 462 | final PortNumber number = portDescription.portNumber(); |
| 463 | processed.add(number); |
| 464 | |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 465 | final Port oldPort = ports.get(number); |
| 466 | final Port newPort; |
| 467 | |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 468 | |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 469 | final Timestamped<PortDescription> existingPortDesc = descs.getPortDesc(number); |
| 470 | if (existingPortDesc == null || |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 471 | newTimestamp.compareTo(existingPortDesc.timestamp()) >= 0) { |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 472 | // on new port or valid update |
| 473 | // update description |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 474 | descs.putPortDesc(new Timestamped<>(portDescription, |
| 475 | portDescriptions.timestamp())); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 476 | newPort = composePort(device, number, descsMap); |
| 477 | } else { |
| 478 | // outdated event, ignored. |
| 479 | continue; |
| 480 | } |
| 481 | |
| 482 | events.add(oldPort == null ? |
| 483 | createPort(device, newPort, ports) : |
| 484 | updatePort(device, oldPort, newPort, ports)); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | events.addAll(pruneOldPorts(device, ports, processed)); |
| 488 | } |
| 489 | return FluentIterable.from(events).filter(notNull()).toList(); |
| 490 | } |
| 491 | |
| 492 | // Creates a new port based on the port description adds it to the map and |
| 493 | // Returns corresponding event. |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 494 | // Guarded by deviceDescs value (=Device lock) |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 495 | private DeviceEvent createPort(Device device, Port newPort, |
| 496 | Map<PortNumber, Port> ports) { |
| 497 | ports.put(newPort.number(), newPort); |
| 498 | return new DeviceEvent(PORT_ADDED, device, newPort); |
| 499 | } |
| 500 | |
| 501 | // Checks if the specified port requires update and if so, it replaces the |
| 502 | // existing entry in the map and returns corresponding event. |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 503 | // Guarded by deviceDescs value (=Device lock) |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 504 | private DeviceEvent updatePort(Device device, Port oldPort, |
| 505 | Port newPort, |
| 506 | Map<PortNumber, Port> ports) { |
| 507 | if (oldPort.isEnabled() != newPort.isEnabled() || |
Yuta HIGUCHI | 39ede6a | 2014-10-03 15:23:33 -0700 | [diff] [blame] | 508 | !AnnotationsUtil.isEqual(oldPort.annotations(), newPort.annotations())) { |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 509 | |
| 510 | ports.put(oldPort.number(), newPort); |
| 511 | return new DeviceEvent(PORT_UPDATED, device, newPort); |
| 512 | } |
| 513 | return null; |
| 514 | } |
| 515 | |
| 516 | // Prunes the specified list of ports based on which ports are in the |
| 517 | // processed list and returns list of corresponding events. |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 518 | // Guarded by deviceDescs value (=Device lock) |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 519 | private List<DeviceEvent> pruneOldPorts(Device device, |
| 520 | Map<PortNumber, Port> ports, |
| 521 | Set<PortNumber> processed) { |
| 522 | List<DeviceEvent> events = new ArrayList<>(); |
| 523 | Iterator<PortNumber> iterator = ports.keySet().iterator(); |
| 524 | while (iterator.hasNext()) { |
| 525 | PortNumber portNumber = iterator.next(); |
| 526 | if (!processed.contains(portNumber)) { |
| 527 | events.add(new DeviceEvent(PORT_REMOVED, device, |
| 528 | ports.get(portNumber))); |
| 529 | iterator.remove(); |
| 530 | } |
| 531 | } |
| 532 | return events; |
| 533 | } |
| 534 | |
| 535 | // Gets the map of ports for the specified device; if one does not already |
| 536 | // exist, it creates and registers a new one. |
| 537 | private ConcurrentMap<PortNumber, Port> getPortMap(DeviceId deviceId) { |
| 538 | return createIfAbsentUnchecked(devicePorts, deviceId, |
Yuta HIGUCHI | 0d6a5e6 | 2014-10-03 15:54:09 -0700 | [diff] [blame] | 539 | NewConcurrentHashMap.<PortNumber, Port>ifNeeded()); |
| 540 | } |
| 541 | |
| 542 | private ConcurrentMap<ProviderId, DeviceDescriptions> getDeviceDescriptions( |
| 543 | DeviceId deviceId) { |
| 544 | return createIfAbsentUnchecked(deviceDescs, deviceId, |
| 545 | NewConcurrentHashMap.<ProviderId, DeviceDescriptions>ifNeeded()); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | @Override |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 549 | public synchronized DeviceEvent updatePortStatus(ProviderId providerId, |
| 550 | DeviceId deviceId, |
| 551 | PortDescription portDescription) { |
| 552 | |
Yuta HIGUCHI | 093e83e | 2014-10-10 22:26:11 -0700 | [diff] [blame] | 553 | final Timestamp newTimestamp = deviceClockService.getTimestamp(deviceId); |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 554 | final Timestamped<PortDescription> deltaDesc |
| 555 | = new Timestamped<>(portDescription, newTimestamp); |
| 556 | final DeviceEvent event; |
| 557 | final Timestamped<PortDescription> mergedDesc; |
| 558 | synchronized (getDeviceDescriptions(deviceId)) { |
| 559 | event = updatePortStatusInternal(providerId, deviceId, deltaDesc); |
| 560 | mergedDesc = getDeviceDescriptions(deviceId).get(providerId) |
| 561 | .getPortDesc(portDescription.portNumber()); |
| 562 | } |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 563 | if (event != null) { |
Madan Jampani | 47c9373 | 2014-10-06 20:46:08 -0700 | [diff] [blame] | 564 | log.info("Notifying peers of a port status update topology event for providerId: {} and deviceId: {}", |
| 565 | providerId, deviceId); |
| 566 | try { |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 567 | notifyPeers(new InternalPortStatusEvent(providerId, deviceId, mergedDesc)); |
Madan Jampani | 47c9373 | 2014-10-06 20:46:08 -0700 | [diff] [blame] | 568 | } catch (IOException e) { |
| 569 | log.error("Failed to notify peers of a port status update topology event or providerId: " |
| 570 | + providerId + " and deviceId: " + deviceId, e); |
| 571 | } |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 572 | } |
| 573 | return event; |
| 574 | } |
| 575 | |
| 576 | private DeviceEvent updatePortStatusInternal(ProviderId providerId, DeviceId deviceId, |
| 577 | Timestamped<PortDescription> deltaDesc) { |
| 578 | |
| 579 | Device device = devices.get(deviceId); |
| 580 | checkArgument(device != null, DEVICE_NOT_FOUND, deviceId); |
| 581 | |
| 582 | ConcurrentMap<ProviderId, DeviceDescriptions> descsMap = deviceDescs.get(deviceId); |
| 583 | checkArgument(descsMap != null, DEVICE_NOT_FOUND, deviceId); |
| 584 | |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 585 | synchronized (descsMap) { |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 586 | |
| 587 | if (isDeviceRemoved(deviceId, deltaDesc.timestamp())) { |
| 588 | log.debug("Ignoring outdated event: {}", deltaDesc); |
| 589 | return null; |
| 590 | } |
| 591 | |
| 592 | DeviceDescriptions descs = descsMap.get(providerId); |
| 593 | // assuming all providers must to give DeviceDescription |
| 594 | checkArgument(descs != null, |
| 595 | "Device description for Device ID %s from Provider %s was not found", |
| 596 | deviceId, providerId); |
| 597 | |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 598 | ConcurrentMap<PortNumber, Port> ports = getPortMap(deviceId); |
| 599 | final PortNumber number = deltaDesc.value().portNumber(); |
| 600 | final Port oldPort = ports.get(number); |
| 601 | final Port newPort; |
| 602 | |
| 603 | final Timestamped<PortDescription> existingPortDesc = descs.getPortDesc(number); |
| 604 | if (existingPortDesc == null || |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 605 | deltaDesc.isNewer(existingPortDesc)) { |
| 606 | // on new port or valid update |
| 607 | // update description |
| 608 | descs.putPortDesc(deltaDesc); |
| 609 | newPort = composePort(device, number, descsMap); |
| 610 | } else { |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 611 | // same or outdated event, ignored. |
| 612 | log.trace("ignore same or outdated {} >= {}", existingPortDesc, deltaDesc); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 613 | return null; |
| 614 | } |
| 615 | |
| 616 | if (oldPort == null) { |
| 617 | return createPort(device, newPort, ports); |
| 618 | } else { |
| 619 | return updatePort(device, oldPort, newPort, ports); |
| 620 | } |
| 621 | } |
| 622 | } |
| 623 | |
| 624 | @Override |
| 625 | public List<Port> getPorts(DeviceId deviceId) { |
| 626 | Map<PortNumber, Port> ports = devicePorts.get(deviceId); |
| 627 | if (ports == null) { |
| 628 | return Collections.emptyList(); |
| 629 | } |
| 630 | return ImmutableList.copyOf(ports.values()); |
| 631 | } |
| 632 | |
| 633 | @Override |
| 634 | public Port getPort(DeviceId deviceId, PortNumber portNumber) { |
| 635 | Map<PortNumber, Port> ports = devicePorts.get(deviceId); |
| 636 | return ports == null ? null : ports.get(portNumber); |
| 637 | } |
| 638 | |
| 639 | @Override |
| 640 | public boolean isAvailable(DeviceId deviceId) { |
| 641 | return availableDevices.contains(deviceId); |
| 642 | } |
| 643 | |
| 644 | @Override |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 645 | public synchronized DeviceEvent removeDevice(DeviceId deviceId) { |
Yuta HIGUCHI | 093e83e | 2014-10-10 22:26:11 -0700 | [diff] [blame] | 646 | Timestamp timestamp = deviceClockService.getTimestamp(deviceId); |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 647 | DeviceEvent event = removeDeviceInternal(deviceId, timestamp); |
Madan Jampani | 3fc72ed | 2014-10-08 12:50:27 -0700 | [diff] [blame] | 648 | if (event != null) { |
| 649 | log.info("Notifying peers of a device removed topology event for deviceId: {}", |
| 650 | deviceId); |
| 651 | try { |
| 652 | notifyPeers(new InternalDeviceRemovedEvent(deviceId, timestamp)); |
| 653 | } catch (IOException e) { |
| 654 | log.error("Failed to notify peers of a device removed topology event for deviceId: {}", |
| 655 | deviceId); |
| 656 | } |
| 657 | } |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 658 | return event; |
| 659 | } |
| 660 | |
| 661 | private DeviceEvent removeDeviceInternal(DeviceId deviceId, |
| 662 | Timestamp timestamp) { |
| 663 | |
| 664 | Map<ProviderId, DeviceDescriptions> descs = getDeviceDescriptions(deviceId); |
Yuta HIGUCHI | 0d6a5e6 | 2014-10-03 15:54:09 -0700 | [diff] [blame] | 665 | synchronized (descs) { |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 666 | // accept removal request if given timestamp is newer than |
| 667 | // the latest Timestamp from Primary provider |
| 668 | DeviceDescriptions primDescs = getPrimaryDescriptions(descs); |
| 669 | Timestamp lastTimestamp = primDescs.getLatestTimestamp(); |
| 670 | if (timestamp.compareTo(lastTimestamp) <= 0) { |
| 671 | // outdated event ignore |
| 672 | return null; |
| 673 | } |
| 674 | removalRequest.put(deviceId, timestamp); |
| 675 | |
Yuta HIGUCHI | 0d6a5e6 | 2014-10-03 15:54:09 -0700 | [diff] [blame] | 676 | Device device = devices.remove(deviceId); |
| 677 | // should DEVICE_REMOVED carry removed ports? |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 678 | Map<PortNumber, Port> ports = devicePorts.get(deviceId); |
| 679 | if (ports != null) { |
| 680 | ports.clear(); |
| 681 | } |
| 682 | markOfflineInternal(deviceId, timestamp); |
Yuta HIGUCHI | 0d6a5e6 | 2014-10-03 15:54:09 -0700 | [diff] [blame] | 683 | descs.clear(); |
| 684 | return device == null ? null : |
| 685 | new DeviceEvent(DEVICE_REMOVED, device, null); |
| 686 | } |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 687 | } |
| 688 | |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 689 | /** |
| 690 | * Checks if given timestamp is superseded by removal request |
| 691 | * with more recent timestamp. |
| 692 | * |
| 693 | * @param deviceId identifier of a device |
| 694 | * @param timestampToCheck timestamp of an event to check |
| 695 | * @return true if device is already removed |
| 696 | */ |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 697 | private boolean isDeviceRemoved(DeviceId deviceId, Timestamp timestampToCheck) { |
| 698 | Timestamp removalTimestamp = removalRequest.get(deviceId); |
| 699 | if (removalTimestamp != null && |
| 700 | removalTimestamp.compareTo(timestampToCheck) >= 0) { |
| 701 | // removalRequest is more recent |
| 702 | return true; |
| 703 | } |
| 704 | return false; |
| 705 | } |
| 706 | |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 707 | /** |
| 708 | * Returns a Device, merging description given from multiple Providers. |
| 709 | * |
| 710 | * @param deviceId device identifier |
| 711 | * @param providerDescs Collection of Descriptions from multiple providers |
| 712 | * @return Device instance |
| 713 | */ |
| 714 | private Device composeDevice(DeviceId deviceId, |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 715 | Map<ProviderId, DeviceDescriptions> providerDescs) { |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 716 | |
| 717 | checkArgument(!providerDescs.isEmpty(), "No Device descriptions supplied"); |
| 718 | |
| 719 | ProviderId primary = pickPrimaryPID(providerDescs); |
| 720 | |
| 721 | DeviceDescriptions desc = providerDescs.get(primary); |
| 722 | |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 723 | final DeviceDescription base = desc.getDeviceDesc().value(); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 724 | Type type = base.type(); |
| 725 | String manufacturer = base.manufacturer(); |
| 726 | String hwVersion = base.hwVersion(); |
| 727 | String swVersion = base.swVersion(); |
| 728 | String serialNumber = base.serialNumber(); |
| 729 | DefaultAnnotations annotations = DefaultAnnotations.builder().build(); |
| 730 | annotations = merge(annotations, base.annotations()); |
| 731 | |
| 732 | for (Entry<ProviderId, DeviceDescriptions> e : providerDescs.entrySet()) { |
| 733 | if (e.getKey().equals(primary)) { |
| 734 | continue; |
| 735 | } |
| 736 | // TODO: should keep track of Description timestamp |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 737 | // and only merge conflicting keys when timestamp is newer. |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 738 | // Currently assuming there will never be a key conflict between |
| 739 | // providers |
| 740 | |
| 741 | // annotation merging. not so efficient, should revisit later |
| 742 | annotations = merge(annotations, e.getValue().getDeviceDesc().value().annotations()); |
| 743 | } |
| 744 | |
| 745 | return new DefaultDevice(primary, deviceId , type, manufacturer, |
| 746 | hwVersion, swVersion, serialNumber, annotations); |
| 747 | } |
| 748 | |
| 749 | /** |
| 750 | * Returns a Port, merging description given from multiple Providers. |
| 751 | * |
| 752 | * @param device device the port is on |
| 753 | * @param number port number |
| 754 | * @param providerDescs Collection of Descriptions from multiple providers |
| 755 | * @return Port instance |
| 756 | */ |
| 757 | private Port composePort(Device device, PortNumber number, |
| 758 | ConcurrentMap<ProviderId, DeviceDescriptions> providerDescs) { |
| 759 | |
| 760 | ProviderId primary = pickPrimaryPID(providerDescs); |
| 761 | DeviceDescriptions primDescs = providerDescs.get(primary); |
| 762 | // if no primary, assume not enabled |
| 763 | // TODO: revisit this default port enabled/disabled behavior |
| 764 | boolean isEnabled = false; |
| 765 | DefaultAnnotations annotations = DefaultAnnotations.builder().build(); |
| 766 | |
| 767 | final Timestamped<PortDescription> portDesc = primDescs.getPortDesc(number); |
| 768 | if (portDesc != null) { |
| 769 | isEnabled = portDesc.value().isEnabled(); |
| 770 | annotations = merge(annotations, portDesc.value().annotations()); |
| 771 | } |
| 772 | |
| 773 | for (Entry<ProviderId, DeviceDescriptions> e : providerDescs.entrySet()) { |
| 774 | if (e.getKey().equals(primary)) { |
| 775 | continue; |
| 776 | } |
| 777 | // TODO: should keep track of Description timestamp |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 778 | // and only merge conflicting keys when timestamp is newer. |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 779 | // Currently assuming there will never be a key conflict between |
| 780 | // providers |
| 781 | |
| 782 | // annotation merging. not so efficient, should revisit later |
| 783 | final Timestamped<PortDescription> otherPortDesc = e.getValue().getPortDesc(number); |
| 784 | if (otherPortDesc != null) { |
| 785 | annotations = merge(annotations, otherPortDesc.value().annotations()); |
| 786 | } |
| 787 | } |
| 788 | |
| 789 | return new DefaultPort(device, number, isEnabled, annotations); |
| 790 | } |
| 791 | |
| 792 | /** |
| 793 | * @return primary ProviderID, or randomly chosen one if none exists |
| 794 | */ |
| 795 | private ProviderId pickPrimaryPID( |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 796 | Map<ProviderId, DeviceDescriptions> providerDescs) { |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 797 | ProviderId fallBackPrimary = null; |
| 798 | for (Entry<ProviderId, DeviceDescriptions> e : providerDescs.entrySet()) { |
| 799 | if (!e.getKey().isAncillary()) { |
| 800 | return e.getKey(); |
| 801 | } else if (fallBackPrimary == null) { |
| 802 | // pick randomly as a fallback in case there is no primary |
| 803 | fallBackPrimary = e.getKey(); |
| 804 | } |
| 805 | } |
| 806 | return fallBackPrimary; |
| 807 | } |
| 808 | |
Yuta HIGUCHI | c35efac | 2014-10-06 14:43:53 -0700 | [diff] [blame] | 809 | private DeviceDescriptions getPrimaryDescriptions( |
| 810 | Map<ProviderId, DeviceDescriptions> providerDescs) { |
| 811 | ProviderId pid = pickPrimaryPID(providerDescs); |
| 812 | return providerDescs.get(pid); |
| 813 | } |
| 814 | |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 815 | // TODO: should we be throwing exception? |
| 816 | private void unicastMessage(NodeId recipient, MessageSubject subject, Object event) throws IOException { |
| 817 | ClusterMessage message = new ClusterMessage( |
| 818 | clusterService.getLocalNode().id(), |
| 819 | subject, |
| 820 | SERIALIZER.encode(event)); |
| 821 | clusterCommunicator.unicast(message, recipient); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 822 | } |
| 823 | |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 824 | // TODO: should we be throwing exception? |
| 825 | private void broadcastMessage(MessageSubject subject, Object event) throws IOException { |
| 826 | ClusterMessage message = new ClusterMessage( |
| 827 | clusterService.getLocalNode().id(), |
| 828 | subject, |
| 829 | SERIALIZER.encode(event)); |
| 830 | clusterCommunicator.broadcast(message); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 831 | } |
Madan Jampani | 47c9373 | 2014-10-06 20:46:08 -0700 | [diff] [blame] | 832 | |
| 833 | private void notifyPeers(InternalDeviceEvent event) throws IOException { |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 834 | broadcastMessage(GossipDeviceStoreMessageSubjects.DEVICE_UPDATE, event); |
Madan Jampani | 47c9373 | 2014-10-06 20:46:08 -0700 | [diff] [blame] | 835 | } |
| 836 | |
Madan Jampani | 2532253 | 2014-10-08 11:20:38 -0700 | [diff] [blame] | 837 | private void notifyPeers(InternalDeviceOfflineEvent event) throws IOException { |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 838 | broadcastMessage(GossipDeviceStoreMessageSubjects.DEVICE_OFFLINE, event); |
Madan Jampani | 2532253 | 2014-10-08 11:20:38 -0700 | [diff] [blame] | 839 | } |
| 840 | |
Madan Jampani | 3fc72ed | 2014-10-08 12:50:27 -0700 | [diff] [blame] | 841 | private void notifyPeers(InternalDeviceRemovedEvent event) throws IOException { |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 842 | broadcastMessage(GossipDeviceStoreMessageSubjects.DEVICE_REMOVED, event); |
Madan Jampani | 3fc72ed | 2014-10-08 12:50:27 -0700 | [diff] [blame] | 843 | } |
| 844 | |
Madan Jampani | 47c9373 | 2014-10-06 20:46:08 -0700 | [diff] [blame] | 845 | private void notifyPeers(InternalPortEvent event) throws IOException { |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 846 | broadcastMessage(GossipDeviceStoreMessageSubjects.PORT_UPDATE, event); |
Madan Jampani | 47c9373 | 2014-10-06 20:46:08 -0700 | [diff] [blame] | 847 | } |
| 848 | |
| 849 | private void notifyPeers(InternalPortStatusEvent event) throws IOException { |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 850 | broadcastMessage(GossipDeviceStoreMessageSubjects.PORT_STATUS_UPDATE, event); |
| 851 | } |
| 852 | |
| 853 | private void notifyPeer(NodeId recipient, InternalDeviceEvent event) { |
| 854 | try { |
| 855 | unicastMessage(recipient, GossipDeviceStoreMessageSubjects.DEVICE_UPDATE, event); |
| 856 | } catch (IOException e) { |
| 857 | log.error("Failed to send" + event + " to " + recipient, e); |
| 858 | } |
| 859 | } |
| 860 | |
| 861 | private void notifyPeer(NodeId recipient, InternalDeviceOfflineEvent event) { |
| 862 | try { |
| 863 | unicastMessage(recipient, GossipDeviceStoreMessageSubjects.DEVICE_OFFLINE, event); |
| 864 | } catch (IOException e) { |
| 865 | log.error("Failed to send" + event + " to " + recipient, e); |
| 866 | } |
| 867 | } |
| 868 | |
| 869 | private void notifyPeer(NodeId recipient, InternalDeviceRemovedEvent event) { |
| 870 | try { |
| 871 | unicastMessage(recipient, GossipDeviceStoreMessageSubjects.DEVICE_REMOVED, event); |
| 872 | } catch (IOException e) { |
| 873 | log.error("Failed to send" + event + " to " + recipient, e); |
| 874 | } |
| 875 | } |
| 876 | |
| 877 | private void notifyPeer(NodeId recipient, InternalPortEvent event) { |
| 878 | try { |
| 879 | unicastMessage(recipient, GossipDeviceStoreMessageSubjects.PORT_UPDATE, event); |
| 880 | } catch (IOException e) { |
| 881 | log.error("Failed to send" + event + " to " + recipient, e); |
| 882 | } |
| 883 | } |
| 884 | |
| 885 | private void notifyPeer(NodeId recipient, InternalPortStatusEvent event) { |
| 886 | try { |
| 887 | unicastMessage(recipient, GossipDeviceStoreMessageSubjects.PORT_STATUS_UPDATE, event); |
| 888 | } catch (IOException e) { |
| 889 | log.error("Failed to send" + event + " to " + recipient, e); |
| 890 | } |
| 891 | } |
| 892 | |
| 893 | private DeviceAntiEntropyAdvertisement createAdvertisement() { |
| 894 | final NodeId self = clusterService.getLocalNode().id(); |
| 895 | |
| 896 | Map<DeviceFragmentId, Timestamp> devices = new HashMap<>(deviceDescs.size()); |
| 897 | final int portsPerDevice = 8; // random guess to minimize reallocation |
| 898 | Map<PortFragmentId, Timestamp> ports = new HashMap<>(devices.size() * portsPerDevice); |
| 899 | Map<DeviceId, Timestamp> offline = new HashMap<>(devices.size()); |
| 900 | |
| 901 | for (Entry<DeviceId, ConcurrentMap<ProviderId, DeviceDescriptions>> |
| 902 | provs : deviceDescs.entrySet()) { |
| 903 | |
| 904 | final DeviceId deviceId = provs.getKey(); |
| 905 | final ConcurrentMap<ProviderId, DeviceDescriptions> devDescs = provs.getValue(); |
| 906 | synchronized (devDescs) { |
| 907 | |
| 908 | offline.put(deviceId, this.offline.get(deviceId)); |
| 909 | |
| 910 | for (Entry<ProviderId, DeviceDescriptions> |
| 911 | prov : devDescs.entrySet()) { |
| 912 | |
| 913 | final ProviderId provId = prov.getKey(); |
| 914 | final DeviceDescriptions descs = prov.getValue(); |
| 915 | |
| 916 | devices.put(new DeviceFragmentId(deviceId, provId), |
| 917 | descs.getDeviceDesc().timestamp()); |
| 918 | |
| 919 | for (Entry<PortNumber, Timestamped<PortDescription>> |
| 920 | portDesc : descs.getPortDescs().entrySet()) { |
| 921 | |
| 922 | final PortNumber number = portDesc.getKey(); |
| 923 | ports.put(new PortFragmentId(deviceId, provId, number), |
| 924 | portDesc.getValue().timestamp()); |
| 925 | } |
| 926 | } |
| 927 | } |
| 928 | } |
| 929 | |
| 930 | return new DeviceAntiEntropyAdvertisement(self, devices, ports, offline); |
| 931 | } |
| 932 | |
| 933 | /** |
| 934 | * Responds to anti-entropy advertisement message. |
| 935 | * <P> |
| 936 | * Notify sender about out-dated information using regular replication message. |
| 937 | * Send back advertisement to sender if not in sync. |
| 938 | * |
| 939 | * @param advertisement to respond to |
| 940 | */ |
| 941 | private void handleAdvertisement(DeviceAntiEntropyAdvertisement advertisement) { |
| 942 | |
| 943 | final NodeId sender = advertisement.sender(); |
| 944 | |
| 945 | Map<DeviceFragmentId, Timestamp> devAds = new HashMap<>(advertisement.deviceFingerPrints()); |
| 946 | Map<PortFragmentId, Timestamp> portAds = new HashMap<>(advertisement.ports()); |
| 947 | Map<DeviceId, Timestamp> offlineAds = new HashMap<>(advertisement.offline()); |
| 948 | |
| 949 | // Fragments to request |
| 950 | Collection<DeviceFragmentId> reqDevices = new ArrayList<>(); |
| 951 | Collection<PortFragmentId> reqPorts = new ArrayList<>(); |
| 952 | |
| 953 | for (Entry<DeviceId, ConcurrentMap<ProviderId, DeviceDescriptions>> de : deviceDescs.entrySet()) { |
| 954 | final DeviceId deviceId = de.getKey(); |
| 955 | final Map<ProviderId, DeviceDescriptions> lDevice = de.getValue(); |
| 956 | |
| 957 | synchronized (lDevice) { |
| 958 | // latestTimestamp across provider |
| 959 | // Note: can be null initially |
| 960 | Timestamp localLatest = offline.get(deviceId); |
| 961 | |
| 962 | // handle device Ads |
| 963 | for (Entry<ProviderId, DeviceDescriptions> prov : lDevice.entrySet()) { |
| 964 | final ProviderId provId = prov.getKey(); |
| 965 | final DeviceDescriptions lDeviceDescs = prov.getValue(); |
| 966 | |
| 967 | final DeviceFragmentId devFragId = new DeviceFragmentId(deviceId, provId); |
| 968 | |
| 969 | |
| 970 | Timestamped<DeviceDescription> lProvDevice = lDeviceDescs.getDeviceDesc(); |
| 971 | Timestamp advDevTimestamp = devAds.get(devFragId); |
| 972 | |
| 973 | if (advDevTimestamp == null || lProvDevice.isNewer(advDevTimestamp)) { |
| 974 | // remote does not have it or outdated, suggest |
| 975 | notifyPeer(sender, new InternalDeviceEvent(provId, deviceId, lProvDevice)); |
| 976 | } else if (!lProvDevice.timestamp().equals(advDevTimestamp)) { |
| 977 | // local is outdated, request |
| 978 | reqDevices.add(devFragId); |
| 979 | } |
| 980 | |
| 981 | // handle port Ads |
| 982 | for (Entry<PortNumber, Timestamped<PortDescription>> |
| 983 | pe : lDeviceDescs.getPortDescs().entrySet()) { |
| 984 | |
| 985 | final PortNumber num = pe.getKey(); |
| 986 | final Timestamped<PortDescription> lPort = pe.getValue(); |
| 987 | |
| 988 | final PortFragmentId portFragId = new PortFragmentId(deviceId, provId, num); |
| 989 | |
| 990 | Timestamp advPortTimestamp = portAds.get(portFragId); |
Yuta HIGUCHI | ec76bfe | 2014-10-09 20:17:07 -0700 | [diff] [blame] | 991 | if (advPortTimestamp == null || lPort.isNewer(advPortTimestamp)) { |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 992 | // remote does not have it or outdated, suggest |
| 993 | notifyPeer(sender, new InternalPortStatusEvent(provId, deviceId, lPort)); |
| 994 | } else if (!lPort.timestamp().equals(advPortTimestamp)) { |
| 995 | // local is outdated, request |
| 996 | log.trace("need update {} < {}", lPort.timestamp(), advPortTimestamp); |
| 997 | reqPorts.add(portFragId); |
| 998 | } |
| 999 | |
| 1000 | // remove port Ad already processed |
| 1001 | portAds.remove(portFragId); |
| 1002 | } // end local port loop |
| 1003 | |
| 1004 | // remove device Ad already processed |
| 1005 | devAds.remove(devFragId); |
| 1006 | |
| 1007 | // find latest and update |
| 1008 | final Timestamp providerLatest = lDeviceDescs.getLatestTimestamp(); |
| 1009 | if (localLatest == null || |
| 1010 | providerLatest.compareTo(localLatest) > 0) { |
| 1011 | localLatest = providerLatest; |
| 1012 | } |
| 1013 | } // end local provider loop |
| 1014 | |
| 1015 | // checking if remote timestamp is more recent. |
| 1016 | Timestamp rOffline = offlineAds.get(deviceId); |
| 1017 | if (rOffline != null && |
| 1018 | rOffline.compareTo(localLatest) > 0) { |
| 1019 | // remote offline timestamp suggests that the |
| 1020 | // device is off-line |
| 1021 | markOfflineInternal(deviceId, rOffline); |
| 1022 | } |
| 1023 | |
| 1024 | Timestamp lOffline = offline.get(deviceId); |
| 1025 | if (lOffline != null && rOffline == null) { |
| 1026 | // locally offline, but remote is online, suggest offline |
| 1027 | notifyPeer(sender, new InternalDeviceOfflineEvent(deviceId, lOffline)); |
| 1028 | } |
| 1029 | |
| 1030 | // remove device offline Ad already processed |
| 1031 | offlineAds.remove(deviceId); |
| 1032 | } // end local device loop |
| 1033 | } // device lock |
| 1034 | |
| 1035 | // If there is any Ads left, request them |
| 1036 | log.trace("Ads left {}, {}", devAds, portAds); |
| 1037 | reqDevices.addAll(devAds.keySet()); |
| 1038 | reqPorts.addAll(portAds.keySet()); |
| 1039 | |
| 1040 | if (reqDevices.isEmpty() && reqPorts.isEmpty()) { |
| 1041 | log.trace("Nothing to request to remote peer {}", sender); |
| 1042 | return; |
| 1043 | } |
| 1044 | |
| 1045 | log.info("Need to sync {} {}", reqDevices, reqPorts); |
| 1046 | |
| 1047 | // 2-way Anti-Entropy for now |
| 1048 | try { |
| 1049 | unicastMessage(sender, DEVICE_ADVERTISE, createAdvertisement()); |
| 1050 | } catch (IOException e) { |
| 1051 | log.error("Failed to send response advertisement to " + sender, e); |
| 1052 | } |
| 1053 | |
| 1054 | // Sketch of 3-way Anti-Entropy |
| 1055 | // DeviceAntiEntropyRequest request = new DeviceAntiEntropyRequest(self, reqDevices, reqPorts); |
| 1056 | // ClusterMessage message = new ClusterMessage( |
| 1057 | // clusterService.getLocalNode().id(), |
| 1058 | // GossipDeviceStoreMessageSubjects.DEVICE_REQUEST, |
| 1059 | // SERIALIZER.encode(request)); |
| 1060 | // |
| 1061 | // try { |
| 1062 | // clusterCommunicator.unicast(message, advertisement.sender()); |
| 1063 | // } catch (IOException e) { |
| 1064 | // log.error("Failed to send advertisement reply to " |
| 1065 | // + advertisement.sender(), e); |
| 1066 | // } |
Madan Jampani | 47c9373 | 2014-10-06 20:46:08 -0700 | [diff] [blame] | 1067 | } |
| 1068 | |
Madan Jampani | 255a58b | 2014-10-09 12:08:20 -0700 | [diff] [blame] | 1069 | private void notifyDelegateIfNotNull(DeviceEvent event) { |
| 1070 | if (event != null) { |
| 1071 | notifyDelegate(event); |
| 1072 | } |
| 1073 | } |
| 1074 | |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 1075 | private final class SendAdvertisementTask implements Runnable { |
| 1076 | |
| 1077 | @Override |
| 1078 | public void run() { |
| 1079 | if (Thread.currentThread().isInterrupted()) { |
| 1080 | log.info("Interrupted, quitting"); |
| 1081 | return; |
| 1082 | } |
| 1083 | |
| 1084 | try { |
| 1085 | final NodeId self = clusterService.getLocalNode().id(); |
| 1086 | Set<ControllerNode> nodes = clusterService.getNodes(); |
| 1087 | |
| 1088 | ImmutableList<NodeId> nodeIds = FluentIterable.from(nodes) |
| 1089 | .transform(toNodeId()) |
| 1090 | .toList(); |
| 1091 | |
| 1092 | if (nodeIds.size() == 1 && nodeIds.get(0).equals(self)) { |
Yuta HIGUCHI | 3708308 | 2014-10-13 10:38:38 -0700 | [diff] [blame^] | 1093 | log.debug("No other peers in the cluster."); |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 1094 | return; |
| 1095 | } |
| 1096 | |
| 1097 | NodeId peer; |
| 1098 | do { |
| 1099 | int idx = RandomUtils.nextInt(0, nodeIds.size()); |
| 1100 | peer = nodeIds.get(idx); |
| 1101 | } while (peer.equals(self)); |
| 1102 | |
| 1103 | DeviceAntiEntropyAdvertisement ad = createAdvertisement(); |
| 1104 | |
| 1105 | if (Thread.currentThread().isInterrupted()) { |
| 1106 | log.info("Interrupted, quitting"); |
| 1107 | return; |
| 1108 | } |
| 1109 | |
| 1110 | try { |
| 1111 | unicastMessage(peer, DEVICE_ADVERTISE, ad); |
| 1112 | } catch (IOException e) { |
| 1113 | log.error("Failed to send anti-entropy advertisement", e); |
| 1114 | return; |
| 1115 | } |
| 1116 | } catch (Exception e) { |
| 1117 | // catch all Exception to avoid Scheduled task being suppressed. |
| 1118 | log.error("Exception thrown while sending advertisement", e); |
| 1119 | } |
| 1120 | } |
| 1121 | } |
| 1122 | |
Madan Jampani | 47c9373 | 2014-10-06 20:46:08 -0700 | [diff] [blame] | 1123 | private class InternalDeviceEventListener implements ClusterMessageHandler { |
| 1124 | @Override |
| 1125 | public void handle(ClusterMessage message) { |
Madan Jampani | 2532253 | 2014-10-08 11:20:38 -0700 | [diff] [blame] | 1126 | |
Madan Jampani | 47c9373 | 2014-10-06 20:46:08 -0700 | [diff] [blame] | 1127 | log.info("Received device update event from peer: {}", message.sender()); |
Madan Jampani | 53e44e6 | 2014-10-07 12:39:51 -0700 | [diff] [blame] | 1128 | InternalDeviceEvent event = (InternalDeviceEvent) SERIALIZER.decode(message.payload()); |
Madan Jampani | 2532253 | 2014-10-08 11:20:38 -0700 | [diff] [blame] | 1129 | |
Madan Jampani | 47c9373 | 2014-10-06 20:46:08 -0700 | [diff] [blame] | 1130 | ProviderId providerId = event.providerId(); |
| 1131 | DeviceId deviceId = event.deviceId(); |
| 1132 | Timestamped<DeviceDescription> deviceDescription = event.deviceDescription(); |
Madan Jampani | 2532253 | 2014-10-08 11:20:38 -0700 | [diff] [blame] | 1133 | |
Madan Jampani | 255a58b | 2014-10-09 12:08:20 -0700 | [diff] [blame] | 1134 | notifyDelegateIfNotNull(createOrUpdateDeviceInternal(providerId, deviceId, deviceDescription)); |
Madan Jampani | 47c9373 | 2014-10-06 20:46:08 -0700 | [diff] [blame] | 1135 | } |
| 1136 | } |
| 1137 | |
Madan Jampani | 2532253 | 2014-10-08 11:20:38 -0700 | [diff] [blame] | 1138 | private class InternalDeviceOfflineEventListener implements ClusterMessageHandler { |
| 1139 | @Override |
| 1140 | public void handle(ClusterMessage message) { |
| 1141 | |
| 1142 | log.info("Received device offline event from peer: {}", message.sender()); |
| 1143 | InternalDeviceOfflineEvent event = (InternalDeviceOfflineEvent) SERIALIZER.decode(message.payload()); |
| 1144 | |
| 1145 | DeviceId deviceId = event.deviceId(); |
| 1146 | Timestamp timestamp = event.timestamp(); |
| 1147 | |
Madan Jampani | 255a58b | 2014-10-09 12:08:20 -0700 | [diff] [blame] | 1148 | notifyDelegateIfNotNull(markOfflineInternal(deviceId, timestamp)); |
Madan Jampani | 2532253 | 2014-10-08 11:20:38 -0700 | [diff] [blame] | 1149 | } |
| 1150 | } |
| 1151 | |
Madan Jampani | 3fc72ed | 2014-10-08 12:50:27 -0700 | [diff] [blame] | 1152 | private class InternalDeviceRemovedEventListener implements ClusterMessageHandler { |
| 1153 | @Override |
| 1154 | public void handle(ClusterMessage message) { |
| 1155 | |
| 1156 | log.info("Received device removed event from peer: {}", message.sender()); |
| 1157 | InternalDeviceRemovedEvent event = (InternalDeviceRemovedEvent) SERIALIZER.decode(message.payload()); |
| 1158 | |
| 1159 | DeviceId deviceId = event.deviceId(); |
| 1160 | Timestamp timestamp = event.timestamp(); |
| 1161 | |
Madan Jampani | 255a58b | 2014-10-09 12:08:20 -0700 | [diff] [blame] | 1162 | notifyDelegateIfNotNull(removeDeviceInternal(deviceId, timestamp)); |
Madan Jampani | 3fc72ed | 2014-10-08 12:50:27 -0700 | [diff] [blame] | 1163 | } |
| 1164 | } |
| 1165 | |
Madan Jampani | 47c9373 | 2014-10-06 20:46:08 -0700 | [diff] [blame] | 1166 | private class InternalPortEventListener implements ClusterMessageHandler { |
| 1167 | @Override |
| 1168 | public void handle(ClusterMessage message) { |
| 1169 | |
| 1170 | log.info("Received port update event from peer: {}", message.sender()); |
Madan Jampani | 53e44e6 | 2014-10-07 12:39:51 -0700 | [diff] [blame] | 1171 | InternalPortEvent event = (InternalPortEvent) SERIALIZER.decode(message.payload()); |
Madan Jampani | 47c9373 | 2014-10-06 20:46:08 -0700 | [diff] [blame] | 1172 | |
| 1173 | ProviderId providerId = event.providerId(); |
| 1174 | DeviceId deviceId = event.deviceId(); |
| 1175 | Timestamped<List<PortDescription>> portDescriptions = event.portDescriptions(); |
| 1176 | |
Madan Jampani | 255a58b | 2014-10-09 12:08:20 -0700 | [diff] [blame] | 1177 | notifyDelegate(updatePortsInternal(providerId, deviceId, portDescriptions)); |
Madan Jampani | 47c9373 | 2014-10-06 20:46:08 -0700 | [diff] [blame] | 1178 | } |
| 1179 | } |
| 1180 | |
| 1181 | private class InternalPortStatusEventListener implements ClusterMessageHandler { |
| 1182 | @Override |
| 1183 | public void handle(ClusterMessage message) { |
| 1184 | |
| 1185 | log.info("Received port status update event from peer: {}", message.sender()); |
Madan Jampani | 53e44e6 | 2014-10-07 12:39:51 -0700 | [diff] [blame] | 1186 | InternalPortStatusEvent event = (InternalPortStatusEvent) SERIALIZER.decode(message.payload()); |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 1187 | log.info("{}", event); |
Madan Jampani | 47c9373 | 2014-10-06 20:46:08 -0700 | [diff] [blame] | 1188 | |
| 1189 | ProviderId providerId = event.providerId(); |
| 1190 | DeviceId deviceId = event.deviceId(); |
| 1191 | Timestamped<PortDescription> portDescription = event.portDescription(); |
| 1192 | |
Madan Jampani | 255a58b | 2014-10-09 12:08:20 -0700 | [diff] [blame] | 1193 | notifyDelegateIfNotNull(updatePortStatusInternal(providerId, deviceId, portDescription)); |
Madan Jampani | 47c9373 | 2014-10-06 20:46:08 -0700 | [diff] [blame] | 1194 | } |
| 1195 | } |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 1196 | |
| 1197 | private final class InternalDeviceAdvertisementListener |
| 1198 | implements ClusterMessageHandler { |
| 1199 | |
| 1200 | @Override |
| 1201 | public void handle(ClusterMessage message) { |
| 1202 | log.info("Received Device advertisement from peer: {}", message.sender()); |
| 1203 | DeviceAntiEntropyAdvertisement advertisement = SERIALIZER.decode(message.payload()); |
| 1204 | handleAdvertisement(advertisement); |
| 1205 | } |
| 1206 | } |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 1207 | } |