2 * Copyright (c) 2013 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 static java.util.Objects.requireNonNull;
12 import com.google.common.base.Preconditions;
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.Unpooled;
15 import org.opendaylight.protocol.bgp.parser.spi.AttributeParser;
16 import org.opendaylight.protocol.bgp.parser.spi.AttributeSerializer;
17 import org.opendaylight.protocol.bgp.parser.spi.AttributeUtil;
18 import org.opendaylight.protocol.util.Ipv4Util;
19 import org.opendaylight.protocol.util.ReferenceCache;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.Attributes;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.AttributesBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.Aggregator;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.AggregatorBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.ShortAsNumber;
27 import org.opendaylight.yangtools.yang.binding.DataObject;
29 public final class AggregatorAttributeParser implements AttributeParser, AttributeSerializer {
31 public static final int TYPE = 7;
33 private final ReferenceCache refCache;
35 public AggregatorAttributeParser(final ReferenceCache refCache) {
36 this.refCache = requireNonNull(refCache);
40 * Parse {@link Aggregator} from bytes
42 * @param buffer byte buffer to be parsed
43 * @param builder AttributesBuilder into which parsed {@link Aggregator} will be set
46 public void parseAttribute(final ByteBuf buffer, final AttributesBuilder builder) {
47 final AsNumber asNumber = this.refCache.getSharedReference(new AsNumber(buffer.readUnsignedInt()));
48 final Ipv4Address address = Ipv4Util.addressForByteBuf(buffer);
49 builder.setAggregator(new AggregatorBuilder().setAsNumber(asNumber).setNetworkAddress(address).build());
53 public void serializeAttribute(final DataObject attribute, final ByteBuf byteAggregator) {
54 Preconditions.checkArgument(attribute instanceof Attributes, "Attribute parameter is not a PathAttribute object.");
55 final Attributes pathAttributes = (Attributes) attribute;
56 final Aggregator aggregator = pathAttributes.getAggregator();
57 if (aggregator == null) {
60 Preconditions.checkArgument(aggregator.getAsNumber() != null, "Missing AS number that formed the aggregate route (encoded as 2 octets).");
61 final ShortAsNumber shortAsNumber = new ShortAsNumber(aggregator.getAsNumber());
62 final ByteBuf buffer = Unpooled.buffer();
63 buffer.writeInt(shortAsNumber.getValue().intValue());
64 buffer.writeBytes(Ipv4Util.bytesForAddress(aggregator.getNetworkAddress()));
65 AttributeUtil.formatAttribute(AttributeUtil.OPTIONAL | AttributeUtil.TRANSITIVE, TYPE, buffer, byteAggregator);