blob: 551e731c17d2090b9fc47e7dd6b16f4009293f1d [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 Open Networking Laboratory
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07003 *
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'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.store.link.impl;
Madan Jampani2ff05592014-10-10 15:42:47 -070017
Ray Milkeyb7f0f642016-01-22 16:08:14 -080018import java.io.IOException;
19import java.util.Collections;
20import java.util.HashMap;
21import java.util.HashSet;
22import java.util.Map;
23import java.util.Map.Entry;
24import java.util.Set;
25import java.util.concurrent.ConcurrentHashMap;
26import java.util.concurrent.ConcurrentMap;
27import java.util.concurrent.ExecutorService;
28import java.util.concurrent.Executors;
29import java.util.concurrent.ScheduledExecutorService;
30import java.util.concurrent.TimeUnit;
31
Madan Jampania97e8202014-10-10 17:01:33 -070032import org.apache.commons.lang3.RandomUtils;
Madan Jampani2ff05592014-10-10 15:42:47 -070033import org.apache.felix.scr.annotations.Activate;
Madan Jampani2ff05592014-10-10 15:42:47 -070034import org.apache.felix.scr.annotations.Deactivate;
35import org.apache.felix.scr.annotations.Reference;
36import org.apache.felix.scr.annotations.ReferenceCardinality;
37import org.apache.felix.scr.annotations.Service;
Ray Milkey7bbeb3f2014-12-11 14:59:26 -080038import org.onlab.util.KryoNamespace;
Brian O'Connorabafb502014-12-02 22:26:20 -080039import org.onosproject.cluster.ClusterService;
40import org.onosproject.cluster.ControllerNode;
41import org.onosproject.cluster.NodeId;
Marc De Leenheerb473b9d2015-02-06 15:21:03 -080042import org.onosproject.mastership.MastershipService;
Brian O'Connorabafb502014-12-02 22:26:20 -080043import org.onosproject.net.AnnotationsUtil;
44import org.onosproject.net.ConnectPoint;
45import org.onosproject.net.DefaultAnnotations;
46import org.onosproject.net.DefaultLink;
47import org.onosproject.net.DeviceId;
48import org.onosproject.net.Link;
49import org.onosproject.net.Link.Type;
50import org.onosproject.net.LinkKey;
51import org.onosproject.net.SparseAnnotations;
52import org.onosproject.net.device.DeviceClockService;
53import org.onosproject.net.link.DefaultLinkDescription;
54import org.onosproject.net.link.LinkDescription;
55import org.onosproject.net.link.LinkEvent;
56import org.onosproject.net.link.LinkStore;
57import org.onosproject.net.link.LinkStoreDelegate;
58import org.onosproject.net.provider.ProviderId;
59import org.onosproject.store.AbstractStore;
60import org.onosproject.store.Timestamp;
61import org.onosproject.store.cluster.messaging.ClusterCommunicationService;
62import org.onosproject.store.cluster.messaging.ClusterMessage;
63import org.onosproject.store.cluster.messaging.ClusterMessageHandler;
64import org.onosproject.store.cluster.messaging.MessageSubject;
65import org.onosproject.store.impl.Timestamped;
66import org.onosproject.store.serializers.KryoSerializer;
Brian O'Connor6de2e202015-05-21 14:30:41 -070067import org.onosproject.store.serializers.custom.DistributedStoreSerializers;
Madan Jampani2ff05592014-10-10 15:42:47 -070068import org.slf4j.Logger;
69
Ray Milkeyb7f0f642016-01-22 16:08:14 -080070import com.google.common.base.Function;
71import com.google.common.collect.FluentIterable;
72import com.google.common.collect.ImmutableList;
73import com.google.common.collect.Multimaps;
74import com.google.common.collect.SetMultimap;
75import com.google.common.collect.Sets;
Madan Jampani2ff05592014-10-10 15:42:47 -070076
Thomas Vachuska57126fe2014-11-11 17:13:24 -080077import static com.google.common.base.Preconditions.checkNotNull;
78import static com.google.common.base.Predicates.notNull;
79import static com.google.common.collect.Multimaps.synchronizedSetMultimap;
Madan Jampania97e8202014-10-10 17:01:33 -070080import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
Thomas Vachuska6f94ded2015-02-21 14:02:38 -080081import static org.onlab.util.Tools.groupedThreads;
Ray Milkey7bbeb3f2014-12-11 14:59:26 -080082import static org.onlab.util.Tools.minPriority;
Brian O'Connorabafb502014-12-02 22:26:20 -080083import static org.onosproject.cluster.ControllerNodeToNodeId.toNodeId;
84import static org.onosproject.net.DefaultAnnotations.merge;
85import static org.onosproject.net.DefaultAnnotations.union;
86import static org.onosproject.net.Link.State.ACTIVE;
87import static org.onosproject.net.Link.State.INACTIVE;
88import static org.onosproject.net.Link.Type.DIRECT;
89import static org.onosproject.net.Link.Type.INDIRECT;
90import static org.onosproject.net.LinkKey.linkKey;
Jian Li11599162016-01-15 15:46:16 -080091import static org.onosproject.net.link.LinkEvent.Type.LINK_ADDED;
92import static org.onosproject.net.link.LinkEvent.Type.LINK_REMOVED;
93import static org.onosproject.net.link.LinkEvent.Type.LINK_UPDATED;
Brian O'Connorabafb502014-12-02 22:26:20 -080094import static org.onosproject.store.link.impl.GossipLinkStoreMessageSubjects.LINK_ANTI_ENTROPY_ADVERTISEMENT;
Madan Jampani2ff05592014-10-10 15:42:47 -070095import static org.slf4j.LoggerFactory.getLogger;
Madan Jampani2ff05592014-10-10 15:42:47 -070096
97/**
98 * Manages inventory of infrastructure links in distributed data store
99 * that uses optimistic replication and gossip based techniques.
100 */
Jian Li11599162016-01-15 15:46:16 -0800101//@Component(immediate = true, enabled = false)
Madan Jampani2ff05592014-10-10 15:42:47 -0700102@Service
103public class GossipLinkStore
104 extends AbstractStore<LinkEvent, LinkStoreDelegate>
105 implements LinkStore {
106
Marc De Leenheerb473b9d2015-02-06 15:21:03 -0800107 // Timeout in milliseconds to process links on remote master node
108 private static final int REMOTE_MASTER_TIMEOUT = 1000;
109
Madan Jampani2ff05592014-10-10 15:42:47 -0700110 private final Logger log = getLogger(getClass());
111
112 // Link inventory
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700113 private final ConcurrentMap<LinkKey, Map<ProviderId, Timestamped<LinkDescription>>> linkDescs =
Madan Jampani2ff05592014-10-10 15:42:47 -0700114 new ConcurrentHashMap<>();
115
116 // Link instance cache
117 private final ConcurrentMap<LinkKey, Link> links = new ConcurrentHashMap<>();
118
119 // Egress and ingress link sets
120 private final SetMultimap<DeviceId, LinkKey> srcLinks = createSynchronizedHashMultiMap();
121 private final SetMultimap<DeviceId, LinkKey> dstLinks = createSynchronizedHashMultiMap();
122
123 // Remove links
Yuta HIGUCHIb9125562014-12-01 23:28:22 -0800124 private final Map<LinkKey, Timestamp> removedLinks = new ConcurrentHashMap<>();
Madan Jampani2ff05592014-10-10 15:42:47 -0700125
126 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Yuta HIGUCHI093e83e2014-10-10 22:26:11 -0700127 protected DeviceClockService deviceClockService;
Madan Jampani2ff05592014-10-10 15:42:47 -0700128
129 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
130 protected ClusterCommunicationService clusterCommunicator;
131
132 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
133 protected ClusterService clusterService;
134
Marc De Leenheerb473b9d2015-02-06 15:21:03 -0800135 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
136 protected MastershipService mastershipService;
137
Yuta HIGUCHI3e5d11a2014-11-04 14:16:44 -0800138 protected static final KryoSerializer SERIALIZER = new KryoSerializer() {
Madan Jampani2ff05592014-10-10 15:42:47 -0700139 @Override
140 protected void setupKryoPool() {
Yuta HIGUCHI8d143d22014-10-19 23:15:09 -0700141 serializerPool = KryoNamespace.newBuilder()
Yuta HIGUCHI91768e32014-11-22 05:06:35 -0800142 .register(DistributedStoreSerializers.STORE_COMMON)
143 .nextId(DistributedStoreSerializers.STORE_CUSTOM_BEGIN)
Madan Jampani2ff05592014-10-10 15:42:47 -0700144 .register(InternalLinkEvent.class)
145 .register(InternalLinkRemovedEvent.class)
Madan Jampanid2054d42014-10-10 17:27:06 -0700146 .register(LinkAntiEntropyAdvertisement.class)
147 .register(LinkFragmentId.class)
Marc De Leenheerb473b9d2015-02-06 15:21:03 -0800148 .register(LinkInjectedEvent.class)
Yuta HIGUCHI91768e32014-11-22 05:06:35 -0800149 .build();
Madan Jampani2ff05592014-10-10 15:42:47 -0700150 }
151 };
152
Yuta HIGUCHI80d56592014-11-25 15:11:13 -0800153 private ExecutorService executor;
154
Yuta HIGUCHI06586272014-11-25 14:27:03 -0800155 private ScheduledExecutorService backgroundExecutors;
Madan Jampania97e8202014-10-10 17:01:33 -0700156
Madan Jampani2ff05592014-10-10 15:42:47 -0700157 @Activate
158 public void activate() {
159
Thomas Vachuska6f94ded2015-02-21 14:02:38 -0800160 executor = Executors.newCachedThreadPool(groupedThreads("onos/link", "fg-%d"));
Yuta HIGUCHI80d56592014-11-25 15:11:13 -0800161
Yuta HIGUCHI06586272014-11-25 14:27:03 -0800162 backgroundExecutors =
Thomas Vachuska6f94ded2015-02-21 14:02:38 -0800163 newSingleThreadScheduledExecutor(minPriority(groupedThreads("onos/link", "bg-%d")));
Madan Jampania97e8202014-10-10 17:01:33 -0700164
Madan Jampani2af244a2015-02-22 13:12:01 -0800165 clusterCommunicator.addSubscriber(
166 GossipLinkStoreMessageSubjects.LINK_UPDATE,
167 new InternalLinkEventListener(), executor);
168 clusterCommunicator.addSubscriber(
169 GossipLinkStoreMessageSubjects.LINK_REMOVED,
170 new InternalLinkRemovedEventListener(), executor);
171 clusterCommunicator.addSubscriber(
172 GossipLinkStoreMessageSubjects.LINK_ANTI_ENTROPY_ADVERTISEMENT,
173 new InternalLinkAntiEntropyAdvertisementListener(), backgroundExecutors);
174 clusterCommunicator.addSubscriber(
175 GossipLinkStoreMessageSubjects.LINK_INJECTED,
176 new LinkInjectedEventListener(), executor);
177
Madan Jampania97e8202014-10-10 17:01:33 -0700178 long initialDelaySec = 5;
179 long periodSec = 5;
180 // start anti-entropy thread
Yuta HIGUCHI06586272014-11-25 14:27:03 -0800181 backgroundExecutors.scheduleAtFixedRate(new SendAdvertisementTask(),
Madan Jampania97e8202014-10-10 17:01:33 -0700182 initialDelaySec, periodSec, TimeUnit.SECONDS);
Madan Jampani2ff05592014-10-10 15:42:47 -0700183
184 log.info("Started");
185 }
186
187 @Deactivate
188 public void deactivate() {
Madan Jampani3ffbb272014-10-13 11:19:37 -0700189
Yuta HIGUCHI80d56592014-11-25 15:11:13 -0800190 executor.shutdownNow();
191
Yuta HIGUCHI06586272014-11-25 14:27:03 -0800192 backgroundExecutors.shutdownNow();
Madan Jampani3ffbb272014-10-13 11:19:37 -0700193 try {
Yuta HIGUCHI06586272014-11-25 14:27:03 -0800194 if (!backgroundExecutors.awaitTermination(5, TimeUnit.SECONDS)) {
Madan Jampani3ffbb272014-10-13 11:19:37 -0700195 log.error("Timeout during executor shutdown");
196 }
197 } catch (InterruptedException e) {
198 log.error("Error during executor shutdown", e);
199 }
200
Madan Jampani2ff05592014-10-10 15:42:47 -0700201 linkDescs.clear();
202 links.clear();
203 srcLinks.clear();
204 dstLinks.clear();
205 log.info("Stopped");
206 }
207
208 @Override
209 public int getLinkCount() {
210 return links.size();
211 }
212
213 @Override
214 public Iterable<Link> getLinks() {
215 return Collections.unmodifiableCollection(links.values());
216 }
217
218 @Override
219 public Set<Link> getDeviceEgressLinks(DeviceId deviceId) {
220 // lock for iteration
221 synchronized (srcLinks) {
222 return FluentIterable.from(srcLinks.get(deviceId))
223 .transform(lookupLink())
224 .filter(notNull())
225 .toSet();
226 }
227 }
228
229 @Override
230 public Set<Link> getDeviceIngressLinks(DeviceId deviceId) {
231 // lock for iteration
232 synchronized (dstLinks) {
233 return FluentIterable.from(dstLinks.get(deviceId))
234 .transform(lookupLink())
235 .filter(notNull())
236 .toSet();
237 }
238 }
239
240 @Override
241 public Link getLink(ConnectPoint src, ConnectPoint dst) {
Yuta HIGUCHI18ab8a92014-10-13 11:16:19 -0700242 return links.get(linkKey(src, dst));
Madan Jampani2ff05592014-10-10 15:42:47 -0700243 }
244
245 @Override
246 public Set<Link> getEgressLinks(ConnectPoint src) {
247 Set<Link> egress = new HashSet<>();
HIGUCHI Yuta4d973eb2015-02-26 14:28:49 -0800248 //
249 // Change `srcLinks` to ConcurrentMap<DeviceId, (Concurrent)Set>
250 // to remove this synchronized block, if we hit performance issue.
251 // SetMultiMap#get returns wrapped collection to provide modifiable-view.
252 // And the wrapped collection is not concurrent access safe.
253 //
254 // Our use case here does not require returned collection to be modifiable,
255 // so the wrapped collection forces us to lock the whole multiset,
256 // for benefit we don't need.
257 //
258 // Same applies to `dstLinks`
259 synchronized (srcLinks) {
260 for (LinkKey linkKey : srcLinks.get(src.deviceId())) {
261 if (linkKey.src().equals(src)) {
262 Link link = links.get(linkKey);
263 if (link != null) {
264 egress.add(link);
265 } else {
266 log.debug("Egress link for {} was null, skipped", linkKey);
267 }
Ray Milkey7bbeb3f2014-12-11 14:59:26 -0800268 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700269 }
270 }
271 return egress;
272 }
273
274 @Override
275 public Set<Link> getIngressLinks(ConnectPoint dst) {
276 Set<Link> ingress = new HashSet<>();
HIGUCHI Yuta4d973eb2015-02-26 14:28:49 -0800277 synchronized (dstLinks) {
278 for (LinkKey linkKey : dstLinks.get(dst.deviceId())) {
279 if (linkKey.dst().equals(dst)) {
280 Link link = links.get(linkKey);
281 if (link != null) {
282 ingress.add(link);
283 } else {
284 log.debug("Ingress link for {} was null, skipped", linkKey);
285 }
Ray Milkey7bbeb3f2014-12-11 14:59:26 -0800286 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700287 }
288 }
289 return ingress;
290 }
291
292 @Override
293 public LinkEvent createOrUpdateLink(ProviderId providerId,
294 LinkDescription linkDescription) {
295
Marc De Leenheerb473b9d2015-02-06 15:21:03 -0800296 final DeviceId dstDeviceId = linkDescription.dst().deviceId();
297 final NodeId localNode = clusterService.getLocalNode().id();
298 final NodeId dstNode = mastershipService.getMasterFor(dstDeviceId);
Madan Jampani2ff05592014-10-10 15:42:47 -0700299
Marc De Leenheerb473b9d2015-02-06 15:21:03 -0800300 // Process link update only if we're the master of the destination node,
301 // otherwise signal the actual master.
302 LinkEvent linkEvent = null;
303 if (localNode.equals(dstNode)) {
Madan Jampani2ff05592014-10-10 15:42:47 -0700304
Marc De Leenheerb473b9d2015-02-06 15:21:03 -0800305 Timestamp newTimestamp = deviceClockService.getTimestamp(dstDeviceId);
306
307 final Timestamped<LinkDescription> deltaDesc = new Timestamped<>(linkDescription, newTimestamp);
308
309 LinkKey key = linkKey(linkDescription.src(), linkDescription.dst());
310 final Timestamped<LinkDescription> mergedDesc;
311 Map<ProviderId, Timestamped<LinkDescription>> map = getOrCreateLinkDescriptions(key);
312
313 synchronized (map) {
314 linkEvent = createOrUpdateLinkInternal(providerId, deltaDesc);
315 mergedDesc = map.get(providerId);
316 }
317
318 if (linkEvent != null) {
Madan Jampanif2af7712015-05-29 18:43:52 -0700319 log.debug("Notifying peers of a link update topology event from providerId: "
Marc De Leenheerb473b9d2015-02-06 15:21:03 -0800320 + "{} between src: {} and dst: {}",
321 providerId, linkDescription.src(), linkDescription.dst());
322 notifyPeers(new InternalLinkEvent(providerId, mergedDesc));
323 }
324
325 } else {
HIGUCHI Yuta1979f552015-12-28 21:24:26 -0800326 // Only forward for ConfigProvider
327 // Forwarding was added as a workaround for ONOS-490
HIGUCHI Yuta4ea4e422016-01-13 16:40:34 -0800328 if (!providerId.scheme().equals("cfg")) {
HIGUCHI Yuta1979f552015-12-28 21:24:26 -0800329 return null;
330 }
HIGUCHI Yutadc2e7c22015-02-24 12:19:47 -0800331 // FIXME Temporary hack for NPE (ONOS-1171).
332 // Proper fix is to implement forwarding to master on ConfigProvider
333 // redo ONOS-490
334 if (dstNode == null) {
335 // silently ignore
336 return null;
337 }
338
Marc De Leenheerb473b9d2015-02-06 15:21:03 -0800339
340 LinkInjectedEvent linkInjectedEvent = new LinkInjectedEvent(providerId, linkDescription);
Marc De Leenheerb473b9d2015-02-06 15:21:03 -0800341
Brian O'Connor5eb77c82015-03-02 18:09:39 -0800342 // TODO check unicast return value
Madan Jampani2bfa94c2015-04-11 05:03:49 -0700343 clusterCommunicator.unicast(linkInjectedEvent,
344 GossipLinkStoreMessageSubjects.LINK_INJECTED,
345 SERIALIZER::encode,
346 dstNode);
Yuta HIGUCHI92cd51e2014-10-13 10:51:45 -0700347 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700348
Marc De Leenheerb473b9d2015-02-06 15:21:03 -0800349 return linkEvent;
Madan Jampani2ff05592014-10-10 15:42:47 -0700350 }
351
Thomas Vachuska57126fe2014-11-11 17:13:24 -0800352 @Override
353 public LinkEvent removeOrDownLink(ConnectPoint src, ConnectPoint dst) {
354 Link link = getLink(src, dst);
355 if (link == null) {
356 return null;
357 }
358
359 if (link.isDurable()) {
360 // FIXME: this is not the right thing to call for the gossip store; will not sync link state!!!
361 return link.state() == INACTIVE ? null :
362 updateLink(linkKey(link.src(), link.dst()), link,
Ray Milkey2693bda2016-01-22 16:08:14 -0800363 DefaultLink.builder()
364 .providerId(link.providerId())
365 .src(link.src())
366 .dst(link.dst())
367 .type(link.type())
368 .state(INACTIVE)
369 .isExpected(link.isExpected())
370 .annotations(link.annotations())
371 .build());
Thomas Vachuska57126fe2014-11-11 17:13:24 -0800372 }
373 return removeLink(src, dst);
374 }
375
Madan Jampani2ff05592014-10-10 15:42:47 -0700376 private LinkEvent createOrUpdateLinkInternal(
377 ProviderId providerId,
378 Timestamped<LinkDescription> linkDescription) {
379
Yuta HIGUCHIa85542b2014-10-21 19:29:49 -0700380 final LinkKey key = linkKey(linkDescription.value().src(),
Marc De Leenheerb473b9d2015-02-06 15:21:03 -0800381 linkDescription.value().dst());
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700382 Map<ProviderId, Timestamped<LinkDescription>> descs = getOrCreateLinkDescriptions(key);
Madan Jampani2ff05592014-10-10 15:42:47 -0700383
384 synchronized (descs) {
385 // if the link was previously removed, we should proceed if and
386 // only if this request is more recent.
387 Timestamp linkRemovedTimestamp = removedLinks.get(key);
388 if (linkRemovedTimestamp != null) {
Jonathan Hart403ea932015-02-20 16:23:00 -0800389 if (linkDescription.isNewerThan(linkRemovedTimestamp)) {
Madan Jampani2ff05592014-10-10 15:42:47 -0700390 removedLinks.remove(key);
391 } else {
Yuta HIGUCHIa85542b2014-10-21 19:29:49 -0700392 log.trace("Link {} was already removed ignoring.", key);
Madan Jampani2ff05592014-10-10 15:42:47 -0700393 return null;
394 }
395 }
396
397 final Link oldLink = links.get(key);
398 // update description
399 createOrUpdateLinkDescription(descs, providerId, linkDescription);
400 final Link newLink = composeLink(descs);
401 if (oldLink == null) {
402 return createLink(key, newLink);
403 }
404 return updateLink(key, oldLink, newLink);
405 }
406 }
407
408 // Guarded by linkDescs value (=locking each Link)
409 private Timestamped<LinkDescription> createOrUpdateLinkDescription(
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700410 Map<ProviderId, Timestamped<LinkDescription>> descs,
Madan Jampani2ff05592014-10-10 15:42:47 -0700411 ProviderId providerId,
412 Timestamped<LinkDescription> linkDescription) {
413
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700414 // merge existing annotations
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700415 Timestamped<LinkDescription> existingLinkDescription = descs.get(providerId);
Madan Jampani2ff05592014-10-10 15:42:47 -0700416 if (existingLinkDescription != null && existingLinkDescription.isNewer(linkDescription)) {
Yuta HIGUCHIa85542b2014-10-21 19:29:49 -0700417 log.trace("local info is more up-to-date, ignoring {}.", linkDescription);
Madan Jampani2ff05592014-10-10 15:42:47 -0700418 return null;
419 }
420 Timestamped<LinkDescription> newLinkDescription = linkDescription;
421 if (existingLinkDescription != null) {
Yuta HIGUCHIa85542b2014-10-21 19:29:49 -0700422 // we only allow transition from INDIRECT -> DIRECT
423 final Type newType;
424 if (existingLinkDescription.value().type() == DIRECT) {
425 newType = DIRECT;
426 } else {
427 newType = linkDescription.value().type();
428 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700429 SparseAnnotations merged = union(existingLinkDescription.value().annotations(),
430 linkDescription.value().annotations());
Thomas Vachuska57126fe2014-11-11 17:13:24 -0800431 newLinkDescription = new Timestamped<>(
Madan Jampani2ff05592014-10-10 15:42:47 -0700432 new DefaultLinkDescription(
433 linkDescription.value().src(),
434 linkDescription.value().dst(),
Ray Milkeyb7f0f642016-01-22 16:08:14 -0800435 newType,
436 existingLinkDescription.value().isExpected(),
437 merged),
Madan Jampani2ff05592014-10-10 15:42:47 -0700438 linkDescription.timestamp());
439 }
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700440 return descs.put(providerId, newLinkDescription);
Madan Jampani2ff05592014-10-10 15:42:47 -0700441 }
442
443 // Creates and stores the link and returns the appropriate event.
444 // Guarded by linkDescs value (=locking each Link)
445 private LinkEvent createLink(LinkKey key, Link newLink) {
Madan Jampani2ff05592014-10-10 15:42:47 -0700446 links.put(key, newLink);
447 srcLinks.put(newLink.src().deviceId(), key);
448 dstLinks.put(newLink.dst().deviceId(), key);
449 return new LinkEvent(LINK_ADDED, newLink);
450 }
451
452 // Updates, if necessary the specified link and returns the appropriate event.
453 // Guarded by linkDescs value (=locking each Link)
454 private LinkEvent updateLink(LinkKey key, Link oldLink, Link newLink) {
Yuta HIGUCHIa85542b2014-10-21 19:29:49 -0700455 // Note: INDIRECT -> DIRECT transition only
456 // so that BDDP discovered Link will not overwrite LDDP Link
Thomas Vachuska57126fe2014-11-11 17:13:24 -0800457 if (oldLink.state() != newLink.state() ||
458 (oldLink.type() == INDIRECT && newLink.type() == DIRECT) ||
Madan Jampani2ff05592014-10-10 15:42:47 -0700459 !AnnotationsUtil.isEqual(oldLink.annotations(), newLink.annotations())) {
460
461 links.put(key, newLink);
Marc De Leenheerb473b9d2015-02-06 15:21:03 -0800462 // strictly speaking following can be omitted
Madan Jampani2ff05592014-10-10 15:42:47 -0700463 srcLinks.put(oldLink.src().deviceId(), key);
464 dstLinks.put(oldLink.dst().deviceId(), key);
465 return new LinkEvent(LINK_UPDATED, newLink);
466 }
467 return null;
468 }
469
470 @Override
471 public LinkEvent removeLink(ConnectPoint src, ConnectPoint dst) {
Yuta HIGUCHI18ab8a92014-10-13 11:16:19 -0700472 final LinkKey key = linkKey(src, dst);
Madan Jampani2ff05592014-10-10 15:42:47 -0700473
474 DeviceId dstDeviceId = dst.deviceId();
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700475 Timestamp timestamp = null;
476 try {
477 timestamp = deviceClockService.getTimestamp(dstDeviceId);
478 } catch (IllegalStateException e) {
Thomas Vachuska6f90c592015-06-03 20:06:58 -0700479 log.debug("Failed to remove link {}, was not the master", key);
480 // there are times when this is called before mastership
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700481 // handoff correctly completes.
482 return null;
483 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700484
485 LinkEvent event = removeLinkInternal(key, timestamp);
486
487 if (event != null) {
Madan Jampanif2af7712015-05-29 18:43:52 -0700488 log.debug("Notifying peers of a link removed topology event for a link "
Madan Jampani2ff05592014-10-10 15:42:47 -0700489 + "between src: {} and dst: {}", src, dst);
Jonathan Hart7d656f42015-01-27 14:07:23 -0800490 notifyPeers(new InternalLinkRemovedEvent(key, timestamp));
Madan Jampani2ff05592014-10-10 15:42:47 -0700491 }
492 return event;
493 }
494
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700495 private static Timestamped<LinkDescription> getPrimaryDescription(
496 Map<ProviderId, Timestamped<LinkDescription>> linkDescriptions) {
497
Madan Jampani2ff05592014-10-10 15:42:47 -0700498 synchronized (linkDescriptions) {
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700499 for (Entry<ProviderId, Timestamped<LinkDescription>>
500 e : linkDescriptions.entrySet()) {
501
502 if (!e.getKey().isAncillary()) {
503 return e.getValue();
504 }
505 }
506 }
507 return null;
508 }
509
510
511 // TODO: consider slicing out as Timestamp utils
512 /**
513 * Checks is timestamp is more recent than timestamped object.
514 *
515 * @param timestamp to check if this is more recent then other
516 * @param timestamped object to be tested against
517 * @return true if {@code timestamp} is more recent than {@code timestamped}
518 * or {@code timestamped is null}
519 */
520 private static boolean isMoreRecent(Timestamp timestamp, Timestamped<?> timestamped) {
521 checkNotNull(timestamp);
522 if (timestamped == null) {
523 return true;
524 }
525 return timestamp.compareTo(timestamped.timestamp()) > 0;
526 }
527
528 private LinkEvent removeLinkInternal(LinkKey key, Timestamp timestamp) {
529 Map<ProviderId, Timestamped<LinkDescription>> linkDescriptions
530 = getOrCreateLinkDescriptions(key);
531
532 synchronized (linkDescriptions) {
533 if (linkDescriptions.isEmpty()) {
534 // never seen such link before. keeping timestamp for record
535 removedLinks.put(key, timestamp);
536 return null;
537 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700538 // accept removal request if given timestamp is newer than
539 // the latest Timestamp from Primary provider
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700540 Timestamped<LinkDescription> prim = getPrimaryDescription(linkDescriptions);
541 if (!isMoreRecent(timestamp, prim)) {
542 // outdated remove request, ignore
Madan Jampani2ff05592014-10-10 15:42:47 -0700543 return null;
544 }
545 removedLinks.put(key, timestamp);
Thomas Vachuska57126fe2014-11-11 17:13:24 -0800546 Link link = links.remove(key);
Madan Jampani2ff05592014-10-10 15:42:47 -0700547 linkDescriptions.clear();
548 if (link != null) {
549 srcLinks.remove(link.src().deviceId(), key);
550 dstLinks.remove(link.dst().deviceId(), key);
551 return new LinkEvent(LINK_REMOVED, link);
552 }
553 return null;
554 }
555 }
556
Yuta HIGUCHI800fac62014-12-11 19:23:01 -0800557 /**
558 * Creates concurrent readable, synchronized HashMultimap.
559 *
560 * @return SetMultimap
561 */
Madan Jampani2ff05592014-10-10 15:42:47 -0700562 private static <K, V> SetMultimap<K, V> createSynchronizedHashMultiMap() {
Yuta HIGUCHI800fac62014-12-11 19:23:01 -0800563 return synchronizedSetMultimap(
Sho SHIMIZU7a4087b2015-09-10 09:23:16 -0700564 Multimaps.newSetMultimap(new ConcurrentHashMap<>(),
Yuta HIGUCHI800fac62014-12-11 19:23:01 -0800565 () -> Sets.newConcurrentHashSet()));
Madan Jampani2ff05592014-10-10 15:42:47 -0700566 }
567
568 /**
569 * @return primary ProviderID, or randomly chosen one if none exists
570 */
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700571 private static ProviderId pickBaseProviderId(
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700572 Map<ProviderId, Timestamped<LinkDescription>> linkDescriptions) {
Madan Jampani2ff05592014-10-10 15:42:47 -0700573
574 ProviderId fallBackPrimary = null;
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700575 for (Entry<ProviderId, Timestamped<LinkDescription>> e : linkDescriptions.entrySet()) {
Madan Jampani2ff05592014-10-10 15:42:47 -0700576 if (!e.getKey().isAncillary()) {
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700577 // found primary
Madan Jampani2ff05592014-10-10 15:42:47 -0700578 return e.getKey();
579 } else if (fallBackPrimary == null) {
580 // pick randomly as a fallback in case there is no primary
581 fallBackPrimary = e.getKey();
582 }
583 }
584 return fallBackPrimary;
585 }
586
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700587 // Guarded by linkDescs value (=locking each Link)
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700588 private Link composeLink(Map<ProviderId, Timestamped<LinkDescription>> descs) {
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700589 ProviderId baseProviderId = pickBaseProviderId(descs);
590 Timestamped<LinkDescription> base = descs.get(baseProviderId);
Madan Jampani2ff05592014-10-10 15:42:47 -0700591
592 ConnectPoint src = base.value().src();
593 ConnectPoint dst = base.value().dst();
594 Type type = base.value().type();
595 DefaultAnnotations annotations = DefaultAnnotations.builder().build();
596 annotations = merge(annotations, base.value().annotations());
597
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700598 for (Entry<ProviderId, Timestamped<LinkDescription>> e : descs.entrySet()) {
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700599 if (baseProviderId.equals(e.getKey())) {
Madan Jampani2ff05592014-10-10 15:42:47 -0700600 continue;
601 }
602
Yuta HIGUCHI65934892014-12-04 17:47:44 -0800603 // Note: In the long run we should keep track of Description timestamp
Madan Jampani2ff05592014-10-10 15:42:47 -0700604 // and only merge conflicting keys when timestamp is newer
605 // Currently assuming there will never be a key conflict between
606 // providers
607
608 // annotation merging. not so efficient, should revisit later
609 annotations = merge(annotations, e.getValue().value().annotations());
610 }
611
Ray Milkeyb7f0f642016-01-22 16:08:14 -0800612 //boolean isDurable = Objects.equals(annotations.value(AnnotationKeys.DURABLE), "true");
613
614 // TEMP
615 Link.State initialLinkState = base.value().isExpected() ? ACTIVE : INACTIVE;
Ray Milkey2693bda2016-01-22 16:08:14 -0800616 return DefaultLink.builder()
617 .providerId(baseProviderId)
618 .src(src)
619 .dst(dst)
620 .type(type)
Ray Milkeyb7f0f642016-01-22 16:08:14 -0800621 .state(initialLinkState)
622 .isExpected(base.value().isExpected())
Ray Milkey2693bda2016-01-22 16:08:14 -0800623 .annotations(annotations)
624 .build();
Madan Jampani2ff05592014-10-10 15:42:47 -0700625 }
626
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700627 private Map<ProviderId, Timestamped<LinkDescription>> getOrCreateLinkDescriptions(LinkKey key) {
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700628 Map<ProviderId, Timestamped<LinkDescription>> r;
629 r = linkDescs.get(key);
630 if (r != null) {
631 return r;
632 }
633 r = new HashMap<>();
634 final Map<ProviderId, Timestamped<LinkDescription>> concurrentlyAdded;
635 concurrentlyAdded = linkDescs.putIfAbsent(key, r);
636 if (concurrentlyAdded != null) {
637 return concurrentlyAdded;
638 } else {
639 return r;
640 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700641 }
642
643 private final Function<LinkKey, Link> lookupLink = new LookupLink();
Thomas Vachuska57126fe2014-11-11 17:13:24 -0800644
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700645 /**
646 * Returns a Function to lookup Link instance using LinkKey from cache.
Thomas Vachuska57126fe2014-11-11 17:13:24 -0800647 *
648 * @return lookup link function
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700649 */
Madan Jampani2ff05592014-10-10 15:42:47 -0700650 private Function<LinkKey, Link> lookupLink() {
651 return lookupLink;
652 }
653
654 private final class LookupLink implements Function<LinkKey, Link> {
655 @Override
656 public Link apply(LinkKey input) {
Yuta HIGUCHI023295a2014-10-15 23:29:46 -0700657 if (input == null) {
658 return null;
659 } else {
660 return links.get(input);
661 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700662 }
663 }
664
Madan Jampani2ff05592014-10-10 15:42:47 -0700665 private void notifyDelegateIfNotNull(LinkEvent event) {
666 if (event != null) {
667 notifyDelegate(event);
668 }
669 }
670
Jonathan Hart7d656f42015-01-27 14:07:23 -0800671 private void broadcastMessage(MessageSubject subject, Object event) {
Madan Jampani2bfa94c2015-04-11 05:03:49 -0700672 clusterCommunicator.broadcast(event, subject, SERIALIZER::encode);
Madan Jampani2ff05592014-10-10 15:42:47 -0700673 }
674
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700675 private void unicastMessage(NodeId recipient, MessageSubject subject, Object event) throws IOException {
Madan Jampani2bfa94c2015-04-11 05:03:49 -0700676 clusterCommunicator.unicast(event, subject, SERIALIZER::encode, recipient);
Madan Jampania97e8202014-10-10 17:01:33 -0700677 }
678
Jonathan Hart7d656f42015-01-27 14:07:23 -0800679 private void notifyPeers(InternalLinkEvent event) {
Madan Jampani2ff05592014-10-10 15:42:47 -0700680 broadcastMessage(GossipLinkStoreMessageSubjects.LINK_UPDATE, event);
681 }
682
Jonathan Hart7d656f42015-01-27 14:07:23 -0800683 private void notifyPeers(InternalLinkRemovedEvent event) {
Madan Jampani2ff05592014-10-10 15:42:47 -0700684 broadcastMessage(GossipLinkStoreMessageSubjects.LINK_REMOVED, event);
685 }
686
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700687 // notify peer, silently ignoring error
Madan Jampania97e8202014-10-10 17:01:33 -0700688 private void notifyPeer(NodeId peer, InternalLinkEvent event) {
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700689 try {
690 unicastMessage(peer, GossipLinkStoreMessageSubjects.LINK_UPDATE, event);
691 } catch (IOException e) {
692 log.debug("Failed to notify peer {} with message {}", peer, event);
693 }
Madan Jampania97e8202014-10-10 17:01:33 -0700694 }
695
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700696 // notify peer, silently ignoring error
Madan Jampania97e8202014-10-10 17:01:33 -0700697 private void notifyPeer(NodeId peer, InternalLinkRemovedEvent event) {
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700698 try {
699 unicastMessage(peer, GossipLinkStoreMessageSubjects.LINK_REMOVED, event);
700 } catch (IOException e) {
701 log.debug("Failed to notify peer {} with message {}", peer, event);
702 }
Madan Jampania97e8202014-10-10 17:01:33 -0700703 }
704
705 private final class SendAdvertisementTask implements Runnable {
706
707 @Override
708 public void run() {
709 if (Thread.currentThread().isInterrupted()) {
Yuta HIGUCHI1a012722014-11-20 15:21:41 -0800710 log.debug("Interrupted, quitting");
Madan Jampania97e8202014-10-10 17:01:33 -0700711 return;
712 }
713
714 try {
715 final NodeId self = clusterService.getLocalNode().id();
716 Set<ControllerNode> nodes = clusterService.getNodes();
717
718 ImmutableList<NodeId> nodeIds = FluentIterable.from(nodes)
719 .transform(toNodeId())
720 .toList();
721
722 if (nodeIds.size() == 1 && nodeIds.get(0).equals(self)) {
Yuta HIGUCHIfaf9e1c2014-11-20 00:31:29 -0800723 log.trace("No other peers in the cluster.");
Madan Jampania97e8202014-10-10 17:01:33 -0700724 return;
725 }
726
727 NodeId peer;
728 do {
729 int idx = RandomUtils.nextInt(0, nodeIds.size());
730 peer = nodeIds.get(idx);
731 } while (peer.equals(self));
732
733 LinkAntiEntropyAdvertisement ad = createAdvertisement();
734
735 if (Thread.currentThread().isInterrupted()) {
Yuta HIGUCHI1a012722014-11-20 15:21:41 -0800736 log.debug("Interrupted, quitting");
Madan Jampania97e8202014-10-10 17:01:33 -0700737 return;
738 }
739
740 try {
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700741 unicastMessage(peer, LINK_ANTI_ENTROPY_ADVERTISEMENT, ad);
742 } catch (IOException e) {
743 log.debug("Failed to send anti-entropy advertisement to {}", peer);
Madan Jampania97e8202014-10-10 17:01:33 -0700744 return;
745 }
746 } catch (Exception e) {
747 // catch all Exception to avoid Scheduled task being suppressed.
748 log.error("Exception thrown while sending advertisement", e);
749 }
750 }
751 }
752
753 private LinkAntiEntropyAdvertisement createAdvertisement() {
754 final NodeId self = clusterService.getLocalNode().id();
755
756 Map<LinkFragmentId, Timestamp> linkTimestamps = new HashMap<>(linkDescs.size());
757 Map<LinkKey, Timestamp> linkTombstones = new HashMap<>(removedLinks.size());
758
Yuta HIGUCHIb6cfac32014-11-25 13:37:27 -0800759 linkDescs.forEach((linkKey, linkDesc) -> {
Madan Jampania97e8202014-10-10 17:01:33 -0700760 synchronized (linkDesc) {
761 for (Map.Entry<ProviderId, Timestamped<LinkDescription>> e : linkDesc.entrySet()) {
762 linkTimestamps.put(new LinkFragmentId(linkKey, e.getKey()), e.getValue().timestamp());
763 }
764 }
Yuta HIGUCHIb6cfac32014-11-25 13:37:27 -0800765 });
Madan Jampania97e8202014-10-10 17:01:33 -0700766
767 linkTombstones.putAll(removedLinks);
768
769 return new LinkAntiEntropyAdvertisement(self, linkTimestamps, linkTombstones);
770 }
771
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700772 private void handleAntiEntropyAdvertisement(LinkAntiEntropyAdvertisement ad) {
Madan Jampania97e8202014-10-10 17:01:33 -0700773
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700774 final NodeId sender = ad.sender();
775 boolean localOutdated = false;
Madan Jampania97e8202014-10-10 17:01:33 -0700776
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700777 for (Entry<LinkKey, Map<ProviderId, Timestamped<LinkDescription>>>
778 l : linkDescs.entrySet()) {
Madan Jampania97e8202014-10-10 17:01:33 -0700779
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700780 final LinkKey key = l.getKey();
781 final Map<ProviderId, Timestamped<LinkDescription>> link = l.getValue();
782 synchronized (link) {
783 Timestamp localLatest = removedLinks.get(key);
Madan Jampania97e8202014-10-10 17:01:33 -0700784
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700785 for (Entry<ProviderId, Timestamped<LinkDescription>> p : link.entrySet()) {
786 final ProviderId providerId = p.getKey();
787 final Timestamped<LinkDescription> pDesc = p.getValue();
Madan Jampania97e8202014-10-10 17:01:33 -0700788
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700789 final LinkFragmentId fragId = new LinkFragmentId(key, providerId);
790 // remote
791 Timestamp remoteTimestamp = ad.linkTimestamps().get(fragId);
792 if (remoteTimestamp == null) {
793 remoteTimestamp = ad.linkTombstones().get(key);
794 }
795 if (remoteTimestamp == null ||
Jonathan Hart403ea932015-02-20 16:23:00 -0800796 pDesc.isNewerThan(remoteTimestamp)) {
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700797 // I have more recent link description. update peer.
798 notifyPeer(sender, new InternalLinkEvent(providerId, pDesc));
799 } else {
800 final Timestamp remoteLive = ad.linkTimestamps().get(fragId);
801 if (remoteLive != null &&
802 remoteLive.compareTo(pDesc.timestamp()) > 0) {
803 // I have something outdated
804 localOutdated = true;
805 }
806 }
807
808 // search local latest along the way
809 if (localLatest == null ||
Jonathan Hart403ea932015-02-20 16:23:00 -0800810 pDesc.isNewerThan(localLatest)) {
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700811 localLatest = pDesc.timestamp();
812 }
813 }
814 // Tests if remote remove is more recent then local latest.
815 final Timestamp remoteRemove = ad.linkTombstones().get(key);
816 if (remoteRemove != null) {
817 if (localLatest != null &&
818 localLatest.compareTo(remoteRemove) < 0) {
819 // remote remove is more recent
820 notifyDelegateIfNotNull(removeLinkInternal(key, remoteRemove));
821 }
822 }
Madan Jampania97e8202014-10-10 17:01:33 -0700823 }
824 }
825
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700826 // populate remove info if not known locally
827 for (Entry<LinkKey, Timestamp> remoteRm : ad.linkTombstones().entrySet()) {
828 final LinkKey key = remoteRm.getKey();
829 final Timestamp remoteRemove = remoteRm.getValue();
830 // relying on removeLinkInternal to ignore stale info
831 notifyDelegateIfNotNull(removeLinkInternal(key, remoteRemove));
832 }
Madan Jampania97e8202014-10-10 17:01:33 -0700833
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700834 if (localOutdated) {
835 // send back advertisement to speed up convergence
836 try {
837 unicastMessage(sender, LINK_ANTI_ENTROPY_ADVERTISEMENT,
838 createAdvertisement());
839 } catch (IOException e) {
840 log.debug("Failed to send back active advertisement");
Madan Jampania97e8202014-10-10 17:01:33 -0700841 }
842 }
843 }
844
Yuta HIGUCHI80d56592014-11-25 15:11:13 -0800845 private final class InternalLinkEventListener
846 implements ClusterMessageHandler {
Madan Jampani2ff05592014-10-10 15:42:47 -0700847 @Override
848 public void handle(ClusterMessage message) {
849
Yuta HIGUCHIc01d2aa2014-10-19 01:19:34 -0700850 log.trace("Received link event from peer: {}", message.sender());
Sho SHIMIZU5eb79c52015-09-29 14:32:49 -0700851 InternalLinkEvent event = SERIALIZER.decode(message.payload());
Madan Jampani2ff05592014-10-10 15:42:47 -0700852
853 ProviderId providerId = event.providerId();
854 Timestamped<LinkDescription> linkDescription = event.linkDescription();
855
Madan Jampani2af244a2015-02-22 13:12:01 -0800856 try {
857 notifyDelegateIfNotNull(createOrUpdateLinkInternal(providerId, linkDescription));
858 } catch (Exception e) {
859 log.warn("Exception thrown handling link event", e);
860 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700861 }
862 }
863
Yuta HIGUCHI80d56592014-11-25 15:11:13 -0800864 private final class InternalLinkRemovedEventListener
865 implements ClusterMessageHandler {
Madan Jampani2ff05592014-10-10 15:42:47 -0700866 @Override
867 public void handle(ClusterMessage message) {
868
Yuta HIGUCHIc01d2aa2014-10-19 01:19:34 -0700869 log.trace("Received link removed event from peer: {}", message.sender());
Sho SHIMIZU5eb79c52015-09-29 14:32:49 -0700870 InternalLinkRemovedEvent event = SERIALIZER.decode(message.payload());
Madan Jampani2ff05592014-10-10 15:42:47 -0700871
872 LinkKey linkKey = event.linkKey();
873 Timestamp timestamp = event.timestamp();
874
Madan Jampani2af244a2015-02-22 13:12:01 -0800875 try {
876 notifyDelegateIfNotNull(removeLinkInternal(linkKey, timestamp));
877 } catch (Exception e) {
878 log.warn("Exception thrown handling link removed", e);
879 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700880 }
881 }
Madan Jampania97e8202014-10-10 17:01:33 -0700882
Yuta HIGUCHI80d56592014-11-25 15:11:13 -0800883 private final class InternalLinkAntiEntropyAdvertisementListener
884 implements ClusterMessageHandler {
Madan Jampania97e8202014-10-10 17:01:33 -0700885
886 @Override
887 public void handle(ClusterMessage message) {
Yuta HIGUCHIfaf9e1c2014-11-20 00:31:29 -0800888 log.trace("Received Link Anti-Entropy advertisement from peer: {}", message.sender());
Madan Jampania97e8202014-10-10 17:01:33 -0700889 LinkAntiEntropyAdvertisement advertisement = SERIALIZER.decode(message.payload());
Madan Jampani2af244a2015-02-22 13:12:01 -0800890 try {
891 handleAntiEntropyAdvertisement(advertisement);
892 } catch (Exception e) {
893 log.warn("Exception thrown while handling Link advertisements", e);
894 throw e;
895 }
Madan Jampania97e8202014-10-10 17:01:33 -0700896 }
897 }
Marc De Leenheerb473b9d2015-02-06 15:21:03 -0800898
899 private final class LinkInjectedEventListener
900 implements ClusterMessageHandler {
901 @Override
902 public void handle(ClusterMessage message) {
903
904 log.trace("Received injected link event from peer: {}", message.sender());
905 LinkInjectedEvent linkInjectedEvent = SERIALIZER.decode(message.payload());
906
907 ProviderId providerId = linkInjectedEvent.providerId();
908 LinkDescription linkDescription = linkInjectedEvent.linkDescription();
909
HIGUCHI Yuta0b4d2982015-02-27 23:18:52 -0800910 final DeviceId deviceId = linkDescription.dst().deviceId();
911 if (!deviceClockService.isTimestampAvailable(deviceId)) {
912 // workaround for ONOS-1208
913 log.warn("Not ready to accept update. Dropping {}", linkDescription);
914 return;
915 }
916
Madan Jampani2af244a2015-02-22 13:12:01 -0800917 try {
918 createOrUpdateLink(providerId, linkDescription);
919 } catch (Exception e) {
920 log.warn("Exception thrown while handling link injected event", e);
921 }
Marc De Leenheerb473b9d2015-02-06 15:21:03 -0800922 }
923 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700924}