Thomas Vachuska | b645147 | 2015-03-31 16:03:56 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Thomas Vachuska | b645147 | 2015-03-31 16:03:56 -0700 | [diff] [blame] | 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 | */ |
| 16 | package org.onosproject.demo; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 20 | import org.onlab.rest.BaseResource; |
| 21 | |
| 22 | import javax.ws.rs.Consumes; |
| 23 | import javax.ws.rs.GET; |
| 24 | import javax.ws.rs.POST; |
| 25 | import javax.ws.rs.Path; |
| 26 | import javax.ws.rs.Produces; |
| 27 | import javax.ws.rs.core.MediaType; |
| 28 | import javax.ws.rs.core.Response; |
| 29 | import java.io.IOException; |
| 30 | import java.io.InputStream; |
| 31 | import java.util.Optional; |
| 32 | |
| 33 | /** |
| 34 | * Rest API for demos. |
| 35 | */ |
| 36 | @Path("intents") |
| 37 | public class DemoResource extends BaseResource { |
| 38 | |
| 39 | |
Ray Milkey | f2ab6f3 | 2015-08-20 14:25:51 -0700 | [diff] [blame] | 40 | /** |
| 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 Vachuska | b645147 | 2015-03-31 16:03:56 -0700 | [diff] [blame] | 47 | @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 Hart | d9df7bd | 2015-11-10 17:10:25 -0800 | [diff] [blame] | 54 | DemoApi demo = get(DemoApi.class); |
Thomas Vachuska | b645147 | 2015-03-31 16:03:56 -0700 | [diff] [blame] | 55 | return Response.ok(demo.flowTest(Optional.ofNullable(cfg)).toString()).build(); |
| 56 | } |
| 57 | |
Ray Milkey | f2ab6f3 | 2015-08-20 14:25:51 -0700 | [diff] [blame] | 58 | /** |
| 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 Vachuska | b645147 | 2015-03-31 16:03:56 -0700 | [diff] [blame] | 65 | @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 Hart | d9df7bd | 2015-11-10 17:10:25 -0800 | [diff] [blame] | 78 | DemoApi.InstallType type = DemoApi.InstallType.valueOf( |
Thomas Vachuska | b645147 | 2015-03-31 16:03:56 -0700 | [diff] [blame] | 79 | cfg.get("type").asText().toUpperCase()); |
Jonathan Hart | d9df7bd | 2015-11-10 17:10:25 -0800 | [diff] [blame] | 80 | DemoApi demo = get(DemoApi.class); |
Thomas Vachuska | b645147 | 2015-03-31 16:03:56 -0700 | [diff] [blame] | 81 | demo.setup(type, Optional.ofNullable(cfg.get("runParams"))); |
| 82 | |
| 83 | return Response.ok(mapper.createObjectNode().toString()).build(); |
| 84 | } |
| 85 | |
Ray Milkey | f2ab6f3 | 2015-08-20 14:25:51 -0700 | [diff] [blame] | 86 | /** |
| 87 | * Tear down the flow test. |
| 88 | * |
| 89 | * @return response code OK |
| 90 | */ |
Thomas Vachuska | b645147 | 2015-03-31 16:03:56 -0700 | [diff] [blame] | 91 | @GET |
| 92 | @Path("teardown") |
| 93 | @Produces(MediaType.APPLICATION_JSON) |
Ray Milkey | f2ab6f3 | 2015-08-20 14:25:51 -0700 | [diff] [blame] | 94 | public Response tearDown() { |
Thomas Vachuska | b645147 | 2015-03-31 16:03:56 -0700 | [diff] [blame] | 95 | ObjectMapper mapper = new ObjectMapper(); |
Jonathan Hart | d9df7bd | 2015-11-10 17:10:25 -0800 | [diff] [blame] | 96 | DemoApi demo = get(DemoApi.class); |
Thomas Vachuska | b645147 | 2015-03-31 16:03:56 -0700 | [diff] [blame] | 97 | demo.tearDown(); |
| 98 | return Response.ok(mapper.createObjectNode().toString()).build(); |
| 99 | } |
| 100 | |
| 101 | } |