blob: 423029841d552cc201be0974d566e779dbfdb4cc [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;
8import net.onrc.onos.api.newintent.IntentId;
9import net.onrc.onos.api.newintent.IntentService;
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 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() {
53 IntentService intentService =
54 (IntentService) getContext().getAttributes()
55 .get(IntentService.class.getCanonicalName());
56
57 Set<Intent> intents = intentService.getIntents();
58
59 return intentSerializers.applySerializers(
60 (JacksonRepresentation<?>) toRepresentation(intents, null));
61 }
62}