2 * Copyright (c) 2015 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
9 package org.opendaylight.protocol.bmp.spi.parser;
11 import static java.util.Objects.requireNonNull;
13 import com.google.common.base.Preconditions;
14 import io.netty.buffer.ByteBuf;
15 import io.netty.buffer.ByteBufUtil;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev200120.Tlv;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
20 public abstract class AbstractBmpMessageWithTlvParser<T> extends AbstractBmpMessageParser {
22 private static final Logger LOG = LoggerFactory.getLogger(AbstractBmpMessageWithTlvParser.class);
24 private final BmpTlvRegistry tlvRegistry;
26 public AbstractBmpMessageWithTlvParser(final BmpTlvRegistry tlvRegistry) {
27 this.tlvRegistry = tlvRegistry;
30 protected final void parseTlvs(final T builder, final ByteBuf bytes) throws BmpDeserializationException {
31 Preconditions.checkArgument(bytes != null, "Array of bytes is mandatory. Can't be null.");
32 if (!bytes.isReadable()) {
35 while (bytes.isReadable()) {
36 final int type = bytes.readUnsignedShort();
37 final int length = bytes.readUnsignedShort();
38 if (length > bytes.readableBytes()) {
39 throw new BmpDeserializationException("Wrong length specified. Passed: " + length
40 + "; Expected: <= " + bytes.readableBytes() + ".");
42 final ByteBuf tlvBytes = bytes.readSlice(length);
43 LOG.trace("Parsing BMP TLV : {}", ByteBufUtil.hexDump(tlvBytes));
45 final Tlv tlv = this.tlvRegistry.parseTlv(type, tlvBytes);
47 LOG.trace("Parsed BMP TLV {}.", tlv);
53 protected final void serializeTlv(final Tlv tlv, final ByteBuf buffer) {
54 requireNonNull(tlv, "BMP TLV is mandatory.");
55 LOG.trace("Serializing BMP TLV {}", tlv);
56 this.tlvRegistry.serializeTlv(tlv, buffer);
57 LOG.trace("Serialized BMP TLV : {}.", ByteBufUtil.hexDump(buffer));
60 protected void addTlv(final T builder, final Tlv tlv) {