BUG-2982 : moved path-attributes container to grouping
[bgpcep.git] / bgp / parser-impl / src / main / java / org / opendaylight / protocol / bgp / parser / impl / message / update / AggregatorAttributeParser.java
1 /*
2  * Copyright (c) 2013 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 org.opendaylight.protocol.bgp.parser.spi.AttributeParser;
14 import org.opendaylight.protocol.bgp.parser.spi.AttributeSerializer;
15 import org.opendaylight.protocol.bgp.parser.spi.AttributeUtil;
16 import org.opendaylight.protocol.util.Ipv4Util;
17 import org.opendaylight.protocol.util.ReferenceCache;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.Attributes;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.AttributesBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.Aggregator;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.AggregatorBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.ShortAsNumber;
25 import org.opendaylight.yangtools.yang.binding.DataObject;
26
27 public final class AggregatorAttributeParser implements AttributeParser, AttributeSerializer {
28
29     public static final int TYPE = 7;
30
31     private final ReferenceCache refCache;
32
33     public AggregatorAttributeParser(final ReferenceCache refCache) {
34         this.refCache = Preconditions.checkNotNull(refCache);
35     }
36
37     /**
38      * Parse {@link Aggregator} from bytes
39      *
40      * @param buffer byte buffer to be parsed
41      * @param builder PathAttributesBuilder into which parsed {@link Aggregator} will be set
42      */
43     @Override
44     public void parseAttribute(final ByteBuf buffer, final AttributesBuilder builder) {
45         final AsNumber asNumber = this.refCache.getSharedReference(new AsNumber(buffer.readUnsignedInt()));
46         final Ipv4Address address = Ipv4Util.addressForByteBuf(buffer);
47         builder.setAggregator(new AggregatorBuilder().setAsNumber(asNumber).setNetworkAddress(address).build());
48     }
49
50     @Override
51     public void serializeAttribute(final DataObject attribute, final ByteBuf byteAggregator) {
52         Preconditions.checkArgument(attribute instanceof Attributes, "Attribute parameter is not a PathAttribute object.");
53         final Attributes pathAttributes = (Attributes) attribute;
54         final Aggregator aggregator = pathAttributes.getAggregator();
55         if (aggregator == null) {
56             return;
57         }
58         Preconditions.checkArgument(aggregator.getAsNumber() != null, "Missing AS number that formed the aggregate route (encoded as 2 octets).");
59         final ShortAsNumber shortAsNumber = new ShortAsNumber(aggregator.getAsNumber());
60         final ByteBuf buffer = Unpooled.buffer();
61         buffer.writeInt(shortAsNumber.getValue().intValue());
62         buffer.writeBytes(Ipv4Util.bytesForAddress(aggregator.getNetworkAddress()));
63         AttributeUtil.formatAttribute(AttributeUtil.OPTIONAL | AttributeUtil.TRANSITIVE, TYPE, buffer, byteAggregator);
64     }
65 }