2 * Copyright (c) 2015 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
9 package org.opendaylight.netconf.notifications.impl;
11 import com.google.common.base.Preconditions;
12 import com.google.common.collect.HashMultimap;
13 import com.google.common.collect.HashMultiset;
14 import com.google.common.collect.Lists;
15 import com.google.common.collect.Maps;
16 import com.google.common.collect.Multimap;
17 import com.google.common.collect.Multiset;
18 import com.google.common.collect.Sets;
21 import javax.annotation.concurrent.GuardedBy;
22 import javax.annotation.concurrent.ThreadSafe;
23 import org.opendaylight.netconf.notifications.BaseNotificationPublisherRegistration;
24 import org.opendaylight.netconf.notifications.NetconfNotification;
25 import org.opendaylight.netconf.notifications.NetconfNotificationCollector;
26 import org.opendaylight.netconf.notifications.NetconfNotificationListener;
27 import org.opendaylight.netconf.notifications.NetconfNotificationRegistry;
28 import org.opendaylight.netconf.notifications.NotificationListenerRegistration;
29 import org.opendaylight.netconf.notifications.NotificationPublisherRegistration;
30 import org.opendaylight.netconf.notifications.NotificationRegistration;
31 import org.opendaylight.netconf.notifications.impl.ops.NotificationsTransformUtil;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.StreamNameType;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf.Streams;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf.StreamsBuilder;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf.streams.Stream;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf.streams.StreamBuilder;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf.streams.StreamKey;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.notifications.rev120206.NetconfCapabilityChange;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
43 public class NetconfNotificationManager implements NetconfNotificationCollector, NetconfNotificationRegistry, NetconfNotificationListener, AutoCloseable {
45 public static final StreamNameType BASE_STREAM_NAME = new StreamNameType("NETCONF");
46 public static final Stream BASE_NETCONF_STREAM;
49 BASE_NETCONF_STREAM = new StreamBuilder()
50 .setName(BASE_STREAM_NAME)
51 .setKey(new StreamKey(BASE_STREAM_NAME))
52 .setReplaySupport(false)
53 .setDescription("Default Event Stream")
57 private static final Logger LOG = LoggerFactory.getLogger(NetconfNotificationManager.class);
59 // TODO excessive synchronization provides thread safety but is most likely not optimal (combination of concurrent collections might improve performance)
60 // And also calling callbacks from a synchronized block is dangerous since the listeners/publishers can block the whole notification processing
63 private final Multimap<StreamNameType, GenericNotificationListenerReg> notificationListeners = HashMultimap.create();
66 private final Set<NetconfNotificationStreamListener> streamListeners = Sets.newHashSet();
69 private final Map<StreamNameType, Stream> streamMetadata = Maps.newHashMap();
72 private final Multiset<StreamNameType> availableStreams = HashMultiset.create();
75 private final Set<GenericNotificationPublisherReg> notificationPublishers = Sets.newHashSet();
78 public synchronized void onNotification(final StreamNameType stream, final NetconfNotification notification) {
79 LOG.debug("Notification of type {} detected", stream);
80 if (LOG.isTraceEnabled()) {
81 LOG.debug("Notification of type {} detected: {}", stream, notification);
84 for (final GenericNotificationListenerReg listenerReg : notificationListeners.get(BASE_STREAM_NAME)) {
85 listenerReg.getListener().onNotification(BASE_STREAM_NAME, notification);
90 public synchronized NotificationListenerRegistration registerNotificationListener(final StreamNameType stream, final NetconfNotificationListener listener) {
91 Preconditions.checkNotNull(stream);
92 Preconditions.checkNotNull(listener);
94 LOG.trace("Notification listener registered for stream: {}", stream);
96 final GenericNotificationListenerReg genericNotificationListenerReg = new GenericNotificationListenerReg(listener) {
99 synchronized (NetconfNotificationManager.this) {
100 LOG.trace("Notification listener unregistered for stream: {}", stream);
106 notificationListeners.put(BASE_STREAM_NAME, genericNotificationListenerReg);
107 return genericNotificationListenerReg;
111 public synchronized Streams getNotificationPublishers() {
112 return new StreamsBuilder().setStream(Lists.newArrayList(streamMetadata.values())).build();
116 public synchronized boolean isStreamAvailable(final StreamNameType streamNameType) {
117 return availableStreams.contains(streamNameType);
121 public synchronized NotificationRegistration registerStreamListener(final NetconfNotificationStreamListener listener) {
122 streamListeners.add(listener);
124 // Notify about all already available
125 for (final Stream availableStream : streamMetadata.values()) {
126 listener.onStreamRegistered(availableStream);
129 return new NotificationRegistration() {
131 public void close() {
132 synchronized (NetconfNotificationManager.this) {
133 streamListeners.remove(listener);
140 public synchronized void close() {
141 // Unregister all listeners
142 for (final GenericNotificationListenerReg genericNotificationListenerReg : notificationListeners.values()) {
143 genericNotificationListenerReg.close();
145 notificationListeners.clear();
147 // Unregister all publishers
148 for (final GenericNotificationPublisherReg notificationPublisher : notificationPublishers) {
149 notificationPublisher.close();
151 notificationPublishers.clear();
153 // Clear stream Listeners
154 streamListeners.clear();
158 public synchronized NotificationPublisherRegistration registerNotificationPublisher(final Stream stream) {
159 Preconditions.checkNotNull(stream);
160 final StreamNameType streamName = stream.getName();
162 LOG.debug("Notification publisher registered for stream: {}", streamName);
163 if (LOG.isTraceEnabled()) {
164 LOG.trace("Notification publisher registered for stream: {}", stream);
167 if (streamMetadata.containsKey(streamName)) {
168 LOG.warn("Notification stream {} already registered as: {}. Will be reused", streamName, streamMetadata.get(streamName));
170 streamMetadata.put(streamName, stream);
173 availableStreams.add(streamName);
175 final GenericNotificationPublisherReg genericNotificationPublisherReg = new GenericNotificationPublisherReg(this, streamName) {
177 public void close() {
178 synchronized (NetconfNotificationManager.this) {
184 notificationPublishers.add(genericNotificationPublisherReg);
186 notifyStreamAdded(stream);
187 return genericNotificationPublisherReg;
190 private void unregisterNotificationPublisher(final StreamNameType streamName, final GenericNotificationPublisherReg genericNotificationPublisherReg) {
191 availableStreams.remove(streamName);
192 notificationPublishers.remove(genericNotificationPublisherReg);
194 LOG.debug("Notification publisher unregistered for stream: {}", streamName);
196 // Notify stream listeners if all publishers are gone and also clear metadata for stream
197 if (!isStreamAvailable(streamName)) {
198 LOG.debug("Notification stream: {} became unavailable", streamName);
199 streamMetadata.remove(streamName);
200 notifyStreamRemoved(streamName);
204 private synchronized void notifyStreamAdded(final Stream stream) {
205 for (final NetconfNotificationStreamListener streamListener : streamListeners) {
206 streamListener.onStreamRegistered(stream);
210 private synchronized void notifyStreamRemoved(final StreamNameType stream) {
211 for (final NetconfNotificationStreamListener streamListener : streamListeners) {
212 streamListener.onStreamUnregistered(stream);
217 public BaseNotificationPublisherRegistration registerBaseNotificationPublisher() {
218 final NotificationPublisherRegistration notificationPublisherRegistration = registerNotificationPublisher(BASE_NETCONF_STREAM);
219 return new BaseNotificationPublisherReg(notificationPublisherRegistration);
222 private static class GenericNotificationPublisherReg implements NotificationPublisherRegistration {
223 private NetconfNotificationManager baseListener;
224 private final StreamNameType registeredStream;
226 public GenericNotificationPublisherReg(final NetconfNotificationManager baseListener, final StreamNameType registeredStream) {
227 this.baseListener = baseListener;
228 this.registeredStream = registeredStream;
232 public void close() {
233 baseListener.unregisterNotificationPublisher(registeredStream, this);
238 public void onNotification(final StreamNameType stream, final NetconfNotification notification) {
239 Preconditions.checkState(baseListener != null, "Already closed");
240 Preconditions.checkArgument(stream.equals(registeredStream));
241 baseListener.onNotification(stream, notification);
245 private static class BaseNotificationPublisherReg implements BaseNotificationPublisherRegistration {
247 private final NotificationPublisherRegistration baseRegistration;
249 public BaseNotificationPublisherReg(final NotificationPublisherRegistration baseRegistration) {
250 this.baseRegistration = baseRegistration;
254 public void close() {
255 baseRegistration.close();
258 private static NetconfNotification serializeNotification(final NetconfCapabilityChange capabilityChange) {
259 return NotificationsTransformUtil.transform(capabilityChange);
263 public void onCapabilityChanged(final NetconfCapabilityChange capabilityChange) {
264 baseRegistration.onNotification(BASE_STREAM_NAME, serializeNotification(capabilityChange));
265 // baseRegistration.onNotification(BASE_STREAM_NAME, serializeNotification(computeDiff(removed, added)));
269 private class GenericNotificationListenerReg implements NotificationListenerRegistration {
270 private final NetconfNotificationListener listener;
272 public GenericNotificationListenerReg(final NetconfNotificationListener listener) {
273 this.listener = listener;
276 public NetconfNotificationListener getListener() {
281 public void close() {
282 notificationListeners.remove(BASE_STREAM_NAME, this);