Bug-479: Implementation of Vendor-Information TLV
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / object / PCEPGlobalConstraintsObjectParser.java
index f046722db314af9c2a2d46418ce23bdb6bfead4b..9ddf03a68d8c120b1a5b4497e035c75994e515e6 100644 (file)
@@ -7,80 +7,81 @@
  */
 package org.opendaylight.protocol.pcep.impl.object;
 
+import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedByte;
+
+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.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.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.gc.object.Gc;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.gc.object.GcBuilder;
-
-import com.google.common.primitives.UnsignedBytes;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.gc.object.gc.Tlvs;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.gc.object.gc.TlvsBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.vendor.information.tlvs.VendorInformationTlv;
 
 /**
  * Parser for {@link Gc}
  */
-public class PCEPGlobalConstraintsObjectParser extends AbstractObjectWithTlvsParser<GcBuilder> {
-
-       public static final int CLASS = 24;
-
-       public static final int TYPE = 1;
-
-       private static final int MAX_HOP_F_LENGTH = 1;
-       private static final int MAX_UTIL_F_LENGTH = 1;
-       private static final int MIN_UTIL_F_LENGTH = 1;
-       private static final int OVER_BOOKING_FACTOR_F_LENGTH = 1;
+public class PCEPGlobalConstraintsObjectParser extends AbstractObjectWithTlvsParser<TlvsBuilder> {
 
-       private static final int MAX_HOP_F_OFFSET = 0;
-       private static final int MAX_UTIL_F_OFFSET = MAX_HOP_F_OFFSET + MAX_HOP_F_LENGTH;
-       private static final int MIN_UTIL_F_OFFSET = MAX_UTIL_F_OFFSET + MAX_UTIL_F_LENGTH;
-       private static final int OVER_BOOKING_FACTOR_F_OFFSET = MIN_UTIL_F_OFFSET + MIN_UTIL_F_LENGTH;
+    public static final int CLASS = 24;
 
-       private static final int TLVS_OFFSET = OVER_BOOKING_FACTOR_F_OFFSET + OVER_BOOKING_FACTOR_F_LENGTH;
+    public static final int TYPE = 1;
 
-       public PCEPGlobalConstraintsObjectParser(final TlvRegistry tlvReg) {
-               super(tlvReg);
-       }
+    public PCEPGlobalConstraintsObjectParser(final TlvRegistry tlvReg, final VendorInformationTlvRegistry viTlvReg) {
+        super(tlvReg, viTlvReg);
+    }
 
-       @Override
-       public Gc 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 GcBuilder builder = new GcBuilder();
+    @Override
+    public Gc 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 GcBuilder builder = new GcBuilder();
 
-               builder.setIgnore(header.isIgnore());
-               builder.setProcessingRule(header.isProcessingRule());
+        builder.setIgnore(header.isIgnore());
+        builder.setProcessingRule(header.isProcessingRule());
 
-               builder.setMaxHop((short) UnsignedBytes.toInt(bytes[MAX_HOP_F_OFFSET]));
-               builder.setMinUtilization((short) UnsignedBytes.toInt(bytes[MIN_UTIL_F_OFFSET]));
-               builder.setMaxUtilization((short) UnsignedBytes.toInt(bytes[MAX_UTIL_F_OFFSET]));
-               builder.setOverBookingFactor((short) UnsignedBytes.toInt(bytes[OVER_BOOKING_FACTOR_F_OFFSET]));
-               return builder.build();
-       }
+        builder.setMaxHop((short) UnsignedBytes.toInt(bytes.readByte()));
+        builder.setMaxUtilization((short) UnsignedBytes.toInt(bytes.readByte()));
+        builder.setMinUtilization((short) UnsignedBytes.toInt(bytes.readByte()));
+        builder.setOverBookingFactor((short) UnsignedBytes.toInt(bytes.readByte()));
+        final TlvsBuilder tlvsBuilder = new TlvsBuilder();
+        parseTlvs(tlvsBuilder, bytes.slice());
+        builder.setTlvs(tlvsBuilder.build());
+        return builder.build();
+    }
 
-       @Override
-       public byte[] serializeObject(final Object object) {
-               if (!(object instanceof Gc)) {
-                       throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed GcObject.");
-               }
-               final Gc specObj = (Gc) object;
-               final byte[] retBytes = new byte[TLVS_OFFSET + 0];
-               retBytes[MAX_HOP_F_OFFSET] = UnsignedBytes.checkedCast(specObj.getMaxHop());
-               retBytes[MAX_UTIL_F_OFFSET] = UnsignedBytes.checkedCast(specObj.getMaxUtilization());
-               retBytes[MIN_UTIL_F_OFFSET] = UnsignedBytes.checkedCast(specObj.getMinUtilization());
-               retBytes[OVER_BOOKING_FACTOR_F_OFFSET] = UnsignedBytes.checkedCast(specObj.getOverBookingFactor());
-               return ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), retBytes);
-       }
+    @Override
+    public void serializeObject(final Object object, final ByteBuf buffer) {
+        Preconditions.checkArgument(object instanceof Gc, "Wrong instance of PCEPObject. Passed %s. Needed GcObject.", object.getClass());
+        final Gc specObj = (Gc) object;
+        final ByteBuf body = Unpooled.buffer();
+        writeUnsignedByte(specObj.getMaxHop(), body);
+        writeUnsignedByte(specObj.getMaxUtilization(), body);
+        writeUnsignedByte(specObj.getMinUtilization(), body);
+        writeUnsignedByte(specObj.getOverBookingFactor(), body);
+        serializeTlvs(specObj.getTlvs(), body);
+        ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
+    }
 
-       @Override
-       public int getObjectType() {
-               return TYPE;
-       }
+    public void serializeTlvs(final Tlvs tlvs, final ByteBuf body) {
+        if (tlvs == null) {
+            return;
+        }
+        serializeVendorInformationTlvs(tlvs.getVendorInformationTlv(), body);
+    }
 
-       @Override
-       public int getObjectClass() {
-               return CLASS;
-       }
+    @Override
+    protected final void addVendorInformationTlvs(final TlvsBuilder builder, final List<VendorInformationTlv> tlvs) {
+        if (!tlvs.isEmpty()) {
+            builder.setVendorInformationTlv(tlvs);
+        }
+    }
 }