Example usage for org.apache.commons.lang3 StringUtils leftPad

List of usage examples for org.apache.commons.lang3 StringUtils leftPad

Introduction

In this page you can find the example usage for org.apache.commons.lang3 StringUtils leftPad.

Prototype

public static String leftPad(final String str, final int size, String padStr) 

Source Link

Document

Left pad a String with a specified String.

Pad to a size of size .

 StringUtils.leftPad(null, *, *)      = null StringUtils.leftPad("", 3, "z")      = "zzz" StringUtils.leftPad("bat", 3, "yz")  = "bat" StringUtils.leftPad("bat", 5, "yz")  = "yzbat" StringUtils.leftPad("bat", 8, "yz")  = "yzyzybat" StringUtils.leftPad("bat", 1, "yz")  = "bat" StringUtils.leftPad("bat", -1, "yz") = "bat" StringUtils.leftPad("bat", 5, null)  = "  bat" StringUtils.leftPad("bat", 5, "")    = "  bat" 

Usage

From source file:org.opendaylight.genius.alivenessmonitor.protocols.internal.AlivenessProtocolHandlerLLDP.java

public Ethernet makeLLDPPacket(String nodeId, long portNum, int serviceId, byte[] srcMac,
        String sourceInterface) {

    // Create LLDP TTL TLV
    LLDPTLV lldpTlvTTL = buildLLDTLV(LLDPTLV.TLVType.TTL, new byte[] { (byte) 0, (byte) 120 });

    LLDPTLV lldpTlvChassisId = buildLLDTLV(LLDPTLV.TLVType.ChassisID,
            LLDPTLV.createChassisIDTLVValue(colonize(StringUtils
                    .leftPad(Long.toHexString(MDSALUtil.getDpnIdFromNodeName(nodeId).longValue()), 16, "0"))));
    LLDPTLV lldpTlvSystemName = buildLLDTLV(TLVType.SystemName, LLDPTLV.createSystemNameTLVValue(nodeId));

    LLDPTLV lldpTlvPortId = buildLLDTLV(TLVType.PortID,
            LLDPTLV.createPortIDTLVValue(Long.toHexString(portNum)));

    String customValue = sourceInterface + "#" + getPacketId();

    LOG.debug("Sending LLDP packet, custome value " + customValue);

    LLDPTLV lldpTlvCustom = buildLLDTLV(TLVType.Custom, customValue.getBytes(LLDPTLV_CHARSET));

    @SuppressWarnings("AbbreviationAsWordInName")
    List<LLDPTLV> lstLLDPTLVCustom = new ArrayList<>();
    lstLLDPTLVCustom.add(lldpTlvCustom);

    LLDP lldpDiscoveryPacket = new LLDP();
    lldpDiscoveryPacket.setChassisId(lldpTlvChassisId).setPortId(lldpTlvPortId).setTtl(lldpTlvTTL)
            .setSystemNameId(lldpTlvSystemName).setOptionalTLVList(lstLLDPTLVCustom);

    byte[] destMac = LLDP.LLDPMulticastMac;

    Ethernet ethernetPacket = new Ethernet();
    ethernetPacket.setSourceMACAddress(srcMac).setDestinationMACAddress(destMac)
            .setEtherType(EtherTypes.LLDP.shortValue()).setPayload(lldpDiscoveryPacket);

    return ethernetPacket;
}

From source file:org.opendaylight.latency.util.LatencyPacketUtil.java

private static String bigIntegerToPaddedHex(final BigInteger dataPathId) {
    return StringUtils.leftPad(dataPathId.toString(16), 16, "0");
}

From source file:org.opendaylight.netvirt.ipv6service.utils.Ipv6ServiceUtils.java

public Ipv6Address getIpv6LinkLocalAddressFromMac(MacAddress mac) {
    byte[] octets = bytesFromHexString(mac.getValue());

    /* As per the RFC2373, steps involved to generate a LLA include
       1. Convert the 48 bit MAC address to 64 bit value by inserting 0xFFFE
      between OUI and NIC Specific part.
       2. Invert the Universal/Local flag in the OUI portion of the address.
       3. Use the prefix "FE80::/10" along with the above 64 bit Interface
      identifier to generate the IPv6 LLA. */

    StringBuffer interfaceID = new StringBuffer();
    short u8byte = (short) (octets[0] & 0xff);
    u8byte ^= 1 << 1;
    interfaceID.append(Integer.toHexString(0xFF & u8byte));
    interfaceID.append(StringUtils.leftPad(Integer.toHexString(0xFF & octets[1]), 2, "0"));
    interfaceID.append(":");
    interfaceID.append(Integer.toHexString(0xFF & octets[2]));
    interfaceID.append("ff:fe");
    interfaceID.append(StringUtils.leftPad(Integer.toHexString(0xFF & octets[3]), 2, "0"));
    interfaceID.append(":");
    interfaceID.append(Integer.toHexString(0xFF & octets[4]));
    interfaceID.append(StringUtils.leftPad(Integer.toHexString(0xFF & octets[5]), 2, "0"));

    // Return the address in its fully expanded format.
    Ipv6Address ipv6LLA = new Ipv6Address(
            InetAddresses.forString("fe80:0:0:0:" + interfaceID.toString()).getHostAddress());
    return ipv6LLA;
}

From source file:org.opendaylight.netvirt.ipv6service.utils.Ipv6ServiceUtils.java

public Ipv6Address getIpv6SolicitedNodeMcastAddress(Ipv6Address ipv6Address) {

    /* According to RFC 4291, a Solicited Node Multicast Address is derived by adding the 24
       lower order bits with the Solicited Node multicast prefix (i.e., FF02::1:FF00:0/104).
       Example: For IPv6Address of FE80::2AA:FF:FE28:9C5A, the corresponding solicited node
       multicast address would be FF02::1:FF28:9C5A
     *//*  ww w  .  j  a va 2  s  . co m*/

    byte[] octets;
    try {
        octets = InetAddress.getByName(ipv6Address.getValue()).getAddress();
    } catch (UnknownHostException e) {
        LOG.error("getIpv6SolicitedNodeMcastAddress: Failed to serialize ipv6Address ", e);
        return null;
    }

    // Return the address in its fully expanded format.
    Ipv6Address solictedV6Address = new Ipv6Address(
            InetAddresses
                    .forString(
                            "ff02::1:ff" + StringUtils.leftPad(Integer.toHexString(0xFF & octets[13]), 2, "0")
                                    + ":" + StringUtils.leftPad(Integer.toHexString(0xFF & octets[14]), 2, "0")
                                    + StringUtils.leftPad(Integer.toHexString(0xFF & octets[15]), 2, "0"))
                    .getHostAddress());

    return solictedV6Address;
}

From source file:org.opendaylight.netvirt.ipv6service.utils.Ipv6ServiceUtils.java

public MacAddress getIpv6MulticastMacAddress(Ipv6Address ipv6Address) {

    /* According to RFC 2464, a Multicast MAC address is derived by concatenating 32 lower
       order bits of IPv6 Multicast Address with the multicast prefix (i.e., 33:33).
       Example: For Multicast IPv6Address of FF02::1:FF28:9C5A, the corresponding L2 multicast
       address would be 33:33:28:9C:5A/* w  ww.j av  a 2  s.co  m*/
     */
    byte[] octets;
    try {
        octets = InetAddress.getByName(ipv6Address.getValue()).getAddress();
    } catch (UnknownHostException e) {
        LOG.error("getIpv6MulticastMacAddress: Failed to serialize ipv6Address ", e);
        return null;
    }

    StringBuffer macAddress = new StringBuffer();
    macAddress.append("33:33:");
    macAddress.append(StringUtils.leftPad(Integer.toHexString(0xFF & octets[12]), 2, "0") + ":");
    macAddress.append(StringUtils.leftPad(Integer.toHexString(0xFF & octets[13]), 2, "0") + ":");
    macAddress.append(StringUtils.leftPad(Integer.toHexString(0xFF & octets[14]), 2, "0") + ":");
    macAddress.append(StringUtils.leftPad(Integer.toHexString(0xFF & octets[15]), 2, "0"));

    return new MacAddress(macAddress.toString());
}

From source file:org.opendaylight.netvirt.it.Ipv6Utils.java

public String getIpv6AddressUsingEui64(String ipPrefix, String mac) {
    byte[] octets = bytesFromHexString(mac);

    StringBuffer interfaceID = new StringBuffer();
    short u8byte = (short) (octets[0] & 0xff);
    u8byte ^= 1 << 1;
    interfaceID.append(Integer.toHexString(0xFF & u8byte));
    interfaceID.append(StringUtils.leftPad(Integer.toHexString(0xFF & octets[1]), 2, "0"));
    interfaceID.append(":");
    interfaceID.append(Integer.toHexString(0xFF & octets[2]));
    interfaceID.append("ff:fe");
    interfaceID.append(StringUtils.leftPad(Integer.toHexString(0xFF & octets[3]), 2, "0"));
    interfaceID.append(":");
    interfaceID.append(Integer.toHexString(0xFF & octets[4]));
    interfaceID.append(StringUtils.leftPad(Integer.toHexString(0xFF & octets[5]), 2, "0"));

    Ipv6Address ipv6LLA = new Ipv6Address(
            InetAddresses.forString(ipPrefix + interfaceID.toString()).getHostAddress());
    return ipv6LLA.getValue();
}

From source file:org.opendaylight.netvirt.neutronvpn.NeutronvpnUtils.java

protected static IpAddress getIpv6LinkLocalAddressFromMac(MacAddress mac) {
    byte[] octets = bytesFromHexString(mac.getValue());

    /* As per the RFC2373, steps involved to generate a LLA include
       1. Convert the 48 bit MAC address to 64 bit value by inserting 0xFFFE
      between OUI and NIC Specific part.
       2. Invert the Universal/Local flag in the OUI portion of the address.
       3. Use the prefix "FE80::/10" along with the above 64 bit Interface
      identifier to generate the IPv6 LLA. */

    StringBuffer interfaceID = new StringBuffer();
    short u8byte = (short) (octets[0] & 0xff);
    u8byte ^= 1 << 1;
    interfaceID.append(Integer.toHexString(0xFF & u8byte));
    interfaceID.append(StringUtils.leftPad(Integer.toHexString(0xFF & octets[1]), 2, "0"));
    interfaceID.append(":");
    interfaceID.append(Integer.toHexString(0xFF & octets[2]));
    interfaceID.append("ff:fe");
    interfaceID.append(StringUtils.leftPad(Integer.toHexString(0xFF & octets[3]), 2, "0"));
    interfaceID.append(":");
    interfaceID.append(Integer.toHexString(0xFF & octets[4]));
    interfaceID.append(StringUtils.leftPad(Integer.toHexString(0xFF & octets[5]), 2, "0"));

    Ipv6Address ipv6LLA = new Ipv6Address("fe80:0:0:0:" + interfaceID.toString());
    IpAddress ipAddress = new IpAddress(ipv6LLA.getValue().toCharArray());
    return ipAddress;
}

From source file:org.opendaylight.openflowplugin.openflow.md.lldp.LLDPSpeaker.java

private byte[] lldpDataFrom(InstanceIdentifier<Node> nodeInstanceId,
        InstanceIdentifier<NodeConnector> nodeConnectorInstanceId, MacAddress src) {

    NodeId nodeId = InstanceIdentifier.keyOf(nodeInstanceId).getId();
    NodeConnectorId nodeConnectorId = InstanceIdentifier.keyOf(nodeConnectorInstanceId).getId();
    // Create LLDP TTL TLV
    byte[] ttl = new byte[] { (byte) 0, (byte) 120 };
    LLDPTLV ttlTlv = new LLDPTLV();
    ttlTlv.setType(LLDPTLV.TLVType.TTL.getValue()).setLength((short) ttl.length).setValue(ttl);

    // Create LLDP ChassisID TLV
    byte[] cidValue = LLDPTLV.createChassisIDTLVValue(colonize(StringUtils
            .leftPad(Long.toHexString(InventoryDataServiceUtil.dataPathIdFromNodeId(nodeId)), 16, "0")));
    LLDPTLV chassisIdTlv = new LLDPTLV();
    chassisIdTlv.setType(LLDPTLV.TLVType.ChassisID.getValue());
    chassisIdTlv.setType(LLDPTLV.TLVType.ChassisID.getValue()).setLength((short) cidValue.length)
            .setValue(cidValue);/*from   ww w . j a va2s.c o  m*/

    // Create LLDP SystemName TLV
    byte[] snValue = LLDPTLV.createSystemNameTLVValue(nodeId.getValue());
    LLDPTLV systemNameTlv = new LLDPTLV();
    systemNameTlv.setType(LLDPTLV.TLVType.SystemName.getValue());
    systemNameTlv.setType(LLDPTLV.TLVType.SystemName.getValue()).setLength((short) snValue.length)
            .setValue(snValue);

    // Create LLDP PortID TL
    Long portNo = InventoryDataServiceUtil.portNumberfromNodeConnectorId(nodeConnectorId);
    String hexString = Long.toHexString(portNo);
    byte[] pidValue = LLDPTLV.createPortIDTLVValue(hexString);
    LLDPTLV portIdTlv = new LLDPTLV();
    portIdTlv.setType(LLDPTLV.TLVType.PortID.getValue()).setLength((short) pidValue.length).setValue(pidValue);
    portIdTlv.setType(LLDPTLV.TLVType.PortID.getValue());

    // Create LLDP Custom TLV
    byte[] customValue = LLDPTLV.createCustomTLVValue(nodeConnectorId.getValue());
    LLDPTLV customTlv = new LLDPTLV();
    customTlv.setType(LLDPTLV.TLVType.Custom.getValue()).setLength((short) customValue.length)
            .setValue(customValue);

    // Create LLDP Custom Option list
    List<LLDPTLV> customList = new ArrayList<LLDPTLV>();
    customList.add(customTlv);

    // Create discovery pkt
    LLDP discoveryPkt = new LLDP();
    discoveryPkt.setChassisId(chassisIdTlv).setPortId(portIdTlv).setTtl(ttlTlv).setSystemNameId(systemNameTlv)
            .setOptionalTLVList(customList);

    // Create ethernet pkt
    byte[] sourceMac = HexEncode.bytesFromHexString(src.getValue());
    Ethernet ethPkt = new Ethernet();
    ethPkt.setSourceMACAddress(sourceMac).setDestinationMACAddress(LLDP.LLDPMulticastMac)
            .setEtherType(EtherTypes.LLDP.shortValue()).setPayload(discoveryPkt);

    try {
        byte[] data = ethPkt.serialize();
        return data;
    } catch (PacketException e) {
        LOG.error("Error creating LLDP packet", e);
    }
    return null;
}

From source file:org.opendaylight.openflowplugin.openflow.md.util.InventoryDataServiceUtil.java

/**
 * @param dataPathId datapath id in big interger value
 * @return string of size 16, padded with '0'
 *///  w w  w.  j  av a2  s .com
public static String bigIntegerToPaddedHex(final BigInteger dataPathId) {
    return StringUtils.leftPad(dataPathId.toString(16), 16, "0");
}

From source file:org.opendaylight.vpnservice.alivenessmonitor.internal.AlivenessProtocolHandlerLLDP.java

public Ethernet makeLLDPPacket(String nodeId, long portNum, int serviceId, byte[] srcMac,
        String sourceInterface) {

    // Create LLDP TTL TLV
    LLDPTLV lldpTlvTTL = buildLLDTLV(LLDPTLV.TLVType.TTL, new byte[] { (byte) 0, (byte) 120 });

    LLDPTLV lldpTlvChassisId = buildLLDTLV(LLDPTLV.TLVType.ChassisID,
            LLDPTLV.createChassisIDTLVValue(colonize(StringUtils
                    .leftPad(Long.toHexString(MDSALUtil.getDpnIdFromNodeName(nodeId).longValue()), 16, "0"))));
    LLDPTLV lldpTlvSystemName = buildLLDTLV(TLVType.SystemName, LLDPTLV.createSystemNameTLVValue(nodeId));

    LLDPTLV lldpTlvPortId = buildLLDTLV(TLVType.PortID,
            LLDPTLV.createPortIDTLVValue(Long.toHexString(portNum)));

    String customValue = sourceInterface + "#" + getPacketId();

    LOG.debug("Sending LLDP packet, custome value " + customValue);

    LLDPTLV lldpTlvCustom = buildLLDTLV(TLVType.Custom, customValue.getBytes());

    List<LLDPTLV> lstLLDPTLVCustom = new ArrayList<>();
    lstLLDPTLVCustom.add(lldpTlvCustom);

    LLDP lldpDiscoveryPacket = new LLDP();
    lldpDiscoveryPacket.setChassisId(lldpTlvChassisId).setPortId(lldpTlvPortId).setTtl(lldpTlvTTL)
            .setSystemNameId(lldpTlvSystemName).setOptionalTLVList(lstLLDPTLVCustom);

    byte[] destMac = LLDP.LLDPMulticastMac;

    Ethernet ethernetPacket = new Ethernet();
    ethernetPacket.setSourceMACAddress(srcMac).setDestinationMACAddress(destMac)
            .setEtherType(EtherTypes.LLDP.shortValue()).setPayload(lldpDiscoveryPacket);

    return ethernetPacket;
}