Jonathan Hart | 472062d | 2014-04-03 10:56:48 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.topology.web; |
Pavlin Radoslavov | 97af90a | 2014-02-25 18:34:02 -0800 | [diff] [blame] | 2 | |
| 3 | import java.io.IOException; |
| 4 | import java.util.LinkedList; |
| 5 | import java.util.List; |
| 6 | |
Jonathan Hart | aa38097 | 2014-04-03 10:24:46 -0700 | [diff] [blame] | 7 | import net.onrc.onos.core.intent.ConstrainedBFSTree; |
Jonathan Hart | 472062d | 2014-04-03 10:56:48 -0700 | [diff] [blame] | 8 | import net.onrc.onos.core.topology.INetworkGraphService; |
| 9 | import net.onrc.onos.core.topology.Link; |
| 10 | import net.onrc.onos.core.topology.LinkEvent; |
| 11 | import net.onrc.onos.core.topology.NetworkGraph; |
| 12 | import net.onrc.onos.core.topology.Path; |
| 13 | import net.onrc.onos.core.topology.Switch; |
| 14 | import net.onrc.onos.core.topology.serializers.LinkSerializer; |
Jonathan Hart | 23701d1 | 2014-04-03 10:45:48 -0700 | [diff] [blame] | 15 | import net.onrc.onos.core.util.Dpid; |
Pavlin Radoslavov | 97af90a | 2014-02-25 18:34:02 -0800 | [diff] [blame] | 16 | |
| 17 | import org.codehaus.jackson.Version; |
| 18 | import org.codehaus.jackson.map.ObjectMapper; |
| 19 | import org.codehaus.jackson.map.module.SimpleModule; |
| 20 | import org.restlet.resource.Get; |
| 21 | import org.restlet.resource.ServerResource; |
| 22 | import org.slf4j.Logger; |
| 23 | import org.slf4j.LoggerFactory; |
| 24 | |
| 25 | public class NetworkGraphShortestPathResource extends ServerResource { |
| 26 | |
| 27 | private static final Logger log = LoggerFactory.getLogger(NetworkGraphShortestPathResource.class); |
| 28 | |
| 29 | @Get("json") |
| 30 | public String retrieve() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame^] | 31 | INetworkGraphService networkGraphService = |
| 32 | (INetworkGraphService) getContext().getAttributes(). |
| 33 | get(INetworkGraphService.class.getCanonicalName()); |
Pavlin Radoslavov | 97af90a | 2014-02-25 18:34:02 -0800 | [diff] [blame] | 34 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame^] | 35 | NetworkGraph graph = networkGraphService.getNetworkGraph(); |
Pavlin Radoslavov | 97af90a | 2014-02-25 18:34:02 -0800 | [diff] [blame] | 36 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame^] | 37 | ObjectMapper mapper = new ObjectMapper(); |
| 38 | SimpleModule module = new SimpleModule("module", new Version(1, 0, 0, null)); |
| 39 | module.addSerializer(new LinkSerializer()); |
| 40 | mapper.registerModule(module); |
Pavlin Radoslavov | 97af90a | 2014-02-25 18:34:02 -0800 | [diff] [blame] | 41 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame^] | 42 | // |
| 43 | // Fetch the attributes |
| 44 | // |
| 45 | String srcDpidStr = (String) getRequestAttributes().get("src-dpid"); |
| 46 | String dstDpidStr = (String) getRequestAttributes().get("dst-dpid"); |
| 47 | Dpid srcDpid = new Dpid(srcDpidStr); |
| 48 | Dpid dstDpid = new Dpid(dstDpidStr); |
| 49 | log.debug("Getting Shortest Path {}--{}", srcDpidStr, dstDpidStr); |
Pavlin Radoslavov | 97af90a | 2014-02-25 18:34:02 -0800 | [diff] [blame] | 50 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame^] | 51 | // |
| 52 | // Do the Shortest Path computation and return the result: list of |
| 53 | // links. |
| 54 | // |
| 55 | try { |
| 56 | graph.acquireReadLock(); |
| 57 | Switch srcSwitch = graph.getSwitch(srcDpid.value()); |
| 58 | Switch dstSwitch = graph.getSwitch(dstDpid.value()); |
| 59 | if ((srcSwitch == null) || (dstSwitch == null)) |
| 60 | return ""; |
| 61 | ConstrainedBFSTree bfsTree = new ConstrainedBFSTree(srcSwitch); |
| 62 | Path path = bfsTree.getPath(dstSwitch); |
| 63 | List<Link> links = new LinkedList<>(); |
| 64 | for (LinkEvent linkEvent : path) { |
| 65 | Link link = graph.getLink(linkEvent.getSrc().getDpid(), |
| 66 | linkEvent.getSrc().getNumber(), |
| 67 | linkEvent.getDst().getDpid(), |
| 68 | linkEvent.getDst().getNumber()); |
| 69 | if (link == null) |
| 70 | return ""; |
| 71 | links.add(link); |
| 72 | } |
| 73 | return mapper.writeValueAsString(links); |
| 74 | } catch (IOException e) { |
| 75 | log.error("Error writing Shortest Path to JSON", e); |
| 76 | return ""; |
| 77 | } finally { |
| 78 | graph.releaseReadLock(); |
| 79 | } |
Pavlin Radoslavov | 97af90a | 2014-02-25 18:34:02 -0800 | [diff] [blame] | 80 | } |
| 81 | } |