Skip to content

Commit

Permalink
methods to serialize flows and records to xml
Browse files Browse the repository at this point in the history
  • Loading branch information
eevans committed Apr 15, 2013
1 parent 7dac592 commit 7ea2ab2
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 3 deletions.
23 changes: 21 additions & 2 deletions src/com/rackspace/flewton/AbstractRecord.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,28 @@
import org.jboss.netty.buffer.ChannelBuffer;

public abstract class AbstractRecord {
private static final String FLOWS_TAG = "flows";

public List<Flow> flows = new ArrayList<Flow>();

public AbstractRecord(ChannelBuffer buffer) {

}

/**
* Serialize record to an XML string.
*
* @return XML representation of record.
*/
public String toXmlString() {
StringBuilder out = new StringBuilder();
out.append('<').append(FLOWS_TAG).append('>');

for (Flow flow : flows) {
out.append(flow.toXmlString());
}

out.append("</").append(FLOWS_TAG).append('>');

return out.toString();
}
}
53 changes: 52 additions & 1 deletion src/com/rackspace/flewton/Flow.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@
import java.net.InetAddress;

public class Flow {
private static final String FLOW_TAG = "flow";
private static final String ATTR_TAG = "attribute";
private static final String NAME_TAG = "name";
private static final String VALUE_TAG = "value";

public InetAddress sourceAddr;
public InetAddress destAddr;
public InetAddress nextHop;
Expand All @@ -47,6 +52,52 @@ public class Flow {
public byte tos;
public short sourceAS;
public short destAS;

public long timestampCalculated;

private static String wrapAttribute(String name, Object value) {
return String.format(
"<%s><%s>%s</%s><%s>%s</%s></%s>",
ATTR_TAG,
NAME_TAG,
name,
NAME_TAG,
VALUE_TAG,
value,
VALUE_TAG,
ATTR_TAG);
}

/**
* Serialize flow to XML string.
*
* @return XML representation of flow.
*/
public String toXmlString() {
StringBuilder out = new StringBuilder();

out.append('<').append(FLOW_TAG).append('>');

out.append(wrapAttribute("sourceAddr", sourceAddr));
out.append(wrapAttribute("destAddr", destAddr));
out.append(wrapAttribute("nextHop", nextHop));
out.append(wrapAttribute("snmpIn", snmpIn));
out.append(wrapAttribute("snmpOut", snmpOut));
out.append(wrapAttribute("numPackets", numPackets));
out.append(wrapAttribute("numOctets", numOctets));
out.append(wrapAttribute("timeFirst", timeFirst));
out.append(wrapAttribute("timeLast", timeLast));
out.append(wrapAttribute("sourcePort", sourcePort));
out.append(wrapAttribute("destPort", destPort));
out.append(wrapAttribute("tcpFlags", tcpFlags));
out.append(wrapAttribute("protocol", protocol));
out.append(wrapAttribute("tos", tos));
out.append(wrapAttribute("sourceAS", sourceAS));
out.append(wrapAttribute("destAS", destAS));
out.append(wrapAttribute("timestampCalculated", timestampCalculated));

out.append("</").append(FLOW_TAG).append('>');

return out.toString();
}
}

0 comments on commit 7ea2ab2

Please sign in to comment.