*/
package org.opendaylight.protocol.pcep.impl.object;
-import com.google.common.base.Preconditions;
-import com.google.common.primitives.UnsignedBytes;
+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 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.AbstractObjectWithTlvsParser;
+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.pcep.spi.TlvRegistry;
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.Object;
/**
* Parser for {@link Metric}
*/
-public class PCEPMetricObjectParser extends AbstractObjectWithTlvsParser<MetricBuilder> {
+public class PCEPMetricObjectParser implements ObjectParser, ObjectSerializer {
public static final int CLASS = 6;
* 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
*/
- private static final int FLAGS_F_OFFSET = 2;
- private static final int TYPE_F_OFFSET = FLAGS_F_OFFSET + FLAGS_F_LENGTH;
- private static final int METRIC_VALUE_F_OFFSET = TYPE_F_OFFSET + TYPE_F_LENGTH;
+ 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_OFFSET + METRIC_VALUE_F_LENGTH;
-
- public PCEPMetricObjectParser(final TlvRegistry tlvReg) {
- super(tlvReg);
- }
+ 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 {
throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + bytes.readableBytes() + "; Expected: " + SIZE
+ ".");
}
- bytes.readerIndex(bytes.readerIndex() + FLAGS_F_OFFSET);
+ bytes.skipBytes(RESERVED);
final byte[] flagBytes = { bytes.readByte() };
final BitSet flags = ByteArray.bytesToBitSet(flagBytes);
final MetricBuilder builder = new MetricBuilder();
builder.setProcessingRule(header.isProcessingRule());
builder.setBound(flags.get(B_FLAG_OFFSET));
builder.setComputed(flags.get(C_FLAG_OFFSET));
- builder.setMetricType((short) UnsignedBytes.toInt(bytes.readByte()));
+ builder.setMetricType(bytes.readUnsignedByte());
builder.setValue(new Float32(ByteArray.readBytes(bytes, METRIC_VALUE_F_LENGTH)));
return builder.build();
}
@Override
- public byte[] serializeObject(final Object object) {
- if (!(object instanceof Metric)) {
- throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed MetricObject.");
- }
+ 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 byte[] retBytes = new byte[SIZE];
+ final ByteBuf body = Unpooled.buffer(SIZE);
+ body.writeZero(RESERVED);
final BitSet flags = new BitSet(FLAGS_F_LENGTH * Byte.SIZE);
- flags.set(C_FLAG_OFFSET, mObj.isComputed());
- flags.set(B_FLAG_OFFSET, mObj.isBound());
- ByteArray.copyWhole(ByteArray.bitSetToBytes(flags, FLAGS_F_LENGTH), retBytes, FLAGS_F_OFFSET);
- retBytes[TYPE_F_OFFSET] = UnsignedBytes.checkedCast(mObj.getMetricType());
- System.arraycopy(mObj.getValue().getValue(), 0, retBytes, METRIC_VALUE_F_OFFSET, METRIC_VALUE_F_LENGTH);
- return ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), retBytes);
+ 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);
}
}