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;
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)
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');
}
}