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