blob: 9f0019cfdfcc64c3a8ed4cce650b055fca221345 [file] [log] [blame]
Pavlin Radoslavov13669052014-05-13 10:33:39 -07001package net.onrc.onos.core.intent.runtime.web;
2
Pavlin Radoslavov13669052014-05-13 10:33:39 -07003import java.util.LinkedList;
4import java.util.List;
5
6import net.onrc.onos.core.intent.ConstrainedBFSTree;
7import net.onrc.onos.core.intent.Path;
Jonathan Harte37e4e22014-05-13 19:12:02 -07008import net.onrc.onos.core.topology.ITopologyService;
Pavlin Radoslavov13669052014-05-13 10:33:39 -07009import net.onrc.onos.core.topology.Link;
10import net.onrc.onos.core.topology.LinkEvent;
Pavlin Radoslavov13669052014-05-13 10:33:39 -070011import net.onrc.onos.core.topology.Switch;
Jonathan Harte37e4e22014-05-13 19:12:02 -070012import net.onrc.onos.core.topology.Topology;
Pavlin Radoslavov13669052014-05-13 10:33:39 -070013import net.onrc.onos.core.util.Dpid;
14
Pavlin Radoslavovc097fdf2014-05-23 17:40:57 -070015import org.restlet.representation.Representation;
Pavlin Radoslavov13669052014-05-13 10:33:39 -070016import org.restlet.resource.Get;
17import org.restlet.resource.ServerResource;
18import org.slf4j.Logger;
19import org.slf4j.LoggerFactory;
20
21/**
22 * A class to access Shortest-Path information between switches.
23 */
24public class ShortestPathResource extends ServerResource {
Pavlin Radoslavov13669052014-05-13 10:33:39 -070025 private static final Logger log = LoggerFactory.getLogger(ShortestPathResource.class);
26
27 /**
28 * Gets the Shortest-Path infomration between switches.
29 *
Pavlin Radoslavovc097fdf2014-05-23 17:40:57 -070030 * @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 Radoslavov13669052014-05-13 10:33:39 -070033 */
34 @Get("json")
Pavlin Radoslavovc097fdf2014-05-23 17:40:57 -070035 public Representation retrieve() {
Jonathan Harte37e4e22014-05-13 19:12:02 -070036 ITopologyService topologyService =
Pavlin Radoslavovc097fdf2014-05-23 17:40:57 -070037 (ITopologyService) getContext().getAttributes()
38 .get(ITopologyService.class.getCanonicalName());
Pavlin Radoslavov13669052014-05-13 10:33:39 -070039
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 Radoslavovc097fdf2014-05-23 17:40:57 -070053 Topology topology = topologyService.getTopology();
Jonathan Harte37e4e22014-05-13 19:12:02 -070054 topology.acquireReadLock();
Pavlin Radoslavov13669052014-05-13 10:33:39 -070055 try {
Jonathan Harte37e4e22014-05-13 19:12:02 -070056 Switch srcSwitch = topology.getSwitch(srcDpid.value());
57 Switch dstSwitch = topology.getSwitch(dstDpid.value());
Pavlin Radoslavov13669052014-05-13 10:33:39 -070058 if ((srcSwitch == null) || (dstSwitch == null)) {
Pavlin Radoslavovc097fdf2014-05-23 17:40:57 -070059 return null;
Pavlin Radoslavov13669052014-05-13 10:33:39 -070060 }
61 ConstrainedBFSTree bfsTree = new ConstrainedBFSTree(srcSwitch);
62 Path path = bfsTree.getPath(dstSwitch);
Pavlin Radoslavov8bfacf32014-06-09 16:02:56 -070063 if (path == null) {
64 return null;
65 }
Pavlin Radoslavov13669052014-05-13 10:33:39 -070066 List<Link> links = new LinkedList<>();
67 for (LinkEvent linkEvent : path) {
Jonathan Harte37e4e22014-05-13 19:12:02 -070068 Link link = topology.getLink(linkEvent.getSrc().getDpid(),
Pavlin Radoslavov13669052014-05-13 10:33:39 -070069 linkEvent.getSrc().getNumber(),
70 linkEvent.getDst().getDpid(),
71 linkEvent.getDst().getNumber());
72 if (link == null) {
Pavlin Radoslavovc097fdf2014-05-23 17:40:57 -070073 return null;
Pavlin Radoslavov13669052014-05-13 10:33:39 -070074 }
75 links.add(link);
76 }
Pavlin Radoslavovc097fdf2014-05-23 17:40:57 -070077 return toRepresentation(links, null);
Pavlin Radoslavov13669052014-05-13 10:33:39 -070078 } finally {
Jonathan Harte37e4e22014-05-13 19:12:02 -070079 topology.releaseReadLock();
Pavlin Radoslavov13669052014-05-13 10:33:39 -070080 }
81 }
82}