1 package org.opendaylight.controller.sal.streams.listeners;
4 import java.util.concurrent.ConcurrentHashMap;
5 import java.util.concurrent.locks.Lock;
6 import java.util.concurrent.locks.ReentrantLock;
8 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
11 * {@link Notificator} is responsible to create, remove and find {@link ListenerAdapter} listener.
13 public class Notificator {
15 private static Map<String, ListenerAdapter> listenersByStreamName = new ConcurrentHashMap<>();
16 private static Map<InstanceIdentifier, ListenerAdapter> listenersByInstanceIdentifier = new ConcurrentHashMap<>();
17 private static final Lock lock = new ReentrantLock();
19 private Notificator() {
23 * Gets {@link ListenerAdapter} specified by stream name.
26 * The name of the stream.
27 * @return {@link ListenerAdapter} specified by stream name.
29 public static ListenerAdapter getListenerFor(String streamName) {
30 return listenersByStreamName.get(streamName);
34 * Gets {@link ListenerAdapter} listener specified by
35 * {@link InstanceIdentifier} path.
38 * Path to data in data repository.
39 * @return ListenerAdapter
41 public static ListenerAdapter getListenerFor(InstanceIdentifier path) {
42 return listenersByInstanceIdentifier.get(path);
46 * Checks if the listener specified by {@link InstanceIdentifier} path
50 * Path to data in data repository.
51 * @return True if the listener exist, false otherwise.
53 public static boolean existListenerFor(InstanceIdentifier path) {
54 return listenersByInstanceIdentifier.containsKey(path);
58 * Creates new {@link ListenerAdapter} listener from
59 * {@link InstanceIdentifier} path and stream name.
62 * Path to data in data repository.
64 * The name of the stream.
65 * @return New {@link ListenerAdapter} listener from
66 * {@link InstanceIdentifier} path and stream name.
68 public static ListenerAdapter createListener(InstanceIdentifier path,
70 ListenerAdapter listener = new ListenerAdapter(path, streamName);
73 listenersByInstanceIdentifier.put(path, listener);
74 listenersByStreamName.put(streamName, listener);
82 * Looks for listener determined by {@link InstanceIdentifier} path and
88 public static void removeListener(InstanceIdentifier path) {
89 ListenerAdapter listener = listenersByInstanceIdentifier.get(path);
90 deleteListener(listener);
94 * Creates String representation of stream name from URI. Removes slash from
95 * URI in start and end position.
98 * URI for creation stream name.
99 * @return String representation of stream name.
101 public static String createStreamNameFromUri(String uri) {
106 if (result.startsWith("/")) {
107 result = result.substring(1);
109 if (result.endsWith("/")) {
110 result = result.substring(0, result.length());
116 * Removes all listeners.
118 public static void removeAllListeners() {
119 for (ListenerAdapter listener : listenersByInstanceIdentifier.values()) {
122 } catch (Exception e) {
127 listenersByStreamName = new ConcurrentHashMap<>();
128 listenersByInstanceIdentifier = new ConcurrentHashMap<>();
135 * Checks if listener has at least one subscriber. In case it has any, delete
141 public static void removeListenerIfNoSubscriberExists(
142 ListenerAdapter listener) {
143 if (!listener.hasSubscribers()) {
144 deleteListener(listener);
149 * Delete {@link ListenerAdapter} listener specified in parameter.
154 private static void deleteListener(ListenerAdapter listener) {
155 if (listener != null) {
158 } catch (Exception e) {
162 listenersByInstanceIdentifier.remove(listener.getPath());
163 listenersByStreamName.remove(listener.getStreamName());