From be1da0446a8427a6c2faac8b4b9725004057c4a8 Mon Sep 17 00:00:00 2001
From: Benoit Lavenier <benoit.lavenier@e-is.pro>
Date: Wed, 30 Dec 2020 16:25:06 +0100
Subject: [PATCH] [enh] Add GVA and GVASUB API to endpoint parser

---
 .../core/client/model/bma/EndpointApi.java    |  2 +
 .../core/client/model/bma/Endpoints.java      | 62 ++++++++++++++-----
 .../core/client/model/bma/NetworkPeering.java | 14 +++++
 .../core/client/model/bma/EndpointsTest.java  | 24 +++++++
 4 files changed, 86 insertions(+), 16 deletions(-)

diff --git a/duniter4j-core-client/src/main/java/org/duniter/core/client/model/bma/EndpointApi.java b/duniter4j-core-client/src/main/java/org/duniter/core/client/model/bma/EndpointApi.java
index 0b58d854..94314e19 100644
--- a/duniter4j-core-client/src/main/java/org/duniter/core/client/model/bma/EndpointApi.java
+++ b/duniter4j-core-client/src/main/java/org/duniter/core/client/model/bma/EndpointApi.java
@@ -37,6 +37,8 @@ public enum EndpointApi implements IEndpointApi {
     BMATOR(),
     WS2P(),
     WS2PTOR(),
+    GVA(),
+    GVASUB(),
     ES_CORE_API(),
     ES_USER_API(),
     ES_SUBSCRIPTION_API(),
diff --git a/duniter4j-core-client/src/main/java/org/duniter/core/client/model/bma/Endpoints.java b/duniter4j-core-client/src/main/java/org/duniter/core/client/model/bma/Endpoints.java
index 717e7599..21c56c53 100644
--- a/duniter4j-core-client/src/main/java/org/duniter/core/client/model/bma/Endpoints.java
+++ b/duniter4j-core-client/src/main/java/org/duniter/core/client/model/bma/Endpoints.java
@@ -46,11 +46,15 @@ public class Endpoints {
     public static final String BMA_API_REGEXP = "^BASIC_MERKLED_API" + EP_END_REGEXP;
     public static final String BMAS_API_REGEXP = "^BMAS" + EP_END_REGEXP;
     public static final String WS2P_API_REGEXP = "^(WS2P(?:TOR)?) ([a-f0-9]{7,8})" + EP_END_REGEXP;
+    public static final String GVA_API_REGEXP = "^GVA(:? S)?" + EP_END_REGEXP;
+    public static final String GVASUB_API_REGEXP = "^GVASUB(:? S)?" + EP_END_REGEXP;
     public static final String OTHER_API_REGEXP = "^([A-Z_-]+)" + EP_END_REGEXP;
 
     private static Pattern bmaPattern = Pattern.compile(BMA_API_REGEXP);
     private static Pattern bmasPattern = Pattern.compile(BMAS_API_REGEXP);
     private static Pattern ws2pPattern = Pattern.compile(WS2P_API_REGEXP);
+    private static Pattern gvaPattern = Pattern.compile(GVA_API_REGEXP);
+    private static Pattern gvaSubPattern = Pattern.compile(GVASUB_API_REGEXP);
     private static Pattern otherApiPattern = Pattern.compile(OTHER_API_REGEXP);
 
     private Endpoints() {
@@ -62,8 +66,49 @@ public class Endpoints {
         NetworkPeering.Endpoint endpoint = new NetworkPeering.Endpoint();
         endpoint.setRaw(raw);
 
+        // WS2P API
+        Matcher mather = ws2pPattern.matcher(raw);
+        if (mather.matches()) {
+            String api = mather.group(1);
+            try {
+                endpoint.api = EndpointApi.valueOf(api).label();
+                endpoint.id = mather.group(2);
+                parseDefaultFormatEndPoint(mather, endpoint, 3);
+                return Optional.of(endpoint);
+            } catch(Exception e) {
+                // Unknown API
+                throw new IOException("Unable to deserialize endpoint: WS2P api [" + api + "]", e); // link the exception
+            }
+        }
+
+        // GVA API
+        mather = gvaPattern.matcher(raw);
+        if (mather.matches()) {
+            endpoint.api = EndpointApi.GVA.label();
+            try {
+                parseDefaultFormatEndPoint(mather, endpoint, 2);
+                return Optional.of(endpoint);
+            } catch(Exception e) {
+                // Unknown API
+                throw new IOException("Unable to deserialize endpoint: api [" + endpoint.api + "]", e); // link the exception
+            }
+        }
+
+        // GVA SUB API
+        mather = gvaSubPattern.matcher(raw);
+        if (mather.matches()) {
+            endpoint.api = EndpointApi.GVASUB.label();
+            try {
+                parseDefaultFormatEndPoint(mather, endpoint, 2);
+                return Optional.of(endpoint);
+            } catch(Exception e) {
+                // Unknown API
+                throw new IOException("Unable to deserialize endpoint: api [" + endpoint.api + "]", e); // link the exception
+            }
+        }
+
         // BMA API
-        Matcher mather = bmaPattern.matcher(raw);
+        mather = bmaPattern.matcher(raw);
         if (mather.matches()) {
             endpoint.api = EndpointApi.BASIC_MERKLED_API.label();
             parseDefaultFormatEndPoint(mather, endpoint, 1);
@@ -78,21 +123,6 @@ public class Endpoints {
             return Optional.of(endpoint);
         }
 
-        // WS2P API
-        mather = ws2pPattern.matcher(raw);
-        if (mather.matches()) {
-            String api = mather.group(1);
-            try {
-                endpoint.api = EndpointApi.valueOf(api).label();
-                endpoint.id = mather.group(2);
-                parseDefaultFormatEndPoint(mather, endpoint, 3);
-                return Optional.of(endpoint);
-            } catch(Exception e) {
-                // Unknown API
-                throw new IOException("Unable to deserialize endpoint: WS2P api [" + api + "]", e); // link the exception
-            }
-        }
-
         // Other API
         mather = otherApiPattern.matcher(raw);
         if (mather.matches()) {
diff --git a/duniter4j-core-client/src/main/java/org/duniter/core/client/model/bma/NetworkPeering.java b/duniter4j-core-client/src/main/java/org/duniter/core/client/model/bma/NetworkPeering.java
index 654e93fd..b4be598a 100644
--- a/duniter4j-core-client/src/main/java/org/duniter/core/client/model/bma/NetworkPeering.java
+++ b/duniter4j-core-client/src/main/java/org/duniter/core/client/model/bma/NetworkPeering.java
@@ -169,6 +169,7 @@ public class NetworkPeering implements Serializable {
         public Integer port;
         public String id;
         public String path;
+        public Boolean useSsl;
         public String raw;
 
         public String getApi() {
@@ -227,6 +228,14 @@ public class NetworkPeering implements Serializable {
             this.path = path;
         }
 
+        public Boolean useSsl() {
+            return useSsl;
+        }
+
+        public void setUseSsl(Boolean useSsl) {
+            this.useSsl = useSsl;
+        }
+
         @JsonIgnore
         public String getRaw() {
             return raw;
@@ -246,6 +255,10 @@ public class NetworkPeering implements Serializable {
             if (api != null) {
                 joiner.add(api);
             }
+            // SSL
+            if (useSsl != null && useSsl.booleanValue()) {
+                joiner.add("S"); // useSsl ? used by GVA or GVASUB
+            }
             // Id (use for WS2P)
             if (StringUtils.isNotBlank(id)) {
                 joiner.add(id);
@@ -277,6 +290,7 @@ public class NetworkPeering implements Serializable {
         public boolean equals(Object obj) {
             if (obj instanceof Endpoint) {
                 return Objects.equals(((Endpoint) obj).api, api)
+                        && Objects.equals(((Endpoint) obj).useSsl, useSsl)
                         && Objects.equals(((Endpoint) obj).id, id)
                         && Objects.equals(((Endpoint) obj).dns, dns)
                         && Objects.equals(((Endpoint) obj).ipv4, ipv4)
diff --git a/duniter4j-core-client/src/test/java/org/duniter/core/client/model/bma/EndpointsTest.java b/duniter4j-core-client/src/test/java/org/duniter/core/client/model/bma/EndpointsTest.java
index 83309887..cdef30fc 100644
--- a/duniter4j-core-client/src/test/java/org/duniter/core/client/model/bma/EndpointsTest.java
+++ b/duniter4j-core-client/src/test/java/org/duniter/core/client/model/bma/EndpointsTest.java
@@ -62,6 +62,30 @@ public class EndpointsTest {
         Assert.assertNotNull(ep.port);
         Assert.assertEquals(20901, ep.port.intValue());
 
+
+        // GVA
+        ep = Endpoints.parse("GVA S g1.librelois.fr 443 gva").orElse(null);
+        Assert.assertNotNull(ep);
+        Assert.assertEquals(EndpointApi.GVA.label(), ep.api);
+        Assert.assertEquals("g1.librelois.fr", ep.dns);
+        Assert.assertNull(ep.ipv4);
+        Assert.assertNotNull(ep.port);
+        Assert.assertEquals(443, ep.port.intValue());
+        Assert.assertNull(ep.id);
+        Assert.assertEquals("gva", ep.path);
+
+        // GVA SUB
+        ep = Endpoints.parse("GVASUB S g1.librelois.fr 443 gva-sub").orElse(null);
+        Assert.assertNotNull(ep);
+        Assert.assertEquals(EndpointApi.GVASUB.label(), ep.api);
+        Assert.assertEquals("g1.librelois.fr", ep.dns);
+        Assert.assertNull(ep.ipv4);
+        Assert.assertNotNull(ep.port);
+        Assert.assertEquals(443, ep.port.intValue());
+        Assert.assertNull(ep.id);
+        Assert.assertEquals("gva-sub", ep.path);
+
+        // Any other api
         ep = Endpoints.parse("GCHANGE_API data.gchange.fr 443").orElse(null);
         Assert.assertNotNull(ep);
         Assert.assertEquals(ep.api, "GCHANGE_API");
-- 
GitLab