Bump upstreams for Silicon
[netconf.git] / netconf / mdsal-netconf-notification / src / main / java / org / opendaylight / netconf / mdsal / notification / impl / NetconfNotificationManager.java
index d9d18e008c3d740dddf220f7d65a397c218906a7..63ea9be8c70dab5c4b30057d09dcf57be248110f 100644 (file)
@@ -7,18 +7,23 @@
  */
 package org.opendaylight.netconf.mdsal.notification.impl;
 
-import com.google.common.base.Preconditions;
+import static com.google.common.base.Preconditions.checkArgument;
+import static com.google.common.base.Preconditions.checkState;
+import static java.util.Objects.requireNonNull;
+
 import com.google.common.collect.HashMultimap;
 import com.google.common.collect.HashMultiset;
-import com.google.common.collect.Lists;
+import com.google.common.collect.Maps;
 import com.google.common.collect.Multimap;
 import com.google.common.collect.Multiset;
+import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Map;
 import java.util.Set;
-import javax.annotation.concurrent.GuardedBy;
-import javax.annotation.concurrent.ThreadSafe;
+import javax.inject.Inject;
+import javax.inject.Singleton;
+import org.checkerframework.checker.lock.qual.GuardedBy;
 import org.opendaylight.netconf.mdsal.notification.impl.ops.NotificationsTransformUtil;
 import org.opendaylight.netconf.notifications.BaseNotificationPublisherRegistration;
 import org.opendaylight.netconf.notifications.NetconfNotification;
@@ -28,6 +33,7 @@ import org.opendaylight.netconf.notifications.NetconfNotificationRegistry;
 import org.opendaylight.netconf.notifications.NotificationListenerRegistration;
 import org.opendaylight.netconf.notifications.NotificationPublisherRegistration;
 import org.opendaylight.netconf.notifications.NotificationRegistration;
+import org.opendaylight.netconf.notifications.YangLibraryPublisherRegistration;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.StreamNameType;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf.Streams;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf.StreamsBuilder;
@@ -37,26 +43,27 @@ import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.r
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.notifications.rev120206.NetconfCapabilityChange;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.notifications.rev120206.NetconfSessionEnd;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.notifications.rev120206.NetconfSessionStart;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.YangLibraryChange;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.YangLibraryUpdate;
 import org.opendaylight.yangtools.yang.binding.Notification;
 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-@ThreadSafe
+/**
+ *  A thread-safe implementation NetconfNotificationRegistry.
+ */
+@Singleton
 public class NetconfNotificationManager implements NetconfNotificationCollector, NetconfNotificationRegistry,
         NetconfNotificationListener, AutoCloseable {
 
     public static final StreamNameType BASE_STREAM_NAME = new StreamNameType("NETCONF");
-    public static final Stream BASE_NETCONF_STREAM;
-
-    static {
-        BASE_NETCONF_STREAM = new StreamBuilder()
+    public static final Stream BASE_NETCONF_STREAM = new StreamBuilder()
                 .setName(BASE_STREAM_NAME)
                 .withKey(new StreamKey(BASE_STREAM_NAME))
                 .setReplaySupport(false)
                 .setDescription("Default Event Stream")
                 .build();
-    }
 
     private static final Logger LOG = LoggerFactory.getLogger(NetconfNotificationManager.class);
 
@@ -80,6 +87,12 @@ public class NetconfNotificationManager implements NetconfNotificationCollector,
 
     @GuardedBy("this")
     private final Set<GenericNotificationPublisherReg> notificationPublishers = new HashSet<>();
+    private final NotificationsTransformUtil transformUtil;
+
+    @Inject
+    public NetconfNotificationManager(final NotificationsTransformUtil transformUtil) {
+        this.transformUtil = requireNonNull(transformUtil);
+    }
 
     @Override
     public synchronized void onNotification(final StreamNameType stream, final NetconfNotification notification) {
@@ -97,8 +110,8 @@ public class NetconfNotificationManager implements NetconfNotificationCollector,
     public synchronized NotificationListenerRegistration registerNotificationListener(
             final StreamNameType stream,
             final NetconfNotificationListener listener) {
-        Preconditions.checkNotNull(stream);
-        Preconditions.checkNotNull(listener);
+        requireNonNull(stream);
+        requireNonNull(listener);
 
         LOG.trace("Notification listener registered for stream: {}", stream);
 
@@ -119,7 +132,7 @@ public class NetconfNotificationManager implements NetconfNotificationCollector,
 
     @Override
     public synchronized Streams getNotificationPublishers() {
-        return new StreamsBuilder().setStream(Lists.newArrayList(streamMetadata.values())).build();
+        return new StreamsBuilder().setStream(Maps.uniqueIndex(streamMetadata.values(), Stream::key)).build();
     }
 
     @Override
@@ -164,8 +177,7 @@ public class NetconfNotificationManager implements NetconfNotificationCollector,
 
     @Override
     public synchronized NotificationPublisherRegistration registerNotificationPublisher(final Stream stream) {
-        Preconditions.checkNotNull(stream);
-        final StreamNameType streamName = stream.getName();
+        final StreamNameType streamName = requireNonNull(stream).getName();
 
         LOG.debug("Notification publisher registered for stream: {}", streamName);
         if (LOG.isTraceEnabled()) {
@@ -197,6 +209,8 @@ public class NetconfNotificationManager implements NetconfNotificationCollector,
         return genericNotificationPublisherReg;
     }
 
+    @SuppressFBWarnings(value = "UPM_UNCALLED_PRIVATE_METHOD",
+            justification = "https://github.com/spotbugs/spotbugs/issues/811")
     private void unregisterNotificationPublisher(
             final StreamNameType streamName,
             final GenericNotificationPublisherReg genericNotificationPublisherReg) {
@@ -229,7 +243,14 @@ public class NetconfNotificationManager implements NetconfNotificationCollector,
     public BaseNotificationPublisherRegistration registerBaseNotificationPublisher() {
         final NotificationPublisherRegistration notificationPublisherRegistration =
                 registerNotificationPublisher(BASE_NETCONF_STREAM);
-        return new BaseNotificationPublisherReg(notificationPublisherRegistration);
+        return new BaseNotificationPublisherReg(transformUtil, notificationPublisherRegistration);
+    }
+
+    @Override
+    public YangLibraryPublisherRegistration registerYangLibraryPublisher() {
+        final NotificationPublisherRegistration notificationPublisherRegistration =
+                registerNotificationPublisher(BASE_NETCONF_STREAM);
+        return new YangLibraryPublisherReg(transformUtil, notificationPublisherRegistration);
     }
 
     private static class GenericNotificationPublisherReg implements NotificationPublisherRegistration {
@@ -250,8 +271,8 @@ public class NetconfNotificationManager implements NetconfNotificationCollector,
 
         @Override
         public void onNotification(final StreamNameType stream, final NetconfNotification notification) {
-            Preconditions.checkState(baseListener != null, "Already closed");
-            Preconditions.checkArgument(stream.equals(registeredStream));
+            checkState(baseListener != null, "Already closed");
+            checkArgument(stream.equals(registeredStream));
             baseListener.onNotification(stream, notification);
         }
     }
@@ -263,8 +284,11 @@ public class NetconfNotificationManager implements NetconfNotificationCollector,
         static final SchemaPath SESSION_END_PATH = SchemaPath.create(true, NetconfSessionEnd.QNAME);
 
         private final NotificationPublisherRegistration baseRegistration;
+        private final NotificationsTransformUtil transformUtil;
 
-        BaseNotificationPublisherReg(final NotificationPublisherRegistration baseRegistration) {
+        BaseNotificationPublisherReg(final NotificationsTransformUtil transformUtil,
+                final NotificationPublisherRegistration baseRegistration) {
+            this.transformUtil = requireNonNull(transformUtil);
             this.baseRegistration = baseRegistration;
         }
 
@@ -273,9 +297,8 @@ public class NetconfNotificationManager implements NetconfNotificationCollector,
             baseRegistration.close();
         }
 
-        private static NetconfNotification serializeNotification(final Notification notification,
-                final SchemaPath path) {
-            return NotificationsTransformUtil.transform(notification, path);
+        private NetconfNotification serializeNotification(final Notification notification, final SchemaPath path) {
+            return transformUtil.transform(notification, path);
         }
 
         @Override
@@ -295,6 +318,38 @@ public class NetconfNotificationManager implements NetconfNotificationCollector,
         }
     }
 
+    private static class YangLibraryPublisherReg implements YangLibraryPublisherRegistration {
+        static final SchemaPath YANG_LIBRARY_CHANGE_PATH = SchemaPath.create(true, YangLibraryChange.QNAME);
+        static final SchemaPath YANG_LIBRARY_UPDATE_PATH = SchemaPath.create(true, YangLibraryUpdate.QNAME);
+
+        private final NotificationPublisherRegistration baseRegistration;
+        private final NotificationsTransformUtil transformUtil;
+
+        YangLibraryPublisherReg(final NotificationsTransformUtil transformUtil,
+                final NotificationPublisherRegistration baseRegistration) {
+            this.transformUtil = requireNonNull(transformUtil);
+            this.baseRegistration = baseRegistration;
+        }
+
+        @Override
+        @Deprecated
+        public void onYangLibraryChange(final YangLibraryChange yangLibraryChange) {
+            baseRegistration.onNotification(BASE_STREAM_NAME,
+                transformUtil.transform(yangLibraryChange, YANG_LIBRARY_CHANGE_PATH));
+        }
+
+        @Override
+        public void onYangLibraryUpdate(YangLibraryUpdate yangLibraryUpdate) {
+            baseRegistration.onNotification(BASE_STREAM_NAME,
+                    transformUtil.transform(yangLibraryUpdate, YANG_LIBRARY_UPDATE_PATH));
+        }
+
+        @Override
+        public void close() {
+            baseRegistration.close();
+        }
+    }
+
     private class GenericNotificationListenerReg implements NotificationListenerRegistration {
         private final NetconfNotificationListener listener;