From: Tom Pantelis Date: Wed, 30 Aug 2017 18:01:33 +0000 (-0400) Subject: Convert NetconfEventSourceManager to DataTreeChangeListener X-Git-Tag: release/oxygen~85^2 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=commitdiff_plain;h=6345ccffb82ea4392a0b89d6ae11655e6302a46a;p=netconf.git Convert NetconfEventSourceManager to DataTreeChangeListener DataChangeListener has been deprecated since Carbon with possible removal in Oxygen so convert to DataTreeChangeListener. Change-Id: I23e4318a9973a7cffcaa95c42ac6df7449b59b61 Signed-off-by: Tom Pantelis --- diff --git a/netconf/messagebus-netconf/src/main/java/org/opendaylight/netconf/messagebus/eventsources/netconf/NetconfEventSourceManager.java b/netconf/messagebus-netconf/src/main/java/org/opendaylight/netconf/messagebus/eventsources/netconf/NetconfEventSourceManager.java index 516c595ef7..91df5f7812 100644 --- a/netconf/messagebus-netconf/src/main/java/org/opendaylight/netconf/messagebus/eventsources/netconf/NetconfEventSourceManager.java +++ b/netconf/messagebus-netconf/src/main/java/org/opendaylight/netconf/messagebus/eventsources/netconf/NetconfEventSourceManager.java @@ -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, 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 listenerRegistration; + private ListenerRegistration 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, DataObject> event) { - - LOG.debug("[DataChangeEvent, DataObject>: {}]", event); - for (final Map.Entry, DataObject> changeEntry : event.getCreatedData().entrySet()) { - if (changeEntry.getValue() instanceof Node) { - nodeCreated(changeEntry.getKey(), (Node) changeEntry.getValue()); + public void onDataTreeChanged(Collection> changes) { + for (DataTreeModification change: changes) { + LOG.debug("DataTreeModification: {}", change); + final DataObjectModification rootNode = change.getRootNode(); + final InstanceIdentifier 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, 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 +} diff --git a/netconf/messagebus-netconf/src/test/java/org/opendaylight/netconf/messagebus/eventsources/netconf/NetconfEventSourceManagerTest.java b/netconf/messagebus-netconf/src/test/java/org/opendaylight/netconf/messagebus/eventsources/netconf/NetconfEventSourceManagerTest.java index 6d9938cdc8..7a61344cdf 100644 --- a/netconf/messagebus-netconf/src/test/java/org/opendaylight/netconf/messagebus/eventsources/netconf/NetconfEventSourceManagerTest.java +++ b/netconf/messagebus-netconf/src/test/java/org/opendaylight/netconf/messagebus/eventsources/netconf/NetconfEventSourceManagerTest.java @@ -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 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 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 mapCreate = new HashMap<>(); - Map mapUpdate = new HashMap<>(); + dataTreeModificationMock = mock(DataTreeModification.class); + DataObjectModification 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 +}