*/
package org.opendaylight.protocol.pcep.crabbe.initiated00;
+import com.google.common.base.Preconditions;
+
import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
-import org.opendaylight.protocol.pcep.impl.tlv.TlvUtil;
import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
import org.opendaylight.protocol.pcep.spi.TlvParser;
import org.opendaylight.protocol.pcep.spi.TlvSerializer;
-import org.opendaylight.protocol.util.ByteArray;
+import org.opendaylight.protocol.pcep.spi.TlvUtil;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated._00.rev140113.lsp.cleanup.tlv.LspCleanup;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated._00.rev140113.lsp.cleanup.tlv.LspCleanupBuilder;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
public static final int TYPE = 26;
@Override
- public byte[] serializeTlv(final Tlv tlv) {
- return TlvUtil.formatTlv(TYPE, ByteArray.intToBytes(((LspCleanup) tlv).getTimeout().intValue()));
+ public void serializeTlv(final Tlv tlv, final ByteBuf buffer) {
+ Preconditions.checkArgument(tlv != null, "LSPCleanupTlv is mandatory.");
+ TlvUtil.formatTlv(TYPE, Unpooled.copyInt(((LspCleanup) tlv).getTimeout().intValue()), buffer);
}
@Override
*/
package org.opendaylight.protocol.pcep.crabbe.initiated00;
+import io.netty.buffer.ByteBuf;
+
import org.opendaylight.protocol.pcep.ietf.stateful02.Stateful02OpenObjectParser;
import org.opendaylight.protocol.pcep.spi.TlvRegistry;
-import org.opendaylight.protocol.util.ByteArray;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated._00.rev140113.Tlvs1;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated._00.rev140113.Tlvs1Builder;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated._00.rev140113.lsp.cleanup.tlv.LspCleanup;
}
@Override
- public byte[] serializeTlvs(final Tlvs tlvs) {
+ public void serializeTlvs(final Tlvs tlvs, final ByteBuf body) {
if (tlvs == null) {
- return new byte[0];
+ return;
}
- final byte[] prev = super.serializeTlvs(tlvs);
- int finalLength = prev.length;
- byte[] cleanupBytes = null;
+ super.serializeTlvs(tlvs, body);
if (tlvs.getAugmentation(Tlvs1.class) != null) {
final Tlvs1 cleanupTlv = tlvs.getAugmentation(Tlvs1.class);
if (cleanupTlv.getLspCleanup() != null) {
- cleanupBytes = serializeTlv(cleanupTlv.getLspCleanup());
- finalLength += cleanupBytes.length;
+ serializeTlv(cleanupTlv.getLspCleanup(), body);
}
}
- final byte[] result = new byte[finalLength];
- ByteArray.copyWhole(prev, result, 0);
- int offset = prev.length;
- if (cleanupBytes != null) {
- ByteArray.copyWhole(cleanupBytes, result, offset);
- offset += cleanupBytes.length;
- }
- return result;
}
}
*/
package org.opendaylight.protocol.pcep.crabbe.initiated00;
+import com.google.common.base.Preconditions;
+
import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
import java.util.BitSet;
import org.opendaylight.protocol.pcep.ietf.stateful02.Stateful02StatefulCapabilityTlvParser;
-import org.opendaylight.protocol.pcep.impl.tlv.TlvUtil;
import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
+import org.opendaylight.protocol.pcep.spi.TlvUtil;
import org.opendaylight.protocol.util.ByteArray;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated._00.rev140113.Stateful1;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated._00.rev140113.Stateful1Builder;
}
@Override
- public byte[] serializeTlv(final Tlv tlv) {
- if (tlv == null) {
- throw new IllegalArgumentException("StatefulCapabilityTlv is mandatory.");
- }
+ public void serializeTlv(final Tlv tlv, final ByteBuf buffer) {
+ Preconditions.checkArgument(tlv != null, "StatefulCapabilityTlv is mandatory.");
final Stateful sct = (Stateful) tlv;
-
+ final ByteBuf body = Unpooled.buffer();
final BitSet flags = new BitSet(FLAGS_F_LENGTH * Byte.SIZE);
-
final Stateful1 sfi = sct.getAugmentation(Stateful1.class);
if (sfi != null) {
flags.set(I_FLAG_OFFSET, sfi.isInitiation());
}
- if (sct.isLspUpdateCapability() != null && sct.isLspUpdateCapability()) {
+ if (sct.isLspUpdateCapability() != null) {
flags.set(U_FLAG_OFFSET, sct.isLspUpdateCapability());
}
- if (sct.isIncludeDbVersion() != null && sct.isIncludeDbVersion()) {
+ if (sct.isIncludeDbVersion() != null) {
flags.set(S_FLAG_OFFSET, sct.isIncludeDbVersion());
}
- return TlvUtil.formatTlv(TYPE, ByteArray.bitSetToBytes(flags, FLAGS_F_LENGTH));
+ body.writeBytes(ByteArray.bitSetToBytes(flags, FLAGS_F_LENGTH));
+ TlvUtil.formatTlv(TYPE, body, buffer);
}
-}
\ No newline at end of file
+}
import com.google.common.base.Preconditions;
import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
import java.math.BigInteger;
-import org.opendaylight.protocol.pcep.impl.tlv.TlvUtil;
import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
import org.opendaylight.protocol.pcep.spi.TlvParser;
import org.opendaylight.protocol.pcep.spi.TlvSerializer;
-import org.opendaylight.protocol.util.ByteArray;
+import org.opendaylight.protocol.pcep.spi.TlvUtil;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.lsp.db.version.tlv.LspDbVersion;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.lsp.db.version.tlv.LspDbVersionBuilder;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
public static final int TYPE = 23;
- private static final int DBV_F_LENGTH = 8;
-
@Override
public LspDbVersion parseTlv(final ByteBuf buffer) throws PCEPDeserializerException {
if (buffer == null) {
}
@Override
- public byte[] serializeTlv(final Tlv tlv) {
+ public void serializeTlv(final Tlv tlv, final ByteBuf buffer) {
Preconditions.checkNotNull(tlv, "LspDbVersionTlv is mandatory.");
- final LspDbVersion lsp = (LspDbVersion) tlv;
- return TlvUtil.formatTlv(TYPE, ByteArray.longToBytes(lsp.getVersion().longValue(), DBV_F_LENGTH));
+ TlvUtil.formatTlv(TYPE, Unpooled.copyLong(((LspDbVersion) tlv).getVersion().longValue()), buffer);
}
}
flags.set(OPERATIONAL_FLAG_OFFSET);
}
body.writeByte(ByteArray.bitSetToBytes(flags, 2)[1]);
- //FIXME: switch to ByteBuf
- final byte[] tlvs = serializeTlvs(specObj.getTlvs());
- body.writeBytes(tlvs);
+ serializeTlvs(specObj.getTlvs(), body);
ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
}
- public byte[] serializeTlvs(final Tlvs tlvs) {
+ public void serializeTlvs(final Tlvs tlvs, final ByteBuf body) {
if (tlvs == null) {
- return new byte[0];
+ return;
}
- int finalLength = 0;
- byte[] rsvpErrBytes = null;
- byte[] symbBytes = null;
- byte[] dbvBytes = null;
if (tlvs.getRsvpErrorSpec() != null) {
- rsvpErrBytes = serializeTlv(tlvs.getRsvpErrorSpec());
- finalLength += rsvpErrBytes.length;
+ serializeTlv(tlvs.getRsvpErrorSpec(), body);
}
if (tlvs.getSymbolicPathName() != null) {
- symbBytes = serializeTlv(tlvs.getSymbolicPathName());
- finalLength += symbBytes.length;
+ serializeTlv(tlvs.getSymbolicPathName(), body);
}
if (tlvs.getLspDbVersion() != null) {
- dbvBytes = serializeTlv(tlvs.getLspDbVersion());
- finalLength += dbvBytes.length;
+ serializeTlv(tlvs.getLspDbVersion(), body);
}
- int offset = 0;
- final byte[] result = new byte[finalLength];
- if (rsvpErrBytes != null) {
- ByteArray.copyWhole(rsvpErrBytes, result, offset);
- offset += rsvpErrBytes.length;
- }
- if (symbBytes != null) {
- ByteArray.copyWhole(symbBytes, result, offset);
- offset += symbBytes.length;
- }
- if (dbvBytes != null) {
- ByteArray.copyWhole(dbvBytes, result, offset);
- offset += dbvBytes.length;
- }
- return result;
}
}
*/
package org.opendaylight.protocol.pcep.ietf.stateful02;
+import com.google.common.base.Preconditions;
+
import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
-import org.opendaylight.protocol.pcep.impl.tlv.TlvUtil;
import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
import org.opendaylight.protocol.pcep.spi.TlvParser;
import org.opendaylight.protocol.pcep.spi.TlvSerializer;
+import org.opendaylight.protocol.pcep.spi.TlvUtil;
import org.opendaylight.protocol.util.ByteArray;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.symbolic.path.name.tlv.SymbolicPathName;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.symbolic.path.name.tlv.SymbolicPathNameBuilder;
}
@Override
- public byte[] serializeTlv(final Tlv tlv) {
- if (tlv == null) {
- throw new IllegalArgumentException("SymbolicPathNameTlv is mandatory.");
- }
- final SymbolicPathName spn = (SymbolicPathName) tlv;
- return TlvUtil.formatTlv(TYPE, spn.getPathName().getValue());
+ public void serializeTlv(final Tlv tlv, final ByteBuf buffer) {
+ Preconditions.checkArgument(tlv != null, "SymbolicPathNameTlv is mandatory.");
+ TlvUtil.formatTlv(TYPE, Unpooled.copiedBuffer(((SymbolicPathName) tlv).getPathName().getValue()), buffer);
}
}
*/
package org.opendaylight.protocol.pcep.ietf.stateful02;
+import io.netty.buffer.ByteBuf;
+
import org.opendaylight.protocol.pcep.impl.object.PCEPLspaObjectParser;
import org.opendaylight.protocol.pcep.spi.TlvRegistry;
-import org.opendaylight.protocol.util.ByteArray;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated._00.rev140113.Tlvs2;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated._00.rev140113.Tlvs2Builder;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.symbolic.path.name.tlv.SymbolicPathName;
}
@Override
- public byte[] serializeTlvs(final Tlvs tlvs) {
+ public void serializeTlvs(final Tlvs tlvs, final ByteBuf body) {
if (tlvs == null) {
- return new byte[0];
+ return;
}
- final byte[] prev = super.serializeTlvs(tlvs);
- int finalLength = prev.length;
- byte[] nameBytes = null;
+ super.serializeTlvs(tlvs, body);
if (tlvs.getAugmentation(Tlvs2.class) != null) {
final Tlvs2 nameTlvs = tlvs.getAugmentation(Tlvs2.class);
if (nameTlvs.getSymbolicPathName() != null) {
- nameBytes = serializeTlv(nameTlvs.getSymbolicPathName());
- finalLength += nameBytes.length;
+ serializeTlv(nameTlvs.getSymbolicPathName(), body);
}
}
- final byte[] result = new byte[finalLength];
- ByteArray.copyWhole(prev, result, 0);
- int offset = prev.length;
- if (nameBytes != null) {
- ByteArray.copyWhole(nameBytes, result, offset);
- offset += nameBytes.length;
- }
- return result;
}
}
*/
package org.opendaylight.protocol.pcep.ietf.stateful02;
+import com.google.common.base.Preconditions;
+
import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
-import org.opendaylight.protocol.pcep.impl.tlv.TlvUtil;
import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
import org.opendaylight.protocol.pcep.spi.TlvParser;
import org.opendaylight.protocol.pcep.spi.TlvSerializer;
+import org.opendaylight.protocol.pcep.spi.TlvUtil;
import org.opendaylight.protocol.util.ByteArray;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.NodeIdentifier;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.node.identifier.tlv.NodeIdentifierBuilder;
public static final int TYPE = 24;
@Override
- public byte[] serializeTlv(final Tlv tlv) {
- return TlvUtil.formatTlv(
- TYPE,
- ((org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.node.identifier.tlv.NodeIdentifier) tlv).getNodeId().getValue());
+ public void serializeTlv(final Tlv tlv, final ByteBuf buffer) {
+ Preconditions.checkArgument(tlv != null, "NodeIdentifierTlv is mandatory.");
+ TlvUtil.formatTlv(TYPE, Unpooled.copiedBuffer(((org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.node.identifier.tlv.NodeIdentifier) tlv).getNodeId().getValue()), buffer);
}
@Override
*/
package org.opendaylight.protocol.pcep.ietf.stateful02;
+import io.netty.buffer.ByteBuf;
+
import org.opendaylight.protocol.pcep.impl.object.PCEPOpenObjectParser;
import org.opendaylight.protocol.pcep.spi.TlvRegistry;
-import org.opendaylight.protocol.util.ByteArray;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.Tlvs2;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.Tlvs2Builder;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.node.identifier.tlv.NodeIdentifier;
}
@Override
- public byte[] serializeTlvs(final Tlvs tlvs) {
+ public void serializeTlvs(final Tlvs tlvs, final ByteBuf body) {
if (tlvs == null) {
- return new byte[0];
+ return;
}
- final byte[] prev = super.serializeTlvs(tlvs);
- int finalLength = prev.length;
- byte[] statefulBytes = null;
- byte[] nodeIdBytes = null;
+ super.serializeTlvs(tlvs, body);
if (tlvs.getAugmentation(Tlvs2.class) != null) {
final Tlvs2 statefulTlvs = tlvs.getAugmentation(Tlvs2.class);
if (statefulTlvs.getStateful() != null) {
- statefulBytes = serializeTlv(statefulTlvs.getStateful());
- finalLength += statefulBytes.length;
+ serializeTlv(statefulTlvs.getStateful(), body);
}
if (statefulTlvs.getNodeIdentifier() != null) {
- nodeIdBytes = serializeTlv(statefulTlvs.getNodeIdentifier());
- finalLength += nodeIdBytes.length;
+ serializeTlv(statefulTlvs.getNodeIdentifier(), body);
}
}
-
- final byte[] result = new byte[finalLength];
- ByteArray.copyWhole(prev, result, 0);
- int offset = prev.length;
- if (statefulBytes != null) {
- ByteArray.copyWhole(statefulBytes, result, offset);
- offset += statefulBytes.length;
- }
- if (nodeIdBytes != null) {
- ByteArray.copyWhole(nodeIdBytes, result, offset);
- offset += nodeIdBytes.length;
- }
- return result;
}
}
*/
package org.opendaylight.protocol.pcep.ietf.stateful02;
+import com.google.common.base.Preconditions;
import com.google.common.primitives.UnsignedBytes;
import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
import java.util.BitSet;
import org.opendaylight.protocol.concepts.Ipv4Util;
import org.opendaylight.protocol.concepts.Ipv6Util;
-import org.opendaylight.protocol.pcep.impl.tlv.TlvUtil;
import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
import org.opendaylight.protocol.pcep.spi.TlvParser;
import org.opendaylight.protocol.pcep.spi.TlvSerializer;
+import org.opendaylight.protocol.pcep.spi.TlvUtil;
import org.opendaylight.protocol.util.ByteArray;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.rsvp.error.spec.tlv.RsvpErrorSpec;
public static final int TYPE = 21;
- private static final int IP4_F_LENGTH = 4;
- private static final int IP6_F_LENGTH = 16;
private static final int FLAGS_F_LENGTH = 1;
- private static final int ERROR_CODE_F_LENGTH = 1;
- private static final int ERROR_VALUE_F_LENGTH = 2;
private static final int IN_PLACE_FLAG_OFFSET = 7;
private static final int NOT_GUILTY_FLAGS_OFFSET = 6;
}
final RsvpErrorBuilder builder = new RsvpErrorBuilder();
if (buffer.readableBytes() == V4_RSVP_LENGTH) {
- builder.setNode(new IpAddress(Ipv4Util.addressForBytes(ByteArray.readBytes(buffer, IP4_F_LENGTH))));
+ builder.setNode(new IpAddress(Ipv4Util.addressForBytes(ByteArray.readBytes(buffer, Ipv4Util.IP4_LENGTH))));
} else if (buffer.readableBytes() == V6_RSVP_LENGTH) {
- builder.setNode(new IpAddress(Ipv6Util.addressForBytes(ByteArray.readBytes(buffer, IP6_F_LENGTH))));
+ builder.setNode(new IpAddress(Ipv6Util.addressForBytes(ByteArray.readBytes(buffer, Ipv6Util.IPV6_LENGTH))));
}
final BitSet flags = ByteArray.bytesToBitSet(ByteArray.readBytes(buffer, FLAGS_F_LENGTH));
builder.setFlags(new Flags(flags.get(IN_PLACE_FLAG_OFFSET), flags.get(NOT_GUILTY_FLAGS_OFFSET)));
}
@Override
- public byte[] serializeTlv(final Tlv tlv) {
- if (tlv == null) {
- throw new IllegalArgumentException("RSVPErrorSpecTlv is mandatory.");
- }
+ public void serializeTlv(final Tlv tlv, final ByteBuf buffer) {
+ Preconditions.checkArgument(tlv != null, "RSVPErrorSpecTlv is mandatory.");
final RsvpErrorSpec rsvpTlv = (RsvpErrorSpec) tlv;
final RsvpError rsvp = rsvpTlv.getRsvpError();
+ final ByteBuf body = Unpooled.buffer();
final BitSet flags = new BitSet(FLAGS_F_LENGTH * Byte.SIZE);
- flags.set(IN_PLACE_FLAG_OFFSET, rsvp.getFlags().isInPlace());
- flags.set(NOT_GUILTY_FLAGS_OFFSET, rsvp.getFlags().isNotGuilty());
- int offset = 0;
+ Flags f = rsvp.getFlags();
+ if (f.isInPlace() != null) {
+ flags.set(IN_PLACE_FLAG_OFFSET, f.isInPlace());
+ }
+ if (f.isNotGuilty() != null) {
+ flags.set(NOT_GUILTY_FLAGS_OFFSET, f.isNotGuilty());
+ }
final IpAddress node = rsvp.getNode();
- byte[] bytes;
if (node.getIpv4Address() != null) {
- bytes = new byte[V4_RSVP_LENGTH];
- ByteArray.copyWhole(Ipv4Util.bytesForAddress(node.getIpv4Address()), bytes, offset);
- offset += IP4_F_LENGTH;
+ body.writeBytes(Ipv4Util.bytesForAddress(node.getIpv4Address()));
} else {
- bytes = new byte[V6_RSVP_LENGTH];
- ByteArray.copyWhole(Ipv6Util.bytesForAddress(node.getIpv6Address()), bytes, offset);
- offset += IP6_F_LENGTH;
+ body.writeBytes(Ipv6Util.bytesForAddress(node.getIpv6Address()));
}
- bytes[offset] = ByteArray.bitSetToBytes(flags, FLAGS_F_LENGTH)[0];
- offset += FLAGS_F_LENGTH;
- bytes[offset] = UnsignedBytes.checkedCast(rsvp.getCode());
- offset += ERROR_CODE_F_LENGTH;
- final byte[] value = ByteArray.intToBytes(rsvp.getValue().intValue(), ERROR_VALUE_F_LENGTH);
- ByteArray.copyWhole(value, bytes, offset);
- return TlvUtil.formatTlv(TYPE, bytes);
+ body.writeBytes(ByteArray.bitSetToBytes(flags, FLAGS_F_LENGTH));
+ body.writeByte(rsvp.getCode());
+ body.writeShort(rsvp.getValue().shortValue());
+ TlvUtil.formatTlv(TYPE, body, buffer);
}
}
*/
package org.opendaylight.protocol.pcep.ietf.stateful02;
+import com.google.common.base.Preconditions;
+
import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
import java.util.BitSet;
-import org.opendaylight.protocol.pcep.impl.tlv.TlvUtil;
import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
import org.opendaylight.protocol.pcep.spi.TlvParser;
import org.opendaylight.protocol.pcep.spi.TlvSerializer;
+import org.opendaylight.protocol.pcep.spi.TlvUtil;
import org.opendaylight.protocol.util.ByteArray;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.stateful.capability.tlv.Stateful;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.stateful.capability.tlv.StatefulBuilder;
}
@Override
- public byte[] serializeTlv(final Tlv tlv) {
- if (tlv == null) {
- throw new IllegalArgumentException("StatefulCapabilityTlv is mandatory.");
- }
+ public void serializeTlv(final Tlv tlv, final ByteBuf buffer) {
+ Preconditions.checkArgument(tlv != null, "StatefulCapabilityTlv is mandatory.");
final Stateful sct = (Stateful) tlv;
+ final ByteBuf body = Unpooled.buffer();
final BitSet flags = new BitSet(FLAGS_F_LENGTH * Byte.SIZE);
- flags.set(U_FLAG_OFFSET, sct.isLspUpdateCapability());
- flags.set(S_FLAG_OFFSET, sct.isIncludeDbVersion());
- return TlvUtil.formatTlv(TYPE, ByteArray.bitSetToBytes(flags, FLAGS_F_LENGTH));
+ if (sct.isLspUpdateCapability() != null) {
+ flags.set(U_FLAG_OFFSET, sct.isLspUpdateCapability());
+ }
+ if (sct.isIncludeDbVersion() != null) {
+ flags.set(S_FLAG_OFFSET, sct.isIncludeDbVersion());
+ }
+ body.writeBytes(ByteArray.bitSetToBytes(flags, FLAGS_F_LENGTH));
+ TlvUtil.formatTlv(TYPE, body, buffer);
}
}
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
+import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import java.math.BigInteger;
final Stateful02StatefulCapabilityTlvParser parser = new Stateful02StatefulCapabilityTlvParser();
final Stateful tlv = new StatefulBuilder().setLspUpdateCapability(Boolean.TRUE).setIncludeDbVersion(false).build();
assertEquals(tlv, parser.parseTlv(Unpooled.wrappedBuffer(ByteArray.cutBytes(statefulBytes, 4))));
- assertArrayEquals(statefulBytes, parser.serializeTlv(tlv));
+ final ByteBuf buff = Unpooled.buffer();
+ parser.serializeTlv(tlv, buff);
+ assertArrayEquals(statefulBytes, ByteArray.getAllBytes(buff));
}
@Test
final SymbolicPathName tlv = new SymbolicPathNameBuilder().setPathName(
new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.SymbolicPathName("Med test of symbolic name".getBytes())).build();
assertEquals(tlv, parser.parseTlv(Unpooled.wrappedBuffer(ByteArray.subByte(symbolicNameBytes, 4, 25))));
- assertArrayEquals(symbolicNameBytes, parser.serializeTlv(tlv));
+ final ByteBuf buff = Unpooled.buffer();
+ parser.serializeTlv(tlv, buff);
+ assertArrayEquals(symbolicNameBytes, ByteArray.getAllBytes(buff));
}
@Test
new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.NodeIdentifier(ByteArray.subByte(
nodeIdentifierBytes, 4, 25))).build();
assertEquals(tlv, parser.parseTlv(Unpooled.wrappedBuffer(ByteArray.subByte(nodeIdentifierBytes, 4, 25))));
- assertArrayEquals(nodeIdentifierBytes, parser.serializeTlv(tlv));
+ final ByteBuf buff = Unpooled.buffer();
+ parser.serializeTlv(tlv, buff);
+ assertArrayEquals(nodeIdentifierBytes, ByteArray.getAllBytes(buff));
}
@Test
builder.setValue(5634);
final RsvpErrorSpec tlv = new RsvpErrorSpecBuilder().setRsvpError(builder.build()).build();
assertEquals(tlv, parser.parseTlv(Unpooled.wrappedBuffer(ByteArray.subByte(rsvpErrorBytes, 4, 8))));
- assertArrayEquals(rsvpErrorBytes, parser.serializeTlv(tlv));
+ final ByteBuf buff = Unpooled.buffer();
+ parser.serializeTlv(tlv, buff);
+ assertArrayEquals(rsvpErrorBytes, ByteArray.getAllBytes(buff));
}
@Test
builder.setValue(50649);
final RsvpErrorSpec tlv = new RsvpErrorSpecBuilder().setRsvpError(builder.build()).build();
assertEquals(tlv, parser.parseTlv(Unpooled.wrappedBuffer(ByteArray.subByte(rsvpError6Bytes, 4, 20))));
- assertArrayEquals(rsvpError6Bytes, parser.serializeTlv(tlv));
+ final ByteBuf buff = Unpooled.buffer();
+ parser.serializeTlv(tlv, buff);
+ assertArrayEquals(rsvpError6Bytes, ByteArray.getAllBytes(buff));
}
@Test
final Stateful02LspDbVersionTlvParser parser = new Stateful02LspDbVersionTlvParser();
final LspDbVersion tlv = new LspDbVersionBuilder().setVersion(BigInteger.valueOf(180L)).build();
assertEquals(tlv, parser.parseTlv(Unpooled.wrappedBuffer(ByteArray.cutBytes(lspDbBytes, 4))));
- assertArrayEquals(lspDbBytes, parser.serializeTlv(tlv));
+ final ByteBuf buff = Unpooled.buffer();
+ parser.serializeTlv(tlv, buff);
+ assertArrayEquals(lspDbBytes, ByteArray.getAllBytes(buff));
}
}
retBytes[3] |= (op & 7) << 4;
}
body.writeBytes(retBytes);
- // FIXME: switch to ByteBuf
- final byte[] tlvs = serializeTlvs(specObj.getTlvs());
- body.writeBytes(tlvs);
+ serializeTlvs(specObj.getTlvs(), body);
ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
}
}
public void serializeObject(final Object object, final ByteBuf buffer) {
Preconditions.checkArgument(object instanceof Srp, "Wrong instance of PCEPObject. Passed %s. Needed SrpObject.", object.getClass());
final Srp srp = (Srp) object;
- ByteBuf body = Unpooled.buffer();
- //FIXME: switch to ByteBuf
- final byte[] tlvs = serializeTlvs(srp.getTlvs());
+ final ByteBuf body = Unpooled.buffer();
final BitSet flags = new BitSet(FLAGS_SIZE * Byte.SIZE);
if (srp.getAugmentation(Srp1.class) != null && srp.getAugmentation(Srp1.class).isRemove()) {
flags.set(REMOVE_FLAG, srp.getAugmentation(Srp1.class).isRemove());
}
body.writeBytes(ByteArray.bitSetToBytes(flags, FLAGS_SIZE));
body.writeInt(srp.getOperationId().getValue().intValue());
- body.writeBytes(tlvs);
+ serializeTlvs(srp.getTlvs(), body);
ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
}
}
*/
package org.opendaylight.protocol.pcep.ietf.initiated00;
+import com.google.common.base.Preconditions;
+
import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
import java.util.BitSet;
import org.opendaylight.protocol.pcep.ietf.stateful07.Stateful07StatefulCapabilityTlvParser;
-import org.opendaylight.protocol.pcep.impl.tlv.TlvUtil;
import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
+import org.opendaylight.protocol.pcep.spi.TlvUtil;
import org.opendaylight.protocol.util.ByteArray;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated.rev131126.Stateful1;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated.rev131126.Stateful1Builder;
}
@Override
- public byte[] serializeTlv(final Tlv tlv) {
- if (tlv == null) {
- throw new IllegalArgumentException("StatefulCapabilityTlv is mandatory.");
- }
+ public void serializeTlv(final Tlv tlv, final ByteBuf buffer) {
+ Preconditions.checkArgument(tlv != null, "StatefulCapabilityTlv is mandatory.");
final Stateful sct = (Stateful) tlv;
-
+ final ByteBuf body = Unpooled.buffer();
final BitSet flags = new BitSet(FLAGS_F_LENGTH * Byte.SIZE);
-
final Stateful1 sfi = sct.getAugmentation(Stateful1.class);
if (sfi != null) {
flags.set(I_FLAG_OFFSET, sfi.isInitiation());
}
- flags.set(U_FLAG_OFFSET, sct.isLspUpdateCapability());
- return TlvUtil.formatTlv(TYPE, ByteArray.bitSetToBytes(flags, FLAGS_F_LENGTH));
+ if (sct.isLspUpdateCapability() != null) {
+ flags.set(U_FLAG_OFFSET, sct.isLspUpdateCapability());
+ }
+ body.writeBytes(ByteArray.bitSetToBytes(flags, FLAGS_F_LENGTH));
+ TlvUtil.formatTlv(TYPE, body, buffer);
}
}
*/
package org.opendaylight.protocol.pcep.ietf.stateful07;
+import com.google.common.base.Preconditions;
+
import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
import org.opendaylight.protocol.concepts.Ipv4Util;
-import org.opendaylight.protocol.pcep.impl.tlv.TlvUtil;
import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
import org.opendaylight.protocol.pcep.spi.TlvParser;
import org.opendaylight.protocol.pcep.spi.TlvSerializer;
+import org.opendaylight.protocol.pcep.spi.TlvUtil;
import org.opendaylight.protocol.util.ByteArray;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.lsp.identifiers.tlv.LspIdentifiers;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.lsp.identifiers.tlv.LspIdentifiersBuilder;
public static final int TYPE = 18;
- private static final int IP4_F_LENGTH = 4;
private static final int EX_TUNNEL_ID4_F_LENGTH = 4;
- private static final int LSP_ID_F_LENGTH = 2;
- private static final int TUNNEL_ID_F_LENGTH = 2;
-
private static final int V4_LENGTH = 16;
@Override
throw new IllegalArgumentException("Length " + buffer.readableBytes() + " does not match LSP Identifiers Ipv4 tlv length.");
}
final Ipv4Builder builder = new Ipv4Builder();
- builder.setIpv4TunnelSenderAddress(Ipv4Util.addressForBytes(ByteArray.readBytes(buffer, IP4_F_LENGTH)));
+ builder.setIpv4TunnelSenderAddress(Ipv4Util.addressForBytes(ByteArray.readBytes(buffer, Ipv4Util.IP4_LENGTH)));
final LspId lspId = new LspId((long) buffer.readUnsignedShort());
final TunnelId tunnelId = new TunnelId(buffer.readUnsignedShort());
builder.setIpv4ExtendedTunnelId(new Ipv4ExtendedTunnelId(Ipv4Util.addressForBytes(ByteArray.readBytes(buffer,
EX_TUNNEL_ID4_F_LENGTH))));
- builder.setIpv4TunnelEndpointAddress(Ipv4Util.addressForBytes(ByteArray.readBytes(buffer, IP4_F_LENGTH)));
+ builder.setIpv4TunnelEndpointAddress(Ipv4Util.addressForBytes(ByteArray.readBytes(buffer, Ipv4Util.IP4_LENGTH)));
final AddressFamily afi = new Ipv4CaseBuilder().setIpv4(builder.build()).build();
return new LspIdentifiersBuilder().setAddressFamily(afi).setLspId(lspId).setTunnelId(tunnelId).build();
}
@Override
- public byte[] serializeTlv(final Tlv tlv) {
- if (tlv == null) {
- throw new IllegalArgumentException("LspIdentifiersTlv is mandatory.");
- }
+ public void serializeTlv(final Tlv tlv, final ByteBuf buffer) {
+ Preconditions.checkArgument(tlv != null, "LspIdentifiersTlv is mandatory.");
final LspIdentifiers lsp = (LspIdentifiers) tlv;
final AddressFamily afi = lsp.getAddressFamily();
-
+ final ByteBuf body = Unpooled.buffer();
if (afi.getImplementedInterface().equals(Ipv6Case.class)) {
- return new Stateful07LSPIdentifierIpv6TlvParser().serializeTlv(tlv);
+ new Stateful07LSPIdentifierIpv6TlvParser().serializeTlv(tlv, buffer);
}
-
- final byte[] bytes = new byte[V4_LENGTH];
- int offset = 0;
final Ipv4 ipv4 = ((Ipv4Case) afi).getIpv4();
- ByteArray.copyWhole(Ipv4Util.bytesForAddress(ipv4.getIpv4TunnelSenderAddress()), bytes, offset);
- offset += IP4_F_LENGTH;
- ByteArray.copyWhole(ByteArray.longToBytes(lsp.getLspId().getValue(), LSP_ID_F_LENGTH), bytes, offset);
- offset += LSP_ID_F_LENGTH;
- ByteArray.copyWhole(ByteArray.intToBytes(lsp.getTunnelId().getValue(), TUNNEL_ID_F_LENGTH), bytes, offset);
- offset += TUNNEL_ID_F_LENGTH;
- ByteArray.copyWhole(Ipv4Util.bytesForAddress(ipv4.getIpv4ExtendedTunnelId()), bytes, offset);
- offset += EX_TUNNEL_ID4_F_LENGTH;
- ByteArray.copyWhole(Ipv4Util.bytesForAddress(ipv4.getIpv4TunnelEndpointAddress()), bytes, offset);
- return TlvUtil.formatTlv(TYPE, bytes);
+ body.writeBytes(Ipv4Util.bytesForAddress(ipv4.getIpv4TunnelSenderAddress()));
+ body.writeShort(lsp.getLspId().getValue().shortValue());
+ body.writeShort(lsp.getTunnelId().getValue().shortValue());
+ body.writeBytes(Ipv4Util.bytesForAddress(ipv4.getIpv4ExtendedTunnelId()));
+ body.writeBytes(Ipv4Util.bytesForAddress(ipv4.getIpv4TunnelEndpointAddress()));
+ TlvUtil.formatTlv(TYPE, body, buffer);
}
}
*/
package org.opendaylight.protocol.pcep.ietf.stateful07;
+import com.google.common.base.Preconditions;
+
import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
import org.opendaylight.protocol.concepts.Ipv6Util;
-import org.opendaylight.protocol.pcep.impl.tlv.TlvUtil;
import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
import org.opendaylight.protocol.pcep.spi.TlvParser;
import org.opendaylight.protocol.pcep.spi.TlvSerializer;
+import org.opendaylight.protocol.pcep.spi.TlvUtil;
import org.opendaylight.protocol.util.ByteArray;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.lsp.identifiers.tlv.LspIdentifiers;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.lsp.identifiers.tlv.LspIdentifiersBuilder;
public static final int TYPE = 19;
- private static final int IP6_F_LENGTH = 16;
private static final int EX_TUNNEL_ID6_F_LENGTH = 16;
- private static final int LSP_ID_F_LENGTH = 2;
- private static final int TUNNEL_ID_F_LENGTH = 2;
-
private static final int V6_LENGTH = 52;
@Override
throw new IllegalArgumentException("Length " + buffer.readableBytes() + " does not match LSP Identifiers Ipv6 tlv length.");
}
final Ipv6Builder builder = new Ipv6Builder();
- builder.setIpv6TunnelSenderAddress(Ipv6Util.addressForBytes(ByteArray.readBytes(buffer, IP6_F_LENGTH)));
+ builder.setIpv6TunnelSenderAddress(Ipv6Util.addressForBytes(ByteArray.readBytes(buffer, Ipv6Util.IPV6_LENGTH)));
final LspId lspId = new LspId((long) buffer.readUnsignedShort());
final TunnelId tunnelId = new TunnelId(buffer.readUnsignedShort());
builder.setIpv6ExtendedTunnelId(new Ipv6ExtendedTunnelId(Ipv6Util.addressForBytes(ByteArray.readBytes(buffer,
EX_TUNNEL_ID6_F_LENGTH))));
- builder.setIpv6TunnelEndpointAddress(Ipv6Util.addressForBytes(ByteArray.readBytes(buffer, IP6_F_LENGTH)));
+ builder.setIpv6TunnelEndpointAddress(Ipv6Util.addressForBytes(ByteArray.readBytes(buffer, Ipv6Util.IPV6_LENGTH)));
final AddressFamily afi = new Ipv6CaseBuilder().setIpv6(builder.build()).build();
return new LspIdentifiersBuilder().setAddressFamily(afi).setLspId(lspId).setTunnelId(tunnelId).build();
}
@Override
- public byte[] serializeTlv(final Tlv tlv) {
- if (tlv == null) {
- throw new IllegalArgumentException("LspIdentifiersTlv is mandatory.");
- }
- final byte[] bytes = new byte[V6_LENGTH];
- int offset = 0;
+ public void serializeTlv(final Tlv tlv, final ByteBuf buffer) {
+ Preconditions.checkArgument(tlv != null, "LspIdentifiersTlv is mandatory.");
final LspIdentifiers lsp = (LspIdentifiers) tlv;
+ final ByteBuf body = Unpooled.buffer();
final Ipv6 ipv6 = ((Ipv6Case) lsp.getAddressFamily()).getIpv6();
- ByteArray.copyWhole(Ipv6Util.bytesForAddress(ipv6.getIpv6TunnelSenderAddress()), bytes, offset);
- offset += IP6_F_LENGTH;
- ByteArray.copyWhole(ByteArray.longToBytes(lsp.getLspId().getValue(), LSP_ID_F_LENGTH), bytes, offset);
- offset += LSP_ID_F_LENGTH;
- ByteArray.copyWhole(ByteArray.intToBytes(lsp.getTunnelId().getValue(), TUNNEL_ID_F_LENGTH), bytes, offset);
- offset += TUNNEL_ID_F_LENGTH;
- ByteArray.copyWhole(Ipv6Util.bytesForAddress(ipv6.getIpv6ExtendedTunnelId()), bytes, offset);
- offset += EX_TUNNEL_ID6_F_LENGTH;
- ByteArray.copyWhole(Ipv6Util.bytesForAddress(ipv6.getIpv6TunnelEndpointAddress()), bytes, offset);
- return TlvUtil.formatTlv(TYPE, bytes);
+ body.writeBytes(Ipv6Util.bytesForAddress(ipv6.getIpv6TunnelSenderAddress()));
+ body.writeShort(lsp.getLspId().getValue().shortValue());
+ body.writeShort(lsp.getTunnelId().getValue().shortValue());
+ body.writeBytes(Ipv6Util.bytesForAddress(ipv6.getIpv6ExtendedTunnelId()));
+ body.writeBytes(Ipv6Util.bytesForAddress(ipv6.getIpv6TunnelEndpointAddress()));
+ TlvUtil.formatTlv(TYPE, body, buffer);
}
}
retBytes[3] |= (op & 7) << 4;
}
body.writeBytes(retBytes);
- // FIXME: switch to ByteBuf
- final byte[] tlvs = serializeTlvs(specObj.getTlvs());
- body.writeBytes(tlvs);
+ serializeTlvs(specObj.getTlvs(), body);
ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
}
- public byte[] serializeTlvs(final Tlvs tlvs) {
+ public void serializeTlvs(final Tlvs tlvs, final ByteBuf body) {
if (tlvs == null) {
- return new byte[0];
+ return;
}
- int finalLength = 0;
- byte[] lspErrBytes = null;
- byte[] lspIdBytes = null;
- byte[] rsvpErrBytes = null;
- byte[] symbBytes = null;
- byte[] vsTlvBytes = null;
if (tlvs.getLspErrorCode() != null) {
- lspErrBytes = serializeTlv(tlvs.getLspErrorCode());
- finalLength += lspErrBytes.length;
+ serializeTlv(tlvs.getLspErrorCode(), body);
}
if (tlvs.getLspIdentifiers() != null) {
- lspIdBytes = serializeTlv(tlvs.getLspIdentifiers());
- finalLength += lspIdBytes.length;
+ serializeTlv(tlvs.getLspIdentifiers(), body);
}
if (tlvs.getRsvpErrorSpec() != null) {
- rsvpErrBytes = serializeTlv(tlvs.getRsvpErrorSpec());
- finalLength += rsvpErrBytes.length;
+ serializeTlv(tlvs.getRsvpErrorSpec(), body);
}
if (tlvs.getSymbolicPathName() != null) {
- symbBytes = serializeTlv(tlvs.getSymbolicPathName());
- finalLength += symbBytes.length;
+ serializeTlv(tlvs.getSymbolicPathName(), body);
}
if (tlvs.getVsTlv() != null) {
- vsTlvBytes = serializeTlv(tlvs.getVsTlv());
- finalLength += vsTlvBytes.length;
+ serializeTlv(tlvs.getVsTlv(), body);
}
- int offset = 0;
- final byte[] result = new byte[finalLength];
- if (lspErrBytes != null) {
- ByteArray.copyWhole(lspErrBytes, result, offset);
- offset += lspErrBytes.length;
- }
- if (lspIdBytes != null) {
- ByteArray.copyWhole(lspIdBytes, result, offset);
- offset += lspIdBytes.length;
- }
- if (rsvpErrBytes != null) {
- ByteArray.copyWhole(rsvpErrBytes, result, offset);
- offset += rsvpErrBytes.length;
- }
- if (symbBytes != null) {
- ByteArray.copyWhole(symbBytes, result, offset);
- offset += symbBytes.length;
- }
- if (vsTlvBytes != null) {
- ByteArray.copyWhole(vsTlvBytes, result, offset);
- offset += vsTlvBytes.length;
- }
- return result;
}
}
*/
package org.opendaylight.protocol.pcep.ietf.stateful07;
+import com.google.common.base.Preconditions;
+
import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
-import org.opendaylight.protocol.pcep.impl.tlv.TlvUtil;
import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
import org.opendaylight.protocol.pcep.spi.TlvParser;
import org.opendaylight.protocol.pcep.spi.TlvSerializer;
+import org.opendaylight.protocol.pcep.spi.TlvUtil;
import org.opendaylight.protocol.util.ByteArray;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.symbolic.path.name.tlv.SymbolicPathName;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.symbolic.path.name.tlv.SymbolicPathNameBuilder;
}
@Override
- public byte[] serializeTlv(final Tlv tlv) {
- if (tlv == null) {
- throw new IllegalArgumentException("SymbolicPathNameTlv is mandatory.");
- }
- final SymbolicPathName spn = (SymbolicPathName) tlv;
- return TlvUtil.formatTlv(TYPE, spn.getPathName().getValue());
+ public void serializeTlv(final Tlv tlv, final ByteBuf buffer) {
+ Preconditions.checkArgument(tlv != null, "SymbolicPathNameTlv is mandatory.");
+ TlvUtil.formatTlv(TYPE, Unpooled.copiedBuffer(((SymbolicPathName) tlv).getPathName().getValue()), buffer);
}
}
*/
package org.opendaylight.protocol.pcep.ietf.stateful07;
+import com.google.common.base.Preconditions;
+
import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
-import org.opendaylight.protocol.pcep.impl.tlv.TlvUtil;
import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
import org.opendaylight.protocol.pcep.spi.TlvParser;
import org.opendaylight.protocol.pcep.spi.TlvSerializer;
-import org.opendaylight.protocol.util.ByteArray;
+import org.opendaylight.protocol.pcep.spi.TlvUtil;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.lsp.error.code.tlv.LspErrorCode;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.lsp.error.code.tlv.LspErrorCodeBuilder;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
public static final int TYPE = 20;
- private static final int UPDATE_ERR_CODE_LENGTH = 4;
-
@Override
public LspErrorCode parseTlv(final ByteBuf buffer) throws PCEPDeserializerException {
if (buffer == null) {
}
@Override
- public byte[] serializeTlv(final Tlv tlv) {
- if (tlv == null) {
- throw new IllegalArgumentException("LspErrorCodeTlv is mandatory.");
- }
- final LspErrorCode lsp = (LspErrorCode) tlv;
- return TlvUtil.formatTlv(TYPE, ByteArray.longToBytes(lsp.getErrorCode(), UPDATE_ERR_CODE_LENGTH));
+ public void serializeTlv(final Tlv tlv, final ByteBuf buffer) {
+ Preconditions.checkArgument(tlv != null, "LspErrorCodeTlv is mandatory.");
+ TlvUtil.formatTlv(TYPE, Unpooled.copyInt(((LspErrorCode) tlv).getErrorCode().intValue()), buffer);
}
}
*/
package org.opendaylight.protocol.pcep.ietf.stateful07;
+import io.netty.buffer.ByteBuf;
+
import org.opendaylight.protocol.pcep.impl.object.PCEPLspaObjectParser;
import org.opendaylight.protocol.pcep.spi.TlvRegistry;
-import org.opendaylight.protocol.util.ByteArray;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.Tlvs2;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.Tlvs2Builder;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.symbolic.path.name.tlv.SymbolicPathName;
}
@Override
- public byte[] serializeTlvs(final Tlvs tlvs) {
+ public void serializeTlvs(final Tlvs tlvs, final ByteBuf body) {
if (tlvs == null) {
- return new byte[0];
+ return;
}
- final byte[] prev = super.serializeTlvs(tlvs);
- int finalLength = prev.length;
- byte[] nameBytes = null;
+ super.serializeTlvs(tlvs, body);
if (tlvs.getAugmentation(Tlvs2.class) != null) {
final Tlvs2 nameTlvs = tlvs.getAugmentation(Tlvs2.class);
if (nameTlvs.getSymbolicPathName() != null) {
- nameBytes = serializeTlv(nameTlvs.getSymbolicPathName());
- finalLength += nameBytes.length;
+ serializeTlv(nameTlvs.getSymbolicPathName(), body);
}
}
- final byte[] result = new byte[finalLength];
- ByteArray.copyWhole(prev, result, 0);
- int offset = prev.length;
- if (nameBytes != null) {
- ByteArray.copyWhole(nameBytes, result, offset);
- offset += nameBytes.length;
- }
- return result;
}
}
*/
package org.opendaylight.protocol.pcep.ietf.stateful07;
+import io.netty.buffer.ByteBuf;
+
import org.opendaylight.protocol.pcep.impl.object.PCEPOpenObjectParser;
import org.opendaylight.protocol.pcep.spi.TlvRegistry;
-import org.opendaylight.protocol.util.ByteArray;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.Tlvs1;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.Tlvs1Builder;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.stateful.capability.tlv.Stateful;
}
@Override
- public byte[] serializeTlvs(final Tlvs tlvs) {
+ public void serializeTlvs(final Tlvs tlvs, final ByteBuf body) {
if (tlvs == null) {
- return new byte[0];
+ return;
}
- final byte[] prev = super.serializeTlvs(tlvs);
- int finalLength = prev.length;
- byte[] ofListBytes = null;
- byte[] statefulBytes = null;
+ super.serializeTlvs(tlvs, body);
if (tlvs.getOfList() != null) {
- ofListBytes = serializeTlv(tlvs.getOfList());
- finalLength += ofListBytes.length;
+ serializeTlv(tlvs.getOfList(), body);
}
if (tlvs.getAugmentation(Tlvs1.class) != null) {
final Tlvs1 statefulTlvs = tlvs.getAugmentation(Tlvs1.class);
if (statefulTlvs.getStateful() != null) {
- statefulBytes = serializeTlv(statefulTlvs.getStateful());
- finalLength += statefulBytes.length;
+ serializeTlv(statefulTlvs.getStateful(), body);
}
}
- final byte[] result = new byte[finalLength];
- ByteArray.copyWhole(prev, result, 0);
- int offset = prev.length;
- if (ofListBytes != null) {
- ByteArray.copyWhole(ofListBytes, result, offset);
- offset += ofListBytes.length;
- }
- if (statefulBytes != null) {
- ByteArray.copyWhole(statefulBytes, result, offset);
- offset += statefulBytes.length;
- }
- return result;
}
}
*/
package org.opendaylight.protocol.pcep.ietf.stateful07;
+import com.google.common.base.Preconditions;
import com.google.common.primitives.UnsignedBytes;
import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
import java.util.BitSet;
import org.opendaylight.protocol.concepts.Ipv4Util;
import org.opendaylight.protocol.concepts.Ipv6Util;
-import org.opendaylight.protocol.pcep.impl.tlv.TlvUtil;
import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
import org.opendaylight.protocol.pcep.spi.TlvParser;
import org.opendaylight.protocol.pcep.spi.TlvSerializer;
+import org.opendaylight.protocol.pcep.spi.TlvUtil;
import org.opendaylight.protocol.util.ByteArray;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.iana.rev130816.EnterpriseNumber;
public static final int TYPE = 21;
- private static final int IP4_F_LENGTH = 4;
- private static final int IP6_F_LENGTH = 16;
private static final int FLAGS_F_LENGTH = 1;
- private static final int ERROR_CODE_F_LENGTH = 1;
- private static final int ERROR_VALUE_F_LENGTH = 2;
-
- private static final int ENTERPRISE_F_LENGTH = 4;
- private static final int SUB_ORG_F_LENGTH = 1;
- private static final int ERR_DESCR_LENGTH_F_LENGTH = 1;
- private static final int USER_VALUE_F_LENGTH = 2;
private static final int RSVP_ERROR_CLASS_NUM = 6;
private static final int RSVP_IPV4_ERROR_CLASS_TYPE = 1;
private static final int IN_PLACE_FLAG_OFFSET = 7;
private static final int NOT_GUILTY_FLAGS_OFFSET = 6;
- private static final int V4_RSVP_LENGTH = 10;
- private static final int V6_RSVP_LENGTH = 22;
-
@Override
public RsvpErrorSpec parseTlv(final ByteBuf buffer) throws PCEPDeserializerException {
if (buffer == null) {
}
@Override
- public byte[] serializeTlv(final Tlv tlv) {
- if (tlv == null) {
- throw new IllegalArgumentException("RSVPErrorSpecTlv is mandatory.");
- }
+ public void serializeTlv(final Tlv tlv, final ByteBuf buffer) {
+ Preconditions.checkArgument(tlv != null, "RSVPErrorSpecTlv is mandatory.");
final RsvpErrorSpec rsvp = (RsvpErrorSpec) tlv;
-
+ final ByteBuf body = Unpooled.buffer();
if (rsvp.getErrorType().getImplementedInterface().equals(RsvpCase.class)) {
final RsvpCase r = (RsvpCase) rsvp.getErrorType();
- return TlvUtil.formatTlv(TYPE, serializeRsvp(r.getRsvpError()));
+ serializeRsvp(r.getRsvpError(), body);
+ TlvUtil.formatTlv(TYPE, body, buffer);
} else {
final UserCase u = (UserCase) rsvp.getErrorType();
- return TlvUtil.formatTlv(TYPE, serializerUserError(u.getUserError()));
+ serializerUserError(u.getUserError(), body);
+ TlvUtil.formatTlv(TYPE, body, buffer);
}
}
return new UserCaseBuilder().setUserError(error.build()).build();
}
- private byte[] serializerUserError(final UserError ue) {
- final byte[] enterprise = ByteArray.longToBytes(ue.getEnterprise().getValue(), ENTERPRISE_F_LENGTH);
- final byte suborg = UnsignedBytes.checkedCast(ue.getSubOrg());
- final byte[] value = ByteArray.intToBytes(ue.getValue(), USER_VALUE_F_LENGTH);
- final byte[] desc = (ue.getDescription() == null) ? new byte[0] : ue.getDescription().getBytes();
- final byte descLen = UnsignedBytes.checkedCast(desc.length);
- // if we have any subobjects, place the implementation here
- final byte[] bytes = new byte[2 + ENTERPRISE_F_LENGTH + SUB_ORG_F_LENGTH + USER_VALUE_F_LENGTH + ERR_DESCR_LENGTH_F_LENGTH
- + desc.length];
- bytes[0] = UnsignedBytes.checkedCast(USER_ERROR_CLASS_NUM);
- bytes[1] = UnsignedBytes.checkedCast(USER_ERROR_CLASS_TYPE);
- int offset = 2;
- ByteArray.copyWhole(enterprise, bytes, offset);
- offset += ENTERPRISE_F_LENGTH;
- bytes[offset] = suborg;
- offset += SUB_ORG_F_LENGTH;
- bytes[offset] = descLen;
- offset += ERR_DESCR_LENGTH_F_LENGTH;
- ByteArray.copyWhole(value, bytes, offset);
- offset += USER_VALUE_F_LENGTH;
- ByteArray.copyWhole(desc, bytes, offset);
- return bytes;
+ private void serializerUserError(final UserError ue, final ByteBuf body) {
+ byte[] desc = (ue.getDescription() == null) ? new byte[0] : ue.getDescription().getBytes();
+ body.writeByte(USER_ERROR_CLASS_NUM);
+ body.writeByte(USER_ERROR_CLASS_TYPE);
+ body.writeInt(ue.getEnterprise().getValue().intValue());
+ body.writeByte(ue.getSubOrg());
+ body.writeByte(desc.length);
+ body.writeShort(ue.getValue().shortValue());
+ body.writeBytes(desc);
}
private RsvpCase parseRsvp(final int classType, final ByteBuf buffer) {
final RsvpErrorBuilder builder = new RsvpErrorBuilder();
if (classType == RSVP_IPV4_ERROR_CLASS_TYPE) {
- builder.setNode(new IpAddress(Ipv4Util.addressForBytes(ByteArray.readBytes(buffer, IP4_F_LENGTH))));
+ builder.setNode(new IpAddress(Ipv4Util.addressForBytes(ByteArray.readBytes(buffer, Ipv4Util.IP4_LENGTH))));
} else if (classType == RSVP_IPV6_ERROR_CLASS_TYPE) {
- builder.setNode(new IpAddress(Ipv6Util.addressForBytes(ByteArray.readBytes(buffer, IP6_F_LENGTH))));
+ builder.setNode(new IpAddress(Ipv6Util.addressForBytes(ByteArray.readBytes(buffer, Ipv6Util.IPV6_LENGTH))));
}
final BitSet flags = ByteArray.bytesToBitSet(ByteArray.readBytes(buffer, FLAGS_F_LENGTH));
builder.setFlags(new Flags(flags.get(IN_PLACE_FLAG_OFFSET), flags.get(NOT_GUILTY_FLAGS_OFFSET)));
return new RsvpCaseBuilder().setRsvpError(builder.build()).build();
}
- private byte[] serializeRsvp(final RsvpError rsvp) {
+ private void serializeRsvp(final RsvpError rsvp, final ByteBuf body) {
final BitSet flags = new BitSet(FLAGS_F_LENGTH * Byte.SIZE);
flags.set(IN_PLACE_FLAG_OFFSET, rsvp.getFlags().isInPlace());
flags.set(NOT_GUILTY_FLAGS_OFFSET, rsvp.getFlags().isNotGuilty());
- int offset = 0;
final IpAddress node = rsvp.getNode();
- byte[] bytes;
if (node.getIpv4Address() != null) {
- bytes = new byte[V4_RSVP_LENGTH];
- bytes[0] = RSVP_ERROR_CLASS_NUM;
- bytes[1] = RSVP_IPV4_ERROR_CLASS_TYPE;
- offset += 2;
- ByteArray.copyWhole(Ipv4Util.bytesForAddress(node.getIpv4Address()), bytes, offset);
- offset += IP4_F_LENGTH;
+ body.writeByte(RSVP_ERROR_CLASS_NUM);
+ body.writeByte(RSVP_IPV4_ERROR_CLASS_TYPE);
+ body.writeBytes(Ipv4Util.bytesForAddress(node.getIpv4Address()));
} else {
- bytes = new byte[V6_RSVP_LENGTH];
- bytes[0] = RSVP_ERROR_CLASS_NUM;
- bytes[1] = RSVP_IPV6_ERROR_CLASS_TYPE;
- offset += 2;
- ByteArray.copyWhole(Ipv6Util.bytesForAddress(node.getIpv6Address()), bytes, offset);
- offset += IP6_F_LENGTH;
+ body.writeByte(RSVP_ERROR_CLASS_NUM);
+ body.writeByte(RSVP_IPV6_ERROR_CLASS_TYPE);
+ body.writeBytes(Ipv6Util.bytesForAddress(node.getIpv6Address()));
}
- bytes[offset] = ByteArray.bitSetToBytes(flags, FLAGS_F_LENGTH)[0];
- offset += FLAGS_F_LENGTH;
- bytes[offset] = UnsignedBytes.checkedCast(rsvp.getCode());
- offset += ERROR_CODE_F_LENGTH;
- final byte[] value = ByteArray.intToBytes(rsvp.getValue().intValue(), ERROR_VALUE_F_LENGTH);
- ByteArray.copyWhole(value, bytes, offset);
- return bytes;
+ body.writeBytes(ByteArray.bitSetToBytes(flags, FLAGS_F_LENGTH));
+ body.writeByte(rsvp.getCode());
+ body.writeShort(rsvp.getValue().shortValue());
}
}
public void serializeObject(final Object object, final ByteBuf buffer) {
Preconditions.checkArgument(object instanceof Srp, "Wrong instance of PCEPObject. Passed %s . Needed SrpObject.", object.getClass());
final Srp srp = (Srp) object;
- ByteBuf body = Unpooled.buffer();
- //FIXME: switch to ByteBuf
- final byte[] tlvs = serializeTlvs(srp.getTlvs());
+ final ByteBuf body = Unpooled.buffer();
body.writerIndex(body.writerIndex() + FLAGS_SIZE);
final Long id = srp.getOperationId().getValue();
body.writeInt(id.intValue());
- body.writeBytes(tlvs);
+ serializeTlvs(srp.getTlvs(), body);
ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
}
- public byte[] serializeTlvs(final Tlvs tlvs) {
+ public void serializeTlvs(final Tlvs tlvs, final ByteBuf body) {
if (tlvs == null) {
- return new byte[0];
+ return;
} else if (tlvs.getSymbolicPathName() != null) {
- return serializeTlv(tlvs.getSymbolicPathName());
+ serializeTlv(tlvs.getSymbolicPathName(), body);
}
- return new byte[0];
}
}
*/
package org.opendaylight.protocol.pcep.ietf.stateful07;
+import com.google.common.base.Preconditions;
+
import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
import java.util.BitSet;
-import org.opendaylight.protocol.pcep.impl.tlv.TlvUtil;
import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
import org.opendaylight.protocol.pcep.spi.TlvParser;
import org.opendaylight.protocol.pcep.spi.TlvSerializer;
+import org.opendaylight.protocol.pcep.spi.TlvUtil;
import org.opendaylight.protocol.util.ByteArray;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.stateful.capability.tlv.Stateful;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.stateful.capability.tlv.StatefulBuilder;
}
@Override
- public byte[] serializeTlv(final Tlv tlv) {
- if (tlv == null) {
- throw new IllegalArgumentException("StatefulCapabilityTlv is mandatory.");
- }
+ public void serializeTlv(final Tlv tlv, final ByteBuf buffer) {
+ Preconditions.checkArgument(tlv != null, "StatefulCapabilityTlv is mandatory.");
final Stateful sct = (Stateful) tlv;
-
+ final ByteBuf body = Unpooled.buffer();
final BitSet flags = new BitSet(FLAGS_F_LENGTH * Byte.SIZE);
-
- flags.set(U_FLAG_OFFSET, sct.isLspUpdateCapability());
- return TlvUtil.formatTlv(TYPE, ByteArray.bitSetToBytes(flags, FLAGS_F_LENGTH));
+ if (sct.isLspUpdateCapability() != null) {
+ flags.set(U_FLAG_OFFSET, sct.isLspUpdateCapability());
+ }
+ body.writeBytes(ByteArray.bitSetToBytes(flags, FLAGS_F_LENGTH));
+ TlvUtil.formatTlv(TYPE, body, buffer);
}
}
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
+import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import org.junit.Test;
final Stateful07StatefulCapabilityTlvParser parser = new Stateful07StatefulCapabilityTlvParser();
final Stateful tlv = new StatefulBuilder().setLspUpdateCapability(Boolean.TRUE).build();
assertEquals(tlv, parser.parseTlv(Unpooled.wrappedBuffer(ByteArray.cutBytes(statefulBytes, 4))));
- assertArrayEquals(statefulBytes, parser.serializeTlv(tlv));
+ final ByteBuf buff = Unpooled.buffer();
+ parser.serializeTlv(tlv, buff);
+ assertArrayEquals(statefulBytes, ByteArray.getAllBytes(buff));
}
@Test
final SymbolicPathName tlv = new SymbolicPathNameBuilder().setPathName(
new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.SymbolicPathName("Med test of symbolic nameeee".getBytes())).build();
assertEquals(tlv, parser.parseTlv(Unpooled.wrappedBuffer(ByteArray.cutBytes(symbolicNameBytes, 4))));
- assertArrayEquals(symbolicNameBytes, parser.serializeTlv(tlv));
+ final ByteBuf buff = Unpooled.buffer();
+ parser.serializeTlv(tlv, buff);
+ assertArrayEquals(symbolicNameBytes, ByteArray.getAllBytes(buff));
}
@Test
final Stateful07LspUpdateErrorTlvParser parser = new Stateful07LspUpdateErrorTlvParser();
final LspErrorCode tlv = new LspErrorCodeBuilder().setErrorCode(627610883L).build();
assertEquals(tlv, parser.parseTlv(Unpooled.wrappedBuffer(ByteArray.cutBytes(lspUpdateErrorBytes, 4))));
- assertArrayEquals(lspUpdateErrorBytes, parser.serializeTlv(tlv));
+ final ByteBuf buff = Unpooled.buffer();
+ parser.serializeTlv(tlv, buff);
+ assertArrayEquals(lspUpdateErrorBytes, ByteArray.getAllBytes(buff));
}
@Test
final LspIdentifiers tlv = new LspIdentifiersBuilder().setAddressFamily(new Ipv4CaseBuilder().setIpv4(afi.build()).build()).setLspId(
new LspId(65535L)).setTunnelId(new TunnelId(4660)).build();
assertEquals(tlv, parser.parseTlv(Unpooled.wrappedBuffer(ByteArray.cutBytes(lspIdentifiers4Bytes, 4))));
- assertArrayEquals(lspIdentifiers4Bytes, parser.serializeTlv(tlv));
+ final ByteBuf buff = Unpooled.buffer();
+ parser.serializeTlv(tlv, buff);
+ assertArrayEquals(lspIdentifiers4Bytes, ByteArray.getAllBytes(buff));
}
@Test
final LspIdentifiers tlv = new LspIdentifiersBuilder().setAddressFamily(new Ipv6CaseBuilder().setIpv6(afi.build()).build()).setLspId(
new LspId(4660L)).setTunnelId(new TunnelId(65535)).build();
assertEquals(tlv, parser.parseTlv(Unpooled.wrappedBuffer(ByteArray.cutBytes(lspIdentifiers6Bytes, 4))));
- assertArrayEquals(lspIdentifiers6Bytes, parser.serializeTlv(tlv));
+ final ByteBuf buff = Unpooled.buffer();
+ parser.serializeTlv(tlv, buff);
+ assertArrayEquals(lspIdentifiers6Bytes, ByteArray.getAllBytes(buff));
}
@Test
builder.setValue(5634);
final RsvpErrorSpec tlv = new RsvpErrorSpecBuilder().setErrorType(new RsvpCaseBuilder().setRsvpError(builder.build()).build()).build();
assertEquals(tlv, parser.parseTlv(Unpooled.wrappedBuffer(ByteArray.cutBytes(rsvpErrorBytes, 4))));
- assertArrayEquals(rsvpErrorBytes, parser.serializeTlv(tlv));
+ final ByteBuf buff = Unpooled.buffer();
+ parser.serializeTlv(tlv, buff);
+ assertArrayEquals(rsvpErrorBytes, ByteArray.getAllBytes(buff));
}
@Test
builder.setValue(50649);
final RsvpErrorSpec tlv = new RsvpErrorSpecBuilder().setErrorType(new RsvpCaseBuilder().setRsvpError(builder.build()).build()).build();
assertEquals(tlv, parser.parseTlv(Unpooled.wrappedBuffer(ByteArray.cutBytes(rsvpError6Bytes, 4))));
- assertArrayEquals(rsvpError6Bytes, parser.serializeTlv(tlv));
+ final ByteBuf buff = Unpooled.buffer();
+ parser.serializeTlv(tlv, buff);
+ assertArrayEquals(rsvpError6Bytes, ByteArray.getAllBytes(buff));
}
@Test
builder.setDescription("user desc");
final RsvpErrorSpec tlv = new RsvpErrorSpecBuilder().setErrorType(new UserCaseBuilder().setUserError(builder.build()).build()).build();
assertEquals(tlv, parser.parseTlv(Unpooled.wrappedBuffer(ByteArray.cutBytes(userErrorBytes, 4))));
- assertArrayEquals(userErrorBytes, parser.serializeTlv(tlv));
+ final ByteBuf buff = Unpooled.buffer();
+ parser.serializeTlv(tlv, buff);
+ assertArrayEquals(userErrorBytes, ByteArray.getAllBytes(buff));
}
}
body.writeZero(FLAGS_F_LENGTH + RESERVED);
body.writeByte(errObj.getType());
body.writeByte(errObj.getValue());
- // FIXME: switch to ByteBuf
- final byte[] tlvs = serializeTlvs(errObj.getTlvs());
- if (tlvs.length != 0) {
- body.writeBytes(tlvs);
- }
+ serializeTlvs(errObj.getTlvs(), body);
ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
}
- public byte[] serializeTlvs(final Tlvs tlvs) {
+ public void serializeTlvs(final Tlvs tlvs, final ByteBuf body) {
if (tlvs == null) {
- return new byte[0];
+ return;
} else if (tlvs.getReqMissing() != null) {
- return serializeTlv(tlvs.getReqMissing());
+ serializeTlv(tlvs.getReqMissing(), body);
}
- return new byte[0];
}
}
}
body.writeBytes(ByteArray.bitSetToBytes(flags, FLAGS_F_LENGTH));
body.writeZero(RESERVED);
- // FIXME: switch to ByteBuf
- final byte[] tlvs = serializeTlvs(lspaObj.getTlvs());
- if (tlvs.length != 0) {
- body.writeBytes(tlvs);
- }
+ serializeTlvs(lspaObj.getTlvs(), body);
ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
}
- public byte[] serializeTlvs(final Tlvs tlvs) {
- return new byte[0];
+ public void serializeTlvs(final Tlvs tlvs, final ByteBuf body) {
+ return;
}
}
}
body.writeBytes(ByteArray.bitSetToBytes(flags, FLAGS_F_LENGTH));
body.writeZero(RESERVED_F_LENGTH);
- // FIXME: switch to ByteBuf
- final byte[] tlvs = serializeTlvs(nPObj.getTlvs());
- if (tlvs != null) {
- body.writeBytes(tlvs);
- }
+ serializeTlvs(nPObj.getTlvs(), body);
ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
}
- public byte[] serializeTlvs(final Tlvs tlvs) {
+ public void serializeTlvs(final Tlvs tlvs, final ByteBuf body) {
if (tlvs == null) {
- return new byte[0];
- } else if (tlvs.getNoPathVector() != null) {
- return serializeTlv(tlvs.getNoPathVector());
+ return;
+ }
+ if (tlvs.getNoPathVector() != null) {
+ serializeTlv(tlvs.getNoPathVector(), body);
}
- return new byte[0];
}
}
body.writeZero(NT_F_OFFSET);
body.writeByte(notObj.getType());
body.writeByte(notObj.getValue());
- // FIXME: switch to ByteBuf
- final byte[] tlvs = serializeTlvs(notObj.getTlvs());
- if (tlvs.length != 0) {
- body.writeBytes(tlvs);
- }
+ serializeTlvs(notObj.getTlvs(), body);
ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
}
- public byte[] serializeTlvs(final Tlvs tlvs) {
+ public void serializeTlvs(final Tlvs tlvs, final ByteBuf body) {
if (tlvs == null) {
- return new byte[0];
- } else if (tlvs.getOverloadDuration() != null) {
- return serializeTlv(tlvs.getOverloadDuration());
+ return;
+ }
+ if (tlvs.getOverloadDuration() != null) {
+ serializeTlv(tlvs.getOverloadDuration(), body);
}
- return new byte[0];
}
}
body.writeByte(open.getKeepalive());
body.writeByte(open.getDeadTimer());
body.writeByte(open.getSessionId());
- //FIXME: switch to ByteBuf
- body.writeBytes(serializeTlvs(open.getTlvs()));
+ serializeTlvs(open.getTlvs(), body);
ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
}
- public byte[] serializeTlvs(final Tlvs tlvs) {
+ public void serializeTlvs(final Tlvs tlvs, final ByteBuf body) {
if (tlvs == null) {
- return new byte[0];
+ return;
}
- byte[] ofListBytes = null;
if (tlvs.getOfList() != null) {
- ofListBytes = serializeTlv(tlvs.getOfList());
+ serializeTlv(tlvs.getOfList(), body);
}
- byte[] result = new byte[0];
- if (ofListBytes != null) {
- result = new byte[ofListBytes.length];
- ByteArray.copyWhole(ofListBytes, result, 0);
- }
- return result;
}
}
}
body.writeBytes(ByteArray.bitSetToBytes(flags, FLAGS_PRI_MF_LENGTH));
body.writeInt(rpObj.getRequestId().getValue().intValue());
- //FIXME: switch to ByteBuf
- final byte[] tlvs = serializeTlvs(rpObj.getTlvs());
- if (tlvs.length != 0) {
- body.writeBytes(tlvs);
- }
+ serializeTlvs(rpObj.getTlvs(), body);
ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
}
- public byte[] serializeTlvs(final Tlvs tlvs) {
+ public void serializeTlvs(final Tlvs tlvs, final ByteBuf body) {
if (tlvs == null) {
- return new byte[0];
+ return;
} else if (tlvs.getOrder() != null) {
- return serializeTlv(tlvs.getOrder());
+ serializeTlv(tlvs.getOrder(), body);
}
- return new byte[0];
}
}
*/
package org.opendaylight.protocol.pcep.impl.tlv;
+import com.google.common.base.Preconditions;
+
import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
import org.opendaylight.protocol.pcep.spi.TlvParser;
import org.opendaylight.protocol.pcep.spi.TlvSerializer;
+import org.opendaylight.protocol.pcep.spi.TlvUtil;
import org.opendaylight.protocol.util.ByteArray;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.iana.rev130816.EnterpriseNumber;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
protected static final int ENTERPRISE_NUM_LENGTH = 4;
@Override
- public byte[] serializeTlv(final Tlv tlv) {
- if (tlv == null) {
- throw new IllegalArgumentException("Vendor Specific Tlv is mandatory.");
- }
+ public void serializeTlv(final Tlv tlv, final ByteBuf buffer) {
+ Preconditions.checkArgument(tlv != null, "Vendor Specific Tlv is mandatory.");
final VsTlv vsTlv = (VsTlv) tlv;
+ final ByteBuf body = Unpooled.buffer();
if (vsTlv.getEnterpriseNumber().getValue() == getEnterpriseNumber()) {
- final byte[] payloadBytes = serializeVendorPayload(vsTlv.getVendorPayload());
- final byte[] ianaNumBytes = ByteArray.longToBytes(vsTlv.getEnterpriseNumber().getValue(), ENTERPRISE_NUM_LENGTH);
-
- final byte[] bytes = new byte[ianaNumBytes.length + payloadBytes.length];
- System.arraycopy(ianaNumBytes, 0, bytes, 0, ENTERPRISE_NUM_LENGTH);
- System.arraycopy(payloadBytes, 0, bytes, ENTERPRISE_NUM_LENGTH, payloadBytes.length);
- return TlvUtil.formatTlv(TYPE, bytes);
+ body.writeInt(vsTlv.getEnterpriseNumber().getValue().intValue());
+ body.writeBytes(serializeVendorPayload(vsTlv.getVendorPayload()));
+ TlvUtil.formatTlv(TYPE, body, buffer);
}
- return new byte[0];
}
@Override
*/
package org.opendaylight.protocol.pcep.impl.tlv;
+import com.google.common.base.Preconditions;
+
import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
import java.util.BitSet;
import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
import org.opendaylight.protocol.pcep.spi.TlvParser;
import org.opendaylight.protocol.pcep.spi.TlvSerializer;
+import org.opendaylight.protocol.pcep.spi.TlvUtil;
import org.opendaylight.protocol.util.ByteArray;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.NoPathVectorTlv.Flags;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
}
@Override
- public byte[] serializeTlv(final Tlv tlvs) {
- if (tlvs == null) {
- throw new IllegalArgumentException("NoPathVectorTlv is mandatory.");
- }
- final NoPathVector tlv = (NoPathVector) tlvs;
-
+ public void serializeTlv(final Tlv tlv, final ByteBuf buffer) {
+ Preconditions.checkArgument(tlv != null, "NoPathVectorTlv is mandatory.");
+ final NoPathVector noPath = (NoPathVector) tlv;
+ final ByteBuf body = Unpooled.buffer();
final BitSet flags = new BitSet(FLAGS_F_LENGTH * Byte.SIZE);
- flags.set(REACHABLITY_PROBLEM, tlv.getFlags().isP2mpUnreachable());
- flags.set(NO_GCO_SOLUTION, tlv.getFlags().isNoGcoSolution());
- flags.set(NO_GCO_MIGRATION_PATH, tlv.getFlags().isNoGcoMigration());
- flags.set(PATH_KEY, tlv.getFlags().isPathKey());
- flags.set(CHAIN_UNAVAILABLE, tlv.getFlags().isChainUnavailable());
- flags.set(UNKNOWN_SRC, tlv.getFlags().isUnknownSource());
- flags.set(UNKNOWN_DEST, tlv.getFlags().isUnknownDestination());
- flags.set(PCE_UNAVAILABLE, tlv.getFlags().isPceUnavailable());
- return TlvUtil.formatTlv(TYPE, ByteArray.bitSetToBytes(flags, FLAGS_F_LENGTH));
+ Flags f = noPath.getFlags();
+ if (f.isP2mpUnreachable() != null) {
+ flags.set(REACHABLITY_PROBLEM, f.isP2mpUnreachable());
+ }
+ if (f.isNoGcoSolution() != null) {
+ flags.set(NO_GCO_SOLUTION, f.isNoGcoSolution());
+ }
+ if (f.isNoGcoMigration() != null) {
+ flags.set(NO_GCO_MIGRATION_PATH, f.isNoGcoMigration());
+ }
+ if (f.isPathKey() != null) {
+ flags.set(PATH_KEY, f.isPathKey());
+ }
+ if (f.isChainUnavailable() != null) {
+ flags.set(CHAIN_UNAVAILABLE, f.isChainUnavailable());
+ }
+ if (f.isUnknownSource() != null) {
+ flags.set(UNKNOWN_SRC, f.isUnknownSource());
+ }
+ if (f.isUnknownDestination() != null) {
+ flags.set(UNKNOWN_DEST, f.isUnknownDestination());
+ }
+ if (f.isPceUnavailable() != null) {
+ flags.set(PCE_UNAVAILABLE, f.isPceUnavailable());
+ }
+ body.writeBytes(ByteArray.bitSetToBytes(flags, FLAGS_F_LENGTH));
+ TlvUtil.formatTlv(TYPE, body, buffer);
}
}
*/
package org.opendaylight.protocol.pcep.impl.tlv;
+import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
import java.util.List;
import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
import org.opendaylight.protocol.pcep.spi.TlvParser;
import org.opendaylight.protocol.pcep.spi.TlvSerializer;
-import org.opendaylight.protocol.util.ByteArray;
+import org.opendaylight.protocol.pcep.spi.TlvUtil;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.OfId;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.of.list.tlv.OfList;
}
@Override
- public byte[] serializeTlv(final Tlv tlv) {
- if (tlv == null) {
- throw new IllegalArgumentException("OFListTlv is mandatory.");
- }
+ public void serializeTlv(final Tlv tlv, final ByteBuf buffer) {
+ Preconditions.checkArgument(tlv != null, "OFListTlv is mandatory.");
final OfList oft = (OfList) tlv;
-
+ final ByteBuf body = Unpooled.buffer();
final List<OfId> ofCodes = oft.getCodes();
- final byte[] retBytes = new byte[ofCodes.size() * OF_CODE_ELEMENT_LENGTH];
-
- final int size = ofCodes.size();
- for (int i = 0; i < size; i++) {
- ByteArray.copyWhole(ByteArray.shortToBytes(ofCodes.get(i).getValue().shortValue()), retBytes, i * OF_CODE_ELEMENT_LENGTH);
+ for (OfId id : ofCodes) {
+ body.writeShort(id.getValue().shortValue());
}
- return TlvUtil.formatTlv(TYPE, retBytes);
+ TlvUtil.formatTlv(TYPE, body, buffer);
}
}
*/
package org.opendaylight.protocol.pcep.impl.tlv;
+import com.google.common.base.Preconditions;
+
import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
import org.opendaylight.protocol.pcep.spi.TlvParser;
import org.opendaylight.protocol.pcep.spi.TlvSerializer;
-import org.opendaylight.protocol.util.ByteArray;
+import org.opendaylight.protocol.pcep.spi.TlvUtil;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.order.tlv.Order;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.order.tlv.OrderBuilder;
public static final int TYPE = 5;
- private static final int ORDR_DEL_LENGTH = 4;
-
- private static final int ORDR_SETUP_LENGTH = 4;
-
@Override
public Order parseTlv(final ByteBuf buffer) throws PCEPDeserializerException {
if (buffer == null) {
}
@Override
- public byte[] serializeTlv(final Tlv tlv) {
- if (tlv == null) {
- throw new IllegalArgumentException("OrderTlv is mandatory.");
- }
+ public void serializeTlv(final Tlv tlv, final ByteBuf buffer) {
+ Preconditions.checkArgument(tlv != null, "OrderTlv is mandatory.");
final Order otlv = (Order) tlv;
- final byte[] bytes = new byte[ORDR_DEL_LENGTH + ORDR_SETUP_LENGTH];
- int offset = 0;
- ByteArray.copyWhole(ByteArray.longToBytes(otlv.getDelete(), ORDR_DEL_LENGTH), bytes, offset);
- offset += ORDR_DEL_LENGTH;
- ByteArray.copyWhole(ByteArray.longToBytes(otlv.getSetup(), ORDR_SETUP_LENGTH), bytes, offset);
- return TlvUtil.formatTlv(TYPE, bytes);
+ final ByteBuf body = Unpooled.buffer();
+ body.writeInt(otlv.getDelete().intValue());
+ body.writeInt(otlv.getSetup().intValue());
+ TlvUtil.formatTlv(TYPE, body, buffer);
}
}
*/
package org.opendaylight.protocol.pcep.impl.tlv;
+import com.google.common.base.Preconditions;
+
import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
import org.opendaylight.protocol.pcep.spi.TlvParser;
import org.opendaylight.protocol.pcep.spi.TlvSerializer;
-import org.opendaylight.protocol.util.ByteArray;
+import org.opendaylight.protocol.pcep.spi.TlvUtil;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.overload.duration.tlv.OverloadDuration;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.overload.duration.tlv.OverloadDurationBuilder;
public static final int TYPE = 2;
- private static final int OVERLOADED_DURATION_LENGTH = 4;
-
@Override
public OverloadDuration parseTlv(final ByteBuf buffer) throws PCEPDeserializerException {
if (buffer == null) {
}
@Override
- public byte[] serializeTlv(final Tlv tlv) {
- if (tlv == null) {
- throw new IllegalArgumentException("OverloadedTlv is mandatory.");
- }
- final OverloadDuration odt = (OverloadDuration) tlv;
- return TlvUtil.formatTlv(TYPE, ByteArray.longToBytes(odt.getDuration(), OVERLOADED_DURATION_LENGTH));
+ public void serializeTlv(final Tlv tlv, final ByteBuf buffer) {
+ Preconditions.checkArgument(tlv != null, "OverloadedTlv is mandatory.");
+ TlvUtil.formatTlv(TYPE, Unpooled.copyInt(((OverloadDuration) tlv).getDuration().intValue()), buffer);
}
}
*/
package org.opendaylight.protocol.pcep.impl.tlv;
+import com.google.common.base.Preconditions;
+
import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
import org.opendaylight.protocol.pcep.spi.TlvParser;
import org.opendaylight.protocol.pcep.spi.TlvSerializer;
-import org.opendaylight.protocol.util.ByteArray;
+import org.opendaylight.protocol.pcep.spi.TlvUtil;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.RequestId;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.req.missing.tlv.ReqMissing;
public static final int TYPE = 3;
- private static final int REQ_ID_LENGTH = 4;
-
@Override
public ReqMissing parseTlv(final ByteBuf buffer) throws PCEPDeserializerException {
if (buffer == null) {
}
@Override
- public byte[] serializeTlv(final Tlv tlv) {
- if (tlv == null) {
- throw new IllegalArgumentException("ReqMissingTlv is mandatory.");
- }
+ public void serializeTlv(final Tlv tlv, final ByteBuf buffer) {
+ Preconditions.checkArgument(tlv != null, "ReqMissingTlv is mandatory.");
final ReqMissing req = (ReqMissing) tlv;
- return TlvUtil.formatTlv(TYPE, ByteArray.longToBytes(req.getRequestId().getValue(), REQ_ID_LENGTH));
+ final ByteBuf body = Unpooled.buffer();
+ body.writeInt(req.getRequestId().getValue().intValue());
+ TlvUtil.formatTlv(TYPE, body, buffer);
}
}
+++ /dev/null
-/*
- * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
- *
- * This program and the accompanying materials are made available under the
- * 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.pcep.impl.tlv;
-
-import org.opendaylight.protocol.pcep.spi.AbstractObjectWithTlvsParser;
-import org.opendaylight.protocol.util.ByteArray;
-
-public final class TlvUtil {
-
- private static final int TLV_TYPE_F_LENGTH = 2;
- private static final int TLV_LENGTH_F_LENGTH = 2;
- private static final int HEADER_SIZE = TLV_LENGTH_F_LENGTH + TLV_TYPE_F_LENGTH;
-
- protected static final int PADDED_TO = 4;
-
- private TlvUtil() {
- throw new UnsupportedOperationException("Utility class should not be instantiated");
- }
-
- public static byte[] formatTlv(final int type, final byte[] valueBytes) {
- final byte[] typeBytes = ByteArray.intToBytes(type, TLV_TYPE_F_LENGTH);
-
- final byte[] lengthBytes = ByteArray.intToBytes(valueBytes.length, TLV_LENGTH_F_LENGTH);
-
- final byte[] bytes = new byte[HEADER_SIZE + valueBytes.length
- + AbstractObjectWithTlvsParser.getPadding(HEADER_SIZE + valueBytes.length, PADDED_TO)];
-
- int byteOffset = 0;
- System.arraycopy(typeBytes, 0, bytes, byteOffset, TLV_TYPE_F_LENGTH);
- byteOffset += TLV_TYPE_F_LENGTH;
- System.arraycopy(lengthBytes, 0, bytes, byteOffset, TLV_LENGTH_F_LENGTH);
- byteOffset += TLV_LENGTH_F_LENGTH;
- System.arraycopy(valueBytes, 0, bytes, byteOffset, valueBytes.length);
- return bytes;
- }
-}
import com.google.common.collect.Lists;
+import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import java.util.List;
final NoPathVectorTlv tlv = new NoPathVectorBuilder().setFlags(
new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.NoPathVectorTlv.Flags(false, true, false, true, false, true, true, true)).build();
assertEquals(tlv, parser.parseTlv(Unpooled.wrappedBuffer(ByteArray.cutBytes(noPathVectorBytes, 4))));
- assertArrayEquals(noPathVectorBytes, parser.serializeTlv(tlv));
+ final ByteBuf buff = Unpooled.buffer();
+ parser.serializeTlv(tlv, buff);
+ assertArrayEquals(noPathVectorBytes, ByteArray.getAllBytes(buff));
}
@Test
final OverloadedDurationTlvParser parser = new OverloadedDurationTlvParser();
final OverloadDuration tlv = new OverloadDurationBuilder().setDuration(0x7FFFFFFFL).build();
assertEquals(tlv, parser.parseTlv(Unpooled.wrappedBuffer(ByteArray.cutBytes(overloadedBytes, 4))));
- assertArrayEquals(overloadedBytes, parser.serializeTlv(tlv));
+ final ByteBuf buff = Unpooled.buffer();
+ parser.serializeTlv(tlv, buff);
+ assertArrayEquals(overloadedBytes, ByteArray.getAllBytes(buff));
}
+ @Test
public void testReqMissingTlv() throws PCEPDeserializerException {
final ReqMissingTlvParser parser = new ReqMissingTlvParser();
final ReqMissing tlv = new ReqMissingBuilder().setRequestId(new RequestId(0xF7823517L)).build();
assertEquals(tlv, parser.parseTlv(Unpooled.wrappedBuffer(ByteArray.cutBytes(reqMissingBytes, 4))));
- assertArrayEquals(reqMissingBytes, parser.serializeTlv(tlv));
+ final ByteBuf buff = Unpooled.buffer();
+ parser.serializeTlv(tlv, buff);
+ assertArrayEquals(reqMissingBytes, ByteArray.getAllBytes(buff));
}
@Test
final OrderTlvParser parser = new OrderTlvParser();
final Order tlv = new OrderBuilder().setDelete(0xFFFFFFFFL).setSetup(0x00000001L).build();
assertEquals(tlv, parser.parseTlv(Unpooled.wrappedBuffer(ByteArray.cutBytes(orderBytes, 4))));
- assertArrayEquals(orderBytes, parser.serializeTlv(tlv));
+ final ByteBuf buff = Unpooled.buffer();
+ parser.serializeTlv(tlv, buff);
+ assertArrayEquals(orderBytes, ByteArray.getAllBytes(buff));
}
@Test
ids.add(new OfId(0x5678));
final OfList tlv = new OfListBuilder().setCodes(ids).build();
assertEquals(tlv, parser.parseTlv(Unpooled.wrappedBuffer(ByteArray.cutBytes(ofListBytes, 4))));
- assertArrayEquals(ofListBytes, parser.serializeTlv(tlv));
+ final ByteBuf buff = Unpooled.buffer();
+ parser.serializeTlv(tlv, buff);
+ assertArrayEquals(ofListBytes, ByteArray.getAllBytes(buff));
}
@Test
public void testVendorSpecificTlv() throws PCEPDeserializerException {
VsTlv tlv = new VsTlvBuilder().setEnterpriseNumber(new EnterpriseNumber(9L)).setVendorPayload(this.vp).build();
assertEquals(tlv, this.vsParser.parseTlv(Unpooled.wrappedBuffer(ByteArray.cutBytes(vsTlvBytes, 4))));
- assertArrayEquals(vsTlvBytes, this.vsParser.serializeTlv(tlv));
+ final ByteBuf buff = Unpooled.buffer();
+ this.vsParser.serializeTlv(tlv, buff);
+ assertArrayEquals(vsTlvBytes, ByteArray.getAllBytes(buff));
}
}
* 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.pcep.lsp.setup.type01;
+import io.netty.buffer.ByteBuf;
+
import org.opendaylight.protocol.pcep.ietf.initiated00.CInitiated00SrpObjectParser;
import org.opendaylight.protocol.pcep.spi.TlvRegistry;
-import org.opendaylight.protocol.util.ByteArray;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.srp.object.SrpBuilder;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.srp.object.srp.Tlvs;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.srp.object.srp.TlvsBuilder;
}
@Override
- public byte[] serializeTlvs(Tlvs tlvs) {
+ public void serializeTlvs(final Tlvs tlvs, final ByteBuf body) {
if (tlvs == null) {
- return new byte[0];
+ return;
}
- final byte[] prev = super.serializeTlvs(tlvs);
- int finalLength = prev.length;
- byte[] nameBytes = null;
+ super.serializeTlvs(tlvs, body);
if (tlvs.getAugmentation(Tlvs8.class) != null) {
final Tlvs8 nameTlvs = tlvs.getAugmentation(Tlvs8.class);
if (nameTlvs.getPathSetupType() != null) {
- nameBytes = serializeTlv(nameTlvs.getPathSetupType());
- finalLength += nameBytes.length;
+ serializeTlv(nameTlvs.getPathSetupType(), body);
}
}
- final byte[] result = new byte[finalLength];
- ByteArray.copyWhole(prev, result, 0);
- int offset = prev.length;
- if (nameBytes != null) {
- ByteArray.copyWhole(nameBytes, result, offset);
- offset += nameBytes.length;
- }
- return result;
}
-
}
* 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.pcep.lsp.setup.type01;
import com.google.common.base.Preconditions;
+
import io.netty.buffer.ByteBuf;
-import org.opendaylight.protocol.pcep.impl.tlv.TlvUtil;
+import io.netty.buffer.Unpooled;
+
import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
import org.opendaylight.protocol.pcep.spi.TlvParser;
import org.opendaylight.protocol.pcep.spi.TlvSerializer;
+import org.opendaylight.protocol.pcep.spi.TlvUtil;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.lsp.setup.type._01.rev140507.path.setup.type.tlv.PathSetupType;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.lsp.setup.type._01.rev140507.path.setup.type.tlv.PathSetupTypeBuilder;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
private static final int OFFSET = 4 - PST_LENGTH;
@Override
- public byte[] serializeTlv(Tlv tlv) {
+ public void serializeTlv(final Tlv tlv, final ByteBuf buffer) {
Preconditions.checkNotNull(tlv, "PathSetupType is mandatory.");
final PathSetupType pstTlv = (PathSetupType) tlv;
- return TlvUtil.formatTlv(TYPE, new byte[] { 0, 0, 0, (byte) (pstTlv.isPst() ? 1 : 0) });
+ ByteBuf body = Unpooled.buffer();
+ body.writeZero(OFFSET);
+ body.writeBoolean((pstTlv.isPst() != null) ? pstTlv.isPst() : false);
+ TlvUtil.formatTlv(TYPE, body, buffer);
}
@Override
- public Tlv parseTlv(ByteBuf buffer) throws PCEPDeserializerException {
+ public Tlv parseTlv(final ByteBuf buffer) throws PCEPDeserializerException {
if (buffer == null) {
return null;
}
* 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.pcep.lsp.setup.type01;
+import io.netty.buffer.ByteBuf;
+
import org.opendaylight.protocol.pcep.impl.object.PCEPRequestParameterObjectParser;
import org.opendaylight.protocol.pcep.spi.TlvRegistry;
-import org.opendaylight.protocol.util.ByteArray;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.lsp.setup.type._01.rev140507.Tlvs1;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.lsp.setup.type._01.rev140507.Tlvs1Builder;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.lsp.setup.type._01.rev140507.path.setup.type.tlv.PathSetupType;
}
@Override
- public byte[] serializeTlvs(Tlvs tlvs) {
+ public void serializeTlvs(final Tlvs tlvs, final ByteBuf body) {
if (tlvs == null) {
- return new byte[0];
+ return;
}
- final byte[] prev = super.serializeTlvs(tlvs);
- int finalLength = prev.length;
- byte[] nameBytes = null;
+ super.serializeTlvs(tlvs, body);
if (tlvs.getAugmentation(Tlvs1.class) != null) {
final Tlvs1 nameTlvs = tlvs.getAugmentation(Tlvs1.class);
if (nameTlvs.getPathSetupType() != null) {
- nameBytes = serializeTlv(nameTlvs.getPathSetupType());
- finalLength += nameBytes.length;
+ serializeTlv(nameTlvs.getPathSetupType(), body);
}
}
- final byte[] result = new byte[finalLength];
- ByteArray.copyWhole(prev, result, 0);
- int offset = prev.length;
- if (nameBytes != null) {
- ByteArray.copyWhole(nameBytes, result, offset);
- offset += nameBytes.length;
- }
- return result;
}
-
}
* 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.pcep.segment.routing02;
+import io.netty.buffer.ByteBuf;
+
import org.opendaylight.protocol.pcep.impl.object.PCEPOpenObjectParser;
import org.opendaylight.protocol.pcep.spi.TlvRegistry;
-import org.opendaylight.protocol.util.ByteArray;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.segment.routing._02.rev140506.Tlvs1;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.segment.routing._02.rev140506.Tlvs1Builder;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.segment.routing._02.rev140506.sr.pce.capability.tlv.SrPceCapability;
}
@Override
- public byte[] serializeTlvs(Tlvs tlvs) {
+ public void serializeTlvs(final Tlvs tlvs, final ByteBuf body) {
if (tlvs == null) {
- return new byte[0];
+ return;
}
- final byte[] prev = super.serializeTlvs(tlvs);
- int finalLength = prev.length;
- byte[] nameBytes = null;
+ super.serializeTlvs(tlvs, body);
if (tlvs.getAugmentation(Tlvs1.class) != null) {
final Tlvs1 spcTlvs = tlvs.getAugmentation(Tlvs1.class);
if (spcTlvs.getSrPceCapability() != null) {
- nameBytes = serializeTlv(spcTlvs.getSrPceCapability());
- finalLength += nameBytes.length;
+ serializeTlv(spcTlvs.getSrPceCapability(), body);
}
}
- final byte[] result = new byte[finalLength];
- ByteArray.copyWhole(prev, result, 0);
- int offset = prev.length;
- if (nameBytes != null) {
- ByteArray.copyWhole(nameBytes, result, offset);
- offset += nameBytes.length;
- }
- return result;
}
-
}
* 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.pcep.segment.routing02;
import com.google.common.base.Preconditions;
+
import io.netty.buffer.ByteBuf;
-import org.opendaylight.protocol.pcep.impl.tlv.TlvUtil;
+import io.netty.buffer.Unpooled;
+
import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
import org.opendaylight.protocol.pcep.spi.TlvParser;
import org.opendaylight.protocol.pcep.spi.TlvSerializer;
-import org.opendaylight.protocol.util.ByteArray;
+import org.opendaylight.protocol.pcep.spi.TlvUtil;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.segment.routing._02.rev140506.sr.pce.capability.tlv.SrPceCapability;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.segment.routing._02.rev140506.sr.pce.capability.tlv.SrPceCapabilityBuilder;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
private static final int OFFSET = 4 - MSD_LENGTH;
@Override
- public byte[] serializeTlv(Tlv tlv) {
+ public void serializeTlv(final Tlv tlv, final ByteBuf buffer) {
Preconditions.checkNotNull(tlv, "SrPceCapability is mandatory.");
- final SrPceCapability spcTlv = (SrPceCapability) tlv;
- return TlvUtil.formatTlv(TYPE, ByteArray.intToBytes(spcTlv.getMsd()));
+ TlvUtil.formatTlv(TYPE, Unpooled.copyInt(((SrPceCapability) tlv).getMsd()), buffer);
}
@Override
final short msd = buffer.readerIndex(OFFSET).readUnsignedByte();
return new SrPceCapabilityBuilder().setMsd(msd).build();
}
-
}
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
-
+import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
+
import org.junit.Test;
import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
import org.opendaylight.protocol.util.ByteArray;
public void testPathSetupTypeTlvParser() throws PCEPDeserializerException {
final PathSetupTypeTlvParser parser = new PathSetupTypeTlvParser();
final PathSetupType pstTlv = new PathSetupTypeBuilder().setPst(true).build();
-
assertEquals(pstTlv, parser.parseTlv(Unpooled.wrappedBuffer(ByteArray.cutBytes(pstTlvBytes, 4))));
- assertArrayEquals(pstTlvBytes, parser.serializeTlv(pstTlv));
+ final ByteBuf buff = Unpooled.buffer();
+ parser.serializeTlv(pstTlv, buff);
+ assertArrayEquals(pstTlvBytes, ByteArray.getAllBytes(buff));
}
}
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
-
+import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
+
import org.junit.Test;
import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
import org.opendaylight.protocol.util.ByteArray;
final SrPceCapabilityTlvParser parser = new SrPceCapabilityTlvParser();
final SrPceCapability spcTlv = new SrPceCapabilityBuilder().setMsd((short) 1).build();
assertEquals(spcTlv, parser.parseTlv(Unpooled.wrappedBuffer(ByteArray.cutBytes(spcTlvBytes, 4))));
- assertArrayEquals(spcTlvBytes, parser.serializeTlv(spcTlv));
+ final ByteBuf buff = Unpooled.buffer();
+ parser.serializeTlv(spcTlv, buff);
+ assertArrayEquals(spcTlvBytes, ByteArray.getAllBytes(buff));
}
}
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
-import java.util.Arrays;
-
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
}
}
- protected final byte[] serializeTlv(final Tlv tlv) {
+ protected final void serializeTlv(final Tlv tlv, final ByteBuf buffer) {
Preconditions.checkNotNull(tlv, "PCEP TLV is mandatory.");
LOG.trace("Serializing PCEP TLV {}", tlv);
- final byte[] ret = this.tlvReg.serializeTlv(tlv);
- if (ret == null) {
- LOG.warn("TLV serializer for type {} could not be found.", tlv);
- }
- LOG.trace("Serialized PCEP TLV {}.", Arrays.toString(ret));
- return ret;
+ this.tlvReg.serializeTlv(tlv, buffer);
+ LOG.trace("Serialized PCEP TLV {}.", ByteBufUtil.hexDump(buffer));
}
protected void addTlv(final T builder, final Tlv tlv) {
public interface TlvRegistry {
Tlv parseTlv(final int type, final ByteBuf buffer) throws PCEPDeserializerException;
- byte[] serializeTlv(final Tlv tlv);
+ void serializeTlv(final Tlv tlv, final ByteBuf buffer);
}
*/
package org.opendaylight.protocol.pcep.spi;
+import io.netty.buffer.ByteBuf;
+
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
public interface TlvSerializer {
- byte[] serializeTlv(Tlv tlv);
+ void serializeTlv(final Tlv tlv, final ByteBuf buffer);
}
--- /dev/null
+/*
+ * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * 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.pcep.spi;
+
+import io.netty.buffer.ByteBuf;
+
+public final class TlvUtil {
+
+ private static final int HEADER_SIZE = 4;
+
+ protected static final int PADDED_TO = 4;
+
+ private TlvUtil() {
+ throw new UnsupportedOperationException("Utility class should not be instantiated");
+ }
+
+ public static void formatTlv(final int type,final ByteBuf body, final ByteBuf out) {
+ out.writeShort(type);
+ out.writeShort(body.writerIndex());
+ out.writeBytes(body);
+ out.writeZero(AbstractObjectWithTlvsParser.getPadding(HEADER_SIZE + body.writerIndex(), PADDED_TO));
+ }
+}
}
@Override
- public byte[] serializeTlv(final Tlv tlv) {
+ public void serializeTlv(final Tlv tlv, final ByteBuf buffer) {
final TlvSerializer serializer = this.handlers.getSerializer(tlv.getImplementedInterface());
if (serializer == null) {
- return null;
+ return;
}
- return serializer.serializeTlv(tlv);
+ serializer.serializeTlv(tlv, buffer);
}
}