Pavlin Radoslavov | cf2b553 | 2014-05-23 18:12:23 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.topology.web; |
| 2 | |
| 3 | import net.onrc.onos.core.topology.ITopologyService; |
Yuta HIGUCHI | d92b10c | 2014-08-25 09:30:28 -0700 | [diff] [blame] | 4 | import net.onrc.onos.core.topology.MutableTopology; |
Pavlin Radoslavov | cf2b553 | 2014-05-23 18:12:23 -0700 | [diff] [blame] | 5 | |
Yuta HIGUCHI | a0dcd47 | 2014-07-06 21:06:29 -0700 | [diff] [blame] | 6 | import org.restlet.engine.io.BufferingRepresentation; |
Pavlin Radoslavov | cf2b553 | 2014-05-23 18:12:23 -0700 | [diff] [blame] | 7 | import org.restlet.representation.Representation; |
| 8 | import org.restlet.resource.Get; |
| 9 | import org.restlet.resource.ServerResource; |
| 10 | |
| 11 | /** |
| 12 | * A class to access the network topology information. |
| 13 | */ |
Yuta HIGUCHI | 2ffc3b7 | 2014-06-09 15:38:28 -0700 | [diff] [blame] | 14 | public class TopologyResource extends ServerResource { |
Pavlin Radoslavov | cf2b553 | 2014-05-23 18:12:23 -0700 | [diff] [blame] | 15 | /** |
| 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 HIGUCHI | d92b10c | 2014-08-25 09:30:28 -0700 | [diff] [blame] | 26 | MutableTopology mutableTopology = topologyService.getTopology(); |
| 27 | mutableTopology.acquireReadLock(); |
Pavlin Radoslavov | cf2b553 | 2014-05-23 18:12:23 -0700 | [diff] [blame] | 28 | try { |
Yuta HIGUCHI | d92b10c | 2014-08-25 09:30:28 -0700 | [diff] [blame] | 29 | return eval(toRepresentation(mutableTopology, null)); |
Pavlin Radoslavov | cf2b553 | 2014-05-23 18:12:23 -0700 | [diff] [blame] | 30 | } finally { |
Yuta HIGUCHI | d92b10c | 2014-08-25 09:30:28 -0700 | [diff] [blame] | 31 | mutableTopology.releaseReadLock(); |
Pavlin Radoslavov | cf2b553 | 2014-05-23 18:12:23 -0700 | [diff] [blame] | 32 | } |
| 33 | } |
Yuta HIGUCHI | a0dcd47 | 2014-07-06 21:06:29 -0700 | [diff] [blame] | 34 | |
| 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 Radoslavov | cf2b553 | 2014-05-23 18:12:23 -0700 | [diff] [blame] | 48 | } |