MVPN RFC6514 Extendend communities
[bgpcep.git] / bgp / parser-impl / src / main / java / org / opendaylight / protocol / bgp / parser / impl / message / update / CommunitiesAttributeParser.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 static java.util.Objects.requireNonNull;
11
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.rev180329.path.attributes.Attributes;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.AttributesBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.Communities;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.Community;
30 import org.opendaylight.yangtools.yang.binding.DataObject;
31
32 public final class CommunitiesAttributeParser implements AttributeParser, AttributeSerializer {
33
34     public static final int TYPE = 8;
35
36     private static final int COMMUNITY_LENGTH = 4;
37
38     private static final byte[] NO_EXPORT = new byte[] { (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0x01 };
39
40     private static final byte[] NO_ADVERTISE = new byte[] { (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0x02 };
41
42     private static final byte[] NO_EXPORT_SUBCONFED = new byte[] { (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0x03 };
43
44     private final ReferenceCache refCache;
45
46     public CommunitiesAttributeParser(final ReferenceCache refCache) {
47         this.refCache = requireNonNull(refCache);
48     }
49
50     @Override
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)));
55         }
56         builder.setCommunities(set);
57     }
58
59    /**
60     * Parse known Community, if unknown, a new one will be created.
61     *
62     * @param refCache
63     * @param buffer byte array to be parsed
64     * @return new Community
65     * @throws BGPDocumentedException
66     */
67     @VisibleForTesting
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);
71         }
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;
79         }
80         return CommunityUtil.create(refCache, buffer.readUnsignedShort(), buffer.readUnsignedShort());
81     }
82
83     @Override
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()) {
88             return;
89         }
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());
94         }
95         AttributeUtil.formatAttribute(AttributeUtil.OPTIONAL | AttributeUtil.TRANSITIVE, TYPE, communitiesBuffer, byteAggregator);
96     }
97 }