Mercurial > hg > thermostat-ng > web-gateway
changeset 177:24135452bb7d
Add /system-cpu API
This patch adds the /system-cpu API - see bug 3401
http://icedtea.classpath.org/bugzilla/show_bug.cgi?id=3401
Reviewed-by: sgehwolf
Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2017-June/023611.html
line wrap: on
line diff
--- a/distribution/pom.xml Tue Jun 13 11:44:41 2017 -0400 +++ b/distribution/pom.xml Fri Jun 02 23:38:52 2017 -0400 @@ -220,6 +220,20 @@ </artifactItem> <artifactItem> <groupId>com.redhat.thermostat</groupId> + <artifactId>thermostat-web-gateway-service-system-cpu</artifactId> + <version>${project.version}</version> + <type>war</type> + <overWrite>false</overWrite> + </artifactItem> + <!--artifactItem> + <groupId>com.redhat.thermostat</groupId> + <artifactId>thermostat-web-gateway-service-system-memory</artifactId> + <version>${project.version}</version> + <type>war</type> + <overWrite>false</overWrite> + </artifactItem--> + <artifactItem> + <groupId>com.redhat.thermostat</groupId> <artifactId>thermostat-web-gateway-service-white-pages</artifactId> <version>${project.version}</version> <type>war</type> @@ -267,6 +281,20 @@ </dependency> <dependency> <groupId>com.redhat.thermostat</groupId> + <artifactId>thermostat-web-gateway-service-system-cpu</artifactId> + <version>${project.version}</version> + <type>war</type> + <scope>provided</scope> + </dependency> + <!--dependency> + <groupId>com.redhat.thermostat</groupId> + <artifactId>thermostat-web-gateway-service-system-memory</artifactId> + <version>${project.version}</version> + <type>war</type> + <scope>provided</scope> + </dependency--> + <dependency> + <groupId>com.redhat.thermostat</groupId> <artifactId>thermostat-web-gateway-service-commands</artifactId> <version>${project.version}</version> <type>war</type>
--- a/distribution/src/etc/services.properties Tue Jun 13 11:44:41 2017 -0400 +++ b/distribution/src/etc/services.properties Fri Jun 02 23:38:52 2017 -0400 @@ -3,4 +3,5 @@ /jvm-memory = thermostat-web-gateway-service-jvm-memory-@project.version@.war /jvms = thermostat-web-gateway-service-jvms-@project.version@.war /systems = thermostat-web-gateway-service-systems-@project.version@.war +/system-cpu = thermostat-web-gateway-service-system-cpu-@project.version@.war /white-pages = thermostat-web-gateway-service-white-pages-@project.version@.war
--- a/services/pom.xml Tue Jun 13 11:44:41 2017 -0400 +++ b/services/pom.xml Fri Jun 02 23:38:52 2017 -0400 @@ -56,6 +56,8 @@ <module>jvms</module> <module>jvm-memory</module> <module>systems</module> + <module>system-cpu</module> + <!--module>system-memory</module--> <module>white-pages</module> </modules>
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/services/system-cpu/pom.xml Fri Jun 02 23:38:52 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. + +--> +<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/maven-v4_0_0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <parent> + <artifactId>thermostat-web-gateway-services</artifactId> + <groupId>com.redhat.thermostat</groupId> + <version>1.99.12-SNAPSHOT</version> + </parent> + + <artifactId>thermostat-web-gateway-service-system-cpu</artifactId> + + <packaging>war</packaging> + + <name>Thermostat Web Gateway System CPU Service</name> + + <properties> + <com.redhat.thermostat.gateway.SERVICE_NAME>system-cpu</com.redhat.thermostat.gateway.SERVICE_NAME> + </properties> + + <build> + <plugins> + <plugin> + <artifactId>maven-war-plugin</artifactId> + <configuration> + <webResources> + <resource> + <directory>src/main/webapp</directory> + <filtering>true</filtering> + </resource> + </webResources> + </configuration> + </plugin> + </plugins> + </build> + + <dependencies> + <!-- Thermostat Web Gateway Dependencies --> + <dependency> + <groupId>com.redhat.thermostat</groupId> + <artifactId>thermostat-web-gateway-common-mongodb</artifactId> + <version>${project.version}</version> + </dependency> + + <!-- Servlet API deps --> + <dependency> + <groupId>javax.servlet</groupId> + <artifactId>javax.servlet-api</artifactId> + <version>${javax.servlet.version}</version> + <scope>provided</scope> + </dependency> + + <!-- JAX-RS Dependencies --> + <dependency> + <groupId>javax.ws.rs</groupId> + <artifactId>javax.ws.rs-api</artifactId> + <version>${javax-rs-api.version}</version> + <scope>provided</scope> + </dependency> + </dependencies> +</project>
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/services/system-cpu/src/main/java/com/redhat/thermostat/service/cpuinfo/http/Parameters.java Fri Jun 02 23:38:52 2017 -0400 @@ -0,0 +1,50 @@ +/* + * 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.service.cpuinfo.http; + +class Parameters { + static final String SYSTEM_ID = "systemId"; + + static final String SORT = "sort"; + static final String QUERY = "query"; + static final String OFFSET = "offset"; + static final String LIMIT = "limit"; + static final String INCLUDE = "include"; + static final String EXCLUDE = "exclude"; + + static final String TIMESTAMP = "timeStamp"; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/services/system-cpu/src/main/java/com/redhat/thermostat/service/cpuinfo/http/SwaggerSpecResourceHandler.java Fri Jun 02 23:38:52 2017 -0400 @@ -0,0 +1,59 @@ +/* + * 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.service.cpuinfo.http; + +import java.io.IOException; + +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; + +import com.redhat.thermostat.gateway.common.core.servlet.BasicResourceHandler; + +@Path("doc/{fileName: .+\\.yaml}") +@Produces(MediaType.TEXT_PLAIN) +public class SwaggerSpecResourceHandler extends BasicResourceHandler { + + @GET + public Response getFileAsPlainText(@PathParam("fileName") String fileName) throws IOException { + return getFileAsResponse(SwaggerSpecResourceHandler.class.getClassLoader(), fileName); + } + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/services/system-cpu/src/main/java/com/redhat/thermostat/service/cpuinfo/http/SystemInfoCPUHttpHandler.java Fri Jun 02 23:38:52 2017 -0400 @@ -0,0 +1,133 @@ +/* + * 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.service.cpuinfo.http; + +import javax.servlet.ServletContext; +import javax.ws.rs.Consumes; +import javax.ws.rs.DELETE; +import javax.ws.rs.DefaultValue; +import javax.ws.rs.GET; +import javax.ws.rs.POST; +import javax.ws.rs.PUT; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.QueryParam; +import javax.ws.rs.core.Context; +import javax.ws.rs.core.Response; + + +import com.mongodb.DBObject; +import com.redhat.thermostat.gateway.common.mongodb.ThermostatMongoStorage; +import com.redhat.thermostat.gateway.common.mongodb.servlet.ServletContextConstants; +import com.redhat.thermostat.service.cpuinfo.mongo.MongoStorageHandler; + +@Path("/") +public class SystemInfoCPUHttpHandler { + private final MongoStorageHandler mongoStorageHandler = new MongoStorageHandler(); + private final String collectionName = "cpu-info"; + + @GET + @Path("/systems/{" + Parameters.SYSTEM_ID +"}") + @Consumes({ "application/json" }) + @Produces({ "application/json", "text/html; charset=utf-8" }) + public Response getCPUInfo(@PathParam(Parameters.SYSTEM_ID) String systemId, + @QueryParam(Parameters.LIMIT) @DefaultValue("1") Integer limit, + @QueryParam(Parameters.OFFSET) @DefaultValue("0") Integer offset, + @QueryParam(Parameters.SORT) String sort, + @QueryParam(Parameters.INCLUDE) String includes, + @QueryParam(Parameters.EXCLUDE) String excludes, + @Context ServletContext context + ) { + try { + ThermostatMongoStorage storage = (ThermostatMongoStorage) context.getAttribute(ServletContextConstants.MONGODB_CLIENT_ATTRIBUTE); + String message = mongoStorageHandler.getOne(storage.getDatabase().getCollection(collectionName), systemId, limit, offset, sort, includes, excludes); + return Response.status(Response.Status.OK).entity(message).build(); + } catch (Exception e) { + return Response.status(Response.Status.BAD_REQUEST).build(); + } + } + + @PUT + @Path("/systems/{" + Parameters.SYSTEM_ID +"}") + @Consumes({ "application/json" }) + @Produces({ "application/json", "text/html; charset=utf-8" }) + public Response putCPUInfo(String body, + @PathParam(Parameters.SYSTEM_ID) String systemId, + @QueryParam(Parameters.QUERY) String queries, + @Context ServletContext context) { + try { + ThermostatMongoStorage storage = (ThermostatMongoStorage) context.getAttribute(ServletContextConstants.MONGODB_CLIENT_ATTRIBUTE); + mongoStorageHandler.updateOne(storage.getDatabase().getCollection(collectionName), body, systemId, queries); + return Response.status(Response.Status.OK).build(); + } catch (Exception e) { + return Response.status(Response.Status.BAD_REQUEST).build(); + } + } + + @POST + @Path("/systems/{" + Parameters.SYSTEM_ID +"}") + @Consumes({ "application/json" }) + @Produces({ "application/json", "text/html; charset=utf-8" }) + public Response postCPUInfo(String body, + @PathParam(Parameters.SYSTEM_ID) String systemId, + @Context ServletContext context) { + try { + ThermostatMongoStorage storage = (ThermostatMongoStorage) context.getAttribute(ServletContextConstants.MONGODB_CLIENT_ATTRIBUTE); + mongoStorageHandler.addMany(storage.getDatabase().getCollection(collectionName, DBObject.class), body, systemId); + return Response.status(Response.Status.OK).build(); + } catch (Exception e) { + return Response.status(Response.Status.BAD_REQUEST).build(); + } + } + + @DELETE + @Path("/systems/{" + Parameters.SYSTEM_ID +"}") + @Consumes({ "application/json" }) + @Produces({ "application/json", "text/html; charset=utf-8" }) + public Response deleteCPUInfo(@PathParam(Parameters.SYSTEM_ID) String systemId, + @QueryParam(Parameters.QUERY) String queries, + @Context ServletContext context) { + try { + ThermostatMongoStorage storage = (ThermostatMongoStorage) context.getAttribute(ServletContextConstants.MONGODB_CLIENT_ATTRIBUTE); + mongoStorageHandler.delete(storage.getDatabase().getCollection(collectionName), systemId); + return Response.status(Response.Status.OK).build(); + } catch (Exception e) { + return Response.status(Response.Status.BAD_REQUEST).build(); + } + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/services/system-cpu/src/main/java/com/redhat/thermostat/service/cpuinfo/mongo/Fields.java Fri Jun 02 23:38:52 2017 -0400 @@ -0,0 +1,48 @@ +/* + * Copyright 2012-2017 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.service.cpuinfo.mongo; + +class Fields { + static final String SYSTEM_ID = "systemId"; + + static final String SET = "set"; + static final String LAST_UPDATED = "lastUpdated"; + static final String STOP_TIME = "stopTime"; + + static final String RESPONSE = "response"; + static final String SYSTEMS = "systems"; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/services/system-cpu/src/main/java/com/redhat/thermostat/service/cpuinfo/mongo/MongoStorageHandler.java Fri Jun 02 23:38:52 2017 -0400 @@ -0,0 +1,169 @@ +/* + * 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.service.cpuinfo.mongo; + +import com.mongodb.BasicDBObject; +import com.mongodb.CursorType; +import com.mongodb.DBObject; +import com.mongodb.client.FindIterable; +import com.mongodb.client.MongoCollection; +import com.mongodb.util.JSON; +import com.redhat.thermostat.gateway.common.mongodb.filters.MongoRequestFilters; +import com.redhat.thermostat.gateway.common.mongodb.filters.MongoSortFilters; +import com.redhat.thermostat.gateway.common.mongodb.response.MongoResponseBuilder; +import org.bson.Document; +import org.bson.conversions.Bson; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +import static com.mongodb.client.model.Filters.and; +import static com.mongodb.client.model.Filters.eq; +import static com.mongodb.client.model.Filters.or; +import static com.mongodb.client.model.Projections.exclude; +import static com.mongodb.client.model.Projections.excludeId; +import static com.mongodb.client.model.Projections.fields; +import static com.mongodb.client.model.Projections.include; + +public class MongoStorageHandler { + + private final MongoResponseBuilder mongoResponseBuilder = new MongoResponseBuilder(); + + public String getMany(MongoCollection<Document> collection, Integer limit, Integer offset, String sort, String queries, String includes, String excludes) { + FindIterable<Document> documents; + if (queries != null) { + List<String> queriesList = Arrays.asList(queries.split(",")); + final Bson query = MongoRequestFilters.buildQueriesFilter(queriesList); + documents = collection.find(query); + } else { + documents = collection.find(); + } + + documents = buildProjection(documents, includes, excludes); + + final Bson sortObject = MongoSortFilters.createSortObject(sort); + documents = documents.sort(sortObject).limit(limit).skip(offset).batchSize(limit).cursorType(CursorType.NonTailable); + + return mongoResponseBuilder.buildGetResponseString(documents); + } + + public String getOne(MongoCollection<Document> collection, String systemId, Integer limit, Integer offset, String sort, String includes, String excludes) { + Bson query = eq(Fields.SYSTEM_ID, systemId); + FindIterable<Document> documents = collection.find(query); + + documents = buildProjection(documents, includes, excludes); + + final Bson sortObject = MongoSortFilters.createSortObject(sort); + documents = documents.sort(sortObject).limit(limit).skip(offset).batchSize(limit + offset).cursorType(CursorType.NonTailable); + + return mongoResponseBuilder.buildGetResponseString(documents); + } + + private FindIterable<Document> buildProjection(FindIterable<Document> documents, String includes, String excludes) { + if (excludes != null) { + List<String> excludesList = Arrays.asList(excludes.split(",")); + documents = documents.projection(fields(exclude(excludesList), excludeId())); + } else if (includes != null) { + List<String> includesList = Arrays.asList(includes.split(",")); + documents = documents.projection(fields(include(includesList), excludeId())); + } else { + documents = documents.projection(excludeId()); + } + + return documents; + } + + public void addMany(MongoCollection<DBObject> collection, String body, String systemId) { + if (body.length() > 0) { + List<DBObject> inputList = (List<DBObject>) JSON.parse(body); + for (DBObject o : inputList) { + o.put(Fields.SYSTEM_ID, systemId); + } + collection.insertMany(inputList); + } + } + + public void delete(MongoCollection<Document> collection, String systemId) { + Bson query = eq(Fields.SYSTEM_ID, systemId); + deleteDocuments(collection, query); + } + + private void deleteDocuments(MongoCollection<Document> collection, Bson query) { + collection.deleteMany(query); + } + + public void updateOne(MongoCollection<Document> collection, String body, String systemId, String queries) { + Bson baseQuery = eq(Fields.SYSTEM_ID, systemId); + + BasicDBObject inputObject = (BasicDBObject) JSON.parse(body); + BasicDBObject setObject = (BasicDBObject) inputObject.get(Fields.SET); + if (setObject.containsField(Fields.SYSTEM_ID)) { + throw new UnsupportedOperationException("Updating " + Fields.SYSTEM_ID + " fields is not allowed"); + } + + final List<String> queriesList; + if (queries != null) { + queriesList = Arrays.asList(queries.split(",")); + } else { + queriesList = Collections.emptyList(); + } + + final Bson fields = new Document("$set", setObject); + + collection.updateMany(and(baseQuery, MongoRequestFilters.buildQueriesFilter(queriesList)), fields); + } + + public void updateTimestamps(MongoCollection<Document> collection, String body, String systemId, Long timeStamp) { + final Bson filter; + if (body != null && body.length() > 0) { + List<String> systems = (List<String>) JSON.parse(body); + List<Bson> systemFilters = new ArrayList<>(); + for (String id : systems) { + systemFilters.add(eq(Fields.SYSTEM_ID, id)); + } + filter = or(eq(Fields.SYSTEM_ID, systemId), or(systemFilters)); + } else { + filter = eq(Fields.SYSTEM_ID, systemId); + } + + String setDocument = "{ \"$set\" : { \"" + Fields.LAST_UPDATED + "\":" + timeStamp + " } }"; + final Bson update = Document.parse(setDocument); + collection.updateMany(filter, update); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/services/system-cpu/src/main/resources/cpu-info-swagger.yaml Fri Jun 02 23:38:52 2017 -0400 @@ -0,0 +1,170 @@ +swagger: '2.0' +info: + version: 0.0.1 + title: Thermostat Web Gateway CPU Info API + license: + name: GPL v2 with Classpath Exception + url: 'http://www.gnu.org/licenses' +consumes: + - application/json +produces: + - application/json + - text/html; charset=utf-8 +basePath: /system-cpu/0.0.1 +paths: + /: + get: + description: Get all CPU information for all systems. + parameters: + - $ref: '#/parameters/limit' + - $ref: '#/parameters/offset' + - $ref: '#/parameters/sort' + - $ref: '#/parameters/include' + - $ref: '#/parameters/exclude' + - $ref: '#/parameters/query' + responses: + '200': + description: OK + schema: + $ref: '#/definitions/cpu-info-get-response' + /systems/{systemId}: + parameters: + - $ref: '#/parameters/system-id' + get: + description: Get CPU information for system {systemId}. + parameters: + - $ref: '#/parameters/limit' + - $ref: '#/parameters/offset' + - $ref: '#/parameters/sort' + - $ref: '#/parameters/include' + - $ref: '#/parameters/exclude' + - $ref: '#/parameters/query' + responses: + '200': + description: OK + schema: + $ref: '#/definitions/cpu-info-get-response' + put: + description: Update CPU information for system {systemId}. + parameters: + - $ref: '#/parameters/cpu-info-put-body' + - $ref: '#/parameters/query' + responses: + '200': + description: OK + post: + description: Add CPU information for system {systemId} + parameters: + - $ref: '#/parameters/cpu-info-array' + responses: + '200': + description: OK + delete: + description: Delete CPU information for system ID {systemId}. + parameters: + - $ref: '#/parameters/query' + responses: + '200': + description: OK +definitions: + cpu-info-get-response: + type: object + properties: + response: + $ref: '#/definitions/cpu-info-array' + cpu-info-array: + type: array + items: + $ref: '#/definitions/cpu-info' + cpu-info: + type: object + properties: + systemId: + type: string + agentId: + type: string + timestamp: + type: integer + format: int64 + perProcessorUsage: + type: array + items: + type: integer + format: int64 + cpu-info-put-body: + type: object + properties: + "set": + type: object +parameters: + system-id: + name: systemId + in: path + required: true + type: string + cpu-info-array: + name: cpu-info-array + in: body + description: The system CPU information + required: true + schema: + $ref: '#/definitions/cpu-info-array' + cpu-info-put-body: + name: body + in: body + description: >- + The JSON object containing a 'set' object. This contains single item JSON + objects that specify the field to replace and the JSON value to replace with. + Must not include 'systemId' field. Example { "set" : { + "field" : "value", "field2":{"object":"item"} } + required: true + schema: + $ref: '#/definitions/cpu-info-put-body' + limit: + name: limit + in: query + description: Limit of items to return. Example '1' + type: integer + required: false + default: 1 + offset: + name: offset + in: query + description: Offset of items to return. Example '0' + type: integer + required: true + default: 0 + sort: + name: sort + in: query + description: Sort string. Comma separated list of fields prefixed with '+' for ascending or '-' for descending. Example '?sort=+a,-b' Fields use dot notation for embedded documents. Example 'outer.inner' refers to field inner contained in field outer. + type: string + required: false + query: + name: query + in: query + description: Query string. Comma separated list of key, comparator, value pairs. Comparator supports '==', '<=', '>=', '<', '>', '!='. Example '?query=a==b,c!=d'. Keys are fields in documents and use dot notation for embedded documents. Example 'outer.inner' refers to field inner contained in field outer. + type: string + required: false + include: + name: include + in: query + description: >- + Inclusion string. Comma separated list of fields to include in the + response. Example '?include=a,b' Fields use dot notation for embedded + documents. Example 'outer.inner' refers to field inner contained in field + outer. Cannot be used in combination with 'exclude' parameter Overriden by + 'exclude' parameter + type: string + required: false + exclude: + name: exclude + in: query + description: >- + Exclusion string. Comma separated list of fields to exclude in the + response. Example '?exclude=a,b' Fields use dot notation for embedded + documents. Example 'outer.inner' refers to field inner contained in field + outer. Cannot be used in combination with 'include' parameter; takes + precedence over 'include' parameter + type: string + required: false
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/services/system-cpu/src/main/webapp/WEB-INF/web.xml Fri Jun 02 23:38:52 2017 -0400 @@ -0,0 +1,68 @@ +<!-- + + 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. + +--> +<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee + http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" + version="3.1"> + <servlet> + <servlet-name>SystemInfoCPUServlet</servlet-name> + <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> + <init-param> + <param-name> + jersey.config.server.provider.packages + </param-name> + <param-value> + com.redhat.thermostat.service.cpuinfo, + </param-value> + </init-param> + </servlet> + <servlet-mapping> + <servlet-name>SystemInfoCPUServlet</servlet-name> + <url-pattern>/0.0.1/*</url-pattern> + </servlet-mapping> + <!-- Service configuration --> + <context-param> + <param-name>com.redhat.thermostat.gateway.SERVICE_NAME</param-name> + <param-value>@com.redhat.thermostat.gateway.SERVICE_NAME@</param-value> + </context-param> + <!-- Listener for setting up the storage connection --> + <listener> + <listener-class>com.redhat.thermostat.gateway.common.mongodb.servlet.StorageConnectionSettingListener</listener-class> + </listener> +</web-app> \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/integration-tests/src/test/java/com/redhat/thermostat/gateway/service/system/cpu/SystemCPUIntegrationTest.java Fri Jun 02 23:38:52 2017 -0400 @@ -0,0 +1,314 @@ +/* + * 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.gateway.service.system.cpu; + +import com.google.gson.JsonArray; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; + +import com.redhat.thermostat.gateway.tests.integration.MongoIntegrationTest; +import org.eclipse.jetty.client.api.ContentResponse; +import org.eclipse.jetty.client.api.Request; +import org.eclipse.jetty.client.util.StringContentProvider; +import org.eclipse.jetty.http.HttpHeader; +import org.eclipse.jetty.http.HttpMethod; +import org.junit.Before; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeoutException; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +public class SystemCPUIntegrationTest extends MongoIntegrationTest { + + private static final String collectionName = "cpu-info"; + private static final String serviceURL = baseUrl + "/system-cpu/0.0.1"; + private static final int HTTP_200_OK = 200; + private static final int HTTP_404_NOTFOUND = 404; + private static final String TIMESTAMP_TOKEN = "$TIMESTAMP$"; + private static final String AGENT_ID = getRandomSystemId(); + private static final String cpuInfoJSON = + "{\n" + + " \"perProcessorUsage\" : [ \n" + + " 94.0, \n" + + " 76.0, \n" + + " 94.0, \n" + + " 82.0\n" + + " ],\n" + + " \"timeStamp\" : " + TIMESTAMP_TOKEN + ",\n" + + " \"agentId\": \"" + AGENT_ID + "\",\n" + + "}"; + + + private static class TinyCPUInfo { + TinyCPUInfo(String systemId, String agentId, int[] ppusage) { + this.systemId = systemId; + this.agentId = agentId; + this.perProcessorUsage = ppusage; + } + String agentId; + String systemId; + int[] perProcessorUsage; + } + + public SystemCPUIntegrationTest() { + super(serviceURL, collectionName); + } + + @Before + public void beforeIntegrationTest() { + mongodTestUtil.dropCollection(collectionName); + } + + @Test + public void testGetAll() throws InterruptedException, TimeoutException, ExecutionException { + final String systemid = getRandomSystemId(); + + post(systemid); + Thread.sleep(5); + post(systemid); + Thread.sleep(5); + post(systemid); + + ContentResponse response = get(systemid); + final List<TinyCPUInfo> list = parse(response, systemid); + assertEquals(1, list.size()); + + ContentResponse response2 = get(systemid, "?limit=2"); + final List<TinyCPUInfo> list2 = parse(response2, systemid); + assertEquals(2, list2.size()); + + ContentResponse response3 = get(systemid, "?limit=0"); + final List<TinyCPUInfo> list3 = parse(response3, systemid); + assertEquals(3, list3.size()); + } + + @Test + public void testGetAllFails() throws InterruptedException, TimeoutException, ExecutionException { + ContentResponse response = client.newRequest(serviceURL).method(HttpMethod.GET).send(); + assertEquals(HTTP_404_NOTFOUND, response.getStatus()); + } + + @Test + public void testGetUnknown() throws InterruptedException, TimeoutException, ExecutionException { + final String systemid = getRandomSystemId(); + getUnknown(systemid); + } + + @Test + public void testCreateOne() throws InterruptedException, TimeoutException, ExecutionException { + + final String systemid = getRandomSystemId(); + post(systemid); + getKnown(systemid); + } + + @Test + public void testPut() throws InterruptedException, TimeoutException, ExecutionException { + + final String systemid = getRandomSystemId(); + final long timestamp = getTimestamp(); + + // create it + post(systemid); + + // retrieve it + final ContentResponse response1 = getKnown(systemid); + final List<TinyCPUInfo> list1 = parse(response1, systemid); + assertEquals(1, list1.size()); + //assertEquals(CPU_STRING1, list1.get(0).cpuModel); + + // modify it + put(systemid, timestamp+1); + + // ensure it was changed + final ContentResponse response2 = getKnown(systemid); + final List<TinyCPUInfo> list2 = parse(response2, systemid); + assertEquals(1, list2.size()); + //assertEquals(timestamp+1, list2.get(0).????); + } + + @Test + public void testDeleteUnknown() throws InterruptedException, TimeoutException, ExecutionException { + final String systemid = getRandomSystemId(); + + // delete it + delete(systemid); + } + + @Test + public void testDeleteOne() throws InterruptedException, ExecutionException, TimeoutException { + final String systemid = getRandomSystemId(); + + // create the new record + post(systemid); + + // check that it's there + getKnown(systemid); + + // delete it + delete(systemid); + + // check that it's not there + getUnknown(systemid); + } + + private ContentResponse post(final String systemid) throws InterruptedException, ExecutionException, TimeoutException { + final Request request = client.newRequest(serviceURL + "/systems/" + systemid); + request.header(HttpHeader.CONTENT_TYPE, "application/json"); + request.content(new StringContentProvider("[" + createJSON() + "]")); + ContentResponse response = request.method(HttpMethod.POST).send(); + assertEquals(HTTP_200_OK, response.getStatus()); + final String expected = ""; + assertEquals(expected, response.getContentAsString()); + return response; + } + + private List<TinyCPUInfo> parse(ContentResponse contentResponse, final String expectedSystemId) { + + JsonParser parser = new JsonParser(); + JsonObject json = (JsonObject) parser.parse(contentResponse.getContentAsString()); + JsonElement response = json.get("response"); + + JsonArray allData = response.getAsJsonArray(); + List<TinyCPUInfo> result = new ArrayList<>(); + + for (JsonElement entry : allData) { + + json = (JsonObject) parser.parse(entry.toString()); + + assertTrue(json.has("systemId")); + assertTrue(json.has("agentId")); + assertTrue(json.has("timeStamp")); + assertTrue(json.has("perProcessorUsage")); + + final String systemId = json.get("systemId").getAsString(); + final String agentId = json.get("agentId").getAsString(); + // TODO timestamp is special because it's long // final long timeStamp = json.get("timeStamp").getAsLong(); + final JsonArray usages = json.get("perProcessorUsage").getAsJsonArray(); + + final int[] cpuUsages; + + // Deal with the case of a non-array value. + if (usages == null) { + cpuUsages = new int[0]; + } else { + cpuUsages = new int[usages.size()]; + for (int i = 0; i < usages.size(); ++i) { + cpuUsages[i] = usages.get(i).getAsInt(); + } + } + assertEquals(AGENT_ID, agentId); + if (expectedSystemId != null) { + assertEquals(expectedSystemId, systemId); + } + + TinyCPUInfo hi = new TinyCPUInfo(systemId, agentId, cpuUsages); + + result.add(hi); + } + return result; + } + + private ContentResponse put(final String systemid, final long ts) throws InterruptedException, ExecutionException, TimeoutException { + final Request request = client.newRequest(serviceURL + "/systems/" + systemid); + request.header(HttpHeader.CONTENT_TYPE, "application/json"); + final String contentStr = createJSON(ts); + request.content(new StringContentProvider("{ \"set\" : " +contentStr + "}")); + ContentResponse response = request.method(HttpMethod.PUT).send(); + assertEquals(HTTP_200_OK, response.getStatus()); + final String expected = ""; + assertEquals(expected, response.getContentAsString()); + return response; + } + + private ContentResponse getUnknown(final String systemid) throws InterruptedException, ExecutionException, TimeoutException { + ContentResponse response = get(systemid); + assertTrue(parse(response, systemid).isEmpty()); + return response; + } + + private ContentResponse getKnown(final String systemid) throws InterruptedException, ExecutionException, TimeoutException { + ContentResponse response = get(systemid); + assertEquals(1, parse(response, systemid).size()); + return response; + } + + private ContentResponse get(final String systemid) throws InterruptedException, ExecutionException, TimeoutException { + ContentResponse response = client.newRequest(serviceURL + "/systems/" + systemid).method(HttpMethod.GET).send(); + assertEquals(HTTP_200_OK, response.getStatus()); + return response; + } + + private ContentResponse get(final String systemid, final String query) throws InterruptedException, ExecutionException, TimeoutException { + final Request rq = client.newRequest(serviceURL + "/systems/" + systemid + query); + rq.method(HttpMethod.GET); + ContentResponse response = rq.send(); + assertEquals(HTTP_200_OK, response.getStatus()); + return response; + } + + private ContentResponse delete(final String systemid) throws InterruptedException, ExecutionException, TimeoutException { + ContentResponse response = client.newRequest(serviceURL + "/systems/" + systemid).method(HttpMethod.DELETE).send(); + assertEquals(HTTP_200_OK, response.getStatus()); + final String expected = ""; + assertEquals(expected, response.getContentAsString()); + return response; + } + + private static String getRandomSystemId() { + return UUID.randomUUID().toString(); + } + + private static long getTimestamp() { + return java.lang.System.nanoTime(); + } + + private String createJSON() { + return createJSON(getTimestamp()); + } + + private String createJSON(final long ts) { + return cpuInfoJSON.replace(TIMESTAMP_TOKEN, Long.toString(ts)); + } +}