Mass replace CRLF->LF
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / core / OFDatagramPacketEncoder.java
1 /*
2  * Copyright (c) 2014 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 java.util.List;
12
13 import org.opendaylight.openflowjava.protocol.impl.core.connection.UdpMessageListenerWrapper;
14 import org.opendaylight.openflowjava.protocol.impl.serialization.SerializationFactory;
15 import org.slf4j.Logger;
16 import org.slf4j.LoggerFactory;
17
18 import io.netty.buffer.ByteBuf;
19 import io.netty.buffer.PooledByteBufAllocator;
20 import io.netty.channel.ChannelHandlerContext;
21 import io.netty.channel.socket.DatagramPacket;
22 import io.netty.handler.codec.MessageToMessageEncoder;
23 import io.netty.util.concurrent.Future;
24
25 /**
26  * @author michal.polkorab
27  *
28  */
29 public class OFDatagramPacketEncoder extends MessageToMessageEncoder<UdpMessageListenerWrapper> {
30
31     private static final Logger LOGGER = LoggerFactory.getLogger(OFDatagramPacketEncoder.class);
32     private SerializationFactory serializationFactory;
33
34     @Override
35     protected void encode(ChannelHandlerContext ctx,
36             UdpMessageListenerWrapper wrapper, List<Object> out) throws Exception {
37         LOGGER.trace("Encoding");
38         try {
39             ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
40             serializationFactory.messageToBuffer(wrapper.getMsg().getVersion(), buffer, wrapper.getMsg());
41             out.add(new DatagramPacket(buffer, wrapper.getAddress()));
42         } catch(Exception e) {
43             LOGGER.warn("Message serialization failed: {}", e.getMessage());
44             Future<Void> newFailedFuture = ctx.newFailedFuture(e);
45             wrapper.getListener().operationComplete(newFailedFuture);
46             return;
47         }
48     }
49
50     /**
51      * @param serializationFactory
52      */
53     public void setSerializationFactory(SerializationFactory serializationFactory) {
54         this.serializationFactory = serializationFactory;
55     }
56 }