blob: b4cd133485f085e5a08eea7d225e3b9b89f29f5f [file] [log] [blame]
Jonathan Hart77803d32014-08-19 08:53:00 -07001package net.onrc.onos.core.newintent.web;
2
3import java.io.IOException;
4import java.util.Set;
5
6import net.floodlightcontroller.restserver.CustomSerializerHelper;
7import net.onrc.onos.api.newintent.Intent;
Jonathan Hart68e8dd62014-08-26 18:06:23 -07008import net.onrc.onos.api.newintent.IntentFloodlightService;
Jonathan Hart77803d32014-08-19 08:53:00 -07009import net.onrc.onos.api.newintent.IntentId;
Jonathan Hart77803d32014-08-19 08:53:00 -070010
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 intent resources.
22 */
23public class IntentResource extends ServerResource {
24
25 CustomSerializerHelper intentSerializers;
26
27 /**
28 * Constructs an IntentResource.
29 * <p/>
30 * A custom serializer for {@link IntentId} is automatically registered,
31 * because IntentId can't be serialized by default.
32 */
33 public IntentResource() {
34 intentSerializers = new CustomSerializerHelper();
35 intentSerializers.addSerializer(IntentId.class,
36 new SerializerBase<IntentId>(IntentId.class) {
37 @Override
38 public void serialize(IntentId id, JsonGenerator jGen,
39 SerializerProvider sp) throws IOException,
40 JsonProcessingException {
41 jGen.writeString(id.toString());
42 }
43 });
44 }
45
46 /**
47 * Handles REST requests for all intent resources.
48 *
49 * @return JSON-serializable Representation of all intent resources
50 */
51 @Get("json")
52 public Representation retrieve() {
Jonathan Hart68e8dd62014-08-26 18:06:23 -070053 IntentFloodlightService intentService =
54 (IntentFloodlightService) getContext().getAttributes()
55 .get(IntentFloodlightService.class.getCanonicalName());
Jonathan Hart77803d32014-08-19 08:53:00 -070056
57 Set<Intent> intents = intentService.getIntents();
58
59 return intentSerializers.applySerializers(
60 (JacksonRepresentation<?>) toRepresentation(intents, null));
61 }
62}