BUG-2982 : moved path-attributes container to grouping
[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 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;
29
30 public final class CommunitiesAttributeParser implements AttributeParser, AttributeSerializer {
31
32     public static final int TYPE = 8;
33
34     private static final int COMMUNITY_LENGTH = 4;
35
36     private static final byte[] NO_EXPORT = new byte[] { (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0x01 };
37
38     private static final byte[] NO_ADVERTISE = new byte[] { (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0x02 };
39
40     private static final byte[] NO_EXPORT_SUBCONFED = new byte[] { (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0x03 };
41
42     private final ReferenceCache refCache;
43
44     public CommunitiesAttributeParser(final ReferenceCache refCache) {
45         this.refCache = Preconditions.checkNotNull(refCache);
46     }
47
48     @Override
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)));
53         }
54         builder.setCommunities(set);
55     }
56
57    /**
58     * Parse known Community, if unknown, a new one will be created.
59     *
60     * @param refCache
61     * @param buffer byte array to be parsed
62     * @return new Community
63     * @throws BGPDocumentedException
64     */
65     @VisibleForTesting
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);
69         }
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;
77         }
78         return CommunityUtil.create(refCache, buffer.readUnsignedShort(), buffer.readUnsignedShort());
79     }
80
81     @Override
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) {
87             return;
88         }
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());
93         }
94         AttributeUtil.formatAttribute(AttributeUtil.OPTIONAL | AttributeUtil.TRANSITIVE, TYPE, communitiesBuffer, byteAggregator);
95     }
96 }