Mass replace CRLF->LF
[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
14 import org.opendaylight.openflowjava.protocol.impl.core.connection.MessageConsumer;
15 import org.opendaylight.openflowjava.protocol.impl.deserialization.DeserializationFactory;
16 import org.opendaylight.openflowjava.util.ByteBufUtils;
17 import org.opendaylight.yangtools.yang.binding.DataObject;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 /**
22  * @author michal.polkorab
23  *
24  */
25 public class OFDatagramPacketDecoder extends SimpleChannelInboundHandler<VersionMessageUdpWrapper>{
26
27     private static final Logger LOGGER = LoggerFactory.getLogger(OFDatagramPacketDecoder.class);
28     private DeserializationFactory deserializationFactory;
29
30     @Override
31     public void channelRead0(ChannelHandlerContext ctx, VersionMessageUdpWrapper msg)
32             throws Exception {
33         if (LOGGER.isDebugEnabled()) {
34                 LOGGER.debug("UdpVersionMessageWrapper received");
35                 LOGGER.debug("<< " + ByteBufUtils.byteBufToHexString(msg.getMessageBuffer()));
36         }
37         DataObject dataObject = null;
38         try {
39             dataObject = deserializationFactory.deserialize(msg.getMessageBuffer(),msg.getVersion());
40             if (dataObject == null) {
41                 LOGGER.warn("Translated POJO is null");
42             } else {
43                 MessageConsumer consumer = UdpConnectionMap.getMessageConsumer(msg.getAddress());
44                 consumer.consume(dataObject);
45             }
46         } catch(Exception e) {
47             LOGGER.warn("Message deserialization failed");
48             LOGGER.warn(e.getMessage(), e);
49             // TODO: delegate exception to allow easier deserialization
50             // debugging / deserialization problem awareness
51         } finally {
52             msg.getMessageBuffer().release();
53         }
54     }
55
56     /**
57      * @param deserializationFactory
58      */
59     public void setDeserializationFactory(DeserializationFactory deserializationFactory) {
60         this.deserializationFactory = deserializationFactory;
61     }
62 }