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.bgp.rib.impl;
10 import static java.util.Objects.requireNonNull;
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;
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;
31 BGPByteToMessageDecoder(final MessageRegistry registry) {
32 this.constraints = new PeerSpecificParserConstraintImpl();
33 this.registry = requireNonNull(registry);
36 <T extends PeerConstraint> boolean addDecoderConstraint(final Class<T> classType, final T peerConstraint) {
37 return this.constraints.addPeerConstraint(classType, peerConstraint);
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));
47 out.add(this.registry.parseMessage(in, this.constraints));
49 LOG.trace("No more content in incoming buffer.");