Switch statements should end with a default case.
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / PCEPMessageToByteEncoder.java
index fedd8ad01417d5857f4d72a43c43e0430f19bd5b..dc68f6ecc2203fc9659f9eb891cdd49c045e6821 100644 (file)
@@ -7,52 +7,36 @@
  */
 package org.opendaylight.protocol.pcep.impl;
 
+import com.google.common.base.Preconditions;
+
 import io.netty.buffer.ByteBuf;
-import io.netty.buffer.Unpooled;
 import io.netty.channel.ChannelHandler.Sharable;
 import io.netty.channel.ChannelHandlerContext;
 import io.netty.handler.codec.MessageToByteEncoder;
 
-import org.opendaylight.protocol.pcep.spi.MessageHandlerRegistry;
-import org.opendaylight.protocol.pcep.spi.MessageSerializer;
+import org.opendaylight.protocol.pcep.spi.MessageRegistry;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Message;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import com.google.common.base.Preconditions;
-import com.google.common.primitives.UnsignedBytes;
-
 /**
  *
  */
 @Sharable
 public final class PCEPMessageToByteEncoder extends MessageToByteEncoder<Message> {
-       private static final Logger LOG = LoggerFactory.getLogger(PCEPMessageToByteEncoder.class);
-       private static final int VERSION_SF_LENGTH = 3;
-       private final MessageHandlerRegistry registry;
-
-       public PCEPMessageToByteEncoder(final MessageHandlerRegistry registry) {
-               this.registry = Preconditions.checkNotNull(registry);
-       }
-
-       @Override
-       protected void encode(final ChannelHandlerContext ctx, final Message msg, final ByteBuf out) throws Exception {
-               Preconditions.checkNotNull(msg);
-               LOG.debug("Sent to encode : {}", msg);
-
-               final ByteBuf body = Unpooled.buffer();
-               final MessageSerializer serializer = this.registry.getMessageSerializer(msg);
-               serializer.serializeMessage(msg, body);
-
-               final int msgLength = body.readableBytes() + PCEPMessageConstants.COMMON_HEADER_LENGTH;
-               final byte[] header = new byte[] {
-                               UnsignedBytes.checkedCast(PCEPMessageConstants.PCEP_VERSION << (Byte.SIZE - VERSION_SF_LENGTH)),
-                               UnsignedBytes.checkedCast(serializer.getMessageType()),
-                               UnsignedBytes.checkedCast(msgLength / 256),
-                               UnsignedBytes.checkedCast(msgLength % 256)
-               };
-               Preconditions.checkState(header.length == PCEPMessageConstants.COMMON_HEADER_LENGTH);
-               out.writeBytes(header);
-               out.writeBytes(body);
-       }
+
+    private static final Logger LOG = LoggerFactory.getLogger(PCEPMessageToByteEncoder.class);
+
+    private final MessageRegistry registry;
+
+    public PCEPMessageToByteEncoder(final MessageRegistry registry) {
+        this.registry = Preconditions.checkNotNull(registry);
+    }
+
+    @Override
+    protected void encode(final ChannelHandlerContext ctx, final Message msg, final ByteBuf out) {
+        Preconditions.checkNotNull(msg);
+        this.registry.serializeMessage(msg, out);
+        LOG.debug("Encoded : {}", msg);
+    }
 }