Merge "Bug-2250: pcc-mock support TCP MD5"
[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.PathAttributes;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.Communities;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.PathAttributesBuilder;
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 PathAttributesBuilder 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     *
62     * @param bytes byte array to be parsed
63     * @return new Community
64     * @throws BGPDocumentedException
65     */
66     @VisibleForTesting
67     public static Community parseCommunity(final ReferenceCache refCache, final ByteBuf buffer) throws BGPDocumentedException {
68         if (buffer.readableBytes() != COMMUNITY_LENGTH) {
69             throw new BGPDocumentedException("Community with wrong length: " + buffer.readableBytes(), BGPError.OPT_ATTR_ERROR);
70         }
71         final byte[] body = ByteArray.getBytes(buffer, COMMUNITY_LENGTH);
72         if (Arrays.equals(body, NO_EXPORT)) {
73             return CommunityUtil.NO_EXPORT;
74         } else if (Arrays.equals(body, NO_ADVERTISE)) {
75             return CommunityUtil.NO_ADVERTISE;
76         } else if (Arrays.equals(body, NO_EXPORT_SUBCONFED)) {
77             return CommunityUtil.NO_EXPORT_SUBCONFED;
78         }
79         return CommunityUtil.create(refCache, buffer.readUnsignedShort(), buffer.readUnsignedShort());
80     }
81
82     @Override
83     public void serializeAttribute(final DataObject attribute, final ByteBuf byteAggregator) {
84         Preconditions.checkArgument(attribute instanceof PathAttributes, "Attribute parameter is not a PathAttribute object.");
85         final PathAttributes pathAttributes = (PathAttributes) attribute;
86         final List<Communities> communities = pathAttributes.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 }