Remove trailing whitespace
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / core / OFEncoder.java
index a62384178473422f9cbaf96825fab3e08485e010..075b99da2bb30bddaca8c1fc4707cdd18d44b1eb 100644 (file)
@@ -11,9 +11,13 @@ package org.opendaylight.openflowjava.protocol.impl.core;
 import io.netty.buffer.ByteBuf;
 import io.netty.channel.ChannelHandlerContext;
 import io.netty.handler.codec.MessageToByteEncoder;
+import io.netty.util.concurrent.Future;
 
+import org.opendaylight.openflowjava.protocol.impl.core.connection.MessageListenerWrapper;
 import org.opendaylight.openflowjava.protocol.impl.serialization.SerializationFactory;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
+import org.opendaylight.openflowjava.statistics.CounterEventTypes;
+import org.opendaylight.openflowjava.statistics.StatisticsCounters;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowModInput;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -22,33 +26,43 @@ import org.slf4j.LoggerFactory;
  * @author michal.polkorab
  * @author timotej.kubas
  */
-public class OFEncoder extends MessageToByteEncoder<OfHeader> {
+public class OFEncoder extends MessageToByteEncoder<MessageListenerWrapper> {
 
     private static final Logger LOGGER = LoggerFactory.getLogger(OFEncoder.class);
-    
+    private SerializationFactory serializationFactory;
+    private StatisticsCounters statisticsCounters;
+
     /** Constructor of class */
     public OFEncoder() {
-        LOGGER.debug("Creating OF13Encoder");
+        statisticsCounters = StatisticsCounters.getInstance();
+        LOGGER.trace("Creating OF13Encoder");
     }
-    
+
     @Override
-    protected void encode(ChannelHandlerContext ctx, OfHeader msg, ByteBuf out)
+    protected void encode(ChannelHandlerContext ctx, MessageListenerWrapper wrapper, ByteBuf out)
             throws Exception {
-        LOGGER.debug("Encoding");
+        LOGGER.trace("Encoding");
         try {
-            SerializationFactory.messageToBuffer(msg.getVersion(), out, msg);
+            serializationFactory.messageToBuffer(wrapper.getMsg().getVersion(), out, wrapper.getMsg());
+            if(wrapper.getMsg() instanceof FlowModInput){
+                statisticsCounters.incrementCounter(CounterEventTypes.DS_FLOW_MODS_SENT);
+            }
+            statisticsCounters.incrementCounter(CounterEventTypes.DS_ENCODE_SUCCESS);
         } catch(Exception e) {
-            LOGGER.error("Message serialization failed");
-            LOGGER.error(e.getMessage(), e);
+            LOGGER.warn("Message serialization failed ", e);
+            statisticsCounters.incrementCounter(CounterEventTypes.DS_ENCODE_FAIL);
+            Future<Void> newFailedFuture = ctx.newFailedFuture(e);
+            wrapper.getListener().operationComplete(newFailedFuture);
             out.clear();
             return;
         }
-        if (out.readableBytes() > 0) {
-            out.retain();
-            ctx.writeAndFlush(out);
-        } else {
-            LOGGER.warn("Translated buffer is empty");
-        }
+    }
+
+    /**
+     * @param serializationFactory
+     */
+    public void setSerializationFactory(SerializationFactory serializationFactory) {
+        this.serializationFactory = serializationFactory;
     }
 
 }