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