create a FnvUtils class in common mapping 41/93641/8
authorguillaume.lambert <guillaume.lambert@orange.com>
Fri, 6 Nov 2020 20:36:08 +0000 (21:36 +0100)
committerguillaume.lambert <guillaume.lambert@orange.com>
Fri, 20 Nov 2020 14:49:20 +0000 (15:49 +0100)
This way, all the various PortMapping versions now call the same FNV-1
 64 bit function. The related common code is extracted from PortMapping
classes and mutualized in FnvUtils.

JIRA: TRNSPRTPCE-352
Signed-off-by: guillaume.lambert <guillaume.lambert@orange.com>
Change-Id: I1562ed2b919cb784f4c1b833af946568f0ac75e1

common/src/main/java/org/opendaylight/transportpce/common/mapping/FnvUtils.java [new file with mode: 0644]
common/src/main/java/org/opendaylight/transportpce/common/mapping/PortMappingVersion121.java
common/src/main/java/org/opendaylight/transportpce/common/mapping/PortMappingVersion221.java
common/src/main/java/org/opendaylight/transportpce/common/mapping/PortMappingVersion710.java

diff --git a/common/src/main/java/org/opendaylight/transportpce/common/mapping/FnvUtils.java b/common/src/main/java/org/opendaylight/transportpce/common/mapping/FnvUtils.java
new file mode 100644 (file)
index 0000000..1076b0a
--- /dev/null
@@ -0,0 +1,46 @@
+/*
+ * Copyright © 2020 Orange, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+
+package org.opendaylight.transportpce.common.mapping;
+
+import java.math.BigInteger;
+import java.nio.charset.StandardCharsets;
+import java.util.Base64;
+
+public final class FnvUtils {
+
+    //FNV1 64 bit hash constants
+    private static final BigInteger FNV_PRIME = new BigInteger("100000001b3", 16);
+    private static final BigInteger FNV_INIT = new BigInteger("cbf29ce484222325", 16);
+    private static final BigInteger FNV_MOD = new BigInteger("2").pow(64);
+
+    /**
+     * Implements the FNV-1 64bit algorithm.
+     * FNV-1 128bit would be ideal for 16 bytes but we need an overhead for Base64 encoding.
+     * Otherwise, the hash cannot be stored in a UTF-8 string.
+     * https://www.wikiwand.com/en/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function#/FNV-1_hash
+     * https://github.com/pmdamora/fnv-cracker-app/blob/master/src/main/java/passwordcrack/cracking/HashChecker.java
+     * @param stringdata the String to be hashed
+     * @return the hash string
+     */
+    protected static String fnv1_64(String stringdata) {
+        BigInteger hash = FNV_INIT;
+        byte[] data = stringdata.getBytes(StandardCharsets.UTF_8);
+
+        for (byte b : data) {
+            hash = hash.multiply(FNV_PRIME).mod(FNV_MOD);
+            hash = hash.xor(BigInteger.valueOf((int) b & 0xff));
+        }
+
+        return Base64.getEncoder().encodeToString(hash.toByteArray());
+    }
+
+    private FnvUtils() {
+        //Noop - should not be called
+    }
+}
index 382d96149b418797a554e68a2189490a7c9c4628..d7def4d7a61e0956c1b9be683e864b8981571e10 100644 (file)
@@ -10,10 +10,7 @@ package org.opendaylight.transportpce.common.mapping;
 
 import com.google.common.util.concurrent.FluentFuture;
 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
-import java.math.BigInteger;
-import java.nio.charset.StandardCharsets;
 import java.util.ArrayList;
-import java.util.Base64;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.Comparator;
@@ -90,11 +87,6 @@ public class PortMappingVersion121 {
     private final DeviceTransactionManager deviceTransactionManager;
     private final OpenRoadmInterfaces openRoadmInterfaces;
 
-    //FNV1 64 bit hash constants
-    private static final BigInteger FNV_PRIME = new BigInteger("100000001b3", 16);
-    private static final BigInteger FNV_INIT = new BigInteger("cbf29ce484222325", 16);
-    private static final BigInteger FNV_MOD = new BigInteger("2").pow(64);
-
     public PortMappingVersion121(DataBroker dataBroker, DeviceTransactionManager deviceTransactionManager,
         OpenRoadmInterfaces openRoadmInterfaces) {
         this.dataBroker = dataBroker;
@@ -699,7 +691,7 @@ public class PortMappingVersion121 {
                 .setSupportingCircuitPackName(circuitPackName)
                 .setSupportingPort(port.getPortName())
                 .setPortDirection(port.getPortDirection().getName())
-                .setLcpHashVal(fnv(nodeIdLcp));
+                .setLcpHashVal(FnvUtils.fnv1_64(nodeIdLcp));
             if (port.getPortQual() != null) {
                 mpBldr.setPortQual(port.getPortQual().getName());
             }
@@ -877,25 +869,4 @@ public class PortMappingVersion121 {
         }
         return nodeInfoBldr.build();
     }
-
-    /**
-     * Implements the FNV-1 64bit algorithm.
-     * FNV-1 128bit would be ideal for 16 bytes but we need an overhead for Base64 encoding.
-     * Otherwise, the hash cannot be stored in a UTF-8 string.
-     * https://www.wikiwand.com/en/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function#/FNV-1_hash
-     * https://github.com/pmdamora/fnv-cracker-app/blob/master/src/main/java/passwordcrack/cracking/HashChecker.java
-     * @param stringdata the String to be hashed
-     * @return the base64 formatted hash string
-     */
-    private String fnv(String stringdata) {
-        BigInteger hash = FNV_INIT;
-        byte[] data = stringdata.getBytes(StandardCharsets.UTF_8);
-
-        for (byte b : data) {
-            hash = hash.multiply(FNV_PRIME).mod(FNV_MOD);
-            hash = hash.xor(BigInteger.valueOf((int) b & 0xff));
-        }
-
-        return Base64.getEncoder().encodeToString(hash.toByteArray());
-    }
 }
index 1a9af0b8bc33961963655cb9d0d1d094835db592..9669c3420a8f5fcc76abeec647b87259ac95734b 100644 (file)
@@ -10,10 +10,7 @@ package org.opendaylight.transportpce.common.mapping;
 
 import com.google.common.util.concurrent.FluentFuture;
 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
-import java.math.BigInteger;
-import java.nio.charset.StandardCharsets;
 import java.util.ArrayList;
-import java.util.Base64;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.Comparator;
@@ -105,10 +102,6 @@ public class PortMappingVersion221 {
     private final DataBroker dataBroker;
     private final DeviceTransactionManager deviceTransactionManager;
     private final OpenRoadmInterfaces openRoadmInterfaces;
-    //FNV1 64 bit hash constants
-    private static final BigInteger FNV_PRIME = new BigInteger("100000001b3", 16);
-    private static final BigInteger FNV_INIT = new BigInteger("cbf29ce484222325", 16);
-    private static final BigInteger FNV_MOD = new BigInteger("2").pow(64);
 
     public PortMappingVersion221(DataBroker dataBroker, DeviceTransactionManager deviceTransactionManager,
         OpenRoadmInterfaces openRoadmInterfaces) {
@@ -972,7 +965,7 @@ public class PortMappingVersion221 {
                 .setSupportingCircuitPackName(circuitPackName)
                 .setSupportingPort(port.getPortName())
                 .setPortDirection(port.getPortDirection().getName())
-                .setLcpHashVal(fnv(nodeIdLcp));
+                .setLcpHashVal(FnvUtils.fnv1_64(nodeIdLcp));
             if (port.getPortQual() != null) {
                 mpBldr.setPortQual(port.getPortQual().getName());
             }
@@ -1170,24 +1163,4 @@ public class PortMappingVersion221 {
         return nodeInfoBldr.build();
     }
 
-    /**
-     * Implements the FNV-1 64bit algorithm.
-     * FNV-1 128bit would be ideal for 16 bytes but we need an overhead for Base64 encoding.
-     * Otherwise, the hash cannot be stored in a UTF-8 string.
-     * https://www.wikiwand.com/en/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function#/FNV-1_hash
-     * https://github.com/pmdamora/fnv-cracker-app/blob/master/src/main/java/passwordcrack/cracking/HashChecker.java
-     * @param stringdata the String to be hashed
-     * @return the hash string
-     */
-    private String fnv(String stringdata) {
-        BigInteger hash = FNV_INIT;
-        byte[] data = stringdata.getBytes(StandardCharsets.UTF_8);
-
-        for (byte b : data) {
-            hash = hash.multiply(FNV_PRIME).mod(FNV_MOD);
-            hash = hash.xor(BigInteger.valueOf((int) b & 0xff));
-        }
-
-        return Base64.getEncoder().encodeToString(hash.toByteArray());
-    }
 }
index bac76e0dd1cc022ab70c936464f41877b0bdd242..077c949550ee54e3a8053534405cf4ff5b603e79 100644 (file)
@@ -10,10 +10,7 @@ package org.opendaylight.transportpce.common.mapping;
 
 import com.google.common.util.concurrent.FluentFuture;
 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
-import java.math.BigInteger;
-import java.nio.charset.StandardCharsets;
 import java.util.ArrayList;
-import java.util.Base64;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.Comparator;
@@ -108,10 +105,6 @@ public class PortMappingVersion710 {
     private final DataBroker dataBroker;
     private final DeviceTransactionManager deviceTransactionManager;
     private final OpenRoadmInterfaces openRoadmInterfaces;
-    //FNV1 64 bit hash constants
-    private static final BigInteger FNV_PRIME = new BigInteger("100000001b3", 16);
-    private static final BigInteger FNV_INIT = new BigInteger("cbf29ce484222325", 16);
-    private static final BigInteger FNV_MOD = new BigInteger("2").pow(64);
 
     public PortMappingVersion710(DataBroker dataBroker, DeviceTransactionManager deviceTransactionManager,
         OpenRoadmInterfaces openRoadmInterfaces) {
@@ -1015,7 +1008,7 @@ public class PortMappingVersion710 {
                 .setSupportingCircuitPackName(circuitPackName)
                 .setSupportingPort(ports.getPortName())
                 .setPortDirection(ports.getPortDirection().getName())
-                .setLcpHashVal(fnv(nodeIdLcp));
+                .setLcpHashVal(FnvUtils.fnv1_64(nodeIdLcp));
 
             if (ports.getPortQual() != null) {
                 mpBldr.setPortQual(ports.getPortQual().getName());
@@ -1215,25 +1208,4 @@ public class PortMappingVersion710 {
 
         return nodeInfoBldr.build();
     }
-
-    /**
-     * Implements the FNV-1 64bit algorithm.
-     * FNV-1 128bit would be ideal for 16 bytes but we need an overhead for Base64 encoding.
-     * Otherwise, the hash cannot be stored in a UTF-8 string.
-     * https://www.wikiwand.com/en/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function#/FNV-1_hash
-     * https://github.com/pmdamora/fnv-cracker-app/blob/master/src/main/java/passwordcrack/cracking/HashChecker.java
-     * @param stringdata the String to be hashed
-     * @return the hash string
-     */
-    private String fnv(String stringdata) {
-        BigInteger hash = FNV_INIT;
-        byte[] data = stringdata.getBytes(StandardCharsets.UTF_8);
-
-        for (byte b : data) {
-            hash = hash.multiply(FNV_PRIME).mod(FNV_MOD);
-            hash = hash.xor(BigInteger.valueOf((int) b & 0xff));
-        }
-
-        return Base64.getEncoder().encodeToString(hash.toByteArray());
-    }
 }