Merge dev/fluorine work across to master
[unimgr.git] / impl / src / test / java / org / opendaylight / unimgr / mef / nrp / impl / AbstractNodeHandlerTest.java
index 67fecfbd3a430de201de3529d860b8fed7d46b95..c3b719dcf932402d8bd551192008ec4f1ebca91e 100644 (file)
@@ -7,25 +7,11 @@
  */
 package org.opendaylight.unimgr.mef.nrp.impl;
 
-import com.google.common.util.concurrent.CheckedFuture;
-import org.junit.After;
-import org.junit.Before;
-import org.junit.Test;
-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.TransactionCommitFailedException;
-import org.opendaylight.unimgr.mef.nrp.api.TapiConstants;
-import org.opendaylight.unimgr.mef.nrp.common.NrpDao;
-import org.opendaylight.unimgr.mef.nrp.common.TapiUtils;
-import org.opendaylight.yang.gen.v1.urn.onf.params.xml.ns.yang.tapi.common.rev171113.Context;
-import org.opendaylight.yang.gen.v1.urn.onf.params.xml.ns.yang.tapi.common.rev171113.ETH;
-import org.opendaylight.yang.gen.v1.urn.onf.params.xml.ns.yang.tapi.common.rev171113.TerminationDirection;
-import org.opendaylight.yang.gen.v1.urn.onf.params.xml.ns.yang.tapi.common.rev171113.Uuid;
-import org.opendaylight.yang.gen.v1.urn.onf.params.xml.ns.yang.tapi.topology.rev171113.node.OwnedNodeEdgePoint;
-import org.opendaylight.yang.gen.v1.urn.onf.params.xml.ns.yang.tapi.topology.rev171113.node.OwnedNodeEdgePointBuilder;
-import org.opendaylight.yang.gen.v1.urn.onf.params.xml.ns.yang.tapi.topology.rev171113.node.OwnedNodeEdgePointKey;
-import org.opendaylight.yang.gen.v1.urn.onf.params.xml.ns.yang.tapi.topology.rev171113.topology.Node;
-import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import com.google.common.util.concurrent.FluentFuture;
 
 import java.util.Arrays;
 import java.util.Collections;
@@ -36,19 +22,36 @@ import java.util.function.BiConsumer;
 import java.util.stream.Collectors;
 import java.util.stream.IntStream;
 
-import static org.junit.Assert.*;
+import org.eclipse.jdt.annotation.NonNull;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+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.TransactionCommitFailedException;
+import org.opendaylight.mdsal.common.api.CommitInfo;
+import org.opendaylight.unimgr.mef.nrp.api.TapiConstants;
+import org.opendaylight.unimgr.mef.nrp.common.NrpDao;
+import org.opendaylight.unimgr.mef.nrp.common.TapiUtils;
+import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev180307.Context;
+import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev180307.LayerProtocolName;
+import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev180307.TerminationDirection;
+import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev180307.Uuid;
+import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev180307.node.OwnedNodeEdgePoint;
+import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev180307.node.OwnedNodeEdgePointBuilder;
+import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev180307.node.OwnedNodeEdgePointKey;
+import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev180307.node.edge.point.MappedServiceInterfacePoint;
+import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev180307.topology.Node;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 
-/**
- * @author marek.ryznar@amartus.com
- */
 public class AbstractNodeHandlerTest extends AbstractTestWithTopo {
 
 
     private AbstractNodeHandler abstractNodeHandler;
-    private static final String testSystemNodeName = "testSystemNode";
-    private static final String testNepName = "testNep";
-    private static final String sipPrefix = "sip:";
-    private static final int init_neps_count = 4;
+    private static final String TEST_SYSTEM_NODE_NAME = "testSystemNode";
+    private static final String TEST_NEP_NAME = "testNep";
+    private static final String SIP_PREFIX = "sip:";
+    private static final int INIT_NEPS_COUNT = 4;
 
     @Before
     public void setUp() {
@@ -72,62 +75,68 @@ public class AbstractNodeHandlerTest extends AbstractTestWithTopo {
 
         tx.delete(LogicalDatastoreType.OPERATIONAL, InstanceIdentifier
                 .create(Context.class));
-        tx.submit().get();
+        tx.commit().get();
     }
 
     @Test
-    public void testNodeAddition() throws TransactionCommitFailedException {
+    public void testNodeAddition()
+            throws TransactionCommitFailedException, InterruptedException, ExecutionException {
         //when
-        performNrpDaoAction(addNode,null).checkedGet();
+        performNrpDaoAction(addNode,null).get();
 
         //then
         Node node = getAbstractNodeNotNullNep();
 
         assertEquals(
-                node.getOwnedNodeEdgePoint().stream().map(nep -> nep.getUuid().getValue()).collect(Collectors.toSet()),
-                new HashSet(Arrays.asList(testNepName+"0", testNepName+"1", testNepName+"2", testNepName+"3"))
+                node.getOwnedNodeEdgePoint().stream().map(
+                    nep -> nep.getUuid().getValue()).collect(Collectors.toSet()),
+                new HashSet<>(Arrays.asList(
+                    TEST_NEP_NAME + "0", TEST_NEP_NAME + "1", TEST_NEP_NAME + "2", TEST_NEP_NAME + "3"))
         );
 
     }
 
     @Test
-    public void testNepAddition() throws TransactionCommitFailedException {
+    public void testNepAddition()
+            throws TransactionCommitFailedException, InterruptedException, ExecutionException {
         //given
         String newNepName = "newNep";
-        performNrpDaoAction(addNode,null).checkedGet();
+        performNrpDaoAction(addNode,null).get();
 
         //when
         OwnedNodeEdgePoint newNep = createNep(newNepName,TerminationDirection.BIDIRECTIONAL);
-        performNrpDaoAction(update, newNep).checkedGet();
+        performNrpDaoAction(update, newNep).get();
 
         //then
-        Node node = getAbstractNode(n -> n.getOwnedNodeEdgePoint().size() == init_neps_count + 1);
+        Node node = getAbstractNode(n -> n.getOwnedNodeEdgePoint().size() == INIT_NEPS_COUNT + 1);
 
         assertTrue(node.getOwnedNodeEdgePoint().contains(newNep));
     }
 
     @Test
-    public void testNepUpdate() throws TransactionCommitFailedException {
+    public void testNepUpdate() throws TransactionCommitFailedException, InterruptedException, ExecutionException {
         //given
-        performNrpDaoAction(addNode, null).checkedGet();
+        performNrpDaoAction(addNode, null).get();
 
         //when changing not sip related attribute
-        OwnedNodeEdgePoint toUpdateNep = createNep(testNepName + "1", TerminationDirection.UNDEFINEDORUNKNOWN);
-        performNrpDaoAction(update, toUpdateNep).checkedGet();
+        OwnedNodeEdgePoint toUpdateNep = createNep(TEST_NEP_NAME + "1", TerminationDirection.UNDEFINEDORUNKNOWN);
+        performNrpDaoAction(update, toUpdateNep).get();
 
 
         Node node = getAbstractNodeNotNullNep();
         //There could be more neps if our node was added insted of updated
-        assertEquals(init_neps_count,node.getOwnedNodeEdgePoint().size());
+        assertEquals(INIT_NEPS_COUNT,node.getOwnedNodeEdgePoint().size());
         assertTrue(node.getOwnedNodeEdgePoint().contains(toUpdateNep));
     }
 
     @Test
-    public void testNepUpdatedWithSipAddition() throws ExecutionException, InterruptedException, TransactionCommitFailedException {
+    public void testNepUpdatedWithSipAddition()
+            throws ExecutionException, InterruptedException, TransactionCommitFailedException {
+
         //given
         ReadWriteTransaction tx = dataBroker.newReadWriteTransaction();
-        Node n1 = n(tx, false, "n1", "n1:1", "n1:2");
-        tx.submit().get();
+        final Node n1 = n(tx, false, new Uuid("n1"), "d1", "n1:1", "n1:2");
+        tx.commit().get();
 
         Node node = getAbstractNode();
         int neps = node.getOwnedNodeEdgePoint() == null ? 0 : node.getOwnedNodeEdgePoint().size();
@@ -136,10 +145,13 @@ public class AbstractNodeHandlerTest extends AbstractTestWithTopo {
         //when
         tx = dataBroker.newReadWriteTransaction();
         OwnedNodeEdgePoint n11 = new OwnedNodeEdgePointBuilder(n1.getOwnedNodeEdgePoint().get(0))
-                .setMappedServiceInterfacePoint(Collections.singletonList(new Uuid("sip:n1:1")))
+                .setMappedServiceInterfacePoint(
+                        Collections.singletonList(
+                                TapiUtils.toSipRef(new Uuid("sip:n1:1"),
+                                MappedServiceInterfacePoint.class)))
                 .build();
         new NrpDao(tx).updateNep("n1", n11);
-        tx.submit().checkedGet();
+        tx.commit().get();
 
         //then
         node = getAbstractNodeNotNullNep();
@@ -149,11 +161,12 @@ public class AbstractNodeHandlerTest extends AbstractTestWithTopo {
     }
 
     @Test
-    public void testNepUpdatedWithSipRemoval() throws ExecutionException, InterruptedException, TransactionCommitFailedException {
+    public void testNepUpdatedWithSipRemoval()
+            throws ExecutionException, InterruptedException, TransactionCommitFailedException {
         //given we have sips
         ReadWriteTransaction tx = dataBroker.newReadWriteTransaction();
-        Node n1 = n(tx, true, "n1", "n1:1", "n1:2");
-        tx.submit().checkedGet();
+        final Node n1 = n(tx, true, new Uuid("n1"), "d1", "n1:1", "n1:2");
+        tx.commit().get();
 
         //assert
         Node node = getAbstractNodeNotNullNep();
@@ -165,19 +178,19 @@ public class AbstractNodeHandlerTest extends AbstractTestWithTopo {
                 .setMappedServiceInterfacePoint(Collections.emptyList())
                 .build();
         new NrpDao(tx).updateNep("n1", n11);
-        tx.submit().checkedGet();
+        tx.commit().get();
 
         //then a nep was removed
         getAbstractNode(n -> n.getOwnedNodeEdgePoint().size() == 1);
     }
 
     @Test
-    public void testNodeRemoval() throws TransactionCommitFailedException {
+    public void testNodeRemoval() throws TransactionCommitFailedException, InterruptedException, ExecutionException {
         //given
-        performNrpDaoAction(addNode,null).checkedGet();
+        performNrpDaoAction(addNode,null).get();
 
         //when
-        performNrpDaoAction(removeNode,null).checkedGet();
+        performNrpDaoAction(removeNode,null).get();
 
         //then
         Node node = getAbstractNode(n -> n.getOwnedNodeEdgePoint() != null && n.getOwnedNodeEdgePoint().isEmpty());
@@ -185,36 +198,46 @@ public class AbstractNodeHandlerTest extends AbstractTestWithTopo {
     }
 
     @Test
-    public void testNepRemoval() throws TransactionCommitFailedException {
+    public void testNepRemoval() throws TransactionCommitFailedException, InterruptedException, ExecutionException {
         //given
-        performNrpDaoAction(addNode,null).checkedGet();
-        String nepNameToRemove = testNepName + "0";
+        performNrpDaoAction(addNode,null).get();
+        String nepNameToRemove = TEST_NEP_NAME + "0";
 
         //when
-        performNrpDaoAction(removeNep,nepNameToRemove).checkedGet();
+        performNrpDaoAction(removeNep,nepNameToRemove).get();
 
         //then
-        Node node = getAbstractNode(n -> n.getOwnedNodeEdgePoint().size() == init_neps_count - 1);
+        Node node = getAbstractNode(n -> n.getOwnedNodeEdgePoint().size() == INIT_NEPS_COUNT - 1);
 
         assertFalse(node.getOwnedNodeEdgePoint().stream()
             .anyMatch(nep -> nep.getUuid().getValue().equals(nepNameToRemove)));
     }
 
-    private BiConsumer<NrpDao,String> removeNep = (dao, nepId) -> dao.removeNep(testSystemNodeName,nepId,false);
-    private BiConsumer<NrpDao,String> removeNode = (dao, nepId) -> dao.removeNode(testSystemNodeName,false);
-    private BiConsumer<NrpDao,String> addNode = (dao, nepId) -> dao.createNode(TapiConstants.PRESTO_SYSTEM_TOPO, testSystemNodeName, ETH.class,createTestOwnedNodeEdgePointList());
-    private BiConsumer<NrpDao,OwnedNodeEdgePoint> update = (dao, nep) -> dao.updateNep(testSystemNodeName,nep);
+    private BiConsumer<NrpDao,String> removeNep =
+        (dao, nepId) -> dao.removeNep(TEST_SYSTEM_NODE_NAME, nepId, false);
+    private BiConsumer<NrpDao,String> removeNode =
+        (dao, nepId) -> dao.removeNode(TEST_SYSTEM_NODE_NAME, false);
+    private BiConsumer<NrpDao,String> addNode =
+        (dao, nepId) -> dao.createNode(
+                TapiConstants.PRESTO_SYSTEM_TOPO,
+                TEST_SYSTEM_NODE_NAME,
+                LayerProtocolName.ETH,
+                createTestOwnedNodeEdgePointList());
+    private BiConsumer<NrpDao,OwnedNodeEdgePoint> update =
+        (dao, nep) -> dao.updateNep(TEST_SYSTEM_NODE_NAME, nep);
+
+    private <T extends Object> @NonNull FluentFuture<? extends @NonNull CommitInfo>
+        performNrpDaoAction(BiConsumer<NrpDao,T> action, T attr) {
 
-    private <T extends Object> CheckedFuture<Void, TransactionCommitFailedException> performNrpDaoAction(BiConsumer<NrpDao,T> action, T attr) {
         ReadWriteTransaction tx = dataBroker.newReadWriteTransaction();
         NrpDao nrpDao = new NrpDao(tx);
         action.accept(nrpDao,attr);
-        return tx.submit();
+        return tx.commit();
     }
 
     private List<OwnedNodeEdgePoint> createTestOwnedNodeEdgePointList() {
-        return IntStream.range(0,init_neps_count)
-            .mapToObj(i -> createNep(testNepName + i, TerminationDirection.BIDIRECTIONAL))
+        return IntStream.range(0,INIT_NEPS_COUNT)
+            .mapToObj(i -> createNep(TEST_NEP_NAME + i, TerminationDirection.BIDIRECTIONAL))
             .collect(Collectors.toList());
     }
 
@@ -225,13 +248,15 @@ public class AbstractNodeHandlerTest extends AbstractTestWithTopo {
     private OwnedNodeEdgePoint createNep(String nepName, boolean associateSip, TerminationDirection td) {
         Uuid uuid = new Uuid(nepName);
         OwnedNodeEdgePointBuilder builder = new OwnedNodeEdgePointBuilder()
-                .setKey(new OwnedNodeEdgePointKey(uuid))
-                .setLayerProtocol(Collections.singletonList(TapiUtils.toNepPN(ETH.class)))
+                .withKey(new OwnedNodeEdgePointKey(uuid))
+                .setLayerProtocolName(LayerProtocolName.ETH)
                 .setUuid(uuid);
                 // TODO donaldh .setTerminationDirection(td);
 
         if (associateSip) {
-            builder.setMappedServiceInterfacePoint(Collections.singletonList(new Uuid(sipPrefix + nepName)));
+            MappedServiceInterfacePoint sipRef =
+                    TapiUtils.toSipRef(new Uuid(SIP_PREFIX + nepName), MappedServiceInterfacePoint.class);
+            builder.setMappedServiceInterfacePoint(Collections.singletonList(sipRef));
         }
 
         return builder.build();