blob: 94fcd69961635368e02339635edbee7a22ab71df [file] [log] [blame]
sangho1e575652015-05-14 00:39:53 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
sangho1e575652015-05-14 00:39:53 -07003 *
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
Ray Milkeyf2ab6f32015-08-20 14:25:51 -070037/**
38 * Query, create and remove segment routing tunnels.
39 */
sangho1e575652015-05-14 00:39:53 -070040@Path("tunnel")
41public class TunnelWebResource extends AbstractWebResource {
42
43 private static final TunnelCodec TUNNEL_CODEC = new TunnelCodec();
44
Ray Milkeyf2ab6f32015-08-20 14:25:51 -070045 /**
46 * Get all segment routing tunnels.
47 * Returns an array of segment routing tunnels.
48 *
49 * @return status of OK
50 */
sangho1e575652015-05-14 00:39:53 -070051 @GET
52 @Produces(MediaType.APPLICATION_JSON)
53 public Response getTunnel() {
54 SegmentRoutingService srService = get(SegmentRoutingService.class);
55 List<Tunnel> tunnels = srService.getTunnels();
56 ObjectNode result = new ObjectMapper().createObjectNode();
57 result.set("tunnel", new TunnelCodec().encode(tunnels, this));
58
59 return ok(result.toString()).build();
60 }
61
Ray Milkeyf2ab6f32015-08-20 14:25:51 -070062 /**
63 * Create a new segment routing tunnel.
64 *
65 * @param input JSON stream for tunnel to create
66 * @return status of the request - OK if the tunnel is created,
Ray Milkeyf2ab6f32015-08-20 14:25:51 -070067 * @throws IOException if the JSON is invalid
68 */
sangho1e575652015-05-14 00:39:53 -070069 @POST
70 @Consumes(MediaType.APPLICATION_JSON)
71 public Response createTunnel(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);
sangho0b2b6d12015-05-20 22:16:38 -070076 srService.createTunnel(tunnelInfo);
sangho1e575652015-05-14 00:39:53 -070077
78 return Response.ok().build();
79 }
80
Ray Milkeyf2ab6f32015-08-20 14:25:51 -070081 /**
82 * Delete a segment routing tunnel.
83 *
84 * @param input JSON stream for tunnel to delete
Jian Lic2a542b2016-05-10 11:48:19 -070085 * @return 204 NO CONTENT, if the tunnel is removed
Ray Milkeyf2ab6f32015-08-20 14:25:51 -070086 * @throws IOException if JSON is invalid
87 */
sangho1e575652015-05-14 00:39:53 -070088 @DELETE
89 @Consumes(MediaType.APPLICATION_JSON)
90 public Response removeTunnel(InputStream input) throws IOException {
91 ObjectMapper mapper = new ObjectMapper();
92 ObjectNode tunnelJson = (ObjectNode) mapper.readTree(input);
93 SegmentRoutingService srService = get(SegmentRoutingService.class);
94 Tunnel tunnelInfo = TUNNEL_CODEC.decode(tunnelJson, this);
95 srService.removeTunnel(tunnelInfo);
96
Jian Lic2a542b2016-05-10 11:48:19 -070097 return Response.noContent().build();
sangho1e575652015-05-14 00:39:53 -070098 }
99
100}