Check for transport protocol confi
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / core / OFEncoder.java
index 6080003f99297b53374b8a0e7980df9e69d7112f..2f356693773803aac9b87628f77588f19c0299e8 100644 (file)
@@ -12,37 +12,48 @@ 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.connection.MessageListenerWrapper;
+import org.opendaylight.openflowjava.protocol.impl.core.connection.MessageListenerWrapper;
 import org.opendaylight.openflowjava.protocol.impl.serialization.SerializationFactory;
+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;
 
 /**
- * Transforms OpenFlow Protocol messages to POJOs
+ * Transforms OpenFlow Protocol messages to POJOs.
  * @author michal.polkorab
  * @author timotej.kubas
  */
 public class OFEncoder extends MessageToByteEncoder<MessageListenerWrapper> {
 
-    private static final Logger LOGGER = LoggerFactory.getLogger(OFEncoder.class);
+    private static final Logger LOG = LoggerFactory.getLogger(OFEncoder.class);
     private SerializationFactory serializationFactory;
+    private final StatisticsCounters statisticsCounters;
 
     /** Constructor of class */
     public OFEncoder() {
-        LOGGER.trace("Creating OF13Encoder");
+        statisticsCounters = StatisticsCounters.getInstance();
+        LOG.trace("Creating OFEncoder");
     }
 
     @Override
-    protected void encode(ChannelHandlerContext ctx, MessageListenerWrapper wrapper, ByteBuf out)
+    protected void encode(final ChannelHandlerContext ctx, final MessageListenerWrapper wrapper, final ByteBuf out)
             throws Exception {
-        LOGGER.trace("Encoding");
+        LOG.trace("Encoding");
         try {
             serializationFactory.messageToBuffer(wrapper.getMsg().getVersion(), out, wrapper.getMsg());
-        } catch(Exception e) {
-            LOGGER.warn("Message serialization failed: {}", e.getMessage());
-            Future<Void> newFailedFuture = ctx.newFailedFuture(e);
-            wrapper.getListener().operationComplete(newFailedFuture);
+            if(wrapper.getMsg() instanceof FlowModInput){
+                statisticsCounters.incrementCounter(CounterEventTypes.DS_FLOW_MODS_SENT);
+            }
+            statisticsCounters.incrementCounter(CounterEventTypes.DS_ENCODE_SUCCESS);
+        } catch(final Exception e) {
+            LOG.warn("Message serialization failed ", e);
+            statisticsCounters.incrementCounter(CounterEventTypes.DS_ENCODE_FAIL);
+            if (wrapper.getListener() != null) {
+                final Future<Void> newFailedFuture = ctx.newFailedFuture(e);
+                wrapper.getListener().operationComplete(newFailedFuture);
+            }
             out.clear();
             return;
         }
@@ -51,7 +62,7 @@ public class OFEncoder extends MessageToByteEncoder<MessageListenerWrapper> {
     /**
      * @param serializationFactory
      */
-    public void setSerializationFactory(SerializationFactory serializationFactory) {
+    public void setSerializationFactory(final SerializationFactory serializationFactory) {
         this.serializationFactory = serializationFactory;
     }