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