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.buffer.ByteBuf;
12 import io.netty.channel.ChannelHandlerContext;
13 import io.netty.handler.codec.MessageToByteEncoder;
14 import io.netty.util.concurrent.Future;
15 import org.opendaylight.openflowjava.protocol.impl.core.connection.MessageListenerWrapper;
16 import org.opendaylight.openflowjava.protocol.impl.serialization.SerializationFactory;
17 import org.opendaylight.openflowjava.statistics.CounterEventTypes;
18 import org.opendaylight.openflowjava.statistics.StatisticsCounters;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowModInput;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
24 * Transforms OpenFlow Protocol messages to POJOs.
26 * @author michal.polkorab
27 * @author timotej.kubas
29 public class OFEncoder extends MessageToByteEncoder<MessageListenerWrapper> {
31 private static final Logger LOG = LoggerFactory.getLogger(OFEncoder.class);
32 private SerializationFactory serializationFactory;
33 private final StatisticsCounters statisticsCounters;
36 statisticsCounters = StatisticsCounters.getInstance();
37 LOG.trace("Creating OFEncoder");
41 @SuppressWarnings("checkstyle:IllegalCatch")
42 protected void encode(final ChannelHandlerContext ctx, final MessageListenerWrapper wrapper, final ByteBuf out)
44 LOG.trace("Encoding");
46 serializationFactory.messageToBuffer(wrapper.getMsg().getVersion(), out, wrapper.getMsg());
47 if (wrapper.getMsg() instanceof FlowModInput) {
48 statisticsCounters.incrementCounter(CounterEventTypes.DS_FLOW_MODS_SENT);
50 statisticsCounters.incrementCounter(CounterEventTypes.DS_ENCODE_SUCCESS);
51 } catch (RuntimeException e) {
52 LOG.warn("Message serialization failed ", e);
53 statisticsCounters.incrementCounter(CounterEventTypes.DS_ENCODE_FAIL);
54 if (wrapper.getListener() != null) {
55 final Future<Void> newFailedFuture = ctx.newFailedFuture(e);
56 wrapper.getListener().operationComplete(newFailedFuture);
63 public void setSerializationFactory(final SerializationFactory serializationFactory) {
64 this.serializationFactory = serializationFactory;