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 | |
| 18 | import java.util.Set; |
| 19 | |
| 20 | import javax.ws.rs.GET; |
| 21 | import javax.ws.rs.Path; |
| 22 | import javax.ws.rs.PathParam; |
| 23 | import javax.ws.rs.Produces; |
| 24 | import javax.ws.rs.core.MediaType; |
| 25 | import javax.ws.rs.core.Response; |
| 26 | |
| 27 | import org.onosproject.net.DeviceId; |
| 28 | import org.onosproject.net.ElementId; |
| 29 | import org.onosproject.net.HostId; |
| 30 | import org.onosproject.net.topology.PathService; |
| 31 | |
| 32 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Jonathan Hart | 9bb32ab | 2015-05-05 18:17:31 -0700 | [diff] [blame] | 33 | import org.onosproject.rest.AbstractWebResource; |
Ray Milkey | 19ffea3 | 2015-01-28 10:03:06 -0800 | [diff] [blame] | 34 | |
| 35 | /** |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 36 | * Compute paths in the network graph. |
Ray Milkey | 19ffea3 | 2015-01-28 10:03:06 -0800 | [diff] [blame] | 37 | */ |
| 38 | @Path("paths") |
| 39 | public class PathsWebResource extends AbstractWebResource { |
| 40 | |
Ray Milkey | 19ffea3 | 2015-01-28 10:03:06 -0800 | [diff] [blame] | 41 | /** |
| 42 | * Determines if the id appears to be the id of a host. |
Ray Milkey | 5d915f4 | 2015-08-13 10:27:53 -0700 | [diff] [blame] | 43 | * Host id format is 00:00:00:00:00:01/-1 |
Ray Milkey | 19ffea3 | 2015-01-28 10:03:06 -0800 | [diff] [blame] | 44 | * |
| 45 | * @param id id string |
| 46 | * @return HostId if the id is valid, null otherwise |
| 47 | */ |
| 48 | private HostId isHostId(String id) { |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 49 | return id.matches("..:..:..:..:..:../.*") ? HostId.hostId(id) : null; |
Ray Milkey | 19ffea3 | 2015-01-28 10:03:06 -0800 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | /** |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 53 | * Get all shortest paths between any two hosts or devices. |
| 54 | * Returns array of all shortest paths between any two elements. |
Ray Milkey | 19ffea3 | 2015-01-28 10:03:06 -0800 | [diff] [blame] | 55 | * |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 56 | * @param src source identifier |
| 57 | * @param dst destination identifier |
Ray Milkey | 19ffea3 | 2015-01-28 10:03:06 -0800 | [diff] [blame] | 58 | * @return path data |
| 59 | */ |
| 60 | @GET |
| 61 | @Produces(MediaType.APPLICATION_JSON) |
| 62 | @Path("{src}/{dst}") |
| 63 | public Response getPath(@PathParam("src") String src, |
| 64 | @PathParam("dst") String dst) { |
| 65 | PathService pathService = get(PathService.class); |
| 66 | |
| 67 | ElementId srcElement = isHostId(src); |
| 68 | ElementId dstElement = isHostId(dst); |
| 69 | |
| 70 | if (srcElement == null) { |
| 71 | // Doesn't look like a host, assume it is a device |
| 72 | srcElement = DeviceId.deviceId(src); |
| 73 | } |
| 74 | |
| 75 | if (dstElement == null) { |
| 76 | // Doesn't look like a host, assume it is a device |
| 77 | dstElement = DeviceId.deviceId(dst); |
| 78 | } |
| 79 | |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 80 | Set<org.onosproject.net.Path> paths = pathService.getPaths(srcElement, dstElement); |
| 81 | ObjectNode root = encodeArray(org.onosproject.net.Path.class, "paths", paths); |
Ray Milkey | 19ffea3 | 2015-01-28 10:03:06 -0800 | [diff] [blame] | 82 | return ok(root).build(); |
| 83 | } |
| 84 | |
| 85 | } |