blob: c531e3f423eb90e95d72a02e1fa3c60a1b59bd4f [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;
22import org.onosproject.segmentrouting.DefaultTunnel;
23import org.onosproject.segmentrouting.SegmentRoutingManager;
24import org.onosproject.segmentrouting.SegmentRoutingService;
25import org.onosproject.segmentrouting.Tunnel;
26
27import javax.ws.rs.Consumes;
28import javax.ws.rs.DELETE;
29import javax.ws.rs.GET;
30import javax.ws.rs.POST;
31import javax.ws.rs.Path;
32import javax.ws.rs.Produces;
33import javax.ws.rs.core.MediaType;
34import javax.ws.rs.core.Response;
35import java.io.IOException;
36import java.io.InputStream;
37import java.util.List;
38
39@Path("tunnel")
40public 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}