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.restconf.nb.rfc8040.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.yang.gen.v1.urn.sal.restconf.event.subscription.rev140708.NotificationOutputTypeGrouping.NotificationOutputType;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
19 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
20 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
25 * {@link Notificator} is responsible to create, remove and find
26 * {@link ListenerAdapter} listener.
28 public final class Notificator {
30 private static Map<String, ListenerAdapter> dataChangeListener = new ConcurrentHashMap<>();
31 private static Map<String, List<NotificationListenerAdapter>> notificationListenersByStreamName =
32 new ConcurrentHashMap<>();
34 private static final Logger LOG = LoggerFactory.getLogger(Notificator.class);
35 private static final Lock LOCK = new ReentrantLock();
37 private Notificator() {
41 * Returns list of all stream names.
43 public static Set<String> getStreamNames() {
44 return dataChangeListener.keySet();
48 * Gets {@link ListenerAdapter} specified by stream name.
51 * The name of the stream.
52 * @return {@link ListenerAdapter} specified by stream name.
54 public static ListenerAdapter getListenerFor(final String streamName) {
55 return dataChangeListener.get(streamName);
59 * Checks if the listener specified by {@link YangInstanceIdentifier} path exist.
61 * @param streamName name of the stream
62 * @return True if the listener exist, false otherwise.
64 public static boolean existListenerFor(final String streamName) {
65 return dataChangeListener.containsKey(streamName);
69 * Creates new {@link ListenerAdapter} listener from
70 * {@link YangInstanceIdentifier} path and stream name.
73 * Path to data in data repository.
75 * The name of the stream.
77 * Spcific type of output for notifications - XML or JSON
78 * @return New {@link ListenerAdapter} listener from
79 * {@link YangInstanceIdentifier} path and stream name.
81 public static ListenerAdapter createListener(final YangInstanceIdentifier path, final String streamName,
82 final NotificationOutputType outputType) {
83 final ListenerAdapter listener = new ListenerAdapter(path, streamName, outputType);
86 dataChangeListener.put(streamName, listener);
94 * Looks for listener determined by {@link YangInstanceIdentifier} path and removes it.
95 * Creates String representation of stream name from URI. Removes slash from URI in start and end position.
98 * URI for creation stream name.
99 * @return String representation of stream name.
101 public static String createStreamNameFromUri(final String uri) {
106 if (result.startsWith("/")) {
107 result = result.substring(1);
109 if (result.endsWith("/")) {
110 result = result.substring(0, result.length() - 1);
116 * Removes all listeners.
118 @SuppressWarnings("checkstyle:IllegalCatch")
119 public static void removeAllListeners() {
120 for (final ListenerAdapter listener : dataChangeListener.values()) {
123 } catch (final Exception e) {
124 LOG.error("Failed to close listener", e);
129 dataChangeListener = new ConcurrentHashMap<>();
136 * Delete {@link ListenerAdapter} listener specified in parameter.
143 @SuppressWarnings("checkstyle:IllegalCatch")
144 private static <T extends BaseListenerInterface> void deleteListener(final T listener) {
145 if (listener != null) {
148 } catch (final Exception e) {
149 LOG.error("Failed to close listener", e);
153 dataChangeListener.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 * Prepare listener for notification ({@link NotificationDefinition}).
175 * paths of notifications
177 * name of stream (generated by paths)
179 * type of output for onNotification - XML or JSON
180 * @return List of {@link NotificationListenerAdapter} by paths
182 public static List<NotificationListenerAdapter> createNotificationListener(final List<SchemaPath> paths,
183 final String streamName, final String outputType) {
184 final List<NotificationListenerAdapter> listListeners = new ArrayList<>();
185 for (final SchemaPath path : paths) {
186 final NotificationListenerAdapter listener = new NotificationListenerAdapter(path, streamName, outputType);
187 listListeners.add(listener);
191 notificationListenersByStreamName.put(streamName, listListeners);
195 return listListeners;
198 public static <T extends BaseListenerInterface> void removeListenerIfNoSubscriberExists(final T listener) {
199 if (!listener.hasSubscribers()) {
200 if (listener instanceof NotificationListenerAdapter) {
201 deleteNotificationListener(listener);
203 deleteListener(listener);
208 @SuppressWarnings("checkstyle:IllegalCatch")
209 private static <T extends BaseListenerInterface> void deleteNotificationListener(final T listener) {
210 if (listener != null) {
213 } catch (final Exception e) {
214 LOG.error("Failed to close listener", e);
218 notificationListenersByStreamName.remove(listener.getStreamName());
225 public static List<NotificationListenerAdapter> getNotificationListenerFor(final String streamName) {
226 return notificationListenersByStreamName.get(streamName);