changeset 2685:3765e5b895dc

Use DS for host-overview. Reviewed-by: neugens, stooke Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2017-June/023525.html
author Severin Gehwolf <sgehwolf@redhat.com>
date Thu, 08 Jun 2017 19:45:19 +0200
parents 78729b17ec74
children 74a3254fdbdb
files plugins/host-overview/agent/pom.xml plugins/host-overview/agent/src/main/java/com/redhat/thermostat/host/overview/internal/Activator.java plugins/host-overview/agent/src/main/java/com/redhat/thermostat/host/overview/internal/common/PluginDAOBase.java plugins/host-overview/agent/src/main/java/com/redhat/thermostat/host/overview/internal/models/HostInfoDAOImpl.java plugins/host-overview/agent/src/test/java/com/redhat/thermostat/host/overview/internal/models/HostInfoDAOTest.java
diffstat 5 files changed, 58 insertions(+), 162 deletions(-) [+]
line wrap: on
line diff
--- a/plugins/host-overview/agent/pom.xml	Tue Jun 06 09:07:16 2017 -0400
+++ b/plugins/host-overview/agent/pom.xml	Thu Jun 08 19:45:19 2017 +0200
@@ -59,7 +59,6 @@
           <instructions>
             <Bundle-Vendor>Red Hat, Inc.</Bundle-Vendor>
             <Bundle-SymbolicName>com.redhat.thermostat.host.overview.agent</Bundle-SymbolicName>
-            <Bundle-Activator>com.redhat.thermostat.host.overview.internal.Activator</Bundle-Activator>
             <Export-Package>com.redhat.thermostat.host.overview.model</Export-Package>
             <Private-Package>
               com.redhat.thermostat.host.overview.internal,
--- a/plugins/host-overview/agent/src/main/java/com/redhat/thermostat/host/overview/internal/Activator.java	Tue Jun 06 09:07:16 2017 -0400
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,107 +0,0 @@
-/*
- * 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.host.overview.internal;
-
-import com.redhat.thermostat.common.config.experimental.ConfigurationInfoSource;
-import com.redhat.thermostat.common.utils.LoggingUtils;
-import com.redhat.thermostat.host.overview.internal.common.PluginConfiguration;
-import com.redhat.thermostat.host.overview.internal.models.HostInfoDAO;
-import com.redhat.thermostat.host.overview.internal.models.HostInfoDAOImpl;
-import org.osgi.framework.BundleActivator;
-import org.osgi.framework.BundleContext;
-import org.osgi.framework.ServiceReference;
-import org.osgi.framework.ServiceRegistration;
-import org.osgi.util.tracker.ServiceTracker;
-
-import java.util.logging.Level;
-import java.util.logging.Logger;
-
-public class Activator implements BundleActivator {
-
-    private static final Logger logger = LoggingUtils.getLogger(Activator.class);
-
-    private final HostInfoDAOCreator creator;
-    private ServiceTracker tracker;
-    private ServiceRegistration reg;
-
-    public Activator() {
-        this(new HostInfoDAOCreator());
-    }
-
-    Activator(HostInfoDAOCreator creator) {
-        this.creator = creator;
-    }
-
-    @Override
-    public void start(BundleContext context) throws Exception {
-        tracker = new ServiceTracker(context, ConfigurationInfoSource.class.getName(), null) {
-            @Override
-            public Object addingService(ServiceReference reference) {
-                ConfigurationInfoSource source = (ConfigurationInfoSource) super.addingService(reference);
-                try {
-                    HostInfoDAO dao = creator.createDAO(new PluginConfiguration(source, HostInfoDAOImpl.PLUGIN_ID));
-                    reg = context.registerService(HostInfoDAO.class.getName(), dao, null);
-                } catch (Exception e) {
-                    logger.log(Level.SEVERE, "Failed to create " + HostInfoDAO.class.getSimpleName(), e);
-                }
-                return source;
-            }
-
-            @Override
-            public void removedService(ServiceReference reference, Object service) {
-                if (reg != null) {
-                    reg.unregister();
-                }
-                super.removedService(reference, service);
-            }
-        };
-        tracker.open();
-    }
-
-    @Override
-    public void stop(BundleContext context) throws Exception {
-        tracker.close();
-    }
-
-    static class HostInfoDAOCreator {
-        HostInfoDAO createDAO(PluginConfiguration config) throws Exception {
-            return new HostInfoDAOImpl(config);
-        }
-    }
-
-}
-
--- a/plugins/host-overview/agent/src/main/java/com/redhat/thermostat/host/overview/internal/common/PluginDAOBase.java	Tue Jun 06 09:07:16 2017 -0400
+++ b/plugins/host-overview/agent/src/main/java/com/redhat/thermostat/host/overview/internal/common/PluginDAOBase.java	Thu Jun 08 19:45:19 2017 +0200
@@ -46,6 +46,7 @@
 import org.eclipse.jetty.client.api.ContentResponse;
 import org.eclipse.jetty.client.api.Request;
 import org.eclipse.jetty.client.util.StringContentProvider;
+import org.eclipse.jetty.http.HttpHeader;
 import org.eclipse.jetty.http.HttpMethod;
 import org.eclipse.jetty.http.HttpStatus;
 
@@ -57,24 +58,25 @@
 
     private static final String CONTENT_TYPE = "application/json";
 
-    private final String gatewayURL;
     protected final HttpClient httpClient;
 
-    public PluginDAOBase(PluginConfiguration config, HttpClient client) throws IOException {
-        this.gatewayURL = config.getGatewayURL();
+    public PluginDAOBase(HttpClient client) {
         this.httpClient = client;
     }
 
     protected abstract String toJsonString(Tobj obj) throws IOException;
+    protected abstract PluginConfiguration getConfig();
 
     public void put(String systemid, final Tobj obj) {
         try {
+            final String gatewayURL = getConfig().getGatewayURL();
             final String json = toJsonString(obj);
             final StringContentProvider provider =  new StringContentProvider(json);
             final String url = gatewayURL + "/systems/" + systemid;
             final Request httpRequest = httpClient.newRequest(url);
             httpRequest.method(HttpMethod.POST);
-            httpRequest.content(provider, CONTENT_TYPE);
+            httpRequest.content(provider);
+            httpRequest.header(HttpHeader.CONTENT_TYPE, CONTENT_TYPE);
             sendRequest(httpRequest);
         } catch (IOException | InterruptedException | TimeoutException | ExecutionException e) {
             logger.log(Level.WARNING, "Failed to send " + obj.getClass().getName() + " to web gateway", e);
--- a/plugins/host-overview/agent/src/main/java/com/redhat/thermostat/host/overview/internal/models/HostInfoDAOImpl.java	Tue Jun 06 09:07:16 2017 -0400
+++ b/plugins/host-overview/agent/src/main/java/com/redhat/thermostat/host/overview/internal/models/HostInfoDAOImpl.java	Thu Jun 08 19:45:19 2017 +0200
@@ -37,48 +37,62 @@
 package com.redhat.thermostat.host.overview.internal.models;
 
 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;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.Service;
+import org.eclipse.jetty.client.HttpClient;
+import org.eclipse.jetty.client.util.StringContentProvider;
+
+import com.redhat.thermostat.common.config.experimental.ConfigurationInfoSource;
+import com.redhat.thermostat.common.utils.LoggingUtils;
 import com.redhat.thermostat.host.overview.internal.HostInfoTypeAdapter;
 import com.redhat.thermostat.host.overview.internal.common.PluginConfiguration;
 import com.redhat.thermostat.host.overview.internal.common.PluginDAOBase;
-import org.eclipse.jetty.client.HttpClient;
-import org.eclipse.jetty.client.HttpContentResponse;
-import org.eclipse.jetty.client.HttpRequest;
-import org.eclipse.jetty.client.api.Request;
-import org.eclipse.jetty.client.util.StringContentProvider;
-import org.eclipse.jetty.http.HttpStatus;
-
-import com.redhat.thermostat.common.utils.LoggingUtils;
 import com.redhat.thermostat.host.overview.model.HostInfo;
 
+@Component
+@Service(value = HostInfoDAO.class)
 public class HostInfoDAOImpl extends PluginDAOBase<HostInfo, HostInfoDAOImpl> implements HostInfoDAO {
     
     private static final Logger logger = LoggingUtils.getLogger(HostInfoDAOImpl.class);
-
+    
     public static final String PLUGIN_ID = "host-overview";
 
     private final JsonHelper jsonHelper;
     private final HttpHelper httpHelper;
 
-    public HostInfoDAOImpl(PluginConfiguration config) throws Exception {
-        this(config, new HttpClient());
+    private final ConfigurationCreator configCreator;
+    
+    @Reference
+    private ConfigurationInfoSource configInfoSource;
+    private PluginConfiguration config;
+
+    public HostInfoDAOImpl() {
+        this(new HttpClient(), new JsonHelper(new HostInfoTypeAdapter()), new HttpHelper(), 
+                new ConfigurationCreator(), null);
     }
 
-    public HostInfoDAOImpl(PluginConfiguration config, HttpClient client) throws Exception {
-        this(config, client, new JsonHelper(new HostInfoTypeAdapter()), new HttpHelper(client));
+    HostInfoDAOImpl(HttpClient client, JsonHelper jh, HttpHelper hh, ConfigurationCreator creator, 
+            ConfigurationInfoSource source) {
+        super(client);
+        this.jsonHelper = jh;
+        this.httpHelper = hh;
+        this.configCreator = creator;
+        this.configInfoSource = source;
     }
 
-    HostInfoDAOImpl(PluginConfiguration config, HttpClient client, JsonHelper jsonHelper, HttpHelper httpHelper) throws Exception {
-        super(config, client);
-        this.jsonHelper = jsonHelper;
-        this.httpHelper = httpHelper;
-        this.httpHelper.startClient(this.httpClient);
+    @Activate
+    void activate() throws Exception {
+        this.config = configCreator.create(configInfoSource);
+        httpHelper.startClient(httpClient);
     }
 
+
     public String getPluginId() {
         return PLUGIN_ID;
     }
@@ -91,6 +105,11 @@
     protected String toJsonString(HostInfo obj) throws IOException {
         return jsonHelper.toJson(Arrays.asList(obj));
     }
+    
+    @Override
+    protected PluginConfiguration getConfig() {
+    	return config;
+    }
 
     // For testing purposes
     static class JsonHelper {
@@ -110,12 +129,6 @@
     // For testing purposes
     static class HttpHelper {
         
-        private final HttpClient httpClient;
-
-        HttpHelper(HttpClient httpClient) {
-            this.httpClient = httpClient;
-        }
-        
         void startClient(HttpClient httpClient) throws Exception {
             httpClient.start();
         }
@@ -124,30 +137,13 @@
             return new StringContentProvider(content);
         }
         
-        Request newRequest(String url) {
-            return new MockRequest(httpClient, URI.create(url));
-        }
-        
     }
     
-    // FIXME This class should be removed when the web gateway has a microservice for this DAO
-    private static class MockRequest extends HttpRequest {
-
-        MockRequest(HttpClient client, URI uri) {
-            super(client, uri);
-        }
-    }
-    
-    // FIXME This class should be removed when the web gateway has a microservice for this DAO
-    private static class MockResponse extends HttpContentResponse {
-
-        MockResponse() {
-            super(null, null, null);
-        }
+    // For Testing purposes
+    static class ConfigurationCreator {
         
-        @Override
-        public int getStatus() {
-            return HttpStatus.OK_200;
+        PluginConfiguration create(ConfigurationInfoSource source) {
+            return new PluginConfiguration(source, PLUGIN_ID);
         }
         
     }
--- a/plugins/host-overview/agent/src/test/java/com/redhat/thermostat/host/overview/internal/models/HostInfoDAOTest.java	Tue Jun 06 09:07:16 2017 -0400
+++ b/plugins/host-overview/agent/src/test/java/com/redhat/thermostat/host/overview/internal/models/HostInfoDAOTest.java	Thu Jun 08 19:45:19 2017 +0200
@@ -49,12 +49,14 @@
 
 import com.redhat.thermostat.common.config.experimental.ConfigurationInfoSource;
 import com.redhat.thermostat.host.overview.internal.common.PluginConfiguration;
+import com.redhat.thermostat.host.overview.internal.models.HostInfoDAOImpl.ConfigurationCreator;
 
 import org.eclipse.jetty.client.HttpClient;
 import org.eclipse.jetty.client.api.ContentProvider;
 import org.eclipse.jetty.client.api.ContentResponse;
 import org.eclipse.jetty.client.api.Request;
 import org.eclipse.jetty.client.util.StringContentProvider;
+import org.eclipse.jetty.http.HttpHeader;
 import org.eclipse.jetty.http.HttpMethod;
 import org.eclipse.jetty.http.HttpStatus;
 import org.junit.Before;
@@ -73,7 +75,6 @@
     private static final String CPU_MODEL = "some cpu that runs fast";
     private static final int CPU_NUM = -1;
     private static final long MEMORY_TOTAL = 0xCAFEBABEl;
-    private static final String CONTENT_TYPE = "application/json";
 
     private static final String URL_PROP = "gatewayURL";
 
@@ -85,6 +86,7 @@
     private Request request;
     private ContentResponse response;
     private ConfigurationInfoSource cfiSource;
+    private ConfigurationCreator configCreator;
     
     @Before
     public void setup() throws Exception {
@@ -97,7 +99,6 @@
         httpClient = mock(HttpClient.class);
         request = mock(Request.class);
         when(httpClient.newRequest(anyString())).thenReturn(request);
-        when(httpHelper.newRequest(anyString())).thenReturn(request);
         response = mock(ContentResponse.class);
         when(response.getStatus()).thenReturn(HttpStatus.OK_200);
         when(request.send()).thenReturn(response);
@@ -109,18 +110,23 @@
         Map<String,String> map = new HashMap<>();
         map.put(URL_PROP, URL);
         when(cfiSource.getConfiguration(anyString(),anyString())).thenReturn(map);
+        configCreator = mock(ConfigurationCreator.class);
+        when(configCreator.create(eq(cfiSource))).thenReturn(new PluginConfiguration(cfiSource, HostInfoDAOImpl.PLUGIN_ID));
     }
 
     @Test
     public void testPutHostInfo() throws Exception {
 
-        HostInfoDAOImpl dao = new HostInfoDAOImpl(new PluginConfiguration(cfiSource, HostInfoDAOImpl.PLUGIN_ID), httpClient, jsonHelper, httpHelper);
+        HostInfoDAOImpl dao = new HostInfoDAOImpl(httpClient, jsonHelper, httpHelper, configCreator, cfiSource);
+        dao.activate();
+        
         dao.put(info.getAgentId(), info);
         
         verify(httpClient).newRequest(URL + "/systems/" + info.getAgentId());
         verify(request).method(HttpMethod.POST);
         verify(jsonHelper).toJson(eq(Arrays.asList(info)));
-        verify(request).content(Matchers.any(ContentProvider.class), eq(CONTENT_TYPE));
+        verify(request).content(Matchers.any(ContentProvider.class));
+        verify(request).header(HttpHeader.CONTENT_TYPE, "application/json");
         verify(request).send();
         verify(response).getStatus();
     }