MVPN RFC6514 Extendend communities
[bgpcep.git] / bgp / parser-impl / src / main / java / org / opendaylight / protocol / bgp / parser / impl / message / update / AigpAttributeParser.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 package org.opendaylight.protocol.bgp.parser.impl.message.update;
9
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.rev180329.path.attributes.Attributes;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.AttributesBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.Aigp;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.AigpBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.aigp.AigpTlv;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.aigp.AigpTlvBuilder;
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;
29
30 public class AigpAttributeParser implements AttributeParser, AttributeSerializer {
31
32     private static final Logger LOG = LoggerFactory.getLogger(AigpAttributeParser.class);
33
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;
37
38     public static final int TYPE = 26;
39
40     @Override
41     public void parseAttribute(final ByteBuf buffer, final AttributesBuilder builder)
42             throws BGPDocumentedException, BGPParsingException {
43         if(!buffer.isReadable()) {
44             return;
45         }
46         builder.setAigp(new AigpBuilder().setAigpTlv(parseAigpTLV(buffer)).build());
47     }
48
49     @Override
50     public void serializeAttribute(final DataObject attribute, final ByteBuf byteAggregator) {
51         Preconditions.checkArgument(attribute instanceof Attributes,
52                 "Attribute parameter is not a PathAttribute object.");
53         final Aigp aigpAttribute = ((Attributes) attribute).getAigp();
54         if (aigpAttribute == null) {
55             return;
56         }
57         AttributeUtil.formatAttribute(AttributeUtil.OPTIONAL, TYPE, serializeAigpTLV(aigpAttribute), byteAggregator);
58     }
59
60     /**
61      * Reads data from buffer until reaches TLV of type AIGP TLV.
62      *
63      * @param buffer TLVs in ByteBuf format
64      * @return instance of AigpTlv class or null if buffer contains unknown TLV type
65      */
66     private static AigpTlv parseAigpTLV(final ByteBuf buffer) {
67         final int tlvType = buffer.readByte();
68         buffer.skipBytes(TLV_SIZE_IN_BYTES);
69         if (tlvType != AIGP_TLV_TYPE) {
70             LOG.warn("AIGP attribute contains unknown TLV type {}.", tlvType);
71             return null;
72         }
73         return new AigpTlvBuilder().setMetric(new AccumulatedIgpMetric(BigInteger.valueOf(buffer.readLong()))).build();
74     }
75
76     /**
77      * Transform AIGP attribute data from instance of Aigp class into byte buffer representation.
78      *
79      * @param aigp
80      *          instance of Aigp class
81      * @return
82      *          byte buffer representation or empty buffer if AIGP TLV is null
83      */
84     private static ByteBuf serializeAigpTLV(final Aigp aigp) {
85         final AigpTlv tlv = aigp.getAigpTlv();
86         if (tlv == null) {
87             return Unpooled.EMPTY_BUFFER;
88         }
89         final ByteBuf value = Unpooled.buffer(AIGP_TLV_SIZE);
90         value.writeByte(AIGP_TLV_TYPE);
91         value.writeShort(AIGP_TLV_SIZE);
92         value.writeLong(tlv.getMetric().getValue().longValue());
93         return value;
94     }
95 }