blob: 010f6c9d3d3353560f129c401c15f40ee0d59c9b [file] [log] [blame]
sangho1e575652015-05-14 00:39:53 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
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;
sangho1e575652015-05-14 00:39:53 -070029import javax.ws.rs.Produces;
30import javax.ws.rs.core.MediaType;
31import javax.ws.rs.core.Response;
32import java.io.IOException;
33import java.io.InputStream;
34import java.util.List;
35
Ray Milkey86ee5e82018-04-02 15:33:07 -070036import static org.onlab.util.Tools.readTreeFromStream;
37
Ray Milkeyf2ab6f32015-08-20 14:25:51 -070038/**
39 * Query, create and remove segment routing tunnels.
40 */
Andreas Pantelopouloscd339592018-02-23 14:18:00 -080041// @Path("tunnel")
sangho1e575652015-05-14 00:39:53 -070042public class TunnelWebResource extends AbstractWebResource {
43
44 private static final TunnelCodec TUNNEL_CODEC = new TunnelCodec();
45
Ray Milkeyf2ab6f32015-08-20 14:25:51 -070046 /**
47 * Get all segment routing tunnels.
48 * Returns an array of segment routing tunnels.
49 *
50 * @return status of OK
51 */
sangho1e575652015-05-14 00:39:53 -070052 @GET
53 @Produces(MediaType.APPLICATION_JSON)
54 public Response getTunnel() {
55 SegmentRoutingService srService = get(SegmentRoutingService.class);
56 List<Tunnel> tunnels = srService.getTunnels();
57 ObjectNode result = new ObjectMapper().createObjectNode();
58 result.set("tunnel", new TunnelCodec().encode(tunnels, this));
59
60 return ok(result.toString()).build();
61 }
62
Ray Milkeyf2ab6f32015-08-20 14:25:51 -070063 /**
64 * Create a new segment routing tunnel.
65 *
66 * @param input JSON stream for tunnel to create
67 * @return status of the request - OK if the tunnel is created,
Ray Milkeyf2ab6f32015-08-20 14:25:51 -070068 * @throws IOException if the JSON is invalid
69 */
sangho1e575652015-05-14 00:39:53 -070070 @POST
71 @Consumes(MediaType.APPLICATION_JSON)
72 public Response createTunnel(InputStream input) throws IOException {
73 ObjectMapper mapper = new ObjectMapper();
Ray Milkey86ee5e82018-04-02 15:33:07 -070074 ObjectNode tunnelJson = readTreeFromStream(mapper, input);
sangho1e575652015-05-14 00:39:53 -070075 SegmentRoutingService srService = get(SegmentRoutingService.class);
76 Tunnel tunnelInfo = TUNNEL_CODEC.decode(tunnelJson, this);
sangho0b2b6d12015-05-20 22:16:38 -070077 srService.createTunnel(tunnelInfo);
sangho1e575652015-05-14 00:39:53 -070078
79 return Response.ok().build();
80 }
81
Ray Milkeyf2ab6f32015-08-20 14:25:51 -070082 /**
83 * Delete a segment routing tunnel.
84 *
85 * @param input JSON stream for tunnel to delete
Jian Lic2a542b2016-05-10 11:48:19 -070086 * @return 204 NO CONTENT, if the tunnel is removed
Ray Milkeyf2ab6f32015-08-20 14:25:51 -070087 * @throws IOException if JSON is invalid
88 */
sangho1e575652015-05-14 00:39:53 -070089 @DELETE
90 @Consumes(MediaType.APPLICATION_JSON)
91 public Response removeTunnel(InputStream input) throws IOException {
92 ObjectMapper mapper = new ObjectMapper();
Ray Milkey86ee5e82018-04-02 15:33:07 -070093 ObjectNode tunnelJson = readTreeFromStream(mapper, input);
sangho1e575652015-05-14 00:39:53 -070094 SegmentRoutingService srService = get(SegmentRoutingService.class);
95 Tunnel tunnelInfo = TUNNEL_CODEC.decode(tunnelJson, this);
96 srService.removeTunnel(tunnelInfo);
97
Jian Lic2a542b2016-05-10 11:48:19 -070098 return Response.noContent().build();
sangho1e575652015-05-14 00:39:53 -070099 }
100
101}