Getting rid of readerIndex handling.
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / object / PCEPMetricObjectParser.java
index 2f50386bc1b51cc18d6142e0c7b424a275b9bb3e..1e418afd20bbd72940f79378b57a616ee05a4e61 100644 (file)
  */
 package org.opendaylight.protocol.pcep.impl.object;
 
-import java.util.BitSet;
+import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeBitSet;
+import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeFloat32;
+import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedByte;
 
-import org.opendaylight.protocol.pcep.PCEPDeserializerException;
-import org.opendaylight.protocol.pcep.PCEPDocumentedException;
-import org.opendaylight.protocol.pcep.spi.AbstractObjectParser;
-import org.opendaylight.protocol.pcep.spi.HandlerRegistry;
+import com.google.common.base.Preconditions;
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
+import java.util.BitSet;
+import org.opendaylight.protocol.pcep.spi.ObjectParser;
+import org.opendaylight.protocol.pcep.spi.ObjectSerializer;
+import org.opendaylight.protocol.pcep.spi.ObjectUtil;
+import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
 import org.opendaylight.protocol.util.ByteArray;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ieee754.rev130819.Float32;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.MetricObject;
 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.pcreq.message.pcreq.message.svec.Metric;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcreq.message.pcreq.message.svec.MetricBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.metric.object.Metric;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.metric.object.MetricBuilder;
 
 /**
- * Parser for {@link org.opendaylight.protocol.pcep.object.PCEPMetricObject PCEPMetricObject}
+ * Parser for {@link Metric}
  */
-public class PCEPMetricObjectParser extends AbstractObjectParser<MetricBuilder> {
-
-       public static final int CLASS = 6;
-
-       public static final int TYPE = 1;
-
-       /*
-        * lengths of fields in bytes
-        */
-       private static final int FLAGS_F_LENGTH = 1;
-       private static final int TYPE_F_LENGTH = 1;
-       private static final int METRIC_VALUE_F_LENGTH = 4;
-
-       /*
-        * offsets of fields in bytes
-        */
-       public static final int FLAGS_F_OFFSET = 2;
-       public static final int TYPE_F_OFFSET = FLAGS_F_OFFSET + FLAGS_F_LENGTH;
-       public static final int METRIC_VALUE_F_OFFSET = TYPE_F_OFFSET + TYPE_F_LENGTH;
-
-       /*
-        * flags offsets inside flags field in bits
-        */
-       private static final int C_FLAG_OFFSET = 6;
-       private static final int B_FLAG_OFFSET = 7;
-
-       public static final int SIZE = METRIC_VALUE_F_OFFSET + METRIC_VALUE_F_LENGTH;
-
-       public PCEPMetricObjectParser(final HandlerRegistry registry) {
-               super(registry);
-       }
-
-       @Override
-       public MetricObject parseObject(final ObjectHeader header, final byte[] bytes) throws PCEPDeserializerException,
-                       PCEPDocumentedException {
-               if (bytes == null || bytes.length == 0)
-                       throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty.");
-               if (bytes.length != SIZE)
-                       throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + bytes.length + "; Expected: " + SIZE + ".");
-               final byte[] flagBytes = { bytes[FLAGS_F_OFFSET] };
-               final BitSet flags = ByteArray.bytesToBitSet(flagBytes);
-
-               final MetricBuilder builder = new MetricBuilder();
-
-               builder.setIgnore(header.isIgnore());
-               builder.setProcessingRule(header.isProcessingRule());
-
-               builder.setBound(flags.get(B_FLAG_OFFSET));
-               builder.setComputed(flags.get(C_FLAG_OFFSET));
-               builder.setMetricType((short) (bytes[TYPE_F_OFFSET] & 0xFF));
-               builder.setValue(new Float32(ByteArray.subByte(bytes, METRIC_VALUE_F_OFFSET, METRIC_VALUE_F_LENGTH)));
-
-               return builder.build();
-       }
-
-       @Override
-       public void addTlv(final MetricBuilder builder, final Tlv tlv) {
-               // No tlvs defined
-       }
-
-       @Override
-       public byte[] serializeObject(final Object object) {
-               if (!(object instanceof MetricObject))
-                       throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed MetricObject.");
-
-               final MetricObject mObj = (MetricObject) object;
-
-               final byte[] retBytes = new byte[SIZE];
-               final BitSet flags = new BitSet(FLAGS_F_LENGTH * Byte.SIZE);
-               flags.set(C_FLAG_OFFSET, ((Metric) mObj).isComputed());
-               flags.set(B_FLAG_OFFSET, mObj.isBound());
-
-               ByteArray.copyWhole(ByteArray.bitSetToBytes(flags, FLAGS_F_LENGTH), retBytes, FLAGS_F_OFFSET);
-
-               System.arraycopy(mObj.getValue().getValue(), 0, retBytes, METRIC_VALUE_F_OFFSET, METRIC_VALUE_F_LENGTH);
-
-               return retBytes;
-       }
-
-       @Override
-       public int getObjectType() {
-               return TYPE;
-       }
-
-       @Override
-       public int getObjectClass() {
-               return CLASS;
-       }
+public class PCEPMetricObjectParser implements ObjectParser, ObjectSerializer {
+
+    public static final int CLASS = 6;
+
+    public static final int TYPE = 1;
+
+    /*
+     * lengths of fields in bytes
+     */
+    private static final int FLAGS_F_LENGTH = 1;
+    private static final int METRIC_VALUE_F_LENGTH = 4;
+
+    /*
+     * offsets of fields in bytes
+     */
+    private static final int RESERVED = 2;
+
+    /*
+     * flags offsets inside flags field in bits
+     */
+    private static final int C_FLAG_OFFSET = 6;
+    private static final int B_FLAG_OFFSET = 7;
+
+    private static final int SIZE = METRIC_VALUE_F_LENGTH + METRIC_VALUE_F_LENGTH;
+
+    @Override
+    public Metric 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.");
+        if (bytes.readableBytes() != SIZE) {
+            throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + bytes.readableBytes() + "; Expected: " + SIZE
+                    + ".");
+        }
+        bytes.skipBytes(RESERVED);
+        final byte[] flagBytes = { bytes.readByte() };
+        final BitSet flags = ByteArray.bytesToBitSet(flagBytes);
+        final MetricBuilder builder = new MetricBuilder();
+        builder.setIgnore(header.isIgnore());
+        builder.setProcessingRule(header.isProcessingRule());
+        builder.setBound(flags.get(B_FLAG_OFFSET));
+        builder.setComputed(flags.get(C_FLAG_OFFSET));
+        builder.setMetricType(bytes.readUnsignedByte());
+        builder.setValue(new Float32(ByteArray.readBytes(bytes, METRIC_VALUE_F_LENGTH)));
+        return builder.build();
+    }
+
+    @Override
+    public void serializeObject(final Object object, final ByteBuf buffer) {
+        Preconditions.checkArgument(object instanceof Metric, "Wrong instance of PCEPObject. Passed %s. Needed MetricObject.", object.getClass());
+        final Metric mObj = (Metric) object;
+        final ByteBuf body = Unpooled.buffer(SIZE);
+        body.writeZero(RESERVED);
+        final BitSet flags = new BitSet(FLAGS_F_LENGTH * Byte.SIZE);
+        if (mObj.isComputed() != null) {
+            flags.set(C_FLAG_OFFSET, mObj.isComputed());
+        }
+        if (mObj.isBound() != null) {
+            flags.set(B_FLAG_OFFSET, mObj.isBound());
+        }
+        writeBitSet(flags, FLAGS_F_LENGTH, body);
+        Preconditions.checkArgument(mObj.getMetricType() != null, "MetricType is mandatory.");
+        writeUnsignedByte(mObj.getMetricType(), body);
+        writeFloat32(mObj.getValue(), body);
+        ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
+    }
 }