Mercurial > hg > thermostat-ng > web-gateway
changeset 188:96db1ee35432
Add /sytem-network/0.0.1 API
This patch adds a new /system-network API
The intended use (from a client perspective) is to retrieve the most recent record for a given system, which will include an array of all known network interfaces on that system, any assigned IPV4 and/or IPV6 addresses, and an optional 'display name', which (if it exists) may be more verbose than the system name for each network interface.
On my Windows box I see over 30 returned interfaces.
Reviewed-by: neugens
Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2017-June/023946.html
line wrap: on
line diff
--- a/distribution/pom.xml Wed Jun 28 09:07:14 2017 -0400 +++ b/distribution/pom.xml Fri Jun 23 08:49:10 2017 -0400 @@ -234,6 +234,13 @@ </artifactItem> <artifactItem> <groupId>com.redhat.thermostat</groupId> + <artifactId>thermostat-web-gateway-service-system-network</artifactId> + <version>${project.version}</version> + <type>war</type> + <overWrite>false</overWrite> + </artifactItem> + <artifactItem> + <groupId>com.redhat.thermostat</groupId> <artifactId>thermostat-web-gateway-service-white-pages</artifactId> <version>${project.version}</version> <type>war</type> @@ -295,6 +302,13 @@ </dependency> <dependency> <groupId>com.redhat.thermostat</groupId> + <artifactId>thermostat-web-gateway-service-system-network</artifactId> + <version>${project.version}</version> + <type>war</type> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>com.redhat.thermostat</groupId> <artifactId>thermostat-web-gateway-service-commands</artifactId> <version>${project.version}</version> <type>war</type>
--- a/distribution/src/etc/services.properties Wed Jun 28 09:07:14 2017 -0400 +++ b/distribution/src/etc/services.properties Fri Jun 23 08:49:10 2017 -0400 @@ -5,4 +5,5 @@ /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 +/system-network = thermostat-web-gateway-service-system-network-@project.version@.war /white-pages = thermostat-web-gateway-service-white-pages-@project.version@.war
--- a/services/pom.xml Wed Jun 28 09:07:14 2017 -0400 +++ b/services/pom.xml Fri Jun 23 08:49:10 2017 -0400 @@ -58,6 +58,7 @@ <module>systems</module> <module>system-cpu</module> <module>system-memory</module> + <module>system-network</module> <module>white-pages</module> </modules>
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/services/system-network/pom.xml Fri Jun 23 08:49:10 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-network</artifactId> + + <packaging>war</packaging> + + <name>Thermostat Web Gateway System Network Service</name> + + <properties> + <com.redhat.thermostat.gateway.SERVICE_NAME>system-network</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-network/src/main/java/com/redhat/thermostat/service/system/network/http/Parameters.java Fri Jun 23 08:49:10 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.system.network.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-network/src/main/java/com/redhat/thermostat/service/system/network/http/SwaggerSpecResourceHandler.java Fri Jun 23 08:49:10 2017 -0400 @@ -0,0 +1,58 @@ +/* + * 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.network.http; + +import com.redhat.thermostat.gateway.common.core.servlet.BasicResourceHandler; + +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 java.io.IOException; + +@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-network/src/main/java/com/redhat/thermostat/service/system/network/http/SystemNetworkHttpHandler.java Fri Jun 23 08:49:10 2017 -0400 @@ -0,0 +1,132 @@ +/* + * 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.network.http; + +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.network.mongo.MongoStorageHandler; + +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; + +@Path("/") +public class SystemNetworkHttpHandler { + private final MongoStorageHandler mongoStorageHandler = new MongoStorageHandler(); + private final String collectionName = "network-info"; + + @GET + @Path("/systems/{" + Parameters.SYSTEM_ID +"}") + @Consumes({ "application/json" }) + @Produces({ "application/json", "text/html; charset=utf-8" }) + public Response getNetworkInfo(@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 putNetworkInfo(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 postNetworkInfo(String body, + @PathParam(Parameters.SYSTEM_ID) String systemId, + @Context ServletContext context) { + try { + ThermostatMongoStorage storage = (ThermostatMongoStorage) context.getAttribute(ServletContextConstants.MONGODB_CLIENT_ATTRIBUTE); + mongoStorageHandler.addOne(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 deleteNetworkInfo(@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-network/src/main/java/com/redhat/thermostat/service/system/network/mongo/Fields.java Fri Jun 23 08:49:10 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.system.network.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-network/src/main/java/com/redhat/thermostat/service/system/network/mongo/MongoStorageHandler.java Fri Jun 23 08:49:10 2017 -0400 @@ -0,0 +1,140 @@ +/* + * 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.network.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.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 addOne(MongoCollection<DBObject> collection, String body, String systemId) { + if (body.length() > 0) { + DBObject obj = (DBObject) JSON.parse(body); + obj.put(Fields.SYSTEM_ID, systemId); + collection.insertOne(obj); + } + } + + 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-network/src/main/resources/system-network-swagger.yaml Fri Jun 23 08:49:10 2017 -0400 @@ -0,0 +1,165 @@ +swagger: '2.0' +info: + version: 0.0.1 + title: Thermostat Web Gateway System Network 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-network/0.0.1 +paths: + /systems/{systemId}: + parameters: + - $ref: '#/parameters/system-id' + get: + description: Get network 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/network-info-get-response' + put: + description: Update network information for system {systemId}. + parameters: + - $ref: '#/parameters/network-info-put-body' + - $ref: '#/parameters/query' + responses: + '200': + description: OK + post: + description: Add network information for system {systemId} + parameters: + - $ref: '#/parameters/network-info-array' + responses: + '200': + description: OK + delete: + description: Delete network information for system ID {systemId}. + parameters: + - $ref: '#/parameters/query' + responses: + '200': + description: OK +definitions: + network-info-get-response: + type: object + properties: + response: + $ref: '#/definitions/network-info-array' + network-info-array: + type: array + items: + $ref: '#/definitions/network-info' + network-info: + type: object + properties: + systemId: + type: string + agentId: + type: string + timeStamp: + type: integer + format: int64 + interfaces: + type: array + items: + $ref: '#/definitions/interface-info' + interface-info: + type: object + properties: + interfaceName: + type: string + displayName: + type: string + ipv4Addr: + type: string + ipV6Addr: + type: string + network-info-put-body: + type: object + properties: + "set": + type: object +parameters: + system-id: + name: systemId + in: path + required: true + type: string + network-info-array: + name: network-info-array + in: body + description: The system network information + required: true + schema: + $ref: '#/definitions/network-info-array' + network-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/network-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-network/src/main/webapp/WEB-INF/web.xml Fri Jun 23 08:49:10 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>SystemNetworkServlet</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.system.network.http, + </param-value> + </init-param> + </servlet> + <servlet-mapping> + <servlet-name>SystemNetworkServlet</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/network/SystemNetworkIntegrationTest.java Fri Jun 23 08:49:10 2017 -0400 @@ -0,0 +1,307 @@ +/* + * 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.network; + +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.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 SystemNetworkIntegrationTest extends MongoIntegrationTest { + + private static final String collectionName = "network-info"; + private static final String serviceURL = baseUrl + "/system-network/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" + + " \"timeStamp\" : " + TIMESTAMP_TOKEN + ",\n" + + " \"agentId\" : \"" + AGENT_ID + "\",\n" + + " \"interfaces\":[\n" + + " {\n" + + " \"interfaceName\":\"lo\",\n" + + " \"ip4Addr\":\"127.0.0.1\",\n" + + " \"ip6Addr\":\"0:0:0:0:0:0:0:1\"\n" + + " },\n" + + " {\n" + + " \"interfaceName\":\"wlan1\",\n" + + " \"ip4Addr\":null,\n" + + " \"ip6Addr\":\"fe80:0:0:0:8814:46e7:ff0a:21cf%wlan1\"\n" + + " }\n" + + " ]\n" + + "}\n"; + + private static class TineyNetworkInfo { + TineyNetworkInfo(String systemId, String agentId) { + this.systemId = systemId; + this.agentId = agentId; + } + String agentId; + String systemId; + } + + public SystemNetworkIntegrationTest() { + 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<TineyNetworkInfo> list = parse(response, systemid); + assertEquals(1, list.size()); + + ContentResponse response2 = get(systemid, "?limit=2"); + final List<TineyNetworkInfo> list2 = parse(response2, systemid); + assertEquals(2, list2.size()); + + ContentResponse response3 = get(systemid, "?limit=0"); + final List<TineyNetworkInfo> 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<TineyNetworkInfo> 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<TineyNetworkInfo> 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() + "]")); + 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<TineyNetworkInfo> 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<TineyNetworkInfo> 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")); + + final String systemId = json.get("systemId").getAsString(); + final String agentId = json.get("agentId").getAsString(); + //final long timeStamp = getLong(json, "timeStamp"); + + assertEquals(AGENT_ID, agentId); + if (expectedSystemId != null) { + assertEquals(expectedSystemId, systemId); + } + + TineyNetworkInfo hi = new TineyNetworkInfo(systemId, agentId); + + 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)); + } +}