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