Mercurial > hg > thermostat-ng > agent
changeset 2708:f88bf3542b62
Add host-cpu plugin
http://icedtea.classpath.org/pipermail/thermostat/2017-June/023900.html
Reviewed-by: sgehwolf, neugens
Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2017-June/023887.html
author | Simon Tooke <stooke@redhat.com> |
---|---|
date | Thu, 22 Jun 2017 02:59:08 -0400 |
parents | 00feaa00f469 |
children | b9ab00ccce2f |
files | distribution/assembly/plugin-assembly.xml distribution/pom.xml plugins/host-cpu/agent/pom.xml plugins/host-cpu/agent/src/main/java/com/redhat/thermostat/host/cpu/agent/internal/Activator.java plugins/host-cpu/agent/src/main/java/com/redhat/thermostat/host/cpu/agent/internal/CpuStatBuilder.java plugins/host-cpu/agent/src/main/java/com/redhat/thermostat/host/cpu/agent/internal/CpuStatDAO.java plugins/host-cpu/agent/src/main/java/com/redhat/thermostat/host/cpu/agent/internal/CpuStatDAOImpl.java plugins/host-cpu/agent/src/main/java/com/redhat/thermostat/host/cpu/agent/internal/HostCpuBackend.java plugins/host-cpu/agent/src/main/java/com/redhat/thermostat/host/cpu/agent/internal/LinuxCpuStatBuilder.java plugins/host-cpu/agent/src/main/java/com/redhat/thermostat/host/cpu/agent/internal/WindowsCpuStatBuilder.java plugins/host-cpu/agent/src/main/java/com/redhat/thermostat/host/cpu/model/CpuStat.java plugins/host-cpu/agent/src/main/java/com/redhat/thermostat/host/cpu/model/CpuStatTypeAdapter.java plugins/host-cpu/agent/src/test/java/com/redhat/thermostat/host/cpu/agent/internal/ActivatorTest.java plugins/host-cpu/agent/src/test/java/com/redhat/thermostat/host/cpu/agent/internal/CpuStatDAOTest.java plugins/host-cpu/agent/src/test/java/com/redhat/thermostat/host/cpu/agent/internal/HostCpuBackendTest.java plugins/host-cpu/agent/src/test/java/com/redhat/thermostat/host/cpu/agent/internal/LinuxCpuStatBuilderTest.java plugins/host-cpu/agent/src/test/java/com/redhat/thermostat/host/cpu/agent/internal/WindowsCpuStatBuilderTest.java plugins/host-cpu/agent/src/test/java/com/redhat/thermostat/host/cpu/model/CpuStatTest.java plugins/host-cpu/agent/src/test/java/com/redhat/thermostat/host/cpu/model/CpuStatTypeAdapterTest.java plugins/host-cpu/common/pom.xml plugins/host-cpu/common/src/main/java/com/redhat/thermostat/host/cpu/common/CpuStatDAO.java plugins/host-cpu/common/src/main/java/com/redhat/thermostat/host/cpu/common/internal/Activator.java plugins/host-cpu/common/src/main/java/com/redhat/thermostat/host/cpu/common/internal/CpuStatCategoryRegistration.java plugins/host-cpu/common/src/main/java/com/redhat/thermostat/host/cpu/common/internal/CpuStatDAOImpl.java plugins/host-cpu/common/src/main/java/com/redhat/thermostat/host/cpu/common/internal/CpuStatDAOImplStatementDescriptorRegistration.java plugins/host-cpu/common/src/main/java/com/redhat/thermostat/host/cpu/common/internal/CpuStatTypeAdapter.java plugins/host-cpu/common/src/main/java/com/redhat/thermostat/host/cpu/common/model/CpuStat.java plugins/host-cpu/common/src/main/resources/META-INF/services/com.redhat.thermostat.storage.core.auth.CategoryRegistration plugins/host-cpu/common/src/main/resources/META-INF/services/com.redhat.thermostat.storage.core.auth.StatementDescriptorRegistration plugins/host-cpu/common/src/test/java/com/redhat/thermostat/host/cpu/common/internal/ActivatorTest.java plugins/host-cpu/common/src/test/java/com/redhat/thermostat/host/cpu/common/internal/CpuStatCategoryRegistrationTest.java plugins/host-cpu/common/src/test/java/com/redhat/thermostat/host/cpu/common/internal/CpuStatDAOImplStatementDescriptorRegistrationTest.java plugins/host-cpu/common/src/test/java/com/redhat/thermostat/host/cpu/common/internal/CpuStatDAOTest.java plugins/host-cpu/common/src/test/java/com/redhat/thermostat/host/cpu/common/internal/CpuStatTypeAdapterTest.java plugins/host-cpu/common/src/test/java/com/redhat/thermostat/host/cpu/common/model/CpuStatTest.java plugins/host-cpu/distribution/assemblies/plugin-assembly.xml plugins/host-cpu/distribution/configFiles/gateway.properties plugins/host-cpu/distribution/thermostat-plugin.xml plugins/host-cpu/pom.xml plugins/pom.xml |
diffstat | 40 files changed, 946 insertions(+), 1678 deletions(-) [+] |
line wrap: on
line diff
--- a/distribution/assembly/plugin-assembly.xml Thu Jun 22 02:36:39 2017 -0400 +++ b/distribution/assembly/plugin-assembly.xml Thu Jun 22 02:59:08 2017 -0400 @@ -50,11 +50,11 @@ <unpack>true</unpack> <includes> <include>com.redhat.thermostat:thermostat-host-overview-distribution</include> + <include>com.redhat.thermostat:thermostat-host-cpu-distribution</include> + <!--include>com.redhat.thermostat:thermostat-host-memory-distribution</include--> <include>com.redhat.thermostat:thermostat-jvm-overview-distribution</include> <include>com.redhat.thermostat:thermostat-vm-gc-distribution</include> <include>com.redhat.thermostat:thermostat-vm-memory-distribution</include> -<!-- <include>com.redhat.thermostat:thermostat-host-cpu-distribution</include>--> -<!-- <include>com.redhat.thermostat:thermostat-host-memory-distribution</include>--> <!-- <include>com.redhat.thermostat:thermostat-numa-distribution</include>--> <!-- <include>com.redhat.thermostat:thermostat-thread-distribution</include>--> <!-- <include>com.redhat.thermostat:thermostat-vm-byteman-distribution</include>-->
--- a/distribution/pom.xml Thu Jun 22 02:36:39 2017 -0400 +++ b/distribution/pom.xml Thu Jun 22 02:59:08 2017 -0400 @@ -484,6 +484,18 @@ </dependency> <dependency> <groupId>com.redhat.thermostat</groupId> + <artifactId>thermostat-host-cpu-distribution</artifactId> + <version>${project.version}</version> + <type>zip</type> + </dependency> + <!--dependency> + <groupId>com.redhat.thermostat</groupId> + <artifactId>thermostat-host-memory-distribution</artifactId> + <version>${project.version}</version> + <type>zip</type> + </dependency--> + <dependency> + <groupId>com.redhat.thermostat</groupId> <artifactId>thermostat-jvm-overview-distribution</artifactId> <version>${project.version}</version> <type>zip</type> @@ -514,12 +526,6 @@ </dependency> <!--<dependency>--> <!--<groupId>com.redhat.thermostat</groupId>--> - <!--<artifactId>thermostat-host-cpu-distribution</artifactId>--> - <!--<version>${project.version}</version>--> - <!--<type>zip</type>--> - <!--</dependency>--> - <!--<dependency>--> - <!--<groupId>com.redhat.thermostat</groupId>--> <!--<artifactId>thermostat-host-memory-distribution</artifactId>--> <!--<version>${project.version}</version>--> <!--<type>zip</type>-->
--- a/plugins/host-cpu/agent/pom.xml Thu Jun 22 02:36:39 2017 -0400 +++ b/plugins/host-cpu/agent/pom.xml Thu Jun 22 02:59:08 2017 -0400 @@ -59,8 +59,7 @@ <instructions> <Bundle-Vendor>Red Hat, Inc.</Bundle-Vendor> <Bundle-SymbolicName>com.redhat.thermostat.host.cpu.agent</Bundle-SymbolicName> - <Bundle-Activator>com.redhat.thermostat.host.cpu.agent.internal.Activator</Bundle-Activator> - <Export-Package /> + <Export-Package>com.redhat.thermostat.host.cpu.model</Export-Package> <Private-Package> com.redhat.thermostat.host.cpu.agent.internal </Private-Package> @@ -70,6 +69,18 @@ </configuration> </plugin> + <plugin> + <groupId>org.apache.felix</groupId> + <artifactId>maven-scr-plugin</artifactId> + <executions> + <execution> + <id>generate-scr-scrdescriptor</id> + <goals> + <goal>scr</goal> + </goals> + </execution> + </executions> + </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> @@ -101,6 +112,7 @@ <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <configuration> + <!-- the test needs to link to common-portability native libraries --> <systemPropertyVariables> <com.redhat.thermostat.shared.loader.testNativesHome>${project.build.directory}</com.redhat.thermostat.shared.loader.testNativesHome> </systemPropertyVariables> @@ -131,18 +143,23 @@ <scope>provided</scope> </dependency> <dependency> + <groupId>com.google.code.gson</groupId> + <artifactId>gson</artifactId> + <version>${gson.version}</version> + </dependency> + <dependency> <groupId>com.redhat.thermostat</groupId> <artifactId>thermostat-common-core</artifactId> <version>${project.version}</version> </dependency> <dependency> <groupId>com.redhat.thermostat</groupId> - <artifactId>thermostat-common-portability</artifactId> + <artifactId>thermostat-common-plugin</artifactId> <version>${project.version}</version> </dependency> <dependency> <groupId>com.redhat.thermostat</groupId> - <artifactId>thermostat-host-cpu-common</artifactId> + <artifactId>thermostat-common-portability</artifactId> <version>${project.version}</version> </dependency> <dependency> @@ -161,5 +178,12 @@ <version>${project.version}</version> <scope>test</scope> </dependency> + + <!-- declarative services --> + <dependency> + <groupId>org.apache.felix</groupId> + <artifactId>org.apache.felix.scr.annotations</artifactId> + </dependency> </dependencies> </project> +
--- a/plugins/host-cpu/agent/src/main/java/com/redhat/thermostat/host/cpu/agent/internal/Activator.java Thu Jun 22 02:36:39 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,105 +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.cpu.agent.internal; - -import java.util.concurrent.Executors; -import java.util.concurrent.ScheduledExecutorService; - -import org.osgi.framework.BundleActivator; -import org.osgi.framework.BundleContext; -import org.osgi.framework.ServiceRegistration; - -import com.redhat.thermostat.backend.Backend; -import com.redhat.thermostat.backend.BackendService; -import com.redhat.thermostat.common.MultipleServiceTracker; -import com.redhat.thermostat.common.MultipleServiceTracker.Action; -import com.redhat.thermostat.common.MultipleServiceTracker.DependencyProvider; -import com.redhat.thermostat.common.Version; -import com.redhat.thermostat.host.cpu.common.CpuStatDAO; -import com.redhat.thermostat.storage.core.WriterID; - -public class Activator implements BundleActivator { - - private ScheduledExecutorService executor; - private MultipleServiceTracker tracker; - private HostCpuBackend backend; - private ServiceRegistration<Backend> reg; - - @Override - public void start(final BundleContext context) throws Exception { - executor = Executors.newSingleThreadScheduledExecutor(); - - Class<?>[] deps = new Class<?>[] { - BackendService.class, - CpuStatDAO.class, - WriterID.class, // Host cpu backend uses it - }; - tracker = new MultipleServiceTracker(context, deps, new Action() { - - @Override - public void dependenciesAvailable(DependencyProvider services) { - CpuStatDAO cpuStatDao = services.get(CpuStatDAO.class); - Version version = new Version(context.getBundle()); - WriterID id = services.get(WriterID.class); - backend = new HostCpuBackend(executor, cpuStatDao, version, id); - reg = context.registerService(Backend.class, backend, null); - } - - @Override - public void dependenciesUnavailable() { - if (backend.isActive()) { - backend.deactivate(); - } - reg.unregister(); - } - }); - tracker.open(); - } - - @Override - public void stop(BundleContext context) throws Exception { - tracker.close(); - } - - /* - * For testing purposes only. - */ - HostCpuBackend getBackend() { - return backend; - } -} -
--- a/plugins/host-cpu/agent/src/main/java/com/redhat/thermostat/host/cpu/agent/internal/CpuStatBuilder.java Thu Jun 22 02:36:39 2017 -0400 +++ b/plugins/host-cpu/agent/src/main/java/com/redhat/thermostat/host/cpu/agent/internal/CpuStatBuilder.java Thu Jun 22 02:59:08 2017 -0400 @@ -36,7 +36,7 @@ package com.redhat.thermostat.host.cpu.agent.internal; -import com.redhat.thermostat.host.cpu.common.model.CpuStat; +import com.redhat.thermostat.host.cpu.model.CpuStat; interface CpuStatBuilder { CpuStat build();
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/plugins/host-cpu/agent/src/main/java/com/redhat/thermostat/host/cpu/agent/internal/CpuStatDAO.java Thu Jun 22 02:59:08 2017 -0400 @@ -0,0 +1,46 @@ +/* + * 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.cpu.agent.internal; + +import com.redhat.thermostat.annotations.Service; +import com.redhat.thermostat.host.cpu.model.CpuStat; + +@Service +public interface CpuStatDAO { + void put(CpuStat stat); +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/plugins/host-cpu/agent/src/main/java/com/redhat/thermostat/host/cpu/agent/internal/CpuStatDAOImpl.java Thu Jun 22 02:59:08 2017 -0400 @@ -0,0 +1,160 @@ +/* + * 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.cpu.agent.internal; + +import java.io.IOException; +import java.util.Arrays; +import java.util.List; +import java.util.logging.Logger; + +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.common.utils.LoggingUtils; +import com.redhat.thermostat.common.plugin.PluginDAOBase; +import com.redhat.thermostat.host.cpu.model.CpuStat; +import com.redhat.thermostat.host.cpu.model.CpuStatTypeAdapter; +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; + +@Component +@Service(value = CpuStatDAO.class) +public class CpuStatDAOImpl extends PluginDAOBase<CpuStat, CpuStatDAOImpl> implements CpuStatDAO { + + private static final Logger logger = LoggingUtils.getLogger(CpuStatDAOImpl.class); + + public static final String PLUGIN_ID = "host-cpu"; + + private final JsonHelper jsonHelper; + private final ConfigurationCreator configCreator; + private PluginConfiguration config; + + @Reference + private ConfigurationInfoSource configurationInfoSource; + + @Reference + private SystemID systemID; + + @Reference + private HttpRequestService httpRequestService; + + public CpuStatDAOImpl() { + this(new JsonHelper(new CpuStatTypeAdapter()), new ConfigurationCreator()); + } + + CpuStatDAOImpl(JsonHelper jh, ConfigurationCreator creator) { + this.jsonHelper = jh; + this.configCreator = creator; + } + + @Activate + public void activate() throws Exception { + config = configCreator.create(configurationInfoSource); + } + + public String getPluginId() { + return PLUGIN_ID; + } + + public Logger getLogger() { + return logger; + } + + @Override + protected HttpRequestService getHttpRequestService() { + return httpRequestService; + } + + @Override + protected String toJsonString(CpuStat obj) throws IOException { + return jsonHelper.toJson(Arrays.asList(obj)); + } + + @Override + protected PluginConfiguration getConfig() { + return config; + } + + @Override + protected String getURL(String basepath) { + return basepath + "/systems/" + systemID.getSystemID(); + } + + // DS bind method + protected void bindSystemID(SystemID systemid) { + this.systemID = systemid; + } + + protected void bindConfigurationInfoSource(ConfigurationInfoSource cfg) { + this.configurationInfoSource = cfg; + } + + protected void bindHttpRequestService(HttpRequestService httpRequestService) { + this.httpRequestService = httpRequestService; + } + + protected void unbindHttpRequestService(HttpRequestService httpRequestService) { + this.httpRequestService = null; + } + + // For testing purposes + static class JsonHelper { + + private final CpuStatTypeAdapter typeAdapter; + + JsonHelper(CpuStatTypeAdapter typeAdapter) { + this.typeAdapter = typeAdapter; + } + + String toJson(List<CpuStat> infos) throws IOException { + return typeAdapter.toJson(infos); + } + } + // For Testing purposes + static class ConfigurationCreator { + + PluginConfiguration create(ConfigurationInfoSource source) { + return new PluginConfiguration(source, PLUGIN_ID); + } + } +} +
--- a/plugins/host-cpu/agent/src/main/java/com/redhat/thermostat/host/cpu/agent/internal/HostCpuBackend.java Thu Jun 22 02:36:39 2017 -0400 +++ b/plugins/host-cpu/agent/src/main/java/com/redhat/thermostat/host/cpu/agent/internal/HostCpuBackend.java Thu Jun 22 02:59:08 2017 -0400 @@ -36,23 +36,57 @@ package com.redhat.thermostat.host.cpu.agent.internal; +import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; +import com.redhat.thermostat.backend.Backend; import com.redhat.thermostat.backend.HostPollingAction; import com.redhat.thermostat.backend.HostPollingBackend; import com.redhat.thermostat.common.Version; -import com.redhat.thermostat.host.cpu.common.CpuStatDAO; +import com.redhat.thermostat.host.cpu.model.CpuStat; import com.redhat.thermostat.storage.core.WriterID; +import org.apache.felix.scr.annotations.Activate; +import org.apache.felix.scr.annotations.Component; +import org.apache.felix.scr.annotations.Deactivate; +import org.apache.felix.scr.annotations.Reference; +import org.apache.felix.scr.annotations.Service; +import org.osgi.framework.BundleContext; + +@Component +@Service(value = Backend.class) public class HostCpuBackend extends HostPollingBackend { - public HostCpuBackend(ScheduledExecutorService executor, - CpuStatDAO cpuStatDAO, Version version, final WriterID writerId) { - super("Host CPU Backend", - "Gathers CPU statistics about a host", - "Red Hat, Inc.", - version, executor); - registerAction(new CpuProcBackendAction(writerId, cpuStatDAO)); + @Reference + private CpuStatDAO cpuStatDAO; + + @Reference + private WriterID writerID; + + public HostCpuBackend() { + this(Executors.newSingleThreadScheduledExecutor()); + } + + public HostCpuBackend(ScheduledExecutorService executor) { + this("Host CPU Backend", "Gathers CPU statistics about a host", "Red Hat, Inc.", new Version(), executor); + } + + public HostCpuBackend(String name, String descr, String vendor, Version version, ScheduledExecutorService executor) { + super(name, descr, vendor, version, executor); + } + + @Activate + protected void componentActivated(BundleContext context) { + Version version = new Version(context.getBundle()); + setVersion(version.getVersionNumber()); + registerAction(new CpuProcBackendAction(writerID, cpuStatDAO)); + } + + @Deactivate + protected void componentDeactivated() { + if (isActive()) { + deactivate(); + } } private static class CpuProcBackendAction implements HostPollingAction { @@ -70,11 +104,20 @@ if (!builder.isInitialized()) { builder.initialize(); } else { - dao.putCpuStat(builder.build()); + final CpuStat info = builder.build(); + dao.put(info); } } } + void bindCpuStatDAO(CpuStatDAO dao) { + this.cpuStatDAO = dao; + } + + void bindWriterID(WriterID id) { + this.writerID = id; + } + @Override public int getOrderValue() { return ORDER_CPU_GROUP;
--- a/plugins/host-cpu/agent/src/main/java/com/redhat/thermostat/host/cpu/agent/internal/LinuxCpuStatBuilder.java Thu Jun 22 02:36:39 2017 -0400 +++ b/plugins/host-cpu/agent/src/main/java/com/redhat/thermostat/host/cpu/agent/internal/LinuxCpuStatBuilder.java Thu Jun 22 02:59:08 2017 -0400 @@ -44,7 +44,7 @@ import com.redhat.thermostat.common.portability.linux.ProcDataSource; import com.redhat.thermostat.common.Clock; import com.redhat.thermostat.common.utils.LoggingUtils; -import com.redhat.thermostat.host.cpu.common.model.CpuStat; +import com.redhat.thermostat.host.cpu.model.CpuStat; import com.redhat.thermostat.storage.core.WriterID; public class LinuxCpuStatBuilder implements CpuStatBuilder {
--- a/plugins/host-cpu/agent/src/main/java/com/redhat/thermostat/host/cpu/agent/internal/WindowsCpuStatBuilder.java Thu Jun 22 02:36:39 2017 -0400 +++ b/plugins/host-cpu/agent/src/main/java/com/redhat/thermostat/host/cpu/agent/internal/WindowsCpuStatBuilder.java Thu Jun 22 02:59:08 2017 -0400 @@ -39,7 +39,7 @@ import com.redhat.thermostat.common.Clock; import com.redhat.thermostat.common.portability.PortableHost; import com.redhat.thermostat.common.portability.PortableHostFactory; -import com.redhat.thermostat.host.cpu.common.model.CpuStat; +import com.redhat.thermostat.host.cpu.model.CpuStat; import com.redhat.thermostat.storage.core.WriterID; public class WindowsCpuStatBuilder implements CpuStatBuilder {
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/plugins/host-cpu/agent/src/main/java/com/redhat/thermostat/host/cpu/model/CpuStat.java Thu Jun 22 02:59:08 2017 -0400 @@ -0,0 +1,83 @@ +/* + * 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.cpu.model; + +import com.redhat.thermostat.storage.core.Entity; +import com.redhat.thermostat.storage.core.Persist; +import com.redhat.thermostat.storage.model.BasePojo; +import com.redhat.thermostat.storage.model.TimeStampedPojo; + +@Entity +public class CpuStat extends BasePojo implements TimeStampedPojo { + + public static final double INVALID_LOAD = Double.MIN_VALUE; + + private long timeStamp; + private double[] perProcessorUsage; + + public CpuStat() { + this(null, -1, null); + } + + public CpuStat(String writerId, long timestamp, double[] perProcessorUsage) { + super(writerId); + this.timeStamp = timestamp; + this.perProcessorUsage = perProcessorUsage; + } + + @Persist + public double[] getPerProcessorUsage() { + return perProcessorUsage; + } + + @Persist + public void setPerProcessorUsage(double[] perProcessorUsage) { + this.perProcessorUsage = perProcessorUsage; + } + + @Persist + @Override + public long getTimeStamp() { + return timeStamp; + } + + @Persist + public void setTimeStamp(long timeStamp) { + this.timeStamp = timeStamp; + } +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/plugins/host-cpu/agent/src/main/java/com/redhat/thermostat/host/cpu/model/CpuStatTypeAdapter.java Thu Jun 22 02:59:08 2017 -0400 @@ -0,0 +1,200 @@ +/* + * 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.cpu.model; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.logging.Level; +import java.util.logging.Logger; + +import com.google.gson.stream.JsonReader; +import com.google.gson.stream.JsonWriter; +import com.google.gson.TypeAdapter; +import com.redhat.thermostat.common.utils.LoggingUtils; + +public class CpuStatTypeAdapter extends TypeAdapter<List<CpuStat>> { + + private static final String TIMESTAMP = "timeStamp"; + private static final String PROCESSOR_USAGE = "perProcessorUsage"; + private static final String TYPE_LONG = "$numberLong"; + private static final String SERVER_TIME = "time"; + private static final String RESPONSE_ROOT = "response"; + private static final String AGENT_ID = "agentId"; + + private static final Logger logger = LoggingUtils.getLogger(CpuStatTypeAdapter.class); + + @Override + public void write(JsonWriter out, List<CpuStat> value) throws IOException { + out.beginArray(); + + for (CpuStat stat : value) { + writeCpuStat(out, stat); + } + + out.endArray(); + } + + private void writeCpuStat(JsonWriter out, CpuStat stat) throws IOException { + out.beginObject(); + out.name(PROCESSOR_USAGE); + out.beginArray(); + for (double val : stat.getPerProcessorUsage()) { + out.value(val); + } + out.endArray(); + out.name(TIMESTAMP); + writeLong(out, stat.getTimeStamp()); + out.name(AGENT_ID); + out.value(stat.getAgentId()); + out.endObject(); + } + + @Override + public List<CpuStat> read(JsonReader in) throws IOException { + List<CpuStat> values = null; + try { + in.beginObject(); + while (in.hasNext()) { + String name = in.nextName(); + switch (name) { + case RESPONSE_ROOT: + values = readResponse(in); + break; + case SERVER_TIME: + in.nextString(); + break; + default: + throw new IOException("Unexpected JSON name: " + name); + } + } + in.endObject(); + } catch (IllegalStateException e) { + throw new IOException("Reading JSON response failed."); + } + return values; + } + + private List<CpuStat> readResponse(JsonReader in) throws IOException { + List<CpuStat> values = new ArrayList<>(); + + in.beginArray(); + + while (in.hasNext()) { + try { + CpuStat stat = readCpuStat(in); + values.add(stat); + } catch(IOException | IllegalStateException | NumberFormatException e) { + logger.log(Level.WARNING, "Skipping malformed cpu stat record", e); + } + } + in.endArray(); + + return values; + } + + private CpuStat readCpuStat(JsonReader in) throws IOException { + String agentId = null; + double[] perProcessorUsage = null; + long timestamp = -1; + + in.beginObject(); + + while (in.hasNext()) { + String name = in.nextName(); + switch (name) { + case PROCESSOR_USAGE: + perProcessorUsage = readProcessorUsage(in); + break; + case TIMESTAMP: + timestamp = readLong(in); + break; + case AGENT_ID: + agentId = in.nextString(); + break; + default: + throw new IOException("Unexpected JSON name: " + name); + } + } + + in.endObject(); + + if (perProcessorUsage == null || agentId == null || timestamp < 0) { + throw new IOException("CpuStat information is incomplete."); + } + return new CpuStat(agentId, timestamp, perProcessorUsage); + } + + private double[] readProcessorUsage(JsonReader in) throws IOException { + List<Double> procUsage = new ArrayList<Double>(); + in.beginArray(); + while (in.hasNext()) { + procUsage.add(in.nextDouble()); + } + in.endArray(); + double[] result = new double[procUsage.size()]; + for (int i = 0; i < result.length; i++) { + result[i] = procUsage.get(i); + } + return result; + } + + private long readLong(JsonReader in) throws IOException { + // Read MongoDB representation of a Long + in.beginObject(); + String name = in.nextName(); + expectName(TYPE_LONG, name); + long ret = Long.valueOf(in.nextString()); + in.endObject(); + return ret; + } + + private void writeLong(JsonWriter out, long timestamp) throws IOException { + // Write MongoDB representation of a Long + out.beginObject(); + out.name(TYPE_LONG); + out.value(String.valueOf(timestamp)); + out.endObject(); + } + + private void expectName(String expected, String actual) throws IOException { + if (!expected.equals(actual)) { + throw new IOException("Expected JSON name '" + expected + "', got '" + actual + "'"); + } + } + +}
--- a/plugins/host-cpu/agent/src/test/java/com/redhat/thermostat/host/cpu/agent/internal/ActivatorTest.java Thu Jun 22 02:36:39 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,108 +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.cpu.agent.internal; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -import org.junit.Test; -import org.osgi.framework.Bundle; -import org.osgi.framework.Version; - -import com.redhat.thermostat.backend.Backend; -import com.redhat.thermostat.backend.BackendService; -import com.redhat.thermostat.host.cpu.common.CpuStatDAO; -import com.redhat.thermostat.storage.core.WriterID; -import com.redhat.thermostat.testutils.StubBundleContext; - -public class ActivatorTest { - - @Test - public void verifyActivatorDoesNotRegisterServiceOnMissingDeps() throws Exception { - StubBundleContext context = new StubBundleContext(); - - Activator activator = new Activator(); - - activator.start(context); - - assertEquals(0, context.getAllServices().size()); - assertEquals(3, context.getServiceListeners().size()); - - activator.stop(context); - } - - @Test - public void verifyActivatorRegistersServices() throws Exception { - StubBundleContext context = new StubBundleContext() { - @Override - public Bundle getBundle() { - Bundle result = mock(Bundle.class); - when(result.getVersion()).thenReturn(Version.emptyVersion); - return result; - } - }; - - BackendService service = mock(BackendService.class); - CpuStatDAO cpuStatDAO = mock(CpuStatDAO.class); - WriterID id = mock(WriterID.class); - - context.registerService(BackendService.class, service, null); - context.registerService(CpuStatDAO.class, cpuStatDAO, null); - context.registerService(WriterID.class, id, null); - - Activator activator = new Activator(); - - activator.start(context); - - assertTrue(context.isServiceRegistered(Backend.class.getName(), HostCpuBackend.class)); - HostCpuBackend backend = activator.getBackend(); - assertNotNull(backend); - - activator.stop(context); - - assertFalse(backend.isActive()); - - assertEquals(0, context.getServiceListeners().size()); - assertEquals(3, context.getAllServices().size()); - } - -} -
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/plugins/host-cpu/agent/src/test/java/com/redhat/thermostat/host/cpu/agent/internal/CpuStatDAOTest.java Thu Jun 22 02:59:08 2017 -0400 @@ -0,0 +1,132 @@ +/* + * 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.cpu.agent.internal; + +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.anyListOf; +import static org.mockito.Matchers.anyString; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; + +import com.redhat.thermostat.agent.http.HttpRequestService; +import com.redhat.thermostat.common.Clock; +import com.redhat.thermostat.common.SystemClock; +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.host.cpu.model.CpuStat; + +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.HttpMethod; +import org.eclipse.jetty.http.HttpStatus; +import org.junit.Before; +import org.junit.Test; + +public class CpuStatDAOTest { + + private static final String URL = "http://localhost:26000/api/system-cpu/0.0.1"; + private static final String SOME_JSON = "{\"some\" : \"json\"}"; + private static final double times[] = { 33., 44, }; + private static final String CONTENT_TYPE = "application/json"; + private static final String HOST_NAME = "somehostname"; + + private static final String URL_PROP = "gatewayURL"; + + private CpuStat info; + private CpuStatDAOImpl.JsonHelper jsonHelper; + private ConfigurationInfoSource cfiSource; + private CpuStatDAOImpl.ConfigurationCreator configCreator; + + private SystemID idservice; + private HttpRequestService httpRequestService; + + @Before + public void setup() throws Exception { + Clock clock = new SystemClock(); + info = new CpuStat("foo-agent", clock.getRealTimeMillis(), times); + + Request request = mock(Request.class); + HttpClient httpClient = mock(HttpClient.class); + when(httpClient.newRequest(anyString())).thenReturn(request); + ContentResponse response = mock(ContentResponse.class); + when(response.getStatus()).thenReturn(HttpStatus.OK_200); + when(request.send()).thenReturn(response); + + jsonHelper = mock(CpuStatDAOImpl.JsonHelper.class); + when(jsonHelper.toJson(anyListOf(CpuStat.class))).thenReturn(SOME_JSON); + + cfiSource = mock(ConfigurationInfoSource.class); + Map<String,String> map = new HashMap<>(); + map.put(URL_PROP, URL); + when(cfiSource.getConfiguration(anyString(),anyString())).thenReturn(map); + + configCreator = mock(CpuStatDAOImpl.ConfigurationCreator.class); + when(configCreator.create(eq(cfiSource))).thenReturn(new PluginConfiguration(cfiSource, CpuStatDAOImpl.PLUGIN_ID)); + + httpRequestService = mock(HttpRequestService.class); + ContentResponse contentResponse = mock(ContentResponse.class); + + when(httpRequestService.sendHttpRequest(anyString(), anyString(), any(HttpMethod.class))).thenReturn(contentResponse); + when(contentResponse.getStatus()).thenReturn(HttpStatus.OK_200); + + idservice = mock(SystemID.class); + when(idservice.getSystemID()).thenReturn(HOST_NAME); + } + + @Test + public void testPut() throws Exception { + + CpuStatDAOImpl dao = new CpuStatDAOImpl(jsonHelper, configCreator); + dao.bindSystemID(idservice); + dao.bindConfigurationInfoSource(cfiSource); + dao.bindHttpRequestService(httpRequestService); + dao.activate(); + dao.put(info); + + verify(httpRequestService, times(1)).sendHttpRequest(SOME_JSON, URL + "/systems/" + HOST_NAME, HttpMethod.POST); + } + +}
--- a/plugins/host-cpu/agent/src/test/java/com/redhat/thermostat/host/cpu/agent/internal/HostCpuBackendTest.java Thu Jun 22 02:36:39 2017 -0400 +++ b/plugins/host-cpu/agent/src/test/java/com/redhat/thermostat/host/cpu/agent/internal/HostCpuBackendTest.java Thu Jun 22 02:59:08 2017 -0400 @@ -39,6 +39,8 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import static org.mockito.Matchers.any; +import static org.mockito.Matchers.anyString; +import static org.mockito.Matchers.matches; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; import static org.mockito.Mockito.verify; @@ -52,42 +54,56 @@ import org.mockito.ArgumentCaptor; import com.redhat.thermostat.common.Version; -import com.redhat.thermostat.host.cpu.common.CpuStatDAO; -import com.redhat.thermostat.host.cpu.common.model.CpuStat; +import com.redhat.thermostat.host.cpu.model.CpuStat; import com.redhat.thermostat.storage.core.WriterID; +import org.osgi.framework.Bundle; +import org.osgi.framework.BundleContext; public class HostCpuBackendTest { - + private HostCpuBackend backend; private ScheduledExecutorService executor; - private CpuStatDAO cpuStatDao; + private CpuStatDAO cpuStatDAO; + private WriterID writerID; + private Version version; @Before public void setup() { executor = mock(ScheduledExecutorService.class); - cpuStatDao = mock(CpuStatDAO.class); - Version version = mock(Version.class); + + version = mock(Version.class); when(version.getVersionNumber()).thenReturn("0.0.0"); - WriterID id = mock(WriterID.class); - - backend = new HostCpuBackend(executor, cpuStatDao, version, id); + + // these two are created via OSGI wiring + cpuStatDAO = mock(CpuStatDAO.class); + writerID = mock(WriterID.class); + + backend = new HostCpuBackend("Host CPU Backend", "Gathers CPU statistics about a host", "Red Hat, Inc.", version, executor); + backend.bindCpuStatDAO(cpuStatDAO); + backend.bindWriterID(writerID); } @Test public void testActivate() { + org.osgi.framework.Version osgiVersion = mock(org.osgi.framework.Version.class); + Bundle bundle = mock(Bundle.class); + when(bundle.getVersion()).thenReturn(osgiVersion); + BundleContext ctx = mock(BundleContext.class); + when(ctx.getBundle()).thenReturn(bundle); + backend.componentActivated(ctx); backend.activate(); ArgumentCaptor<Runnable> captor = ArgumentCaptor.forClass(Runnable.class); verify(executor).scheduleAtFixedRate(captor.capture(), any(Long.class), any(Long.class), any(TimeUnit.class)); assertTrue(backend.isActive()); - + // Run to ensure working Runnable runnable = captor.getValue(); runnable.run(); - verify(cpuStatDao, never()).putCpuStat(any(CpuStat.class)); + verify(cpuStatDAO, never()).put(any(CpuStat.class)); runnable.run(); - verify(cpuStatDao).putCpuStat(any(CpuStat.class)); + verify(cpuStatDAO).put(any(CpuStat.class)); } - + @Test public void testDeactivate() { backend.activate(); @@ -95,5 +111,4 @@ verify(executor).shutdown(); assertFalse(backend.isActive()); } -} - +} \ No newline at end of file
--- a/plugins/host-cpu/agent/src/test/java/com/redhat/thermostat/host/cpu/agent/internal/LinuxCpuStatBuilderTest.java Thu Jun 22 02:36:39 2017 -0400 +++ b/plugins/host-cpu/agent/src/test/java/com/redhat/thermostat/host/cpu/agent/internal/LinuxCpuStatBuilderTest.java Thu Jun 22 02:59:08 2017 -0400 @@ -54,7 +54,7 @@ import com.redhat.thermostat.common.portability.linux.ProcDataSource; import com.redhat.thermostat.common.Clock; import com.redhat.thermostat.common.SystemClock; -import com.redhat.thermostat.host.cpu.common.model.CpuStat; +import com.redhat.thermostat.host.cpu.model.CpuStat; import com.redhat.thermostat.storage.core.WriterID; public class LinuxCpuStatBuilderTest {
--- a/plugins/host-cpu/agent/src/test/java/com/redhat/thermostat/host/cpu/agent/internal/WindowsCpuStatBuilderTest.java Thu Jun 22 02:36:39 2017 -0400 +++ b/plugins/host-cpu/agent/src/test/java/com/redhat/thermostat/host/cpu/agent/internal/WindowsCpuStatBuilderTest.java Thu Jun 22 02:59:08 2017 -0400 @@ -38,7 +38,7 @@ import com.redhat.thermostat.common.Clock; import com.redhat.thermostat.common.SystemClock; -import com.redhat.thermostat.host.cpu.common.model.CpuStat; +import com.redhat.thermostat.host.cpu.model.CpuStat; import com.redhat.thermostat.shared.config.OS; import com.redhat.thermostat.storage.core.WriterID; import org.junit.Assume;
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/plugins/host-cpu/agent/src/test/java/com/redhat/thermostat/host/cpu/model/CpuStatTest.java Thu Jun 22 02:59:08 2017 -0400 @@ -0,0 +1,48 @@ +/* + * 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.cpu.model; + +import com.redhat.thermostat.testutils.DataObjectTest; + +public class CpuStatTest extends DataObjectTest { + + @Override + public Class<?>[] getDataClasses() { + return new Class[] { CpuStat.class }; + } +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/plugins/host-cpu/agent/src/test/java/com/redhat/thermostat/host/cpu/model/CpuStatTypeAdapterTest.java Thu Jun 22 02:59:08 2017 -0400 @@ -0,0 +1,127 @@ +/* + * 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.cpu.model; + +import com.google.gson.GsonBuilder; +import com.google.gson.Gson; +import com.google.gson.reflect.TypeToken; +import org.junit.Test; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertArrayEquals; + +import java.util.List; +import java.util.ArrayList; +import java.lang.reflect.Type; + +public class CpuStatTypeAdapterTest { + + @Test + public void testTypeAdapterSerializesSingletonCorrectly() { + GsonBuilder builder = new GsonBuilder(); + Type cpuStatListType = new TypeToken<ArrayList<CpuStat>>(){}.getType(); + builder.registerTypeAdapter(cpuStatListType, new CpuStatTypeAdapter().nullSafe()); + Gson gson = builder.create(); + List<CpuStat> stats = new ArrayList<>(); + stats.add(new CpuStat("1", 1234567890l, new double[]{9.99, 8.88, 3.1415, 7.777})); + String result = gson.toJson(stats, cpuStatListType); + assertEquals("[{\"perProcessorUsage\":[9.99,8.88,3.1415,7.777],\"timeStamp\":{\"$numberLong\":\"1234567890\"},\"agentId\":\"1\"}]", result); + } + + @Test + public void testTypeAdapterSerializesListCorrectly() { + GsonBuilder builder = new GsonBuilder(); + Type cpuStatListType = new TypeToken<ArrayList<CpuStat>>(){}.getType(); + builder.registerTypeAdapter(cpuStatListType, new CpuStatTypeAdapter().nullSafe()); + Gson gson = builder.create(); + List<CpuStat> stats = new ArrayList<>(); + stats.add(new CpuStat("1", 1000230101l, new double[]{1.23, 4.56, 7.89, 10.1112})); + stats.add(new CpuStat("2", 10002333101l, new double[]{1.323, 4.456, 7.789, 10.101112})); + stats.add(new CpuStat("3", 10002320101l, new double[]{1.234, 4.567, 7.8910, 10.111213})); + stats.add(new CpuStat("4", 100023313101l, new double[]{1.3235, 4.4567, 7.78911, 10.10111241})); + String result = gson.toJson(stats, cpuStatListType); + assertEquals("[{\"perProcessorUsage\":[1.23,4.56,7.89,10.1112],\"timeStamp\":{\"$numberLong\":\"1000230101\"},\"agentId\":\"1\"}," + + "{\"perProcessorUsage\":[1.323,4.456,7.789,10.101112],\"timeStamp\":{\"$numberLong\":\"10002333101\"},\"agentId\":\"2\"}," + + "{\"perProcessorUsage\":[1.234,4.567,7.891,10.111213],\"timeStamp\":{\"$numberLong\":\"10002320101\"},\"agentId\":\"3\"}," + + "{\"perProcessorUsage\":[1.3235,4.4567,7.78911,10.10111241],\"timeStamp\":{\"$numberLong\":\"100023313101\"},\"agentId\":\"4\"}]", result); + } + + @Test + public void testTypeAdapterDeserializesSingletonCorrectly() { + GsonBuilder builder = new GsonBuilder(); + Type cpuStatListType = new TypeToken<ArrayList<CpuStat>>(){}.getType(); + builder.registerTypeAdapter(cpuStatListType, new CpuStatTypeAdapter().nullSafe()); + Gson gson = builder.create(); + String serialized = "{\"response\":[{\"perProcessorUsage\":[1.23,4.56,7.89,11.1],\"timeStamp\":{\"$numberLong\":\"12345\"},\"agentId\":\"1\"}],\"time\":\"123\"}"; + List<CpuStat> result = gson.fromJson(serialized, cpuStatListType); + CpuStat stat = result.get(0); + assertEquals(12345, stat.getTimeStamp()); + assertArrayEquals(new double[]{1.23, 4.56, 7.89, 11.1}, stat.getPerProcessorUsage(), 0.000001); + assertEquals("1", stat.getAgentId()); + } + + @Test + public void testTypeAdapterDeserializesListCorrectly() { + GsonBuilder builder = new GsonBuilder(); + Type cpuStatListType = new TypeToken<List<CpuStat>>(){}.getType(); + builder.registerTypeAdapter(cpuStatListType, new CpuStatTypeAdapter().nullSafe()); + Gson gson = builder.create(); + String serialized = "{\"response\":[" + + "{\"perProcessorUsage\":[1.23,4.56,7.89,11.1],\"timeStamp\":{\"$numberLong\":\"12345\"},\"agentId\":\"1\"}," + + "{\"perProcessorUsage\":[3.21,6.54,9.87,1.11],\"timeStamp\":{\"$numberLong\":\"54321\"},\"agentId\":\"2\"}," + + "{\"perProcessorUsage\":[3.1415,2.765,9.999,1.11234],\"timeStamp\":{\"$numberLong\":\"98765\"},\"agentId\":\"3\"}," + + "{\"perProcessorUsage\":[444.678,9000.99,1243.5654,7.897867],\"timeStamp\":{\"$numberLong\":\"56789\"},\"agentId\":\"4\"}" + + "],\"time\":\"123\"}"; + List<CpuStat> result = gson.fromJson(serialized, cpuStatListType); + CpuStat stat = result.get(0); + assertEquals(12345, stat.getTimeStamp()); + assertArrayEquals(new double[]{1.23, 4.56, 7.89, 11.1}, stat.getPerProcessorUsage(), 0.000001); + assertEquals("1", stat.getAgentId()); + stat = result.get(1); + assertEquals(54321, stat.getTimeStamp()); + assertArrayEquals(new double[]{3.21,6.54,9.87,1.11}, stat.getPerProcessorUsage(), 0.000001); + assertEquals("2", stat.getAgentId()); + stat = result.get(2); + assertEquals(98765, stat.getTimeStamp()); + assertArrayEquals(new double[]{3.1415,2.765,9.999,1.11234}, stat.getPerProcessorUsage(), 0.000001); + assertEquals("3", stat.getAgentId()); + stat = result.get(3); + assertEquals(56789, stat.getTimeStamp()); + assertArrayEquals(new double[]{444.678,9000.99,1243.5654,7.897867}, stat.getPerProcessorUsage(), 0.000001); + assertEquals("4", stat.getAgentId()); + } + +}
--- a/plugins/host-cpu/common/pom.xml Thu Jun 22 02:36:39 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,133 +0,0 @@ -<?xml version="1.0" encoding="UTF-8"?> -<!-- - - 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. - ---> -<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> - <modelVersion>4.0.0</modelVersion> - <parent> - <artifactId>thermostat-host-cpu</artifactId> - <groupId>com.redhat.thermostat</groupId> - <version>1.99.12-SNAPSHOT</version> - </parent> - <artifactId>thermostat-host-cpu-common</artifactId> - <packaging>bundle</packaging> - <name>Thermostat Host CPU Common plugin</name> - <build> - <resources> - <resource> - <directory>src/main/resources</directory> - <filtering>true</filtering> - <excludes> - <exclude>**/*.png</exclude> - </excludes> - </resource> - <resource> - <directory>src/main/resources</directory> - <filtering>false</filtering> - <includes> - <include>**/*.png</include> - </includes> - </resource> - </resources> - <plugins> - <plugin> - <groupId>org.apache.felix</groupId> - <artifactId>maven-bundle-plugin</artifactId> - <extensions>true</extensions> - <configuration> - <instructions> - <Bundle-Vendor>Red Hat, Inc.</Bundle-Vendor> - <Bundle-SymbolicName>com.redhat.thermostat.host.cpu.common</Bundle-SymbolicName> - <Bundle-Activator>com.redhat.thermostat.host.cpu.common.internal.Activator</Bundle-Activator> - <Export-Package> - com.redhat.thermostat.host.cpu.common, - com.redhat.thermostat.host.cpu.common.model - </Export-Package> - <Private-Package> - com.redhat.thermostat.host.cpu.common.internal - </Private-Package> - <!-- Do not autogenerate uses clauses in Manifests --> - <_nouses>true</_nouses> - </instructions> - </configuration> - </plugin> - </plugins> - </build> - <dependencies> - <dependency> - <groupId>junit</groupId> - <artifactId>junit</artifactId> - <scope>test</scope> - </dependency> - <dependency> - <groupId>org.mockito</groupId> - <artifactId>mockito-core</artifactId> - <scope>test</scope> - </dependency> - <dependency> - <groupId>org.osgi</groupId> - <artifactId>org.osgi.core</artifactId> - <scope>provided</scope> - </dependency> - <dependency> - <groupId>org.osgi</groupId> - <artifactId>org.osgi.compendium</artifactId> - <scope>provided</scope> - </dependency> - <dependency> - <groupId>com.redhat.thermostat</groupId> - <artifactId>thermostat-common-core</artifactId> - <version>${project.version}</version> - </dependency> - <dependency> - <groupId>com.redhat.thermostat</groupId> - <artifactId>thermostat-storage-core</artifactId> - <version>${project.version}</version> - </dependency> - <dependency> - <groupId>com.redhat.thermostat</groupId> - <artifactId>thermostat-common-test</artifactId> - <version>${project.version}</version> - <scope>test</scope> - </dependency> - <dependency> - <groupId>com.google.code.gson</groupId> - <artifactId>gson</artifactId> - <version>${gson.version}</version> - </dependency> - </dependencies> -</project>
--- a/plugins/host-cpu/common/src/main/java/com/redhat/thermostat/host/cpu/common/CpuStatDAO.java Thu Jun 22 02:36:39 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,68 +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.cpu.common; - -import java.util.Arrays; -import java.util.List; - -import com.redhat.thermostat.annotations.Service; -import com.redhat.thermostat.host.cpu.common.model.CpuStat; -import com.redhat.thermostat.storage.core.HostRef; -import com.redhat.thermostat.storage.core.Category; -import com.redhat.thermostat.storage.core.HostTimeIntervalPojoListGetter; -import com.redhat.thermostat.storage.core.Key; - -@Service -public interface CpuStatDAO { - - static Key<List<Double>> cpuLoadKey = new Key<>("perProcessorUsage"); - - static final Category<CpuStat> cpuStatCategory = new Category<>("cpu-stats", CpuStat.class, - Arrays.<Key<?>>asList(Key.AGENT_ID, Key.TIMESTAMP, cpuLoadKey), Arrays.<Key<?>>asList(Key.TIMESTAMP)); - - List<CpuStat> getLatestCpuStats(HostRef ref, long since); - - void putCpuStat(CpuStat stat); - - List<CpuStat> getCpuStats(HostRef ref, long since, long to); - - CpuStat getOldest(HostRef ref); - - CpuStat getNewest(HostRef ref); - -} -
--- a/plugins/host-cpu/common/src/main/java/com/redhat/thermostat/host/cpu/common/internal/Activator.java Thu Jun 22 02:36:39 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,80 +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.cpu.common.internal; - -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 com.redhat.thermostat.host.cpu.common.CpuStatDAO; -import com.redhat.thermostat.storage.core.Storage; - -public class Activator implements BundleActivator { - - private ServiceTracker tracker; - private ServiceRegistration reg; - - @Override - public void start(BundleContext context) throws Exception { - tracker = new ServiceTracker(context, Storage.class.getName(), null) { - @Override - public Object addingService(ServiceReference reference) { - Storage storage = (Storage) context.getService(reference); - CpuStatDAO cpuStatDao = new CpuStatDAOImpl(storage); - reg = context.registerService(CpuStatDAO.class.getName(), cpuStatDao, null); - return super.addingService(reference); - } - - @Override - public void removedService(ServiceReference reference, - Object service) { - reg.unregister(); - super.removedService(reference, service); - } - }; - tracker.open(); - } - - @Override - public void stop(BundleContext context) throws Exception { - tracker.close(); - } - -} -
--- a/plugins/host-cpu/common/src/main/java/com/redhat/thermostat/host/cpu/common/internal/CpuStatCategoryRegistration.java Thu Jun 22 02:36:39 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,60 +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.cpu.common.internal; - -import java.util.HashSet; -import java.util.Set; - -import com.redhat.thermostat.storage.core.auth.CategoryRegistration; - -/** - * Registers the category used by this maven module. The web storage - * endpoint only allows categories to be registered which it knows of - * ahead of time. - * - */ -public class CpuStatCategoryRegistration implements CategoryRegistration { - - @Override - public Set<String> getCategoryNames() { - Set<String> categories = new HashSet<>(1); - categories.add(CpuStatDAOImpl.cpuStatCategory.getName()); - return categories; - } - -} -
--- a/plugins/host-cpu/common/src/main/java/com/redhat/thermostat/host/cpu/common/internal/CpuStatDAOImpl.java Thu Jun 22 02:36:39 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,124 +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.cpu.common.internal; - -import java.util.List; -import java.util.logging.Level; -import java.util.logging.Logger; - -import com.redhat.thermostat.common.utils.LoggingUtils; -import com.redhat.thermostat.host.cpu.common.CpuStatDAO; -import com.redhat.thermostat.host.cpu.common.model.CpuStat; -import com.redhat.thermostat.storage.core.DescriptorParsingException; -import com.redhat.thermostat.storage.core.HostBoundaryPojoGetter; -import com.redhat.thermostat.storage.core.HostLatestPojoListGetter; -import com.redhat.thermostat.storage.core.HostRef; -import com.redhat.thermostat.storage.core.HostTimeIntervalPojoListGetter; -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.AbstractDao; -import com.redhat.thermostat.storage.dao.AbstractDaoStatement; -import com.redhat.thermostat.storage.model.Pojo; - -class CpuStatDAOImpl extends AbstractDao implements CpuStatDAO { - - private static final Logger logger = LoggingUtils.getLogger(CpuStatDAOImpl.class); - // ADD cpu-stats SET 'agentId' = ?s , \ - // 'perProcessorUsage' = ?d[ , \ - // 'timeStamp' = ?l - static final String DESC_ADD_CPU_STAT = "ADD " + cpuStatCategory.getName() + - " SET '" + Key.AGENT_ID.getName() + "' = ?s , " + - "'" + cpuLoadKey.getName() + "' = ?d[ , " + - "'" + Key.TIMESTAMP.getName() + "' = ?l"; - - private final Storage storage; - - private final HostLatestPojoListGetter<CpuStat> latestGetter; - private final HostTimeIntervalPojoListGetter<CpuStat> intervalGetter; - private final HostBoundaryPojoGetter<CpuStat> boundaryGetter; - - CpuStatDAOImpl(Storage storage) { - this.storage = storage; - storage.registerCategory(cpuStatCategory); - this.latestGetter = new HostLatestPojoListGetter<>(storage, cpuStatCategory); - this.intervalGetter = new HostTimeIntervalPojoListGetter<>(storage, cpuStatCategory); - this.boundaryGetter = new HostBoundaryPojoGetter<>(storage, cpuStatCategory); - } - - @Override - public List<CpuStat> getLatestCpuStats(HostRef ref, long lastTimeStamp) { - return latestGetter.getLatest(ref, lastTimeStamp); - } - - @Override - public void putCpuStat(final CpuStat stat) { - executeStatement(new AbstractDaoStatement<CpuStat>(storage, cpuStatCategory, DESC_ADD_CPU_STAT) { - @Override - public PreparedStatement<CpuStat> customize(PreparedStatement<CpuStat> preparedStatement) { - preparedStatement.setString(0, stat.getAgentId()); - preparedStatement.setDoubleList(1, stat.getPerProcessorUsage()); - preparedStatement.setLong(2, stat.getTimeStamp()); - return preparedStatement; - } - }); - } - - @Override - public List<CpuStat> getCpuStats(HostRef ref, long since, long to) { - return intervalGetter.getLatest(ref, since, to); - } - - @Override - public CpuStat getOldest(HostRef ref) { - return boundaryGetter.getOldestStat(ref); - } - - @Override - public CpuStat getNewest(HostRef ref) { - return boundaryGetter.getNewestStat(ref); - } - - @Override - public Logger getLogger() { - return logger; - } - -} -
--- a/plugins/host-cpu/common/src/main/java/com/redhat/thermostat/host/cpu/common/internal/CpuStatDAOImplStatementDescriptorRegistration.java Thu Jun 22 02:36:39 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,80 +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.cpu.common.internal; - -import java.util.HashSet; -import java.util.Set; - -import com.redhat.thermostat.host.cpu.common.CpuStatDAO; -import com.redhat.thermostat.storage.core.HostBoundaryPojoGetter; -import com.redhat.thermostat.storage.core.HostLatestPojoListGetter; -import com.redhat.thermostat.storage.core.HostTimeIntervalPojoListGetter; -import com.redhat.thermostat.storage.core.auth.StatementDescriptorRegistration; - -/** - * Registers the prepared query issued by this maven module via - * {@link HostLatestPojoListGetter}. - * - */ -public class CpuStatDAOImplStatementDescriptorRegistration implements - StatementDescriptorRegistration { - - static final String LATEST = String.format( - HostLatestPojoListGetter.HOST_LATEST_QUERY_FORMAT, - CpuStatDAO.cpuStatCategory.getName()); - static final String RANGE = String.format( - HostTimeIntervalPojoListGetter.HOST_INTERVAL_QUERY_FORMAT, - CpuStatDAO.cpuStatCategory.getName()); - static final String LATEST_STAT = String.format( - HostBoundaryPojoGetter.DESC_NEWEST_HOST_STAT, - CpuStatDAO.cpuStatCategory.getName()); - static final String OLDEST_STAT = String.format( - HostBoundaryPojoGetter.DESC_OLDEST_HOST_STAT, - CpuStatDAO.cpuStatCategory.getName()); - @Override - public Set<String> getStatementDescriptors() { - Set<String> descs = new HashSet<>(5); - descs.add(LATEST); - descs.add(RANGE); - descs.add(CpuStatDAOImpl.DESC_ADD_CPU_STAT); - descs.add(LATEST_STAT); - descs.add(OLDEST_STAT); - return descs; - } - -} -
--- a/plugins/host-cpu/common/src/main/java/com/redhat/thermostat/host/cpu/common/internal/CpuStatTypeAdapter.java Thu Jun 22 02:36:39 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,201 +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.cpu.common.internal; - -import java.io.IOException; -import java.util.ArrayList; -import java.util.List; -import java.util.logging.Level; -import java.util.logging.Logger; - -import com.google.gson.stream.JsonReader; -import com.google.gson.stream.JsonWriter; -import com.google.gson.TypeAdapter; -import com.redhat.thermostat.common.utils.LoggingUtils; -import com.redhat.thermostat.host.cpu.common.model.CpuStat; - -public class CpuStatTypeAdapter extends TypeAdapter<List<CpuStat>> { - - private static final String TIMESTAMP = "timeStamp"; - private static final String PROCESSOR_USAGE = "perProcessorUsage"; - private static final String TYPE_LONG = "$numberLong"; - private static final String SERVER_TIME = "time"; - private static final String RESPONSE_ROOT = "response"; - private static final String AGENT_ID = "agentId"; - - private static final Logger logger = LoggingUtils.getLogger(CpuStatTypeAdapter.class); - - @Override - public void write(JsonWriter out, List<CpuStat> value) throws IOException { - out.beginArray(); - - for (CpuStat stat : value) { - writeCpuStat(out, stat); - } - - out.endArray(); - } - - private void writeCpuStat(JsonWriter out, CpuStat stat) throws IOException { - out.beginObject(); - out.name(PROCESSOR_USAGE); - out.beginArray(); - for (double val : stat.getPerProcessorUsage()) { - out.value(val); - } - out.endArray(); - out.name(TIMESTAMP); - writeLong(out, stat.getTimeStamp()); - out.name(AGENT_ID); - out.value(stat.getAgentId()); - out.endObject(); - } - - @Override - public List<CpuStat> read(JsonReader in) throws IOException { - List<CpuStat> values = null; - try { - in.beginObject(); - while (in.hasNext()) { - String name = in.nextName(); - switch (name) { - case RESPONSE_ROOT: - values = readResponse(in); - break; - case SERVER_TIME: - in.nextString(); - break; - default: - throw new IOException("Unexpected JSON name: " + name); - } - } - in.endObject(); - } catch (IllegalStateException e) { - throw new IOException("Reading JSON response failed."); - } - return values; - } - - private List<CpuStat> readResponse(JsonReader in) throws IOException { - List<CpuStat> values = new ArrayList<>(); - - in.beginArray(); - - while (in.hasNext()) { - try { - CpuStat stat = readCpuStat(in); - values.add(stat); - } catch(IOException | IllegalStateException | NumberFormatException e) { - logger.log(Level.WARNING, "Skipping malformed cpu stat record", e); - } - } - in.endArray(); - - return values; - } - - private CpuStat readCpuStat(JsonReader in) throws IOException { - String agentId = null; - double[] perProcessorUsage = null; - long timestamp = -1; - - in.beginObject(); - - while (in.hasNext()) { - String name = in.nextName(); - switch (name) { - case PROCESSOR_USAGE: - perProcessorUsage = readProcessorUsage(in); - break; - case TIMESTAMP: - timestamp = readLong(in); - break; - case AGENT_ID: - agentId = in.nextString(); - break; - default: - throw new IOException("Unexpected JSON name: " + name); - } - } - - in.endObject(); - - if (perProcessorUsage == null || agentId == null || timestamp < 0) { - throw new IOException("CpuStat information is incomplete."); - } - return new CpuStat(agentId, timestamp, perProcessorUsage); - } - - private double[] readProcessorUsage(JsonReader in) throws IOException { - List<Double> procUsage = new ArrayList<Double>(); - in.beginArray(); - while (in.hasNext()) { - procUsage.add(in.nextDouble()); - } - in.endArray(); - double[] result = new double[procUsage.size()]; - for (int i = 0; i < result.length; i++) { - result[i] = procUsage.get(i); - } - return result; - } - - private long readLong(JsonReader in) throws IOException { - // Read MongoDB representation of a Long - in.beginObject(); - String name = in.nextName(); - expectName(TYPE_LONG, name); - long ret = Long.valueOf(in.nextString()); - in.endObject(); - return ret; - } - - private void writeLong(JsonWriter out, long timestamp) throws IOException { - // Write MongoDB representation of a Long - out.beginObject(); - out.name(TYPE_LONG); - out.value(String.valueOf(timestamp)); - out.endObject(); - } - - private void expectName(String expected, String actual) throws IOException { - if (!expected.equals(actual)) { - throw new IOException("Expected JSON name '" + expected + "', got '" + actual + "'"); - } - } - -}
--- a/plugins/host-cpu/common/src/main/java/com/redhat/thermostat/host/cpu/common/model/CpuStat.java Thu Jun 22 02:36:39 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,83 +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.cpu.common.model; - -import com.redhat.thermostat.storage.core.Entity; -import com.redhat.thermostat.storage.core.Persist; -import com.redhat.thermostat.storage.model.BasePojo; -import com.redhat.thermostat.storage.model.TimeStampedPojo; - -@Entity -public class CpuStat extends BasePojo implements TimeStampedPojo { - - public static final double INVALID_LOAD = Double.MIN_VALUE; - - private long timeStamp; - private double[] perProcessorUsage; - - public CpuStat() { - this(null, -1, null); - } - - public CpuStat(String writerId, long timestamp, double[] perProcessorUsage) { - super(writerId); - this.timeStamp = timestamp; - this.perProcessorUsage = perProcessorUsage; - } - - @Persist - public double[] getPerProcessorUsage() { - return perProcessorUsage; - } - - @Persist - public void setPerProcessorUsage(double[] perProcessorUsage) { - this.perProcessorUsage = perProcessorUsage; - } - - @Persist - @Override - public long getTimeStamp() { - return timeStamp; - } - - @Persist - public void setTimeStamp(long timeStamp) { - this.timeStamp = timeStamp; - } -} -
--- a/plugins/host-cpu/common/src/main/resources/META-INF/services/com.redhat.thermostat.storage.core.auth.CategoryRegistration Thu Jun 22 02:36:39 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -com.redhat.thermostat.host.cpu.common.internal.CpuStatCategoryRegistration \ No newline at end of file
--- a/plugins/host-cpu/common/src/main/resources/META-INF/services/com.redhat.thermostat.storage.core.auth.StatementDescriptorRegistration Thu Jun 22 02:36:39 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -com.redhat.thermostat.host.cpu.common.internal.CpuStatDAOImplStatementDescriptorRegistration \ No newline at end of file
--- a/plugins/host-cpu/common/src/test/java/com/redhat/thermostat/host/cpu/common/internal/ActivatorTest.java Thu Jun 22 02:36:39 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,85 +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.cpu.common.internal; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; -import static org.mockito.Mockito.mock; - -import org.junit.Test; - -import com.redhat.thermostat.host.cpu.common.CpuStatDAO; -import com.redhat.thermostat.storage.core.Storage; -import com.redhat.thermostat.testutils.StubBundleContext; - -public class ActivatorTest { - - @Test - public void verifyActivatorDoesNotRegisterServiceOnMissingDeps() throws Exception { - StubBundleContext context = new StubBundleContext(); - - Activator activator = new Activator(); - - activator.start(context); - - assertEquals(0, context.getAllServices().size()); - assertEquals(1, context.getServiceListeners().size()); - - activator.stop(context); - } - - @Test - public void verifyActivatorRegistersServices() throws Exception { - StubBundleContext context = new StubBundleContext(); - Storage storage = mock(Storage.class); - - context.registerService(Storage.class, storage, null); - - Activator activator = new Activator(); - - activator.start(context); - - assertTrue(context.isServiceRegistered(CpuStatDAO.class.getName(), CpuStatDAOImpl.class)); - - activator.stop(context); - - assertEquals(0, context.getServiceListeners().size()); - assertEquals(1, context.getAllServices().size()); - } - -} -
--- a/plugins/host-cpu/common/src/test/java/com/redhat/thermostat/host/cpu/common/internal/CpuStatCategoryRegistrationTest.java Thu Jun 22 02:36:39 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,67 +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.cpu.common.internal; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - -import java.util.Set; - -import org.junit.Test; - -import com.redhat.thermostat.host.cpu.common.CpuStatDAO; -import com.redhat.thermostat.storage.core.auth.CategoryRegistration; -import com.redhat.thermostat.testutils.ServiceLoaderTest; - -public class CpuStatCategoryRegistrationTest extends ServiceLoaderTest<CategoryRegistration> { - - public CpuStatCategoryRegistrationTest() { - super(CategoryRegistration.class, STORAGE_SERVICES, CpuStatCategoryRegistration.class); - } - - @Test - public void registersAllCategories() { - CpuStatCategoryRegistration reg = new CpuStatCategoryRegistration(); - Set<String> categories = reg.getCategoryNames(); - assertEquals(1, categories.size()); - assertFalse("null descriptor not allowed", categories.contains(null)); - assertTrue(categories.contains(CpuStatDAO.cpuStatCategory.getName())); - } - -} -
--- a/plugins/host-cpu/common/src/test/java/com/redhat/thermostat/host/cpu/common/internal/CpuStatDAOImplStatementDescriptorRegistrationTest.java Thu Jun 22 02:36:39 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,65 +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.cpu.common.internal; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; - -import java.util.Set; - -import org.junit.Test; - -import com.redhat.thermostat.storage.core.auth.StatementDescriptorRegistration; -import com.redhat.thermostat.storage.internal.dao.DAOImplStatementDescriptorRegistration; -import com.redhat.thermostat.testutils.ServiceLoaderTest; - -public class CpuStatDAOImplStatementDescriptorRegistrationTest extends ServiceLoaderTest<StatementDescriptorRegistration> { - - public CpuStatDAOImplStatementDescriptorRegistrationTest() { - super(StatementDescriptorRegistration.class, STORAGE_SERVICES, DAOImplStatementDescriptorRegistration.class); - } - - @Test - public void registersAllDescriptors() { - CpuStatDAOImplStatementDescriptorRegistration reg = new CpuStatDAOImplStatementDescriptorRegistration(); - Set<String> descriptors = reg.getStatementDescriptors(); - assertEquals(5, descriptors.size()); - assertFalse("null descriptor not allowed", descriptors.contains(null)); - } - -} -
--- a/plugins/host-cpu/common/src/test/java/com/redhat/thermostat/host/cpu/common/internal/CpuStatDAOTest.java Thu Jun 22 02:36:39 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,185 +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.cpu.common.internal; - -import static org.junit.Assert.assertArrayEquals; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; -import static org.mockito.Matchers.any; -import static org.mockito.Mockito.atLeastOnce; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.verifyNoMoreInteractions; -import static org.mockito.Mockito.when; - -import java.util.Collection; -import java.util.List; - -import org.junit.Test; -import org.mockito.ArgumentCaptor; - -import com.redhat.thermostat.host.cpu.common.CpuStatDAO; -import com.redhat.thermostat.host.cpu.common.model.CpuStat; -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; - -public class CpuStatDAOTest { - - @Test - public void testStatementDescriptorsAreSane() { - String addCpuStat = "ADD cpu-stats SET 'agentId' = ?s , " + - "'perProcessorUsage' = ?d[ , " + - "'timeStamp' = ?l"; - assertEquals(addCpuStat, CpuStatDAOImpl.DESC_ADD_CPU_STAT); - } - - @Test - public void testCategory() { - assertEquals("cpu-stats", CpuStatDAO.cpuStatCategory.getName()); - Collection<Key<?>> keys = CpuStatDAO.cpuStatCategory.getKeys(); - assertTrue(keys.contains(new Key<>("agentId"))); - assertTrue(keys.contains(new Key<Long>("timeStamp"))); - assertTrue(keys.contains(new Key<Double>("perProcessorUsage"))); - - assertEquals(3, keys.size()); - } - - @Test - public void testGetLatestCpuStats() throws DescriptorParsingException, StatementExecutionException { - - @SuppressWarnings("unchecked") - Cursor<CpuStat> cursor = (Cursor<CpuStat>) mock(Cursor.class); - Storage storage = mock(Storage.class); - @SuppressWarnings("unchecked") - PreparedStatement<CpuStat> stmt = (PreparedStatement<CpuStat>) mock(PreparedStatement.class); - HostRef hostRef = mock(HostRef.class); - CpuStatDAO dao = new CpuStatDAOImpl(storage); - - Double LOAD = 5.0; - CpuStat cpuStat = new CpuStat("foo", 1234L, new double[] { LOAD }); - - when(cursor.hasNext()).thenReturn(true).thenReturn(false); - when(cursor.next()).thenReturn(cpuStat); - - when(storage.prepareStatement(anyDescriptor())).thenReturn(stmt); - when(stmt.executeQuery()).thenReturn(cursor); - when(hostRef.getAgentId()).thenReturn("system"); - - List<CpuStat> cpuStats = dao.getLatestCpuStats(hostRef, Long.MIN_VALUE); - - verify(storage).prepareStatement(anyDescriptor()); - verify(stmt).setString(0, "system"); - verify(stmt).setLong(1, Long.MIN_VALUE); - verify(stmt).executeQuery(); - verifyNoMoreInteractions(stmt); - - assertEquals(1, cpuStats.size()); - CpuStat stat = cpuStats.get(0); - assertEquals(1234L, stat.getTimeStamp()); - assertArrayEquals(new double[] { LOAD }, stat.getPerProcessorUsage(), 0.001); - - } - - @SuppressWarnings("unchecked") - private StatementDescriptor<CpuStat> anyDescriptor() { - return (StatementDescriptor<CpuStat>) any(StatementDescriptor.class); - } - - @Test - public void testGetLatestCpuStatsTwice() throws DescriptorParsingException, StatementExecutionException { - @SuppressWarnings("unchecked") - Cursor<CpuStat> cursor = (Cursor<CpuStat>) mock(Cursor.class); - Storage storage = mock(Storage.class); - @SuppressWarnings("unchecked") - PreparedStatement<CpuStat> stmt = (PreparedStatement<CpuStat>) mock(PreparedStatement.class); - HostRef hostRef = mock(HostRef.class); - - CpuStatDAO dao = new CpuStatDAOImpl(storage); - - CpuStat cpuStat = new CpuStat("foo", 1234L, new double[] { 5.0 }); - - when(cursor.hasNext()).thenReturn(true).thenReturn(false); - when(cursor.next()).thenReturn(cpuStat); - - when(storage.prepareStatement(anyDescriptor())).thenReturn(stmt); - when(stmt.executeQuery()).thenReturn(cursor); - when(hostRef.getAgentId()).thenReturn("system"); - - dao.getLatestCpuStats(hostRef, Long.MIN_VALUE); - dao.getLatestCpuStats(hostRef, Long.MIN_VALUE); - - verify(storage, atLeastOnce()).prepareStatement(anyDescriptor()); - verify(stmt, times(2)).setString(0, "system"); - verify(stmt, times(2)).setLong(1, Long.MIN_VALUE); - verify(stmt, times(2)).executeQuery(); - verifyNoMoreInteractions(stmt); - } - - @SuppressWarnings("unchecked") - @Test - public void testPutCpuStat() throws DescriptorParsingException, - StatementExecutionException { - Storage storage = mock(Storage.class); - PreparedStatement<CpuStat> add = mock(PreparedStatement.class); - when(storage.prepareStatement(any(StatementDescriptor.class))).thenReturn(add); - - CpuStat stat = new CpuStat("foo", 1, new double[] {5.0, 10.0, 15.0}); - CpuStatDAO dao = new CpuStatDAOImpl(storage); - dao.putCpuStat(stat); - - @SuppressWarnings("rawtypes") - ArgumentCaptor<StatementDescriptor> captor = ArgumentCaptor.forClass(StatementDescriptor.class); - - verify(storage).prepareStatement(captor.capture()); - StatementDescriptor<?> desc = captor.getValue(); - assertEquals(CpuStatDAOImpl.DESC_ADD_CPU_STAT, desc.getDescriptor()); - - verify(add).setString(0, stat.getAgentId()); - verify(add).setDoubleList(1, stat.getPerProcessorUsage()); - verify(add).setLong(2, stat.getTimeStamp()); - verify(add).execute(); - verifyNoMoreInteractions(add); - } -} -
--- a/plugins/host-cpu/common/src/test/java/com/redhat/thermostat/host/cpu/common/internal/CpuStatTypeAdapterTest.java Thu Jun 22 02:36:39 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,128 +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.cpu.common.internal; - -import com.redhat.thermostat.host.cpu.common.model.CpuStat; -import com.google.gson.GsonBuilder; -import com.google.gson.Gson; -import com.google.gson.reflect.TypeToken; -import org.junit.Test; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertArrayEquals; - -import java.util.List; -import java.util.ArrayList; -import java.lang.reflect.Type; - -public class CpuStatTypeAdapterTest { - - @Test - public void testTypeAdapterSerializesSingletonCorrectly() { - GsonBuilder builder = new GsonBuilder(); - Type cpuStatListType = new TypeToken<ArrayList<CpuStat>>(){}.getType(); - builder.registerTypeAdapter(cpuStatListType, new CpuStatTypeAdapter().nullSafe()); - Gson gson = builder.create(); - List<CpuStat> stats = new ArrayList<>(); - stats.add(new CpuStat("1", 1234567890l, new double[]{9.99, 8.88, 3.1415, 7.777})); - String result = gson.toJson(stats, cpuStatListType); - assertEquals("[{\"perProcessorUsage\":[9.99,8.88,3.1415,7.777],\"timeStamp\":{\"$numberLong\":\"1234567890\"},\"agentId\":\"1\"}]", result); - } - - @Test - public void testTypeAdapterSerializesListCorrectly() { - GsonBuilder builder = new GsonBuilder(); - Type cpuStatListType = new TypeToken<ArrayList<CpuStat>>(){}.getType(); - builder.registerTypeAdapter(cpuStatListType, new CpuStatTypeAdapter().nullSafe()); - Gson gson = builder.create(); - List<CpuStat> stats = new ArrayList<>(); - stats.add(new CpuStat("1", 1000230101l, new double[]{1.23, 4.56, 7.89, 10.1112})); - stats.add(new CpuStat("2", 10002333101l, new double[]{1.323, 4.456, 7.789, 10.101112})); - stats.add(new CpuStat("3", 10002320101l, new double[]{1.234, 4.567, 7.8910, 10.111213})); - stats.add(new CpuStat("4", 100023313101l, new double[]{1.3235, 4.4567, 7.78911, 10.10111241})); - String result = gson.toJson(stats, cpuStatListType); - assertEquals("[{\"perProcessorUsage\":[1.23,4.56,7.89,10.1112],\"timeStamp\":{\"$numberLong\":\"1000230101\"},\"agentId\":\"1\"}," + - "{\"perProcessorUsage\":[1.323,4.456,7.789,10.101112],\"timeStamp\":{\"$numberLong\":\"10002333101\"},\"agentId\":\"2\"}," + - "{\"perProcessorUsage\":[1.234,4.567,7.891,10.111213],\"timeStamp\":{\"$numberLong\":\"10002320101\"},\"agentId\":\"3\"}," + - "{\"perProcessorUsage\":[1.3235,4.4567,7.78911,10.10111241],\"timeStamp\":{\"$numberLong\":\"100023313101\"},\"agentId\":\"4\"}]", result); - } - - @Test - public void testTypeAdapterDeserializesSingletonCorrectly() { - GsonBuilder builder = new GsonBuilder(); - Type cpuStatListType = new TypeToken<ArrayList<CpuStat>>(){}.getType(); - builder.registerTypeAdapter(cpuStatListType, new CpuStatTypeAdapter().nullSafe()); - Gson gson = builder.create(); - String serialized = "{\"response\":[{\"perProcessorUsage\":[1.23,4.56,7.89,11.1],\"timeStamp\":{\"$numberLong\":\"12345\"},\"agentId\":\"1\"}],\"time\":\"123\"}"; - List<CpuStat> result = gson.fromJson(serialized, cpuStatListType); - CpuStat stat = result.get(0); - assertEquals(12345, stat.getTimeStamp()); - assertArrayEquals(new double[]{1.23, 4.56, 7.89, 11.1}, stat.getPerProcessorUsage(), 0.000001); - assertEquals("1", stat.getAgentId()); - } - - @Test - public void testTypeAdapterDeserializesListCorrectly() { - GsonBuilder builder = new GsonBuilder(); - Type cpuStatListType = new TypeToken<List<CpuStat>>(){}.getType(); - builder.registerTypeAdapter(cpuStatListType, new CpuStatTypeAdapter().nullSafe()); - Gson gson = builder.create(); - String serialized = "{\"response\":[" + - "{\"perProcessorUsage\":[1.23,4.56,7.89,11.1],\"timeStamp\":{\"$numberLong\":\"12345\"},\"agentId\":\"1\"}," + - "{\"perProcessorUsage\":[3.21,6.54,9.87,1.11],\"timeStamp\":{\"$numberLong\":\"54321\"},\"agentId\":\"2\"}," + - "{\"perProcessorUsage\":[3.1415,2.765,9.999,1.11234],\"timeStamp\":{\"$numberLong\":\"98765\"},\"agentId\":\"3\"}," + - "{\"perProcessorUsage\":[444.678,9000.99,1243.5654,7.897867],\"timeStamp\":{\"$numberLong\":\"56789\"},\"agentId\":\"4\"}" + - "],\"time\":\"123\"}"; - List<CpuStat> result = gson.fromJson(serialized, cpuStatListType); - CpuStat stat = result.get(0); - assertEquals(12345, stat.getTimeStamp()); - assertArrayEquals(new double[]{1.23, 4.56, 7.89, 11.1}, stat.getPerProcessorUsage(), 0.000001); - assertEquals("1", stat.getAgentId()); - stat = result.get(1); - assertEquals(54321, stat.getTimeStamp()); - assertArrayEquals(new double[]{3.21,6.54,9.87,1.11}, stat.getPerProcessorUsage(), 0.000001); - assertEquals("2", stat.getAgentId()); - stat = result.get(2); - assertEquals(98765, stat.getTimeStamp()); - assertArrayEquals(new double[]{3.1415,2.765,9.999,1.11234}, stat.getPerProcessorUsage(), 0.000001); - assertEquals("3", stat.getAgentId()); - stat = result.get(3); - assertEquals(56789, stat.getTimeStamp()); - assertArrayEquals(new double[]{444.678,9000.99,1243.5654,7.897867}, stat.getPerProcessorUsage(), 0.000001); - assertEquals("4", stat.getAgentId()); - } - -}
--- a/plugins/host-cpu/common/src/test/java/com/redhat/thermostat/host/cpu/common/model/CpuStatTest.java Thu Jun 22 02:36:39 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,48 +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.cpu.common.model; - -import com.redhat.thermostat.testutils.DataObjectTest; - -public class CpuStatTest extends DataObjectTest { - - @Override - public Class<?>[] getDataClasses() { - return new Class[] { CpuStat.class }; - } -} -
--- a/plugins/host-cpu/distribution/assemblies/plugin-assembly.xml Thu Jun 22 02:36:39 2017 -0400 +++ b/plugins/host-cpu/distribution/assemblies/plugin-assembly.xml Thu Jun 22 02:59:08 2017 -0400 @@ -43,26 +43,32 @@ <formats> <format>zip</format> </formats> - <baseDirectory>${thermostat.plugin}</baseDirectory> - <includeBaseDirectory>true</includeBaseDirectory> - + <includeBaseDirectory>false</includeBaseDirectory> + <dependencySets> <dependencySet> <includes> - <include>com.redhat.thermostat:thermostat-host-cpu-common</include> <include>com.redhat.thermostat:thermostat-host-cpu-agent</include> </includes> <useProjectArtifact>false</useProjectArtifact> <useStrictFiltering>true</useStrictFiltering> + <outputDirectory>plugins/${thermostat.plugin}</outputDirectory> </dependencySet> </dependencySets> - - <files> - <file> - <source>thermostat-plugin.xml</source> - <outputDirectory>/</outputDirectory> + + <fileSets> + <fileSet> + <includes> + <include>thermostat-plugin.xml</include> + </includes> + <outputDirectory>plugins/${thermostat.plugin}</outputDirectory> <filtered>true</filtered> - </file> - </files> + </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/host-cpu/distribution/configFiles/gateway.properties Thu Jun 22 02:59:08 2017 -0400 @@ -0,0 +1,2 @@ +# URL to the microservice provided by the Thermostat web gateway +gatewayURL=http://localhost:30000/system-cpu/0.0.1 \ No newline at end of file
--- a/plugins/host-cpu/distribution/thermostat-plugin.xml Thu Jun 22 02:36:39 2017 -0400 +++ b/plugins/host-cpu/distribution/thermostat-plugin.xml Thu Jun 22 02:59:08 2017 -0400 @@ -43,7 +43,6 @@ <extension> <name>agent</name> <bundles> - <bundle><symbolic-name>com.redhat.thermostat.host.cpu.common</symbolic-name><version>${project.version}</version></bundle> <bundle><symbolic-name>com.redhat.thermostat.host.cpu.agent</symbolic-name><version>${project.version}</version></bundle> </bundles> </extension>
--- a/plugins/host-cpu/pom.xml Thu Jun 22 02:36:39 2017 -0400 +++ b/plugins/host-cpu/pom.xml Thu Jun 22 02:59:08 2017 -0400 @@ -52,7 +52,6 @@ <modules> <module>agent</module> - <module>common</module> <module>distribution</module> </modules>
--- a/plugins/pom.xml Thu Jun 22 02:36:39 2017 -0400 +++ b/plugins/pom.xml Thu Jun 22 02:59:08 2017 -0400 @@ -54,12 +54,12 @@ <!--<module>thread</module>--> <module>killvm</module> <module>host-overview</module> + <module>host-cpu</module> + <!--module>host-memory</module--> <module>jvm-overview</module> <module>vm-gc</module> <module>vm-memory</module> <module>commands</module> - <!--<module>host-cpu</module>--> - <!--<module>host-memory</module>--> <!--<module>vm-byteman</module>--> <!--<module>vm-cpu</module>--> <!--<module>vm-classstat</module>-->