sangho | 1e57565 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
sangho | 1e57565 | 2015-05-14 00:39:53 -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.segmentrouting.web; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 20 | |
| 21 | import org.onosproject.rest.AbstractWebResource; |
sangho | 1e57565 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 22 | import org.onosproject.segmentrouting.SegmentRoutingService; |
| 23 | import org.onosproject.segmentrouting.Tunnel; |
| 24 | |
| 25 | import javax.ws.rs.Consumes; |
| 26 | import javax.ws.rs.DELETE; |
| 27 | import javax.ws.rs.GET; |
| 28 | import javax.ws.rs.POST; |
| 29 | import javax.ws.rs.Path; |
| 30 | import javax.ws.rs.Produces; |
| 31 | import javax.ws.rs.core.MediaType; |
| 32 | import javax.ws.rs.core.Response; |
| 33 | import java.io.IOException; |
| 34 | import java.io.InputStream; |
| 35 | import java.util.List; |
| 36 | |
Ray Milkey | b784adb | 2018-04-02 15:33:07 -0700 | [diff] [blame^] | 37 | import static org.onlab.util.Tools.readTreeFromStream; |
| 38 | |
Ray Milkey | f2ab6f3 | 2015-08-20 14:25:51 -0700 | [diff] [blame] | 39 | /** |
| 40 | * Query, create and remove segment routing tunnels. |
| 41 | */ |
sangho | 1e57565 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 42 | @Path("tunnel") |
| 43 | public class TunnelWebResource extends AbstractWebResource { |
| 44 | |
| 45 | private static final TunnelCodec TUNNEL_CODEC = new TunnelCodec(); |
| 46 | |
Ray Milkey | f2ab6f3 | 2015-08-20 14:25:51 -0700 | [diff] [blame] | 47 | /** |
| 48 | * Get all segment routing tunnels. |
| 49 | * Returns an array of segment routing tunnels. |
| 50 | * |
| 51 | * @return status of OK |
| 52 | */ |
sangho | 1e57565 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 53 | @GET |
| 54 | @Produces(MediaType.APPLICATION_JSON) |
| 55 | public Response getTunnel() { |
| 56 | SegmentRoutingService srService = get(SegmentRoutingService.class); |
| 57 | List<Tunnel> tunnels = srService.getTunnels(); |
| 58 | ObjectNode result = new ObjectMapper().createObjectNode(); |
| 59 | result.set("tunnel", new TunnelCodec().encode(tunnels, this)); |
| 60 | |
| 61 | return ok(result.toString()).build(); |
| 62 | } |
| 63 | |
Ray Milkey | f2ab6f3 | 2015-08-20 14:25:51 -0700 | [diff] [blame] | 64 | /** |
| 65 | * Create a new segment routing tunnel. |
| 66 | * |
| 67 | * @param input JSON stream for tunnel to create |
| 68 | * @return status of the request - OK if the tunnel is created, |
Ray Milkey | f2ab6f3 | 2015-08-20 14:25:51 -0700 | [diff] [blame] | 69 | * @throws IOException if the JSON is invalid |
| 70 | */ |
sangho | 1e57565 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 71 | @POST |
| 72 | @Consumes(MediaType.APPLICATION_JSON) |
| 73 | public Response createTunnel(InputStream input) throws IOException { |
| 74 | ObjectMapper mapper = new ObjectMapper(); |
Ray Milkey | b784adb | 2018-04-02 15:33:07 -0700 | [diff] [blame^] | 75 | ObjectNode tunnelJson = readTreeFromStream(mapper, input); |
sangho | 1e57565 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 76 | SegmentRoutingService srService = get(SegmentRoutingService.class); |
| 77 | Tunnel tunnelInfo = TUNNEL_CODEC.decode(tunnelJson, this); |
sangho | 0b2b6d1 | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 78 | srService.createTunnel(tunnelInfo); |
sangho | 1e57565 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 79 | |
| 80 | return Response.ok().build(); |
| 81 | } |
| 82 | |
Ray Milkey | f2ab6f3 | 2015-08-20 14:25:51 -0700 | [diff] [blame] | 83 | /** |
| 84 | * Delete a segment routing tunnel. |
| 85 | * |
| 86 | * @param input JSON stream for tunnel to delete |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 87 | * @return 204 NO CONTENT, if the tunnel is removed |
Ray Milkey | f2ab6f3 | 2015-08-20 14:25:51 -0700 | [diff] [blame] | 88 | * @throws IOException if JSON is invalid |
| 89 | */ |
sangho | 1e57565 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 90 | @DELETE |
| 91 | @Consumes(MediaType.APPLICATION_JSON) |
| 92 | public Response removeTunnel(InputStream input) throws IOException { |
| 93 | ObjectMapper mapper = new ObjectMapper(); |
Ray Milkey | b784adb | 2018-04-02 15:33:07 -0700 | [diff] [blame^] | 94 | ObjectNode tunnelJson = readTreeFromStream(mapper, input); |
sangho | 1e57565 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 95 | SegmentRoutingService srService = get(SegmentRoutingService.class); |
| 96 | Tunnel tunnelInfo = TUNNEL_CODEC.decode(tunnelJson, this); |
| 97 | srService.removeTunnel(tunnelInfo); |
| 98 | |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 99 | return Response.noContent().build(); |
sangho | 1e57565 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | } |