All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.opendaylight.protocol.bgp.rib.impl.BGPByteToMessageDecoder Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 */
package org.opendaylight.protocol.bgp.rib.impl;

import static java.util.Objects.requireNonNull;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;
import java.util.List;
import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
import org.opendaylight.protocol.bgp.parser.BGPParsingException;
import org.opendaylight.protocol.bgp.parser.spi.MessageRegistry;
import org.opendaylight.protocol.bgp.parser.spi.PeerConstraint;
import org.opendaylight.protocol.bgp.parser.spi.PeerSpecificParserConstraintProvider;
import org.opendaylight.protocol.bgp.parser.spi.pojo.PeerSpecificParserConstraintImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

final class BGPByteToMessageDecoder extends ByteToMessageDecoder {
    private static final Logger LOG = LoggerFactory.getLogger(BGPByteToMessageDecoder.class);
    private final MessageRegistry registry;
    private final PeerSpecificParserConstraintProvider constraints;

    BGPByteToMessageDecoder(final MessageRegistry registry) {
        this.constraints = new PeerSpecificParserConstraintImpl();
        this.registry = requireNonNull(registry);
    }

     boolean addDecoderConstraint(final Class classType, final T peerConstraint) {
        return this.constraints.addPeerConstraint(classType, peerConstraint);
    }

    @Override
    protected void decode(final ChannelHandlerContext ctx, final ByteBuf in, final List out)
            throws BGPDocumentedException, BGPParsingException {
        if (in.isReadable()) {
            if (LOG.isTraceEnabled()) {
                LOG.trace("Received to decode: {}", ByteBufUtil.hexDump(in));
            }
            out.add(this.registry.parseMessage(in, this.constraints));
        } else {
            LOG.trace("No more content in incoming buffer.");
        }
    }
}