Jonathan Hart | 01f2d27 | 2013-04-04 20:03:46 -0700 | [diff] [blame^] | 1 | package net.floodlightcontroller.flowcache.web; |
| 2 | |
| 3 | import java.io.IOException; |
| 4 | |
| 5 | import org.codehaus.jackson.JsonGenerator; |
| 6 | import org.codehaus.jackson.JsonProcessingException; |
| 7 | import org.codehaus.jackson.map.JsonSerializer; |
| 8 | import org.codehaus.jackson.map.SerializerProvider; |
| 9 | import org.slf4j.Logger; |
| 10 | import org.slf4j.LoggerFactory; |
| 11 | |
| 12 | public 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 | } |