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