package org.opendaylight.protocol.pcep.impl.object;
-import java.util.List;
-
-import org.opendaylight.protocol.pcep.PCEPDeserializerException;
-import org.opendaylight.protocol.pcep.PCEPDocumentedException;
-import org.opendaylight.protocol.pcep.PCEPErrors;
-import org.opendaylight.protocol.pcep.PCEPObject;
-import org.opendaylight.protocol.pcep.PCEPTlv;
-import org.opendaylight.protocol.pcep.impl.PCEPObjectParser;
-import org.opendaylight.protocol.pcep.impl.PCEPTlvParser;
-import org.opendaylight.protocol.pcep.impl.Util;
-import org.opendaylight.protocol.pcep.object.PCEPOpenObject;
-import org.opendaylight.protocol.pcep.tlv.OFListTlv;
+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.PCEPErrors;
+import org.opendaylight.protocol.pcep.spi.TlvRegistry;
+import org.opendaylight.protocol.pcep.spi.UnknownObject;
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.ProtocolVersion;
+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.of.list.tlv.OfList;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.Open;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.OpenBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.open.Tlvs;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.open.TlvsBuilder;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
import com.google.common.primitives.UnsignedBytes;
/**
- * Parser for {@link org.opendaylight.protocol.pcep.object.PCEPOpenObject PCEPOpenObject}
+ * Parser for {@link Open}
*/
+public class PCEPOpenObjectParser extends AbstractObjectWithTlvsParser<TlvsBuilder> {
+ private static final Logger LOG = LoggerFactory.getLogger(PCEPOpenObjectParser.class);
+
+ public static final int CLASS = 1;
-public class PCEPOpenObjectParser implements PCEPObjectParser {
+ public static final int TYPE = 1;
/*
* lengths of fields in bytes
*/
- public static final int VER_FLAGS_MF_LENGTH = 1; // multi-field
- public static final int KEEPALIVE_F_LENGTH = 1;
- public static final int DEAD_TIMER_LENGTH = 1;
- public static final int SID_F_LENGTH = 1;
+ private static final int VER_FLAGS_MF_LENGTH = 1;
+ private static final int KEEPALIVE_F_LENGTH = 1;
+ private static final int DEAD_TIMER_LENGTH = 1;
+ private static final int SID_F_LENGTH = 1;
/*
* lengths of subfields inside multi-field in bits
*/
- public static final int VERSION_SF_LENGTH = 3;
- public static final int FLAGS_SF_LENGTH = 5;
+ private static final int VERSION_SF_LENGTH = 3;
/*
* offsets of field in bytes
*/
-
- public static final int VER_FLAGS_MF_OFFSET = 0;
- public static final int KEEPALIVE_F_OFFSET = VER_FLAGS_MF_OFFSET + VER_FLAGS_MF_LENGTH;
- public static final int DEAD_TIMER_OFFSET = KEEPALIVE_F_OFFSET + KEEPALIVE_F_LENGTH;
- public static final int SID_F_OFFSET = DEAD_TIMER_OFFSET + DEAD_TIMER_LENGTH;
- public static final int TLVS_OFFSET = SID_F_OFFSET + SID_F_LENGTH;
+ private static final int VER_FLAGS_MF_OFFSET = 0;
+ private static final int KEEPALIVE_F_OFFSET = VER_FLAGS_MF_OFFSET + VER_FLAGS_MF_LENGTH;
+ private static final int DEAD_TIMER_OFFSET = KEEPALIVE_F_OFFSET + KEEPALIVE_F_LENGTH;
+ private static final int SID_F_OFFSET = DEAD_TIMER_OFFSET + DEAD_TIMER_LENGTH;
+ private static final int TLVS_OFFSET = SID_F_OFFSET + SID_F_LENGTH;
/*
* offsets of subfields inside multi-field in bits
*/
+ private static final int VERSION_SF_OFFSET = 0;
- public static final int VERSION_SF_OFFSET = 0;
- public static final int FLAGS_SF_OFFSET = VERSION_SF_LENGTH + VERSION_SF_OFFSET;
+ private static final int PCEP_VERSION = 1;
- public static final int PADDED_TO = 4;
+ public PCEPOpenObjectParser(final TlvRegistry tlvReg) {
+ super(tlvReg);
+ }
@Override
- public PCEPOpenObject parse(byte[] bytes, boolean processed, boolean ignored) throws PCEPDeserializerException, PCEPDocumentedException {
- if (bytes == null || bytes.length == 0)
+ public Object 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 < TLVS_OFFSET)
- throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + bytes.length + "; Expected: >=" + TLVS_OFFSET + ".");
-
- // parse version
+ }
final int versionValue = ByteArray.copyBitsRange(bytes[VER_FLAGS_MF_OFFSET], VERSION_SF_OFFSET, VERSION_SF_LENGTH);
- if (versionValue != PCEPOpenObject.PCEP_VERSION)
- throw new PCEPDocumentedException("Unsupported PCEP version " + versionValue, PCEPErrors.PCEP_VERSION_NOT_SUPPORTED);
-
- final List<PCEPTlv> tlvs = PCEPTlvParser.parse(ByteArray.cutBytes(bytes, TLVS_OFFSET));
- boolean ofListOccure = false;
+ final OpenBuilder builder = new OpenBuilder();
+ builder.setVersion(new ProtocolVersion((short) versionValue));
+ builder.setProcessingRule(header.isProcessingRule());
+ builder.setIgnore(header.isIgnore());
+ builder.setDeadTimer((short) UnsignedBytes.toInt(bytes[DEAD_TIMER_OFFSET]));
+ builder.setKeepalive((short) UnsignedBytes.toInt(bytes[KEEPALIVE_F_OFFSET]));
+ builder.setSessionId((short) UnsignedBytes.toInt(bytes[SID_F_OFFSET]));
+
+ final TlvsBuilder tbuilder = new TlvsBuilder();
+ parseTlvs(tbuilder, ByteArray.cutBytes(bytes, TLVS_OFFSET));
+ builder.setTlvs(tbuilder.build());
+
+ final Open obj = builder.build();
+ if (versionValue != PCEP_VERSION) {
+ // TODO: Should we move this check into the negotiator
+ LOG.debug("Unsupported PCEP version {}", versionValue);
+ return new UnknownObject(PCEPErrors.PCEP_VERSION_NOT_SUPPORTED, obj);
+ }
- for (final PCEPTlv tlv : tlvs) {
- if (tlv instanceof OFListTlv) {
- if (ofListOccure)
- throw new PCEPDocumentedException("Invalid or unexpected message", PCEPErrors.NON_OR_INVALID_OPEN_MSG);
+ return obj;
+ }
- ofListOccure = true;
- }
+ @Override
+ public void addTlv(final TlvsBuilder tbuilder, final Tlv tlv) {
+ if (tlv instanceof OfList) {
+ tbuilder.setOfList((OfList) tlv);
}
-
- return new PCEPOpenObject(UnsignedBytes.toInt(bytes[KEEPALIVE_F_OFFSET]), UnsignedBytes.toInt(bytes[DEAD_TIMER_OFFSET]),
- UnsignedBytes.toInt(bytes[SID_F_OFFSET]), tlvs);
}
@Override
- public byte[] put(PCEPObject obj) {
- if (!(obj instanceof PCEPOpenObject))
- throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + obj.getClass() + ". Needed PCEPOpenObject.");
+ public byte[] serializeObject(final Object object) {
+ if (!(object instanceof Open)) {
+ throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed OpenObject.");
+ }
+ final Open open = (Open) object;
- final PCEPOpenObject openObj = (PCEPOpenObject) obj;
+ final byte versionFlagMF = (byte) (PCEP_VERSION << (Byte.SIZE - VERSION_SF_LENGTH));
- final byte versionFlagMF = (byte) (PCEPOpenObject.PCEP_VERSION << (Byte.SIZE - VERSION_SF_LENGTH));
+ final byte[] tlvs = serializeTlvs(open.getTlvs());
- final byte[] tlvs = PCEPTlvParser.put(openObj.getTlvs());
- final byte[] bytes = new byte[TLVS_OFFSET + tlvs.length + Util.getPadding(TLVS_OFFSET + tlvs.length, PADDED_TO)];
+ final byte[] bytes = new byte[TLVS_OFFSET + tlvs.length + getPadding(TLVS_OFFSET + tlvs.length, PADDED_TO)];
- // serialize version_flags multi-field
bytes[VER_FLAGS_MF_OFFSET] = versionFlagMF;
+ bytes[KEEPALIVE_F_OFFSET] = UnsignedBytes.checkedCast(open.getKeepalive());
+ bytes[DEAD_TIMER_OFFSET] = UnsignedBytes.checkedCast(open.getDeadTimer());
+ bytes[SID_F_OFFSET] = UnsignedBytes.checkedCast(open.getSessionId());
+ if (tlvs.length != 0) {
+ ByteArray.copyWhole(tlvs, bytes, TLVS_OFFSET);
+ }
+ return ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), bytes);
+ }
- // serialize keepalive
- bytes[KEEPALIVE_F_OFFSET] = (byte) openObj.getKeepAliveTimerValue();
-
- // serialize dead timer
- bytes[DEAD_TIMER_OFFSET] = (byte) openObj.getDeadTimerValue();
-
- // serialize SID
- bytes[SID_F_OFFSET] = (byte) openObj.getSessionId();
+ public byte[] serializeTlvs(final Tlvs tlvs) {
+ if (tlvs == null) {
+ return new byte[0];
+ }
+ byte[] ofListBytes = null;
+ if (tlvs.getOfList() != null) {
+ ofListBytes = serializeTlv(tlvs.getOfList());
+ }
+ byte[] result = new byte[0];
+ if (ofListBytes != null) {
+ result = new byte[ofListBytes.length];
+ ByteArray.copyWhole(ofListBytes, result, 0);
+ }
+ return result;
+ }
- // serialize tlvs
- ByteArray.copyWhole(tlvs, bytes, TLVS_OFFSET);
+ @Override
+ public final int getObjectType() {
+ return TYPE;
+ }
- return bytes;
+ @Override
+ public final int getObjectClass() {
+ return CLASS;
}
}