Instruction experimenterId fix
[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 LOGGER = LoggerFactory.getLogger(OFDecoder.class);
31     private DeserializationFactory deserializationFactory;
32     private StatisticsCounters statisticsCounter;
33     /**
34      * Constructor of class
35      */
36     public OFDecoder() {
37         LOGGER.trace("Creating OF 1.3 Decoder");
38         statisticsCounter = StatisticsCounters.getInstance();
39     }
40
41     @Override
42     protected void decode(ChannelHandlerContext ctx, VersionMessageWrapper msg,
43             List<Object> out) throws Exception {
44         statisticsCounter.incrementCounter(CounterEventTypes.US_RECEIVED_IN_OFJAVA);
45         if (LOGGER.isDebugEnabled()) {
46             LOGGER.debug("VersionMessageWrapper received");
47             LOGGER.debug("<< " + ByteBufUtils.byteBufToHexString(msg.getMessageBuffer()));
48         }
49
50         DataObject dataObject = null;
51         try {
52             dataObject = deserializationFactory.deserialize(msg.getMessageBuffer(),
53                     msg.getVersion());
54             if (dataObject == null) {
55                 LOGGER.warn("Translated POJO is null");
56                 statisticsCounter.incrementCounter(CounterEventTypes.US_DECODE_FAIL);
57             } else {
58                 out.add(dataObject);
59                 statisticsCounter.incrementCounter(CounterEventTypes.US_DECODE_SUCCESS);
60             }
61         } catch(Exception e) {
62             LOGGER.warn("Message deserialization failed");
63             LOGGER.warn(e.getMessage(), e);
64             statisticsCounter.incrementCounter(CounterEventTypes.US_DECODE_FAIL);
65         } finally {
66             msg.getMessageBuffer().release();
67         }
68     }
69
70     /**
71      * @param deserializationFactory
72      */
73     public void setDeserializationFactory(DeserializationFactory deserializationFactory) {
74         this.deserializationFactory = deserializationFactory;
75     }
76
77 }