blob: 83d9ef0f39f7dbc116536620198824871872e6aa [file] [log] [blame]
Jonathan Hart0cf8c182017-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.rest.resources;
18
19import com.fasterxml.jackson.databind.node.ObjectNode;
20import org.onosproject.incubator.net.routing.ResolvedRoute;
21import org.onosproject.incubator.net.routing.Route;
22import org.onosproject.incubator.net.routing.RouteAdminService;
23import org.onosproject.incubator.net.routing.RouteService;
24import org.onosproject.rest.AbstractWebResource;
25
26import javax.ws.rs.Consumes;
27import javax.ws.rs.DELETE;
28import javax.ws.rs.GET;
29import javax.ws.rs.POST;
30import javax.ws.rs.Path;
31import javax.ws.rs.Produces;
32import javax.ws.rs.core.MediaType;
33import javax.ws.rs.core.Response;
34import java.io.IOException;
35import java.io.InputStream;
36import java.net.URI;
37import java.util.Collections;
38import java.util.List;
39import java.util.stream.Collectors;
40
41/**
42 * Manage the unicast routing information.
43 */
Jonathan Hart878868a2017-08-16 16:46:36 -070044@Path("routes")
45public class RoutesWebResource extends AbstractWebResource {
Jonathan Hart0cf8c182017-08-15 15:46:14 -070046
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}