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;
10 public class Notificator {
12 private static Map<String, ListenerAdapter> listenersByStreamName = new ConcurrentHashMap<>();
13 private static Map<InstanceIdentifier, ListenerAdapter> listenersByInstanceIdentifier = new ConcurrentHashMap<>();
14 private static final Lock lock = new ReentrantLock();
16 private Notificator() {
19 public static ListenerAdapter getListenerFor(String streamName) {
20 return listenersByStreamName.get(streamName);
23 public static ListenerAdapter getListenerFor(InstanceIdentifier path) {
24 return listenersByInstanceIdentifier.get(path);
27 public static boolean existListenerFor(InstanceIdentifier path) {
28 return listenersByInstanceIdentifier.containsKey(path);
31 public static ListenerAdapter createListener(InstanceIdentifier path, String streamName) {
32 ListenerAdapter listener = new ListenerAdapter(path, streamName);
35 listenersByInstanceIdentifier.put(path, listener);
36 listenersByStreamName.put(streamName, listener);
43 public static void removeListener(InstanceIdentifier path) {
44 ListenerAdapter listener = listenersByInstanceIdentifier.get(path);
45 deleteListener(listener);
48 public static String createStreamNameFromUri(String uri) {
53 if (result.startsWith("/")) {
54 result = result.substring(1);
56 if (result.endsWith("/")) {
57 result = result.substring(0, result.length());
62 public static void removeAllListeners() {
63 for (ListenerAdapter listener : listenersByInstanceIdentifier.values()) {
66 } catch (Exception e) {
71 listenersByStreamName = new ConcurrentHashMap<>();
72 listenersByInstanceIdentifier = new ConcurrentHashMap<>();
78 public static void removeListenerIfNoSubscriberExists(ListenerAdapter listener) {
79 if (!listener.hasSubscribers()) {
80 deleteListener(listener);
84 private static void deleteListener(ListenerAdapter listener) {
85 if (listener != null) {
88 } catch (Exception e) {
92 listenersByInstanceIdentifier.remove(listener.getPath());
93 listenersByStreamName.remove(listener.getStreamName());