Copyright update
[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
15 import org.opendaylight.openflowjava.protocol.impl.serialization.SerializationFactory;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19
20 /**
21  * Transforms OpenFlow Protocol messages to POJOs
22  * @author michal.polkorab
23  * @author timotej.kubas
24  */
25 public class OFEncoder extends MessageToByteEncoder<OfHeader> {
26
27     private static final Logger LOGGER = LoggerFactory.getLogger(OFEncoder.class);
28     
29     /** Constructor of class */
30     public OFEncoder() {
31         LOGGER.debug("Creating OF13Encoder");
32     }
33     
34     @Override
35     protected void encode(ChannelHandlerContext ctx, OfHeader msg, ByteBuf out)
36             throws Exception {
37         LOGGER.debug("Encoding");
38         try {
39             SerializationFactory.messageToBuffer(msg.getVersion(), out, msg);
40         } catch(Exception e) {
41             LOGGER.error("Message serialization failed");
42             LOGGER.error(e.getMessage(), e);
43             return;
44         }
45         if (out.readableBytes() > 0) {
46             out.retain();
47             ctx.writeAndFlush(out);
48         } else {
49             LOGGER.warn("Translated buffer is empty");
50         }
51     }
52
53 }