Switch to MD-SAL APIs
[openflowplugin.git] / applications / topology-manager / src / test / java / org / opendaylight / openflowplugin / applications / topology / manager / TerminationPointChangeListenerImplTest.java
index 8f46f13128f7b1bcad3176b2935981ecfdb923c9..a7b7f7dbe0f88fd53e2f7669ba0e354e8323ad36 100644 (file)
@@ -1,5 +1,5 @@
-/**
- * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
+/*
+ * Copyright (c) 2015, 2017 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,
@@ -9,29 +9,41 @@ package org.opendaylight.openflowplugin.applications.topology.manager;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
-import static org.mockito.Matchers.any;
-import static org.mockito.Matchers.eq;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.eq;
 import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.verify;
-import static org.opendaylight.openflowplugin.applications.topology.manager.TestUtils.*;
+import static org.mockito.Mockito.when;
+import static org.opendaylight.mdsal.binding.api.DataObjectModification.ModificationType.DELETE;
+import static org.opendaylight.mdsal.binding.api.DataObjectModification.ModificationType.WRITE;
+import static org.opendaylight.openflowplugin.applications.topology.manager.TestUtils.assertDeletedIDs;
+import static org.opendaylight.openflowplugin.applications.topology.manager.TestUtils.newDestTp;
+import static org.opendaylight.openflowplugin.applications.topology.manager.TestUtils.newInvNodeConnKey;
+import static org.opendaylight.openflowplugin.applications.topology.manager.TestUtils.newInvNodeKey;
+import static org.opendaylight.openflowplugin.applications.topology.manager.TestUtils.newLink;
+import static org.opendaylight.openflowplugin.applications.topology.manager.TestUtils.newNodeConnID;
+import static org.opendaylight.openflowplugin.applications.topology.manager.TestUtils.newSourceTp;
+import static org.opendaylight.openflowplugin.applications.topology.manager.TestUtils.setReadFutureAsync;
+import static org.opendaylight.openflowplugin.applications.topology.manager.TestUtils.setupStubbedDeletes;
+import static org.opendaylight.openflowplugin.applications.topology.manager.TestUtils.setupStubbedSubmit;
+import static org.opendaylight.openflowplugin.applications.topology.manager.TestUtils.verifyMockTx;
+import static org.opendaylight.openflowplugin.applications.topology.manager.TestUtils.waitForDeletes;
+import static org.opendaylight.openflowplugin.applications.topology.manager.TestUtils.waitForSubmit;
 
-import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRef;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.model.topology.inventory.rev131030.InventoryNodeConnector;
-import org.opendaylight.yangtools.yang.binding.DataObject;
-
-import com.google.common.base.Optional;
-import com.google.common.util.concurrent.Futures;
 import com.google.common.util.concurrent.SettableFuture;
 import java.util.Arrays;
 import java.util.Collections;
 import java.util.List;
+import java.util.Optional;
 import java.util.concurrent.CountDownLatch;
 import org.junit.Test;
 import org.mockito.ArgumentCaptor;
-import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
-import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
-import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
+import org.opendaylight.mdsal.binding.api.DataTreeModification;
+import org.opendaylight.mdsal.binding.api.ReadWriteTransaction;
+import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRef;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.model.topology.inventory.rev131030.InventoryNodeConnector;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TpId;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
@@ -42,13 +54,10 @@ 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.topology.NodeKey;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointKey;
+import org.opendaylight.yangtools.util.concurrent.FluentFutures;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 
-/**
- * @author joe
- *
- */
-public class TerminationPointChangeListenerImplTest extends DataChangeListenerBase{
+public class TerminationPointChangeListenerImplTest extends DataTreeChangeListenerBase {
     @SuppressWarnings("rawtypes")
     @Test
     public void testOnNodeConnectorRemoved() {
@@ -56,16 +65,16 @@ public class TerminationPointChangeListenerImplTest extends DataChangeListenerBa
         NodeKey topoNodeKey = new NodeKey(new NodeId("node1"));
         TerminationPointKey terminationPointKey = new TerminationPointKey(new TpId("tp1"));
 
-        InstanceIdentifier<Node> topoNodeII = topologyIID.child(Node.class, topoNodeKey);
-        Node topoNode = new NodeBuilder().setKey(topoNodeKey).build();
+        final InstanceIdentifier<Node> topoNodeII = topologyIID.child(Node.class, topoNodeKey);
+        Node topoNode = new NodeBuilder().withKey(topoNodeKey).build();
 
-        org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey
-                                                                  nodeKey = newInvNodeKey(topoNodeKey.getNodeId().getValue());
+        org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes
+                .NodeKey nodeKey = newInvNodeKey(topoNodeKey.getNodeId().getValue());
 
         org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey ncKey =
                 newInvNodeConnKey(terminationPointKey.getTpId().getValue());
 
-        InstanceIdentifier<?> invNodeConnID = newNodeConnID(nodeKey, ncKey);
+        final InstanceIdentifier<?> invNodeConnID = newNodeConnID(nodeKey, ncKey);
 
         List<Link> linkList = Arrays.asList(
                 newLink("link1", newSourceTp("tp1"), newDestTp("dest")),
@@ -73,9 +82,9 @@ public class TerminationPointChangeListenerImplTest extends DataChangeListenerBa
                 newLink("link3", newSourceTp("source2"), newDestTp("dest2")));
         final Topology topology = new TopologyBuilder().setLink(linkList).build();
 
-        InstanceIdentifier[] expDeletedIIDs = {
-                topologyIID.child(Link.class, linkList.get(0).getKey()),
-                topologyIID.child(Link.class, linkList.get(1).getKey()),
+        final InstanceIdentifier[] expDeletedIIDs = {
+                topologyIID.child(Link.class, linkList.get(0).key()),
+                topologyIID.child(Link.class, linkList.get(1).key()),
                 topologyIID.child(Node.class, new NodeKey(new NodeId("node1")))
                         .child(TerminationPoint.class, new TerminationPointKey(new TpId("tp1")))
             };
@@ -83,15 +92,13 @@ public class TerminationPointChangeListenerImplTest extends DataChangeListenerBa
         final SettableFuture<Optional<Topology>> readFuture = SettableFuture.create();
         readFuture.set(Optional.of(topology));
         ReadWriteTransaction mockTx1 = mock(ReadWriteTransaction.class);
-        doReturn(Futures.makeChecked(readFuture, ReadFailedException.MAPPER)).when(mockTx1)
-                .read(LogicalDatastoreType.OPERATIONAL, topologyIID);
+        doReturn(readFuture).when(mockTx1).read(LogicalDatastoreType.OPERATIONAL, topologyIID);
 
         SettableFuture<Optional<Node>> readFutureNode = SettableFuture.create();
         readFutureNode.set(Optional.of(topoNode));
-        doReturn(Futures.makeChecked(readFutureNode, ReadFailedException.MAPPER)).when(mockTx1)
-                .read(LogicalDatastoreType.OPERATIONAL, topoNodeII);
+        doReturn(readFutureNode).when(mockTx1).read(LogicalDatastoreType.OPERATIONAL, topoNodeII);
 
-        CountDownLatch submitLatch1 = setupStubbedSubmit(mockTx1);
+        final CountDownLatch submitLatch1 = setupStubbedSubmit(mockTx1);
 
         int expDeleteCalls = expDeletedIIDs.length;
         CountDownLatch deleteLatch = new CountDownLatch(expDeleteCalls);
@@ -101,8 +108,8 @@ public class TerminationPointChangeListenerImplTest extends DataChangeListenerBa
 
         doReturn(mockTx1).when(mockTxChain).newReadWriteTransaction();
 
-        mockDataChangeListener(null, null, Collections.singleton(invNodeConnID));
-        terminationPointListener.onDataChanged(mockedDataChangeListener);
+        DataTreeModification dataTreeModification = setupDataTreeChange(DELETE, invNodeConnID);
+        terminationPointListener.onDataTreeChanged(Collections.singleton(dataTreeModification));
 
         waitForSubmit(submitLatch1);
 
@@ -123,7 +130,7 @@ public class TerminationPointChangeListenerImplTest extends DataChangeListenerBa
         TerminationPointKey terminationPointKey = new TerminationPointKey(new TpId("tp1"));
 
         InstanceIdentifier<Node> topoNodeII = topologyIID.child(Node.class, topoNodeKey);
-        Node topoNode = new NodeBuilder().setKey(topoNodeKey).build();
+        Node topoNode = new NodeBuilder().withKey(topoNodeKey).build();
 
         org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey
                 nodeKey = newInvNodeKey(topoNodeKey.getNodeId().getValue());
@@ -131,21 +138,19 @@ public class TerminationPointChangeListenerImplTest extends DataChangeListenerBa
         org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey ncKey =
                 newInvNodeConnKey(terminationPointKey.getTpId().getValue());
 
-        InstanceIdentifier<?> invNodeConnID = newNodeConnID(nodeKey, ncKey);
+        final InstanceIdentifier<?> invNodeConnID = newNodeConnID(nodeKey, ncKey);
 
-        InstanceIdentifier[] expDeletedIIDs = {
+        final InstanceIdentifier[] expDeletedIIDs = {
                 topologyIID.child(Node.class, new NodeKey(new NodeId("node1")))
                         .child(TerminationPoint.class, new TerminationPointKey(new TpId("tp1")))
             };
 
         ReadWriteTransaction mockTx = mock(ReadWriteTransaction.class);
-        doReturn(Futures.immediateCheckedFuture(Optional.absent())).when(mockTx)
+        doReturn(FluentFutures.immediateFluentFuture(Optional.empty())).when(mockTx)
                 .read(LogicalDatastoreType.OPERATIONAL, topologyIID);
-        CountDownLatch submitLatch = setupStubbedSubmit(mockTx);
+        final CountDownLatch submitLatch = setupStubbedSubmit(mockTx);
 
-        SettableFuture<Optional<Node>> readFutureNode = SettableFuture.create();
-        readFutureNode.set(Optional.of(topoNode));
-        doReturn(Futures.makeChecked(readFutureNode, ReadFailedException.MAPPER)).when(mockTx)
+        doReturn(FluentFutures.immediateFluentFuture(Optional.of(topoNode))).when(mockTx)
                 .read(LogicalDatastoreType.OPERATIONAL, topoNodeII);
 
         CountDownLatch deleteLatch = new CountDownLatch(1);
@@ -155,8 +160,8 @@ public class TerminationPointChangeListenerImplTest extends DataChangeListenerBa
 
         doReturn(mockTx).when(mockTxChain).newReadWriteTransaction();
 
-        mockDataChangeListener(null, null, Collections.singleton(invNodeConnID));
-        terminationPointListener.onDataChanged(mockedDataChangeListener);
+        DataTreeModification dataTreeModification = setupDataTreeChange(DELETE, invNodeConnID);
+        terminationPointListener.onDataTreeChanged(Collections.singleton(dataTreeModification));
 
         waitForSubmit(submitLatch);
 
@@ -180,9 +185,8 @@ public class TerminationPointChangeListenerImplTest extends DataChangeListenerBa
         CountDownLatch submitLatch = setupStubbedSubmit(mockTx);
         doReturn(mockTx).when(mockTxChain).newReadWriteTransaction();
 
-        mockDataChangeListener(Collections.<InstanceIdentifier<?>, DataObject> singletonMap(
-                invNodeConnID, null), null, null);
-        terminationPointListener.onDataChanged(mockedDataChangeListener);
+        DataTreeModification dataTreeModification = setupDataTreeChange(WRITE, invNodeConnID);
+        terminationPointListener.onDataTreeChanged(Collections.singleton(dataTreeModification));
 
         waitForSubmit(submitLatch);
 
@@ -195,7 +199,7 @@ public class TerminationPointChangeListenerImplTest extends DataChangeListenerBa
         verify(mockTx).merge(eq(LogicalDatastoreType.OPERATIONAL), eq(expTpPath),
                 mergedNode.capture(), eq(true));
         assertEquals("getTpId", expTpId, mergedNode.getValue().getTpId());
-        InventoryNodeConnector augmentation = mergedNode.getValue().getAugmentation(
+        InventoryNodeConnector augmentation = mergedNode.getValue().augmentation(
                 InventoryNodeConnector.class);
         assertNotNull("Missing augmentation", augmentation);
         assertEquals("getInventoryNodeConnectorRef", new NodeConnectorRef(invNodeConnID),
@@ -212,13 +216,13 @@ public class TerminationPointChangeListenerImplTest extends DataChangeListenerBa
         org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey ncKey =
                 newInvNodeConnKey("tp1");
 
-        InstanceIdentifier<?> invNodeConnID = newNodeConnID(nodeKey, ncKey);
+        final InstanceIdentifier<?> invNodeConnID = newNodeConnID(nodeKey, ncKey);
 
         List<Link> linkList = Arrays.asList(newLink("link1", newSourceTp("tp1"), newDestTp("dest")));
         Topology topology = new TopologyBuilder().setLink(linkList).build();
 
         ReadWriteTransaction mockTx = mock(ReadWriteTransaction.class);
-        doReturn(Futures.immediateCheckedFuture(Optional.of(topology))).when(mockTx)
+        doReturn(FluentFutures.immediateFluentFuture(Optional.of(topology))).when(mockTx)
                 .read(LogicalDatastoreType.OPERATIONAL, topologyIID);
         setupStubbedSubmit(mockTx);
 
@@ -229,9 +233,10 @@ public class TerminationPointChangeListenerImplTest extends DataChangeListenerBa
 
         doReturn(mockTx).when(mockTxChain).newReadWriteTransaction();
 
-        mockDataChangeListener(Collections.<InstanceIdentifier<?>, DataObject> singletonMap(
-                invNodeConnID, provideFlowCapableNodeConnector(true, false)), null, null);
-        terminationPointListener.onDataChanged(mockedDataChangeListener);
+        DataTreeModification dataTreeModification = setupDataTreeChange(WRITE, invNodeConnID);
+        when(dataTreeModification.getRootNode().getDataAfter())
+                .thenReturn(provideFlowCapableNodeConnector(true, false));
+        terminationPointListener.onDataTreeChanged(Collections.singleton(dataTreeModification));
 
         waitForDeletes(1, deleteLatch);
 
@@ -243,10 +248,9 @@ public class TerminationPointChangeListenerImplTest extends DataChangeListenerBa
                 any(TerminationPoint.class), eq(true));
 
         assertDeletedIDs(new InstanceIdentifier[]{topologyIID.child(Link.class,
-                linkList.get(0).getKey())}, deletedLinkIDs);
+                linkList.get(0).key())}, deletedLinkIDs);
     }
 
-
     @SuppressWarnings("rawtypes")
     @Test
     public void testOnNodeConnectorUpdatedWithPortDown() {
@@ -257,13 +261,13 @@ public class TerminationPointChangeListenerImplTest extends DataChangeListenerBa
         org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey ncKey =
                 newInvNodeConnKey("tp1");
 
-        InstanceIdentifier<?> invNodeConnID = newNodeConnID(nodeKey, ncKey);
+        final InstanceIdentifier<?> invNodeConnID = newNodeConnID(nodeKey, ncKey);
 
         List<Link> linkList = Arrays.asList(newLink("link1", newSourceTp("tp1"), newDestTp("dest")));
         Topology topology = new TopologyBuilder().setLink(linkList).build();
 
         ReadWriteTransaction mockTx = mock(ReadWriteTransaction.class);
-        doReturn(Futures.immediateCheckedFuture(Optional.of(topology))).when(mockTx)
+        doReturn(FluentFutures.immediateFluentFuture(Optional.of(topology))).when(mockTx)
                 .read(LogicalDatastoreType.OPERATIONAL, topologyIID);
         setupStubbedSubmit(mockTx);
 
@@ -274,9 +278,10 @@ public class TerminationPointChangeListenerImplTest extends DataChangeListenerBa
 
         doReturn(mockTx).when(mockTxChain).newReadWriteTransaction();
 
-        mockDataChangeListener(Collections.<InstanceIdentifier<?>, DataObject> singletonMap(
-                invNodeConnID, provideFlowCapableNodeConnector(false, true)), null, null);
-        terminationPointListener.onDataChanged(mockedDataChangeListener);
+        DataTreeModification dataTreeModification = setupDataTreeChange(WRITE, invNodeConnID);
+        when(dataTreeModification.getRootNode().getDataAfter())
+                .thenReturn(provideFlowCapableNodeConnector(false, true));
+        terminationPointListener.onDataTreeChanged(Collections.singleton(dataTreeModification));
 
         waitForDeletes(1, deleteLatch);
 
@@ -288,6 +293,6 @@ public class TerminationPointChangeListenerImplTest extends DataChangeListenerBa
                 any(TerminationPoint.class), eq(true));
 
         assertDeletedIDs(new InstanceIdentifier[]{topologyIID.child(Link.class,
-                linkList.get(0).getKey())}, deletedLinkIDs);
+                linkList.get(0).key())}, deletedLinkIDs);
     }
 }