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 | df19755 | 2014-10-10 14:59:00 -0700 | [diff] [blame] | 9 | import net.onrc.onos.core.packet.IPv4; |
Fahad Naeem Khan | 8a8daf2 | 2014-10-06 14:07:43 -0700 | [diff] [blame] | 10 | |
Fahad Naeem Khan | 0c1c7b3 | 2014-10-07 16:22:23 -0700 | [diff] [blame] | 11 | import org.projectfloodlight.openflow.protocol.action.*; |
Fahad Naeem Khan | 8a8daf2 | 2014-10-06 14:07:43 -0700 | [diff] [blame] | 12 | import org.codehaus.jackson.JsonGenerationException; |
| 13 | import org.codehaus.jackson.JsonGenerator; |
| 14 | import org.codehaus.jackson.map.SerializerProvider; |
| 15 | import org.codehaus.jackson.map.ser.std.SerializerBase; |
Fahad Naeem Khan | 0c1c7b3 | 2014-10-07 16:22:23 -0700 | [diff] [blame] | 16 | import org.projectfloodlight.openflow.protocol.OFActionType; |
Fahad Naeem Khan | d89448d | 2014-10-06 18:40:45 -0700 | [diff] [blame] | 17 | import org.projectfloodlight.openflow.protocol.OFFlowModFlags; |
| 18 | import org.projectfloodlight.openflow.protocol.OFFlowStatsEntry; |
Fahad Naeem Khan | 0c1c7b3 | 2014-10-07 16:22:23 -0700 | [diff] [blame] | 19 | import org.projectfloodlight.openflow.protocol.OFInstructionType; |
Fahad Naeem Khan | d89448d | 2014-10-06 18:40:45 -0700 | [diff] [blame] | 20 | import org.projectfloodlight.openflow.protocol.OFMatchV3; |
| 21 | import org.projectfloodlight.openflow.protocol.OFOxmList; |
Fahad Naeem Khan | 0c1c7b3 | 2014-10-07 16:22:23 -0700 | [diff] [blame] | 22 | import org.projectfloodlight.openflow.protocol.action.OFAction; |
| 23 | import org.projectfloodlight.openflow.protocol.instruction.*; |
Fahad Naeem Khan | d89448d | 2014-10-06 18:40:45 -0700 | [diff] [blame] | 24 | import org.projectfloodlight.openflow.protocol.match.MatchFields; |
| 25 | import org.projectfloodlight.openflow.protocol.oxm.OFOxm; |
Fahad Naeem Khan | 8a8daf2 | 2014-10-06 14:07:43 -0700 | [diff] [blame] | 26 | |
| 27 | public 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 Khan | d89448d | 2014-10-06 18:40:45 -0700 | [diff] [blame] | 37 | OFFlowStatsEntry flowStatsEntry = FlowStatsEntryMod.getFlowStatsEntry(); |
| 38 | OFOxmList matches = ((OFMatchV3)flowStatsEntry.getMatch()).getOxmList(); |
Fahad Naeem Khan | 0c1c7b3 | 2014-10-07 16:22:23 -0700 | [diff] [blame] | 39 | |
| 40 | List<OFInstruction> instructions = flowStatsEntry.getInstructions(); |
Fahad Naeem Khan | d89448d | 2014-10-06 18:40:45 -0700 | [diff] [blame] | 41 | Set<OFFlowModFlags> flags = flowStatsEntry.getFlags(); |
| 42 | jGen.writeStartObject(); |
Fahad Naeem Khan | df19755 | 2014-10-10 14:59:00 -0700 | [diff] [blame] | 43 | //System.out.println("flowstats:\n\n\n"+ flowStatsEntry); |
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()); |
| 53 | jGen.writeArrayFieldStart("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( |
| 64 | matchGeneric.getMask().toString())):"0")); |
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( |
| 72 | matchGeneric.getMask().toString())):"0")); |
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 | df19755 | 2014-10-10 14:59:00 -0700 | [diff] [blame] | 81 | jGen.writeNumberField("dataLayerType", Integer.decode(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 | } |
| 89 | else if (matchGeneric.getMatchField().id == MatchFields.MPLS_LABEL){ |
Fahad Naeem Khan | df19755 | 2014-10-10 14:59:00 -0700 | [diff] [blame] | 90 | jGen.writeNumberField("mplsLabel", Integer.decode(matchGeneric.getValue().toString())); |
Fahad Naeem Khan | 0c1c7b3 | 2014-10-07 16:22:23 -0700 | [diff] [blame] | 91 | } |
| 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 Khan | df19755 | 2014-10-10 14:59:00 -0700 | [diff] [blame] | 96 | else if (matchGeneric.getMatchField().id == MatchFields.TCP_DST |
| 97 | || matchGeneric.getMatchField().id == MatchFields.UDP_DST){ |
Fahad Naeem Khan | 0c1c7b3 | 2014-10-07 16:22:23 -0700 | [diff] [blame] | 98 | jGen.writeNumberField("transportDestination", Integer.parseInt(matchGeneric.getValue().toString())); |
| 99 | } |
Fahad Naeem Khan | df19755 | 2014-10-10 14:59:00 -0700 | [diff] [blame] | 100 | else if (matchGeneric.getMatchField().id == MatchFields.TCP_SRC |
| 101 | || matchGeneric.getMatchField().id == MatchFields.UDP_SRC){ |
Fahad Naeem Khan | 0c1c7b3 | 2014-10-07 16:22:23 -0700 | [diff] [blame] | 102 | jGen.writeNumberField("transportSource", Integer.parseInt(matchGeneric.getValue().toString())); |
| 103 | } |
Fahad Naeem Khan | d89448d | 2014-10-06 18:40:45 -0700 | [diff] [blame] | 104 | } |
Fahad Naeem Khan | 0c1c7b3 | 2014-10-07 16:22:23 -0700 | [diff] [blame] | 105 | jGen.writeEndObject(); |
Fahad Naeem Khan | d89448d | 2014-10-06 18:40:45 -0700 | [diff] [blame] | 106 | jGen.writeEndArray(); |
Fahad Naeem Khan | df19755 | 2014-10-10 14:59:00 -0700 | [diff] [blame] | 107 | jGen.writeFieldName("instructions"); |
| 108 | jGen.writeStartArray(); |
| 109 | List<OFAction> actions = null; |
Fahad Naeem Khan | 0c1c7b3 | 2014-10-07 16:22:23 -0700 | [diff] [blame] | 110 | for (OFInstruction instruction: instructions){ |
Fahad Naeem Khan | df19755 | 2014-10-10 14:59:00 -0700 | [diff] [blame] | 111 | if(instruction.getType().equals(OFInstructionType.APPLY_ACTIONS)){ |
| 112 | actions = ((OFInstructionApplyActions)instruction).getActions(); |
Fahad Naeem Khan | 0c1c7b3 | 2014-10-07 16:22:23 -0700 | [diff] [blame] | 113 | } |
Fahad Naeem Khan | df19755 | 2014-10-10 14:59:00 -0700 | [diff] [blame] | 114 | 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 Khan | 0c1c7b3 | 2014-10-07 16:22:23 -0700 | [diff] [blame] | 160 | } |
| 161 | jGen.writeEndObject(); |
Fahad Naeem Khan | 0c1c7b3 | 2014-10-07 16:22:23 -0700 | [diff] [blame] | 162 | } |
Fahad Naeem Khan | d89448d | 2014-10-06 18:40:45 -0700 | [diff] [blame] | 163 | jGen.writeEndObject(); |
Fahad Naeem Khan | df19755 | 2014-10-10 14:59:00 -0700 | [diff] [blame] | 164 | jGen.writeEndArray(); |
Fahad Naeem Khan | 0c1c7b3 | 2014-10-07 16:22:23 -0700 | [diff] [blame] | 165 | jGen.writeEndObject(); |
Fahad Naeem Khan | 8a8daf2 | 2014-10-06 14:07:43 -0700 | [diff] [blame] | 166 | } |
Fahad Naeem Khan | df19755 | 2014-10-10 14:59:00 -0700 | [diff] [blame] | 167 | 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 Khan | 8a8daf2 | 2014-10-06 14:07:43 -0700 | [diff] [blame] | 175 | |
| 176 | } |