Jonathan Hart | 77803d3 | 2014-08-19 08:53:00 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.matchaction.web; |
| 2 | |
| 3 | import java.io.IOException; |
| 4 | import java.util.Set; |
| 5 | |
| 6 | import net.floodlightcontroller.restserver.CustomSerializerHelper; |
| 7 | import net.onrc.onos.core.matchaction.MatchAction; |
Jonathan Hart | 763fb7e | 2014-09-02 15:06:46 -0700 | [diff] [blame] | 8 | import net.onrc.onos.core.matchaction.MatchActionFloodlightService; |
Jonathan Hart | 77803d3 | 2014-08-19 08:53:00 -0700 | [diff] [blame] | 9 | import net.onrc.onos.core.matchaction.MatchActionId; |
Jonathan Hart | 77803d3 | 2014-08-19 08:53:00 -0700 | [diff] [blame] | 10 | |
| 11 | import org.codehaus.jackson.JsonGenerator; |
| 12 | import org.codehaus.jackson.JsonProcessingException; |
| 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 match-action resources. |
| 22 | */ |
| 23 | public class MatchActionResource extends ServerResource { |
| 24 | |
| 25 | CustomSerializerHelper matchActionSerializers; |
| 26 | |
| 27 | /** |
| 28 | * Constructs a MatchActionResource. |
| 29 | * <p/> |
| 30 | * A custom serializer for {@link MatchActionId} is automatically |
| 31 | * registered, because MatchActionId can't be serialized by default. |
| 32 | */ |
| 33 | public MatchActionResource() { |
| 34 | matchActionSerializers = new CustomSerializerHelper(); |
| 35 | matchActionSerializers.addSerializer(MatchActionId.class, |
| 36 | new SerializerBase<MatchActionId>(MatchActionId.class) { |
| 37 | @Override |
| 38 | public void serialize(MatchActionId id, JsonGenerator jGen, SerializerProvider sp) |
| 39 | throws IOException, JsonProcessingException { |
| 40 | jGen.writeString(id.toString()); |
| 41 | } |
| 42 | }); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Handles REST requests for all match-action resources. |
| 47 | * |
| 48 | * @return JSON-serializable Representation of all match-action resources |
| 49 | */ |
| 50 | @Get("json") |
| 51 | public Representation retrieve() { |
Jonathan Hart | 763fb7e | 2014-09-02 15:06:46 -0700 | [diff] [blame] | 52 | MatchActionFloodlightService matchActionService = |
| 53 | (MatchActionFloodlightService) getContext().getAttributes() |
| 54 | .get(MatchActionFloodlightService.class.getCanonicalName()); |
Jonathan Hart | 77803d3 | 2014-08-19 08:53:00 -0700 | [diff] [blame] | 55 | |
| 56 | Set<MatchAction> matchActions = matchActionService.getMatchActions(); |
| 57 | |
| 58 | return matchActionSerializers.applySerializers( |
| 59 | (JacksonRepresentation<?>) toRepresentation(matchActions, null)); |
| 60 | } |
| 61 | } |