Move pcep base parser Activator to its own bundle
[bgpcep.git] / pcep / ietf-stateful07 / src / main / java / org / opendaylight / protocol / pcep / ietf / stateful07 / Stateful07LspObjectParser.java
index 3db875c93c396131ad1bc15717fe39af31eea673..bdec990ddef0bb9c86bcaa8703ba8575fbae4bc8 100644 (file)
@@ -7,17 +7,18 @@
  */
 package org.opendaylight.protocol.pcep.ietf.stateful07;
 
+import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeMedium;
 import com.google.common.base.Preconditions;
+import com.google.common.primitives.UnsignedBytes;
 import io.netty.buffer.ByteBuf;
 import io.netty.buffer.Unpooled;
-import java.util.BitSet;
 import java.util.List;
 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.pcep.spi.VendorInformationTlvRegistry;
-import org.opendaylight.protocol.util.ByteArray;
+import org.opendaylight.protocol.util.BitArray;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.OperationalStatus;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.PlspId;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.lsp.error.code.tlv.LspErrorCode;
@@ -26,6 +27,7 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.iet
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.lsp.object.LspBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.lsp.object.lsp.Tlvs;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.lsp.object.lsp.TlvsBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.path.binding.tlv.PathBinding;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.rsvp.error.spec.tlv.RsvpErrorSpec;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.symbolic.path.name.tlv.SymbolicPathName;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
@@ -43,20 +45,17 @@ public class Stateful07LspObjectParser extends AbstractObjectWithTlvsParser<Tlvs
 
     public static final int TYPE = 1;
 
-    /*
-     * offset of TLVs offset of other fields are not defined as constants
-     * because of non-standard mapping of bits
-     */
-    protected static final int TLVS_OFFSET = 4;
-
     /*
      * 12b extended to 16b so first 4b are restricted (belongs to LSP ID)
      */
-    protected static final int DELEGATE_FLAG_OFFSET = 15;
-    protected static final int SYNC_FLAG_OFFSET = 14;
-    protected static final int REMOVE_FLAG_OFFSET = 13;
-    protected static final int ADMINISTRATIVE_FLAG_OFFSET = 12;
-    protected static final int OPERATIONAL_OFFSET = 9;
+    protected static final int DELEGATE = 11;
+    protected static final int SYNC = 10;
+    protected static final int REMOVE = 9;
+    protected static final int ADMINISTRATIVE = 8;
+    protected static final int OPERATIONAL = 5;
+
+    protected static final int FOUR_BITS_SHIFT = 4;
+    protected static final int FLAGS_SIZE = 12;
 
     public Stateful07LspObjectParser(final TlvRegistry tlvReg, final VendorInformationTlvRegistry viTlvReg) {
         super(tlvReg, viTlvReg);
@@ -68,24 +67,28 @@ public class Stateful07LspObjectParser extends AbstractObjectWithTlvsParser<Tlvs
         final LspBuilder builder = new LspBuilder();
         builder.setIgnore(header.isIgnore());
         builder.setProcessingRule(header.isProcessingRule());
-        int[] plspIdRaw = new int[] { bytes.readUnsignedByte(), bytes.readUnsignedByte(), bytes.getUnsignedByte(2), };
-        builder.setPlspId(new PlspId((long) ((plspIdRaw[0] << 12) | (plspIdRaw[1] << 4) | (plspIdRaw[2] >> 4))));
-        final BitSet flags = ByteArray.bytesToBitSet(ByteArray.readBytes(bytes, 2));
-        builder.setDelegate(flags.get(DELEGATE_FLAG_OFFSET));
-        builder.setSync(flags.get(SYNC_FLAG_OFFSET));
-        builder.setRemove(flags.get(REMOVE_FLAG_OFFSET));
-        builder.setAdministrative(flags.get(ADMINISTRATIVE_FLAG_OFFSET));
-        short s = 0;
-        s |= flags.get(OPERATIONAL_OFFSET + 2) ? 1 : 0;
-        s |= (flags.get(OPERATIONAL_OFFSET + 1) ? 1 : 0) << 1;
-        s |= (flags.get(OPERATIONAL_OFFSET) ? 1 : 0) << 2;
-        builder.setOperational(OperationalStatus.forValue(s));
+        final int[] plspIdRaw = new int[] { bytes.readUnsignedByte(), bytes.readUnsignedByte(), bytes.getUnsignedByte(2), };
+        builder.setPlspId(new PlspId((long) ((plspIdRaw[0] << FLAGS_SIZE) | (plspIdRaw[1] << FOUR_BITS_SHIFT) | (plspIdRaw[2] >> FOUR_BITS_SHIFT))));
+        parseFlags(builder, bytes);
         final TlvsBuilder b = new TlvsBuilder();
         parseTlvs(b, bytes.slice());
         builder.setTlvs(b.build());
         return builder.build();
     }
 
+    protected void parseFlags(final LspBuilder builder, final ByteBuf bytes) {
+        final BitArray flags = BitArray.valueOf(bytes, FLAGS_SIZE);
+        builder.setDelegate(flags.get(DELEGATE));
+        builder.setSync(flags.get(SYNC));
+        builder.setRemove(flags.get(REMOVE));
+        builder.setAdministrative(flags.get(ADMINISTRATIVE));
+        short s = 0;
+        s |= flags.get(OPERATIONAL + 2) ? 1 : 0;
+        s |= (flags.get(OPERATIONAL + 1) ? 1 : 0) << 1;
+        s |= (flags.get(OPERATIONAL) ? 1 : 0) << 2;
+        builder.setOperational(OperationalStatus.forValue(s));
+    }
+
     @Override
     public void addTlv(final TlvsBuilder builder, final Tlv tlv) {
         if (tlv instanceof LspErrorCode) {
@@ -98,6 +101,8 @@ public class Stateful07LspObjectParser extends AbstractObjectWithTlvsParser<Tlvs
             builder.setSymbolicPathName((SymbolicPathName) tlv);
         } else if (tlv instanceof VsTlv) {
             builder.setVsTlv((VsTlv) tlv);
+        } else if (tlv instanceof PathBinding) {
+            builder.setPathBinding((PathBinding) tlv);
         }
     }
 
@@ -106,35 +111,30 @@ public class Stateful07LspObjectParser extends AbstractObjectWithTlvsParser<Tlvs
         Preconditions.checkArgument(object instanceof Lsp, "Wrong instance of PCEPObject. Passed %s . Needed LspObject.", object.getClass());
         final Lsp specObj = (Lsp) object;
         final ByteBuf body = Unpooled.buffer();
-
-        final byte[] retBytes = new byte[4];
-
         Preconditions.checkArgument(specObj.getPlspId() != null, "PLSP-ID not present");
-        final int lspID = specObj.getPlspId().getValue().intValue();
-        retBytes[0] = (byte) (lspID >> 12);
-        retBytes[1] = (byte) (lspID >> 4);
-        retBytes[2] = (byte) (lspID << 4);
-        if (specObj.isDelegate() != null && specObj.isDelegate()) {
-            retBytes[3] |= 1 << (Byte.SIZE - (DELEGATE_FLAG_OFFSET - Byte.SIZE) - 1);
-        }
-        if (specObj.isRemove() != null && specObj.isRemove()) {
-            retBytes[3] |= 1 << (Byte.SIZE - (REMOVE_FLAG_OFFSET - Byte.SIZE) - 1);
-        }
-        if (specObj.isSync() != null && specObj.isSync()) {
-            retBytes[3] |= 1 << (Byte.SIZE - (SYNC_FLAG_OFFSET - Byte.SIZE) - 1);
-        }
-        if (specObj.isAdministrative() != null && specObj.isAdministrative()) {
-            retBytes[3] |= 1 << (Byte.SIZE - (ADMINISTRATIVE_FLAG_OFFSET - Byte.SIZE) - 1);
-        }
+        writeMedium(specObj.getPlspId().getValue().intValue() << FOUR_BITS_SHIFT, body);
+        final BitArray flags = serializeFlags(specObj);
+        byte op = 0;
         if (specObj.getOperational() != null) {
-            final int op = specObj.getOperational().getIntValue();
-            retBytes[3] |= (op & 7) << 4;
+            op = UnsignedBytes.checkedCast(specObj.getOperational().getIntValue());
+            op = (byte) (op << FOUR_BITS_SHIFT);
         }
-        body.writeBytes(retBytes);
+        final byte[] res = flags.array();
+        res[res.length -1] = (byte) (res[res.length -1] | op);
+        body.writeByte(res[res.length -1]);
         serializeTlvs(specObj.getTlvs(), body);
         ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
     }
 
+    protected BitArray serializeFlags(final Lsp specObj) {
+        final BitArray flags = new BitArray(FLAGS_SIZE);
+        flags.set(DELEGATE, specObj.isDelegate());
+        flags.set(REMOVE, specObj.isRemove());
+        flags.set(SYNC, specObj.isSync());
+        flags.set(ADMINISTRATIVE, specObj.isAdministrative());
+        return flags;
+    }
+
     public void serializeTlvs(final Tlvs tlvs, final ByteBuf body) {
         if (tlvs == null) {
             return;
@@ -155,6 +155,9 @@ public class Stateful07LspObjectParser extends AbstractObjectWithTlvsParser<Tlvs
             serializeTlv(tlvs.getVsTlv(), body);
         }
         serializeVendorInformationTlvs(tlvs.getVendorInformationTlv(), body);
+        if (tlvs.getPathBinding() != null) {
+            serializeTlv(tlvs.getPathBinding(), body);
+        }
     }
 
     @Override