Merge "Added explicit revision date import of ietf-inet-types to opendaylight-statist...
[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.Set;
5 import java.util.concurrent.ConcurrentHashMap;
6 import java.util.concurrent.locks.Lock;
7 import java.util.concurrent.locks.ReentrantLock;
8
9 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
10
11 /**
12  * {@link Notificator} is responsible to create, remove and find {@link ListenerAdapter} listener.
13  */
14 public class Notificator {
15
16     private static Map<String, ListenerAdapter> listenersByStreamName = new ConcurrentHashMap<>();
17         private static Map<InstanceIdentifier, ListenerAdapter> listenersByInstanceIdentifier = new ConcurrentHashMap<>();
18         private static final Lock lock = new ReentrantLock();
19
20         private Notificator() {
21         }
22
23     /**
24      * Returns list of all stream names
25      */
26     public static Set<String> getStreamNames() {
27         return listenersByStreamName.keySet();
28     }
29
30
31         /**
32          * Gets {@link ListenerAdapter} specified by stream name.
33          * 
34          * @param streamName
35          *            The name of the stream.
36          * @return {@link ListenerAdapter} specified by stream name.
37          */
38         public static ListenerAdapter getListenerFor(String streamName) {
39                 return listenersByStreamName.get(streamName);
40         }
41
42         /**
43          * Gets {@link ListenerAdapter} listener specified by
44          * {@link InstanceIdentifier} path.
45          * 
46          * @param path
47          *            Path to data in data repository.
48          * @return ListenerAdapter
49          */
50         public static ListenerAdapter getListenerFor(InstanceIdentifier path) {
51                 return listenersByInstanceIdentifier.get(path);
52         }
53
54         /**
55          * Checks if the listener specified by {@link InstanceIdentifier} path
56          * exist.
57          * 
58          * @param path
59          *            Path to data in data repository.
60          * @return True if the listener exist, false otherwise.
61          */
62         public static boolean existListenerFor(InstanceIdentifier path) {
63                 return listenersByInstanceIdentifier.containsKey(path);
64         }
65
66         /**
67          * Creates new {@link ListenerAdapter} listener from
68          * {@link InstanceIdentifier} path and stream name.
69          * 
70          * @param path
71          *            Path to data in data repository.
72          * @param streamName
73          *            The name of the stream.
74          * @return New {@link ListenerAdapter} listener from
75          *         {@link InstanceIdentifier} path and stream name.
76          */
77         public static ListenerAdapter createListener(InstanceIdentifier path,
78                         String streamName) {
79                 ListenerAdapter listener = new ListenerAdapter(path, streamName);
80                 try {
81                         lock.lock();
82                         listenersByInstanceIdentifier.put(path, listener);
83                         listenersByStreamName.put(streamName, listener);
84                 } finally {
85                         lock.unlock();
86                 }
87                 return listener;
88         }
89
90         /**
91          * Looks for listener determined by {@link InstanceIdentifier} path and
92          * removes it.
93          * 
94          * @param path
95          *            InstanceIdentifier
96          */
97         public static void removeListener(InstanceIdentifier path) {
98                 ListenerAdapter listener = listenersByInstanceIdentifier.get(path);
99                 deleteListener(listener);
100         }
101
102         /**
103          * Creates String representation of stream name from URI. Removes slash from
104          * URI in start and end position.
105          * 
106          * @param uri
107          *            URI for creation stream name.
108          * @return String representation of stream name.
109          */
110         public static String createStreamNameFromUri(String uri) {
111                 if (uri == null) {
112                         return null;
113                 }
114                 String result = uri;
115                 if (result.startsWith("/")) {
116                         result = result.substring(1);
117                 }
118                 if (result.endsWith("/")) {
119                         result = result.substring(0, result.length());
120                 }
121                 return result;
122         }
123
124         /**
125          * Removes all listeners.
126          */
127         public static void removeAllListeners() {
128                 for (ListenerAdapter listener : listenersByInstanceIdentifier.values()) {
129                         try {
130                                 listener.close();
131                         } catch (Exception e) {
132                         }
133                 }
134                 try {
135                         lock.lock();
136                         listenersByStreamName = new ConcurrentHashMap<>();
137                         listenersByInstanceIdentifier = new ConcurrentHashMap<>();
138                 } finally {
139                         lock.unlock();
140                 }
141         }
142
143         /**
144          * Checks if listener has at least one subscriber. In case it doesn't have any, delete
145          * listener.
146          * 
147          * @param listener
148          *            ListenerAdapter
149          */
150         public static void removeListenerIfNoSubscriberExists(
151                         ListenerAdapter listener) {
152                 if (!listener.hasSubscribers()) {
153                         deleteListener(listener);
154                 }
155         }
156
157         /**
158          * Delete {@link ListenerAdapter} listener specified in parameter.
159          * 
160          * @param listener
161          *            ListenerAdapter
162          */
163         private static void deleteListener(ListenerAdapter listener) {
164                 if (listener != null) {
165                         try {
166                                 listener.close();
167                         } catch (Exception e) {
168                         }
169                         try {
170                                 lock.lock();
171                                 listenersByInstanceIdentifier.remove(listener.getPath());
172                                 listenersByStreamName.remove(listener.getStreamName());
173                         } finally {
174                                 lock.unlock();
175                         }
176                 }
177         }
178
179 }