alshabib | ab98466 | 2014-12-04 18:56:18 -0800 | [diff] [blame^] | 1 | /* |
| 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'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.demo; |
alshabib | fd23d31 | 2014-11-11 18:14:47 -0800 | [diff] [blame] | 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; |
alshabib | 486349d | 2014-11-25 18:09:25 -0500 | [diff] [blame] | 31 | import java.util.Optional; |
alshabib | fd23d31 | 2014-11-11 18:14:47 -0800 | [diff] [blame] | 32 | |
| 33 | /** |
| 34 | * Rest API for demos. |
| 35 | */ |
| 36 | @Path("intents") |
| 37 | public 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 | |
alshabib | 486349d | 2014-11-25 18:09:25 -0500 | [diff] [blame] | 52 | |
alshabib | fd23d31 | 2014-11-11 18:14:47 -0800 | [diff] [blame] | 53 | DemoAPI.InstallType type = DemoAPI.InstallType.valueOf( |
| 54 | cfg.get("type").asText().toUpperCase()); |
| 55 | DemoAPI demo = get(DemoAPI.class); |
alshabib | 486349d | 2014-11-25 18:09:25 -0500 | [diff] [blame] | 56 | demo.setup(type, Optional.ofNullable(cfg.get("runParams"))); |
alshabib | fd23d31 | 2014-11-11 18:14:47 -0800 | [diff] [blame] | 57 | |
| 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 | } |