Fix a few eclipse-reported warnings
[controller.git] / opendaylight / commons / protocol-framework / src / main / java / org / opendaylight / protocol / framework / ProtocolMessageDecoder.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. 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 package org.opendaylight.protocol.framework;
9
10 import io.netty.buffer.ByteBuf;
11 import io.netty.buffer.ByteBufUtil;
12 import io.netty.channel.ChannelHandlerContext;
13 import io.netty.handler.codec.ByteToMessageDecoder;
14
15 import java.util.List;
16
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19
20 import com.google.common.base.Preconditions;
21
22 /**
23  * @deprecated This is an adaptor class for turning ProtocolMessageFactory into Netty decoder. Use Netty-provided
24  *             classes directly, by subclassing {@link io.netty.handler.codec.ByteToMessageDecoder} or similar instead.
25  */
26 @Deprecated
27 public final class ProtocolMessageDecoder<T> extends ByteToMessageDecoder {
28
29     private static final Logger LOG = LoggerFactory.getLogger(ProtocolMessageDecoder.class);
30
31     private final ProtocolMessageFactory<T> factory;
32
33     public ProtocolMessageDecoder(final ProtocolMessageFactory<T> factory) {
34         this.factory = Preconditions.checkNotNull(factory);
35     }
36
37     @Override
38     protected void decode(final ChannelHandlerContext ctx, final ByteBuf in, final List<Object> out) throws Exception {
39         if (in.readableBytes() == 0) {
40             LOG.debug("No more content in incoming buffer.");
41             return;
42         }
43         in.markReaderIndex();
44         try {
45             LOG.trace("Received to decode: {}", ByteBufUtil.hexDump(in));
46             final byte[] bytes = new byte[in.readableBytes()];
47             in.readBytes(bytes);
48             out.add(this.factory.parse(bytes));
49         } catch (DeserializerException | DocumentedException e) {
50             LOG.debug("Failed to decode protocol message", e);
51             this.exceptionCaught(ctx, e);
52         }
53         in.discardReadBytes();
54     }
55 }