changeset 2716:cc297a1bf67f

Fix /jvms returning empty response Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2017-July/023975.html Reviewed-by: jerboaa
author Joshua Matsuoka <jmatsuok@redhat.com>
date Wed, 05 Jul 2017 14:29:14 -0400
parents 752a90af8d85
children 08b2cadf2d42
files plugins/jvm-overview/agent/pom.xml plugins/jvm-overview/agent/src/main/java/com/redhat/thermostat/jvm/overview/agent/internal/model/VmInfoDAOImpl.java plugins/jvm-overview/agent/src/main/java/com/redhat/thermostat/jvm/overview/agent/internal/model/VmInfoTypeAdapter.java plugins/jvm-overview/agent/src/test/java/com/redhat/thermostat/jvm/overview/agent/internal/model/VmInfoDAOImplTest.java plugins/jvm-overview/agent/src/test/java/com/redhat/thermostat/jvm/overview/agent/internal/model/VmInfoTypeAdapterTest.java plugins/jvm-overview/distribution/assemblies/plugin-assembly.xml plugins/jvm-overview/distribution/configFiles/gateway.properties
diffstat 7 files changed, 136 insertions(+), 67 deletions(-) [+]
line wrap: on
line diff
--- a/plugins/jvm-overview/agent/pom.xml	Mon Jun 26 11:22:04 2017 +0200
+++ b/plugins/jvm-overview/agent/pom.xml	Wed Jul 05 14:29:14 2017 -0400
@@ -153,6 +153,11 @@
     </dependency>
     <dependency>
       <groupId>com.redhat.thermostat</groupId>
+      <artifactId>thermostat-common-plugin</artifactId>
+      <version>${project.version}</version>
+    </dependency>
+    <dependency>
+      <groupId>com.redhat.thermostat</groupId>
       <artifactId>thermostat-common-portability</artifactId>
       <version>${project.version}</version>
     </dependency>
--- a/plugins/jvm-overview/agent/src/main/java/com/redhat/thermostat/jvm/overview/agent/internal/model/VmInfoDAOImpl.java	Mon Jun 26 11:22:04 2017 +0200
+++ b/plugins/jvm-overview/agent/src/main/java/com/redhat/thermostat/jvm/overview/agent/internal/model/VmInfoDAOImpl.java	Wed Jul 05 14:29:14 2017 -0400
@@ -44,12 +44,16 @@
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
+import org.apache.felix.scr.annotations.Activate;
 import org.apache.felix.scr.annotations.Component;
 import org.apache.felix.scr.annotations.Reference;
 import org.apache.felix.scr.annotations.Service;
 
 import com.redhat.thermostat.agent.http.HttpRequestService;
 import com.redhat.thermostat.agent.http.HttpRequestService.RequestFailedException;
+import com.redhat.thermostat.common.config.experimental.ConfigurationInfoSource;
+import com.redhat.thermostat.common.plugin.PluginConfiguration;
+import com.redhat.thermostat.common.plugin.SystemID;
 import com.redhat.thermostat.common.utils.LoggingUtils;
 import com.redhat.thermostat.jvm.overview.agent.internal.model.VmInfoTypeAdapter.VmInfoUpdateTypeAdapter;
 import com.redhat.thermostat.jvm.overview.agent.model.VmId;
@@ -59,24 +63,42 @@
 @Component
 @Service(VmInfoDAO.class)
 public class VmInfoDAOImpl implements VmInfoDAO {
-    
+
+    private static final String PLUGIN_ID = "jvm-overview";
+    private static final String SYSTEM_PATH = "systems/";
+    private static final String VM_PATH = "/jvms/";
+    private static final String UPDATE_PREFIX = "update/";
+
     private final Logger logger = LoggingUtils.getLogger(VmInfoDAOImpl.class);
     
-    private static final String GATEWAY_URL = "http://localhost:26000/api/v100"; // TODO configurable
-    private static final String GATEWAY_PATH = "/vm-info/systems/*/agents/";
-    private static final String GATEWAY_PATH_JVM_SUFFIX = "/jvms/";
-    
     private final JsonHelper jsonHelper;
-    
+    private final ConfigurationCreator configCreator;
+
+    private String gatewayURL;
+
+    @Reference
+    private ConfigurationInfoSource configInfoSource;
+
     @Reference
     private HttpRequestService httpRequestService;
 
+    @Reference
+    private SystemID systemID;
+
     public VmInfoDAOImpl() throws Exception {
-        this(new JsonHelper(new VmInfoTypeAdapter(), new VmInfoUpdateTypeAdapter()));
+        this(new JsonHelper(new VmInfoTypeAdapter(), new VmInfoUpdateTypeAdapter()), new ConfigurationCreator(), null);
     }
 
-    VmInfoDAOImpl(JsonHelper jsonHelper) throws Exception {
+    VmInfoDAOImpl(JsonHelper jsonHelper, ConfigurationCreator creator, ConfigurationInfoSource source) throws Exception {
         this.jsonHelper = jsonHelper;
+        this.configCreator = creator;
+        this.configInfoSource = source;
+    }
+
+    @Activate
+    public void activate() throws Exception {
+        PluginConfiguration config = configCreator.create(configInfoSource);
+        this.gatewayURL = config.getGatewayURL();
     }
 
     @Override
@@ -91,16 +113,14 @@
 
     @Override
     public void putVmInfo(final VmInfo info) {
-        // FIXME: Re-enable once /jvms endpoint is being used properly and re-enable ignored
-        //        unit-tests.
-//        try {
-//            // Encode as JSON and send as POST request
-//            String json = jsonHelper.toJson(Arrays.asList(info));
-//            String url = getAddURL(info.getAgentId());
-//            httpRequestService.sendHttpRequest(json, url, HttpRequestService.POST);
-//        } catch (IOException | RequestFailedException e) {
-//           logger.log(Level.WARNING, "Failed to send JVM information to web gateway", e);
-//        }
+        try {
+            // Encode as JSON and send as POST request
+            String json = jsonHelper.toJson(Arrays.asList(info));
+            String url = getAddURL();
+            httpRequestService.sendHttpRequest(json, url, HttpRequestService.POST);
+        } catch (IOException | RequestFailedException e) {
+           logger.log(Level.WARNING, "Failed to send JVM information to web gateway", e);
+        }
     }
 
     @Override
@@ -109,29 +129,33 @@
             // Encode as JSON and send as PUT request
             VmInfoUpdate update = new VmInfoUpdate(timestamp);
             String json = jsonHelper.toJson(update);
-            String url = getUpdateURL(agentId, vmId);
+            String url = getUpdateURL(vmId);
             httpRequestService.sendHttpRequest(json, url, HttpRequestService.PUT);
         } catch (IOException | RequestFailedException e) {
            logger.log(Level.WARNING, "Failed to send JVM information update to web gateway", e);
         }
     }
     
-    private String getAddURL(String agentId) {
-        StringBuilder builder = buildURL(agentId);
+    private String getAddURL() {
+        StringBuilder builder = buildURL();
+        builder.append(SYSTEM_PATH);
+        builder.append(systemID.getSystemID());
         return builder.toString();
     }
 
-    private StringBuilder buildURL(String agentId) {
+    private StringBuilder buildURL() {
         StringBuilder builder = new StringBuilder();
-        builder.append(GATEWAY_URL);
-        builder.append(GATEWAY_PATH);
-        builder.append(agentId);
+        builder.append(gatewayURL);
         return builder;
     }
     
-    private String getUpdateURL(String agentId, String vmId) {
-        StringBuilder builder = buildURL(agentId);
-        builder.append(GATEWAY_PATH_JVM_SUFFIX);
+    private String getUpdateURL(String vmId) {
+        StringBuilder builder = new StringBuilder();
+        builder.append(buildURL());
+        builder.append(UPDATE_PREFIX);
+        builder.append(SYSTEM_PATH);
+        builder.append(systemID.getSystemID());
+        builder.append(VM_PATH);
         builder.append(vmId);
         return builder.toString();
     }
@@ -144,6 +168,14 @@
         this.httpRequestService = null;
         logger.log(Level.INFO, "Unbound HTTP service. Further attempts to store data will fail until bound again.");
     }
+
+    protected void bindSystemId(SystemID id) {
+        this.systemID = id;
+    }
+
+    protected void unbindSystemId(SystemID id) {
+        this.systemID = null;
+    }
     
     static class VmInfoUpdate {
         
@@ -178,5 +210,14 @@
         }
         
     }
+
+    // For Testing purposes
+    static class ConfigurationCreator {
+
+        PluginConfiguration create(ConfigurationInfoSource source) {
+            return new PluginConfiguration(source, PLUGIN_ID);
+        }
+
+    }
 }
 
--- a/plugins/jvm-overview/agent/src/main/java/com/redhat/thermostat/jvm/overview/agent/internal/model/VmInfoTypeAdapter.java	Mon Jun 26 11:22:04 2017 +0200
+++ b/plugins/jvm-overview/agent/src/main/java/com/redhat/thermostat/jvm/overview/agent/internal/model/VmInfoTypeAdapter.java	Wed Jul 05 14:29:14 2017 -0400
@@ -51,27 +51,27 @@
 public class VmInfoTypeAdapter extends TypeAdapter<List<VmInfo>> {
     
     private static final String AGENT_ID = "agentId";
-    private static final String VM_ID = "vmId";
-    private static final String VM_PID = "vmPid";
+    private static final String VM_ID = "jvmId";
+    private static final String VM_PID = "jvmPid";
     private static final String JAVA_VERSION = "javaVersion";
     private static final String JAVA_HOME = "javaHome";
     private static final String MAIN_CLASS = "mainClass";
     private static final String JAVA_COMMAND_LINE = "javaCommandLine";
-    private static final String VM_ARGUMENTS = "vmArguments";
-    private static final String VM_NAME = "vmName";
-    private static final String VM_INFO = "vmInfo";
-    private static final String VM_VERSION = "vmVersion";
-    private static final String PROPERTIES_AS_ARRAY = "propertiesAsArray";
-    private static final String ENVIRONMENT_AS_ARRAY = "environmentAsArray";
+    private static final String VM_ARGUMENTS = "jvmArguments";
+    private static final String VM_NAME = "jvmName";
+    private static final String VM_INFO = "jvmInfo";
+    private static final String VM_VERSION = "jvmVersion";
+    private static final String PROPERTIES_AS_ARRAY = "properties";
+    private static final String ENVIRONMENT_AS_ARRAY = "environment";
     private static final String LOADED_NATIVE_LIBRARIES = "loadedNativeLibraries";
-    private static final String START_TIME_STAMP = "startTimeStamp";
-    private static final String STOP_TIME_STAMP = "stopTimeStamp";
+    private static final String START_TIME_STAMP = "startTime";
+    private static final String STOP_TIME_STAMP = "stopTime";
     private static final String UID = "uid";
     private static final String USERNAME = "username";
     private static final String TYPE_LONG = "$numberLong";
     private static final String KEY = "key";
     private static final String VALUE = "value";
-    
+
     @Override
     public void write(JsonWriter out, List<VmInfo> value) throws IOException {
         // Request is an array of VmInfo objects
@@ -86,7 +86,7 @@
 
     private void writeVmInfo(JsonWriter out, VmInfo info) throws IOException {
         out.beginObject();
-        
+
         // Write each field of VmInfo as part of a JSON object
         out.name(AGENT_ID);
         out.value(info.getAgentId());
--- a/plugins/jvm-overview/agent/src/test/java/com/redhat/thermostat/jvm/overview/agent/internal/model/VmInfoDAOImplTest.java	Mon Jun 26 11:22:04 2017 +0200
+++ b/plugins/jvm-overview/agent/src/test/java/com/redhat/thermostat/jvm/overview/agent/internal/model/VmInfoDAOImplTest.java	Wed Jul 05 14:29:14 2017 -0400
@@ -54,20 +54,29 @@
 import org.mockito.ArgumentCaptor;
 
 import com.redhat.thermostat.agent.http.HttpRequestService;
+import com.redhat.thermostat.common.config.experimental.ConfigurationInfoSource;
+import com.redhat.thermostat.common.plugin.PluginConfiguration;
+import com.redhat.thermostat.common.plugin.SystemID;
+import com.redhat.thermostat.jvm.overview.agent.internal.model.VmInfoDAOImpl.ConfigurationCreator;
 import com.redhat.thermostat.jvm.overview.agent.internal.model.VmInfoDAOImpl.JsonHelper;
 import com.redhat.thermostat.jvm.overview.agent.internal.model.VmInfoDAOImpl.VmInfoUpdate;
 import com.redhat.thermostat.jvm.overview.agent.model.VmInfo;
 
 public class VmInfoDAOImplTest {
 
-    private static final String URL = "http://localhost:26000/api/v100/vm-info/systems/*/agents/foo-agent";
-    private static final String UPDATE_URL = URL + "/jvms/vmId";
+    private static final String GATEWAY_URL = "http://localhost:30000/jvms/0.0.1/";
+    private static final String POST_URL = GATEWAY_URL + "systems/foo";
+    private static final String UPDATE_URL = GATEWAY_URL + "update/systems/foo/jvms/vmId";
     private static final String SOME_JSON = "{\"some\" : \"json\"}";
     private static final String SOME_OTHER_JSON = "{\"some\" : {\"other\" : \"json\"}}";
     
     private VmInfo info;
     private JsonHelper jsonHelper;
     private HttpRequestService httpRequestService;
+    private SystemID systemID;
+    private ConfigurationCreator creator;
+    private ConfigurationInfoSource source;
+    private PluginConfiguration config;
 
     @Before
     public void setUp() throws Exception {
@@ -91,30 +100,39 @@
         info = new VmInfo("foo-agent", vmId, vmPid, startTime, stopTime, jVersion, jHome,
                 mainClass, commandLine, vmName, vmInfo, vmVersion, vmArgs,
                 props, env, libs, uid, username);
-        
+
+        source = mock(ConfigurationInfoSource.class);
+        config = mock(PluginConfiguration.class);
+        when(config.getGatewayURL()).thenReturn(GATEWAY_URL);
+        creator = mock(ConfigurationCreator.class);
+        when(creator.create(source)).thenReturn(config);
+        systemID = mock(SystemID.class);
+        when(systemID.getSystemID()).thenReturn("foo");
+
         httpRequestService = mock(HttpRequestService.class);
         jsonHelper = mock(JsonHelper.class);
         when(jsonHelper.toJson(anyListOf(VmInfo.class))).thenReturn(SOME_JSON);
         when(jsonHelper.toJson(any(VmInfoUpdate.class))).thenReturn(SOME_OTHER_JSON);
     }
 
-    @Ignore("Re-enable when proper /jvms endpoint is being used")
     @Test
     public void testPutVmInfo() throws Exception {
-        VmInfoDAOImpl dao = new VmInfoDAOImpl(jsonHelper);
+        VmInfoDAOImpl dao = new VmInfoDAOImpl(jsonHelper, creator, source);
+        dao.bindSystemId(systemID);
         dao.bindHttpRequestService(httpRequestService);
+        dao.activate();
         dao.putVmInfo(info);
         
         verify(jsonHelper).toJson(eq(Arrays.asList(info)));
-        verify(httpRequestService).sendHttpRequest(SOME_JSON, URL, HttpRequestService.POST);
+        verify(httpRequestService).sendHttpRequest(SOME_JSON, POST_URL, HttpRequestService.POST);
     }
 
-    @Ignore("Re-enable when proper /jvms endpoint is being used")
     @Test
     public void testPutVmStoppedTime() throws Exception {
-        VmInfoDAOImpl dao = new VmInfoDAOImpl(jsonHelper);
+        VmInfoDAOImpl dao = new VmInfoDAOImpl(jsonHelper, creator, source);
+        dao.bindSystemId(systemID);
         dao.bindHttpRequestService(httpRequestService);
-        
+        dao.activate();
         dao.putVmStoppedTime("foo-agent", "vmId", 3L);
 
         ArgumentCaptor<VmInfoUpdate> updateCaptor = ArgumentCaptor.forClass(VmInfoUpdate.class);
--- a/plugins/jvm-overview/agent/src/test/java/com/redhat/thermostat/jvm/overview/agent/internal/model/VmInfoTypeAdapterTest.java	Mon Jun 26 11:22:04 2017 +0200
+++ b/plugins/jvm-overview/agent/src/test/java/com/redhat/thermostat/jvm/overview/agent/internal/model/VmInfoTypeAdapterTest.java	Wed Jul 05 14:29:14 2017 -0400
@@ -53,23 +53,21 @@
     @Test
     public void testWrite() throws Exception {
         VmInfoTypeAdapter adapter = new VmInfoTypeAdapter();
-        final String expected = "[{\"agentId\":\"agent1\",\"vmId\":\"vm1\",\"vmPid\":8000,"
-                + "\"startTimeStamp\":{\"$numberLong\":\"50000\"},\"stopTimeStamp\":"
-                + "{\"$numberLong\":\"-9223372036854775808\"},\"javaVersion\":\"1.8.0\","
-                + "\"javaHome\":\"/path/to/java\",\"mainClass\":\"myClass\",\"javaCommandLine\":\"java myClass\","
-                + "\"vmName\":\"myJVM\",\"vmArguments\":\"-Dhello\",\"vmInfo\":\"interesting\","
-                + "\"vmVersion\":\"1800\",\"propertiesAsArray\":[{\"key\":\"A\",\"value\":\"B\"},"
-                + "{\"key\":\"C\",\"value\":\"D\"}],\"environmentAsArray\":[{\"key\":\"E\",\"value\":\"F\"},"
-                + "{\"key\":\"G\",\"value\":\"H\"}],\"loadedNativeLibraries\":[],\"uid\":{\"$numberLong\":\"1234\"},"
-                + "\"username\":\"test\"},"
-                + "{\"agentId\":\"agent2\",\"vmId\":\"vm2\",\"vmPid\":9000,\"startTimeStamp\":"
-                + "{\"$numberLong\":\"100000\"},\"stopTimeStamp\":{\"$numberLong\":\"200000\"},"
-                + "\"javaVersion\":\"1.7.0\",\"javaHome\":\"/path/to/jre\",\"mainClass\":\"myOtherClass\","
-                + "\"javaCommandLine\":\"otherClass.sh\",\"vmName\":\"myOtherJVM\",\"vmArguments\":\"-Dworld\","
-                + "\"vmInfo\":\"info\",\"vmVersion\":\"1700\",\"propertiesAsArray\":[],\"environmentAsArray\":"
-                + "[{\"key\":\"A\",\"value\":\"B\"},{\"key\":\"C\",\"value\":\"D\"}],"
-                + "\"loadedNativeLibraries\":[\"libhello\",\"libworld\"],\"uid\":{\"$numberLong\":\"5678\"}"
-                + ",\"username\":\"user\"}]";
+        final String expected = "[{\"agentId\":\"agent1\",\"jvmId\":\"vm1\",\"jvmPid\":8000," +
+                "\"startTime\":{\"$numberLong\":\"50000\"},\"stopTime\":{\"$numberLong\":\"-9223372036854775808\"}," +
+                "\"javaVersion\":\"1.8.0\",\"javaHome\":\"/path/to/java\",\"mainClass\":\"myClass\"," +
+                "\"javaCommandLine\":\"java myClass\",\"jvmName\":\"myJVM\",\"jvmArguments\":\"-Dhello\",\"" +
+                "jvmInfo\":\"interesting\",\"jvmVersion\":\"1800\"," +
+                "\"properties\":[{\"key\":\"A\",\"value\":\"B\"},{\"key\":\"C\",\"value\":\"D\"}]," +
+                "\"environment\":[{\"key\":\"E\",\"value\":\"F\"},{\"key\":\"G\",\"value\":\"H\"}]," +
+                "\"loadedNativeLibraries\":[],\"uid\":{\"$numberLong\":\"1234\"},\"username\":\"test\"}," +
+                "{\"agentId\":\"agent2\",\"jvmId\":\"vm2\",\"jvmPid\":9000,\"startTime\":{\"$numberLong\":\"100000\"}," +
+                "\"stopTime\":{\"$numberLong\":\"200000\"},\"javaVersion\":\"1.7.0\",\"javaHome\":\"/path/to/jre\"," +
+                "\"mainClass\":\"myOtherClass\",\"javaCommandLine\":\"otherClass.sh\",\"jvmName\":\"myOtherJVM\"," +
+                "\"jvmArguments\":\"-Dworld\",\"jvmInfo\":\"info\",\"jvmVersion\":\"1700\",\"properties\":[]," +
+                "\"environment\":[{\"key\":\"A\",\"value\":\"B\"},{\"key\":\"C\",\"value\":\"D\"}]," +
+                "\"loadedNativeLibraries\":[\"libhello\",\"libworld\"],\"uid\":{\"$numberLong\":\"5678\"}," +
+                "\"username\":\"user\"}]";
         
         final Map<String, String> props = new HashMap<>();
         props.put("A", "B");
@@ -96,7 +94,7 @@
     @Test
     public void testUpdate() throws Exception {
         VmInfoUpdateTypeAdapter adapter = new VmInfoUpdateTypeAdapter();
-        final String expected = "{\"set\":{\"stopTimeStamp\":{\"$numberLong\":\"5000\"}}}";
+        final String expected = "{\"set\":{\"stopTime\":{\"$numberLong\":\"5000\"}}}";
         
         VmInfoUpdate update = new VmInfoUpdate(5000L);
         String json = adapter.toJson(update);
--- a/plugins/jvm-overview/distribution/assemblies/plugin-assembly.xml	Mon Jun 26 11:22:04 2017 +0200
+++ b/plugins/jvm-overview/distribution/assemblies/plugin-assembly.xml	Wed Jul 05 14:29:14 2017 -0400
@@ -64,6 +64,11 @@
       <outputDirectory>plugins/${thermostat.plugin}</outputDirectory>
       <filtered>true</filtered>
     </fileSet>
+    <fileSet>
+      <directory>configFiles</directory>
+      <outputDirectory>etc/plugins.d/${thermostat.plugin}</outputDirectory>
+      <filtered>true</filtered>
+    </fileSet>
   </fileSets>
 </assembly>
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/plugins/jvm-overview/distribution/configFiles/gateway.properties	Wed Jul 05 14:29:14 2017 -0400
@@ -0,0 +1,2 @@
+# URL to the jvm-memory microservice provided by the Thermostat web gateway
+gatewayURL=http://localhost:30000/jvms/0.0.1/
\ No newline at end of file