changeset 2636:5e95cc9e0a21 web-gateway

Add TypeAdapter for NetworkInterfaceInfo, DAO cannot be ported yet due to missing replace functionality in gateway.
author Elliott Baron <ebaron@redhat.com>
date Mon, 10 Apr 2017 13:30:48 -0400
parents b849b3bd05c3
children 32ede143c625
files storage/core/src/main/java/com/redhat/thermostat/storage/internal/dao/NetworkInterfaceInfoDAOImpl.java storage/core/src/main/java/com/redhat/thermostat/storage/internal/dao/NetworkInterfaceInfoTypeAdapter.java storage/core/src/test/java/com/redhat/thermostat/storage/internal/dao/NetworkInterfaceInfoDAOTest.java storage/core/src/test/java/com/redhat/thermostat/storage/internal/dao/NetworkInterfaceInfoTypeAdapterTest.java
diffstat 4 files changed, 211 insertions(+), 23 deletions(-) [+]
line wrap: on
line diff
--- a/storage/core/src/main/java/com/redhat/thermostat/storage/internal/dao/NetworkInterfaceInfoDAOImpl.java	Fri Apr 07 18:18:32 2017 -0400
+++ b/storage/core/src/main/java/com/redhat/thermostat/storage/internal/dao/NetworkInterfaceInfoDAOImpl.java	Mon Apr 10 13:30:48 2017 -0400
@@ -36,22 +36,23 @@
 
 package com.redhat.thermostat.storage.internal.dao;
 
+import java.io.IOException;
 import java.util.Collections;
 import java.util.List;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeoutException;
 import java.util.logging.Logger;
 
+import org.eclipse.jetty.client.HttpClient;
+import org.eclipse.jetty.client.api.ContentResponse;
+import org.eclipse.jetty.client.api.Request;
+import org.eclipse.jetty.http.HttpStatus;
+
 import com.redhat.thermostat.common.utils.LoggingUtils;
-import com.redhat.thermostat.storage.core.Category;
-import com.redhat.thermostat.storage.core.CategoryAdapter;
 import com.redhat.thermostat.storage.core.HostRef;
 import com.redhat.thermostat.storage.core.Key;
-import com.redhat.thermostat.storage.core.PreparedStatement;
-import com.redhat.thermostat.storage.core.Storage;
-import com.redhat.thermostat.storage.dao.AbstractDaoQuery;
-import com.redhat.thermostat.storage.dao.AbstractDaoStatement;
 import com.redhat.thermostat.storage.dao.BaseCountable;
 import com.redhat.thermostat.storage.dao.NetworkInterfaceInfoDAO;
-import com.redhat.thermostat.storage.model.AggregateCount;
 import com.redhat.thermostat.storage.model.NetworkInterfaceInfo;
 
 public class NetworkInterfaceInfoDAOImpl extends BaseCountable implements NetworkInterfaceInfoDAO {
@@ -77,15 +78,22 @@
     static final String AGGREGATE_COUNT_ALL_NETWORK_INTERFACES = "QUERY-COUNT " +
             networkInfoCategory.getName();
 
-
-    private final Category<AggregateCount> aggregateCategory;
+    private static final String GATEWAY_URL = "http://localhost:26000/api/v100"; // TODO configurable
+    private static final String GATEWAY_PATH = "/agent-config/systems/*/agents/";
+    private static final String CONTENT_TYPE = "application/json";
+    
+    private final HttpClient httpClient;
+    private final NetworkInterfaceInfoTypeAdapter typeAdapter;
 
-    public NetworkInterfaceInfoDAOImpl() {
-        // TODO storage.registerCategory(networkInfoCategory);
-        CategoryAdapter<NetworkInterfaceInfo, AggregateCount> adapter =
-                new CategoryAdapter<>(networkInfoCategory);
-        aggregateCategory = adapter.getAdapted(AggregateCount.class);
-        // TODO storage.registerCategory(aggregateCategory);
+    public NetworkInterfaceInfoDAOImpl() throws Exception {
+        this(new HttpClient(), new NetworkInterfaceInfoTypeAdapter());
+    }
+
+    NetworkInterfaceInfoDAOImpl(HttpClient httpClient, NetworkInterfaceInfoTypeAdapter typeAdapter) throws Exception {
+        this.httpClient = httpClient;
+        this.typeAdapter = typeAdapter;
+        
+        this.httpClient.start();
     }
 
     @Override
@@ -117,7 +125,7 @@
                         preparedStatement.setString(5, info.getInterfaceName());
                         return preparedStatement;
                     }
-                }); */ // TODO
+                }); */ // TODO Replace functionality not yet present in web gateway
     }
 
     @Override
@@ -130,5 +138,22 @@
     protected Logger getLogger() {
         return logger;
     }
+    
+    private void sendRequest(Request httpRequest)
+            throws InterruptedException, TimeoutException, ExecutionException, IOException {
+        ContentResponse resp = httpRequest.send();
+        int status = resp.getStatus();
+        if (status != HttpStatus.OK_200) {
+            throw new IOException("Gateway returned HTTP status " + String.valueOf(status) + " - " + resp.getReason());
+        }
+    }
+    
+    private String getURL(String agentId) {
+        StringBuilder builder = new StringBuilder();
+        builder.append(GATEWAY_URL);
+        builder.append(GATEWAY_PATH);
+        builder.append(agentId);
+        return builder.toString();
+    }
 }
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/storage/core/src/main/java/com/redhat/thermostat/storage/internal/dao/NetworkInterfaceInfoTypeAdapter.java	Mon Apr 10 13:30:48 2017 -0400
@@ -0,0 +1,87 @@
+/*
+ * Copyright 2012-2017 Red Hat, Inc.
+ *
+ * This file is part of Thermostat.
+ *
+ * Thermostat is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published
+ * by the Free Software Foundation; either version 2, or (at your
+ * option) any later version.
+ *
+ * Thermostat is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Thermostat; see the file COPYING.  If not see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * Linking this code with other modules is making a combined work
+ * based on this code.  Thus, the terms and conditions of the GNU
+ * General Public License cover the whole combination.
+ *
+ * As a special exception, the copyright holders of this code give
+ * you permission to link this code with independent modules to
+ * produce an executable, regardless of the license terms of these
+ * independent modules, and to copy and distribute the resulting
+ * executable under terms of your choice, provided that you also
+ * meet, for each linked independent module, the terms and conditions
+ * of the license of that module.  An independent module is a module
+ * which is not derived from or based on this code.  If you modify
+ * this code, you may extend this exception to your version of the
+ * library, but you are not obligated to do so.  If you do not wish
+ * to do so, delete this exception statement from your version.
+ */
+
+package com.redhat.thermostat.storage.internal.dao;
+
+import java.io.IOException;
+import java.util.List;
+
+import com.google.gson.TypeAdapter;
+import com.google.gson.stream.JsonReader;
+import com.google.gson.stream.JsonWriter;
+import com.redhat.thermostat.storage.model.NetworkInterfaceInfo;
+
+public class NetworkInterfaceInfoTypeAdapter extends TypeAdapter<List<NetworkInterfaceInfo>> {
+    
+    private static final String AGENT_ID = "agentId";
+    private static final String INTERFACE_NAME = "interfaceName";
+    private static final String IP4_ADDR = "ip4Addr";
+    private static final String IP6_ADDR = "ip6Addr";
+    
+    @Override
+    public void write(JsonWriter out, List<NetworkInterfaceInfo> value) throws IOException {
+        // Request is an array of NetworkInterfaceInfo objects
+        out.beginArray();
+        
+        for (NetworkInterfaceInfo info : value) {
+            writeNetworkInterfaceInfo(out, info);
+        }
+        
+        out.endArray();
+    }
+
+    private void writeNetworkInterfaceInfo(JsonWriter out, NetworkInterfaceInfo info) throws IOException {
+        out.beginObject();
+        
+        // Write each field of NetworkInterfaceInfo as part of a JSON object
+        out.name(AGENT_ID);
+        out.value(info.getAgentId());
+        out.name(INTERFACE_NAME);
+        out.value(info.getInterfaceName());
+        out.name(IP4_ADDR);
+        out.value(info.getIp4Addr());
+        out.name(IP6_ADDR);
+        out.value(info.getIp6Addr());
+        
+        out.endObject();
+    }
+
+    @Override
+    public List<NetworkInterfaceInfo> read(JsonReader in) throws IOException {
+        throw new UnsupportedOperationException();
+    }
+    
+}
--- a/storage/core/src/test/java/com/redhat/thermostat/storage/internal/dao/NetworkInterfaceInfoDAOTest.java	Fri Apr 07 18:18:32 2017 -0400
+++ b/storage/core/src/test/java/com/redhat/thermostat/storage/internal/dao/NetworkInterfaceInfoDAOTest.java	Mon Apr 10 13:30:48 2017 -0400
@@ -53,12 +53,10 @@
 import org.mockito.ArgumentCaptor;
 
 import com.redhat.thermostat.storage.core.Cursor;
-import com.redhat.thermostat.storage.core.DescriptorParsingException;
 import com.redhat.thermostat.storage.core.HostRef;
 import com.redhat.thermostat.storage.core.Key;
 import com.redhat.thermostat.storage.core.PreparedStatement;
 import com.redhat.thermostat.storage.core.StatementDescriptor;
-import com.redhat.thermostat.storage.core.StatementExecutionException;
 import com.redhat.thermostat.storage.core.Storage;
 import com.redhat.thermostat.storage.dao.NetworkInterfaceInfoDAO;
 import com.redhat.thermostat.storage.model.AggregateCount;
@@ -100,7 +98,7 @@
     }
 
     @Test
-    public void testGetNetworkInterfaces() throws DescriptorParsingException, StatementExecutionException {
+    public void testGetNetworkInterfaces() throws Exception {
 
         NetworkInterfaceInfo niInfo = new NetworkInterfaceInfo("foo-agent", INTERFACE_NAME);
         niInfo.setIp4Addr(IPV4_ADDR);
@@ -144,8 +142,7 @@
 
     @SuppressWarnings("unchecked")
     @Test
-    public void testPutNetworkInterfaceInfo()
-            throws DescriptorParsingException, StatementExecutionException {
+    public void testPutNetworkInterfaceInfo() throws Exception {
         String agentId = "fooAgent";
         Storage storage = mock(Storage.class);
         PreparedStatement<NetworkInterfaceInfo> replace = mock(PreparedStatement.class);
@@ -177,8 +174,7 @@
     }
 
     @Test
-    public void testGetCount()
-            throws DescriptorParsingException, StatementExecutionException {
+    public void testGetCount() throws Exception {
         AggregateCount count = new AggregateCount();
         count.setCount(2);
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/storage/core/src/test/java/com/redhat/thermostat/storage/internal/dao/NetworkInterfaceInfoTypeAdapterTest.java	Mon Apr 10 13:30:48 2017 -0400
@@ -0,0 +1,80 @@
+/*
+ * Copyright 2012-2017 Red Hat, Inc.
+ *
+ * This file is part of Thermostat.
+ *
+ * Thermostat is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published
+ * by the Free Software Foundation; either version 2, or (at your
+ * option) any later version.
+ *
+ * Thermostat is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Thermostat; see the file COPYING.  If not see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * Linking this code with other modules is making a combined work
+ * based on this code.  Thus, the terms and conditions of the GNU
+ * General Public License cover the whole combination.
+ *
+ * As a special exception, the copyright holders of this code give
+ * you permission to link this code with independent modules to
+ * produce an executable, regardless of the license terms of these
+ * independent modules, and to copy and distribute the resulting
+ * executable under terms of your choice, provided that you also
+ * meet, for each linked independent module, the terms and conditions
+ * of the license of that module.  An independent module is a module
+ * which is not derived from or based on this code.  If you modify
+ * this code, you may extend this exception to your version of the
+ * library, but you are not obligated to do so.  If you do not wish
+ * to do so, delete this exception statement from your version.
+ */
+
+package com.redhat.thermostat.storage.internal.dao;
+
+import static org.junit.Assert.assertEquals;
+
+import java.util.Arrays;
+import java.util.List;
+
+import org.junit.Before;
+import org.junit.Test;
+
+import com.redhat.thermostat.storage.model.NetworkInterfaceInfo;
+
+public class NetworkInterfaceInfoTypeAdapterTest {
+    
+    private NetworkInterfaceInfoTypeAdapter adapter;
+    
+    @Before
+    public void setup() {
+        adapter = new NetworkInterfaceInfoTypeAdapter();
+    }
+
+    @Test
+    public void testWrite() throws Exception {
+        final String expected = "[{\"agentId\":\"agent1\",\"interfaceName\":\"lo\",\"ip4Addr\":\"127.0.0.1\","
+                + "\"ip6Addr\":\"0:0:0:0:0:0:0:1%lo\"},{\"agentId\":\"agent2\",\"interfaceName\":\"if1\","
+                + "\"ip4Addr\":\"1.2.3.4\",\"ip6Addr\":\"1:2:3:4:5:6:7:8%if1\"}]";
+        
+        NetworkInterfaceInfo first = createNetworkInterfaceInfo("agent1", "lo", "127.0.0.1", "0:0:0:0:0:0:0:1%lo");
+        NetworkInterfaceInfo second = createNetworkInterfaceInfo("agent2", "if1", "1.2.3.4", "1:2:3:4:5:6:7:8%if1");
+        List<NetworkInterfaceInfo> infos = Arrays.asList(first, second);
+        
+        String json = adapter.toJson(infos);
+        assertEquals(expected, json);
+    }
+    
+    private NetworkInterfaceInfo createNetworkInterfaceInfo(String agentId, String iFace, String ip4Addr, 
+            String ip6Addr) {
+        NetworkInterfaceInfo info = new NetworkInterfaceInfo(agentId, iFace);
+        info.setIp4Addr(ip4Addr);
+        info.setIp6Addr(ip6Addr);
+        return info;
+    }
+
+}