package org.opendaylight.protocol.pcep.impl.object;
import com.google.common.base.Preconditions;
-import com.google.common.collect.Lists;
import com.google.common.primitives.UnsignedBytes;
import io.netty.buffer.ByteBuf;
return subs;
}
- protected final byte[] serializeSubobject(final List<Subobject> subobjects) {
- final List<byte[]> result = Lists.newArrayList();
- int finalLength = 0;
+ protected final void serializeSubobject(final List<Subobject> subobjects, final ByteBuf buffer) {
for (final Subobject subobject : subobjects) {
- final byte[] bytes = this.subobjReg.serializeSubobject(subobject);
- if (bytes == null) {
- LOG.warn("Could not find serializer for subobject type: {}. Skipping subobject.", subobject.getSubobjectType());
- } else {
- finalLength += bytes.length;
- result.add(bytes);
- }
- }
- final byte[] resultBytes = new byte[finalLength];
- int byteOffset = 0;
- for (final byte[] b : result) {
- System.arraycopy(b, 0, resultBytes, byteOffset, b.length);
- byteOffset += b.length;
+ this.subobjReg.serializeSubobject(subobject, buffer);
}
- return resultBytes;
}
}
if (obj.getFlags().isFail() != null) {
body.writeBoolean(obj.getFlags().isFail());
}
- // FIXME: switch to ByteBuf
- final byte[] bytes = serializeSubobject(obj.getSubobject());
- body.writeBytes(bytes);
+ serializeSubobject(obj.getSubobject(), body);
ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
}
}
+++ /dev/null
-/*
- * Copyright (c) 2013 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.object;
-
-import com.google.common.primitives.UnsignedBytes;
-
-public final class XROSubobjectUtil {
-
- private static final int HEADER_SIZE = 2;
-
- private XROSubobjectUtil() {
- }
-
- public static byte[] formatSubobject(final int type, final boolean mandatory, final byte[] value) {
- final byte[] bytes = new byte[HEADER_SIZE + value.length];
- bytes[0] = (byte) (UnsignedBytes.checkedCast(type) | (mandatory ? 1 << 7 : 0));
- bytes[1] = UnsignedBytes.checkedCast(value.length + HEADER_SIZE);
- System.arraycopy(value, 0, bytes, HEADER_SIZE, value.length);
- return bytes;
- }
-}
b.setSubobjectType(ex.getSubobjectType());
list.add(b.build());
}
- //FIXME: switch to ByteBuf
- EROSubobjectUtil.formatSubobject(TYPE, subobject.isLoose(), Unpooled.copiedBuffer(serializeSubobject(list)), buffer);
+ final ByteBuf body = Unpooled.buffer();
+ serializeSubobject(list, body);
+ EROSubobjectUtil.formatSubobject(TYPE, subobject.isLoose(), body, buffer);
}
private List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.Subobject> parseSubobject(
return subs;
}
- private byte[] serializeSubobject(
- final List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.Subobject> subobjects) {
-
- final List<byte[]> result = Lists.newArrayList();
-
- int finalLength = 0;
-
+ private void serializeSubobject(
+ final List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.Subobject> subobjects, final ByteBuf body) {
for (final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.Subobject subobject : subobjects) {
-
- final byte[] bytes = this.registry.serializeSubobject(subobject);
- finalLength += bytes.length;
- result.add(bytes);
- }
-
- final byte[] resultBytes = new byte[finalLength];
- int byteOffset = 0;
- for (final byte[] b : result) {
- System.arraycopy(b, 0, resultBytes, byteOffset, b.length);
- byteOffset += b.length;
+ this.registry.serializeSubobject(subobject, body);
}
- return resultBytes;
}
}
import com.google.common.base.Preconditions;
import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
-import org.opendaylight.protocol.pcep.impl.object.XROSubobjectUtil;
import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
import org.opendaylight.protocol.pcep.spi.XROSubobjectParser;
import org.opendaylight.protocol.pcep.spi.XROSubobjectSerializer;
-import org.opendaylight.protocol.util.ByteArray;
+import org.opendaylight.protocol.pcep.spi.XROSubobjectUtil;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.Subobject;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.SubobjectBuilder;
public static final int TYPE = 32;
- private static final int AS_NUMBER_LENGTH = 2;
-
- private static final int AS_NUMBER_OFFSET = 0;
-
- private static final int CONTENT_LENGTH = AS_NUMBER_LENGTH + AS_NUMBER_OFFSET;
+ private static final int CONTENT_LENGTH = 2;
@Override
public Subobject parseSubobject(final ByteBuf buffer, final boolean mandatory) throws PCEPDeserializerException {
}
@Override
- public byte[] serializeSubobject(final Subobject subobject) {
- if (!(subobject.getSubobjectType() instanceof AsNumberCase)) {
- throw new IllegalArgumentException("Unknown PCEPXROSubobject instance. Passed " + subobject.getSubobjectType().getClass()
- + ". Needed AsNumberCase.");
- }
- final byte[] retBytes = new byte[CONTENT_LENGTH];
- final AsNumberSubobject obj = ((AsNumberCase) subobject.getSubobjectType()).getAsNumber();
- System.arraycopy(ByteArray.longToBytes(obj.getAsNumber().getValue(), AS_NUMBER_LENGTH), 0, retBytes, AS_NUMBER_OFFSET,
- AS_NUMBER_LENGTH);
- return XROSubobjectUtil.formatSubobject(TYPE, subobject.isMandatory(), retBytes);
+ public void serializeSubobject(final Subobject subobject, final ByteBuf buffer) {
+ Preconditions.checkArgument(subobject.getSubobjectType() instanceof AsNumberCase, "Unknown subobject instance. Passed %s. Needed AsNumberCase.", subobject.getSubobjectType().getClass());
+ final AsNumberSubobject s = ((AsNumberCase) subobject.getSubobjectType()).getAsNumber();
+ XROSubobjectUtil.formatSubobject(TYPE, subobject.isMandatory(), Unpooled.copyShort(s.getAsNumber().getValue().shortValue()), buffer);
}
}
import com.google.common.primitives.UnsignedBytes;
import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
import org.opendaylight.protocol.concepts.Ipv4Util;
-import org.opendaylight.protocol.pcep.impl.object.XROSubobjectUtil;
import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
import org.opendaylight.protocol.pcep.spi.XROSubobjectParser;
import org.opendaylight.protocol.pcep.spi.XROSubobjectSerializer;
+import org.opendaylight.protocol.pcep.spi.XROSubobjectUtil;
import org.opendaylight.protocol.util.ByteArray;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpPrefix;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.Subobject;
public static final int TYPE = 1;
- private static final int IP4_F_LENGTH = 4;
private static final int PREFIX_F_LENGTH = 1;
- private static final int ATTRIBUTE_LENGTH = 1;
- private static final int IP_F_OFFSET = 0;
- private static final int PREFIX4_F_OFFSET = IP_F_OFFSET + IP4_F_LENGTH;
- private static final int ATTRIBUTE4_OFFSET = PREFIX4_F_OFFSET + PREFIX_F_LENGTH;
+ private static final int PREFIX4_F_OFFSET = Ipv4Util.IP4_LENGTH;
- private static final int CONTENT4_LENGTH = ATTRIBUTE4_OFFSET + ATTRIBUTE_LENGTH;
+ private static final int CONTENT4_LENGTH = PREFIX4_F_OFFSET + PREFIX_F_LENGTH + 1;
@Override
public Subobject parseSubobject(final ByteBuf buffer, final boolean mandatory) throws PCEPDeserializerException {
}
final int length = UnsignedBytes.toInt(buffer.getByte(PREFIX4_F_OFFSET));
IpPrefixBuilder prefix = new IpPrefixBuilder().setIpPrefix(new IpPrefix(Ipv4Util.prefixForBytes(ByteArray.readBytes(buffer,
- IP4_F_LENGTH), length)));
+ Ipv4Util.IP4_LENGTH), length)));
builder.setSubobjectType(new IpPrefixCaseBuilder().setIpPrefix(prefix.build()).build());
buffer.readerIndex(buffer.readerIndex() + PREFIX_F_LENGTH);
builder.setAttribute(Attribute.forValue(UnsignedBytes.toInt(buffer.readByte())));
}
@Override
- public byte[] serializeSubobject(final Subobject subobject) {
- if (!(subobject.getSubobjectType() instanceof IpPrefixCase)) {
- throw new IllegalArgumentException("Unknown PCEPXROSubobject instance. Passed " + subobject.getSubobjectType().getClass()
- + ". Needed IpPrefixCase.");
- }
-
+ public void serializeSubobject(final Subobject subobject, final ByteBuf buffer) {
+ Preconditions.checkArgument(subobject.getSubobjectType() instanceof IpPrefixCase, "Unknown subobject instance. Passed %s. Needed IpPrefixCase.", subobject.getSubobjectType().getClass());
final IpPrefixSubobject specObj = ((IpPrefixCase) subobject.getSubobjectType()).getIpPrefix();
final IpPrefix prefix = specObj.getIpPrefix();
-
- if (prefix.getIpv4Prefix() == null && prefix.getIpv6Prefix() == null) {
- throw new IllegalArgumentException("Unknown AbstractPrefix instance. Passed " + prefix.getClass() + ".");
- }
-
- if (prefix.getIpv4Prefix() == null) {
- return new XROIpv6PrefixSubobjectParser().serializeSubobject(subobject);
+ Preconditions.checkArgument(prefix.getIpv4Prefix() != null || prefix.getIpv6Prefix() != null, "Unknown AbstractPrefix instance. Passed %s.", prefix.getClass());
+ if (prefix.getIpv6Prefix() != null) {
+ new XROIpv6PrefixSubobjectParser().serializeSubobject(subobject, buffer);
+ } else {
+ final ByteBuf body = Unpooled.buffer(CONTENT4_LENGTH);
+ body.writeBytes(Ipv4Util.bytesForPrefix(prefix.getIpv4Prefix()));
+ body.writeByte(subobject.getAttribute().getIntValue());
+ XROSubobjectUtil.formatSubobject(TYPE, subobject.isMandatory(), body, buffer);
}
- final byte[] retBytes = new byte[CONTENT4_LENGTH];
- ByteArray.copyWhole(Ipv4Util.bytesForPrefix(prefix.getIpv4Prefix()), retBytes, IP_F_OFFSET);
- retBytes[PREFIX4_F_OFFSET] = UnsignedBytes.checkedCast(Ipv4Util.getPrefixLength(prefix));
- retBytes[ATTRIBUTE4_OFFSET] = UnsignedBytes.checkedCast(subobject.getAttribute().getIntValue());
- return XROSubobjectUtil.formatSubobject(TYPE, subobject.isMandatory(), retBytes);
}
}
import com.google.common.primitives.UnsignedBytes;
import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
-import org.opendaylight.protocol.concepts.Ipv4Util;
import org.opendaylight.protocol.concepts.Ipv6Util;
-import org.opendaylight.protocol.pcep.impl.object.XROSubobjectUtil;
import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
import org.opendaylight.protocol.pcep.spi.XROSubobjectParser;
import org.opendaylight.protocol.pcep.spi.XROSubobjectSerializer;
+import org.opendaylight.protocol.pcep.spi.XROSubobjectUtil;
import org.opendaylight.protocol.util.ByteArray;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpPrefix;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.Subobject;
public static final int TYPE = 2;
private static final int PREFIX_F_LENGTH = 1;
- private static final int ATTRIBUTE_LENGTH = 1;
- private static final int IP_F_OFFSET = 0;
+ private static final int PREFIX6_F_OFFSET = Ipv6Util.IPV6_LENGTH;
- private static final int IP6_F_LENGTH = 16;
- private static final int PREFIX6_F_OFFSET = IP_F_OFFSET + IP6_F_LENGTH;
- private static final int ATTRIBUTE6_OFFSET = PREFIX6_F_OFFSET + PREFIX_F_LENGTH;
-
- private static final int CONTENT6_LENGTH = ATTRIBUTE6_OFFSET + ATTRIBUTE_LENGTH;
+ private static final int CONTENT6_LENGTH = PREFIX6_F_OFFSET + PREFIX_F_LENGTH + 1;
@Override
public Subobject parseSubobject(final ByteBuf buffer, final boolean mandatory) throws PCEPDeserializerException {
}
final int length = UnsignedBytes.toInt(buffer.getByte(PREFIX6_F_OFFSET));
IpPrefixBuilder prefix = new IpPrefixBuilder().setIpPrefix(new IpPrefix(Ipv6Util.prefixForBytes(ByteArray.readBytes(buffer,
- IP6_F_LENGTH), length)));
+ Ipv6Util.IPV6_LENGTH), length)));
builder.setSubobjectType(new IpPrefixCaseBuilder().setIpPrefix(prefix.build()).build());
buffer.readerIndex(buffer.readerIndex() + PREFIX_F_LENGTH);
builder.setAttribute(Attribute.forValue(UnsignedBytes.toInt(buffer.readByte())));
}
@Override
- public byte[] serializeSubobject(final Subobject subobject) {
- if (!(subobject.getSubobjectType() instanceof IpPrefixCase)) {
- throw new IllegalArgumentException("Unknown PCEPXROSubobject instance. Passed " + subobject.getSubobjectType().getClass()
- + ". Needed IpPrefixCase.");
- }
+ public void serializeSubobject(final Subobject subobject, final ByteBuf buffer) {
+ Preconditions.checkArgument(subobject.getSubobjectType() instanceof IpPrefixCase, "Unknown subobject instance. Passed %s. Needed IpPrefixCase.", subobject.getSubobjectType().getClass());
final IpPrefixSubobject specObj = ((IpPrefixCase) subobject.getSubobjectType()).getIpPrefix();
final IpPrefix prefix = specObj.getIpPrefix();
-
- if (prefix.getIpv6Prefix() == null) {
- throw new IllegalArgumentException("Unknown AbstractPrefix instance. Passed " + prefix.getClass() + ".");
- }
- final byte[] retBytes = new byte[CONTENT6_LENGTH];
- ByteArray.copyWhole(Ipv6Util.bytesForPrefix(prefix.getIpv6Prefix()), retBytes, IP_F_OFFSET);
- retBytes[PREFIX6_F_OFFSET] = UnsignedBytes.checkedCast(Ipv4Util.getPrefixLength(prefix));
- retBytes[ATTRIBUTE6_OFFSET] = UnsignedBytes.checkedCast(subobject.getAttribute().getIntValue());
- return XROSubobjectUtil.formatSubobject(TYPE, subobject.isMandatory(), retBytes);
+ final ByteBuf body = Unpooled.buffer(CONTENT6_LENGTH);
+ body.writeBytes(Ipv6Util.bytesForPrefix(prefix.getIpv6Prefix()));
+ body.writeByte(subobject.getAttribute().getIntValue());
+ XROSubobjectUtil.formatSubobject(TYPE, subobject.isMandatory(), body, buffer);
}
}
import com.google.common.base.Preconditions;
import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
-import org.opendaylight.protocol.pcep.impl.object.XROSubobjectUtil;
import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
import org.opendaylight.protocol.pcep.spi.XROSubobjectParser;
import org.opendaylight.protocol.pcep.spi.XROSubobjectSerializer;
+import org.opendaylight.protocol.pcep.spi.XROSubobjectUtil;
import org.opendaylight.protocol.util.ByteArray;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.PathKey;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.PceId;
public static final int TYPE = 65;
- private static final int PK_F_LENGTH = 2;
-
private static final int PCE128_ID_F_LENGTH = 16;
- private static final int PK_F_OFFSET = 0;
- private static final int PCE_ID_F_OFFSET = PK_F_OFFSET + PK_F_LENGTH;
-
- private static final int CONTENT128_LENGTH = PCE_ID_F_OFFSET + PCE128_ID_F_LENGTH;
+ private static final int CONTENT128_LENGTH = 2 + PCE128_ID_F_LENGTH;
@Override
public Subobject parseSubobject(final ByteBuf buffer, final boolean mandatory) throws PCEPDeserializerException {
}
@Override
- public byte[] serializeSubobject(final Subobject subobject) {
+ public void serializeSubobject(final Subobject subobject, final ByteBuf buffer) {
+ Preconditions.checkArgument(subobject.getSubobjectType() instanceof PathKeyCase, "Unknown subobject instance. Passed %s. Needed PathKey.", subobject.getSubobjectType().getClass());
final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.subobject.subobject.type.path.key._case.PathKey pk = ((PathKeyCase) subobject.getSubobjectType()).getPathKey();
- final int pathKey = pk.getPathKey().getValue();
- final byte[] pceId = pk.getPceId().getBinary();
- final byte[] retBytes = new byte[PK_F_LENGTH + pceId.length];
- System.arraycopy(ByteArray.shortToBytes((short) pathKey), 0, retBytes, PK_F_OFFSET, PK_F_LENGTH);
- System.arraycopy(pceId, 0, retBytes, PCE_ID_F_OFFSET, pceId.length);
- return XROSubobjectUtil.formatSubobject(TYPE, subobject.isMandatory(), retBytes);
+ final ByteBuf body = Unpooled.buffer();
+ body.writeShort(pk.getPathKey().getValue().shortValue());
+ body.writeBytes(pk.getPceId().getBinary());
+ XROSubobjectUtil.formatSubobject(TYPE, subobject.isMandatory(), body, buffer);
}
}
import com.google.common.base.Preconditions;
import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
-import org.opendaylight.protocol.pcep.impl.object.XROSubobjectUtil;
import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
import org.opendaylight.protocol.pcep.spi.XROSubobjectParser;
import org.opendaylight.protocol.pcep.spi.XROSubobjectSerializer;
+import org.opendaylight.protocol.pcep.spi.XROSubobjectUtil;
import org.opendaylight.protocol.util.ByteArray;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.PathKey;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.PceId;
public static final int TYPE = 64;
- private static final int PK_F_LENGTH = 2;
private static final int PCE_ID_F_LENGTH = 4;
- private static final int PK_F_OFFSET = 0;
- private static final int PCE_ID_F_OFFSET = PK_F_OFFSET + PK_F_LENGTH;
-
- private static final int CONTENT_LENGTH = PCE_ID_F_OFFSET + PCE_ID_F_LENGTH;
+ private static final int CONTENT_LENGTH = 2 + PCE_ID_F_LENGTH;
@Override
public Subobject parseSubobject(final ByteBuf buffer, final boolean mandatory) throws PCEPDeserializerException {
}
@Override
- public byte[] serializeSubobject(final Subobject subobject) {
+ public void serializeSubobject(final Subobject subobject, final ByteBuf buffer) {
+ Preconditions.checkArgument(subobject.getSubobjectType() instanceof PathKeyCase, "Unknown subobject instance. Passed %s. Needed PathKey.", subobject.getSubobjectType().getClass());
final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.subobject.subobject.type.path.key._case.PathKey pk = ((PathKeyCase) subobject.getSubobjectType()).getPathKey();
- final int pathKey = pk.getPathKey().getValue();
- final byte[] pceId = pk.getPceId().getBinary();
- final byte[] retBytes = new byte[PK_F_LENGTH + pceId.length];
- System.arraycopy(ByteArray.shortToBytes((short) pathKey), 0, retBytes, PK_F_OFFSET, PK_F_LENGTH);
- System.arraycopy(pceId, 0, retBytes, PCE_ID_F_OFFSET, pceId.length);
- return XROSubobjectUtil.formatSubobject(TYPE, subobject.isMandatory(), retBytes);
+ final ByteBuf body = Unpooled.buffer();
+ body.writeShort(pk.getPathKey().getValue().shortValue());
+ body.writeBytes(pk.getPceId().getBinary());
+ XROSubobjectUtil.formatSubobject(TYPE, subobject.isMandatory(), body, buffer);
}
}
package org.opendaylight.protocol.pcep.impl.subobject;
import com.google.common.base.Preconditions;
-import com.google.common.primitives.UnsignedBytes;
import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
-import org.opendaylight.protocol.pcep.impl.object.XROSubobjectUtil;
import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
import org.opendaylight.protocol.pcep.spi.XROSubobjectParser;
import org.opendaylight.protocol.pcep.spi.XROSubobjectSerializer;
-import org.opendaylight.protocol.util.ByteArray;
+import org.opendaylight.protocol.pcep.spi.XROSubobjectUtil;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.Subobject;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.SubobjectBuilder;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.ExcludeRouteSubobjects.Attribute;
public static final int TYPE = 34;
- private static final int SRLG_ID_NUMBER_LENGTH = 4;
- private static final int ATTRIBUTE_LENGTH = 1;
-
- private static final int SRLG_ID_NUMBER_OFFSET = 0;
- private static final int ATTRIBUTE_OFFSET = SRLG_ID_NUMBER_OFFSET + SRLG_ID_NUMBER_LENGTH;
-
- private static final int CONTENT_LENGTH = SRLG_ID_NUMBER_LENGTH + ATTRIBUTE_LENGTH;
+ private static final int CONTENT_LENGTH = 5;
@Override
public Subobject parseSubobject(final ByteBuf buffer, final boolean mandatory) throws PCEPDeserializerException {
throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.readableBytes() + "; Expected: "
+ CONTENT_LENGTH + ".");
}
-
final SubobjectBuilder builder = new SubobjectBuilder();
builder.setMandatory(mandatory);
builder.setAttribute(Attribute.Srlg);
}
@Override
- public byte[] serializeSubobject(final Subobject subobject) {
- if (!(subobject.getSubobjectType() instanceof SrlgCase)) {
- throw new IllegalArgumentException("Unknown PCEPXROSubobject instance. Passed " + subobject.getSubobjectType().getClass()
- + ". Needed SrlgCase.");
- }
-
- byte[] retBytes;
- retBytes = new byte[CONTENT_LENGTH];
+ public void serializeSubobject(final Subobject subobject, final ByteBuf buffer) {
+ Preconditions.checkArgument(subobject.getSubobjectType() instanceof SrlgCase, "Unknown subobject instance. Passed %s. Needed SrlgCase.", subobject.getSubobjectType().getClass());
final SrlgSubobject specObj = ((SrlgCase) subobject.getSubobjectType()).getSrlg();
-
- ByteArray.copyWhole(ByteArray.longToBytes(specObj.getSrlgId().getValue(), SRLG_ID_NUMBER_LENGTH), retBytes, SRLG_ID_NUMBER_OFFSET);
- retBytes[ATTRIBUTE_OFFSET] = UnsignedBytes.checkedCast(subobject.getAttribute().getIntValue());
-
- return XROSubobjectUtil.formatSubobject(TYPE, subobject.isMandatory(), retBytes);
+ final ByteBuf body = Unpooled.buffer(CONTENT_LENGTH);
+ body.writeInt(specObj.getSrlgId().getValue().intValue());
+ body.writeByte(subobject.getAttribute().getIntValue());
+ XROSubobjectUtil.formatSubobject(TYPE, subobject.isMandatory(), body, buffer);
}
}
import com.google.common.primitives.UnsignedBytes;
import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
-import org.opendaylight.protocol.pcep.impl.object.XROSubobjectUtil;
import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
import org.opendaylight.protocol.pcep.spi.XROSubobjectParser;
import org.opendaylight.protocol.pcep.spi.XROSubobjectSerializer;
-import org.opendaylight.protocol.util.ByteArray;
+import org.opendaylight.protocol.pcep.spi.XROSubobjectUtil;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.Subobject;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.SubobjectBuilder;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.ExcludeRouteSubobjects.Attribute;
public static final int TYPE = 4;
- private static final int ATTRIBUTE_LENGTH = 1;
- private static final int ROUTER_ID_NUMBER_LENGTH = 4;
- private static final int INTERFACE_ID_NUMBER_LENGTH = 4;
+ private static final int RESERVED = 1;
- private static final int ATTRIBUTE_OFFSET = 1;
- private static final int ROUTER_ID_NUMBER_OFFSET = ATTRIBUTE_OFFSET + ATTRIBUTE_LENGTH;
- private static final int INTERFACE_ID_NUMBER_OFFSET = ROUTER_ID_NUMBER_OFFSET + ROUTER_ID_NUMBER_LENGTH;
-
- private static final int CONTENT_LENGTH = INTERFACE_ID_NUMBER_OFFSET + INTERFACE_ID_NUMBER_LENGTH;
+ private static final int CONTENT_LENGTH = 10;
@Override
public Subobject parseSubobject(final ByteBuf buffer, final boolean mandatory) throws PCEPDeserializerException {
throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.readableBytes() + "; Expected: "
+ CONTENT_LENGTH + ".");
}
- buffer.readerIndex(buffer.readerIndex() + ATTRIBUTE_OFFSET);
+ buffer.readerIndex(buffer.readerIndex() + RESERVED);
final SubobjectBuilder builder = new SubobjectBuilder();
builder.setMandatory(mandatory);
builder.setAttribute(Attribute.forValue(UnsignedBytes.toInt(buffer.readByte())));
}
@Override
- public byte[] serializeSubobject(final Subobject subobject) {
- if (!(subobject.getSubobjectType() instanceof UnnumberedCase)) {
- throw new IllegalArgumentException("Unknown PCEPXROSubobject instance. Passed " + subobject.getSubobjectType().getClass()
- + ". Needed UnnumberedCase.");
- }
-
- final byte[] retBytes = new byte[CONTENT_LENGTH];
+ public void serializeSubobject(final Subobject subobject, final ByteBuf buffer) {
+ Preconditions.checkArgument(subobject.getSubobjectType() instanceof UnnumberedCase, "Unknown subobject instance. Passed %s. Needed UnnumberedCase.", subobject.getSubobjectType().getClass());
final UnnumberedSubobject specObj = ((UnnumberedCase) subobject.getSubobjectType()).getUnnumbered();
-
- retBytes[ATTRIBUTE_OFFSET] = UnsignedBytes.checkedCast(subobject.getAttribute().getIntValue());
- ByteArray.copyWhole(ByteArray.longToBytes(specObj.getRouterId(), ROUTER_ID_NUMBER_LENGTH), retBytes, ROUTER_ID_NUMBER_OFFSET);
- System.arraycopy(ByteArray.longToBytes(specObj.getInterfaceId(), INTERFACE_ID_NUMBER_LENGTH), 0, retBytes,
- INTERFACE_ID_NUMBER_OFFSET, INTERFACE_ID_NUMBER_LENGTH);
- return XROSubobjectUtil.formatSubobject(TYPE, subobject.isMandatory(), retBytes);
+ final ByteBuf body = Unpooled.buffer(CONTENT_LENGTH);
+ body.writeZero(RESERVED);
+ body.writeByte(subobject.getAttribute().getIntValue());
+ body.writeInt(specObj.getRouterId().intValue());
+ body.writeInt(specObj.getInterfaceId().intValue());
+ XROSubobjectUtil.formatSubobject(TYPE, subobject.isMandatory(), 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;
subs.setSubobjectType(new IpPrefixCaseBuilder().setIpPrefix(
new IpPrefixBuilder().setIpPrefix(new IpPrefix(new Ipv4Prefix("255.255.255.255/22"))).build()).build());
assertEquals(subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(ip4PrefixBytes, 2)), false));
- assertArrayEquals(ip4PrefixBytes, parser.serializeSubobject(subs.build()));
+ final ByteBuf buff = Unpooled.buffer();
+ parser.serializeSubobject(subs.build(), buff);
+ assertArrayEquals(ip4PrefixBytes, ByteArray.getAllBytes(buff));
}
@Test
(byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
(byte) 0xFF, (byte) 0xFF, (byte) 0xFF }, 22))).build()).build());
assertEquals(subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(ip6PrefixBytes, 2)), true));
- assertArrayEquals(ip6PrefixBytes, parser.serializeSubobject(subs.build()));
+ final ByteBuf buff = Unpooled.buffer();
+ parser.serializeSubobject(subs.build(), buff);
+ assertArrayEquals(ip6PrefixBytes, ByteArray.getAllBytes(buff));
}
@Test
subs.setAttribute(Attribute.Srlg);
subs.setSubobjectType(new SrlgCaseBuilder().setSrlg(new SrlgBuilder().setSrlgId(new SrlgId(0x12345678L)).build()).build());
assertEquals(subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(srlgBytes, 2)), true));
- assertArrayEquals(srlgBytes, parser.serializeSubobject(subs.build()));
+ final ByteBuf buff = Unpooled.buffer();
+ parser.serializeSubobject(subs.build(), buff);
+ assertArrayEquals(srlgBytes, ByteArray.getAllBytes(buff));
}
@Test
subs.setSubobjectType(new UnnumberedCaseBuilder().setUnnumbered(
new UnnumberedBuilder().setRouterId(0x12345000L).setInterfaceId(0xffffffffL).build()).build());
assertEquals(subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(unnumberedBytes, 2)), true));
- assertArrayEquals(unnumberedBytes, parser.serializeSubobject(subs.build()));
+ final ByteBuf buff = Unpooled.buffer();
+ parser.serializeSubobject(subs.build(), buff);
+ assertArrayEquals(unnumberedBytes, ByteArray.getAllBytes(buff));
}
@Test
subs.setMandatory(true);
subs.setSubobjectType(new AsNumberCaseBuilder().setAsNumber(new AsNumberBuilder().setAsNumber(new AsNumber(0x64L)).build()).build());
assertEquals(subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(asNumberBytes, 2)), true));
- assertArrayEquals(asNumberBytes, parser.serializeSubobject(subs.build()));
+ final ByteBuf buff = Unpooled.buffer();
+ parser.serializeSubobject(subs.build(), buff);
+ assertArrayEquals(asNumberBytes, ByteArray.getAllBytes(buff));
}
@Test
pBuilder.setPathKey(new PathKey(4660));
subs.setSubobjectType(new PathKeyCaseBuilder().setPathKey(pBuilder.build()).build());
assertEquals(subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(pathKey32Bytes, 2)), true));
- assertArrayEquals(pathKey32Bytes, parser.serializeSubobject(subs.build()));
+ final ByteBuf buff = Unpooled.buffer();
+ parser.serializeSubobject(subs.build(), buff);
+ assertArrayEquals(pathKey32Bytes, ByteArray.getAllBytes(buff));
}
@Test
pBuilder.setPathKey(new PathKey(4660));
subs.setSubobjectType(new PathKeyCaseBuilder().setPathKey(pBuilder.build()).build());
assertEquals(subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(pathKey128Bytes, 2)), true));
- assertArrayEquals(pathKey128Bytes, parser.serializeSubobject(subs.build()));
+ final ByteBuf buff = Unpooled.buffer();
+ parser.serializeSubobject(subs.build(), buff);
+ assertArrayEquals(pathKey128Bytes, ByteArray.getAllBytes(buff));
}
}
* Find serializer for given subobject. Delegates parsing to found serializer.
*
* @param subobject to be parsed
- * @return null if the serializer for this subobject could not be found
+ * @param buffer buffer where the serialized subobject will be parsed
*/
- byte[] serializeSubobject(final Subobject subobject);
+ void serializeSubobject(final Subobject subobject, 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.exclude.route.object.xro.Subobject;
public interface XROSubobjectSerializer {
- byte[] serializeSubobject(Subobject subobject);
+ void serializeSubobject(final Subobject subobject, final ByteBuf buffer);
}
--- /dev/null
+/*
+ * Copyright (c) 2013 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 XROSubobjectUtil {
+
+ private static final int HEADER_SIZE = 2;
+
+ private XROSubobjectUtil() {
+ }
+
+ public static void formatSubobject(final int type, final boolean mandatory, final ByteBuf body, final ByteBuf buffer) {
+ buffer.writeByte(type | (mandatory ? 1 << 7 : 0));
+ buffer.writeByte(body.writerIndex() + HEADER_SIZE);
+ buffer.writeBytes(body);
+ }
+}
}
@Override
- public byte[] serializeSubobject(Subobject subobject) {
+ public void serializeSubobject(final Subobject subobject, final ByteBuf buffer) {
final XROSubobjectSerializer serializer = this.handlers.getSerializer(subobject.getSubobjectType().getImplementedInterface());
if (serializer == null) {
- return null;
+ return;
}
- return serializer.serializeSubobject(subobject);
+ serializer.serializeSubobject(subobject, buffer);
}
}