Bug 2245 - Fixed Avoid cycle between java packages
[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.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 /**
22  * Transforms OpenFlow Protocol messages to POJOs
23  * @author michal.polkorab
24  * @author timotej.kubas
25  */
26 public class OFEncoder extends MessageToByteEncoder<MessageListenerWrapper> {
27
28     private static final Logger LOGGER = LoggerFactory.getLogger(OFEncoder.class);
29     private SerializationFactory serializationFactory;
30
31     /** Constructor of class */
32     public OFEncoder() {
33         LOGGER.trace("Creating OF13Encoder");
34     }
35
36     @Override
37     protected void encode(ChannelHandlerContext ctx, MessageListenerWrapper wrapper, ByteBuf out)
38             throws Exception {
39         LOGGER.trace("Encoding");
40         try {
41             serializationFactory.messageToBuffer(wrapper.getMsg().getVersion(), out, wrapper.getMsg());
42         } catch(Exception e) {
43             LOGGER.warn("Message serialization failed ", e);
44             Future<Void> newFailedFuture = ctx.newFailedFuture(e);
45             wrapper.getListener().operationComplete(newFailedFuture);
46             out.clear();
47             return;
48         }
49     }
50
51     /**
52      * @param serializationFactory
53      */
54     public void setSerializationFactory(SerializationFactory serializationFactory) {
55         this.serializationFactory = serializationFactory;
56     }
57
58 }