BUG-2982 : moved path-attributes container to grouping
[bgpcep.git] / bgp / parser-impl / src / main / java / org / opendaylight / protocol / bgp / parser / impl / message / update / ExtendedCommunitiesAttributeParser.java
index 5e7f9e09383b7c668beeed03922f22b0fc7d4696..b6ce40e3c0e1e30f70dcdcef75d1266aa570e278 100644 (file)
@@ -8,7 +8,6 @@
 package org.opendaylight.protocol.bgp.parser.impl.message.update;
 
 import com.google.common.base.Preconditions;
-import com.google.common.primitives.UnsignedBytes;
 import io.netty.buffer.ByteBuf;
 import io.netty.buffer.Unpooled;
 import java.util.ArrayList;
@@ -22,10 +21,10 @@ import org.opendaylight.protocol.util.ByteArray;
 import org.opendaylight.protocol.util.ByteBufWriteUtil;
 import org.opendaylight.protocol.util.Ipv4Util;
 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.ExtendedCommunities;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.ExtendedCommunitiesBuilder;
-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.ExtendedCommunities;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.ExtendedCommunitiesBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.ShortAsNumber;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.ExtendedCommunity;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.AsSpecificExtendedCommunityCase;
@@ -85,7 +84,7 @@ public class ExtendedCommunitiesAttributeParser implements AttributeParser,Attri
     }
 
     @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<ExtendedCommunities> set = new ArrayList<>();
         while (buffer.isReadable()) {
             final ExtendedCommunitiesBuilder exBuilder = new ExtendedCommunitiesBuilder();
@@ -97,14 +96,16 @@ public class ExtendedCommunitiesAttributeParser implements AttributeParser,Attri
     }
 
     protected void parseHeader(final ExtendedCommunitiesBuilder exBuilder, final ByteBuf buffer) {
-        exBuilder.setCommType((short) UnsignedBytes.toInt(buffer.readByte()));
-        exBuilder.setCommSubType((short) UnsignedBytes.toInt(buffer.readByte()));
+        exBuilder.setCommType(buffer.readUnsignedByte());
+        exBuilder.setCommSubType(buffer.readUnsignedByte());
     }
 
     /**
      * Parse Extended Community according to their type.
      *
-     * @param bytes byte array to be parsed
+     * @param refCache
+     * @param comm ExtendedCommunitiesBuilder based on which community type will be new ExtendedCommunity created
+     * @param buffer byte array to be parsed
      * @return new Specific Extended Community
      * @throws BGPDocumentedException if the type is not recognized
      */
@@ -116,21 +117,13 @@ public class ExtendedCommunitiesAttributeParser implements AttributeParser,Attri
             c = parseAsTransCommunity(comm, buffer);
             break;
         case AS_TYPE_NON_TRANS:
-            ShortAsNumber as = new ShortAsNumber((long) buffer.readUnsignedShort());
-            byte[] value = ByteArray.readBytes(buffer, AS_LOCAL_ADMIN_LENGTH);
+            final ShortAsNumber as = new ShortAsNumber((long) buffer.readUnsignedShort());
+            final byte[] value = ByteArray.readBytes(buffer, AS_LOCAL_ADMIN_LENGTH);
             c = new AsSpecificExtendedCommunityCaseBuilder().setAsSpecificExtendedCommunity(
                 new AsSpecificExtendedCommunityBuilder().setTransitive(true).setGlobalAdministrator(as).setLocalAdministrator(value).build()).build();
             break;
         case ROUTE_TYPE_ONLY:
-            as = new ShortAsNumber((long) buffer.readUnsignedShort());
-            value = ByteArray.readBytes(buffer, AS_LOCAL_ADMIN_LENGTH);
-            if (comm.getCommSubType() == ROUTE_TARGET_SUBTYPE) {
-                c = new RouteTargetExtendedCommunityCaseBuilder().setRouteTargetExtendedCommunity(new RouteTargetExtendedCommunityBuilder().setGlobalAdministrator(as).setLocalAdministrator(value).build()).build();
-            } else if (comm.getCommSubType() == ROUTE_ORIGIN_SUBTYPE) {
-                c = new RouteOriginExtendedCommunityCaseBuilder().setRouteOriginExtendedCommunity(new RouteOriginExtendedCommunityBuilder().setGlobalAdministrator(as).setLocalAdministrator(value).build()).build();
-            } else {
-                throw new BGPDocumentedException("Could not parse Extended Community subtype: " + comm.getCommSubType(), BGPError.OPT_ATTR_ERROR);
-            }
+            c =  parseRouteTypeOnlyCommunity(buffer, comm.getCommSubType());
             break;
         case INET_TYPE_TRANS:
             c = parseInetTypeCommunity(comm, buffer);
@@ -153,6 +146,18 @@ public class ExtendedCommunitiesAttributeParser implements AttributeParser,Attri
         return comm.setExtendedCommunity(c).build();
     }
 
+    private static ExtendedCommunity parseRouteTypeOnlyCommunity(final ByteBuf buffer, final int subtype) throws BGPDocumentedException {
+        final ShortAsNumber as = new ShortAsNumber((long) buffer.readUnsignedShort());
+        final byte[] value = ByteArray.readBytes(buffer, AS_LOCAL_ADMIN_LENGTH);
+        if (subtype == ROUTE_TARGET_SUBTYPE) {
+            return new RouteTargetExtendedCommunityCaseBuilder().setRouteTargetExtendedCommunity(new RouteTargetExtendedCommunityBuilder().setGlobalAdministrator(as).setLocalAdministrator(value).build()).build();
+        } else if (subtype == ROUTE_ORIGIN_SUBTYPE) {
+            return new RouteOriginExtendedCommunityCaseBuilder().setRouteOriginExtendedCommunity(new RouteOriginExtendedCommunityBuilder().setGlobalAdministrator(as).setLocalAdministrator(value).build()).build();
+        } else {
+            throw new BGPDocumentedException("Could not parse Extended Community subtype: " + subtype, BGPError.OPT_ATTR_ERROR);
+        }
+    }
+
     private static ExtendedCommunity parseAsTransCommunity(final ExtendedCommunitiesBuilder comm, final ByteBuf buffer) {
         final ShortAsNumber as = new ShortAsNumber((long) buffer.readUnsignedShort());
         final byte[] value = ByteArray.readBytes(buffer, AS_LOCAL_ADMIN_LENGTH);
@@ -187,8 +192,8 @@ public class ExtendedCommunitiesAttributeParser implements AttributeParser,Attri
 
     @Override
     public void serializeAttribute(final DataObject attribute, final ByteBuf byteAggregator) {
-        Preconditions.checkArgument(attribute instanceof PathAttributes, "Attribute parameter is not a PathAttribute object.");
-        final List<ExtendedCommunities> communitiesList = ((PathAttributes) attribute).getExtendedCommunities();
+        Preconditions.checkArgument(attribute instanceof Attributes, "Attribute parameter is not a PathAttribute object.");
+        final List<ExtendedCommunities> communitiesList = ((Attributes) attribute).getExtendedCommunities();
         if (communitiesList == null) {
             return;
         }