Ray Milkey | 19ffea3 | 2015-01-28 10:03:06 -0800 | [diff] [blame] | 1 | /* |
| 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 Hart | 9bb32ab | 2015-05-05 18:17:31 -0700 | [diff] [blame] | 16 | package org.onosproject.rest.resources; |
Ray Milkey | 19ffea3 | 2015-01-28 10:03:06 -0800 | [diff] [blame] | 17 | |
Thomas Vachuska | 48e64e4 | 2015-09-22 15:32:55 -0700 | [diff] [blame] | 18 | import org.onosproject.net.DeviceId; |
| 19 | import org.onosproject.net.ElementId; |
| 20 | import org.onosproject.net.HostId; |
| 21 | import org.onosproject.net.topology.PathService; |
| 22 | import org.onosproject.rest.AbstractWebResource; |
Ray Milkey | 19ffea3 | 2015-01-28 10:03:06 -0800 | [diff] [blame] | 23 | |
| 24 | import javax.ws.rs.GET; |
| 25 | import javax.ws.rs.Path; |
| 26 | import javax.ws.rs.PathParam; |
| 27 | import javax.ws.rs.Produces; |
| 28 | import javax.ws.rs.core.MediaType; |
| 29 | import javax.ws.rs.core.Response; |
Thomas Vachuska | 48e64e4 | 2015-09-22 15:32:55 -0700 | [diff] [blame] | 30 | import java.util.Set; |
Ray Milkey | 19ffea3 | 2015-01-28 10:03:06 -0800 | [diff] [blame] | 31 | |
| 32 | /** |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 33 | * Compute paths in the network graph. |
Ray Milkey | 19ffea3 | 2015-01-28 10:03:06 -0800 | [diff] [blame] | 34 | */ |
| 35 | @Path("paths") |
| 36 | public class PathsWebResource extends AbstractWebResource { |
| 37 | |
Ray Milkey | 19ffea3 | 2015-01-28 10:03:06 -0800 | [diff] [blame] | 38 | /** |
| 39 | * Determines if the id appears to be the id of a host. |
Ray Milkey | 5d915f4 | 2015-08-13 10:27:53 -0700 | [diff] [blame] | 40 | * Host id format is 00:00:00:00:00:01/-1 |
Ray Milkey | 19ffea3 | 2015-01-28 10:03:06 -0800 | [diff] [blame] | 41 | * |
| 42 | * @param id id string |
| 43 | * @return HostId if the id is valid, null otherwise |
| 44 | */ |
| 45 | private HostId isHostId(String id) { |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 46 | return id.matches("..:..:..:..:..:../.*") ? HostId.hostId(id) : null; |
Ray Milkey | 19ffea3 | 2015-01-28 10:03:06 -0800 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | /** |
Thomas Vachuska | 48e64e4 | 2015-09-22 15:32:55 -0700 | [diff] [blame] | 50 | * 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 Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 61 | * Get all shortest paths between any two hosts or devices. |
| 62 | * Returns array of all shortest paths between any two elements. |
Andrea Campanella | 10c4adc | 2015-12-03 15:27:54 -0800 | [diff] [blame] | 63 | * @onos.rsModel Paths |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 64 | * @param src source identifier |
| 65 | * @param dst destination identifier |
Ray Milkey | 19ffea3 | 2015-01-28 10:03:06 -0800 | [diff] [blame] | 66 | * @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 Vachuska | 48e64e4 | 2015-09-22 15:32:55 -0700 | [diff] [blame] | 74 | 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 Milkey | 19ffea3 | 2015-01-28 10:03:06 -0800 | [diff] [blame] | 77 | } |
| 78 | |
Thomas Vachuska | 48e64e4 | 2015-09-22 15:32:55 -0700 | [diff] [blame] | 79 | /** |
| 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 Campanella | 10c4adc | 2015-12-03 15:27:54 -0800 | [diff] [blame] | 82 | * @onos.rsModel Paths |
Thomas Vachuska | 48e64e4 | 2015-09-22 15:32:55 -0700 | [diff] [blame] | 83 | * @param src source identifier |
| 84 | * @param dst destination identifier |
| 85 | * @return path data |
| 86 | */ |
Nikhil Cheerla | 2ec191f | 2015-07-09 12:34:54 -0700 | [diff] [blame] | 87 | @GET |
| 88 | @Produces(MediaType.APPLICATION_JSON) |
| 89 | @Path("{src}/{dst}/disjoint") |
Nikhil Cheerla | 2ec191f | 2015-07-09 12:34:54 -0700 | [diff] [blame] | 90 | public Response getDisjointPath(@PathParam("src") String src, |
Thomas Vachuska | 48e64e4 | 2015-09-22 15:32:55 -0700 | [diff] [blame] | 91 | @PathParam("dst") String dst) { |
Nikhil Cheerla | 2ec191f | 2015-07-09 12:34:54 -0700 | [diff] [blame] | 92 | PathService pathService = get(PathService.class); |
Nikhil Cheerla | 2ec191f | 2015-07-09 12:34:54 -0700 | [diff] [blame] | 93 | Set<org.onosproject.net.DisjointPath> paths = |
Thomas Vachuska | 48e64e4 | 2015-09-22 15:32:55 -0700 | [diff] [blame] | 94 | pathService.getDisjointPaths(elementId(src), elementId(dst)); |
| 95 | return ok(encodeArray(org.onosproject.net.DisjointPath.class, "paths", paths)).build(); |
Nikhil Cheerla | 2ec191f | 2015-07-09 12:34:54 -0700 | [diff] [blame] | 96 | } |
Ray Milkey | 19ffea3 | 2015-01-28 10:03:06 -0800 | [diff] [blame] | 97 | } |