blob: 7406075512a1434eb89e3bdeeb6568761a91806a [file] [log] [blame]
Pavlin Radoslavovcf2b5532014-05-23 18:12:23 -07001package net.onrc.onos.core.topology.web;
2
3import net.onrc.onos.core.topology.ITopologyService;
Yuta HIGUCHId92b10c2014-08-25 09:30:28 -07004import net.onrc.onos.core.topology.MutableTopology;
Pavlin Radoslavovcf2b5532014-05-23 18:12:23 -07005
Yuta HIGUCHIa0dcd472014-07-06 21:06:29 -07006import org.restlet.engine.io.BufferingRepresentation;
Pavlin Radoslavovcf2b5532014-05-23 18:12:23 -07007import org.restlet.representation.Representation;
8import org.restlet.resource.Get;
9import org.restlet.resource.ServerResource;
10
11/**
12 * A class to access the network topology information.
13 */
Yuta HIGUCHI2ffc3b72014-06-09 15:38:28 -070014public class TopologyResource extends ServerResource {
Pavlin Radoslavovcf2b5532014-05-23 18:12:23 -070015 /**
16 * Gets the network topology information.
17 *
18 * @return a Representation of the network topology.
19 */
20 @Get("json")
21 public Representation retrieve() {
22 ITopologyService topologyService =
23 (ITopologyService) getContext().getAttributes()
24 .get(ITopologyService.class.getCanonicalName());
25
Yuta HIGUCHId92b10c2014-08-25 09:30:28 -070026 MutableTopology mutableTopology = topologyService.getTopology();
27 mutableTopology.acquireReadLock();
Pavlin Radoslavovcf2b5532014-05-23 18:12:23 -070028 try {
Yuta HIGUCHId92b10c2014-08-25 09:30:28 -070029 return eval(toRepresentation(mutableTopology, null));
Pavlin Radoslavovcf2b5532014-05-23 18:12:23 -070030 } finally {
Yuta HIGUCHId92b10c2014-08-25 09:30:28 -070031 mutableTopology.releaseReadLock();
Pavlin Radoslavovcf2b5532014-05-23 18:12:23 -070032 }
33 }
Yuta HIGUCHIa0dcd472014-07-06 21:06:29 -070034
35 /**
36 * Workaround code to trigger evaluation of Representation immediately.
37 *
38 * @param repr Representation to evaluate immediately
39 * @return Evaluated Representation
40 */
41 public static Representation eval(final Representation repr) {
42
43 BufferingRepresentation eval = new BufferingRepresentation(repr);
44 // trigger evaluation
45 eval.getSize();
46 return eval;
47 }
Pavlin Radoslavovcf2b5532014-05-23 18:12:23 -070048}