blob: 4a83230262aee316b37cd4974fe5dcb8927cda17 [file] [log] [blame]
Fahad Naeem Khan8a8daf22014-10-06 14:07:43 -07001package net.floodlightcontroller.core.web.serializers;
2
3import java.io.IOException;
Fahad Naeem Khand89448d2014-10-06 18:40:45 -07004import java.util.Iterator;
Fahad Naeem Khan0c1c7b32014-10-07 16:22:23 -07005import java.util.List;
Fahad Naeem Khand89448d2014-10-06 18:40:45 -07006import java.util.Set;
Fahad Naeem Khan8a8daf22014-10-06 14:07:43 -07007
8import net.floodlightcontroller.core.web.OFFlowStatsEntryMod;
Fahad Naeem Khandf197552014-10-10 14:59:00 -07009import net.onrc.onos.core.packet.IPv4;
Fahad Naeem Khan8a8daf22014-10-06 14:07:43 -070010
Fahad Naeem Khan0c1c7b32014-10-07 16:22:23 -070011import org.projectfloodlight.openflow.protocol.action.*;
Fahad Naeem Khan8a8daf22014-10-06 14:07:43 -070012import org.codehaus.jackson.JsonGenerationException;
13import org.codehaus.jackson.JsonGenerator;
14import org.codehaus.jackson.map.SerializerProvider;
15import org.codehaus.jackson.map.ser.std.SerializerBase;
Fahad Naeem Khan0c1c7b32014-10-07 16:22:23 -070016import org.projectfloodlight.openflow.protocol.OFActionType;
Fahad Naeem Khand89448d2014-10-06 18:40:45 -070017import org.projectfloodlight.openflow.protocol.OFFlowModFlags;
18import org.projectfloodlight.openflow.protocol.OFFlowStatsEntry;
Fahad Naeem Khan0c1c7b32014-10-07 16:22:23 -070019import org.projectfloodlight.openflow.protocol.OFInstructionType;
Fahad Naeem Khand89448d2014-10-06 18:40:45 -070020import org.projectfloodlight.openflow.protocol.OFMatchV3;
21import org.projectfloodlight.openflow.protocol.OFOxmList;
Fahad Naeem Khan0c1c7b32014-10-07 16:22:23 -070022import org.projectfloodlight.openflow.protocol.action.OFAction;
23import org.projectfloodlight.openflow.protocol.instruction.*;
Fahad Naeem Khand89448d2014-10-06 18:40:45 -070024import org.projectfloodlight.openflow.protocol.match.MatchFields;
25import org.projectfloodlight.openflow.protocol.oxm.OFOxm;
Fahad Naeem Khan8a8daf22014-10-06 14:07:43 -070026
27public class OFFlowStatsEntryModSerializer extends SerializerBase<OFFlowStatsEntryMod> {
28
29 protected OFFlowStatsEntryModSerializer(){
30 super(OFFlowStatsEntryMod.class);
31 }
32
33 @Override
34 public void serialize(OFFlowStatsEntryMod FlowStatsEntryMod, JsonGenerator jGen,
35 SerializerProvider sp) throws IOException,
36 JsonGenerationException {
Fahad Naeem Khand89448d2014-10-06 18:40:45 -070037 OFFlowStatsEntry flowStatsEntry = FlowStatsEntryMod.getFlowStatsEntry();
38 OFOxmList matches = ((OFMatchV3)flowStatsEntry.getMatch()).getOxmList();
Fahad Naeem Khan0c1c7b32014-10-07 16:22:23 -070039
40 List<OFInstruction> instructions = flowStatsEntry.getInstructions();
Fahad Naeem Khand89448d2014-10-06 18:40:45 -070041 Set<OFFlowModFlags> flags = flowStatsEntry.getFlags();
42 jGen.writeStartObject();
Fahad Naeem Khandf197552014-10-10 14:59:00 -070043 //System.out.println("flowstats:\n\n\n"+ flowStatsEntry);
Fahad Naeem Khand89448d2014-10-06 18:40:45 -070044 jGen.writeNumberField("byteCount", flowStatsEntry.getByteCount().getValue());
Fahad Naeem Khanf35e4f62014-10-07 16:45:39 -070045 jGen.writeNumberField("packetCount", flowStatsEntry.getPacketCount().getValue());
Fahad Naeem Khan0c1c7b32014-10-07 16:22:23 -070046 jGen.writeNumberField("priority", flowStatsEntry.getPriority());
Fahad Naeem Khand89448d2014-10-06 18:40:45 -070047 jGen.writeNumberField("cookie", flowStatsEntry.getCookie().getValue());
48 jGen.writeNumberField("durationNsec", flowStatsEntry.getDurationNsec());
49 jGen.writeNumberField("durationSec", flowStatsEntry.getDurationSec());
Fahad Naeem Khandf197552014-10-10 14:59:00 -070050 jGen.writeObjectField("flags", flowStatsEntry.getFlags());
Fahad Naeem Khand89448d2014-10-06 18:40:45 -070051 jGen.writeNumberField("hardTimeout", flowStatsEntry.getHardTimeout());
52 jGen.writeNumberField("idleTimeout", flowStatsEntry.getIdleTimeout());
53 jGen.writeArrayFieldStart("match");
Fahad Naeem Khan0c1c7b32014-10-07 16:22:23 -070054 jGen.writeStartObject();
Fahad Naeem Khand89448d2014-10-06 18:40:45 -070055 Iterator<OFOxm<?>> match= matches.iterator();
56 while(match.hasNext()){
57 OFOxm<?> matchGeneric = match.next();
58 if (matchGeneric.getMatchField().id == MatchFields.IPV4_DST){
Fahad Naeem Khandf197552014-10-10 14:59:00 -070059 jGen.writeStringField("networkDestination", matchGeneric.getValue().toString()
60 +"/"
61 +(matchGeneric.isMasked() ?
62 OFFlowStatsEntryModSerializer.covertToMask(
63 IPv4.toIPv4Address(
64 matchGeneric.getMask().toString())):"0"));
Fahad Naeem Khand89448d2014-10-06 18:40:45 -070065 }
Fahad Naeem Khan0c1c7b32014-10-07 16:22:23 -070066 else if (matchGeneric.getMatchField().id == MatchFields.IPV4_SRC){
Fahad Naeem Khandf197552014-10-10 14:59:00 -070067 jGen.writeStringField("networkSource", matchGeneric.getValue().toString()
68 +"/"
69 +(matchGeneric.isMasked() ?
70 OFFlowStatsEntryModSerializer.covertToMask(
71 IPv4.toIPv4Address(
72 matchGeneric.getMask().toString())):"0"));
Fahad Naeem Khan0c1c7b32014-10-07 16:22:23 -070073 }
74 else if (matchGeneric.getMatchField().id == MatchFields.ETH_DST){
75 jGen.writeStringField("dataLayerDestination", matchGeneric.getValue().toString());
76 }
77 else if (matchGeneric.getMatchField().id == MatchFields.ETH_SRC){
78 jGen.writeStringField("dataLayerSource", matchGeneric.getValue().toString());
79 }
80 else if (matchGeneric.getMatchField().id == MatchFields.ETH_TYPE){
Fahad Naeem Khandf197552014-10-10 14:59:00 -070081 jGen.writeNumberField("dataLayerType", Integer.decode(matchGeneric.getValue().toString()));
Fahad Naeem Khan0c1c7b32014-10-07 16:22:23 -070082 }
83 else if (matchGeneric.getMatchField().id == MatchFields.IN_PORT){
84 jGen.writeNumberField("inputPort", Integer.parseInt(matchGeneric.getValue().toString()));
85 }
86 else if (matchGeneric.getMatchField().id == MatchFields.MPLS_TC){
Fahad Naeem Khandf197552014-10-10 14:59:00 -070087 jGen.writeNumberField("mplsTc", Integer.decode(matchGeneric.getValue().toString()));
Fahad Naeem Khan0c1c7b32014-10-07 16:22:23 -070088 }
89 else if (matchGeneric.getMatchField().id == MatchFields.MPLS_LABEL){
Fahad Naeem Khandf197552014-10-10 14:59:00 -070090 jGen.writeNumberField("mplsLabel", Integer.decode(matchGeneric.getValue().toString()));
Fahad Naeem Khan0c1c7b32014-10-07 16:22:23 -070091 }
92 else if (matchGeneric.getMatchField().id == MatchFields.IP_PROTO){
93 jGen.writeNumberField("networkProtocol", Integer.parseInt(matchGeneric.getValue().toString()));
94 }
95 //TODO: Ask Saurav about the implementation of tcp and udp.
Fahad Naeem Khandf197552014-10-10 14:59:00 -070096 else if (matchGeneric.getMatchField().id == MatchFields.TCP_DST
97 || matchGeneric.getMatchField().id == MatchFields.UDP_DST){
Fahad Naeem Khan0c1c7b32014-10-07 16:22:23 -070098 jGen.writeNumberField("transportDestination", Integer.parseInt(matchGeneric.getValue().toString()));
99 }
Fahad Naeem Khandf197552014-10-10 14:59:00 -0700100 else if (matchGeneric.getMatchField().id == MatchFields.TCP_SRC
101 || matchGeneric.getMatchField().id == MatchFields.UDP_SRC){
Fahad Naeem Khan0c1c7b32014-10-07 16:22:23 -0700102 jGen.writeNumberField("transportSource", Integer.parseInt(matchGeneric.getValue().toString()));
103 }
Fahad Naeem Khand89448d2014-10-06 18:40:45 -0700104 }
Fahad Naeem Khan0c1c7b32014-10-07 16:22:23 -0700105 jGen.writeEndObject();
Fahad Naeem Khand89448d2014-10-06 18:40:45 -0700106 jGen.writeEndArray();
Fahad Naeem Khandf197552014-10-10 14:59:00 -0700107 jGen.writeFieldName("instructions");
108 jGen.writeStartArray();
109 List<OFAction> actions = null;
Fahad Naeem Khan0c1c7b32014-10-07 16:22:23 -0700110 for (OFInstruction instruction: instructions){
Fahad Naeem Khandf197552014-10-10 14:59:00 -0700111 if(instruction.getType().equals(OFInstructionType.APPLY_ACTIONS)){
112 actions = ((OFInstructionApplyActions)instruction).getActions();
Fahad Naeem Khan0c1c7b32014-10-07 16:22:23 -0700113 }
Fahad Naeem Khandf197552014-10-10 14:59:00 -0700114 else if(instruction.getType().equals(OFInstructionType.WRITE_ACTIONS)){
115 actions = ((OFInstructionWriteActions)instruction).getActions();
116 }
117 else{
118 continue;
119 }
120 jGen.writeStartObject();
121 jGen.writeFieldName(instruction.getType().name());
122 jGen.writeStartObject();
123 for (OFAction action : actions){
124 if (action.getType().equals(OFActionType.GROUP)){
125 jGen.writeNumberField("group", ((OFActionGroup)action).getGroup().getGroupNumber());
126 }
127 else if (action.getType().equals(OFActionType.OUTPUT)){
128 jGen.writeNumberField("group", ((OFActionOutput)action).getPort().getPortNumber());
129 }
130 else if(action.getType().compareTo(OFActionType.POP_MPLS) == 0
131 || action.getType().compareTo(OFActionType.COPY_TTL_IN) == 0
132 || action.getType().compareTo(OFActionType.COPY_TTL_OUT) == 0
133 || action.getType().compareTo(OFActionType.DEC_MPLS_TTL) == 0
134 || action.getType().compareTo(OFActionType.DEC_NW_TTL) == 0
135 || action.getType().compareTo(OFActionType.POP_PBB) == 0
136 || action.getType().compareTo(OFActionType.POP_VLAN) == 0){
137 jGen.writeStringField(action.getType().name(), "True");
138 }
139 else if(action.getType().compareTo(OFActionType.COPY_TTL_IN) == 0){
140 jGen.writeStringField("POP_MPLS", "True");
141 }
142 else if (action.getType().equals(OFActionType.SET_FIELD)){
143 //TODO Support for more setFields
144 if (((OFActionSetField)action).getField().toString().contains("OFOxmEthSrcVer13")){
145 jGen.writeStringField("SET_DL_SRC", ((OFActionSetField)action).getField().getValue().toString());
146 }
147 else if (((OFActionSetField)action).getField().toString().contains("OFOxmEthDstVer13")){
148 jGen.writeStringField("SET_DL_DST", ((OFActionSetField)action).getField().getValue().toString());
149 }
150 else if (((OFActionSetField)action).getField().toString().contains("OFOxmNwDstVer13")){
151 jGen.writeStringField("SET_NW_SRC", ((OFActionSetField)action).getField().getValue().toString());
152 }
153 else if (((OFActionSetField)action).getField().toString().contains("OFOxmNwDstVer13")){
154 jGen.writeStringField("SET_NW_DST", ((OFActionSetField)action).getField().getValue().toString());
155 }
156 else if (((OFActionSetField)action).getField().toString().contains("OFOxmMplsLabelVer13")){
157 jGen.writeStringField("PUSH_MPLS", ((OFActionSetField)action).getField().getValue().toString());
158 }
159 }
Fahad Naeem Khan0c1c7b32014-10-07 16:22:23 -0700160 }
161 jGen.writeEndObject();
Fahad Naeem Khan0c1c7b32014-10-07 16:22:23 -0700162 }
Fahad Naeem Khand89448d2014-10-06 18:40:45 -0700163 jGen.writeEndObject();
Fahad Naeem Khandf197552014-10-10 14:59:00 -0700164 jGen.writeEndArray();
Fahad Naeem Khan0c1c7b32014-10-07 16:22:23 -0700165 jGen.writeEndObject();
Fahad Naeem Khan8a8daf22014-10-06 14:07:43 -0700166 }
Fahad Naeem Khandf197552014-10-10 14:59:00 -0700167 public static int covertToMask(int x) {
168 x = x - ((x >>> 1) & 0x55555555);
169 x = (x & 0x33333333) + ((x >>> 2) & 0x33333333);
170 x = (x + (x >>> 4)) & 0x0F0F0F0F;
171 x = x + (x >>> 8);
172 x = x + (x >>> 16);
173 return x & 0x0000003F;
174 }
Fahad Naeem Khan8a8daf22014-10-06 14:07:43 -0700175
176}