blob: cf46b15fb10aa57ad987823d7e5719fbd7a2b320 [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
Jonathan Hart7d656f42015-01-27 14:07:23 -080018import com.google.common.base.Function;
19import com.google.common.collect.FluentIterable;
20import com.google.common.collect.ImmutableList;
21import com.google.common.collect.Multimaps;
22import com.google.common.collect.SetMultimap;
23import com.google.common.collect.Sets;
Madan Jampania97e8202014-10-10 17:01:33 -070024import org.apache.commons.lang3.RandomUtils;
Madan Jampani2ff05592014-10-10 15:42:47 -070025import org.apache.felix.scr.annotations.Activate;
Madan Jampani2ff05592014-10-10 15:42:47 -070026import org.apache.felix.scr.annotations.Deactivate;
27import org.apache.felix.scr.annotations.Reference;
28import org.apache.felix.scr.annotations.ReferenceCardinality;
29import org.apache.felix.scr.annotations.Service;
Ray Milkey7bbeb3f2014-12-11 14:59:26 -080030import org.onlab.util.KryoNamespace;
Brian O'Connorabafb502014-12-02 22:26:20 -080031import org.onosproject.cluster.ClusterService;
32import org.onosproject.cluster.ControllerNode;
33import org.onosproject.cluster.NodeId;
Marc De Leenheerb473b9d2015-02-06 15:21:03 -080034import org.onosproject.mastership.MastershipService;
Brian O'Connorabafb502014-12-02 22:26:20 -080035import org.onosproject.net.AnnotationKeys;
36import org.onosproject.net.AnnotationsUtil;
37import org.onosproject.net.ConnectPoint;
38import org.onosproject.net.DefaultAnnotations;
39import org.onosproject.net.DefaultLink;
40import org.onosproject.net.DeviceId;
41import org.onosproject.net.Link;
42import org.onosproject.net.Link.Type;
43import org.onosproject.net.LinkKey;
44import org.onosproject.net.SparseAnnotations;
45import org.onosproject.net.device.DeviceClockService;
46import org.onosproject.net.link.DefaultLinkDescription;
47import org.onosproject.net.link.LinkDescription;
48import org.onosproject.net.link.LinkEvent;
49import org.onosproject.net.link.LinkStore;
50import org.onosproject.net.link.LinkStoreDelegate;
51import org.onosproject.net.provider.ProviderId;
52import org.onosproject.store.AbstractStore;
53import org.onosproject.store.Timestamp;
54import org.onosproject.store.cluster.messaging.ClusterCommunicationService;
55import org.onosproject.store.cluster.messaging.ClusterMessage;
56import org.onosproject.store.cluster.messaging.ClusterMessageHandler;
57import org.onosproject.store.cluster.messaging.MessageSubject;
58import org.onosproject.store.impl.Timestamped;
59import org.onosproject.store.serializers.KryoSerializer;
Brian O'Connor6de2e202015-05-21 14:30:41 -070060import org.onosproject.store.serializers.custom.DistributedStoreSerializers;
Madan Jampani2ff05592014-10-10 15:42:47 -070061import org.slf4j.Logger;
62
Jonathan Hart7d656f42015-01-27 14:07:23 -080063import java.io.IOException;
Jonathan Hart7d656f42015-01-27 14:07:23 -080064import java.util.Collections;
65import java.util.HashMap;
66import java.util.HashSet;
67import java.util.Map;
68import java.util.Map.Entry;
69import java.util.Objects;
70import java.util.Set;
71import java.util.concurrent.ConcurrentHashMap;
72import java.util.concurrent.ConcurrentMap;
73import java.util.concurrent.ExecutorService;
74import java.util.concurrent.Executors;
75import java.util.concurrent.ScheduledExecutorService;
76import java.util.concurrent.TimeUnit;
Madan Jampani2ff05592014-10-10 15:42:47 -070077
Thomas Vachuska57126fe2014-11-11 17:13:24 -080078import static com.google.common.base.Preconditions.checkNotNull;
79import static com.google.common.base.Predicates.notNull;
80import static com.google.common.collect.Multimaps.synchronizedSetMultimap;
Madan Jampania97e8202014-10-10 17:01:33 -070081import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
Thomas Vachuska6f94ded2015-02-21 14:02:38 -080082import static org.onlab.util.Tools.groupedThreads;
Ray Milkey7bbeb3f2014-12-11 14:59:26 -080083import static org.onlab.util.Tools.minPriority;
Brian O'Connorabafb502014-12-02 22:26:20 -080084import static org.onosproject.cluster.ControllerNodeToNodeId.toNodeId;
85import static org.onosproject.net.DefaultAnnotations.merge;
86import static org.onosproject.net.DefaultAnnotations.union;
87import static org.onosproject.net.Link.State.ACTIVE;
88import static org.onosproject.net.Link.State.INACTIVE;
89import static org.onosproject.net.Link.Type.DIRECT;
90import static org.onosproject.net.Link.Type.INDIRECT;
91import static org.onosproject.net.LinkKey.linkKey;
Jian Li11599162016-01-15 15:46:16 -080092import static org.onosproject.net.link.LinkEvent.Type.LINK_ADDED;
93import static org.onosproject.net.link.LinkEvent.Type.LINK_REMOVED;
94import static org.onosproject.net.link.LinkEvent.Type.LINK_UPDATED;
Brian O'Connorabafb502014-12-02 22:26:20 -080095import static org.onosproject.store.link.impl.GossipLinkStoreMessageSubjects.LINK_ANTI_ENTROPY_ADVERTISEMENT;
Madan Jampani2ff05592014-10-10 15:42:47 -070096import static org.slf4j.LoggerFactory.getLogger;
Madan Jampani2ff05592014-10-10 15:42:47 -070097
98/**
99 * Manages inventory of infrastructure links in distributed data store
100 * that uses optimistic replication and gossip based techniques.
101 */
Jian Li11599162016-01-15 15:46:16 -0800102//@Component(immediate = true, enabled = false)
Madan Jampani2ff05592014-10-10 15:42:47 -0700103@Service
104public class GossipLinkStore
105 extends AbstractStore<LinkEvent, LinkStoreDelegate>
106 implements LinkStore {
107
Marc De Leenheerb473b9d2015-02-06 15:21:03 -0800108 // Timeout in milliseconds to process links on remote master node
109 private static final int REMOTE_MASTER_TIMEOUT = 1000;
110
Madan Jampani2ff05592014-10-10 15:42:47 -0700111 private final Logger log = getLogger(getClass());
112
113 // Link inventory
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700114 private final ConcurrentMap<LinkKey, Map<ProviderId, Timestamped<LinkDescription>>> linkDescs =
Madan Jampani2ff05592014-10-10 15:42:47 -0700115 new ConcurrentHashMap<>();
116
117 // Link instance cache
118 private final ConcurrentMap<LinkKey, Link> links = new ConcurrentHashMap<>();
119
120 // Egress and ingress link sets
121 private final SetMultimap<DeviceId, LinkKey> srcLinks = createSynchronizedHashMultiMap();
122 private final SetMultimap<DeviceId, LinkKey> dstLinks = createSynchronizedHashMultiMap();
123
124 // Remove links
Yuta HIGUCHIb9125562014-12-01 23:28:22 -0800125 private final Map<LinkKey, Timestamp> removedLinks = new ConcurrentHashMap<>();
Madan Jampani2ff05592014-10-10 15:42:47 -0700126
127 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Yuta HIGUCHI093e83e2014-10-10 22:26:11 -0700128 protected DeviceClockService deviceClockService;
Madan Jampani2ff05592014-10-10 15:42:47 -0700129
130 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
131 protected ClusterCommunicationService clusterCommunicator;
132
133 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
134 protected ClusterService clusterService;
135
Marc De Leenheerb473b9d2015-02-06 15:21:03 -0800136 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
137 protected MastershipService mastershipService;
138
Yuta HIGUCHI3e5d11a2014-11-04 14:16:44 -0800139 protected static final KryoSerializer SERIALIZER = new KryoSerializer() {
Madan Jampani2ff05592014-10-10 15:42:47 -0700140 @Override
141 protected void setupKryoPool() {
Yuta HIGUCHI8d143d22014-10-19 23:15:09 -0700142 serializerPool = KryoNamespace.newBuilder()
Yuta HIGUCHI91768e32014-11-22 05:06:35 -0800143 .register(DistributedStoreSerializers.STORE_COMMON)
144 .nextId(DistributedStoreSerializers.STORE_CUSTOM_BEGIN)
Madan Jampani2ff05592014-10-10 15:42:47 -0700145 .register(InternalLinkEvent.class)
146 .register(InternalLinkRemovedEvent.class)
Madan Jampanid2054d42014-10-10 17:27:06 -0700147 .register(LinkAntiEntropyAdvertisement.class)
148 .register(LinkFragmentId.class)
Marc De Leenheerb473b9d2015-02-06 15:21:03 -0800149 .register(LinkInjectedEvent.class)
Yuta HIGUCHI91768e32014-11-22 05:06:35 -0800150 .build();
Madan Jampani2ff05592014-10-10 15:42:47 -0700151 }
152 };
153
Yuta HIGUCHI80d56592014-11-25 15:11:13 -0800154 private ExecutorService executor;
155
Yuta HIGUCHI06586272014-11-25 14:27:03 -0800156 private ScheduledExecutorService backgroundExecutors;
Madan Jampania97e8202014-10-10 17:01:33 -0700157
Madan Jampani2ff05592014-10-10 15:42:47 -0700158 @Activate
159 public void activate() {
160
Thomas Vachuska6f94ded2015-02-21 14:02:38 -0800161 executor = Executors.newCachedThreadPool(groupedThreads("onos/link", "fg-%d"));
Yuta HIGUCHI80d56592014-11-25 15:11:13 -0800162
Yuta HIGUCHI06586272014-11-25 14:27:03 -0800163 backgroundExecutors =
Thomas Vachuska6f94ded2015-02-21 14:02:38 -0800164 newSingleThreadScheduledExecutor(minPriority(groupedThreads("onos/link", "bg-%d")));
Madan Jampania97e8202014-10-10 17:01:33 -0700165
Madan Jampani2af244a2015-02-22 13:12:01 -0800166 clusterCommunicator.addSubscriber(
167 GossipLinkStoreMessageSubjects.LINK_UPDATE,
168 new InternalLinkEventListener(), executor);
169 clusterCommunicator.addSubscriber(
170 GossipLinkStoreMessageSubjects.LINK_REMOVED,
171 new InternalLinkRemovedEventListener(), executor);
172 clusterCommunicator.addSubscriber(
173 GossipLinkStoreMessageSubjects.LINK_ANTI_ENTROPY_ADVERTISEMENT,
174 new InternalLinkAntiEntropyAdvertisementListener(), backgroundExecutors);
175 clusterCommunicator.addSubscriber(
176 GossipLinkStoreMessageSubjects.LINK_INJECTED,
177 new LinkInjectedEventListener(), executor);
178
Madan Jampania97e8202014-10-10 17:01:33 -0700179 long initialDelaySec = 5;
180 long periodSec = 5;
181 // start anti-entropy thread
Yuta HIGUCHI06586272014-11-25 14:27:03 -0800182 backgroundExecutors.scheduleAtFixedRate(new SendAdvertisementTask(),
Madan Jampania97e8202014-10-10 17:01:33 -0700183 initialDelaySec, periodSec, TimeUnit.SECONDS);
Madan Jampani2ff05592014-10-10 15:42:47 -0700184
185 log.info("Started");
186 }
187
188 @Deactivate
189 public void deactivate() {
Madan Jampani3ffbb272014-10-13 11:19:37 -0700190
Yuta HIGUCHI80d56592014-11-25 15:11:13 -0800191 executor.shutdownNow();
192
Yuta HIGUCHI06586272014-11-25 14:27:03 -0800193 backgroundExecutors.shutdownNow();
Madan Jampani3ffbb272014-10-13 11:19:37 -0700194 try {
Yuta HIGUCHI06586272014-11-25 14:27:03 -0800195 if (!backgroundExecutors.awaitTermination(5, TimeUnit.SECONDS)) {
Madan Jampani3ffbb272014-10-13 11:19:37 -0700196 log.error("Timeout during executor shutdown");
197 }
198 } catch (InterruptedException e) {
199 log.error("Error during executor shutdown", e);
200 }
201
Madan Jampani2ff05592014-10-10 15:42:47 -0700202 linkDescs.clear();
203 links.clear();
204 srcLinks.clear();
205 dstLinks.clear();
206 log.info("Stopped");
207 }
208
209 @Override
210 public int getLinkCount() {
211 return links.size();
212 }
213
214 @Override
215 public Iterable<Link> getLinks() {
216 return Collections.unmodifiableCollection(links.values());
217 }
218
219 @Override
220 public Set<Link> getDeviceEgressLinks(DeviceId deviceId) {
221 // lock for iteration
222 synchronized (srcLinks) {
223 return FluentIterable.from(srcLinks.get(deviceId))
224 .transform(lookupLink())
225 .filter(notNull())
226 .toSet();
227 }
228 }
229
230 @Override
231 public Set<Link> getDeviceIngressLinks(DeviceId deviceId) {
232 // lock for iteration
233 synchronized (dstLinks) {
234 return FluentIterable.from(dstLinks.get(deviceId))
235 .transform(lookupLink())
236 .filter(notNull())
237 .toSet();
238 }
239 }
240
241 @Override
242 public Link getLink(ConnectPoint src, ConnectPoint dst) {
Yuta HIGUCHI18ab8a92014-10-13 11:16:19 -0700243 return links.get(linkKey(src, dst));
Madan Jampani2ff05592014-10-10 15:42:47 -0700244 }
245
246 @Override
247 public Set<Link> getEgressLinks(ConnectPoint src) {
248 Set<Link> egress = new HashSet<>();
HIGUCHI Yuta4d973eb2015-02-26 14:28:49 -0800249 //
250 // Change `srcLinks` to ConcurrentMap<DeviceId, (Concurrent)Set>
251 // to remove this synchronized block, if we hit performance issue.
252 // SetMultiMap#get returns wrapped collection to provide modifiable-view.
253 // And the wrapped collection is not concurrent access safe.
254 //
255 // Our use case here does not require returned collection to be modifiable,
256 // so the wrapped collection forces us to lock the whole multiset,
257 // for benefit we don't need.
258 //
259 // Same applies to `dstLinks`
260 synchronized (srcLinks) {
261 for (LinkKey linkKey : srcLinks.get(src.deviceId())) {
262 if (linkKey.src().equals(src)) {
263 Link link = links.get(linkKey);
264 if (link != null) {
265 egress.add(link);
266 } else {
267 log.debug("Egress link for {} was null, skipped", linkKey);
268 }
Ray Milkey7bbeb3f2014-12-11 14:59:26 -0800269 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700270 }
271 }
272 return egress;
273 }
274
275 @Override
276 public Set<Link> getIngressLinks(ConnectPoint dst) {
277 Set<Link> ingress = new HashSet<>();
HIGUCHI Yuta4d973eb2015-02-26 14:28:49 -0800278 synchronized (dstLinks) {
279 for (LinkKey linkKey : dstLinks.get(dst.deviceId())) {
280 if (linkKey.dst().equals(dst)) {
281 Link link = links.get(linkKey);
282 if (link != null) {
283 ingress.add(link);
284 } else {
285 log.debug("Ingress link for {} was null, skipped", linkKey);
286 }
Ray Milkey7bbeb3f2014-12-11 14:59:26 -0800287 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700288 }
289 }
290 return ingress;
291 }
292
293 @Override
294 public LinkEvent createOrUpdateLink(ProviderId providerId,
295 LinkDescription linkDescription) {
296
Marc De Leenheerb473b9d2015-02-06 15:21:03 -0800297 final DeviceId dstDeviceId = linkDescription.dst().deviceId();
298 final NodeId localNode = clusterService.getLocalNode().id();
299 final NodeId dstNode = mastershipService.getMasterFor(dstDeviceId);
Madan Jampani2ff05592014-10-10 15:42:47 -0700300
Marc De Leenheerb473b9d2015-02-06 15:21:03 -0800301 // Process link update only if we're the master of the destination node,
302 // otherwise signal the actual master.
303 LinkEvent linkEvent = null;
304 if (localNode.equals(dstNode)) {
Madan Jampani2ff05592014-10-10 15:42:47 -0700305
Marc De Leenheerb473b9d2015-02-06 15:21:03 -0800306 Timestamp newTimestamp = deviceClockService.getTimestamp(dstDeviceId);
307
308 final Timestamped<LinkDescription> deltaDesc = new Timestamped<>(linkDescription, newTimestamp);
309
310 LinkKey key = linkKey(linkDescription.src(), linkDescription.dst());
311 final Timestamped<LinkDescription> mergedDesc;
312 Map<ProviderId, Timestamped<LinkDescription>> map = getOrCreateLinkDescriptions(key);
313
314 synchronized (map) {
315 linkEvent = createOrUpdateLinkInternal(providerId, deltaDesc);
316 mergedDesc = map.get(providerId);
317 }
318
319 if (linkEvent != null) {
Madan Jampanif2af7712015-05-29 18:43:52 -0700320 log.debug("Notifying peers of a link update topology event from providerId: "
Marc De Leenheerb473b9d2015-02-06 15:21:03 -0800321 + "{} between src: {} and dst: {}",
322 providerId, linkDescription.src(), linkDescription.dst());
323 notifyPeers(new InternalLinkEvent(providerId, mergedDesc));
324 }
325
326 } else {
HIGUCHI Yuta1979f552015-12-28 21:24:26 -0800327 // Only forward for ConfigProvider
328 // Forwarding was added as a workaround for ONOS-490
HIGUCHI Yuta4ea4e422016-01-13 16:40:34 -0800329 if (!providerId.scheme().equals("cfg")) {
HIGUCHI Yuta1979f552015-12-28 21:24:26 -0800330 return null;
331 }
HIGUCHI Yutadc2e7c22015-02-24 12:19:47 -0800332 // FIXME Temporary hack for NPE (ONOS-1171).
333 // Proper fix is to implement forwarding to master on ConfigProvider
334 // redo ONOS-490
335 if (dstNode == null) {
336 // silently ignore
337 return null;
338 }
339
Marc De Leenheerb473b9d2015-02-06 15:21:03 -0800340
341 LinkInjectedEvent linkInjectedEvent = new LinkInjectedEvent(providerId, linkDescription);
Marc De Leenheerb473b9d2015-02-06 15:21:03 -0800342
Brian O'Connor5eb77c82015-03-02 18:09:39 -0800343 // TODO check unicast return value
Madan Jampani2bfa94c2015-04-11 05:03:49 -0700344 clusterCommunicator.unicast(linkInjectedEvent,
345 GossipLinkStoreMessageSubjects.LINK_INJECTED,
346 SERIALIZER::encode,
347 dstNode);
Yuta HIGUCHI92cd51e2014-10-13 10:51:45 -0700348 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700349
Marc De Leenheerb473b9d2015-02-06 15:21:03 -0800350 return linkEvent;
Madan Jampani2ff05592014-10-10 15:42:47 -0700351 }
352
Thomas Vachuska57126fe2014-11-11 17:13:24 -0800353 @Override
354 public LinkEvent removeOrDownLink(ConnectPoint src, ConnectPoint dst) {
355 Link link = getLink(src, dst);
356 if (link == null) {
357 return null;
358 }
359
360 if (link.isDurable()) {
361 // FIXME: this is not the right thing to call for the gossip store; will not sync link state!!!
362 return link.state() == INACTIVE ? null :
363 updateLink(linkKey(link.src(), link.dst()), link,
Ray Milkey2693bda2016-01-22 16:08:14 -0800364 DefaultLink.builder()
365 .providerId(link.providerId())
366 .src(link.src())
367 .dst(link.dst())
368 .type(link.type())
369 .state(INACTIVE)
370 .isExpected(link.isExpected())
371 .annotations(link.annotations())
372 .build());
Thomas Vachuska57126fe2014-11-11 17:13:24 -0800373 }
374 return removeLink(src, dst);
375 }
376
Madan Jampani2ff05592014-10-10 15:42:47 -0700377 private LinkEvent createOrUpdateLinkInternal(
378 ProviderId providerId,
379 Timestamped<LinkDescription> linkDescription) {
380
Yuta HIGUCHIa85542b2014-10-21 19:29:49 -0700381 final LinkKey key = linkKey(linkDescription.value().src(),
Marc De Leenheerb473b9d2015-02-06 15:21:03 -0800382 linkDescription.value().dst());
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700383 Map<ProviderId, Timestamped<LinkDescription>> descs = getOrCreateLinkDescriptions(key);
Madan Jampani2ff05592014-10-10 15:42:47 -0700384
385 synchronized (descs) {
386 // if the link was previously removed, we should proceed if and
387 // only if this request is more recent.
388 Timestamp linkRemovedTimestamp = removedLinks.get(key);
389 if (linkRemovedTimestamp != null) {
Jonathan Hart403ea932015-02-20 16:23:00 -0800390 if (linkDescription.isNewerThan(linkRemovedTimestamp)) {
Madan Jampani2ff05592014-10-10 15:42:47 -0700391 removedLinks.remove(key);
392 } else {
Yuta HIGUCHIa85542b2014-10-21 19:29:49 -0700393 log.trace("Link {} was already removed ignoring.", key);
Madan Jampani2ff05592014-10-10 15:42:47 -0700394 return null;
395 }
396 }
397
398 final Link oldLink = links.get(key);
399 // update description
400 createOrUpdateLinkDescription(descs, providerId, linkDescription);
401 final Link newLink = composeLink(descs);
402 if (oldLink == null) {
403 return createLink(key, newLink);
404 }
405 return updateLink(key, oldLink, newLink);
406 }
407 }
408
409 // Guarded by linkDescs value (=locking each Link)
410 private Timestamped<LinkDescription> createOrUpdateLinkDescription(
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700411 Map<ProviderId, Timestamped<LinkDescription>> descs,
Madan Jampani2ff05592014-10-10 15:42:47 -0700412 ProviderId providerId,
413 Timestamped<LinkDescription> linkDescription) {
414
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700415 // merge existing annotations
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700416 Timestamped<LinkDescription> existingLinkDescription = descs.get(providerId);
Madan Jampani2ff05592014-10-10 15:42:47 -0700417 if (existingLinkDescription != null && existingLinkDescription.isNewer(linkDescription)) {
Yuta HIGUCHIa85542b2014-10-21 19:29:49 -0700418 log.trace("local info is more up-to-date, ignoring {}.", linkDescription);
Madan Jampani2ff05592014-10-10 15:42:47 -0700419 return null;
420 }
421 Timestamped<LinkDescription> newLinkDescription = linkDescription;
422 if (existingLinkDescription != null) {
Yuta HIGUCHIa85542b2014-10-21 19:29:49 -0700423 // we only allow transition from INDIRECT -> DIRECT
424 final Type newType;
425 if (existingLinkDescription.value().type() == DIRECT) {
426 newType = DIRECT;
427 } else {
428 newType = linkDescription.value().type();
429 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700430 SparseAnnotations merged = union(existingLinkDescription.value().annotations(),
431 linkDescription.value().annotations());
Thomas Vachuska57126fe2014-11-11 17:13:24 -0800432 newLinkDescription = new Timestamped<>(
Madan Jampani2ff05592014-10-10 15:42:47 -0700433 new DefaultLinkDescription(
434 linkDescription.value().src(),
435 linkDescription.value().dst(),
Yuta HIGUCHIa85542b2014-10-21 19:29:49 -0700436 newType, merged),
Madan Jampani2ff05592014-10-10 15:42:47 -0700437 linkDescription.timestamp());
438 }
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700439 return descs.put(providerId, newLinkDescription);
Madan Jampani2ff05592014-10-10 15:42:47 -0700440 }
441
442 // Creates and stores the link and returns the appropriate event.
443 // Guarded by linkDescs value (=locking each Link)
444 private LinkEvent createLink(LinkKey key, Link newLink) {
Madan Jampani2ff05592014-10-10 15:42:47 -0700445 links.put(key, newLink);
446 srcLinks.put(newLink.src().deviceId(), key);
447 dstLinks.put(newLink.dst().deviceId(), key);
448 return new LinkEvent(LINK_ADDED, newLink);
449 }
450
451 // Updates, if necessary the specified link and returns the appropriate event.
452 // Guarded by linkDescs value (=locking each Link)
453 private LinkEvent updateLink(LinkKey key, Link oldLink, Link newLink) {
Yuta HIGUCHIa85542b2014-10-21 19:29:49 -0700454 // Note: INDIRECT -> DIRECT transition only
455 // so that BDDP discovered Link will not overwrite LDDP Link
Thomas Vachuska57126fe2014-11-11 17:13:24 -0800456 if (oldLink.state() != newLink.state() ||
457 (oldLink.type() == INDIRECT && newLink.type() == DIRECT) ||
Madan Jampani2ff05592014-10-10 15:42:47 -0700458 !AnnotationsUtil.isEqual(oldLink.annotations(), newLink.annotations())) {
459
460 links.put(key, newLink);
Marc De Leenheerb473b9d2015-02-06 15:21:03 -0800461 // strictly speaking following can be omitted
Madan Jampani2ff05592014-10-10 15:42:47 -0700462 srcLinks.put(oldLink.src().deviceId(), key);
463 dstLinks.put(oldLink.dst().deviceId(), key);
464 return new LinkEvent(LINK_UPDATED, newLink);
465 }
466 return null;
467 }
468
469 @Override
470 public LinkEvent removeLink(ConnectPoint src, ConnectPoint dst) {
Yuta HIGUCHI18ab8a92014-10-13 11:16:19 -0700471 final LinkKey key = linkKey(src, dst);
Madan Jampani2ff05592014-10-10 15:42:47 -0700472
473 DeviceId dstDeviceId = dst.deviceId();
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700474 Timestamp timestamp = null;
475 try {
476 timestamp = deviceClockService.getTimestamp(dstDeviceId);
477 } catch (IllegalStateException e) {
Thomas Vachuska6f90c592015-06-03 20:06:58 -0700478 log.debug("Failed to remove link {}, was not the master", key);
479 // there are times when this is called before mastership
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700480 // handoff correctly completes.
481 return null;
482 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700483
484 LinkEvent event = removeLinkInternal(key, timestamp);
485
486 if (event != null) {
Madan Jampanif2af7712015-05-29 18:43:52 -0700487 log.debug("Notifying peers of a link removed topology event for a link "
Madan Jampani2ff05592014-10-10 15:42:47 -0700488 + "between src: {} and dst: {}", src, dst);
Jonathan Hart7d656f42015-01-27 14:07:23 -0800489 notifyPeers(new InternalLinkRemovedEvent(key, timestamp));
Madan Jampani2ff05592014-10-10 15:42:47 -0700490 }
491 return event;
492 }
493
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700494 private static Timestamped<LinkDescription> getPrimaryDescription(
495 Map<ProviderId, Timestamped<LinkDescription>> linkDescriptions) {
496
Madan Jampani2ff05592014-10-10 15:42:47 -0700497 synchronized (linkDescriptions) {
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700498 for (Entry<ProviderId, Timestamped<LinkDescription>>
499 e : linkDescriptions.entrySet()) {
500
501 if (!e.getKey().isAncillary()) {
502 return e.getValue();
503 }
504 }
505 }
506 return null;
507 }
508
509
510 // TODO: consider slicing out as Timestamp utils
511 /**
512 * Checks is timestamp is more recent than timestamped object.
513 *
514 * @param timestamp to check if this is more recent then other
515 * @param timestamped object to be tested against
516 * @return true if {@code timestamp} is more recent than {@code timestamped}
517 * or {@code timestamped is null}
518 */
519 private static boolean isMoreRecent(Timestamp timestamp, Timestamped<?> timestamped) {
520 checkNotNull(timestamp);
521 if (timestamped == null) {
522 return true;
523 }
524 return timestamp.compareTo(timestamped.timestamp()) > 0;
525 }
526
527 private LinkEvent removeLinkInternal(LinkKey key, Timestamp timestamp) {
528 Map<ProviderId, Timestamped<LinkDescription>> linkDescriptions
529 = getOrCreateLinkDescriptions(key);
530
531 synchronized (linkDescriptions) {
532 if (linkDescriptions.isEmpty()) {
533 // never seen such link before. keeping timestamp for record
534 removedLinks.put(key, timestamp);
535 return null;
536 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700537 // accept removal request if given timestamp is newer than
538 // the latest Timestamp from Primary provider
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700539 Timestamped<LinkDescription> prim = getPrimaryDescription(linkDescriptions);
540 if (!isMoreRecent(timestamp, prim)) {
541 // outdated remove request, ignore
Madan Jampani2ff05592014-10-10 15:42:47 -0700542 return null;
543 }
544 removedLinks.put(key, timestamp);
Thomas Vachuska57126fe2014-11-11 17:13:24 -0800545 Link link = links.remove(key);
Madan Jampani2ff05592014-10-10 15:42:47 -0700546 linkDescriptions.clear();
547 if (link != null) {
548 srcLinks.remove(link.src().deviceId(), key);
549 dstLinks.remove(link.dst().deviceId(), key);
550 return new LinkEvent(LINK_REMOVED, link);
551 }
552 return null;
553 }
554 }
555
Yuta HIGUCHI800fac62014-12-11 19:23:01 -0800556 /**
557 * Creates concurrent readable, synchronized HashMultimap.
558 *
559 * @return SetMultimap
560 */
Madan Jampani2ff05592014-10-10 15:42:47 -0700561 private static <K, V> SetMultimap<K, V> createSynchronizedHashMultiMap() {
Yuta HIGUCHI800fac62014-12-11 19:23:01 -0800562 return synchronizedSetMultimap(
Sho SHIMIZU7a4087b2015-09-10 09:23:16 -0700563 Multimaps.newSetMultimap(new ConcurrentHashMap<>(),
Yuta HIGUCHI800fac62014-12-11 19:23:01 -0800564 () -> Sets.newConcurrentHashSet()));
Madan Jampani2ff05592014-10-10 15:42:47 -0700565 }
566
567 /**
568 * @return primary ProviderID, or randomly chosen one if none exists
569 */
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700570 private static ProviderId pickBaseProviderId(
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700571 Map<ProviderId, Timestamped<LinkDescription>> linkDescriptions) {
Madan Jampani2ff05592014-10-10 15:42:47 -0700572
573 ProviderId fallBackPrimary = null;
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700574 for (Entry<ProviderId, Timestamped<LinkDescription>> e : linkDescriptions.entrySet()) {
Madan Jampani2ff05592014-10-10 15:42:47 -0700575 if (!e.getKey().isAncillary()) {
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700576 // found primary
Madan Jampani2ff05592014-10-10 15:42:47 -0700577 return e.getKey();
578 } else if (fallBackPrimary == null) {
579 // pick randomly as a fallback in case there is no primary
580 fallBackPrimary = e.getKey();
581 }
582 }
583 return fallBackPrimary;
584 }
585
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700586 // Guarded by linkDescs value (=locking each Link)
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700587 private Link composeLink(Map<ProviderId, Timestamped<LinkDescription>> descs) {
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700588 ProviderId baseProviderId = pickBaseProviderId(descs);
589 Timestamped<LinkDescription> base = descs.get(baseProviderId);
Madan Jampani2ff05592014-10-10 15:42:47 -0700590
591 ConnectPoint src = base.value().src();
592 ConnectPoint dst = base.value().dst();
593 Type type = base.value().type();
594 DefaultAnnotations annotations = DefaultAnnotations.builder().build();
595 annotations = merge(annotations, base.value().annotations());
596
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700597 for (Entry<ProviderId, Timestamped<LinkDescription>> e : descs.entrySet()) {
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700598 if (baseProviderId.equals(e.getKey())) {
Madan Jampani2ff05592014-10-10 15:42:47 -0700599 continue;
600 }
601
Yuta HIGUCHI65934892014-12-04 17:47:44 -0800602 // Note: In the long run we should keep track of Description timestamp
Madan Jampani2ff05592014-10-10 15:42:47 -0700603 // and only merge conflicting keys when timestamp is newer
604 // Currently assuming there will never be a key conflict between
605 // providers
606
607 // annotation merging. not so efficient, should revisit later
608 annotations = merge(annotations, e.getValue().value().annotations());
609 }
610
Thomas Vachuska57126fe2014-11-11 17:13:24 -0800611 boolean isDurable = Objects.equals(annotations.value(AnnotationKeys.DURABLE), "true");
Ray Milkey2693bda2016-01-22 16:08:14 -0800612 return DefaultLink.builder()
613 .providerId(baseProviderId)
614 .src(src)
615 .dst(dst)
616 .type(type)
617 .state(ACTIVE)
618 .isExpected(isDurable)
619 .annotations(annotations)
620 .build();
Madan Jampani2ff05592014-10-10 15:42:47 -0700621 }
622
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700623 private Map<ProviderId, Timestamped<LinkDescription>> getOrCreateLinkDescriptions(LinkKey key) {
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700624 Map<ProviderId, Timestamped<LinkDescription>> r;
625 r = linkDescs.get(key);
626 if (r != null) {
627 return r;
628 }
629 r = new HashMap<>();
630 final Map<ProviderId, Timestamped<LinkDescription>> concurrentlyAdded;
631 concurrentlyAdded = linkDescs.putIfAbsent(key, r);
632 if (concurrentlyAdded != null) {
633 return concurrentlyAdded;
634 } else {
635 return r;
636 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700637 }
638
639 private final Function<LinkKey, Link> lookupLink = new LookupLink();
Thomas Vachuska57126fe2014-11-11 17:13:24 -0800640
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700641 /**
642 * Returns a Function to lookup Link instance using LinkKey from cache.
Thomas Vachuska57126fe2014-11-11 17:13:24 -0800643 *
644 * @return lookup link function
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700645 */
Madan Jampani2ff05592014-10-10 15:42:47 -0700646 private Function<LinkKey, Link> lookupLink() {
647 return lookupLink;
648 }
649
650 private final class LookupLink implements Function<LinkKey, Link> {
651 @Override
652 public Link apply(LinkKey input) {
Yuta HIGUCHI023295a2014-10-15 23:29:46 -0700653 if (input == null) {
654 return null;
655 } else {
656 return links.get(input);
657 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700658 }
659 }
660
Madan Jampani2ff05592014-10-10 15:42:47 -0700661 private void notifyDelegateIfNotNull(LinkEvent event) {
662 if (event != null) {
663 notifyDelegate(event);
664 }
665 }
666
Jonathan Hart7d656f42015-01-27 14:07:23 -0800667 private void broadcastMessage(MessageSubject subject, Object event) {
Madan Jampani2bfa94c2015-04-11 05:03:49 -0700668 clusterCommunicator.broadcast(event, subject, SERIALIZER::encode);
Madan Jampani2ff05592014-10-10 15:42:47 -0700669 }
670
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700671 private void unicastMessage(NodeId recipient, MessageSubject subject, Object event) throws IOException {
Madan Jampani2bfa94c2015-04-11 05:03:49 -0700672 clusterCommunicator.unicast(event, subject, SERIALIZER::encode, recipient);
Madan Jampania97e8202014-10-10 17:01:33 -0700673 }
674
Jonathan Hart7d656f42015-01-27 14:07:23 -0800675 private void notifyPeers(InternalLinkEvent event) {
Madan Jampani2ff05592014-10-10 15:42:47 -0700676 broadcastMessage(GossipLinkStoreMessageSubjects.LINK_UPDATE, event);
677 }
678
Jonathan Hart7d656f42015-01-27 14:07:23 -0800679 private void notifyPeers(InternalLinkRemovedEvent event) {
Madan Jampani2ff05592014-10-10 15:42:47 -0700680 broadcastMessage(GossipLinkStoreMessageSubjects.LINK_REMOVED, event);
681 }
682
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700683 // notify peer, silently ignoring error
Madan Jampania97e8202014-10-10 17:01:33 -0700684 private void notifyPeer(NodeId peer, InternalLinkEvent event) {
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700685 try {
686 unicastMessage(peer, GossipLinkStoreMessageSubjects.LINK_UPDATE, event);
687 } catch (IOException e) {
688 log.debug("Failed to notify peer {} with message {}", peer, event);
689 }
Madan Jampania97e8202014-10-10 17:01:33 -0700690 }
691
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700692 // notify peer, silently ignoring error
Madan Jampania97e8202014-10-10 17:01:33 -0700693 private void notifyPeer(NodeId peer, InternalLinkRemovedEvent event) {
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700694 try {
695 unicastMessage(peer, GossipLinkStoreMessageSubjects.LINK_REMOVED, event);
696 } catch (IOException e) {
697 log.debug("Failed to notify peer {} with message {}", peer, event);
698 }
Madan Jampania97e8202014-10-10 17:01:33 -0700699 }
700
701 private final class SendAdvertisementTask implements Runnable {
702
703 @Override
704 public void run() {
705 if (Thread.currentThread().isInterrupted()) {
Yuta HIGUCHI1a012722014-11-20 15:21:41 -0800706 log.debug("Interrupted, quitting");
Madan Jampania97e8202014-10-10 17:01:33 -0700707 return;
708 }
709
710 try {
711 final NodeId self = clusterService.getLocalNode().id();
712 Set<ControllerNode> nodes = clusterService.getNodes();
713
714 ImmutableList<NodeId> nodeIds = FluentIterable.from(nodes)
715 .transform(toNodeId())
716 .toList();
717
718 if (nodeIds.size() == 1 && nodeIds.get(0).equals(self)) {
Yuta HIGUCHIfaf9e1c2014-11-20 00:31:29 -0800719 log.trace("No other peers in the cluster.");
Madan Jampania97e8202014-10-10 17:01:33 -0700720 return;
721 }
722
723 NodeId peer;
724 do {
725 int idx = RandomUtils.nextInt(0, nodeIds.size());
726 peer = nodeIds.get(idx);
727 } while (peer.equals(self));
728
729 LinkAntiEntropyAdvertisement ad = createAdvertisement();
730
731 if (Thread.currentThread().isInterrupted()) {
Yuta HIGUCHI1a012722014-11-20 15:21:41 -0800732 log.debug("Interrupted, quitting");
Madan Jampania97e8202014-10-10 17:01:33 -0700733 return;
734 }
735
736 try {
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700737 unicastMessage(peer, LINK_ANTI_ENTROPY_ADVERTISEMENT, ad);
738 } catch (IOException e) {
739 log.debug("Failed to send anti-entropy advertisement to {}", peer);
Madan Jampania97e8202014-10-10 17:01:33 -0700740 return;
741 }
742 } catch (Exception e) {
743 // catch all Exception to avoid Scheduled task being suppressed.
744 log.error("Exception thrown while sending advertisement", e);
745 }
746 }
747 }
748
749 private LinkAntiEntropyAdvertisement createAdvertisement() {
750 final NodeId self = clusterService.getLocalNode().id();
751
752 Map<LinkFragmentId, Timestamp> linkTimestamps = new HashMap<>(linkDescs.size());
753 Map<LinkKey, Timestamp> linkTombstones = new HashMap<>(removedLinks.size());
754
Yuta HIGUCHIb6cfac32014-11-25 13:37:27 -0800755 linkDescs.forEach((linkKey, linkDesc) -> {
Madan Jampania97e8202014-10-10 17:01:33 -0700756 synchronized (linkDesc) {
757 for (Map.Entry<ProviderId, Timestamped<LinkDescription>> e : linkDesc.entrySet()) {
758 linkTimestamps.put(new LinkFragmentId(linkKey, e.getKey()), e.getValue().timestamp());
759 }
760 }
Yuta HIGUCHIb6cfac32014-11-25 13:37:27 -0800761 });
Madan Jampania97e8202014-10-10 17:01:33 -0700762
763 linkTombstones.putAll(removedLinks);
764
765 return new LinkAntiEntropyAdvertisement(self, linkTimestamps, linkTombstones);
766 }
767
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700768 private void handleAntiEntropyAdvertisement(LinkAntiEntropyAdvertisement ad) {
Madan Jampania97e8202014-10-10 17:01:33 -0700769
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700770 final NodeId sender = ad.sender();
771 boolean localOutdated = false;
Madan Jampania97e8202014-10-10 17:01:33 -0700772
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700773 for (Entry<LinkKey, Map<ProviderId, Timestamped<LinkDescription>>>
774 l : linkDescs.entrySet()) {
Madan Jampania97e8202014-10-10 17:01:33 -0700775
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700776 final LinkKey key = l.getKey();
777 final Map<ProviderId, Timestamped<LinkDescription>> link = l.getValue();
778 synchronized (link) {
779 Timestamp localLatest = removedLinks.get(key);
Madan Jampania97e8202014-10-10 17:01:33 -0700780
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700781 for (Entry<ProviderId, Timestamped<LinkDescription>> p : link.entrySet()) {
782 final ProviderId providerId = p.getKey();
783 final Timestamped<LinkDescription> pDesc = p.getValue();
Madan Jampania97e8202014-10-10 17:01:33 -0700784
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700785 final LinkFragmentId fragId = new LinkFragmentId(key, providerId);
786 // remote
787 Timestamp remoteTimestamp = ad.linkTimestamps().get(fragId);
788 if (remoteTimestamp == null) {
789 remoteTimestamp = ad.linkTombstones().get(key);
790 }
791 if (remoteTimestamp == null ||
Jonathan Hart403ea932015-02-20 16:23:00 -0800792 pDesc.isNewerThan(remoteTimestamp)) {
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700793 // I have more recent link description. update peer.
794 notifyPeer(sender, new InternalLinkEvent(providerId, pDesc));
795 } else {
796 final Timestamp remoteLive = ad.linkTimestamps().get(fragId);
797 if (remoteLive != null &&
798 remoteLive.compareTo(pDesc.timestamp()) > 0) {
799 // I have something outdated
800 localOutdated = true;
801 }
802 }
803
804 // search local latest along the way
805 if (localLatest == null ||
Jonathan Hart403ea932015-02-20 16:23:00 -0800806 pDesc.isNewerThan(localLatest)) {
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700807 localLatest = pDesc.timestamp();
808 }
809 }
810 // Tests if remote remove is more recent then local latest.
811 final Timestamp remoteRemove = ad.linkTombstones().get(key);
812 if (remoteRemove != null) {
813 if (localLatest != null &&
814 localLatest.compareTo(remoteRemove) < 0) {
815 // remote remove is more recent
816 notifyDelegateIfNotNull(removeLinkInternal(key, remoteRemove));
817 }
818 }
Madan Jampania97e8202014-10-10 17:01:33 -0700819 }
820 }
821
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700822 // populate remove info if not known locally
823 for (Entry<LinkKey, Timestamp> remoteRm : ad.linkTombstones().entrySet()) {
824 final LinkKey key = remoteRm.getKey();
825 final Timestamp remoteRemove = remoteRm.getValue();
826 // relying on removeLinkInternal to ignore stale info
827 notifyDelegateIfNotNull(removeLinkInternal(key, remoteRemove));
828 }
Madan Jampania97e8202014-10-10 17:01:33 -0700829
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700830 if (localOutdated) {
831 // send back advertisement to speed up convergence
832 try {
833 unicastMessage(sender, LINK_ANTI_ENTROPY_ADVERTISEMENT,
834 createAdvertisement());
835 } catch (IOException e) {
836 log.debug("Failed to send back active advertisement");
Madan Jampania97e8202014-10-10 17:01:33 -0700837 }
838 }
839 }
840
Yuta HIGUCHI80d56592014-11-25 15:11:13 -0800841 private final class InternalLinkEventListener
842 implements ClusterMessageHandler {
Madan Jampani2ff05592014-10-10 15:42:47 -0700843 @Override
844 public void handle(ClusterMessage message) {
845
Yuta HIGUCHIc01d2aa2014-10-19 01:19:34 -0700846 log.trace("Received link event from peer: {}", message.sender());
Sho SHIMIZU5eb79c52015-09-29 14:32:49 -0700847 InternalLinkEvent event = SERIALIZER.decode(message.payload());
Madan Jampani2ff05592014-10-10 15:42:47 -0700848
849 ProviderId providerId = event.providerId();
850 Timestamped<LinkDescription> linkDescription = event.linkDescription();
851
Madan Jampani2af244a2015-02-22 13:12:01 -0800852 try {
853 notifyDelegateIfNotNull(createOrUpdateLinkInternal(providerId, linkDescription));
854 } catch (Exception e) {
855 log.warn("Exception thrown handling link event", e);
856 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700857 }
858 }
859
Yuta HIGUCHI80d56592014-11-25 15:11:13 -0800860 private final class InternalLinkRemovedEventListener
861 implements ClusterMessageHandler {
Madan Jampani2ff05592014-10-10 15:42:47 -0700862 @Override
863 public void handle(ClusterMessage message) {
864
Yuta HIGUCHIc01d2aa2014-10-19 01:19:34 -0700865 log.trace("Received link removed event from peer: {}", message.sender());
Sho SHIMIZU5eb79c52015-09-29 14:32:49 -0700866 InternalLinkRemovedEvent event = SERIALIZER.decode(message.payload());
Madan Jampani2ff05592014-10-10 15:42:47 -0700867
868 LinkKey linkKey = event.linkKey();
869 Timestamp timestamp = event.timestamp();
870
Madan Jampani2af244a2015-02-22 13:12:01 -0800871 try {
872 notifyDelegateIfNotNull(removeLinkInternal(linkKey, timestamp));
873 } catch (Exception e) {
874 log.warn("Exception thrown handling link removed", e);
875 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700876 }
877 }
Madan Jampania97e8202014-10-10 17:01:33 -0700878
Yuta HIGUCHI80d56592014-11-25 15:11:13 -0800879 private final class InternalLinkAntiEntropyAdvertisementListener
880 implements ClusterMessageHandler {
Madan Jampania97e8202014-10-10 17:01:33 -0700881
882 @Override
883 public void handle(ClusterMessage message) {
Yuta HIGUCHIfaf9e1c2014-11-20 00:31:29 -0800884 log.trace("Received Link Anti-Entropy advertisement from peer: {}", message.sender());
Madan Jampania97e8202014-10-10 17:01:33 -0700885 LinkAntiEntropyAdvertisement advertisement = SERIALIZER.decode(message.payload());
Madan Jampani2af244a2015-02-22 13:12:01 -0800886 try {
887 handleAntiEntropyAdvertisement(advertisement);
888 } catch (Exception e) {
889 log.warn("Exception thrown while handling Link advertisements", e);
890 throw e;
891 }
Madan Jampania97e8202014-10-10 17:01:33 -0700892 }
893 }
Marc De Leenheerb473b9d2015-02-06 15:21:03 -0800894
895 private final class LinkInjectedEventListener
896 implements ClusterMessageHandler {
897 @Override
898 public void handle(ClusterMessage message) {
899
900 log.trace("Received injected link event from peer: {}", message.sender());
901 LinkInjectedEvent linkInjectedEvent = SERIALIZER.decode(message.payload());
902
903 ProviderId providerId = linkInjectedEvent.providerId();
904 LinkDescription linkDescription = linkInjectedEvent.linkDescription();
905
HIGUCHI Yuta0b4d2982015-02-27 23:18:52 -0800906 final DeviceId deviceId = linkDescription.dst().deviceId();
907 if (!deviceClockService.isTimestampAvailable(deviceId)) {
908 // workaround for ONOS-1208
909 log.warn("Not ready to accept update. Dropping {}", linkDescription);
910 return;
911 }
912
Madan Jampani2af244a2015-02-22 13:12:01 -0800913 try {
914 createOrUpdateLink(providerId, linkDescription);
915 } catch (Exception e) {
916 log.warn("Exception thrown while handling link injected event", e);
917 }
Marc De Leenheerb473b9d2015-02-06 15:21:03 -0800918 }
919 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700920}