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