blob: 476bcd8d4b3de661d2314e1ac5d161a26818b72c [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 Milkeyf2ab6f32015-08-20 14:25:51 -070036/**
37 * Query, create and remove segment routing tunnels.
38 */
Andreas Pantelopouloscd339592018-02-23 14:18:00 -080039// @Path("tunnel")
sangho1e575652015-05-14 00:39:53 -070040public class TunnelWebResource extends AbstractWebResource {
41
42 private static final TunnelCodec TUNNEL_CODEC = new TunnelCodec();
43
Ray Milkeyf2ab6f32015-08-20 14:25:51 -070044 /**
45 * Get all segment routing tunnels.
46 * Returns an array of segment routing tunnels.
47 *
48 * @return status of OK
49 */
sangho1e575652015-05-14 00:39:53 -070050 @GET
51 @Produces(MediaType.APPLICATION_JSON)
52 public Response getTunnel() {
53 SegmentRoutingService srService = get(SegmentRoutingService.class);
54 List<Tunnel> tunnels = srService.getTunnels();
55 ObjectNode result = new ObjectMapper().createObjectNode();
56 result.set("tunnel", new TunnelCodec().encode(tunnels, this));
57
58 return ok(result.toString()).build();
59 }
60
Ray Milkeyf2ab6f32015-08-20 14:25:51 -070061 /**
62 * Create a new segment routing tunnel.
63 *
64 * @param input JSON stream for tunnel to create
65 * @return status of the request - OK if the tunnel is created,
Ray Milkeyf2ab6f32015-08-20 14:25:51 -070066 * @throws IOException if the JSON is invalid
67 */
sangho1e575652015-05-14 00:39:53 -070068 @POST
69 @Consumes(MediaType.APPLICATION_JSON)
70 public Response createTunnel(InputStream input) throws IOException {
71 ObjectMapper mapper = new ObjectMapper();
72 ObjectNode tunnelJson = (ObjectNode) mapper.readTree(input);
73 SegmentRoutingService srService = get(SegmentRoutingService.class);
74 Tunnel tunnelInfo = TUNNEL_CODEC.decode(tunnelJson, this);
sangho0b2b6d12015-05-20 22:16:38 -070075 srService.createTunnel(tunnelInfo);
sangho1e575652015-05-14 00:39:53 -070076
77 return Response.ok().build();
78 }
79
Ray Milkeyf2ab6f32015-08-20 14:25:51 -070080 /**
81 * Delete a segment routing tunnel.
82 *
83 * @param input JSON stream for tunnel to delete
Jian Lic2a542b2016-05-10 11:48:19 -070084 * @return 204 NO CONTENT, if the tunnel is removed
Ray Milkeyf2ab6f32015-08-20 14:25:51 -070085 * @throws IOException if JSON is invalid
86 */
sangho1e575652015-05-14 00:39:53 -070087 @DELETE
88 @Consumes(MediaType.APPLICATION_JSON)
89 public Response removeTunnel(InputStream input) throws IOException {
90 ObjectMapper mapper = new ObjectMapper();
91 ObjectNode tunnelJson = (ObjectNode) mapper.readTree(input);
92 SegmentRoutingService srService = get(SegmentRoutingService.class);
93 Tunnel tunnelInfo = TUNNEL_CODEC.decode(tunnelJson, this);
94 srService.removeTunnel(tunnelInfo);
95
Jian Lic2a542b2016-05-10 11:48:19 -070096 return Response.noContent().build();
sangho1e575652015-05-14 00:39:53 -070097 }
98
99}