*/
package org.opendaylight.protocol.pcep.impl.subobject;
-import java.util.Arrays;
+import io.netty.buffer.ByteBuf;
+
import java.util.BitSet;
-import org.opendaylight.protocol.pcep.PCEPDeserializerException;
-import org.opendaylight.protocol.pcep.impl.Util.BiParsersMap;
-import org.opendaylight.protocol.pcep.subobject.EROGeneralizedLabelSubobject;
-import org.opendaylight.protocol.pcep.subobject.EROLabelSubobject;
-import org.opendaylight.protocol.pcep.subobject.EROType1LabelSubobject;
-import org.opendaylight.protocol.pcep.subobject.EROWavebandSwitchingLabelSubobject;
+import org.opendaylight.protocol.pcep.impl.object.EROSubobjectUtil;
+import org.opendaylight.protocol.pcep.spi.EROSubobjectParser;
+import org.opendaylight.protocol.pcep.spi.EROSubobjectSerializer;
+import org.opendaylight.protocol.pcep.spi.LabelRegistry;
+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.pcep.types.rev131005.explicit.route.object.ero.Subobject;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.SubobjectBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.LabelCase;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.LabelCaseBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.label._case.Label;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.label._case.LabelBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.label.subobject.LabelType;
-public class EROLabelSubobjectParser {
+import com.google.common.base.Preconditions;
+import com.google.common.primitives.UnsignedBytes;
- public static final int RES_F_LENGTH = 1;
+public class EROLabelSubobjectParser implements EROSubobjectParser, EROSubobjectSerializer {
- public static final int C_TYPE_F_LENGTH = 1;
+ public static final int TYPE = 3;
- public static final int RES_F_OFFSET = 0;
+ private static final int RES_F_LENGTH = 1;
- public static final int C_TYPE_F_OFFSET = RES_F_OFFSET + RES_F_LENGTH;
+ private static final int C_TYPE_F_LENGTH = 1;
- public static final int HEADER_LENGTH = C_TYPE_F_OFFSET + C_TYPE_F_LENGTH;
+ private static final int RES_F_OFFSET = 0;
- public static final int U_FLAG_OFFSET = 0;
+ private static final int C_TYPE_F_OFFSET = RES_F_OFFSET + RES_F_LENGTH;
- private static class MapOfParsers extends BiParsersMap<Class<? extends EROLabelSubobject>, Integer, EROLabelParser> {
- private final static MapOfParsers instance = new MapOfParsers();
+ private static final int HEADER_LENGTH = C_TYPE_F_OFFSET + C_TYPE_F_LENGTH;
- private MapOfParsers() {
- this.fillInMap();
- }
+ private static final int U_FLAG_OFFSET = 0;
- private void fillInMap() {
- this.put(EROType1LabelSubobject.class, 1, new EROType1LabelSubobjectParser());
- this.put(EROGeneralizedLabelSubobject.class, 2, new EROGeneralizedLabelSubobjectParser());
- this.put(EROWavebandSwitchingLabelSubobject.class, 3, new EROWavebandSwitchingLabelSubobjectParser());
- }
+ private final LabelRegistry registry;
- public static MapOfParsers getInstance() {
- return instance;
+ public EROLabelSubobjectParser(final LabelRegistry labelReg) {
+ this.registry = Preconditions.checkNotNull(labelReg);
}
- }
-
- public static EROLabelSubobject parse(byte[] soContentsBytes, boolean loose) throws PCEPDeserializerException {
- if (soContentsBytes == null || soContentsBytes.length == 0)
- throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty.");
- if (soContentsBytes.length < HEADER_LENGTH)
- throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + soContentsBytes.length + "; Expected: >" + HEADER_LENGTH + ".");
- final BitSet reserved = ByteArray.bytesToBitSet(Arrays.copyOfRange(soContentsBytes, RES_F_OFFSET, RES_F_LENGTH));
-
- final int c_type = soContentsBytes[C_TYPE_F_OFFSET] & 0xFF;
-
- final EROLabelParser parser = MapOfParsers.getInstance().getValueFromKeyValue(c_type);
-
- if (parser == null) {
- throw new PCEPDeserializerException("Unknown C-TYPE for ero label subobject. Passed: " + c_type);
+ @Override
+ public Subobject parseSubobject(final ByteBuf buffer, final boolean loose) throws PCEPDeserializerException {
+ Preconditions.checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
+ if (buffer.readableBytes() < HEADER_LENGTH) {
+ throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.readableBytes() + "; Expected: >"
+ + HEADER_LENGTH + ".");
+ }
+ final BitSet reserved = ByteArray.bytesToBitSet(ByteArray.readBytes(buffer, RES_F_LENGTH));
+ final short cType = (short) UnsignedBytes.toInt(buffer.readByte());
+
+ //FIXME: switch to ByteBuf
+ final LabelType labelType = this.registry.parseLabel(cType, ByteArray.readAllBytes(buffer));
+ if (labelType == null) {
+ throw new PCEPDeserializerException("Unknown C-TYPE for ero label subobject. Passed: " + cType);
+ }
+ final LabelBuilder builder = new LabelBuilder();
+ builder.setUniDirectional(reserved.get(U_FLAG_OFFSET));
+ builder.setLabelType(labelType);
+ return new SubobjectBuilder().setLoose(loose).setSubobjectType(new LabelCaseBuilder().setLabel(builder.build()).build()).build();
}
- return parser.parse(ByteArray.cutBytes(soContentsBytes, HEADER_LENGTH), reserved.get(U_FLAG_OFFSET), loose);
- }
-
- public static byte[] put(EROLabelSubobject objToSerialize) {
- final Integer c_type = MapOfParsers.getInstance().getKeyValueFromKey(objToSerialize.getClass());
- final EROLabelParser parser = MapOfParsers.getInstance().getValueFromKeyValue(c_type);
-
- if (c_type == null || parser == null)
- throw new IllegalArgumentException("Unknown EROLabelSubobject instance. Passed " + objToSerialize.getClass());
-
- final byte[] labelbytes = parser.put(objToSerialize);
-
- final byte[] retBytes = new byte[labelbytes.length + HEADER_LENGTH];
-
- System.arraycopy(labelbytes, 0, retBytes, HEADER_LENGTH, labelbytes.length);
-
- final BitSet reserved = new BitSet();
- reserved.set(U_FLAG_OFFSET, objToSerialize.isUpStream());
- System.arraycopy(ByteArray.bitSetToBytes(reserved, RES_F_LENGTH), 0, retBytes, RES_F_OFFSET, RES_F_LENGTH);
-
- retBytes[C_TYPE_F_OFFSET] = (byte) c_type.intValue();
-
- return retBytes;
- }
+ @Override
+ public byte[] serializeSubobject(final Subobject subobject) {
+ 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());
+ }
+ return EROSubobjectUtil.formatSubobject(TYPE, subobject.isLoose(), labelbytes);
+ }
}