Moved Ipv4Util and Ipv6Util from concepts to util
[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.AttributeFlags;
14 import org.opendaylight.protocol.bgp.parser.spi.AttributeParser;
15 import org.opendaylight.protocol.bgp.parser.spi.AttributeSerializer;
16 import org.opendaylight.protocol.util.ByteArray;
17 import org.opendaylight.protocol.util.Ipv4Util;
18 import org.opendaylight.protocol.util.ReferenceCache;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.PathAttributes;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.Aggregator;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.AggregatorBuilder;
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.bgp.types.rev130919.ShortAsNumber;
26 import org.opendaylight.yangtools.yang.binding.DataObject;
27
28 public final class AggregatorAttributeParser implements AttributeParser, AttributeSerializer {
29
30     public static final int TYPE = 7;
31
32     private final ReferenceCache refCache;
33
34     public AggregatorAttributeParser(final ReferenceCache refCache) {
35         this.refCache = Preconditions.checkNotNull(refCache);
36     }
37
38     /**
39      * Parse AGGREGATOR from bytes
40      *
41      * @param buffer byte buffer to be parsed
42      * @return {@link Aggregator} BGP Aggregator
43      */
44     @Override
45     public void parseAttribute(final ByteBuf buffer, final PathAttributesBuilder builder) {
46         final AsNumber asNumber = this.refCache.getSharedReference(new AsNumber(buffer.readUnsignedInt()));
47         final Ipv4Address address = Ipv4Util.addressForBytes(ByteArray.readAllBytes(buffer));
48         builder.setAggregator(new AggregatorBuilder().setAsNumber(asNumber).setNetworkAddress(address).build());
49     }
50
51     @Override
52     public void serializeAttribute(DataObject attribute, ByteBuf byteAggregator) {
53         PathAttributes pathAttributes = (PathAttributes) attribute;
54         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         ShortAsNumber shortAsNumber = new ShortAsNumber(aggregator.getAsNumber());
60         byteAggregator.writeByte(AttributeFlags.OPTIONAL | AttributeFlags.TRANSITIVE);
61         byteAggregator.writeByte(TYPE);
62         ByteBuf aggregatorBuffer = Unpooled.buffer();
63         aggregatorBuffer.writeInt(shortAsNumber.getValue().intValue());
64         aggregatorBuffer.writeBytes(Ipv4Util.bytesForAddress(aggregator.getNetworkAddress()));
65         byteAggregator.writeByte(aggregatorBuffer.writerIndex());
66         byteAggregator.writeBytes(aggregatorBuffer);
67     }
68 }