alshabib | fd23d31 | 2014-11-11 18:14:47 -0800 | [diff] [blame^] | 1 | package org.onlab.onos.demo; |
| 2 | |
| 3 | import com.fasterxml.jackson.databind.JsonNode; |
| 4 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 5 | import org.onlab.rest.BaseResource; |
| 6 | |
| 7 | import javax.ws.rs.Consumes; |
| 8 | import javax.ws.rs.GET; |
| 9 | import javax.ws.rs.POST; |
| 10 | import javax.ws.rs.Path; |
| 11 | import javax.ws.rs.Produces; |
| 12 | import javax.ws.rs.core.MediaType; |
| 13 | import javax.ws.rs.core.Response; |
| 14 | import java.io.IOException; |
| 15 | import java.io.InputStream; |
| 16 | |
| 17 | /** |
| 18 | * Rest API for demos. |
| 19 | */ |
| 20 | @Path("intents") |
| 21 | public class DemoResource extends BaseResource { |
| 22 | |
| 23 | |
| 24 | @POST |
| 25 | @Path("setup") |
| 26 | @Consumes(MediaType.APPLICATION_JSON) |
| 27 | @Produces(MediaType.APPLICATION_JSON) |
| 28 | public Response setup(InputStream input) throws IOException { |
| 29 | ObjectMapper mapper = new ObjectMapper(); |
| 30 | JsonNode cfg = mapper.readTree(input); |
| 31 | if (!cfg.has("type")) { |
| 32 | return Response.status(Response.Status.BAD_REQUEST) |
| 33 | .entity("Expected type field containing either mesh or random.").build(); |
| 34 | } |
| 35 | |
| 36 | DemoAPI.InstallType type = DemoAPI.InstallType.valueOf( |
| 37 | cfg.get("type").asText().toUpperCase()); |
| 38 | DemoAPI demo = get(DemoAPI.class); |
| 39 | demo.setup(type); |
| 40 | |
| 41 | return Response.ok(mapper.createObjectNode().toString()).build(); |
| 42 | } |
| 43 | |
| 44 | @GET |
| 45 | @Path("teardown") |
| 46 | @Produces(MediaType.APPLICATION_JSON) |
| 47 | public Response tearDown() throws IOException { |
| 48 | ObjectMapper mapper = new ObjectMapper(); |
| 49 | DemoAPI demo = get(DemoAPI.class); |
| 50 | demo.tearDown(); |
| 51 | return Response.ok(mapper.createObjectNode().toString()).build(); |
| 52 | } |
| 53 | |
| 54 | } |