Optimize LLDPUtil.colonize() 30/94330/3
authorRobert Varga <robert.varga@pantheon.tech>
Fri, 1 Jan 2021 21:58:00 +0000 (22:58 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Fri, 1 Jan 2021 23:19:54 +0000 (00:19 +0100)
We are performing a regex-based replacement here, pre-compile the
Pattern so we get faster replacements.

Change-Id: Ib1b09e3fbb5975ad18dd6f41e45022661bb1e220
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
applications/lldp-speaker/src/main/java/org/opendaylight/openflowplugin/applications/lldpspeaker/LLDPUtil.java

index a087dd67d59c578913db72afbc657f1948e7dbb1..639ab10cbacc8db26ae951e5647317b14a87127f 100644 (file)
@@ -11,6 +11,7 @@ import static org.opendaylight.openflowplugin.applications.topology.lldp.utils.L
 
 import com.google.common.base.Strings;
 import java.math.BigInteger;
+import java.util.regex.Pattern;
 import org.eclipse.jdt.annotation.NonNull;
 import org.opendaylight.openflowplugin.libraries.liblldp.EtherTypes;
 import org.opendaylight.openflowplugin.libraries.liblldp.Ethernet;
@@ -22,30 +23,25 @@ import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
 import org.opendaylight.yangtools.yang.common.Uint32;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
 /**
  * Utility class for dealing with LLDP packets.
  */
 public final class LLDPUtil {
-    private static final Logger LOG = LoggerFactory.getLogger(LLDPUtil.class);
-
+    private static final Pattern COLONIZE_REGEX = Pattern.compile("(?<=..)(..)");
     private static final String OF_URI_PREFIX = "openflow:";
 
     private LLDPUtil() {
+        // Hidden on purpose
     }
 
-    @NonNull
-    static byte[] buildLldpFrame(final NodeId nodeId, final NodeConnectorId nodeConnectorId, final MacAddress src,
-            final Uint32 outPortNo, final MacAddress destinationAddress)
-                    throws PacketException {
+    static byte @NonNull [] buildLldpFrame(final NodeId nodeId, final NodeConnectorId nodeConnectorId,
+            final MacAddress src, final Uint32 outPortNo, final MacAddress destinationAddress) throws PacketException {
         // Create discovery pkt
         LLDP discoveryPkt = new LLDP();
 
         // Create LLDP ChassisID TLV
-        BigInteger dataPathId = dataPathIdFromNodeId(nodeId);
-        byte[] cidValue = LLDPTLV.createChassisIDTLVValue(colonize(bigIntegerToPaddedHex(dataPathId)));
+        byte[] cidValue = LLDPTLV.createChassisIDTLVValue(colonize(paddedDataPathIdFromNodeId(nodeId)));
         LLDPTLV chassisIdTlv = new LLDPTLV();
         chassisIdTlv.setType(LLDPTLV.TLVType.ChassisID.getValue());
         chassisIdTlv.setType(LLDPTLV.TLVType.ChassisID.getValue()).setLength((short) cidValue.length)
@@ -102,22 +98,17 @@ public final class LLDPUtil {
         return ethPkt.serialize();
     }
 
-    @NonNull
-    static byte[] buildLldpFrame(final NodeId nodeId, final NodeConnectorId nodeConnectorId,
+    static byte @NonNull[] buildLldpFrame(final NodeId nodeId, final NodeConnectorId nodeConnectorId,
             final MacAddress srcMacAddress, final Uint32 outputPortNo) throws PacketException {
         return buildLldpFrame(nodeId, nodeConnectorId, srcMacAddress, outputPortNo, null);
     }
 
     private static String colonize(final String orig) {
-        return orig.replaceAll("(?<=..)(..)", ":$1");
-    }
-
-    private static BigInteger dataPathIdFromNodeId(final NodeId nodeId) {
-        String dpids = nodeId.getValue().replace(OF_URI_PREFIX, "");
-        return new BigInteger(dpids);
+        return COLONIZE_REGEX.matcher(orig).replaceAll(":$1");
     }
 
-    private static String bigIntegerToPaddedHex(final BigInteger dataPathId) {
-        return Strings.padStart(dataPathId.toString(16), 16, '0');
+    private static String paddedDataPathIdFromNodeId(final NodeId nodeId) {
+        // FIXME: there are certainly more efficient waits to perform this operation
+        return Strings.padStart(new BigInteger(nodeId.getValue().replace(OF_URI_PREFIX, "")).toString(16), 16, '0');
     }
 }