Removed some sonar warnings from bgp.
[bgpcep.git] / bgp / rib-impl / src / main / java / org / opendaylight / protocol / bgp / rib / impl / BGPByteToMessageDecoder.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.bgp.rib.impl;
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.opendaylight.protocol.bgp.parser.BGPDocumentedException;
18 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
19 import org.opendaylight.protocol.bgp.parser.spi.MessageRegistry;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 import com.google.common.base.Preconditions;
24
25 /**
26  *
27  */
28 final class BGPByteToMessageDecoder extends ByteToMessageDecoder {
29         private static final Logger LOG = LoggerFactory.getLogger(BGPByteToMessageDecoder.class);
30         private final MessageRegistry registry;
31
32         public BGPByteToMessageDecoder(final MessageRegistry registry) {
33                 this.registry = Preconditions.checkNotNull(registry);
34         }
35
36         @Override
37         protected void decode(final ChannelHandlerContext ctx, final ByteBuf in, final List<Object> out) throws Exception {
38                 if (in.readableBytes() == 0) {
39                         LOG.debug("No more content in incoming buffer.");
40                         return;
41                 }
42                 in.markReaderIndex();
43                 try {
44                         LOG.trace("Received to decode: {}", ByteBufUtil.hexDump(in));
45                         final byte[] bytes = new byte[in.readableBytes()];
46                         in.readBytes(bytes);
47                         out.add(this.registry.parseMessage(bytes));
48                 } catch (BGPParsingException | BGPDocumentedException e) {
49                         LOG.debug("Failed to decode protocol message", e);
50                         this.exceptionCaught(ctx, e);
51                 }
52                 in.discardReadBytes();
53         }
54 }