c68a6e5bc8a79f8d18fa6d0d57ca46a59a39e8cc
[bgpcep.git] / bgp / parser-impl / src / main / java / org / opendaylight / protocol / bgp / parser / impl / message / update / ExtendedCommunitiesAttributeParser.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 com.google.common.base.Preconditions;
11 import com.google.common.collect.Lists;
12
13 import io.netty.buffer.ByteBuf;
14
15 import java.util.List;
16
17 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
18 import org.opendaylight.protocol.bgp.parser.spi.AttributeParser;
19 import org.opendaylight.protocol.util.ReferenceCache;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.ExtendedCommunities;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.PathAttributesBuilder;
22
23 public final class ExtendedCommunitiesAttributeParser implements AttributeParser {
24     public static final int TYPE = 16;
25
26     private final ReferenceCache refCache;
27
28     public ExtendedCommunitiesAttributeParser(final ReferenceCache refCache) {
29         this.refCache = Preconditions.checkNotNull(refCache);
30     }
31
32     @Override
33     public void parseAttribute(final ByteBuf buffer, final PathAttributesBuilder builder) throws BGPDocumentedException {
34         final List<ExtendedCommunities> set = Lists.newArrayList();
35         while (buffer.isReadable()) {
36             final ExtendedCommunities comm = CommunitiesParser.parseExtendedCommunity(this.refCache, buffer.slice(buffer.readerIndex(),
37                     CommunitiesParser.EXTENDED_COMMUNITY_LENGTH));
38             buffer.skipBytes(CommunitiesParser.EXTENDED_COMMUNITY_LENGTH);
39             set.add(comm);
40         }
41         builder.setExtendedCommunities(set);
42     }
43 }