9c706a8848712ecf87a439054cec2dbda98c611d
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / core / OFDecoder.java
1 /*
2  * Copyright (c) 2013 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.handler.codec.MessageToMessageDecoder;
13
14 import java.util.List;
15
16 import org.opendaylight.openflowjava.protocol.impl.deserialization.DeserializationFactory;
17 import org.opendaylight.openflowjava.protocol.impl.util.ByteBufUtils;
18 import org.opendaylight.yangtools.yang.binding.DataObject;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 /**
23  * Transforms OpenFlow Protocol messages to POJOs
24  * @author michal.polkorab
25  */
26 public class OFDecoder extends MessageToMessageDecoder<VersionMessageWrapper> {
27
28     private static final Logger LOGGER = LoggerFactory.getLogger(OFDecoder.class);
29
30     /**
31      * Constructor of class
32      */
33     public OFDecoder() {
34         LOGGER.trace("Creating OF 1.3 Decoder");
35     }
36
37     @Override
38     protected void decode(ChannelHandlerContext ctx, VersionMessageWrapper msg,
39             List<Object> out) throws Exception {
40         if (LOGGER.isDebugEnabled()) {
41             LOGGER.debug("VersionMessageWrapper received");
42             LOGGER.debug("<< " + ByteBufUtils.byteBufToHexString(msg.getMessageBuffer()));
43         }
44         DataObject dataObject = null;
45         try {
46             dataObject = DeserializationFactory.bufferToMessage(msg.getMessageBuffer(),
47                     msg.getVersion());
48         } catch(Exception e) {
49             LOGGER.error("Message deserialization failed");
50             LOGGER.error(e.getMessage(), e);
51             return;
52         }
53         if (dataObject == null) {
54             LOGGER.warn("Translated POJO is null");
55             return;
56         }
57         msg.getMessageBuffer().release();
58         out.add(dataObject);
59     }
60 }