Fix checkstyle complains
[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 static java.util.Objects.requireNonNull;
11
12 import io.netty.buffer.ByteBuf;
13 import io.netty.buffer.ByteBufUtil;
14 import io.netty.channel.ChannelHandlerContext;
15 import io.netty.handler.codec.ByteToMessageDecoder;
16 import java.util.List;
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.opendaylight.protocol.bgp.parser.spi.PeerConstraint;
21 import org.opendaylight.protocol.bgp.parser.spi.PeerSpecificParserConstraintProvider;
22 import org.opendaylight.protocol.bgp.parser.spi.pojo.PeerSpecificParserConstraintImpl;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 final class BGPByteToMessageDecoder extends ByteToMessageDecoder {
27     private static final Logger LOG = LoggerFactory.getLogger(BGPByteToMessageDecoder.class);
28     private final MessageRegistry registry;
29     private final PeerSpecificParserConstraintProvider constraints;
30
31     BGPByteToMessageDecoder(final MessageRegistry registry) {
32         this.constraints = new PeerSpecificParserConstraintImpl();
33         this.registry = requireNonNull(registry);
34     }
35
36     <T extends PeerConstraint> boolean addDecoderConstraint(final Class<T> classType, final T peerConstraint) {
37         return this.constraints.addPeerConstraint(classType, peerConstraint);
38     }
39
40     @Override
41     protected void decode(final ChannelHandlerContext ctx, final ByteBuf in, final List<Object> out)
42             throws BGPDocumentedException, BGPParsingException {
43         if (in.isReadable()) {
44             if (LOG.isTraceEnabled()) {
45                 LOG.trace("Received to decode: {}", ByteBufUtil.hexDump(in));
46             }
47             out.add(this.registry.parseMessage(in, this.constraints));
48         } else {
49             LOG.trace("No more content in incoming buffer.");
50         }
51     }
52 }