changeset 179:30a7fa71b76a

Add the /system-memory API to the web gateway. The bug # is 3402 - http://icedtea.classpath.org/bugzilla/show_bug.cgi?id=3402 Reviewed-by: sgehwolf Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2017-June/023671.html
author Simon Tooke <stooke@redhat.com>
date Wed, 14 Jun 2017 13:59:56 -0400
parents ce4241a084e1
children 823905fe645a
files distribution/pom.xml distribution/src/etc/services.properties services/pom.xml services/system-memory/pom.xml 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/resources/memory-info-swagger.yaml services/system-memory/src/main/webapp/WEB-INF/web.xml tests/integration-tests/src/test/java/com/redhat/thermostat/gateway/service/system/memory/SystemMemoryIntegrationTest.java
diffstat 12 files changed, 1077 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/distribution/pom.xml	Wed Jun 14 09:07:23 2017 -0400
+++ b/distribution/pom.xml	Wed Jun 14 13:59:56 2017 -0400
@@ -225,13 +225,13 @@
                             <type>war</type>
                             <overWrite>false</overWrite>
                         </artifactItem>
-                        <!--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>
                         <artifactItem>
                             <groupId>com.redhat.thermostat</groupId>
                             <artifactId>thermostat-web-gateway-service-white-pages</artifactId>
@@ -286,13 +286,13 @@
             <type>war</type>
             <scope>provided</scope>
         </dependency>
-        <!--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>
         <dependency>
             <groupId>com.redhat.thermostat</groupId>
             <artifactId>thermostat-web-gateway-service-commands</artifactId>
--- a/distribution/src/etc/services.properties	Wed Jun 14 09:07:23 2017 -0400
+++ b/distribution/src/etc/services.properties	Wed Jun 14 13:59:56 2017 -0400
@@ -4,4 +4,5 @@
 /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
+/system-memory = thermostat-web-gateway-service-system-memory-@project.version@.war
 /white-pages = thermostat-web-gateway-service-white-pages-@project.version@.war
--- a/services/pom.xml	Wed Jun 14 09:07:23 2017 -0400
+++ b/services/pom.xml	Wed Jun 14 13:59:56 2017 -0400
@@ -57,7 +57,7 @@
         <module>jvm-memory</module>
         <module>systems</module>
         <module>system-cpu</module>
-        <!--module>system-memory</module-->
+        <module>system-memory</module>
         <module>white-pages</module>
     </modules>
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/services/system-memory/pom.xml	Wed Jun 14 13:59:56 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-memory</artifactId>
+
+    <packaging>war</packaging>
+
+    <name>Thermostat Web Gateway System Memory Service</name>
+
+    <properties>
+        <com.redhat.thermostat.gateway.SERVICE_NAME>system-memory</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-memory/src/main/java/com/redhat/thermostat/service/systemmemory/http/Parameters.java	Wed Jun 14 13:59:56 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.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";
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/services/system-memory/src/main/java/com/redhat/thermostat/service/systemmemory/http/SwaggerSpecResourceHandler.java	Wed Jun 14 13:59:56 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.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);
+    }
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/services/system-memory/src/main/java/com/redhat/thermostat/service/systemmemory/http/SystemMemoryHttpHandler.java	Wed Jun 14 13:59:56 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.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();
+        }
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/services/system-memory/src/main/java/com/redhat/thermostat/service/systemmemory/mongo/Fields.java	Wed Jun 14 13:59:56 2017 -0400
@@ -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.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";
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/services/system-memory/src/main/java/com/redhat/thermostat/service/systemmemory/mongo/MongoStorageHandler.java	Wed Jun 14 13:59:56 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.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 mongoResponseBuilder = new MongoResponseBuilder();
+
+    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);
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/services/system-memory/src/main/resources/memory-info-swagger.yaml	Wed Jun 14 13:59:56 2017 -0400
@@ -0,0 +1,171 @@
+swagger: '2.0'
+info:
+  version: 0.0.1
+  title: Thermostat Web Gateway Memory 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-memory/0.0.1
+paths:
+  /systems/{systemId}:
+    parameters:
+      - $ref: '#/parameters/system-id'
+    get:
+      description: Get Memory 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/memory-info-get-response'
+    put:
+      description: Update Memory information for system {systemId}.
+      parameters:
+        - $ref: '#/parameters/memory-info-put-body'
+        - $ref: '#/parameters/query'
+      responses:
+        '200':
+          description: OK
+    post:
+      description: Add Memory information for system {systemId}
+      parameters:
+        - $ref: '#/parameters/memory-info-array'
+      responses:
+        '200':
+          description: OK
+    delete:
+      description: Delete Memory information for system ID {systemId}.
+      parameters:
+        - $ref: '#/parameters/query'
+      responses:
+        '200':
+          description: OK
+definitions:
+  memory-info-get-response:
+    type: object
+    properties:
+      response:
+        $ref: '#/definitions/memory-info-array'
+  memory-info-array:
+    type: array
+    items:
+      $ref: '#/definitions/memory-info'
+  memory-info:
+    type: object
+    properties:
+      systemId:
+        type: string
+      agentId:
+        type: string
+      timestamp:
+        type: integer
+        format: int64
+      total:
+        type: integer
+        format: int64
+      free:
+        type: integer
+        format: int64
+      buffers:
+        type: integer
+        format: int64
+      cached:
+        type: integer
+        format: int64
+      swapTotal:
+        type: integer
+        format: int64
+      swapFree:
+        type: integer
+        format: int64
+      commitLimit:
+        type: integer
+        format: int64
+  memory-info-put-body:
+    type: object
+    properties:
+      "set":
+        type: object
+parameters:
+  system-id:
+    name: systemId
+    in: path
+    required: true
+    type: string
+  memory-info-array:
+    name: memory-info-array
+    in: body
+    description: The system Memory information
+    required: true
+    schema:
+      $ref: '#/definitions/memory-info-array'
+  memory-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/memory-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-memory/src/main/webapp/WEB-INF/web.xml	Wed Jun 14 13:59:56 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>SystemInfoMemoryServlet</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,
+            </param-value>
+        </init-param>
+    </servlet>
+    <servlet-mapping>
+        <servlet-name>SystemInfoMemoryServlet</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/memory/SystemMemoryIntegrationTest.java	Wed Jun 14 13:59:56 2017 -0400
@@ -0,0 +1,316 @@
+/*
+ * 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.memory;
+
+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 SystemMemoryIntegrationTest extends MongoIntegrationTest {
+    
+    private static final String collectionName = "memory-info";
+    private static final String serviceURL = baseUrl + "/system-memory/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 long timeStamp = java.lang.System.nanoTime();
+    private static final String memInfoJSON =
+            "{\n" +
+            "    \"total\" : 12566220800,\n" +
+            "    \"free\" : 2338582528,\n" +
+            "    \"buffers\" : 0,\n" +
+            "    \"cached\" : 0,\n" +
+            "    \"swapTotal\" : 17666494464,\n" +
+            "    \"swapFree\" : 3524055040,\n" +
+            "    \"commitLimit\" : 0,\n" +
+            "    \"timeStamp\" : " + TIMESTAMP_TOKEN + ",\n" +
+            "    \"agentId\" : \"" + AGENT_ID + "\",\n" +
+            "}";
+
+    private static class TinyMemoryInfo {
+        TinyMemoryInfo(String systemId, String agentId, long total, long free, long buffers) {
+            this.systemId = systemId;
+            this.agentId = agentId;
+            this.total = total;
+            this.free = free;
+            this.buffers = buffers;
+        }
+        String agentId;
+        String systemId;
+        long total;
+        long free;
+        long buffers;
+    }
+
+    public SystemMemoryIntegrationTest() {
+        super(serviceURL, collectionName);
+    }
+
+    @Test
+    public void testGetAll() throws InterruptedException, TimeoutException, ExecutionException {
+
+        final String systemid = getRandomSystemId();
+
+        post(systemid);
+        post(systemid);
+        post(systemid);
+
+        ContentResponse response = get(systemid);
+        final List<TinyMemoryInfo> list = parse(response, systemid);
+        assertEquals(1, list.size());
+
+        ContentResponse response2 = get(systemid, "?limit=2");
+        final List<TinyMemoryInfo> list2 = parse(response2, systemid);
+        assertEquals(2, list2.size());
+
+        ContentResponse response3 = get(systemid, "?limit=0");
+        final List<TinyMemoryInfo> 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<TinyMemoryInfo> list1 = parse(response1, systemid);
+        assertEquals(1, list1.size());
+
+        // modify it
+        put(systemid, timestamp+1);
+
+        // ensure it was changed
+        final ContentResponse response2 = getKnown(systemid);
+        final List<TinyMemoryInfo> list2 = parse(response2, systemid);
+        assertEquals(1, list2.size());
+    }
+
+    @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 static long getLong(JsonObject json, final String id) {
+        JsonElement el = json.get(id);
+        if (el.isJsonObject()) {
+            final JsonObject o = el.getAsJsonObject();
+            return o.get("$numberLong").getAsLong();
+        } else {
+            return el.getAsLong();
+        }
+    }
+
+    private List<TinyMemoryInfo> 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<TinyMemoryInfo> 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("total"));
+            assertTrue(json.has("free"));
+            assertTrue(json.has("buffers"));
+
+            final String systemId = json.get("systemId").getAsString();
+            final String agentId = json.get("agentId").getAsString();
+            //final long timeStamp = getLong(json, "timeStamp");
+            final long total = getLong(json, "total");
+            final long free = getLong(json, "free");
+            final long bufsiz = getLong(json, "buffers");
+
+            assertEquals(AGENT_ID, agentId);
+            if (expectedSystemId != null) {
+                assertEquals(expectedSystemId, systemId);
+            }
+
+            TinyMemoryInfo hi = new TinyMemoryInfo(systemId, agentId, total, free, bufsiz);
+
+            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() {
+        timeStamp += 1;
+        return timeStamp;
+    }
+
+    private String createJSON() {
+        return createJSON(getTimestamp());
+    }
+
+    private String createJSON(final long ts) {
+        return memInfoJSON.replace(TIMESTAMP_TOKEN, Long.toString(ts));
+    }
+}