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.netconf.sal.streams.listeners;
10 import java.util.ArrayList;
11 import java.util.List;
14 import java.util.concurrent.ConcurrentHashMap;
15 import java.util.concurrent.locks.Lock;
16 import java.util.concurrent.locks.ReentrantLock;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
18 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
23 * {@link Notificator} is responsible to create, remove and find
24 * {@link ListenerAdapter} listener.
26 public class Notificator {
28 private static Map<String, ListenerAdapter> listenersByStreamName = new ConcurrentHashMap<>();
29 private static Map<String, List<NotificationListenerAdapter>> notificationListenersByStreamName = new ConcurrentHashMap<>();
31 private static final Logger LOG = LoggerFactory.getLogger(Notificator.class);
32 private static final Lock lock = new ReentrantLock();
34 private Notificator() {
38 * Returns list of all stream names
40 public static Set<String> getStreamNames() {
41 return listenersByStreamName.keySet();
45 * Gets {@link ListenerAdapter} specified by stream name.
48 * The name of the stream.
49 * @return {@link ListenerAdapter} specified by stream name.
51 public static ListenerAdapter getListenerFor(final String streamName) {
52 return listenersByStreamName.get(streamName);
56 * Checks if the listener specified by {@link YangInstanceIdentifier} path exist.
59 * @return True if the listener exist, false otherwise.
61 public static boolean existListenerFor(final String streamName) {
62 return listenersByStreamName.containsKey(streamName);
66 * Creates new {@link ListenerAdapter} listener from {@link YangInstanceIdentifier} path and stream name.
69 * Path to data in data repository.
71 * The name of the stream.
72 * @return New {@link ListenerAdapter} listener from {@link YangInstanceIdentifier} path and stream name.
74 public static ListenerAdapter createListener(final YangInstanceIdentifier path, final String streamName) {
75 final ListenerAdapter listener = new ListenerAdapter(path, streamName);
78 listenersByStreamName.put(streamName, listener);
86 * Looks for listener determined by {@link YangInstanceIdentifier} path and removes it.
87 * Creates String representation of stream name from URI. Removes slash from URI in start and end position.
90 * URI for creation stream name.
91 * @return String representation of stream name.
93 public static String createStreamNameFromUri(final String uri) {
98 if (result.startsWith("/")) {
99 result = result.substring(1);
101 if (result.endsWith("/")) {
102 result = result.substring(0, result.length()-1);
108 * Removes all listeners.
110 public static void removeAllListeners() {
111 for (final ListenerAdapter listener : listenersByStreamName.values()) {
114 } catch (final Exception e) {
115 LOG.error("Failed to close listener", e);
120 listenersByStreamName = new ConcurrentHashMap<>();
127 * Checks if listener has at least one subscriber. In case it doesn't have any, delete listener.
132 public static void removeListenerIfNoSubscriberExists(final ListenerAdapter listener) {
133 if (!listener.hasSubscribers()) {
134 deleteListener(listener);
139 * Delete {@link ListenerAdapter} listener specified in parameter.
144 private static void deleteListener(final ListenerAdapter listener) {
145 if (listener != null) {
148 } catch (final Exception e) {
149 LOG.error("Failed to close listener", e);
153 listenersByStreamName.remove(listener.getStreamName());
161 * Check if the listener specified by qnames of request exist.
165 * @return True if the listener exist, false otherwise.
167 public static boolean existNotificationListenerFor(final String streamName) {
168 return notificationListenersByStreamName.containsKey(streamName);
172 public static List<NotificationListenerAdapter> createNotificationListener(final List<SchemaPath> paths,
173 final String streamName, final String outputType) {
174 final List<NotificationListenerAdapter> listListeners = new ArrayList<>();
175 for (final SchemaPath path : paths) {
176 final NotificationListenerAdapter listener = new NotificationListenerAdapter(path, streamName, outputType);
177 listListeners.add(listener);
181 notificationListenersByStreamName.put(streamName, listListeners);
185 return listListeners;
188 public static void removeNotificationListenerIfNoSubscriberExists(final NotificationListenerAdapter listener) {
189 if (!listener.hasSubscribers()) {
190 deleteNotificationListener(listener);
194 private static void deleteNotificationListener(final NotificationListenerAdapter listener) {
195 if (listener != null) {
198 } catch (final Exception e) {
199 LOG.error("Failed to close listener", e);
203 notificationListenersByStreamName.remove(listener.getStreamName());
210 public static List<NotificationListenerAdapter> getNotificationListenerFor(final String streamName) {
211 return notificationListenersByStreamName.get(streamName);