Jonathan Hart | 0cf8c18 | 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.rest.resources; |
| 18 | |
| 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 20 | import org.onosproject.incubator.net.routing.ResolvedRoute; |
| 21 | import org.onosproject.incubator.net.routing.Route; |
| 22 | import org.onosproject.incubator.net.routing.RouteAdminService; |
| 23 | import org.onosproject.incubator.net.routing.RouteService; |
| 24 | import org.onosproject.rest.AbstractWebResource; |
| 25 | |
| 26 | import javax.ws.rs.Consumes; |
| 27 | import javax.ws.rs.DELETE; |
| 28 | import javax.ws.rs.GET; |
| 29 | import javax.ws.rs.POST; |
| 30 | import javax.ws.rs.Path; |
| 31 | import javax.ws.rs.Produces; |
| 32 | import javax.ws.rs.core.MediaType; |
| 33 | import javax.ws.rs.core.Response; |
| 34 | import java.io.IOException; |
| 35 | import java.io.InputStream; |
| 36 | import java.net.URI; |
| 37 | import java.util.Collections; |
| 38 | import java.util.List; |
| 39 | import java.util.stream.Collectors; |
| 40 | |
| 41 | /** |
| 42 | * Manage the unicast routing information. |
| 43 | */ |
| 44 | @Path("route") |
| 45 | public class RouteWebResource extends AbstractWebResource { |
| 46 | |
| 47 | /** |
| 48 | * Get all unicast routes. |
| 49 | * Returns array of all known unicast routes. |
| 50 | * |
| 51 | * @return 200 OK with array of all known unicast routes |
| 52 | * @onos.rsModel RoutesGet |
| 53 | */ |
| 54 | @GET |
| 55 | @Produces(MediaType.APPLICATION_JSON) |
| 56 | public Response getRoutes() { |
| 57 | RouteService service = get(RouteService.class); |
| 58 | ObjectNode root = mapper().createObjectNode(); |
| 59 | service.getRouteTables().forEach(table -> { |
| 60 | List<Route> routes = service.getRoutes(table).stream() |
| 61 | .flatMap(ri -> ri.allRoutes().stream()) |
| 62 | .map(ResolvedRoute::route) |
| 63 | .collect(Collectors.toList()); |
| 64 | root.put(table.name(), codec(Route.class).encode(routes, this)); |
| 65 | }); |
| 66 | return ok(root).build(); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Create new unicast route. |
| 71 | * Creates a new route in the unicast RIB. Routes created through the REST |
| 72 | * API are always created as STATIC routes, so there is no need to specify |
| 73 | * the type. |
| 74 | * |
| 75 | * @onos.rsModel RoutePost |
| 76 | * @param route unicast route JSON |
| 77 | * @return status of the request - CREATED if the JSON is correct, |
| 78 | * BAD_REQUEST if the JSON is invalid |
| 79 | */ |
| 80 | @POST |
| 81 | @Consumes(MediaType.APPLICATION_JSON) |
| 82 | @Produces(MediaType.APPLICATION_JSON) |
| 83 | public Response createRoute(InputStream route) { |
| 84 | RouteAdminService service = get(RouteAdminService.class); |
| 85 | try { |
| 86 | ObjectNode jsonTree = (ObjectNode) mapper().readTree(route); |
| 87 | Route r = codec(Route.class).decode(jsonTree, this); |
| 88 | service.update(Collections.singletonList(r)); |
| 89 | } catch (IOException ex) { |
| 90 | throw new IllegalArgumentException(ex); |
| 91 | } |
| 92 | |
| 93 | return Response |
| 94 | .created(URI.create("")) |
| 95 | .build(); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Remove a unicast route. |
| 100 | * Removes a route from the unicast RIB. |
| 101 | * |
| 102 | * @param route unicast route JSON |
| 103 | * @return 204 NO CONTENT |
| 104 | * @onos.rsModel RoutePost |
| 105 | */ |
| 106 | @DELETE |
| 107 | @Consumes(MediaType.APPLICATION_JSON) |
| 108 | public Response deleteRoute(InputStream route) { |
| 109 | RouteAdminService service = get(RouteAdminService.class); |
| 110 | try { |
| 111 | ObjectNode jsonTree = (ObjectNode) mapper().readTree(route); |
| 112 | Route r = codec(Route.class).decode(jsonTree, this); |
| 113 | service.withdraw(Collections.singletonList(r)); |
| 114 | } catch (IOException ex) { |
| 115 | throw new IllegalArgumentException(ex); |
| 116 | } |
| 117 | return Response.noContent().build(); |
| 118 | } |
| 119 | } |