BUG-612 : switched PCEP message serializers to ByteBuf
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / object / PCEPExplicitRouteObjectParser.java
index 387634c73ee755e3c7969289db73969a3d3c507d..108f417244d73317c7936f81a63e434a89bdf061 100644 (file)
@@ -7,7 +7,13 @@
  */
 package org.opendaylight.protocol.pcep.impl.object;
 
-import org.opendaylight.protocol.pcep.spi.EROSubobjectHandlerRegistry;
+import com.google.common.base.Preconditions;
+
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
+
+import org.opendaylight.protocol.pcep.spi.EROSubobjectRegistry;
+import org.opendaylight.protocol.pcep.spi.ObjectUtil;
 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
 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;
@@ -19,47 +25,33 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.typ
  */
 public class PCEPExplicitRouteObjectParser extends AbstractEROWithSubobjectsParser {
 
-       public static final int CLASS = 7;
-
-       public static final int TYPE = 1;
-
-       public PCEPExplicitRouteObjectParser(final EROSubobjectHandlerRegistry subobjReg) {
-               super(subobjReg);
-       }
-
-       @Override
-       public Ero parseObject(final ObjectHeader header, final byte[] bytes) throws PCEPDeserializerException {
-               if (bytes == null || bytes.length == 0) {
-                       throw new IllegalArgumentException("Byte array is mandatory. Can't be null or empty.");
-               }
-               final EroBuilder builder = new EroBuilder();
-               builder.setIgnore(header.isIgnore());
-               builder.setProcessingRule(header.isProcessingRule());
-               builder.setSubobjects(parseSubobjects(bytes));
-               return builder.build();
-       }
-
-       @Override
-       public byte[] serializeObject(final Object object) {
-               if (!(object instanceof Ero)) {
-                       throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass()
-                                       + ". Needed ExplicitRouteObject.");
-               }
-               final Ero ero = ((Ero) object);
-
-               assert !(ero.getSubobjects().isEmpty()) : "Empty Explicit Route Object.";
-
-               return ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(),
-                               serializeSubobject(ero.getSubobjects()));
-       }
-
-       @Override
-       public int getObjectType() {
-               return TYPE;
-       }
-
-       @Override
-       public int getObjectClass() {
-               return CLASS;
-       }
+    public static final int CLASS = 7;
+
+    public static final int TYPE = 1;
+
+    public PCEPExplicitRouteObjectParser(final EROSubobjectRegistry subobjReg) {
+        super(subobjReg);
+    }
+
+    @Override
+    public Ero parseObject(final ObjectHeader header, final ByteBuf buffer) throws PCEPDeserializerException {
+        // Explicit approval of empty ERO
+        Preconditions.checkArgument(buffer != null, "Array of bytes is mandatory. Can't be null.");
+        final EroBuilder builder = new EroBuilder();
+        builder.setIgnore(header.isIgnore());
+        builder.setProcessingRule(header.isProcessingRule());
+        builder.setSubobject(parseSubobjects(buffer));
+        return builder.build();
+    }
+
+    @Override
+    public void serializeObject(final Object object, final ByteBuf buffer) {
+        Preconditions.checkArgument(object instanceof Ero, "Wrong instance of PCEPObject. Passed %s. Needed EroObject.", object.getClass());
+        final Ero ero = ((Ero) object);
+        final ByteBuf body = Unpooled.buffer();
+        // FIXME: switch to ByteBuf
+        final byte[] bytes = serializeSubobject(ero.getSubobject());
+        body.writeBytes(bytes);
+        ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
+    }
 }