blob: b0f782419f405d957b8ef0639e1eb6ef437771d5 [file] [log] [blame]
Yuta HIGUCHIa7f1cdd2014-06-09 15:05:20 -07001package net.onrc.onos.core.topology.web.serializers;
TeruU65bc5ff2014-04-23 22:22:32 -07002
3import java.io.IOException;
4
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -07005import net.onrc.onos.core.topology.Host;
TeruU65bc5ff2014-04-23 22:22:32 -07006import net.onrc.onos.core.topology.Port;
Yuta HIGUCHI1222ac52014-07-09 16:50:28 -07007import net.onrc.onos.core.topology.TopologyElement;
TeruU65bc5ff2014-04-23 22:22:32 -07008
TeruU65bc5ff2014-04-23 22:22:32 -07009import org.codehaus.jackson.JsonGenerator;
10import org.codehaus.jackson.map.SerializerProvider;
11import org.codehaus.jackson.map.ser.std.SerializerBase;
12
Ray Milkey92897212014-07-21 10:33:16 -070013/**
Pavlin Radoslavov17660382014-07-24 21:21:07 -070014 * JSON serializer for Host objects.
Ray Milkey92897212014-07-21 10:33:16 -070015 */
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -070016public class HostSerializer extends SerializerBase<Host> {
Ray Milkey92897212014-07-21 10:33:16 -070017 /**
Pavlin Radoslavov17660382014-07-24 21:21:07 -070018 * Default constructor.
Ray Milkey92897212014-07-21 10:33:16 -070019 */
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -070020 public HostSerializer() {
21 super(Host.class);
TeruU65bc5ff2014-04-23 22:22:32 -070022 }
23
Pavlin Radoslavov17660382014-07-24 21:21:07 -070024 /**
25 * Serializes a Host object in JSON.
26 *
27 * @param host the Host that is being converted to JSON
28 * @param jsonGenerator generator to place the serialized JSON into
29 * @param serializerProvider unused but required for method override
30 * @throws IOException if the JSON serialization process fails
31 */
TeruU65bc5ff2014-04-23 22:22:32 -070032 @Override
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -070033 public void serialize(Host host, JsonGenerator jsonGenerator,
Pavlin Radoslavov17660382014-07-24 21:21:07 -070034 SerializerProvider serializerProvider)
35 throws IOException {
Pavlin Radoslavov5cf1fe02014-07-03 22:52:25 -070036
37 //
38 // TODO: For now, the JSON format of the serialized output should
Yuta HIGUCHI93d35ea2014-08-31 23:26:13 -070039 // be same as the JSON format of the corresponding class HostData.
Pavlin Radoslavov5cf1fe02014-07-03 22:52:25 -070040 // In the future, we will use a single serializer.
41 //
42
TeruU65bc5ff2014-04-23 22:22:32 -070043 jsonGenerator.writeStartObject();
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -070044 jsonGenerator.writeStringField(TopologyElement.TYPE, host.getType());
45 jsonGenerator.writeStringField("mac", host.getMacAddress().toString());
TeruU65bc5ff2014-04-23 22:22:32 -070046 jsonGenerator.writeFieldName("attachmentPoints");
47 jsonGenerator.writeStartArray();
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -070048 for (Port port : host.getAttachmentPoints()) {
Yuta HIGUCHIcd14dda2014-07-24 09:57:22 -070049 jsonGenerator.writeObject(port.getSwitchPort());
TeruU65bc5ff2014-04-23 22:22:32 -070050 }
51 jsonGenerator.writeEndArray();
Pavlin Radoslavov17660382014-07-24 21:21:07 -070052 //
53 // NOTE: Class Host itself doesn't have stringAttributes.
Yuta HIGUCHI93d35ea2014-08-31 23:26:13 -070054 // Adding empty object for now for consistency with HostDataSerializer
Pavlin Radoslavov17660382014-07-24 21:21:07 -070055 //
56 jsonGenerator.writeObjectFieldStart("stringAttributes");
57 /*
58 for (Entry<String, String> entry : host.getAllStringAttributes().entrySet()) {
59 jsonGenerator.writeStringField(entry.getKey(), entry.getValue());
60 }
61 */
62 jsonGenerator.writeEndObject(); // stringAttributes
TeruU65bc5ff2014-04-23 22:22:32 -070063 jsonGenerator.writeEndObject();
64 }
TeruU65bc5ff2014-04-23 22:22:32 -070065}