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 com.google.common.base.Preconditions;
11 import io.netty.buffer.ByteBuf;
12 import io.netty.buffer.Unpooled;
13 import java.math.BigInteger;
14 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
15 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
16 import org.opendaylight.protocol.bgp.parser.spi.AttributeParser;
17 import org.opendaylight.protocol.bgp.parser.spi.AttributeSerializer;
18 import org.opendaylight.protocol.bgp.parser.spi.AttributeUtil;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.Aigp;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.AigpBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.aigp.AigpTlv;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.aigp.AigpTlvBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.PathAttributes;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.PathAttributesBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.AccumulatedIgpMetric;
26 import org.opendaylight.yangtools.yang.binding.DataObject;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
30 public class AigpAttributeParser implements AttributeParser, AttributeSerializer {
32 private static final Logger LOG = LoggerFactory.getLogger(AigpAttributeParser.class);
34 private static final byte AIGP_TLV_TYPE = 1;
35 private static final short AIGP_TLV_SIZE = 11;
36 private static final int TLV_SIZE_IN_BYTES = 2;
38 public static final int TYPE = 26;
41 public void parseAttribute(final ByteBuf buffer, final PathAttributesBuilder builder)
42 throws BGPDocumentedException, BGPParsingException {
43 if(!buffer.isReadable()) {
46 builder.setAigp(new AigpBuilder().setAigpTlv(parseAigpTLV(buffer)).build());
50 public void serializeAttribute(final DataObject attribute, final ByteBuf byteAggregator) {
51 Preconditions.checkArgument(attribute instanceof PathAttributes,
52 "Attribute parameter is not a PathAttribute object.");
53 final Aigp aigpAttribute = ((PathAttributes) attribute).getAigp();
54 if (aigpAttribute == null) {
57 AttributeUtil.formatAttribute(AttributeUtil.OPTIONAL, TYPE, serializeAigpTLV(aigpAttribute), byteAggregator);
61 * Reads data from buffer until reaches TLV of type AIGP TLV.
64 * TLVs in ByteBuf format
66 * instance of AigpTlv class or null if buffer contains unknown TLV type
68 private static AigpTlv parseAigpTLV(final ByteBuf buffer) {
69 final int tlvType = buffer.readByte();
70 buffer.skipBytes(TLV_SIZE_IN_BYTES);
71 if (tlvType != AIGP_TLV_TYPE) {
72 LOG.warn("AIGP attribute contains unknown TLV type {}.", tlvType);
75 return new AigpTlvBuilder().setMetric(new AccumulatedIgpMetric(BigInteger.valueOf(buffer.readLong()))).build();
79 * Transform AIGP attribute data from instance of Aigp class into byte buffer representation.
82 * instance of Aigp class
84 * byte buffer representation or empty buffer if AIGP TLV is null
86 private static ByteBuf serializeAigpTLV(final Aigp aigp) {
87 final AigpTlv tlv = aigp.getAigpTlv();
89 return Unpooled.EMPTY_BUFFER;
91 final ByteBuf value = Unpooled.buffer(AIGP_TLV_SIZE);
92 value.writeByte(AIGP_TLV_TYPE);
93 value.writeShort(AIGP_TLV_SIZE);
94 value.writeLong(tlv.getMetric().getValue().longValue());