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