blob: d5138047593b336d2f4f4cc86b0e01a20be96641 [file] [log] [blame]
Jonathan Hart472062d2014-04-03 10:56:48 -07001package net.onrc.onos.core.topology.web;
Pavlin Radoslavov97af90a2014-02-25 18:34:02 -08002
3import java.io.IOException;
4import java.util.LinkedList;
5import java.util.List;
6
Jonathan Hartaa380972014-04-03 10:24:46 -07007import net.onrc.onos.core.intent.ConstrainedBFSTree;
Yuta HIGUCHI1fc395e2014-05-13 14:06:28 -07008import net.onrc.onos.core.intent.Path;
Jonathan Hart472062d2014-04-03 10:56:48 -07009import net.onrc.onos.core.topology.INetworkGraphService;
10import net.onrc.onos.core.topology.Link;
11import net.onrc.onos.core.topology.LinkEvent;
12import net.onrc.onos.core.topology.NetworkGraph;
Jonathan Hart472062d2014-04-03 10:56:48 -070013import net.onrc.onos.core.topology.Switch;
14import net.onrc.onos.core.topology.serializers.LinkSerializer;
Jonathan Hart23701d12014-04-03 10:45:48 -070015import net.onrc.onos.core.util.Dpid;
Pavlin Radoslavov97af90a2014-02-25 18:34:02 -080016
17import org.codehaus.jackson.Version;
18import org.codehaus.jackson.map.ObjectMapper;
19import org.codehaus.jackson.map.module.SimpleModule;
20import org.restlet.resource.Get;
21import org.restlet.resource.ServerResource;
22import org.slf4j.Logger;
23import org.slf4j.LoggerFactory;
24
25public class NetworkGraphShortestPathResource extends ServerResource {
26
27 private static final Logger log = LoggerFactory.getLogger(NetworkGraphShortestPathResource.class);
28
29 @Get("json")
30 public String retrieve() {
Ray Milkey269ffb92014-04-03 14:43:30 -070031 INetworkGraphService networkGraphService =
32 (INetworkGraphService) getContext().getAttributes().
33 get(INetworkGraphService.class.getCanonicalName());
Pavlin Radoslavov97af90a2014-02-25 18:34:02 -080034
Ray Milkey269ffb92014-04-03 14:43:30 -070035 NetworkGraph graph = networkGraphService.getNetworkGraph();
Pavlin Radoslavov97af90a2014-02-25 18:34:02 -080036
Ray Milkey269ffb92014-04-03 14:43:30 -070037 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 Radoslavov97af90a2014-02-25 18:34:02 -080041
Ray Milkey269ffb92014-04-03 14:43:30 -070042 //
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 Radoslavov97af90a2014-02-25 18:34:02 -080050
Ray Milkey269ffb92014-04-03 14:43:30 -070051 //
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());
Ray Milkeyb29e6262014-04-09 16:02:14 -070059 if ((srcSwitch == null) || (dstSwitch == null)) {
Ray Milkey269ffb92014-04-03 14:43:30 -070060 return "";
Ray Milkeyb29e6262014-04-09 16:02:14 -070061 }
Ray Milkey269ffb92014-04-03 14:43:30 -070062 ConstrainedBFSTree bfsTree = new ConstrainedBFSTree(srcSwitch);
63 Path path = bfsTree.getPath(dstSwitch);
64 List<Link> links = new LinkedList<>();
65 for (LinkEvent linkEvent : path) {
66 Link link = graph.getLink(linkEvent.getSrc().getDpid(),
67 linkEvent.getSrc().getNumber(),
68 linkEvent.getDst().getDpid(),
69 linkEvent.getDst().getNumber());
Ray Milkeyb29e6262014-04-09 16:02:14 -070070 if (link == null) {
Ray Milkey269ffb92014-04-03 14:43:30 -070071 return "";
Ray Milkeyb29e6262014-04-09 16:02:14 -070072 }
Ray Milkey269ffb92014-04-03 14:43:30 -070073 links.add(link);
74 }
75 return mapper.writeValueAsString(links);
76 } catch (IOException e) {
77 log.error("Error writing Shortest Path to JSON", e);
78 return "";
79 } finally {
80 graph.releaseReadLock();
81 }
Pavlin Radoslavov97af90a2014-02-25 18:34:02 -080082 }
83}