--- /dev/null
+/*
+ * Copyright (c) 2015 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.rsvp.parser.impl.subobject;
+
+import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeShort;
+
+import com.google.common.base.Preconditions;
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
+import org.opendaylight.protocol.rsvp.parser.spi.RSVPParsingException;
+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.rsvp.rev150820.AsNumberSubobject;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.AsNumberCase;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.AsNumberCaseBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.as.number._case.AsNumberBuilder;
+
+public class AsNumberCaseParser {
+ private static final int CONTENT_LENGTH = 2;
+
+ private AsNumberCaseParser() {
+ throw new UnsupportedOperationException();
+ }
+
+ public static AsNumberCase parseSubobject(final ByteBuf buffer) throws RSVPParsingException {
+ Preconditions.checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
+ if (buffer.readableBytes() != CONTENT_LENGTH) {
+ throw new RSVPParsingException("Wrong length of array of bytes. Passed: " + buffer.readableBytes() + "; Expected: "
+ + CONTENT_LENGTH + ".");
+ }
+ return new AsNumberCaseBuilder().setAsNumber(new AsNumberBuilder().setAsNumber(new AsNumber((long) buffer.readUnsignedShort())).build()).build();
+ }
+
+
+ public static ByteBuf serializeSubobject(final AsNumberCase asCase) {
+ final AsNumberSubobject asNumber = asCase.getAsNumber();
+ final ByteBuf body = Unpooled.buffer(CONTENT_LENGTH);
+ Preconditions.checkArgument(asNumber.getAsNumber() != null, "AsNumber is mandatory.");
+ writeShort(asNumber.getAsNumber().getValue().shortValue(), body);
+ return body;
+ }
+}
--- /dev/null
+/*
+ * Copyright (c) 2015 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.rsvp.parser.impl.subobject.Label;
+
+import com.google.common.base.Preconditions;
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
+import org.opendaylight.protocol.rsvp.parser.spi.LabelParser;
+import org.opendaylight.protocol.rsvp.parser.spi.LabelSerializer;
+import org.opendaylight.protocol.rsvp.parser.spi.RSVPParsingException;
+import org.opendaylight.protocol.util.ByteArray;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.label.subobject.LabelType;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.label.subobject.label.type.GeneralizedLabelCase;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.label.subobject.label.type.GeneralizedLabelCaseBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.label.subobject.label.type.generalized.label._case.GeneralizedLabel;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.label.subobject.label.type.generalized.label._case.GeneralizedLabelBuilder;
+
+/**
+ * Parser for {@link GeneralizedLabelCase}
+ */
+public class GeneralizedLabelParser implements LabelParser, LabelSerializer {
+
+ public static final int CTYPE = 2;
+
+ @Override
+ public final LabelType parseLabel(final ByteBuf buffer) throws RSVPParsingException {
+ Preconditions.checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
+ return new GeneralizedLabelCaseBuilder().setGeneralizedLabel(
+ new GeneralizedLabelBuilder().setGeneralizedLabel(ByteArray.readAllBytes(buffer)).build()).build();
+ }
+
+ @Override
+ public final 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 GeneralizedLabel gl = ((GeneralizedLabelCase) subobject).getGeneralizedLabel();
+ Preconditions.checkArgument(gl != null, "GeneralizedLabel is mandatory.");
+ final ByteBuf body = Unpooled.wrappedBuffer(gl.getGeneralizedLabel());
+ LabelUtil.formatLabel(CTYPE, unidirectional, global, body, buffer);
+ }
+}
\ No newline at end of file
--- /dev/null
+/*
+ * Copyright (c) 2015 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.rsvp.parser.impl.subobject.Label;
+
+import io.netty.buffer.ByteBuf;
+import org.opendaylight.protocol.util.BitArray;
+
+public final class LabelUtil {
+
+ private static final int FLAGS_SIZE = 8;
+
+ private static final int UNIDIRECTIONAL = 0;
+ private static final int GLOBAL = 7;
+
+ private LabelUtil() {
+ throw new UnsupportedOperationException();
+ }
+
+ public static void formatLabel(final int type, final Boolean unidirectional, final Boolean global, final ByteBuf body, final ByteBuf buffer) {
+ final BitArray reserved = new BitArray(FLAGS_SIZE);
+ reserved.set(UNIDIRECTIONAL, unidirectional);
+ reserved.set(GLOBAL, global);
+ reserved.toByteBuf(buffer);
+ buffer.writeByte(type);
+ buffer.writeBytes(body);
+ }
+}
\ No newline at end of file
--- /dev/null
+/*
+ * Copyright (c) 2015 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.rsvp.parser.impl.subobject.Label;
+
+import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedInt;
+
+import com.google.common.base.Preconditions;
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
+import org.opendaylight.protocol.rsvp.parser.spi.LabelParser;
+import org.opendaylight.protocol.rsvp.parser.spi.LabelSerializer;
+import org.opendaylight.protocol.rsvp.parser.spi.RSVPParsingException;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.label.subobject.LabelType;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.label.subobject.label.type.Type1LabelCase;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.label.subobject.label.type.Type1LabelCaseBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.label.subobject.label.type.type1.label._case.Type1Label;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.label.subobject.label.type.type1.label._case.Type1LabelBuilder;
+
+/**
+ * Parser for {@link Type1LabelCase}
+ */
+public class Type1LabelParser implements LabelParser, LabelSerializer {
+
+ public static final int CTYPE = 1;
+
+ public static final int LABEL_LENGTH = 4;
+
+ @Override
+ public LabelType parseLabel(final ByteBuf buffer) throws RSVPParsingException {
+ Preconditions.checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
+ if (buffer.readableBytes() != LABEL_LENGTH) {
+ throw new RSVPParsingException("Wrong length of array of bytes. Passed: " + buffer.readableBytes() + "; Expected: "
+ + LABEL_LENGTH + ".");
+ }
+ return new Type1LabelCaseBuilder().setType1Label(new Type1LabelBuilder().setType1Label(buffer.readUnsignedInt()).build()).build();
+ }
+
+ @Override
+ 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());
+ final ByteBuf body = Unpooled.buffer(LABEL_LENGTH);
+ final Type1Label type1Label = ((Type1LabelCase) subobject).getType1Label();
+ Preconditions.checkArgument(type1Label != null, "Type1Label is mandatory.");
+ writeUnsignedInt(type1Label.getType1Label(), body);
+ LabelUtil.formatLabel(CTYPE, unidirectional, global, body, buffer);
+ }
+}
--- /dev/null
+/*
+ * Copyright (c) 2015 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.rsvp.parser.impl.subobject.Label;
+
+import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedInt;
+
+import com.google.common.base.Preconditions;
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
+import org.opendaylight.protocol.rsvp.parser.spi.LabelParser;
+import org.opendaylight.protocol.rsvp.parser.spi.LabelSerializer;
+import org.opendaylight.protocol.rsvp.parser.spi.RSVPParsingException;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.label.subobject.LabelType;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.label.subobject.label.type.WavebandSwitchingLabelCase;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.label.subobject.label.type.WavebandSwitchingLabelCaseBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.label.subobject.label.type.waveband.switching.label._case.WavebandSwitchingLabel;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.label.subobject.label.type.waveband.switching.label._case.WavebandSwitchingLabelBuilder;
+
+/**
+ * Parser for {@link WavebandSwitchingLabelCase}
+ */
+public class WavebandSwitchingLabelParser implements LabelParser, LabelSerializer {
+
+ public static final int CTYPE = 3;
+
+ private static final int WAVEB_F_LENGTH = 4;
+ private static final int START_F_LENGTH = 4;
+ private static final int END_F_LENGTH = 4;
+
+ private static final int CONTENT_LENGTH = WAVEB_F_LENGTH + START_F_LENGTH + END_F_LENGTH;
+
+ @Override
+ public final LabelType parseLabel(final ByteBuf buffer) throws RSVPParsingException {
+ Preconditions.checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
+ if (buffer.readableBytes() != CONTENT_LENGTH) {
+ throw new RSVPParsingException("Wrong length of array of bytes. Passed: " + buffer.readableBytes() + "; Expected: "
+ + CONTENT_LENGTH + ".");
+ }
+ final WavebandSwitchingLabelBuilder builder = new WavebandSwitchingLabelBuilder();
+ builder.setWavebandId(buffer.readUnsignedInt());
+ builder.setStartLabel(buffer.readUnsignedInt());
+ builder.setEndLabel(buffer.readUnsignedInt());
+ return new WavebandSwitchingLabelCaseBuilder().setWavebandSwitchingLabel(builder.build()).build();
+ }
+
+ @Override
+ public final 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();
+ final ByteBuf body = Unpooled.buffer(CONTENT_LENGTH);
+ Preconditions.checkArgument(obj.getWavebandId() != null, "WavebandId is mandatory.");
+ writeUnsignedInt(obj.getWavebandId(), body);
+ Preconditions.checkArgument(obj.getStartLabel() != null, "StartLabel is mandatory.");
+ writeUnsignedInt(obj.getStartLabel(), body);
+ Preconditions.checkArgument(obj.getEndLabel() != null, "EndLabel is mandatory.");
+ writeUnsignedInt(obj.getEndLabel(), body);
+ LabelUtil.formatLabel(CTYPE, unidirectional, global, body, buffer);
+ }
+}
--- /dev/null
+/*
+ * Copyright (c) 2015 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.rsvp.parser.impl;
+
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
+
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
+import org.junit.Test;
+import org.opendaylight.protocol.rsvp.parser.impl.subobject.Label.GeneralizedLabelParser;
+import org.opendaylight.protocol.rsvp.parser.impl.subobject.Label.Type1LabelParser;
+import org.opendaylight.protocol.rsvp.parser.impl.subobject.Label.WavebandSwitchingLabelParser;
+import org.opendaylight.protocol.rsvp.parser.spi.RSVPParsingException;
+import org.opendaylight.protocol.util.ByteArray;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.label.subobject.label.type.GeneralizedLabelCaseBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.label.subobject.label.type.Type1LabelCaseBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.label.subobject.label.type.WavebandSwitchingLabelCaseBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.label.subobject.label.type.generalized.label._case.GeneralizedLabelBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.label.subobject.label.type.type1.label._case.Type1LabelBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.label.subobject.label.type.waveband.switching.label._case.WavebandSwitchingLabelBuilder;
+
+public class LabelSubobjectParserTest {
+ private static final byte[] generalizedLabelBytes = {(byte) 0x80, 0x02, 0x00, 0x04, 0x12, 0x00, 0x25, (byte) 0xFF};
+
+ private static final byte[] typeOneLabelBytes = {(byte) 0x81, 0x01, 0x12, 0x00, 0x25, (byte) 0xFF};
+
+ private static final byte[] wavebandLabelBytes = {0x01, 0x03, 0x00, 0x00, 0x12, 0x34, 0x00, 0x00, (byte) 0x99, (byte) 0x99, 0x00,
+ 0x00, 0x11, 0x11};
+
+ @Test
+ public void testGeneralizedLabel() throws RSVPParsingException {
+ final GeneralizedLabelParser parser = new GeneralizedLabelParser();
+ final GeneralizedLabelBuilder iBuilder = new GeneralizedLabelBuilder();
+ 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))));
+ final ByteBuf buff = Unpooled.buffer();
+ parser.serializeLabel(true, false, builder.build(), buff);
+ assertArrayEquals(generalizedLabelBytes, ByteArray.getAllBytes(buff));
+
+ try {
+ parser.parseLabel(null);
+ fail();
+ } catch (final IllegalArgumentException e) {
+ assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
+ }
+
+ try {
+ parser.parseLabel(Unpooled.EMPTY_BUFFER);
+ fail();
+ } catch (final IllegalArgumentException e) {
+ assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
+ }
+ }
+
+ @Test
+ public void testWavebandLabel() throws RSVPParsingException {
+ final WavebandSwitchingLabelParser parser = new WavebandSwitchingLabelParser();
+ final WavebandSwitchingLabelBuilder iBuilder = new WavebandSwitchingLabelBuilder();
+ iBuilder.setWavebandId(0x1234L);
+ iBuilder.setStartLabel(0x9999L);
+ iBuilder.setEndLabel(0x1111L);
+ final WavebandSwitchingLabelCaseBuilder builder = new WavebandSwitchingLabelCaseBuilder().setWavebandSwitchingLabel(iBuilder.build());
+ assertEquals(builder.build(), parser.parseLabel(Unpooled.wrappedBuffer(ByteArray.cutBytes(wavebandLabelBytes, 2))));
+ final ByteBuf buff = Unpooled.buffer();
+ parser.serializeLabel(false, true, builder.build(), buff);
+ assertArrayEquals(wavebandLabelBytes, ByteArray.getAllBytes(buff));
+
+ try {
+ parser.parseLabel(null);
+ fail();
+ } catch (final IllegalArgumentException e) {
+ assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
+ }
+ try {
+ parser.parseLabel(Unpooled.EMPTY_BUFFER);
+ fail();
+ } catch (final IllegalArgumentException e) {
+ assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
+ }
+ }
+
+ @Test
+ public void testTypeOneLabel() throws RSVPParsingException {
+ final Type1LabelParser parser = new Type1LabelParser();
+ final Type1LabelBuilder iBuilder = new Type1LabelBuilder();
+ iBuilder.setType1Label(0x120025ffL);
+ final Type1LabelCaseBuilder builder = new Type1LabelCaseBuilder().setType1Label(iBuilder.build());
+ assertEquals(builder.build(), parser.parseLabel(Unpooled.wrappedBuffer(ByteArray.cutBytes(typeOneLabelBytes, 2))));
+ final ByteBuf buff = Unpooled.buffer();
+ parser.serializeLabel(true, true, builder.build(), buff);
+ assertArrayEquals(typeOneLabelBytes, ByteArray.getAllBytes(buff));
+
+ try {
+ parser.parseLabel(null);
+ fail();
+ } catch (final IllegalArgumentException e) {
+ assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
+ }
+ try {
+ parser.parseLabel(Unpooled.EMPTY_BUFFER);
+ fail();
+ } catch (final IllegalArgumentException e) {
+ assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
+ }
+ }
+}