Upgrade ietf-{inet,yang}-types to 2013-07-15
[bgpcep.git] / bgp / bmp-impl / src / main / java / org / opendaylight / protocol / bmp / impl / BmpByteToMessageDecoder.java
1 /*
2  * Copyright (c) 2015 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
9 package org.opendaylight.protocol.bmp.impl;
10
11 import com.google.common.base.Preconditions;
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.bmp.spi.parser.BmpDeserializationException;
18 import org.opendaylight.protocol.bmp.spi.registry.BmpMessageRegistry;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 public class BmpByteToMessageDecoder extends ByteToMessageDecoder {
23
24     private static final Logger LOG = LoggerFactory.getLogger(BmpByteToMessageDecoder.class);
25     private final BmpMessageRegistry registry;
26
27     public BmpByteToMessageDecoder(final BmpMessageRegistry registry) {
28         this.registry = Preconditions.checkNotNull(registry);
29     }
30
31     @Override
32     protected void decode(final ChannelHandlerContext ctx, final ByteBuf in, final List<Object> out) throws BmpDeserializationException {
33         if (in.isReadable()) {
34             LOG.trace("Received to decode: {}", ByteBufUtil.hexDump(in));
35             out.add(this.registry.parseMessage(in));
36         } else {
37             LOG.trace("No more content in incoming buffer.");
38         }
39     }
40
41 }