yangtools is providing ByteBufUtils, which are up to all the jobs
we really need for interacting with ByteBuf.
Since we are touching OpaqueUtil, we slightly optimize its
buildOpaqueValue() by reusing splitter/joiner instances.
Change-Id: Ic415d50c2bc1b14098f0a30b663e9d27790c5fdd
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
import java.util.List;
import javax.xml.bind.DatatypeConverter;
import org.opendaylight.protocol.util.ByteArray;
import java.util.List;
import javax.xml.bind.DatatypeConverter;
import org.opendaylight.protocol.util.ByteArray;
-import org.opendaylight.protocol.util.ByteBufWriteUtil;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.HexString;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pmsi.tunnel.rev180329.Opaque;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pmsi.tunnel.rev180329.pmsi.tunnel.pmsi.tunnel.tunnel.identifier.mldp.p2mp.lsp.mldp.p2mp.lsp.OpaqueValue;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.HexString;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pmsi.tunnel.rev180329.Opaque;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pmsi.tunnel.rev180329.pmsi.tunnel.pmsi.tunnel.tunnel.identifier.mldp.p2mp.lsp.mldp.p2mp.lsp.OpaqueValue;
public static final short EXTENDED_TYPE = 255;
private static final Logger LOG = LoggerFactory.getLogger(OpaqueUtil.class);
private static final String SEPARATOR = ":";
public static final short EXTENDED_TYPE = 255;
private static final Logger LOG = LoggerFactory.getLogger(OpaqueUtil.class);
private static final String SEPARATOR = ":";
- private static final String EMPTY_SEPARATOR = "";
+ private static final Joiner SEPARATOR_JOINER = Joiner.on(SEPARATOR);
+ private static final Splitter TWO_CHAR_SPLITTER = Splitter.fixedLength(2);
private OpaqueUtil() {
// Hidden on purpose
private OpaqueUtil() {
// Hidden on purpose
final Uint8 type = opaque.getOpaqueType();
switch (type.toJava()) {
case GENERIC_LSP_IDENTIFIER:
final Uint8 type = opaque.getOpaqueType();
switch (type.toJava()) {
case GENERIC_LSP_IDENTIFIER:
- ByteBufWriteUtil.writeUnsignedByte(type, byteBuf);
+ ByteBufUtils.write(byteBuf, type);
writeGeneric(opaque.getOpaque(), byteBuf);
break;
case EXTENDED_TYPE:
writeGeneric(opaque.getOpaque(), byteBuf);
break;
case EXTENDED_TYPE:
- ByteBufWriteUtil.writeUnsignedByte(type, byteBuf);
+ ByteBufUtils.write(byteBuf, type);
writeExtended(opaque.getOpaque(), opaque.getOpaqueExtendedType(), byteBuf);
break;
default:
writeExtended(opaque.getOpaque(), opaque.getOpaqueExtendedType(), byteBuf);
break;
default:
private static void writeExtended(final HexString opaque, final Uint16 opaqueExtendedType, final ByteBuf byteBuf) {
final byte[] output = writeOpaqueValue(opaque.getValue());
private static void writeExtended(final HexString opaque, final Uint16 opaqueExtendedType, final ByteBuf byteBuf) {
final byte[] output = writeOpaqueValue(opaque.getValue());
- ByteBufWriteUtil.writeUnsignedShort(opaqueExtendedType, byteBuf);
+ ByteBufUtils.writeOrZero(byteBuf, opaqueExtendedType);
byteBuf.writeShort(output.length);
byteBuf.writeBytes(output);
}
byteBuf.writeShort(output.length);
byteBuf.writeBytes(output);
}
}
private static byte[] writeOpaqueValue(final String opaque) {
}
private static byte[] writeOpaqueValue(final String opaque) {
- final String joined = opaque.replace(SEPARATOR, EMPTY_SEPARATOR);
- return DatatypeConverter.parseHexBinary(joined);
+ return DatatypeConverter.parseHexBinary(opaque.replace(SEPARATOR, ""));
}
public static Opaque parseOpaque(final ByteBuf buffer) {
}
public static Opaque parseOpaque(final ByteBuf buffer) {
LOG.debug("Skipping parsing of Opaque Value {}", buffer);
return null;
}
LOG.debug("Skipping parsing of Opaque Value {}", buffer);
return null;
}
- builder.setOpaqueType(type);
- return builder.build();
+ return builder.setOpaqueType(type).build();
}
private static void buildExtended(final OpaqueValueBuilder builder, final ByteBuf buffer) {
}
private static void buildExtended(final OpaqueValueBuilder builder, final ByteBuf buffer) {
private static HexString buildOpaqueValue(final ByteBuf buffer) {
final int length = buffer.readUnsignedShort();
final byte[] value = ByteArray.readBytes(buffer, length);
private static HexString buildOpaqueValue(final ByteBuf buffer) {
final int length = buffer.readUnsignedShort();
final byte[] value = ByteArray.readBytes(buffer, length);
- final String hexDump = ByteBufUtil.hexDump(value);
- final Iterable<String> splitted = Splitter.fixedLength(2).split(hexDump);
- return new HexString(Joiner.on(SEPARATOR).join(splitted));
+ return new HexString(SEPARATOR_JOINER.join(TWO_CHAR_SPLITTER.split(ByteBufUtil.hexDump(value))));
}
public static List<OpaqueValue> parseOpaqueList(final ByteBuf byteBuf) {
}
public static List<OpaqueValue> parseOpaqueList(final ByteBuf byteBuf) {
* terms of the Eclipse Public License v1.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v10.html
*/
* terms of the Eclipse Public License v1.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v10.html
*/
package org.opendaylight.protocol.bgp.mvpn.impl.attributes.tunnel.identifier;
import static org.opendaylight.protocol.bgp.mvpn.impl.attributes.OpaqueUtil.serializeOpaqueList;
package org.opendaylight.protocol.bgp.mvpn.impl.attributes.tunnel.identifier;
import static org.opendaylight.protocol.bgp.mvpn.impl.attributes.OpaqueUtil.serializeOpaqueList;
import org.opendaylight.protocol.bgp.mvpn.impl.attributes.OpaqueUtil;
import org.opendaylight.protocol.bgp.mvpn.spi.attributes.tunnel.identifier.AbstractTunnelIdentifier;
import org.opendaylight.protocol.bgp.parser.spi.AddressFamilyRegistry;
import org.opendaylight.protocol.bgp.mvpn.impl.attributes.OpaqueUtil;
import org.opendaylight.protocol.bgp.mvpn.spi.attributes.tunnel.identifier.AbstractTunnelIdentifier;
import org.opendaylight.protocol.bgp.parser.spi.AddressFamilyRegistry;
-import org.opendaylight.protocol.util.ByteBufWriteUtil;
import org.opendaylight.protocol.util.Ipv4Util;
import org.opendaylight.protocol.util.Ipv6Util;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
import org.opendaylight.protocol.util.Ipv4Util;
import org.opendaylight.protocol.util.Ipv6Util;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
return NO_TUNNEL_INFORMATION_PRESENT;
}
final IpAddress rootNode = mldpP2mpLsp.getRootNodeAddress();
return NO_TUNNEL_INFORMATION_PRESENT;
}
final IpAddress rootNode = mldpP2mpLsp.getRootNodeAddress();
- ByteBufWriteUtil.writeUnsignedByte(P2MP_TYPE, buffer);
- ByteBufWriteUtil.writeUnsignedShort(addressFamily, buffer);
- ByteBufWriteUtil.writeUnsignedByte(getAdressFamilyLength(rootNode), buffer);
+ buffer.writeByte(P2MP_TYPE);
+ buffer.writeShort(addressFamily);
+ buffer.writeByte(rootNode.getIpv4Address() != null ? Ipv4Util.IP4_LENGTH : Ipv6Util.IPV6_LENGTH);
serializeIpAddress(rootNode, buffer);
serializeIpAddress(rootNode, buffer);
- ByteBufWriteUtil.writeUnsignedShort(opaqueValues.readableBytes(), buffer);
+ buffer.writeShort(opaqueValues.readableBytes());
buffer.writeBytes(opaqueValues);
return getType();
}
buffer.writeBytes(opaqueValues);
return getType();
}
return PmsiTunnelType.MldpP2mpLsp.getIntValue();
}
return PmsiTunnelType.MldpP2mpLsp.getIntValue();
}
- private static short getAdressFamilyLength(final IpAddress ipAddress) {
- if (ipAddress.getIpv4Address() == null) {
- return Ipv6Util.IPV6_LENGTH;
- }
- return Ipv4Util.IP4_LENGTH;
- }
-
private int getAddressFamilyValue(final Class<? extends AddressFamily> addressFamily) {
final Integer type = this.addressFamilyRegistry.numberForClass(addressFamily);
private int getAddressFamilyValue(final Class<? extends AddressFamily> addressFamily) {
final Integer type = this.addressFamilyRegistry.numberForClass(addressFamily);
- if (type == null) {
- return 0;
- }
- return type;
+ return type == null ? 0 : type;
import io.netty.buffer.ByteBuf;
import org.opendaylight.protocol.bgp.mvpn.spi.attributes.tunnel.identifier.AbstractTunnelIdentifier;
import io.netty.buffer.ByteBuf;
import org.opendaylight.protocol.bgp.mvpn.spi.attributes.tunnel.identifier.AbstractTunnelIdentifier;
-import org.opendaylight.protocol.util.ByteBufWriteUtil;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pmsi.tunnel.rev180329.PmsiTunnelType;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pmsi.tunnel.rev180329.pmsi.tunnel.pmsi.tunnel.TunnelIdentifier;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pmsi.tunnel.rev180329.pmsi.tunnel.pmsi.tunnel.tunnel.identifier.RsvpTeP2mpLsp;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pmsi.tunnel.rev180329.PmsiTunnelType;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pmsi.tunnel.rev180329.pmsi.tunnel.pmsi.tunnel.TunnelIdentifier;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pmsi.tunnel.rev180329.pmsi.tunnel.pmsi.tunnel.tunnel.identifier.RsvpTeP2mpLsp;
final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pmsi.tunnel.rev180329.pmsi.tunnel.pmsi
.tunnel.tunnel.identifier.rsvp.te.p2mp.lsp
.RsvpTeP2mpLsp rsvpTeP2mpLsp = tunnelIdentifier.getRsvpTeP2mpLsp();
final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pmsi.tunnel.rev180329.pmsi.tunnel.pmsi
.tunnel.tunnel.identifier.rsvp.te.p2mp.lsp
.RsvpTeP2mpLsp rsvpTeP2mpLsp = tunnelIdentifier.getRsvpTeP2mpLsp();
- ByteBufWriteUtil.writeUnsignedInt(rsvpTeP2mpLsp.getP2mpId(), buffer);
+ ByteBufUtils.writeOrZero(buffer, rsvpTeP2mpLsp.getP2mpId());
buffer.writeZero(RESERVED);
buffer.writeZero(RESERVED);
- ByteBufWriteUtil.writeUnsignedShort(rsvpTeP2mpLsp.getTunnelId(), buffer);
+ ByteBufUtils.writeOrZero(buffer, rsvpTeP2mpLsp.getTunnelId());
serializeIpAddress(rsvpTeP2mpLsp.getExtendedTunnelId(), buffer);
return getType();
}
serializeIpAddress(rsvpTeP2mpLsp.getExtendedTunnelId(), buffer);
return getType();
}