b911369cf049d1dd07869aaa8accd257d0d56c9f
[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
19  * {@link ListenerAdapter} listener.
20  */
21 public class Notificator {
22
23     private static Map<String, ListenerAdapter> listenersByStreamName = 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      * Checks if the listener specified by {@link YangInstanceIdentifier} path exist.
49      *
50      * @param streamName
51      * @return True if the listener exist, false otherwise.
52      */
53     public static boolean existListenerFor(String streamName) {
54         return listenersByStreamName.containsKey(streamName);
55     }
56
57     /**
58      * Creates new {@link ListenerAdapter} listener from {@link YangInstanceIdentifier} path and stream name.
59      *
60      * @param path
61      *            Path to data in data repository.
62      * @param streamName
63      *            The name of the stream.
64      * @return New {@link ListenerAdapter} listener from {@link YangInstanceIdentifier} path and stream name.
65      */
66     public static ListenerAdapter createListener(YangInstanceIdentifier path, String streamName) {
67         ListenerAdapter listener = new ListenerAdapter(path, streamName);
68         try {
69             lock.lock();
70             listenersByStreamName.put(streamName, listener);
71         } finally {
72             lock.unlock();
73         }
74         return listener;
75     }
76
77     /**
78      * Looks for listener determined by {@link YangInstanceIdentifier} path and removes it.
79      * Creates String representation of stream name from URI. Removes slash from URI in start and end position.
80      *
81      * @param uri
82      *            URI for creation stream name.
83      * @return String representation of stream name.
84      */
85     public static String createStreamNameFromUri(String uri) {
86         if (uri == null) {
87             return null;
88         }
89         String result = uri;
90         if (result.startsWith("/")) {
91             result = result.substring(1);
92         }
93         if (result.endsWith("/")) {
94             result = result.substring(0, result.length());
95         }
96         return result;
97     }
98
99     /**
100      * Removes all listeners.
101      */
102     public static void removeAllListeners() {
103         for (ListenerAdapter listener : listenersByStreamName.values()) {
104             try {
105                 listener.close();
106             } catch (Exception e) {
107             }
108         }
109         try {
110             lock.lock();
111             listenersByStreamName = new ConcurrentHashMap<>();
112         } finally {
113             lock.unlock();
114         }
115     }
116
117     /**
118      * Checks if listener has at least one subscriber. In case it doesn't have any, delete listener.
119      *
120      * @param listener
121      *            ListenerAdapter
122      */
123     public static void removeListenerIfNoSubscriberExists(ListenerAdapter listener) {
124         if (!listener.hasSubscribers()) {
125             deleteListener(listener);
126         }
127     }
128
129     /**
130      * Delete {@link ListenerAdapter} listener specified in parameter.
131      *
132      * @param listener
133      *            ListenerAdapter
134      */
135     private static void deleteListener(ListenerAdapter listener) {
136         if (listener != null) {
137             try {
138                 listener.close();
139             } catch (Exception e) {
140             }
141             try {
142                 lock.lock();
143                 listenersByStreamName.remove(listener.getStreamName());
144             } finally {
145                 lock.unlock();
146             }
147         }
148     }
149
150 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.