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