blob: f780fd7eca5d7b5078d834468445f19a120ae7fd [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());
52 jGen.writeArrayFieldStart("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 }
88 else if (matchGeneric.getMatchField().id == MatchFields.MPLS_LABEL){
Fahad Naeem Khandf197552014-10-10 14:59:00 -070089 jGen.writeNumberField("mplsLabel", Integer.decode(matchGeneric.getValue().toString()));
Fahad Naeem Khan0c1c7b32014-10-07 16:22:23 -070090 }
91 else if (matchGeneric.getMatchField().id == MatchFields.IP_PROTO){
92 jGen.writeNumberField("networkProtocol", Integer.parseInt(matchGeneric.getValue().toString()));
93 }
94 //TODO: Ask Saurav about the implementation of tcp and udp.
Fahad Naeem Khandf197552014-10-10 14:59:00 -070095 else if (matchGeneric.getMatchField().id == MatchFields.TCP_DST
96 || matchGeneric.getMatchField().id == MatchFields.UDP_DST){
Fahad Naeem Khan0c1c7b32014-10-07 16:22:23 -070097 jGen.writeNumberField("transportDestination", Integer.parseInt(matchGeneric.getValue().toString()));
98 }
Fahad Naeem Khandf197552014-10-10 14:59:00 -070099 else if (matchGeneric.getMatchField().id == MatchFields.TCP_SRC
100 || matchGeneric.getMatchField().id == MatchFields.UDP_SRC){
Fahad Naeem Khan0c1c7b32014-10-07 16:22:23 -0700101 jGen.writeNumberField("transportSource", Integer.parseInt(matchGeneric.getValue().toString()));
102 }
Fahad Naeem Khand89448d2014-10-06 18:40:45 -0700103 }
Fahad Naeem Khan0c1c7b32014-10-07 16:22:23 -0700104 jGen.writeEndObject();
Fahad Naeem Khand89448d2014-10-06 18:40:45 -0700105 jGen.writeEndArray();
Fahad Naeem Khandf197552014-10-10 14:59:00 -0700106 jGen.writeFieldName("instructions");
107 jGen.writeStartArray();
108 List<OFAction> actions = null;
Fahad Naeem Khan0c1c7b32014-10-07 16:22:23 -0700109 for (OFInstruction instruction: instructions){
Fahad Naeem Khanf59c2852014-10-10 18:52:26 -0700110
Fahad Naeem Khandf197552014-10-10 14:59:00 -0700111 if(instruction.getType().equals(OFInstructionType.APPLY_ACTIONS)){
112 actions = ((OFInstructionApplyActions)instruction).getActions();
Fahad Naeem Khan0c1c7b32014-10-07 16:22:23 -0700113 }
Fahad Naeem Khandf197552014-10-10 14:59:00 -0700114 else if(instruction.getType().equals(OFInstructionType.WRITE_ACTIONS)){
115 actions = ((OFInstructionWriteActions)instruction).getActions();
116 }
Fahad Naeem Khanf59c2852014-10-10 18:52:26 -0700117 else if(instruction.getType().equals(OFInstructionType.GOTO_TABLE)){
118 jGen.writeFieldName(instruction.getType().name());
119 jGen.writeStartObject();
120 jGen.writeNumberField("tableId"
121 , ((OFInstructionGotoTable)instruction).getTableId().getValue());
122 jGen.writeEndObject();
123 continue;
124 }
Fahad Naeem Khandf197552014-10-10 14:59:00 -0700125 else{
126 continue;
127 }
128 jGen.writeStartObject();
129 jGen.writeFieldName(instruction.getType().name());
130 jGen.writeStartObject();
131 for (OFAction action : actions){
132 if (action.getType().equals(OFActionType.GROUP)){
133 jGen.writeNumberField("group", ((OFActionGroup)action).getGroup().getGroupNumber());
134 }
135 else if (action.getType().equals(OFActionType.OUTPUT)){
Fahad Naeem Khanf59c2852014-10-10 18:52:26 -0700136 if (((OFActionOutput)action).getPort().getPortNumber() == -3){
137 //Controller port
138 jGen.writeStringField("output", "CONTROLLER");
139 }
140 else{
141 jGen.writeNumberField("output", ((OFActionOutput)action).getPort().getPortNumber());
142 }
Fahad Naeem Khandf197552014-10-10 14:59:00 -0700143 }
144 else if(action.getType().compareTo(OFActionType.POP_MPLS) == 0
145 || action.getType().compareTo(OFActionType.COPY_TTL_IN) == 0
146 || action.getType().compareTo(OFActionType.COPY_TTL_OUT) == 0
147 || action.getType().compareTo(OFActionType.DEC_MPLS_TTL) == 0
148 || action.getType().compareTo(OFActionType.DEC_NW_TTL) == 0
149 || action.getType().compareTo(OFActionType.POP_PBB) == 0
150 || action.getType().compareTo(OFActionType.POP_VLAN) == 0){
151 jGen.writeStringField(action.getType().name(), "True");
152 }
153 else if(action.getType().compareTo(OFActionType.COPY_TTL_IN) == 0){
154 jGen.writeStringField("POP_MPLS", "True");
155 }
156 else if (action.getType().equals(OFActionType.SET_FIELD)){
157 //TODO Support for more setFields
158 if (((OFActionSetField)action).getField().toString().contains("OFOxmEthSrcVer13")){
159 jGen.writeStringField("SET_DL_SRC", ((OFActionSetField)action).getField().getValue().toString());
160 }
161 else if (((OFActionSetField)action).getField().toString().contains("OFOxmEthDstVer13")){
162 jGen.writeStringField("SET_DL_DST", ((OFActionSetField)action).getField().getValue().toString());
163 }
164 else if (((OFActionSetField)action).getField().toString().contains("OFOxmNwDstVer13")){
165 jGen.writeStringField("SET_NW_SRC", ((OFActionSetField)action).getField().getValue().toString());
166 }
167 else if (((OFActionSetField)action).getField().toString().contains("OFOxmNwDstVer13")){
168 jGen.writeStringField("SET_NW_DST", ((OFActionSetField)action).getField().getValue().toString());
169 }
170 else if (((OFActionSetField)action).getField().toString().contains("OFOxmMplsLabelVer13")){
171 jGen.writeStringField("PUSH_MPLS", ((OFActionSetField)action).getField().getValue().toString());
172 }
173 }
Fahad Naeem Khan0c1c7b32014-10-07 16:22:23 -0700174 }
175 jGen.writeEndObject();
Fahad Naeem Khan0c1c7b32014-10-07 16:22:23 -0700176 }
Fahad Naeem Khand89448d2014-10-06 18:40:45 -0700177 jGen.writeEndObject();
Fahad Naeem Khandf197552014-10-10 14:59:00 -0700178 jGen.writeEndArray();
Fahad Naeem Khan0c1c7b32014-10-07 16:22:23 -0700179 jGen.writeEndObject();
Fahad Naeem Khan8a8daf22014-10-06 14:07:43 -0700180 }
Fahad Naeem Khanf59c2852014-10-10 18:52:26 -0700181 /**
182 * Get the number of 1's in the 32bit integer
183 * Use full to convert wildcard mask(x.x.x.x) to \x notation
184 * for example
185 * ("0.0.0.255") to int to "/8" or
186 * ("0.0.255.255") to int to "/16"
187 * @param x
188 * @return
189 */
190
Fahad Naeem Khandf197552014-10-10 14:59:00 -0700191 public static int covertToMask(int x) {
Fahad Naeem Khanf59c2852014-10-10 18:52:26 -0700192 x = x - ((x >>> 1) & 0x55555555);
193 x = (x & 0x33333333) + ((x >>> 2) & 0x33333333);
194 x = (x + (x >>> 4)) & 0x0F0F0F0F;
195 x = x + (x >>> 8);
196 x = x + (x >>> 16);
197 return 32 - (x & 0x0000003F);
Fahad Naeem Khandf197552014-10-10 14:59:00 -0700198 }
Fahad Naeem Khan8a8daf22014-10-06 14:07:43 -0700199
200}