Pavlin Radoslavov | 1366905 | 2014-05-13 10:33:39 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.intent.runtime.web; |
| 2 | |
Yuta HIGUCHI | a0dcd47 | 2014-07-06 21:06:29 -0700 | [diff] [blame] | 3 | import static net.onrc.onos.core.topology.web.TopologyResource.eval; |
Pavlin Radoslavov | 1366905 | 2014-05-13 10:33:39 -0700 | [diff] [blame] | 4 | import java.util.LinkedList; |
| 5 | import java.util.List; |
| 6 | |
| 7 | import net.onrc.onos.core.intent.ConstrainedBFSTree; |
| 8 | import net.onrc.onos.core.intent.Path; |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 9 | import net.onrc.onos.core.topology.ITopologyService; |
Pavlin Radoslavov | 1366905 | 2014-05-13 10:33:39 -0700 | [diff] [blame] | 10 | import net.onrc.onos.core.topology.Link; |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 11 | import net.onrc.onos.core.topology.LinkData; |
Pavlin Radoslavov | 1366905 | 2014-05-13 10:33:39 -0700 | [diff] [blame] | 12 | import net.onrc.onos.core.topology.Switch; |
Yuta HIGUCHI | d92b10c | 2014-08-25 09:30:28 -0700 | [diff] [blame] | 13 | import net.onrc.onos.core.topology.MutableTopology; |
Pavlin Radoslavov | 1366905 | 2014-05-13 10:33:39 -0700 | [diff] [blame] | 14 | import net.onrc.onos.core.util.Dpid; |
Yuta HIGUCHI | a0dcd47 | 2014-07-06 21:06:29 -0700 | [diff] [blame] | 15 | |
Pavlin Radoslavov | c097fdf | 2014-05-23 17:40:57 -0700 | [diff] [blame] | 16 | import org.restlet.representation.Representation; |
Pavlin Radoslavov | 1366905 | 2014-05-13 10:33:39 -0700 | [diff] [blame] | 17 | import org.restlet.resource.Get; |
| 18 | import org.restlet.resource.ServerResource; |
| 19 | import org.slf4j.Logger; |
| 20 | import org.slf4j.LoggerFactory; |
| 21 | |
| 22 | /** |
| 23 | * A class to access Shortest-Path information between switches. |
| 24 | */ |
| 25 | public class ShortestPathResource extends ServerResource { |
Pavlin Radoslavov | 1366905 | 2014-05-13 10:33:39 -0700 | [diff] [blame] | 26 | private static final Logger log = LoggerFactory.getLogger(ShortestPathResource.class); |
| 27 | |
| 28 | /** |
| 29 | * Gets the Shortest-Path infomration between switches. |
| 30 | * |
Pavlin Radoslavov | c097fdf | 2014-05-23 17:40:57 -0700 | [diff] [blame] | 31 | * @return a Representation with the Shortest-Path information between |
| 32 | * switches if found, otherwise null. The Shortest-Path information is an |
| 33 | * ordered collection of Links. |
Pavlin Radoslavov | 1366905 | 2014-05-13 10:33:39 -0700 | [diff] [blame] | 34 | */ |
| 35 | @Get("json") |
Pavlin Radoslavov | c097fdf | 2014-05-23 17:40:57 -0700 | [diff] [blame] | 36 | public Representation retrieve() { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 37 | ITopologyService topologyService = |
Pavlin Radoslavov | c097fdf | 2014-05-23 17:40:57 -0700 | [diff] [blame] | 38 | (ITopologyService) getContext().getAttributes() |
| 39 | .get(ITopologyService.class.getCanonicalName()); |
Pavlin Radoslavov | 1366905 | 2014-05-13 10:33:39 -0700 | [diff] [blame] | 40 | |
| 41 | // |
| 42 | // Fetch the attributes |
| 43 | // |
| 44 | String srcDpidStr = (String) getRequestAttributes().get("src-dpid"); |
| 45 | String dstDpidStr = (String) getRequestAttributes().get("dst-dpid"); |
| 46 | Dpid srcDpid = new Dpid(srcDpidStr); |
| 47 | Dpid dstDpid = new Dpid(dstDpidStr); |
| 48 | log.debug("Getting Shortest Path {}--{}", srcDpidStr, dstDpidStr); |
| 49 | |
| 50 | // |
| 51 | // Do the Shortest Path computation and return the result: a list of |
| 52 | // links. |
| 53 | // |
Yuta HIGUCHI | d92b10c | 2014-08-25 09:30:28 -0700 | [diff] [blame] | 54 | MutableTopology mutableTopology = topologyService.getTopology(); |
| 55 | mutableTopology.acquireReadLock(); |
Pavlin Radoslavov | 1366905 | 2014-05-13 10:33:39 -0700 | [diff] [blame] | 56 | try { |
Yuta HIGUCHI | d92b10c | 2014-08-25 09:30:28 -0700 | [diff] [blame] | 57 | Switch srcSwitch = mutableTopology.getSwitch(srcDpid); |
| 58 | Switch dstSwitch = mutableTopology.getSwitch(dstDpid); |
Pavlin Radoslavov | 1366905 | 2014-05-13 10:33:39 -0700 | [diff] [blame] | 59 | if ((srcSwitch == null) || (dstSwitch == null)) { |
Pavlin Radoslavov | c097fdf | 2014-05-23 17:40:57 -0700 | [diff] [blame] | 60 | return null; |
Pavlin Radoslavov | 1366905 | 2014-05-13 10:33:39 -0700 | [diff] [blame] | 61 | } |
| 62 | ConstrainedBFSTree bfsTree = new ConstrainedBFSTree(srcSwitch); |
| 63 | Path path = bfsTree.getPath(dstSwitch); |
Pavlin Radoslavov | 8bfacf3 | 2014-06-09 16:02:56 -0700 | [diff] [blame] | 64 | if (path == null) { |
| 65 | return null; |
| 66 | } |
Pavlin Radoslavov | 1366905 | 2014-05-13 10:33:39 -0700 | [diff] [blame] | 67 | List<Link> links = new LinkedList<>(); |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 68 | for (LinkData linkData : path) { |
Yuta HIGUCHI | d92b10c | 2014-08-25 09:30:28 -0700 | [diff] [blame] | 69 | Link link = mutableTopology.getLink( |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 70 | linkData.getSrc().getDpid(), |
| 71 | linkData.getSrc().getPortNumber(), |
| 72 | linkData.getDst().getDpid(), |
| 73 | linkData.getDst().getPortNumber()); |
Pavlin Radoslavov | 1366905 | 2014-05-13 10:33:39 -0700 | [diff] [blame] | 74 | if (link == null) { |
Pavlin Radoslavov | c097fdf | 2014-05-23 17:40:57 -0700 | [diff] [blame] | 75 | return null; |
Pavlin Radoslavov | 1366905 | 2014-05-13 10:33:39 -0700 | [diff] [blame] | 76 | } |
| 77 | links.add(link); |
| 78 | } |
Yuta HIGUCHI | a0dcd47 | 2014-07-06 21:06:29 -0700 | [diff] [blame] | 79 | return eval(toRepresentation(links, null)); |
Pavlin Radoslavov | 1366905 | 2014-05-13 10:33:39 -0700 | [diff] [blame] | 80 | } finally { |
Yuta HIGUCHI | d92b10c | 2014-08-25 09:30:28 -0700 | [diff] [blame] | 81 | mutableTopology.releaseReadLock(); |
Pavlin Radoslavov | 1366905 | 2014-05-13 10:33:39 -0700 | [diff] [blame] | 82 | } |
| 83 | } |
| 84 | } |