ec1f43d329e1efa23ea0171607fcaf63642a4319
[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.statistics.CounterEventTypes;
18 import org.opendaylight.openflowjava.statistics.StatisticsCounters;
19 import org.opendaylight.openflowjava.util.ByteBufUtils;
20 import org.opendaylight.yangtools.yang.binding.DataObject;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 /**
25  * Transforms OpenFlow Protocol messages to POJOs
26  * @author michal.polkorab
27  */
28 public class OFDecoder extends MessageToMessageDecoder<VersionMessageWrapper> {
29
30     private static final Logger LOG = LoggerFactory.getLogger(OFDecoder.class);
31     private final StatisticsCounters statisticsCounter;
32
33     // TODO: make this final?
34     private DeserializationFactory deserializationFactory;
35
36     /**
37      * Constructor of class
38      */
39     public OFDecoder() {
40         LOG.trace("Creating OF 1.3 Decoder");
41         // TODO: pass as argument
42         statisticsCounter = StatisticsCounters.getInstance();
43     }
44
45     @Override
46     protected void decode(ChannelHandlerContext ctx, VersionMessageWrapper msg,
47             List<Object> out) throws Exception {
48         statisticsCounter.incrementCounter(CounterEventTypes.US_RECEIVED_IN_OFJAVA);
49         if (LOG.isDebugEnabled()) {
50             LOG.debug("VersionMessageWrapper received");
51             LOG.debug("<< {}", ByteBufUtils.byteBufToHexString(msg.getMessageBuffer()));
52         }
53
54         try {
55             final DataObject dataObject = deserializationFactory.deserialize(msg.getMessageBuffer(),
56                     msg.getVersion());
57             if (dataObject == null) {
58                 LOG.warn("Translated POJO is null");
59                 statisticsCounter.incrementCounter(CounterEventTypes.US_DECODE_FAIL);
60             } else {
61                 out.add(dataObject);
62                 statisticsCounter.incrementCounter(CounterEventTypes.US_DECODE_SUCCESS);
63             }
64         } catch (Exception e) {
65             LOG.warn("Message deserialization failed", e);
66             statisticsCounter.incrementCounter(CounterEventTypes.US_DECODE_FAIL);
67         } finally {
68             msg.getMessageBuffer().release();
69         }
70     }
71
72     /**
73      * @param deserializationFactory
74      */
75     public void setDeserializationFactory(DeserializationFactory deserializationFactory) {
76         this.deserializationFactory = deserializationFactory;
77     }
78
79 }