blob: 02da751485db12f4df30a661c6daa97d730f1a88 [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 Khand89448d2014-10-06 18:40:45 -070042 jGen.writeNumberField("byteCount", flowStatsEntry.getByteCount().getValue());
Fahad Naeem Khanf35e4f62014-10-07 16:45:39 -070043 jGen.writeNumberField("packetCount", flowStatsEntry.getPacketCount().getValue());
Fahad Naeem Khan0c1c7b32014-10-07 16:22:23 -070044 jGen.writeNumberField("priority", flowStatsEntry.getPriority());
Fahad Naeem Khand89448d2014-10-06 18:40:45 -070045 jGen.writeNumberField("cookie", flowStatsEntry.getCookie().getValue());
46 jGen.writeNumberField("durationNsec", flowStatsEntry.getDurationNsec());
47 jGen.writeNumberField("durationSec", flowStatsEntry.getDurationSec());
Fahad Naeem Khandf197552014-10-10 14:59:00 -070048 jGen.writeObjectField("flags", flowStatsEntry.getFlags());
Fahad Naeem Khand89448d2014-10-06 18:40:45 -070049 jGen.writeNumberField("hardTimeout", flowStatsEntry.getHardTimeout());
50 jGen.writeNumberField("idleTimeout", flowStatsEntry.getIdleTimeout());
Fahad Naeem Khandcbab082014-10-13 13:44:27 -070051 jGen.writeFieldName("match");
Fahad Naeem Khan0c1c7b32014-10-07 16:22:23 -070052 jGen.writeStartObject();
Fahad Naeem Khand89448d2014-10-06 18:40:45 -070053 Iterator<OFOxm<?>> match= matches.iterator();
54 while(match.hasNext()){
55 OFOxm<?> matchGeneric = match.next();
56 if (matchGeneric.getMatchField().id == MatchFields.IPV4_DST){
Fahad Naeem Khandf197552014-10-10 14:59:00 -070057 jGen.writeStringField("networkDestination", matchGeneric.getValue().toString()
58 +"/"
59 +(matchGeneric.isMasked() ?
60 OFFlowStatsEntryModSerializer.covertToMask(
61 IPv4.toIPv4Address(
Fahad Naeem Khanf59c2852014-10-10 18:52:26 -070062 matchGeneric.getMask().toString())):"32"));
Fahad Naeem Khand89448d2014-10-06 18:40:45 -070063 }
Fahad Naeem Khan0c1c7b32014-10-07 16:22:23 -070064 else if (matchGeneric.getMatchField().id == MatchFields.IPV4_SRC){
Fahad Naeem Khandf197552014-10-10 14:59:00 -070065 jGen.writeStringField("networkSource", matchGeneric.getValue().toString()
66 +"/"
67 +(matchGeneric.isMasked() ?
68 OFFlowStatsEntryModSerializer.covertToMask(
69 IPv4.toIPv4Address(
Fahad Naeem Khanf59c2852014-10-10 18:52:26 -070070 matchGeneric.getMask().toString())):"32"));
Fahad Naeem Khan0c1c7b32014-10-07 16:22:23 -070071 }
72 else if (matchGeneric.getMatchField().id == MatchFields.ETH_DST){
73 jGen.writeStringField("dataLayerDestination", matchGeneric.getValue().toString());
74 }
75 else if (matchGeneric.getMatchField().id == MatchFields.ETH_SRC){
76 jGen.writeStringField("dataLayerSource", matchGeneric.getValue().toString());
77 }
78 else if (matchGeneric.getMatchField().id == MatchFields.ETH_TYPE){
Fahad Naeem Khandf197552014-10-10 14:59:00 -070079 jGen.writeNumberField("dataLayerType", Integer.decode(matchGeneric.getValue().toString()));
Fahad Naeem Khan0c1c7b32014-10-07 16:22:23 -070080 }
81 else if (matchGeneric.getMatchField().id == MatchFields.IN_PORT){
82 jGen.writeNumberField("inputPort", Integer.parseInt(matchGeneric.getValue().toString()));
83 }
84 else if (matchGeneric.getMatchField().id == MatchFields.MPLS_TC){
Fahad Naeem Khandf197552014-10-10 14:59:00 -070085 jGen.writeNumberField("mplsTc", Integer.decode(matchGeneric.getValue().toString()));
Fahad Naeem Khan0c1c7b32014-10-07 16:22:23 -070086 }
Fahad Naeem Khanc517ae92014-10-13 14:57:00 -070087 else if (matchGeneric.getMatchField().id == MatchFields.MPLS_BOS){
Fahad Naeem Khane36cf2e2014-10-13 15:12:11 -070088 jGen.writeStringField("mplsBos", matchGeneric.getValue().toString());
Fahad Naeem Khanc517ae92014-10-13 14:57:00 -070089 }
Fahad Naeem Khan0c1c7b32014-10-07 16:22:23 -070090 else if (matchGeneric.getMatchField().id == MatchFields.MPLS_LABEL){
Fahad Naeem Khandf197552014-10-10 14:59:00 -070091 jGen.writeNumberField("mplsLabel", Integer.decode(matchGeneric.getValue().toString()));
Fahad Naeem Khan0c1c7b32014-10-07 16:22:23 -070092 }
93 else if (matchGeneric.getMatchField().id == MatchFields.IP_PROTO){
94 jGen.writeNumberField("networkProtocol", Integer.parseInt(matchGeneric.getValue().toString()));
95 }
96 //TODO: Ask Saurav about the implementation of tcp and udp.
Fahad Naeem Khandf197552014-10-10 14:59:00 -070097 else if (matchGeneric.getMatchField().id == MatchFields.TCP_DST
98 || matchGeneric.getMatchField().id == MatchFields.UDP_DST){
Fahad Naeem Khan0c1c7b32014-10-07 16:22:23 -070099 jGen.writeNumberField("transportDestination", Integer.parseInt(matchGeneric.getValue().toString()));
100 }
Fahad Naeem Khandf197552014-10-10 14:59:00 -0700101 else if (matchGeneric.getMatchField().id == MatchFields.TCP_SRC
102 || matchGeneric.getMatchField().id == MatchFields.UDP_SRC){
Fahad Naeem Khan0c1c7b32014-10-07 16:22:23 -0700103 jGen.writeNumberField("transportSource", Integer.parseInt(matchGeneric.getValue().toString()));
104 }
Fahad Naeem Khand89448d2014-10-06 18:40:45 -0700105 }
Fahad Naeem Khan0c1c7b32014-10-07 16:22:23 -0700106 jGen.writeEndObject();
Fahad Naeem Khandcbab082014-10-13 13:44:27 -0700107
Fahad Naeem Khandf197552014-10-10 14:59:00 -0700108 jGen.writeFieldName("instructions");
109 jGen.writeStartArray();
Fahad Naeem Khandcbab082014-10-13 13:44:27 -0700110 jGen.writeStartObject();
Fahad Naeem Khandf197552014-10-10 14:59:00 -0700111 List<OFAction> actions = null;
Fahad Naeem Khan0c1c7b32014-10-07 16:22:23 -0700112 for (OFInstruction instruction: instructions){
Fahad Naeem Khanf59c2852014-10-10 18:52:26 -0700113
Fahad Naeem Khandf197552014-10-10 14:59:00 -0700114 if(instruction.getType().equals(OFInstructionType.APPLY_ACTIONS)){
115 actions = ((OFInstructionApplyActions)instruction).getActions();
Fahad Naeem Khan0c1c7b32014-10-07 16:22:23 -0700116 }
Fahad Naeem Khandf197552014-10-10 14:59:00 -0700117 else if(instruction.getType().equals(OFInstructionType.WRITE_ACTIONS)){
118 actions = ((OFInstructionWriteActions)instruction).getActions();
119 }
Fahad Naeem Khanf59c2852014-10-10 18:52:26 -0700120 else if(instruction.getType().equals(OFInstructionType.GOTO_TABLE)){
Fahad Naeem Khandcbab082014-10-13 13:44:27 -0700121
Fahad Naeem Khanf59c2852014-10-10 18:52:26 -0700122 jGen.writeFieldName(instruction.getType().name());
123 jGen.writeStartObject();
124 jGen.writeNumberField("tableId"
125 , ((OFInstructionGotoTable)instruction).getTableId().getValue());
126 jGen.writeEndObject();
127 continue;
Fahad Naeem Khandcbab082014-10-13 13:44:27 -0700128 }//*/
Fahad Naeem Khandf197552014-10-10 14:59:00 -0700129 else{
130 continue;
131 }
Fahad Naeem Khandcbab082014-10-13 13:44:27 -0700132 jGen.writeObjectFieldStart(instruction.getType().name());
Fahad Naeem Khandf197552014-10-10 14:59:00 -0700133 for (OFAction action : actions){
134 if (action.getType().equals(OFActionType.GROUP)){
135 jGen.writeNumberField("group", ((OFActionGroup)action).getGroup().getGroupNumber());
136 }
137 else if (action.getType().equals(OFActionType.OUTPUT)){
Fahad Naeem Khanf59c2852014-10-10 18:52:26 -0700138 if (((OFActionOutput)action).getPort().getPortNumber() == -3){
139 //Controller port
140 jGen.writeStringField("output", "CONTROLLER");
141 }
142 else{
143 jGen.writeNumberField("output", ((OFActionOutput)action).getPort().getPortNumber());
144 }
Fahad Naeem Khan243bbad2014-10-24 16:36:26 -0700145 }
Fahad Naeem Khan6beb40c2014-10-27 12:01:40 -0700146 else if(action.getType().compareTo(OFActionType.COPY_TTL_IN) == 0
Fahad Naeem Khandf197552014-10-10 14:59:00 -0700147 || action.getType().compareTo(OFActionType.COPY_TTL_OUT) == 0
148 || action.getType().compareTo(OFActionType.DEC_MPLS_TTL) == 0
149 || action.getType().compareTo(OFActionType.DEC_NW_TTL) == 0
150 || action.getType().compareTo(OFActionType.POP_PBB) == 0
151 || action.getType().compareTo(OFActionType.POP_VLAN) == 0){
152 jGen.writeStringField(action.getType().name(), "True");
Fahad Naeem Khandf197552014-10-10 14:59:00 -0700153 }
Fahad Naeem Khan6beb40c2014-10-27 12:01:40 -0700154 else if(action.getType().compareTo(OFActionType.POP_MPLS) == 0){
Fahad Naeem Khan243bbad2014-10-24 16:36:26 -0700155 jGen.writeStringField("POP_MPLS", ((OFActionPopMpls)action).getEthertype().toString());
Fahad Naeem Khan6beb40c2014-10-27 12:01:40 -0700156 }
Fahad Naeem Khandf197552014-10-10 14:59:00 -0700157 else if (action.getType().equals(OFActionType.SET_FIELD)){
158 //TODO Support for more setFields
159 if (((OFActionSetField)action).getField().toString().contains("OFOxmEthSrcVer13")){
160 jGen.writeStringField("SET_DL_SRC", ((OFActionSetField)action).getField().getValue().toString());
161 }
162 else if (((OFActionSetField)action).getField().toString().contains("OFOxmEthDstVer13")){
163 jGen.writeStringField("SET_DL_DST", ((OFActionSetField)action).getField().getValue().toString());
164 }
165 else if (((OFActionSetField)action).getField().toString().contains("OFOxmNwDstVer13")){
166 jGen.writeStringField("SET_NW_SRC", ((OFActionSetField)action).getField().getValue().toString());
167 }
168 else if (((OFActionSetField)action).getField().toString().contains("OFOxmNwDstVer13")){
169 jGen.writeStringField("SET_NW_DST", ((OFActionSetField)action).getField().getValue().toString());
170 }
171 else if (((OFActionSetField)action).getField().toString().contains("OFOxmMplsLabelVer13")){
172 jGen.writeStringField("PUSH_MPLS", ((OFActionSetField)action).getField().getValue().toString());
173 }
174 }
Fahad Naeem Khan0c1c7b32014-10-07 16:22:23 -0700175 }
176 jGen.writeEndObject();
Fahad Naeem Khan0c1c7b32014-10-07 16:22:23 -0700177 }
Fahad Naeem Khand89448d2014-10-06 18:40:45 -0700178 jGen.writeEndObject();
Fahad Naeem Khandf197552014-10-10 14:59:00 -0700179 jGen.writeEndArray();
Fahad Naeem Khan0c1c7b32014-10-07 16:22:23 -0700180 jGen.writeEndObject();
Fahad Naeem Khan8a8daf22014-10-06 14:07:43 -0700181 }
Fahad Naeem Khanf59c2852014-10-10 18:52:26 -0700182 /**
183 * Get the number of 1's in the 32bit integer
184 * Use full to convert wildcard mask(x.x.x.x) to \x notation
185 * for example
186 * ("0.0.0.255") to int to "/8" or
187 * ("0.0.255.255") to int to "/16"
188 * @param x
189 * @return
190 */
191
Fahad Naeem Khandf197552014-10-10 14:59:00 -0700192 public static int covertToMask(int x) {
Fahad Naeem Khanf59c2852014-10-10 18:52:26 -0700193 x = x - ((x >>> 1) & 0x55555555);
194 x = (x & 0x33333333) + ((x >>> 2) & 0x33333333);
195 x = (x + (x >>> 4)) & 0x0F0F0F0F;
196 x = x + (x >>> 8);
197 x = x + (x >>> 16);
198 return 32 - (x & 0x0000003F);
Fahad Naeem Khandf197552014-10-10 14:59:00 -0700199 }
Fahad Naeem Khan8a8daf22014-10-06 14:07:43 -0700200
201}