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
index 9e8359094df847c7d82feafa5dc30a18bc87c80e..e678ab7b1857760f0fa5f564685ec025f4abb9f2 100644 (file)
@@ -7,36 +7,90 @@
  */
 package org.opendaylight.protocol.bgp.parser.impl.message.update;
 
+import com.google.common.annotations.VisibleForTesting;
+import com.google.common.base.Preconditions;
+import com.google.common.collect.Lists;
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
+import java.util.Arrays;
 import java.util.List;
-
 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
+import org.opendaylight.protocol.bgp.parser.BGPError;
 import org.opendaylight.protocol.bgp.parser.spi.AttributeParser;
+import org.opendaylight.protocol.bgp.parser.spi.AttributeSerializer;
+import org.opendaylight.protocol.bgp.parser.spi.AttributeUtil;
 import org.opendaylight.protocol.util.ByteArray;
 import org.opendaylight.protocol.util.ReferenceCache;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.Communities;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.PathAttributesBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.Attributes;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.AttributesBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.Communities;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Community;
+import org.opendaylight.yangtools.yang.binding.DataObject;
 
-import com.google.common.base.Preconditions;
-import com.google.common.collect.Lists;
+public final class CommunitiesAttributeParser implements AttributeParser, AttributeSerializer {
+
+    public static final int TYPE = 8;
+
+    private static final int COMMUNITY_LENGTH = 4;
+
+    private static final byte[] NO_EXPORT = new byte[] { (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0x01 };
+
+    private static final byte[] NO_ADVERTISE = new byte[] { (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0x02 };
+
+    private static final byte[] NO_EXPORT_SUBCONFED = new byte[] { (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0x03 };
 
-public final class CommunitiesAttributeParser implements AttributeParser {
-       public static final int TYPE = 8;
+    private final ReferenceCache refCache;
 
-       private final ReferenceCache refCache;
+    public CommunitiesAttributeParser(final ReferenceCache refCache) {
+        this.refCache = Preconditions.checkNotNull(refCache);
+    }
 
-       public CommunitiesAttributeParser(final ReferenceCache refCache) {
-               this.refCache = Preconditions.checkNotNull(refCache);
-       }
+    @Override
+    public void parseAttribute(final ByteBuf buffer, final AttributesBuilder builder) throws BGPDocumentedException {
+        final List<Communities> set = Lists.newArrayList();
+        while (buffer.isReadable()) {
+            set.add((Communities) parseCommunity(this.refCache, buffer.readSlice(COMMUNITY_LENGTH)));
+        }
+        builder.setCommunities(set);
+    }
 
-       @Override
-       public void parseAttribute(final byte[] bytes, final PathAttributesBuilder builder) throws BGPDocumentedException {
-               final List<Communities> set = Lists.newArrayList();
-               int i = 0;
-               while (i < bytes.length) {
-                       set.add((Communities) CommunitiesParser.parseCommunity(refCache, ByteArray.subByte(bytes, i, CommunitiesParser.COMMUNITY_LENGTH)));
-                       i += CommunitiesParser.COMMUNITY_LENGTH;
-               }
+   /**
+    * Parse known Community, if unknown, a new one will be created.
+    *
+    * @param refCache
+    * @param buffer byte array to be parsed
+    * @return new Community
+    * @throws BGPDocumentedException
+    */
+    @VisibleForTesting
+    public static Community parseCommunity(final ReferenceCache refCache, final ByteBuf buffer) throws BGPDocumentedException {
+        if (buffer.readableBytes() != COMMUNITY_LENGTH) {
+            throw new BGPDocumentedException("Community with wrong length: " + buffer.readableBytes(), BGPError.OPT_ATTR_ERROR);
+        }
+        final byte[] body = ByteArray.getBytes(buffer, COMMUNITY_LENGTH);
+        if (Arrays.equals(body, NO_EXPORT)) {
+            return CommunityUtil.NO_EXPORT;
+        } else if (Arrays.equals(body, NO_ADVERTISE)) {
+            return CommunityUtil.NO_ADVERTISE;
+        } else if (Arrays.equals(body, NO_EXPORT_SUBCONFED)) {
+            return CommunityUtil.NO_EXPORT_SUBCONFED;
+        }
+        return CommunityUtil.create(refCache, buffer.readUnsignedShort(), buffer.readUnsignedShort());
+    }
 
-               builder.setCommunities(set);
-       }
-}
\ No newline at end of file
+    @Override
+    public void serializeAttribute(final DataObject attribute, final ByteBuf byteAggregator) {
+        Preconditions.checkArgument(attribute instanceof Attributes, "Attribute parameter is not a PathAttribute object.");
+        final Attributes pathAttributes = (Attributes) attribute;
+        final List<Communities> communities = pathAttributes.getCommunities();
+        if (communities == null) {
+            return;
+        }
+        final ByteBuf communitiesBuffer = Unpooled.buffer();
+        for (final Community community : communities) {
+            communitiesBuffer.writeShort(community.getAsNumber().getValue().shortValue());
+            communitiesBuffer.writeShort(community.getSemantics().shortValue());
+        }
+        AttributeUtil.formatAttribute(AttributeUtil.OPTIONAL | AttributeUtil.TRANSITIVE, TYPE, communitiesBuffer, byteAggregator);
+    }
+}