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;
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;
}
@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();
}
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
*/
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);
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);
@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;
}