Replace Preconditions.CheckNotNull per RequireNonNull
[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 /**
27  *
28  */
29 final class BGPByteToMessageDecoder extends ByteToMessageDecoder {
30     private static final Logger LOG = LoggerFactory.getLogger(BGPByteToMessageDecoder.class);
31     private final MessageRegistry registry;
32     private final PeerSpecificParserConstraintProvider constraints;
33
34     public BGPByteToMessageDecoder(final MessageRegistry registry) {
35         this.constraints = new PeerSpecificParserConstraintImpl();
36         this.registry = requireNonNull(registry);
37     }
38
39     public <T extends PeerConstraint> boolean addDecoderConstraint(final Class<T> classType, final T peerConstraint) {
40         return this.constraints.addPeerConstraint(classType, peerConstraint);
41     }
42
43     @Override
44     protected void decode(final ChannelHandlerContext ctx, final ByteBuf in, final List<Object> out) throws BGPDocumentedException,
45             BGPParsingException {
46         if (in.isReadable()) {
47             if (LOG.isTraceEnabled()) {
48                 LOG.trace("Received to decode: {}", ByteBufUtil.hexDump(in));
49             }
50             out.add(this.registry.parseMessage(in, this.constraints));
51         } else {
52             LOG.trace("No more content in incoming buffer.");
53         }
54     }
55 }