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 9e513bc4eef0b86852a0c0b7288eda1b18d36229..e678ab7b1857760f0fa5f564685ec025f4abb9f2 100644 (file)
@@ -7,26 +7,37 @@
  */
 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.PathAttributes;
-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;
 
 public final class CommunitiesAttributeParser implements AttributeParser, AttributeSerializer {
 
     public static final int TYPE = 8;
-    public static final int ATTR_LENGTH = 4;
+
+    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 };
 
     private final ReferenceCache refCache;
 
@@ -35,21 +46,44 @@ public final class CommunitiesAttributeParser implements AttributeParser, Attrib
     }
 
     @Override
-    public void parseAttribute(final ByteBuf buffer, final PathAttributesBuilder builder) throws BGPDocumentedException {
+    public void parseAttribute(final ByteBuf buffer, final AttributesBuilder builder) throws BGPDocumentedException {
         final List<Communities> set = Lists.newArrayList();
         while (buffer.isReadable()) {
-            set.add((Communities) CommunitiesParser.parseCommunity(this.refCache, buffer.slice(buffer.readerIndex(),
-                    CommunitiesParser.COMMUNITY_LENGTH)));
-            buffer.skipBytes(CommunitiesParser.COMMUNITY_LENGTH);
+            set.add((Communities) parseCommunity(this.refCache, buffer.readSlice(COMMUNITY_LENGTH)));
         }
         builder.setCommunities(set);
     }
 
+   /**
+    * 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());
+    }
+
     @Override
     public void serializeAttribute(final DataObject attribute, final ByteBuf byteAggregator) {
-        final PathAttributes pathAttributes = (PathAttributes) attribute;
+        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 || communities.isEmpty()) {
+        if (communities == null) {
             return;
         }
         final ByteBuf communitiesBuffer = Unpooled.buffer();