Jonathan Hart | 8338b8d | 2017-08-15 15:46:14 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.routeservice.rest; |
| 18 | |
Andrea Campanella | 40bccdb | 2018-03-09 17:53:25 -0800 | [diff] [blame] | 19 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 21 | import org.onosproject.rest.AbstractWebResource; |
| 22 | import org.onosproject.routeservice.ResolvedRoute; |
| 23 | import org.onosproject.routeservice.Route; |
| 24 | import org.onosproject.routeservice.RouteAdminService; |
| 25 | import org.onosproject.routeservice.RouteService; |
Jonathan Hart | 8338b8d | 2017-08-15 15:46:14 -0700 | [diff] [blame] | 26 | |
| 27 | import javax.ws.rs.Consumes; |
| 28 | import javax.ws.rs.DELETE; |
| 29 | import javax.ws.rs.GET; |
| 30 | import javax.ws.rs.POST; |
| 31 | import javax.ws.rs.Path; |
| 32 | import javax.ws.rs.Produces; |
| 33 | import javax.ws.rs.core.MediaType; |
| 34 | import javax.ws.rs.core.Response; |
Andrea Campanella | 40bccdb | 2018-03-09 17:53:25 -0800 | [diff] [blame] | 35 | import java.io.IOException; |
| 36 | import java.io.InputStream; |
| 37 | import java.util.Collections; |
| 38 | import java.util.List; |
| 39 | import java.util.stream.Collectors; |
Jonathan Hart | 8338b8d | 2017-08-15 15:46:14 -0700 | [diff] [blame] | 40 | |
Andrea Campanella | 40bccdb | 2018-03-09 17:53:25 -0800 | [diff] [blame] | 41 | import static org.onlab.util.Tools.nullIsIllegal; |
Ray Milkey | 86ee5e8 | 2018-04-02 15:33:07 -0700 | [diff] [blame] | 42 | import static org.onlab.util.Tools.readTreeFromStream; |
Jonathan Hart | 8338b8d | 2017-08-15 15:46:14 -0700 | [diff] [blame] | 43 | |
| 44 | /** |
| 45 | * Manage the unicast routing information. |
| 46 | */ |
| 47 | @Path("routes") |
| 48 | public class RouteServiceWebResource extends AbstractWebResource { |
| 49 | |
Andrea Campanella | 40bccdb | 2018-03-09 17:53:25 -0800 | [diff] [blame] | 50 | protected static final String ROUTES = "routes"; |
| 51 | protected static final String ROUTES_KEY_ERROR = "Routes key must be present"; |
| 52 | |
Jonathan Hart | 8338b8d | 2017-08-15 15:46:14 -0700 | [diff] [blame] | 53 | /** |
| 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 Hart | 8338b8d | 2017-08-15 15:46:14 -0700 | [diff] [blame] | 81 | * @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 Campanella | 40bccdb | 2018-03-09 17:53:25 -0800 | [diff] [blame] | 84 | * @onos.rsModel RoutePost |
Jonathan Hart | 8338b8d | 2017-08-15 15:46:14 -0700 | [diff] [blame] | 85 | */ |
| 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 Milkey | 86ee5e8 | 2018-04-02 15:33:07 -0700 | [diff] [blame] | 92 | ObjectNode jsonTree = readTreeFromStream(mapper(), route); |
Jonathan Hart | 8338b8d | 2017-08-15 15:46:14 -0700 | [diff] [blame] | 93 | 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 Campanella | 40bccdb | 2018-03-09 17:53:25 -0800 | [diff] [blame] | 105 | * 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 Milkey | 86ee5e8 | 2018-04-02 15:33:07 -0700 | [diff] [blame] | 122 | ObjectNode jsonTree = readTreeFromStream(mapper(), routesStream); |
Andrea Campanella | 40bccdb | 2018-03-09 17:53:25 -0800 | [diff] [blame] | 123 | 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 Hart | 8338b8d | 2017-08-15 15:46:14 -0700 | [diff] [blame] | 138 | * 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 Milkey | 86ee5e8 | 2018-04-02 15:33:07 -0700 | [diff] [blame] | 150 | ObjectNode jsonTree = readTreeFromStream(mapper(), route); |
Jonathan Hart | 8338b8d | 2017-08-15 15:46:14 -0700 | [diff] [blame] | 151 | 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 Campanella | 40bccdb | 2018-03-09 17:53:25 -0800 | [diff] [blame] | 158 | |
| 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 Milkey | 86ee5e8 | 2018-04-02 15:33:07 -0700 | [diff] [blame] | 172 | ObjectNode jsonTree = readTreeFromStream(mapper(), routesStream); |
Andrea Campanella | 40bccdb | 2018-03-09 17:53:25 -0800 | [diff] [blame] | 173 | 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 Hart | 8338b8d | 2017-08-15 15:46:14 -0700 | [diff] [blame] | 186 | } |