YANG revision dates mass-update
[bgpcep.git] / bgp / parser-impl / src / main / java / org / opendaylight / protocol / bgp / parser / impl / message / update / ExtendedCommunitiesAttributeParser.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 static java.util.Objects.requireNonNull;
11
12 import io.netty.buffer.ByteBuf;
13 import io.netty.buffer.Unpooled;
14 import java.util.ArrayList;
15 import java.util.List;
16 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
17 import org.opendaylight.protocol.bgp.parser.BGPError;
18 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
19 import org.opendaylight.protocol.bgp.parser.BGPTreatAsWithdrawException;
20 import org.opendaylight.protocol.bgp.parser.spi.AbstractAttributeParser;
21 import org.opendaylight.protocol.bgp.parser.spi.AttributeSerializer;
22 import org.opendaylight.protocol.bgp.parser.spi.AttributeUtil;
23 import org.opendaylight.protocol.bgp.parser.spi.PeerSpecificParserConstraint;
24 import org.opendaylight.protocol.bgp.parser.spi.RevisedErrorHandling;
25 import org.opendaylight.protocol.bgp.parser.spi.extended.community.ExtendedCommunityRegistry;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.Attributes;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.AttributesBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.attributes.ExtendedCommunities;
29
30 public final class ExtendedCommunitiesAttributeParser extends AbstractAttributeParser implements AttributeSerializer {
31
32     public static final int TYPE = 16;
33
34     private final ExtendedCommunityRegistry ecReg;
35
36     public ExtendedCommunitiesAttributeParser(final ExtendedCommunityRegistry ecReg) {
37         this.ecReg = requireNonNull(ecReg);
38     }
39
40     @Override
41     public void parseAttribute(final ByteBuf buffer, final AttributesBuilder builder,
42             final RevisedErrorHandling errorHandling, final PeerSpecificParserConstraint constraint)
43                     throws BGPDocumentedException, BGPTreatAsWithdrawException {
44         final int readable = buffer.readableBytes();
45         if (errorHandling != RevisedErrorHandling.NONE) {
46             if (readable == 0) {
47                 throw new BGPTreatAsWithdrawException(BGPError.ATTR_LENGTH_ERROR, "Empty Extended Community attribute");
48             }
49         }
50         if (readable % 8 != 0) {
51             throw errorHandling.reportError(BGPError.ATTR_LENGTH_ERROR,
52                 "Extended Community attribute length must be a multiple of 8, have %s", readable);
53         }
54         final List<ExtendedCommunities> set = new ArrayList<>(readable / 8);
55         while (buffer.isReadable()) {
56             final ExtendedCommunities exComm;
57             try {
58                 // FIXME: BGPCEP-359: revise API contract here
59                 exComm = this.ecReg.parseExtendedCommunity(buffer);
60             } catch (BGPParsingException e) {
61                 throw errorHandling.reportError(BGPError.MALFORMED_ATTR_LIST, e, "Failed to parse extended community");
62             }
63             if (exComm != null) {
64                 set.add(exComm);
65             }
66         }
67         builder.setExtendedCommunities(set);
68     }
69
70     @Override
71     public void serializeAttribute(final Attributes pathAttributes, final ByteBuf byteAggregator) {
72         final List<ExtendedCommunities> communitiesList = pathAttributes.getExtendedCommunities();
73         if (communitiesList == null || communitiesList.isEmpty()) {
74             return;
75         }
76         final ByteBuf extendedCommunitiesBuffer = Unpooled.buffer();
77         for (final ExtendedCommunities extendedCommunities : communitiesList) {
78             this.ecReg.serializeExtendedCommunity(extendedCommunities, extendedCommunitiesBuffer);
79         }
80         if (extendedCommunitiesBuffer.readableBytes() > 0) {
81             AttributeUtil.formatAttribute(AttributeUtil.OPTIONAL | AttributeUtil.TRANSITIVE, TYPE,
82                     extendedCommunitiesBuffer, byteAggregator);
83         }
84     }
85 }