From 87b907ef1c7aedd8470904ce44f06144a19a20b0 Mon Sep 17 00:00:00 2001
From: Ryan Izard <rizard@g.clemson.edu>
Date: Fri, 8 Aug 2014 17:01:26 -0700
Subject: [PATCH] Quick fix for OF1.0. Should find a way so that all modules
 don't have to worry about how to get Match from PI. OF1.0-1.2 use
 pi.getInPort(); OF1.3 uses pi.getMatch(MatchField.IN_PORT). Workaroud is:
 (pi.getVersion().compareTo(OFVersion.OF_13) < 0 ? pi.getInPort() :
 pi.getMatch().get(MatchField.IN_PORT))

---
 .../internal/DeviceManagerImpl.java           |  7 ++---
 .../forwarding/Forwarding.java                | 26 +++++++++----------
 .../internal/LinkDiscoveryManager.java        |  3 ++-
 .../routing/ForwardingBase.java               |  5 ++--
 .../topology/TopologyManager.java             |  3 ++-
 5 files changed, 24 insertions(+), 20 deletions(-)

diff --git a/src/main/java/net/floodlightcontroller/devicemanager/internal/DeviceManagerImpl.java b/src/main/java/net/floodlightcontroller/devicemanager/internal/DeviceManagerImpl.java
index ea3a8aba8..63aad5e3e 100755
--- a/src/main/java/net/floodlightcontroller/devicemanager/internal/DeviceManagerImpl.java
+++ b/src/main/java/net/floodlightcontroller/devicemanager/internal/DeviceManagerImpl.java
@@ -95,6 +95,7 @@ DeviceManagerImpl.DeviceUpdate.Change.*;
 
 import org.projectfloodlight.openflow.protocol.OFMessage;
 import org.projectfloodlight.openflow.protocol.OFPacketIn;
+import org.projectfloodlight.openflow.protocol.OFVersion;
 import org.projectfloodlight.openflow.types.DatapathId;
 import org.projectfloodlight.openflow.types.IPv4Address;
 import org.projectfloodlight.openflow.types.MacAddress;
@@ -1152,7 +1153,7 @@ public class DeviceManagerImpl implements IDeviceService, IOFMessageListener, IT
 		Ethernet eth = IFloodlightProviderService.bcStore.get(cntx,IFloodlightProviderService.CONTEXT_PI_PAYLOAD);
 
 		// Extract source entity information
-		Entity srcEntity = getSourceEntityFromPacket(eth, sw.getId(), pi.getMatch().get(MatchField.IN_PORT));
+		Entity srcEntity = getSourceEntityFromPacket(eth, sw.getId(), (pi.getVersion().compareTo(OFVersion.OF_13) < 0 ? pi.getInPort() : pi.getMatch().get(MatchField.IN_PORT)));
 		if (srcEntity == null) {
 			cntInvalidSource.increment();
 			return Command.STOP;
@@ -1164,7 +1165,7 @@ public class DeviceManagerImpl implements IDeviceService, IOFMessageListener, IT
 		// the IP to MAC mapping of the VRRP IP address.  The source
 		// entity will not have that information.  Hence, a separate call
 		// to learn devices in such cases.
-		learnDeviceFromArpResponseData(eth, sw.getId(), pi.getMatch().get(MatchField.IN_PORT));
+		learnDeviceFromArpResponseData(eth, sw.getId(), (pi.getVersion().compareTo(OFVersion.OF_13) < 0 ? pi.getInPort() : pi.getMatch().get(MatchField.IN_PORT)));
 
 		// Learn/lookup device information
 		Device srcDevice = learnDeviceByEntity(srcEntity);
@@ -1197,7 +1198,7 @@ public class DeviceManagerImpl implements IDeviceService, IOFMessageListener, IT
 		if (logger.isTraceEnabled()) {
 			logger.trace("Received PI: {} on switch {}, port {} *** eth={}" +
 					" *** srcDev={} *** dstDev={} *** ",
-					new Object[] { pi, sw.getId().toString(), pi.getMatch().get(MatchField.IN_PORT), eth,
+					new Object[] { pi, sw.getId().toString(), (pi.getVersion().compareTo(OFVersion.OF_13) < 0 ? pi.getInPort() : pi.getMatch().get(MatchField.IN_PORT)), eth,
 					srcDevice, dstDevice });
 		}
 
diff --git a/src/main/java/net/floodlightcontroller/forwarding/Forwarding.java b/src/main/java/net/floodlightcontroller/forwarding/Forwarding.java
index 77ebe9a8a..abf899c9c 100644
--- a/src/main/java/net/floodlightcontroller/forwarding/Forwarding.java
+++ b/src/main/java/net/floodlightcontroller/forwarding/Forwarding.java
@@ -47,7 +47,6 @@ import net.floodlightcontroller.routing.IRoutingDecision;
 import net.floodlightcontroller.routing.IRoutingService;
 import net.floodlightcontroller.routing.Route;
 import net.floodlightcontroller.topology.ITopologyService;
-import net.floodlightcontroller.util.MatchUtils;
 
 import org.projectfloodlight.openflow.protocol.OFFlowMod;
 import org.projectfloodlight.openflow.protocol.match.Match;
@@ -55,6 +54,7 @@ import org.projectfloodlight.openflow.protocol.match.MatchField;
 import org.projectfloodlight.openflow.protocol.OFFlowModCommand;
 import org.projectfloodlight.openflow.protocol.OFPacketIn;
 import org.projectfloodlight.openflow.protocol.OFPacketOut;
+import org.projectfloodlight.openflow.protocol.OFVersion;
 import org.projectfloodlight.openflow.types.DatapathId;
 import org.projectfloodlight.openflow.types.EthType;
 import org.projectfloodlight.openflow.types.IPv4Address;
@@ -171,8 +171,7 @@ public class Forwarding extends ForwardingBase implements IFloodlightModule {
 	}
 
 	protected void doForwardFlow(IOFSwitch sw, OFPacketIn pi, FloodlightContext cntx, boolean requestFlowRemovedNotifn) {
-		Match m = pi.getMatch();
-
+		OFPort inPort = (pi.getVersion().compareTo(OFVersion.OF_13) < 0 ? pi.getInPort() : pi.getMatch().get(MatchField.IN_PORT));
 		// Check if we have the location of the destination
 		IDevice dstDevice = IDeviceService.fcStore.get(cntx, IDeviceService.CONTEXT_DST_DEVICE);
 
@@ -186,7 +185,7 @@ public class Forwarding extends ForwardingBase implements IFloodlightModule {
 			}
 			if (srcIsland == null) {
 				log.debug("No openflow island found for source {}/{}",
-						sw.getId().toString(), pi.getMatch().get(MatchField.IN_PORT));
+						sw.getId().toString(), (pi.getVersion().compareTo(OFVersion.OF_13) < 0 ? pi.getInPort() : pi.getMatch().get(MatchField.IN_PORT)));
 				return;
 			}
 
@@ -199,7 +198,7 @@ public class Forwarding extends ForwardingBase implements IFloodlightModule {
 				DatapathId dstIsland = topologyService.getL2DomainId(dstSwDpid);
 				if ((dstIsland != null) && dstIsland.equals(srcIsland)) {
 					on_same_island = true;
-					if ((sw.getId().equals(dstSwDpid)) && (pi.getMatch().get(MatchField.IN_PORT).equals(dstDap.getPort()))) {
+					if ((sw.getId().equals(dstSwDpid)) && ((pi.getVersion().compareTo(OFVersion.OF_13) < 0 ? pi.getInPort() : pi.getMatch().get(MatchField.IN_PORT)).equals(dstDap.getPort()))) {
 						on_same_if = true;
 					}
 					break;
@@ -220,7 +219,7 @@ public class Forwarding extends ForwardingBase implements IFloodlightModule {
 				if (log.isTraceEnabled()) {
 					log.trace("Both source and destination are on the same " +
 							"switch/port {}/{}, Action = NOP",
-							sw.toString(), pi.getMatch().get(MatchField.IN_PORT));
+							sw.toString(), (pi.getVersion().compareTo(OFVersion.OF_13) < 0 ? pi.getInPort() : pi.getMatch().get(MatchField.IN_PORT)));
 				}
 				return;
 			}
@@ -254,9 +253,9 @@ public class Forwarding extends ForwardingBase implements IFloodlightModule {
 										dstDap.getPort(), U64.of(0)); //cookie = 0, i.e., default route
 										if (route != null) {
 											if (log.isTraceEnabled()) {
-												log.trace("pushRoute match={} route={} " +
+												log.trace("pushRoute inPort={} route={} " +
 														"destination={}:{}",
-														new Object[] { m, route,
+														new Object[] { inPort, route,
 														dstDap.getSwitchDPID(),
 														dstDap.getPort()});
 											}
@@ -280,8 +279,9 @@ public class Forwarding extends ForwardingBase implements IFloodlightModule {
 												
 												// A retentive builder will remember all MatchFields of the parent the builder was generated from
 												// With a normal builder, all parent MatchFields will be lost if any MatchFields are added, mod, del
-												Match.Builder mb = MatchUtils.createRetentiveBuilder(m);
-												mb.setExact(MatchField.ETH_SRC, srcMac)
+												Match.Builder mb = sw.getOFFactory().buildMatch();
+												mb.setExact(MatchField.IN_PORT, inPort)
+												.setExact(MatchField.ETH_SRC, srcMac)
 												.setExact(MatchField.ETH_DST, dstMac);
 												
 												if (!vlan.equals(VlanVid.ZERO)) {
@@ -336,11 +336,11 @@ public class Forwarding extends ForwardingBase implements IFloodlightModule {
 							"out message to the switch",
 							recommendation=LogMessageDoc.CHECK_SWITCH)
 	protected void doFlood(IOFSwitch sw, OFPacketIn pi, FloodlightContext cntx) {
-		if (topologyService.isIncomingBroadcastAllowed(sw.getId(), pi.getMatch().get(MatchField.IN_PORT)) == false) {
+		if (topologyService.isIncomingBroadcastAllowed(sw.getId(), (pi.getVersion().compareTo(OFVersion.OF_13) < 0 ? pi.getInPort() : pi.getMatch().get(MatchField.IN_PORT))) == false) {
 			if (log.isTraceEnabled()) {
 				log.trace("doFlood, drop broadcast packet, pi={}, " +
 						"from a blocked port, srcSwitch=[{},{}], linkInfo={}",
-						new Object[] {pi, sw.getId(),pi.getMatch().get(MatchField.IN_PORT)});
+						new Object[] {pi, sw.getId(), (pi.getVersion().compareTo(OFVersion.OF_13) < 0 ? pi.getInPort() : pi.getMatch().get(MatchField.IN_PORT))});
 			}
 			return;
 		}
@@ -357,7 +357,7 @@ public class Forwarding extends ForwardingBase implements IFloodlightModule {
 
 		// set buffer-id, in-port and packet-data based on packet-in
 		pob.setBufferId(OFBufferId.NO_BUFFER);
-		pob.setInPort(pi.getMatch().get(MatchField.IN_PORT));
+		pob.setInPort((pi.getVersion().compareTo(OFVersion.OF_13) < 0 ? pi.getInPort() : pi.getMatch().get(MatchField.IN_PORT)));
 		pob.setData(pi.getData());
 
 		try {
diff --git a/src/main/java/net/floodlightcontroller/linkdiscovery/internal/LinkDiscoveryManager.java b/src/main/java/net/floodlightcontroller/linkdiscovery/internal/LinkDiscoveryManager.java
index f280411b4..e2b53a777 100644
--- a/src/main/java/net/floodlightcontroller/linkdiscovery/internal/LinkDiscoveryManager.java
+++ b/src/main/java/net/floodlightcontroller/linkdiscovery/internal/LinkDiscoveryManager.java
@@ -97,6 +97,7 @@ import org.projectfloodlight.openflow.protocol.OFPacketIn;
 import org.projectfloodlight.openflow.protocol.OFPacketOut;
 import org.projectfloodlight.openflow.protocol.OFPortDesc;
 import org.projectfloodlight.openflow.protocol.OFPortState;
+import org.projectfloodlight.openflow.protocol.OFVersion;
 import org.projectfloodlight.openflow.types.DatapathId;
 import org.projectfloodlight.openflow.types.MacAddress;
 import org.projectfloodlight.openflow.types.OFBufferId;
@@ -594,7 +595,7 @@ IFloodlightModule, IInfoProvider {
 		}
 
 		// If packet-in is from a quarantine port, stop processing.
-		NodePortTuple npt = new NodePortTuple(sw, pi.getMatch().get(MatchField.IN_PORT));
+		NodePortTuple npt = new NodePortTuple(sw, (pi.getVersion().compareTo(OFVersion.OF_13) < 0 ? pi.getInPort() : pi.getMatch().get(MatchField.IN_PORT)));
 		if (quarantineQueue.contains(npt)) {
 			ctrQuarantineDrops.increment();
 			return Command.STOP;
diff --git a/src/main/java/net/floodlightcontroller/routing/ForwardingBase.java b/src/main/java/net/floodlightcontroller/routing/ForwardingBase.java
index 594ec51a3..878db1c99 100644
--- a/src/main/java/net/floodlightcontroller/routing/ForwardingBase.java
+++ b/src/main/java/net/floodlightcontroller/routing/ForwardingBase.java
@@ -54,6 +54,7 @@ import org.projectfloodlight.openflow.protocol.OFMessage;
 import org.projectfloodlight.openflow.protocol.OFPacketIn;
 import org.projectfloodlight.openflow.protocol.OFPacketOut;
 import org.projectfloodlight.openflow.protocol.OFType;
+import org.projectfloodlight.openflow.protocol.OFVersion;
 import org.projectfloodlight.openflow.protocol.action.OFAction;
 import org.projectfloodlight.openflow.protocol.action.OFActionOutput;
 import org.projectfloodlight.openflow.types.DatapathId;
@@ -348,7 +349,7 @@ public abstract class ForwardingBase implements IOFMessageListener {
 		// The assumption here is (sw) is the switch that generated the
 		// packet-in. If the input port is the same as output port, then
 		// the packet-out should be ignored.
-		if (pi.getMatch().get(MatchField.IN_PORT).equals(outport)) {
+		if ((pi.getVersion().compareTo(OFVersion.OF_13) < 0 ? pi.getInPort() : pi.getMatch().get(MatchField.IN_PORT)).equals(outport)) {
 			if (log.isDebugEnabled()) {
 				log.debug("Attempting to do packet-out to the same " +
 						"interface as packet-in. Dropping packet. " +
@@ -380,7 +381,7 @@ public abstract class ForwardingBase implements IOFMessageListener {
 			pob.setData(packetData);
 		}
 
-		pob.setInPort(pi.getMatch().get(MatchField.IN_PORT));
+		pob.setInPort((pi.getVersion().compareTo(OFVersion.OF_13) < 0 ? pi.getInPort() : pi.getMatch().get(MatchField.IN_PORT)));
 
 		try {
 			//TODO @Ryan counterStore.updatePktOutFMCounterStoreLocal(sw, po);
diff --git a/src/main/java/net/floodlightcontroller/topology/TopologyManager.java b/src/main/java/net/floodlightcontroller/topology/TopologyManager.java
index 91079108d..605dc65f6 100644
--- a/src/main/java/net/floodlightcontroller/topology/TopologyManager.java
+++ b/src/main/java/net/floodlightcontroller/topology/TopologyManager.java
@@ -69,6 +69,7 @@ import org.projectfloodlight.openflow.protocol.OFMessage;
 import org.projectfloodlight.openflow.protocol.OFPacketIn;
 import org.projectfloodlight.openflow.protocol.OFPacketOut;
 import org.projectfloodlight.openflow.protocol.OFType;
+import org.projectfloodlight.openflow.protocol.OFVersion;
 import org.projectfloodlight.openflow.protocol.action.OFAction;
 import org.projectfloodlight.openflow.protocol.match.MatchField;
 import org.projectfloodlight.openflow.types.DatapathId;
@@ -923,7 +924,7 @@ public class TopologyManager implements IFloodlightModule, ITopologyService, IRo
 	protected Command dropFilter(DatapathId sw, OFPacketIn pi,
 			FloodlightContext cntx) {
 		Command result = Command.CONTINUE;
-		OFPort port = pi.getMatch().get(MatchField.IN_PORT);
+		OFPort port = (pi.getVersion().compareTo(OFVersion.OF_13) < 0 ? pi.getInPort() : pi.getMatch().get(MatchField.IN_PORT));
 
 		// If the input port is not allowed for data traffic, drop everything.
 		// BDDP packets will not reach this stage.
-- 
GitLab