Mercurial > hg > thermostat-ng > agent
changeset 2631:690d9498ebb2
Move host information gathering to host-overview plugin backend
Reviewed-by: jerboaa, neugens, stooke
Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2017-March/022550.html
line wrap: on
line diff
--- a/agent/core/src/main/java/com/redhat/thermostat/agent/VmBlacklist.java Fri Apr 14 06:11:10 2017 -0400 +++ b/agent/core/src/main/java/com/redhat/thermostat/agent/VmBlacklist.java Fri May 05 12:10:15 2017 -0400 @@ -37,7 +37,6 @@ package com.redhat.thermostat.agent; import com.redhat.thermostat.common.Filter; -import com.redhat.thermostat.storage.core.VmRef; /** * Maintains a list of JVM processes that Thermostat should not @@ -46,28 +45,28 @@ public interface VmBlacklist { /** - * Adds a {@link Filter} to the blacklist. Virtual machines that - * match the filter will not be monitored. + * Adds a {@link Filter} to the blacklist. Virtual machines whose + * main class match the filter will not be monitored. * @param filter - a filter whose matching VMs should not be monitored */ - void addVmFilter(Filter<VmRef> filter); + void addVmFilter(Filter<String> filter); /** * Removes a {@link Filter} from the blacklist. New virtual machines * will no longer be tested against this filter for blacklisting. * @param filter - a filter previously in the blacklist */ - void removeVmFilter(Filter<VmRef> filter); + void removeVmFilter(Filter<String> filter); /** * Returns whether the given virtual machine should be monitored * by matching it against filters in the blacklist. If any filter * matches, then this method will return true. - * @param ref - a reference to the virtual machine to be tested - * against the blacklist + * @param mainClass - a class name to check against classes covered + * by this blacklist * @return true if blacklisted, false otherwise */ - boolean isBlacklisted(VmRef ref); + boolean isBlacklisted(String mainClass); }
--- a/agent/core/src/main/java/com/redhat/thermostat/agent/internal/VmBlacklistImpl.java Fri Apr 14 06:11:10 2017 -0400 +++ b/agent/core/src/main/java/com/redhat/thermostat/agent/internal/VmBlacklistImpl.java Fri May 05 12:10:15 2017 -0400 @@ -41,31 +41,30 @@ import com.redhat.thermostat.agent.VmBlacklist; import com.redhat.thermostat.common.Filter; -import com.redhat.thermostat.storage.core.VmRef; public class VmBlacklistImpl implements VmBlacklist { - private final List<Filter<VmRef>> filters; + private final List<Filter<String>> filters; public VmBlacklistImpl() { this.filters = new CopyOnWriteArrayList<>(); } @Override - public void addVmFilter(Filter<VmRef> filter) { + public void addVmFilter(Filter<String> filter) { filters.add(filter); } @Override - public void removeVmFilter(Filter<VmRef> filter) { + public void removeVmFilter(Filter<String> filter) { filters.remove(filter); } @Override - public boolean isBlacklisted(VmRef ref) { + public boolean isBlacklisted(String mainClass) { boolean result = false; - for (Filter<VmRef> filter : filters) { - if (filter.matches(ref)) { + for (Filter<String> filter : filters) { + if (filter.matches(mainClass)) { result = true; } }
--- a/agent/core/src/main/java/com/redhat/thermostat/utils/management/internal/AgentProxyFilter.java Fri Apr 14 06:11:10 2017 -0400 +++ b/agent/core/src/main/java/com/redhat/thermostat/utils/management/internal/AgentProxyFilter.java Fri May 05 12:10:15 2017 -0400 @@ -37,19 +37,17 @@ package com.redhat.thermostat.utils.management.internal; import com.redhat.thermostat.common.Filter; -import com.redhat.thermostat.storage.core.VmRef; /** * Prevents Agent Proxies from being monitored, which would create * an infinite chain of agent proxies being created. */ -public class AgentProxyFilter extends Filter<VmRef> { +public class AgentProxyFilter extends Filter<String> { private static final String AGENT_PROXY_CLASS = "com.redhat.thermostat.agent.proxy.server.AgentProxy"; @Override - public boolean matches(VmRef toMatch) { - String mainClass = toMatch.getName(); + public boolean matches(String mainClass) { return AGENT_PROXY_CLASS.equals(mainClass); }
--- a/distribution/assembly/plugin-assembly.xml Fri Apr 14 06:11:10 2017 -0400 +++ b/distribution/assembly/plugin-assembly.xml Fri May 05 12:10:15 2017 -0400 @@ -49,6 +49,7 @@ <useProjectArtifact>false</useProjectArtifact> <unpack>true</unpack> <includes> + <include>com.redhat.thermostat:thermostat-host-overview-distribution</include> <include>com.redhat.thermostat:thermostat-host-cpu-distribution</include> <include>com.redhat.thermostat:thermostat-host-memory-distribution</include> <include>com.redhat.thermostat:thermostat-numa-distribution</include>
--- a/distribution/pom.xml Fri Apr 14 06:11:10 2017 -0400 +++ b/distribution/pom.xml Fri May 05 12:10:15 2017 -0400 @@ -502,6 +502,12 @@ with type "zip", and also to ../distribution/assembly/plugin-assembly*.xml --> <dependency> <groupId>com.redhat.thermostat</groupId> + <artifactId>thermostat-host-overview-distribution</artifactId> + <version>${project.version}</version> + <type>zip</type> + </dependency> + <dependency> + <groupId>com.redhat.thermostat</groupId> <artifactId>thermostat-host-cpu-distribution</artifactId> <version>${project.version}</version> <type>zip</type>
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/plugins/com.redhat.thermostat.host.overview/agent/pom.xml Fri May 05 12:10:15 2017 -0400 @@ -0,0 +1,145 @@ +<?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-overview</artifactId> + <groupId>com.redhat.thermostat</groupId> + <version>1.99.12-SNAPSHOT</version> + </parent> + + <artifactId>thermostat-host-overview-agent</artifactId> + <packaging>bundle</packaging> + <name>Thermostat Host Overview Agent plugin</name> + + <build> + <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.overview.agent</Bundle-SymbolicName> + <Export-Package /> + <Private-Package> + com.redhat.thermostat.host.overview.agent.internal, + com.redhat.thermostat.host.overview.agent.internal.models + </Private-Package> + <!-- Do not autogenerate uses clauses in Manifests --> + <_nouses>true</_nouses> + </instructions> + </configuration> + </plugin> + <plugin> + <groupId>org.apache.felix</groupId> + <artifactId>maven-scr-plugin</artifactId> + <executions> + <execution> + <id>generate-scr-scrdescriptor</id> + <goals> + <goal>scr</goal> + </goals> + </execution> + </executions> + </plugin> + </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-common-portability</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>com.redhat.thermostat</groupId> + <artifactId>thermostat-host-overview-common</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>com.redhat.thermostat</groupId> + <artifactId>thermostat-agent-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> + <!-- declarative services --> + <dependency> + <groupId>org.apache.felix</groupId> + <artifactId>org.apache.felix.scr.annotations</artifactId> + </dependency> + </dependencies> +</project>
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/plugins/com.redhat.thermostat.host.overview/agent/src/main/java/com/redhat/thermostat/host/overview/agent/internal/HostInfoBuilderImpl.java Fri May 05 12:10:15 2017 -0400 @@ -0,0 +1,73 @@ +/* + * Copyright 2012-2017 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.host.overview.agent.internal; + +import com.redhat.thermostat.common.portability.PortableHost; +import com.redhat.thermostat.common.portability.PortableHostImpl; +import com.redhat.thermostat.host.overview.agent.internal.models.HostInfoBuilder; +import com.redhat.thermostat.host.overview.common.model.HostInfo; +import com.redhat.thermostat.storage.core.WriterID; + +/** + * Build Host information via helper classes + */ +class HostInfoBuilderImpl implements HostInfoBuilder { + + private final WriterID writerID; + private final PortableHost helper; + + HostInfoBuilderImpl(final WriterID writerID) { + this(writerID, PortableHostImpl.getInstance()); + } + + HostInfoBuilderImpl(final WriterID writerID, PortableHost helper) { + this.writerID = writerID; + this.helper = helper; + } + + @Override + public HostInfo build() { + String wId = writerID.getWriterID(); + return new HostInfo(wId, + helper.getHostName(), + helper.getOSName(), + helper.getOSVersion(), + helper.getCPUModel(), + helper.getCPUCount(), + helper.getTotalMemory()); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/plugins/com.redhat.thermostat.host.overview/agent/src/main/java/com/redhat/thermostat/host/overview/agent/internal/HostOverviewBackend.java Fri May 05 12:10:15 2017 -0400 @@ -0,0 +1,125 @@ +/* + * Copyright 2012-2017 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.host.overview.agent.internal; + +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.FrameworkUtil; + +import com.redhat.thermostat.backend.Backend; +import com.redhat.thermostat.backend.BaseBackend; +import com.redhat.thermostat.common.Version; +import com.redhat.thermostat.host.overview.agent.internal.models.HostInfoBuilder; +import com.redhat.thermostat.host.overview.common.HostInfoDAO; +import com.redhat.thermostat.host.overview.common.model.HostInfo; +import com.redhat.thermostat.storage.core.WriterID; + +@Component +@Service(value = Backend.class) +public class HostOverviewBackend extends BaseBackend { + + private final HostInfoBuilderCreator builderCreator; + + @Reference + private HostInfoDAO hostInfoDAO; + + @Reference + private WriterID writerID; + + private boolean started; + + public HostOverviewBackend() { + this(new Version(FrameworkUtil.getBundle(HostOverviewBackend.class)), null, null, new HostInfoBuilderCreator()); + } + + HostOverviewBackend(Version version, HostInfoDAO hostInfoDAO, WriterID writerID, + HostInfoBuilderCreator builderCreator) { + super("Host Overview Backend", + "Gathers general information about a host", + "Red Hat, Inc.", + version.getVersionNumber()); + this.hostInfoDAO = hostInfoDAO; + this.writerID = writerID; + this.builderCreator = builderCreator; + } + + @Override + public boolean activate() { + HostInfoBuilder builder = builderCreator.create(writerID); + HostInfo hostInfo = builder.build(); + hostInfoDAO.putHostInfo(hostInfo); + started = true; + return true; + } + + @Override + public boolean deactivate() { + started = false; + return true; + } + + @Override + public boolean isActive() { + return started; + } + + @Override + public int getOrderValue() { + return ORDER_DEFAULT_GROUP; + } + + // For testing purposes + static class HostInfoBuilderCreator { + HostInfoBuilder create(WriterID writerID) { + return new HostInfoBuilderImpl(writerID); + } + } + + @Activate + @Deactivate + protected void noop() { + /* Map unused DS activate/deactivate methods to this NOOP method to + * prevent it from trying to use Backend.activate/deactivate and + * giving an error about them being incompatible. + */ + } + +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/plugins/com.redhat.thermostat.host.overview/agent/src/main/java/com/redhat/thermostat/host/overview/agent/internal/models/HostInfoBuilder.java Fri May 05 12:10:15 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.overview.agent.internal.models; + +import com.redhat.thermostat.host.overview.common.model.HostInfo; + +/** + * Interface for building a HostInfo structure + * HostInfo contains information about a target machine's OS and hardware + */ +public interface HostInfoBuilder { + HostInfo build(); +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/plugins/com.redhat.thermostat.host.overview/agent/src/test/java/com/redhat/thermostat/host/overview/agent/internal/HostInfoBuilderTest.java Fri May 05 12:10:15 2017 -0400 @@ -0,0 +1,87 @@ +/* + * Copyright 2012-2017 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.host.overview.agent.internal; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import org.junit.Before; +import org.junit.Test; + +import com.redhat.thermostat.common.portability.PortableHost; +import com.redhat.thermostat.host.overview.agent.internal.models.HostInfoBuilder; +import com.redhat.thermostat.host.overview.common.model.HostInfo; +import com.redhat.thermostat.storage.core.WriterID; + +public class HostInfoBuilderTest { + + private WriterID writerId; + private PortableHost helper; + + @Before + public void setup() { + writerId = mock(WriterID.class); + helper = mock(PortableHost.class); + when(helper.getHostName()).thenReturn("testhost"); + when(helper.getOSName()).thenReturn("testos"); + when(helper.getOSVersion()).thenReturn("testversion"); + when(helper.getCPUModel()).thenReturn("testcpu"); + when(helper.getCPUCount()).thenReturn(4567); + when(helper.getTotalMemory()).thenReturn(9876L); + } + + @Test + public void testSimpleBuild() { + Assume.assumeTrue(OS.IS_WINDOWS); + HostInfo info = new HostInfoBuilderImpl(writerId).build(); + assertNotNull(info); + } + + @Test + public void testGetInfo() { + final HostInfoBuilder ib = new HostInfoBuilderImpl(writerId, helper); + final HostInfo hi = ib.build(); + assertEquals("testhost",hi.getHostname()); + assertEquals("testos", hi.getOsName()); + assertEquals("testcpu", hi.getCpuModel()); + assertEquals("testversion", hi.getOsKernel()); + assertEquals(4567, hi.getCpuCount()); + assertEquals(9876L, hi.getTotalMemory()); + } +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/plugins/com.redhat.thermostat.host.overview/agent/src/test/java/com/redhat/thermostat/host/overview/agent/internal/HostOverviewBackendTest.java Fri May 05 12:10:15 2017 -0400 @@ -0,0 +1,97 @@ +/* + * Copyright 2012-2017 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.host.overview.agent.internal; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import org.junit.Before; +import org.junit.Test; + +import com.redhat.thermostat.common.Version; +import com.redhat.thermostat.host.overview.agent.internal.HostOverviewBackend.HostInfoBuilderCreator; +import com.redhat.thermostat.host.overview.agent.internal.models.HostInfoBuilder; +import com.redhat.thermostat.host.overview.common.HostInfoDAO; +import com.redhat.thermostat.host.overview.common.model.HostInfo; +import com.redhat.thermostat.storage.core.WriterID; + +public class HostOverviewBackendTest { + + private HostOverviewBackend backend; + private HostInfoDAO hostInfoDAO; + private WriterID writerID; + private HostInfo info; + private HostInfoBuilder builder; + private HostInfoBuilderCreator builderCreator; + + @Before + public void setup() { + hostInfoDAO = mock(HostInfoDAO.class); + Version version = mock(Version.class); + when(version.getVersionNumber()).thenReturn("0.0.0"); + writerID = mock(WriterID.class); + + info = mock(HostInfo.class); + builder = mock(HostInfoBuilder.class); + when(builder.build()).thenReturn(info); + builderCreator = mock(HostInfoBuilderCreator.class); + when(builderCreator.create(writerID)).thenReturn(builder); + + backend = new HostOverviewBackend(version, hostInfoDAO, writerID, builderCreator); + } + + @Test + public void testActivate() { + backend.activate(); + assertTrue(backend.isActive()); + + verify(builderCreator).create(writerID); + verify(builder).build(); + verify(hostInfoDAO).putHostInfo(info); + } + + @Test + public void testDeactivate() { + backend.activate(); + backend.deactivate(); + assertFalse(backend.isActive()); + } +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/plugins/com.redhat.thermostat.host.overview/common/pom.xml Fri May 05 12:10:15 2017 -0400 @@ -0,0 +1,145 @@ +<?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-overview</artifactId> + <groupId>com.redhat.thermostat</groupId> + <version>1.99.12-SNAPSHOT</version> + </parent> + <artifactId>thermostat-host-overview-common</artifactId> + <packaging>bundle</packaging> + <name>Thermostat Host Overview 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.overview.common</Bundle-SymbolicName> + <Export-Package> + com.redhat.thermostat.host.overview.common, + com.redhat.thermostat.host.overview.common.model + </Export-Package> + <Private-Package> + com.redhat.thermostat.host.overview.common.internal + </Private-Package> + <!-- Do not autogenerate uses clauses in Manifests --> + <_nouses>true</_nouses> + </instructions> + </configuration> + </plugin> + <plugin> + <groupId>org.apache.felix</groupId> + <artifactId>maven-scr-plugin</artifactId> + <executions> + <execution> + <id>generate-scr-scrdescriptor</id> + <goals> + <goal>scr</goal> + </goals> + </execution> + </executions> + </plugin> + </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> + <!-- declarative services --> + <dependency> + <groupId>org.apache.felix</groupId> + <artifactId>org.apache.felix.scr.annotations</artifactId> + </dependency> + </dependencies> +</project>
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/plugins/com.redhat.thermostat.host.overview/common/src/main/java/com/redhat/thermostat/host/overview/common/HostInfoDAO.java Fri May 05 12:10:15 2017 -0400 @@ -0,0 +1,76 @@ +/* + * Copyright 2012-2017 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.host.overview.common; + +import java.util.List; + +import com.redhat.thermostat.annotations.Service; +import com.redhat.thermostat.host.overview.common.model.HostInfo; +import com.redhat.thermostat.storage.core.AgentId; +import com.redhat.thermostat.storage.core.Category; +import com.redhat.thermostat.storage.core.Countable; +import com.redhat.thermostat.storage.core.Key; + +@Service +public interface HostInfoDAO extends Countable { + + static Key<String> hostNameKey = new Key<>("hostname"); + static Key<String> osNameKey = new Key<>("osName"); + static Key<String> osKernelKey = new Key<>("osKernel"); + static Key<Integer> cpuCountKey = new Key<>("cpuCount"); + static Key<String> cpuModelKey = new Key<>("cpuModel"); + static Key<Long> hostMemoryTotalKey = new Key<>("totalMemory"); + + static final Category<HostInfo> hostInfoCategory = new Category<>("host-info", HostInfo.class, + Key.AGENT_ID, hostNameKey, osNameKey, osKernelKey, + cpuCountKey, cpuModelKey, hostMemoryTotalKey); + + /** @return information on all known hosts */ + List<HostInfo> getAllHostInfos(); + + /** + * + * @param agentId The Agent Id for which to get the HostInfo object for. + * @return The corresponding HostInfo object. May return null if the user + * is not permitted to retrieve this HostInfo. + */ + HostInfo getHostInfo(AgentId agentId); + + void putHostInfo(HostInfo info); + +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/plugins/com.redhat.thermostat.host.overview/common/src/main/java/com/redhat/thermostat/host/overview/common/internal/HostInfoDAOCategoryRegistration.java Fri May 05 12:10:15 2017 -0400 @@ -0,0 +1,60 @@ +/* + * Copyright 2012-2017 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.host.overview.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 HostInfoDAOCategoryRegistration implements CategoryRegistration { + + @Override + public Set<String> getCategoryNames() { + Set<String> categories = new HashSet<>(1); + categories.add(HostInfoDAOImpl.hostInfoCategory.getName()); + return categories; + } + +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/plugins/com.redhat.thermostat.host.overview/common/src/main/java/com/redhat/thermostat/host/overview/common/internal/HostInfoDAOImpl.java Fri May 05 12:10:15 2017 -0400 @@ -0,0 +1,157 @@ +/* + * Copyright 2012-2017 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.host.overview.common.internal; + +import java.util.List; +import java.util.logging.Logger; + +import org.apache.felix.scr.annotations.Activate; +import org.apache.felix.scr.annotations.Component; +import org.apache.felix.scr.annotations.Reference; +import org.apache.felix.scr.annotations.Service; + +import com.redhat.thermostat.common.utils.LoggingUtils; +import com.redhat.thermostat.host.overview.common.HostInfoDAO; +import com.redhat.thermostat.host.overview.common.model.HostInfo; +import com.redhat.thermostat.storage.core.AgentId; +import com.redhat.thermostat.storage.core.Category; +import com.redhat.thermostat.storage.core.CategoryAdapter; +import com.redhat.thermostat.storage.core.Key; +import com.redhat.thermostat.storage.core.PreparedStatement; +import com.redhat.thermostat.storage.core.Storage; +import com.redhat.thermostat.storage.dao.AbstractDaoQuery; +import com.redhat.thermostat.storage.dao.AbstractDaoStatement; +import com.redhat.thermostat.storage.dao.BaseCountable; +import com.redhat.thermostat.storage.dao.SimpleDaoQuery; +import com.redhat.thermostat.storage.model.AggregateCount; + +@Component +@Service(value = HostInfoDAO.class) +public class HostInfoDAOImpl extends BaseCountable implements HostInfoDAO { + + private static final Logger logger = LoggingUtils.getLogger(HostInfoDAOImpl.class); + static final String QUERY_HOST_INFO = "QUERY " + + hostInfoCategory.getName() + " WHERE '" + + Key.AGENT_ID.getName() + "' = ?s LIMIT 1"; + static final String QUERY_ALL_HOSTS = "QUERY " + hostInfoCategory.getName(); + // We can use hostInfoCategory.getName() here since this query + // only changes the data class. When executed we use the adapted + // aggregate category. + static final String AGGREGATE_COUNT_ALL_HOSTS = "QUERY-COUNT " + hostInfoCategory.getName(); + // ADD host-info SET 'agentId' = ?s , \ + // 'hostname' = ?s , \ + // 'osName' = ?s , \ + // 'osKernel' = ?s , \ + // 'cpuModel' = ?s , \ + // 'cpuCount' = ?i , \ + // 'totalMemory' = ?l + static final String DESC_ADD_HOST_INFO = "ADD " + hostInfoCategory.getName() + + " SET '" + Key.AGENT_ID.getName() + "' = ?s , " + + "'" + hostNameKey.getName() + "' = ?s , " + + "'" + osNameKey.getName() + "' = ?s , " + + "'" + osKernelKey.getName() + "' = ?s , " + + "'" + cpuModelKey.getName() + "' = ?s , " + + "'" + cpuCountKey.getName() + "' = ?i , " + + "'" + hostMemoryTotalKey.getName() + "' = ?l"; + + private final Category<AggregateCount> aggregateCategory; + + @Reference + private Storage storage; + + public HostInfoDAOImpl() { + this(null); + } + + public HostInfoDAOImpl(Storage storage) { + this.storage = storage; + // Adapt category to the aggregate form + CategoryAdapter<HostInfo, AggregateCount> adapter = new CategoryAdapter<>(hostInfoCategory); + this.aggregateCategory = adapter.getAdapted(AggregateCount.class); + } + + @Activate + private void activate() { + storage.registerCategory(hostInfoCategory); + storage.registerCategory(aggregateCategory); + } + + @Override + public HostInfo getHostInfo(final AgentId agentId) { + return executeQuery(new AbstractDaoQuery<HostInfo>(storage, hostInfoCategory, QUERY_HOST_INFO) { + @Override + public PreparedStatement<HostInfo> customize(PreparedStatement<HostInfo> preparedStatement) { + preparedStatement.setString(0, agentId.get()); + return preparedStatement; + } + }).head(); + } + + @Override + public void putHostInfo(final HostInfo info) { + executeStatement(new AbstractDaoStatement<HostInfo>(storage, hostInfoCategory, DESC_ADD_HOST_INFO) { + @Override + public PreparedStatement<HostInfo> customize(PreparedStatement<HostInfo> preparedStatement) { + preparedStatement.setString(0, info.getAgentId()); + preparedStatement.setString(1, info.getHostname()); + preparedStatement.setString(2, info.getOsName()); + preparedStatement.setString(3, info.getOsKernel()); + preparedStatement.setString(4, info.getCpuModel()); + preparedStatement.setInt(5, info.getCpuCount()); + preparedStatement.setLong(6, info.getTotalMemory()); + return preparedStatement; + } + }); + } + + @Override + public List<HostInfo> getAllHostInfos() { + return executeQuery(new SimpleDaoQuery<>(storage, hostInfoCategory, QUERY_ALL_HOSTS)).asList(); + } + + @Override + public long getCount() { + return getCount(storage, aggregateCategory, AGGREGATE_COUNT_ALL_HOSTS); + } + + @Override + protected Logger getLogger() { + return logger; + } + +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/plugins/com.redhat.thermostat.host.overview/common/src/main/java/com/redhat/thermostat/host/overview/common/internal/HostInfoDAOImplStatementDescriptorRegistration.java Fri May 05 12:10:15 2017 -0400 @@ -0,0 +1,64 @@ +/* + * Copyright 2012-2017 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.host.overview.common.internal; + +import java.util.HashSet; +import java.util.Set; + +import com.redhat.thermostat.storage.core.HostLatestPojoListGetter; +import com.redhat.thermostat.storage.core.auth.StatementDescriptorRegistration; + +/** + * Registers the prepared query issued by this maven module via + * {@link HostLatestPojoListGetter}. + * + */ +public class HostInfoDAOImplStatementDescriptorRegistration implements + StatementDescriptorRegistration { + + @Override + public Set<String> getStatementDescriptors() { + Set<String> descs = new HashSet<>(4); + descs.add(HostInfoDAOImpl.QUERY_HOST_INFO); + descs.add(HostInfoDAOImpl.QUERY_ALL_HOSTS); + descs.add(HostInfoDAOImpl.AGGREGATE_COUNT_ALL_HOSTS); + descs.add(HostInfoDAOImpl.DESC_ADD_HOST_INFO); + return descs; + } + +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/plugins/com.redhat.thermostat.host.overview/common/src/main/java/com/redhat/thermostat/host/overview/common/model/HostInfo.java Fri May 05 12:10:15 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.overview.common.model; + +import com.redhat.thermostat.storage.core.Entity; +import com.redhat.thermostat.storage.core.Persist; +import com.redhat.thermostat.storage.model.BasePojo; + +@Entity +public class HostInfo extends BasePojo { + + private String hostname; + private String osName; + private String osKernel; + private String cpuModel; + private int cpuCount; + private long totalMemory; + + public HostInfo() { + this(null, null, null, null, null, -1, -1); + } + + public HostInfo(String writerId, String hostname, String osName, String osKernel, String cpuModel, int cpuCount, long totalMemory) { + super(writerId); + this.hostname = hostname; + this.osName = osName; + this.osKernel = osKernel; + this.cpuModel = cpuModel; + this.cpuCount = cpuCount; + this.totalMemory = totalMemory; + } + + @Persist + public void setHostname(String hostname) { + this.hostname = hostname; + } + + @Persist + public void setOsName(String osName) { + this.osName = osName; + } + + @Persist + public void setOsKernel(String osKernel) { + this.osKernel = osKernel; + } + + @Persist + public void setCpuModel(String cpuModel) { + this.cpuModel = cpuModel; + } + + @Persist + public void setCpuCount(int cpuCount) { + this.cpuCount = cpuCount; + } + + @Persist + public void setTotalMemory(long totalMemory) { + this.totalMemory = totalMemory; + } + + @Persist + public String getHostname() { + return hostname; + } + + @Persist + public String getOsName() { + return osName; + } + + @Persist + public String getOsKernel() { + return osKernel; + } + + @Persist + public String getCpuModel() { + return cpuModel; + } + + @Persist + public int getCpuCount() { + return cpuCount; + } + + /** + * Total memory in bytes + */ + @Persist + public long getTotalMemory() { + return totalMemory; + } + +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/plugins/com.redhat.thermostat.host.overview/common/src/main/resources/META-INF/services/com.redhat.thermostat.storage.core.auth.CategoryRegistration Fri May 05 12:10:15 2017 -0400 @@ -0,0 +1,1 @@ +com.redhat.thermostat.host.overview.common.internal.HostInfoDAOCategoryRegistration \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/plugins/com.redhat.thermostat.host.overview/common/src/main/resources/META-INF/services/com.redhat.thermostat.storage.core.auth.StatementDescriptorRegistration Fri May 05 12:10:15 2017 -0400 @@ -0,0 +1,1 @@ +com.redhat.thermostat.host.overview.common.internal.HostInfoDAOImplStatementDescriptorRegistration \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/plugins/com.redhat.thermostat.host.overview/common/src/test/java/com/redhat/thermostat/host/overview/common/internal/HostInfoDAOCategoryRegistrationTest.java Fri May 05 12:10:15 2017 -0400 @@ -0,0 +1,67 @@ +/* + * Copyright 2012-2017 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.host.overview.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.overview.common.HostInfoDAO; +import com.redhat.thermostat.storage.core.auth.CategoryRegistration; +import com.redhat.thermostat.testutils.ServiceLoaderTest; + +public class HostInfoDAOCategoryRegistrationTest extends ServiceLoaderTest<CategoryRegistration> { + + public HostInfoDAOCategoryRegistrationTest() { + super(CategoryRegistration.class, STORAGE_SERVICES, HostInfoDAOCategoryRegistration.class); + } + + @Test + public void registersAllCategories() { + HostInfoDAOCategoryRegistration reg = new HostInfoDAOCategoryRegistration(); + Set<String> categories = reg.getCategoryNames(); + assertEquals(1, categories.size()); + assertFalse("null descriptor not allowed", categories.contains(null)); + assertTrue(categories.contains(HostInfoDAO.hostInfoCategory.getName())); + } + +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/plugins/com.redhat.thermostat.host.overview/common/src/test/java/com/redhat/thermostat/host/overview/common/internal/HostInfoDAOImplStatementDescriptorRegistrationTest.java Fri May 05 12:10:15 2017 -0400 @@ -0,0 +1,66 @@ +/* + * Copyright 2012-2017 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.host.overview.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.host.overview.common.internal.HostInfoDAOImplStatementDescriptorRegistration; +import com.redhat.thermostat.storage.core.auth.StatementDescriptorRegistration; +import com.redhat.thermostat.storage.internal.dao.DAOImplStatementDescriptorRegistration; +import com.redhat.thermostat.testutils.ServiceLoaderTest; + +public class HostInfoDAOImplStatementDescriptorRegistrationTest extends ServiceLoaderTest<StatementDescriptorRegistration> { + + public HostInfoDAOImplStatementDescriptorRegistrationTest() { + super(StatementDescriptorRegistration.class, STORAGE_SERVICES, DAOImplStatementDescriptorRegistration.class); + } + + @Test + public void registersAllDescriptors() { + HostInfoDAOImplStatementDescriptorRegistration reg = new HostInfoDAOImplStatementDescriptorRegistration(); + Set<String> descriptors = reg.getStatementDescriptors(); + assertEquals(4, descriptors.size()); + assertFalse("null descriptor not allowed", descriptors.contains(null)); + } + +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/plugins/com.redhat.thermostat.host.overview/common/src/test/java/com/redhat/thermostat/host/overview/common/internal/HostInfoDAOTest.java Fri May 05 12:10:15 2017 -0400 @@ -0,0 +1,200 @@ +/* + * Copyright 2012-2017 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.host.overview.common.internal; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertSame; +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.when; + +import java.util.Collection; +import java.util.NoSuchElementException; + +import org.junit.Test; +import org.mockito.ArgumentCaptor; +import org.mockito.Mockito; + +import com.redhat.thermostat.host.overview.common.HostInfoDAO; +import com.redhat.thermostat.host.overview.common.model.HostInfo; +import com.redhat.thermostat.storage.core.AgentId; +import com.redhat.thermostat.storage.core.Cursor; +import com.redhat.thermostat.storage.core.DescriptorParsingException; +import com.redhat.thermostat.storage.core.Key; +import com.redhat.thermostat.storage.core.PreparedStatement; +import com.redhat.thermostat.storage.core.StatementDescriptor; +import com.redhat.thermostat.storage.core.StatementExecutionException; +import com.redhat.thermostat.storage.core.Storage; +import com.redhat.thermostat.storage.model.AggregateCount; + +public class HostInfoDAOTest { + + static class Triple<S, T, U> { + final S first; + final T second; + final U third; + + public Triple(S first, T second, U third) { + this.first = first; + this.second = second; + this.third = third; + } + } + + private static final String HOST_NAME = "a host name"; + private static final String OS_NAME = "some os"; + private static final String OS_KERNEL = "some kernel"; + private static final String CPU_MODEL = "some cpu that runs fast"; + private static final int CPU_NUM = -1; + private static final long MEMORY_TOTAL = 0xCAFEBABEl; + + @Test + public void preparedQueryDescriptorsAreSane() { + String expectedHostInfo = "QUERY host-info WHERE 'agentId' = ?s LIMIT 1"; + assertEquals(expectedHostInfo, HostInfoDAOImpl.QUERY_HOST_INFO); + String expectedAllHosts = "QUERY host-info"; + assertEquals(expectedAllHosts, HostInfoDAOImpl.QUERY_ALL_HOSTS); + String aggregateAllHosts = "QUERY-COUNT host-info"; + assertEquals(aggregateAllHosts, HostInfoDAOImpl.AGGREGATE_COUNT_ALL_HOSTS); + String addHostInfo = "ADD host-info SET 'agentId' = ?s , " + + "'hostname' = ?s , " + + "'osName' = ?s , " + + "'osKernel' = ?s , " + + "'cpuModel' = ?s , " + + "'cpuCount' = ?i , " + + "'totalMemory' = ?l"; + assertEquals(addHostInfo, HostInfoDAOImpl.DESC_ADD_HOST_INFO); + } + + @Test + public void testCategory() { + assertEquals("host-info", HostInfoDAO.hostInfoCategory.getName()); + Collection<Key<?>> keys = HostInfoDAO.hostInfoCategory.getKeys(); + assertTrue(keys.contains(new Key<>("agentId"))); + assertTrue(keys.contains(new Key<String>("hostname"))); + assertTrue(keys.contains(new Key<String>("osName"))); + assertTrue(keys.contains(new Key<String>("osKernel"))); + assertTrue(keys.contains(new Key<String>("cpuModel"))); + assertTrue(keys.contains(new Key<Integer>("cpuCount"))); + assertTrue(keys.contains(new Key<Long>("totalMemory"))); + assertEquals(7, keys.size()); + } + + @Test + public void testGetHostInfoUsingAgentId() throws DescriptorParsingException, StatementExecutionException { + Storage storage = mock(Storage.class); + @SuppressWarnings("unchecked") + PreparedStatement<HostInfo> prepared = (PreparedStatement<HostInfo>) mock(PreparedStatement.class); + when(storage.prepareStatement(anyDescriptor())).thenReturn(prepared); + + HostInfo info = new HostInfo("foo-agent", HOST_NAME, OS_NAME, OS_KERNEL, CPU_MODEL, CPU_NUM, MEMORY_TOTAL); + @SuppressWarnings("unchecked") + Cursor<HostInfo> cursor = (Cursor<HostInfo>) mock(Cursor.class); + when(cursor.hasNext()).thenReturn(true).thenReturn(false); + when(cursor.next()).thenReturn(info).thenReturn(null); + when(prepared.executeQuery()).thenReturn(cursor); + + HostInfo result = new HostInfoDAOImpl(storage).getHostInfo(new AgentId("some uid")); + + verify(storage).prepareStatement(anyDescriptor()); + verify(prepared).setString(0, "some uid"); + verify(prepared).executeQuery(); + assertSame(result, info); + } + + @SuppressWarnings("unchecked") + private StatementDescriptor<HostInfo> anyDescriptor() { + return (StatementDescriptor<HostInfo>) any(StatementDescriptor.class); + } + + @SuppressWarnings("unchecked") + @Test + public void testPutHostInfo() throws DescriptorParsingException, + StatementExecutionException { + Storage storage = mock(Storage.class); + PreparedStatement<HostInfo> add = mock(PreparedStatement.class); + when(storage.prepareStatement(any(StatementDescriptor.class))).thenReturn(add); + + HostInfo info = new HostInfo("foo-agent", HOST_NAME, OS_NAME, OS_KERNEL, CPU_MODEL, CPU_NUM, MEMORY_TOTAL); + HostInfoDAO dao = new HostInfoDAOImpl(storage); + dao.putHostInfo(info); + + @SuppressWarnings("rawtypes") + ArgumentCaptor<StatementDescriptor> captor = ArgumentCaptor.forClass(StatementDescriptor.class); + + verify(storage).prepareStatement(captor.capture()); + StatementDescriptor<?> desc = captor.getValue(); + assertEquals(HostInfoDAOImpl.DESC_ADD_HOST_INFO, desc.getDescriptor()); + + verify(add).setString(0, info.getAgentId()); + verify(add).setString(1, info.getHostname()); + verify(add).setString(2, info.getOsName()); + verify(add).setString(3, info.getOsKernel()); + verify(add).setString(4, info.getCpuModel()); + verify(add).setInt(5, info.getCpuCount()); + verify(add).setLong(6, info.getTotalMemory()); + verify(add).execute(); + Mockito.verifyNoMoreInteractions(add); + } + + @Test + public void testGetCount() throws DescriptorParsingException, + StatementExecutionException { + AggregateCount count = new AggregateCount(); + count.setCount(2); + + @SuppressWarnings("unchecked") + Cursor<AggregateCount> c = (Cursor<AggregateCount>) mock(Cursor.class); + when(c.hasNext()).thenReturn(true).thenReturn(false); + when(c.next()).thenReturn(count).thenThrow(new NoSuchElementException()); + + Storage storage = mock(Storage.class); + @SuppressWarnings("unchecked") + PreparedStatement<AggregateCount> stmt = (PreparedStatement<AggregateCount>) mock(PreparedStatement.class); + @SuppressWarnings("unchecked") + StatementDescriptor<AggregateCount> desc = any(StatementDescriptor.class); + when(storage.prepareStatement(desc)).thenReturn(stmt); + when(stmt.executeQuery()).thenReturn(c); + HostInfoDAOImpl dao = new HostInfoDAOImpl(storage); + + assertEquals(2, dao.getCount()); + } + +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/plugins/com.redhat.thermostat.host.overview/distribution/assemblies/plugin-assembly.xml Fri May 05 12:10:15 2017 -0400 @@ -0,0 +1,68 @@ +<?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. + +--> +<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd"> + <id>plugin-assembly</id> + <formats> + <format>zip</format> + </formats> + <baseDirectory>${thermostat.plugin}</baseDirectory> + <includeBaseDirectory>true</includeBaseDirectory> + + <dependencySets> + <dependencySet> + <includes> + <include>com.redhat.thermostat:thermostat-host-overview-common</include> + <include>com.redhat.thermostat:thermostat-host-overview-agent</include> + </includes> + <useProjectArtifact>false</useProjectArtifact> + <useStrictFiltering>true</useStrictFiltering> + </dependencySet> + </dependencySets> + + <files> + <file> + <source>thermostat-plugin.xml</source> + <outputDirectory>/</outputDirectory> + <filtered>true</filtered> + </file> + </files> +</assembly> +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/plugins/com.redhat.thermostat.host.overview/distribution/pom.xml Fri May 05 12:10:15 2017 -0400 @@ -0,0 +1,89 @@ +<?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> + <groupId>com.redhat.thermostat</groupId> + <artifactId>thermostat-host-overview</artifactId> + <version>1.99.12-SNAPSHOT</version> + </parent> + + <artifactId>thermostat-host-overview-distribution</artifactId> + <packaging>pom</packaging> + + <name>Thermostat Host Overview plugin distribution</name> + + <properties> + <thermostat.plugin>host-overview</thermostat.plugin> + </properties> + + <build> + <plugins> + <plugin> + <artifactId>maven-assembly-plugin</artifactId> + <configuration> + <descriptors> + <descriptor>assemblies/plugin-assembly.xml</descriptor> + </descriptors> + <appendAssemblyId>false</appendAssemblyId> + </configuration> + <executions> + <execution> + <id>assemble-plugin</id> + <phase>package</phase> + <goals> + <goal>single</goal> + </goals> + </execution> + </executions> + </plugin> + </plugins> + </build> + + <dependencies> + <dependency> + <groupId>com.redhat.thermostat</groupId> + <artifactId>thermostat-host-overview-agent</artifactId> + <version>${project.version}</version> + </dependency> + </dependencies> + +</project> +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/plugins/com.redhat.thermostat.host.overview/distribution/thermostat-plugin.xml Fri May 05 12:10:15 2017 -0400 @@ -0,0 +1,52 @@ +<?xml version="1.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. + +--> +<plugin xmlns="http://icedtea.classpath.org/thermostat/plugins/v1.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://icedtea.classpath.org/thermostat/plugins/v1.0 thermostat-plugin.xsd"> + <extensions> + <extension> + <name>agent</name> + <bundles> + <bundle><symbolic-name>com.redhat.thermostat.host.overview.common</symbolic-name><version>${project.version}</version></bundle> + <bundle><symbolic-name>com.redhat.thermostat.host.overview.agent</symbolic-name><version>${project.version}</version></bundle> + </bundles> + </extension> + </extensions> +</plugin> +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/plugins/com.redhat.thermostat.host.overview/pom.xml Fri May 05 12:10:15 2017 -0400 @@ -0,0 +1,60 @@ +<?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> + <groupId>com.redhat.thermostat</groupId> + <artifactId>thermostat-plugins</artifactId> + <version>1.99.12-SNAPSHOT</version> + </parent> + + <artifactId>thermostat-host-overview</artifactId> + <packaging>pom</packaging> + + <name>Thermostat Host Overview plugin</name> + + <modules> + <module>common</module> + <module>agent</module> + <module>distribution</module> + </modules> + +</project> +
--- a/plugins/pom.xml Fri Apr 14 06:11:10 2017 -0400 +++ b/plugins/pom.xml Fri May 05 12:10:15 2017 -0400 @@ -53,6 +53,7 @@ <modules> <module>thread</module> <module>killvm</module> + <module>com.redhat.thermostat.host.overview</module> <module>host-cpu</module> <module>host-memory</module> <module>vm-byteman</module>
--- a/storage/core/src/main/java/com/redhat/thermostat/storage/core/DefaultHostsVMsLoader.java Fri Apr 14 06:11:10 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.storage.core; - -import java.util.Collection; - -import com.redhat.thermostat.storage.dao.HostInfoDAO; -import com.redhat.thermostat.storage.dao.VmInfoDAO; - -public class DefaultHostsVMsLoader implements HostsVMsLoader { - - private HostInfoDAO hostsDAO; - private VmInfoDAO vmsDAO; - private boolean liveHosts; - - /** - * - * @param hostDAO - * @param vmsDAO - * @param liveHosts {@code true} if only alive agent documents should get retrieved. - */ - public DefaultHostsVMsLoader(HostInfoDAO hostDAO, VmInfoDAO vmsDAO, boolean liveHosts) { - this.hostsDAO = hostDAO; - this.vmsDAO = vmsDAO; - this.liveHosts = liveHosts; - } - - @Override - public Collection<HostRef> getHosts() { - if (liveHosts) { - return hostsDAO.getAliveHosts(); - } else { - return hostsDAO.getHosts(); - } - } - - @Override - public Collection<VmRef> getVMs(HostRef host) { - return vmsDAO.getVMs(host); - } - -} -
--- a/storage/core/src/main/java/com/redhat/thermostat/storage/dao/HostInfoDAO.java Fri Apr 14 06:11:10 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,116 +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.storage.dao; - -import java.util.Collection; -import java.util.List; - -import com.redhat.thermostat.annotations.Service; -import com.redhat.thermostat.storage.core.AgentId; -import com.redhat.thermostat.storage.core.Category; -import com.redhat.thermostat.storage.core.Countable; -import com.redhat.thermostat.storage.core.HostRef; -import com.redhat.thermostat.storage.core.Key; -import com.redhat.thermostat.storage.model.HostInfo; - -@Service -public interface HostInfoDAO extends Countable { - - static Key<String> hostNameKey = new Key<>("hostname"); - static Key<String> osNameKey = new Key<>("osName"); - static Key<String> osKernelKey = new Key<>("osKernel"); - static Key<Integer> cpuCountKey = new Key<>("cpuCount"); - static Key<String> cpuModelKey = new Key<>("cpuModel"); - static Key<Long> hostMemoryTotalKey = new Key<>("totalMemory"); - - static final Category<HostInfo> hostInfoCategory = new Category<>("host-info", HostInfo.class, - Key.AGENT_ID, hostNameKey, osNameKey, osKernelKey, - cpuCountKey, cpuModelKey, hostMemoryTotalKey); - - /** @return information on all known hosts */ - List<HostInfo> getAllHostInfos(); - - /** - * - * @param ref The host ref for which to get the HostInfo object for. - * @return The corresponding HostInfo object. May return null if the user - * is not permitted to retrieve this HostInfo. - */ - @Deprecated - HostInfo getHostInfo(HostRef ref); - - /** - * - * @param agentId The Agent Id for which to get the HostInfo object for. - * @return The corresponding HostInfo object. May return null if the user - * is not permitted to retrieve this HostInfo. - */ - HostInfo getHostInfo(AgentId agentId); - - void putHostInfo(HostInfo info); - - /** - * - * @return A collection of hosts (HostRefs), which may be empty. - * - * @deprecated use {@link com.redhat.thermostat.storage.dao.AgentInfoDAO#getAgentIds()} - * instead. - */ - @Deprecated - Collection<HostRef> getHosts(); - - /** - * - * @return A collection of alive hosts which may be empty. - * - * @deprecated use {@link com.redhat.thermostat.storage.dao.AgentInfoDAO#getAliveAgentIds()} - * instead. - */ - @Deprecated - Collection<HostRef> getAliveHosts(); - - /** - * - * @return if this host is alive. - * @deprecated use {@link com.redhat.thermostat.storage.dao.AgentInfoDAO#isAlive(AgentId)} - * instead. - */ - @Deprecated - boolean isAlive(HostRef ref); - -} -
--- a/storage/core/src/main/java/com/redhat/thermostat/storage/internal/Activator.java Fri Apr 14 06:11:10 2017 -0400 +++ b/storage/core/src/main/java/com/redhat/thermostat/storage/internal/Activator.java Fri May 05 12:10:15 2017 -0400 @@ -42,36 +42,28 @@ 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.common.ApplicationService; -import com.redhat.thermostat.common.MultipleServiceTracker; -import com.redhat.thermostat.common.MultipleServiceTracker.DependencyProvider; -import com.redhat.thermostat.common.TimerFactory; import com.redhat.thermostat.storage.core.Storage; import com.redhat.thermostat.storage.core.WriterID; import com.redhat.thermostat.storage.dao.AgentInfoDAO; import com.redhat.thermostat.storage.dao.BackendInfoDAO; -import com.redhat.thermostat.storage.dao.HostInfoDAO; import com.redhat.thermostat.storage.dao.NetworkInterfaceInfoDAO; import com.redhat.thermostat.storage.dao.SchemaInfoDAO; import com.redhat.thermostat.storage.dao.VmInfoDAO; import com.redhat.thermostat.storage.internal.dao.AgentInfoDAOImpl; import com.redhat.thermostat.storage.internal.dao.BackendInfoDAOImpl; -import com.redhat.thermostat.storage.internal.dao.HostInfoDAOImpl; import com.redhat.thermostat.storage.internal.dao.NetworkInterfaceInfoDAOImpl; import com.redhat.thermostat.storage.internal.dao.SchemaInfoDAOImpl; import com.redhat.thermostat.storage.internal.dao.VmInfoDAOImpl; -import com.redhat.thermostat.storage.monitor.HostMonitor; -import com.redhat.thermostat.storage.monitor.NetworkMonitor; -import com.redhat.thermostat.storage.monitor.internal.HostMonitorImpl; -import com.redhat.thermostat.storage.monitor.internal.NetworkMonitorImpl; public class Activator implements BundleActivator { private static final String WRITER_UUID = UUID.randomUUID().toString(); - MultipleServiceTracker tracker; + ServiceTracker tracker; List<ServiceRegistration<?>> regs; public Activator() { @@ -82,7 +74,6 @@ public void start(final BundleContext context) throws Exception { Class<?>[] deps = new Class<?>[] { Storage.class, - ApplicationService.class, }; // WriterID has to be registered unconditionally (at least not as part @@ -92,12 +83,10 @@ final ServiceRegistration<?> reg = context.registerService(WriterID.class, writerID, null); regs.add(reg); - tracker = new MultipleServiceTracker(context, deps, new MultipleServiceTracker.Action() { - - @Override - public void dependenciesAvailable(DependencyProvider services) { - - Storage storage = services.get(Storage.class); + tracker = new ServiceTracker(context, Storage.class.getName(), null) { + + public Object addingService(ServiceReference reference) { + Storage storage = (Storage) super.addingService(reference); SchemaInfoDAO schemaInfoDAO = new SchemaInfoDAOImpl(storage); ServiceRegistration<?> reg = context.registerService(SchemaInfoDAO.class.getName(), schemaInfoDAO, null); regs.add(reg); @@ -110,10 +99,6 @@ reg = context.registerService(BackendInfoDAO.class.getName(), backendInfoDao, null); regs.add(reg); - HostInfoDAO hostInfoDao = new HostInfoDAOImpl(storage, agentInfoDao); - reg = context.registerService(HostInfoDAO.class.getName(), hostInfoDao, null); - regs.add(reg); - NetworkInterfaceInfoDAO networkInfoDao = new NetworkInterfaceInfoDAOImpl(storage); reg = context.registerService(NetworkInterfaceInfoDAO.class.getName(), networkInfoDao, null); regs.add(reg); @@ -121,23 +106,16 @@ VmInfoDAO vmInfoDao = new VmInfoDAOImpl(storage); reg = context.registerService(VmInfoDAO.class.getName(), vmInfoDao, null); regs.add(reg); - - ApplicationService appService = services.get(ApplicationService.class); - TimerFactory timers = appService.getTimerFactory(); - NetworkMonitor networkMonitor = new NetworkMonitorImpl(timers, hostInfoDao); - reg = context.registerService(NetworkMonitor.class.getName(), networkMonitor, null); - regs.add(reg); - - HostMonitor hostMonitor = new HostMonitorImpl(timers, vmInfoDao); - reg = context.registerService(HostMonitor.class.getName(), hostMonitor, null); - regs.add(reg); + + return storage; } - + @Override - public void dependenciesUnavailable() { + public void removedService(ServiceReference reference, Object service) { unregisterServices(); + super.removedService(reference, service); } - }); + }; tracker.open(); }
--- a/storage/core/src/main/java/com/redhat/thermostat/storage/internal/dao/DAOImplCategoryRegistration.java Fri Apr 14 06:11:10 2017 -0400 +++ b/storage/core/src/main/java/com/redhat/thermostat/storage/internal/dao/DAOImplCategoryRegistration.java Fri May 05 12:10:15 2017 -0400 @@ -43,7 +43,6 @@ import com.redhat.thermostat.storage.core.auth.CategoryRegistration; import com.redhat.thermostat.storage.dao.AgentInfoDAO; import com.redhat.thermostat.storage.dao.BackendInfoDAO; -import com.redhat.thermostat.storage.dao.HostInfoDAO; import com.redhat.thermostat.storage.dao.NetworkInterfaceInfoDAO; import com.redhat.thermostat.storage.dao.VmInfoDAO; @@ -58,7 +57,6 @@ @Override public Set<String> getCategoryNames() { Set<String> categories = new HashSet<>(5); - categories.add(HostInfoDAO.hostInfoCategory.getName()); categories.add(AgentInfoDAO.CATEGORY.getName()); categories.add(VmInfoDAO.vmInfoCategory.getName()); categories.add(BackendInfoDAO.CATEGORY.getName());
--- a/storage/core/src/main/java/com/redhat/thermostat/storage/internal/dao/DAOImplStatementDescriptorRegistration.java Fri Apr 14 06:11:10 2017 -0400 +++ b/storage/core/src/main/java/com/redhat/thermostat/storage/internal/dao/DAOImplStatementDescriptorRegistration.java Fri May 05 12:10:15 2017 -0400 @@ -62,10 +62,6 @@ daoDescs.add(BackendInfoDAOImpl.QUERY_BACKEND_INFO); daoDescs.add(BackendInfoDAOImpl.DESC_ADD_BACKEND_INFO); daoDescs.add(BackendInfoDAOImpl.DESC_REMOVE_BACKEND_INFO); - daoDescs.add(HostInfoDAOImpl.QUERY_HOST_INFO); - daoDescs.add(HostInfoDAOImpl.QUERY_ALL_HOSTS); - daoDescs.add(HostInfoDAOImpl.AGGREGATE_COUNT_ALL_HOSTS); - daoDescs.add(HostInfoDAOImpl.DESC_ADD_HOST_INFO); daoDescs.add(NetworkInterfaceInfoDAOImpl.QUERY_NETWORK_INFO); daoDescs.add(NetworkInterfaceInfoDAOImpl.DESC_REPLACE_NETWORK_INFO); daoDescs.add(NetworkInterfaceInfoDAOImpl.AGGREGATE_COUNT_ALL_NETWORK_INTERFACES);
--- a/storage/core/src/main/java/com/redhat/thermostat/storage/internal/dao/HostInfoDAOImpl.java Fri Apr 14 06:11:10 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,191 +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.storage.internal.dao; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; -import java.util.logging.Logger; - -import com.redhat.thermostat.common.utils.LoggingUtils; -import com.redhat.thermostat.storage.core.AgentId; -import com.redhat.thermostat.storage.core.Category; -import com.redhat.thermostat.storage.core.CategoryAdapter; -import com.redhat.thermostat.storage.core.HostRef; -import com.redhat.thermostat.storage.core.Key; -import com.redhat.thermostat.storage.core.PreparedStatement; -import com.redhat.thermostat.storage.core.Storage; -import com.redhat.thermostat.storage.dao.AbstractDaoQuery; -import com.redhat.thermostat.storage.dao.AbstractDaoStatement; -import com.redhat.thermostat.storage.dao.AgentInfoDAO; -import com.redhat.thermostat.storage.dao.BaseCountable; -import com.redhat.thermostat.storage.dao.HostInfoDAO; -import com.redhat.thermostat.storage.dao.SimpleDaoQuery; -import com.redhat.thermostat.storage.model.AgentInformation; -import com.redhat.thermostat.storage.model.AggregateCount; -import com.redhat.thermostat.storage.model.HostInfo; - -public class HostInfoDAOImpl extends BaseCountable implements HostInfoDAO { - - private static final Logger logger = LoggingUtils.getLogger(HostInfoDAOImpl.class); - static final String QUERY_HOST_INFO = "QUERY " - + hostInfoCategory.getName() + " WHERE '" - + Key.AGENT_ID.getName() + "' = ?s LIMIT 1"; - static final String QUERY_ALL_HOSTS = "QUERY " + hostInfoCategory.getName(); - // We can use hostInfoCategory.getName() here since this query - // only changes the data class. When executed we use the adapted - // aggregate category. - static final String AGGREGATE_COUNT_ALL_HOSTS = "QUERY-COUNT " + hostInfoCategory.getName(); - // ADD host-info SET 'agentId' = ?s , \ - // 'hostname' = ?s , \ - // 'osName' = ?s , \ - // 'osKernel' = ?s , \ - // 'cpuModel' = ?s , \ - // 'cpuCount' = ?i , \ - // 'totalMemory' = ?l - static final String DESC_ADD_HOST_INFO = "ADD " + hostInfoCategory.getName() + - " SET '" + Key.AGENT_ID.getName() + "' = ?s , " + - "'" + hostNameKey.getName() + "' = ?s , " + - "'" + osNameKey.getName() + "' = ?s , " + - "'" + osKernelKey.getName() + "' = ?s , " + - "'" + cpuModelKey.getName() + "' = ?s , " + - "'" + cpuCountKey.getName() + "' = ?i , " + - "'" + hostMemoryTotalKey.getName() + "' = ?l"; - - private final Storage storage; - private final AgentInfoDAO agentInfoDao; - private final Category<AggregateCount> aggregateCategory; - - - public HostInfoDAOImpl(Storage storage, AgentInfoDAO agentInfo) { - this.storage = storage; - this.agentInfoDao = agentInfo; - // Adapt category to the aggregate form - CategoryAdapter<HostInfo, AggregateCount> adapter = new CategoryAdapter<>(hostInfoCategory); - this.aggregateCategory = adapter.getAdapted(AggregateCount.class); - storage.registerCategory(hostInfoCategory); - storage.registerCategory(aggregateCategory); - } - - @Override - public HostInfo getHostInfo(HostRef ref) { - return getHostInfo(new AgentId(ref.getAgentId())); - } - - @Override - public HostInfo getHostInfo(final AgentId agentId) { - return executeQuery(new AbstractDaoQuery<HostInfo>(storage, hostInfoCategory, QUERY_HOST_INFO) { - @Override - public PreparedStatement<HostInfo> customize(PreparedStatement<HostInfo> preparedStatement) { - preparedStatement.setString(0, agentId.get()); - return preparedStatement; - } - }).head(); - } - - @Override - public void putHostInfo(final HostInfo info) { - executeStatement(new AbstractDaoStatement<HostInfo>(storage, hostInfoCategory, DESC_ADD_HOST_INFO) { - @Override - public PreparedStatement<HostInfo> customize(PreparedStatement<HostInfo> preparedStatement) { - preparedStatement.setString(0, info.getAgentId()); - preparedStatement.setString(1, info.getHostname()); - preparedStatement.setString(2, info.getOsName()); - preparedStatement.setString(3, info.getOsKernel()); - preparedStatement.setString(4, info.getCpuModel()); - preparedStatement.setInt(5, info.getCpuCount()); - preparedStatement.setLong(6, info.getTotalMemory()); - return preparedStatement; - } - }); - } - - @Override - public Collection<HostRef> getHosts() { - List<HostRef> result = new ArrayList<>(); - for (HostInfo hostInfo : getAllHostInfos()) { - result.add(toHostRef(hostInfo)); - } - - return result; - } - - @Override - public List<HostInfo> getAllHostInfos() { - return executeQuery(new SimpleDaoQuery<>(storage, hostInfoCategory, QUERY_ALL_HOSTS)).asList(); - } - - @Override - public Collection<HostRef> getAliveHosts() { - List<HostRef> hosts = new ArrayList<>(); - List<AgentInformation> agentInfos = agentInfoDao.getAliveAgents(); - for (AgentInformation agentInfo : agentInfos) { - HostInfo hostInfo = getHostInfo(new AgentId(agentInfo.getAgentId())); - // getHostInfo may return null if user is not allowed to - // see the given host by ACL. - if (hostInfo != null) { - hosts.add(toHostRef(hostInfo)); - } - } - - return hosts; - } - - private HostRef toHostRef(HostInfo hostInfo) { - String agentId = hostInfo.getAgentId(); - String hostName = hostInfo.getHostname(); - return new HostRef(agentId, hostName); - } - - @Override - public long getCount() { - return getCount(storage, aggregateCategory, AGGREGATE_COUNT_ALL_HOSTS); - } - - @Override - public boolean isAlive(HostRef ref) { - AgentInformation info = agentInfoDao.getAgentInformation(ref); - return (info != null && info.isAlive()); - } - - @Override - protected Logger getLogger() { - return logger; - } - -} -
--- a/storage/core/src/main/java/com/redhat/thermostat/storage/model/HostInfo.java Fri Apr 14 06:11:10 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,130 +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.storage.model; - -import com.redhat.thermostat.storage.core.Entity; -import com.redhat.thermostat.storage.core.Persist; - -@Entity -public class HostInfo extends BasePojo { - - private String hostname; - private String osName; - private String osKernel; - private String cpuModel; - private int cpuCount; - private long totalMemory; - - public HostInfo() { - this(null, null, null, null, null, -1, -1); - } - - public HostInfo(String writerId, String hostname, String osName, String osKernel, String cpuModel, int cpuCount, long totalMemory) { - super(writerId); - this.hostname = hostname; - this.osName = osName; - this.osKernel = osKernel; - this.cpuModel = cpuModel; - this.cpuCount = cpuCount; - this.totalMemory = totalMemory; - } - - @Persist - public void setHostname(String hostname) { - this.hostname = hostname; - } - - @Persist - public void setOsName(String osName) { - this.osName = osName; - } - - @Persist - public void setOsKernel(String osKernel) { - this.osKernel = osKernel; - } - - @Persist - public void setCpuModel(String cpuModel) { - this.cpuModel = cpuModel; - } - - @Persist - public void setCpuCount(int cpuCount) { - this.cpuCount = cpuCount; - } - - @Persist - public void setTotalMemory(long totalMemory) { - this.totalMemory = totalMemory; - } - - @Persist - public String getHostname() { - return hostname; - } - - @Persist - public String getOsName() { - return osName; - } - - @Persist - public String getOsKernel() { - return osKernel; - } - - @Persist - public String getCpuModel() { - return cpuModel; - } - - @Persist - public int getCpuCount() { - return cpuCount; - } - - /** - * Total memory in bytes - */ - @Persist - public long getTotalMemory() { - return totalMemory; - } - -} -
--- a/storage/core/src/main/java/com/redhat/thermostat/storage/monitor/HostMonitor.java Fri Apr 14 06:11:10 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,72 +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.storage.monitor; - -import java.util.List; - -import com.redhat.thermostat.common.ActionListener; -import com.redhat.thermostat.common.Filter; -import com.redhat.thermostat.storage.core.HostRef; -import com.redhat.thermostat.storage.core.VmRef; - -/** - * Monitors selected {@link HostRef} for {@link VmRef} lifecycle changes. - */ -public interface HostMonitor { - - public enum Action { - VM_ADDED, - VM_REMOVED, - } - - /** - * Adds this listener to the given {@link HostRef} - */ - void addHostChangeListener(HostRef host, ActionListener<Action> listener); - - /** - * Removes the listener to the given {@link HostRef} - */ - void removeHostChangeListener(HostRef host, ActionListener<Action> listener); - - /** - * Returns all the {@link VmRef} tracked by the given host and matching the - * given {@link Filter}. - */ - List<VmRef> getVirtualMachines(HostRef host, Filter<VmRef> matcher); -} -
--- a/storage/core/src/main/java/com/redhat/thermostat/storage/monitor/NetworkMonitor.java Fri Apr 14 06:11:10 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,63 +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.storage.monitor; - -import java.util.List; - -import com.redhat.thermostat.common.ActionListener; -import com.redhat.thermostat.common.Filter; -import com.redhat.thermostat.storage.core.HostRef; - -/** - * Monitors the network for addition, removals of new {@link HostRef}. - */ -public interface NetworkMonitor { - - public enum Action { - HOST_ADDED, - HOST_REMOVED, - } - - /** - * Returns all the {@link HostRef} tracked matching the given {@link Filter}. - */ - List<HostRef> getHosts(Filter<HostRef> matcher); - - void addNetworkChangeListener(ActionListener<Action> listener); - void removeNetworkChangeListener(ActionListener<Action> listener); -} -
--- a/storage/core/src/main/java/com/redhat/thermostat/storage/monitor/internal/HostMonitorAction.java Fri Apr 14 06:11:10 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,88 +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.storage.monitor.internal; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; - -import com.redhat.thermostat.common.ActionNotifier; -import com.redhat.thermostat.storage.core.AgentId; -import com.redhat.thermostat.storage.core.HostRef; -import com.redhat.thermostat.storage.core.VmRef; -import com.redhat.thermostat.storage.dao.VmInfoDAO; -import com.redhat.thermostat.storage.model.VmInfo; -import com.redhat.thermostat.storage.monitor.HostMonitor; -import com.redhat.thermostat.storage.monitor.HostMonitor.Action; - -class HostMonitorAction extends MonitorAction<VmRef, HostMonitor.Action> { - - private VmInfoDAO vmsDao; - private HostRef host; - - public HostMonitorAction(ActionNotifier<Action> notifier, VmInfoDAO vmsDao, - HostRef host) - { - super(notifier); - this.host = host; - this.vmsDao = vmsDao; - } - - @Override - protected Action getAddAction() { - return HostMonitor.Action.VM_ADDED; - } - - @Override - protected Action getRemoveAction() { - return HostMonitor.Action.VM_REMOVED; - } - - @Override - protected Collection<VmRef> getNewReferences() { - List<VmInfo> vms = vmsDao.getAllVmInfosForAgent(new AgentId(host.getAgentId())); - Collection<VmRef> livingVMS = new ArrayList<>(); - for (VmInfo vmInfo : vms) { - if (vmInfo.isAlive()) { - VmRef vmRef = new VmRef(host, vmInfo); - livingVMS.add(vmRef); - } - } - return livingVMS; - } -} -
--- a/storage/core/src/main/java/com/redhat/thermostat/storage/monitor/internal/HostMonitorImpl.java Fri Apr 14 06:11:10 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,127 +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.storage.monitor.internal; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.TimeUnit; - -import com.redhat.thermostat.common.ActionListener; -import com.redhat.thermostat.common.ActionNotifier; -import com.redhat.thermostat.common.Filter; -import com.redhat.thermostat.common.Pair; -import com.redhat.thermostat.common.Timer; -import com.redhat.thermostat.common.TimerFactory; -import com.redhat.thermostat.storage.core.HostRef; -import com.redhat.thermostat.storage.core.VmRef; -import com.redhat.thermostat.storage.dao.VmInfoDAO; -import com.redhat.thermostat.storage.monitor.HostMonitor; - -public class HostMonitorImpl implements HostMonitor { - - static final long DELAY = 200; - - private VmInfoDAO vmDao; - private TimerFactory timerFactory; - - private Map<HostRef, Pair<Timer, ActionNotifier<HostMonitor.Action>>> listeners; - - public HostMonitorImpl(TimerFactory timerFactory, VmInfoDAO vmDao) { - this.vmDao = vmDao; - this.timerFactory = timerFactory; - listeners = new ConcurrentHashMap<>(); - } - - Map<HostRef, Pair<Timer, ActionNotifier<HostMonitor.Action>>> getListeners() { - return listeners; - } - - @Override - public List<VmRef> getVirtualMachines(HostRef host, Filter<VmRef> matcher) { - List<VmRef> vms = new ArrayList<>(); - Collection<VmRef> _vms = vmDao.getVMs(host); - for (VmRef vm : _vms) { - if (matcher.matches(vm)) { - vms.add(vm); - } - } - return vms; - } - - @Override - public void addHostChangeListener(HostRef host, - ActionListener<Action> listener) - { - Pair<Timer, ActionNotifier<HostMonitor.Action>> payload = - listeners.get(host); - if (payload == null) { - ActionNotifier<Action> notifier = new ActionNotifier<>(this); - Timer timer = timerFactory.createTimer(); - - timer.setTimeUnit(TimeUnit.MILLISECONDS); - timer.setDelay(DELAY); - timer.setSchedulingType(Timer.SchedulingType.FIXED_RATE); - timer.setAction(new HostMonitorAction(notifier, vmDao, host)); - timer.start(); - - payload = new Pair<>(timer, notifier); - listeners.put(host, payload); - } - - payload.getSecond().addActionListener(listener); - } - - @Override - public void removeHostChangeListener(HostRef host, - ActionListener<Action> listener) - { - Pair<Timer, ActionNotifier<HostMonitor.Action>> payload = - listeners.get(host); - if (payload != null) { - ActionNotifier<HostMonitor.Action> notifier = payload.getSecond(); - notifier.removeActionListener(listener); - if (notifier.listenersCount() == 0) { - payload.getFirst().stop(); - listeners.remove(host); - } - } - } -} -
--- a/storage/core/src/main/java/com/redhat/thermostat/storage/monitor/internal/MonitorAction.java Fri Apr 14 06:11:10 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,96 +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.storage.monitor.internal; - -import java.util.ArrayList; -import java.util.Collection; - -import com.redhat.thermostat.common.ActionNotifier; -import com.redhat.thermostat.storage.core.Ref; - -abstract class MonitorAction<R extends Ref, A extends Enum<?>> implements Runnable { - - private static final String LOCK = new String("MonitorAction_LOCK"); - - private ActionNotifier<A> notifier; - Collection<R> references; - - public MonitorAction(ActionNotifier<A> notifier) { - references = new ArrayList<>(); - this.notifier = notifier; - } - - @Override - public void run() { - Collection<R> newReferences = getNewReferences(); - Collection<R> _refs = null; - - synchronized (LOCK) { - _refs = new ArrayList<>(references); - references = new ArrayList<>(newReferences); - } - - handleRemovedHosts(_refs, newReferences); - handleAddedReferences(_refs, newReferences); - } - - private void handleAddedReferences(Collection<R> currentReference, - Collection<R> newReference) - { - Collection<R> copy = new ArrayList<>(newReference); - copy.removeAll(currentReference); - for (R reference : copy) { - notifier.fireAction(getAddAction(), reference); - } - } - - private void handleRemovedHosts(Collection<R> currentReference, - Collection<R> newReference) - { - Collection<R> copy = new ArrayList<>(currentReference); - copy.removeAll(newReference); - for (R reference : copy) { - notifier.fireAction(getRemoveAction(), reference); - } - } - - protected abstract A getAddAction(); - protected abstract A getRemoveAction(); - - protected abstract Collection<R> getNewReferences(); -} -
--- a/storage/core/src/main/java/com/redhat/thermostat/storage/monitor/internal/NetworkMonitorAction.java Fri Apr 14 06:11:10 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,75 +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.storage.monitor.internal; - -import java.util.Collection; - -import com.redhat.thermostat.common.ActionNotifier; - -import com.redhat.thermostat.storage.core.HostRef; -import com.redhat.thermostat.storage.dao.HostInfoDAO; - -import com.redhat.thermostat.storage.monitor.NetworkMonitor; -import com.redhat.thermostat.storage.monitor.NetworkMonitor.Action; - -class NetworkMonitorAction extends MonitorAction<HostRef, NetworkMonitor.Action> { - - private HostInfoDAO hostDAO; - - public NetworkMonitorAction(ActionNotifier<NetworkMonitor.Action> notifier, - HostInfoDAO hostDAO) - { - super(notifier); - this.hostDAO = hostDAO; - } - - @Override - protected Action getAddAction() { - return NetworkMonitor.Action.HOST_ADDED; - } - - @Override - protected Action getRemoveAction() { - return NetworkMonitor.Action.HOST_REMOVED; - } - - @Override - protected Collection<HostRef> getNewReferences() { - return hostDAO.getAliveHosts(); - } -} -
--- a/storage/core/src/main/java/com/redhat/thermostat/storage/monitor/internal/NetworkMonitorImpl.java Fri Apr 14 06:11:10 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,103 +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.storage.monitor.internal; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; -import java.util.concurrent.TimeUnit; - -import com.redhat.thermostat.common.ActionListener; -import com.redhat.thermostat.common.ActionNotifier; -import com.redhat.thermostat.common.Filter; -import com.redhat.thermostat.common.Timer; -import com.redhat.thermostat.common.TimerFactory; -import com.redhat.thermostat.storage.core.HostRef; -import com.redhat.thermostat.storage.dao.HostInfoDAO; -import com.redhat.thermostat.storage.monitor.NetworkMonitor; - -public class NetworkMonitorImpl implements NetworkMonitor { - - public static final long DELAY = 1; - - protected final ActionNotifier<NetworkMonitor.Action> notifier; - - private Timer timer; - private HostInfoDAO hostDAO; - - public NetworkMonitorImpl(TimerFactory timerFactory, HostInfoDAO hostDAO) { - - this.hostDAO = hostDAO; - - notifier = new ActionNotifier<>(this); - - timer = timerFactory.createTimer(); - timer.setTimeUnit(TimeUnit.SECONDS); - timer.setDelay(DELAY); - timer.setSchedulingType(Timer.SchedulingType.FIXED_RATE); - timer.setAction(new NetworkMonitorAction(notifier, hostDAO)); - } - - @Override - public List<HostRef> getHosts(Filter<HostRef> matcher) { - List<HostRef> hosts = new ArrayList<>(); - Collection<HostRef> _hosts = hostDAO.getHosts(); - for (HostRef host : _hosts) { - if (matcher.matches(host)) { - hosts.add(host); - } - } - return hosts; - } - - @Override - public void addNetworkChangeListener(ActionListener<Action> listener) { - notifier.addActionListener(listener); - if (notifier.listenersCount() == 1) { - timer.start(); - } - } - - @Override - public void removeNetworkChangeListener(ActionListener<Action> listener) { - notifier.removeActionListener(listener); - if (notifier.listenersCount() == 0) { - timer.stop(); - } - } -} -
--- a/storage/core/src/test/java/com/redhat/thermostat/storage/core/CategoryTest.java Fri Apr 14 06:11:10 2017 -0400 +++ b/storage/core/src/test/java/com/redhat/thermostat/storage/core/CategoryTest.java Fri May 05 12:10:15 2017 -0400 @@ -51,7 +51,6 @@ import org.junit.Test; -import com.redhat.thermostat.storage.dao.HostInfoDAO; import com.redhat.thermostat.storage.model.Pojo; public class CategoryTest { @@ -107,7 +106,9 @@ Key<String> key3 = new Key<String>("key3"); Category<TestObj> category = new Category<>("testEquals", TestObj.class, key1, key2, key3); assertTrue(category.equals(category)); - assertFalse(category.equals(HostInfoDAO.hostInfoCategory)); + Key<String> key4 = new Key<String>("key4"); + Category<TestObj> otherCategory = new Category<>("testEquals2", TestObj.class, key1, key2, key4); + assertFalse(category.equals(otherCategory)); } @Test
--- a/storage/core/src/test/java/com/redhat/thermostat/storage/core/DefaultHostsVMsLoaderTest.java Fri Apr 14 06:11:10 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,109 +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.storage.core; - -import static org.junit.Assert.*; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -import java.util.ArrayList; -import java.util.Collection; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -import com.redhat.thermostat.storage.core.DefaultHostsVMsLoader; -import com.redhat.thermostat.storage.core.HostRef; -import com.redhat.thermostat.storage.core.VmRef; -import com.redhat.thermostat.storage.dao.HostInfoDAO; -import com.redhat.thermostat.storage.dao.VmInfoDAO; - -public class DefaultHostsVMsLoaderTest { - - private HostInfoDAO mockHostsDAO; - private VmInfoDAO mockVmsDAO; - private DefaultHostsVMsLoader loader; - - @Before - public void setUp() throws Exception { - mockHostsDAO = mock(HostInfoDAO.class); - mockVmsDAO = mock(VmInfoDAO.class); - } - - @After - public void tearDown() throws Exception { - mockHostsDAO = null; - mockVmsDAO = null; - loader = null; - } - - @Test - public void canGetHosts() { - Collection<HostRef> expectedHosts = new ArrayList<>(); - expectedHosts.add(new HostRef("123", "fluffhost1")); - expectedHosts.add(new HostRef("456", "fluffhost2")); - expectedHosts.add(new HostRef("007", "deadHost")); - - loader = new DefaultHostsVMsLoader(mockHostsDAO, mockVmsDAO, false); - when(mockHostsDAO.getHosts()).thenReturn(expectedHosts); - assertEquals(loader.getHosts(), expectedHosts); - - loader = new DefaultHostsVMsLoader(mockHostsDAO, mockVmsDAO, true); - Collection<HostRef> aliveHosts = new ArrayList<>(); - expectedHosts.add(new HostRef("123", "fluffhost1")); - expectedHosts.add(new HostRef("456", "fluffhost2")); - when(mockHostsDAO.getAliveHosts()).thenReturn(aliveHosts); - assertEquals(loader.getHosts(), aliveHosts); - } - - @Test - public void canGetVms() { - HostRef hostR = mock(HostRef.class); - Collection<VmRef> expectedVms = new ArrayList<>(); - expectedVms.add(new VmRef(hostR, "321", 1, "test1")); - expectedVms.add(new VmRef(hostR, "654", 2, "test2")); - - loader = new DefaultHostsVMsLoader(mockHostsDAO, mockVmsDAO, false /* irrelevant */); - when(mockVmsDAO.getVMs(hostR)).thenReturn(expectedVms); - assertEquals(loader.getVMs(hostR), expectedVms); - - assertEquals(0, loader.getVMs(mock(HostRef.class)).size()); - } - -} -
--- a/storage/core/src/test/java/com/redhat/thermostat/storage/internal/ActivatorTest.java Fri Apr 14 06:11:10 2017 -0400 +++ b/storage/core/src/test/java/com/redhat/thermostat/storage/internal/ActivatorTest.java Fri May 05 12:10:15 2017 -0400 @@ -51,20 +51,14 @@ import com.redhat.thermostat.storage.core.WriterID; import com.redhat.thermostat.storage.dao.AgentInfoDAO; import com.redhat.thermostat.storage.dao.BackendInfoDAO; -import com.redhat.thermostat.storage.dao.HostInfoDAO; import com.redhat.thermostat.storage.dao.NetworkInterfaceInfoDAO; import com.redhat.thermostat.storage.dao.SchemaInfoDAO; import com.redhat.thermostat.storage.dao.VmInfoDAO; import com.redhat.thermostat.storage.internal.dao.AgentInfoDAOImpl; import com.redhat.thermostat.storage.internal.dao.BackendInfoDAOImpl; -import com.redhat.thermostat.storage.internal.dao.HostInfoDAOImpl; import com.redhat.thermostat.storage.internal.dao.NetworkInterfaceInfoDAOImpl; import com.redhat.thermostat.storage.internal.dao.SchemaInfoDAOImpl; import com.redhat.thermostat.storage.internal.dao.VmInfoDAOImpl; -import com.redhat.thermostat.storage.monitor.HostMonitor; -import com.redhat.thermostat.storage.monitor.NetworkMonitor; -import com.redhat.thermostat.storage.monitor.internal.HostMonitorImpl; -import com.redhat.thermostat.storage.monitor.internal.NetworkMonitorImpl; import com.redhat.thermostat.testutils.StubBundleContext; public class ActivatorTest { @@ -79,7 +73,7 @@ // WriterID should get registered unconditionally assertEquals("At least WriterID service must be registered", 1, context.getAllServices().size()); - assertEquals(2, context.getServiceListeners().size()); + assertEquals(1, context.getServiceListeners().size()); activator.stop(context); assertEquals(0, context.getAllServices().size()); @@ -107,7 +101,6 @@ assertTrue(context.isServiceRegistered(SchemaInfoDAO.class.getName(), SchemaInfoDAOImpl.class)); assertTrue(context.isServiceRegistered(WriterID.class.getName(), WriterIDImpl.class)); - assertTrue(context.isServiceRegistered(HostInfoDAO.class.getName(), HostInfoDAOImpl.class)); assertTrue(context.isServiceRegistered(NetworkInterfaceInfoDAO.class.getName(), NetworkInterfaceInfoDAOImpl.class)); assertTrue(context.isServiceRegistered(VmInfoDAO.class.getName(), VmInfoDAOImpl.class)); assertTrue(context.isServiceRegistered(AgentInfoDAO.class.getName(), AgentInfoDAOImpl.class)); @@ -142,7 +135,6 @@ activator.stop(context); assertFalse(context.isServiceRegistered(SchemaInfoDAO.class.getName(), SchemaInfoDAOImpl.class)); - assertFalse(context.isServiceRegistered(HostInfoDAO.class.getName(), HostInfoDAOImpl.class)); assertFalse(context.isServiceRegistered(NetworkInterfaceInfoDAO.class.getName(), NetworkInterfaceInfoDAOImpl.class)); assertFalse(context.isServiceRegistered(VmInfoDAO.class.getName(), VmInfoDAOImpl.class)); assertFalse(context.isServiceRegistered(AgentInfoDAO.class.getName(), AgentInfoDAOImpl.class)); @@ -171,11 +163,7 @@ activator.start(context); - assertTrue(context.isServiceRegistered(NetworkMonitor.class.getName(), NetworkMonitorImpl.class)); - assertTrue(context.isServiceRegistered(HostMonitor.class.getName(), HostMonitorImpl.class)); - assertTrue(context.isServiceRegistered(SchemaInfoDAO.class.getName(), SchemaInfoDAOImpl.class)); - assertTrue(context.isServiceRegistered(HostInfoDAO.class.getName(), HostInfoDAOImpl.class)); assertTrue(context.isServiceRegistered(NetworkInterfaceInfoDAO.class.getName(), NetworkInterfaceInfoDAOImpl.class)); assertTrue(context.isServiceRegistered(VmInfoDAO.class.getName(), VmInfoDAOImpl.class)); assertTrue(context.isServiceRegistered(AgentInfoDAO.class.getName(), AgentInfoDAOImpl.class)); @@ -190,10 +178,6 @@ activator.start(context); assertTrue(context.isServiceRegistered(SchemaInfoDAO.class.getName(), SchemaInfoDAOImpl.class)); - assertTrue(context.isServiceRegistered(NetworkMonitor.class.getName(), NetworkMonitorImpl.class)); - assertTrue(context.isServiceRegistered(HostMonitor.class.getName(), HostMonitorImpl.class)); - - assertTrue(context.isServiceRegistered(HostInfoDAO.class.getName(), HostInfoDAOImpl.class)); assertTrue(context.isServiceRegistered(NetworkInterfaceInfoDAO.class.getName(), NetworkInterfaceInfoDAOImpl.class)); assertTrue(context.isServiceRegistered(VmInfoDAO.class.getName(), VmInfoDAOImpl.class)); assertTrue(context.isServiceRegistered(AgentInfoDAO.class.getName(), AgentInfoDAOImpl.class));
--- a/storage/core/src/test/java/com/redhat/thermostat/storage/internal/dao/DAOImplCategoryRegistrationTest.java Fri Apr 14 06:11:10 2017 -0400 +++ b/storage/core/src/test/java/com/redhat/thermostat/storage/internal/dao/DAOImplCategoryRegistrationTest.java Fri May 05 12:10:15 2017 -0400 @@ -48,7 +48,6 @@ import com.redhat.thermostat.storage.core.auth.CategoryRegistration; import com.redhat.thermostat.storage.dao.AgentInfoDAO; import com.redhat.thermostat.storage.dao.BackendInfoDAO; -import com.redhat.thermostat.storage.dao.HostInfoDAO; import com.redhat.thermostat.storage.dao.NetworkInterfaceInfoDAO; import com.redhat.thermostat.storage.dao.VmInfoDAO; import com.redhat.thermostat.testutils.ServiceLoaderTest; @@ -63,9 +62,8 @@ public void registersAllCategories() { DAOImplCategoryRegistration reg = new DAOImplCategoryRegistration(); Set<String> categories = reg.getCategoryNames(); - assertEquals(6, categories.size()); + assertEquals(5, categories.size()); assertFalse("null descriptor not allowed", categories.contains(null)); - assertTrue(categories.contains(HostInfoDAO.hostInfoCategory.getName())); assertTrue(categories.contains(VmInfoDAO.vmInfoCategory.getName())); assertTrue(categories.contains(AgentInfoDAO.CATEGORY.getName())); assertTrue(categories.contains(NetworkInterfaceInfoDAO.networkInfoCategory.getName()));
--- a/storage/core/src/test/java/com/redhat/thermostat/storage/internal/dao/DAOImplStatementDescriptorRegistrationTest.java Fri Apr 14 06:11:10 2017 -0400 +++ b/storage/core/src/test/java/com/redhat/thermostat/storage/internal/dao/DAOImplStatementDescriptorRegistrationTest.java Fri May 05 12:10:15 2017 -0400 @@ -56,7 +56,7 @@ public void registersAllQueries() { DAOImplStatementDescriptorRegistration reg = new DAOImplStatementDescriptorRegistration(); Set<String> descriptors = reg.getStatementDescriptors(); - assertEquals(25, descriptors.size()); + assertEquals(21, descriptors.size()); assertFalse(descriptors.contains(null)); }
--- a/storage/core/src/test/java/com/redhat/thermostat/storage/internal/dao/HostInfoDAOTest.java Fri Apr 14 06:11:10 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,474 +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.storage.internal.dao; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertSame; -import static org.junit.Assert.assertTrue; -import static org.mockito.Matchers.any; -import static org.mockito.Mockito.atLeast; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -import java.util.Arrays; -import java.util.Collection; -import java.util.NoSuchElementException; - -import com.redhat.thermostat.storage.core.AgentId; -import org.junit.Test; -import org.mockito.ArgumentCaptor; -import org.mockito.Mockito; - -import com.redhat.thermostat.storage.core.Cursor; -import com.redhat.thermostat.storage.core.DescriptorParsingException; -import com.redhat.thermostat.storage.core.HostRef; -import com.redhat.thermostat.storage.core.Key; -import com.redhat.thermostat.storage.core.PreparedStatement; -import com.redhat.thermostat.storage.core.StatementDescriptor; -import com.redhat.thermostat.storage.core.StatementExecutionException; -import com.redhat.thermostat.storage.core.Storage; -import com.redhat.thermostat.storage.dao.AgentInfoDAO; -import com.redhat.thermostat.storage.dao.HostInfoDAO; -import com.redhat.thermostat.storage.model.AgentInformation; -import com.redhat.thermostat.storage.model.AggregateCount; -import com.redhat.thermostat.storage.model.HostInfo; - -public class HostInfoDAOTest { - - static class Triple<S, T, U> { - final S first; - final T second; - final U third; - - public Triple(S first, T second, U third) { - this.first = first; - this.second = second; - this.third = third; - } - } - - private static final String HOST_NAME = "a host name"; - private static final String OS_NAME = "some os"; - private static final String OS_KERNEL = "some kernel"; - private static final String CPU_MODEL = "some cpu that runs fast"; - private static final int CPU_NUM = -1; - private static final long MEMORY_TOTAL = 0xCAFEBABEl; - - @Test - public void preparedQueryDescriptorsAreSane() { - String expectedHostInfo = "QUERY host-info WHERE 'agentId' = ?s LIMIT 1"; - assertEquals(expectedHostInfo, HostInfoDAOImpl.QUERY_HOST_INFO); - String expectedAllHosts = "QUERY host-info"; - assertEquals(expectedAllHosts, HostInfoDAOImpl.QUERY_ALL_HOSTS); - String aggregateAllHosts = "QUERY-COUNT host-info"; - assertEquals(aggregateAllHosts, HostInfoDAOImpl.AGGREGATE_COUNT_ALL_HOSTS); - String addHostInfo = "ADD host-info SET 'agentId' = ?s , " + - "'hostname' = ?s , " + - "'osName' = ?s , " + - "'osKernel' = ?s , " + - "'cpuModel' = ?s , " + - "'cpuCount' = ?i , " + - "'totalMemory' = ?l"; - assertEquals(addHostInfo, HostInfoDAOImpl.DESC_ADD_HOST_INFO); - } - - @Test - public void testCategory() { - assertEquals("host-info", HostInfoDAO.hostInfoCategory.getName()); - Collection<Key<?>> keys = HostInfoDAO.hostInfoCategory.getKeys(); - assertTrue(keys.contains(new Key<>("agentId"))); - assertTrue(keys.contains(new Key<String>("hostname"))); - assertTrue(keys.contains(new Key<String>("osName"))); - assertTrue(keys.contains(new Key<String>("osKernel"))); - assertTrue(keys.contains(new Key<String>("cpuModel"))); - assertTrue(keys.contains(new Key<Integer>("cpuCount"))); - assertTrue(keys.contains(new Key<Long>("totalMemory"))); - assertEquals(7, keys.size()); - } - - @Test - public void testGetHostInfo() throws DescriptorParsingException, StatementExecutionException { - Storage storage = mock(Storage.class); - @SuppressWarnings("unchecked") - PreparedStatement<HostInfo> prepared = (PreparedStatement<HostInfo>) mock(PreparedStatement.class); - when(storage.prepareStatement(anyDescriptor())).thenReturn(prepared); - - HostInfo info = new HostInfo("foo-agent", HOST_NAME, OS_NAME, OS_KERNEL, CPU_MODEL, CPU_NUM, MEMORY_TOTAL); - @SuppressWarnings("unchecked") - Cursor<HostInfo> cursor = (Cursor<HostInfo>) mock(Cursor.class); - when(cursor.hasNext()).thenReturn(true).thenReturn(false); - when(cursor.next()).thenReturn(info).thenReturn(null); - when(prepared.executeQuery()).thenReturn(cursor); - AgentInfoDAO agentInfoDao = mock(AgentInfoDAO.class); - - HostInfo result = new HostInfoDAOImpl(storage, agentInfoDao).getHostInfo(new HostRef("some uid", HOST_NAME)); - - verify(storage).prepareStatement(anyDescriptor()); - verify(prepared).setString(0, "some uid"); - verify(prepared).executeQuery(); - assertSame(result, info); - } - - @Test - public void testGetHostInfoUsingAgentId() throws DescriptorParsingException, StatementExecutionException { - Storage storage = mock(Storage.class); - @SuppressWarnings("unchecked") - PreparedStatement<HostInfo> prepared = (PreparedStatement<HostInfo>) mock(PreparedStatement.class); - when(storage.prepareStatement(anyDescriptor())).thenReturn(prepared); - - HostInfo info = new HostInfo("foo-agent", HOST_NAME, OS_NAME, OS_KERNEL, CPU_MODEL, CPU_NUM, MEMORY_TOTAL); - @SuppressWarnings("unchecked") - Cursor<HostInfo> cursor = (Cursor<HostInfo>) mock(Cursor.class); - when(cursor.hasNext()).thenReturn(true).thenReturn(false); - when(cursor.next()).thenReturn(info).thenReturn(null); - when(prepared.executeQuery()).thenReturn(cursor); - AgentInfoDAO agentInfoDao = mock(AgentInfoDAO.class); - - HostInfo result = new HostInfoDAOImpl(storage, agentInfoDao).getHostInfo(new AgentId("some uid")); - - verify(storage).prepareStatement(anyDescriptor()); - verify(prepared).setString(0, "some uid"); - verify(prepared).executeQuery(); - assertSame(result, info); - } - - @SuppressWarnings("unchecked") - private StatementDescriptor<HostInfo> anyDescriptor() { - return (StatementDescriptor<HostInfo>) any(StatementDescriptor.class); - } - - @Test - public void testGetHostsSingleHost() throws DescriptorParsingException, StatementExecutionException { - - Storage storage = setupStorageForSingleHost(); - AgentInfoDAO agentInfo = mock(AgentInfoDAO.class); - - HostInfoDAO hostsDAO = new HostInfoDAOImpl(storage, agentInfo); - Collection<HostRef> hosts = hostsDAO.getHosts(); - - assertEquals(1, hosts.size()); - assertTrue(hosts.contains(new HostRef("123", "fluffhost1"))); - } - - private Storage setupStorageForSingleHost() throws DescriptorParsingException, StatementExecutionException { - HostInfo hostConfig = new HostInfo("foo-agent", "fluffhost1", OS_NAME, OS_KERNEL, CPU_MODEL, CPU_NUM, MEMORY_TOTAL); - hostConfig.setAgentId("123"); - - @SuppressWarnings("unchecked") - Cursor<HostInfo> cursor = (Cursor<HostInfo>) mock(Cursor.class); - when(cursor.hasNext()).thenReturn(true).thenReturn(false); - when(cursor.next()).thenReturn(hostConfig); - - Storage storage = mock(Storage.class); - @SuppressWarnings("unchecked") - PreparedStatement<HostInfo> stmt = (PreparedStatement<HostInfo>) mock(PreparedStatement.class); - when(storage.prepareStatement(anyDescriptor())).thenReturn(stmt); - when(stmt.executeQuery()).thenReturn(cursor); - return storage; - } - - @Test - public void testGetHosts3Hosts() throws DescriptorParsingException, StatementExecutionException { - - Storage storage = setupStorageFor3Hosts(); - AgentInfoDAO agentInfo = mock(AgentInfoDAO.class); - - HostInfoDAO hostsDAO = new HostInfoDAOImpl(storage, agentInfo); - Collection<HostRef> hosts = hostsDAO.getHosts(); - - assertEquals(3, hosts.size()); - assertTrue(hosts.contains(new HostRef("foo-agent-123", "fluffhost1"))); - assertTrue(hosts.contains(new HostRef("foo-agent-456", "fluffhost2"))); - assertTrue(hosts.contains(new HostRef("foo-agent-789", "fluffhost3"))); - } - - private Storage setupStorageFor3Hosts() throws DescriptorParsingException, StatementExecutionException { - - HostInfo hostConfig1 = new HostInfo("foo-agent-123", "fluffhost1", OS_NAME, OS_KERNEL, CPU_MODEL, CPU_NUM, MEMORY_TOTAL); - HostInfo hostConfig2 = new HostInfo("foo-agent-456", "fluffhost2", OS_NAME, OS_KERNEL, CPU_MODEL, CPU_NUM, MEMORY_TOTAL); - HostInfo hostConfig3 = new HostInfo("foo-agent-789", "fluffhost3", OS_NAME, OS_KERNEL, CPU_MODEL, CPU_NUM, MEMORY_TOTAL); - - @SuppressWarnings("unchecked") - Cursor<HostInfo> cursor = (Cursor<HostInfo>) mock(Cursor.class); - when(cursor.hasNext()).thenReturn(true).thenReturn(true).thenReturn(true).thenReturn(false); - when(cursor.next()).thenReturn(hostConfig1).thenReturn(hostConfig2).thenReturn(hostConfig3); - - Storage storage = mock(Storage.class); - @SuppressWarnings("unchecked") - PreparedStatement<HostInfo> stmt = (PreparedStatement<HostInfo>) mock(PreparedStatement.class); - when(storage.prepareStatement(anyDescriptor())).thenReturn(stmt); - when(stmt.executeQuery()).thenReturn(cursor); - - return storage; - } - - @SuppressWarnings("unchecked") - @Test - public void testPutHostInfo() throws DescriptorParsingException, - StatementExecutionException { - Storage storage = mock(Storage.class); - PreparedStatement<HostInfo> add = mock(PreparedStatement.class); - when(storage.prepareStatement(any(StatementDescriptor.class))).thenReturn(add); - - AgentInfoDAO agentInfo = mock(AgentInfoDAO.class); - - HostInfo info = new HostInfo("foo-agent", HOST_NAME, OS_NAME, OS_KERNEL, CPU_MODEL, CPU_NUM, MEMORY_TOTAL); - HostInfoDAO dao = new HostInfoDAOImpl(storage, agentInfo); - dao.putHostInfo(info); - - @SuppressWarnings("rawtypes") - ArgumentCaptor<StatementDescriptor> captor = ArgumentCaptor.forClass(StatementDescriptor.class); - - verify(storage).prepareStatement(captor.capture()); - StatementDescriptor<?> desc = captor.getValue(); - assertEquals(HostInfoDAOImpl.DESC_ADD_HOST_INFO, desc.getDescriptor()); - - verify(add).setString(0, info.getAgentId()); - verify(add).setString(1, info.getHostname()); - verify(add).setString(2, info.getOsName()); - verify(add).setString(3, info.getOsKernel()); - verify(add).setString(4, info.getCpuModel()); - verify(add).setInt(5, info.getCpuCount()); - verify(add).setLong(6, info.getTotalMemory()); - verify(add).execute(); - Mockito.verifyNoMoreInteractions(add); - } - - @Test - public void testGetCount() throws DescriptorParsingException, - StatementExecutionException { - AggregateCount count = new AggregateCount(); - count.setCount(2); - - @SuppressWarnings("unchecked") - Cursor<AggregateCount> c = (Cursor<AggregateCount>) mock(Cursor.class); - when(c.hasNext()).thenReturn(true).thenReturn(false); - when(c.next()).thenReturn(count).thenThrow(new NoSuchElementException()); - - Storage storage = mock(Storage.class); - @SuppressWarnings("unchecked") - PreparedStatement<AggregateCount> stmt = (PreparedStatement<AggregateCount>) mock(PreparedStatement.class); - @SuppressWarnings("unchecked") - StatementDescriptor<AggregateCount> desc = any(StatementDescriptor.class); - when(storage.prepareStatement(desc)).thenReturn(stmt); - when(stmt.executeQuery()).thenReturn(c); - HostInfoDAOImpl dao = new HostInfoDAOImpl(storage, null); - - assertEquals(2, dao.getCount()); - } - - @Test - public void getAliveHostSingle() throws DescriptorParsingException, StatementExecutionException { - Triple<Storage, AgentInfoDAO, PreparedStatement<HostInfo>> setup = setupForSingleAliveHost(); - Storage storage = setup.first; - AgentInfoDAO agentInfoDao = setup.second; - PreparedStatement<HostInfo> stmt = setup.third; - - HostInfoDAO hostsDAO = new HostInfoDAOImpl(storage, agentInfoDao); - Collection<HostRef> hosts = hostsDAO.getAliveHosts(); - - assertEquals(1, hosts.size()); - assertTrue(hosts.contains(new HostRef("123", "fluffhost1"))); - verify(storage).prepareStatement(anyDescriptor()); - verify(stmt).setString(0, "123"); - verify(stmt).executeQuery(); - } - - @Test - public void getAliveHostsEmptyDueToHostInfoBeingNull() throws DescriptorParsingException, StatementExecutionException { - Triple<Storage, AgentInfoDAO, PreparedStatement<HostInfo>> setup = setupForNullHostInfo(); - Storage storage = setup.first; - AgentInfoDAO agentInfoDao = setup.second; - PreparedStatement<HostInfo> stmt = setup.third; - - HostInfoDAO hostsDAO = new HostInfoDAOImpl(storage, agentInfoDao); - Collection<HostRef> hosts = hostsDAO.getAliveHosts(); - - assertEquals(0, hosts.size()); - verify(storage).prepareStatement(anyDescriptor()); - verify(stmt).setString(0, "123"); - verify(stmt).executeQuery(); - } - - private Triple<Storage, AgentInfoDAO, PreparedStatement<HostInfo>> setupForSingleAliveHost() - throws DescriptorParsingException, StatementExecutionException { - - // agents - - AgentInformation agentInfo1 = new AgentInformation("123"); - agentInfo1.setAlive(true); - - // hosts - - HostInfo hostConfig1 = new HostInfo(); - hostConfig1.setHostname("fluffhost1"); - hostConfig1.setAgentId("123"); - - HostInfo hostConfig2 = new HostInfo(); - hostConfig2.setHostname("fluffhost2"); - hostConfig2.setAgentId("456"); - - // cursor - - @SuppressWarnings("unchecked") - Cursor<HostInfo> cursor1 = mock(Cursor.class); - when(cursor1.hasNext()).thenReturn(true).thenReturn(false); - when(cursor1.next()).thenReturn(hostConfig1); - - // storage - - Storage storage = mock(Storage.class); - @SuppressWarnings("unchecked") - PreparedStatement<HostInfo> stmt = (PreparedStatement<HostInfo>) mock(PreparedStatement.class); - when(storage.prepareStatement(anyDescriptor())).thenReturn(stmt); - when(stmt.executeQuery()).thenReturn(cursor1); - - AgentInfoDAO agentDao = mock(AgentInfoDAO.class); - when(agentDao.getAliveAgents()).thenReturn(Arrays.asList(agentInfo1)); - - return new Triple<>(storage, agentDao, stmt); - } - - private Triple<Storage, AgentInfoDAO, PreparedStatement<HostInfo>> setupForNullHostInfo() - throws DescriptorParsingException, StatementExecutionException { - - // agents - - AgentInformation agentInfo1 = new AgentInformation("123"); - agentInfo1.setAlive(true); - - // cursor - - @SuppressWarnings("unchecked") - Cursor<HostInfo> cursor1 = mock(Cursor.class); - when(cursor1.hasNext()).thenReturn(false); - - // storage - - Storage storage = mock(Storage.class); - @SuppressWarnings("unchecked") - PreparedStatement<HostInfo> stmt = (PreparedStatement<HostInfo>) mock(PreparedStatement.class); - when(storage.prepareStatement(anyDescriptor())).thenReturn(stmt); - when(stmt.executeQuery()).thenReturn(cursor1); - - AgentInfoDAO agentDao = mock(AgentInfoDAO.class); - when(agentDao.getAliveAgents()).thenReturn(Arrays.asList(agentInfo1)); - - return new Triple<>(storage, agentDao, stmt); - } - - @Test - public void getAliveHost3() throws DescriptorParsingException, StatementExecutionException { - Triple<Storage, AgentInfoDAO, PreparedStatement<HostInfo>> setup = setupForAliveHost3(); - Storage storage = setup.first; - AgentInfoDAO agentInfoDao = setup.second; - PreparedStatement<HostInfo> stmt = setup.third; - - HostInfoDAO hostsDAO = new HostInfoDAOImpl(storage, agentInfoDao); - Collection<HostRef> hosts = hostsDAO.getAliveHosts(); - - // cursor 3 from the above storage should not be used - assertEquals(3, hosts.size()); - assertTrue(hosts.contains(new HostRef("123", "fluffhost1"))); - assertTrue(hosts.contains(new HostRef("456", "fluffhost2"))); - assertTrue(hosts.contains(new HostRef("678", "fluffhost3"))); - verify(storage, atLeast(3)).prepareStatement(anyDescriptor()); - verify(stmt).setString(0, "123"); - verify(stmt).setString(0, "456"); - verify(stmt).setString(0, "678"); - verify(stmt, atLeast(3)).executeQuery(); - } - - private Triple<Storage, AgentInfoDAO, PreparedStatement<HostInfo>> setupForAliveHost3() - throws DescriptorParsingException, StatementExecutionException { - - // agents - AgentInformation agentInfo1 = new AgentInformation("123"); - agentInfo1.setAlive(true); - - AgentInformation agentInfo2 = new AgentInformation("456"); - agentInfo2.setAlive(true); - - AgentInformation agentInfo3 = new AgentInformation("678"); - agentInfo3.setAlive(true); - - // hosts - - HostInfo hostConfig1 = new HostInfo(); - hostConfig1.setHostname("fluffhost1"); - hostConfig1.setAgentId("123"); - - HostInfo hostConfig2 = new HostInfo(); - hostConfig2.setHostname("fluffhost2"); - hostConfig2.setAgentId("456"); - - HostInfo hostConfig3 = new HostInfo(); - hostConfig3.setHostname("fluffhost3"); - hostConfig3.setAgentId("678"); - - @SuppressWarnings("unchecked") - Cursor<HostInfo> cursor1 = mock(Cursor.class); - when(cursor1.hasNext()).thenReturn(true).thenReturn(false); - when(cursor1.next()).thenReturn(hostConfig1); - - @SuppressWarnings("unchecked") - Cursor<HostInfo> cursor2 = mock(Cursor.class); - when(cursor2.hasNext()).thenReturn(true).thenReturn(false); - when(cursor2.next()).thenReturn(hostConfig2); - - @SuppressWarnings("unchecked") - Cursor<HostInfo> cursor3 = mock(Cursor.class); - when(cursor3.hasNext()).thenReturn(true).thenReturn(false); - when(cursor3.next()).thenReturn(hostConfig3); - - // storage - - Storage storage = mock(Storage.class); - @SuppressWarnings("unchecked") - PreparedStatement<HostInfo> stmt = (PreparedStatement<HostInfo>) mock(PreparedStatement.class); - when(storage.prepareStatement(anyDescriptor())).thenReturn(stmt); - when(stmt.executeQuery()).thenReturn(cursor1).thenReturn(cursor2).thenReturn(cursor3); - - AgentInfoDAO agentDao = mock(AgentInfoDAO.class); - when(agentDao.getAliveAgents()).thenReturn(Arrays.asList(agentInfo1, agentInfo2, agentInfo3)); - - return new Triple<>(storage, agentDao, stmt); - } - -} -
--- a/storage/core/src/test/java/com/redhat/thermostat/storage/model/PojoModelInstantiationTest.java Fri Apr 14 06:11:10 2017 -0400 +++ b/storage/core/src/test/java/com/redhat/thermostat/storage/model/PojoModelInstantiationTest.java Fri May 05 12:10:15 2017 -0400 @@ -45,7 +45,6 @@ BackendInformation.class, NetworkInterfaceInfo.class, VmInfo.class, - HostInfo.class }; @Override
--- a/storage/core/src/test/java/com/redhat/thermostat/storage/monitor/internal/HostMonitorActionTest.java Fri Apr 14 06:11:10 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,148 +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.storage.monitor.internal; - -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.any; -import static org.mockito.Mockito.eq; -import static org.mockito.Mockito.times; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; - -import com.redhat.thermostat.storage.core.AgentId; -import org.junit.Before; -import org.junit.Test; - -import com.redhat.thermostat.common.ActionNotifier; -import com.redhat.thermostat.storage.core.HostRef; -import com.redhat.thermostat.storage.core.VmRef; -import com.redhat.thermostat.storage.dao.VmInfoDAO; -import com.redhat.thermostat.storage.model.VmInfo; -import com.redhat.thermostat.storage.monitor.HostMonitor; -import com.redhat.thermostat.storage.monitor.HostMonitor.Action; - -public class HostMonitorActionTest { - - private VmInfoDAO vmsDAO; - - private ActionNotifier<HostMonitor.Action> notifier; - - @SuppressWarnings("unchecked") - @Before - public void setup() { - vmsDAO = mock(VmInfoDAO.class); - notifier = mock(ActionNotifier.class); - } - - @Test - public void testAddRemoveVMS() { - - HostRef host = new HostRef("01", "01"); - - List<VmInfo> currentVMs = new ArrayList<>(); - - VmInfo info_a = mock(VmInfo.class); - when(info_a.isAlive()).thenReturn(true); - when(info_a.getVmId()).thenReturn("0"); - when(info_a.getVmPid()).thenReturn(0); - when(info_a.getVmName()).thenReturn("0"); - - VmInfo info_b = mock(VmInfo.class); - when(info_b.isAlive()).thenReturn(true); - when(info_b.getVmId()).thenReturn("1"); - when(info_b.getVmPid()).thenReturn(1); - when(info_b.getVmName()).thenReturn("1"); - - VmInfo info_c = mock(VmInfo.class); - when(info_c.isAlive()).thenReturn(true); - when(info_c.getVmId()).thenReturn("2"); - when(info_c.getVmPid()).thenReturn(2); - when(info_c.getVmName()).thenReturn("2"); - - VmInfo info_d = mock(VmInfo.class); - when(info_d.isAlive()).thenReturn(true); - when(info_d.getVmId()).thenReturn("3"); - when(info_d.getVmPid()).thenReturn(3); - when(info_d.getVmName()).thenReturn("3"); - - VmInfo info_e = mock(VmInfo.class); - when(info_e.isAlive()).thenReturn(false); - when(info_e.getVmId()).thenReturn("4"); - when(info_e.getVmPid()).thenReturn(4); - when(info_e.getVmName()).thenReturn("4"); - - currentVMs.add(info_a); - currentVMs.add(info_b); - currentVMs.add(info_c); - currentVMs.add(info_d); - currentVMs.add(info_e); - - when(vmsDAO.getAllVmInfosForAgent(any(AgentId.class))).thenReturn(currentVMs); - - - // the first result is to be notified of all those vms - HostMonitorAction action = new HostMonitorAction(notifier, vmsDAO, host); - action.run(); - - verify(notifier, times(4)).fireAction(eq(Action.VM_ADDED), any(VmRef.class)); - verify(notifier, times(0)).fireAction(eq(Action.VM_REMOVED), any(VmRef.class)); - - // now remove one vm from each side - currentVMs.remove(info_b); - currentVMs.remove(info_c); - - action.run(); - - verify(notifier, times(4)).fireAction(eq(Action.VM_ADDED), any(VmRef.class)); - verify(notifier, times(2)).fireAction(eq(Action.VM_REMOVED), any(VmRef.class)); - - when(info_a.isAlive()).thenReturn(false); - - // not that a process can ever become alive again :) - when(info_e.isAlive()).thenReturn(true); - - action.run(); - - verify(notifier, times(5)).fireAction(eq(Action.VM_ADDED), any(VmRef.class)); - verify(notifier, times(3)).fireAction(eq(Action.VM_REMOVED), any(VmRef.class)); - } -} -
--- a/storage/core/src/test/java/com/redhat/thermostat/storage/monitor/internal/HostMonitorImplTest.java Fri Apr 14 06:11:10 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,185 +0,0 @@ -/* - * Copyright 2012-2017 Red Hat, Inc. - * - * This file is part of Thermostat. - * - * Thermostat is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published - * by the Free Software Foundation; either version 2, or (at your - * option) any later version. - * - * Thermostat is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Thermostat; see the file COPYING. If not see - * <http://www.gnu.org/licenses/>. - * - * Linking this code with other modules is making a combined work - * based on this code. Thus, the terms and conditions of the GNU - * General Public License cover the whole combination. - * - * As a special exception, the copyright holders of this code give - * you permission to link this code with independent modules to - * produce an executable, regardless of the license terms of these - * independent modules, and to copy and distribute the resulting - * executable under terms of your choice, provided that you also - * meet, for each linked independent module, the terms and conditions - * of the license of that module. An independent module is a module - * which is not derived from or based on this code. If you modify - * this code, you may extend this exception to your version of the - * library, but you are not obligated to do so. If you do not wish - * to do so, delete this exception statement from your version. - */ - -package com.redhat.thermostat.storage.monitor.internal; - -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.any; -import static org.mockito.Mockito.times; - -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.assertEquals; - -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.concurrent.TimeUnit; - -import org.junit.Before; -import org.junit.Test; - -import com.redhat.thermostat.common.ActionListener; -import com.redhat.thermostat.common.ActionNotifier; -import com.redhat.thermostat.common.AllPassFilter; -import com.redhat.thermostat.common.Filter; -import com.redhat.thermostat.common.Pair; -import com.redhat.thermostat.common.Timer; -import com.redhat.thermostat.common.TimerFactory; -import com.redhat.thermostat.storage.core.HostRef; -import com.redhat.thermostat.storage.core.VmRef; -import com.redhat.thermostat.storage.dao.VmInfoDAO; -import com.redhat.thermostat.storage.monitor.HostMonitor; - -public class HostMonitorImplTest { - - private VmInfoDAO vmDao; - private TimerFactory timerFactory; - private Timer timer1; - - @Before - public void setup() { - vmDao = mock(VmInfoDAO.class); - timerFactory = mock(TimerFactory.class); - timer1 = mock(Timer.class); - when(timerFactory.createTimer()).thenReturn(timer1); - } - - @Test - public void testGetVirtualMachines() { - List<VmRef> testData = new ArrayList<>(); - List<VmRef> testData2 = new ArrayList<>(); - - HostRef host0 = new HostRef("0", "0"); - HostRef host1 = new HostRef("1", "1"); - - VmRef vm0 = new VmRef(host0, "0", 0, "0"); - VmRef vm1 = new VmRef(host0, "1", 1, "2"); - VmRef vm2 = new VmRef(host0, "2", 2, "3"); - VmRef vm3 = new VmRef(host0, "3", 3, "3"); - VmRef vm4 = new VmRef(host0, "4", 4, "4"); - VmRef vm5 = new VmRef(host0, "5", 5, "5"); - - testData.add(vm0); - testData.add(vm1); - testData.add(vm2); - testData.add(vm3); - testData.add(vm4); - testData.add(vm5); - - when(vmDao.getVMs(host0)).thenReturn(testData); - when(vmDao.getVMs(host1)).thenReturn(testData2); - - HostMonitor monitor = new HostMonitorImpl(timerFactory, vmDao); - List<VmRef> vms = monitor.getVirtualMachines(host0, new AllPassFilter<VmRef>()); - assertEquals(testData.size(), vms.size()); - for (VmRef ref : testData) { - assertTrue(vms.contains(ref)); - } - - vms = monitor.getVirtualMachines(host1, new AllPassFilter<VmRef>()); - assertEquals(0, vms.size()); - - Filter<VmRef> bandFilter = new Filter<VmRef>() { - @Override - public boolean matches(VmRef toMatch) { - return toMatch.getName().equals("1") || - toMatch.getName().equals("2") || - toMatch.getName().equals("3"); - } - }; - - vms = monitor.getVirtualMachines(host0, bandFilter); - assertEquals(3, vms.size()); - - assertTrue(vms.contains(vm1)); - assertTrue(vms.contains(vm2)); - assertTrue(vms.contains(vm3)); - } - - @SuppressWarnings("unchecked") - @Test - public void test() { - ActionListener<HostMonitor.Action> listener1 = mock(ActionListener.class); - ActionListener<HostMonitor.Action> listener2 = mock(ActionListener.class); - - HostRef host1 = new HostRef("0", "0"); - - HostMonitor monitor = new HostMonitorImpl(timerFactory, vmDao); - Map<HostRef, Pair<Timer, ActionNotifier<HostMonitor.Action>>> listeners = - ((HostMonitorImpl) monitor).getListeners(); - assertTrue(listeners.isEmpty()); - - monitor.addHostChangeListener(host1, listener1); - - assertEquals(1, listeners.size()); - - verify(timer1, times(1)).setTimeUnit(TimeUnit.MILLISECONDS); - verify(timer1, times(1)).setDelay(HostMonitorImpl.DELAY); - verify(timer1, times(1)).setSchedulingType(Timer.SchedulingType.FIXED_RATE); - verify(timer1, times(1)).start(); - - verify(timer1).setAction(any(HostMonitorAction.class)); - - monitor.addHostChangeListener(host1, listener2); - - assertEquals(1, listeners.size()); - - verify(timer1, times(1)).setTimeUnit(TimeUnit.MILLISECONDS); - verify(timer1, times(1)).setDelay(HostMonitorImpl.DELAY); - verify(timer1, times(1)).start(); - verify(timer1, times(1)).setSchedulingType(Timer.SchedulingType.FIXED_RATE); - - verify(timer1).setAction(any(HostMonitorAction.class)); - - monitor.removeHostChangeListener(host1, listener1); - verify(timer1, times(0)).stop(); - - assertEquals(1, listeners.size()); - monitor.removeHostChangeListener(host1, listener2); - - assertTrue(listeners.isEmpty()); - verify(timer1, times(1)).stop(); - - HostRef host2 = new HostRef("1", "1"); - - monitor.addHostChangeListener(host1, listener1); - monitor.addHostChangeListener(host2, listener2); - } - -} -
--- a/storage/core/src/test/java/com/redhat/thermostat/storage/monitor/internal/NetworkMonitorActionTest.java Fri Apr 14 06:11:10 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,126 +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.storage.monitor.internal; - -import static org.mockito.Matchers.any; -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.ArrayList; -import java.util.Collection; - -import org.junit.Before; -import org.junit.Test; - -import com.redhat.thermostat.common.ActionNotifier; -import com.redhat.thermostat.storage.core.HostRef; -import com.redhat.thermostat.storage.dao.HostInfoDAO; -import com.redhat.thermostat.storage.monitor.NetworkMonitor; -import com.redhat.thermostat.storage.monitor.NetworkMonitor.Action; - -public class NetworkMonitorActionTest { - - private HostInfoDAO hostDAO; - - private ActionNotifier<NetworkMonitor.Action> notifier; - - @SuppressWarnings("unchecked") - @Before - public void setup() { - hostDAO = mock(HostInfoDAO.class); - notifier = mock(ActionNotifier.class); - } - - @Test - public void testAddRemoveHost() { - - Collection<HostRef> currentHosts = new ArrayList<>(); - HostRef a = new HostRef("0", "a"); - HostRef b = new HostRef("1", "b"); - HostRef c = new HostRef("2", "c"); - HostRef d = new HostRef("3", "d"); - - currentHosts.add(a); - currentHosts.add(b); - currentHosts.add(c); - currentHosts.add(d); - - when(hostDAO.getAliveHosts()).thenReturn(currentHosts); - - // the first result is to be notified of all those hosts - NetworkMonitorAction action = new NetworkMonitorAction(notifier, hostDAO); - action.run(); - - verify(notifier).fireAction(Action.HOST_ADDED, a); - verify(notifier).fireAction(Action.HOST_ADDED, b); - verify(notifier).fireAction(Action.HOST_ADDED, c); - verify(notifier).fireAction(Action.HOST_ADDED, d); - - verify(notifier, times(0)).fireAction(Action.HOST_REMOVED, eq(any(HostRef.class))); - - // now remove a from the series, add e - HostRef e = new HostRef("4", "e"); - currentHosts.add(e); - currentHosts.remove(a); - - action.run(); - - verify(notifier).fireAction(Action.HOST_REMOVED, a); - verify(notifier).fireAction(Action.HOST_ADDED, e); - - // now add f from the series, no host removal - HostRef f = new HostRef("5", "f"); - currentHosts.add(f); - - action.run(); - - verify(notifier).fireAction(Action.HOST_ADDED, f); - verify(notifier, times(0)).fireAction(Action.HOST_REMOVED, eq(any(HostRef.class))); - - // now only remove f from the series, no other changes - currentHosts.remove(f); - - action.run(); - - verify(notifier).fireAction(Action.HOST_REMOVED, f); - verify(notifier, times(0)).fireAction(Action.HOST_ADDED, eq(any(HostRef.class))); - } -} -
--- a/storage/core/src/test/java/com/redhat/thermostat/storage/monitor/internal/NetworkMonitorImplTest.java Fri Apr 14 06:11:10 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,152 +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.storage.monitor.internal; - -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; -import static org.mockito.Mockito.times; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; - -import java.util.ArrayList; -import java.util.List; -import java.util.concurrent.TimeUnit; - -import org.junit.Before; -import org.junit.Test; - -import com.redhat.thermostat.common.ActionListener; -import com.redhat.thermostat.common.AllPassFilter; -import com.redhat.thermostat.common.Filter; -import com.redhat.thermostat.common.Timer; -import com.redhat.thermostat.common.TimerFactory; -import com.redhat.thermostat.storage.core.HostRef; -import com.redhat.thermostat.storage.dao.HostInfoDAO; -import com.redhat.thermostat.storage.monitor.NetworkMonitor; -import com.redhat.thermostat.storage.monitor.NetworkMonitor.Action; - -public class NetworkMonitorImplTest { - - private HostInfoDAO hostDao; - private TimerFactory timerFactory; - private Timer timer; - - @Before - public void setup() { - hostDao = mock(HostInfoDAO.class); - timerFactory = mock(TimerFactory.class); - timer = mock(Timer.class); - when(timerFactory.createTimer()).thenReturn(timer); - } - - @Test - public void testGetHost() { - List<HostRef> testData = new ArrayList<>(); - HostRef ref0 = new HostRef("0", "test#0"); - HostRef ref1 = new HostRef("1", "test#1"); - HostRef ref2 = new HostRef("2", "test#2"); - HostRef ref3 = new HostRef("3", "test#3"); - HostRef ref4 = new HostRef("4", "test#4"); - HostRef ref5 = new HostRef("5", "test#5"); - - testData.add(ref0); - testData.add(ref1); - testData.add(ref2); - testData.add(ref3); - testData.add(ref4); - testData.add(ref5); - - when(hostDao.getHosts()).thenReturn(testData); - - NetworkMonitor monitor = new NetworkMonitorImpl(timerFactory, hostDao); - List<HostRef> hosts = monitor.getHosts(new AllPassFilter<HostRef>()); - assertEquals(testData.size(), hosts.size()); - - for (HostRef ref : testData) { - assertTrue(hosts.contains(ref)); - } - - Filter<HostRef> bandFilter = new Filter<HostRef>() { - @Override - public boolean matches(HostRef toMatch) { - return toMatch.getName().equals("test#1") || - toMatch.getName().equals("test#2") || - toMatch.getName().equals("test#3"); - } - }; - - hosts = monitor.getHosts(bandFilter); - assertEquals(3, hosts.size()); - - assertTrue(hosts.contains(ref1)); - assertTrue(hosts.contains(ref2)); - assertTrue(hosts.contains(ref3)); - } - - @SuppressWarnings("unchecked") - @Test - public void test() { - - ActionListener<Action> listener1 = mock(ActionListener.class); - ActionListener<Action> listener2 = mock(ActionListener.class); - - NetworkMonitor monitor = new NetworkMonitorImpl(timerFactory, hostDao); - monitor.addNetworkChangeListener(listener1); - - verify(timer).setTimeUnit(TimeUnit.SECONDS); - verify(timer).setDelay(NetworkMonitorImpl.DELAY); - verify(timer).setSchedulingType(Timer.SchedulingType.FIXED_RATE); - - verify(timer).start(); - verify(timer, times(0)).stop(); - - monitor.addNetworkChangeListener(listener2); - - verify(timer, times(1)).start(); - verify(timer, times(0)).stop(); - - monitor.removeNetworkChangeListener(listener1); - verify(timer, times(0)).stop(); - verify(timer, times(1)).start(); - - monitor.removeNetworkChangeListener(listener2); - verify(timer).stop(); - } - -} -
--- a/storage/mongo/src/test/java/com/redhat/thermostat/storage/mongodb/internal/MongoStorageTest.java Fri Apr 14 06:11:10 2017 -0400 +++ b/storage/mongo/src/test/java/com/redhat/thermostat/storage/mongodb/internal/MongoStorageTest.java Fri May 05 12:10:15 2017 -0400 @@ -57,7 +57,6 @@ import java.util.NoSuchElementException; import java.util.concurrent.CountDownLatch; -import com.redhat.thermostat.common.internal.test.Bug; import org.bson.Document; import org.bson.conversions.Bson; import org.junit.After; @@ -81,6 +80,7 @@ import com.mongodb.client.model.UpdateOptions; import com.mongodb.client.result.DeleteResult; import com.mongodb.client.result.UpdateResult; +import com.redhat.thermostat.common.internal.test.Bug; import com.redhat.thermostat.shared.config.SSLConfiguration; import com.redhat.thermostat.storage.core.Add; import com.redhat.thermostat.storage.core.AggregateQuery; @@ -103,11 +103,9 @@ import com.redhat.thermostat.storage.core.Statement; import com.redhat.thermostat.storage.core.StorageCredentials; import com.redhat.thermostat.storage.core.Update; -import com.redhat.thermostat.storage.dao.HostInfoDAO; import com.redhat.thermostat.storage.model.AggregateCount; import com.redhat.thermostat.storage.model.BasePojo; import com.redhat.thermostat.storage.model.DistinctResult; -import com.redhat.thermostat.storage.model.HostInfo; import com.redhat.thermostat.storage.model.Pojo; import com.redhat.thermostat.storage.query.Expression; import com.redhat.thermostat.storage.query.ExpressionFactory; @@ -383,13 +381,15 @@ @SuppressWarnings("unchecked") com.mongodb.client.MongoCursor<String> mockCursor = mock(com.mongodb.client.MongoCursor.class); when(mockIterable.iterator()).thenReturn(mockCursor); - storage.registerCategory(HostInfoDAO.hostInfoCategory); - Category<AggregateCount> countCat = new CategoryAdapter<HostInfo, AggregateCount>(HostInfoDAO.hostInfoCategory).getAdapted(AggregateCount.class); + Key<String> key = new Key<>("key1"); + Category<TestObj> category = new Category<>("testRegisterCategory", TestObj.class, key); + storage.registerCategory(category); + Category<AggregateCount> countCat = new CategoryAdapter<TestObj, AggregateCount>(category).getAdapted(AggregateCount.class); storage.registerCategory(countCat); ArgumentCaptor<Document> msgCaptor = ArgumentCaptor.forClass(Document.class); ArgumentCaptor<Document> msgCaptor2 = ArgumentCaptor.forClass(Document.class); - Document expectedObject1 = new Document(SchemaInfo.NAME.getName(), HostInfoDAO.hostInfoCategory.getName()); + Document expectedObject1 = new Document(SchemaInfo.NAME.getName(), category.getName()); verify(testCollection).replaceOne(msgCaptor.capture(), msgCaptor2.capture(), any(UpdateOptions.class)); @@ -400,9 +400,13 @@ assertTrue(resultObject2.get(SchemaInfo.NAME.getName()) != null); assertTrue(resultObject2.get(Key.TIMESTAMP.getName()) != null); - assertEquals(HostInfoDAO.hostInfoCategory.getName(), resultObject2.get(SchemaInfo.NAME.getName())); + assertEquals(category.getName(), resultObject2.get(SchemaInfo.NAME.getName())); assertNotNull(resultObject2.get(Key.TIMESTAMP.getName())); } + + private static class TestObj implements Pojo { + // Dummy class for testing. + } @Test public void verifyFindAllReturnsCursor() throws Exception {
--- a/system-backend/src/main/java/com/redhat/thermostat/backend/system/internal/HostInfoBuilderImpl.java Fri Apr 14 06:11:10 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,73 +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.backend.system.internal; - -import com.redhat.thermostat.common.portability.PortableHost; -import com.redhat.thermostat.backend.system.internal.models.HostInfoBuilder; -import com.redhat.thermostat.common.portability.PortableHostImpl; -import com.redhat.thermostat.storage.core.WriterID; -import com.redhat.thermostat.storage.model.HostInfo; - -/** - * Build Host information via helper classes - */ -class HostInfoBuilderImpl implements HostInfoBuilder { - - private final WriterID writerID; - private final PortableHost helper; - - HostInfoBuilderImpl(final WriterID writerID) { - this(writerID, PortableHostImpl.getInstance()); - } - - HostInfoBuilderImpl(final WriterID writerID, PortableHost helper) { - this.writerID = writerID; - this.helper = helper; - } - - @Override - public HostInfo build() { - String wId = writerID.getWriterID(); - return new HostInfo(wId, - helper.getHostName(), - helper.getOSName(), - helper.getOSVersion(), - helper.getCPUModel(), - helper.getCPUCount(), - helper.getTotalMemory()); - } -}
--- a/system-backend/src/main/java/com/redhat/thermostat/backend/system/internal/InfoBuilderFactoryImpl.java Fri Apr 14 06:11:10 2017 -0400 +++ b/system-backend/src/main/java/com/redhat/thermostat/backend/system/internal/InfoBuilderFactoryImpl.java Fri May 05 12:10:15 2017 -0400 @@ -36,14 +36,12 @@ package com.redhat.thermostat.backend.system.internal; +import com.redhat.thermostat.backend.system.internal.models.InfoBuilderFactory; +import com.redhat.thermostat.backend.system.internal.models.ProcessEnvironmentBuilder; import com.redhat.thermostat.common.portability.ProcessUserInfo; import com.redhat.thermostat.common.portability.ProcessUserInfoBuilder; import com.redhat.thermostat.common.portability.UserNameUtil; -import com.redhat.thermostat.backend.system.internal.models.HostInfoBuilder; -import com.redhat.thermostat.backend.system.internal.models.InfoBuilderFactory; -import com.redhat.thermostat.backend.system.internal.models.ProcessEnvironmentBuilder; import com.redhat.thermostat.common.portability.linux.ProcDataSource; -import com.redhat.thermostat.storage.core.WriterID; /** * Allows callers to access Windows-specific builders portably @@ -53,10 +51,6 @@ public InfoBuilderFactoryImpl() { } - public HostInfoBuilder createHostInfoBuilder(final WriterID writerID) { - return new HostInfoBuilderImpl(writerID); - } - public ProcessEnvironmentBuilder createProcessEnvironmentBuilder() { return new ProcessEnvironmentBuilderImpl(); }
--- a/system-backend/src/main/java/com/redhat/thermostat/backend/system/internal/JvmStatHostListener.java Fri Apr 14 06:11:10 2017 -0400 +++ b/system-backend/src/main/java/com/redhat/thermostat/backend/system/internal/JvmStatHostListener.java Fri May 05 12:10:15 2017 -0400 @@ -44,9 +44,17 @@ import java.util.logging.Level; import java.util.logging.Logger; +import com.redhat.thermostat.agent.VmBlacklist; +import com.redhat.thermostat.agent.VmStatusListener.Status; +import com.redhat.thermostat.backend.system.internal.models.InfoBuilderFactory; +import com.redhat.thermostat.common.Pair; import com.redhat.thermostat.common.portability.ProcessUserInfo; import com.redhat.thermostat.common.portability.ProcessUserInfoBuilder; -import com.redhat.thermostat.backend.system.internal.models.InfoBuilderFactory; +import com.redhat.thermostat.common.utils.LoggingUtils; +import com.redhat.thermostat.storage.core.WriterID; +import com.redhat.thermostat.storage.dao.VmInfoDAO; +import com.redhat.thermostat.storage.model.VmInfo; + import sun.jvmstat.monitor.MonitorException; import sun.jvmstat.monitor.MonitoredHost; import sun.jvmstat.monitor.MonitoredVm; @@ -55,16 +63,6 @@ import sun.jvmstat.monitor.event.HostListener; import sun.jvmstat.monitor.event.VmStatusChangeEvent; -import com.redhat.thermostat.agent.VmBlacklist; -import com.redhat.thermostat.agent.VmStatusListener.Status; -import com.redhat.thermostat.common.Pair; -import com.redhat.thermostat.common.utils.LoggingUtils; -import com.redhat.thermostat.storage.core.HostRef; -import com.redhat.thermostat.storage.core.VmRef; -import com.redhat.thermostat.storage.core.WriterID; -import com.redhat.thermostat.storage.dao.VmInfoDAO; -import com.redhat.thermostat.storage.model.VmInfo; - class JvmStatHostListener implements HostListener { private static final Logger logger = LoggingUtils.getLogger(JvmStatHostListener.class); @@ -74,17 +72,15 @@ private final ProcessUserInfoBuilder userInfoBuilder; private final WriterID writerId; private Map<Integer, Pair<String, MonitoredVm>> monitoredVms = new HashMap<>(); - private final HostRef hostRef; private final VmBlacklist blacklist; JvmStatHostListener(VmInfoDAO vmInfoDAO, VmStatusChangeNotifier notifier, - ProcessUserInfoBuilder userInfoBuilder, WriterID writerId, HostRef hostRef, + ProcessUserInfoBuilder userInfoBuilder, WriterID writerId, VmBlacklist blacklist) { this.vmInfoDAO = vmInfoDAO; this.notifier = notifier; this.userInfoBuilder = userInfoBuilder; this.writerId = writerId; - this.hostRef = hostRef; this.blacklist = blacklist; } @@ -136,8 +132,7 @@ VmInfo info = createVmInfo(vmId, vmPid, startTime, stopTime, extractor); // Check blacklist - VmRef vmRef = new VmRef(hostRef, vmId, vmPid, info.getMainClass()); - if (!blacklist.isBlacklisted(vmRef)) { + if (!blacklist.isBlacklisted(info.getMainClass())) { vmInfoDAO.putVmInfo(info); notifier.notifyVmStatusChange(Status.VM_STARTED, vmId, vmPid);
--- a/system-backend/src/main/java/com/redhat/thermostat/backend/system/internal/SystemBackend.java Fri Apr 14 06:11:10 2017 -0400 +++ b/system-backend/src/main/java/com/redhat/thermostat/backend/system/internal/SystemBackend.java Fri May 05 12:10:15 2017 -0400 @@ -42,31 +42,26 @@ import java.util.logging.Level; import java.util.logging.Logger; +import com.redhat.thermostat.agent.VmBlacklist; +import com.redhat.thermostat.backend.BaseBackend; +import com.redhat.thermostat.backend.system.internal.models.InfoBuilderFactory; +import com.redhat.thermostat.common.Version; import com.redhat.thermostat.common.portability.ProcessUserInfoBuilder; -import com.redhat.thermostat.backend.system.internal.models.InfoBuilderFactory; +import com.redhat.thermostat.common.portability.UserNameUtil; +import com.redhat.thermostat.common.utils.LoggingUtils; +import com.redhat.thermostat.storage.core.WriterID; +import com.redhat.thermostat.storage.dao.NetworkInterfaceInfoDAO; +import com.redhat.thermostat.storage.dao.VmInfoDAO; +import com.redhat.thermostat.storage.model.NetworkInterfaceInfo; import sun.jvmstat.monitor.HostIdentifier; import sun.jvmstat.monitor.MonitorException; import sun.jvmstat.monitor.MonitoredHost; -import com.redhat.thermostat.agent.VmBlacklist; -import com.redhat.thermostat.common.portability.UserNameUtil; -import com.redhat.thermostat.backend.BaseBackend; -import com.redhat.thermostat.common.Version; -import com.redhat.thermostat.common.utils.LoggingUtils; -import com.redhat.thermostat.storage.core.HostRef; -import com.redhat.thermostat.storage.core.WriterID; -import com.redhat.thermostat.storage.dao.HostInfoDAO; -import com.redhat.thermostat.storage.dao.NetworkInterfaceInfoDAO; -import com.redhat.thermostat.storage.dao.VmInfoDAO; -import com.redhat.thermostat.storage.model.HostInfo; -import com.redhat.thermostat.storage.model.NetworkInterfaceInfo; - public class SystemBackend extends BaseBackend { private static final Logger logger = LoggingUtils.getLogger(SystemBackend.class); - private HostInfoDAO hostInfos; private NetworkInterfaceInfoDAO networkInterfaces; private VmInfoDAO vmInfoDAO; @@ -84,14 +79,13 @@ private final WriterID writerId; private final VmBlacklist blacklist; - public SystemBackend(HostInfoDAO hostInfoDAO, NetworkInterfaceInfoDAO netInfoDAO, VmInfoDAO vmInfoDAO, + public SystemBackend(NetworkInterfaceInfoDAO netInfoDAO, VmInfoDAO vmInfoDAO, Version version, VmStatusChangeNotifier notifier, UserNameUtil userNameUtil, WriterID writerId, VmBlacklist blacklist) { super("System Backend", "Gathers basic information from the system", "Red Hat, Inc.", version.getVersionNumber(), true); - this.hostInfos = hostInfoDAO; this.networkInterfaces = netInfoDAO; this.vmInfoDAO = vmInfoDAO; this.notifier = notifier; @@ -111,8 +105,6 @@ if (!getObserveNewJvm()) { logger.fine("not monitoring new vms"); } - HostInfo hostInfo = InfoBuilderFactory.INSTANCE.createHostInfoBuilder(writerId).build(); - hostInfos.putHostInfo(hostInfo); timer = new Timer(); timer.scheduleAtFixedRate(new TimerTask() { @@ -126,9 +118,8 @@ try { hostId = new HostIdentifier((String) null); - HostRef hostRef = new HostRef(hostInfo.getAgentId(), hostInfo.getHostname()); hostListener = new JvmStatHostListener(vmInfoDAO, notifier, - userInfoBuilder, writerId, hostRef, blacklist); + userInfoBuilder, writerId, blacklist); host = MonitoredHost.getMonitoredHost(hostId); host.addHostListener(hostListener); } catch (MonitorException me) {
--- a/system-backend/src/main/java/com/redhat/thermostat/backend/system/internal/SystemBackendActivator.java Fri Apr 14 06:11:10 2017 -0400 +++ b/system-backend/src/main/java/com/redhat/thermostat/backend/system/internal/SystemBackendActivator.java Fri May 05 12:10:15 2017 -0400 @@ -41,15 +41,14 @@ import org.osgi.framework.ServiceRegistration; import com.redhat.thermostat.agent.VmBlacklist; -import com.redhat.thermostat.common.portability.UserNameUtil; 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.common.portability.UserNameUtil; import com.redhat.thermostat.storage.core.WriterID; -import com.redhat.thermostat.storage.dao.HostInfoDAO; import com.redhat.thermostat.storage.dao.NetworkInterfaceInfoDAO; import com.redhat.thermostat.storage.dao.VmInfoDAO; @@ -69,7 +68,6 @@ Class<?>[] deps = new Class<?>[] { BackendService.class, - HostInfoDAO.class, NetworkInterfaceInfoDAO.class, VmInfoDAO.class, UserNameUtil.class, @@ -79,14 +77,13 @@ tracker = new MultipleServiceTracker(context, deps, new Action() { @Override public void dependenciesAvailable(DependencyProvider services) { - HostInfoDAO hostInfoDAO = services.get(HostInfoDAO.class); NetworkInterfaceInfoDAO netInfoDAO = services.get(NetworkInterfaceInfoDAO.class); VmInfoDAO vmInfoDAO = services.get(VmInfoDAO.class); UserNameUtil userNameUtil = services.get(UserNameUtil.class); Version version = new Version(context.getBundle()); WriterID id = services.get(WriterID.class); VmBlacklist blacklist = services.get(VmBlacklist.class); - backend = new SystemBackend(hostInfoDAO, netInfoDAO, vmInfoDAO, version, notifier, + backend = new SystemBackend(netInfoDAO, vmInfoDAO, version, notifier, userNameUtil, id, blacklist); reg = context.registerService(Backend.class, backend, null); }
--- a/system-backend/src/main/java/com/redhat/thermostat/backend/system/internal/models/HostInfoBuilder.java Fri Apr 14 06:11:10 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.backend.system.internal.models; - -import com.redhat.thermostat.storage.model.HostInfo; - -/** - * Interface for building a HostInfo structure - * HostInfo contains information about a target machine's OS and hardware - */ -public interface HostInfoBuilder { - HostInfo build(); -}
--- a/system-backend/src/main/java/com/redhat/thermostat/backend/system/internal/models/InfoBuilderFactory.java Fri Apr 14 06:11:10 2017 -0400 +++ b/system-backend/src/main/java/com/redhat/thermostat/backend/system/internal/models/InfoBuilderFactory.java Fri May 05 12:10:15 2017 -0400 @@ -36,10 +36,9 @@ package com.redhat.thermostat.backend.system.internal.models; +import com.redhat.thermostat.backend.system.internal.InfoBuilderFactoryImpl; import com.redhat.thermostat.common.portability.ProcessUserInfoBuilder; import com.redhat.thermostat.common.portability.UserNameUtil; -import com.redhat.thermostat.backend.system.internal.InfoBuilderFactoryImpl; -import com.redhat.thermostat.storage.core.WriterID; /** * Allows callers to access OS-specific builders portably @@ -48,8 +47,6 @@ static InfoBuilderFactory INSTANCE = new InfoBuilderFactoryImpl(); - public HostInfoBuilder createHostInfoBuilder(final WriterID writerID); - public ProcessEnvironmentBuilder createProcessEnvironmentBuilder(); public ProcessUserInfoBuilder createProcessUserInfoBuilder(final UserNameUtil userNameUtil);
--- a/system-backend/src/test/java/com/redhat/thermostat/backend/system/internal/HostInfoBuilderTest.java Fri Apr 14 06:11:10 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,90 +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.backend.system.internal; - -import com.redhat.thermostat.common.portability.PortableHost; -import com.redhat.thermostat.backend.system.internal.models.HostInfoBuilder; -import com.redhat.thermostat.shared.config.OS; -import com.redhat.thermostat.storage.core.WriterID; -import com.redhat.thermostat.storage.model.HostInfo; -import org.junit.Assume; -import org.junit.Before; -import org.junit.Ignore; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -public class HostInfoBuilderTest { - - private WriterID writerId; - private PortableHost helper; - - @Before - public void setup() { - writerId = mock(WriterID.class); - helper = mock(PortableHost.class); - when(helper.getHostName()).thenReturn("testhost"); - when(helper.getOSName()).thenReturn("testos"); - when(helper.getOSVersion()).thenReturn("testversion"); - when(helper.getCPUModel()).thenReturn("testcpu"); - when(helper.getCPUCount()).thenReturn(4567); - when(helper.getTotalMemory()).thenReturn(9876L); - } - - @Test - public void testSimpleBuild() { - Assume.assumeTrue(OS.IS_WINDOWS); - HostInfo info = new HostInfoBuilderImpl(writerId).build(); - assertNotNull(info); - } - - @Test - public void testGetInfo() { - final HostInfoBuilder ib = new HostInfoBuilderImpl(writerId, helper); - final HostInfo hi = ib.build(); - assertEquals("testhost",hi.getHostname()); - assertEquals("testos", hi.getOsName()); - assertEquals("testcpu", hi.getCpuModel()); - assertEquals("testversion", hi.getOsKernel()); - assertEquals(4567, hi.getCpuCount()); - assertEquals(9876L, hi.getTotalMemory()); - } -} -
--- a/system-backend/src/test/java/com/redhat/thermostat/backend/system/internal/InfoBuilderFactoryTest.java Fri Apr 14 06:11:10 2017 -0400 +++ b/system-backend/src/test/java/com/redhat/thermostat/backend/system/internal/InfoBuilderFactoryTest.java Fri May 05 12:10:15 2017 -0400 @@ -36,23 +36,15 @@ package com.redhat.thermostat.backend.system.internal; -import com.redhat.thermostat.backend.system.internal.models.HostInfoBuilder; -import com.redhat.thermostat.backend.system.internal.models.InfoBuilderFactory; -import com.redhat.thermostat.backend.system.internal.models.ProcessEnvironmentBuilder; -import org.junit.Test; - import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; -public class InfoBuilderFactoryTest { +import org.junit.Test; - @Test - public void testCreateHostInfoBuilder() { - final InfoBuilderFactory builder = new InfoBuilderFactoryImpl(); - final HostInfoBuilder hib = builder.createHostInfoBuilder(null); - assertNotNull(hib); - assertTrue(hib instanceof HostInfoBuilderImpl); - } +import com.redhat.thermostat.backend.system.internal.models.InfoBuilderFactory; +import com.redhat.thermostat.backend.system.internal.models.ProcessEnvironmentBuilder; + +public class InfoBuilderFactoryTest { @Test public void testCreateProcessEnvironmentBuilder() {
--- a/system-backend/src/test/java/com/redhat/thermostat/backend/system/internal/JvmStatHostListenerTest.java Fri Apr 14 06:11:10 2017 -0400 +++ b/system-backend/src/test/java/com/redhat/thermostat/backend/system/internal/JvmStatHostListenerTest.java Fri May 05 12:10:15 2017 -0400 @@ -56,16 +56,14 @@ import java.util.Set; import java.util.UUID; -import com.redhat.thermostat.common.portability.ProcessUserInfo; -import com.redhat.thermostat.common.portability.ProcessUserInfoBuilder; import org.junit.Before; import org.junit.Test; import org.mockito.ArgumentCaptor; import com.redhat.thermostat.agent.VmBlacklist; import com.redhat.thermostat.agent.VmStatusListener.Status; -import com.redhat.thermostat.storage.core.HostRef; -import com.redhat.thermostat.storage.core.VmRef; +import com.redhat.thermostat.common.portability.ProcessUserInfo; +import com.redhat.thermostat.common.portability.ProcessUserInfoBuilder; import com.redhat.thermostat.storage.core.WriterID; import com.redhat.thermostat.storage.dao.VmInfoDAO; import com.redhat.thermostat.storage.model.VmInfo; @@ -110,9 +108,8 @@ when(userInfoBuilder.build(any(int.class))).thenReturn(userInfo); WriterID id = mock(WriterID.class); - HostRef hostRef = mock(HostRef.class); blacklist = mock(VmBlacklist.class); - hostListener = new JvmStatHostListener(vmInfoDAO, notifier, userInfoBuilder, id, hostRef, blacklist); + hostListener = new JvmStatHostListener(vmInfoDAO, notifier, userInfoBuilder, id, blacklist); host = mock(MonitoredHost.class); HostIdentifier hostId = mock(HostIdentifier.class); @@ -163,7 +160,7 @@ @Test public void testNewVMBlackListed() throws InterruptedException, MonitorException { - when(blacklist.isBlacklisted(any(VmRef.class))).thenReturn(true).thenReturn(false); + when(blacklist.isBlacklisted(anyString())).thenReturn(true).thenReturn(false); startVMs(); assertFalse(hostListener.getMonitoredVms().containsKey(1));
--- a/system-backend/src/test/java/com/redhat/thermostat/backend/system/internal/SystemBackendTest.java Fri Apr 14 06:11:10 2017 -0400 +++ b/system-backend/src/test/java/com/redhat/thermostat/backend/system/internal/SystemBackendTest.java Fri May 05 12:10:15 2017 -0400 @@ -42,16 +42,15 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; -import com.redhat.thermostat.shared.config.OS; import org.junit.Assume; import org.junit.Before; import org.junit.Test; import com.redhat.thermostat.agent.VmBlacklist; +import com.redhat.thermostat.common.Version; import com.redhat.thermostat.common.portability.UserNameUtil; -import com.redhat.thermostat.common.Version; +import com.redhat.thermostat.shared.config.OS; import com.redhat.thermostat.storage.core.WriterID; -import com.redhat.thermostat.storage.dao.HostInfoDAO; import com.redhat.thermostat.storage.dao.NetworkInterfaceInfoDAO; import com.redhat.thermostat.storage.dao.VmInfoDAO; @@ -62,7 +61,6 @@ @Before public void setUp() { - HostInfoDAO hDAO = mock(HostInfoDAO.class); NetworkInterfaceInfoDAO nDAO = mock(NetworkInterfaceInfoDAO.class); VmInfoDAO vmInfoDAO = mock(VmInfoDAO.class); @@ -74,7 +72,7 @@ WriterID id = mock(WriterID.class); VmBlacklist blacklist = mock(VmBlacklist.class); - b = new SystemBackend(hDAO, nDAO, vmInfoDAO, version, notifier, util, id, blacklist); + b = new SystemBackend(nDAO, vmInfoDAO, version, notifier, util, id, blacklist); } @Test
--- a/web/common/src/test/java/com/redhat/thermostat/web/common/typeadapters/WebQueryResponseTypeAdapterTest.java Fri Apr 14 06:11:10 2017 -0400 +++ b/web/common/src/test/java/com/redhat/thermostat/web/common/typeadapters/WebQueryResponseTypeAdapterTest.java Fri May 05 12:10:15 2017 -0400 @@ -48,11 +48,8 @@ import com.google.gson.GsonBuilder; import com.google.gson.reflect.TypeToken; import com.redhat.thermostat.storage.model.AgentInformation; -import com.redhat.thermostat.storage.model.HostInfo; import com.redhat.thermostat.web.common.PreparedStatementResponseCode; import com.redhat.thermostat.web.common.WebQueryResponse; -import com.redhat.thermostat.web.common.typeadapters.PojoTypeAdapterFactory; -import com.redhat.thermostat.web.common.typeadapters.WebQueryResponseTypeAdapterFactory; public class WebQueryResponseTypeAdapterTest { @@ -189,31 +186,6 @@ AgentInformation actualInfo = actualList[0]; assertEquals(false, actualInfo.isAlive()); assertEquals("testing", actualInfo.getAgentId()); - - // Do it again using HostInfo as model - HostInfo hostInfo = new HostInfo(); - hostInfo.setAgentId("something"); - hostInfo.setCpuCount(56); - hostInfo.setHostname("flukebox"); - - HostInfo[] hostInfoResults = new HostInfo[] { - hostInfo - }; - - WebQueryResponse<HostInfo> expected = new WebQueryResponse<>(); - expected.setResultList(hostInfoResults); - expected.setResponseCode(PreparedStatementResponseCode.QUERY_SUCCESS); - - jsonStr = gson.toJson(expected); - Type hostinfoQueryResponseType = new TypeToken<WebQueryResponse<HostInfo>>() {}.getType(); - WebQueryResponse<HostInfo> actualResp = gson.fromJson(jsonStr, hostinfoQueryResponseType); - - assertEquals(PreparedStatementResponseCode.QUERY_SUCCESS, actualResp.getResponseCode()); - HostInfo[] hostInfoList = actualResp.getResultList(); - assertEquals(1, hostInfoList.length); - assertEquals("something", hostInfoList[0].getAgentId()); - assertEquals(56, hostInfoList[0].getCpuCount()); - assertEquals("flukebox", hostInfoList[0].getHostname()); } }