Convert NetconfEventSourceManager to DataTreeChangeListener 60/62460/2
authorTom Pantelis <tompantelis@gmail.com>
Wed, 30 Aug 2017 18:01:33 +0000 (14:01 -0400)
committerTom Pantelis <tompantelis@gmail.com>
Wed, 30 Aug 2017 18:32:59 +0000 (14:32 -0400)
DataChangeListener has been deprecated since Carbon with possible removal
in Oxygen so convert to DataTreeChangeListener.

Change-Id: I23e4318a9973a7cffcaa95c42ac6df7449b59b61
Signed-off-by: Tom Pantelis <tompantelis@gmail.com>
netconf/messagebus-netconf/src/main/java/org/opendaylight/netconf/messagebus/eventsources/netconf/NetconfEventSourceManager.java
netconf/messagebus-netconf/src/test/java/org/opendaylight/netconf/messagebus/eventsources/netconf/NetconfEventSourceManagerTest.java

index 516c595ef7e98a8e3f17d97100e6cb5088180d0d..91df5f781218707acccf96c1f1319d06627af033 100644 (file)
@@ -9,12 +9,14 @@
 package org.opendaylight.netconf.messagebus.eventsources.netconf;
 
 import com.google.common.base.Preconditions;
+import java.util.Collection;
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
-import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
-import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
-import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
+import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
+import org.opendaylight.controller.md.sal.binding.api.DataTreeChangeListener;
+import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
+import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
 import org.opendaylight.controller.md.sal.dom.api.DOMNotificationPublishService;
@@ -27,7 +29,6 @@ import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
 import org.opendaylight.yangtools.concepts.ListenerRegistration;
-import org.opendaylight.yangtools.yang.binding.DataObject;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -36,7 +37,7 @@ import org.slf4j.LoggerFactory;
  * NetconfEventSourceManager implements DataChangeListener. On topology changes, it manages creation,
  * updating and removing registrations of event sources.
  */
-public final class NetconfEventSourceManager implements DataChangeListener, AutoCloseable {
+public final class NetconfEventSourceManager implements DataTreeChangeListener<Node>, AutoCloseable {
 
     private static final Logger LOG = LoggerFactory.getLogger(NetconfEventSourceManager.class);
     private static final TopologyKey NETCONF_TOPOLOGY_KEY = new TopologyKey(
@@ -49,7 +50,7 @@ public final class NetconfEventSourceManager implements DataChangeListener, Auto
             new ConcurrentHashMap<>();
     private final DOMNotificationPublishService publishService;
     private final DOMMountPointService domMounts;
-    private ListenerRegistration<DataChangeListener> listenerRegistration;
+    private ListenerRegistration<NetconfEventSourceManager> listenerRegistration;
     private final EventSourceRegistry eventSourceRegistry;
     private final DataBroker dataBroker;
 
@@ -72,35 +73,29 @@ public final class NetconfEventSourceManager implements DataChangeListener, Auto
      */
     public void initialize() {
         Preconditions.checkNotNull(dataBroker);
-        listenerRegistration = dataBroker
-                .registerDataChangeListener(LogicalDatastoreType.OPERATIONAL, NETCONF_DEVICE_PATH, this,
-                        DataChangeScope.SUBTREE);
+        listenerRegistration = dataBroker.registerDataTreeChangeListener(new DataTreeIdentifier<>(
+                LogicalDatastoreType.OPERATIONAL, NETCONF_DEVICE_PATH), this);
         LOG.info("NetconfEventSourceManager initialized.");
     }
 
     @Override
-    public void onDataChanged(final AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> event) {
-
-        LOG.debug("[DataChangeEvent<InstanceIdentifier<?>, DataObject>: {}]", event);
-        for (final Map.Entry<InstanceIdentifier<?>, DataObject> changeEntry : event.getCreatedData().entrySet()) {
-            if (changeEntry.getValue() instanceof Node) {
-                nodeCreated(changeEntry.getKey(), (Node) changeEntry.getValue());
+    public void onDataTreeChanged(Collection<DataTreeModification<Node>> changes) {
+        for (DataTreeModification<Node> change: changes) {
+            LOG.debug("DataTreeModification: {}", change);
+            final DataObjectModification<Node> rootNode = change.getRootNode();
+            final InstanceIdentifier<Node> identifier = change.getRootPath().getRootIdentifier();
+            switch (rootNode.getModificationType()) {
+                case WRITE:
+                case SUBTREE_MODIFIED:
+                    nodeCreated(identifier, rootNode.getDataAfter());
+                    break;
+                case DELETE:
+                    nodeRemoved(identifier);
+                    break;
+                default:
+                    break;
             }
         }
-
-        for (final Map.Entry<InstanceIdentifier<?>, DataObject> changeEntry : event.getUpdatedData().entrySet()) {
-            if (changeEntry.getValue() instanceof Node) {
-                nodeUpdated(changeEntry.getKey(), (Node) changeEntry.getValue());
-            }
-        }
-
-        for (InstanceIdentifier<?> removePath : event.getRemovedPaths()) {
-            DataObject removeObject = event.getOriginalData().get(removePath);
-            if (removeObject instanceof Node) {
-                nodeRemoved(removePath);
-            }
-        }
-
     }
 
     private void nodeCreated(final InstanceIdentifier<?> key, final Node node) {
@@ -188,5 +183,4 @@ public final class NetconfEventSourceManager implements DataChangeListener, Auto
         }
         registrationMap.clear();
     }
-
-}
\ No newline at end of file
+}
index 6d9938cdc81f8f9674bacde70e4aca23786b4ddd..7a61344cdfc1731f5d26a22545910f79f5713967 100644 (file)
@@ -8,7 +8,6 @@
 package org.opendaylight.netconf.messagebus.eventsources.netconf;
 
 import static org.mockito.Matchers.any;
-import static org.mockito.Matchers.eq;
 import static org.mockito.Matchers.notNull;
 import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.mock;
@@ -18,15 +17,16 @@ import static org.mockito.Mockito.verify;
 import com.google.common.base.Optional;
 import com.google.common.util.concurrent.CheckedFuture;
 import com.google.common.util.concurrent.Futures;
+import java.util.Collections;
 import java.util.HashMap;
-import java.util.Map;
 import org.junit.Before;
 import org.junit.BeforeClass;
 import org.junit.Test;
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
+import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
+import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
+import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
 import org.opendaylight.controller.md.sal.binding.api.MountPointService;
-import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker;
-import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
@@ -44,8 +44,6 @@ import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.r
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeConnectionStatus.ConnectionStatus;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
 import org.opendaylight.yangtools.concepts.ListenerRegistration;
-import org.opendaylight.yangtools.yang.binding.DataObject;
-import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
 
@@ -56,7 +54,7 @@ public class NetconfEventSourceManagerTest {
     DOMMountPointService domMountPointServiceMock;
     MountPointService mountPointServiceMock;
     EventSourceRegistry eventSourceTopologyMock;
-    AsyncDataChangeEvent asyncDataChangeEventMock;
+    DataTreeModification<Node> dataTreeModificationMock;
     RpcProviderRegistry rpcProviderRegistryMock;
     EventSourceRegistry eventSourceRegistry;
 
@@ -64,6 +62,7 @@ public class NetconfEventSourceManagerTest {
     public static void initTestClass() throws IllegalAccessException, InstantiationException {
     }
 
+    @SuppressWarnings("unchecked")
     @Before
     public void setUp() throws Exception {
         final DataBroker dataBrokerMock = mock(DataBroker.class);
@@ -75,9 +74,8 @@ public class NetconfEventSourceManagerTest {
         eventSourceRegistry = mock(EventSourceRegistry.class);
 
         listenerRegistrationMock = mock(ListenerRegistration.class);
-        doReturn(listenerRegistrationMock).when(dataBrokerMock).registerDataChangeListener(eq(LogicalDatastoreType
-                .OPERATIONAL), any(InstanceIdentifier.class), any(NetconfEventSourceManager.class), eq(
-                AsyncDataBroker.DataChangeScope.SUBTREE));
+        doReturn(listenerRegistrationMock).when(dataBrokerMock).registerDataTreeChangeListener(
+                any(DataTreeIdentifier.class), any(NetconfEventSourceManager.class));
 
         DOMMountPoint domMountPointMock = mock(DOMMountPoint.class);
         Optional<DOMMountPoint> optionalDomMountServiceMock = Optional.of(domMountPointMock);
@@ -106,42 +104,42 @@ public class NetconfEventSourceManagerTest {
     @Test
     public void onDataChangedCreateEventSourceTestByCreateEntry() throws Exception {
         onDataChangedTestHelper(true, false, true, NetconfTestUtils.NOTIFICATION_CAPABILITY_PREFIX);
-        netconfEventSourceManager.onDataChanged(asyncDataChangeEventMock);
+        netconfEventSourceManager.onDataTreeChanged(Collections.singletonList(dataTreeModificationMock));
         verify(eventSourceRegistry, times(1)).registerEventSource(any(EventSource.class));
     }
 
     @Test
     public void onDataChangedCreateEventSourceTestByUpdateEntry() throws Exception {
         onDataChangedTestHelper(false, true, true, NetconfTestUtils.NOTIFICATION_CAPABILITY_PREFIX);
-        netconfEventSourceManager.onDataChanged(asyncDataChangeEventMock);
+        netconfEventSourceManager.onDataTreeChanged(Collections.singletonList(dataTreeModificationMock));
         verify(eventSourceRegistry, times(1)).registerEventSource(any(EventSource.class));
     }
 
     @Test
     public void onDataChangedCreateEventSourceTestNotNeconf() throws Exception {
         onDataChangedTestHelper(false, true, false, NetconfTestUtils.NOTIFICATION_CAPABILITY_PREFIX);
-        netconfEventSourceManager.onDataChanged(asyncDataChangeEventMock);
+        netconfEventSourceManager.onDataTreeChanged(Collections.singletonList(dataTreeModificationMock));
         verify(eventSourceRegistry, times(0)).registerEventSource(any(EventSource.class));
     }
 
     @Test
     public void onDataChangedCreateEventSourceTestNotNotificationCapability() throws Exception {
         onDataChangedTestHelper(true, false, true, "bad-prefix");
-        netconfEventSourceManager.onDataChanged(asyncDataChangeEventMock);
+        netconfEventSourceManager.onDataTreeChanged(Collections.singletonList(dataTreeModificationMock));
         verify(eventSourceRegistry, times(0)).registerEventSource(any(EventSource.class));
     }
 
+    @SuppressWarnings("unchecked")
     private void onDataChangedTestHelper(boolean create, boolean update, boolean isNetconf, String
             notificationCapabilityPrefix) throws Exception {
-        asyncDataChangeEventMock = mock(AsyncDataChangeEvent.class);
-        Map<InstanceIdentifier, DataObject> mapCreate = new HashMap<>();
-        Map<InstanceIdentifier, DataObject> mapUpdate = new HashMap<>();
+        dataTreeModificationMock = mock(DataTreeModification.class);
+        DataObjectModification<Node> mockModification = mock(DataObjectModification.class);
+        doReturn(create ? DataObjectModification.ModificationType.WRITE :
+            DataObjectModification.ModificationType.SUBTREE_MODIFIED).when(mockModification).getModificationType();
+        doReturn(mockModification).when(dataTreeModificationMock).getRootNode();
 
-        Node node01;
+        final Node node01;
         String nodeId = "Node01";
-        doReturn(mapCreate).when(asyncDataChangeEventMock).getCreatedData();
-        doReturn(mapUpdate).when(asyncDataChangeEventMock).getUpdatedData();
-
         if (isNetconf) {
             node01 = NetconfTestUtils
                     .getNetconfNode(nodeId, "node01.test.local", ConnectionStatus.Connected,
@@ -151,13 +149,10 @@ public class NetconfEventSourceManagerTest {
             node01 = NetconfTestUtils.getNode(nodeId);
         }
 
-        if (create) {
-            mapCreate.put(NetconfTestUtils.getInstanceIdentifier(node01), node01);
-        }
-        if (update) {
-            mapUpdate.put(NetconfTestUtils.getInstanceIdentifier(node01), node01);
-        }
+        doReturn(node01).when(mockModification).getDataAfter();
 
+        doReturn(new DataTreeIdentifier<>(LogicalDatastoreType.OPERATIONAL,
+                NetconfTestUtils.getInstanceIdentifier(node01))).when(dataTreeModificationMock).getRootPath();
     }
 
-}
\ No newline at end of file
+}