replace portmapping FNV1-128 by FNV1-64 + Base64 05/90605/13
authorguillaume.lambert <guillaume.lambert@orange.com>
Tue, 23 Jun 2020 14:14:31 +0000 (16:14 +0200)
committerGuillaume Lambert <guillaume.lambert@orange.com>
Wed, 24 Jun 2020 07:02:30 +0000 (07:02 +0000)
FNV1 128bit hash produces 16 bytes results but they cannot easily be
represented as a UTF-8 readable string of 16 characters as required
by SAPI/DAPI OpenROADM specification.
Base64 conversion allows to fix this but generates an overhead.
By using FNV1 64bit hash + Base64, we can guarantee the results
will not exceed 16 characters (usually around 13).

Signed-off-by: guillaume.lambert <guillaume.lambert@orange.com>
Change-Id: I559b5439f83cffc619a1db4e29b9d78c8401ed06

common/src/main/java/org/opendaylight/transportpce/common/mapping/PortMappingVersion121.java
common/src/main/java/org/opendaylight/transportpce/common/mapping/PortMappingVersion221.java
tests/transportpce_tests/1.2.1/test_portmapping.py
tests/transportpce_tests/1.2.1/test_renderer_service_path_nominal.py
tests/transportpce_tests/2.2.1/test_otn_renderer.py
tests/transportpce_tests/2.2.1/test_portmapping.py
tests/transportpce_tests/2.2.1/test_renderer_service_path_nominal.py

index d006ea737b1cd312ba12c6719d92e4e908e5e4cc..7ece44d5cd6a25180a3054c0568ef19c351041e8 100644 (file)
@@ -13,6 +13,7 @@ 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.Collections;
 import java.util.Comparator;
 import java.util.HashMap;
@@ -85,10 +86,10 @@ public class PortMappingVersion121 {
     private final DeviceTransactionManager deviceTransactionManager;
     private final OpenRoadmInterfaces openRoadmInterfaces;
 
-    //FNV1 128 bit hash constants
-    private static final BigInteger FNV_PRIME = new BigInteger("309485009821345068724781371");
-    private static final BigInteger FNV_INIT = new BigInteger("6c62272e07bb014262b821756295c58d", 16);
-    private static final BigInteger FNV_MOD = new BigInteger("2").pow(128);
+    //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) {
@@ -858,11 +859,13 @@ public class PortMappingVersion121 {
     }
 
     /**
-     * Implements the FNV-1 128bit algorithm.
+     * 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
+     * @return the base64 formatted hash string
      */
     private String fnv(String stringdata) {
         BigInteger hash = FNV_INIT;
@@ -873,6 +876,6 @@ public class PortMappingVersion121 {
             hash = hash.xor(BigInteger.valueOf((int) b & 0xff));
         }
 
-        return hash.toString(16);
+        return Base64.getEncoder().encodeToString(hash.toByteArray());
     }
 }
index a939b8ce6108802a1495493c8dcf007dcd554c6c..db21ac39744db588db8040682863eec5aae50a26 100644 (file)
@@ -13,6 +13,7 @@ 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.Collections;
 import java.util.Comparator;
 import java.util.HashMap;
@@ -91,10 +92,10 @@ public class PortMappingVersion221 {
     private final DataBroker dataBroker;
     private final DeviceTransactionManager deviceTransactionManager;
     private final OpenRoadmInterfaces openRoadmInterfaces;
-    //FNV1 128 bit hash constants
-    private static final BigInteger FNV_PRIME = new BigInteger("309485009821345068724781371");
-    private static final BigInteger FNV_INIT = new BigInteger("6c62272e07bb014262b821756295c58d", 16);
-    private static final BigInteger FNV_MOD = new BigInteger("2").pow(128);
+    //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) {
@@ -1019,7 +1020,9 @@ public class PortMappingVersion221 {
     }
 
     /**
-     * Implements the FNV-1 128bit algorithm.
+     * 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
@@ -1034,6 +1037,6 @@ public class PortMappingVersion221 {
             hash = hash.xor(BigInteger.valueOf((int) b & 0xff));
         }
 
-        return hash.toString(16);
+        return Base64.getEncoder().encodeToString(hash.toByteArray());
     }
 }
index 8b495fa6f538ad52ad144ced00c8875f76373b4b..8d8f39f3a2f6eaf5c539d2886ee4c6b539f2cf93 100644 (file)
@@ -173,7 +173,7 @@ class TransportPCEPortMappingTesting(unittest.TestCase):
             {'supporting-port': '1', 'supporting-circuit-pack-name': '1/0/1-PLUG-NET',
              'logical-connection-point': 'XPDR1-NETWORK1', 'port-direction': 'bidirectional',
              'connection-map-lcp': 'XPDR1-CLIENT1', 'port-qual': 'xpdr-network',
-             'lcp-hash-val': '3b3ab304d2a6eb3c3623e52746dbb7aa'},
+             'lcp-hash-val': 'APdT8Jzj+EVC'},
             res['mapping'])
 
     def test_11_xpdr_portmapping_NETWORK2(self):
@@ -188,7 +188,7 @@ class TransportPCEPortMappingTesting(unittest.TestCase):
             {'supporting-port': '1', 'supporting-circuit-pack-name': '1/0/2-PLUG-NET',
              'logical-connection-point': 'XPDR1-NETWORK2', 'port-direction': 'bidirectional',
              'connection-map-lcp': 'XPDR1-CLIENT3', 'port-qual': 'xpdr-network',
-             'lcp-hash-val': '3b3ab304d2a6eb3c3623e52746dbb7a9'},
+             'lcp-hash-val': 'APdT8Jzj+EVB'},
             res['mapping'])
 
     def test_12_xpdr_portmapping_CLIENT1(self):
@@ -204,7 +204,7 @@ class TransportPCEPortMappingTesting(unittest.TestCase):
              'supporting-circuit-pack-name': '1/0/C1-PLUG-CLIENT',
              'logical-connection-point': 'XPDR1-CLIENT1', 'port-direction': 'bidirectional',
              'connection-map-lcp': 'XPDR1-NETWORK1', 'port-qual': 'xpdr-client',
-             'lcp-hash-val': '64b8effe7ba72211420bf267d0ca1ae5'},
+             'lcp-hash-val': 'UHyVf5o4TF0='},
             res['mapping'])
 
     def test_13_xpdr_portmapping_CLIENT2(self):
@@ -220,7 +220,7 @@ class TransportPCEPortMappingTesting(unittest.TestCase):
              'supporting-circuit-pack-name': '1/0/C2-PLUG-CLIENT',
              'logical-connection-point': 'XPDR1-CLIENT2', 'port-direction': 'bidirectional',
              'port-qual': 'xpdr-client',
-             'lcp-hash-val': '64b8effe7ba72211420bf267d0ca1ae6'},
+             'lcp-hash-val': 'UHyVf5o4TF4='},
             res['mapping'])
 
     def test_14_xpdr_portmapping_CLIENT3(self):
@@ -236,7 +236,7 @@ class TransportPCEPortMappingTesting(unittest.TestCase):
              'supporting-circuit-pack-name': '1/0/C3-PLUG-CLIENT',
              'logical-connection-point': 'XPDR1-CLIENT3',
              'connection-map-lcp': 'XPDR1-NETWORK2', 'port-direction': 'bidirectional',
-             'port-qual': 'xpdr-client', 'lcp-hash-val': '64b8effe7ba72211420bf267d0ca1ae7'},
+             'port-qual': 'xpdr-client', 'lcp-hash-val': 'UHyVf5o4TF8='},
             res['mapping'])
 
     def test_15_xpdr_portmapping_CLIENT4(self):
@@ -251,7 +251,7 @@ class TransportPCEPortMappingTesting(unittest.TestCase):
             {'supporting-port': 'C4',
              'supporting-circuit-pack-name': '1/0/C4-PLUG-CLIENT',
              'logical-connection-point': 'XPDR1-CLIENT4', 'port-direction': 'bidirectional',
-             'port-qual': 'xpdr-client', 'lcp-hash-val': '64b8effe7ba72211420bf267d0ca1ae0'},
+             'port-qual': 'xpdr-client', 'lcp-hash-val': 'UHyVf5o4TFg='},
             res['mapping'])
 
     def test_16_xpdr_device_disconnection(self):
index 68ee4102ba00bdaea74de5a97ff59a39beaf630a..32c31786eda59235c27c2c8d1154b1587d6cbdde 100644 (file)
@@ -73,14 +73,14 @@ class TransportPCERendererTesting(unittest.TestCase):
             {'supporting-port': '1', 'supporting-circuit-pack-name': '1/0/1-PLUG-NET',
              'logical-connection-point': 'XPDR1-NETWORK1', 'port-direction': 'bidirectional',
              'connection-map-lcp': 'XPDR1-CLIENT1', 'port-qual': 'xpdr-network',
-             'lcp-hash-val': '3b3ab304d2a6eb3c3623e52746dbb7aa'},
+             'lcp-hash-val': 'APdT8Jzj+EVC'},
             res['nodes'][0]['mapping'])
         self.assertIn(
             {'supporting-port': 'C1',
              'supporting-circuit-pack-name': '1/0/C1-PLUG-CLIENT',
              'logical-connection-point': 'XPDR1-CLIENT1', 'port-direction': 'bidirectional',
              'connection-map-lcp': 'XPDR1-NETWORK1', 'port-qual': 'xpdr-client',
-             'lcp-hash-val': '64b8effe7ba72211420bf267d0ca1ae5'},
+             'lcp-hash-val': 'UHyVf5o4TF0='},
             res['nodes'][0]['mapping'])
 
     def test_05_service_path_create(self):
index 6157b038adc82e54552fb15c618f866b95a1f7bd..2644439c5835416be151d9bb35fbe4af778fb9cc 100644 (file)
@@ -72,7 +72,7 @@ class TransportPCEtesting(unittest.TestCase):
              'logical-connection-point': 'XPDR1-CLIENT1',
              'port-direction': 'bidirectional',
              'port-qual': 'xpdr-client',
-             'lcp-hash-val': '8b3efff522736722500b5e68fb6e696e'},
+             'lcp-hash-val': 'AIg960BwzGKe'},
             res['mapping'])
 
     def test_03_get_portmapping_NETWORK1(self):
@@ -93,7 +93,7 @@ class TransportPCEtesting(unittest.TestCase):
                 "port-qual": "xpdr-network",
                 "supporting-circuit-pack-name": "CP1-CFP0",
                 "xponder-type": "mpdr",
-             'lcp-hash-val': '1021db8d2affe7386705c438c67ea21f'},
+             'lcp-hash-val': 'AM7VlW5NpPm3'},
             res['mapping'])
 
     def test_04_service_path_create_OCH_OTU4(self):
@@ -137,7 +137,7 @@ class TransportPCEtesting(unittest.TestCase):
                 "port-qual": "xpdr-network",
                 "supporting-circuit-pack-name": "CP1-CFP0",
                 "xponder-type": "mpdr",
-                "lcp-hash-val": "1021db8d2affe7386705c438c67ea21f"},
+                "lcp-hash-val": "AM7VlW5NpPm3"},
             res['mapping'])
 
     def test_06_check_interface_och(self):
@@ -245,7 +245,7 @@ class TransportPCEtesting(unittest.TestCase):
                 "supporting-circuit-pack-name": "CP1-CFP0",
                 "xponder-type": "mpdr",
                 "supporting-odu4": "XPDR1-NETWORK1-ODU4",
-                "lcp-hash-val": "1021db8d2affe7386705c438c67ea21f"
+                "lcp-hash-val": "AM7VlW5NpPm3"
              },
             res['mapping'])
 
index 551bae62e306a6132d21ca47016a252762071852..73015c209a00a904ccacd1589a696a26fc60750b 100644 (file)
@@ -169,7 +169,7 @@ class TransportPCEPortMappingTesting(unittest.TestCase):
              'supporting-port': '1', 'supporting-circuit-pack-name': '1/0/1-PLUG-NET',
              'logical-connection-point': 'XPDR1-NETWORK1', 'port-qual': 'xpdr-network',
              'port-direction': 'bidirectional', 'connection-map-lcp': 'XPDR1-CLIENT1',
-             'lcp-hash-val': '8e128ba57560403cfd4ffafae38cd941'},
+             'lcp-hash-val': 'e54EtOovlcE='},
             res['mapping'])
 
     def test_12_xpdr_portmapping_NETWORK2(self):
@@ -185,7 +185,7 @@ class TransportPCEPortMappingTesting(unittest.TestCase):
              'supporting-port': '1', 'supporting-circuit-pack-name': '1/0/2-PLUG-NET',
              'logical-connection-point': 'XPDR1-NETWORK2', 'port-direction': 'bidirectional',
              'connection-map-lcp': 'XPDR1-CLIENT2', 'port-qual': 'xpdr-network',
-             'lcp-hash-val': '8e128ba57560403cfd4ffafae38cd942'},
+             'lcp-hash-val': 'e54EtOovlcI='},
             res['mapping'])
 
     def test_13_xpdr_portmapping_CLIENT1(self):
@@ -202,7 +202,7 @@ class TransportPCEPortMappingTesting(unittest.TestCase):
              'supporting-circuit-pack-name': '1/0/1-PLUG-CLIENT',
              'logical-connection-point': 'XPDR1-CLIENT1', 'port-direction': 'bidirectional',
              'connection-map-lcp': 'XPDR1-NETWORK1', 'port-qual': 'xpdr-client',
-             'lcp-hash-val': '3ed8ed1336784ac7c2f66c22f2f03d8'},
+             'lcp-hash-val': 'BIyxYXnFEFA='},
             res['mapping'])
 
     def test_14_xpdr_portmapping_CLIENT2(self):
@@ -219,7 +219,7 @@ class TransportPCEPortMappingTesting(unittest.TestCase):
              'supporting-circuit-pack-name': '1/0/2-PLUG-CLIENT',
              'logical-connection-point': 'XPDR1-CLIENT2', 'port-direction': 'bidirectional',
              'connection-map-lcp': 'XPDR1-NETWORK2', 'port-qual': 'xpdr-client',
-             'lcp-hash-val': '3ed8ed1336784ac7c2f66c22f2f03db'},
+             'lcp-hash-val': 'BIyxYXnFEFM='},
             res['mapping'])
 
     def test_15_xpdr_device_disconnection(self):
index 29f4556e7b93bb483aadbdfdde66548b3894ed49..4162f5b574531075d7a1a5d54c7114b8a24a8506 100644 (file)
@@ -71,7 +71,7 @@ class TransportPCERendererTesting(unittest.TestCase):
              'supporting-port': '1', 'supporting-circuit-pack-name': '1/0/1-PLUG-NET',
              'logical-connection-point': 'XPDR1-NETWORK1', 'port-qual': 'xpdr-network',
              'port-direction': 'bidirectional', 'connection-map-lcp': 'XPDR1-CLIENT1',
-             'lcp-hash-val': '8e128ba57560403cfd4ffafae38cd941'},
+             'lcp-hash-val': 'e54EtOovlcE='},
             res['nodes'][0]['mapping'])
         self.assertIn(
             {'supported-interface-capability': ['org-openroadm-port-types:if-100GE'],
@@ -79,7 +79,7 @@ class TransportPCERendererTesting(unittest.TestCase):
              'supporting-circuit-pack-name': '1/0/1-PLUG-CLIENT',
              'logical-connection-point': 'XPDR1-CLIENT1', 'port-direction': 'bidirectional',
              'connection-map-lcp': 'XPDR1-NETWORK1', 'port-qual': 'xpdr-client',
-             'lcp-hash-val': '3ed8ed1336784ac7c2f66c22f2f03d8'},
+             'lcp-hash-val': 'BIyxYXnFEFA='},
             res['nodes'][0]['mapping'])
 
     def test_05_service_path_create(self):