blob: 5ce3ab818e4e8c758924cbc502271cfb8d6e1230 [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 /**
sivachidambaram subramanianf773b652017-05-09 14:40:04 +053059 * Start the flow objective test.
60 *
61 * @param input JSON describing how to run the flow objective test
62 * @return response code OK
63 * @throws IOException if the JSON processing fails
64 */
65 @POST
66 @Path("flowObjTest")
67 @Consumes(MediaType.APPLICATION_JSON)
68 @Produces(MediaType.APPLICATION_JSON)
69 public Response flowObjTest(InputStream input) throws IOException {
70 ObjectMapper mapper = new ObjectMapper();
71 JsonNode cfg = mapper.readTree(input);
72 DemoApi demo = get(DemoApi.class);
73 return Response.ok(demo.flowObjTest(Optional.ofNullable(cfg)).toString()).build();
74 }
75
76 /**
Ray Milkeyf2ab6f32015-08-20 14:25:51 -070077 * Set up the flow test.
78 *
79 * @param input JSON describing how to configure the flow test
80 * @return response code OK
81 * @throws IOException if the JSON processing fails
82 */
Thomas Vachuskab6451472015-03-31 16:03:56 -070083 @POST
84 @Path("setup")
85 @Consumes(MediaType.APPLICATION_JSON)
86 @Produces(MediaType.APPLICATION_JSON)
87 public Response setup(InputStream input) throws IOException {
88 ObjectMapper mapper = new ObjectMapper();
89 JsonNode cfg = mapper.readTree(input);
90 if (!cfg.has("type")) {
91 return Response.status(Response.Status.BAD_REQUEST)
92 .entity("Expected type field containing either mesh or random.").build();
93 }
94
95
Jonathan Hartd9df7bd2015-11-10 17:10:25 -080096 DemoApi.InstallType type = DemoApi.InstallType.valueOf(
Thomas Vachuskab6451472015-03-31 16:03:56 -070097 cfg.get("type").asText().toUpperCase());
Jonathan Hartd9df7bd2015-11-10 17:10:25 -080098 DemoApi demo = get(DemoApi.class);
Thomas Vachuskab6451472015-03-31 16:03:56 -070099 demo.setup(type, Optional.ofNullable(cfg.get("runParams")));
100
101 return Response.ok(mapper.createObjectNode().toString()).build();
102 }
103
Ray Milkeyf2ab6f32015-08-20 14:25:51 -0700104 /**
105 * Tear down the flow test.
106 *
107 * @return response code OK
108 */
Thomas Vachuskab6451472015-03-31 16:03:56 -0700109 @GET
110 @Path("teardown")
111 @Produces(MediaType.APPLICATION_JSON)
Ray Milkeyf2ab6f32015-08-20 14:25:51 -0700112 public Response tearDown() {
Thomas Vachuskab6451472015-03-31 16:03:56 -0700113 ObjectMapper mapper = new ObjectMapper();
Jonathan Hartd9df7bd2015-11-10 17:10:25 -0800114 DemoApi demo = get(DemoApi.class);
Thomas Vachuskab6451472015-03-31 16:03:56 -0700115 demo.tearDown();
116 return Response.ok(mapper.createObjectNode().toString()).build();
117 }
118
119}