Add verigraph code base
[parser.git] / verigraph / src / main / java / it / polito / escape / verify / resources / GraphResource.java
1 /*******************************************************************************
2  * Copyright (c) 2017 Politecnico di Torino and others.
3  *
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
10 package it.polito.escape.verify.resources;
11
12 import java.net.URI;
13 import java.util.List;
14
15 import javax.ws.rs.BeanParam;
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
29 import io.swagger.annotations.Api;
30 import io.swagger.annotations.ApiOperation;
31 import io.swagger.annotations.ApiParam;
32 import io.swagger.annotations.ApiResponse;
33 import io.swagger.annotations.ApiResponses;
34 import it.polito.escape.verify.model.ErrorMessage;
35 import it.polito.escape.verify.model.Graph;
36 import it.polito.escape.verify.model.Verification;
37 import it.polito.escape.verify.resources.beans.VerificationBean;
38 import it.polito.escape.verify.service.GraphService;
39 import it.polito.escape.verify.service.VerificationService;
40
41 @Path("/graphs")
42 @Api(value = "/graphs", description = "Manage graphs")
43 @Consumes(MediaType.APPLICATION_JSON)
44 @Produces(MediaType.APPLICATION_JSON)
45 public class GraphResource {
46         GraphService            graphService            = new GraphService();
47         VerificationService     verificationService     = new VerificationService();
48
49         @GET
50         @ApiOperation(  httpMethod = "GET",
51                                         value = "Returns all graphs",
52                                         notes = "Returns an array of graphs",
53                                         response = Graph.class,
54                                         responseContainer = "List")
55         @ApiResponses(value = { @ApiResponse(   code = 200,
56                                                                                         message = "All the graphs have been returned in the message body",
57                                                                                         response = Graph.class,
58                                                                                         responseContainer = "List"),
59                                 @ApiResponse(code = 500, message = "Internal server error", response = ErrorMessage.class)})
60         public List<Graph> getGraphs() {
61                 return graphService.getAllGraphs();
62         }
63
64         @POST
65         @ApiOperation(  httpMethod = "POST",
66                                         value = "Creates a graph",
67                                         notes = "Creates a signle graph",
68                                         response = Response.class)
69         @ApiResponses(value = { @ApiResponse(code = 400, message = "Invalid graph supplied", response = ErrorMessage.class),
70                                                         @ApiResponse(code = 500, message = "Internal server error", response = ErrorMessage.class),
71                                                         @ApiResponse(code = 201, message = "Graph successfully created", response = Graph.class) })
72         public Response addGraph(       @ApiParam(value = "New graph object", required = true) Graph graph,
73                                                                 @Context UriInfo uriInfo) {
74                 Graph newGraph = graphService.addGraph(graph);
75                 String newId = String.valueOf(newGraph.getId());
76                 URI uri = uriInfo.getAbsolutePathBuilder().path(newId).build();
77                 return Response.created(uri).entity(newGraph).build();
78         }
79
80         @GET
81         @Path("/{graphId}")
82         @ApiOperation(  httpMethod = "GET",
83                                         value = "Returns a graph",
84                                         notes = "Returns a signle graph",
85                                         response = Graph.class)
86         @ApiResponses(value = { @ApiResponse(code = 403, message = "Invalid graph id", response = ErrorMessage.class),
87                                                         @ApiResponse(code = 404, message = "Graph not found", response = ErrorMessage.class),
88                                                         @ApiResponse(code = 500, message = "Internal server error", response = ErrorMessage.class),
89                                                         @ApiResponse(   code = 200,
90                                                                                         message = "The requested graph has been returned in the message body",
91                                                                                         response = Graph.class) })
92         public Graph getGraph(  @ApiParam(value = "Graph id", required = true) @PathParam("graphId") long graphId,
93                                                         @Context UriInfo uriInfo) {
94                 Graph graph = graphService.getGraph(graphId);
95                 graph.addLink(getUriForSelf(uriInfo, graph), "self");
96                 graph.addLink(getUriForNodes(uriInfo, graph), "nodes");
97                 return graph;
98         }
99
100         @PUT
101         @Path("/{graphId}")
102         @ApiOperation(httpMethod = "PUT", value = "Edits a graph", notes = "Edits a single graph", response = Graph.class)
103         @ApiResponses(value = { @ApiResponse(code = 400, message = "Invalid graph object", response = ErrorMessage.class),
104                                                         @ApiResponse(code = 403, message = "Invalid graph id", response = ErrorMessage.class),
105                                                         @ApiResponse(code = 404, message = "Graph not found", response = ErrorMessage.class),
106                                                         @ApiResponse(code = 500, message = "Internal server error", response = ErrorMessage.class),
107                                                         @ApiResponse(code = 200, message = "Graph edited successfully", response = Graph.class) })
108         public Graph updateGraph(       @ApiParam(value = "Graph id", required = true) @PathParam("graphId") long id,
109                                                                 @ApiParam(value = "Updated graph object", required = true) Graph graph) {
110                 graph.setId(id);
111                 return graphService.updateGraph(graph);
112         }
113
114         @DELETE
115         @Path("/{graphId}")
116         @ApiOperation(httpMethod = "DELETE", value = "Deletes a graph", notes = "Deletes a signle graph")
117         @ApiResponses(value = { @ApiResponse(code = 403, message = "Invalid graph id", response = ErrorMessage.class),
118                                                         @ApiResponse(code = 500, message = "Internal server error", response = ErrorMessage.class),
119                                                         @ApiResponse(code = 204, message = "Graph successfully deleted") })
120         public void deleteGraph(@ApiParam(value = "Graph id", required = true) @PathParam("graphId") long id) {
121                 graphService.removeGraph(id);
122         }
123
124         @GET
125         @Path("/{graphId}/policy")
126         @ApiOperation(  httpMethod = "GET",
127                                         value = "Verifies a given policy in a graph",
128                                         notes = "In order to verify a given policy (e.g. 'reachability') all nodes of the desired graph must have a valid configuration.")
129         @ApiResponses(value = { @ApiResponse(   code = 403,
130                                                                                         message = "Invalid graph id or invalid configuration for source and/or destination node",
131                                                                                         response = ErrorMessage.class),
132                                                         @ApiResponse(   code = 404,
133                                                                                         message = "Graph not found or source node not found or destination node not found or configuration for source and/or destination node not available",
134                                                                                         response = ErrorMessage.class),
135                                                         @ApiResponse(code = 500, message = "Internal server error", response = ErrorMessage.class),})
136         public Verification verifyGraph(@ApiParam(value = "Graph id", required = true) @PathParam("graphId") long graphId,
137                                                                         @ApiParam(      value = "'source' and 'destination' must refer to names of existing nodes in the same graph, 'type' refers to the required verification between the two (e.g. 'reachability')",
138                                                                                                 required = true) @BeanParam VerificationBean verificationBean) {
139
140                 return verificationService.verify(graphId, verificationBean);
141         }
142
143         private String getUriForSelf(UriInfo uriInfo, Graph graph) {
144                 String uri = uriInfo.getBaseUriBuilder()
145                                                         .path(GraphResource.class)
146                                                         .path(Long.toString(graph.getId()))
147                                                         .build()
148                                                         .toString();
149                 return uri;
150         }
151
152         private String getUriForNodes(UriInfo uriInfo, Graph graph) {
153                 String uri = uriInfo.getBaseUriBuilder()
154                                                         .path(GraphResource.class)
155                                                         .path(GraphResource.class, "getNodeResource")
156                                                         // .path(NodeResource.class)
157                                                         .resolveTemplate("graphId", graph.getId())
158                                                         .build()
159                                                         .toString();
160                 return uri;
161         }
162
163         @Path("/{graphId}/nodes")
164         public NodeResource getNodeResource() {
165                 return new NodeResource();
166         }
167 }