2 * Copyright (c) 2013 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
8 package org.opendaylight.protocol.bgp.parser.impl.message.update;
10 import com.google.common.base.Preconditions;
11 import com.google.common.collect.Lists;
12 import io.netty.buffer.ByteBuf;
13 import io.netty.buffer.Unpooled;
14 import java.util.List;
15 import org.opendaylight.protocol.bgp.parser.AttributeFlags;
16 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
17 import org.opendaylight.protocol.bgp.parser.spi.AttributeParser;
18 import org.opendaylight.protocol.bgp.parser.spi.AttributeSerializer;
19 import org.opendaylight.protocol.util.ReferenceCache;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.PathAttributes;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.Communities;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.PathAttributesBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Community;
24 import org.opendaylight.yangtools.yang.binding.DataObject;
26 public final class CommunitiesAttributeParser implements AttributeParser, AttributeSerializer {
28 public static final int TYPE = 8;
29 public static final int ATTR_LENGTH = 4;
31 private final ReferenceCache refCache;
33 public CommunitiesAttributeParser(final ReferenceCache refCache) {
34 this.refCache = Preconditions.checkNotNull(refCache);
38 public void parseAttribute(final ByteBuf buffer, final PathAttributesBuilder builder) throws BGPDocumentedException {
39 final List<Communities> set = Lists.newArrayList();
40 while (buffer.isReadable()) {
41 set.add((Communities) CommunitiesParser.parseCommunity(this.refCache, buffer.slice(buffer.readerIndex(),
42 CommunitiesParser.COMMUNITY_LENGTH)));
43 buffer.skipBytes(CommunitiesParser.COMMUNITY_LENGTH);
45 builder.setCommunities(set);
49 public void serializeAttribute(DataObject attribute, ByteBuf byteAggregator) {
50 PathAttributes pathAttributes = (PathAttributes) attribute;
51 List<Communities> communities = pathAttributes.getCommunities();
52 if (communities == null) {
55 ByteBuf communitiesBuffer = Unpooled.buffer();
56 for (Community community : communities) {
57 communitiesBuffer.writeShort(community.getAsNumber().getValue().shortValue());
58 communitiesBuffer.writeShort(community.getSemantics().shortValue());
60 byteAggregator.writeByte(AttributeFlags.TRANSITIVE | AttributeFlags.PARTIAL);
61 byteAggregator.writeByte(CommunitiesAttributeParser.TYPE);
62 byteAggregator.writeByte(communitiesBuffer.writerIndex());
63 byteAggregator.writeBytes(communitiesBuffer);