*/
package org.opendaylight.protocol.pcep.impl.object;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ExcludeRouteObject;
+import java.util.Arrays;
+import java.util.BitSet;
+import java.util.List;
+
+import org.opendaylight.protocol.util.ByteArray;
+import org.opendaylight.protocol.pcep.PCEPDeserializerException;
+import org.opendaylight.protocol.pcep.PCEPDocumentedException;
+import org.opendaylight.protocol.pcep.PCEPObject;
+import org.opendaylight.protocol.pcep.impl.PCEPObjectParser;
+import org.opendaylight.protocol.pcep.impl.PCEPXROSubobjectParser;
+import org.opendaylight.protocol.pcep.object.PCEPExcludeRouteObject;
+import org.opendaylight.protocol.pcep.subobject.ExcludeRouteSubobject;
/**
- * Parser for {@link ExcludeRouteObject}
+ * Parser for {@link org.opendaylight.protocol.pcep.object.PCEPExcludeRouteObject
+ * PCEPExcludeRouteObject}
*/
-// FIXME: fix model, this object is not used in a message
-public final class PCEPExcludeRouteObjectParser { // extends AbstractObjectParser<ExcludeRouterBuilder> {
+public class PCEPExcludeRouteObjectParser implements PCEPObjectParser {
+
+ /*
+ * lengths of fields in bytes
+ */
+ public final int FLAGS_F_LENGTH = 2;
+
+ /*
+ * offsets of fields in bytes
+ */
+ public final int FLAGS_F_OFFSET = 2; // added reserved 2 bytes
+ public final int SO_F_OFFSET = this.FLAGS_F_OFFSET + this.FLAGS_F_LENGTH;
+
+ /*
+ * Flag offsets inside flags field in bits
+ */
+ public final int F_FLAG_OFFSET = 15;
+
+ @Override
+ public PCEPObject parse(byte[] bytes, boolean processed, boolean ignored) throws PCEPDeserializerException, PCEPDocumentedException {
+ if (bytes == null || bytes.length == 0)
+ throw new IllegalArgumentException("Byte array is mandatory. Can't be null or empty.");
+
+ final BitSet flags = ByteArray.bytesToBitSet(Arrays.copyOfRange(bytes, this.FLAGS_F_OFFSET, this.FLAGS_F_LENGTH));
+
+ final List<ExcludeRouteSubobject> subobjects = PCEPXROSubobjectParser.parse(ByteArray.cutBytes(bytes, this.SO_F_OFFSET));
+ if (subobjects.isEmpty())
+ throw new PCEPDeserializerException("Empty Exclude Route Object.");
+
+ return new PCEPExcludeRouteObject(subobjects, flags.get(this.F_FLAG_OFFSET), processed, ignored);
+ }
+
+ @Override
+ public byte[] put(PCEPObject obj) {
+ if (!(obj instanceof PCEPExcludeRouteObject))
+ throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + obj.getClass() + ". Needed PCEPExcludeRouteObject.");
+
+ assert !(((PCEPExcludeRouteObject) obj).getSubobjects().isEmpty()) : "Empty Exclude Route Object.";
- public static final int CLASS = 7; // FIXME: to actual value
+ final byte[] subObjsBytes = PCEPXROSubobjectParser.put(((PCEPExcludeRouteObject) obj).getSubobjects());
+ final byte[] retBytes = new byte[this.SO_F_OFFSET + subObjsBytes.length];
+ final BitSet flags = new BitSet(this.FLAGS_F_LENGTH * Byte.SIZE);
+ flags.set(this.F_FLAG_OFFSET, ((PCEPExcludeRouteObject) obj).isFail());
+ ByteArray.copyWhole(ByteArray.bitSetToBytes(flags, this.FLAGS_F_LENGTH), retBytes, this.FLAGS_F_OFFSET);
+ ByteArray.copyWhole(subObjsBytes, retBytes, this.SO_F_OFFSET);
- public static final int TYPE = 1;
+ return retBytes;
+ }
- // public PCEPExcludeRouteObjectParser(final HandlerRegistry registry) {
- // super(registry);
- // }
- //
- // @Override
- // public ExcludeRouteObject parseObject(final ObjectHeader header, final byte[] bytes) throws
- // PCEPDeserializerException,
- // PCEPDocumentedException {
- // if (bytes == null || bytes.length == 0)
- // throw new IllegalArgumentException("Byte array is mandatory. Can't be null or empty.");
- //
- // final ExcludeRouterBuilder builder = new ExcludeRouterBuilder();
- //
- // builder.setIgnore(header.isIgnore());
- // builder.setProcessingRule(header.isProcessingRule());
- // // FIXME: add subobjects
- // return builder.build();
- // }
- //
- // @Override
- // public void addTlv(final ExcludeRouterBuilder builder, final Tlv tlv) {
- // // No tlvs defined
- // }
- //
- // @Override
- // public byte[] serializeObject(final Object object) {
- // if (!(object instanceof ExcludeRouteObject))
- // throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() +
- // ". Needed ExcludeRouteObject.");
- //
- // assert !(((ExcludeRouteObject) object).getSubobjects().isEmpty()) : "Empty Excluded Route Object.";
- //
- // // return PCEPEROSubobjectParser.put(((ExplicitRouteObject) obj).getSubobjects());
- //
- // // FIXME: add subobjects
- // return null;
- // }
- //
- // @Override
- // public int getObjectType() {
- // return TYPE;
- // }
- //
- // @Override
- // public int getObjectClass() {
- // return CLASS;
- // }
}