Moving from local to netty transport.

Change-Id: Id37af6fa4d0971fd34ed18951196dde47bc4a12d
diff --git a/core/store/primitives/src/test/java/org/onosproject/store/primitives/resources/impl/AsyncConsistentSetMultimapTest.java b/core/store/primitives/src/test/java/org/onosproject/store/primitives/resources/impl/AsyncConsistentSetMultimapTest.java
index aad068a..04fcdbc 100644
--- a/core/store/primitives/src/test/java/org/onosproject/store/primitives/resources/impl/AsyncConsistentSetMultimapTest.java
+++ b/core/store/primitives/src/test/java/org/onosproject/store/primitives/resources/impl/AsyncConsistentSetMultimapTest.java
@@ -20,27 +20,18 @@
 import com.google.common.collect.Multiset;
 import com.google.common.collect.TreeMultiset;
 import com.google.common.io.Files;
-
-import io.atomix.catalyst.transport.Address;
-import io.atomix.catalyst.transport.local.LocalTransport;
-import io.atomix.copycat.server.CopycatServer;
-import io.atomix.copycat.server.storage.Storage;
-import io.atomix.copycat.server.storage.StorageLevel;
-import io.atomix.manager.internal.ResourceManagerState;
 import io.atomix.resource.ResourceType;
-
 import org.apache.commons.collections.keyvalue.DefaultMapEntry;
-import org.junit.Ignore;
 import org.junit.Test;
 import org.onlab.util.Tools;
 
 import java.io.File;
-import java.time.Duration;
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.Comparator;
 import java.util.List;
 import java.util.Map;
+import java.util.concurrent.atomic.AtomicInteger;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
@@ -49,7 +40,6 @@
 /**
  * Tests the {@link AsyncConsistentSetMultimap}.
  */
-@Ignore
 public class AsyncConsistentSetMultimapTest extends AtomixTestBase {
     private final File testDir = Files.createTempDir();
     private final String keyOne = "hello";
@@ -66,6 +56,7 @@
                                                               valueTwo,
                                                               valueThree,
                                                               valueFour);
+    private final AtomicInteger port = new AtomicInteger(49200);
 
     @Override
     protected ResourceType resourceType() {
@@ -411,7 +402,6 @@
         clearTests();
     }
 
-
     private AsyncConsistentSetMultimap createResource(int clusterSize) {
         try {
             createCopycatServers(clusterSize);
@@ -424,24 +414,6 @@
         }
     }
 
-    @Override
-    protected CopycatServer createCopycatServer(Address address) {
-        CopycatServer server = CopycatServer.builder(address)
-                .withTransport(new LocalTransport(registry))
-                .withStorage(Storage.builder()
-                                     .withStorageLevel(StorageLevel.MEMORY)
-                                     .withDirectory(testDir + "/" + address.port())
-                                     .build())
-                .withStateMachine(ResourceManagerState::new)
-                .withSerializer(serializer.clone())
-                .withHeartbeatInterval(Duration.ofMillis(25))
-                .withElectionTimeout(Duration.ofMillis(50))
-                .withSessionTimeout(Duration.ofMillis(100))
-                .build();
-        copycatServers.add(server);
-        return server;
-    }
-
     /**
      * Returns two arrays contain the same set of elements,
      * regardless of order.
diff --git a/core/store/primitives/src/test/java/org/onosproject/store/primitives/resources/impl/AtomixConsistentMapTest.java b/core/store/primitives/src/test/java/org/onosproject/store/primitives/resources/impl/AtomixConsistentMapTest.java
index 0ec21f7..df44ecf 100644
--- a/core/store/primitives/src/test/java/org/onosproject/store/primitives/resources/impl/AtomixConsistentMapTest.java
+++ b/core/store/primitives/src/test/java/org/onosproject/store/primitives/resources/impl/AtomixConsistentMapTest.java
@@ -15,19 +15,9 @@
  */
 package org.onosproject.store.primitives.resources.impl;
 
+import com.google.common.base.Throwables;
+import com.google.common.collect.Sets;
 import io.atomix.resource.ResourceType;
-import static org.hamcrest.Matchers.*;
-import static org.junit.Assert.*;
-
-import java.util.Arrays;
-import java.util.ConcurrentModificationException;
-import java.util.List;
-import java.util.concurrent.ArrayBlockingQueue;
-import java.util.concurrent.BlockingQueue;
-import java.util.concurrent.CompletionException;
-import java.util.stream.Collectors;
-
-import org.junit.Ignore;
 import org.junit.Test;
 import org.onlab.util.Tools;
 import org.onosproject.store.primitives.MapUpdate;
@@ -37,13 +27,27 @@
 import org.onosproject.store.service.MapTransaction;
 import org.onosproject.store.service.Versioned;
 
-import com.google.common.base.Throwables;
-import com.google.common.collect.Sets;
+import java.util.Arrays;
+import java.util.ConcurrentModificationException;
+import java.util.List;
+import java.util.concurrent.ArrayBlockingQueue;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.CompletionException;
+import java.util.stream.Collectors;
+
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
 
 /**
  * Unit tests for {@link AtomixConsistentMap}.
  */
-@Ignore
 public class AtomixConsistentMapTest extends AtomixTestBase {
 
     @Override
diff --git a/core/store/primitives/src/test/java/org/onosproject/store/primitives/resources/impl/AtomixLeaderElectorTest.java b/core/store/primitives/src/test/java/org/onosproject/store/primitives/resources/impl/AtomixLeaderElectorTest.java
index 6fb064b..f6be9f7 100644
--- a/core/store/primitives/src/test/java/org/onosproject/store/primitives/resources/impl/AtomixLeaderElectorTest.java
+++ b/core/store/primitives/src/test/java/org/onosproject/store/primitives/resources/impl/AtomixLeaderElectorTest.java
@@ -20,7 +20,6 @@
 import java.util.concurrent.CompletableFuture;
 import java.util.function.Consumer;
 
-import org.junit.Ignore;
 import org.junit.Test;
 
 import static org.junit.Assert.*;
@@ -36,7 +35,6 @@
 /**
  * Unit tests for {@link AtomixLeaderElector}.
  */
-@Ignore
 public class AtomixLeaderElectorTest extends AtomixTestBase {
 
     NodeId node1 = new NodeId("node1");
diff --git a/core/store/primitives/src/test/java/org/onosproject/store/primitives/resources/impl/AtomixLongTest.java b/core/store/primitives/src/test/java/org/onosproject/store/primitives/resources/impl/AtomixLongTest.java
index 49625b6..ae7bfee 100644
--- a/core/store/primitives/src/test/java/org/onosproject/store/primitives/resources/impl/AtomixLongTest.java
+++ b/core/store/primitives/src/test/java/org/onosproject/store/primitives/resources/impl/AtomixLongTest.java
@@ -15,19 +15,18 @@
  */
 package org.onosproject.store.primitives.resources.impl;
 
-import static org.junit.Assert.*;
-
-import org.junit.Ignore;
-import org.junit.Test;
-
 import io.atomix.Atomix;
 import io.atomix.resource.ResourceType;
 import io.atomix.variables.DistributedLong;
+import org.junit.Test;
 
-/**
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+/**git s
  * Unit tests for {@link AtomixCounter}.
  */
-@Ignore
 public class AtomixLongTest extends AtomixTestBase {
 
     @Override
diff --git a/core/store/primitives/src/test/java/org/onosproject/store/primitives/resources/impl/AtomixTestBase.java b/core/store/primitives/src/test/java/org/onosproject/store/primitives/resources/impl/AtomixTestBase.java
index 82265ad..01c33ec 100644
--- a/core/store/primitives/src/test/java/org/onosproject/store/primitives/resources/impl/AtomixTestBase.java
+++ b/core/store/primitives/src/test/java/org/onosproject/store/primitives/resources/impl/AtomixTestBase.java
@@ -15,17 +15,22 @@
  */
 package org.onosproject.store.primitives.resources.impl;
 
+import com.google.common.util.concurrent.Uninterruptibles;
 import io.atomix.AtomixClient;
 import io.atomix.catalyst.serializer.Serializer;
 import io.atomix.catalyst.transport.Address;
 import io.atomix.catalyst.transport.local.LocalServerRegistry;
-import io.atomix.catalyst.transport.local.LocalTransport;
+import io.atomix.catalyst.transport.netty.NettyTransport;
 import io.atomix.copycat.client.CopycatClient;
 import io.atomix.copycat.server.CopycatServer;
 import io.atomix.copycat.server.storage.Storage;
 import io.atomix.copycat.server.storage.StorageLevel;
 import io.atomix.manager.internal.ResourceManagerState;
 import io.atomix.resource.ResourceType;
+import org.junit.After;
+import org.junit.Before;
+import org.onlab.junit.TestTools;
+import org.onosproject.store.primitives.impl.CatalystSerializers;
 
 import java.io.File;
 import java.io.IOException;
@@ -35,12 +40,7 @@
 import java.util.List;
 import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.CountDownLatch;
-
-import org.junit.After;
-import org.junit.Before;
-import org.onosproject.store.primitives.impl.CatalystSerializers;
-
-import com.google.common.util.concurrent.Uninterruptibles;
+import java.util.concurrent.atomic.AtomicInteger;
 
 /**
  * Base class for various Atomix* tests.
@@ -48,7 +48,7 @@
 public abstract class AtomixTestBase {
     private static final File TEST_DIR = new File("target/test-logs");
     protected LocalServerRegistry registry;
-    protected int port;
+    protected final AtomicInteger port = new AtomicInteger(49200);
     protected List<Address> members;
     protected List<CopycatClient> copycatClients = new ArrayList<>();
     protected List<CopycatServer> copycatServers = new ArrayList<>();
@@ -69,7 +69,8 @@
      * @return The next server address.
      */
     private Address nextAddress() {
-        Address address = new Address("localhost", port++);
+        Address address = new Address("127.0.0.1",
+                          TestTools.findAvailablePort(port.getAndIncrement()));
         members.add(address);
         return address;
     }
@@ -82,13 +83,16 @@
         List<CopycatServer> servers = new ArrayList<>();
 
         List<Address> members = new ArrayList<>();
-        for (int i = 0; i < nodes; i++) {
-            members.add(nextAddress());
-        }
 
         for (int i = 0; i < nodes; i++) {
-            CopycatServer server = createCopycatServer(members.get(i));
-            server.bootstrap(members).thenRun(latch::countDown);
+            Address address = nextAddress();
+            members.add(address);
+            CopycatServer server = createCopycatServer(address);
+            if (members.size() <= 1) {
+                server.bootstrap().thenRun(latch::countDown).join();
+            } else {
+                server.join(members).thenRun(latch::countDown);
+            }
             servers.add(server);
         }
 
@@ -102,11 +106,10 @@
      */
     protected CopycatServer createCopycatServer(Address address) {
         CopycatServer server = CopycatServer.builder(address)
-                .withTransport(new LocalTransport(registry))
+                .withTransport(NettyTransport.builder().withThreads(1).build())
                 .withStorage(Storage.builder()
-                        .withStorageLevel(StorageLevel.DISK)
-                        .withDirectory(TEST_DIR + "/" + address.port())
-                        .build())
+                             .withStorageLevel(StorageLevel.MEMORY)
+                             .build())
                 .withStateMachine(ResourceManagerState::new)
                 .withSerializer(serializer.clone())
                 .withHeartbeatInterval(Duration.ofMillis(25))
@@ -122,7 +125,6 @@
     public void clearTests() throws Exception {
         registry = new LocalServerRegistry();
         members = new ArrayList<>();
-        port = 5000;
 
         CompletableFuture<Void> closeClients =
                 CompletableFuture.allOf(atomixClients.stream()
@@ -165,7 +167,7 @@
     protected AtomixClient createAtomixClient() {
         CountDownLatch latch = new CountDownLatch(1);
         AtomixClient client = AtomixClient.builder()
-                .withTransport(new LocalTransport(registry))
+                .withTransport(NettyTransport.builder().withThreads(1).build())
                 .withSerializer(serializer.clone())
                 .build();
         client.connect(members).thenRun(latch::countDown);