Jonathan Hart | 77803d3 | 2014-08-19 08:53:00 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.flowmanager.web; |
| 2 | |
| 3 | import java.io.IOException; |
| 4 | import java.util.Collection; |
| 5 | |
| 6 | import net.floodlightcontroller.restserver.CustomSerializerHelper; |
| 7 | import net.onrc.onos.api.flowmanager.Flow; |
| 8 | import net.onrc.onos.api.flowmanager.FlowId; |
Jonathan Hart | 68e8dd6 | 2014-08-26 18:06:23 -0700 | [diff] [blame] | 9 | import net.onrc.onos.api.flowmanager.FlowManagerFloodlightService; |
Jonathan Hart | 77803d3 | 2014-08-19 08:53:00 -0700 | [diff] [blame] | 10 | |
| 11 | import org.codehaus.jackson.JsonGenerationException; |
| 12 | import org.codehaus.jackson.JsonGenerator; |
| 13 | import org.codehaus.jackson.map.SerializerProvider; |
| 14 | import org.codehaus.jackson.map.ser.std.SerializerBase; |
| 15 | import org.restlet.ext.jackson.JacksonRepresentation; |
| 16 | import org.restlet.representation.Representation; |
| 17 | import org.restlet.resource.Get; |
| 18 | import org.restlet.resource.ServerResource; |
| 19 | |
| 20 | /** |
| 21 | * Handles REST requests for flow resources. |
| 22 | */ |
| 23 | public class FlowResource extends ServerResource { |
| 24 | |
Jonathan Hart | 68e8dd6 | 2014-08-26 18:06:23 -0700 | [diff] [blame] | 25 | private CustomSerializerHelper flowSerializers; |
Jonathan Hart | 77803d3 | 2014-08-19 08:53:00 -0700 | [diff] [blame] | 26 | |
| 27 | /** |
| 28 | * Constructs a FlowResource. |
| 29 | * <p/> |
| 30 | * A custom serializer for {@link FlowId} is automatically registered, |
| 31 | * because FlowId can't be serialized by default. |
| 32 | */ |
| 33 | public FlowResource() { |
| 34 | flowSerializers = new CustomSerializerHelper(); |
| 35 | flowSerializers.addSerializer(FlowId.class, new SerializerBase<FlowId>(FlowId.class) { |
| 36 | @Override |
| 37 | public void serialize(FlowId flowId, JsonGenerator jgen, |
| 38 | SerializerProvider provider) throws IOException, |
| 39 | JsonGenerationException { |
| 40 | jgen.writeString(flowId.toString()); |
| 41 | } |
| 42 | }); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Handles REST requests for all flow resources. |
| 47 | * |
| 48 | * @return JSON-serializable Representation of all flow resources |
| 49 | */ |
| 50 | @Get("json") |
| 51 | public Representation retrieve() { |
Jonathan Hart | 68e8dd6 | 2014-08-26 18:06:23 -0700 | [diff] [blame] | 52 | FlowManagerFloodlightService flowService = |
| 53 | (FlowManagerFloodlightService) getContext().getAttributes() |
| 54 | .get(FlowManagerFloodlightService.class.getCanonicalName()); |
Jonathan Hart | 77803d3 | 2014-08-19 08:53:00 -0700 | [diff] [blame] | 55 | |
| 56 | Collection<Flow> flows = flowService.getFlows(); |
| 57 | |
| 58 | return flowSerializers.applySerializers( |
| 59 | (JacksonRepresentation<?>) toRepresentation(flows, null)); |
| 60 | } |
Jonathan Hart | 68e8dd6 | 2014-08-26 18:06:23 -0700 | [diff] [blame] | 61 | |
Jonathan Hart | 77803d3 | 2014-08-19 08:53:00 -0700 | [diff] [blame] | 62 | } |