blob: eec7391b23447563590dc0968a15946f3e326928 [file] [log] [blame]
Jonathan Hartbcd8c032017-08-15 15:46:14 -07001/*
2 * Copyright 2017-present Open Networking Foundation
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 */
16
17package org.onosproject.routeservice.rest;
18
19import java.io.IOException;
20import java.io.InputStream;
21import java.util.Collections;
22import java.util.List;
23import java.util.stream.Collectors;
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;
33
34import org.onosproject.rest.AbstractWebResource;
35import org.onosproject.routeservice.ResolvedRoute;
36import org.onosproject.routeservice.Route;
37import org.onosproject.routeservice.RouteAdminService;
38import org.onosproject.routeservice.RouteService;
39
40import com.fasterxml.jackson.databind.node.ObjectNode;
41
Ray Milkeyb784adb2018-04-02 15:33:07 -070042import static org.onlab.util.Tools.readTreeFromStream;
43
Jonathan Hartbcd8c032017-08-15 15:46:14 -070044/**
45 * Manage the unicast routing information.
46 */
47@Path("routes")
48public class RouteServiceWebResource extends AbstractWebResource {
49
50 /**
51 * Get all unicast routes.
52 * Returns array of all known unicast routes.
53 *
54 * @return 200 OK with array of all known unicast routes
55 * @onos.rsModel RoutesGet
56 */
57 @GET
58 @Produces(MediaType.APPLICATION_JSON)
59 public Response getRoutes() {
60 RouteService service = get(RouteService.class);
61 ObjectNode root = mapper().createObjectNode();
62 service.getRouteTables().forEach(table -> {
63 List<Route> routes = service.getRoutes(table).stream()
64 .flatMap(ri -> ri.allRoutes().stream())
65 .map(ResolvedRoute::route)
66 .collect(Collectors.toList());
67 root.put(table.name(), codec(Route.class).encode(routes, this));
68 });
69 return ok(root).build();
70 }
71
72 /**
73 * Create new unicast route.
74 * Creates a new route in the unicast RIB. Routes created through the REST
75 * API are always created as STATIC routes, so there is no need to specify
76 * the type.
77 *
78 * @onos.rsModel RoutePost
79 * @param route unicast route JSON
80 * @return status of the request - CREATED if the JSON is correct,
81 * BAD_REQUEST if the JSON is invalid, NO_CONTENT otherwise
82 */
83 @POST
84 @Consumes(MediaType.APPLICATION_JSON)
85 @Produces(MediaType.APPLICATION_JSON)
86 public Response createRoute(InputStream route) {
87 RouteAdminService service = get(RouteAdminService.class);
88 try {
Ray Milkeyb784adb2018-04-02 15:33:07 -070089 ObjectNode jsonTree = readTreeFromStream(mapper(), route);
Jonathan Hartbcd8c032017-08-15 15:46:14 -070090 Route r = codec(Route.class).decode(jsonTree, this);
91 service.update(Collections.singletonList(r));
92 } catch (IOException ex) {
93 throw new IllegalArgumentException(ex);
94 }
95
96 return Response
97 .noContent()
98 .build();
99 }
100
101 /**
102 * Remove a unicast route.
103 * Removes a route from the unicast RIB.
104 *
105 * @param route unicast route JSON
106 * @return 204 NO CONTENT
107 * @onos.rsModel RoutePost
108 */
109 @DELETE
110 @Consumes(MediaType.APPLICATION_JSON)
111 public Response deleteRoute(InputStream route) {
112 RouteAdminService service = get(RouteAdminService.class);
113 try {
Ray Milkeyb784adb2018-04-02 15:33:07 -0700114 ObjectNode jsonTree = readTreeFromStream(mapper(), route);
Jonathan Hartbcd8c032017-08-15 15:46:14 -0700115 Route r = codec(Route.class).decode(jsonTree, this);
116 service.withdraw(Collections.singletonList(r));
117 } catch (IOException ex) {
118 throw new IllegalArgumentException(ex);
119 }
120 return Response.noContent().build();
121 }
122}