blob: b780e5ccff14d7be51e36d010fed2fe32e2cc0b1 [file] [log] [blame]
Jonathan Hart01f2d272013-04-04 20:03:46 -07001package net.floodlightcontroller.flowcache.web;
2
3import java.io.IOException;
4
5import org.codehaus.jackson.JsonGenerator;
6import org.codehaus.jackson.JsonProcessingException;
7import org.codehaus.jackson.map.JsonSerializer;
8import org.codehaus.jackson.map.SerializerProvider;
9import org.slf4j.Logger;
10import org.slf4j.LoggerFactory;
11
12public class DatapathSummarySerializer extends JsonSerializer<String>{
13 static Logger log = LoggerFactory.getLogger(DatapathSummarySerializer.class);
14
15 @Override
16 public void serialize(String datapathSummary, JsonGenerator jGen,
17 SerializerProvider serializer) throws IOException,
18 JsonProcessingException {
19
20 String[] flowEntries = datapathSummary.split(";");
21 if (flowEntries.length < 2){
22 log.debug("datapathSummary string to short to parse: {}",
23 datapathSummary);
24 jGen.writeStartObject();
25 jGen.writeEndObject();
26 return;
27 }
28
29 String[] srcFlowEntry = flowEntries[0].split("/");
30 String[] dstFlowEntry = flowEntries[flowEntries.length - 1].split("/");
31 if (srcFlowEntry.length != 3 || dstFlowEntry.length != 3){
32 log.debug("Malformed datapathSummary string: {}", datapathSummary);
33 jGen.writeStartObject();
34 jGen.writeEndObject();
35 return;
36 }
37
38 jGen.writeStartObject();
39
40 /*
41 jGen.writeObjectFieldStart("srcPort");
42 jGen.writeObjectFieldStart("dpid");
43 jGen.writeStringField("value", srcFlowEntry[1]);
44 jGen.writeEndObject();
45 jGen.writeObjectFieldStart("port");
46 jGen.writeStringField("value", srcFlowEntry[0]);
47 jGen.writeEndObject();
48 jGen.writeEndObject();
49
50 jGen.writeObjectFieldStart("dstPort");
51 jGen.writeObjectFieldStart("dpid");
52 jGen.writeStringField("value", srcFlowEntry[1]);
53 jGen.writeEndObject();
54 jGen.writeObjectFieldStart("port");
55 jGen.writeStringField("value", srcFlowEntry[2]);
56 jGen.writeEndObject();
57 jGen.writeEndObject();
58 */
59 jGen.writeArrayFieldStart("flowEntries");
60
61 for (String flowEntryString : flowEntries){
62 String[] flowEntry = flowEntryString.split("/");
63 if (flowEntry.length != 3){
64 log.debug("Malformed datapathSummary string: {}", datapathSummary);
65 jGen.writeStartObject();
66 jGen.writeEndObject();
67 continue;
68 }
69
70 jGen.writeStartObject();
71 jGen.writeObjectFieldStart("dpid");
72 jGen.writeStringField("value", flowEntry[1]);
73 jGen.writeEndObject();
74 jGen.writeEndObject();
75 }
76
77 jGen.writeEndArray();
78
79 jGen.writeEndObject();
80 }
81
82}