Fahad Naeem Khan | 8a8daf2 | 2014-10-06 14:07:43 -0700 | [diff] [blame] | 1 | package net.floodlightcontroller.core.web.serializers; |
| 2 | |
| 3 | import java.io.IOException; |
Fahad Naeem Khan | d89448d | 2014-10-06 18:40:45 -0700 | [diff] [blame] | 4 | import java.util.Iterator; |
Fahad Naeem Khan | 0c1c7b3 | 2014-10-07 16:22:23 -0700 | [diff] [blame] | 5 | import java.util.List; |
Fahad Naeem Khan | d89448d | 2014-10-06 18:40:45 -0700 | [diff] [blame] | 6 | import java.util.Set; |
Fahad Naeem Khan | 8a8daf2 | 2014-10-06 14:07:43 -0700 | [diff] [blame] | 7 | |
| 8 | import net.floodlightcontroller.core.web.OFFlowStatsEntryMod; |
Fahad Naeem Khan | 1ab00c4 | 2014-10-29 13:49:09 -0700 | [diff] [blame^] | 9 | import net.onrc.onos.core.drivermanager.OFSwitchImplSpringOpenTTP; |
Fahad Naeem Khan | df19755 | 2014-10-10 14:59:00 -0700 | [diff] [blame] | 10 | import net.onrc.onos.core.packet.IPv4; |
Fahad Naeem Khan | 8a8daf2 | 2014-10-06 14:07:43 -0700 | [diff] [blame] | 11 | |
Fahad Naeem Khan | 0c1c7b3 | 2014-10-07 16:22:23 -0700 | [diff] [blame] | 12 | import org.projectfloodlight.openflow.protocol.action.*; |
Fahad Naeem Khan | 8a8daf2 | 2014-10-06 14:07:43 -0700 | [diff] [blame] | 13 | import org.codehaus.jackson.JsonGenerationException; |
| 14 | import org.codehaus.jackson.JsonGenerator; |
| 15 | import org.codehaus.jackson.map.SerializerProvider; |
| 16 | import org.codehaus.jackson.map.ser.std.SerializerBase; |
Fahad Naeem Khan | 0c1c7b3 | 2014-10-07 16:22:23 -0700 | [diff] [blame] | 17 | import org.projectfloodlight.openflow.protocol.OFActionType; |
Fahad Naeem Khan | d89448d | 2014-10-06 18:40:45 -0700 | [diff] [blame] | 18 | import org.projectfloodlight.openflow.protocol.OFFlowModFlags; |
| 19 | import org.projectfloodlight.openflow.protocol.OFFlowStatsEntry; |
Fahad Naeem Khan | 0c1c7b3 | 2014-10-07 16:22:23 -0700 | [diff] [blame] | 20 | import org.projectfloodlight.openflow.protocol.OFInstructionType; |
Fahad Naeem Khan | d89448d | 2014-10-06 18:40:45 -0700 | [diff] [blame] | 21 | import org.projectfloodlight.openflow.protocol.OFMatchV3; |
| 22 | import org.projectfloodlight.openflow.protocol.OFOxmList; |
Fahad Naeem Khan | 0c1c7b3 | 2014-10-07 16:22:23 -0700 | [diff] [blame] | 23 | import org.projectfloodlight.openflow.protocol.action.OFAction; |
| 24 | import org.projectfloodlight.openflow.protocol.instruction.*; |
Fahad Naeem Khan | d89448d | 2014-10-06 18:40:45 -0700 | [diff] [blame] | 25 | import org.projectfloodlight.openflow.protocol.match.MatchFields; |
| 26 | import org.projectfloodlight.openflow.protocol.oxm.OFOxm; |
Fahad Naeem Khan | 8a8daf2 | 2014-10-06 14:07:43 -0700 | [diff] [blame] | 27 | |
| 28 | public class OFFlowStatsEntryModSerializer extends SerializerBase<OFFlowStatsEntryMod> { |
| 29 | |
| 30 | protected OFFlowStatsEntryModSerializer(){ |
| 31 | super(OFFlowStatsEntryMod.class); |
| 32 | } |
| 33 | |
| 34 | @Override |
| 35 | public void serialize(OFFlowStatsEntryMod FlowStatsEntryMod, JsonGenerator jGen, |
| 36 | SerializerProvider sp) throws IOException, |
| 37 | JsonGenerationException { |
Fahad Naeem Khan | d89448d | 2014-10-06 18:40:45 -0700 | [diff] [blame] | 38 | OFFlowStatsEntry flowStatsEntry = FlowStatsEntryMod.getFlowStatsEntry(); |
| 39 | OFOxmList matches = ((OFMatchV3)flowStatsEntry.getMatch()).getOxmList(); |
Fahad Naeem Khan | 1ab00c4 | 2014-10-29 13:49:09 -0700 | [diff] [blame^] | 40 | OFSwitchImplSpringOpenTTP sw = (OFSwitchImplSpringOpenTTP)FlowStatsEntryMod.getSwitch(); |
Fahad Naeem Khan | 0c1c7b3 | 2014-10-07 16:22:23 -0700 | [diff] [blame] | 41 | |
| 42 | List<OFInstruction> instructions = flowStatsEntry.getInstructions(); |
Fahad Naeem Khan | d89448d | 2014-10-06 18:40:45 -0700 | [diff] [blame] | 43 | jGen.writeStartObject(); |
Fahad Naeem Khan | d89448d | 2014-10-06 18:40:45 -0700 | [diff] [blame] | 44 | jGen.writeNumberField("byteCount", flowStatsEntry.getByteCount().getValue()); |
Fahad Naeem Khan | f35e4f6 | 2014-10-07 16:45:39 -0700 | [diff] [blame] | 45 | jGen.writeNumberField("packetCount", flowStatsEntry.getPacketCount().getValue()); |
Fahad Naeem Khan | 0c1c7b3 | 2014-10-07 16:22:23 -0700 | [diff] [blame] | 46 | jGen.writeNumberField("priority", flowStatsEntry.getPriority()); |
Fahad Naeem Khan | d89448d | 2014-10-06 18:40:45 -0700 | [diff] [blame] | 47 | jGen.writeNumberField("cookie", flowStatsEntry.getCookie().getValue()); |
| 48 | jGen.writeNumberField("durationNsec", flowStatsEntry.getDurationNsec()); |
| 49 | jGen.writeNumberField("durationSec", flowStatsEntry.getDurationSec()); |
Fahad Naeem Khan | df19755 | 2014-10-10 14:59:00 -0700 | [diff] [blame] | 50 | jGen.writeObjectField("flags", flowStatsEntry.getFlags()); |
Fahad Naeem Khan | d89448d | 2014-10-06 18:40:45 -0700 | [diff] [blame] | 51 | jGen.writeNumberField("hardTimeout", flowStatsEntry.getHardTimeout()); |
| 52 | jGen.writeNumberField("idleTimeout", flowStatsEntry.getIdleTimeout()); |
Fahad Naeem Khan | dcbab08 | 2014-10-13 13:44:27 -0700 | [diff] [blame] | 53 | jGen.writeFieldName("match"); |
Fahad Naeem Khan | 0c1c7b3 | 2014-10-07 16:22:23 -0700 | [diff] [blame] | 54 | jGen.writeStartObject(); |
Fahad Naeem Khan | d89448d | 2014-10-06 18:40:45 -0700 | [diff] [blame] | 55 | Iterator<OFOxm<?>> match= matches.iterator(); |
| 56 | while(match.hasNext()){ |
| 57 | OFOxm<?> matchGeneric = match.next(); |
| 58 | if (matchGeneric.getMatchField().id == MatchFields.IPV4_DST){ |
Fahad Naeem Khan | df19755 | 2014-10-10 14:59:00 -0700 | [diff] [blame] | 59 | jGen.writeStringField("networkDestination", matchGeneric.getValue().toString() |
| 60 | +"/" |
| 61 | +(matchGeneric.isMasked() ? |
| 62 | OFFlowStatsEntryModSerializer.covertToMask( |
| 63 | IPv4.toIPv4Address( |
Fahad Naeem Khan | f59c285 | 2014-10-10 18:52:26 -0700 | [diff] [blame] | 64 | matchGeneric.getMask().toString())):"32")); |
Fahad Naeem Khan | d89448d | 2014-10-06 18:40:45 -0700 | [diff] [blame] | 65 | } |
Fahad Naeem Khan | 0c1c7b3 | 2014-10-07 16:22:23 -0700 | [diff] [blame] | 66 | else if (matchGeneric.getMatchField().id == MatchFields.IPV4_SRC){ |
Fahad Naeem Khan | df19755 | 2014-10-10 14:59:00 -0700 | [diff] [blame] | 67 | jGen.writeStringField("networkSource", matchGeneric.getValue().toString() |
| 68 | +"/" |
| 69 | +(matchGeneric.isMasked() ? |
| 70 | OFFlowStatsEntryModSerializer.covertToMask( |
| 71 | IPv4.toIPv4Address( |
Fahad Naeem Khan | f59c285 | 2014-10-10 18:52:26 -0700 | [diff] [blame] | 72 | matchGeneric.getMask().toString())):"32")); |
Fahad Naeem Khan | 0c1c7b3 | 2014-10-07 16:22:23 -0700 | [diff] [blame] | 73 | } |
| 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 Khan | b4b8a71 | 2014-10-28 14:51:11 -0700 | [diff] [blame] | 81 | jGen.writeStringField("dataLayerType", "0x"+(matchGeneric.getValue().toString())); |
Fahad Naeem Khan | 0c1c7b3 | 2014-10-07 16:22:23 -0700 | [diff] [blame] | 82 | } |
| 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 Khan | df19755 | 2014-10-10 14:59:00 -0700 | [diff] [blame] | 87 | jGen.writeNumberField("mplsTc", Integer.decode(matchGeneric.getValue().toString())); |
Fahad Naeem Khan | 0c1c7b3 | 2014-10-07 16:22:23 -0700 | [diff] [blame] | 88 | } |
Fahad Naeem Khan | c517ae9 | 2014-10-13 14:57:00 -0700 | [diff] [blame] | 89 | else if (matchGeneric.getMatchField().id == MatchFields.MPLS_BOS){ |
Fahad Naeem Khan | e36cf2e | 2014-10-13 15:12:11 -0700 | [diff] [blame] | 90 | jGen.writeStringField("mplsBos", matchGeneric.getValue().toString()); |
Fahad Naeem Khan | c517ae9 | 2014-10-13 14:57:00 -0700 | [diff] [blame] | 91 | } |
Fahad Naeem Khan | 0c1c7b3 | 2014-10-07 16:22:23 -0700 | [diff] [blame] | 92 | else if (matchGeneric.getMatchField().id == MatchFields.MPLS_LABEL){ |
Fahad Naeem Khan | df19755 | 2014-10-10 14:59:00 -0700 | [diff] [blame] | 93 | jGen.writeNumberField("mplsLabel", Integer.decode(matchGeneric.getValue().toString())); |
Fahad Naeem Khan | 0c1c7b3 | 2014-10-07 16:22:23 -0700 | [diff] [blame] | 94 | } |
| 95 | else if (matchGeneric.getMatchField().id == MatchFields.IP_PROTO){ |
| 96 | jGen.writeNumberField("networkProtocol", Integer.parseInt(matchGeneric.getValue().toString())); |
| 97 | } |
| 98 | //TODO: Ask Saurav about the implementation of tcp and udp. |
Fahad Naeem Khan | df19755 | 2014-10-10 14:59:00 -0700 | [diff] [blame] | 99 | else if (matchGeneric.getMatchField().id == MatchFields.TCP_DST |
| 100 | || matchGeneric.getMatchField().id == MatchFields.UDP_DST){ |
Fahad Naeem Khan | 0c1c7b3 | 2014-10-07 16:22:23 -0700 | [diff] [blame] | 101 | jGen.writeNumberField("transportDestination", Integer.parseInt(matchGeneric.getValue().toString())); |
| 102 | } |
Fahad Naeem Khan | df19755 | 2014-10-10 14:59:00 -0700 | [diff] [blame] | 103 | else if (matchGeneric.getMatchField().id == MatchFields.TCP_SRC |
| 104 | || matchGeneric.getMatchField().id == MatchFields.UDP_SRC){ |
Fahad Naeem Khan | 0c1c7b3 | 2014-10-07 16:22:23 -0700 | [diff] [blame] | 105 | jGen.writeNumberField("transportSource", Integer.parseInt(matchGeneric.getValue().toString())); |
| 106 | } |
Fahad Naeem Khan | d89448d | 2014-10-06 18:40:45 -0700 | [diff] [blame] | 107 | } |
Fahad Naeem Khan | 0c1c7b3 | 2014-10-07 16:22:23 -0700 | [diff] [blame] | 108 | jGen.writeEndObject(); |
Fahad Naeem Khan | dcbab08 | 2014-10-13 13:44:27 -0700 | [diff] [blame] | 109 | |
Fahad Naeem Khan | df19755 | 2014-10-10 14:59:00 -0700 | [diff] [blame] | 110 | jGen.writeFieldName("instructions"); |
| 111 | jGen.writeStartArray(); |
Fahad Naeem Khan | dcbab08 | 2014-10-13 13:44:27 -0700 | [diff] [blame] | 112 | jGen.writeStartObject(); |
Fahad Naeem Khan | df19755 | 2014-10-10 14:59:00 -0700 | [diff] [blame] | 113 | List<OFAction> actions = null; |
Fahad Naeem Khan | 0c1c7b3 | 2014-10-07 16:22:23 -0700 | [diff] [blame] | 114 | for (OFInstruction instruction: instructions){ |
Fahad Naeem Khan | f59c285 | 2014-10-10 18:52:26 -0700 | [diff] [blame] | 115 | |
Fahad Naeem Khan | df19755 | 2014-10-10 14:59:00 -0700 | [diff] [blame] | 116 | if(instruction.getType().equals(OFInstructionType.APPLY_ACTIONS)){ |
| 117 | actions = ((OFInstructionApplyActions)instruction).getActions(); |
Fahad Naeem Khan | 0c1c7b3 | 2014-10-07 16:22:23 -0700 | [diff] [blame] | 118 | } |
Fahad Naeem Khan | df19755 | 2014-10-10 14:59:00 -0700 | [diff] [blame] | 119 | else if(instruction.getType().equals(OFInstructionType.WRITE_ACTIONS)){ |
| 120 | actions = ((OFInstructionWriteActions)instruction).getActions(); |
| 121 | } |
Fahad Naeem Khan | f59c285 | 2014-10-10 18:52:26 -0700 | [diff] [blame] | 122 | else if(instruction.getType().equals(OFInstructionType.GOTO_TABLE)){ |
Fahad Naeem Khan | dcbab08 | 2014-10-13 13:44:27 -0700 | [diff] [blame] | 123 | |
Fahad Naeem Khan | f59c285 | 2014-10-10 18:52:26 -0700 | [diff] [blame] | 124 | jGen.writeFieldName(instruction.getType().name()); |
| 125 | jGen.writeStartObject(); |
Fahad Naeem Khan | 1ab00c4 | 2014-10-29 13:49:09 -0700 | [diff] [blame^] | 126 | if(((OFInstructionGotoTable)instruction).getTableId().getValue()== |
| 127 | sw.getTableId("ip").getValue()){ |
| 128 | jGen.writeStringField("tableId", "ip"); |
| 129 | } |
| 130 | else if(((OFInstructionGotoTable)instruction).getTableId().getValue()== |
| 131 | sw.getTableId("mpls").getValue()){ |
| 132 | jGen.writeStringField("tableId", "mpls"); |
| 133 | } |
| 134 | else if(((OFInstructionGotoTable)instruction).getTableId().getValue()== |
| 135 | sw.getTableId("acl").getValue()){ |
| 136 | jGen.writeStringField("tableId", "acl"); |
| 137 | } |
| 138 | else{ |
| 139 | jGen.writeNumberField("tableId" |
| 140 | , ((OFInstructionGotoTable)instruction).getTableId().getValue()); |
| 141 | } |
Fahad Naeem Khan | f59c285 | 2014-10-10 18:52:26 -0700 | [diff] [blame] | 142 | jGen.writeEndObject(); |
| 143 | continue; |
Fahad Naeem Khan | dcbab08 | 2014-10-13 13:44:27 -0700 | [diff] [blame] | 144 | }//*/ |
Fahad Naeem Khan | df19755 | 2014-10-10 14:59:00 -0700 | [diff] [blame] | 145 | else{ |
| 146 | continue; |
| 147 | } |
Fahad Naeem Khan | dcbab08 | 2014-10-13 13:44:27 -0700 | [diff] [blame] | 148 | jGen.writeObjectFieldStart(instruction.getType().name()); |
Fahad Naeem Khan | df19755 | 2014-10-10 14:59:00 -0700 | [diff] [blame] | 149 | for (OFAction action : actions){ |
| 150 | if (action.getType().equals(OFActionType.GROUP)){ |
| 151 | jGen.writeNumberField("group", ((OFActionGroup)action).getGroup().getGroupNumber()); |
| 152 | } |
| 153 | else if (action.getType().equals(OFActionType.OUTPUT)){ |
Fahad Naeem Khan | f59c285 | 2014-10-10 18:52:26 -0700 | [diff] [blame] | 154 | if (((OFActionOutput)action).getPort().getPortNumber() == -3){ |
| 155 | //Controller port |
| 156 | jGen.writeStringField("output", "CONTROLLER"); |
| 157 | } |
| 158 | else{ |
| 159 | jGen.writeNumberField("output", ((OFActionOutput)action).getPort().getPortNumber()); |
| 160 | } |
Fahad Naeem Khan | 243bbad | 2014-10-24 16:36:26 -0700 | [diff] [blame] | 161 | } |
Fahad Naeem Khan | 6beb40c | 2014-10-27 12:01:40 -0700 | [diff] [blame] | 162 | else if(action.getType().compareTo(OFActionType.COPY_TTL_IN) == 0 |
Fahad Naeem Khan | df19755 | 2014-10-10 14:59:00 -0700 | [diff] [blame] | 163 | || action.getType().compareTo(OFActionType.COPY_TTL_OUT) == 0 |
| 164 | || action.getType().compareTo(OFActionType.DEC_MPLS_TTL) == 0 |
| 165 | || action.getType().compareTo(OFActionType.DEC_NW_TTL) == 0 |
| 166 | || action.getType().compareTo(OFActionType.POP_PBB) == 0 |
| 167 | || action.getType().compareTo(OFActionType.POP_VLAN) == 0){ |
| 168 | jGen.writeStringField(action.getType().name(), "True"); |
Fahad Naeem Khan | df19755 | 2014-10-10 14:59:00 -0700 | [diff] [blame] | 169 | } |
Fahad Naeem Khan | 6beb40c | 2014-10-27 12:01:40 -0700 | [diff] [blame] | 170 | else if(action.getType().compareTo(OFActionType.POP_MPLS) == 0){ |
Fahad Naeem Khan | b4b8a71 | 2014-10-28 14:51:11 -0700 | [diff] [blame] | 171 | jGen.writeStringField("POP_MPLS", "0x"+((OFActionPopMpls)action).getEthertype().toString()); |
Fahad Naeem Khan | 6beb40c | 2014-10-27 12:01:40 -0700 | [diff] [blame] | 172 | } |
Fahad Naeem Khan | df19755 | 2014-10-10 14:59:00 -0700 | [diff] [blame] | 173 | else if (action.getType().equals(OFActionType.SET_FIELD)){ |
| 174 | //TODO Support for more setFields |
| 175 | if (((OFActionSetField)action).getField().toString().contains("OFOxmEthSrcVer13")){ |
| 176 | jGen.writeStringField("SET_DL_SRC", ((OFActionSetField)action).getField().getValue().toString()); |
| 177 | } |
| 178 | else if (((OFActionSetField)action).getField().toString().contains("OFOxmEthDstVer13")){ |
| 179 | jGen.writeStringField("SET_DL_DST", ((OFActionSetField)action).getField().getValue().toString()); |
| 180 | } |
| 181 | else if (((OFActionSetField)action).getField().toString().contains("OFOxmNwDstVer13")){ |
| 182 | jGen.writeStringField("SET_NW_SRC", ((OFActionSetField)action).getField().getValue().toString()); |
| 183 | } |
| 184 | else if (((OFActionSetField)action).getField().toString().contains("OFOxmNwDstVer13")){ |
| 185 | jGen.writeStringField("SET_NW_DST", ((OFActionSetField)action).getField().getValue().toString()); |
| 186 | } |
| 187 | else if (((OFActionSetField)action).getField().toString().contains("OFOxmMplsLabelVer13")){ |
| 188 | jGen.writeStringField("PUSH_MPLS", ((OFActionSetField)action).getField().getValue().toString()); |
| 189 | } |
| 190 | } |
Fahad Naeem Khan | 0c1c7b3 | 2014-10-07 16:22:23 -0700 | [diff] [blame] | 191 | } |
| 192 | jGen.writeEndObject(); |
Fahad Naeem Khan | 0c1c7b3 | 2014-10-07 16:22:23 -0700 | [diff] [blame] | 193 | } |
Fahad Naeem Khan | d89448d | 2014-10-06 18:40:45 -0700 | [diff] [blame] | 194 | jGen.writeEndObject(); |
Fahad Naeem Khan | df19755 | 2014-10-10 14:59:00 -0700 | [diff] [blame] | 195 | jGen.writeEndArray(); |
Fahad Naeem Khan | 0c1c7b3 | 2014-10-07 16:22:23 -0700 | [diff] [blame] | 196 | jGen.writeEndObject(); |
Fahad Naeem Khan | 8a8daf2 | 2014-10-06 14:07:43 -0700 | [diff] [blame] | 197 | } |
Fahad Naeem Khan | f59c285 | 2014-10-10 18:52:26 -0700 | [diff] [blame] | 198 | /** |
| 199 | * Get the number of 1's in the 32bit integer |
| 200 | * Use full to convert wildcard mask(x.x.x.x) to \x notation |
| 201 | * for example |
| 202 | * ("0.0.0.255") to int to "/8" or |
| 203 | * ("0.0.255.255") to int to "/16" |
| 204 | * @param x |
| 205 | * @return |
| 206 | */ |
Fahad Naeem Khan | df19755 | 2014-10-10 14:59:00 -0700 | [diff] [blame] | 207 | public static int covertToMask(int x) { |
Fahad Naeem Khan | f59c285 | 2014-10-10 18:52:26 -0700 | [diff] [blame] | 208 | x = x - ((x >>> 1) & 0x55555555); |
| 209 | x = (x & 0x33333333) + ((x >>> 2) & 0x33333333); |
| 210 | x = (x + (x >>> 4)) & 0x0F0F0F0F; |
| 211 | x = x + (x >>> 8); |
| 212 | x = x + (x >>> 16); |
| 213 | return 32 - (x & 0x0000003F); |
Fahad Naeem Khan | df19755 | 2014-10-10 14:59:00 -0700 | [diff] [blame] | 214 | } |
Fahad Naeem Khan | 8a8daf2 | 2014-10-06 14:07:43 -0700 | [diff] [blame] | 215 | |
| 216 | } |