2 * Copyright (c) 2015 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
9 package org.opendaylight.protocol.bgp.parser.impl.message.update;
11 import static java.util.Objects.requireNonNull;
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.Unpooled;
15 import java.util.ArrayList;
16 import java.util.List;
17 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
18 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
19 import org.opendaylight.protocol.bgp.parser.spi.AttributeParser;
20 import org.opendaylight.protocol.bgp.parser.spi.AttributeSerializer;
21 import org.opendaylight.protocol.bgp.parser.spi.AttributeUtil;
22 import org.opendaylight.protocol.bgp.parser.spi.extended.community.ExtendedCommunityRegistry;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.Attributes;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.AttributesBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.ExtendedCommunities;
27 public final class ExtendedCommunitiesAttributeParser implements AttributeParser, AttributeSerializer {
29 public static final int TYPE = 16;
31 private final ExtendedCommunityRegistry ecReg;
33 public ExtendedCommunitiesAttributeParser(final ExtendedCommunityRegistry ecReg) {
34 this.ecReg = requireNonNull(ecReg);
38 public void parseAttribute(final ByteBuf buffer, final AttributesBuilder builder)
39 throws BGPDocumentedException, BGPParsingException {
40 final List<ExtendedCommunities> set = new ArrayList<>();
41 while (buffer.isReadable()) {
42 final ExtendedCommunities exComm = this.ecReg.parseExtendedCommunity(buffer);
47 builder.setExtendedCommunities(set);
51 public void serializeAttribute(final Attributes pathAttributes, final ByteBuf byteAggregator) {
52 final List<ExtendedCommunities> communitiesList = pathAttributes.getExtendedCommunities();
53 if (communitiesList == null || communitiesList.isEmpty()) {
56 final ByteBuf extendedCommunitiesBuffer = Unpooled.buffer();
57 for (final ExtendedCommunities extendedCommunities : communitiesList) {
58 this.ecReg.serializeExtendedCommunity(extendedCommunities, extendedCommunitiesBuffer);
60 if (extendedCommunitiesBuffer.readableBytes() > 0) {
61 AttributeUtil.formatAttribute(AttributeUtil.OPTIONAL | AttributeUtil.TRANSITIVE, TYPE,
62 extendedCommunitiesBuffer, byteAggregator);