BUG 4181: Fix PCEP Parsers/Serializers
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / object / PCEPLoadBalancingObjectParser.java
index 8eb1d4035b3b7b38a3d68b9fd5c634d887f2d014..c035a56156cf379fc4a6159446999efd9a08f1fa 100644 (file)
@@ -7,92 +7,61 @@
  */
 package org.opendaylight.protocol.pcep.impl.object;
 
-import org.opendaylight.protocol.pcep.PCEPDeserializerException;
-import org.opendaylight.protocol.pcep.PCEPDocumentedException;
-import org.opendaylight.protocol.pcep.impl.message.AbstractObjectWithTlvsParser;
-import org.opendaylight.protocol.pcep.spi.TlvHandlerRegistry;
+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 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.LoadBalancingObject;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.Bandwidth;
 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.requests.segment.computation.p2p.LoadBalancingBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.load.balancing.object.LoadBalancing;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.load.balancing.object.LoadBalancingBuilder;
 
 /**
- * Parser for {@link org.opendaylight.protocol.pcep.object.PCEPLoadBalancingObject PCEPLoadBalancingObject}
+ * Parser for {@link LoadBalancing}
  */
-public class PCEPLoadBalancingObjectParser extends AbstractObjectWithTlvsParser<LoadBalancingBuilder> {
-
-       public static final int CLASS = 14;
-
-       public static final int TYPE = 1;
-
-       public static final int FLAGS_F_LENGTH = 1;
-       public static final int MAX_LSP_F_LENGTH = 1;
-       public static final int MIN_BAND_F_LENGTH = 4;
-
-       public static final int FLAGS_F_OFFSET = 2;
-       public static final int MAX_LSP_F_OFFSET = FLAGS_F_OFFSET + FLAGS_F_LENGTH;
-       public static final int MIN_BAND_F_OFFSET = MAX_LSP_F_OFFSET + MAX_LSP_F_LENGTH;
-
-       public static final int SIZE = MIN_BAND_F_OFFSET + MIN_BAND_F_LENGTH;
-
-       public PCEPLoadBalancingObjectParser(final TlvHandlerRegistry tlvReg) {
-               super(tlvReg);
-       }
-
-       @Override
-       public LoadBalancingObject parseObject(final ObjectHeader header, final byte[] bytes) throws PCEPDeserializerException,
-       PCEPDocumentedException {
-               if (bytes == null || bytes.length == 0) {
-                       throw new IllegalArgumentException("Byte array 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 LoadBalancingBuilder builder = new LoadBalancingBuilder();
-
-               builder.setIgnore(header.isIgnore());
-               builder.setProcessingRule(header.isProcessingRule());
-
-               builder.setMaxLsp((short) (bytes[MAX_LSP_F_OFFSET] & 0xFF));
-               builder.setMinBandwidth(new Float32(ByteArray.subByte(bytes, MIN_BAND_F_OFFSET, MIN_BAND_F_LENGTH)));
-
-               return builder.build();
-       }
-
-       @Override
-       public void addTlv(final LoadBalancingBuilder builder, final Tlv tlv) {
-               // No tlvs defined
-       }
-
-       @Override
-       public byte[] serializeObject(final Object object) {
-               if (!(object instanceof LoadBalancingObject)) {
-                       throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass()
-                                       + ". Needed LoadBalancingObject.");
-               }
-
-               final LoadBalancingObject specObj = (LoadBalancingObject) object;
-
-               final byte[] retBytes = new byte[SIZE];
-
-               retBytes[MAX_LSP_F_OFFSET] = ByteArray.shortToBytes(specObj.getMaxLsp())[1];
-               ByteArray.copyWhole(specObj.getMinBandwidth().getValue(), retBytes, MIN_BAND_F_OFFSET);
-
-               return retBytes;
-       }
-
-       @Override
-       public int getObjectType() {
-               return TYPE;
-       }
-
-       @Override
-       public int getObjectClass() {
-               return CLASS;
-       }
+public class PCEPLoadBalancingObjectParser implements ObjectParser, ObjectSerializer {
+
+    public static final int CLASS = 14;
+
+    public static final int TYPE = 1;
+
+    private static final int RESERVED = 2;
+    private static final int FLAGS_F_LENGTH = 1;
+
+    private static final int SIZE = 8;
+
+    @Override
+    public LoadBalancing 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
+                    + ".");
+        }
+        final LoadBalancingBuilder builder = new LoadBalancingBuilder();
+        builder.setIgnore(header.isIgnore());
+        builder.setProcessingRule(header.isProcessingRule());
+        bytes.skipBytes(RESERVED + FLAGS_F_LENGTH);
+        builder.setMaxLsp(bytes.readUnsignedByte());
+        builder.setMinBandwidth(new Bandwidth(ByteArray.readAllBytes(bytes)));
+        return builder.build();
+    }
+
+    @Override
+    public void serializeObject(final Object object, final ByteBuf buffer) {
+        Preconditions.checkArgument(object instanceof LoadBalancing, "Wrong instance of PCEPObject. Passed %s. Needed LoadBalancingObject.", object.getClass());
+        final LoadBalancing specObj = (LoadBalancing) object;
+        final ByteBuf body = Unpooled.buffer(SIZE);
+        body.writeZero(RESERVED + FLAGS_F_LENGTH);
+        writeUnsignedByte(specObj.getMaxLsp(), body);
+        writeFloat32(specObj.getMinBandwidth(), body);
+        ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
+    }
 }