2 * Copyright (c) 2014 Pantheon Technologies s.r.o. and others. All rights reserved.
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
9 package org.opendaylight.openflowjava.protocol.impl.core;
11 import java.util.List;
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;
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;
26 * @author michal.polkorab
29 public class OFDatagramPacketEncoder extends MessageToMessageEncoder<UdpMessageListenerWrapper> {
31 private static final Logger LOGGER = LoggerFactory.getLogger(OFDatagramPacketEncoder.class);
32 private SerializationFactory serializationFactory;
35 protected void encode(ChannelHandlerContext ctx,
36 UdpMessageListenerWrapper wrapper, List<Object> out) throws Exception {
37 LOGGER.trace("Encoding");
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);
51 * @param serializationFactory
53 public void setSerializationFactory(SerializationFactory serializationFactory) {
54 this.serializationFactory = serializationFactory;