X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=openflow-protocol-impl%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fopenflowjava%2Fprotocol%2Fimpl%2Fcore%2FOFEncoder.java;h=075b99da2bb30bddaca8c1fc4707cdd18d44b1eb;hb=2b5094b8ffd0e06dfaad372437e01ff1bd194623;hp=9a0cdbb1c9ae3960a0acb9a6ac05ac6b63b0c150;hpb=28f200e58797a5676ca55b0f7134e17ca0664111;p=openflowjava.git diff --git a/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/core/OFEncoder.java b/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/core/OFEncoder.java index 9a0cdbb1..075b99da 100644 --- a/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/core/OFEncoder.java +++ b/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/core/OFEncoder.java @@ -1,46 +1,68 @@ -/* Copyright (C)2013 Pantheon Technologies, s.r.o. All rights reserved. */ -package org.opendaylight.openflowjava.protocol.impl.core; - -import io.netty.buffer.ByteBuf; -import io.netty.channel.ChannelHandlerContext; -import io.netty.handler.codec.MessageToByteEncoder; - -import org.opendaylight.openflowjava.protocol.impl.serialization.SerializationFactory; -import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * Transforms OpenFlow Protocol messages to POJOs - * @author michal.polkorab - * @author timotej.kubas - */ -public class OFEncoder extends MessageToByteEncoder { - - private static final Logger LOGGER = LoggerFactory.getLogger(OFEncoder.class); - - /** Constructor of class */ - public OFEncoder() { - LOGGER.debug("Creating OF13Encoder"); - } - - @Override - protected void encode(ChannelHandlerContext ctx, OfHeader msg, ByteBuf out) - throws Exception { - LOGGER.debug("Encoding"); - try { - SerializationFactory.messageToBuffer(msg.getVersion(), out, msg); - } catch(Exception e) { - LOGGER.error("Message serialization failed"); - LOGGER.error(e.getMessage(), e); - return; - } - if (out.readableBytes() > 0) { - out.retain(); - ctx.writeAndFlush(out); - } else { - LOGGER.warn("Translated buffer is empty"); - } - } - -} +/* + * Copyright (c) 2013 Pantheon Technologies s.r.o. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ + +package org.opendaylight.openflowjava.protocol.impl.core; + +import io.netty.buffer.ByteBuf; +import io.netty.channel.ChannelHandlerContext; +import io.netty.handler.codec.MessageToByteEncoder; +import io.netty.util.concurrent.Future; + +import org.opendaylight.openflowjava.protocol.impl.core.connection.MessageListenerWrapper; +import org.opendaylight.openflowjava.protocol.impl.serialization.SerializationFactory; +import org.opendaylight.openflowjava.statistics.CounterEventTypes; +import org.opendaylight.openflowjava.statistics.StatisticsCounters; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowModInput; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Transforms OpenFlow Protocol messages to POJOs + * @author michal.polkorab + * @author timotej.kubas + */ +public class OFEncoder extends MessageToByteEncoder { + + private static final Logger LOGGER = LoggerFactory.getLogger(OFEncoder.class); + private SerializationFactory serializationFactory; + private StatisticsCounters statisticsCounters; + + /** Constructor of class */ + public OFEncoder() { + statisticsCounters = StatisticsCounters.getInstance(); + LOGGER.trace("Creating OF13Encoder"); + } + + @Override + protected void encode(ChannelHandlerContext ctx, MessageListenerWrapper wrapper, ByteBuf out) + throws Exception { + LOGGER.trace("Encoding"); + try { + serializationFactory.messageToBuffer(wrapper.getMsg().getVersion(), out, wrapper.getMsg()); + if(wrapper.getMsg() instanceof FlowModInput){ + statisticsCounters.incrementCounter(CounterEventTypes.DS_FLOW_MODS_SENT); + } + statisticsCounters.incrementCounter(CounterEventTypes.DS_ENCODE_SUCCESS); + } catch(Exception e) { + LOGGER.warn("Message serialization failed ", e); + statisticsCounters.incrementCounter(CounterEventTypes.DS_ENCODE_FAIL); + Future newFailedFuture = ctx.newFailedFuture(e); + wrapper.getListener().operationComplete(newFailedFuture); + out.clear(); + return; + } + } + + /** + * @param serializationFactory + */ + public void setSerializationFactory(SerializationFactory serializationFactory) { + this.serializationFactory = serializationFactory; + } + +}