import java.util.BitSet;
import java.util.List;
-import org.opendaylight.protocol.pcep.PCEPDeserializerException;
-import org.opendaylight.protocol.pcep.PCEPDocumentedException;
-import org.opendaylight.protocol.pcep.spi.AbstractObjectParser;
-import org.opendaylight.protocol.pcep.spi.SubobjectHandlerRegistry;
-import org.opendaylight.protocol.pcep.spi.TlvHandlerRegistry;
+import org.opendaylight.protocol.pcep.spi.AbstractObjectWithTlvsParser;
+import org.opendaylight.protocol.pcep.spi.ObjectUtil;
+import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
+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.types.rev131005.Object;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
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.SvecObject;
-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.pcreq.message.pcreq.message.SvecBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.svec.object.Svec;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.svec.object.SvecBuilder;
import com.google.common.collect.Lists;
/**
- * Parser for {@link SvecObject}
+ * Parser for {@link Svec}
*/
-public class PCEPSvecObjectParser extends AbstractObjectParser<SvecBuilder> {
+public class PCEPSvecObjectParser extends AbstractObjectWithTlvsParser<SvecBuilder> {
public static final int CLASS = 11;
/*
* field lengths in bytes
*/
- public static final int FLAGS_F_LENGTH = 3;
- public static final int REQ_LIST_ITEM_LENGTH = 4;
+ private static final int FLAGS_F_LENGTH = 3;
+ private static final int REQ_LIST_ITEM_LENGTH = 4;
/*
* fields offsets in bytes
*/
- public static final int FLAGS_F_OFFSET = 1; // aded reserved field of size 1
- public static final int REQ_ID_LIST_OFFSET = FLAGS_F_OFFSET + FLAGS_F_LENGTH;
+ private static final int FLAGS_F_OFFSET = 1;
+ private static final int REQ_ID_LIST_OFFSET = FLAGS_F_OFFSET + FLAGS_F_LENGTH;
/*
* flags offsets inside flags field in bits
*/
- public static final int S_FLAG_OFFSET = 21;
- public static final int N_FLAG_OFFSET = 22;
- public static final int L_FLAG_OFFSET = 23;
+ private static final int S_FLAG_OFFSET = 21;
+ private static final int N_FLAG_OFFSET = 22;
+ private static final int L_FLAG_OFFSET = 23;
/*
* min size in bytes
*/
- public static final int MIN_SIZE = FLAGS_F_LENGTH + FLAGS_F_OFFSET;
+ private static final int MIN_SIZE = FLAGS_F_LENGTH + FLAGS_F_OFFSET;
- public PCEPSvecObjectParser(final SubobjectHandlerRegistry subobjReg, final TlvHandlerRegistry tlvReg) {
- super(subobjReg, tlvReg);
+ public PCEPSvecObjectParser(final TlvRegistry tlvReg) {
+ super(tlvReg);
}
@Override
- public SvecObject parseObject(final ObjectHeader header, final byte[] bytes) throws PCEPDeserializerException, PCEPDocumentedException {
+ public Svec parseObject(final ObjectHeader header, final byte[] bytes) throws PCEPDeserializerException {
if (bytes == null || bytes.length == 0) {
throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty.");
}
-
if (bytes.length < MIN_SIZE) {
throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + bytes.length + "; Expected: >=" + MIN_SIZE
+ ".");
}
-
final BitSet flags = ByteArray.bytesToBitSet(ByteArray.subByte(bytes, FLAGS_F_OFFSET, FLAGS_F_LENGTH));
final List<RequestId> requestIDs = Lists.newArrayList();
for (int i = REQ_ID_LIST_OFFSET; i < bytes.length; i += REQ_LIST_ITEM_LENGTH) {
requestIDs.add(new RequestId(ByteArray.bytesToLong(ByteArray.subByte(bytes, i, REQ_LIST_ITEM_LENGTH))));
}
-
if (requestIDs.isEmpty()) {
throw new PCEPDeserializerException("Empty Svec Object - no request ids.");
}
-
final SvecBuilder builder = new SvecBuilder();
builder.setIgnore(header.isIgnore());
return builder.build();
}
- @Override
- public void addTlv(final SvecBuilder builder, final Tlv tlv) {
- // No tlvs defined
- }
-
@Override
public byte[] serializeObject(final Object object) {
- if (!(object instanceof SvecObject)) {
+ if (!(object instanceof Svec)) {
throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed SvecObject.");
}
- final SvecObject svecObj = (SvecObject) object;
+ final Svec svecObj = (Svec) object;
final byte[] retBytes = new byte[svecObj.getRequestsIds().size() * REQ_LIST_ITEM_LENGTH + REQ_ID_LIST_OFFSET];
final List<RequestId> requestIDs = svecObj.getRequestsIds();
final BitSet flags = new BitSet(FLAGS_F_LENGTH * Byte.SIZE);
ByteArray.copyWhole(ByteArray.bitSetToBytes(flags, FLAGS_F_LENGTH), retBytes, FLAGS_F_OFFSET);
for (int i = 0; i < requestIDs.size(); i++) {
- System.arraycopy(ByteArray.longToBytes(requestIDs.get(i).getValue()), 4, retBytes, REQ_LIST_ITEM_LENGTH * i
- + REQ_ID_LIST_OFFSET, REQ_LIST_ITEM_LENGTH);
+ System.arraycopy(ByteArray.longToBytes(requestIDs.get(i).getValue(), REQ_LIST_ITEM_LENGTH), 0, retBytes, REQ_LIST_ITEM_LENGTH
+ * i + REQ_ID_LIST_OFFSET, REQ_LIST_ITEM_LENGTH);
}
-
assert !(requestIDs.isEmpty()) : "Empty Svec Object - no request ids.";
-
- return retBytes;
- }
-
- @Override
- public int getObjectType() {
- return TYPE;
- }
-
- @Override
- public int getObjectClass() {
- return CLASS;
+ return ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), retBytes);
}
}