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