Allow any hello mesage and extend hello support for v1.4, v1.5
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / core / OFDecoder.java
index a1f9fb948bc69aab821c931127fc1a9c8d043c65..90d0ab0227dffe084be247402b0caccf8bf55e2e 100644 (file)
@@ -10,53 +10,57 @@ package org.opendaylight.openflowjava.protocol.impl.core;
 
 import io.netty.channel.ChannelHandlerContext;
 import io.netty.handler.codec.MessageToMessageDecoder;
-
 import java.util.List;
-
 import org.opendaylight.openflowjava.protocol.impl.deserialization.DeserializationFactory;
-import org.opendaylight.openflowjava.protocol.impl.util.ByteBufUtils;
+import org.opendaylight.openflowjava.statistics.CounterEventTypes;
+import org.opendaylight.openflowjava.statistics.StatisticsCounters;
+import org.opendaylight.openflowjava.util.ByteBufUtils;
 import org.opendaylight.yangtools.yang.binding.DataObject;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 /**
- * Transforms OpenFlow Protocol messages to POJOs
+ * Transforms OpenFlow Protocol messages to POJOs.
  * @author michal.polkorab
  */
 public class OFDecoder extends MessageToMessageDecoder<VersionMessageWrapper> {
 
-    private static final Logger LOGGER = LoggerFactory.getLogger(OFDecoder.class);
+    private static final Logger LOG = LoggerFactory.getLogger(OFDecoder.class);
+    private final StatisticsCounters statisticsCounter;
+
+    // TODO: make this final?
     private DeserializationFactory deserializationFactory;
 
-    /**
-     * Constructor of class
-     */
     public OFDecoder() {
-        LOGGER.trace("Creating OF 1.3 Decoder");
+        LOG.trace("Creating OFDecoder");
+           // TODO: pass as argument
+        statisticsCounter = StatisticsCounters.getInstance();
     }
 
     @Override
-    protected void decode(ChannelHandlerContext ctx, VersionMessageWrapper msg,
-            List<Object> out) throws Exception {
-        if (LOGGER.isDebugEnabled()) {
-            LOGGER.debug("VersionMessageWrapper received");
-            LOGGER.debug("<< " + ByteBufUtils.byteBufToHexString(msg.getMessageBuffer()));
+    protected void decode(ChannelHandlerContext ctx, VersionMessageWrapper msg, List<Object> out) throws Exception {
+        statisticsCounter.incrementCounter(CounterEventTypes.US_RECEIVED_IN_OFJAVA);
+        if (LOG.isDebugEnabled()) {
+            LOG.debug("VersionMessageWrapper received");
+            LOG.debug("<< {}", ByteBufUtils.byteBufToHexString(msg.getMessageBuffer()));
         }
-        DataObject dataObject = null;
+
         try {
-            dataObject = deserializationFactory.deserialize(msg.getMessageBuffer(),
+            final DataObject dataObject = deserializationFactory.deserialize(msg.getMessageBuffer(),
                     msg.getVersion());
-        } catch(Exception e) {
-            LOGGER.error("Message deserialization failed");
-            LOGGER.error(e.getMessage(), e);
-            return;
-        }
-        if (dataObject == null) {
-            LOGGER.warn("Translated POJO is null");
-            return;
+            if (dataObject == null) {
+                LOG.warn("Translated POJO is null");
+                statisticsCounter.incrementCounter(CounterEventTypes.US_DECODE_FAIL);
+            } else {
+                out.add(dataObject);
+                statisticsCounter.incrementCounter(CounterEventTypes.US_DECODE_SUCCESS);
+            }
+        } catch (Exception e) {
+            LOG.warn("Message deserialization failed", e);
+            statisticsCounter.incrementCounter(CounterEventTypes.US_DECODE_FAIL);
+        } finally {
+            msg.getMessageBuffer().release();
         }
-        msg.getMessageBuffer().release();
-        out.add(dataObject);
     }
 
     /**