450eb4bb1757909d407ee06d424289cbea497e2e
[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.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     private DeserializationFactory deserializationFactory;
30
31     /**
32      * Constructor of class
33      */
34     public OFDecoder() {
35         LOGGER.trace("Creating OF 1.3 Decoder");
36     }
37
38     @Override
39     protected void decode(ChannelHandlerContext ctx, VersionMessageWrapper msg,
40             List<Object> out) throws Exception {
41         if (LOGGER.isDebugEnabled()) {
42             LOGGER.debug("VersionMessageWrapper received");
43             LOGGER.debug("<< " + ByteBufUtils.byteBufToHexString(msg.getMessageBuffer()));
44         }
45
46         DataObject dataObject = null;
47         try {
48             dataObject = deserializationFactory.deserialize(msg.getMessageBuffer(),
49                     msg.getVersion());
50             if (dataObject == null) {
51                 LOGGER.warn("Translated POJO is null");
52             } else {
53                 out.add(dataObject);
54             }
55         } catch(Exception e) {
56             LOGGER.error("Message deserialization failed");
57             LOGGER.error(e.getMessage(), e);
58             // TODO: delegate exception to allow easier deserialization
59             // debugging / deserialization problem awareness
60         } finally {
61             msg.getMessageBuffer().release();
62         }
63     }
64
65     /**
66      * @param deserializationFactory
67      */
68     public void setDeserializationFactory(DeserializationFactory deserializationFactory) {
69         this.deserializationFactory = deserializationFactory;
70     }
71
72 }