Extract JGroups configuration into jgroups section

Until now all JGroups configuration was contained in the
peerInfo.jgroups subsection. This made sense if we would only use
JGroups for peer discovery. However, if we want to support message
transport over JGroups then we need a standalone [jgroups] section,
same like we have a standalone [http] section for REST based message
transport.

This change is a preparation for JGroups based message transport.

Change-Id: Ib8b831b524f69604effe59f50df221f4ec630b23
diff --git a/src/main/java/com/ericsson/gerrit/plugins/highavailability/Configuration.java b/src/main/java/com/ericsson/gerrit/plugins/highavailability/Configuration.java
index 8cb57f3..fb74e34 100644
--- a/src/main/java/com/ericsson/gerrit/plugins/highavailability/Configuration.java
+++ b/src/main/java/com/ericsson/gerrit/plugins/highavailability/Configuration.java
@@ -54,6 +54,11 @@
   static final String STRATEGY_KEY = "strategy";
   static final String MY_URL_KEY = "myUrl";
 
+  // jgroups section
+  static final String JGROUPS_SECTION = "jgroups";
+  static final String SKIP_INTERFACE_KEY = "skipInterface";
+  static final String CLUSTER_NAME_KEY = "clusterName";
+
   // http section
   static final String HTTP_SECTION = "http";
   static final String USER_KEY = "user";
@@ -83,10 +88,6 @@
   static final String WEBSESSION_SECTION = "websession";
   static final String CLEANUP_INTERVAL_KEY = "cleanupInterval";
 
-  // jgroups section used if peerInfo.strategy == jgroups
-  static final String SKIP_INTERFACE_KEY = "skipInterface";
-  static final String CLUSTER_NAME_KEY = "clusterName";
-
   static final int DEFAULT_TIMEOUT_MS = 5000;
   static final int DEFAULT_MAX_TRIES = 5;
   static final int DEFAULT_RETRY_INTERVAL = 1000;
@@ -101,6 +102,7 @@
 
   private final Main main;
   private final PeerInfo peerInfo;
+  private final JGroups jgroups;
   private final Http http;
   private final Cache cache;
   private final Event event;
@@ -130,6 +132,7 @@
       default:
         throw new IllegalArgumentException("Not supported strategy: " + peerInfo.strategy);
     }
+    jgroups = new JGroups(cfg);
     http = new Http(cfg);
     cache = new Cache(cfg);
     event = new Event(cfg);
@@ -153,6 +156,10 @@
     return peerInfoJGroups;
   }
 
+  public JGroups jgroups() {
+    return jgroups;
+  }
+
   public Http http() {
     return http;
   }
@@ -252,17 +259,27 @@
   }
 
   public static class PeerInfoJGroups {
-    private final ImmutableList<String> skipInterface;
-    private final String clusterName;
     private final String myUrl;
 
     private PeerInfoJGroups(Config cfg) {
-      String[] skip = cfg.getStringList(PEER_INFO_SECTION, JGROUPS_SUBSECTION, SKIP_INTERFACE_KEY);
+      myUrl = trimTrailingSlash(cfg.getString(PEER_INFO_SECTION, JGROUPS_SUBSECTION, MY_URL_KEY));
+    }
+
+    public String myUrl() {
+      return myUrl;
+    }
+  }
+
+  public static class JGroups {
+    private final ImmutableList<String> skipInterface;
+    private final String clusterName;
+
+    private JGroups(Config cfg) {
+      String[] skip = cfg.getStringList(JGROUPS_SECTION, null, SKIP_INTERFACE_KEY);
       skipInterface = skip == null ? DEFAULT_SKIP_INTERFACE_LIST : ImmutableList.copyOf(skip);
       clusterName =
           getString(
-              cfg, PEER_INFO_SECTION, JGROUPS_SUBSECTION, CLUSTER_NAME_KEY, DEFAULT_CLUSTER_NAME);
-      myUrl = trimTrailingSlash(cfg.getString(PEER_INFO_SECTION, JGROUPS_SUBSECTION, MY_URL_KEY));
+              cfg, JGROUPS_SECTION, null, CLUSTER_NAME_KEY, DEFAULT_CLUSTER_NAME);
     }
 
     public ImmutableList<String> skipInterface() {
@@ -272,10 +289,6 @@
     public String clusterName() {
       return clusterName;
     }
-
-    public String myUrl() {
-      return myUrl;
-    }
   }
 
   public static class Http {
diff --git a/src/main/java/com/ericsson/gerrit/plugins/highavailability/peers/jgroups/InetAddressFinder.java b/src/main/java/com/ericsson/gerrit/plugins/highavailability/peers/jgroups/InetAddressFinder.java
index 04c67d4..c973b4a 100644
--- a/src/main/java/com/ericsson/gerrit/plugins/highavailability/peers/jgroups/InetAddressFinder.java
+++ b/src/main/java/com/ericsson/gerrit/plugins/highavailability/peers/jgroups/InetAddressFinder.java
@@ -32,12 +32,12 @@
 public class InetAddressFinder {
 
   private final boolean preferIPv4;
-  private final Configuration.PeerInfoJGroups jgroupsConfig;
+  private final Configuration.JGroups jgroupsConfig;
 
   @Inject
   InetAddressFinder(Configuration pluginConfiguration) {
     preferIPv4 = Boolean.getBoolean("java.net.preferIPv4Stack");
-    jgroupsConfig = pluginConfiguration.peerInfoJGroups();
+    jgroupsConfig = pluginConfiguration.jgroups();
   }
 
   /**
diff --git a/src/main/java/com/ericsson/gerrit/plugins/highavailability/peers/jgroups/JGroupsPeerInfoProvider.java b/src/main/java/com/ericsson/gerrit/plugins/highavailability/peers/jgroups/JGroupsPeerInfoProvider.java
index 39ba578..610d781 100644
--- a/src/main/java/com/ericsson/gerrit/plugins/highavailability/peers/jgroups/JGroupsPeerInfoProvider.java
+++ b/src/main/java/com/ericsson/gerrit/plugins/highavailability/peers/jgroups/JGroupsPeerInfoProvider.java
@@ -45,7 +45,7 @@
     implements Provider<Optional<PeerInfo>>, LifecycleListener {
   private static final Logger log = LoggerFactory.getLogger(JGroupsPeerInfoProvider.class);
 
-  private final Configuration.PeerInfoJGroups jgroupsConfig;
+  private final Configuration.JGroups jgroupsConfig;
   private final InetAddressFinder finder;
   private final String myUrl;
 
@@ -56,7 +56,7 @@
   @Inject
   JGroupsPeerInfoProvider(
       Configuration pluginConfiguration, InetAddressFinder finder, MyUrlProvider myUrlProvider) {
-    this.jgroupsConfig = pluginConfiguration.peerInfoJGroups();
+    this.jgroupsConfig = pluginConfiguration.jgroups();
     this.finder = finder;
     this.myUrl = myUrlProvider.get();
   }
diff --git a/src/main/resources/Documentation/config.md b/src/main/resources/Documentation/config.md
index 236915f..42ce673 100644
--- a/src/main/resources/Documentation/config.md
+++ b/src/main/resources/Documentation/config.md
@@ -23,6 +23,7 @@
 :  strategy = jgroups
 [peerInfo "jgroups"]
 :  myUrl = local_instance_url
+[jgroups]
 :  clusterName = foo
 :  skipInterface = lo*
 :  skipInterface = eth2
@@ -53,20 +54,6 @@
 peerInfo.static.url
 :   Specify the URL for the peer instance.
 
-peerInfo.jgroups.clusterName
-:   The name of the high-availability cluster. When peers discover themselves dynamically this
-    name is used to determine which instances should work together.  Only those Gerrit
-    interfaces which are configured for the same clusterName will communicate with each other.
-    Defaults to "GerritHA".
-
-peerInfo.jgroups.skipInterface
-:   A name or a wildcard of network interface(s) which should be skipped
-    for JGroups communication. Peer discovery may fail if the host has multiple
-    network interfaces and an inappropriate interface is chosen by JGroups.
-    This option can be repeated many times in the `jgroups` section.
-    Defaults to the list of: `lo*`, `utun*`, `awdl*` which are known to be
-    inappropriate for JGroups communication.
-
 peerInfo.jgroups.myUrl
 :   The URL of this instance to be broadcast to other peers. If not specified, the
     URL is determined from the `httpd.listenUrl` in the `gerrit.config`.
@@ -75,6 +62,20 @@
     configured to listen on all local addresses (i.e. using hostname `*`), then
     the URL must be explicitly specified with `myUrl`.
 
+jgroups.clusterName
+:   The name of the high-availability cluster. When peers discover themselves dynamically this
+    name is used to determine which instances should work together.  Only those Gerrit
+    interfaces which are configured for the same clusterName will communicate with each other.
+    Defaults to "GerritHA".
+
+jgroups.skipInterface
+:   A name or a wildcard of network interface(s) which should be skipped
+    for JGroups communication. Peer discovery may fail if the host has multiple
+    network interfaces and an inappropriate interface is chosen by JGroups.
+    This option can be repeated many times in the `jgroups` section.
+    Defaults to the list of: `lo*`, `utun*`, `awdl*` which are known to be
+    inappropriate for JGroups communication.
+
 NOTE: To work properly in certain environments, JGroups needs the System property
 `java.net.preferIPv4Stack` to be set to `true`.
 See (http://jgroups.org/tutorial/index.html#_trouble_shooting).
diff --git a/src/test/java/com/ericsson/gerrit/plugins/highavailability/ConfigurationTest.java b/src/test/java/com/ericsson/gerrit/plugins/highavailability/ConfigurationTest.java
index 145a0c9..f3ff562 100644
--- a/src/test/java/com/ericsson/gerrit/plugins/highavailability/ConfigurationTest.java
+++ b/src/test/java/com/ericsson/gerrit/plugins/highavailability/ConfigurationTest.java
@@ -30,6 +30,7 @@
 import static com.ericsson.gerrit.plugins.highavailability.Configuration.EVENT_SECTION;
 import static com.ericsson.gerrit.plugins.highavailability.Configuration.HTTP_SECTION;
 import static com.ericsson.gerrit.plugins.highavailability.Configuration.INDEX_SECTION;
+import static com.ericsson.gerrit.plugins.highavailability.Configuration.JGROUPS_SECTION;
 import static com.ericsson.gerrit.plugins.highavailability.Configuration.JGROUPS_SUBSECTION;
 import static com.ericsson.gerrit.plugins.highavailability.Configuration.MAIN_SECTION;
 import static com.ericsson.gerrit.plugins.highavailability.Configuration.MAX_TRIES_KEY;
@@ -157,12 +158,12 @@
     when(configMock.getEnum(PEER_INFO_SECTION, null, STRATEGY_KEY, DEFAULT_PEER_INFO_STRATEGY))
         .thenReturn(Configuration.PeerInfoStrategy.JGROUPS);
     initializeConfiguration();
-    assertThat(configuration.peerInfoJGroups().clusterName()).isEqualTo(DEFAULT_CLUSTER_NAME);
+    assertThat(configuration.jgroups().clusterName()).isEqualTo(DEFAULT_CLUSTER_NAME);
 
-    when(configMock.getString(PEER_INFO_SECTION, JGROUPS_SUBSECTION, CLUSTER_NAME_KEY))
+    when(configMock.getString(JGROUPS_SECTION, null, CLUSTER_NAME_KEY))
         .thenReturn("foo");
     initializeConfiguration();
-    assertThat(configuration.peerInfoJGroups().clusterName()).isEqualTo("foo");
+    assertThat(configuration.jgroups().clusterName()).isEqualTo("foo");
   }
 
   @Test
@@ -170,13 +171,13 @@
     when(configMock.getEnum(PEER_INFO_SECTION, null, STRATEGY_KEY, DEFAULT_PEER_INFO_STRATEGY))
         .thenReturn(Configuration.PeerInfoStrategy.JGROUPS);
     initializeConfiguration();
-    assertThat(configuration.peerInfoJGroups().skipInterface())
+    assertThat(configuration.jgroups().skipInterface())
         .isEqualTo(DEFAULT_SKIP_INTERFACE_LIST);
 
-    when(configMock.getStringList(PEER_INFO_SECTION, JGROUPS_SUBSECTION, SKIP_INTERFACE_KEY))
+    when(configMock.getStringList(JGROUPS_SECTION, null, SKIP_INTERFACE_KEY))
         .thenReturn(new String[] {"lo*", "eth0"});
     initializeConfiguration();
-    assertThat(configuration.peerInfoJGroups().skipInterface())
+    assertThat(configuration.jgroups().skipInterface())
         .containsAllOf("lo*", "eth0")
         .inOrder();
   }
diff --git a/src/test/java/com/ericsson/gerrit/plugins/highavailability/peers/jgroups/InetAddressFinderTest.java b/src/test/java/com/ericsson/gerrit/plugins/highavailability/peers/jgroups/InetAddressFinderTest.java
index 7b1dea4..3fb5a90 100644
--- a/src/test/java/com/ericsson/gerrit/plugins/highavailability/peers/jgroups/InetAddressFinderTest.java
+++ b/src/test/java/com/ericsson/gerrit/plugins/highavailability/peers/jgroups/InetAddressFinderTest.java
@@ -29,12 +29,12 @@
 public class InetAddressFinderTest {
 
   @Mock private Configuration configuration;
-  @Mock private Configuration.PeerInfoJGroups jgroupsConfig;
+  @Mock private Configuration.JGroups jgroupsConfig;
   private InetAddressFinder finder;
 
   @Before
   public void setUp() {
-    when(configuration.peerInfoJGroups()).thenReturn(jgroupsConfig);
+    when(configuration.jgroups()).thenReturn(jgroupsConfig);
     finder = new InetAddressFinder(configuration);
   }