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
8 package org.opendaylight.protocol.bgp.parser.impl.message.update;
10 import io.netty.buffer.ByteBuf;
11 import io.netty.buffer.Unpooled;
12 import org.opendaylight.protocol.bgp.parser.spi.AttributeParser;
13 import org.opendaylight.protocol.bgp.parser.spi.AttributeSerializer;
14 import org.opendaylight.protocol.bgp.parser.spi.AttributeUtil;
15 import org.opendaylight.protocol.bgp.parser.spi.PeerSpecificParserConstraint;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.Attributes;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.AttributesBuilder;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.attributes.Aigp;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.attributes.AigpBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.attributes.aigp.AigpTlv;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.attributes.aigp.AigpTlvBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.AccumulatedIgpMetric;
23 import org.opendaylight.yangtools.yang.common.netty.ByteBufUtils;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
27 public class AigpAttributeParser implements AttributeParser, AttributeSerializer {
28 private static final Logger LOG = LoggerFactory.getLogger(AigpAttributeParser.class);
29 private static final byte AIGP_TLV_TYPE = 1;
30 private static final short AIGP_TLV_SIZE = 11;
31 private static final int TLV_SIZE_IN_BYTES = 2;
33 public static final int TYPE = 26;
36 public void parseAttribute(final ByteBuf buffer, final AttributesBuilder builder,
37 final PeerSpecificParserConstraint constraint) {
38 if (buffer.isReadable()) {
39 builder.setAigp(new AigpBuilder().setAigpTlv(parseAigpTLV(buffer)).build());
44 public void serializeAttribute(final Attributes attribute, final ByteBuf byteAggregator) {
45 final Aigp aigpAttribute = attribute.getAigp();
46 if (aigpAttribute != null) {
47 AttributeUtil.formatAttribute(AttributeUtil.OPTIONAL, TYPE, serializeAigpTLV(aigpAttribute),
53 * Reads data from buffer until reaches TLV of type AIGP TLV.
55 * @param buffer TLVs in ByteBuf format
56 * @return instance of AigpTlv class or null if buffer contains unknown TLV type
58 private static AigpTlv parseAigpTLV(final ByteBuf buffer) {
59 final int tlvType = buffer.readByte();
60 buffer.skipBytes(TLV_SIZE_IN_BYTES);
61 if (tlvType != AIGP_TLV_TYPE) {
62 LOG.warn("AIGP attribute contains unknown TLV type {}.", tlvType);
65 return new AigpTlvBuilder().setMetric(new AccumulatedIgpMetric(ByteBufUtils.readUint64(buffer))).build();
69 * Transform AIGP attribute data from instance of Aigp class into byte buffer representation.
71 * @param aigp instance of Aigp class
72 * @return byte buffer representation or empty buffer if AIGP TLV is null
74 private static ByteBuf serializeAigpTLV(final Aigp aigp) {
75 final AigpTlv tlv = aigp.getAigpTlv();
77 return Unpooled.EMPTY_BUFFER;
79 return Unpooled.buffer(AIGP_TLV_SIZE)
80 .writeByte(AIGP_TLV_TYPE)
81 .writeShort(AIGP_TLV_SIZE)
82 .writeLong(tlv.getMetric().getValue().longValue());