Mercurial > hg > thermostat-ng > agent
changeset 2709:b9ab00ccce2f
Add host-memory plugin
This patch restores the host-memory plugin to the agent.
It's almost identical to the host-cpu plugin, except for the builders.
Reviewed-by: sgehwolf
Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2017-June/023889.html
author | Simon Tooke <stooke@redhat.com> |
---|---|
date | Thu, 22 Jun 2017 04:49:39 -0400 |
parents | f88bf3542b62 |
children | dd67c96690d4 |
files | distribution/assembly/plugin-assembly.xml distribution/pom.xml plugins/host-memory/agent/pom.xml plugins/host-memory/agent/src/main/java/com/redhat/thermostat/host/memory/agent/internal/Activator.java plugins/host-memory/agent/src/main/java/com/redhat/thermostat/host/memory/agent/internal/HostMemoryBackend.java plugins/host-memory/agent/src/main/java/com/redhat/thermostat/host/memory/agent/internal/MemoryStatBuilder.java plugins/host-memory/agent/src/main/java/com/redhat/thermostat/host/memory/agent/internal/MemoryStatDAO.java plugins/host-memory/agent/src/main/java/com/redhat/thermostat/host/memory/agent/internal/MemoryStatDAOImpl.java plugins/host-memory/agent/src/main/java/com/redhat/thermostat/host/memory/model/MemoryStat.java plugins/host-memory/agent/src/main/java/com/redhat/thermostat/host/memory/model/MemoryStatTypeAdapter.java plugins/host-memory/agent/src/main/java/com/redhat/thermostat/host/memory/model/MemoryType.java plugins/host-memory/agent/src/test/java/com/redhat/thermostat/host/memory/agent/internal/ActivatorTest.java plugins/host-memory/agent/src/test/java/com/redhat/thermostat/host/memory/agent/internal/HostMemoryBackendTest.java plugins/host-memory/agent/src/test/java/com/redhat/thermostat/host/memory/agent/internal/MemoryStatBuilderTest.java plugins/host-memory/agent/src/test/java/com/redhat/thermostat/host/memory/agent/internal/MemoryStatDAOTest.java plugins/host-memory/agent/src/test/java/com/redhat/thermostat/host/memory/model/MemoryStatTest.java plugins/host-memory/agent/src/test/java/com/redhat/thermostat/host/memory/model/MemoryStatTypeAdapterTest.java plugins/host-memory/common/pom.xml plugins/host-memory/common/src/main/java/com/redhat/thermostat/host/memory/common/MemoryStatDAO.java plugins/host-memory/common/src/main/java/com/redhat/thermostat/host/memory/common/internal/Activator.java plugins/host-memory/common/src/main/java/com/redhat/thermostat/host/memory/common/internal/MemoryStatCategoryRegistration.java plugins/host-memory/common/src/main/java/com/redhat/thermostat/host/memory/common/internal/MemoryStatDAOImpl.java plugins/host-memory/common/src/main/java/com/redhat/thermostat/host/memory/common/internal/MemoryStatDAOImplStatementDescriptorRegistration.java plugins/host-memory/common/src/main/java/com/redhat/thermostat/host/memory/common/internal/MemoryStatTypeAdapter.java plugins/host-memory/common/src/main/java/com/redhat/thermostat/host/memory/common/model/MemoryStat.java plugins/host-memory/common/src/main/java/com/redhat/thermostat/host/memory/common/model/MemoryType.java plugins/host-memory/common/src/main/resources/META-INF/services/com.redhat.thermostat.storage.core.auth.CategoryRegistration plugins/host-memory/common/src/main/resources/META-INF/services/com.redhat.thermostat.storage.core.auth.StatementDescriptorRegistration plugins/host-memory/common/src/test/java/com/redhat/thermostat/host/memory/common/internal/ActivatorTest.java plugins/host-memory/common/src/test/java/com/redhat/thermostat/host/memory/common/internal/MemoryStatCategoryRegistrationTest.java plugins/host-memory/common/src/test/java/com/redhat/thermostat/host/memory/common/internal/MemoryStatDAOImplStatementDescriptorRegistrationTest.java plugins/host-memory/common/src/test/java/com/redhat/thermostat/host/memory/common/internal/MemoryStatDAOTest.java plugins/host-memory/common/src/test/java/com/redhat/thermostat/host/memory/common/internal/MemoryStatTypeAdapterTest.java plugins/host-memory/common/src/test/java/com/redhat/thermostat/host/memory/common/model/MemoryStatTest.java plugins/host-memory/distribution/assemblies/plugin-assembly.xml plugins/host-memory/distribution/configFiles/gateway.properties plugins/host-memory/distribution/thermostat-plugin.xml plugins/host-memory/pom.xml plugins/pom.xml |
diffstat | 39 files changed, 911 insertions(+), 1680 deletions(-) [+] |
line wrap: on
line diff
--- a/distribution/assembly/plugin-assembly.xml Thu Jun 22 02:59:08 2017 -0400 +++ b/distribution/assembly/plugin-assembly.xml Thu Jun 22 04:49:39 2017 -0400 @@ -51,7 +51,7 @@ <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-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>
--- a/distribution/pom.xml Thu Jun 22 02:59:08 2017 -0400 +++ b/distribution/pom.xml Thu Jun 22 04:49:39 2017 -0400 @@ -488,12 +488,12 @@ <version>${project.version}</version> <type>zip</type> </dependency> - <!--dependency> + <dependency> <groupId>com.redhat.thermostat</groupId> <artifactId>thermostat-host-memory-distribution</artifactId> <version>${project.version}</version> <type>zip</type> - </dependency--> + </dependency> <dependency> <groupId>com.redhat.thermostat</groupId> <artifactId>thermostat-jvm-overview-distribution</artifactId> @@ -526,12 +526,6 @@ </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-numa-distribution</artifactId>--> <!--<version>${project.version}</version>--> <!--<type>zip</type>-->
--- a/plugins/host-memory/agent/pom.xml Thu Jun 22 02:59:08 2017 -0400 +++ b/plugins/host-memory/agent/pom.xml Thu Jun 22 04:49:39 2017 -0400 @@ -60,8 +60,7 @@ <instructions> <Bundle-Vendor>Red Hat, Inc.</Bundle-Vendor> <Bundle-SymbolicName>com.redhat.thermostat.host.memory.agent</Bundle-SymbolicName> - <Bundle-Activator>com.redhat.thermostat.host.memory.agent.internal.Activator</Bundle-Activator> - <Export-Package /> + <Export-Package>com.redhat.thermostat.host.memory.model</Export-Package> <Private-Package> com.redhat.thermostat.host.memory.agent.internal </Private-Package> @@ -72,6 +71,19 @@ </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> <artifactId>maven-resources-plugin</artifactId> <executions> @@ -138,12 +150,12 @@ </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-memory-common</artifactId> + <artifactId>thermostat-common-portability</artifactId> <version>${project.version}</version> </dependency> <dependency>
--- a/plugins/host-memory/agent/src/main/java/com/redhat/thermostat/host/memory/agent/internal/Activator.java Thu Jun 22 02:59:08 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.memory.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.memory.common.MemoryStatDAO; -import com.redhat.thermostat.storage.core.WriterID; - -public class Activator implements BundleActivator { - - private ScheduledExecutorService executor; - private MultipleServiceTracker tracker; - private HostMemoryBackend backend; - private ServiceRegistration<?> reg; - - @Override - public void start(final BundleContext context) throws Exception { - executor = Executors.newSingleThreadScheduledExecutor(); - - Class<?>[] deps = new Class<?>[] { - BackendService.class, - MemoryStatDAO.class, - WriterID.class, // host memory backen uses it - }; - tracker = new MultipleServiceTracker(context, deps, new Action() { - - @Override - public void dependenciesAvailable(DependencyProvider services) { - MemoryStatDAO memoryStatDao = services.get(MemoryStatDAO.class); - Version version = new Version(context.getBundle()); - WriterID id = services.get(WriterID.class); - backend = new HostMemoryBackend(executor, memoryStatDao, version, id); - reg = context.registerService(Backend.class.getName(), 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. - */ - HostMemoryBackend getBackend() { - return backend; - } -} -
--- a/plugins/host-memory/agent/src/main/java/com/redhat/thermostat/host/memory/agent/internal/HostMemoryBackend.java Thu Jun 22 02:59:08 2017 -0400 +++ b/plugins/host-memory/agent/src/main/java/com/redhat/thermostat/host/memory/agent/internal/HostMemoryBackend.java Thu Jun 22 04:49:39 2017 -0400 @@ -36,23 +36,58 @@ package com.redhat.thermostat.host.memory.agent.internal; +import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; +import com.redhat.thermostat.backend.Backend; +import com.redhat.thermostat.common.portability.HostName; import com.redhat.thermostat.common.portability.linux.ProcDataSource; import com.redhat.thermostat.backend.HostPollingAction; import com.redhat.thermostat.backend.HostPollingBackend; import com.redhat.thermostat.common.Version; -import com.redhat.thermostat.host.memory.common.MemoryStatDAO; 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 HostMemoryBackend extends HostPollingBackend { - public HostMemoryBackend(ScheduledExecutorService executor, MemoryStatDAO memoryStatDAO, Version version, final WriterID writerId) { - super("Host Memory Backend", - "Gathers memory statistics about a host", - "Red Hat, Inc.", - version, executor); - registerAction(new MemoryProcBackendAction(writerId, memoryStatDAO)); + @Reference + private MemoryStatDAO memoryStatDAO; + + @Reference + private WriterID writerID; + + public HostMemoryBackend() { + this(Executors.newSingleThreadScheduledExecutor()); + } + + public HostMemoryBackend(ScheduledExecutorService executor) { + this("Host Memory Backend", "Gathers memory statistics about a host", "Red Hat, Inc.", new Version(), executor); + } + + public HostMemoryBackend(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 MemoryProcBackendAction(writerID, memoryStatDAO)); + } + + @Deactivate + protected void componentDeactivated() { + if (isActive()) { + deactivate(); + } } private static class MemoryProcBackendAction implements HostPollingAction { @@ -68,11 +103,19 @@ @Override public void run() { - dao.putMemoryStat(builder.build()); + dao.put(builder.build()); } } + void bindMemoryStatDAO(MemoryStatDAO dao) { + this.memoryStatDAO = dao; + } + + void bindWriterID(WriterID id) { + this.writerID = id; + } + @Override public int getOrderValue() { return ORDER_MEMORY_GROUP;
--- a/plugins/host-memory/agent/src/main/java/com/redhat/thermostat/host/memory/agent/internal/MemoryStatBuilder.java Thu Jun 22 02:59:08 2017 -0400 +++ b/plugins/host-memory/agent/src/main/java/com/redhat/thermostat/host/memory/agent/internal/MemoryStatBuilder.java Thu Jun 22 04:49:39 2017 -0400 @@ -47,7 +47,7 @@ import com.redhat.thermostat.common.Size; import com.redhat.thermostat.common.portability.PortableMemoryStat; import com.redhat.thermostat.common.utils.LoggingUtils; -import com.redhat.thermostat.host.memory.common.model.MemoryStat; +import com.redhat.thermostat.host.memory.model.MemoryStat; import com.redhat.thermostat.shared.config.OS; import com.redhat.thermostat.storage.core.WriterID;
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/plugins/host-memory/agent/src/main/java/com/redhat/thermostat/host/memory/agent/internal/MemoryStatDAO.java Thu Jun 22 04:49:39 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.memory.agent.internal; + +import com.redhat.thermostat.annotations.Service; +import com.redhat.thermostat.host.memory.model.MemoryStat; + +@Service +public interface MemoryStatDAO { + + void put(MemoryStat stat); + +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/plugins/host-memory/agent/src/main/java/com/redhat/thermostat/host/memory/agent/internal/MemoryStatDAOImpl.java Thu Jun 22 04:49:39 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.memory.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.PluginDAOBase; +import com.redhat.thermostat.common.plugin.SystemID; +import com.redhat.thermostat.common.utils.LoggingUtils; +import com.redhat.thermostat.host.memory.model.MemoryStat; +import com.redhat.thermostat.host.memory.model.MemoryStatTypeAdapter; + +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; + +@Component +@Service(value = MemoryStatDAO.class) +public class MemoryStatDAOImpl extends PluginDAOBase<MemoryStat, MemoryStatDAOImpl> implements MemoryStatDAO { + + private static final Logger logger = LoggingUtils.getLogger(MemoryStatDAOImpl.class); + + public static final String PLUGIN_ID = "host-memory"; + + private final JsonHelper jsonHelper; + private final ConfigurationCreator configCreator; + private PluginConfiguration config; + + @Reference + private ConfigurationInfoSource configurationInfoSource; + + @Reference + private SystemID systemID; + + @Reference + private HttpRequestService httpRequestService; + + public MemoryStatDAOImpl() { + this(new JsonHelper(new MemoryStatTypeAdapter()), new ConfigurationCreator()); + } + + MemoryStatDAOImpl(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 PluginConfiguration getConfig() { + return config; + } + + @Override + protected HttpRequestService getHttpRequestService() { + return httpRequestService; + } + + @Override + protected String toJsonString(MemoryStat obj) throws IOException { + return jsonHelper.toJson(Arrays.asList(obj)); + } + + @Override + protected String getURL(String basepath) { + return basepath + "/systems/" + systemID.getSystemID(); + } + + // DS bind methods + 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 MemoryStatTypeAdapter typeAdapter; + + JsonHelper(MemoryStatTypeAdapter typeAdapter) { + this.typeAdapter = typeAdapter; + } + + String toJson(List<MemoryStat> infos) throws IOException { + return typeAdapter.toJson(infos); + } + } + + // For Testing purposes + static class ConfigurationCreator { + + PluginConfiguration create(ConfigurationInfoSource source) { + return new PluginConfiguration(source, PLUGIN_ID); + } + } +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/plugins/host-memory/agent/src/main/java/com/redhat/thermostat/host/memory/model/MemoryStat.java Thu Jun 22 04:49:39 2017 -0400 @@ -0,0 +1,154 @@ +/* + * 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.memory.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 MemoryStat extends BasePojo implements TimeStampedPojo { + + private long timeStamp; + private long total; + private long free; + private long buffers; + private long cached; + private long swapTotal; + private long swapFree; + private long commitLimit; + + public MemoryStat() { + super(null); + } + + public MemoryStat(String writerId, long timeStamp, long total, long free, long buffers, long cached, long swapTotal, long swapFree, long commitLimit) { + super(writerId); + this.timeStamp = timeStamp; + this.total = total; + this.free = free; + this.buffers = buffers; + this.cached = cached; + this.swapTotal = swapTotal; + this.swapFree = swapFree; + this.commitLimit = commitLimit; + } + + @Override + @Persist + public long getTimeStamp() { + return timeStamp; + } + + @Persist + public void setTimeStamp(long timeStamp) { + this.timeStamp = timeStamp; + } + + @Persist + public long getTotal() { + return total; + } + + @Persist + public void setTotal(long total) { + this.total = total; + } + + @Persist + public long getFree() { + return free; + } + + @Persist + public void setFree(long free) { + this.free = free; + } + + @Persist + public long getBuffers() { + return buffers; + } + + @Persist + public void setBuffers(long buffers) { + this.buffers = buffers; + } + + @Persist + public long getCached() { + return cached; + } + + @Persist + public void setCached(long cached) { + this.cached = cached; + } + + @Persist + public long getSwapTotal() { + return swapTotal; + } + + @Persist + public void setSwapTotal(long swapTotal) { + this.swapTotal = swapTotal; + } + + @Persist + public long getSwapFree() { + return swapFree; + } + + @Persist + public void setSwapFree(long swapFree) { + this.swapFree = swapFree; + } + + @Persist + public long getCommitLimit() { + return commitLimit; + } + + @Persist + public void setCommitLimit(long commitLimit) { + this.commitLimit = commitLimit; + } + +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/plugins/host-memory/agent/src/main/java/com/redhat/thermostat/host/memory/model/MemoryStatTypeAdapter.java Thu Jun 22 04:49:39 2017 -0400 @@ -0,0 +1,105 @@ +/* + * 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.memory.model; + +import com.google.gson.TypeAdapter; +import com.google.gson.stream.JsonReader; +import com.google.gson.stream.JsonWriter; + +import java.io.IOException; +import java.util.List; + +public class MemoryStatTypeAdapter extends TypeAdapter<List<MemoryStat>> { + + private static final String TYPE_LONG = "$numberLong"; + private static final String TIMESTAMP = "timeStamp"; + private static final String TOTAL = "total"; + private static final String FREE = "free"; + private static final String BUFFERS = "buffers"; + private static final String CACHED = "cached"; + private static final String SWAP_TOTAL = "swapTotal"; + private static final String SWAP_FREE = "swapFree"; + private static final String COMMIT_LIMIT = "commitLimit"; + private static final String AGENT_ID = "agentId"; + + + public void write(JsonWriter out, List<MemoryStat> stats) throws IOException { + out.beginArray(); + + for (MemoryStat stat : stats) { + writeMemoryStat(out, stat); + } + + out.endArray(); + } + + public void writeMemoryStat(JsonWriter out, MemoryStat stats) throws IOException { + out.beginObject(); + out.name(TIMESTAMP); + writeLong(out, stats.getTimeStamp()); + out.name(TOTAL); + writeLong(out, stats.getTotal()); + out.name(FREE); + writeLong(out, stats.getFree()); + out.name(BUFFERS); + writeLong(out, stats.getBuffers()); + out.name(CACHED); + writeLong(out, stats.getCached()); + out.name(SWAP_TOTAL); + writeLong(out, stats.getSwapTotal()); + out.name(SWAP_FREE); + writeLong(out, stats.getSwapFree()); + out.name(COMMIT_LIMIT); + writeLong(out, stats.getCommitLimit()); + out.name(AGENT_ID); + out.value(stats.getAgentId()); + out.endObject(); + } + + public void writeLong(JsonWriter out, long input) throws IOException { + // Write MongoDB representation of a Long + out.beginObject(); + out.name(TYPE_LONG); + out.value(String.valueOf(input)); + out.endObject(); + } + + public List<MemoryStat> read(JsonReader in) throws IOException { + return null; + } + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/plugins/host-memory/agent/src/main/java/com/redhat/thermostat/host/memory/model/MemoryType.java Thu Jun 22 04:49:39 2017 -0400 @@ -0,0 +1,47 @@ +/* + * 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.memory.model; + +public enum MemoryType { + MEMORY_TOTAL, + MEMORY_FREE, + MEMORY_USED, + SWAP_TOTAL, + SWAP_FREE, + BUFFERS; +} +
--- a/plugins/host-memory/agent/src/test/java/com/redhat/thermostat/host/memory/agent/internal/ActivatorTest.java Thu Jun 22 02:59:08 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.memory.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.memory.common.MemoryStatDAO; -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); - MemoryStatDAO cpuStatDAO = mock(MemoryStatDAO.class); - WriterID writerService = mock(WriterID.class); - - context.registerService(BackendService.class, service, null); - context.registerService(MemoryStatDAO.class, cpuStatDAO, null); - context.registerService(WriterID.class, writerService, null); - - Activator activator = new Activator(); - - activator.start(context); - - assertTrue(context.isServiceRegistered(Backend.class.getName(), HostMemoryBackend.class)); - HostMemoryBackend backend = activator.getBackend(); - assertNotNull(backend); - - activator.stop(context); - - assertFalse(backend.isActive()); - - assertEquals(0, context.getServiceListeners().size()); - assertEquals(3, context.getAllServices().size()); - } - -} -
--- a/plugins/host-memory/agent/src/test/java/com/redhat/thermostat/host/memory/agent/internal/HostMemoryBackendTest.java Thu Jun 22 02:59:08 2017 -0400 +++ b/plugins/host-memory/agent/src/test/java/com/redhat/thermostat/host/memory/agent/internal/HostMemoryBackendTest.java Thu Jun 22 04:49:39 2017 -0400 @@ -40,6 +40,7 @@ import static org.junit.Assert.assertTrue; import static org.mockito.Matchers.any; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -51,46 +52,59 @@ import org.mockito.ArgumentCaptor; import com.redhat.thermostat.common.Version; -import com.redhat.thermostat.host.memory.common.MemoryStatDAO; -import com.redhat.thermostat.host.memory.common.model.MemoryStat; +import com.redhat.thermostat.host.memory.model.MemoryStat; import com.redhat.thermostat.storage.core.WriterID; +import org.osgi.framework.Bundle; +import org.osgi.framework.BundleContext; public class HostMemoryBackendTest { - + private HostMemoryBackend backend; private ScheduledExecutorService executor; - private MemoryStatDAO memoryStatDao; + private MemoryStatDAO memoryStatDAO; + private WriterID writerID; + private Version version; @Before public void setup() { executor = mock(ScheduledExecutorService.class); - memoryStatDao = mock(MemoryStatDAO.class); - - Version version = mock(Version.class); + + version = mock(Version.class); when(version.getVersionNumber()).thenReturn("0.0.0"); - WriterID id = mock(WriterID.class); - - backend = new HostMemoryBackend(executor, memoryStatDao, version, id); + + // these two are created via OSGI wiring + memoryStatDAO = mock(MemoryStatDAO.class); + writerID = mock(WriterID.class); + + backend = new HostMemoryBackend("Host Memory Backend", "Gathers memory statistics about a host", "Red Hat, Inc.", version, executor); + backend.bindMemoryStatDAO(memoryStatDAO); + backend.bindWriterID(writerID); } @Test - public void testStart() { + 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(memoryStatDao).putMemoryStat(any(MemoryStat.class)); + verify(memoryStatDAO).put(any(MemoryStat.class)); } - + @Test - public void testStop() { + public void testDeactivate() { backend.activate(); backend.deactivate(); verify(executor).shutdown(); assertFalse(backend.isActive()); } } -
--- a/plugins/host-memory/agent/src/test/java/com/redhat/thermostat/host/memory/agent/internal/MemoryStatBuilderTest.java Thu Jun 22 02:59:08 2017 -0400 +++ b/plugins/host-memory/agent/src/test/java/com/redhat/thermostat/host/memory/agent/internal/MemoryStatBuilderTest.java Thu Jun 22 04:49:39 2017 -0400 @@ -52,7 +52,7 @@ import org.junit.Test; import com.redhat.thermostat.common.portability.linux.ProcDataSource; -import com.redhat.thermostat.host.memory.common.model.MemoryStat; +import com.redhat.thermostat.host.memory.model.MemoryStat; import com.redhat.thermostat.storage.core.WriterID; public class MemoryStatBuilderTest {
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/plugins/host-memory/agent/src/test/java/com/redhat/thermostat/host/memory/agent/internal/MemoryStatDAOTest.java Thu Jun 22 04:49:39 2017 -0400 @@ -0,0 +1,131 @@ +/* + * 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.memory.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.SystemID; +import com.redhat.thermostat.common.plugin.PluginConfiguration; + +import com.redhat.thermostat.host.memory.model.MemoryStat; +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; + +import org.mockito.Matchers; + +public class MemoryStatDAOTest { + + private static final String URL = "http://localhost:26000/api/system-memory/0.0.1"; + private static final String SOME_JSON = "{\"some\" : \"json\"}"; + private static final String CONTENT_TYPE = "application/json"; + private static final String HOST_NAME = "somehostname"; + + private static final String URL_PROP = "gatewayURL"; + + private MemoryStat info; + private MemoryStatDAOImpl.JsonHelper jsonHelper; + private ConfigurationInfoSource cfiSource; + private MemoryStatDAOImpl.ConfigurationCreator configCreator; + private SystemID idservice; + private HttpRequestService httpRequestService; + + @Before + public void setup() throws Exception { + Clock clock = new SystemClock(); + this.info = new MemoryStat("foo-agent", clock.getRealTimeMillis(), 0, 0, 0, 0, 0, 0, 0); + + this.jsonHelper = mock(MemoryStatDAOImpl.JsonHelper.class); + when(jsonHelper.toJson(anyListOf(MemoryStat.class))).thenReturn(SOME_JSON); + + 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); + + cfiSource = mock(ConfigurationInfoSource.class); + Map<String, String> map = new HashMap<>(); + map.put(URL_PROP, URL); + when(cfiSource.getConfiguration(anyString(), anyString())).thenReturn(map); + + 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); + + configCreator = mock(MemoryStatDAOImpl.ConfigurationCreator.class); + when(configCreator.create(eq(cfiSource))).thenReturn(new PluginConfiguration(cfiSource, MemoryStatDAOImpl.PLUGIN_ID)); + + idservice = mock(SystemID.class); + when(idservice.getSystemID()).thenReturn(HOST_NAME); + } + + @Test + public void testPut() throws Exception { + + MemoryStatDAOImpl dao = new MemoryStatDAOImpl(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); + } +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/plugins/host-memory/agent/src/test/java/com/redhat/thermostat/host/memory/model/MemoryStatTest.java Thu Jun 22 04:49:39 2017 -0400 @@ -0,0 +1,49 @@ +/* + * 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.memory.model; + +import com.redhat.thermostat.testutils.DataObjectTest; + +public class MemoryStatTest extends DataObjectTest { + + @Override + public Class<?>[] getDataClasses() { + return new Class[] { MemoryStat.class }; + } + +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/plugins/host-memory/agent/src/test/java/com/redhat/thermostat/host/memory/model/MemoryStatTypeAdapterTest.java Thu Jun 22 04:49:39 2017 -0400 @@ -0,0 +1,95 @@ +/* + * 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.memory.model; + +import com.google.gson.GsonBuilder; +import com.google.gson.Gson; +import com.google.gson.reflect.TypeToken; +import org.junit.Test; + +import java.lang.reflect.Type; +import java.util.ArrayList; +import java.util.List; + +import static org.junit.Assert.assertEquals; + +public class MemoryStatTypeAdapterTest { + + @Test + public void testMemoryStatGetsSerializedCorrectly() { + GsonBuilder builder = new GsonBuilder(); + Type memoryStatListType = new TypeToken<ArrayList<MemoryStat>>(){}.getType(); + builder.registerTypeAdapter(memoryStatListType, new MemoryStatTypeAdapter().nullSafe()); + Gson gson = builder.create(); + List<MemoryStat> stats = new ArrayList<>(); + stats.add(new MemoryStat("1", 123l, 456l, 789l, 101112l, 131415l, + 161718l, 192021l, 222324l)); + System.out.println(gson.toJson(stats, memoryStatListType)); + assertEquals("[{\"timeStamp\":{\"$numberLong\":\"123\"}," + + "\"total\":{\"$numberLong\":\"456\"}," + + "\"free\":{\"$numberLong\":\"789\"}," + + "\"buffers\":{\"$numberLong\":\"101112\"}," + + "\"cached\":{\"$numberLong\":\"131415\"}," + + "\"swapTotal\":{\"$numberLong\":\"161718\"}," + + "\"swapFree\":{\"$numberLong\":\"192021\"}," + + "\"commitLimit\":{\"$numberLong\":\"222324\"}," + + "\"agentId\":\"1\"}]", + gson.toJson(stats, memoryStatListType)); + } + + @Test + public void testMultipleMemoryStatsGetSerializedCorrectly() { + GsonBuilder builder = new GsonBuilder(); + Type memoryStatListType = new TypeToken<ArrayList<MemoryStat>>(){}.getType(); + builder.registerTypeAdapter(memoryStatListType, new MemoryStatTypeAdapter().nullSafe()); + Gson gson = builder.create(); + List<MemoryStat> stats = new ArrayList<>(); + stats.add(new MemoryStat("1", 123l, 456l, 789l, 101112l, 131415l, + 161718l, 192021l, 222324l)); + stats.add(new MemoryStat("2", 1l, 2l, 3l, 4l, 5l,6l, 7l, 8l)); + stats.add(new MemoryStat("3", 17756l, 25365323l, 3124213l, 4465434l, 578687l, + 689821l, 786711l, 823542l)); + stats.add(new MemoryStat("4", 13332l, 24441l, 37721l, 4321345l, 542131l, + 64522l, 71232l, 8231321l)); + assertEquals("[{\"timeStamp\":{\"$numberLong\":\"123\"},\"total\":{\"$numberLong\":\"456\"},\"free\":{\"$numberLong\":\"789\"},\"buffers\":{\"$numberLong\":\"101112\"},\"cached\":{\"$numberLong\":\"131415\"},\"swapTotal\":{\"$numberLong\":\"161718\"},\"swapFree\":{\"$numberLong\":\"192021\"},\"commitLimit\":{\"$numberLong\":\"222324\"},\"agentId\":\"1\"}," + + "{\"timeStamp\":{\"$numberLong\":\"1\"},\"total\":{\"$numberLong\":\"2\"},\"free\":{\"$numberLong\":\"3\"},\"buffers\":{\"$numberLong\":\"4\"},\"cached\":{\"$numberLong\":\"5\"},\"swapTotal\":{\"$numberLong\":\"6\"},\"swapFree\":{\"$numberLong\":\"7\"},\"commitLimit\":{\"$numberLong\":\"8\"},\"agentId\":\"2\"}," + + "{\"timeStamp\":{\"$numberLong\":\"17756\"},\"total\":{\"$numberLong\":\"25365323\"},\"free\":{\"$numberLong\":\"3124213\"},\"buffers\":{\"$numberLong\":\"4465434\"},\"cached\":{\"$numberLong\":\"578687\"},\"swapTotal\":{\"$numberLong\":\"689821\"},\"swapFree\":{\"$numberLong\":\"786711\"},\"commitLimit\":{\"$numberLong\":\"823542\"},\"agentId\":\"3\"}," + + "{\"timeStamp\":{\"$numberLong\":\"13332\"},\"total\":{\"$numberLong\":\"24441\"},\"free\":{\"$numberLong\":\"37721\"},\"buffers\":{\"$numberLong\":\"4321345\"},\"cached\":{\"$numberLong\":\"542131\"},\"swapTotal\":{\"$numberLong\":\"64522\"},\"swapFree\":{\"$numberLong\":\"71232\"},\"commitLimit\":{\"$numberLong\":\"8231321\"},\"agentId\":\"4\"}]", + gson.toJson(stats, memoryStatListType)); + } + +}
--- a/plugins/host-memory/common/pom.xml Thu Jun 22 02:59:08 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-memory</artifactId> - <groupId>com.redhat.thermostat</groupId> - <version>1.99.12-SNAPSHOT</version> - </parent> - <artifactId>thermostat-host-memory-common</artifactId> - <packaging>bundle</packaging> - <name>Thermostat Host Memory 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.memory.common</Bundle-SymbolicName> - <Bundle-Activator>com.redhat.thermostat.host.memory.common.internal.Activator</Bundle-Activator> - <Export-Package> - com.redhat.thermostat.host.memory.common, - com.redhat.thermostat.host.memory.common.model - </Export-Package> - <Private-Package> - com.redhat.thermostat.host.memory.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-memory/common/src/main/java/com/redhat/thermostat/host/memory/common/MemoryStatDAO.java Thu Jun 22 02:59:08 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,74 +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.memory.common; - -import java.util.Arrays; -import java.util.List; - -import com.redhat.thermostat.annotations.Service; -import com.redhat.thermostat.host.memory.common.model.MemoryStat; -import com.redhat.thermostat.storage.core.Category; -import com.redhat.thermostat.storage.core.HostRef; -import com.redhat.thermostat.storage.core.Key; - -@Service -public interface MemoryStatDAO { - - static Key<Long> memoryTotalKey = new Key<>("total"); - static Key<Long> memoryFreeKey = new Key<>("free"); - static Key<Long> memoryBuffersKey = new Key<>("buffers"); - static Key<Long> memoryCachedKey = new Key<>("cached"); - static Key<Long> memorySwapTotalKey = new Key<>("swapTotal"); - static Key<Long> memorySwapFreeKey = new Key<>("swapFree"); - static Key<Long> memoryCommitLimitKey = new Key<>("commitLimit"); - - static final Category<MemoryStat> memoryStatCategory = new Category<>("memory-stats", MemoryStat.class, - Arrays.<Key<?>>asList(Key.AGENT_ID, Key.TIMESTAMP, memoryTotalKey, memoryFreeKey, memoryBuffersKey, - memoryCachedKey, memorySwapTotalKey, memorySwapFreeKey, memoryCommitLimitKey), - Arrays.<Key<?>>asList(Key.TIMESTAMP)); - - public List<MemoryStat> getLatestMemoryStats(HostRef ref, long since); - - public List<MemoryStat> getMemoryStats(HostRef ref, long since, long to); - - public MemoryStat getNewest(HostRef ref); - - public MemoryStat getOldest(HostRef ref); - - void putMemoryStat(MemoryStat stat); -} -
--- a/plugins/host-memory/common/src/main/java/com/redhat/thermostat/host/memory/common/internal/Activator.java Thu Jun 22 02:59:08 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.memory.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.memory.common.MemoryStatDAO; -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); - MemoryStatDAO memoryStatDao = new MemoryStatDAOImpl(storage); - reg = context.registerService(MemoryStatDAO.class.getName(), memoryStatDao, 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-memory/common/src/main/java/com/redhat/thermostat/host/memory/common/internal/MemoryStatCategoryRegistration.java Thu Jun 22 02:59:08 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.memory.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 MemoryStatCategoryRegistration implements CategoryRegistration { - - @Override - public Set<String> getCategoryNames() { - Set<String> categories = new HashSet<>(1); - categories.add(MemoryStatDAOImpl.memoryStatCategory.getName()); - return categories; - } - -} -
--- a/plugins/host-memory/common/src/main/java/com/redhat/thermostat/host/memory/common/internal/MemoryStatDAOImpl.java Thu Jun 22 02:59:08 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,136 +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.memory.common.internal; - -import java.util.List; -import java.util.logging.Logger; - -import com.redhat.thermostat.common.utils.LoggingUtils; -import com.redhat.thermostat.host.memory.common.MemoryStatDAO; -import com.redhat.thermostat.host.memory.common.model.MemoryStat; -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.Storage; -import com.redhat.thermostat.storage.dao.AbstractDao; -import com.redhat.thermostat.storage.dao.AbstractDaoStatement; - -public class MemoryStatDAOImpl extends AbstractDao implements MemoryStatDAO { - - private static final Logger logger = LoggingUtils.getLogger(MemoryStatDAOImpl.class); - // ADD memory-stats SET 'agentId' = ?s , \ - // 'timeStamp' = ?l , \ - // 'total' = ?l , \ - // 'free' = ?l , \ - // 'buffers' = ?l , \ - // 'cached' = ?l , \ - // 'swapTotal' = ?l , \ - // 'swapFree' = ?l , \ - // 'commitLimit' = ?l - static final String DESC_ADD_MEMORY_STAT = "ADD " + memoryStatCategory.getName() + - " SET '" + Key.AGENT_ID.getName() + "' = ?s , " + - "'" + Key.TIMESTAMP.getName() + "' = ?l , " + - "'" + memoryTotalKey.getName() + "' = ?l , " + - "'" + memoryFreeKey.getName() + "' = ?l , " + - "'" + memoryBuffersKey.getName() + "' = ?l , " + - "'" + memoryCachedKey.getName() + "' = ?l , " + - "'" + memorySwapTotalKey.getName() + "' = ?l , " + - "'" + memorySwapFreeKey.getName() + "' = ?l , " + - "'" + memoryCommitLimitKey.getName() + "' = ?l"; - - private final Storage storage; - - private final HostLatestPojoListGetter<MemoryStat> latestGetter; - private final HostTimeIntervalPojoListGetter<MemoryStat> intervalGetter; - private final HostBoundaryPojoGetter<MemoryStat> boundaryGetter; - - MemoryStatDAOImpl(Storage storage) { - this.storage = storage; - storage.registerCategory(memoryStatCategory); - this.latestGetter = new HostLatestPojoListGetter<>(storage, memoryStatCategory); - this.intervalGetter = new HostTimeIntervalPojoListGetter<>(storage, memoryStatCategory); - this.boundaryGetter = new HostBoundaryPojoGetter<>(storage, memoryStatCategory); - } - - @Override - public List<MemoryStat> getLatestMemoryStats(HostRef ref, long lastTimeStamp) { - return latestGetter.getLatest(ref, lastTimeStamp); - } - - @Override - public List<MemoryStat> getMemoryStats(HostRef ref, long since, long to) { - return intervalGetter.getLatest(ref, since, to); - } - - @Override - public MemoryStat getNewest(HostRef ref) { - return boundaryGetter.getNewestStat(ref); - } - - @Override - public MemoryStat getOldest(HostRef ref) { - return boundaryGetter.getOldestStat(ref); - } - - @Override - public void putMemoryStat(final MemoryStat stat) { - executeStatement(new AbstractDaoStatement<MemoryStat>(storage, memoryStatCategory, DESC_ADD_MEMORY_STAT) { - @Override - public PreparedStatement<MemoryStat> customize(PreparedStatement<MemoryStat> preparedStatement) { - preparedStatement.setString(0, stat.getAgentId()); - preparedStatement.setLong(1, stat.getTimeStamp()); - preparedStatement.setLong(2, stat.getTotal()); - preparedStatement.setLong(3, stat.getFree()); - preparedStatement.setLong(4, stat.getBuffers()); - preparedStatement.setLong(5, stat.getCached()); - preparedStatement.setLong(6, stat.getSwapTotal()); - preparedStatement.setLong(7, stat.getSwapFree()); - preparedStatement.setLong(8, stat.getCommitLimit()); - return preparedStatement; - } - }); - } - - @Override - protected Logger getLogger() { - return logger; - } -} -
--- a/plugins/host-memory/common/src/main/java/com/redhat/thermostat/host/memory/common/internal/MemoryStatDAOImplStatementDescriptorRegistration.java Thu Jun 22 02:59:08 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,77 +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.memory.common.internal; - -import java.util.HashSet; -import java.util.Set; - -import com.redhat.thermostat.host.memory.common.MemoryStatDAO; -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 MemoryStatDAOImplStatementDescriptorRegistration implements - StatementDescriptorRegistration { - - static final String latestDescriptor = String.format(HostLatestPojoListGetter.HOST_LATEST_QUERY_FORMAT, - MemoryStatDAO.memoryStatCategory.getName()); - static final String intervalDescriptor = String.format(HostTimeIntervalPojoListGetter.HOST_INTERVAL_QUERY_FORMAT, - MemoryStatDAO.memoryStatCategory.getName()); - static final String latestStatDescriptor = String.format(HostBoundaryPojoGetter.DESC_NEWEST_HOST_STAT, - MemoryStatDAO.memoryStatCategory.getName()); - static final String oldestStatDescriptor = String.format(HostBoundaryPojoGetter.DESC_OLDEST_HOST_STAT, - MemoryStatDAO.memoryStatCategory.getName()); - - @Override - public Set<String> getStatementDescriptors() { - Set<String> descs = new HashSet<>(5); - descs.add(latestDescriptor); - descs.add(intervalDescriptor); - descs.add(latestStatDescriptor); - descs.add(oldestStatDescriptor); - descs.add(MemoryStatDAOImpl.DESC_ADD_MEMORY_STAT); - return descs; - } - -} -
--- a/plugins/host-memory/common/src/main/java/com/redhat/thermostat/host/memory/common/internal/MemoryStatTypeAdapter.java Thu Jun 22 02:59:08 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,106 +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.memory.common.internal; - -import com.google.gson.TypeAdapter; -import com.google.gson.stream.JsonReader; -import com.google.gson.stream.JsonWriter; -import com.redhat.thermostat.host.memory.common.model.MemoryStat; - -import java.io.IOException; -import java.util.List; - -public class MemoryStatTypeAdapter extends TypeAdapter<List<MemoryStat>> { - - private static final String TYPE_LONG = "$numberLong"; - private static final String TIMESTAMP = "timeStamp"; - private static final String TOTAL = "total"; - private static final String FREE = "free"; - private static final String BUFFERS = "buffers"; - private static final String CACHED = "cached"; - private static final String SWAP_TOTAL = "swapTotal"; - private static final String SWAP_FREE = "swapFree"; - private static final String COMMIT_LIMIT = "commitLimit"; - private static final String AGENT_ID = "agentId"; - - - public void write(JsonWriter out, List<MemoryStat> stats) throws IOException { - out.beginArray(); - - for (MemoryStat stat : stats) { - writeMemoryStat(out, stat); - } - - out.endArray(); - } - - public void writeMemoryStat(JsonWriter out, MemoryStat stats) throws IOException { - out.beginObject(); - out.name(TIMESTAMP); - writeLong(out, stats.getTimeStamp()); - out.name(TOTAL); - writeLong(out, stats.getTotal()); - out.name(FREE); - writeLong(out, stats.getFree()); - out.name(BUFFERS); - writeLong(out, stats.getBuffers()); - out.name(CACHED); - writeLong(out, stats.getCached()); - out.name(SWAP_TOTAL); - writeLong(out, stats.getSwapTotal()); - out.name(SWAP_FREE); - writeLong(out, stats.getSwapFree()); - out.name(COMMIT_LIMIT); - writeLong(out, stats.getCommitLimit()); - out.name(AGENT_ID); - out.value(stats.getAgentId()); - out.endObject(); - } - - public void writeLong(JsonWriter out, long input) throws IOException { - // Write MongoDB representation of a Long - out.beginObject(); - out.name(TYPE_LONG); - out.value(String.valueOf(input)); - out.endObject(); - } - - public List<MemoryStat> read(JsonReader in) throws IOException { - return null; - } - -}
--- a/plugins/host-memory/common/src/main/java/com/redhat/thermostat/host/memory/common/model/MemoryStat.java Thu Jun 22 02:59:08 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,154 +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.memory.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 MemoryStat extends BasePojo implements TimeStampedPojo { - - private long timeStamp; - private long total; - private long free; - private long buffers; - private long cached; - private long swapTotal; - private long swapFree; - private long commitLimit; - - public MemoryStat() { - super(null); - } - - public MemoryStat(String writerId, long timeStamp, long total, long free, long buffers, long cached, long swapTotal, long swapFree, long commitLimit) { - super(writerId); - this.timeStamp = timeStamp; - this.total = total; - this.free = free; - this.buffers = buffers; - this.cached = cached; - this.swapTotal = swapTotal; - this.swapFree = swapFree; - this.commitLimit = commitLimit; - } - - @Override - @Persist - public long getTimeStamp() { - return timeStamp; - } - - @Persist - public void setTimeStamp(long timeStamp) { - this.timeStamp = timeStamp; - } - - @Persist - public long getTotal() { - return total; - } - - @Persist - public void setTotal(long total) { - this.total = total; - } - - @Persist - public long getFree() { - return free; - } - - @Persist - public void setFree(long free) { - this.free = free; - } - - @Persist - public long getBuffers() { - return buffers; - } - - @Persist - public void setBuffers(long buffers) { - this.buffers = buffers; - } - - @Persist - public long getCached() { - return cached; - } - - @Persist - public void setCached(long cached) { - this.cached = cached; - } - - @Persist - public long getSwapTotal() { - return swapTotal; - } - - @Persist - public void setSwapTotal(long swapTotal) { - this.swapTotal = swapTotal; - } - - @Persist - public long getSwapFree() { - return swapFree; - } - - @Persist - public void setSwapFree(long swapFree) { - this.swapFree = swapFree; - } - - @Persist - public long getCommitLimit() { - return commitLimit; - } - - @Persist - public void setCommitLimit(long commitLimit) { - this.commitLimit = commitLimit; - } - -} -
--- a/plugins/host-memory/common/src/main/java/com/redhat/thermostat/host/memory/common/model/MemoryType.java Thu Jun 22 02:59:08 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,47 +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.memory.common.model; - -public enum MemoryType { - MEMORY_TOTAL, - MEMORY_FREE, - MEMORY_USED, - SWAP_TOTAL, - SWAP_FREE, - BUFFERS; -} -
--- a/plugins/host-memory/common/src/main/resources/META-INF/services/com.redhat.thermostat.storage.core.auth.CategoryRegistration Thu Jun 22 02:59:08 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -com.redhat.thermostat.host.memory.common.internal.MemoryStatCategoryRegistration \ No newline at end of file
--- a/plugins/host-memory/common/src/main/resources/META-INF/services/com.redhat.thermostat.storage.core.auth.StatementDescriptorRegistration Thu Jun 22 02:59:08 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -com.redhat.thermostat.host.memory.common.internal.MemoryStatDAOImplStatementDescriptorRegistration \ No newline at end of file
--- a/plugins/host-memory/common/src/test/java/com/redhat/thermostat/host/memory/common/internal/ActivatorTest.java Thu Jun 22 02:59:08 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.memory.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.memory.common.MemoryStatDAO; -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(MemoryStatDAO.class.getName(), MemoryStatDAOImpl.class)); - - activator.stop(context); - - assertEquals(0, context.getServiceListeners().size()); - assertEquals(1, context.getAllServices().size()); - } - -} -
--- a/plugins/host-memory/common/src/test/java/com/redhat/thermostat/host/memory/common/internal/MemoryStatCategoryRegistrationTest.java Thu Jun 22 02:59:08 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.memory.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.memory.common.MemoryStatDAO; -import com.redhat.thermostat.storage.core.auth.CategoryRegistration; -import com.redhat.thermostat.testutils.ServiceLoaderTest; - -public class MemoryStatCategoryRegistrationTest extends ServiceLoaderTest<CategoryRegistration> { - - public MemoryStatCategoryRegistrationTest() { - super(CategoryRegistration.class, STORAGE_SERVICES, MemoryStatCategoryRegistration.class); - } - - @Test - public void registersAllCategories() { - MemoryStatCategoryRegistration reg = new MemoryStatCategoryRegistration(); - Set<String> categories = reg.getCategoryNames(); - assertEquals(1, categories.size()); - assertFalse("null descriptor not allowed", categories.contains(null)); - assertTrue(categories.contains(MemoryStatDAO.memoryStatCategory.getName())); - } - -} -
--- a/plugins/host-memory/common/src/test/java/com/redhat/thermostat/host/memory/common/internal/MemoryStatDAOImplStatementDescriptorRegistrationTest.java Thu Jun 22 02:59:08 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,64 +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.memory.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.testutils.ServiceLoaderTest; - -public class MemoryStatDAOImplStatementDescriptorRegistrationTest extends ServiceLoaderTest<StatementDescriptorRegistration> { - - public MemoryStatDAOImplStatementDescriptorRegistrationTest() { - super(StatementDescriptorRegistration.class, STORAGE_SERVICES, MemoryStatDAOImplStatementDescriptorRegistration.class); - } - - @Test - public void registersAllDescriptors() { - MemoryStatDAOImplStatementDescriptorRegistration reg = new MemoryStatDAOImplStatementDescriptorRegistration(); - Set<String> descriptors = reg.getStatementDescriptors(); - assertEquals(5, descriptors.size()); - assertFalse("null descriptor not allowed", descriptors.contains(null)); - } - -} -
--- a/plugins/host-memory/common/src/test/java/com/redhat/thermostat/host/memory/common/internal/MemoryStatDAOTest.java Thu Jun 22 02:59:08 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,183 +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.memory.common.internal; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; -import static org.mockito.Matchers.any; -import static org.mockito.Mockito.mock; -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.memory.common.MemoryStatDAO; -import com.redhat.thermostat.host.memory.common.model.MemoryStat; -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 MemoryStatDAOTest { - - private static long TIMESTAMP = 1; - private static long TOTAL = 2; - private static long FREE = 3; - private static long BUFFERS = 4; - private static long CACHED = 5; - private static long SWAP_TOTAL = 6; - private static long SWAP_FREE = 7; - private static long COMMIT_LIMIT = 8; - - @Test - public void testDescriptorsAreSane() { - String addMemoryStat = "ADD memory-stats SET 'agentId' = ?s , " + - "'timeStamp' = ?l , " + - "'total' = ?l , " + - "'free' = ?l , " + - "'buffers' = ?l , " + - "'cached' = ?l , " + - "'swapTotal' = ?l , " + - "'swapFree' = ?l , " + - "'commitLimit' = ?l"; - assertEquals(addMemoryStat, MemoryStatDAOImpl.DESC_ADD_MEMORY_STAT); - } - - @Test - public void testCategory() { - assertEquals("memory-stats", MemoryStatDAO.memoryStatCategory.getName()); - Collection<Key<?>> keys = MemoryStatDAO.memoryStatCategory.getKeys(); - assertTrue(keys.contains(new Key<>("agentId"))); - assertTrue(keys.contains(new Key<Long>("timeStamp"))); - assertTrue(keys.contains(new Key<Long>("total"))); - assertTrue(keys.contains(new Key<Long>("free"))); - assertTrue(keys.contains(new Key<Long>("buffers"))); - assertTrue(keys.contains(new Key<Long>("cached"))); - assertTrue(keys.contains(new Key<Long>("swapTotal"))); - assertTrue(keys.contains(new Key<Long>("swapFree"))); - assertTrue(keys.contains(new Key<Long>("commitLimit"))); - assertEquals(9, keys.size()); - } - - @Test - public void testGetLatestMemoryStats() throws DescriptorParsingException, StatementExecutionException { - - String agentId = "system"; - MemoryStat memStat1 = new MemoryStat(agentId, TIMESTAMP, TOTAL, FREE, BUFFERS, CACHED, SWAP_TOTAL, SWAP_FREE, COMMIT_LIMIT); - - @SuppressWarnings("unchecked") - Cursor<MemoryStat> cursor = mock(Cursor.class); - when(cursor.hasNext()).thenReturn(true).thenReturn(false); - when(cursor.next()).thenReturn(memStat1); - - Storage storage = mock(Storage.class); - @SuppressWarnings("unchecked") - PreparedStatement<MemoryStat> stmt = (PreparedStatement<MemoryStat>) mock(PreparedStatement.class); - when(storage.prepareStatement(anyDescriptor())).thenReturn(stmt); - when(stmt.executeQuery()).thenReturn(cursor); - - HostRef hostRef = mock(HostRef.class); - when(hostRef.getAgentId()).thenReturn(agentId); - - MemoryStatDAO dao = new MemoryStatDAOImpl(storage); - List<MemoryStat> memoryStats = dao.getLatestMemoryStats(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, memoryStats.size()); - MemoryStat stat = memoryStats.get(0); - - assertEquals(TIMESTAMP, stat.getTimeStamp()); - assertEquals(TOTAL, stat.getTotal()); - assertEquals(FREE, stat.getFree()); - assertEquals(BUFFERS, stat.getBuffers()); - assertEquals(CACHED, stat.getCached()); - assertEquals(SWAP_TOTAL, stat.getSwapTotal()); - assertEquals(SWAP_FREE, stat.getSwapFree()); - assertEquals(COMMIT_LIMIT, stat.getCommitLimit()); - } - - @SuppressWarnings("unchecked") - private StatementDescriptor<MemoryStat> anyDescriptor() { - return (StatementDescriptor<MemoryStat>) any(StatementDescriptor.class); - } - - @SuppressWarnings("unchecked") - @Test - public void testPutMemoryStat() throws DescriptorParsingException, StatementExecutionException { - Storage storage = mock(Storage.class); - PreparedStatement<MemoryStat> add = mock(PreparedStatement.class); - when(storage.prepareStatement(any(StatementDescriptor.class))).thenReturn(add); - - MemoryStat stat = new MemoryStat("foo", TIMESTAMP, TOTAL, FREE, BUFFERS, CACHED, SWAP_TOTAL, SWAP_FREE, COMMIT_LIMIT); - MemoryStatDAO dao = new MemoryStatDAOImpl(storage); - dao.putMemoryStat(stat); - - @SuppressWarnings("rawtypes") - ArgumentCaptor<StatementDescriptor> captor = ArgumentCaptor.forClass(StatementDescriptor.class); - - verify(storage).prepareStatement(captor.capture()); - StatementDescriptor<?> desc = captor.getValue(); - assertEquals(MemoryStatDAOImpl.DESC_ADD_MEMORY_STAT, desc.getDescriptor()); - verify(add).setString(0, stat.getAgentId()); - verify(add).setLong(1, stat.getTimeStamp()); - verify(add).setLong(2, stat.getTotal()); - verify(add).setLong(3, stat.getFree()); - verify(add).setLong(4, stat.getBuffers()); - verify(add).setLong(5, stat.getCached()); - verify(add).setLong(6, stat.getSwapTotal()); - verify(add).setLong(7, stat.getSwapFree()); - verify(add).setLong(8, stat.getCommitLimit()); - verify(add).execute(); - verifyNoMoreInteractions(add); - } - -} -
--- a/plugins/host-memory/common/src/test/java/com/redhat/thermostat/host/memory/common/internal/MemoryStatTypeAdapterTest.java Thu Jun 22 02:59:08 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,97 +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.memory.common.internal; - -import com.redhat.thermostat.host.memory.common.model.MemoryStat; - -import com.google.gson.GsonBuilder; -import com.google.gson.Gson; -import com.google.gson.reflect.TypeToken; -import org.junit.Test; - -import java.lang.reflect.Type; -import java.util.ArrayList; -import java.util.List; - -import static org.junit.Assert.assertEquals; - -public class MemoryStatTypeAdapterTest { - - @Test - public void testMemoryStatGetsSerializedCorrectly() { - GsonBuilder builder = new GsonBuilder(); - Type memoryStatListType = new TypeToken<ArrayList<MemoryStat>>(){}.getType(); - builder.registerTypeAdapter(memoryStatListType, new MemoryStatTypeAdapter().nullSafe()); - Gson gson = builder.create(); - List<MemoryStat> stats = new ArrayList<>(); - stats.add(new MemoryStat("1", 123l, 456l, 789l, 101112l, 131415l, - 161718l, 192021l, 222324l)); - System.out.println(gson.toJson(stats, memoryStatListType)); - assertEquals("[{\"timeStamp\":{\"$numberLong\":\"123\"}," + - "\"total\":{\"$numberLong\":\"456\"}," + - "\"free\":{\"$numberLong\":\"789\"}," + - "\"buffers\":{\"$numberLong\":\"101112\"}," + - "\"cached\":{\"$numberLong\":\"131415\"}," + - "\"swapTotal\":{\"$numberLong\":\"161718\"}," + - "\"swapFree\":{\"$numberLong\":\"192021\"}," + - "\"commitLimit\":{\"$numberLong\":\"222324\"}," + - "\"agentId\":\"1\"}]", - gson.toJson(stats, memoryStatListType)); - } - - @Test - public void testMultipleMemoryStatsGetSerializedCorrectly() { - GsonBuilder builder = new GsonBuilder(); - Type memoryStatListType = new TypeToken<ArrayList<MemoryStat>>(){}.getType(); - builder.registerTypeAdapter(memoryStatListType, new MemoryStatTypeAdapter().nullSafe()); - Gson gson = builder.create(); - List<MemoryStat> stats = new ArrayList<>(); - stats.add(new MemoryStat("1", 123l, 456l, 789l, 101112l, 131415l, - 161718l, 192021l, 222324l)); - stats.add(new MemoryStat("2", 1l, 2l, 3l, 4l, 5l,6l, 7l, 8l)); - stats.add(new MemoryStat("3", 17756l, 25365323l, 3124213l, 4465434l, 578687l, - 689821l, 786711l, 823542l)); - stats.add(new MemoryStat("4", 13332l, 24441l, 37721l, 4321345l, 542131l, - 64522l, 71232l, 8231321l)); - assertEquals("[{\"timeStamp\":{\"$numberLong\":\"123\"},\"total\":{\"$numberLong\":\"456\"},\"free\":{\"$numberLong\":\"789\"},\"buffers\":{\"$numberLong\":\"101112\"},\"cached\":{\"$numberLong\":\"131415\"},\"swapTotal\":{\"$numberLong\":\"161718\"},\"swapFree\":{\"$numberLong\":\"192021\"},\"commitLimit\":{\"$numberLong\":\"222324\"},\"agentId\":\"1\"}," + - "{\"timeStamp\":{\"$numberLong\":\"1\"},\"total\":{\"$numberLong\":\"2\"},\"free\":{\"$numberLong\":\"3\"},\"buffers\":{\"$numberLong\":\"4\"},\"cached\":{\"$numberLong\":\"5\"},\"swapTotal\":{\"$numberLong\":\"6\"},\"swapFree\":{\"$numberLong\":\"7\"},\"commitLimit\":{\"$numberLong\":\"8\"},\"agentId\":\"2\"}," + - "{\"timeStamp\":{\"$numberLong\":\"17756\"},\"total\":{\"$numberLong\":\"25365323\"},\"free\":{\"$numberLong\":\"3124213\"},\"buffers\":{\"$numberLong\":\"4465434\"},\"cached\":{\"$numberLong\":\"578687\"},\"swapTotal\":{\"$numberLong\":\"689821\"},\"swapFree\":{\"$numberLong\":\"786711\"},\"commitLimit\":{\"$numberLong\":\"823542\"},\"agentId\":\"3\"}," + - "{\"timeStamp\":{\"$numberLong\":\"13332\"},\"total\":{\"$numberLong\":\"24441\"},\"free\":{\"$numberLong\":\"37721\"},\"buffers\":{\"$numberLong\":\"4321345\"},\"cached\":{\"$numberLong\":\"542131\"},\"swapTotal\":{\"$numberLong\":\"64522\"},\"swapFree\":{\"$numberLong\":\"71232\"},\"commitLimit\":{\"$numberLong\":\"8231321\"},\"agentId\":\"4\"}]", - gson.toJson(stats, memoryStatListType)); - } - -}
--- a/plugins/host-memory/common/src/test/java/com/redhat/thermostat/host/memory/common/model/MemoryStatTest.java Thu Jun 22 02:59:08 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,49 +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.memory.common.model; - -import com.redhat.thermostat.testutils.DataObjectTest; - -public class MemoryStatTest extends DataObjectTest { - - @Override - public Class<?>[] getDataClasses() { - return new Class[] { MemoryStat.class }; - } - -} -
--- a/plugins/host-memory/distribution/assemblies/plugin-assembly.xml Thu Jun 22 02:59:08 2017 -0400 +++ b/plugins/host-memory/distribution/assemblies/plugin-assembly.xml Thu Jun 22 04:49:39 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-memory-common</include> <include>com.redhat.thermostat:thermostat-host-memory-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-memory/distribution/configFiles/gateway.properties Thu Jun 22 04:49:39 2017 -0400 @@ -0,0 +1,2 @@ +# URL to the microservice provided by the Thermostat web gateway +gatewayURL=http://localhost:30000/system-memory/0.0.1 \ No newline at end of file
--- a/plugins/host-memory/distribution/thermostat-plugin.xml Thu Jun 22 02:59:08 2017 -0400 +++ b/plugins/host-memory/distribution/thermostat-plugin.xml Thu Jun 22 04:49:39 2017 -0400 @@ -43,7 +43,6 @@ <extension> <name>agent</name> <bundles> - <bundle><symbolic-name>com.redhat.thermostat.host.memory.common</symbolic-name><version>${project.version}</version></bundle> <bundle><symbolic-name>com.redhat.thermostat.host.memory.agent</symbolic-name><version>${project.version}</version></bundle> </bundles> </extension>
--- a/plugins/host-memory/pom.xml Thu Jun 22 02:59:08 2017 -0400 +++ b/plugins/host-memory/pom.xml Thu Jun 22 04:49:39 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:59:08 2017 -0400 +++ b/plugins/pom.xml Thu Jun 22 04:49:39 2017 -0400 @@ -55,7 +55,7 @@ <module>killvm</module> <module>host-overview</module> <module>host-cpu</module> - <!--module>host-memory</module--> + <module>host-memory</module> <module>jvm-overview</module> <module>vm-gc</module> <module>vm-memory</module>