YANG revision dates mass-update
[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 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;
26
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;
32
33     public static final int TYPE = 26;
34
35     @Override
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());
40         }
41     }
42
43     @Override
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),
48                 byteAggregator);
49         }
50     }
51
52     /**
53      * Reads data from buffer until reaches TLV of type AIGP TLV.
54      *
55      * @param buffer TLVs in ByteBuf format
56      * @return instance of AigpTlv class or null if buffer contains unknown TLV type
57      */
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);
63             return null;
64         }
65         return new AigpTlvBuilder().setMetric(new AccumulatedIgpMetric(ByteBufUtils.readUint64(buffer))).build();
66     }
67
68     /**
69      * Transform AIGP attribute data from instance of Aigp class into byte buffer representation.
70      *
71      * @param aigp instance of Aigp class
72      * @return byte buffer representation or empty buffer if AIGP TLV is null
73      */
74     private static ByteBuf serializeAigpTLV(final Aigp aigp) {
75         final AigpTlv tlv = aigp.getAigpTlv();
76         if (tlv == null) {
77             return Unpooled.EMPTY_BUFFER;
78         }
79         return Unpooled.buffer(AIGP_TLV_SIZE)
80                 .writeByte(AIGP_TLV_TYPE)
81                 .writeShort(AIGP_TLV_SIZE)
82                 .writeLong(tlv.getMetric().getValue().longValue());
83     }
84 }