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; |
| 22 | import org.onosproject.segmentrouting.DefaultTunnel; |
| 23 | import org.onosproject.segmentrouting.SegmentRoutingManager; |
| 24 | import org.onosproject.segmentrouting.SegmentRoutingService; |
| 25 | import org.onosproject.segmentrouting.Tunnel; |
| 26 | |
| 27 | import javax.ws.rs.Consumes; |
| 28 | import javax.ws.rs.DELETE; |
| 29 | import javax.ws.rs.GET; |
| 30 | import javax.ws.rs.POST; |
| 31 | import javax.ws.rs.Path; |
| 32 | import javax.ws.rs.Produces; |
| 33 | import javax.ws.rs.core.MediaType; |
| 34 | import javax.ws.rs.core.Response; |
| 35 | import java.io.IOException; |
| 36 | import java.io.InputStream; |
| 37 | import java.util.List; |
| 38 | |
| 39 | @Path("tunnel") |
| 40 | public class TunnelWebResource extends AbstractWebResource { |
| 41 | |
| 42 | private static final TunnelCodec TUNNEL_CODEC = new TunnelCodec(); |
| 43 | |
| 44 | @GET |
| 45 | @Produces(MediaType.APPLICATION_JSON) |
| 46 | public Response getTunnel() { |
| 47 | SegmentRoutingService srService = get(SegmentRoutingService.class); |
| 48 | List<Tunnel> tunnels = srService.getTunnels(); |
| 49 | ObjectNode result = new ObjectMapper().createObjectNode(); |
| 50 | result.set("tunnel", new TunnelCodec().encode(tunnels, this)); |
| 51 | |
| 52 | return ok(result.toString()).build(); |
| 53 | } |
| 54 | |
| 55 | @POST |
| 56 | @Consumes(MediaType.APPLICATION_JSON) |
| 57 | public Response createTunnel(InputStream input) throws IOException { |
| 58 | ObjectMapper mapper = new ObjectMapper(); |
| 59 | ObjectNode tunnelJson = (ObjectNode) mapper.readTree(input); |
| 60 | SegmentRoutingService srService = get(SegmentRoutingService.class); |
| 61 | Tunnel tunnelInfo = TUNNEL_CODEC.decode(tunnelJson, this); |
| 62 | Tunnel tunnel = new DefaultTunnel((SegmentRoutingManager) srService, |
| 63 | tunnelInfo.id(), tunnelInfo.labelIds()); |
| 64 | srService.createTunnel(tunnel); |
| 65 | |
| 66 | return Response.ok().build(); |
| 67 | } |
| 68 | |
| 69 | @DELETE |
| 70 | @Consumes(MediaType.APPLICATION_JSON) |
| 71 | public Response removeTunnel(InputStream input) throws IOException { |
| 72 | ObjectMapper mapper = new ObjectMapper(); |
| 73 | ObjectNode tunnelJson = (ObjectNode) mapper.readTree(input); |
| 74 | SegmentRoutingService srService = get(SegmentRoutingService.class); |
| 75 | Tunnel tunnelInfo = TUNNEL_CODEC.decode(tunnelJson, this); |
| 76 | srService.removeTunnel(tunnelInfo); |
| 77 | |
| 78 | return Response.ok().build(); |
| 79 | } |
| 80 | |
| 81 | } |