blob: 3f8a3c535b6ac9e619953921bf7c6aa584fdddc6 [file] [log] [blame]
alshabibab984662014-12-04 18:56:18 -08001/*
2 * Copyright 2014 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.demo;
alshabibfd23d312014-11-11 18:14:47 -080017
18import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.ObjectMapper;
20import org.onlab.rest.BaseResource;
21
22import javax.ws.rs.Consumes;
23import javax.ws.rs.GET;
24import javax.ws.rs.POST;
25import javax.ws.rs.Path;
26import javax.ws.rs.Produces;
27import javax.ws.rs.core.MediaType;
28import javax.ws.rs.core.Response;
29import java.io.IOException;
30import java.io.InputStream;
alshabib486349d2014-11-25 18:09:25 -050031import java.util.Optional;
alshabibfd23d312014-11-11 18:14:47 -080032
33/**
34 * Rest API for demos.
35 */
36@Path("intents")
37public class DemoResource extends BaseResource {
38
39
40 @POST
41 @Path("setup")
42 @Consumes(MediaType.APPLICATION_JSON)
43 @Produces(MediaType.APPLICATION_JSON)
44 public Response setup(InputStream input) throws IOException {
45 ObjectMapper mapper = new ObjectMapper();
46 JsonNode cfg = mapper.readTree(input);
47 if (!cfg.has("type")) {
48 return Response.status(Response.Status.BAD_REQUEST)
49 .entity("Expected type field containing either mesh or random.").build();
50 }
51
alshabib486349d2014-11-25 18:09:25 -050052
alshabibfd23d312014-11-11 18:14:47 -080053 DemoAPI.InstallType type = DemoAPI.InstallType.valueOf(
54 cfg.get("type").asText().toUpperCase());
55 DemoAPI demo = get(DemoAPI.class);
alshabib486349d2014-11-25 18:09:25 -050056 demo.setup(type, Optional.ofNullable(cfg.get("runParams")));
alshabibfd23d312014-11-11 18:14:47 -080057
58 return Response.ok(mapper.createObjectNode().toString()).build();
59 }
60
61 @GET
62 @Path("teardown")
63 @Produces(MediaType.APPLICATION_JSON)
64 public Response tearDown() throws IOException {
65 ObjectMapper mapper = new ObjectMapper();
66 DemoAPI demo = get(DemoAPI.class);
67 demo.tearDown();
68 return Response.ok(mapper.createObjectNode().toString()).build();
69 }
70
71}