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.LinkSerializer; |
| 8 | |
| 9 | import org.codehaus.jackson.Version; |
| 10 | import org.codehaus.jackson.map.ObjectMapper; |
| 11 | import org.codehaus.jackson.map.module.SimpleModule; |
| 12 | import org.restlet.resource.Get; |
| 13 | import org.restlet.resource.ServerResource; |
| 14 | import org.slf4j.Logger; |
| 15 | import org.slf4j.LoggerFactory; |
| 16 | |
| 17 | public class NetworkGraphLinksResource extends ServerResource { |
| 18 | |
| 19 | private static final Logger log = LoggerFactory.getLogger(NetworkGraphLinksResource.class); |
| 20 | |
| 21 | @Get("json") |
| 22 | public String retrieve() { |
| 23 | INetworkGraphService networkGraphService = (INetworkGraphService) getContext().getAttributes(). |
| 24 | get(INetworkGraphService.class.getCanonicalName()); |
| 25 | |
| 26 | NetworkGraph graph = networkGraphService.getNetworkGraph(); |
| 27 | |
| 28 | ObjectMapper mapper = new ObjectMapper(); |
| 29 | SimpleModule module = new SimpleModule("module", new Version(1, 0, 0, null)); |
| 30 | module.addSerializer(new LinkSerializer()); |
| 31 | mapper.registerModule(module); |
| 32 | |
| 33 | try { |
| 34 | return mapper.writeValueAsString(graph.getLinks()); |
| 35 | } catch (IOException e) { |
| 36 | log.error("Error writing link list to JSON", e); |
| 37 | return ""; |
| 38 | } |
| 39 | } |
| 40 | } |