blob: 2ecd6770b20cd8c2a42497b76e005ee1b608ff56 [file] [log] [blame]
Thomas Vachuskab6451472015-03-31 16:03:56 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Thomas Vachuskab6451472015-03-31 16:03:56 -07003 *
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 */
16package org.onosproject.demo;
17
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;
31import java.util.Optional;
32
33/**
34 * Rest API for demos.
35 */
36@Path("intents")
37public class DemoResource extends BaseResource {
38
39
Ray Milkeyf2ab6f32015-08-20 14:25:51 -070040 /**
41 * Start the flow test.
42 *
43 * @param input JSON describing how to run the flow test
44 * @return response code OK
45 * @throws IOException if the JSON processing fails
46 */
Thomas Vachuskab6451472015-03-31 16:03:56 -070047 @POST
48 @Path("flowTest")
49 @Consumes(MediaType.APPLICATION_JSON)
50 @Produces(MediaType.APPLICATION_JSON)
51 public Response flowTest(InputStream input) throws IOException {
52 ObjectMapper mapper = new ObjectMapper();
53 JsonNode cfg = mapper.readTree(input);
Jonathan Hartd9df7bd2015-11-10 17:10:25 -080054 DemoApi demo = get(DemoApi.class);
Thomas Vachuskab6451472015-03-31 16:03:56 -070055 return Response.ok(demo.flowTest(Optional.ofNullable(cfg)).toString()).build();
56 }
57
Ray Milkeyf2ab6f32015-08-20 14:25:51 -070058 /**
59 * Set up the flow test.
60 *
61 * @param input JSON describing how to configure the flow test
62 * @return response code OK
63 * @throws IOException if the JSON processing fails
64 */
Thomas Vachuskab6451472015-03-31 16:03:56 -070065 @POST
66 @Path("setup")
67 @Consumes(MediaType.APPLICATION_JSON)
68 @Produces(MediaType.APPLICATION_JSON)
69 public Response setup(InputStream input) throws IOException {
70 ObjectMapper mapper = new ObjectMapper();
71 JsonNode cfg = mapper.readTree(input);
72 if (!cfg.has("type")) {
73 return Response.status(Response.Status.BAD_REQUEST)
74 .entity("Expected type field containing either mesh or random.").build();
75 }
76
77
Jonathan Hartd9df7bd2015-11-10 17:10:25 -080078 DemoApi.InstallType type = DemoApi.InstallType.valueOf(
Thomas Vachuskab6451472015-03-31 16:03:56 -070079 cfg.get("type").asText().toUpperCase());
Jonathan Hartd9df7bd2015-11-10 17:10:25 -080080 DemoApi demo = get(DemoApi.class);
Thomas Vachuskab6451472015-03-31 16:03:56 -070081 demo.setup(type, Optional.ofNullable(cfg.get("runParams")));
82
83 return Response.ok(mapper.createObjectNode().toString()).build();
84 }
85
Ray Milkeyf2ab6f32015-08-20 14:25:51 -070086 /**
87 * Tear down the flow test.
88 *
89 * @return response code OK
90 */
Thomas Vachuskab6451472015-03-31 16:03:56 -070091 @GET
92 @Path("teardown")
93 @Produces(MediaType.APPLICATION_JSON)
Ray Milkeyf2ab6f32015-08-20 14:25:51 -070094 public Response tearDown() {
Thomas Vachuskab6451472015-03-31 16:03:56 -070095 ObjectMapper mapper = new ObjectMapper();
Jonathan Hartd9df7bd2015-11-10 17:10:25 -080096 DemoApi demo = get(DemoApi.class);
Thomas Vachuskab6451472015-03-31 16:03:56 -070097 demo.tearDown();
98 return Response.ok(mapper.createObjectNode().toString()).build();
99 }
100
101}