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 static com.google.common.base.Preconditions.checkArgument;
11 import static com.google.common.base.Preconditions.checkState;
12 import static java.util.Objects.requireNonNull;
14 import com.google.common.collect.HashMultimap;
15 import com.google.common.collect.HashMultiset;
16 import com.google.common.collect.Maps;
17 import com.google.common.collect.Multimap;
18 import com.google.common.collect.Multiset;
19 import java.util.ArrayList;
20 import java.util.HashMap;
21 import java.util.HashSet;
24 import javax.annotation.PreDestroy;
25 import javax.inject.Inject;
26 import javax.inject.Singleton;
27 import org.checkerframework.checker.lock.qual.GuardedBy;
28 import org.opendaylight.mdsal.binding.dom.codec.spi.BindingDOMCodecFactory;
29 import org.opendaylight.mdsal.binding.runtime.api.BindingRuntimeGenerator;
30 import org.opendaylight.netconf.mdsal.notification.impl.ops.NotificationsTransformUtil;
31 import org.opendaylight.netconf.notifications.BaseNotificationPublisherRegistration;
32 import org.opendaylight.netconf.notifications.NetconfNotification;
33 import org.opendaylight.netconf.notifications.NetconfNotificationCollector;
34 import org.opendaylight.netconf.notifications.NetconfNotificationListener;
35 import org.opendaylight.netconf.notifications.NetconfNotificationRegistry;
36 import org.opendaylight.netconf.notifications.NotificationListenerRegistration;
37 import org.opendaylight.netconf.notifications.NotificationPublisherRegistration;
38 import org.opendaylight.netconf.notifications.NotificationRegistration;
39 import org.opendaylight.netconf.notifications.YangLibraryPublisherRegistration;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.StreamNameType;
41 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf.Streams;
42 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf.StreamsBuilder;
43 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf.streams.Stream;
44 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf.streams.StreamBuilder;
45 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf.streams.StreamKey;
46 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.notifications.rev120206.NetconfCapabilityChange;
47 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.notifications.rev120206.NetconfSessionEnd;
48 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.notifications.rev120206.NetconfSessionStart;
49 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.YangLibraryChange;
50 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.YangLibraryUpdate;
51 import org.opendaylight.yangtools.yang.binding.Notification;
52 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
53 import org.opendaylight.yangtools.yang.parser.api.YangParserException;
54 import org.opendaylight.yangtools.yang.parser.api.YangParserFactory;
55 import org.osgi.service.component.annotations.Activate;
56 import org.osgi.service.component.annotations.Component;
57 import org.osgi.service.component.annotations.Deactivate;
58 import org.osgi.service.component.annotations.Reference;
59 import org.slf4j.Logger;
60 import org.slf4j.LoggerFactory;
63 * A thread-safe implementation NetconfNotificationRegistry.
66 @Component(service = { NetconfNotificationCollector.class, NetconfNotificationRegistry.class }, immediate = true,
67 property = "type=netconf-notification-manager")
68 public final class NetconfNotificationManager implements NetconfNotificationCollector, NetconfNotificationRegistry,
69 NetconfNotificationListener, AutoCloseable {
70 public static final StreamNameType BASE_STREAM_NAME = new StreamNameType("NETCONF");
71 public static final Stream BASE_NETCONF_STREAM = new StreamBuilder()
72 .setName(BASE_STREAM_NAME)
73 .withKey(new StreamKey(BASE_STREAM_NAME))
74 .setReplaySupport(false)
75 .setDescription("Default Event Stream")
78 private static final Logger LOG = LoggerFactory.getLogger(NetconfNotificationManager.class);
80 // TODO excessive synchronization provides thread safety but is most likely not optimal
81 // (combination of concurrent collections might improve performance)
82 // And also calling callbacks from a synchronized block is dangerous
83 // since the listeners/publishers can block the whole notification processing
86 private final Multimap<StreamNameType, GenericNotificationListenerReg> notificationListeners =
87 HashMultimap.create();
90 private final Set<NetconfNotificationStreamListener> streamListeners = new HashSet<>();
93 private final Map<StreamNameType, Stream> streamMetadata = new HashMap<>();
96 private final Multiset<StreamNameType> availableStreams = HashMultiset.create();
99 private final Set<GenericNotificationPublisherReg> notificationPublishers = new HashSet<>();
100 private final NotificationsTransformUtil transformUtil;
104 public NetconfNotificationManager(@Reference final YangParserFactory parserFactory,
105 @Reference final BindingRuntimeGenerator generator, @Reference final BindingDOMCodecFactory codecFactory)
106 throws YangParserException {
107 transformUtil = new NotificationsTransformUtil(parserFactory, generator, codecFactory);
113 public synchronized void close() {
114 // Unregister all listeners
115 // Use new list to avoid ConcurrentModificationException
116 for (final GenericNotificationListenerReg listenerReg : new ArrayList<>(notificationListeners.values())) {
119 notificationListeners.clear();
121 // Unregister all publishers
122 // Use new list to avoid ConcurrentModificationException
123 for (final GenericNotificationPublisherReg notificationPublisher : new ArrayList<>(notificationPublishers)) {
124 notificationPublisher.close();
126 notificationPublishers.clear();
128 // Clear stream Listeners
129 streamListeners.clear();
133 public synchronized void onNotification(final StreamNameType stream, final NetconfNotification notification) {
134 LOG.debug("Notification of type {} detected", stream);
135 if (LOG.isTraceEnabled()) {
136 LOG.debug("Notification of type {} detected: {}", stream, notification);
139 for (final GenericNotificationListenerReg listenerReg : notificationListeners.get(stream)) {
140 listenerReg.getListener().onNotification(stream, notification);
145 public synchronized NotificationListenerRegistration registerNotificationListener(
146 final StreamNameType stream,
147 final NetconfNotificationListener listener) {
148 requireNonNull(stream);
149 requireNonNull(listener);
151 LOG.trace("Notification listener registered for stream: {}", stream);
153 final GenericNotificationListenerReg reg = new GenericNotificationListenerReg(listener, stream) {
155 public void close() {
156 synchronized (NetconfNotificationManager.this) {
157 LOG.trace("Notification listener unregistered for stream: {}", stream);
163 notificationListeners.put(stream, reg);
168 public synchronized Streams getNotificationPublishers() {
169 return new StreamsBuilder().setStream(Maps.uniqueIndex(streamMetadata.values(), Stream::key)).build();
173 public synchronized boolean isStreamAvailable(final StreamNameType streamNameType) {
174 return availableStreams.contains(streamNameType);
178 public synchronized NotificationRegistration registerStreamListener(
179 final NetconfNotificationStreamListener listener) {
180 streamListeners.add(listener);
182 // Notify about all already available
183 for (final Stream availableStream : streamMetadata.values()) {
184 listener.onStreamRegistered(availableStream);
188 synchronized (NetconfNotificationManager.this) {
189 streamListeners.remove(listener);
195 public synchronized NotificationPublisherRegistration registerNotificationPublisher(final Stream stream) {
196 final StreamNameType streamName = requireNonNull(stream).getName();
198 LOG.debug("Notification publisher registered for stream: {}", streamName);
199 if (LOG.isTraceEnabled()) {
200 LOG.trace("Notification publisher registered for stream: {}", stream);
203 if (streamMetadata.containsKey(streamName)) {
204 LOG.warn("Notification stream {} already registered as: {}. Will be reused", streamName,
205 streamMetadata.get(streamName));
207 streamMetadata.put(streamName, stream);
210 availableStreams.add(streamName);
212 final GenericNotificationPublisherReg reg = new GenericNotificationPublisherReg(this, streamName) {
214 public void close() {
215 synchronized (NetconfNotificationManager.this) {
221 notificationPublishers.add(reg);
223 notifyStreamAdded(stream);
227 private void unregisterNotificationPublisher(
228 final StreamNameType streamName,
229 final GenericNotificationPublisherReg genericNotificationPublisherReg) {
230 availableStreams.remove(streamName);
231 notificationPublishers.remove(genericNotificationPublisherReg);
233 LOG.debug("Notification publisher unregistered for stream: {}", streamName);
235 // Notify stream listeners if all publishers are gone and also clear metadata for stream
236 if (!isStreamAvailable(streamName)) {
237 LOG.debug("Notification stream: {} became unavailable", streamName);
238 streamMetadata.remove(streamName);
239 notifyStreamRemoved(streamName);
243 private synchronized void notifyStreamAdded(final Stream stream) {
244 for (final NetconfNotificationStreamListener streamListener : streamListeners) {
245 streamListener.onStreamRegistered(stream);
249 private synchronized void notifyStreamRemoved(final StreamNameType stream) {
250 for (final NetconfNotificationStreamListener streamListener : streamListeners) {
251 streamListener.onStreamUnregistered(stream);
256 public BaseNotificationPublisherRegistration registerBaseNotificationPublisher() {
257 final NotificationPublisherRegistration notificationPublisherRegistration =
258 registerNotificationPublisher(BASE_NETCONF_STREAM);
259 return new BaseNotificationPublisherReg(transformUtil, notificationPublisherRegistration);
263 public YangLibraryPublisherRegistration registerYangLibraryPublisher() {
264 final NotificationPublisherRegistration notificationPublisherRegistration =
265 registerNotificationPublisher(BASE_NETCONF_STREAM);
266 return new YangLibraryPublisherReg(transformUtil, notificationPublisherRegistration);
269 private static class GenericNotificationPublisherReg implements NotificationPublisherRegistration {
270 private NetconfNotificationManager baseListener;
271 private final StreamNameType registeredStream;
273 GenericNotificationPublisherReg(final NetconfNotificationManager baseListener,
274 final StreamNameType registeredStream) {
275 this.baseListener = baseListener;
276 this.registeredStream = registeredStream;
280 public void close() {
281 baseListener.unregisterNotificationPublisher(registeredStream, this);
286 public void onNotification(final StreamNameType stream, final NetconfNotification notification) {
287 checkState(baseListener != null, "Already closed");
288 checkArgument(stream.equals(registeredStream));
289 baseListener.onNotification(stream, notification);
293 private static class BaseNotificationPublisherReg implements BaseNotificationPublisherRegistration {
294 static final Absolute CAPABILITY_CHANGE_SCHEMA_PATH = Absolute.of(NetconfCapabilityChange.QNAME);
295 static final Absolute SESSION_START_PATH = Absolute.of(NetconfSessionStart.QNAME);
296 static final Absolute SESSION_END_PATH = Absolute.of(NetconfSessionEnd.QNAME);
298 private final NotificationPublisherRegistration baseRegistration;
299 private final NotificationsTransformUtil transformUtil;
301 BaseNotificationPublisherReg(final NotificationsTransformUtil transformUtil,
302 final NotificationPublisherRegistration baseRegistration) {
303 this.transformUtil = requireNonNull(transformUtil);
304 this.baseRegistration = baseRegistration;
308 public void close() {
309 baseRegistration.close();
312 private NetconfNotification serializeNotification(final Notification<?> notification, final Absolute path) {
313 return transformUtil.transform(notification, path);
317 public void onCapabilityChanged(final NetconfCapabilityChange capabilityChange) {
318 baseRegistration.onNotification(BASE_STREAM_NAME,
319 serializeNotification(capabilityChange, CAPABILITY_CHANGE_SCHEMA_PATH));
323 public void onSessionStarted(final NetconfSessionStart start) {
324 baseRegistration.onNotification(BASE_STREAM_NAME, serializeNotification(start, SESSION_START_PATH));
328 public void onSessionEnded(final NetconfSessionEnd end) {
329 baseRegistration.onNotification(BASE_STREAM_NAME, serializeNotification(end, SESSION_END_PATH));
333 private static class YangLibraryPublisherReg implements YangLibraryPublisherRegistration {
334 static final Absolute YANG_LIBRARY_CHANGE_PATH = Absolute.of(YangLibraryChange.QNAME);
335 static final Absolute YANG_LIBRARY_UPDATE_PATH = Absolute.of(YangLibraryUpdate.QNAME);
337 private final NotificationPublisherRegistration baseRegistration;
338 private final NotificationsTransformUtil transformUtil;
340 YangLibraryPublisherReg(final NotificationsTransformUtil transformUtil,
341 final NotificationPublisherRegistration baseRegistration) {
342 this.transformUtil = requireNonNull(transformUtil);
343 this.baseRegistration = baseRegistration;
348 public void onYangLibraryChange(final YangLibraryChange yangLibraryChange) {
349 baseRegistration.onNotification(BASE_STREAM_NAME,
350 transformUtil.transform(yangLibraryChange, YANG_LIBRARY_CHANGE_PATH));
354 public void onYangLibraryUpdate(final YangLibraryUpdate yangLibraryUpdate) {
355 baseRegistration.onNotification(BASE_STREAM_NAME,
356 transformUtil.transform(yangLibraryUpdate, YANG_LIBRARY_UPDATE_PATH));
360 public void close() {
361 baseRegistration.close();
365 private class GenericNotificationListenerReg implements NotificationListenerRegistration {
366 private final NetconfNotificationListener listener;
367 private final StreamNameType listenedStream;
369 GenericNotificationListenerReg(final NetconfNotificationListener listener,
370 final StreamNameType listenedStream) {
371 this.listener = listener;
372 this.listenedStream = listenedStream;
375 public NetconfNotificationListener getListener() {
380 public void close() {
381 notificationListeners.remove(listenedStream, this);