Migrate SessionNotificationProducer 23/111723/1
authorRobert Varga <robert.varga@pantheon.tech>
Sat, 18 May 2024 22:52:10 +0000 (00:52 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Sat, 18 May 2024 22:52:10 +0000 (00:52 +0200)
Sessions pertain to oper state, hence in terms of notifications they are
cluster-global. This changes behaviour towards reflecting what the
injected DOMDataBroker is bound to see.

Change-Id: I35549c5a8ec59364de05fd18e5192992d7d69906
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
plugins/netconf-server-mdsal/src/main/java/org/opendaylight/netconf/server/mdsal/notifications/SessionNotificationProducer.java
plugins/netconf-server-mdsal/src/test/java/org/opendaylight/netconf/server/mdsal/notifications/SessionNotificationProducerTest.java

index f2081ecfaa418d44561471bce0ac600da9deb8e6..173e73e4642cc908eb0c877b75aa1f85e8fec1b0 100644 (file)
@@ -45,7 +45,7 @@ public final class SessionNotificationProducer implements DataTreeChangeListener
             @Reference(target = "(type=netconf-notification-manager)") final NetconfNotificationCollector notifManager,
             @Reference final DataBroker dataBroker) {
         baseNotificationPublisherRegistration = notifManager.registerBaseNotificationPublisher();
-        sessionListenerRegistration = dataBroker.registerDataTreeChangeListener(
+        sessionListenerRegistration = dataBroker.registerTreeChangeListener(
             DataTreeIdentifier.of(LogicalDatastoreType.OPERATIONAL,
                 InstanceIdentifier.builder(NetconfState.class).child(Sessions.class).child(Session.class).build()),
             this);
index ae858c02b43dec37f20b2a07698672b7efcf0583..335d4d4bdad7732b0da21e405bf6fe51a7db643a 100644 (file)
@@ -11,7 +11,6 @@ import static org.junit.Assert.assertEquals;
 import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.Mockito.doNothing;
 import static org.mockito.Mockito.doReturn;
-import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.never;
 import static org.mockito.Mockito.verify;
 
@@ -25,8 +24,6 @@ import org.mockito.junit.MockitoJUnitRunner;
 import org.opendaylight.mdsal.binding.api.DataBroker;
 import org.opendaylight.mdsal.binding.api.DataObjectModification;
 import org.opendaylight.mdsal.binding.api.DataObjectModification.ModificationType;
-import org.opendaylight.mdsal.binding.api.DataTreeChangeListener;
-import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
 import org.opendaylight.mdsal.binding.api.DataTreeModification;
 import org.opendaylight.netconf.server.api.notifications.BaseNotificationPublisherRegistration;
 import org.opendaylight.netconf.server.api.notifications.NetconfNotificationCollector;
@@ -55,13 +52,16 @@ public class SessionNotificationProducerTest {
     private NetconfNotificationCollector netconfNotificationCollector;
     @Mock
     private DataBroker dataBroker;
+    @Mock
+    private DataTreeModification<Session> treeChange;
+    @Mock
+    private DataObjectModification<Session> changeObject;
 
     private SessionNotificationProducer publisher;
 
     @Before
     public void setUp() {
-        doReturn(listenerRegistration).when(dataBroker).registerDataTreeChangeListener(any(DataTreeIdentifier.class),
-                any(DataTreeChangeListener.class));
+        doReturn(listenerRegistration).when(dataBroker).registerTreeChangeListener(any(), any());
         doNothing().when(registration).onSessionStarted(any());
         doNothing().when(registration).onSessionEnded(any());
 
@@ -72,12 +72,12 @@ public class SessionNotificationProducerTest {
 
     @Test
     public void testOnDataChangedSessionCreated() throws Exception {
-        final Session session = createSession(Uint32.ONE);
-        final DataTreeModification<Session> treeChange = getTreeModification(session, ModificationType.WRITE);
-        publisher.onDataTreeChanged(List.of(treeChange));
-        ArgumentCaptor<NetconfSessionStart> captor = ArgumentCaptor.forClass(NetconfSessionStart.class);
+        final var session = createSession(Uint32.ONE);
+        final var treeMod = getTreeModification(session, ModificationType.WRITE);
+        publisher.onDataTreeChanged(List.of(treeMod));
+        final var captor = ArgumentCaptor.forClass(NetconfSessionStart.class);
         verify(registration).onSessionStarted(captor.capture());
-        final NetconfSessionStart value = captor.getValue();
+        final var value = captor.getValue();
         assertEquals(session.getSessionId(), value.getSessionId().getValue());
         assertEquals(session.getSourceHost().getIpAddress(), value.getSourceHost());
         assertEquals(session.getUsername(), value.getUsername());
@@ -85,10 +85,8 @@ public class SessionNotificationProducerTest {
 
     @Test
     public void testOnDataChangedSessionUpdated() throws Exception {
-        final DataTreeModification<Session> treeChange = mock(DataTreeModification.class);
-        final DataObjectModification<Session> changeObject = mock(DataObjectModification.class);
-        final Session sessionBefore = createSessionWithInRpcCount(Uint32.ONE, Uint32.ZERO);
-        final Session sessionAfter = createSessionWithInRpcCount(Uint32.ONE, Uint32.ONE);
+        final var sessionBefore = createSessionWithInRpcCount(Uint32.ONE, Uint32.ZERO);
+        final var sessionAfter = createSessionWithInRpcCount(Uint32.ONE, Uint32.ONE);
         doReturn(sessionBefore).when(changeObject).dataBefore();
         doReturn(sessionAfter).when(changeObject).dataAfter();
         doReturn(ModificationType.WRITE).when(changeObject).modificationType();
@@ -101,10 +99,10 @@ public class SessionNotificationProducerTest {
 
     @Test
     public void testOnDataChangedSessionDeleted() throws Exception {
-        final Session session = createSession(Uint32.ONE);
-        final DataTreeModification<Session> data = getTreeModification(session, ModificationType.DELETE);
-        publisher.onDataTreeChanged(List.of(data));
-        ArgumentCaptor<NetconfSessionEnd> captor = ArgumentCaptor.forClass(NetconfSessionEnd.class);
+        final var session = createSession(Uint32.ONE);
+        final var treeMod = getTreeModification(session, ModificationType.DELETE);
+        publisher.onDataTreeChanged(List.of(treeMod));
+        final var captor = ArgumentCaptor.forClass(NetconfSessionEnd.class);
         verify(registration).onSessionEnded(captor.capture());
         final NetconfSessionEnd value = captor.getValue();
         assertEquals(session.getSessionId(), value.getSessionId().getValue());
@@ -118,32 +116,30 @@ public class SessionNotificationProducerTest {
 
     private static Session createSessionWithInRpcCount(final Uint32 id, final Uint32 inRpc) {
         return new SessionBuilder()
-                .setSessionId(id)
-                .setSourceHost(new Host(new IpAddress(new Ipv4Address("0.0.0.0"))))
-                .setUsername("user")
-                .setInRpcs(new ZeroBasedCounter32(inRpc))
-                .build();
+            .setSessionId(id)
+            .setSourceHost(new Host(new IpAddress(new Ipv4Address("0.0.0.0"))))
+            .setUsername("user")
+            .setInRpcs(new ZeroBasedCounter32(inRpc))
+            .build();
     }
 
-    @SuppressWarnings("unchecked")
-    private static DataTreeModification<Session> getTreeModification(final Session session,
-            final ModificationType type) {
-        final DataTreeModification<Session> treeChange = mock(DataTreeModification.class);
-        final DataObjectModification<Session> changeObject = mock(DataObjectModification.class);
-        switch (type) {
-            case WRITE:
+    private DataTreeModification<Session> getTreeModification(final Session session, final ModificationType type) {
+        doReturn(type).when(changeObject).modificationType();
+        doReturn(changeObject).when(treeChange).getRootNode();
+        return switch (type) {
+            case WRITE -> {
                 doReturn(null).when(changeObject).dataBefore();
                 doReturn(session).when(changeObject).dataAfter();
-                break;
-            case DELETE:
+                yield treeChange;
+            }
+            case DELETE -> {
                 doReturn(session).when(changeObject).dataBefore();
-                break;
-            default:
+                yield treeChange;
+            }
+            default -> {
                 LOG.debug("Received intentionally unhandled type: {}.", type);
-        }
-        doReturn(type).when(changeObject).modificationType();
-        doReturn(changeObject).when(treeChange).getRootNode();
-        return treeChange;
+                yield treeChange;
+            }
+        };
     }
-
 }