Statistics collection - initial change
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / core / OFEncoder.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.buffer.ByteBuf;
12 import io.netty.channel.ChannelHandlerContext;
13 import io.netty.handler.codec.MessageToByteEncoder;
14 import io.netty.util.concurrent.Future;
15
16 import org.opendaylight.openflowjava.protocol.impl.core.connection.MessageListenerWrapper;
17 import org.opendaylight.openflowjava.protocol.impl.serialization.SerializationFactory;
18 import org.opendaylight.openflowjava.statistics.CounterEventTypes;
19 import org.opendaylight.openflowjava.statistics.StatisticsCounters;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 /**
24  * Transforms OpenFlow Protocol messages to POJOs
25  * @author michal.polkorab
26  * @author timotej.kubas
27  */
28 public class OFEncoder extends MessageToByteEncoder<MessageListenerWrapper> {
29
30     private static final Logger LOGGER = LoggerFactory.getLogger(OFEncoder.class);
31     private SerializationFactory serializationFactory;
32     private StatisticsCounters statisticsCounters;
33     
34     /** Constructor of class */
35     public OFEncoder() {
36         statisticsCounters = StatisticsCounters.getInstance();
37         LOGGER.trace("Creating OF13Encoder");
38     }
39
40     @Override
41     protected void encode(ChannelHandlerContext ctx, MessageListenerWrapper wrapper, ByteBuf out)
42             throws Exception {
43         LOGGER.trace("Encoding");
44         try {
45             serializationFactory.messageToBuffer(wrapper.getMsg().getVersion(), out, wrapper.getMsg());
46             statisticsCounters.incrementCounter(CounterEventTypes.DS_ENCODE_SUCCESS);
47         } catch(Exception e) {
48             LOGGER.warn("Message serialization failed ", e);
49             statisticsCounters.incrementCounter(CounterEventTypes.DS_ENCODE_FAIL);
50             Future<Void> newFailedFuture = ctx.newFailedFuture(e);
51             wrapper.getListener().operationComplete(newFailedFuture);
52             out.clear();
53             return;
54         }
55     }
56
57     /**
58      * @param serializationFactory
59      */
60     public void setSerializationFactory(SerializationFactory serializationFactory) {
61         this.serializationFactory = serializationFactory;
62     }
63
64 }