5a739497c3703a7dbfb2416520f351a57323a1bb
[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.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowModInput;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 /**
25  * Transforms OpenFlow Protocol messages to POJOs
26  * @author michal.polkorab
27  * @author timotej.kubas
28  */
29 public class OFEncoder extends MessageToByteEncoder<MessageListenerWrapper> {
30
31     private static final Logger LOGGER = LoggerFactory.getLogger(OFEncoder.class);
32     private SerializationFactory serializationFactory;
33     private StatisticsCounters statisticsCounters;
34     
35     /** Constructor of class */
36     public OFEncoder() {
37         statisticsCounters = StatisticsCounters.getInstance();
38         LOGGER.trace("Creating OF13Encoder");
39     }
40
41     @Override
42     protected void encode(ChannelHandlerContext ctx, MessageListenerWrapper wrapper, ByteBuf out)
43             throws Exception {
44         LOGGER.trace("Encoding");
45         try {
46             serializationFactory.messageToBuffer(wrapper.getMsg().getVersion(), out, wrapper.getMsg());
47             if(wrapper.getMsg() instanceof FlowModInput){
48                 statisticsCounters.incrementCounter(CounterEventTypes.DS_FLOW_MODS_SENT);
49             }
50             statisticsCounters.incrementCounter(CounterEventTypes.DS_ENCODE_SUCCESS);
51         } catch(Exception e) {
52             LOGGER.warn("Message serialization failed ", e);
53             statisticsCounters.incrementCounter(CounterEventTypes.DS_ENCODE_FAIL);
54             Future<Void> newFailedFuture = ctx.newFailedFuture(e);
55             wrapper.getListener().operationComplete(newFailedFuture);
56             out.clear();
57             return;
58         }
59     }
60
61     /**
62      * @param serializationFactory
63      */
64     public void setSerializationFactory(SerializationFactory serializationFactory) {
65         this.serializationFactory = serializationFactory;
66     }
67
68 }