2 * Copyright (c) 2015 Yale University and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.alto.northbound;
10 import javax.ws.rs.GET;
11 import javax.ws.rs.POST;
12 import javax.ws.rs.Path;
13 import javax.ws.rs.PathParam;
14 import javax.ws.rs.Consumes;
15 import javax.ws.rs.Produces;
16 import javax.ws.rs.core.Response;
17 import javax.ws.rs.core.Response.Status;
19 import org.opendaylight.alto.commons.types.rfc7285.RFC7285JSONMapper;
20 import org.opendaylight.alto.commons.types.rfc7285.FormatValidator;
21 import org.opendaylight.alto.commons.types.rfc7285.MediaType;
22 import org.opendaylight.alto.commons.types.rfc7285.RFC7285NetworkMap;
23 import org.opendaylight.alto.commons.types.rfc7285.RFC7285CostType;
24 import org.opendaylight.alto.commons.types.rfc7285.RFC7285IRD;
25 import org.opendaylight.alto.commons.types.rfc7285.RFC7285VersionTag;
26 import org.opendaylight.alto.commons.types.rfc7285.RFC7285CostMap;
27 import org.opendaylight.alto.commons.types.rfc7285.RFC7285Endpoint;
29 import org.opendaylight.alto.services.api.rfc7285.AltoService;
31 import org.opendaylight.alto.ext.fake.FakeAltoService;
33 import org.opendaylight.alto.northbound.exception.AltoBasicException;
34 import org.opendaylight.alto.northbound.exception.AltoBadFormatException;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
40 public class AltoNorthbound {
42 private static final Logger logger = LoggerFactory.getLogger(AltoNorthbound.class);
44 private AltoService altoService = new FakeAltoService();
45 private RFC7285JSONMapper mapper = new RFC7285JSONMapper();
47 private void checkAltoService() throws Exception {
48 if (altoService == null)
49 throw new AltoBasicException(Status.SERVICE_UNAVAILABLE, null);
52 private void checkResourceId(String rid) throws AltoBadFormatException {
53 if (!FormatValidator.validResourceId(rid))
54 throw new AltoBadFormatException("resource-id", rid);
57 private void checkTag(String tag) throws AltoBadFormatException {
58 if (!FormatValidator.validTag(tag))
59 throw new AltoBadFormatException("tag", tag);
62 private Response fail(Response.Status status, Object data) {
64 String output = (data == null ? "" : mapper.asJSON(data));
65 return Response.status(status)
67 .type(MediaType.ALTO_ERROR).build();
68 } catch (Exception e) {
69 logger.error("Failed to parse object to json: {}", data.toString());
70 return Response.status(status)
71 .type(MediaType.ALTO_ERROR).build();
75 private Response success(Object data, String mediaType) {
77 String output = mapper.asJSON(data);
78 return Response.ok(output, mediaType).build();
79 } catch (Exception e) {
80 logger.error("Failed to parse object to json: {}", data.toString());
81 return fail(Status.INTERNAL_SERVER_ERROR, null);
86 @Produces({ MediaType.ALTO_DIRECTORY, MediaType.ALTO_ERROR })
87 public Response retrieveIRD() throws Exception {
90 RFC7285IRD ird = altoService.getDefaultIRD();
92 return fail(Status.NOT_FOUND, null);
93 return success(ird, MediaType.ALTO_DIRECTORY);
98 @Produces({ MediaType.ALTO_DIRECTORY, MediaType.ALTO_ERROR })
99 public Response retrieveIRD(
100 @PathParam("id") String id) throws Exception {
104 RFC7285IRD ird = altoService.getIRD(id);
106 return fail(Status.NOT_FOUND, id);
107 return success(ird, MediaType.ALTO_DIRECTORY);
112 @Produces({ MediaType.ALTO_NETWORKMAP, MediaType.ALTO_ERROR })
113 public Response retrieveDefaultNetworkMap() throws Exception {
116 RFC7285NetworkMap map = altoService.getDefaultNetworkMap();
118 return fail(Status.NOT_FOUND, null);
119 return success(map, MediaType.ALTO_NETWORKMAP);
122 @Path("/networkmap/{id}")
124 @Produces({ MediaType.ALTO_NETWORKMAP, MediaType.ALTO_ERROR })
125 public Response retrieveNetworkMap(
126 @PathParam("id") String id) throws Exception {
130 RFC7285NetworkMap map = altoService.getNetworkMap(id);
132 return fail(Status.NOT_FOUND, id);
133 return success(map, MediaType.ALTO_NETWORKMAP);
136 @Path("/networkmap/{id}/{tag}")
138 @Produces({ MediaType.ALTO_NETWORKMAP, MediaType.ALTO_ERROR })
139 public Response retrieveNetworkMap(
140 @PathParam("id") String id,
141 @PathParam("tag") String tag) throws Exception {
146 RFC7285VersionTag vtag = new RFC7285VersionTag(id, tag);
147 RFC7285NetworkMap map = altoService.getNetworkMap(vtag);
149 return fail(Status.NOT_FOUND, vtag);
150 return success(map, MediaType.ALTO_NETWORKMAP);
153 @Path("/costmap/{id}")
155 @Produces({ MediaType.ALTO_COSTMAP, MediaType.ALTO_ERROR})
156 public Response retrieveCostMap(@PathParam("id") String id) throws Exception {
160 RFC7285CostMap map = altoService.getCostMap(id);
162 return fail(Status.NOT_FOUND, id);
163 return success(map, MediaType.ALTO_COSTMAP);
166 @Path("/costmap/{id}/{tag}")
168 @Produces({ MediaType.ALTO_COSTMAP, MediaType.ALTO_ERROR})
169 public Response retrieveCostMap(
170 @PathParam("id") String id,
171 @PathParam("tag") String tag) throws Exception {
176 RFC7285VersionTag vtag = new RFC7285VersionTag(id, tag);
177 RFC7285CostMap map = altoService.getCostMap(vtag);
179 return fail(Status.NOT_FOUND, vtag);
180 return success(map, MediaType.ALTO_COSTMAP);
183 @Path("/costmap/{id}/{mode}/{metric}")
185 @Produces({ MediaType.ALTO_COSTMAP, MediaType.ALTO_ERROR})
186 public Response retrieveCostMap(
187 @PathParam("id") String id,
188 @PathParam("mode") String mode,
189 @PathParam("metric") String metric) throws Exception {
193 RFC7285CostType costType = new RFC7285CostType(mode, metric);
194 if (!altoService.supportCostType(id, costType))
195 return fail(Status.NOT_FOUND, costType);
196 RFC7285CostMap map = altoService.getCostMap(id, costType);
198 return fail(Status.NOT_FOUND, id);
199 return success(map, MediaType.ALTO_COSTMAP);
202 @Path("/costmap/{id}/{tag}/{mode}/{metric}")
204 @Produces({ MediaType.ALTO_COSTMAP, MediaType.ALTO_ERROR})
205 public Response retrieveCostMap(
206 @PathParam("id") String id,
207 @PathParam("tag") String tag,
208 @PathParam("mode") String mode,
209 @PathParam("metric") String metric) throws Exception {
214 RFC7285VersionTag vtag = new RFC7285VersionTag(id, tag);
215 RFC7285CostType costType = new RFC7285CostType(mode, metric);
216 if (!altoService.supportCostType(vtag, costType))
217 return fail(Status.NOT_FOUND, costType);
218 RFC7285CostMap map = altoService.getCostMap(vtag, costType);
220 return fail(Status.NOT_FOUND, vtag);
221 return success(map, MediaType.ALTO_COSTMAP);
224 @Path("/filtered/networkmap/{id}")
226 @Consumes({ MediaType.ALTO_NETWORKMAP_FILTER})
227 @Produces({ MediaType.ALTO_NETWORKMAP, MediaType.ALTO_ERROR})
228 public Response retrieveFilteredNetworkMap(
229 @PathParam("id") String id, String filterJSON) throws Exception {
233 RFC7285NetworkMap.Filter filter = mapper.asNetworkMapFilter(filterJSON);
235 if (!altoService.validateNetworkMapFilter(id, filter))
236 return fail(Status.BAD_REQUEST, filter);
237 RFC7285NetworkMap map = altoService.getNetworkMap(id, filter);
239 return fail(Status.NOT_FOUND, id);
240 return success(map, MediaType.ALTO_NETWORKMAP);
243 @Path("/filtered/networkmap/{id}/{tag}")
245 @Consumes({ MediaType.ALTO_NETWORKMAP_FILTER})
246 @Produces({ MediaType.ALTO_NETWORKMAP, MediaType.ALTO_ERROR})
247 public Response retrieveFilteredNetworkMap(
248 @PathParam("id") String id,
249 @PathParam("tag") String tag,
250 String filterJSON) throws Exception {
255 RFC7285VersionTag vtag = new RFC7285VersionTag(id, tag);
256 RFC7285NetworkMap.Filter filter = mapper.asNetworkMapFilter(filterJSON);
257 if (!altoService.validateNetworkMapFilter(vtag, filter))
258 return fail(Status.BAD_REQUEST, filter);
260 RFC7285NetworkMap map = altoService.getNetworkMap(vtag, filter);
262 return fail(Status.NOT_FOUND, vtag);
263 return success(map, MediaType.ALTO_NETWORKMAP);
266 @Path("/filtered/costmap/{id}")
268 @Consumes({ MediaType.ALTO_COSTMAP_FILTER })
269 @Produces({ MediaType.ALTO_COSTMAP, MediaType.ALTO_ERROR})
270 public Response retrieveFilteredCostMap(
271 @PathParam("id") String id, String filterJSON) throws Exception {
275 RFC7285CostMap.Filter filter = mapper.asCostMapFilter(filterJSON);
276 if (!altoService.validateCostMapFilter(id, filter))
277 return fail(Status.BAD_REQUEST, filter);
279 RFC7285CostMap map = altoService.getCostMap(id, filter);
281 return fail(Status.NOT_FOUND, id);
282 return success(map, MediaType.ALTO_COSTMAP);
285 @Path("/filtered/costmap/{id}/{tag}")
287 @Consumes({ MediaType.ALTO_COSTMAP_FILTER })
288 @Produces({ MediaType.ALTO_COSTMAP, MediaType.ALTO_ERROR})
289 public Response retrieveFilteredCostMap(
290 @PathParam("id") String id,
291 @PathParam("tag") String tag, String filterJSON) throws Exception {
296 RFC7285VersionTag vtag = new RFC7285VersionTag(id, tag);
297 RFC7285CostMap.Filter filter = mapper.asCostMapFilter(filterJSON);
298 if (!altoService.validateCostMapFilter(vtag, filter))
299 return fail(Status.BAD_REQUEST, filter);
301 RFC7285CostMap map = altoService.getCostMap(vtag, filter);
303 return fail(Status.NOT_FOUND, vtag);
304 return success(map, MediaType.ALTO_COSTMAP);
307 @Path("/endpointprop/lookup/{id}")
309 @Consumes({ MediaType.ALTO_ENDPOINT_PROPPARAMS })
310 @Produces({ MediaType.ALTO_ENDPOINT_PROP, MediaType.ALTO_ERROR })
311 public Response retrieveEndpointPropMap(
312 @PathParam("id") String id,
313 String params) throws Exception {
317 RFC7285Endpoint.PropertyRequest request = mapper.asPropertyRequest(params);
318 RFC7285Endpoint.PropertyResponse response = altoService.getEndpointProperty(id, request);
319 if (response == null)
320 return fail(Status.NOT_FOUND, request);
321 return success(response, MediaType.ALTO_ENDPOINT_PROP);
324 @Path("/endpointprop/lookup/{id}/{tag}")
326 @Consumes({ MediaType.ALTO_ENDPOINT_PROPPARAMS })
327 @Produces({ MediaType.ALTO_ENDPOINT_PROP, MediaType.ALTO_ERROR })
328 public Response retrieveEndpointPropMap(
329 @PathParam("id") String id,
330 @PathParam("tag") String tag,
331 String params) throws Exception {
336 RFC7285VersionTag vtag = new RFC7285VersionTag(id, tag);
337 RFC7285Endpoint.PropertyRequest request = mapper.asPropertyRequest(params);
338 RFC7285Endpoint.PropertyResponse response = altoService.getEndpointProperty(vtag, request);
339 if (response == null)
340 return fail(Status.NOT_FOUND, request);
341 return success(response, MediaType.ALTO_ENDPOINT_PROP);
344 @Path("/endpointcost/lookup/{id}")
346 @Consumes({ MediaType.ALTO_ENDPOINT_COSTPARAMS })
347 @Produces({ MediaType.ALTO_ENDPOINT_COST, MediaType.ALTO_ERROR })
348 public Response retrieveEndpointCostMap(
349 @PathParam("id") String id,
350 String params) throws Exception {
354 RFC7285Endpoint.CostRequest request = mapper.asCostRequest(params);
355 RFC7285Endpoint.CostResponse response = altoService.getEndpointCost(id, request);
356 if (response == null)
357 return fail(Status.NOT_FOUND, request);
358 return success(response, MediaType.ALTO_ENDPOINT_COST);
361 @Path("/endpointcost/lookup/{id}/{tag}")
363 @Consumes({ MediaType.ALTO_ENDPOINT_COSTPARAMS })
364 @Produces({ MediaType.ALTO_ENDPOINT_COST, MediaType.ALTO_ERROR })
365 public Response retrieveEndpointCostMap(
366 @PathParam("id") String id,
367 @PathParam("tag") String tag,
368 String params) throws Exception {
373 RFC7285VersionTag vtag = new RFC7285VersionTag(id, tag);
374 RFC7285Endpoint.CostRequest request = mapper.asCostRequest(params);
375 RFC7285Endpoint.CostResponse response = altoService.getEndpointCost(vtag, request);
376 if (response == null)
377 return fail(Status.NOT_FOUND, request);
378 return success(response, MediaType.ALTO_ENDPOINT_COST);