blob: 42bd4e1249cf0587dd9199ae626e74eaccc94e36 [file] [log] [blame]
Ray Milkey19ffea32015-01-28 10:03:06 -08001/*
2 * Copyright 2015 Open Networking Laboratory
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 */
Jonathan Hart9bb32ab2015-05-05 18:17:31 -070016package org.onosproject.rest.resources;
Ray Milkey19ffea32015-01-28 10:03:06 -080017
18import java.util.Set;
19
20import javax.ws.rs.GET;
21import javax.ws.rs.Path;
22import javax.ws.rs.PathParam;
23import javax.ws.rs.Produces;
24import javax.ws.rs.core.MediaType;
25import javax.ws.rs.core.Response;
26
27import org.onosproject.net.DeviceId;
28import org.onosproject.net.ElementId;
29import org.onosproject.net.HostId;
30import org.onosproject.net.topology.PathService;
31
32import com.fasterxml.jackson.databind.node.ObjectNode;
Jonathan Hart9bb32ab2015-05-05 18:17:31 -070033import org.onosproject.rest.AbstractWebResource;
Ray Milkey19ffea32015-01-28 10:03:06 -080034
35/**
36 * REST resource for interacting with path calculations.
37 */
38@Path("paths")
39public class PathsWebResource extends AbstractWebResource {
40
Ray Milkey19ffea32015-01-28 10:03:06 -080041 /**
42 * Determines if the id appears to be the id of a host.
Ray Milkey5d915f42015-08-13 10:27:53 -070043 * Host id format is 00:00:00:00:00:01/-1
Ray Milkey19ffea32015-01-28 10:03:06 -080044 *
45 * @param id id string
46 * @return HostId if the id is valid, null otherwise
47 */
48 private HostId isHostId(String id) {
49
Ray Milkey5d915f42015-08-13 10:27:53 -070050 if (!id.matches("..:..:..:..:..:../.*")) {
Ray Milkey19ffea32015-01-28 10:03:06 -080051 return null;
52 }
53
54 return HostId.hostId(id);
55 }
56
57 /**
58 * Gets the paths between two elements.
59 *
60 * @param src source
61 * @param dst destination
62 * @return path data
63 */
64 @GET
65 @Produces(MediaType.APPLICATION_JSON)
66 @Path("{src}/{dst}")
67 public Response getPath(@PathParam("src") String src,
68 @PathParam("dst") String dst) {
69 PathService pathService = get(PathService.class);
70
71 ElementId srcElement = isHostId(src);
72 ElementId dstElement = isHostId(dst);
73
74 if (srcElement == null) {
75 // Doesn't look like a host, assume it is a device
76 srcElement = DeviceId.deviceId(src);
77 }
78
79 if (dstElement == null) {
80 // Doesn't look like a host, assume it is a device
81 dstElement = DeviceId.deviceId(dst);
82 }
83
84 Set<org.onosproject.net.Path> paths =
85 pathService.getPaths(srcElement, dstElement);
86 ObjectNode root =
87 encodeArray(org.onosproject.net.Path.class, "paths", paths);
88 return ok(root).build();
89 }
90
91}