Adding SAPI/DAPI to Renderer
[transportpce.git] / common / src / main / java / org / opendaylight / transportpce / common / mapping / PortMappingVersion221.java
index e84eefbe4d232640d02d3f7a161b7f55105998f6..f2267ce7b9839614341d077f1f1e1f8d3068c72b 100644 (file)
@@ -10,6 +10,8 @@ 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.Collections;
 import java.util.Comparator;
@@ -89,6 +91,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);
 
     public PortMappingVersion221(DataBroker dataBroker, DeviceTransactionManager deviceTransactionManager,
         OpenRoadmInterfaces openRoadmInterfaces) {
@@ -804,12 +810,16 @@ public class PortMappingVersion221 {
             mpBldr = new MappingBuilder(mapping).setConnectionMapLcp(connectionMapLcp);
         } else {
             // create a new mapping
+            String nodeIdLcp = nodeId + logicalConnectionPoint;
             mpBldr = new MappingBuilder()
                 .withKey(new MappingKey(logicalConnectionPoint))
                 .setLogicalConnectionPoint(logicalConnectionPoint)
                 .setSupportingCircuitPackName(circuitPackName)
                 .setSupportingPort(port.getPortName())
-                .setPortDirection(port.getPortDirection().getName());
+                .setPortDirection(port.getPortDirection().getName())
+                // fnv hash is generated for the combination nodeID and logical connection point; used for SAPI/DAPI
+                .setLcpHashVal(fnv(nodeIdLcp));
+
             if (port.getPortQual() != null) {
                 mpBldr.setPortQual(port.getPortQual().getName());
             }
@@ -984,4 +994,22 @@ public class PortMappingVersion221 {
         return nodeInfoBldr.build();
     }
 
+    /**
+     * Implements the FNV-1 128bit algorithm.
+     * 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 hash.toString(16);
+    }
 }