public void serializeSubobject(final Subobject subobject, final ByteBuf buffer) {
Preconditions.checkArgument(subobject.getSubobjectType() instanceof LabelCase, "Unknown subobject instance. Passed %s. Needed LabelCase.", subobject.getSubobjectType().getClass());
final Label label = ((LabelCase) subobject.getSubobjectType()).getLabel();
- // FIXME: switch to ByteBuf
- final byte[] labelbytes = this.registry.serializeLabel(label.isUniDirectional(), false, label.getLabelType());
- if (labelbytes == null) {
- throw new IllegalArgumentException("Unknown EROLabelSubobject instance. Passed "
- + label.getLabelType().getImplementedInterface());
- }
- EROSubobjectUtil.formatSubobject(TYPE, subobject.isLoose(), Unpooled.copiedBuffer(labelbytes), buffer);
+ final ByteBuf body = Unpooled.buffer();
+ this.registry.serializeLabel(label.isUniDirectional(), false, label.getLabelType(), body);
+ EROSubobjectUtil.formatSubobject(TYPE, subobject.isLoose(), body, buffer);
}
}
import com.google.common.base.Preconditions;
import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
import org.opendaylight.protocol.pcep.spi.LabelParser;
import org.opendaylight.protocol.pcep.spi.LabelSerializer;
}
@Override
- public byte[] serializeLabel(final boolean unidirectional, final boolean global, final LabelType subobject) {
- if (!(subobject instanceof GeneralizedLabelCase)) {
- throw new IllegalArgumentException("Unknown Label Subobject instance. Passed " + subobject.getClass()
- + ". Needed GeneralizedLabelCase.");
- }
- return LabelUtil.formatLabel(CTYPE, unidirectional, global,
- ((GeneralizedLabelCase) subobject).getGeneralizedLabel().getGeneralizedLabel());
+ public void serializeLabel(final boolean unidirectional, final boolean global, final LabelType subobject, final ByteBuf buffer) {
+ Preconditions.checkArgument(subobject instanceof GeneralizedLabelCase, "Unknown Label Subobject instance. Passed {}. Needed GeneralizedLabelCase.", subobject.getClass());
+ final ByteBuf body = Unpooled.wrappedBuffer(((GeneralizedLabelCase) subobject).getGeneralizedLabel().getGeneralizedLabel());
+ LabelUtil.formatLabel(CTYPE, unidirectional, global, body, buffer);
}
}
public void serializeSubobject(final Subobject subobject, final ByteBuf buffer) {
Preconditions.checkNotNull(subobject.getSubobjectType(), "Subobject type cannot be empty.");
final Label label = ((LabelCase) subobject.getSubobjectType()).getLabel();
- final byte[] labelbytes = this.registry.serializeLabel(label.isUniDirectional(), false, label.getLabelType());
- if (labelbytes == null) {
- throw new IllegalArgumentException("Unknown EROLabelSubobject instance. Passed "
- + label.getLabelType().getImplementedInterface());
- }
- //FIXME: switch to ByteBuf
- RROSubobjectUtil.formatSubobject(TYPE, Unpooled.copiedBuffer(labelbytes), buffer);
+ final ByteBuf body = Unpooled.buffer();
+ this.registry.serializeLabel(label.isUniDirectional(), false, label.getLabelType(), body);
+ RROSubobjectUtil.formatSubobject(TYPE, body, buffer);
}
}
import com.google.common.base.Preconditions;
import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
import org.opendaylight.protocol.pcep.spi.LabelParser;
import org.opendaylight.protocol.pcep.spi.LabelSerializer;
import org.opendaylight.protocol.pcep.spi.LabelUtil;
import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
-import org.opendaylight.protocol.util.ByteArray;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.label.subobject.LabelType;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.label.subobject.label.type.Type1LabelCase;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.label.subobject.label.type.Type1LabelCaseBuilder;
}
@Override
- public byte[] serializeLabel(final boolean unidirectional, final boolean global, final LabelType subobject) {
- if (!(subobject instanceof Type1LabelCase)) {
- throw new IllegalArgumentException("Unknown Label Subobject instance. Passed " + subobject.getClass()
- + ". Needed Type1LabelCase.");
- }
- return LabelUtil.formatLabel(CTYPE, unidirectional, global, ByteArray.longToBytes(
- ((Type1LabelCase) subobject).getType1Label().getType1Label().longValue(), LABEL_LENGTH));
+ public void serializeLabel(final boolean unidirectional, final boolean global, final LabelType subobject, final ByteBuf buffer) {
+ Preconditions.checkArgument(subobject instanceof Type1LabelCase, "Unknown Label Subobject instance. Passed {}. Needed Type1LabelCase.", subobject.getClass());
+ LabelUtil.formatLabel(CTYPE, unidirectional, global, Unpooled.copyInt(
+ ((Type1LabelCase) subobject).getType1Label().getType1Label().intValue()), buffer);
}
}
import com.google.common.base.Preconditions;
import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
import org.opendaylight.protocol.pcep.spi.LabelParser;
import org.opendaylight.protocol.pcep.spi.LabelSerializer;
import org.opendaylight.protocol.pcep.spi.LabelUtil;
import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
-import org.opendaylight.protocol.util.ByteArray;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.label.subobject.LabelType;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.label.subobject.label.type.WavebandSwitchingLabelCase;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.label.subobject.label.type.WavebandSwitchingLabelCaseBuilder;
}
@Override
- public byte[] serializeLabel(final boolean unidirectional, final boolean global, final LabelType subobject) {
- if (!(subobject instanceof WavebandSwitchingLabelCase)) {
- throw new IllegalArgumentException("Unknown Label Subobject instance. Passed " + subobject.getClass()
- + ". Needed WavebandSwitchingLabelCase.");
- }
- final byte[] retBytes = new byte[CONTENT_LENGTH];
+ public void serializeLabel(final boolean unidirectional, final boolean global, final LabelType subobject, final ByteBuf buffer) {
+ Preconditions.checkArgument(subobject instanceof WavebandSwitchingLabelCase, "Unknown Label Subobject instance. Passed {}. Needed WavebandSwitchingLabelCase.", subobject.getClass());
final WavebandSwitchingLabel obj = ((WavebandSwitchingLabelCase) subobject).getWavebandSwitchingLabel();
- System.arraycopy(ByteArray.intToBytes(obj.getWavebandId().intValue(), WAVEB_F_LENGTH), 0, retBytes, 0, WAVEB_F_LENGTH);
- System.arraycopy(ByteArray.intToBytes(obj.getStartLabel().intValue(), START_F_LENGTH), 0, retBytes, WAVEB_F_LENGTH, START_F_LENGTH);
- System.arraycopy(ByteArray.intToBytes(obj.getEndLabel().intValue(), END_F_LENGTH), 0, retBytes, WAVEB_F_LENGTH + START_F_LENGTH,
- END_F_LENGTH);
- return LabelUtil.formatLabel(CTYPE, unidirectional, global, retBytes);
+ final ByteBuf body = Unpooled.buffer(CONTENT_LENGTH);
+ body.writeInt(obj.getWavebandId().intValue());
+ body.writeInt(obj.getStartLabel().intValue());
+ body.writeInt(obj.getEndLabel().intValue());
+ LabelUtil.formatLabel(CTYPE, unidirectional, global, 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;
iBuilder.setGeneralizedLabel(ByteArray.cutBytes(generalizedLabelBytes, 2));
final GeneralizedLabelCaseBuilder builder = new GeneralizedLabelCaseBuilder().setGeneralizedLabel(iBuilder.build());
assertEquals(builder.build(), parser.parseLabel(Unpooled.wrappedBuffer(ByteArray.cutBytes(generalizedLabelBytes, 2))));
- assertArrayEquals(generalizedLabelBytes, parser.serializeLabel(true, false, builder.build()));
+ final ByteBuf buff = Unpooled.buffer();
+ parser.serializeLabel(true, false, builder.build(), buff);
+ assertArrayEquals(generalizedLabelBytes, ByteArray.getAllBytes(buff));
}
@Test
iBuilder.setEndLabel(0x1111L);
final WavebandSwitchingLabelCaseBuilder builder = new WavebandSwitchingLabelCaseBuilder().setWavebandSwitchingLabel(iBuilder.build());
assertEquals(builder.build(), parser.parseLabel(Unpooled.wrappedBuffer(ByteArray.cutBytes(wavebandLabelBytes, 2))));
- assertArrayEquals(wavebandLabelBytes, parser.serializeLabel(false, true, builder.build()));
+ final ByteBuf buff = Unpooled.buffer();
+ parser.serializeLabel(false, true, builder.build(), buff);
+ assertArrayEquals(wavebandLabelBytes, ByteArray.getAllBytes(buff));
}
@Test
iBuilder.setType1Label(0x120025ffL);
final Type1LabelCaseBuilder builder = new Type1LabelCaseBuilder().setType1Label(iBuilder.build());
assertEquals(builder.build(), parser.parseLabel(Unpooled.wrappedBuffer(ByteArray.cutBytes(typeOneLabelBytes, 2))));
- assertArrayEquals(typeOneLabelBytes, parser.serializeLabel(true, true, builder.build()));
+ final ByteBuf buff = Unpooled.buffer();
+ parser.serializeLabel(true, true, builder.build(), buff);
+ assertArrayEquals(typeOneLabelBytes, ByteArray.getAllBytes(buff));
}
}
/**
* Find serializer for given label. Delegates parsing to found serializer.
*
- * @param label to be parsed
* @param unidirectional label common header flag
* @param global label commom header flag
- * @return null if the serializer for this label could not be found
+ * @param label to be parsed
+ * @param buffer buffer where the serialized label will be parsed
*/
- byte[] serializeLabel(final boolean unidirectional, final boolean global, final LabelType label);
+ void serializeLabel(final boolean unidirectional, final boolean global, final LabelType label, 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.rsvp.rev130820.label.subobject.LabelType;
public interface LabelSerializer {
- byte[] serializeLabel(boolean unidirectional, boolean global, LabelType subobject);
+ void serializeLabel(boolean unidirectional, boolean global, final LabelType subobject, final ByteBuf buffer);
}
*/
package org.opendaylight.protocol.pcep.spi;
-import com.google.common.primitives.UnsignedBytes;
+import io.netty.buffer.ByteBuf;
import java.util.BitSet;
private static final int RES_F_LENGTH = 1;
- private static final int C_TYPE_F_LENGTH = 1;
-
- private static final int RES_F_OFFSET = 0;
-
- private static final int C_TYPE_F_OFFSET = RES_F_OFFSET + RES_F_LENGTH;
-
- private static final int HEADER_OFFSET = C_TYPE_F_OFFSET + C_TYPE_F_LENGTH;
-
private static final int U_FLAG_OFFSET = 0;
private static final int G_FLAG_OFFSET = 7;
throw new UnsupportedOperationException("Utility class should not be instantiated");
}
- public static byte[] formatLabel(final int type, final boolean unidirectional, final boolean global, final byte[] labelbytes) {
-
- final byte[] retBytes = new byte[labelbytes.length + HEADER_OFFSET];
-
- System.arraycopy(labelbytes, 0, retBytes, HEADER_OFFSET, labelbytes.length);
-
- final BitSet reserved = new BitSet();
+ public static void formatLabel(final int type, final boolean unidirectional, final boolean global, final ByteBuf body, final ByteBuf buffer) {
+ final BitSet reserved = new BitSet(RES_F_LENGTH * Byte.SIZE);
reserved.set(U_FLAG_OFFSET, unidirectional);
reserved.set(G_FLAG_OFFSET, global);
- System.arraycopy(ByteArray.bitSetToBytes(reserved, RES_F_LENGTH), 0, retBytes, RES_F_OFFSET, RES_F_LENGTH);
- retBytes[C_TYPE_F_OFFSET] = UnsignedBytes.checkedCast(type);
- return retBytes;
+ buffer.writeBytes(ByteArray.bitSetToBytes(reserved, RES_F_LENGTH));
+ buffer.writeByte(type);
+ buffer.writeBytes(body);
}
}
}
@Override
- public byte[] serializeLabel(final boolean unidirectional, final boolean global, final LabelType label) {
+ public void serializeLabel(final boolean unidirectional, final boolean global, final LabelType label, final ByteBuf buffer) {
final LabelSerializer serializer = this.handlers.getSerializer(label.getImplementedInterface());
- if (serializer == null) {
- return null;
+ if (serializer != null) {
+ serializer.serializeLabel(unidirectional, global, label, buffer);
}
- return serializer.serializeLabel(unidirectional, global, label);
}
}
@Test
public void testLabelUtil() {
byte[] expected = { (byte) 0x81, 0x04, 0x01, 0x02, 0x03, 0x04 };
- assertArrayEquals(expected, LabelUtil.formatLabel(4, true, true, new byte[] { 1, 2, 3, 4 }));
+ ByteBuf out = Unpooled.buffer();
+ ByteBuf body = Unpooled.copiedBuffer(new byte[] { 1, 2, 3, 4 });
+ LabelUtil.formatLabel(4, true, true, body, out);
+ assertArrayEquals(expected, ByteArray.getAllBytes(out));
}
@Test