blob: a1c02962e31ae177b352fafbb411ada994b6844c [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;
Ray Milkey86ee5e82018-04-02 15:33:07 -070042import static org.onlab.util.Tools.readTreeFromStream;
Jonathan Hart8338b8d2017-08-15 15:46:14 -070043
44/**
45 * Manage the unicast routing information.
46 */
47@Path("routes")
48public class RouteServiceWebResource extends AbstractWebResource {
49
Andrea Campanella40bccdb2018-03-09 17:53:25 -080050 protected static final String ROUTES = "routes";
51 protected static final String ROUTES_KEY_ERROR = "Routes key must be present";
52
Jonathan Hart8338b8d2017-08-15 15:46:14 -070053 /**
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.
psneha36b60f92018-08-29 08:56:08 -040077 * Creates a new route in the unicast RIB. Source field is kept optional.
78 * Without Source field routes are created as STATIC routes. Otherwise as per the mentioned Source
Jonathan Hart8338b8d2017-08-15 15:46:14 -070079 *
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
psneha36b60f92018-08-29 08:56:08 -040083 * @onos.rsModel RouteTypePost
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 {
Ray Milkey86ee5e82018-04-02 15:33:07 -070091 ObjectNode jsonTree = readTreeFromStream(mapper(), route);
Jonathan Hart8338b8d2017-08-15 15:46:14 -070092 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.
psneha36b60f92018-08-29 08:56:08 -0400105 * Creates a new route in the unicast RIB. Source field is kept optional.
106 * Without Source field routes are created as STATIC routes. Otherwise as per the mentioned Source
Andrea Campanella40bccdb2018-03-09 17:53:25 -0800107 *
108 * @param routesStream unicast routes JSON array
109 * @return status of the request - CREATED if the JSON is correct,
110 * BAD_REQUEST if the JSON is invalid, NO_CONTENT otherwise
psneha36b60f92018-08-29 08:56:08 -0400111 * @onos.rsModel RoutesTypePost
Andrea Campanella40bccdb2018-03-09 17:53:25 -0800112 */
113 @POST
114 @Consumes(MediaType.APPLICATION_JSON)
115 @Produces(MediaType.APPLICATION_JSON)
116 @Path("/bulk")
117 public Response createRoutes(InputStream routesStream) {
118 RouteAdminService service = get(RouteAdminService.class);
119 try {
Ray Milkey86ee5e82018-04-02 15:33:07 -0700120 ObjectNode jsonTree = readTreeFromStream(mapper(), routesStream);
Andrea Campanella40bccdb2018-03-09 17:53:25 -0800121 ArrayNode routesArray = nullIsIllegal((ArrayNode) jsonTree.get(ROUTES),
122 ROUTES_KEY_ERROR);
123 List<Route> routes = codec(Route.class).decode(routesArray, this);
124 service.update(routes);
125
126 } catch (IOException ex) {
127 throw new IllegalArgumentException(ex);
128 }
129
130 return Response
131 .noContent()
132 .build();
133 }
134
135 /**
Jonathan Hart8338b8d2017-08-15 15:46:14 -0700136 * Remove a unicast route.
137 * Removes a route from the unicast RIB.
138 *
139 * @param route unicast route JSON
140 * @return 204 NO CONTENT
141 * @onos.rsModel RoutePost
142 */
143 @DELETE
144 @Consumes(MediaType.APPLICATION_JSON)
145 public Response deleteRoute(InputStream route) {
146 RouteAdminService service = get(RouteAdminService.class);
147 try {
Ray Milkey86ee5e82018-04-02 15:33:07 -0700148 ObjectNode jsonTree = readTreeFromStream(mapper(), route);
Jonathan Hart8338b8d2017-08-15 15:46:14 -0700149 Route r = codec(Route.class).decode(jsonTree, this);
150 service.withdraw(Collections.singletonList(r));
151 } catch (IOException ex) {
152 throw new IllegalArgumentException(ex);
153 }
154 return Response.noContent().build();
155 }
Andrea Campanella40bccdb2018-03-09 17:53:25 -0800156
157 /**
158 * Removes unicast routes.
159 * Removes multiple routes from the unicast RIB.
160 *
161 * @param routesStream unicast routes array JSON
162 * @return 204 NO CONTENT
163 */
164 @DELETE
165 @Consumes(MediaType.APPLICATION_JSON)
166 @Path("/bulk")
167 public Response deleteRoutes(InputStream routesStream) {
168 RouteAdminService service = get(RouteAdminService.class);
169 try {
Ray Milkey86ee5e82018-04-02 15:33:07 -0700170 ObjectNode jsonTree = readTreeFromStream(mapper(), routesStream);
Andrea Campanella40bccdb2018-03-09 17:53:25 -0800171 ArrayNode routesArray = nullIsIllegal((ArrayNode) jsonTree.get(ROUTES),
172 ROUTES_KEY_ERROR);
173 List<Route> routes = codec(Route.class).decode(routesArray, this);
174 service.withdraw(routes);
175
176 } catch (IOException ex) {
177 throw new IllegalArgumentException(ex);
178 }
179
180 return Response
181 .noContent()
182 .build();
183 }
Jonathan Hart8338b8d2017-08-15 15:46:14 -0700184}