Instruction experimenterId fix
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / core / OFEncoder.java
index 9a0cdbb1c9ae3960a0acb9a6ac05ac6b63b0c150..075b99da2bb30bddaca8c1fc4707cdd18d44b1eb 100644 (file)
@@ -1,46 +1,68 @@
-/* Copyright (C)2013 Pantheon Technologies, s.r.o. All rights reserved. */\r
-package org.opendaylight.openflowjava.protocol.impl.core;\r
-\r
-import io.netty.buffer.ByteBuf;\r
-import io.netty.channel.ChannelHandlerContext;\r
-import io.netty.handler.codec.MessageToByteEncoder;\r
-\r
-import org.opendaylight.openflowjava.protocol.impl.serialization.SerializationFactory;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;\r
-import org.slf4j.Logger;\r
-import org.slf4j.LoggerFactory;\r
-\r
-/**\r
- * Transforms OpenFlow Protocol messages to POJOs\r
- * @author michal.polkorab\r
- * @author timotej.kubas\r
- */\r
-public class OFEncoder extends MessageToByteEncoder<OfHeader> {\r
-\r
-    private static final Logger LOGGER = LoggerFactory.getLogger(OFEncoder.class);\r
-    \r
-    /** Constructor of class */\r
-    public OFEncoder() {\r
-        LOGGER.debug("Creating OF13Encoder");\r
-    }\r
-    \r
-    @Override\r
-    protected void encode(ChannelHandlerContext ctx, OfHeader msg, ByteBuf out)\r
-            throws Exception {\r
-        LOGGER.debug("Encoding");\r
-        try {\r
-            SerializationFactory.messageToBuffer(msg.getVersion(), out, msg);\r
-        } catch(Exception e) {\r
-            LOGGER.error("Message serialization failed");\r
-            LOGGER.error(e.getMessage(), e);\r
-            return;\r
-        }\r
-        if (out.readableBytes() > 0) {\r
-            out.retain();\r
-            ctx.writeAndFlush(out);\r
-        } else {\r
-            LOGGER.warn("Translated buffer is empty");\r
-        }\r
-    }\r
-\r
-}\r
+/*
+ * Copyright (c) 2013 Pantheon Technologies s.r.o. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+
+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.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
+ * @author michal.polkorab
+ * @author timotej.kubas
+ */
+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() {
+        statisticsCounters = StatisticsCounters.getInstance();
+        LOGGER.trace("Creating OF13Encoder");
+    }
+
+    @Override
+    protected void encode(ChannelHandlerContext ctx, MessageListenerWrapper wrapper, ByteBuf out)
+            throws Exception {
+        LOGGER.trace("Encoding");
+        try {
+            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.warn("Message serialization failed ", e);
+            statisticsCounters.incrementCounter(CounterEventTypes.DS_ENCODE_FAIL);
+            Future<Void> newFailedFuture = ctx.newFailedFuture(e);
+            wrapper.getListener().operationComplete(newFailedFuture);
+            out.clear();
+            return;
+        }
+    }
+
+    /**
+     * @param serializationFactory
+     */
+    public void setSerializationFactory(SerializationFactory serializationFactory) {
+        this.serializationFactory = serializationFactory;
+    }
+
+}