blob: 7bbad93d5a03bb6cff1ecf803ca955fe5db185d4 [file] [log] [blame]
Madan Jampani2ff05592014-10-10 15:42:47 -07001package org.onlab.onos.store.link.impl;
2
3import com.google.common.base.Function;
4import com.google.common.base.Predicate;
5import com.google.common.collect.FluentIterable;
6import com.google.common.collect.HashMultimap;
7import com.google.common.collect.Maps;
8import com.google.common.collect.SetMultimap;
9
10import org.apache.commons.lang3.concurrent.ConcurrentUtils;
11import org.apache.felix.scr.annotations.Activate;
12import org.apache.felix.scr.annotations.Component;
13import org.apache.felix.scr.annotations.Deactivate;
14import org.apache.felix.scr.annotations.Reference;
15import org.apache.felix.scr.annotations.ReferenceCardinality;
16import org.apache.felix.scr.annotations.Service;
17import org.onlab.onos.cluster.ClusterService;
18import org.onlab.onos.net.AnnotationsUtil;
19import org.onlab.onos.net.ConnectPoint;
20import org.onlab.onos.net.DefaultAnnotations;
21import org.onlab.onos.net.DefaultLink;
22import org.onlab.onos.net.DeviceId;
23import org.onlab.onos.net.Link;
24import org.onlab.onos.net.SparseAnnotations;
25import org.onlab.onos.net.Link.Type;
26import org.onlab.onos.net.LinkKey;
27import org.onlab.onos.net.Provided;
28import org.onlab.onos.net.link.DefaultLinkDescription;
29import org.onlab.onos.net.link.LinkDescription;
30import org.onlab.onos.net.link.LinkEvent;
31import org.onlab.onos.net.link.LinkStore;
32import org.onlab.onos.net.link.LinkStoreDelegate;
33import org.onlab.onos.net.provider.ProviderId;
34import org.onlab.onos.store.AbstractStore;
35import org.onlab.onos.store.ClockService;
36import org.onlab.onos.store.Timestamp;
37import org.onlab.onos.store.cluster.messaging.ClusterCommunicationService;
38import org.onlab.onos.store.cluster.messaging.ClusterMessage;
39import org.onlab.onos.store.cluster.messaging.ClusterMessageHandler;
40import org.onlab.onos.store.cluster.messaging.MessageSubject;
41import org.onlab.onos.store.common.impl.Timestamped;
42import org.onlab.onos.store.serializers.DistributedStoreSerializers;
43import org.onlab.onos.store.serializers.KryoSerializer;
44import org.onlab.util.KryoPool;
45import org.onlab.util.NewConcurrentHashMap;
46import org.slf4j.Logger;
47
48import java.io.IOException;
49import java.util.Collections;
50import java.util.HashSet;
51import java.util.Map;
52import java.util.Set;
53import java.util.Map.Entry;
54import java.util.concurrent.ConcurrentHashMap;
55import java.util.concurrent.ConcurrentMap;
56
57import static org.onlab.onos.net.DefaultAnnotations.union;
58import static org.onlab.onos.net.DefaultAnnotations.merge;
59import static org.onlab.onos.net.Link.Type.DIRECT;
60import static org.onlab.onos.net.Link.Type.INDIRECT;
61import static org.onlab.onos.net.link.LinkEvent.Type.*;
62import static org.slf4j.LoggerFactory.getLogger;
63import static com.google.common.collect.Multimaps.synchronizedSetMultimap;
64import static com.google.common.base.Predicates.notNull;
65
66/**
67 * Manages inventory of infrastructure links in distributed data store
68 * that uses optimistic replication and gossip based techniques.
69 */
70@Component(immediate = true)
71@Service
72public class GossipLinkStore
73 extends AbstractStore<LinkEvent, LinkStoreDelegate>
74 implements LinkStore {
75
76 private final Logger log = getLogger(getClass());
77
78 // Link inventory
79 private final ConcurrentMap<LinkKey, ConcurrentMap<ProviderId, Timestamped<LinkDescription>>> linkDescs =
80 new ConcurrentHashMap<>();
81
82 // Link instance cache
83 private final ConcurrentMap<LinkKey, Link> links = new ConcurrentHashMap<>();
84
85 // Egress and ingress link sets
86 private final SetMultimap<DeviceId, LinkKey> srcLinks = createSynchronizedHashMultiMap();
87 private final SetMultimap<DeviceId, LinkKey> dstLinks = createSynchronizedHashMultiMap();
88
89 // Remove links
90 private final Map<LinkKey, Timestamp> removedLinks = Maps.newHashMap();
91
92 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
93 protected ClockService clockService;
94
95 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
96 protected ClusterCommunicationService clusterCommunicator;
97
98 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
99 protected ClusterService clusterService;
100
101 private static final KryoSerializer SERIALIZER = new KryoSerializer() {
102 @Override
103 protected void setupKryoPool() {
104 serializerPool = KryoPool.newBuilder()
105 .register(DistributedStoreSerializers.COMMON)
106 .register(InternalLinkEvent.class)
107 .register(InternalLinkRemovedEvent.class)
108 .build()
109 .populate(1);
110 }
111 };
112
113 @Activate
114 public void activate() {
115
116 clusterCommunicator.addSubscriber(
117 GossipLinkStoreMessageSubjects.LINK_UPDATE, new InternalLinkEventListener());
118 clusterCommunicator.addSubscriber(
119 GossipLinkStoreMessageSubjects.LINK_REMOVED, new InternalLinkRemovedEventListener());
120
121 log.info("Started");
122 }
123
124 @Deactivate
125 public void deactivate() {
126 linkDescs.clear();
127 links.clear();
128 srcLinks.clear();
129 dstLinks.clear();
130 log.info("Stopped");
131 }
132
133 @Override
134 public int getLinkCount() {
135 return links.size();
136 }
137
138 @Override
139 public Iterable<Link> getLinks() {
140 return Collections.unmodifiableCollection(links.values());
141 }
142
143 @Override
144 public Set<Link> getDeviceEgressLinks(DeviceId deviceId) {
145 // lock for iteration
146 synchronized (srcLinks) {
147 return FluentIterable.from(srcLinks.get(deviceId))
148 .transform(lookupLink())
149 .filter(notNull())
150 .toSet();
151 }
152 }
153
154 @Override
155 public Set<Link> getDeviceIngressLinks(DeviceId deviceId) {
156 // lock for iteration
157 synchronized (dstLinks) {
158 return FluentIterable.from(dstLinks.get(deviceId))
159 .transform(lookupLink())
160 .filter(notNull())
161 .toSet();
162 }
163 }
164
165 @Override
166 public Link getLink(ConnectPoint src, ConnectPoint dst) {
167 return links.get(new LinkKey(src, dst));
168 }
169
170 @Override
171 public Set<Link> getEgressLinks(ConnectPoint src) {
172 Set<Link> egress = new HashSet<>();
173 for (LinkKey linkKey : srcLinks.get(src.deviceId())) {
174 if (linkKey.src().equals(src)) {
175 egress.add(links.get(linkKey));
176 }
177 }
178 return egress;
179 }
180
181 @Override
182 public Set<Link> getIngressLinks(ConnectPoint dst) {
183 Set<Link> ingress = new HashSet<>();
184 for (LinkKey linkKey : dstLinks.get(dst.deviceId())) {
185 if (linkKey.dst().equals(dst)) {
186 ingress.add(links.get(linkKey));
187 }
188 }
189 return ingress;
190 }
191
192 @Override
193 public LinkEvent createOrUpdateLink(ProviderId providerId,
194 LinkDescription linkDescription) {
195
196 DeviceId dstDeviceId = linkDescription.dst().deviceId();
197 Timestamp newTimestamp = clockService.getTimestamp(dstDeviceId);
198
199 final Timestamped<LinkDescription> deltaDesc = new Timestamped<>(linkDescription, newTimestamp);
200
201 LinkEvent event = createOrUpdateLinkInternal(providerId, deltaDesc);
202
203 if (event != null) {
204 log.info("Notifying peers of a link update topology event from providerId: "
205 + "{} between src: {} and dst: {}",
206 providerId, linkDescription.src(), linkDescription.dst());
207 try {
208 notifyPeers(new InternalLinkEvent(providerId, deltaDesc));
209 } catch (IOException e) {
210 log.info("Failed to notify peers of a link update topology event from providerId: "
211 + "{} between src: {} and dst: {}",
212 providerId, linkDescription.src(), linkDescription.dst());
213 }
214 }
215 return event;
216 }
217
218 private LinkEvent createOrUpdateLinkInternal(
219 ProviderId providerId,
220 Timestamped<LinkDescription> linkDescription) {
221
222 LinkKey key = new LinkKey(linkDescription.value().src(), linkDescription.value().dst());
223 ConcurrentMap<ProviderId, Timestamped<LinkDescription>> descs = getLinkDescriptions(key);
224
225 synchronized (descs) {
226 // if the link was previously removed, we should proceed if and
227 // only if this request is more recent.
228 Timestamp linkRemovedTimestamp = removedLinks.get(key);
229 if (linkRemovedTimestamp != null) {
230 if (linkDescription.isNewer(linkRemovedTimestamp)) {
231 removedLinks.remove(key);
232 } else {
233 return null;
234 }
235 }
236
237 final Link oldLink = links.get(key);
238 // update description
239 createOrUpdateLinkDescription(descs, providerId, linkDescription);
240 final Link newLink = composeLink(descs);
241 if (oldLink == null) {
242 return createLink(key, newLink);
243 }
244 return updateLink(key, oldLink, newLink);
245 }
246 }
247
248 // Guarded by linkDescs value (=locking each Link)
249 private Timestamped<LinkDescription> createOrUpdateLinkDescription(
250 ConcurrentMap<ProviderId, Timestamped<LinkDescription>> existingLinkDescriptions,
251 ProviderId providerId,
252 Timestamped<LinkDescription> linkDescription) {
253
254 // merge existing attributes and merge
255 Timestamped<LinkDescription> existingLinkDescription = existingLinkDescriptions.get(providerId);
256 if (existingLinkDescription != null && existingLinkDescription.isNewer(linkDescription)) {
257 return null;
258 }
259 Timestamped<LinkDescription> newLinkDescription = linkDescription;
260 if (existingLinkDescription != null) {
261 SparseAnnotations merged = union(existingLinkDescription.value().annotations(),
262 linkDescription.value().annotations());
263 newLinkDescription = new Timestamped<LinkDescription>(
264 new DefaultLinkDescription(
265 linkDescription.value().src(),
266 linkDescription.value().dst(),
267 linkDescription.value().type(), merged),
268 linkDescription.timestamp());
269 }
270 return existingLinkDescriptions.put(providerId, newLinkDescription);
271 }
272
273 // Creates and stores the link and returns the appropriate event.
274 // Guarded by linkDescs value (=locking each Link)
275 private LinkEvent createLink(LinkKey key, Link newLink) {
276
277 if (newLink.providerId().isAncillary()) {
278 // TODO: revisit ancillary only Link handling
279
280 // currently treating ancillary only as down (not visible outside)
281 return null;
282 }
283
284 links.put(key, newLink);
285 srcLinks.put(newLink.src().deviceId(), key);
286 dstLinks.put(newLink.dst().deviceId(), key);
287 return new LinkEvent(LINK_ADDED, newLink);
288 }
289
290 // Updates, if necessary the specified link and returns the appropriate event.
291 // Guarded by linkDescs value (=locking each Link)
292 private LinkEvent updateLink(LinkKey key, Link oldLink, Link newLink) {
293
294 if (newLink.providerId().isAncillary()) {
295 // TODO: revisit ancillary only Link handling
296
297 // currently treating ancillary only as down (not visible outside)
298 return null;
299 }
300
301 if ((oldLink.type() == INDIRECT && newLink.type() == DIRECT) ||
302 !AnnotationsUtil.isEqual(oldLink.annotations(), newLink.annotations())) {
303
304 links.put(key, newLink);
305 // strictly speaking following can be ommitted
306 srcLinks.put(oldLink.src().deviceId(), key);
307 dstLinks.put(oldLink.dst().deviceId(), key);
308 return new LinkEvent(LINK_UPDATED, newLink);
309 }
310 return null;
311 }
312
313 @Override
314 public LinkEvent removeLink(ConnectPoint src, ConnectPoint dst) {
315 final LinkKey key = new LinkKey(src, dst);
316
317 DeviceId dstDeviceId = dst.deviceId();
318 Timestamp timestamp = clockService.getTimestamp(dstDeviceId);
319
320 LinkEvent event = removeLinkInternal(key, timestamp);
321
322 if (event != null) {
323 log.info("Notifying peers of a link removed topology event for a link "
324 + "between src: {} and dst: {}", src, dst);
325 try {
326 notifyPeers(new InternalLinkRemovedEvent(key, timestamp));
327 } catch (IOException e) {
328 log.error("Failed to notify peers of a link removed topology event for a link "
329 + "between src: {} and dst: {}", src, dst);
330 }
331 }
332 return event;
333 }
334
335 private LinkEvent removeLinkInternal(LinkKey key, Timestamp timestamp) {
336 ConcurrentMap<ProviderId, Timestamped<LinkDescription>> linkDescriptions =
337 getLinkDescriptions(key);
338 synchronized (linkDescriptions) {
339 // accept removal request if given timestamp is newer than
340 // the latest Timestamp from Primary provider
341 ProviderId primaryProviderId = pickPrimaryProviderId(linkDescriptions);
342 if (linkDescriptions.get(primaryProviderId).isNewer(timestamp)) {
343 return null;
344 }
345 removedLinks.put(key, timestamp);
346 Link link = links.remove(key);
347 linkDescriptions.clear();
348 if (link != null) {
349 srcLinks.remove(link.src().deviceId(), key);
350 dstLinks.remove(link.dst().deviceId(), key);
351 return new LinkEvent(LINK_REMOVED, link);
352 }
353 return null;
354 }
355 }
356
357 private static <K, V> SetMultimap<K, V> createSynchronizedHashMultiMap() {
358 return synchronizedSetMultimap(HashMultimap.<K, V>create());
359 }
360
361 /**
362 * @return primary ProviderID, or randomly chosen one if none exists
363 */
364 private ProviderId pickPrimaryProviderId(
365 ConcurrentMap<ProviderId, Timestamped<LinkDescription>> providerDescs) {
366
367 ProviderId fallBackPrimary = null;
368 for (Entry<ProviderId, Timestamped<LinkDescription>> e : providerDescs.entrySet()) {
369 if (!e.getKey().isAncillary()) {
370 return e.getKey();
371 } else if (fallBackPrimary == null) {
372 // pick randomly as a fallback in case there is no primary
373 fallBackPrimary = e.getKey();
374 }
375 }
376 return fallBackPrimary;
377 }
378
379 private Link composeLink(ConcurrentMap<ProviderId, Timestamped<LinkDescription>> linkDescriptions) {
380 ProviderId primaryProviderId = pickPrimaryProviderId(linkDescriptions);
381 Timestamped<LinkDescription> base = linkDescriptions.get(primaryProviderId);
382
383 ConnectPoint src = base.value().src();
384 ConnectPoint dst = base.value().dst();
385 Type type = base.value().type();
386 DefaultAnnotations annotations = DefaultAnnotations.builder().build();
387 annotations = merge(annotations, base.value().annotations());
388
389 for (Entry<ProviderId, Timestamped<LinkDescription>> e : linkDescriptions.entrySet()) {
390 if (primaryProviderId.equals(e.getKey())) {
391 continue;
392 }
393
394 // TODO: should keep track of Description timestamp
395 // and only merge conflicting keys when timestamp is newer
396 // Currently assuming there will never be a key conflict between
397 // providers
398
399 // annotation merging. not so efficient, should revisit later
400 annotations = merge(annotations, e.getValue().value().annotations());
401 }
402
403 return new DefaultLink(primaryProviderId , src, dst, type, annotations);
404 }
405
406 private ConcurrentMap<ProviderId, Timestamped<LinkDescription>> getLinkDescriptions(LinkKey key) {
407 return ConcurrentUtils.createIfAbsentUnchecked(linkDescs, key,
408 NewConcurrentHashMap.<ProviderId, Timestamped<LinkDescription>>ifNeeded());
409 }
410
411 private final Function<LinkKey, Link> lookupLink = new LookupLink();
412 private Function<LinkKey, Link> lookupLink() {
413 return lookupLink;
414 }
415
416 private final class LookupLink implements Function<LinkKey, Link> {
417 @Override
418 public Link apply(LinkKey input) {
419 return links.get(input);
420 }
421 }
422
423 private static final Predicate<Provided> IS_PRIMARY = new IsPrimary();
424 private static final Predicate<Provided> isPrimary() {
425 return IS_PRIMARY;
426 }
427
428 private static final class IsPrimary implements Predicate<Provided> {
429
430 @Override
431 public boolean apply(Provided input) {
432 return !input.providerId().isAncillary();
433 }
434 }
435
436 private void notifyDelegateIfNotNull(LinkEvent event) {
437 if (event != null) {
438 notifyDelegate(event);
439 }
440 }
441
442 // TODO: should we be throwing exception?
443 private void broadcastMessage(MessageSubject subject, Object event) throws IOException {
444 ClusterMessage message = new ClusterMessage(
445 clusterService.getLocalNode().id(),
446 subject,
447 SERIALIZER.encode(event));
448 clusterCommunicator.broadcast(message);
449 }
450
451 private void notifyPeers(InternalLinkEvent event) throws IOException {
452 broadcastMessage(GossipLinkStoreMessageSubjects.LINK_UPDATE, event);
453 }
454
455 private void notifyPeers(InternalLinkRemovedEvent event) throws IOException {
456 broadcastMessage(GossipLinkStoreMessageSubjects.LINK_REMOVED, event);
457 }
458
459 private class InternalLinkEventListener implements ClusterMessageHandler {
460 @Override
461 public void handle(ClusterMessage message) {
462
463 log.info("Received link event from peer: {}", message.sender());
464 InternalLinkEvent event = (InternalLinkEvent) SERIALIZER.decode(message.payload());
465
466 ProviderId providerId = event.providerId();
467 Timestamped<LinkDescription> linkDescription = event.linkDescription();
468
469 notifyDelegateIfNotNull(createOrUpdateLinkInternal(providerId, linkDescription));
470 }
471 }
472
473 private class InternalLinkRemovedEventListener implements ClusterMessageHandler {
474 @Override
475 public void handle(ClusterMessage message) {
476
477 log.info("Received link removed event from peer: {}", message.sender());
478 InternalLinkRemovedEvent event = (InternalLinkRemovedEvent) SERIALIZER.decode(message.payload());
479
480 LinkKey linkKey = event.linkKey();
481 Timestamp timestamp = event.timestamp();
482
483 notifyDelegateIfNotNull(removeLinkInternal(linkKey, timestamp));
484 }
485 }
486}