6fd2b87a382956edc30adf3ee0e271941481e15a
[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
9 package org.opendaylight.protocol.bgp.parser.impl.message.update;
10
11 import static java.util.Objects.requireNonNull;
12
13 import com.google.common.base.Preconditions;
14 import io.netty.buffer.ByteBuf;
15 import io.netty.buffer.Unpooled;
16 import java.util.ArrayList;
17 import java.util.List;
18 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
19 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
20 import org.opendaylight.protocol.bgp.parser.spi.AttributeParser;
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.extended.community.ExtendedCommunityRegistry;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.Attributes;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.AttributesBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.attributes.ExtendedCommunities;
27 import org.opendaylight.yangtools.yang.binding.DataObject;
28
29 public final class ExtendedCommunitiesAttributeParser implements AttributeParser, AttributeSerializer {
30
31     public static final int TYPE = 16;
32
33     private final ExtendedCommunityRegistry ecReg;
34
35     public ExtendedCommunitiesAttributeParser(final ExtendedCommunityRegistry ecReg) {
36         this.ecReg = requireNonNull(ecReg);
37     }
38
39     @Override
40     public void parseAttribute(final ByteBuf buffer, final AttributesBuilder builder) throws BGPDocumentedException, BGPParsingException {
41         final List<ExtendedCommunities> set = new ArrayList<>();
42         while (buffer.isReadable()) {
43             final ExtendedCommunities exComm = this.ecReg.parseExtendedCommunity(buffer);
44             if (exComm != null) {
45                 set.add(exComm);
46             }
47         }
48         builder.setExtendedCommunities(set);
49     }
50
51     @Override
52     public void serializeAttribute(final DataObject attribute, final ByteBuf byteAggregator) {
53         Preconditions.checkArgument(attribute instanceof Attributes, "Attribute parameter is not a PathAttribute object.");
54         final List<ExtendedCommunities> communitiesList = ((Attributes) attribute).getExtendedCommunities();
55         if (communitiesList == null || communitiesList.isEmpty()) {
56             return;
57         }
58         final ByteBuf extendedCommunitiesBuffer = Unpooled.buffer();
59         for (final ExtendedCommunities extendedCommunities : communitiesList) {
60             this.ecReg.serializeExtendedCommunity(extendedCommunities, extendedCommunitiesBuffer);
61         }
62         if (extendedCommunitiesBuffer.readableBytes() > 0) {
63             AttributeUtil.formatAttribute(AttributeUtil.OPTIONAL | AttributeUtil.TRANSITIVE, TYPE, extendedCommunitiesBuffer, byteAggregator);
64         }
65     }
66 }