BUG-612 : switched PCEP message serializers to ByteBuf
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / message / PCEPCloseMessageParser.java
index 1a166cd4e70a49ee6193b479d9d9a4771a641c8c..ab630ad4dc4e79d26fe3f0cf58929bfafdd38fc2 100644 (file)
@@ -7,12 +7,17 @@
  */
 package org.opendaylight.protocol.pcep.impl.message;
 
+import com.google.common.base.Preconditions;
+
 import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
 
 import java.util.List;
 
-import org.opendaylight.protocol.pcep.PCEPDeserializerException;
-import org.opendaylight.protocol.pcep.spi.ObjectHandlerRegistry;
+import org.opendaylight.protocol.pcep.spi.AbstractMessageParser;
+import org.opendaylight.protocol.pcep.spi.MessageUtil;
+import org.opendaylight.protocol.pcep.spi.ObjectRegistry;
+import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.Close;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.CloseBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.CloseMessage;
@@ -27,45 +32,39 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.typ
  */
 public class PCEPCloseMessageParser extends AbstractMessageParser {
 
-       public static final int TYPE = 7;
-
-       public PCEPCloseMessageParser(final ObjectHandlerRegistry registry) {
-               super(registry);
-       }
+    public static final int TYPE = 7;
 
-       @Override
-       public void serializeMessage(final Message message, final ByteBuf buffer) {
-               if (!(message instanceof CloseMessage)) {
-                       throw new IllegalArgumentException("Wrong instance of Message. Passed instance of " + message.getClass()
-                                       + ". Nedded CloseMessage.");
-               }
-               final CCloseMessage close = ((CloseMessage) message).getCCloseMessage();
+    public PCEPCloseMessageParser(final ObjectRegistry registry) {
+        super(registry);
+    }
 
-               if (close.getCClose() == null) {
-                       throw new IllegalArgumentException("Close Object must be present in Close Message.");
-               }
-               buffer.writeBytes(serializeObject(close.getCClose()));
-       }
+    @Override
+    public void serializeMessage(final Message message, final ByteBuf out) {
+        Preconditions.checkArgument(message instanceof CloseMessage, "Wrong instance of Message. Passed instance of %s. Need CloseMessage.", message.getClass());
+        final CCloseMessage close = ((CloseMessage) message).getCCloseMessage();
 
-       @Override
-       protected Close validate(final List<Object> objects, final List<Message> errors) throws PCEPDeserializerException {
-               if (objects == null) {
-                       throw new IllegalArgumentException("Passed list can't be null.");
-               }
-               if (objects.isEmpty() || !(objects.get(0) instanceof CClose)) {
-                       throw new PCEPDeserializerException("Close message doesn't contain CLOSE object.");
-               }
-               final Object o = objects.get(0);
-               final CCloseMessage msg = new CCloseMessageBuilder().setCClose((CClose) o).build();
-               objects.remove(0);
-               if (!objects.isEmpty()) {
-                       throw new PCEPDeserializerException("Unprocessed Objects: " + objects);
-               }
-               return new CloseBuilder().setCCloseMessage(msg).build();
-       }
+        if (close.getCClose() == null) {
+            throw new IllegalArgumentException("Close Object must be present in Close Message.");
+        }
+        ByteBuf buffer = Unpooled.buffer();
+        serializeObject(close.getCClose(), buffer);
+        MessageUtil.formatMessage(TYPE, buffer, out);
+    }
 
-       @Override
-       public int getMessageType() {
-               return TYPE;
-       }
+    @Override
+    protected Close validate(final List<Object> objects, final List<Message> errors) throws PCEPDeserializerException {
+        if (objects == null) {
+            throw new IllegalArgumentException("Passed list can't be null.");
+        }
+        if (objects.isEmpty() || !(objects.get(0) instanceof CClose)) {
+            throw new PCEPDeserializerException("Close message doesn't contain CLOSE object.");
+        }
+        final Object o = objects.get(0);
+        final CCloseMessage msg = new CCloseMessageBuilder().setCClose((CClose) o).build();
+        objects.remove(0);
+        if (!objects.isEmpty()) {
+            throw new PCEPDeserializerException("Unprocessed Objects: " + objects);
+        }
+        return new CloseBuilder().setCCloseMessage(msg).build();
+    }
 }