Merge changes I8290457c,If84199a1
[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         lock.lock();
34         listenersByInstanceIdentifier.put(path, listener);
35         listenersByStreamName.put(streamName, listener);
36         lock.unlock();
37         return listener;
38     }
39
40     public static void removeListener(InstanceIdentifier path) {
41         ListenerAdapter listener = listenersByInstanceIdentifier.get(path);
42         deleteListener(listener);
43     }
44
45     public static String createStreamNameFromUri(String uri) {
46         if (uri == null) {
47             return null;
48         }
49         String result = uri;
50         if (result.startsWith("/")) {
51             result = result.substring(1);
52         }
53         if (result.endsWith("/")) {
54             result = result.substring(0, result.length());
55         }
56         return result;
57     }
58
59     public static void removeAllListeners() {
60         for (ListenerAdapter listener : listenersByInstanceIdentifier.values()) {
61             try {
62                 listener.close();
63             } catch (Exception e) {
64             }
65         }
66         lock.lock();
67         listenersByStreamName = new ConcurrentHashMap<>();
68         listenersByInstanceIdentifier = new ConcurrentHashMap<>();
69         lock.unlock();
70     }
71
72     public static void removeListenerIfNoSubscriberExists(ListenerAdapter listener) {
73         if (!listener.hasSubscribers()) {
74             deleteListener(listener);
75         }
76     }
77
78     private static void deleteListener(ListenerAdapter listener) {
79         if (listener != null) {
80             try {
81                 listener.close();
82             } catch (Exception e) {
83             }
84             lock.lock();
85             listenersByInstanceIdentifier.remove(listener.getPath());
86             listenersByStreamName.remove(listener).getStreamName();
87             lock.unlock();
88         }
89     }
90
91 }