Bug 4827 - BGP add-path unit tests
[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 com.google.common.base.Preconditions;
11 import io.netty.buffer.ByteBuf;
12 import io.netty.buffer.ByteBufUtil;
13 import io.netty.channel.ChannelHandlerContext;
14 import io.netty.handler.codec.ByteToMessageDecoder;
15 import java.util.List;
16 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
17 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
18 import org.opendaylight.protocol.bgp.parser.spi.MessageRegistry;
19 import org.opendaylight.protocol.bgp.parser.spi.PeerConstraint;
20 import org.opendaylight.protocol.bgp.parser.spi.PeerSpecificParserConstraintProvider;
21 import org.opendaylight.protocol.bgp.parser.spi.pojo.PeerSpecificParserConstraintImpl;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
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     private final PeerSpecificParserConstraintProvider constraints;
32
33     public BGPByteToMessageDecoder(final MessageRegistry registry) {
34         this.constraints = new PeerSpecificParserConstraintImpl();
35         this.registry = Preconditions.checkNotNull(registry);
36     }
37
38     public <T extends PeerConstraint> boolean addDecoderConstraint(final Class<T> classType, final T peerConstraint) {
39         return this.constraints.addPeerConstraint(classType, peerConstraint);
40     }
41
42     @Override
43     protected void decode(final ChannelHandlerContext ctx, final ByteBuf in, final List<Object> out) throws BGPDocumentedException,
44             BGPParsingException {
45         if (in.isReadable()) {
46             if (LOG.isTraceEnabled()) {
47                 LOG.trace("Received to decode: {}", ByteBufUtil.hexDump(in));
48             }
49             out.add(this.registry.parseMessage(in, this.constraints));
50         } else {
51             LOG.trace("No more content in incoming buffer.");
52         }
53     }
54 }