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
8 package org.opendaylight.netconf.mdsal.notification.impl;
10 import com.google.common.base.Preconditions;
11 import com.google.common.collect.HashMultimap;
12 import com.google.common.collect.HashMultiset;
13 import com.google.common.collect.Lists;
14 import com.google.common.collect.Multimap;
15 import com.google.common.collect.Multiset;
16 import java.util.HashMap;
17 import java.util.HashSet;
20 import javax.annotation.concurrent.GuardedBy;
21 import javax.annotation.concurrent.ThreadSafe;
22 import org.opendaylight.netconf.mdsal.notification.impl.ops.NotificationsTransformUtil;
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.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.StreamNameType;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf.Streams;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf.StreamsBuilder;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf.streams.Stream;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf.streams.StreamBuilder;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf.streams.StreamKey;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.notifications.rev120206.NetconfCapabilityChange;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.notifications.rev120206.NetconfSessionEnd;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.notifications.rev120206.NetconfSessionStart;
40 import org.opendaylight.yangtools.yang.binding.Notification;
41 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
46 public class NetconfNotificationManager implements NetconfNotificationCollector, NetconfNotificationRegistry,
47 NetconfNotificationListener, AutoCloseable {
49 public static final StreamNameType BASE_STREAM_NAME = new StreamNameType("NETCONF");
50 public static final Stream BASE_NETCONF_STREAM;
53 BASE_NETCONF_STREAM = new StreamBuilder()
54 .setName(BASE_STREAM_NAME)
55 .withKey(new StreamKey(BASE_STREAM_NAME))
56 .setReplaySupport(false)
57 .setDescription("Default Event Stream")
61 private static final Logger LOG = LoggerFactory.getLogger(NetconfNotificationManager.class);
63 // TODO excessive synchronization provides thread safety but is most likely not optimal
64 // (combination of concurrent collections might improve performance)
65 // And also calling callbacks from a synchronized block is dangerous
66 // since the listeners/publishers can block the whole notification processing
69 private final Multimap<StreamNameType, GenericNotificationListenerReg> notificationListeners =
70 HashMultimap.create();
73 private final Set<NetconfNotificationStreamListener> streamListeners = new HashSet<>();
76 private final Map<StreamNameType, Stream> streamMetadata = new HashMap<>();
79 private final Multiset<StreamNameType> availableStreams = HashMultiset.create();
82 private final Set<GenericNotificationPublisherReg> notificationPublishers = new HashSet<>();
85 public synchronized void onNotification(final StreamNameType stream, final NetconfNotification notification) {
86 LOG.debug("Notification of type {} detected", stream);
87 if (LOG.isTraceEnabled()) {
88 LOG.debug("Notification of type {} detected: {}", stream, notification);
91 for (final GenericNotificationListenerReg listenerReg : notificationListeners.get(BASE_STREAM_NAME)) {
92 listenerReg.getListener().onNotification(BASE_STREAM_NAME, notification);
97 public synchronized NotificationListenerRegistration registerNotificationListener(
98 final StreamNameType stream,
99 final NetconfNotificationListener listener) {
100 Preconditions.checkNotNull(stream);
101 Preconditions.checkNotNull(listener);
103 LOG.trace("Notification listener registered for stream: {}", stream);
105 final GenericNotificationListenerReg genericNotificationListenerReg =
106 new GenericNotificationListenerReg(listener) {
108 public void close() {
109 synchronized (NetconfNotificationManager.this) {
110 LOG.trace("Notification listener unregistered for stream: {}", stream);
116 notificationListeners.put(BASE_STREAM_NAME, genericNotificationListenerReg);
117 return genericNotificationListenerReg;
121 public synchronized Streams getNotificationPublishers() {
122 return new StreamsBuilder().setStream(Lists.newArrayList(streamMetadata.values())).build();
126 public synchronized boolean isStreamAvailable(final StreamNameType streamNameType) {
127 return availableStreams.contains(streamNameType);
131 public synchronized NotificationRegistration registerStreamListener(
132 final NetconfNotificationStreamListener listener) {
133 streamListeners.add(listener);
135 // Notify about all already available
136 for (final Stream availableStream : streamMetadata.values()) {
137 listener.onStreamRegistered(availableStream);
141 synchronized (NetconfNotificationManager.this) {
142 streamListeners.remove(listener);
148 public synchronized void close() {
149 // Unregister all listeners
150 for (final GenericNotificationListenerReg genericNotificationListenerReg : notificationListeners.values()) {
151 genericNotificationListenerReg.close();
153 notificationListeners.clear();
155 // Unregister all publishers
156 for (final GenericNotificationPublisherReg notificationPublisher : notificationPublishers) {
157 notificationPublisher.close();
159 notificationPublishers.clear();
161 // Clear stream Listeners
162 streamListeners.clear();
166 public synchronized NotificationPublisherRegistration registerNotificationPublisher(final Stream stream) {
167 Preconditions.checkNotNull(stream);
168 final StreamNameType streamName = stream.getName();
170 LOG.debug("Notification publisher registered for stream: {}", streamName);
171 if (LOG.isTraceEnabled()) {
172 LOG.trace("Notification publisher registered for stream: {}", stream);
175 if (streamMetadata.containsKey(streamName)) {
176 LOG.warn("Notification stream {} already registered as: {}. Will be reused", streamName,
177 streamMetadata.get(streamName));
179 streamMetadata.put(streamName, stream);
182 availableStreams.add(streamName);
184 final GenericNotificationPublisherReg genericNotificationPublisherReg =
185 new GenericNotificationPublisherReg(this, streamName) {
187 public void close() {
188 synchronized (NetconfNotificationManager.this) {
194 notificationPublishers.add(genericNotificationPublisherReg);
196 notifyStreamAdded(stream);
197 return genericNotificationPublisherReg;
200 private void unregisterNotificationPublisher(
201 final StreamNameType streamName,
202 final GenericNotificationPublisherReg genericNotificationPublisherReg) {
203 availableStreams.remove(streamName);
204 notificationPublishers.remove(genericNotificationPublisherReg);
206 LOG.debug("Notification publisher unregistered for stream: {}", streamName);
208 // Notify stream listeners if all publishers are gone and also clear metadata for stream
209 if (!isStreamAvailable(streamName)) {
210 LOG.debug("Notification stream: {} became unavailable", streamName);
211 streamMetadata.remove(streamName);
212 notifyStreamRemoved(streamName);
216 private synchronized void notifyStreamAdded(final Stream stream) {
217 for (final NetconfNotificationStreamListener streamListener : streamListeners) {
218 streamListener.onStreamRegistered(stream);
222 private synchronized void notifyStreamRemoved(final StreamNameType stream) {
223 for (final NetconfNotificationStreamListener streamListener : streamListeners) {
224 streamListener.onStreamUnregistered(stream);
229 public BaseNotificationPublisherRegistration registerBaseNotificationPublisher() {
230 final NotificationPublisherRegistration notificationPublisherRegistration =
231 registerNotificationPublisher(BASE_NETCONF_STREAM);
232 return new BaseNotificationPublisherReg(notificationPublisherRegistration);
235 private static class GenericNotificationPublisherReg implements NotificationPublisherRegistration {
236 private NetconfNotificationManager baseListener;
237 private final StreamNameType registeredStream;
239 GenericNotificationPublisherReg(final NetconfNotificationManager baseListener,
240 final StreamNameType registeredStream) {
241 this.baseListener = baseListener;
242 this.registeredStream = registeredStream;
246 public void close() {
247 baseListener.unregisterNotificationPublisher(registeredStream, this);
252 public void onNotification(final StreamNameType stream, final NetconfNotification notification) {
253 Preconditions.checkState(baseListener != null, "Already closed");
254 Preconditions.checkArgument(stream.equals(registeredStream));
255 baseListener.onNotification(stream, notification);
259 private static class BaseNotificationPublisherReg implements BaseNotificationPublisherRegistration {
261 static final SchemaPath CAPABILITY_CHANGE_SCHEMA_PATH = SchemaPath.create(true, NetconfCapabilityChange.QNAME);
262 static final SchemaPath SESSION_START_PATH = SchemaPath.create(true, NetconfSessionStart.QNAME);
263 static final SchemaPath SESSION_END_PATH = SchemaPath.create(true, NetconfSessionEnd.QNAME);
265 private final NotificationPublisherRegistration baseRegistration;
267 BaseNotificationPublisherReg(final NotificationPublisherRegistration baseRegistration) {
268 this.baseRegistration = baseRegistration;
272 public void close() {
273 baseRegistration.close();
276 private static NetconfNotification serializeNotification(final Notification notification,
277 final SchemaPath path) {
278 return NotificationsTransformUtil.transform(notification, path);
282 public void onCapabilityChanged(final NetconfCapabilityChange capabilityChange) {
283 baseRegistration.onNotification(BASE_STREAM_NAME,
284 serializeNotification(capabilityChange, CAPABILITY_CHANGE_SCHEMA_PATH));
288 public void onSessionStarted(final NetconfSessionStart start) {
289 baseRegistration.onNotification(BASE_STREAM_NAME, serializeNotification(start, SESSION_START_PATH));
293 public void onSessionEnded(final NetconfSessionEnd end) {
294 baseRegistration.onNotification(BASE_STREAM_NAME, serializeNotification(end, SESSION_END_PATH));
298 private class GenericNotificationListenerReg implements NotificationListenerRegistration {
299 private final NetconfNotificationListener listener;
301 GenericNotificationListenerReg(final NetconfNotificationListener listener) {
302 this.listener = listener;
305 public NetconfNotificationListener getListener() {
310 public void close() {
311 notificationListeners.remove(BASE_STREAM_NAME, this);