HashUtils are not used anymore 53/21053/1
authorMartin Bobak <mbobak@cisco.com>
Mon, 25 May 2015 10:02:55 +0000 (12:02 +0200)
committerMartin Bobak <mbobak@cisco.com>
Mon, 25 May 2015 13:39:17 +0000 (15:39 +0200)
We will be using Match object when calculating identifier for flow in local flow registry.
This identifier will be normalized as soon as normalization will be introduced. That
will result into device flow registry holding normalized flow keys.

Change-Id: Ieeaf1b573a34fd94c8cd2d7e932d4db8fb5ca83e
Signed-off-by: Martin Bobak <mbobak@cisco.com>
openflowplugin-api/src/main/java/org/opendaylight/openflowplugin/api/openflow/registry/flow/FlowRegistryKey.java
openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/registry/flow/DeviceFlowRegistryImpl.java
openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/registry/flow/FlowRegistryKeyFactory.java [moved from openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/registry/flow/FlowHashFactory.java with 60% similarity]
openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/services/SalFlowServiceImpl.java
openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/statistics/StatisticsGatheringUtils.java
openflowplugin-impl/src/test/java/org/opendaylight/openflowplugin/impl/registry/flow/FlowRegistryKeyFactoryTest.java

index 437faede7fa5755974e75722c8b0b079e53bd9c7..9460552a438edc2f0d08199c5ec1c952dafeda65 100644 (file)
@@ -16,12 +16,11 @@ import java.math.BigInteger;
  */
 public interface FlowRegistryKey {
 
-    long getFlowHash();
-
     short getTableId();
 
     int getPriority();
 
     BigInteger getCookie();
 
+
 }
index 4c57c5539f5cbbfdad49a8a559dce570e131db99..8b802249e1b4ef80926a84037ed0ecbf21e16f69 100644 (file)
@@ -47,6 +47,8 @@ public class DeviceFlowRegistryImpl implements DeviceFlowRegistry {
 
     @Override
     public FlowId storeIfNecessary(final FlowRegistryKey flowRegistryKey, final short tableId) {
+
+
         FlowId alienFlowId = FlowUtil.createAlienFlowId(tableId);
         FlowDescriptor alienFlowDescriptor = FlowDescriptorFactory.create(tableId, alienFlowId);
         synchronized (flowRegistry) {
similarity index 60%
rename from openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/registry/flow/FlowHashFactory.java
rename to openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/registry/flow/FlowRegistryKeyFactory.java
index e9a111a2b126a8e99060aec16e0aa4a824b3ac22..4371a0feaca2395f0816471c09dc6ebdbe940e8e 100644 (file)
@@ -10,23 +10,24 @@ package org.opendaylight.openflowplugin.impl.registry.flow;
 
 import com.google.common.base.MoreObjects;
 import com.google.common.base.Preconditions;
-import com.google.common.primitives.Longs;
 import java.math.BigInteger;
-import java.util.Objects;
 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.yang.gen.v1.urn.opendaylight.flow.types.rev131026.Flow;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;
 
 /**
  * Created by Martin Bobak &lt;mbobak@cisco.com&gt; on 8.4.2015.
  */
-public class FlowHashFactory {
+public class FlowRegistryKeyFactory {
 
 
-    public static FlowRegistryKey create(Flow flow, short version) {
-        long hash = calculateHash(flow, version);
-        return new FlowRegistryKeyDto(hash, flow);
+    public FlowRegistryKeyFactory() {
+    }
+
+    public static FlowRegistryKey create(Flow flow) {
+        return new FlowRegistryKeyDto(flow);
     }
 
     private static long calculateHash(Flow flow, short version) {
@@ -36,48 +37,38 @@ public class FlowHashFactory {
 
     private static final class FlowRegistryKeyDto implements FlowRegistryKey {
 
-        private final long flowHash;
-        private final int intHashCode;
-
         private final short tableId;
         private final int priority;
         private final BigInteger cookie;
+        private final Match match;
 
-        public FlowRegistryKeyDto(final long flowHash, final Flow flow) {
-            this.flowHash = flowHash;
-            this.intHashCode = Longs.hashCode(flowHash);
+        public FlowRegistryKeyDto(final Flow flow) {
             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");
             cookie = MoreObjects.firstNonNull(flow.getCookie(), OFConstants.DEFAULT_FLOW_COOKIE).getValue();
         }
 
-
         @Override
-        public int hashCode() {
-            return intHashCode;
-        }
+        public boolean equals(final Object o) {
+            if (this == o) return true;
+            if (o == null || getClass() != o.getClass()) return false;
 
-        @Override
-        public boolean equals(final Object obj) {
-            if (null == obj) {
-                return false;
-            }
-            if (!(obj instanceof FlowRegistryKey)) {
-                return false;
-            }
-            FlowRegistryKey that = (FlowRegistryKey) obj;
-            if (this.flowHash == that.getFlowHash()
-                    && this.tableId == that.getTableId()
-                    && this.priority == that.getPriority()
-                    && Objects.equals(this.cookie, that.getCookie())) {
-                return true;
-            }
-            return false;
+            final FlowRegistryKeyDto that = (FlowRegistryKeyDto) o;
+
+            if (priority != that.priority) return false;
+            if (tableId != that.tableId) return false;
+            if (!match.equals(that.match)) return false;
+
+            return true;
         }
 
         @Override
-        public long getFlowHash() {
-            return flowHash;
+        public int hashCode() {
+            int result = (int) tableId;
+            result = 31 * result + priority;
+            result = 31 * result + match.hashCode();
+            return result;
         }
 
         @Override
index aa6b59e884070b9c574a7a257113122cf4e79fc5..430da50c07926e4cc3305814aaf88aebf892418c 100644 (file)
@@ -26,7 +26,7 @@ import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowDescriptor
 import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowRegistryKey;
 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
 import org.opendaylight.openflowplugin.impl.registry.flow.FlowDescriptorFactory;
-import org.opendaylight.openflowplugin.impl.registry.flow.FlowHashFactory;
+import org.opendaylight.openflowplugin.impl.registry.flow.FlowRegistryKeyFactory;
 import org.opendaylight.openflowplugin.impl.util.FlowUtil;
 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.FlowConvertor;
 import org.opendaylight.openflowplugin.openflow.md.util.FlowCreatorUtil;
@@ -75,7 +75,7 @@ public class SalFlowServiceImpl extends CommonService implements SalFlowService
         }
 
         final DeviceContext deviceContext = getDeviceContext();
-        final FlowRegistryKey flowRegistryKey = FlowHashFactory.create(input, deviceContext.getPrimaryConnectionContext().getFeatures().getVersion());
+        final FlowRegistryKey flowRegistryKey = FlowRegistryKeyFactory.create(input);
         final FlowDescriptor flowDescriptor = FlowDescriptorFactory.create(input.getTableId(), flowId);
         deviceContext.getDeviceFlowRegistry().store(flowRegistryKey, flowDescriptor);
         Futures.addCallback(future, new FutureCallback<RpcResult<AddFlowOutput>>() {
@@ -114,7 +114,7 @@ public class SalFlowServiceImpl extends CommonService implements SalFlowService
                     public void onSuccess(final RpcResult<RemoveFlowOutput> o) {
                         final DeviceContext deviceContext = getDeviceContext();
                         getMessageSpy().spyMessage(input.getImplementedInterface(), MessageSpy.STATISTIC_GROUP.TO_SWITCH_SUBMIT_SUCCESS);
-                        FlowRegistryKey flowRegistryKey = FlowHashFactory.create(input, deviceContext.getPrimaryConnectionContext().getFeatures().getVersion());
+                        FlowRegistryKey flowRegistryKey = FlowRegistryKeyFactory.create(input);
                         deviceContext.getDeviceFlowRegistry().markToBeremoved(flowRegistryKey);
                     }
 
@@ -174,10 +174,9 @@ public class SalFlowServiceImpl extends CommonService implements SalFlowService
             public void onSuccess(final RpcResult<UpdateFlowOutput> o) {
                 final DeviceContext deviceContext = getDeviceContext();
                 getMessageSpy().spyMessage(input.getImplementedInterface(), MessageSpy.STATISTIC_GROUP.TO_SWITCH_SUBMIT_SUCCESS);
-                final short version = deviceContext.getPrimaryConnectionContext().getFeatures().getVersion();
-                FlowRegistryKey flowRegistryKey = FlowHashFactory.create(original, version);
+                FlowRegistryKey flowRegistryKey = FlowRegistryKeyFactory.create(original);
 
-                FlowRegistryKey updatedflowRegistryKey = FlowHashFactory.create(updated, version);
+                FlowRegistryKey updatedflowRegistryKey = FlowRegistryKeyFactory.create(updated);
                 FlowId flowId = input.getFlowRef().getValue().firstKeyOf(Flow.class, FlowKey.class).getId();
                 FlowDescriptor flowDescriptor = FlowDescriptorFactory.create(updated.getTableId(), flowId);
                 final DeviceFlowRegistry deviceFlowRegistry = deviceContext.getDeviceFlowRegistry();
index 5fda259a1c59212486690bd3464d204eb5d42d77..050a0012e640feffe462c5a8fe47b1912b53943d 100644 (file)
@@ -24,7 +24,7 @@ import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
 import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowRegistryKey;
-import org.opendaylight.openflowplugin.impl.registry.flow.FlowHashFactory;
+import org.opendaylight.openflowplugin.impl.registry.flow.FlowRegistryKeyFactory;
 import org.opendaylight.openflowplugin.impl.statistics.services.dedicated.StatisticsGatheringService;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector;
@@ -194,9 +194,8 @@ public final class StatisticsGatheringUtils {
             for (final FlowAndStatisticsMapList flowStat : flowsStatistics.getFlowAndStatisticsMapList()) {
                 final FlowBuilder flowBuilder = new FlowBuilder(flowStat);
                 short tableId = flowStat.getTableId();
-                final Short version = deviceContext.getPrimaryConnectionContext().getFeatures().getVersion();
 
-                final FlowRegistryKey flowRegistryKey = FlowHashFactory.create(flowBuilder.build(), version);
+                final FlowRegistryKey flowRegistryKey = FlowRegistryKeyFactory.create(flowBuilder.build());
                 final FlowId flowId = deviceContext.getDeviceFlowRegistry().storeIfNecessary(flowRegistryKey, tableId);
 
                 final FlowKey flowKey = new FlowKey(flowId);
index e4d4b8a27971e2b89fd7224de7b4d3a8112048d4..e7199ab61cb7f2d21d4b6e6dd5e392bcf518a459 100644 (file)
@@ -10,6 +10,7 @@ package org.opendaylight.openflowplugin.impl.registry.flow;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotEquals;
+
 import java.math.BigInteger;
 import java.util.ArrayList;
 import java.util.HashSet;
@@ -40,6 +41,7 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026
 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv4MatchBuilder;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+
 @RunWith(MockitoJUnitRunner.class)
 public class FlowRegistryKeyFactoryTest {
 
@@ -91,8 +93,8 @@ public class FlowRegistryKeyFactoryTest {
 
         HashSet<FlowRegistryKey> flowRegistryKeys = new HashSet<>();
         for (FlowAndStatisticsMapList item : flowStats.getFlowAndStatisticsMapList()) {
-            flowRegistryKeys.add(FlowHashFactory.create(item, OFConstants.OFP_VERSION_1_3));
-            flowRegistryKeys.add(FlowHashFactory.create(item, OFConstants.OFP_VERSION_1_3));
+            flowRegistryKeys.add(FlowRegistryKeyFactory.create(item));
+            flowRegistryKeys.add(FlowRegistryKeyFactory.create(item));
         }
         assertEquals(3, flowRegistryKeys.size());
     }
@@ -107,7 +109,7 @@ public class FlowRegistryKeyFactoryTest {
                 .setPriority(2)
                 .setTableId((short) 0);
 
-        FlowRegistryKey flow1Hash = FlowHashFactory.create(flow1Builder.build(), OFConstants.OFP_VERSION_1_3);
+        FlowRegistryKey flow1Hash = FlowRegistryKeyFactory.create(flow1Builder.build());
         LOG.info("flowHash1: {}", flow1Hash.hashCode());
 
 
@@ -117,7 +119,7 @@ public class FlowRegistryKeyFactoryTest {
                 .setCookie(new FlowCookie(BigInteger.valueOf(148)))
                 .setMatch(match2Builder.build());
 
-        FlowRegistryKey flow2Hash = FlowHashFactory.create(flow2Builder.build(), OFConstants.OFP_VERSION_1_3);
+        FlowRegistryKey flow2Hash = FlowRegistryKeyFactory.create(flow2Builder.build());
         LOG.info("flowHash2: {}", flow2Hash.hashCode());
 
         Assert.assertNotSame(flow1Hash, flow2Hash);
@@ -136,7 +138,7 @@ public class FlowRegistryKeyFactoryTest {
         FlowBuilder fb1 = new FlowBuilder(flow1Builder.build());
         fb1.setTableId(null);
         try {
-            FlowHashFactory.create(fb1.build(), OFConstants.OFP_VERSION_1_3);
+            FlowRegistryKeyFactory.create(fb1.build());
             Assert.fail("hash creation should have failed because of NPE");
         } catch (Exception e) {
             // expected
@@ -146,7 +148,7 @@ public class FlowRegistryKeyFactoryTest {
         FlowBuilder fb2 = new FlowBuilder(flow1Builder.build());
         fb2.setPriority(null);
         try {
-            FlowHashFactory.create(fb2.build(), OFConstants.OFP_VERSION_1_3);
+            FlowRegistryKeyFactory.create(fb2.build());
             Assert.fail("hash creation should have failed because of NPE");
         } catch (Exception e) {
             // expected
@@ -155,7 +157,7 @@ public class FlowRegistryKeyFactoryTest {
 
         FlowBuilder fb3 = new FlowBuilder(flow1Builder.build());
         fb3.setCookie(null);
-        FlowRegistryKey flowRegistryKey = FlowHashFactory.create(fb3.build(), OFConstants.OFP_VERSION_1_3);
+        FlowRegistryKey flowRegistryKey = FlowRegistryKeyFactory.create(fb3.build());
         Assert.assertNotNull(flowRegistryKey.getCookie());
         Assert.assertEquals(OFConstants.DEFAULT_COOKIE, flowRegistryKey.getCookie());
     }
@@ -165,7 +167,7 @@ public class FlowRegistryKeyFactoryTest {
         FlowsStatisticsUpdate flowStats = FLOWS_STATISTICS_UPDATE_BUILDER.build();
 
         for (FlowAndStatisticsMapList item : flowStats.getFlowAndStatisticsMapList()) {
-            FlowRegistryKey flowRegistryKey = FlowHashFactory.create(item, OFConstants.OFP_VERSION_1_3);
+            FlowRegistryKey flowRegistryKey = FlowRegistryKeyFactory.create(item);
             FlowRegistryKey lastHash = null;
             if (null != lastHash) {
                 assertNotEquals(lastHash, flowRegistryKey);