YANG revision dates mass-update
[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 static java.util.Objects.requireNonNull;
11
12 import io.netty.buffer.ByteBuf;
13 import io.netty.buffer.Unpooled;
14 import org.opendaylight.protocol.bgp.parser.spi.AbstractAttributeParser;
15 import org.opendaylight.protocol.bgp.parser.spi.AttributeSerializer;
16 import org.opendaylight.protocol.bgp.parser.spi.AttributeUtil;
17 import org.opendaylight.protocol.bgp.parser.spi.PeerSpecificParserConstraint;
18 import org.opendaylight.protocol.bgp.parser.spi.RevisedErrorHandling;
19 import org.opendaylight.protocol.util.Ipv4Util;
20 import org.opendaylight.protocol.util.ReferenceCache;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.Attributes;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.AttributesBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.attributes.Aggregator;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.attributes.AggregatorBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.ShortAsNumber;
27 import org.opendaylight.yangtools.yang.common.netty.ByteBufUtils;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 /**
32  * Parser/serializer for {@link Aggregator}.
33  */
34 public final class AggregatorAttributeParser extends AbstractAttributeParser implements AttributeSerializer {
35     private static final Logger LOG = LoggerFactory.getLogger(AggregatorAttributeParser.class);
36     private static final int AGGREGATOR_LENGTH = 8;
37     public static final int TYPE = 7;
38
39     private final ReferenceCache refCache;
40
41     public AggregatorAttributeParser(final ReferenceCache refCache) {
42         this.refCache = requireNonNull(refCache);
43     }
44
45     @Override
46     public void parseAttribute(final ByteBuf buffer, final AttributesBuilder builder,
47             final RevisedErrorHandling errorHandling, final PeerSpecificParserConstraint constraint) {
48         if (buffer.readableBytes() != AGGREGATOR_LENGTH && errorHandling != RevisedErrorHandling.NONE) {
49             // RFC7606: we do not support non-4-octet AS number peers, perform attribute-discard
50             LOG.debug("Discarded malformed AGGREGATOR attribute");
51             return;
52         }
53
54         builder.setAggregator(new AggregatorBuilder()
55             // FIXME: above check should be expanded, so we report at least underflow errors
56             .setAsNumber(this.refCache.getSharedReference(new AsNumber(ByteBufUtils.readUint32(buffer))))
57             .setNetworkAddress(Ipv4Util.addressForByteBuf(buffer))
58             .build());
59     }
60
61     @Override
62     public void serializeAttribute(final Attributes pathAttributes, final ByteBuf byteAggregator) {
63         final Aggregator aggregator = pathAttributes.getAggregator();
64         if (aggregator != null) {
65             final AsNumber asNumber = aggregator.getAsNumber();
66             if (asNumber != null) {
67                 AttributeUtil.formatAttribute(AttributeUtil.OPTIONAL | AttributeUtil.TRANSITIVE, TYPE,
68                     Unpooled.buffer(AGGREGATOR_LENGTH)
69                         .writeInt(new ShortAsNumber(asNumber).getValue().intValue())
70                         .writeBytes(Ipv4Util.bytesForAddress(aggregator.getNetworkAddress())),
71                         byteAggregator);
72             }
73         }
74     }
75 }