From: guillaume.lambert Date: Tue, 23 Jun 2020 14:14:31 +0000 (+0200) Subject: replace portmapping FNV1-128 by FNV1-64 + Base64 X-Git-Tag: 2.0.0~76 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=transportpce.git;a=commitdiff_plain;h=a276255479f5ba8a3969b0645d001cb593982afe replace portmapping FNV1-128 by FNV1-64 + Base64 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 Change-Id: I559b5439f83cffc619a1db4e29b9d78c8401ed06 --- diff --git a/common/src/main/java/org/opendaylight/transportpce/common/mapping/PortMappingVersion121.java b/common/src/main/java/org/opendaylight/transportpce/common/mapping/PortMappingVersion121.java index d006ea737..7ece44d5c 100644 --- a/common/src/main/java/org/opendaylight/transportpce/common/mapping/PortMappingVersion121.java +++ b/common/src/main/java/org/opendaylight/transportpce/common/mapping/PortMappingVersion121.java @@ -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()); } } diff --git a/common/src/main/java/org/opendaylight/transportpce/common/mapping/PortMappingVersion221.java b/common/src/main/java/org/opendaylight/transportpce/common/mapping/PortMappingVersion221.java index a939b8ce6..db21ac397 100644 --- a/common/src/main/java/org/opendaylight/transportpce/common/mapping/PortMappingVersion221.java +++ b/common/src/main/java/org/opendaylight/transportpce/common/mapping/PortMappingVersion221.java @@ -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()); } } diff --git a/tests/transportpce_tests/1.2.1/test_portmapping.py b/tests/transportpce_tests/1.2.1/test_portmapping.py index 8b495fa6f..8d8f39f3a 100644 --- a/tests/transportpce_tests/1.2.1/test_portmapping.py +++ b/tests/transportpce_tests/1.2.1/test_portmapping.py @@ -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): diff --git a/tests/transportpce_tests/1.2.1/test_renderer_service_path_nominal.py b/tests/transportpce_tests/1.2.1/test_renderer_service_path_nominal.py index 68ee4102b..32c31786e 100644 --- a/tests/transportpce_tests/1.2.1/test_renderer_service_path_nominal.py +++ b/tests/transportpce_tests/1.2.1/test_renderer_service_path_nominal.py @@ -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): diff --git a/tests/transportpce_tests/2.2.1/test_otn_renderer.py b/tests/transportpce_tests/2.2.1/test_otn_renderer.py index 6157b038a..2644439c5 100644 --- a/tests/transportpce_tests/2.2.1/test_otn_renderer.py +++ b/tests/transportpce_tests/2.2.1/test_otn_renderer.py @@ -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']) diff --git a/tests/transportpce_tests/2.2.1/test_portmapping.py b/tests/transportpce_tests/2.2.1/test_portmapping.py index 551bae62e..73015c209 100644 --- a/tests/transportpce_tests/2.2.1/test_portmapping.py +++ b/tests/transportpce_tests/2.2.1/test_portmapping.py @@ -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): diff --git a/tests/transportpce_tests/2.2.1/test_renderer_service_path_nominal.py b/tests/transportpce_tests/2.2.1/test_renderer_service_path_nominal.py index 29f4556e7..4162f5b57 100644 --- a/tests/transportpce_tests/2.2.1/test_renderer_service_path_nominal.py +++ b/tests/transportpce_tests/2.2.1/test_renderer_service_path_nominal.py @@ -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):