f4240bcc8f2fc72e245c1423cbd71bc1285ede2a
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / core / OFDatagramPacketDecoder.java
1 /*\r
2  * Copyright (c) 2014 Pantheon Technologies s.r.o. and others. All rights reserved.\r
3  *\r
4  * This program and the accompanying materials are made available under the\r
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,\r
6  * and is available at http://www.eclipse.org/legal/epl-v10.html\r
7  */\r
8 \r
9 package org.opendaylight.openflowjava.protocol.impl.core;\r
10 \r
11 import io.netty.channel.ChannelHandlerContext;\r
12 import io.netty.channel.SimpleChannelInboundHandler;\r
13 \r
14 import org.opendaylight.openflowjava.protocol.impl.core.connection.MessageConsumer;\r
15 import org.opendaylight.openflowjava.protocol.impl.deserialization.DeserializationFactory;\r
16 import org.opendaylight.openflowjava.util.ByteBufUtils;\r
17 import org.opendaylight.yangtools.yang.binding.DataObject;\r
18 import org.slf4j.Logger;\r
19 import org.slf4j.LoggerFactory;\r
20 \r
21 /**\r
22  * @author michal.polkorab\r
23  *\r
24  */\r
25 public class OFDatagramPacketDecoder extends SimpleChannelInboundHandler<VersionMessageUdpWrapper>{\r
26 \r
27     private static final Logger LOGGER = LoggerFactory.getLogger(OFDatagramPacketDecoder.class);\r
28     private DeserializationFactory deserializationFactory;\r
29 \r
30     @Override\r
31     public void channelRead0(ChannelHandlerContext ctx, VersionMessageUdpWrapper msg)\r
32             throws Exception {\r
33         if (LOGGER.isDebugEnabled()) {\r
34                 LOGGER.debug("UdpVersionMessageWrapper received");\r
35                 LOGGER.debug("<< " + ByteBufUtils.byteBufToHexString(msg.getMessageBuffer()));\r
36         }\r
37         DataObject dataObject = null;\r
38         try {\r
39             dataObject = deserializationFactory.deserialize(msg.getMessageBuffer(),msg.getVersion());\r
40             if (dataObject == null) {\r
41                 LOGGER.warn("Translated POJO is null");\r
42             } else {\r
43                 MessageConsumer consumer = UdpConnectionMap.getMessageConsumer(msg.getAddress());\r
44                 consumer.consume(dataObject);\r
45             }\r
46         } catch(Exception e) {\r
47             LOGGER.warn("Message deserialization failed");\r
48             LOGGER.warn(e.getMessage(), e);\r
49             // TODO: delegate exception to allow easier deserialization\r
50             // debugging / deserialization problem awareness\r
51         } finally {\r
52             msg.getMessageBuffer().release();\r
53         }\r
54     }\r
55 \r
56     /**\r
57      * @param deserializationFactory\r
58      */\r
59     public void setDeserializationFactory(DeserializationFactory deserializationFactory) {\r
60         this.deserializationFactory = deserializationFactory;\r
61     }\r
62 }