3a392e58cc63f7717e285a41a279406584e7bdc8
[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 com.google.common.cache.CacheBuilder;
11 import com.google.common.cache.CacheLoader;
12 import com.google.common.cache.LoadingCache;
13 import com.google.common.collect.ImmutableMultimap;
14 import com.google.common.collect.ImmutableSet;
15 import com.google.common.collect.Multimap;
16 import java.util.Arrays;
17 import java.util.Collection;
18 import java.util.HashSet;
19 import java.util.Set;
20 import java.util.stream.Collectors;
21 import org.opendaylight.yangtools.yang.binding.Notification;
22
23 /**
24  * An immutable view of the current generation of listeners.
25  */
26 @Deprecated
27 final class ListenerMapGeneration {
28     private static final int CACHE_MAX_ENTRIES = 1000;
29
30     /**
31      * Constant map of notification type to subscribed listeners.
32      */
33     private final Multimap<Class<? extends Notification>, NotificationListenerRegistration<?>> typeToListeners;
34
35     /**
36      * Dynamic cache of notification implementation to matching listeners. This cache loads entries based on
37      * the contents of the {@link #typeToListeners} map.
38      */
39     private final LoadingCache<Class<?>, Iterable<NotificationListenerRegistration<?>>> implementationToListeners =
40             CacheBuilder.newBuilder()
41             .weakKeys()
42             .maximumSize(CACHE_MAX_ENTRIES)
43             .build(new CacheLoader<Class<?>, Iterable<NotificationListenerRegistration<?>>>() {
44                 @Override
45                 public Iterable<NotificationListenerRegistration<?>> load(final Class<?> key) {
46                     final Set<NotificationListenerRegistration<?>> regs = new HashSet<>();
47
48                     for (final Class<?> type : getNotificationTypes(key)) {
49                         @SuppressWarnings("unchecked")
50                         final Collection<NotificationListenerRegistration<?>> l =
51                                 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>,
66             NotificationListenerRegistration<?>> listeners) {
67         this.typeToListeners = ImmutableMultimap.copyOf(listeners);
68     }
69
70     /**
71      * Current listeners. Exposed for creating the next generation.
72      *
73      * @return Current type-to-listener map.
74      */
75     Multimap<Class<? extends Notification>, NotificationListenerRegistration<?>> getListeners() {
76         return typeToListeners;
77     }
78
79     /**
80      * Look up the listeners which need to see this notification delivered.
81      *
82      * @param notification Notification object
83      * @return Iterable of listeners, guaranteed to be nonnull.
84      */
85     public Iterable<NotificationListenerRegistration<?>> listenersFor(final Notification notification) {
86         // Safe to use, as our loader does not throw checked exceptions
87         return implementationToListeners.getUnchecked(notification.getClass());
88     }
89
90     public Iterable<Class<? extends Notification>> getKnownTypes() {
91         return typeToListeners.keySet();
92     }
93
94     private static Iterable<Class<?>> getNotificationTypes(final Class<?> cls) {
95         final Class<?>[] ifaces = cls.getInterfaces();
96         return Arrays.stream(ifaces)
97                 .filter(input -> !Notification.class.equals(input) && Notification.class.isAssignableFrom(input))
98                 .collect(Collectors.toList());
99     }
100 }