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; |
alshabib | 486349d | 2014-11-25 18:09:25 -0500 | [diff] [blame^] | 16 | import java.util.Optional; |
alshabib | fd23d31 | 2014-11-11 18:14:47 -0800 | [diff] [blame] | 17 | |
| 18 | /** |
| 19 | * Rest API for demos. |
| 20 | */ |
| 21 | @Path("intents") |
| 22 | public class DemoResource extends BaseResource { |
| 23 | |
| 24 | |
| 25 | @POST |
| 26 | @Path("setup") |
| 27 | @Consumes(MediaType.APPLICATION_JSON) |
| 28 | @Produces(MediaType.APPLICATION_JSON) |
| 29 | public Response setup(InputStream input) throws IOException { |
| 30 | ObjectMapper mapper = new ObjectMapper(); |
| 31 | JsonNode cfg = mapper.readTree(input); |
| 32 | if (!cfg.has("type")) { |
| 33 | return Response.status(Response.Status.BAD_REQUEST) |
| 34 | .entity("Expected type field containing either mesh or random.").build(); |
| 35 | } |
| 36 | |
alshabib | 486349d | 2014-11-25 18:09:25 -0500 | [diff] [blame^] | 37 | |
alshabib | fd23d31 | 2014-11-11 18:14:47 -0800 | [diff] [blame] | 38 | DemoAPI.InstallType type = DemoAPI.InstallType.valueOf( |
| 39 | cfg.get("type").asText().toUpperCase()); |
| 40 | DemoAPI demo = get(DemoAPI.class); |
alshabib | 486349d | 2014-11-25 18:09:25 -0500 | [diff] [blame^] | 41 | demo.setup(type, Optional.ofNullable(cfg.get("runParams"))); |
alshabib | fd23d31 | 2014-11-11 18:14:47 -0800 | [diff] [blame] | 42 | |
| 43 | return Response.ok(mapper.createObjectNode().toString()).build(); |
| 44 | } |
| 45 | |
| 46 | @GET |
| 47 | @Path("teardown") |
| 48 | @Produces(MediaType.APPLICATION_JSON) |
| 49 | public Response tearDown() throws IOException { |
| 50 | ObjectMapper mapper = new ObjectMapper(); |
| 51 | DemoAPI demo = get(DemoAPI.class); |
| 52 | demo.tearDown(); |
| 53 | return Response.ok(mapper.createObjectNode().toString()).build(); |
| 54 | } |
| 55 | |
| 56 | } |