Clean up mdsal-netconf-notification
[netconf.git] / netconf / mdsal-netconf-notification / src / main / java / org / opendaylight / netconf / mdsal / notification / impl / NetconfNotificationManager.java
index d26b2cbd4ac5af7a406dcf617286579d0270dae8..0960aaa37212e3bffdbbe7ae0a356349b2bf0e9c 100644 (file)
@@ -16,16 +16,17 @@ import com.google.common.collect.HashMultiset;
 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.ArrayList;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Map;
 import java.util.Set;
+import javax.annotation.PreDestroy;
 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.mdsal.binding.dom.codec.spi.BindingDOMCodecFactory;
+import org.opendaylight.mdsal.binding.runtime.api.BindingRuntimeGenerator;
 import org.opendaylight.netconf.notifications.BaseNotificationPublisherRegistration;
 import org.opendaylight.netconf.notifications.NetconfNotification;
 import org.opendaylight.netconf.notifications.NetconfNotificationCollector;
@@ -47,7 +48,13 @@ import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.not
 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.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
+import org.opendaylight.yangtools.yang.parser.api.YangParserException;
+import org.opendaylight.yangtools.yang.parser.api.YangParserFactory;
+import org.osgi.service.component.annotations.Activate;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Deactivate;
+import org.osgi.service.component.annotations.Reference;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -55,9 +62,10 @@ import org.slf4j.LoggerFactory;
  *  A thread-safe implementation NetconfNotificationRegistry.
  */
 @Singleton
-public class NetconfNotificationManager implements NetconfNotificationCollector, NetconfNotificationRegistry,
+@Component(service = { NetconfNotificationCollector.class, NetconfNotificationRegistry.class }, immediate = true,
+           property = "type=netconf-notification-manager")
+public final class NetconfNotificationManager implements NetconfNotificationCollector, NetconfNotificationRegistry,
         NetconfNotificationListener, AutoCloseable {
-
     public static final StreamNameType BASE_STREAM_NAME = new StreamNameType("NETCONF");
     public static final Stream BASE_NETCONF_STREAM = new StreamBuilder()
                 .setName(BASE_STREAM_NAME)
@@ -91,8 +99,33 @@ public class NetconfNotificationManager implements NetconfNotificationCollector,
     private final NotificationsTransformUtil transformUtil;
 
     @Inject
-    public NetconfNotificationManager(final NotificationsTransformUtil transformUtil) {
-        this.transformUtil = requireNonNull(transformUtil);
+    @Activate
+    public NetconfNotificationManager(@Reference final YangParserFactory parserFactory,
+            @Reference final BindingRuntimeGenerator generator, @Reference final BindingDOMCodecFactory codecFactory)
+                throws YangParserException {
+        transformUtil = new NotificationsTransformUtil(parserFactory, generator, codecFactory);
+    }
+
+    @PreDestroy
+    @Deactivate
+    @Override
+    public synchronized void close() {
+        // Unregister all listeners
+        // Use new list to avoid ConcurrentModificationException
+        for (final GenericNotificationListenerReg listenerReg : new ArrayList<>(notificationListeners.values())) {
+            listenerReg.close();
+        }
+        notificationListeners.clear();
+
+        // Unregister all publishers
+        // Use new list to avoid ConcurrentModificationException
+        for (final GenericNotificationPublisherReg notificationPublisher : new ArrayList<>(notificationPublishers)) {
+            notificationPublisher.close();
+        }
+        notificationPublishers.clear();
+
+        // Clear stream Listeners
+        streamListeners.clear();
     }
 
     @Override
@@ -157,26 +190,6 @@ public class NetconfNotificationManager implements NetconfNotificationCollector,
         };
     }
 
-    @Override
-    public synchronized void close() {
-        // Unregister all listeners
-        // Use new list to avoid ConcurrentModificationException
-        for (final GenericNotificationListenerReg listenerReg : new ArrayList<>(notificationListeners.values())) {
-            listenerReg.close();
-        }
-        notificationListeners.clear();
-
-        // Unregister all publishers
-        // Use new list to avoid ConcurrentModificationException
-        for (final GenericNotificationPublisherReg notificationPublisher : new ArrayList<>(notificationPublishers)) {
-            notificationPublisher.close();
-        }
-        notificationPublishers.clear();
-
-        // Clear stream Listeners
-        streamListeners.clear();
-    }
-
     @Override
     public synchronized NotificationPublisherRegistration registerNotificationPublisher(final Stream stream) {
         final StreamNameType streamName = requireNonNull(stream).getName();
@@ -210,8 +223,6 @@ public class NetconfNotificationManager implements NetconfNotificationCollector,
         return reg;
     }
 
-    @SuppressFBWarnings(value = "UPM_UNCALLED_PRIVATE_METHOD",
-            justification = "https://github.com/spotbugs/spotbugs/issues/811")
     private void unregisterNotificationPublisher(
             final StreamNameType streamName,
             final GenericNotificationPublisherReg genericNotificationPublisherReg) {
@@ -279,10 +290,9 @@ public class NetconfNotificationManager implements NetconfNotificationCollector,
     }
 
     private static class BaseNotificationPublisherReg implements BaseNotificationPublisherRegistration {
-
-        static final SchemaPath CAPABILITY_CHANGE_SCHEMA_PATH = SchemaPath.create(true, NetconfCapabilityChange.QNAME);
-        static final SchemaPath SESSION_START_PATH = SchemaPath.create(true, NetconfSessionStart.QNAME);
-        static final SchemaPath SESSION_END_PATH = SchemaPath.create(true, NetconfSessionEnd.QNAME);
+        static final Absolute CAPABILITY_CHANGE_SCHEMA_PATH = Absolute.of(NetconfCapabilityChange.QNAME);
+        static final Absolute SESSION_START_PATH = Absolute.of(NetconfSessionStart.QNAME);
+        static final Absolute SESSION_END_PATH = Absolute.of(NetconfSessionEnd.QNAME);
 
         private final NotificationPublisherRegistration baseRegistration;
         private final NotificationsTransformUtil transformUtil;
@@ -298,7 +308,7 @@ public class NetconfNotificationManager implements NetconfNotificationCollector,
             baseRegistration.close();
         }
 
-        private NetconfNotification serializeNotification(final Notification notification, final SchemaPath path) {
+        private NetconfNotification serializeNotification(final Notification<?> notification, final Absolute path) {
             return transformUtil.transform(notification, path);
         }
 
@@ -320,8 +330,8 @@ 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);
+        static final Absolute YANG_LIBRARY_CHANGE_PATH = Absolute.of(YangLibraryChange.QNAME);
+        static final Absolute YANG_LIBRARY_UPDATE_PATH = Absolute.of(YangLibraryUpdate.QNAME);
 
         private final NotificationPublisherRegistration baseRegistration;
         private final NotificationsTransformUtil transformUtil;