blob: 4ed4b8c867dad62072a6016b5f59b662334e6a46 [file] [log] [blame]
sangho1e575652015-05-14 00:39:53 -07001/*
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 */
16package org.onosproject.segmentrouting.web;
17
18import com.fasterxml.jackson.databind.ObjectMapper;
19import com.fasterxml.jackson.databind.node.ObjectNode;
20
21import org.onosproject.rest.AbstractWebResource;
sangho1e575652015-05-14 00:39:53 -070022import org.onosproject.segmentrouting.SegmentRoutingService;
23import org.onosproject.segmentrouting.Tunnel;
24
25import javax.ws.rs.Consumes;
26import javax.ws.rs.DELETE;
27import javax.ws.rs.GET;
28import javax.ws.rs.POST;
29import javax.ws.rs.Path;
30import javax.ws.rs.Produces;
31import javax.ws.rs.core.MediaType;
32import javax.ws.rs.core.Response;
33import java.io.IOException;
34import java.io.InputStream;
35import java.util.List;
36
37@Path("tunnel")
38public 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);
sangho0b2b6d12015-05-20 22:16:38 -070060 srService.createTunnel(tunnelInfo);
sangho1e575652015-05-14 00:39:53 -070061
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}