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