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.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 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.
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 public static void removeAllListeners() {
119 for (final ListenerAdapter listener : dataChangeListener.values()) {
122 } catch (final Exception e) {
123 LOG.error("Failed to close listener", e);
128 dataChangeListener = new ConcurrentHashMap<>();
135 * Checks if listener has at least one subscriber. In case it doesn't have any, delete listener.
140 public static void removeListenerIfNoSubscriberExists(final ListenerAdapter listener) {
141 if (!listener.hasSubscribers()) {
142 deleteListener(listener);
147 * Delete {@link ListenerAdapter} listener specified in parameter.
152 private static void deleteListener(final ListenerAdapter listener) {
153 if (listener != null) {
156 } catch (final Exception e) {
157 LOG.error("Failed to close listener", e);
161 dataChangeListener.remove(listener.getStreamName());
169 * Check if the listener specified by qnames of request exist.
173 * @return True if the listener exist, false otherwise.
175 public static boolean existNotificationListenerFor(final String streamName) {
176 return notificationListenersByStreamName.containsKey(streamName);
181 * Prepare listener for notification ({@link NotificationDefinition})
184 * - paths of notifications
186 * - name of stream (generated by paths)
188 * - type of output for onNotification - XML or JSON
189 * @return List of {@link NotificationListenerAdapter} by paths
191 public static List<NotificationListenerAdapter> createNotificationListener(final List<SchemaPath> paths,
192 final String streamName, final String outputType) {
193 final List<NotificationListenerAdapter> listListeners = new ArrayList<>();
194 for (final SchemaPath path : paths) {
195 final NotificationListenerAdapter listener = new NotificationListenerAdapter(path, streamName, outputType);
196 listListeners.add(listener);
200 notificationListenersByStreamName.put(streamName, listListeners);
204 return listListeners;
207 public static void removeNotificationListenerIfNoSubscriberExists(final NotificationListenerAdapter listener) {
208 if (!listener.hasSubscribers()) {
209 deleteNotificationListener(listener);
213 private static void deleteNotificationListener(final NotificationListenerAdapter listener) {
214 if (listener != null) {
217 } catch (final Exception e) {
218 LOG.error("Failed to close listener", e);
222 notificationListenersByStreamName.remove(listener.getStreamName());
229 public static List<NotificationListenerAdapter> getNotificationListenerFor(final String streamName) {
230 return notificationListenersByStreamName.get(streamName);