Merge "Add support for multiple choice case statements within one augument in config...
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / main / java / org / opendaylight / controller / sal / streams / listeners / Notificator.java
1 package org.opendaylight.controller.sal.streams.listeners;
2
3 import java.util.Map;
4 import java.util.concurrent.ConcurrentHashMap;
5 import java.util.concurrent.locks.Lock;
6 import java.util.concurrent.locks.ReentrantLock;
7
8 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
9
10 public class Notificator {
11
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();
15
16     private Notificator() {
17     }
18
19     public static ListenerAdapter getListenerFor(String streamName) {
20         return listenersByStreamName.get(streamName);
21     }
22
23     public static ListenerAdapter getListenerFor(InstanceIdentifier path) {
24         return listenersByInstanceIdentifier.get(path);
25     }
26
27     public static boolean existListenerFor(InstanceIdentifier path) {
28         return listenersByInstanceIdentifier.containsKey(path);
29     }
30
31     public static ListenerAdapter createListener(InstanceIdentifier path, String streamName) {
32         ListenerAdapter listener = new ListenerAdapter(path, streamName);
33         try {
34             lock.lock();
35             listenersByInstanceIdentifier.put(path, listener);
36             listenersByStreamName.put(streamName, listener);
37         } finally {
38             lock.unlock();
39         }
40         return listener;
41     }
42
43     public static void removeListener(InstanceIdentifier path) {
44         ListenerAdapter listener = listenersByInstanceIdentifier.get(path);
45         deleteListener(listener);
46     }
47
48     public static String createStreamNameFromUri(String uri) {
49         if (uri == null) {
50             return null;
51         }
52         String result = uri;
53         if (result.startsWith("/")) {
54             result = result.substring(1);
55         }
56         if (result.endsWith("/")) {
57             result = result.substring(0, result.length());
58         }
59         return result;
60     }
61
62     public static void removeAllListeners() {
63         for (ListenerAdapter listener : listenersByInstanceIdentifier.values()) {
64             try {
65                 listener.close();
66             } catch (Exception e) {
67             }
68         }
69         try {
70             lock.lock();
71             listenersByStreamName = new ConcurrentHashMap<>();
72             listenersByInstanceIdentifier = new ConcurrentHashMap<>();
73         } finally {
74             lock.unlock();
75         }
76     }
77
78     public static void removeListenerIfNoSubscriberExists(ListenerAdapter listener) {
79         if (!listener.hasSubscribers()) {
80             deleteListener(listener);
81         }
82     }
83
84     private static void deleteListener(ListenerAdapter listener) {
85         if (listener != null) {
86             try {
87                 listener.close();
88             } catch (Exception e) {
89             }
90             try {
91                 lock.lock();
92                 listenersByInstanceIdentifier.remove(listener.getPath());
93                 listenersByStreamName.remove(listener.getStreamName());
94             } finally {
95                 lock.unlock();
96             }
97         }
98     }
99
100 }