blob: e2d7f8efbacbd6f3a97d1c90b54271f1ec5e1fc7 [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 jGen.writeStartObject();
Fahad Naeem Khandf197552014-10-10 14:59:00 -070042 //System.out.println("flowstats:\n\n\n"+ flowStatsEntry);
Fahad Naeem Khand89448d2014-10-06 18:40:45 -070043 jGen.writeNumberField("byteCount", flowStatsEntry.getByteCount().getValue());
Fahad Naeem Khanf35e4f62014-10-07 16:45:39 -070044 jGen.writeNumberField("packetCount", flowStatsEntry.getPacketCount().getValue());
Fahad Naeem Khan0c1c7b32014-10-07 16:22:23 -070045 jGen.writeNumberField("priority", flowStatsEntry.getPriority());
Fahad Naeem Khand89448d2014-10-06 18:40:45 -070046 jGen.writeNumberField("cookie", flowStatsEntry.getCookie().getValue());
47 jGen.writeNumberField("durationNsec", flowStatsEntry.getDurationNsec());
48 jGen.writeNumberField("durationSec", flowStatsEntry.getDurationSec());
Fahad Naeem Khandf197552014-10-10 14:59:00 -070049 jGen.writeObjectField("flags", flowStatsEntry.getFlags());
Fahad Naeem Khand89448d2014-10-06 18:40:45 -070050 jGen.writeNumberField("hardTimeout", flowStatsEntry.getHardTimeout());
51 jGen.writeNumberField("idleTimeout", flowStatsEntry.getIdleTimeout());
Fahad Naeem Khandcbab082014-10-13 13:44:27 -070052 jGen.writeFieldName("match");
Fahad Naeem Khan0c1c7b32014-10-07 16:22:23 -070053 jGen.writeStartObject();
Fahad Naeem Khand89448d2014-10-06 18:40:45 -070054 Iterator<OFOxm<?>> match= matches.iterator();
55 while(match.hasNext()){
56 OFOxm<?> matchGeneric = match.next();
57 if (matchGeneric.getMatchField().id == MatchFields.IPV4_DST){
Fahad Naeem Khandf197552014-10-10 14:59:00 -070058 jGen.writeStringField("networkDestination", matchGeneric.getValue().toString()
59 +"/"
60 +(matchGeneric.isMasked() ?
61 OFFlowStatsEntryModSerializer.covertToMask(
62 IPv4.toIPv4Address(
Fahad Naeem Khanf59c2852014-10-10 18:52:26 -070063 matchGeneric.getMask().toString())):"32"));
Fahad Naeem Khand89448d2014-10-06 18:40:45 -070064 }
Fahad Naeem Khan0c1c7b32014-10-07 16:22:23 -070065 else if (matchGeneric.getMatchField().id == MatchFields.IPV4_SRC){
Fahad Naeem Khandf197552014-10-10 14:59:00 -070066 jGen.writeStringField("networkSource", matchGeneric.getValue().toString()
67 +"/"
68 +(matchGeneric.isMasked() ?
69 OFFlowStatsEntryModSerializer.covertToMask(
70 IPv4.toIPv4Address(
Fahad Naeem Khanf59c2852014-10-10 18:52:26 -070071 matchGeneric.getMask().toString())):"32"));
Fahad Naeem Khan0c1c7b32014-10-07 16:22:23 -070072 }
73 else if (matchGeneric.getMatchField().id == MatchFields.ETH_DST){
74 jGen.writeStringField("dataLayerDestination", matchGeneric.getValue().toString());
75 }
76 else if (matchGeneric.getMatchField().id == MatchFields.ETH_SRC){
77 jGen.writeStringField("dataLayerSource", matchGeneric.getValue().toString());
78 }
79 else if (matchGeneric.getMatchField().id == MatchFields.ETH_TYPE){
Fahad Naeem Khandf197552014-10-10 14:59:00 -070080 jGen.writeNumberField("dataLayerType", Integer.decode(matchGeneric.getValue().toString()));
Fahad Naeem Khan0c1c7b32014-10-07 16:22:23 -070081 }
82 else if (matchGeneric.getMatchField().id == MatchFields.IN_PORT){
83 jGen.writeNumberField("inputPort", Integer.parseInt(matchGeneric.getValue().toString()));
84 }
85 else if (matchGeneric.getMatchField().id == MatchFields.MPLS_TC){
Fahad Naeem Khandf197552014-10-10 14:59:00 -070086 jGen.writeNumberField("mplsTc", Integer.decode(matchGeneric.getValue().toString()));
Fahad Naeem Khan0c1c7b32014-10-07 16:22:23 -070087 }
Fahad Naeem Khanc517ae92014-10-13 14:57:00 -070088 else if (matchGeneric.getMatchField().id == MatchFields.MPLS_BOS){
89 jGen.writeStringField("mplsBos", "True");
90 }
Fahad Naeem Khan0c1c7b32014-10-07 16:22:23 -070091 else if (matchGeneric.getMatchField().id == MatchFields.MPLS_LABEL){
Fahad Naeem Khandf197552014-10-10 14:59:00 -070092 jGen.writeNumberField("mplsLabel", Integer.decode(matchGeneric.getValue().toString()));
Fahad Naeem Khan0c1c7b32014-10-07 16:22:23 -070093 }
94 else if (matchGeneric.getMatchField().id == MatchFields.IP_PROTO){
95 jGen.writeNumberField("networkProtocol", Integer.parseInt(matchGeneric.getValue().toString()));
96 }
97 //TODO: Ask Saurav about the implementation of tcp and udp.
Fahad Naeem Khandf197552014-10-10 14:59:00 -070098 else if (matchGeneric.getMatchField().id == MatchFields.TCP_DST
99 || matchGeneric.getMatchField().id == MatchFields.UDP_DST){
Fahad Naeem Khan0c1c7b32014-10-07 16:22:23 -0700100 jGen.writeNumberField("transportDestination", Integer.parseInt(matchGeneric.getValue().toString()));
101 }
Fahad Naeem Khandf197552014-10-10 14:59:00 -0700102 else if (matchGeneric.getMatchField().id == MatchFields.TCP_SRC
103 || matchGeneric.getMatchField().id == MatchFields.UDP_SRC){
Fahad Naeem Khan0c1c7b32014-10-07 16:22:23 -0700104 jGen.writeNumberField("transportSource", Integer.parseInt(matchGeneric.getValue().toString()));
105 }
Fahad Naeem Khand89448d2014-10-06 18:40:45 -0700106 }
Fahad Naeem Khan0c1c7b32014-10-07 16:22:23 -0700107 jGen.writeEndObject();
Fahad Naeem Khandcbab082014-10-13 13:44:27 -0700108
Fahad Naeem Khandf197552014-10-10 14:59:00 -0700109 jGen.writeFieldName("instructions");
110 jGen.writeStartArray();
Fahad Naeem Khandcbab082014-10-13 13:44:27 -0700111 jGen.writeStartObject();
Fahad Naeem Khandf197552014-10-10 14:59:00 -0700112 List<OFAction> actions = null;
Fahad Naeem Khan0c1c7b32014-10-07 16:22:23 -0700113 for (OFInstruction instruction: instructions){
Fahad Naeem Khanf59c2852014-10-10 18:52:26 -0700114
Fahad Naeem Khandf197552014-10-10 14:59:00 -0700115 if(instruction.getType().equals(OFInstructionType.APPLY_ACTIONS)){
116 actions = ((OFInstructionApplyActions)instruction).getActions();
Fahad Naeem Khan0c1c7b32014-10-07 16:22:23 -0700117 }
Fahad Naeem Khandf197552014-10-10 14:59:00 -0700118 else if(instruction.getType().equals(OFInstructionType.WRITE_ACTIONS)){
119 actions = ((OFInstructionWriteActions)instruction).getActions();
120 }
Fahad Naeem Khanf59c2852014-10-10 18:52:26 -0700121 else if(instruction.getType().equals(OFInstructionType.GOTO_TABLE)){
Fahad Naeem Khandcbab082014-10-13 13:44:27 -0700122
Fahad Naeem Khanf59c2852014-10-10 18:52:26 -0700123 jGen.writeFieldName(instruction.getType().name());
124 jGen.writeStartObject();
125 jGen.writeNumberField("tableId"
126 , ((OFInstructionGotoTable)instruction).getTableId().getValue());
127 jGen.writeEndObject();
128 continue;
Fahad Naeem Khandcbab082014-10-13 13:44:27 -0700129 }//*/
Fahad Naeem Khandf197552014-10-10 14:59:00 -0700130 else{
131 continue;
132 }
Fahad Naeem Khandcbab082014-10-13 13:44:27 -0700133 jGen.writeObjectFieldStart(instruction.getType().name());
Fahad Naeem Khandf197552014-10-10 14:59:00 -0700134 for (OFAction action : actions){
135 if (action.getType().equals(OFActionType.GROUP)){
136 jGen.writeNumberField("group", ((OFActionGroup)action).getGroup().getGroupNumber());
137 }
138 else if (action.getType().equals(OFActionType.OUTPUT)){
Fahad Naeem Khanf59c2852014-10-10 18:52:26 -0700139 if (((OFActionOutput)action).getPort().getPortNumber() == -3){
140 //Controller port
141 jGen.writeStringField("output", "CONTROLLER");
142 }
143 else{
144 jGen.writeNumberField("output", ((OFActionOutput)action).getPort().getPortNumber());
145 }
Fahad Naeem Khandf197552014-10-10 14:59:00 -0700146 }
147 else if(action.getType().compareTo(OFActionType.POP_MPLS) == 0
148 || action.getType().compareTo(OFActionType.COPY_TTL_IN) == 0
149 || action.getType().compareTo(OFActionType.COPY_TTL_OUT) == 0
150 || action.getType().compareTo(OFActionType.DEC_MPLS_TTL) == 0
151 || action.getType().compareTo(OFActionType.DEC_NW_TTL) == 0
152 || action.getType().compareTo(OFActionType.POP_PBB) == 0
153 || action.getType().compareTo(OFActionType.POP_VLAN) == 0){
154 jGen.writeStringField(action.getType().name(), "True");
155 }
156 else if(action.getType().compareTo(OFActionType.COPY_TTL_IN) == 0){
157 jGen.writeStringField("POP_MPLS", "True");
158 }
159 else if (action.getType().equals(OFActionType.SET_FIELD)){
160 //TODO Support for more setFields
161 if (((OFActionSetField)action).getField().toString().contains("OFOxmEthSrcVer13")){
162 jGen.writeStringField("SET_DL_SRC", ((OFActionSetField)action).getField().getValue().toString());
163 }
164 else if (((OFActionSetField)action).getField().toString().contains("OFOxmEthDstVer13")){
165 jGen.writeStringField("SET_DL_DST", ((OFActionSetField)action).getField().getValue().toString());
166 }
167 else if (((OFActionSetField)action).getField().toString().contains("OFOxmNwDstVer13")){
168 jGen.writeStringField("SET_NW_SRC", ((OFActionSetField)action).getField().getValue().toString());
169 }
170 else if (((OFActionSetField)action).getField().toString().contains("OFOxmNwDstVer13")){
171 jGen.writeStringField("SET_NW_DST", ((OFActionSetField)action).getField().getValue().toString());
172 }
173 else if (((OFActionSetField)action).getField().toString().contains("OFOxmMplsLabelVer13")){
174 jGen.writeStringField("PUSH_MPLS", ((OFActionSetField)action).getField().getValue().toString());
175 }
176 }
Fahad Naeem Khan0c1c7b32014-10-07 16:22:23 -0700177 }
178 jGen.writeEndObject();
Fahad Naeem Khan0c1c7b32014-10-07 16:22:23 -0700179 }
Fahad Naeem Khand89448d2014-10-06 18:40:45 -0700180 jGen.writeEndObject();
Fahad Naeem Khandf197552014-10-10 14:59:00 -0700181 jGen.writeEndArray();
Fahad Naeem Khan0c1c7b32014-10-07 16:22:23 -0700182 jGen.writeEndObject();
Fahad Naeem Khan8a8daf22014-10-06 14:07:43 -0700183 }
Fahad Naeem Khanf59c2852014-10-10 18:52:26 -0700184 /**
185 * Get the number of 1's in the 32bit integer
186 * Use full to convert wildcard mask(x.x.x.x) to \x notation
187 * for example
188 * ("0.0.0.255") to int to "/8" or
189 * ("0.0.255.255") to int to "/16"
190 * @param x
191 * @return
192 */
193
Fahad Naeem Khandf197552014-10-10 14:59:00 -0700194 public static int covertToMask(int x) {
Fahad Naeem Khanf59c2852014-10-10 18:52:26 -0700195 x = x - ((x >>> 1) & 0x55555555);
196 x = (x & 0x33333333) + ((x >>> 2) & 0x33333333);
197 x = (x + (x >>> 4)) & 0x0F0F0F0F;
198 x = x + (x >>> 8);
199 x = x + (x >>> 16);
200 return 32 - (x & 0x0000003F);
Fahad Naeem Khandf197552014-10-10 14:59:00 -0700201 }
Fahad Naeem Khan8a8daf22014-10-06 14:07:43 -0700202
203}