2 * Copyright (c) 2013 Pantheon Technologies s.r.o. and others. All rights reserved.
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
9 package org.opendaylight.openflowjava.protocol.impl.core;
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;
23 * Transforms OpenFlow Protocol messages to POJOs.
25 * @author michal.polkorab
27 public class OFDecoder extends MessageToMessageDecoder<VersionMessageWrapper> {
29 private static final Logger LOG = LoggerFactory.getLogger(OFDecoder.class);
30 private final StatisticsCounters statisticsCounter;
32 // TODO: make this final?
33 private DeserializationFactory deserializationFactory;
36 LOG.trace("Creating OFDecoder");
37 // TODO: pass as argument
38 statisticsCounter = StatisticsCounters.getInstance();
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()));
51 final DataObject dataObject = deserializationFactory.deserialize(msg.getMessageBuffer(),
53 if (dataObject == null) {
54 LOG.warn("Translated POJO is null");
55 statisticsCounter.incrementCounter(CounterEventTypes.US_DECODE_FAIL);
58 statisticsCounter.incrementCounter(CounterEventTypes.US_DECODE_SUCCESS);
60 } catch (RuntimeException e) {
61 LOG.warn("Message deserialization failed", e);
62 statisticsCounter.incrementCounter(CounterEventTypes.US_DECODE_FAIL);
64 msg.getMessageBuffer().release();
68 public void setDeserializationFactory(DeserializationFactory deserializationFactory) {
69 this.deserializationFactory = deserializationFactory;