+ private static final Logger LOG = LoggerFactory.getLogger(RpcRegistryTest.class);
+
+ private static ActorSystem node1;
+ private static ActorSystem node2;
+ private static ActorSystem node3;
+
+ 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;
+
+ private int routeIdCounter = 1;
+
+ @BeforeClass
+ public static void staticSetup() {
+ AkkaConfigurationReader reader = ConfigFactory::load;
+
+ RemoteRpcProviderConfig config1 = new RemoteRpcProviderConfig.Builder("memberA").gossipTickInterval("200ms")
+ .withConfigReader(reader).build();
+ RemoteRpcProviderConfig config2 = new RemoteRpcProviderConfig.Builder("memberB").gossipTickInterval("200ms")
+ .withConfigReader(reader).build();
+ RemoteRpcProviderConfig config3 = new RemoteRpcProviderConfig.Builder("memberC").gossipTickInterval("200ms")
+ .withConfigReader(reader).build();
+ node1 = ActorSystem.create("opendaylight-rpc", config1.get());
+ node2 = ActorSystem.create("opendaylight-rpc", config2.get());
+ node3 = ActorSystem.create("opendaylight-rpc", config3.get());
+
+ waitForMembersUp(node1, Cluster.get(node2).selfUniqueAddress(), Cluster.get(node3).selfUniqueAddress());
+ waitForMembersUp(node2, Cluster.get(node1).selfUniqueAddress(), Cluster.get(node3).selfUniqueAddress());
+ }
+
+ static void waitForMembersUp(final ActorSystem node, final UniqueAddress... addresses) {
+ Set<UniqueAddress> otherMembersSet = Sets.newHashSet(addresses);
+ Stopwatch sw = Stopwatch.createStarted();
+ while (sw.elapsed(TimeUnit.SECONDS) <= 10) {
+ CurrentClusterState state = Cluster.get(node).state();
+ for (Member m : state.getMembers()) {
+ if (m.status() == MemberStatus.up() && otherMembersSet.remove(m.uniqueAddress())
+ && otherMembersSet.isEmpty()) {
+ return;
+ }
+ }
+
+ Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS);
+ }
+
+ fail("Member(s) " + otherMembersSet + " are not Up");
+ }
+
+ @AfterClass
+ public static void staticTeardown() {
+ TestKit.shutdownActorSystem(node1);
+ TestKit.shutdownActorSystem(node2);
+ TestKit.shutdownActorSystem(node3);
+ }
+
+ @Before
+ public void setup() {
+ invoker1 = new TestKit(node1);
+ registrar1 = new TestKit(node1);
+ registry1 = node1.actorOf(RpcRegistry.props(config(node1), invoker1.getRef(), registrar1.getRef()));
+ invoker2 = new TestKit(node2);
+ registrar2 = new TestKit(node2);
+ registry2 = node2.actorOf(RpcRegistry.props(config(node2), invoker2.getRef(), registrar2.getRef()));
+ 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());
+ }
+
+ @After
+ public void teardown() {
+ if (registry1 != null) {
+ node1.stop(registry1);
+ }
+ if (registry2 != null) {
+ node2.stop(registry2);
+ }
+ if (registry3 != null) {
+ node3.stop(registry3);
+ }
+
+ if (invoker1 != null) {
+ node1.stop(invoker1.getRef());
+ }
+ if (invoker2 != null) {
+ node2.stop(invoker2.getRef());
+ }
+ if (invoker3 != null) {
+ node3.stop(invoker3.getRef());
+ }
+
+ if (registrar1 != null) {
+ node1.stop(registrar1.getRef());
+ }
+ if (registrar2 != null) {
+ node2.stop(registrar2.getRef());
+ }
+ if (registrar3 != null) {
+ node3.stop(registrar3.getRef());
+ }
+ }
+
+ /**
+ * One node cluster. 1. Register rpc, ensure router can be found 2. Then remove rpc, ensure its
+ * deleted
+ */
+ @Test
+ public void testAddRemoveRpcOnSameNode() {
+ LOG.info("testAddRemoveRpcOnSameNode starting");
+
+ Address nodeAddress = node1.provider().getDefaultAddress();
+
+ // Add rpc on node 1
+
+ List<DOMRpcIdentifier> addedRouteIds = createRouteIds();
+
+ registry1.tell(new AddOrUpdateRoutes(addedRouteIds), ActorRef.noSender());
+
+ // Bucket store should get an update bucket message. Updated bucket contains added rpc.
+ 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);
+ assertEquals("Version for bucket " + nodeAddress, (Long) buckets.get(nodeAddress).getVersion(),
+ versions.get(nodeAddress));
+
+ // Now remove rpc
+ registry1.tell(new RemoveRoutes(addedRouteIds), ActorRef.noSender());
+
+ // Bucket store should get an update bucket message. Rpc is removed in the updated bucket
+
+ verifyEmptyBucket(testKit, registry1, nodeAddress);
+
+ LOG.info("testAddRemoveRpcOnSameNode ending");
+
+ }
+
+ /**
+ * Three node cluster. 1. Register rpc on 1 node, ensure 2nd node gets updated 2. Remove rpc on
+ * 1 node, ensure 2nd node gets updated
+ */
+ @Test
+ public void testRpcAddRemoveInCluster() {
+
+ LOG.info("testRpcAddRemoveInCluster starting");
+
+ List<DOMRpcIdentifier> addedRouteIds = createRouteIds();
+
+ Address node1Address = node1.provider().getDefaultAddress();
+
+ // Add rpc on node 1
+ registry1.tell(new AddOrUpdateRoutes(addedRouteIds), ActorRef.noSender());
+
+ // Bucket store on node2 should get a message to update its local copy of remote buckets
+ final TestKit testKit = new TestKit(node2);
+
+ Map<Address, Bucket<RoutingTable>> buckets = retrieveBuckets(registry2, testKit, node1Address);
+ verifyBucket(buckets.get(node1Address), addedRouteIds);
+
+ // Now remove
+ registry1.tell(new RemoveRoutes(addedRouteIds), ActorRef.noSender());
+
+ // Bucket store on node2 should get a message to update its local copy of remote buckets.
+ // Wait for the bucket for node1 to be empty.
+
+ verifyEmptyBucket(testKit, registry2, node1Address);
+
+ LOG.info("testRpcAddRemoveInCluster ending");
+ }
+
+ private void verifyEmptyBucket(final TestKit testKit, final ActorRef registry, final Address address)
+ throws AssertionError {
+ Map<Address, Bucket<RoutingTable>> buckets;
+ int numTries = 0;
+ while (true) {
+ buckets = retrieveBuckets(registry1, testKit, address);
+
+ try {
+ verifyBucket(buckets.get(address), Collections.emptyList());
+ break;
+ } catch (AssertionError e) {
+ if (++numTries >= 50) {
+ throw e;
+ }
+ }
+
+ Uninterruptibles.sleepUninterruptibly(200, TimeUnit.MILLISECONDS);
+ }
+ }
+
+ /**
+ * Three node cluster. Register rpc on 2 nodes. Ensure 3rd gets updated.
+ */
+ @Test
+ public void testRpcAddedOnMultiNodes() {
+ final TestKit testKit = new TestKit(node3);