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 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;
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;
33 public BGPByteToMessageDecoder(final MessageRegistry registry) {
34 this.constraints = new PeerSpecificParserConstraintImpl();
35 this.registry = Preconditions.checkNotNull(registry);
38 public <T extends PeerConstraint> boolean addDecoderConstraint(final Class<T> classType, final T peerConstraint) {
39 return this.constraints.addPeerConstraint(classType, peerConstraint);
43 protected void decode(final ChannelHandlerContext ctx, final ByteBuf in, final List<Object> out) throws BGPDocumentedException,
45 if (in.isReadable()) {
46 if (LOG.isTraceEnabled()) {
47 LOG.trace("Received to decode: {}", ByteBufUtil.hexDump(in));
49 out.add(this.registry.parseMessage(in, this.constraints));
51 LOG.trace("No more content in incoming buffer.");