Bug-479: Implementation of Vendor-Information TLV
[bgpcep.git] / pcep / ietf-stateful02 / src / main / java / org / opendaylight / protocol / pcep / ietf / stateful02 / Stateful02LspObjectParser.java
index 641d04a4230e9e9d2c374c01e60041fe6f8fbe60..8a40b77fbfb33ac57dffe7eac61ec588e022aede 100644 (file)
@@ -7,12 +7,18 @@
  */
 package org.opendaylight.protocol.pcep.ietf.stateful02;
 
-import java.util.BitSet;
+import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeMedium;
 
+import com.google.common.base.Preconditions;
+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.TlvHandlerRegistry;
+import org.opendaylight.protocol.pcep.spi.TlvRegistry;
+import org.opendaylight.protocol.pcep.spi.VendorInformationTlvRegistry;
 import org.opendaylight.protocol.util.ByteArray;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.PlspId;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.lsp.db.version.tlv.LspDbVersion;
@@ -25,141 +31,111 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.cra
 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.Tlv;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.vendor.information.tlvs.VendorInformationTlv;
 
 /**
  * Parser for {@link Lsp}
  */
 public class Stateful02LspObjectParser extends AbstractObjectWithTlvsParser<TlvsBuilder> {
 
-       public static final int CLASS = 32;
-
-       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
-        */
-       private static final int TLVS_OFFSET = 4;
-
-       /*
-        * 12b extended to 16b so first 4b are restricted (belongs to LSP ID)
-        */
-       private static final int DELEGATE_FLAG_OFFSET = 15;
-       private static final int SYNC_FLAG_OFFSET = 14;
-       private static final int REMOVE_FLAG_OFFSET = 12;
-       private static final int OPERATIONAL_FLAG_OFFSET = 13;
-
-       public Stateful02LspObjectParser(final TlvHandlerRegistry tlvReg) {
-               super(tlvReg);
-       }
-
-       @Override
-       public Lsp 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.");
-               }
-               final BitSet flags = ByteArray.bytesToBitSet(ByteArray.subByte(bytes, 2, 2));
-
-               final LspBuilder builder = new LspBuilder();
-               builder.setIgnore(header.isIgnore());
-               builder.setProcessingRule(header.isProcessingRule());
-
-               builder.setPlspId(new PlspId((ByteArray.bytesToLong(ByteArray.subByte(bytes, 0, 2)) & 0xFFFF) << 4 | (bytes[2] & 0xFF) >> 4));
-               builder.setDelegate(flags.get(DELEGATE_FLAG_OFFSET));
-               builder.setSync(flags.get(SYNC_FLAG_OFFSET));
-               builder.setRemove(flags.get(REMOVE_FLAG_OFFSET));
-               builder.setOperational(flags.get(OPERATIONAL_FLAG_OFFSET));
-               final TlvsBuilder b = new TlvsBuilder();
-               parseTlvs(b, ByteArray.cutBytes(bytes, TLVS_OFFSET));
-               builder.setTlvs(b.build());
-               return builder.build();
-       }
-
-       @Override
-       public void addTlv(final TlvsBuilder builder, final Tlv tlv) {
-               if (tlv instanceof RsvpErrorSpec) {
-                       builder.setRsvpErrorSpec((RsvpErrorSpec) tlv);
-               } else if (tlv instanceof SymbolicPathName) {
-                       builder.setSymbolicPathName((SymbolicPathName) tlv);
-               } else if (tlv instanceof LspDbVersion) {
-                       builder.setLspDbVersion((LspDbVersion) tlv);
-               }
-       }
-
-       @Override
-       public byte[] serializeObject(final Object object) {
-               if (!(object instanceof Lsp)) {
-                       throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed LspObject.");
-               }
-               final Lsp specObj = (Lsp) object;
-
-               final byte[] tlvs = serializeTlvs(specObj.getTlvs());
-               final byte[] retBytes = new byte[TLVS_OFFSET + tlvs.length + getPadding(TLVS_OFFSET + tlvs.length, PADDED_TO)];
-
-               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.isOperational() != null && specObj.isOperational()) {
-                       retBytes[3] |= 1 << (Byte.SIZE - (OPERATIONAL_FLAG_OFFSET - Byte.SIZE) - 1);
-               }
-               ByteArray.copyWhole(tlvs, retBytes, TLVS_OFFSET);
-               return ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), retBytes);
-       }
-
-       public byte[] serializeTlvs(final Tlvs tlvs) {
-               if (tlvs == null) {
-                       return new byte[0];
-               }
-               int finalLength = 0;
-               byte[] rsvpErrBytes = null;
-               byte[] symbBytes = null;
-               byte[] dbvBytes = null;
-               if (tlvs.getRsvpErrorSpec() != null) {
-                       rsvpErrBytes = serializeTlv(tlvs.getRsvpErrorSpec());
-                       finalLength += rsvpErrBytes.length;
-               }
-               if (tlvs.getSymbolicPathName() != null) {
-                       symbBytes = serializeTlv(tlvs.getSymbolicPathName());
-                       finalLength += symbBytes.length;
-               }
-               if (tlvs.getLspDbVersion() != null) {
-                       dbvBytes = serializeTlv(tlvs.getLspDbVersion());
-                       finalLength += dbvBytes.length;
-               }
-               int offset = 0;
-               final byte[] result = new byte[finalLength];
-               if (rsvpErrBytes != null) {
-                       ByteArray.copyWhole(rsvpErrBytes, result, offset);
-                       offset += rsvpErrBytes.length;
-               }
-               if (symbBytes != null) {
-                       ByteArray.copyWhole(symbBytes, result, offset);
-                       offset += symbBytes.length;
-               }
-               if (dbvBytes != null) {
-                       ByteArray.copyWhole(dbvBytes, result, offset);
-                       offset += dbvBytes.length;
-               }
-               return result;
-       }
-
-       @Override
-       public int getObjectType() {
-               return TYPE;
-       }
-
-       @Override
-       public int getObjectClass() {
-               return CLASS;
-       }
+    public static final int CLASS = 32;
+
+    public static final int TYPE = 1;
+
+    /*
+     * first 4b are restricted
+     */
+    private static final int DELEGATE_FLAG_OFFSET = 15;
+    private static final int SYNC_FLAG_OFFSET = 14;
+    private static final int OPERATIONAL_FLAG_OFFSET = 13;
+    private static final int REMOVE_FLAG_OFFSET = 12;
+
+    private static final int ONE_B_OFFSET = 12;
+    private static final int TWO_B_OFFSET = 4;
+
+    private static final int FLAGS_SIZE = 2;
+
+    public Stateful02LspObjectParser(final TlvRegistry tlvReg, final VendorInformationTlvRegistry viTlvReg) {
+        super(tlvReg, viTlvReg);
+    }
+
+    @Override
+    public Lsp parseObject(final ObjectHeader header, final ByteBuf bytes) throws PCEPDeserializerException {
+        Preconditions.checkArgument(bytes != null && bytes.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
+        final LspBuilder builder = new LspBuilder();
+        builder.setIgnore(header.isIgnore());
+        builder.setProcessingRule(header.isProcessingRule());
+        int[] plspIdRaw = { bytes.readUnsignedByte(), bytes.readUnsignedByte(), bytes.getUnsignedByte(2) };
+        builder.setPlspId(new PlspId((long) ((plspIdRaw[0] << ONE_B_OFFSET) | (plspIdRaw[1] << TWO_B_OFFSET) | (plspIdRaw[2] >> TWO_B_OFFSET))));
+        final BitSet flags = ByteArray.bytesToBitSet(ByteArray.readBytes(bytes, FLAGS_SIZE));
+        builder.setDelegate(flags.get(DELEGATE_FLAG_OFFSET));
+        builder.setSync(flags.get(SYNC_FLAG_OFFSET));
+        builder.setRemove(flags.get(REMOVE_FLAG_OFFSET));
+        builder.setOperational(flags.get(OPERATIONAL_FLAG_OFFSET));
+        final TlvsBuilder b = new TlvsBuilder();
+        parseTlvs(b, bytes.slice());
+        builder.setTlvs(b.build());
+        return builder.build();
+    }
+
+    @Override
+    public void addTlv(final TlvsBuilder builder, final Tlv tlv) {
+        if (tlv instanceof RsvpErrorSpec) {
+            builder.setRsvpErrorSpec((RsvpErrorSpec) tlv);
+        } else if (tlv instanceof SymbolicPathName) {
+            builder.setSymbolicPathName((SymbolicPathName) tlv);
+        } else if (tlv instanceof LspDbVersion) {
+            builder.setLspDbVersion((LspDbVersion) tlv);
+        }
+    }
+
+    @Override
+    public void serializeObject(final Object object, final ByteBuf buffer) {
+        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 PlspId plsp = specObj.getPlspId();
+        Preconditions.checkArgument(plsp != null, "PLSP-ID not present");
+        writeMedium(plsp.getValue().intValue() << TWO_B_OFFSET, body);
+
+        BitSet flags = new BitSet(2 * Byte.SIZE);
+        if (specObj.isDelegate() != null && specObj.isDelegate()) {
+            flags.set(DELEGATE_FLAG_OFFSET);
+        }
+        if (specObj.isRemove() != null && specObj.isRemove()) {
+            flags.set(REMOVE_FLAG_OFFSET);
+        }
+        if (specObj.isSync() != null && specObj.isSync()) {
+            flags.set(SYNC_FLAG_OFFSET);
+        }
+        if (specObj.isOperational() != null && specObj.isOperational()) {
+            flags.set(OPERATIONAL_FLAG_OFFSET);
+        }
+        body.writeByte(ByteArray.bitSetToBytes(flags, FLAGS_SIZE)[1]);
+        serializeTlvs(specObj.getTlvs(), body);
+        ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
+    }
+
+    public void serializeTlvs(final Tlvs tlvs, final ByteBuf body) {
+        if (tlvs == null) {
+            return;
+        }
+        if (tlvs.getRsvpErrorSpec() != null) {
+            serializeTlv(tlvs.getRsvpErrorSpec(), body);
+        }
+        if (tlvs.getSymbolicPathName() != null) {
+            serializeTlv(tlvs.getSymbolicPathName(), body);
+        }
+        if (tlvs.getLspDbVersion() != null) {
+            serializeTlv(tlvs.getLspDbVersion(), body);
+        }
+        serializeVendorInformationTlvs(tlvs.getVendorInformationTlv(), body);
+    }
+
+    @Override
+    protected final void addVendorInformationTlvs(final TlvsBuilder builder, final List<VendorInformationTlv> tlvs) {
+        if (!tlvs.isEmpty()) {
+            builder.setVendorInformationTlv(tlvs);
+        }
+    }
 }