blob: a91d7718a831ff3268f29893b2e90611ee70c4d4 [file] [log] [blame]
Brian O'Connorabafb502014-12-02 22:26:20 -08001package org.onosproject.demo;
alshabibfd23d312014-11-11 18:14:47 -08002
3import com.fasterxml.jackson.databind.JsonNode;
4import com.fasterxml.jackson.databind.ObjectMapper;
5import org.onlab.rest.BaseResource;
6
7import javax.ws.rs.Consumes;
8import javax.ws.rs.GET;
9import javax.ws.rs.POST;
10import javax.ws.rs.Path;
11import javax.ws.rs.Produces;
12import javax.ws.rs.core.MediaType;
13import javax.ws.rs.core.Response;
14import java.io.IOException;
15import java.io.InputStream;
alshabib486349d2014-11-25 18:09:25 -050016import java.util.Optional;
alshabibfd23d312014-11-11 18:14:47 -080017
18/**
19 * Rest API for demos.
20 */
21@Path("intents")
22public 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
alshabib486349d2014-11-25 18:09:25 -050037
alshabibfd23d312014-11-11 18:14:47 -080038 DemoAPI.InstallType type = DemoAPI.InstallType.valueOf(
39 cfg.get("type").asText().toUpperCase());
40 DemoAPI demo = get(DemoAPI.class);
alshabib486349d2014-11-25 18:09:25 -050041 demo.setup(type, Optional.ofNullable(cfg.get("runParams")));
alshabibfd23d312014-11-11 18:14:47 -080042
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}