blob: 58f9b2dfd8b20b7a5708dc8edf3d28fd5710a760 [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;
Pavlin Radoslavovc097fdf2014-05-23 17:40:57 -070014import org.restlet.representation.Representation;
Pavlin Radoslavov13669052014-05-13 10:33:39 -070015import org.restlet.resource.Get;
16import org.restlet.resource.ServerResource;
17import org.slf4j.Logger;
18import org.slf4j.LoggerFactory;
19
20/**
21 * A class to access Shortest-Path information between switches.
22 */
23public class ShortestPathResource extends ServerResource {
Pavlin Radoslavov13669052014-05-13 10:33:39 -070024 private static final Logger log = LoggerFactory.getLogger(ShortestPathResource.class);
25
26 /**
27 * Gets the Shortest-Path infomration between switches.
28 *
Pavlin Radoslavovc097fdf2014-05-23 17:40:57 -070029 * @return a Representation with the Shortest-Path information between
30 * switches if found, otherwise null. The Shortest-Path information is an
31 * ordered collection of Links.
Pavlin Radoslavov13669052014-05-13 10:33:39 -070032 */
33 @Get("json")
Pavlin Radoslavovc097fdf2014-05-23 17:40:57 -070034 public Representation retrieve() {
Jonathan Harte37e4e22014-05-13 19:12:02 -070035 ITopologyService topologyService =
Pavlin Radoslavovc097fdf2014-05-23 17:40:57 -070036 (ITopologyService) getContext().getAttributes()
37 .get(ITopologyService.class.getCanonicalName());
Pavlin Radoslavov13669052014-05-13 10:33:39 -070038
39 //
40 // Fetch the attributes
41 //
42 String srcDpidStr = (String) getRequestAttributes().get("src-dpid");
43 String dstDpidStr = (String) getRequestAttributes().get("dst-dpid");
44 Dpid srcDpid = new Dpid(srcDpidStr);
45 Dpid dstDpid = new Dpid(dstDpidStr);
46 log.debug("Getting Shortest Path {}--{}", srcDpidStr, dstDpidStr);
47
48 //
49 // Do the Shortest Path computation and return the result: a list of
50 // links.
51 //
Pavlin Radoslavovc097fdf2014-05-23 17:40:57 -070052 Topology topology = topologyService.getTopology();
Jonathan Harte37e4e22014-05-13 19:12:02 -070053 topology.acquireReadLock();
Pavlin Radoslavov13669052014-05-13 10:33:39 -070054 try {
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070055 Switch srcSwitch = topology.getSwitch(srcDpid);
56 Switch dstSwitch = topology.getSwitch(dstDpid);
Pavlin Radoslavov13669052014-05-13 10:33:39 -070057 if ((srcSwitch == null) || (dstSwitch == null)) {
Pavlin Radoslavovc097fdf2014-05-23 17:40:57 -070058 return null;
Pavlin Radoslavov13669052014-05-13 10:33:39 -070059 }
60 ConstrainedBFSTree bfsTree = new ConstrainedBFSTree(srcSwitch);
61 Path path = bfsTree.getPath(dstSwitch);
Pavlin Radoslavov8bfacf32014-06-09 16:02:56 -070062 if (path == null) {
63 return null;
64 }
Pavlin Radoslavov13669052014-05-13 10:33:39 -070065 List<Link> links = new LinkedList<>();
66 for (LinkEvent linkEvent : path) {
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070067 Link link = topology.getLink(
68 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}