2 * Copyright (c) 2013 Cisco Systems, Inc. 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
8 package org.opendaylight.protocol.framework;
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;
15 import java.util.List;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
20 import com.google.common.base.Preconditions;
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.
27 public final class ProtocolMessageDecoder<T> extends ByteToMessageDecoder {
29 private static final Logger LOG = LoggerFactory.getLogger(ProtocolMessageDecoder.class);
31 private final ProtocolMessageFactory<T> factory;
33 public ProtocolMessageDecoder(final ProtocolMessageFactory<T> factory) {
34 this.factory = Preconditions.checkNotNull(factory);
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.");
45 LOG.trace("Received to decode: {}", ByteBufUtil.hexDump(in));
46 final byte[] bytes = new byte[in.readableBytes()];
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);
53 in.discardReadBytes();