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.annotations.VisibleForTesting;
11 import com.google.common.base.Preconditions;
12 import com.google.common.collect.Lists;
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.Unpooled;
15 import java.util.Arrays;
16 import java.util.List;
17 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
18 import org.opendaylight.protocol.bgp.parser.BGPError;
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.util.ByteArray;
23 import org.opendaylight.protocol.util.ReferenceCache;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.Attributes;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.AttributesBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.Communities;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Community;
28 import org.opendaylight.yangtools.yang.binding.DataObject;
30 public final class CommunitiesAttributeParser implements AttributeParser, AttributeSerializer {
32 public static final int TYPE = 8;
34 private static final int COMMUNITY_LENGTH = 4;
36 private static final byte[] NO_EXPORT = new byte[] { (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0x01 };
38 private static final byte[] NO_ADVERTISE = new byte[] { (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0x02 };
40 private static final byte[] NO_EXPORT_SUBCONFED = new byte[] { (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0x03 };
42 private final ReferenceCache refCache;
44 public CommunitiesAttributeParser(final ReferenceCache refCache) {
45 this.refCache = Preconditions.checkNotNull(refCache);
49 public void parseAttribute(final ByteBuf buffer, final AttributesBuilder builder) throws BGPDocumentedException {
50 final List<Communities> set = Lists.newArrayList();
51 while (buffer.isReadable()) {
52 set.add((Communities) parseCommunity(this.refCache, buffer.readSlice(COMMUNITY_LENGTH)));
54 builder.setCommunities(set);
58 * Parse known Community, if unknown, a new one will be created.
61 * @param buffer byte array to be parsed
62 * @return new Community
63 * @throws BGPDocumentedException
66 public static Community parseCommunity(final ReferenceCache refCache, final ByteBuf buffer) throws BGPDocumentedException {
67 if (buffer.readableBytes() != COMMUNITY_LENGTH) {
68 throw new BGPDocumentedException("Community with wrong length: " + buffer.readableBytes(), BGPError.OPT_ATTR_ERROR);
70 final byte[] body = ByteArray.getBytes(buffer, COMMUNITY_LENGTH);
71 if (Arrays.equals(body, NO_EXPORT)) {
72 return CommunityUtil.NO_EXPORT;
73 } else if (Arrays.equals(body, NO_ADVERTISE)) {
74 return CommunityUtil.NO_ADVERTISE;
75 } else if (Arrays.equals(body, NO_EXPORT_SUBCONFED)) {
76 return CommunityUtil.NO_EXPORT_SUBCONFED;
78 return CommunityUtil.create(refCache, buffer.readUnsignedShort(), buffer.readUnsignedShort());
82 public void serializeAttribute(final DataObject attribute, final ByteBuf byteAggregator) {
83 Preconditions.checkArgument(attribute instanceof Attributes, "Attribute parameter is not a PathAttribute object.");
84 final Attributes pathAttributes = (Attributes) attribute;
85 final List<Communities> communities = pathAttributes.getCommunities();
86 if (communities == null) {
89 final ByteBuf communitiesBuffer = Unpooled.buffer();
90 for (final Community community : communities) {
91 communitiesBuffer.writeShort(community.getAsNumber().getValue().shortValue());
92 communitiesBuffer.writeShort(community.getSemantics().shortValue());
94 AttributeUtil.formatAttribute(AttributeUtil.OPTIONAL | AttributeUtil.TRANSITIVE, TYPE, communitiesBuffer, byteAggregator);