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;
16 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
19 * {@link Notificator} is responsible to create, remove and find {@link ListenerAdapter} listener.
21 public class Notificator {
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();
27 private Notificator() {
31 * Returns list of all stream names
33 public static Set<String> getStreamNames() {
34 return listenersByStreamName.keySet();
39 * Gets {@link ListenerAdapter} specified by stream name.
42 * The name of the stream.
43 * @return {@link ListenerAdapter} specified by stream name.
45 public static ListenerAdapter getListenerFor(String streamName) {
46 return listenersByStreamName.get(streamName);
50 * Gets {@link ListenerAdapter} listener specified by
51 * {@link InstanceIdentifier} path.
54 * Path to data in data repository.
55 * @return ListenerAdapter
57 public static ListenerAdapter getListenerFor(InstanceIdentifier path) {
58 return listenersByInstanceIdentifier.get(path);
62 * Checks if the listener specified by {@link InstanceIdentifier} path
66 * Path to data in data repository.
67 * @return True if the listener exist, false otherwise.
69 public static boolean existListenerFor(InstanceIdentifier path) {
70 return listenersByInstanceIdentifier.containsKey(path);
74 * Creates new {@link ListenerAdapter} listener from
75 * {@link InstanceIdentifier} path and stream name.
78 * Path to data in data repository.
80 * The name of the stream.
81 * @return New {@link ListenerAdapter} listener from
82 * {@link InstanceIdentifier} path and stream name.
84 public static ListenerAdapter createListener(InstanceIdentifier path,
86 ListenerAdapter listener = new ListenerAdapter(path, streamName);
89 listenersByInstanceIdentifier.put(path, listener);
90 listenersByStreamName.put(streamName, listener);
98 * Looks for listener determined by {@link InstanceIdentifier} path and
104 public static void removeListener(InstanceIdentifier path) {
105 ListenerAdapter listener = listenersByInstanceIdentifier.get(path);
106 deleteListener(listener);
110 * Creates String representation of stream name from URI. Removes slash from
111 * URI in start and end position.
114 * URI for creation stream name.
115 * @return String representation of stream name.
117 public static String createStreamNameFromUri(String uri) {
122 if (result.startsWith("/")) {
123 result = result.substring(1);
125 if (result.endsWith("/")) {
126 result = result.substring(0, result.length());
132 * Removes all listeners.
134 public static void removeAllListeners() {
135 for (ListenerAdapter listener : listenersByInstanceIdentifier.values()) {
138 } catch (Exception e) {
143 listenersByStreamName = new ConcurrentHashMap<>();
144 listenersByInstanceIdentifier = new ConcurrentHashMap<>();
151 * Checks if listener has at least one subscriber. In case it doesn't have any, delete
157 public static void removeListenerIfNoSubscriberExists(
158 ListenerAdapter listener) {
159 if (!listener.hasSubscribers()) {
160 deleteListener(listener);
165 * Delete {@link ListenerAdapter} listener specified in parameter.
170 private static void deleteListener(ListenerAdapter listener) {
171 if (listener != null) {
174 } catch (Exception e) {
178 listenersByInstanceIdentifier.remove(listener.getPath());
179 listenersByStreamName.remove(listener.getStreamName());