7ca35aa66ba7d76cb0fdb74c576bc13c997c2a36
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / main / java / org / opendaylight / controller / md / sal / binding / compat / ListenerMapGeneration.java
1 /**
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.md.sal.binding.compat;
9
10 import java.util.Arrays;
11 import java.util.Collection;
12 import java.util.HashSet;
13 import java.util.Set;
14
15 import java.util.stream.Collectors;
16 import org.opendaylight.yangtools.yang.binding.Notification;
17
18 import com.google.common.cache.CacheBuilder;
19 import com.google.common.cache.CacheLoader;
20 import com.google.common.cache.LoadingCache;
21 import com.google.common.collect.ImmutableMultimap;
22 import com.google.common.collect.ImmutableSet;
23 import com.google.common.collect.Multimap;
24
25 /**
26  * An immutable view of the current generation of listeners.
27  */
28 final class ListenerMapGeneration {
29     private static final int CACHE_MAX_ENTRIES = 1000;
30
31     /**
32      * Constant map of notification type to subscribed listeners.
33      */
34     private final Multimap<Class<? extends Notification>, NotificationListenerRegistration<?>> typeToListeners;
35
36     /**
37      * Dynamic cache of notification implementation to matching listeners. This cache loads entries based on
38      * the contents of the {@link #typeToListeners} map.
39      */
40     private final LoadingCache<Class<?>, Iterable<NotificationListenerRegistration<?>>> implementationToListeners =
41             CacheBuilder.newBuilder()
42             .weakKeys()
43             .maximumSize(CACHE_MAX_ENTRIES)
44             .build(new CacheLoader<Class<?>, Iterable<NotificationListenerRegistration<?>>>() {
45                 @Override
46                 public Iterable<NotificationListenerRegistration<?>> load(final Class<?> key) {
47                     final Set<NotificationListenerRegistration<?>> regs = new HashSet<>();
48
49                     for (final Class<?> type : getNotificationTypes(key)) {
50                         @SuppressWarnings("unchecked")
51                         final Collection<NotificationListenerRegistration<?>> l = typeToListeners.get((Class<? extends Notification>) type);
52                         if (l != null) {
53                             regs.addAll(l);
54                         }
55                     }
56
57                     return ImmutableSet.copyOf(regs);
58                 }
59             });
60
61     ListenerMapGeneration() {
62         typeToListeners = ImmutableMultimap.of();
63     }
64
65     ListenerMapGeneration(final Multimap<Class<? extends Notification>, NotificationListenerRegistration<?>> listeners) {
66         this.typeToListeners = ImmutableMultimap.copyOf(listeners);
67     }
68
69     /**
70      * Current listeners. Exposed for creating the next generation.
71      *
72      * @return Current type-to-listener map.
73      */
74     Multimap<Class<? extends Notification>, NotificationListenerRegistration<?>> getListeners() {
75         return typeToListeners;
76     }
77
78     /**
79      * Look up the listeners which need to see this notification delivered.
80      *
81      * @param notification Notification object
82      * @return Iterable of listeners, guaranteed to be nonnull.
83      */
84     public Iterable<NotificationListenerRegistration<?>> listenersFor(final Notification notification) {
85         // Safe to use, as our loader does not throw checked exceptions
86         return implementationToListeners.getUnchecked(notification.getClass());
87     }
88
89     public Iterable<Class<? extends Notification>> getKnownTypes() {
90         return typeToListeners.keySet();
91     }
92
93     private static Iterable<Class<?>> getNotificationTypes(final Class<?> cls) {
94         final Class<?>[] ifaces = cls.getInterfaces();
95         return Arrays.stream(ifaces)
96                 .filter(input -> !Notification.class.equals(input) && Notification.class.isAssignableFrom(input))
97                 .collect(Collectors.toList());
98     }
99 }