2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.sal.streams.listeners;
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;
18 * {@link Notificator} is responsible to create, remove and find {@link ListenerAdapter} listener.
20 public class Notificator {
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();
26 private Notificator() {
30 * Returns list of all stream names
32 public static Set<String> getStreamNames() {
33 return listenersByStreamName.keySet();
37 * Gets {@link ListenerAdapter} specified by stream name.
40 * The name of the stream.
41 * @return {@link ListenerAdapter} specified by stream name.
43 public static ListenerAdapter getListenerFor(String streamName) {
44 return listenersByStreamName.get(streamName);
48 * Gets {@link ListenerAdapter} listener specified by {@link YangInstanceIdentifier} path.
51 * Path to data in data repository.
52 * @return ListenerAdapter
54 public static ListenerAdapter getListenerFor(YangInstanceIdentifier path) {
55 return listenersByInstanceIdentifier.get(path);
59 * Checks if the listener specified by {@link YangInstanceIdentifier} path exist.
62 * Path to data in data repository.
63 * @return True if the listener exist, false otherwise.
65 public static boolean existListenerFor(YangInstanceIdentifier path) {
66 return listenersByInstanceIdentifier.containsKey(path);
70 * Creates new {@link ListenerAdapter} listener from {@link YangInstanceIdentifier} path and stream name.
73 * Path to data in data repository.
75 * The name of the stream.
76 * @return New {@link ListenerAdapter} listener from {@link YangInstanceIdentifier} path and stream name.
78 public static ListenerAdapter createListener(YangInstanceIdentifier path, String streamName) {
79 ListenerAdapter listener = new ListenerAdapter(path, streamName);
82 listenersByInstanceIdentifier.put(path, listener);
83 listenersByStreamName.put(streamName, listener);
91 * Looks for listener determined by {@link YangInstanceIdentifier} path and removes it.
96 public static void removeListener(YangInstanceIdentifier path) {
97 ListenerAdapter listener = listenersByInstanceIdentifier.get(path);
98 deleteListener(listener);
102 * Creates String representation of stream name from URI. Removes slash from URI in start and end position.
105 * URI for creation stream name.
106 * @return String representation of stream name.
108 public static String createStreamNameFromUri(String uri) {
113 if (result.startsWith("/")) {
114 result = result.substring(1);
116 if (result.endsWith("/")) {
117 result = result.substring(0, result.length()-1);
123 * Removes all listeners.
125 public static void removeAllListeners() {
126 for (ListenerAdapter listener : listenersByInstanceIdentifier.values()) {
129 } catch (Exception e) {
134 listenersByStreamName = new ConcurrentHashMap<>();
135 listenersByInstanceIdentifier = new ConcurrentHashMap<>();
142 * Checks if listener has at least one subscriber. In case it doesn't have any, delete listener.
147 public static void removeListenerIfNoSubscriberExists(ListenerAdapter listener) {
148 if (!listener.hasSubscribers()) {
149 deleteListener(listener);
154 * Delete {@link ListenerAdapter} listener specified in parameter.
159 private static void deleteListener(ListenerAdapter listener) {
160 if (listener != null) {
163 } catch (Exception e) {
167 listenersByInstanceIdentifier.remove(listener.getPath());
168 listenersByStreamName.remove(listener.getStreamName());