blob: 561970243c406af1e95dbf662586d97d1af55ebe [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 Khan1ab00c42014-10-29 13:49:09 -07009import net.onrc.onos.core.drivermanager.OFSwitchImplSpringOpenTTP;
Fahad Naeem Khandf197552014-10-10 14:59:00 -070010import net.onrc.onos.core.packet.IPv4;
Fahad Naeem Khan8a8daf22014-10-06 14:07:43 -070011
Fahad Naeem Khan0c1c7b32014-10-07 16:22:23 -070012import org.projectfloodlight.openflow.protocol.action.*;
Fahad Naeem Khan8a8daf22014-10-06 14:07:43 -070013import org.codehaus.jackson.JsonGenerationException;
14import org.codehaus.jackson.JsonGenerator;
15import org.codehaus.jackson.map.SerializerProvider;
16import org.codehaus.jackson.map.ser.std.SerializerBase;
Fahad Naeem Khan0c1c7b32014-10-07 16:22:23 -070017import org.projectfloodlight.openflow.protocol.OFActionType;
Fahad Naeem Khand89448d2014-10-06 18:40:45 -070018import org.projectfloodlight.openflow.protocol.OFFlowModFlags;
19import org.projectfloodlight.openflow.protocol.OFFlowStatsEntry;
Fahad Naeem Khan0c1c7b32014-10-07 16:22:23 -070020import org.projectfloodlight.openflow.protocol.OFInstructionType;
Fahad Naeem Khand89448d2014-10-06 18:40:45 -070021import org.projectfloodlight.openflow.protocol.OFMatchV3;
22import org.projectfloodlight.openflow.protocol.OFOxmList;
Fahad Naeem Khan0c1c7b32014-10-07 16:22:23 -070023import org.projectfloodlight.openflow.protocol.action.OFAction;
24import org.projectfloodlight.openflow.protocol.instruction.*;
Fahad Naeem Khand89448d2014-10-06 18:40:45 -070025import org.projectfloodlight.openflow.protocol.match.MatchFields;
26import org.projectfloodlight.openflow.protocol.oxm.OFOxm;
Fahad Naeem Khan8a8daf22014-10-06 14:07:43 -070027
28public 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 Khand89448d2014-10-06 18:40:45 -070038 OFFlowStatsEntry flowStatsEntry = FlowStatsEntryMod.getFlowStatsEntry();
39 OFOxmList matches = ((OFMatchV3)flowStatsEntry.getMatch()).getOxmList();
Fahad Naeem Khan1ab00c42014-10-29 13:49:09 -070040 OFSwitchImplSpringOpenTTP sw = (OFSwitchImplSpringOpenTTP)FlowStatsEntryMod.getSwitch();
Fahad Naeem Khan0c1c7b32014-10-07 16:22:23 -070041
42 List<OFInstruction> instructions = flowStatsEntry.getInstructions();
Fahad Naeem Khand89448d2014-10-06 18:40:45 -070043 jGen.writeStartObject();
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());
Fahad Naeem Khandcbab082014-10-13 13:44:27 -070053 jGen.writeFieldName("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(
Fahad Naeem Khanf59c2852014-10-10 18:52:26 -070064 matchGeneric.getMask().toString())):"32"));
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(
Fahad Naeem Khanf59c2852014-10-10 18:52:26 -070072 matchGeneric.getMask().toString())):"32"));
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 Khanb4b8a712014-10-28 14:51:11 -070081 jGen.writeStringField("dataLayerType", "0x"+(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 }
Fahad Naeem Khanc517ae92014-10-13 14:57:00 -070089 else if (matchGeneric.getMatchField().id == MatchFields.MPLS_BOS){
Fahad Naeem Khane36cf2e2014-10-13 15:12:11 -070090 jGen.writeStringField("mplsBos", matchGeneric.getValue().toString());
Fahad Naeem Khanc517ae92014-10-13 14:57:00 -070091 }
Fahad Naeem Khan0c1c7b32014-10-07 16:22:23 -070092 else if (matchGeneric.getMatchField().id == MatchFields.MPLS_LABEL){
Fahad Naeem Khandf197552014-10-10 14:59:00 -070093 jGen.writeNumberField("mplsLabel", Integer.decode(matchGeneric.getValue().toString()));
Fahad Naeem Khan0c1c7b32014-10-07 16:22:23 -070094 }
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 Khandf197552014-10-10 14:59:00 -070099 else if (matchGeneric.getMatchField().id == MatchFields.TCP_DST
100 || matchGeneric.getMatchField().id == MatchFields.UDP_DST){
Fahad Naeem Khan0c1c7b32014-10-07 16:22:23 -0700101 jGen.writeNumberField("transportDestination", Integer.parseInt(matchGeneric.getValue().toString()));
102 }
Fahad Naeem Khandf197552014-10-10 14:59:00 -0700103 else if (matchGeneric.getMatchField().id == MatchFields.TCP_SRC
104 || matchGeneric.getMatchField().id == MatchFields.UDP_SRC){
Fahad Naeem Khan0c1c7b32014-10-07 16:22:23 -0700105 jGen.writeNumberField("transportSource", Integer.parseInt(matchGeneric.getValue().toString()));
106 }
Fahad Naeem Khand89448d2014-10-06 18:40:45 -0700107 }
Fahad Naeem Khan0c1c7b32014-10-07 16:22:23 -0700108 jGen.writeEndObject();
Fahad Naeem Khandcbab082014-10-13 13:44:27 -0700109
Fahad Naeem Khandf197552014-10-10 14:59:00 -0700110 jGen.writeFieldName("instructions");
111 jGen.writeStartArray();
Fahad Naeem Khandcbab082014-10-13 13:44:27 -0700112 jGen.writeStartObject();
Fahad Naeem Khandf197552014-10-10 14:59:00 -0700113 List<OFAction> actions = null;
Fahad Naeem Khan0c1c7b32014-10-07 16:22:23 -0700114 for (OFInstruction instruction: instructions){
Fahad Naeem Khanf59c2852014-10-10 18:52:26 -0700115
Fahad Naeem Khandf197552014-10-10 14:59:00 -0700116 if(instruction.getType().equals(OFInstructionType.APPLY_ACTIONS)){
117 actions = ((OFInstructionApplyActions)instruction).getActions();
Fahad Naeem Khan0c1c7b32014-10-07 16:22:23 -0700118 }
Fahad Naeem Khandf197552014-10-10 14:59:00 -0700119 else if(instruction.getType().equals(OFInstructionType.WRITE_ACTIONS)){
120 actions = ((OFInstructionWriteActions)instruction).getActions();
121 }
Fahad Naeem Khanf59c2852014-10-10 18:52:26 -0700122 else if(instruction.getType().equals(OFInstructionType.GOTO_TABLE)){
Fahad Naeem Khandcbab082014-10-13 13:44:27 -0700123
Fahad Naeem Khanf59c2852014-10-10 18:52:26 -0700124 jGen.writeFieldName(instruction.getType().name());
125 jGen.writeStartObject();
Fahad Naeem Khan1ab00c42014-10-29 13:49:09 -0700126 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 Khanf59c2852014-10-10 18:52:26 -0700142 jGen.writeEndObject();
143 continue;
Fahad Naeem Khandcbab082014-10-13 13:44:27 -0700144 }//*/
Fahad Naeem Khandf197552014-10-10 14:59:00 -0700145 else{
146 continue;
147 }
Fahad Naeem Khandcbab082014-10-13 13:44:27 -0700148 jGen.writeObjectFieldStart(instruction.getType().name());
Fahad Naeem Khandf197552014-10-10 14:59:00 -0700149 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 Khanf59c2852014-10-10 18:52:26 -0700154 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 Khan243bbad2014-10-24 16:36:26 -0700161 }
Fahad Naeem Khan6beb40c2014-10-27 12:01:40 -0700162 else if(action.getType().compareTo(OFActionType.COPY_TTL_IN) == 0
Fahad Naeem Khandf197552014-10-10 14:59:00 -0700163 || 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 Khandf197552014-10-10 14:59:00 -0700169 }
Fahad Naeem Khan6beb40c2014-10-27 12:01:40 -0700170 else if(action.getType().compareTo(OFActionType.POP_MPLS) == 0){
Fahad Naeem Khanb4b8a712014-10-28 14:51:11 -0700171 jGen.writeStringField("POP_MPLS", "0x"+((OFActionPopMpls)action).getEthertype().toString());
Fahad Naeem Khan6beb40c2014-10-27 12:01:40 -0700172 }
Fahad Naeem Khandf197552014-10-10 14:59:00 -0700173 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 Khan0c1c7b32014-10-07 16:22:23 -0700191 }
192 jGen.writeEndObject();
Fahad Naeem Khan0c1c7b32014-10-07 16:22:23 -0700193 }
Fahad Naeem Khand89448d2014-10-06 18:40:45 -0700194 jGen.writeEndObject();
Fahad Naeem Khandf197552014-10-10 14:59:00 -0700195 jGen.writeEndArray();
Fahad Naeem Khan0c1c7b32014-10-07 16:22:23 -0700196 jGen.writeEndObject();
Fahad Naeem Khan8a8daf22014-10-06 14:07:43 -0700197 }
Fahad Naeem Khanf59c2852014-10-10 18:52:26 -0700198 /**
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 Khandf197552014-10-10 14:59:00 -0700207 public static int covertToMask(int x) {
Fahad Naeem Khanf59c2852014-10-10 18:52:26 -0700208 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 Khandf197552014-10-10 14:59:00 -0700214 }
Fahad Naeem Khan8a8daf22014-10-06 14:07:43 -0700215
216}