changeset 2728:e80359f05ea7

Change system-network plugin to use new /system-network POST API The system-network plugin needs to be changed to access the new web gateway API. Specifically, the old web-gateway /system-network POST API accepted a single network-info object, while all other POST APIs accept an array. For consistency, the agent plugin now sends an array of length one, instead of a single object. Reviewed-by: jkang, sgehwolf Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2017-July/024325.html
author Simon Tooke <stooke@redhat.com>
date Mon, 31 Jul 2017 10:42:26 -0400
parents 499059dee241
children 9cbc2bcf39c2
files plugins/host-network/agent/src/main/java/com/redhat/thermostat/host/network/internal/NetworkInfoListDAOImpl.java plugins/host-network/agent/src/main/java/com/redhat/thermostat/host/network/model/NetworkInfoListTypeAdapter.java plugins/host-network/agent/src/test/java/com/redhat/thermostat/host/network/internal/NetworkInfoListDAOTest.java plugins/host-network/agent/src/test/java/com/redhat/thermostat/host/network/model/NetworkInfoListTypeAdapterTest.java
diffstat 4 files changed, 28 insertions(+), 95 deletions(-) [+]
line wrap: on
line diff
--- a/plugins/host-network/agent/src/main/java/com/redhat/thermostat/host/network/internal/NetworkInfoListDAOImpl.java	Tue Jul 25 08:25:21 2017 -0400
+++ b/plugins/host-network/agent/src/main/java/com/redhat/thermostat/host/network/internal/NetworkInfoListDAOImpl.java	Mon Jul 31 10:42:26 2017 -0400
@@ -38,6 +38,8 @@
 
 import java.io.IOException;
 import java.net.URI;
+import java.util.Arrays;
+import java.util.List;
 import java.util.logging.Logger;
 
 import org.apache.felix.scr.annotations.Activate;
@@ -92,7 +94,7 @@
 
     @Override
     protected String toJsonString(NetworkInfoList obj) throws IOException {
-        return jsonHelper.toJson(obj);
+        return jsonHelper.toJson(Arrays.asList(obj));
     }
 
     @Override
@@ -137,7 +139,7 @@
             this.typeAdapter = typeAdapter;
         }
 
-        String toJson(NetworkInfoList infos) throws IOException {
+        String toJson(List<NetworkInfoList> infos) throws IOException {
             return typeAdapter.toJson(infos);
         }
     }
--- a/plugins/host-network/agent/src/main/java/com/redhat/thermostat/host/network/model/NetworkInfoListTypeAdapter.java	Tue Jul 25 08:25:21 2017 -0400
+++ b/plugins/host-network/agent/src/main/java/com/redhat/thermostat/host/network/model/NetworkInfoListTypeAdapter.java	Mon Jul 31 10:42:26 2017 -0400
@@ -43,18 +43,16 @@
 import com.google.gson.stream.JsonReader;
 import com.google.gson.stream.JsonWriter;
 
-public class NetworkInfoListTypeAdapter extends TypeAdapter<NetworkInfoList> {
+public class NetworkInfoListTypeAdapter extends TypeAdapter<List<NetworkInfoList>> {
 
     private static final String AGENT_ID = "agentId";
     private static final String TIMESTAMP = "timeStamp";
     private static final String INTERFACE_LIST = "interfaces";
-    private static final String RESPONSE_ROOT = "response";
-    private static final String SERVER_TIME = "time";
     private static final String TYPE_LONG = "$numberLong";
 
     private final NetworkInterfaceInfoTypeAdapter infoTypeAdapter = new NetworkInterfaceInfoTypeAdapter();
 
-    public void write(final JsonWriter out, final NetworkInfoList value) throws IOException {
+    private void write(final JsonWriter out, final NetworkInfoList value) throws IOException {
 
         out.beginObject();
 
@@ -64,72 +62,12 @@
         out.name(TIMESTAMP);
         writeLong(out, value.getTimeStamp());
 
-        out.name("interfaces");
+        out.name(INTERFACE_LIST);
         infoTypeAdapter.write(out, value.getList());
 
         out.endObject();
     }
 
-    @Override
-    public NetworkInfoList read(final JsonReader in) throws IOException {
-        NetworkInfoList infos = null;
-
-        try {
-            // Parse root object
-            in.beginObject();
-            while (in.hasNext()) {
-                String name = in.nextName();
-                switch (name) {
-                    case RESPONSE_ROOT:
-                        infos = readResponse(in);
-                        break;
-                    case SERVER_TIME:
-                        in.nextString();
-                        break;
-                    default:
-                        throw new IOException("Unexpected JSON name in gateway response: '" + name + "'");
-                }
-            }
-            in.endObject();
-        } catch (IllegalStateException e) {
-            throw new IOException("Reading JSON response from web gateway failed", e);
-        }
-
-        return infos;
-    }
-
-    private NetworkInfoList readResponse(final JsonReader in) throws IOException {
-
-        String agentId = null;
-        long timeStamp = 0;
-        List<NetworkInterfaceInfo> infos = null;
-
-        // Begin parsing a NetworkInterfaceInfo record
-        in.beginObject();
-
-        while (in.hasNext()) {
-            String name = in.nextName();
-            switch (name) {
-                case AGENT_ID:
-                    agentId = in.nextString();
-                    break;
-                case TIMESTAMP:
-                    timeStamp = readLong(in);
-                    break;
-                case INTERFACE_LIST: {
-                    infos = infoTypeAdapter.readList(in);
-                }
-                break;
-                default:
-                    throw new IOException("Unexpected JSON name in record: '" + name + "'");
-            }
-        }
-
-        in.endObject();
-
-        return new NetworkInfoList(agentId, timeStamp, infos);
-    }
-
     private void writeLong(final JsonWriter out, final long input) throws IOException {
         // Write MongoDB representation of a Long
         out.beginObject();
@@ -154,4 +92,19 @@
         }
     }
 
+    @Override
+    public void write(JsonWriter out, List<NetworkInfoList> list) throws IOException {
+        out.beginArray();
+
+        for (NetworkInfoList info : list) {
+            write(out, info);
+        }
+
+        out.endArray();
+    }
+
+    @Override
+    public List<NetworkInfoList> read(JsonReader jsonReader) throws IOException {
+        throw new UnsupportedOperationException();
+    }
 }
--- a/plugins/host-network/agent/src/test/java/com/redhat/thermostat/host/network/internal/NetworkInfoListDAOTest.java	Tue Jul 25 08:25:21 2017 -0400
+++ b/plugins/host-network/agent/src/test/java/com/redhat/thermostat/host/network/internal/NetworkInfoListDAOTest.java	Mon Jul 31 10:42:26 2017 -0400
@@ -44,6 +44,7 @@
 
 import java.net.URI;
 import java.util.ArrayList;
+import java.util.List;
 
 import org.eclipse.jetty.http.HttpMethod;
 import org.junit.Before;
@@ -84,7 +85,7 @@
         info.setIp6Addr(IPV6_ADDR);
         
         jsonHelper = mock(JsonHelper.class);
-        when(jsonHelper.toJson(any(NetworkInfoList.class))).thenReturn(SOME_JSON);
+        when(jsonHelper.toJson(any(List.class))).thenReturn(SOME_JSON);
 
         cfiSource = mock(ConfigurationInfoSource.class);
         configCreator = mock(ConfigurationCreator.class);
--- a/plugins/host-network/agent/src/test/java/com/redhat/thermostat/host/network/model/NetworkInfoListTypeAdapterTest.java	Tue Jul 25 08:25:21 2017 -0400
+++ b/plugins/host-network/agent/src/test/java/com/redhat/thermostat/host/network/model/NetworkInfoListTypeAdapterTest.java	Mon Jul 31 10:42:26 2017 -0400
@@ -37,9 +37,9 @@
 package com.redhat.thermostat.host.network.model;
 
 import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNull;
 
 import java.util.ArrayList;
+import java.util.Arrays;
 
 import org.junit.Test;
 
@@ -51,41 +51,18 @@
     @Test
     public void testWrite() throws Exception {
         NetworkInfoListTypeAdapter adapter = new NetworkInfoListTypeAdapter();
-        final String expected = "{\"agentId\":\"some-agent-id\",\"timeStamp\":{\"$numberLong\":\"333\"},\"interfaces\":[{\"interfaceName\":\"lo\",\"ip4Addr\":\"127.0.0.1\","
+        final String expected = "[{\"agentId\":\"some-agent-id\",\"timeStamp\":{\"$numberLong\":\"333\"},\"interfaces\":[{\"interfaceName\":\"lo\",\"ip4Addr\":\"127.0.0.1\","
                 + "\"ip6Addr\":\"0:0:0:0:0:0:0:1%lo\"},{\"interfaceName\":\"if1\","
-                + "\"ip4Addr\":\"1.2.3.4\",\"ip6Addr\":\"1:2:3:4:5:6:7:8%if1\"}]}";
+                + "\"ip4Addr\":\"1.2.3.4\",\"ip6Addr\":\"1:2:3:4:5:6:7:8%if1\"}]}]";
 
         NetworkInterfaceInfo first = createInfo("lo", "127.0.0.1", "0:0:0:0:0:0:0:1%lo");
         NetworkInterfaceInfo second = createInfo("if1", "1.2.3.4", "1:2:3:4:5:6:7:8%if1");
         NetworkInfoList infos = createNetworkInfoList(first, second);
 
-        String json = adapter.toJson(infos);
+        String json = adapter.toJson(Arrays.asList(infos));
         assertEquals(expected, json);
     }
 
-    @Test
-    public void testRead() throws Exception {
-        NetworkInfoListTypeAdapter adapter = new NetworkInfoListTypeAdapter();
-        final String json = "{\"response\" : {\"agentId\":\"some-agent-id\",\"timeStamp\":{\"$numberLong\":\"333\"},\"interfaces\":[{\"interfaceName\" : \"lo\", "
-                + "\"ip4Addr\" : \"127.0.0.1\", \"ip6Addr\" : \"0:0:0:0:0:0:0:1%lo\"}, "
-                + "{\"interfaceName\" : \"if1\", "
-                + "\"ip4Addr\" : \"1.2.3.4\", \"ip6Addr\" : null}]}, "
-                + "\"time\" : \"500000000\"}";
-
-        NetworkInfoList infos = adapter.fromJson(json);
-        assertEquals(2, infos.size());
-
-        NetworkInterfaceInfo first = infos.get(0);
-        assertEquals("lo", first.getInterfaceName());
-        assertEquals("127.0.0.1", first.getIp4Addr());
-        assertEquals("0:0:0:0:0:0:0:1%lo", first.getIp6Addr());
-
-        NetworkInterfaceInfo second = infos.get(1);
-        assertEquals("if1", second.getInterfaceName());
-        assertEquals("1.2.3.4", second.getIp4Addr());
-        assertNull(second.getIp6Addr());
-    }
-
     private NetworkInfoList createNetworkInfoList(String iFace, String ip4Addr, String ip6Addr) {
         NetworkInterfaceInfo info = new NetworkInterfaceInfo(iFace);
         info.setIp4Addr(ip4Addr);