Mercurial > hg > thermostat-ng > web-gateway
changeset 185:550361e046a6
Rename packages for system-cpu and system-memory services.
Reviewed-by: jkang
Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2017-June/023780.html
author | Severin Gehwolf <sgehwolf@redhat.com> |
---|---|
date | Wed, 21 Jun 2017 11:00:17 +0200 |
parents | 586afca62e7f |
children | 69076d3867be |
files | services/system-cpu/src/main/java/com/redhat/thermostat/service/cpuinfo/http/Parameters.java services/system-cpu/src/main/java/com/redhat/thermostat/service/cpuinfo/http/SwaggerSpecResourceHandler.java services/system-cpu/src/main/java/com/redhat/thermostat/service/cpuinfo/http/SystemInfoCPUHttpHandler.java services/system-cpu/src/main/java/com/redhat/thermostat/service/cpuinfo/mongo/Fields.java services/system-cpu/src/main/java/com/redhat/thermostat/service/cpuinfo/mongo/MongoStorageHandler.java services/system-cpu/src/main/java/com/redhat/thermostat/service/system/cpu/http/Parameters.java services/system-cpu/src/main/java/com/redhat/thermostat/service/system/cpu/http/SwaggerSpecResourceHandler.java services/system-cpu/src/main/java/com/redhat/thermostat/service/system/cpu/http/SystemInfoCPUHttpHandler.java services/system-cpu/src/main/java/com/redhat/thermostat/service/system/cpu/mongo/Fields.java services/system-cpu/src/main/java/com/redhat/thermostat/service/system/cpu/mongo/MongoStorageHandler.java services/system-cpu/src/main/webapp/WEB-INF/web.xml services/system-memory/src/main/java/com/redhat/thermostat/service/system/memory/http/Parameters.java services/system-memory/src/main/java/com/redhat/thermostat/service/system/memory/http/SwaggerSpecResourceHandler.java services/system-memory/src/main/java/com/redhat/thermostat/service/system/memory/http/SystemMemoryHttpHandler.java services/system-memory/src/main/java/com/redhat/thermostat/service/system/memory/mongo/Fields.java services/system-memory/src/main/java/com/redhat/thermostat/service/system/memory/mongo/MongoStorageHandler.java services/system-memory/src/main/java/com/redhat/thermostat/service/systemmemory/http/Parameters.java services/system-memory/src/main/java/com/redhat/thermostat/service/systemmemory/http/SwaggerSpecResourceHandler.java services/system-memory/src/main/java/com/redhat/thermostat/service/systemmemory/http/SystemMemoryHttpHandler.java services/system-memory/src/main/java/com/redhat/thermostat/service/systemmemory/mongo/Fields.java services/system-memory/src/main/java/com/redhat/thermostat/service/systemmemory/mongo/MongoStorageHandler.java services/system-memory/src/main/webapp/WEB-INF/web.xml |
diffstat | 22 files changed, 865 insertions(+), 865 deletions(-) [+] |
line wrap: on
line diff
--- a/services/system-cpu/src/main/java/com/redhat/thermostat/service/cpuinfo/http/Parameters.java Tue Jun 20 14:19:38 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,50 +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.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"; -}
--- a/services/system-cpu/src/main/java/com/redhat/thermostat/service/cpuinfo/http/SwaggerSpecResourceHandler.java Tue Jun 20 14:19:38 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,59 +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.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); - } - -}
--- a/services/system-cpu/src/main/java/com/redhat/thermostat/service/cpuinfo/http/SystemInfoCPUHttpHandler.java Tue Jun 20 14:19:38 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,133 +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.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(); - } - } -}
--- a/services/system-cpu/src/main/java/com/redhat/thermostat/service/cpuinfo/mongo/Fields.java Tue Jun 20 14:19:38 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,48 +0,0 @@ -/* - * Copyright 2012-2017 Red Hat, Inc. - * - * This file is part of Thermostat. - * - * Thermostat is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published - * by the Free Software Foundation; either version 2, or (at your - * option) any later version. - * - * Thermostat is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Thermostat; see the file COPYING. If not see - * <http://www.gnu.org/licenses/>. - * - * Linking this code with other modules is making a combined work - * based on this code. Thus, the terms and conditions of the GNU - * General Public License cover the whole combination. - * - * As a special exception, the copyright holders of this code give - * you permission to link this code with independent modules to - * produce an executable, regardless of the license terms of these - * independent modules, and to copy and distribute the resulting - * executable under terms of your choice, provided that you also - * meet, for each linked independent module, the terms and conditions - * of the license of that module. An independent module is a module - * which is not derived from or based on this code. If you modify - * this code, you may extend this exception to your version of the - * library, but you are not obligated to do so. If you do not wish - * to do so, delete this exception statement from your version. - */ - -package com.redhat.thermostat.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"; -}
--- a/services/system-cpu/src/main/java/com/redhat/thermostat/service/cpuinfo/mongo/MongoStorageHandler.java Tue Jun 20 14:19:38 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,150 +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.service.cpuinfo.mongo; - -import static com.mongodb.client.model.Filters.and; -import static com.mongodb.client.model.Filters.eq; -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; - -import java.util.Arrays; -import java.util.Collections; -import java.util.List; - -import org.bson.Document; -import org.bson.conversions.Bson; - -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; - -public class MongoStorageHandler { - - private final MongoResponseBuilder.Builder mongoResponseBuilder = new MongoResponseBuilder.Builder(); - - 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.queryDocuments(documents).build(); - } - - 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.queryDocuments(documents).build(); - } - - 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); - } -}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/services/system-cpu/src/main/java/com/redhat/thermostat/service/system/cpu/http/Parameters.java Wed Jun 21 11:00:17 2017 +0200 @@ -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.system.cpu.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/system/cpu/http/SwaggerSpecResourceHandler.java Wed Jun 21 11:00:17 2017 +0200 @@ -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.system.cpu.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/system/cpu/http/SystemInfoCPUHttpHandler.java Wed Jun 21 11:00:17 2017 +0200 @@ -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.system.cpu.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.system.cpu.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/system/cpu/mongo/Fields.java Wed Jun 21 11:00:17 2017 +0200 @@ -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.system.cpu.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/system/cpu/mongo/MongoStorageHandler.java Wed Jun 21 11:00:17 2017 +0200 @@ -0,0 +1,150 @@ +/* + * 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.system.cpu.mongo; + +import static com.mongodb.client.model.Filters.and; +import static com.mongodb.client.model.Filters.eq; +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; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +import org.bson.Document; +import org.bson.conversions.Bson; + +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; + +public class MongoStorageHandler { + + private final MongoResponseBuilder.Builder mongoResponseBuilder = new MongoResponseBuilder.Builder(); + + 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.queryDocuments(documents).build(); + } + + 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.queryDocuments(documents).build(); + } + + 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); + } +}
--- a/services/system-cpu/src/main/webapp/WEB-INF/web.xml Tue Jun 20 14:19:38 2017 -0400 +++ b/services/system-cpu/src/main/webapp/WEB-INF/web.xml Wed Jun 21 11:00:17 2017 +0200 @@ -41,19 +41,19 @@ http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <servlet> - <servlet-name>SystemInfoCPUServlet</servlet-name> + <servlet-name>SystemCPUServlet</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, + com.redhat.thermostat.service.system.cpu.http, </param-value> </init-param> </servlet> <servlet-mapping> - <servlet-name>SystemInfoCPUServlet</servlet-name> + <servlet-name>SystemCPUServlet</servlet-name> <url-pattern>/0.0.1/*</url-pattern> </servlet-mapping> <!-- Service configuration -->
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/services/system-memory/src/main/java/com/redhat/thermostat/service/system/memory/http/Parameters.java Wed Jun 21 11:00:17 2017 +0200 @@ -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.system.memory.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-memory/src/main/java/com/redhat/thermostat/service/system/memory/http/SwaggerSpecResourceHandler.java Wed Jun 21 11:00:17 2017 +0200 @@ -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.system.memory.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-memory/src/main/java/com/redhat/thermostat/service/system/memory/http/SystemMemoryHttpHandler.java Wed Jun 21 11:00:17 2017 +0200 @@ -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.system.memory.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.system.memory.mongo.MongoStorageHandler; + +@Path("/") +public class SystemMemoryHttpHandler { + private final MongoStorageHandler mongoStorageHandler = new MongoStorageHandler(); + private final String collectionName = "memory-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-memory/src/main/java/com/redhat/thermostat/service/system/memory/mongo/Fields.java Wed Jun 21 11:00:17 2017 +0200 @@ -0,0 +1,44 @@ +/* + * 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.system.memory.mongo; + +class Fields { + static final String SYSTEM_ID = "systemId"; + static final String SET = "set"; + static final String LAST_UPDATED = "lastUpdated"; + static final String RESPONSE = "response"; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/services/system-memory/src/main/java/com/redhat/thermostat/service/system/memory/mongo/MongoStorageHandler.java Wed Jun 21 11:00:17 2017 +0200 @@ -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.system.memory.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.Builder mongoResponseBuilder = new MongoResponseBuilder.Builder(); + + 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.queryDocuments(documents).build(); + } + + 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); + } +}
--- a/services/system-memory/src/main/java/com/redhat/thermostat/service/systemmemory/http/Parameters.java Tue Jun 20 14:19:38 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,50 +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.service.systemmemory.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"; -}
--- a/services/system-memory/src/main/java/com/redhat/thermostat/service/systemmemory/http/SwaggerSpecResourceHandler.java Tue Jun 20 14:19:38 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,59 +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.service.systemmemory.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); - } - -}
--- a/services/system-memory/src/main/java/com/redhat/thermostat/service/systemmemory/http/SystemMemoryHttpHandler.java Tue Jun 20 14:19:38 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,133 +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.service.systemmemory.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.systemmemory.mongo.MongoStorageHandler; - -@Path("/") -public class SystemMemoryHttpHandler { - private final MongoStorageHandler mongoStorageHandler = new MongoStorageHandler(); - private final String collectionName = "memory-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(); - } - } -}
--- a/services/system-memory/src/main/java/com/redhat/thermostat/service/systemmemory/mongo/Fields.java Tue Jun 20 14:19:38 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,44 +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.service.systemmemory.mongo; - -class Fields { - static final String SYSTEM_ID = "systemId"; - static final String SET = "set"; - static final String LAST_UPDATED = "lastUpdated"; - static final String RESPONSE = "response"; -}
--- a/services/system-memory/src/main/java/com/redhat/thermostat/service/systemmemory/mongo/MongoStorageHandler.java Tue Jun 20 14:19:38 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,133 +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.service.systemmemory.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.Builder mongoResponseBuilder = new MongoResponseBuilder.Builder(); - - 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.queryDocuments(documents).build(); - } - - 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); - } -}
--- a/services/system-memory/src/main/webapp/WEB-INF/web.xml Tue Jun 20 14:19:38 2017 -0400 +++ b/services/system-memory/src/main/webapp/WEB-INF/web.xml Wed Jun 21 11:00:17 2017 +0200 @@ -41,19 +41,19 @@ http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <servlet> - <servlet-name>SystemInfoMemoryServlet</servlet-name> + <servlet-name>SystemMemoryServlet</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.systemmemory, + com.redhat.thermostat.service.system.memory.http, </param-value> </init-param> </servlet> <servlet-mapping> - <servlet-name>SystemInfoMemoryServlet</servlet-name> + <servlet-name>SystemMemoryServlet</servlet-name> <url-pattern>/0.0.1/*</url-pattern> </servlet-mapping> <!-- Service configuration -->