Remove OperationalDatastoreListener 27/105727/8
authorRobert Varga <robert.varga@pantheon.tech>
Fri, 28 Apr 2023 08:18:58 +0000 (10:18 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Fri, 28 Apr 2023 13:30:44 +0000 (15:30 +0200)
This is a pretty useless indirection, which is forcing us to store the
listener path in the object. Remove the class and adjust its users --
which allows us to no retain the listened path.

Change-Id: Id9c07d487c315dddbb1c072082aa4465c67a04db
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
apps/netconf-events-mdsal/src/main/java/org/opendaylight/netconf/server/events/mdsal/CapabilityChangeNotificationProducer.java
apps/netconf-events-mdsal/src/main/java/org/opendaylight/netconf/server/events/mdsal/OperationalDatastoreListener.java [deleted file]
apps/netconf-events-mdsal/src/main/java/org/opendaylight/netconf/server/events/mdsal/SessionNotificationProducer.java
apps/netconf-events-mdsal/src/main/java/org/opendaylight/netconf/server/events/mdsal/YangLibraryNotificationProducer.java
apps/netconf-events-mdsal/src/main/java/org/opendaylight/netconf/server/events/mdsal/YangLibraryNotificationProducerRFC8525.java
apps/netconf-events-mdsal/src/test/java/org/opendaylight/netconf/server/events/mdsal/OperationalDatastoreListenerTest.java [deleted file]

index daa27d1227da49bde29ddd235e86a54cd4ac7d8d..b2b13119c87f366550c8d5677f982d7630b768b7 100644 (file)
@@ -13,7 +13,10 @@ import java.util.Collection;
 import java.util.Set;
 import org.opendaylight.mdsal.binding.api.DataBroker;
 import org.opendaylight.mdsal.binding.api.DataObjectModification;
+import org.opendaylight.mdsal.binding.api.DataTreeChangeListener;
+import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
 import org.opendaylight.mdsal.binding.api.DataTreeModification;
+import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
 import org.opendaylight.netconf.server.api.notifications.BaseNotificationPublisherRegistration;
 import org.opendaylight.netconf.server.api.notifications.NetconfNotificationCollector;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
@@ -33,15 +36,11 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 /**
- * Listens on capabilities changes in data store and publishes them to base
- * netconf notification stream listener.
+ * Listens on capabilities changes in data store and publishes them to base NETCONF notification stream listener.
  */
 @Component(service = { })
-public final class CapabilityChangeNotificationProducer extends OperationalDatastoreListener<Capabilities>
-        implements AutoCloseable {
+public final class CapabilityChangeNotificationProducer implements DataTreeChangeListener<Capabilities>, AutoCloseable {
     private static final Logger LOG = LoggerFactory.getLogger(CapabilityChangeNotificationProducer.class);
-    private static final InstanceIdentifier<Capabilities> CAPABILITIES_INSTANCE_IDENTIFIER =
-            InstanceIdentifier.builder(NetconfState.class).child(Capabilities.class).build();
 
     private final BaseNotificationPublisherRegistration baseNotificationPublisherRegistration;
     private final ListenerRegistration<?> capabilityChangeListenerRegistration;
@@ -50,9 +49,10 @@ public final class CapabilityChangeNotificationProducer extends OperationalDatas
     public CapabilityChangeNotificationProducer(
             @Reference(target = "(type=netconf-notification-manager)") final NetconfNotificationCollector notifManager,
             @Reference final DataBroker dataBroker) {
-        super(CAPABILITIES_INSTANCE_IDENTIFIER);
         baseNotificationPublisherRegistration = notifManager.registerBaseNotificationPublisher();
-        capabilityChangeListenerRegistration = registerOnChanges(dataBroker);
+        capabilityChangeListenerRegistration = dataBroker.registerDataTreeChangeListener(
+                DataTreeIdentifier.create(LogicalDatastoreType.OPERATIONAL,
+                    InstanceIdentifier.builder(NetconfState.class).child(Capabilities.class).build()), this);
     }
 
     @Deactivate
diff --git a/apps/netconf-events-mdsal/src/main/java/org/opendaylight/netconf/server/events/mdsal/OperationalDatastoreListener.java b/apps/netconf-events-mdsal/src/main/java/org/opendaylight/netconf/server/events/mdsal/OperationalDatastoreListener.java
deleted file mode 100644 (file)
index db551d0..0000000
+++ /dev/null
@@ -1,48 +0,0 @@
-/*
- * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
- */
-package org.opendaylight.netconf.server.events.mdsal;
-
-import static java.util.Objects.requireNonNull;
-
-import org.opendaylight.mdsal.binding.api.DataBroker;
-import org.opendaylight.mdsal.binding.api.DataTreeChangeListener;
-import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
-import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
-import org.opendaylight.yangtools.concepts.ListenerRegistration;
-import org.opendaylight.yangtools.yang.binding.DataObject;
-import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
-
-/**
- * Abstract base class for subclasses, which want to listen for changes on specified subtree in operational datastore.
- *
- * @param <T> data object class
- */
-abstract class OperationalDatastoreListener<T extends DataObject> implements DataTreeChangeListener<T> {
-    private final InstanceIdentifier<T> instanceIdentifier;
-
-    /**
-     * Constructor.
-     *
-     * @param instanceIdentifier instance identifier of subtree, on which this instance should listen on changes.
-     */
-    OperationalDatastoreListener(final InstanceIdentifier<T> instanceIdentifier) {
-        this.instanceIdentifier = requireNonNull(instanceIdentifier);
-    }
-
-    /**
-     * Registers this instance as OPERATIONAL datastore listener via provided dataBroker.
-     *
-     * @param dataBroker data broker
-     * @return listener registration
-     */
-    ListenerRegistration<OperationalDatastoreListener<T>> registerOnChanges(final DataBroker dataBroker) {
-        DataTreeIdentifier<T> id = DataTreeIdentifier.create(LogicalDatastoreType.OPERATIONAL, instanceIdentifier);
-        return dataBroker.registerDataTreeChangeListener(id, this);
-    }
-
-}
index 9a67c04708b07e5736ba7f226e866b82ce239af5..467c2b431591d9694a7cdd94fb91fbba73adec6e 100644 (file)
@@ -10,7 +10,10 @@ package org.opendaylight.netconf.server.events.mdsal;
 import java.util.Collection;
 import org.opendaylight.mdsal.binding.api.DataBroker;
 import org.opendaylight.mdsal.binding.api.DataObjectModification;
+import org.opendaylight.mdsal.binding.api.DataTreeChangeListener;
+import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
 import org.opendaylight.mdsal.binding.api.DataTreeModification;
+import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
 import org.opendaylight.netconf.server.api.notifications.BaseNotificationPublisherRegistration;
 import org.opendaylight.netconf.server.api.notifications.NetconfNotificationCollector;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.SessionIdOrZeroType;
@@ -32,10 +35,8 @@ import org.slf4j.LoggerFactory;
  * Listens on changes in NetconfState/Sessions/Session datastore and publishes them.
  */
 @Component(service = { })
-public class SessionNotificationProducer extends OperationalDatastoreListener<Session> {
+public final class SessionNotificationProducer implements DataTreeChangeListener<Session>, AutoCloseable {
     private static final Logger LOG = LoggerFactory.getLogger(SessionNotificationProducer.class);
-    private static final InstanceIdentifier<Session> SESSION_INSTANCE_IDENTIFIER =
-            InstanceIdentifier.builder(NetconfState.class).child(Sessions.class).child(Session.class).build();
 
     private final BaseNotificationPublisherRegistration baseNotificationPublisherRegistration;
     private final ListenerRegistration<?> sessionListenerRegistration;
@@ -44,12 +45,14 @@ public class SessionNotificationProducer extends OperationalDatastoreListener<Se
     public SessionNotificationProducer(
             @Reference(target = "(type=netconf-notification-manager)") final NetconfNotificationCollector notifManager,
             @Reference final DataBroker dataBroker) {
-        super(SESSION_INSTANCE_IDENTIFIER);
-
         baseNotificationPublisherRegistration = notifManager.registerBaseNotificationPublisher();
-        sessionListenerRegistration = registerOnChanges(dataBroker);
+        sessionListenerRegistration = dataBroker.registerDataTreeChangeListener(
+            DataTreeIdentifier.create(LogicalDatastoreType.OPERATIONAL,
+                InstanceIdentifier.builder(NetconfState.class).child(Sessions.class).child(Session.class).build()),
+            this);
     }
 
+    @Override
     @Deactivate
     public void close() {
         if (baseNotificationPublisherRegistration != null) {
index 5de59c075b906fbf861d80d1ebca6338ec019e63..647e21f6e856d6fc653a61a6f801a8abfdcf2dda 100644 (file)
@@ -9,7 +9,10 @@ package org.opendaylight.netconf.server.events.mdsal;
 
 import java.util.Collection;
 import org.opendaylight.mdsal.binding.api.DataBroker;
+import org.opendaylight.mdsal.binding.api.DataTreeChangeListener;
+import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
 import org.opendaylight.mdsal.binding.api.DataTreeModification;
+import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
 import org.opendaylight.netconf.server.api.notifications.NetconfNotificationCollector;
 import org.opendaylight.netconf.server.api.notifications.YangLibraryPublisherRegistration;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.ModulesState;
@@ -22,20 +25,15 @@ import org.osgi.service.component.annotations.Deactivate;
 import org.osgi.service.component.annotations.Reference;
 
 /**
- * Listens on the set of modules and submodules changes in data store and publishes
- * to base netconf notification stream listener a server-specific identifier representing
- * the current set of modules and submodules.
+ * Listens on the set of modules and submodules changes in data store and publishes to base NETCONF notification stream
+ * listener a server-specific identifier representing the current set of modules and submodules.
  *
  * @deprecated ietf-yang-library:yang-library-change was deprecated in the new RFC8525.
  *             Use {@link YangLibraryNotificationProducerRFC8525}.
  */
 @Component(service = { })
 @Deprecated(forRemoval = true)
-public final class YangLibraryNotificationProducer extends OperationalDatastoreListener<ModulesState>
-        implements AutoCloseable {
-    private static final InstanceIdentifier<ModulesState> MODULES_STATE_INSTANCE_IDENTIFIER =
-            InstanceIdentifier.create(ModulesState.class);
-
+public final class YangLibraryNotificationProducer implements DataTreeChangeListener<ModulesState>, AutoCloseable {
     private final ListenerRegistration<?> yangLibraryChangeListenerRegistration;
     private final YangLibraryPublisherRegistration yangLibraryPublisherRegistration;
 
@@ -43,9 +41,10 @@ public final class YangLibraryNotificationProducer extends OperationalDatastoreL
     public YangLibraryNotificationProducer(
             @Reference(target = "(type=netconf-notification-manager)") final NetconfNotificationCollector notifManager,
             @Reference final DataBroker dataBroker) {
-        super(MODULES_STATE_INSTANCE_IDENTIFIER);
         yangLibraryPublisherRegistration = notifManager.registerYangLibraryPublisher();
-        yangLibraryChangeListenerRegistration = registerOnChanges(dataBroker);
+        yangLibraryChangeListenerRegistration = dataBroker.registerDataTreeChangeListener(
+            DataTreeIdentifier.create(LogicalDatastoreType.OPERATIONAL, InstanceIdentifier.create(ModulesState.class)),
+            this);
     }
 
     @Deactivate
index 98a8abc3f488980c2552eb79fd8f803e93a311ad..ccd645ed921d30375cab513d90186b16c72bb955 100644 (file)
@@ -9,7 +9,10 @@ package org.opendaylight.netconf.server.events.mdsal;
 
 import java.util.Collection;
 import org.opendaylight.mdsal.binding.api.DataBroker;
+import org.opendaylight.mdsal.binding.api.DataTreeChangeListener;
+import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
 import org.opendaylight.mdsal.binding.api.DataTreeModification;
+import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
 import org.opendaylight.netconf.server.api.notifications.NetconfNotificationCollector;
 import org.opendaylight.netconf.server.api.notifications.YangLibraryPublisherRegistration;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.YangLibrary;
@@ -22,16 +25,13 @@ import org.osgi.service.component.annotations.Deactivate;
 import org.osgi.service.component.annotations.Reference;
 
 /**
- * Listens on the modules, submodules, datastores, and datastore schemas changes in data store and publishes
- * to base netconf notification stream listener a server-specific identifier representing
- * the current set of modules, submodules, datastores, and datastore schemas.
+ * Listens on the modules, submodules, datastores, and datastore schemas changes in data store and publishes to base
+ * NETCONF notification stream listener a server-specific identifier representing the current set of modules,
+ * submodules, datastores, and datastore schemas.
  */
 @Component(service = { })
-public final class YangLibraryNotificationProducerRFC8525 extends OperationalDatastoreListener<YangLibrary>
-        implements AutoCloseable {
-    private static final InstanceIdentifier<YangLibrary> YANG_LIBRARY_INSTANCE_IDENTIFIER =
-            InstanceIdentifier.create(YangLibrary.class);
-
+public final class YangLibraryNotificationProducerRFC8525
+        implements DataTreeChangeListener<YangLibrary>, AutoCloseable {
     private final ListenerRegistration<?> yangLibraryChangeListenerRegistration;
     private final YangLibraryPublisherRegistration yangLibraryPublisherRegistration;
 
@@ -39,9 +39,10 @@ public final class YangLibraryNotificationProducerRFC8525 extends OperationalDat
     public YangLibraryNotificationProducerRFC8525(
             @Reference(target = "(type=netconf-notification-manager)") final NetconfNotificationCollector notifManager,
             @Reference final DataBroker dataBroker) {
-        super(YANG_LIBRARY_INSTANCE_IDENTIFIER);
         yangLibraryPublisherRegistration = notifManager.registerYangLibraryPublisher();
-        yangLibraryChangeListenerRegistration = registerOnChanges(dataBroker);
+        yangLibraryChangeListenerRegistration = dataBroker.registerDataTreeChangeListener(
+            DataTreeIdentifier.create(LogicalDatastoreType.OPERATIONAL, InstanceIdentifier.create(YangLibrary.class)),
+            this);
     }
 
     @Deactivate
diff --git a/apps/netconf-events-mdsal/src/test/java/org/opendaylight/netconf/server/events/mdsal/OperationalDatastoreListenerTest.java b/apps/netconf-events-mdsal/src/test/java/org/opendaylight/netconf/server/events/mdsal/OperationalDatastoreListenerTest.java
deleted file mode 100644 (file)
index fa09fe1..0000000
+++ /dev/null
@@ -1,62 +0,0 @@
-/*
- * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
- */
-package org.opendaylight.netconf.server.events.mdsal;
-
-import static org.junit.Assert.assertEquals;
-import static org.mockito.ArgumentMatchers.any;
-import static org.mockito.Mockito.doReturn;
-import static org.mockito.Mockito.verify;
-
-import java.util.Collection;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.mockito.ArgumentCaptor;
-import org.mockito.Captor;
-import org.mockito.Mock;
-import org.mockito.junit.MockitoJUnitRunner;
-import org.opendaylight.mdsal.binding.api.DataBroker;
-import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
-import org.opendaylight.mdsal.binding.api.DataTreeModification;
-import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
-import org.opendaylight.yangtools.yang.binding.ChildOf;
-import org.opendaylight.yangtools.yang.binding.DataRoot;
-import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
-
-@RunWith(MockitoJUnitRunner.StrictStubs.class)
-public class OperationalDatastoreListenerTest {
-    @Mock
-    private DataBroker dataBroker;
-    @Captor
-    private ArgumentCaptor<DataTreeIdentifier<?>> argumentId;
-
-    @Test
-    public void testDataStoreListener() {
-        final InstanceIdentifier<TestInterface> instanceIdentifier = InstanceIdentifier.create(TestInterface.class);
-
-        final var op = new OperationalDatastoreListener<>(instanceIdentifier) {
-            @Override
-            public void onDataTreeChanged(final Collection<DataTreeModification<TestInterface>> collection) {
-                // no-op
-            }
-        };
-        doReturn(null).when(dataBroker).registerDataTreeChangeListener(any(), any());
-
-        op.registerOnChanges(dataBroker);
-        verify(dataBroker).registerDataTreeChangeListener(argumentId.capture(), any());
-
-        assertEquals(DataTreeIdentifier.create(LogicalDatastoreType.OPERATIONAL, instanceIdentifier),
-            argumentId.getValue());
-    }
-
-    interface TestInterface extends ChildOf<DataRoot> {
-        @Override
-        default Class<TestInterface> implementedInterface() {
-            return TestInterface.class;
-        }
-    }
-}