Fix comparison between port numbers in match
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / registry / flow / FlowRegistryKeyFactory.java
index 4371a0feaca2395f0816471c09dc6ebdbe940e8e..a691b60ba2516d8e0e30129c00834f34b503cb22 100644 (file)
@@ -13,60 +13,63 @@ import com.google.common.base.Preconditions;
 import java.math.BigInteger;
 import org.opendaylight.openflowplugin.api.OFConstants;
 import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowRegistryKey;
-import org.opendaylight.openflowplugin.impl.util.HashUtil;
+import org.opendaylight.openflowplugin.impl.util.MatchComparatorFactory;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.Flow;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
 
 /**
  * Created by Martin Bobak <mbobak@cisco.com> on 8.4.2015.
  */
 public class FlowRegistryKeyFactory {
 
-
-    public FlowRegistryKeyFactory() {
-    }
-
-    public static FlowRegistryKey create(Flow flow) {
-        return new FlowRegistryKeyDto(flow);
+    private FlowRegistryKeyFactory() {
+        // Hide implicit constructor
     }
 
-    private static long calculateHash(Flow flow, short version) {
-        return HashUtil.calculateMatchHash(flow.getMatch(), version);
+    public static FlowRegistryKey create(final short version, final Flow flow) {
+        return new FlowRegistryKeyDto(version, flow);
     }
 
-
     private static final class FlowRegistryKeyDto implements FlowRegistryKey {
-
         private final short tableId;
         private final int priority;
         private final BigInteger cookie;
         private final Match match;
+        private final short version;
 
-        public FlowRegistryKeyDto(final Flow flow) {
+        private FlowRegistryKeyDto(final short version, final Flow flow) {
+            //TODO: mandatory flow input values (or default values) should be specified via yang model
             tableId = Preconditions.checkNotNull(flow.getTableId(), "flow tableId must not be null");
-            priority = Preconditions.checkNotNull(flow.getPriority(), "flow priority must not be null");
-            match = Preconditions.checkNotNull(flow.getMatch(), "Match value must not be null");
+            priority = MoreObjects.firstNonNull(flow.getPriority(), OFConstants.DEFAULT_FLOW_PRIORITY);
+            match = MoreObjects.firstNonNull(flow.getMatch(), OFConstants.EMPTY_MATCH);
             cookie = MoreObjects.firstNonNull(flow.getCookie(), OFConstants.DEFAULT_FLOW_COOKIE).getValue();
+            this.version = version;
         }
 
         @Override
         public boolean equals(final Object o) {
-            if (this == o) return true;
-            if (o == null || getClass() != o.getClass()) return false;
+            if (this == o) {
+                return true;
+            }
 
-            final FlowRegistryKeyDto that = (FlowRegistryKeyDto) o;
+            if (o == null || !(o instanceof FlowRegistryKey)) {
+                return false;
+            }
 
-            if (priority != that.priority) return false;
-            if (tableId != that.tableId) return false;
-            if (!match.equals(that.match)) return false;
+            final FlowRegistryKey that = (FlowRegistryKey) o;
 
-            return true;
+            return getPriority() == that.getPriority() &&
+                    getTableId() == that.getTableId() &&
+                    getCookie().equals(that.getCookie()) &&
+                    MatchComparatorFactory.createMatch().areObjectsEqual(version, getMatch(), that.getMatch());
         }
 
         @Override
         public int hashCode() {
-            int result = (int) tableId;
+            int result = tableId;
             result = 31 * result + priority;
+            result = 31 * result + cookie.hashCode();
             result = 31 * result + match.hashCode();
             return result;
         }
@@ -85,5 +88,10 @@ public class FlowRegistryKeyFactory {
         public BigInteger getCookie() {
             return cookie;
         }
+
+        @Override
+        public Match getMatch() {
+            return match;
+        }
     }
 }