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