Extensibility support (deserialization part)
[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     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         DataObject dataObject = null;
46         try {
47             dataObject = deserializationFactory.deserialize(msg.getMessageBuffer(),
48                     msg.getVersion());
49         } catch(Exception e) {
50             LOGGER.error("Message deserialization failed");
51             LOGGER.error(e.getMessage(), e);
52             return;
53         }
54         if (dataObject == null) {
55             LOGGER.warn("Translated POJO is null");
56             return;
57         }
58         msg.getMessageBuffer().release();
59         out.add(dataObject);
60     }
61
62     /**
63      * @param deserializationFactory
64      */
65     public void setDeserializationFactory(DeserializationFactory deserializationFactory) {
66         this.deserializationFactory = deserializationFactory;
67     }
68
69 }