Exception catch while decoding / encoding DTOs
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / core / OFEncoder.java
1 /* Copyright (C)2013 Pantheon Technologies, s.r.o. All rights reserved. */\r
2 package org.opendaylight.openflowjava.protocol.impl.core;\r
3 \r
4 import io.netty.buffer.ByteBuf;\r
5 import io.netty.channel.ChannelHandlerContext;\r
6 import io.netty.handler.codec.MessageToByteEncoder;\r
7 \r
8 import org.opendaylight.openflowjava.protocol.impl.serialization.SerializationFactory;\r
9 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;\r
10 import org.slf4j.Logger;\r
11 import org.slf4j.LoggerFactory;\r
12 \r
13 /**\r
14  * Transforms OpenFlow Protocol messages to POJOs\r
15  * @author michal.polkorab\r
16  * @author timotej.kubas\r
17  */\r
18 public class OFEncoder extends MessageToByteEncoder<OfHeader> {\r
19 \r
20     private static final Logger LOGGER = LoggerFactory.getLogger(OFEncoder.class);\r
21     \r
22     /** Constructor of class */\r
23     public OFEncoder() {\r
24         LOGGER.debug("Creating OF13Encoder");\r
25     }\r
26     \r
27     @Override\r
28     protected void encode(ChannelHandlerContext ctx, OfHeader msg, ByteBuf out)\r
29             throws Exception {\r
30         LOGGER.debug("Encoding");\r
31         try {\r
32             SerializationFactory.messageToBuffer(msg.getVersion(), out, msg);\r
33         } catch(Exception e) {\r
34             LOGGER.error("Message serialization failed");\r
35             LOGGER.error(e.getMessage(), e);\r
36             return;\r
37         }\r
38         if (out.readableBytes() > 0) {\r
39             out.retain();\r
40             ctx.writeAndFlush(out);\r
41         } else {\r
42             LOGGER.warn("Translated buffer is empty");\r
43         }\r
44     }\r
45 \r
46 }\r