Jonathan Hart | 891d050 | 2014-02-10 10:04:08 -0800 | [diff] [blame] | 1 | package net.onrc.onos.ofcontroller.networkgraph.web; |
| 2 | |
| 3 | import java.io.IOException; |
| 4 | |
| 5 | import net.onrc.onos.ofcontroller.networkgraph.INetworkGraphService; |
| 6 | import net.onrc.onos.ofcontroller.networkgraph.NetworkGraph; |
| 7 | import net.onrc.onos.ofcontroller.networkgraph.serializers.PortSerializer; |
| 8 | import net.onrc.onos.ofcontroller.networkgraph.serializers.SwitchSerializer; |
| 9 | |
| 10 | import org.codehaus.jackson.Version; |
| 11 | import org.codehaus.jackson.map.ObjectMapper; |
| 12 | import org.codehaus.jackson.map.module.SimpleModule; |
| 13 | import org.restlet.resource.Get; |
| 14 | import org.restlet.resource.ServerResource; |
| 15 | import org.slf4j.Logger; |
| 16 | import org.slf4j.LoggerFactory; |
| 17 | |
| 18 | public class NetworkGraphSwitchesResource extends ServerResource { |
| 19 | |
| 20 | private static final Logger log = LoggerFactory.getLogger(NetworkGraphSwitchesResource.class); |
| 21 | |
| 22 | @Get("json") |
| 23 | public String retrieve() { |
| 24 | INetworkGraphService networkGraphService = (INetworkGraphService) getContext().getAttributes(). |
| 25 | get(INetworkGraphService.class.getCanonicalName()); |
| 26 | |
| 27 | NetworkGraph graph = networkGraphService.getNetworkGraph(); |
| 28 | |
| 29 | ObjectMapper mapper = new ObjectMapper(); |
| 30 | SimpleModule module = new SimpleModule("module", new Version(1, 0, 0, null)); |
| 31 | module.addSerializer(new SwitchSerializer()); |
| 32 | module.addSerializer(new PortSerializer()); |
| 33 | mapper.registerModule(module); |
| 34 | |
| 35 | try { |
| 36 | return mapper.writeValueAsString(graph.getSwitches()); |
| 37 | } catch (IOException e) { |
| 38 | log.error("Error writing switch list to JSON", e); |
| 39 | return ""; |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | } |