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