Teach sal-remoterpc-connector to route actions
[controller.git] / opendaylight / md-sal / sal-remoterpc-connector / src / test / java / org / opendaylight / controller / remote / rpc / registry / RpcRegistryTest.java
index 84062735c22ddf5a3cbd6a40e74e64466d745e5d..4ae6e1e57f82c35486a4e58f829cee955d6891be 100644 (file)
@@ -5,9 +5,11 @@
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-
 package org.opendaylight.controller.remote.rpc.registry;
 
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 import static org.opendaylight.controller.remote.rpc.registry.gossip.BucketStoreAccess.Singletons.GET_ALL_BUCKETS;
 import static org.opendaylight.controller.remote.rpc.registry.gossip.BucketStoreAccess.Singletons.GET_BUCKET_VERSIONS;
@@ -20,13 +22,13 @@ import akka.cluster.ClusterEvent.CurrentClusterState;
 import akka.cluster.Member;
 import akka.cluster.MemberStatus;
 import akka.cluster.UniqueAddress;
-import akka.testkit.JavaTestKit;
+import akka.testkit.javadsl.TestKit;
 import com.google.common.base.Stopwatch;
 import com.google.common.collect.Sets;
 import com.google.common.util.concurrent.Uninterruptibles;
 import com.typesafe.config.ConfigFactory;
 import java.net.URI;
-import java.net.URISyntaxException;
+import java.time.Duration;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
@@ -38,24 +40,21 @@ import java.util.Set;
 import java.util.concurrent.TimeUnit;
 import org.junit.After;
 import org.junit.AfterClass;
-import org.junit.Assert;
 import org.junit.Before;
 import org.junit.BeforeClass;
 import org.junit.Test;
 import org.opendaylight.controller.cluster.common.actor.AkkaConfigurationReader;
-import org.opendaylight.controller.md.sal.dom.api.DOMRpcIdentifier;
-import org.opendaylight.controller.remote.rpc.RemoteRpcProviderConfig;
+import org.opendaylight.controller.remote.rpc.RemoteOpsProviderConfig;
 import org.opendaylight.controller.remote.rpc.registry.RpcRegistry.Messages.AddOrUpdateRoutes;
 import org.opendaylight.controller.remote.rpc.registry.RpcRegistry.Messages.RemoveRoutes;
 import org.opendaylight.controller.remote.rpc.registry.RpcRegistry.Messages.UpdateRemoteEndpoints;
 import org.opendaylight.controller.remote.rpc.registry.RpcRegistry.RemoteRpcEndpoint;
 import org.opendaylight.controller.remote.rpc.registry.gossip.Bucket;
+import org.opendaylight.mdsal.dom.api.DOMRpcIdentifier;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
-import scala.concurrent.duration.Duration;
-import scala.concurrent.duration.FiniteDuration;
 
 public class RpcRegistryTest {
     private static final Logger LOG = LoggerFactory.getLogger(RpcRegistryTest.class);
@@ -64,12 +63,12 @@ public class RpcRegistryTest {
     private static ActorSystem node2;
     private static ActorSystem node3;
 
-    private JavaTestKit invoker1;
-    private JavaTestKit invoker2;
-    private JavaTestKit invoker3;
-    private JavaTestKit registrar1;
-    private JavaTestKit registrar2;
-    private JavaTestKit registrar3;
+    private TestKit invoker1;
+    private TestKit invoker2;
+    private TestKit invoker3;
+    private TestKit registrar1;
+    private TestKit registrar2;
+    private TestKit registrar3;
     private ActorRef registry1;
     private ActorRef registry2;
     private ActorRef registry3;
@@ -77,14 +76,14 @@ public class RpcRegistryTest {
     private int routeIdCounter = 1;
 
     @BeforeClass
-    public static void staticSetup() throws InterruptedException {
+    public static void staticSetup() {
         AkkaConfigurationReader reader = ConfigFactory::load;
 
-        RemoteRpcProviderConfig config1 = new RemoteRpcProviderConfig.Builder("memberA").gossipTickInterval("200ms")
+        RemoteOpsProviderConfig config1 = new RemoteOpsProviderConfig.Builder("memberA").gossipTickInterval("200ms")
                 .withConfigReader(reader).build();
-        RemoteRpcProviderConfig config2 = new RemoteRpcProviderConfig.Builder("memberB").gossipTickInterval("200ms")
+        RemoteOpsProviderConfig config2 = new RemoteOpsProviderConfig.Builder("memberB").gossipTickInterval("200ms")
                 .withConfigReader(reader).build();
-        RemoteRpcProviderConfig config3 = new RemoteRpcProviderConfig.Builder("memberC").gossipTickInterval("200ms")
+        RemoteOpsProviderConfig config3 = new RemoteOpsProviderConfig.Builder("memberC").gossipTickInterval("200ms")
                 .withConfigReader(reader).build();
         node1 = ActorSystem.create("opendaylight-rpc", config1.get());
         node2 = ActorSystem.create("opendaylight-rpc", config2.get());
@@ -114,26 +113,26 @@ public class RpcRegistryTest {
 
     @AfterClass
     public static void staticTeardown() {
-        JavaTestKit.shutdownActorSystem(node1);
-        JavaTestKit.shutdownActorSystem(node2);
-        JavaTestKit.shutdownActorSystem(node3);
+        TestKit.shutdownActorSystem(node1);
+        TestKit.shutdownActorSystem(node2);
+        TestKit.shutdownActorSystem(node3);
     }
 
     @Before
     public void setup() {
-        invoker1 = new JavaTestKit(node1);
-        registrar1 = new JavaTestKit(node1);
+        invoker1 = new TestKit(node1);
+        registrar1 = new TestKit(node1);
         registry1 = node1.actorOf(RpcRegistry.props(config(node1), invoker1.getRef(), registrar1.getRef()));
-        invoker2 = new JavaTestKit(node2);
-        registrar2 = new JavaTestKit(node2);
+        invoker2 = new TestKit(node2);
+        registrar2 = new TestKit(node2);
         registry2 = node2.actorOf(RpcRegistry.props(config(node2), invoker2.getRef(), registrar2.getRef()));
-        invoker3 = new JavaTestKit(node3);
-        registrar3 = new JavaTestKit(node3);
+        invoker3 = new TestKit(node3);
+        registrar3 = new TestKit(node3);
         registry3 = node3.actorOf(RpcRegistry.props(config(node3), invoker3.getRef(), registrar3.getRef()));
     }
 
-    private static RemoteRpcProviderConfig config(final ActorSystem node) {
-        return new RemoteRpcProviderConfig(node.settings().config());
+    private static RemoteOpsProviderConfig config(final ActorSystem node) {
+        return new RemoteOpsProviderConfig(node.settings().config());
     }
 
     @After
@@ -174,7 +173,7 @@ public class RpcRegistryTest {
      * deleted
      */
     @Test
-    public void testAddRemoveRpcOnSameNode() throws Exception {
+    public void testAddRemoveRpcOnSameNode() {
         LOG.info("testAddRemoveRpcOnSameNode starting");
 
         Address nodeAddress = node1.provider().getDefaultAddress();
@@ -186,13 +185,13 @@ public class RpcRegistryTest {
         registry1.tell(new AddOrUpdateRoutes(addedRouteIds), ActorRef.noSender());
 
         // Bucket store should get an update bucket message. Updated bucket contains added rpc.
-        final JavaTestKit testKit = new JavaTestKit(node1);
+        final TestKit testKit = new TestKit(node1);
 
         Map<Address, Bucket<RoutingTable>> buckets = retrieveBuckets(registry1, testKit, nodeAddress);
         verifyBucket(buckets.get(nodeAddress), addedRouteIds);
 
         Map<Address, Long> versions = retrieveVersions(registry1, testKit);
-        Assert.assertEquals("Version for bucket " + nodeAddress, (Long) buckets.get(nodeAddress).getVersion(),
+        assertEquals("Version for bucket " + nodeAddress, (Long) buckets.get(nodeAddress).getVersion(),
                 versions.get(nodeAddress));
 
         // Now remove rpc
@@ -211,7 +210,7 @@ public class RpcRegistryTest {
      * 1 node, ensure 2nd node gets updated
      */
     @Test
-    public void testRpcAddRemoveInCluster() throws Exception {
+    public void testRpcAddRemoveInCluster() {
 
         LOG.info("testRpcAddRemoveInCluster starting");
 
@@ -223,7 +222,7 @@ public class RpcRegistryTest {
         registry1.tell(new AddOrUpdateRoutes(addedRouteIds), ActorRef.noSender());
 
         // Bucket store on node2 should get a message to update its local copy of remote buckets
-        final JavaTestKit testKit = new JavaTestKit(node2);
+        final TestKit testKit = new TestKit(node2);
 
         Map<Address, Bucket<RoutingTable>> buckets = retrieveBuckets(registry2, testKit, node1Address);
         verifyBucket(buckets.get(node1Address), addedRouteIds);
@@ -239,7 +238,7 @@ public class RpcRegistryTest {
         LOG.info("testRpcAddRemoveInCluster ending");
     }
 
-    private void verifyEmptyBucket(final JavaTestKit testKit, final ActorRef registry, final Address address)
+    private void verifyEmptyBucket(final TestKit testKit, final ActorRef registry, final Address address)
             throws AssertionError {
         Map<Address, Bucket<RoutingTable>> buckets;
         int numTries = 0;
@@ -263,21 +262,21 @@ public class RpcRegistryTest {
      * Three node cluster. Register rpc on 2 nodes. Ensure 3rd gets updated.
      */
     @Test
-    public void testRpcAddedOnMultiNodes() throws Exception {
-        final JavaTestKit testKit = new JavaTestKit(node3);
+    public void testRpcAddedOnMultiNodes() {
+        final TestKit testKit = new TestKit(node3);
 
         // Add rpc on node 1
         List<DOMRpcIdentifier> addedRouteIds1 = createRouteIds();
         registry1.tell(new AddOrUpdateRoutes(addedRouteIds1), ActorRef.noSender());
 
-        final UpdateRemoteEndpoints req1 = registrar3.expectMsgClass(Duration.create(3, TimeUnit.SECONDS),
+        final UpdateRemoteEndpoints req1 = registrar3.expectMsgClass(Duration.ofSeconds(3),
             UpdateRemoteEndpoints.class);
 
         // Add rpc on node 2
         List<DOMRpcIdentifier> addedRouteIds2 = createRouteIds();
         registry2.tell(new AddOrUpdateRoutes(addedRouteIds2), ActorRef.noSender());
 
-        final UpdateRemoteEndpoints req2 = registrar3.expectMsgClass(Duration.create(3, TimeUnit.SECONDS),
+        final UpdateRemoteEndpoints req2 = registrar3.expectMsgClass(Duration.ofSeconds(3),
             UpdateRemoteEndpoints.class);
         Address node2Address = node2.provider().getDefaultAddress();
         Address node1Address = node1.provider().getDefaultAddress();
@@ -289,9 +288,9 @@ public class RpcRegistryTest {
         verifyBucket(buckets.get(node2Address), addedRouteIds2);
 
         Map<Address, Long> versions = retrieveVersions(registry3, testKit);
-        Assert.assertEquals("Version for bucket " + node1Address, (Long) buckets.get(node1Address).getVersion(),
+        assertEquals("Version for bucket " + node1Address, (Long) buckets.get(node1Address).getVersion(),
                 versions.get(node1Address));
-        Assert.assertEquals("Version for bucket " + node2Address, (Long) buckets.get(node2Address).getVersion(),
+        assertEquals("Version for bucket " + node2Address, (Long) buckets.get(node2Address).getVersion(),
                 versions.get(node2Address));
 
         assertEndpoints(req1, node1Address, invoker1);
@@ -299,51 +298,49 @@ public class RpcRegistryTest {
 
     }
 
-    private static void assertEndpoints(final UpdateRemoteEndpoints msg, final Address address,
-            final JavaTestKit invoker) {
-        final Map<Address, Optional<RemoteRpcEndpoint>> endpoints = msg.getEndpoints();
-        Assert.assertEquals(1, endpoints.size());
+    private static void assertEndpoints(final UpdateRemoteEndpoints msg, final Address address, final TestKit invoker) {
+        final Map<Address, Optional<RemoteRpcEndpoint>> endpoints = msg.getRpcEndpoints();
+        assertEquals(1, endpoints.size());
 
         final Optional<RemoteRpcEndpoint> maybeEndpoint = endpoints.get(address);
-        Assert.assertNotNull(maybeEndpoint);
-        Assert.assertTrue(maybeEndpoint.isPresent());
+        assertNotNull(maybeEndpoint);
+        assertTrue(maybeEndpoint.isPresent());
 
         final RemoteRpcEndpoint endpoint = maybeEndpoint.get();
         final ActorRef router = endpoint.getRouter();
-        Assert.assertNotNull(router);
+        assertNotNull(router);
 
         router.tell("hello", ActorRef.noSender());
-        final String s = invoker.expectMsgClass(Duration.create(3, TimeUnit.SECONDS), String.class);
-        Assert.assertEquals("hello", s);
+        final String s = invoker.expectMsgClass(Duration.ofSeconds(3), String.class);
+        assertEquals("hello", s);
     }
 
-    private static Map<Address, Long> retrieveVersions(final ActorRef bucketStore, final JavaTestKit testKit) {
+    private static Map<Address, Long> retrieveVersions(final ActorRef bucketStore, final TestKit testKit) {
         bucketStore.tell(GET_BUCKET_VERSIONS, testKit.getRef());
         @SuppressWarnings("unchecked")
-        final Map<Address, Long> reply = testKit.expectMsgClass(Duration.create(3, TimeUnit.SECONDS), Map.class);
+        final Map<Address, Long> reply = testKit.expectMsgClass(Duration.ofSeconds(3), Map.class);
         return reply;
     }
 
     private static void verifyBucket(final Bucket<RoutingTable> bucket, final List<DOMRpcIdentifier> expRouteIds) {
         RoutingTable table = bucket.getData();
-        Assert.assertNotNull("Bucket RoutingTable is null", table);
+        assertNotNull("Bucket RoutingTable is null", table);
         for (DOMRpcIdentifier r : expRouteIds) {
             if (!table.contains(r)) {
-                Assert.fail("RoutingTable does not contain " + r + ". Actual: " + table);
+                fail("RoutingTable does not contain " + r + ". Actual: " + table);
             }
         }
 
-        Assert.assertEquals("RoutingTable size", expRouteIds.size(), table.size());
+        assertEquals("RoutingTable size", expRouteIds.size(), table.size());
     }
 
     private static Map<Address, Bucket<RoutingTable>> retrieveBuckets(final ActorRef bucketStore,
-            final JavaTestKit testKit, final Address... addresses) {
+            final TestKit testKit, final Address... addresses) {
         int numTries = 0;
         while (true) {
             bucketStore.tell(GET_ALL_BUCKETS, testKit.getRef());
             @SuppressWarnings("unchecked")
-            Map<Address, Bucket<RoutingTable>> buckets = testKit.expectMsgClass(Duration.create(3, TimeUnit.SECONDS),
-                    Map.class);
+            Map<Address, Bucket<RoutingTable>> buckets = testKit.expectMsgClass(Duration.ofSeconds(3), Map.class);
 
             boolean foundAll = true;
             for (Address addr : addresses) {
@@ -359,8 +356,7 @@ public class RpcRegistryTest {
             }
 
             if (++numTries >= 50) {
-                Assert.fail("Missing expected buckets for addresses: " + Arrays.toString(addresses)
-                        + ", Actual: " + buckets);
+                fail("Missing expected buckets for addresses: " + Arrays.toString(addresses) + ", Actual: " + buckets);
             }
 
             Uninterruptibles.sleepUninterruptibly(200, TimeUnit.MILLISECONDS);
@@ -368,14 +364,14 @@ public class RpcRegistryTest {
     }
 
     @Test
-    public void testAddRoutesConcurrency() throws Exception {
-        final JavaTestKit testKit = new JavaTestKit(node1);
+    public void testAddRoutesConcurrency() {
+        final TestKit testKit = new TestKit(node1);
 
         final int nRoutes = 500;
         final Collection<DOMRpcIdentifier> added = new ArrayList<>(nRoutes);
         for (int i = 0; i < nRoutes; i++) {
             final DOMRpcIdentifier routeId = DOMRpcIdentifier.create(SchemaPath.create(true,
-                    new QName(new URI("/mockrpc"), "type" + i)));
+                    QName.create(URI.create("/mockrpc"), "type" + i)));
             added.add(routeId);
 
             //Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
@@ -383,33 +379,32 @@ public class RpcRegistryTest {
                     ActorRef.noSender());
         }
 
-        FiniteDuration duration = Duration.create(3, TimeUnit.SECONDS);
         int numTries = 0;
         while (true) {
             registry1.tell(GET_ALL_BUCKETS, testKit.getRef());
             @SuppressWarnings("unchecked")
-            Map<Address, Bucket<RoutingTable>> buckets = testKit.expectMsgClass(duration, Map.class);
+            Map<Address, Bucket<RoutingTable>> buckets = testKit.expectMsgClass(Duration.ofSeconds(3), Map.class);
 
             Bucket<RoutingTable> localBucket = buckets.values().iterator().next();
             RoutingTable table = localBucket.getData();
             if (table != null && table.size() == nRoutes) {
                 for (DOMRpcIdentifier r : added) {
-                    Assert.assertTrue("RoutingTable contains " + r, table.contains(r));
+                    assertTrue("RoutingTable contains " + r, table.contains(r));
                 }
 
                 break;
             }
 
             if (++numTries >= 50) {
-                Assert.fail("Expected # routes: " + nRoutes + ", Actual: " + table.size());
+                fail("Expected # routes: " + nRoutes + ", Actual: " + table.size());
             }
 
             Uninterruptibles.sleepUninterruptibly(200, TimeUnit.MILLISECONDS);
         }
     }
 
-    private List<DOMRpcIdentifier> createRouteIds() throws URISyntaxException {
-        QName type = new QName(new URI("/mockrpc"), "mockrpc" + routeIdCounter++);
+    private List<DOMRpcIdentifier> createRouteIds() {
+        QName type = QName.create(URI.create("/mockrpc"), "mockrpc" + routeIdCounter++);
         List<DOMRpcIdentifier> routeIds = new ArrayList<>(1);
         routeIds.add(DOMRpcIdentifier.create(SchemaPath.create(true, type)));
         return routeIds;