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