35e22830746cda833b9ef18166dd536aa97d1b50
[openflowplugin.git] / openflowjava / 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 import java.util.List;
14 import org.opendaylight.openflowjava.protocol.impl.deserialization.DeserializationFactory;
15 import org.opendaylight.openflowjava.statistics.CounterEventTypes;
16 import org.opendaylight.openflowjava.statistics.StatisticsCounters;
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  *
25  * @author michal.polkorab
26  */
27 public class OFDecoder extends MessageToMessageDecoder<VersionMessageWrapper> {
28
29     private static final Logger LOG = LoggerFactory.getLogger(OFDecoder.class);
30     private final StatisticsCounters statisticsCounter;
31
32     // TODO: make this final?
33     private DeserializationFactory deserializationFactory;
34
35     public OFDecoder() {
36         LOG.trace("Creating OFDecoder");
37         // TODO: pass as argument
38         statisticsCounter = StatisticsCounters.getInstance();
39     }
40
41     @Override
42     @SuppressWarnings("checkstyle:IllegalCatch")
43     protected void decode(ChannelHandlerContext ctx, VersionMessageWrapper msg, List<Object> out) throws Exception {
44         statisticsCounter.incrementCounter(CounterEventTypes.US_RECEIVED_IN_OFJAVA);
45         if (LOG.isDebugEnabled()) {
46             LOG.debug("VersionMessageWrapper received");
47             LOG.debug("<< {}", ByteBufUtils.byteBufToHexString(msg.getMessageBuffer()));
48         }
49
50         try {
51             final DataObject dataObject = deserializationFactory.deserialize(msg.getMessageBuffer(),
52                     msg.getVersion());
53             if (dataObject == null) {
54                 LOG.warn("Translated POJO is null");
55                 statisticsCounter.incrementCounter(CounterEventTypes.US_DECODE_FAIL);
56             } else {
57                 out.add(dataObject);
58                 statisticsCounter.incrementCounter(CounterEventTypes.US_DECODE_SUCCESS);
59             }
60         } catch (RuntimeException e) {
61             LOG.warn("Message deserialization failed", e);
62             statisticsCounter.incrementCounter(CounterEventTypes.US_DECODE_FAIL);
63         } finally {
64             msg.getMessageBuffer().release();
65         }
66     }
67
68     public void setDeserializationFactory(DeserializationFactory deserializationFactory) {
69         this.deserializationFactory = deserializationFactory;
70     }
71
72 }