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
19 * {@link ListenerAdapter} listener.
21 public class Notificator {
23 private static Map<String, ListenerAdapter> listenersByStreamName = 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 * Checks if the listener specified by {@link YangInstanceIdentifier} path exist.
51 * @return True if the listener exist, false otherwise.
53 public static boolean existListenerFor(String streamName) {
54 return listenersByStreamName.containsKey(streamName);
58 * Creates new {@link ListenerAdapter} listener from {@link YangInstanceIdentifier} path and stream name.
61 * Path to data in data repository.
63 * The name of the stream.
64 * @return New {@link ListenerAdapter} listener from {@link YangInstanceIdentifier} path and stream name.
66 public static ListenerAdapter createListener(YangInstanceIdentifier path, String streamName) {
67 ListenerAdapter listener = new ListenerAdapter(path, streamName);
70 listenersByStreamName.put(streamName, listener);
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.
82 * URI for creation stream name.
83 * @return String representation of stream name.
85 public static String createStreamNameFromUri(String uri) {
90 if (result.startsWith("/")) {
91 result = result.substring(1);
93 if (result.endsWith("/")) {
94 result = result.substring(0, result.length()-1);
100 * Removes all listeners.
102 public static void removeAllListeners() {
103 for (ListenerAdapter listener : listenersByStreamName.values()) {
106 } catch (Exception e) {
111 listenersByStreamName = new ConcurrentHashMap<>();
118 * Checks if listener has at least one subscriber. In case it doesn't have any, delete listener.
123 public static void removeListenerIfNoSubscriberExists(ListenerAdapter listener) {
124 if (!listener.hasSubscribers()) {
125 deleteListener(listener);
130 * Delete {@link ListenerAdapter} listener specified in parameter.
135 private static void deleteListener(ListenerAdapter listener) {
136 if (listener != null) {
139 } catch (Exception e) {
143 listenersByStreamName.remove(listener.getStreamName());