blob: 037ee88964824454b6a71e70f87cb8e4407162b8 [file] [log] [blame]
Jonathan Hart77803d32014-08-19 08:53:00 -07001package net.onrc.onos.core.matchaction.web;
2
3import java.io.IOException;
4import java.util.Set;
5
6import net.floodlightcontroller.restserver.CustomSerializerHelper;
7import net.onrc.onos.core.matchaction.MatchAction;
8import net.onrc.onos.core.matchaction.MatchActionId;
9import net.onrc.onos.core.matchaction.MatchActionService;
10
11import org.codehaus.jackson.JsonGenerator;
12import org.codehaus.jackson.JsonProcessingException;
13import org.codehaus.jackson.map.SerializerProvider;
14import org.codehaus.jackson.map.ser.std.SerializerBase;
15import org.restlet.ext.jackson.JacksonRepresentation;
16import org.restlet.representation.Representation;
17import org.restlet.resource.Get;
18import org.restlet.resource.ServerResource;
19
20/**
21 * Handles REST requests for match-action resources.
22 */
23public 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() {
52 MatchActionService matchActionService =
53 (MatchActionService) getContext().getAttributes()
54 .get(MatchActionService.class.getCanonicalName());
55
56 Set<MatchAction> matchActions = matchActionService.getMatchActions();
57
58 return matchActionSerializers.applySerializers(
59 (JacksonRepresentation<?>) toRepresentation(matchActions, null));
60 }
61}