blob: fa5e93bc5937bc96538b417a23bebab37334913a [file] [log] [blame]
Pavlin Radoslavov97af90a2014-02-25 18:34:02 -08001package net.onrc.onos.ofcontroller.networkgraph.web;
2
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;
Pavlin Radoslavov97af90a2014-02-25 18:34:02 -08008import net.onrc.onos.ofcontroller.networkgraph.INetworkGraphService;
9import net.onrc.onos.ofcontroller.networkgraph.Link;
10import net.onrc.onos.ofcontroller.networkgraph.LinkEvent;
11import net.onrc.onos.ofcontroller.networkgraph.NetworkGraph;
12import net.onrc.onos.ofcontroller.networkgraph.Path;
13import net.onrc.onos.ofcontroller.networkgraph.Switch;
14import net.onrc.onos.ofcontroller.networkgraph.serializers.LinkSerializer;
15import net.onrc.onos.ofcontroller.util.Dpid;
16
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() {
31 INetworkGraphService networkGraphService =
32 (INetworkGraphService)getContext().getAttributes().
33 get(INetworkGraphService.class.getCanonicalName());
34
35 NetworkGraph graph = networkGraphService.getNetworkGraph();
36
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);
41
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);
50
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 }
80 }
81}