sangho | 1e57565 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 | */ |
| 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 | |
| 37 | @Path("tunnel") |
| 38 | public class TunnelWebResource extends AbstractWebResource { |
| 39 | |
| 40 | private static final TunnelCodec TUNNEL_CODEC = new TunnelCodec(); |
| 41 | |
| 42 | @GET |
| 43 | @Produces(MediaType.APPLICATION_JSON) |
| 44 | public Response getTunnel() { |
| 45 | SegmentRoutingService srService = get(SegmentRoutingService.class); |
| 46 | List<Tunnel> tunnels = srService.getTunnels(); |
| 47 | ObjectNode result = new ObjectMapper().createObjectNode(); |
| 48 | result.set("tunnel", new TunnelCodec().encode(tunnels, this)); |
| 49 | |
| 50 | return ok(result.toString()).build(); |
| 51 | } |
| 52 | |
| 53 | @POST |
| 54 | @Consumes(MediaType.APPLICATION_JSON) |
| 55 | public Response createTunnel(InputStream input) throws IOException { |
| 56 | ObjectMapper mapper = new ObjectMapper(); |
| 57 | ObjectNode tunnelJson = (ObjectNode) mapper.readTree(input); |
| 58 | SegmentRoutingService srService = get(SegmentRoutingService.class); |
| 59 | Tunnel tunnelInfo = TUNNEL_CODEC.decode(tunnelJson, this); |
sangho | 0b2b6d1 | 2015-05-20 22:16:38 -0700 | [diff] [blame^] | 60 | srService.createTunnel(tunnelInfo); |
sangho | 1e57565 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 61 | |
| 62 | return Response.ok().build(); |
| 63 | } |
| 64 | |
| 65 | @DELETE |
| 66 | @Consumes(MediaType.APPLICATION_JSON) |
| 67 | public Response removeTunnel(InputStream input) throws IOException { |
| 68 | ObjectMapper mapper = new ObjectMapper(); |
| 69 | ObjectNode tunnelJson = (ObjectNode) mapper.readTree(input); |
| 70 | SegmentRoutingService srService = get(SegmentRoutingService.class); |
| 71 | Tunnel tunnelInfo = TUNNEL_CODEC.decode(tunnelJson, this); |
| 72 | srService.removeTunnel(tunnelInfo); |
| 73 | |
| 74 | return Response.ok().build(); |
| 75 | } |
| 76 | |
| 77 | } |