blob: 9968d1e7e1a5abeeee1747fd5609c1f6495412b7 [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
Thomas Vachuska48e64e42015-09-22 15:32:55 -070018import org.onosproject.net.DeviceId;
19import org.onosproject.net.ElementId;
20import org.onosproject.net.HostId;
21import org.onosproject.net.topology.PathService;
22import org.onosproject.rest.AbstractWebResource;
Ray Milkey19ffea32015-01-28 10:03:06 -080023
24import javax.ws.rs.GET;
25import javax.ws.rs.Path;
26import javax.ws.rs.PathParam;
27import javax.ws.rs.Produces;
28import javax.ws.rs.core.MediaType;
29import javax.ws.rs.core.Response;
Thomas Vachuska48e64e42015-09-22 15:32:55 -070030import java.util.Set;
Ray Milkey19ffea32015-01-28 10:03:06 -080031
32/**
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070033 * Compute paths in the network graph.
Ray Milkey19ffea32015-01-28 10:03:06 -080034 */
35@Path("paths")
36public class PathsWebResource extends AbstractWebResource {
37
Ray Milkey19ffea32015-01-28 10:03:06 -080038 /**
39 * Determines if the id appears to be the id of a host.
Ray Milkey5d915f42015-08-13 10:27:53 -070040 * Host id format is 00:00:00:00:00:01/-1
Ray Milkey19ffea32015-01-28 10:03:06 -080041 *
42 * @param id id string
43 * @return HostId if the id is valid, null otherwise
44 */
45 private HostId isHostId(String id) {
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070046 return id.matches("..:..:..:..:..:../.*") ? HostId.hostId(id) : null;
Ray Milkey19ffea32015-01-28 10:03:06 -080047 }
48
49 /**
Thomas Vachuska48e64e42015-09-22 15:32:55 -070050 * Returns either host id or device id, depending on the ID format.
51 *
52 * @param id host or device id string
53 * @return element id
54 */
55 private ElementId elementId(String id) {
56 ElementId elementId = isHostId(id);
57 return elementId != null ? elementId : DeviceId.deviceId(id);
58 }
59
60 /**
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070061 * Get all shortest paths between any two hosts or devices.
62 * Returns array of all shortest paths between any two elements.
Andrea Campanella10c4adc2015-12-03 15:27:54 -080063 * @onos.rsModel Paths
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070064 * @param src source identifier
65 * @param dst destination identifier
Ray Milkey19ffea32015-01-28 10:03:06 -080066 * @return path data
67 */
68 @GET
69 @Produces(MediaType.APPLICATION_JSON)
70 @Path("{src}/{dst}")
71 public Response getPath(@PathParam("src") String src,
72 @PathParam("dst") String dst) {
73 PathService pathService = get(PathService.class);
Thomas Vachuska48e64e42015-09-22 15:32:55 -070074 Set<org.onosproject.net.Path> paths =
75 pathService.getPaths(elementId(src), elementId(dst));
76 return ok(encodeArray(org.onosproject.net.Path.class, "paths", paths)).build();
Ray Milkey19ffea32015-01-28 10:03:06 -080077 }
78
Thomas Vachuska48e64e42015-09-22 15:32:55 -070079 /**
80 * Get all shortest disjoint paths between any two hosts or devices.
81 * Returns array of all shortest disjoint paths between any two elements.
Andrea Campanella10c4adc2015-12-03 15:27:54 -080082 * @onos.rsModel Paths
Thomas Vachuska48e64e42015-09-22 15:32:55 -070083 * @param src source identifier
84 * @param dst destination identifier
85 * @return path data
86 */
Nikhil Cheerla2ec191f2015-07-09 12:34:54 -070087 @GET
88 @Produces(MediaType.APPLICATION_JSON)
89 @Path("{src}/{dst}/disjoint")
Nikhil Cheerla2ec191f2015-07-09 12:34:54 -070090 public Response getDisjointPath(@PathParam("src") String src,
Thomas Vachuska48e64e42015-09-22 15:32:55 -070091 @PathParam("dst") String dst) {
Nikhil Cheerla2ec191f2015-07-09 12:34:54 -070092 PathService pathService = get(PathService.class);
Nikhil Cheerla2ec191f2015-07-09 12:34:54 -070093 Set<org.onosproject.net.DisjointPath> paths =
Thomas Vachuska48e64e42015-09-22 15:32:55 -070094 pathService.getDisjointPaths(elementId(src), elementId(dst));
95 return ok(encodeArray(org.onosproject.net.DisjointPath.class, "paths", paths)).build();
Nikhil Cheerla2ec191f2015-07-09 12:34:54 -070096 }
Ray Milkey19ffea32015-01-28 10:03:06 -080097}