blob: eb17d792afbffc38cfc3611d653a1dad873c5d2e [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;
Jonathan Hart8338b8d2017-08-15 15:46:14 -070042
43/**
44 * Manage the unicast routing information.
45 */
46@Path("routes")
47public class RouteServiceWebResource extends AbstractWebResource {
48
Andrea Campanella40bccdb2018-03-09 17:53:25 -080049 protected static final String ROUTES = "routes";
50 protected static final String ROUTES_KEY_ERROR = "Routes key must be present";
51
Jonathan Hart8338b8d2017-08-15 15:46:14 -070052 /**
53 * Get all unicast routes.
54 * Returns array of all known unicast routes.
55 *
56 * @return 200 OK with array of all known unicast routes
57 * @onos.rsModel RoutesGet
58 */
59 @GET
60 @Produces(MediaType.APPLICATION_JSON)
61 public Response getRoutes() {
62 RouteService service = get(RouteService.class);
63 ObjectNode root = mapper().createObjectNode();
64 service.getRouteTables().forEach(table -> {
65 List<Route> routes = service.getRoutes(table).stream()
66 .flatMap(ri -> ri.allRoutes().stream())
67 .map(ResolvedRoute::route)
68 .collect(Collectors.toList());
69 root.put(table.name(), codec(Route.class).encode(routes, this));
70 });
71 return ok(root).build();
72 }
73
74 /**
75 * Create new unicast route.
76 * Creates a new route in the unicast RIB. Routes created through the REST
77 * API are always created as STATIC routes, so there is no need to specify
78 * the type.
79 *
Jonathan Hart8338b8d2017-08-15 15:46:14 -070080 * @param route unicast route JSON
81 * @return status of the request - CREATED if the JSON is correct,
82 * BAD_REQUEST if the JSON is invalid, NO_CONTENT otherwise
Andrea Campanella40bccdb2018-03-09 17:53:25 -080083 * @onos.rsModel RoutePost
Jonathan Hart8338b8d2017-08-15 15:46:14 -070084 */
85 @POST
86 @Consumes(MediaType.APPLICATION_JSON)
87 @Produces(MediaType.APPLICATION_JSON)
88 public Response createRoute(InputStream route) {
89 RouteAdminService service = get(RouteAdminService.class);
90 try {
91 ObjectNode jsonTree = (ObjectNode) mapper().readTree(route);
92 Route r = codec(Route.class).decode(jsonTree, this);
93 service.update(Collections.singletonList(r));
94 } catch (IOException ex) {
95 throw new IllegalArgumentException(ex);
96 }
97
98 return Response
99 .noContent()
100 .build();
101 }
102
103 /**
Andrea Campanella40bccdb2018-03-09 17:53:25 -0800104 * Creates new unicast routes.
105 * Creates new routes in the unicast RIB. Routes created through the REST
106 * API are always created as STATIC routes, so there is no need to specify
107 * the type.
108 *
109 * @param routesStream unicast routes JSON array
110 * @return status of the request - CREATED if the JSON is correct,
111 * BAD_REQUEST if the JSON is invalid, NO_CONTENT otherwise
112 * @onos.rsModel RoutesPost
113 */
114 @POST
115 @Consumes(MediaType.APPLICATION_JSON)
116 @Produces(MediaType.APPLICATION_JSON)
117 @Path("/bulk")
118 public Response createRoutes(InputStream routesStream) {
119 RouteAdminService service = get(RouteAdminService.class);
120 try {
121 ObjectNode jsonTree = (ObjectNode) mapper().readTree(routesStream);
122 ArrayNode routesArray = nullIsIllegal((ArrayNode) jsonTree.get(ROUTES),
123 ROUTES_KEY_ERROR);
124 List<Route> routes = codec(Route.class).decode(routesArray, this);
125 service.update(routes);
126
127 } catch (IOException ex) {
128 throw new IllegalArgumentException(ex);
129 }
130
131 return Response
132 .noContent()
133 .build();
134 }
135
136 /**
Jonathan Hart8338b8d2017-08-15 15:46:14 -0700137 * Remove a unicast route.
138 * Removes a route from the unicast RIB.
139 *
140 * @param route unicast route JSON
141 * @return 204 NO CONTENT
142 * @onos.rsModel RoutePost
143 */
144 @DELETE
145 @Consumes(MediaType.APPLICATION_JSON)
146 public Response deleteRoute(InputStream route) {
147 RouteAdminService service = get(RouteAdminService.class);
148 try {
149 ObjectNode jsonTree = (ObjectNode) mapper().readTree(route);
150 Route r = codec(Route.class).decode(jsonTree, this);
151 service.withdraw(Collections.singletonList(r));
152 } catch (IOException ex) {
153 throw new IllegalArgumentException(ex);
154 }
155 return Response.noContent().build();
156 }
Andrea Campanella40bccdb2018-03-09 17:53:25 -0800157
158 /**
159 * Removes unicast routes.
160 * Removes multiple routes from the unicast RIB.
161 *
162 * @param routesStream unicast routes array JSON
163 * @return 204 NO CONTENT
164 */
165 @DELETE
166 @Consumes(MediaType.APPLICATION_JSON)
167 @Path("/bulk")
168 public Response deleteRoutes(InputStream routesStream) {
169 RouteAdminService service = get(RouteAdminService.class);
170 try {
171 ObjectNode jsonTree = (ObjectNode) mapper().readTree(routesStream);
172 ArrayNode routesArray = nullIsIllegal((ArrayNode) jsonTree.get(ROUTES),
173 ROUTES_KEY_ERROR);
174 List<Route> routes = codec(Route.class).decode(routesArray, this);
175 service.withdraw(routes);
176
177 } catch (IOException ex) {
178 throw new IllegalArgumentException(ex);
179 }
180
181 return Response
182 .noContent()
183 .build();
184 }
Jonathan Hart8338b8d2017-08-15 15:46:14 -0700185}