blob: 2495f18f5dcfdefa872cfddf9a5a1d945583f998 [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;
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 Milkeyb784adb2018-04-02 15:33:07 -070037import static org.onlab.util.Tools.readTreeFromStream;
38
Ray Milkeyf2ab6f32015-08-20 14:25:51 -070039/**
40 * Query, create and remove segment routing tunnels.
41 */
sangho1e575652015-05-14 00:39:53 -070042@Path("tunnel")
43public class TunnelWebResource extends AbstractWebResource {
44
45 private static final TunnelCodec TUNNEL_CODEC = new TunnelCodec();
46
Ray Milkeyf2ab6f32015-08-20 14:25:51 -070047 /**
48 * Get all segment routing tunnels.
49 * Returns an array of segment routing tunnels.
50 *
51 * @return status of OK
52 */
sangho1e575652015-05-14 00:39:53 -070053 @GET
54 @Produces(MediaType.APPLICATION_JSON)
55 public Response getTunnel() {
56 SegmentRoutingService srService = get(SegmentRoutingService.class);
57 List<Tunnel> tunnels = srService.getTunnels();
58 ObjectNode result = new ObjectMapper().createObjectNode();
59 result.set("tunnel", new TunnelCodec().encode(tunnels, this));
60
61 return ok(result.toString()).build();
62 }
63
Ray Milkeyf2ab6f32015-08-20 14:25:51 -070064 /**
65 * Create a new segment routing tunnel.
66 *
67 * @param input JSON stream for tunnel to create
68 * @return status of the request - OK if the tunnel is created,
Ray Milkeyf2ab6f32015-08-20 14:25:51 -070069 * @throws IOException if the JSON is invalid
70 */
sangho1e575652015-05-14 00:39:53 -070071 @POST
72 @Consumes(MediaType.APPLICATION_JSON)
73 public Response createTunnel(InputStream input) throws IOException {
74 ObjectMapper mapper = new ObjectMapper();
Ray Milkeyb784adb2018-04-02 15:33:07 -070075 ObjectNode tunnelJson = readTreeFromStream(mapper, input);
sangho1e575652015-05-14 00:39:53 -070076 SegmentRoutingService srService = get(SegmentRoutingService.class);
77 Tunnel tunnelInfo = TUNNEL_CODEC.decode(tunnelJson, this);
sangho0b2b6d12015-05-20 22:16:38 -070078 srService.createTunnel(tunnelInfo);
sangho1e575652015-05-14 00:39:53 -070079
80 return Response.ok().build();
81 }
82
Ray Milkeyf2ab6f32015-08-20 14:25:51 -070083 /**
84 * Delete a segment routing tunnel.
85 *
86 * @param input JSON stream for tunnel to delete
Jian Lic2a542b2016-05-10 11:48:19 -070087 * @return 204 NO CONTENT, if the tunnel is removed
Ray Milkeyf2ab6f32015-08-20 14:25:51 -070088 * @throws IOException if JSON is invalid
89 */
sangho1e575652015-05-14 00:39:53 -070090 @DELETE
91 @Consumes(MediaType.APPLICATION_JSON)
92 public Response removeTunnel(InputStream input) throws IOException {
93 ObjectMapper mapper = new ObjectMapper();
Ray Milkeyb784adb2018-04-02 15:33:07 -070094 ObjectNode tunnelJson = readTreeFromStream(mapper, input);
sangho1e575652015-05-14 00:39:53 -070095 SegmentRoutingService srService = get(SegmentRoutingService.class);
96 Tunnel tunnelInfo = TUNNEL_CODEC.decode(tunnelJson, this);
97 srService.removeTunnel(tunnelInfo);
98
Jian Lic2a542b2016-05-10 11:48:19 -070099 return Response.noContent().build();
sangho1e575652015-05-14 00:39:53 -0700100 }
101
102}