blob: fc1a09ab3ecc8a62ddc63482b71bf0b9c321d719 [file] [log] [blame]
Jonathan Hart8338b8d2017-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
Andrea Campanella40bccdb2018-03-09 17:53:25 -080019import com.fasterxml.jackson.databind.node.ArrayNode;
20import com.fasterxml.jackson.databind.node.ObjectNode;
21import org.onosproject.rest.AbstractWebResource;
22import org.onosproject.routeservice.ResolvedRoute;
23import org.onosproject.routeservice.Route;
24import org.onosproject.routeservice.RouteAdminService;
25import org.onosproject.routeservice.RouteService;
Jonathan Hart8338b8d2017-08-15 15:46:14 -070026
27import javax.ws.rs.Consumes;
28import javax.ws.rs.DELETE;
29import javax.ws.rs.GET;
30import javax.ws.rs.POST;
31import javax.ws.rs.Path;
32import javax.ws.rs.Produces;
33import javax.ws.rs.core.MediaType;
34import javax.ws.rs.core.Response;
Andrea Campanella40bccdb2018-03-09 17:53:25 -080035import java.io.IOException;
36import java.io.InputStream;
37import java.util.Collections;
38import java.util.List;
39import java.util.stream.Collectors;
Jonathan Hart8338b8d2017-08-15 15:46:14 -070040
Andrea Campanella40bccdb2018-03-09 17:53:25 -080041import static org.onlab.util.Tools.nullIsIllegal;
Ray Milkey86ee5e82018-04-02 15:33:07 -070042import static org.onlab.util.Tools.readTreeFromStream;
Jonathan Hart8338b8d2017-08-15 15:46:14 -070043
44/**
45 * Manage the unicast routing information.
46 */
47@Path("routes")
48public class RouteServiceWebResource extends AbstractWebResource {
49
Andrea Campanella40bccdb2018-03-09 17:53:25 -080050 protected static final String ROUTES = "routes";
51 protected static final String ROUTES_KEY_ERROR = "Routes key must be present";
52
Jonathan Hart8338b8d2017-08-15 15:46:14 -070053 /**
54 * Get all unicast routes.
55 * Returns array of all known unicast routes.
56 *
57 * @return 200 OK with array of all known unicast routes
58 * @onos.rsModel RoutesGet
59 */
60 @GET
61 @Produces(MediaType.APPLICATION_JSON)
62 public Response getRoutes() {
63 RouteService service = get(RouteService.class);
64 ObjectNode root = mapper().createObjectNode();
65 service.getRouteTables().forEach(table -> {
66 List<Route> routes = service.getRoutes(table).stream()
67 .flatMap(ri -> ri.allRoutes().stream())
68 .map(ResolvedRoute::route)
69 .collect(Collectors.toList());
70 root.put(table.name(), codec(Route.class).encode(routes, this));
71 });
72 return ok(root).build();
73 }
74
75 /**
76 * Create new unicast route.
77 * Creates a new route in the unicast RIB. Routes created through the REST
78 * API are always created as STATIC routes, so there is no need to specify
79 * the type.
80 *
Jonathan Hart8338b8d2017-08-15 15:46:14 -070081 * @param route unicast route JSON
82 * @return status of the request - CREATED if the JSON is correct,
83 * BAD_REQUEST if the JSON is invalid, NO_CONTENT otherwise
Andrea Campanella40bccdb2018-03-09 17:53:25 -080084 * @onos.rsModel RoutePost
Jonathan Hart8338b8d2017-08-15 15:46:14 -070085 */
86 @POST
87 @Consumes(MediaType.APPLICATION_JSON)
88 @Produces(MediaType.APPLICATION_JSON)
89 public Response createRoute(InputStream route) {
90 RouteAdminService service = get(RouteAdminService.class);
91 try {
Ray Milkey86ee5e82018-04-02 15:33:07 -070092 ObjectNode jsonTree = readTreeFromStream(mapper(), route);
Jonathan Hart8338b8d2017-08-15 15:46:14 -070093 Route r = codec(Route.class).decode(jsonTree, this);
94 service.update(Collections.singletonList(r));
95 } catch (IOException ex) {
96 throw new IllegalArgumentException(ex);
97 }
98
99 return Response
100 .noContent()
101 .build();
102 }
103
104 /**
Andrea Campanella40bccdb2018-03-09 17:53:25 -0800105 * Creates new unicast routes.
106 * Creates new routes in the unicast RIB. Routes created through the REST
107 * API are always created as STATIC routes, so there is no need to specify
108 * the type.
109 *
110 * @param routesStream unicast routes JSON array
111 * @return status of the request - CREATED if the JSON is correct,
112 * BAD_REQUEST if the JSON is invalid, NO_CONTENT otherwise
113 * @onos.rsModel RoutesPost
114 */
115 @POST
116 @Consumes(MediaType.APPLICATION_JSON)
117 @Produces(MediaType.APPLICATION_JSON)
118 @Path("/bulk")
119 public Response createRoutes(InputStream routesStream) {
120 RouteAdminService service = get(RouteAdminService.class);
121 try {
Ray Milkey86ee5e82018-04-02 15:33:07 -0700122 ObjectNode jsonTree = readTreeFromStream(mapper(), routesStream);
Andrea Campanella40bccdb2018-03-09 17:53:25 -0800123 ArrayNode routesArray = nullIsIllegal((ArrayNode) jsonTree.get(ROUTES),
124 ROUTES_KEY_ERROR);
125 List<Route> routes = codec(Route.class).decode(routesArray, this);
126 service.update(routes);
127
128 } catch (IOException ex) {
129 throw new IllegalArgumentException(ex);
130 }
131
132 return Response
133 .noContent()
134 .build();
135 }
136
137 /**
Jonathan Hart8338b8d2017-08-15 15:46:14 -0700138 * Remove a unicast route.
139 * Removes a route from the unicast RIB.
140 *
141 * @param route unicast route JSON
142 * @return 204 NO CONTENT
143 * @onos.rsModel RoutePost
144 */
145 @DELETE
146 @Consumes(MediaType.APPLICATION_JSON)
147 public Response deleteRoute(InputStream route) {
148 RouteAdminService service = get(RouteAdminService.class);
149 try {
Ray Milkey86ee5e82018-04-02 15:33:07 -0700150 ObjectNode jsonTree = readTreeFromStream(mapper(), route);
Jonathan Hart8338b8d2017-08-15 15:46:14 -0700151 Route r = codec(Route.class).decode(jsonTree, this);
152 service.withdraw(Collections.singletonList(r));
153 } catch (IOException ex) {
154 throw new IllegalArgumentException(ex);
155 }
156 return Response.noContent().build();
157 }
Andrea Campanella40bccdb2018-03-09 17:53:25 -0800158
159 /**
160 * Removes unicast routes.
161 * Removes multiple routes from the unicast RIB.
162 *
163 * @param routesStream unicast routes array JSON
164 * @return 204 NO CONTENT
165 */
166 @DELETE
167 @Consumes(MediaType.APPLICATION_JSON)
168 @Path("/bulk")
169 public Response deleteRoutes(InputStream routesStream) {
170 RouteAdminService service = get(RouteAdminService.class);
171 try {
Ray Milkey86ee5e82018-04-02 15:33:07 -0700172 ObjectNode jsonTree = readTreeFromStream(mapper(), routesStream);
Andrea Campanella40bccdb2018-03-09 17:53:25 -0800173 ArrayNode routesArray = nullIsIllegal((ArrayNode) jsonTree.get(ROUTES),
174 ROUTES_KEY_ERROR);
175 List<Route> routes = codec(Route.class).decode(routesArray, this);
176 service.withdraw(routes);
177
178 } catch (IOException ex) {
179 throw new IllegalArgumentException(ex);
180 }
181
182 return Response
183 .noContent()
184 .build();
185 }
Jonathan Hart8338b8d2017-08-15 15:46:14 -0700186}