1 /*******************************************************************************
2 * Copyright (c) 2017 Politecnico di Torino and others.
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Apache License, Version 2.0
6 * which accompanies this distribution, and is available at
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *******************************************************************************/
9 package it.polito.verigraph.resources;
11 import java.io.IOException;
13 import java.util.HashMap;
14 import java.util.List;
16 import javax.ws.rs.Consumes;
17 import javax.ws.rs.DELETE;
18 import javax.ws.rs.GET;
19 import javax.ws.rs.POST;
20 import javax.ws.rs.PUT;
21 import javax.ws.rs.Path;
22 import javax.ws.rs.PathParam;
23 import javax.ws.rs.Produces;
24 import javax.ws.rs.core.Context;
25 import javax.ws.rs.core.MediaType;
26 import javax.ws.rs.core.Response;
27 import javax.ws.rs.core.UriInfo;
28 import javax.xml.bind.JAXBException;
29 import com.fasterxml.jackson.core.JsonParseException;
30 import com.fasterxml.jackson.databind.JsonMappingException;
31 import io.swagger.annotations.Api;
32 import io.swagger.annotations.ApiOperation;
33 import io.swagger.annotations.ApiParam;
34 import io.swagger.annotations.ApiResponse;
35 import io.swagger.annotations.ApiResponses;
36 import it.polito.neo4j.exceptions.MyInvalidIdException;
37 import it.polito.neo4j.exceptions.MyNotFoundException;
38 import it.polito.neo4j.manager.Neo4jDBManager;
39 import it.polito.verigraph.exception.BadRequestException;
40 import it.polito.verigraph.exception.ForbiddenException;
41 import it.polito.verigraph.model.Configuration;
42 import it.polito.verigraph.model.ErrorMessage;
43 import it.polito.verigraph.model.Graph;
44 import it.polito.verigraph.model.Neighbour;
45 import it.polito.verigraph.model.Node;
46 import it.polito.verigraph.service.GraphService;
47 import it.polito.verigraph.service.NodeService;
49 @Api( hidden= true, value = "", description = "Manage nodes" )
50 @Consumes(MediaType.APPLICATION_JSON)
51 @Produces(MediaType.APPLICATION_JSON)
52 public class NodeResource {
54 NodeService nodeService = new NodeService();
59 value = "Returns all nodes of a given graph",
60 notes = "Returns an array of nodes belonging to a given graph",
61 response = Node.class,
62 responseContainer = "List")
63 @ApiResponses(value = { @ApiResponse(code = 403, message = "Invalid graph id", response = ErrorMessage.class),
64 @ApiResponse(code = 404, message = "Graph not found", response = ErrorMessage.class),
65 @ApiResponse(code = 500, message = "Internal server error", response = ErrorMessage.class),
66 @ApiResponse(code = 200, message = "All the nodes have been returned in the message body", response = Node.class, responseContainer = "List") })
67 public List<Node> getNodes(@ApiParam(value = "Graph id", required = true) @PathParam("graphId") long graphId) throws JsonParseException, JsonMappingException, JAXBException, IOException, MyNotFoundException{
68 return nodeService.getAllNodes(graphId);
74 value = "Creates a node in a given graph",
75 notes = "Creates a single node for a given graph",
76 response = Response.class)
77 @ApiResponses(value = { @ApiResponse(code = 400, message = "Invalid node supplied", response = ErrorMessage.class),
78 @ApiResponse(code = 403, message = "Invalid graph id", response = ErrorMessage.class),
79 @ApiResponse(code = 404, message = "Graph not found", response = ErrorMessage.class),
80 @ApiResponse(code = 500, message = "Internal server error", response = ErrorMessage.class),
81 @ApiResponse(code = 201, message = "Node successfully created", response = Node.class)})
82 public Response addNode(
83 @ApiParam(value = "Graph id", required = true) @PathParam("graphId") long graphId,
84 @ApiParam(value = "New node object", required = true) Node node,
85 @Context UriInfo uriInfo) throws JsonParseException, JsonMappingException, JAXBException, IOException, MyInvalidIdException {
86 Node newNode = nodeService.addNode(graphId, node);
87 String newId = String.valueOf(newNode.getId());
88 URI uri = uriInfo.getAbsolutePathBuilder().path(newId).build();
89 return Response.created(uri)
98 value = "Returns a node of a given graph",
99 notes = "Returns a single node of a given graph",
100 response = Node.class)
101 @ApiResponses(value = { @ApiResponse(code = 403, message = "Invalid graph and/or node id", response = ErrorMessage.class),
102 @ApiResponse(code = 404, message = "Graph and/or node not found", response = ErrorMessage.class),
103 @ApiResponse(code = 500, message = "Internal server error", response = ErrorMessage.class),
104 @ApiResponse(code = 200, message = "The requested node has been returned in the message body", response = Node.class)})
106 @ApiParam(value = "Graph id", required = true) @PathParam("graphId") long graphId,
107 @ApiParam(value = "Node id", required = true) @PathParam("nodeId") long nodeId,
108 @Context UriInfo uriInfo) throws JsonParseException, JsonMappingException, JAXBException, IOException, MyNotFoundException{
109 Node node = nodeService.getNode(graphId, nodeId);
110 node.addLink(getUriForSelf(uriInfo, graphId, node), "self");
111 node.addLink(getUriForNeighbours(uriInfo, graphId, node), "neighbours");
116 @Path("{nodeId}/configuration")
119 value = "Adds/edits a configuration to a node of a given graph",
120 notes = "Configures a node. Once all the nodes of a graph have been configured a given policy can be verified for the graph (e.g. 'reachability' between two nodes).")
121 @ApiResponses(value = { @ApiResponse(code = 403, message = "Invalid graph and/or node id", response = ErrorMessage.class),
122 @ApiResponse(code = 404, message = "Graph and/or node not found", response = ErrorMessage.class),
123 @ApiResponse(code = 500, message = "Internal server error", response = ErrorMessage.class),
124 @ApiResponse(code = 200, message = "Configuration updated for the requested node", response=Configuration.class)})
125 public Configuration addNodeConfiguration(
126 @ApiParam(value = "Graph id", required = true) @PathParam("graphId") long graphId,
127 @ApiParam(value = "Node id", required = true) @PathParam("nodeId") long nodeId,
128 @ApiParam(value = "Node configuration", required = true) Configuration nodeConfiguration,
129 @Context UriInfo uriInfo) throws JsonParseException, JsonMappingException, JAXBException, IOException, MyNotFoundException, MyInvalidIdException{
131 Configuration conf=nodeService.addNodeConfiguration(graphId, nodeId, nodeConfiguration);
141 value = "Edits a node of a given graph",
142 notes = "Edits a single node of a given graph",
143 response = Node.class)
144 @ApiResponses(value = { @ApiResponse(code = 400, message = "Invalid node object", response = ErrorMessage.class),
145 @ApiResponse(code = 403, message = "Invalid graph and/or node id", response = ErrorMessage.class),
146 @ApiResponse(code = 404, message = "Graph and/or node not found", response = ErrorMessage.class),
147 @ApiResponse(code = 500, message = "Internal server error", response = ErrorMessage.class),
148 @ApiResponse(code = 200, message = "Node edited successfully", response = Node.class)})
149 public Node updateNode(
150 @ApiParam(value = "Graph id", required = true) @PathParam("graphId") long graphId,
151 @ApiParam(value = "Node id", required = true) @PathParam("nodeId") long nodeId,
152 @ApiParam(value = "Updated node object", required = true) Node node) throws JAXBException, IOException, MyInvalidIdException{
154 return nodeService.updateNode(graphId, node);
160 httpMethod = "DELETE",
161 value = "Deletes a node of a given graph",
162 notes = "Deletes a single node of a given graph")
163 @ApiResponses(value = { @ApiResponse(code = 403, message = "Invalid graph and/or node id", response = ErrorMessage.class),
164 @ApiResponse(code = 500, message = "Internal server error", response = ErrorMessage.class),
165 @ApiResponse(code = 204, message = "Node successfully deleted")})
166 public void deleteNode(
167 @ApiParam(value = "Graph id", required = true) @PathParam("graphId") long graphId,
168 @ApiParam(value = "Node id", required = true) @PathParam("nodeId") long nodeId) throws JsonParseException, JsonMappingException, JAXBException, IOException{
169 nodeService.removeNode(graphId, nodeId);
172 private String getUriForSelf(UriInfo uriInfo, long graphId, Node node) {
173 String uri = uriInfo.getBaseUriBuilder()
174 //.path(NodeResource.class)
175 .path(GraphResource.class)
176 .path(GraphResource.class, "getNodeResource")
177 .resolveTemplate("graphId", graphId)
178 .path(Long.toString(node.getId()))
184 private String getUriForNeighbours(UriInfo uriInfo, long graphId, Node node) {
185 String uri = uriInfo.getBaseUriBuilder()
186 .path(GraphResource.class)
187 .path(GraphResource.class, "getNodeResource")
188 .resolveTemplate("graphId", graphId)
189 .path(Long.toString(node.getId()))
193 // .path(NodeResource.class)
194 // .path(NodeResource.class, "getNeighbourResource")
195 // .path(NeighbourResource.class)
196 // .resolveTemplate("nodeId", node.getId())
202 @Path("{nodeId}/neighbours")
203 public NeighbourResource getNeighbourResource(){
204 return new NeighbourResource();