blob: e4328daee5ea047444417f5ac0f77ed0be9c3efd [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present 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
sangyun-hanf7fe7632016-02-16 14:47:44 +090077import static com.google.common.base.Preconditions.checkArgument;
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
sangyun-hanf7fe7632016-02-16 14:47:44 +0900111 // Default delay for ScheduledExecutorService of anti-entropy(BackgroundExecutor)
112 private static final long DEFAULT_INITIAL_DELAY = 5;
113
114 // Default period for ScheduledExecutorService of anti-entropy(BackgroundExecutor)
115 private static final long DEFAULT_PERIOD = 5;
116
117 private static long initialDelaySec = DEFAULT_INITIAL_DELAY;
118 private static long periodSec = DEFAULT_PERIOD;
119
Madan Jampani2ff05592014-10-10 15:42:47 -0700120 private final Logger log = getLogger(getClass());
121
122 // Link inventory
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700123 private final ConcurrentMap<LinkKey, Map<ProviderId, Timestamped<LinkDescription>>> linkDescs =
Madan Jampani2ff05592014-10-10 15:42:47 -0700124 new ConcurrentHashMap<>();
125
126 // Link instance cache
127 private final ConcurrentMap<LinkKey, Link> links = new ConcurrentHashMap<>();
128
129 // Egress and ingress link sets
130 private final SetMultimap<DeviceId, LinkKey> srcLinks = createSynchronizedHashMultiMap();
131 private final SetMultimap<DeviceId, LinkKey> dstLinks = createSynchronizedHashMultiMap();
132
133 // Remove links
Yuta HIGUCHIb9125562014-12-01 23:28:22 -0800134 private final Map<LinkKey, Timestamp> removedLinks = new ConcurrentHashMap<>();
Madan Jampani2ff05592014-10-10 15:42:47 -0700135
136 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Yuta HIGUCHI093e83e2014-10-10 22:26:11 -0700137 protected DeviceClockService deviceClockService;
Madan Jampani2ff05592014-10-10 15:42:47 -0700138
139 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
140 protected ClusterCommunicationService clusterCommunicator;
141
142 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
143 protected ClusterService clusterService;
144
Marc De Leenheerb473b9d2015-02-06 15:21:03 -0800145 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
146 protected MastershipService mastershipService;
147
Yuta HIGUCHI3e5d11a2014-11-04 14:16:44 -0800148 protected static final KryoSerializer SERIALIZER = new KryoSerializer() {
Madan Jampani2ff05592014-10-10 15:42:47 -0700149 @Override
150 protected void setupKryoPool() {
Yuta HIGUCHI8d143d22014-10-19 23:15:09 -0700151 serializerPool = KryoNamespace.newBuilder()
Yuta HIGUCHI91768e32014-11-22 05:06:35 -0800152 .register(DistributedStoreSerializers.STORE_COMMON)
153 .nextId(DistributedStoreSerializers.STORE_CUSTOM_BEGIN)
Madan Jampani2ff05592014-10-10 15:42:47 -0700154 .register(InternalLinkEvent.class)
155 .register(InternalLinkRemovedEvent.class)
Madan Jampanid2054d42014-10-10 17:27:06 -0700156 .register(LinkAntiEntropyAdvertisement.class)
157 .register(LinkFragmentId.class)
Marc De Leenheerb473b9d2015-02-06 15:21:03 -0800158 .register(LinkInjectedEvent.class)
Yuta HIGUCHI91768e32014-11-22 05:06:35 -0800159 .build();
Madan Jampani2ff05592014-10-10 15:42:47 -0700160 }
161 };
162
Yuta HIGUCHI80d56592014-11-25 15:11:13 -0800163 private ExecutorService executor;
164
Yuta HIGUCHI06586272014-11-25 14:27:03 -0800165 private ScheduledExecutorService backgroundExecutors;
Madan Jampania97e8202014-10-10 17:01:33 -0700166
Madan Jampani2ff05592014-10-10 15:42:47 -0700167 @Activate
168 public void activate() {
169
Thomas Vachuska6f94ded2015-02-21 14:02:38 -0800170 executor = Executors.newCachedThreadPool(groupedThreads("onos/link", "fg-%d"));
Yuta HIGUCHI80d56592014-11-25 15:11:13 -0800171
Yuta HIGUCHI06586272014-11-25 14:27:03 -0800172 backgroundExecutors =
Thomas Vachuska6f94ded2015-02-21 14:02:38 -0800173 newSingleThreadScheduledExecutor(minPriority(groupedThreads("onos/link", "bg-%d")));
Madan Jampania97e8202014-10-10 17:01:33 -0700174
Madan Jampani2af244a2015-02-22 13:12:01 -0800175 clusterCommunicator.addSubscriber(
176 GossipLinkStoreMessageSubjects.LINK_UPDATE,
177 new InternalLinkEventListener(), executor);
178 clusterCommunicator.addSubscriber(
179 GossipLinkStoreMessageSubjects.LINK_REMOVED,
180 new InternalLinkRemovedEventListener(), executor);
181 clusterCommunicator.addSubscriber(
182 GossipLinkStoreMessageSubjects.LINK_ANTI_ENTROPY_ADVERTISEMENT,
183 new InternalLinkAntiEntropyAdvertisementListener(), backgroundExecutors);
184 clusterCommunicator.addSubscriber(
185 GossipLinkStoreMessageSubjects.LINK_INJECTED,
186 new LinkInjectedEventListener(), executor);
187
Madan Jampania97e8202014-10-10 17:01:33 -0700188 // start anti-entropy thread
Yuta HIGUCHI06586272014-11-25 14:27:03 -0800189 backgroundExecutors.scheduleAtFixedRate(new SendAdvertisementTask(),
Madan Jampania97e8202014-10-10 17:01:33 -0700190 initialDelaySec, periodSec, TimeUnit.SECONDS);
Madan Jampani2ff05592014-10-10 15:42:47 -0700191
192 log.info("Started");
193 }
194
195 @Deactivate
196 public void deactivate() {
Madan Jampani3ffbb272014-10-13 11:19:37 -0700197
Yuta HIGUCHI80d56592014-11-25 15:11:13 -0800198 executor.shutdownNow();
199
Yuta HIGUCHI06586272014-11-25 14:27:03 -0800200 backgroundExecutors.shutdownNow();
Madan Jampani3ffbb272014-10-13 11:19:37 -0700201 try {
Yuta HIGUCHI06586272014-11-25 14:27:03 -0800202 if (!backgroundExecutors.awaitTermination(5, TimeUnit.SECONDS)) {
Madan Jampani3ffbb272014-10-13 11:19:37 -0700203 log.error("Timeout during executor shutdown");
204 }
205 } catch (InterruptedException e) {
206 log.error("Error during executor shutdown", e);
207 }
208
Madan Jampani2ff05592014-10-10 15:42:47 -0700209 linkDescs.clear();
210 links.clear();
211 srcLinks.clear();
212 dstLinks.clear();
213 log.info("Stopped");
214 }
215
216 @Override
217 public int getLinkCount() {
218 return links.size();
219 }
220
221 @Override
222 public Iterable<Link> getLinks() {
223 return Collections.unmodifiableCollection(links.values());
224 }
225
226 @Override
227 public Set<Link> getDeviceEgressLinks(DeviceId deviceId) {
228 // lock for iteration
229 synchronized (srcLinks) {
230 return FluentIterable.from(srcLinks.get(deviceId))
231 .transform(lookupLink())
232 .filter(notNull())
233 .toSet();
234 }
235 }
236
237 @Override
238 public Set<Link> getDeviceIngressLinks(DeviceId deviceId) {
239 // lock for iteration
240 synchronized (dstLinks) {
241 return FluentIterable.from(dstLinks.get(deviceId))
242 .transform(lookupLink())
243 .filter(notNull())
244 .toSet();
245 }
246 }
247
248 @Override
249 public Link getLink(ConnectPoint src, ConnectPoint dst) {
Yuta HIGUCHI18ab8a92014-10-13 11:16:19 -0700250 return links.get(linkKey(src, dst));
Madan Jampani2ff05592014-10-10 15:42:47 -0700251 }
252
253 @Override
254 public Set<Link> getEgressLinks(ConnectPoint src) {
255 Set<Link> egress = new HashSet<>();
HIGUCHI Yuta4d973eb2015-02-26 14:28:49 -0800256 //
257 // Change `srcLinks` to ConcurrentMap<DeviceId, (Concurrent)Set>
258 // to remove this synchronized block, if we hit performance issue.
259 // SetMultiMap#get returns wrapped collection to provide modifiable-view.
260 // And the wrapped collection is not concurrent access safe.
261 //
262 // Our use case here does not require returned collection to be modifiable,
263 // so the wrapped collection forces us to lock the whole multiset,
264 // for benefit we don't need.
265 //
266 // Same applies to `dstLinks`
267 synchronized (srcLinks) {
268 for (LinkKey linkKey : srcLinks.get(src.deviceId())) {
269 if (linkKey.src().equals(src)) {
270 Link link = links.get(linkKey);
271 if (link != null) {
272 egress.add(link);
273 } else {
274 log.debug("Egress link for {} was null, skipped", linkKey);
275 }
Ray Milkey7bbeb3f2014-12-11 14:59:26 -0800276 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700277 }
278 }
279 return egress;
280 }
281
282 @Override
283 public Set<Link> getIngressLinks(ConnectPoint dst) {
284 Set<Link> ingress = new HashSet<>();
HIGUCHI Yuta4d973eb2015-02-26 14:28:49 -0800285 synchronized (dstLinks) {
286 for (LinkKey linkKey : dstLinks.get(dst.deviceId())) {
287 if (linkKey.dst().equals(dst)) {
288 Link link = links.get(linkKey);
289 if (link != null) {
290 ingress.add(link);
291 } else {
292 log.debug("Ingress link for {} was null, skipped", linkKey);
293 }
Ray Milkey7bbeb3f2014-12-11 14:59:26 -0800294 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700295 }
296 }
297 return ingress;
298 }
299
300 @Override
301 public LinkEvent createOrUpdateLink(ProviderId providerId,
302 LinkDescription linkDescription) {
303
Marc De Leenheerb473b9d2015-02-06 15:21:03 -0800304 final DeviceId dstDeviceId = linkDescription.dst().deviceId();
305 final NodeId localNode = clusterService.getLocalNode().id();
306 final NodeId dstNode = mastershipService.getMasterFor(dstDeviceId);
Madan Jampani2ff05592014-10-10 15:42:47 -0700307
Marc De Leenheerb473b9d2015-02-06 15:21:03 -0800308 // Process link update only if we're the master of the destination node,
309 // otherwise signal the actual master.
310 LinkEvent linkEvent = null;
311 if (localNode.equals(dstNode)) {
Madan Jampani2ff05592014-10-10 15:42:47 -0700312
Marc De Leenheerb473b9d2015-02-06 15:21:03 -0800313 Timestamp newTimestamp = deviceClockService.getTimestamp(dstDeviceId);
314
315 final Timestamped<LinkDescription> deltaDesc = new Timestamped<>(linkDescription, newTimestamp);
316
317 LinkKey key = linkKey(linkDescription.src(), linkDescription.dst());
318 final Timestamped<LinkDescription> mergedDesc;
319 Map<ProviderId, Timestamped<LinkDescription>> map = getOrCreateLinkDescriptions(key);
320
321 synchronized (map) {
322 linkEvent = createOrUpdateLinkInternal(providerId, deltaDesc);
323 mergedDesc = map.get(providerId);
324 }
325
326 if (linkEvent != null) {
Madan Jampanif2af7712015-05-29 18:43:52 -0700327 log.debug("Notifying peers of a link update topology event from providerId: "
Marc De Leenheerb473b9d2015-02-06 15:21:03 -0800328 + "{} between src: {} and dst: {}",
329 providerId, linkDescription.src(), linkDescription.dst());
330 notifyPeers(new InternalLinkEvent(providerId, mergedDesc));
331 }
332
333 } else {
HIGUCHI Yuta1979f552015-12-28 21:24:26 -0800334 // Only forward for ConfigProvider
335 // Forwarding was added as a workaround for ONOS-490
HIGUCHI Yuta4ea4e422016-01-13 16:40:34 -0800336 if (!providerId.scheme().equals("cfg")) {
HIGUCHI Yuta1979f552015-12-28 21:24:26 -0800337 return null;
338 }
HIGUCHI Yutadc2e7c22015-02-24 12:19:47 -0800339 // FIXME Temporary hack for NPE (ONOS-1171).
340 // Proper fix is to implement forwarding to master on ConfigProvider
341 // redo ONOS-490
342 if (dstNode == null) {
343 // silently ignore
344 return null;
345 }
346
Marc De Leenheerb473b9d2015-02-06 15:21:03 -0800347
348 LinkInjectedEvent linkInjectedEvent = new LinkInjectedEvent(providerId, linkDescription);
Marc De Leenheerb473b9d2015-02-06 15:21:03 -0800349
Brian O'Connor5eb77c82015-03-02 18:09:39 -0800350 // TODO check unicast return value
Madan Jampani2bfa94c2015-04-11 05:03:49 -0700351 clusterCommunicator.unicast(linkInjectedEvent,
352 GossipLinkStoreMessageSubjects.LINK_INJECTED,
353 SERIALIZER::encode,
354 dstNode);
Yuta HIGUCHI92cd51e2014-10-13 10:51:45 -0700355 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700356
Marc De Leenheerb473b9d2015-02-06 15:21:03 -0800357 return linkEvent;
Madan Jampani2ff05592014-10-10 15:42:47 -0700358 }
359
Thomas Vachuska57126fe2014-11-11 17:13:24 -0800360 @Override
361 public LinkEvent removeOrDownLink(ConnectPoint src, ConnectPoint dst) {
362 Link link = getLink(src, dst);
363 if (link == null) {
364 return null;
365 }
366
367 if (link.isDurable()) {
368 // FIXME: this is not the right thing to call for the gossip store; will not sync link state!!!
369 return link.state() == INACTIVE ? null :
370 updateLink(linkKey(link.src(), link.dst()), link,
Ray Milkey2693bda2016-01-22 16:08:14 -0800371 DefaultLink.builder()
372 .providerId(link.providerId())
373 .src(link.src())
374 .dst(link.dst())
375 .type(link.type())
376 .state(INACTIVE)
377 .isExpected(link.isExpected())
378 .annotations(link.annotations())
379 .build());
Thomas Vachuska57126fe2014-11-11 17:13:24 -0800380 }
381 return removeLink(src, dst);
382 }
383
Madan Jampani2ff05592014-10-10 15:42:47 -0700384 private LinkEvent createOrUpdateLinkInternal(
385 ProviderId providerId,
386 Timestamped<LinkDescription> linkDescription) {
387
Yuta HIGUCHIa85542b2014-10-21 19:29:49 -0700388 final LinkKey key = linkKey(linkDescription.value().src(),
Marc De Leenheerb473b9d2015-02-06 15:21:03 -0800389 linkDescription.value().dst());
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700390 Map<ProviderId, Timestamped<LinkDescription>> descs = getOrCreateLinkDescriptions(key);
Madan Jampani2ff05592014-10-10 15:42:47 -0700391
392 synchronized (descs) {
393 // if the link was previously removed, we should proceed if and
394 // only if this request is more recent.
395 Timestamp linkRemovedTimestamp = removedLinks.get(key);
396 if (linkRemovedTimestamp != null) {
Jonathan Hart403ea932015-02-20 16:23:00 -0800397 if (linkDescription.isNewerThan(linkRemovedTimestamp)) {
Madan Jampani2ff05592014-10-10 15:42:47 -0700398 removedLinks.remove(key);
399 } else {
Yuta HIGUCHIa85542b2014-10-21 19:29:49 -0700400 log.trace("Link {} was already removed ignoring.", key);
Madan Jampani2ff05592014-10-10 15:42:47 -0700401 return null;
402 }
403 }
404
405 final Link oldLink = links.get(key);
406 // update description
407 createOrUpdateLinkDescription(descs, providerId, linkDescription);
408 final Link newLink = composeLink(descs);
409 if (oldLink == null) {
410 return createLink(key, newLink);
411 }
412 return updateLink(key, oldLink, newLink);
413 }
414 }
415
416 // Guarded by linkDescs value (=locking each Link)
417 private Timestamped<LinkDescription> createOrUpdateLinkDescription(
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700418 Map<ProviderId, Timestamped<LinkDescription>> descs,
Madan Jampani2ff05592014-10-10 15:42:47 -0700419 ProviderId providerId,
420 Timestamped<LinkDescription> linkDescription) {
421
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700422 // merge existing annotations
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700423 Timestamped<LinkDescription> existingLinkDescription = descs.get(providerId);
Madan Jampani2ff05592014-10-10 15:42:47 -0700424 if (existingLinkDescription != null && existingLinkDescription.isNewer(linkDescription)) {
Yuta HIGUCHIa85542b2014-10-21 19:29:49 -0700425 log.trace("local info is more up-to-date, ignoring {}.", linkDescription);
Madan Jampani2ff05592014-10-10 15:42:47 -0700426 return null;
427 }
428 Timestamped<LinkDescription> newLinkDescription = linkDescription;
429 if (existingLinkDescription != null) {
Yuta HIGUCHIa85542b2014-10-21 19:29:49 -0700430 // we only allow transition from INDIRECT -> DIRECT
431 final Type newType;
432 if (existingLinkDescription.value().type() == DIRECT) {
433 newType = DIRECT;
434 } else {
435 newType = linkDescription.value().type();
436 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700437 SparseAnnotations merged = union(existingLinkDescription.value().annotations(),
438 linkDescription.value().annotations());
Thomas Vachuska57126fe2014-11-11 17:13:24 -0800439 newLinkDescription = new Timestamped<>(
Madan Jampani2ff05592014-10-10 15:42:47 -0700440 new DefaultLinkDescription(
441 linkDescription.value().src(),
442 linkDescription.value().dst(),
Ray Milkeyb7f0f642016-01-22 16:08:14 -0800443 newType,
444 existingLinkDescription.value().isExpected(),
445 merged),
Madan Jampani2ff05592014-10-10 15:42:47 -0700446 linkDescription.timestamp());
447 }
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700448 return descs.put(providerId, newLinkDescription);
Madan Jampani2ff05592014-10-10 15:42:47 -0700449 }
450
451 // Creates and stores the link and returns the appropriate event.
452 // Guarded by linkDescs value (=locking each Link)
453 private LinkEvent createLink(LinkKey key, Link newLink) {
Madan Jampani2ff05592014-10-10 15:42:47 -0700454 links.put(key, newLink);
455 srcLinks.put(newLink.src().deviceId(), key);
456 dstLinks.put(newLink.dst().deviceId(), key);
457 return new LinkEvent(LINK_ADDED, newLink);
458 }
459
460 // Updates, if necessary the specified link and returns the appropriate event.
461 // Guarded by linkDescs value (=locking each Link)
462 private LinkEvent updateLink(LinkKey key, Link oldLink, Link newLink) {
Yuta HIGUCHIa85542b2014-10-21 19:29:49 -0700463 // Note: INDIRECT -> DIRECT transition only
464 // so that BDDP discovered Link will not overwrite LDDP Link
Thomas Vachuska57126fe2014-11-11 17:13:24 -0800465 if (oldLink.state() != newLink.state() ||
466 (oldLink.type() == INDIRECT && newLink.type() == DIRECT) ||
Madan Jampani2ff05592014-10-10 15:42:47 -0700467 !AnnotationsUtil.isEqual(oldLink.annotations(), newLink.annotations())) {
468
469 links.put(key, newLink);
Marc De Leenheerb473b9d2015-02-06 15:21:03 -0800470 // strictly speaking following can be omitted
Madan Jampani2ff05592014-10-10 15:42:47 -0700471 srcLinks.put(oldLink.src().deviceId(), key);
472 dstLinks.put(oldLink.dst().deviceId(), key);
473 return new LinkEvent(LINK_UPDATED, newLink);
474 }
475 return null;
476 }
477
478 @Override
479 public LinkEvent removeLink(ConnectPoint src, ConnectPoint dst) {
Yuta HIGUCHI18ab8a92014-10-13 11:16:19 -0700480 final LinkKey key = linkKey(src, dst);
Madan Jampani2ff05592014-10-10 15:42:47 -0700481
482 DeviceId dstDeviceId = dst.deviceId();
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700483 Timestamp timestamp = null;
484 try {
485 timestamp = deviceClockService.getTimestamp(dstDeviceId);
486 } catch (IllegalStateException e) {
Thomas Vachuska6f90c592015-06-03 20:06:58 -0700487 log.debug("Failed to remove link {}, was not the master", key);
488 // there are times when this is called before mastership
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700489 // handoff correctly completes.
490 return null;
491 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700492
493 LinkEvent event = removeLinkInternal(key, timestamp);
494
495 if (event != null) {
Madan Jampanif2af7712015-05-29 18:43:52 -0700496 log.debug("Notifying peers of a link removed topology event for a link "
Madan Jampani2ff05592014-10-10 15:42:47 -0700497 + "between src: {} and dst: {}", src, dst);
Jonathan Hart7d656f42015-01-27 14:07:23 -0800498 notifyPeers(new InternalLinkRemovedEvent(key, timestamp));
Madan Jampani2ff05592014-10-10 15:42:47 -0700499 }
500 return event;
501 }
502
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700503 private static Timestamped<LinkDescription> getPrimaryDescription(
504 Map<ProviderId, Timestamped<LinkDescription>> linkDescriptions) {
505
Madan Jampani2ff05592014-10-10 15:42:47 -0700506 synchronized (linkDescriptions) {
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700507 for (Entry<ProviderId, Timestamped<LinkDescription>>
508 e : linkDescriptions.entrySet()) {
509
510 if (!e.getKey().isAncillary()) {
511 return e.getValue();
512 }
513 }
514 }
515 return null;
516 }
517
518
519 // TODO: consider slicing out as Timestamp utils
520 /**
521 * Checks is timestamp is more recent than timestamped object.
522 *
523 * @param timestamp to check if this is more recent then other
524 * @param timestamped object to be tested against
525 * @return true if {@code timestamp} is more recent than {@code timestamped}
526 * or {@code timestamped is null}
527 */
528 private static boolean isMoreRecent(Timestamp timestamp, Timestamped<?> timestamped) {
529 checkNotNull(timestamp);
530 if (timestamped == null) {
531 return true;
532 }
533 return timestamp.compareTo(timestamped.timestamp()) > 0;
534 }
535
536 private LinkEvent removeLinkInternal(LinkKey key, Timestamp timestamp) {
537 Map<ProviderId, Timestamped<LinkDescription>> linkDescriptions
538 = getOrCreateLinkDescriptions(key);
539
540 synchronized (linkDescriptions) {
541 if (linkDescriptions.isEmpty()) {
542 // never seen such link before. keeping timestamp for record
543 removedLinks.put(key, timestamp);
544 return null;
545 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700546 // accept removal request if given timestamp is newer than
547 // the latest Timestamp from Primary provider
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700548 Timestamped<LinkDescription> prim = getPrimaryDescription(linkDescriptions);
549 if (!isMoreRecent(timestamp, prim)) {
550 // outdated remove request, ignore
Madan Jampani2ff05592014-10-10 15:42:47 -0700551 return null;
552 }
553 removedLinks.put(key, timestamp);
Thomas Vachuska57126fe2014-11-11 17:13:24 -0800554 Link link = links.remove(key);
Madan Jampani2ff05592014-10-10 15:42:47 -0700555 linkDescriptions.clear();
556 if (link != null) {
557 srcLinks.remove(link.src().deviceId(), key);
558 dstLinks.remove(link.dst().deviceId(), key);
559 return new LinkEvent(LINK_REMOVED, link);
560 }
561 return null;
562 }
563 }
564
Yuta HIGUCHI800fac62014-12-11 19:23:01 -0800565 /**
566 * Creates concurrent readable, synchronized HashMultimap.
567 *
568 * @return SetMultimap
569 */
Madan Jampani2ff05592014-10-10 15:42:47 -0700570 private static <K, V> SetMultimap<K, V> createSynchronizedHashMultiMap() {
Yuta HIGUCHI800fac62014-12-11 19:23:01 -0800571 return synchronizedSetMultimap(
Sho SHIMIZU7a4087b2015-09-10 09:23:16 -0700572 Multimaps.newSetMultimap(new ConcurrentHashMap<>(),
Yuta HIGUCHI800fac62014-12-11 19:23:01 -0800573 () -> Sets.newConcurrentHashSet()));
Madan Jampani2ff05592014-10-10 15:42:47 -0700574 }
575
576 /**
577 * @return primary ProviderID, or randomly chosen one if none exists
578 */
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700579 private static ProviderId pickBaseProviderId(
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700580 Map<ProviderId, Timestamped<LinkDescription>> linkDescriptions) {
Madan Jampani2ff05592014-10-10 15:42:47 -0700581
582 ProviderId fallBackPrimary = null;
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700583 for (Entry<ProviderId, Timestamped<LinkDescription>> e : linkDescriptions.entrySet()) {
Madan Jampani2ff05592014-10-10 15:42:47 -0700584 if (!e.getKey().isAncillary()) {
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700585 // found primary
Madan Jampani2ff05592014-10-10 15:42:47 -0700586 return e.getKey();
587 } else if (fallBackPrimary == null) {
588 // pick randomly as a fallback in case there is no primary
589 fallBackPrimary = e.getKey();
590 }
591 }
592 return fallBackPrimary;
593 }
594
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700595 // Guarded by linkDescs value (=locking each Link)
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700596 private Link composeLink(Map<ProviderId, Timestamped<LinkDescription>> descs) {
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700597 ProviderId baseProviderId = pickBaseProviderId(descs);
598 Timestamped<LinkDescription> base = descs.get(baseProviderId);
Madan Jampani2ff05592014-10-10 15:42:47 -0700599
600 ConnectPoint src = base.value().src();
601 ConnectPoint dst = base.value().dst();
602 Type type = base.value().type();
603 DefaultAnnotations annotations = DefaultAnnotations.builder().build();
604 annotations = merge(annotations, base.value().annotations());
605
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700606 for (Entry<ProviderId, Timestamped<LinkDescription>> e : descs.entrySet()) {
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700607 if (baseProviderId.equals(e.getKey())) {
Madan Jampani2ff05592014-10-10 15:42:47 -0700608 continue;
609 }
610
Yuta HIGUCHI65934892014-12-04 17:47:44 -0800611 // Note: In the long run we should keep track of Description timestamp
Madan Jampani2ff05592014-10-10 15:42:47 -0700612 // and only merge conflicting keys when timestamp is newer
613 // Currently assuming there will never be a key conflict between
614 // providers
615
616 // annotation merging. not so efficient, should revisit later
617 annotations = merge(annotations, e.getValue().value().annotations());
618 }
619
Ray Milkeyb7f0f642016-01-22 16:08:14 -0800620 //boolean isDurable = Objects.equals(annotations.value(AnnotationKeys.DURABLE), "true");
621
622 // TEMP
623 Link.State initialLinkState = base.value().isExpected() ? ACTIVE : INACTIVE;
Ray Milkey2693bda2016-01-22 16:08:14 -0800624 return DefaultLink.builder()
625 .providerId(baseProviderId)
626 .src(src)
627 .dst(dst)
628 .type(type)
Ray Milkeyb7f0f642016-01-22 16:08:14 -0800629 .state(initialLinkState)
630 .isExpected(base.value().isExpected())
Ray Milkey2693bda2016-01-22 16:08:14 -0800631 .annotations(annotations)
632 .build();
Madan Jampani2ff05592014-10-10 15:42:47 -0700633 }
634
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700635 private Map<ProviderId, Timestamped<LinkDescription>> getOrCreateLinkDescriptions(LinkKey key) {
Yuta HIGUCHI3e1a5bf2014-10-14 19:39:58 -0700636 Map<ProviderId, Timestamped<LinkDescription>> r;
637 r = linkDescs.get(key);
638 if (r != null) {
639 return r;
640 }
641 r = new HashMap<>();
642 final Map<ProviderId, Timestamped<LinkDescription>> concurrentlyAdded;
643 concurrentlyAdded = linkDescs.putIfAbsent(key, r);
644 if (concurrentlyAdded != null) {
645 return concurrentlyAdded;
646 } else {
647 return r;
648 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700649 }
650
651 private final Function<LinkKey, Link> lookupLink = new LookupLink();
Thomas Vachuska57126fe2014-11-11 17:13:24 -0800652
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700653 /**
654 * Returns a Function to lookup Link instance using LinkKey from cache.
Thomas Vachuska57126fe2014-11-11 17:13:24 -0800655 *
656 * @return lookup link function
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700657 */
Madan Jampani2ff05592014-10-10 15:42:47 -0700658 private Function<LinkKey, Link> lookupLink() {
659 return lookupLink;
660 }
661
662 private final class LookupLink implements Function<LinkKey, Link> {
663 @Override
664 public Link apply(LinkKey input) {
Yuta HIGUCHI023295a2014-10-15 23:29:46 -0700665 if (input == null) {
666 return null;
667 } else {
668 return links.get(input);
669 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700670 }
671 }
672
Madan Jampani2ff05592014-10-10 15:42:47 -0700673 private void notifyDelegateIfNotNull(LinkEvent event) {
674 if (event != null) {
675 notifyDelegate(event);
676 }
677 }
678
Jonathan Hart7d656f42015-01-27 14:07:23 -0800679 private void broadcastMessage(MessageSubject subject, Object event) {
Madan Jampani2bfa94c2015-04-11 05:03:49 -0700680 clusterCommunicator.broadcast(event, subject, SERIALIZER::encode);
Madan Jampani2ff05592014-10-10 15:42:47 -0700681 }
682
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700683 private void unicastMessage(NodeId recipient, MessageSubject subject, Object event) throws IOException {
Madan Jampani2bfa94c2015-04-11 05:03:49 -0700684 clusterCommunicator.unicast(event, subject, SERIALIZER::encode, recipient);
Madan Jampania97e8202014-10-10 17:01:33 -0700685 }
686
Jonathan Hart7d656f42015-01-27 14:07:23 -0800687 private void notifyPeers(InternalLinkEvent event) {
Madan Jampani2ff05592014-10-10 15:42:47 -0700688 broadcastMessage(GossipLinkStoreMessageSubjects.LINK_UPDATE, event);
689 }
690
Jonathan Hart7d656f42015-01-27 14:07:23 -0800691 private void notifyPeers(InternalLinkRemovedEvent event) {
Madan Jampani2ff05592014-10-10 15:42:47 -0700692 broadcastMessage(GossipLinkStoreMessageSubjects.LINK_REMOVED, event);
693 }
694
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700695 // notify peer, silently ignoring error
Madan Jampania97e8202014-10-10 17:01:33 -0700696 private void notifyPeer(NodeId peer, InternalLinkEvent event) {
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700697 try {
698 unicastMessage(peer, GossipLinkStoreMessageSubjects.LINK_UPDATE, event);
699 } catch (IOException e) {
700 log.debug("Failed to notify peer {} with message {}", peer, event);
701 }
Madan Jampania97e8202014-10-10 17:01:33 -0700702 }
703
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700704 // notify peer, silently ignoring error
Madan Jampania97e8202014-10-10 17:01:33 -0700705 private void notifyPeer(NodeId peer, InternalLinkRemovedEvent event) {
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700706 try {
707 unicastMessage(peer, GossipLinkStoreMessageSubjects.LINK_REMOVED, event);
708 } catch (IOException e) {
709 log.debug("Failed to notify peer {} with message {}", peer, event);
710 }
Madan Jampania97e8202014-10-10 17:01:33 -0700711 }
712
sangyun-hanf7fe7632016-02-16 14:47:44 +0900713 /**
714 * sets the time to delay first execution for anti-entropy.
715 * (scheduleAtFixedRate of ScheduledExecutorService)
716 *
717 * @param delay the time to delay first execution for anti-entropy
718 */
719 private void setInitialDelaySec(long delay) {
720 checkArgument(delay >= 0, "Initial delay of scheduleAtFixedRate() must be 0 or more");
721 initialDelaySec = delay;
722 }
723
724 /**
725 * sets the period between successive execution for anti-entropy.
726 * (scheduleAtFixedRate of ScheduledExecutorService)
727 *
728 * @param period the period between successive execution for anti-entropy
729 */
730 private void setPeriodSec(long period) {
731 checkArgument(period > 0, "Period of scheduleAtFixedRate() must be greater than 0");
732 periodSec = period;
733 }
734
Madan Jampania97e8202014-10-10 17:01:33 -0700735 private final class SendAdvertisementTask implements Runnable {
736
737 @Override
738 public void run() {
739 if (Thread.currentThread().isInterrupted()) {
Yuta HIGUCHI1a012722014-11-20 15:21:41 -0800740 log.debug("Interrupted, quitting");
Madan Jampania97e8202014-10-10 17:01:33 -0700741 return;
742 }
743
744 try {
745 final NodeId self = clusterService.getLocalNode().id();
746 Set<ControllerNode> nodes = clusterService.getNodes();
747
748 ImmutableList<NodeId> nodeIds = FluentIterable.from(nodes)
749 .transform(toNodeId())
750 .toList();
751
752 if (nodeIds.size() == 1 && nodeIds.get(0).equals(self)) {
Yuta HIGUCHIfaf9e1c2014-11-20 00:31:29 -0800753 log.trace("No other peers in the cluster.");
Madan Jampania97e8202014-10-10 17:01:33 -0700754 return;
755 }
756
757 NodeId peer;
758 do {
759 int idx = RandomUtils.nextInt(0, nodeIds.size());
760 peer = nodeIds.get(idx);
761 } while (peer.equals(self));
762
763 LinkAntiEntropyAdvertisement ad = createAdvertisement();
764
765 if (Thread.currentThread().isInterrupted()) {
Yuta HIGUCHI1a012722014-11-20 15:21:41 -0800766 log.debug("Interrupted, quitting");
Madan Jampania97e8202014-10-10 17:01:33 -0700767 return;
768 }
769
770 try {
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700771 unicastMessage(peer, LINK_ANTI_ENTROPY_ADVERTISEMENT, ad);
772 } catch (IOException e) {
773 log.debug("Failed to send anti-entropy advertisement to {}", peer);
Madan Jampania97e8202014-10-10 17:01:33 -0700774 return;
775 }
776 } catch (Exception e) {
777 // catch all Exception to avoid Scheduled task being suppressed.
778 log.error("Exception thrown while sending advertisement", e);
779 }
780 }
781 }
782
783 private LinkAntiEntropyAdvertisement createAdvertisement() {
784 final NodeId self = clusterService.getLocalNode().id();
785
786 Map<LinkFragmentId, Timestamp> linkTimestamps = new HashMap<>(linkDescs.size());
787 Map<LinkKey, Timestamp> linkTombstones = new HashMap<>(removedLinks.size());
788
Yuta HIGUCHIb6cfac32014-11-25 13:37:27 -0800789 linkDescs.forEach((linkKey, linkDesc) -> {
Madan Jampania97e8202014-10-10 17:01:33 -0700790 synchronized (linkDesc) {
791 for (Map.Entry<ProviderId, Timestamped<LinkDescription>> e : linkDesc.entrySet()) {
792 linkTimestamps.put(new LinkFragmentId(linkKey, e.getKey()), e.getValue().timestamp());
793 }
794 }
Yuta HIGUCHIb6cfac32014-11-25 13:37:27 -0800795 });
Madan Jampania97e8202014-10-10 17:01:33 -0700796
797 linkTombstones.putAll(removedLinks);
798
799 return new LinkAntiEntropyAdvertisement(self, linkTimestamps, linkTombstones);
800 }
801
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700802 private void handleAntiEntropyAdvertisement(LinkAntiEntropyAdvertisement ad) {
Madan Jampania97e8202014-10-10 17:01:33 -0700803
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700804 final NodeId sender = ad.sender();
805 boolean localOutdated = false;
Madan Jampania97e8202014-10-10 17:01:33 -0700806
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700807 for (Entry<LinkKey, Map<ProviderId, Timestamped<LinkDescription>>>
808 l : linkDescs.entrySet()) {
Madan Jampania97e8202014-10-10 17:01:33 -0700809
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700810 final LinkKey key = l.getKey();
811 final Map<ProviderId, Timestamped<LinkDescription>> link = l.getValue();
812 synchronized (link) {
813 Timestamp localLatest = removedLinks.get(key);
Madan Jampania97e8202014-10-10 17:01:33 -0700814
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700815 for (Entry<ProviderId, Timestamped<LinkDescription>> p : link.entrySet()) {
816 final ProviderId providerId = p.getKey();
817 final Timestamped<LinkDescription> pDesc = p.getValue();
Madan Jampania97e8202014-10-10 17:01:33 -0700818
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700819 final LinkFragmentId fragId = new LinkFragmentId(key, providerId);
820 // remote
821 Timestamp remoteTimestamp = ad.linkTimestamps().get(fragId);
822 if (remoteTimestamp == null) {
823 remoteTimestamp = ad.linkTombstones().get(key);
824 }
825 if (remoteTimestamp == null ||
Jonathan Hart403ea932015-02-20 16:23:00 -0800826 pDesc.isNewerThan(remoteTimestamp)) {
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700827 // I have more recent link description. update peer.
828 notifyPeer(sender, new InternalLinkEvent(providerId, pDesc));
829 } else {
830 final Timestamp remoteLive = ad.linkTimestamps().get(fragId);
831 if (remoteLive != null &&
832 remoteLive.compareTo(pDesc.timestamp()) > 0) {
833 // I have something outdated
834 localOutdated = true;
835 }
836 }
837
838 // search local latest along the way
839 if (localLatest == null ||
Jonathan Hart403ea932015-02-20 16:23:00 -0800840 pDesc.isNewerThan(localLatest)) {
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700841 localLatest = pDesc.timestamp();
842 }
843 }
844 // Tests if remote remove is more recent then local latest.
845 final Timestamp remoteRemove = ad.linkTombstones().get(key);
846 if (remoteRemove != null) {
847 if (localLatest != null &&
848 localLatest.compareTo(remoteRemove) < 0) {
849 // remote remove is more recent
850 notifyDelegateIfNotNull(removeLinkInternal(key, remoteRemove));
851 }
852 }
Madan Jampania97e8202014-10-10 17:01:33 -0700853 }
854 }
855
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700856 // populate remove info if not known locally
857 for (Entry<LinkKey, Timestamp> remoteRm : ad.linkTombstones().entrySet()) {
858 final LinkKey key = remoteRm.getKey();
859 final Timestamp remoteRemove = remoteRm.getValue();
860 // relying on removeLinkInternal to ignore stale info
861 notifyDelegateIfNotNull(removeLinkInternal(key, remoteRemove));
862 }
Madan Jampania97e8202014-10-10 17:01:33 -0700863
Yuta HIGUCHI2fe46a22014-10-15 22:51:02 -0700864 if (localOutdated) {
865 // send back advertisement to speed up convergence
866 try {
867 unicastMessage(sender, LINK_ANTI_ENTROPY_ADVERTISEMENT,
868 createAdvertisement());
869 } catch (IOException e) {
870 log.debug("Failed to send back active advertisement");
Madan Jampania97e8202014-10-10 17:01:33 -0700871 }
872 }
873 }
874
Yuta HIGUCHI80d56592014-11-25 15:11:13 -0800875 private final class InternalLinkEventListener
876 implements ClusterMessageHandler {
Madan Jampani2ff05592014-10-10 15:42:47 -0700877 @Override
878 public void handle(ClusterMessage message) {
879
Yuta HIGUCHIc01d2aa2014-10-19 01:19:34 -0700880 log.trace("Received link event from peer: {}", message.sender());
Sho SHIMIZU5eb79c52015-09-29 14:32:49 -0700881 InternalLinkEvent event = SERIALIZER.decode(message.payload());
Madan Jampani2ff05592014-10-10 15:42:47 -0700882
883 ProviderId providerId = event.providerId();
884 Timestamped<LinkDescription> linkDescription = event.linkDescription();
885
Madan Jampani2af244a2015-02-22 13:12:01 -0800886 try {
887 notifyDelegateIfNotNull(createOrUpdateLinkInternal(providerId, linkDescription));
888 } catch (Exception e) {
889 log.warn("Exception thrown handling link event", e);
890 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700891 }
892 }
893
Yuta HIGUCHI80d56592014-11-25 15:11:13 -0800894 private final class InternalLinkRemovedEventListener
895 implements ClusterMessageHandler {
Madan Jampani2ff05592014-10-10 15:42:47 -0700896 @Override
897 public void handle(ClusterMessage message) {
898
Yuta HIGUCHIc01d2aa2014-10-19 01:19:34 -0700899 log.trace("Received link removed event from peer: {}", message.sender());
Sho SHIMIZU5eb79c52015-09-29 14:32:49 -0700900 InternalLinkRemovedEvent event = SERIALIZER.decode(message.payload());
Madan Jampani2ff05592014-10-10 15:42:47 -0700901
902 LinkKey linkKey = event.linkKey();
903 Timestamp timestamp = event.timestamp();
904
Madan Jampani2af244a2015-02-22 13:12:01 -0800905 try {
906 notifyDelegateIfNotNull(removeLinkInternal(linkKey, timestamp));
907 } catch (Exception e) {
908 log.warn("Exception thrown handling link removed", e);
909 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700910 }
911 }
Madan Jampania97e8202014-10-10 17:01:33 -0700912
Yuta HIGUCHI80d56592014-11-25 15:11:13 -0800913 private final class InternalLinkAntiEntropyAdvertisementListener
914 implements ClusterMessageHandler {
Madan Jampania97e8202014-10-10 17:01:33 -0700915
916 @Override
917 public void handle(ClusterMessage message) {
Yuta HIGUCHIfaf9e1c2014-11-20 00:31:29 -0800918 log.trace("Received Link Anti-Entropy advertisement from peer: {}", message.sender());
Madan Jampania97e8202014-10-10 17:01:33 -0700919 LinkAntiEntropyAdvertisement advertisement = SERIALIZER.decode(message.payload());
Madan Jampani2af244a2015-02-22 13:12:01 -0800920 try {
921 handleAntiEntropyAdvertisement(advertisement);
922 } catch (Exception e) {
923 log.warn("Exception thrown while handling Link advertisements", e);
924 throw e;
925 }
Madan Jampania97e8202014-10-10 17:01:33 -0700926 }
927 }
Marc De Leenheerb473b9d2015-02-06 15:21:03 -0800928
929 private final class LinkInjectedEventListener
930 implements ClusterMessageHandler {
931 @Override
932 public void handle(ClusterMessage message) {
933
934 log.trace("Received injected link event from peer: {}", message.sender());
935 LinkInjectedEvent linkInjectedEvent = SERIALIZER.decode(message.payload());
936
937 ProviderId providerId = linkInjectedEvent.providerId();
938 LinkDescription linkDescription = linkInjectedEvent.linkDescription();
939
HIGUCHI Yuta0b4d2982015-02-27 23:18:52 -0800940 final DeviceId deviceId = linkDescription.dst().deviceId();
941 if (!deviceClockService.isTimestampAvailable(deviceId)) {
942 // workaround for ONOS-1208
943 log.warn("Not ready to accept update. Dropping {}", linkDescription);
944 return;
945 }
946
Madan Jampani2af244a2015-02-22 13:12:01 -0800947 try {
948 createOrUpdateLink(providerId, linkDescription);
949 } catch (Exception e) {
950 log.warn("Exception thrown while handling link injected event", e);
951 }
Marc De Leenheerb473b9d2015-02-06 15:21:03 -0800952 }
953 }
Madan Jampani2ff05592014-10-10 15:42:47 -0700954}