*/
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;
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 };
+
private final ReferenceCache refCache;
public CommunitiesAttributeParser(final ReferenceCache refCache) {
public void parseAttribute(final ByteBuf buffer, final PathAttributesBuilder 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.slice(buffer.readerIndex(), COMMUNITY_LENGTH)));
+ buffer.skipBytes(COMMUNITY_LENGTH);
}
builder.setCommunities(set);
}
+ /**
+ * Parse known Community, if unknown, a new one will be created.
+ *
+ * @param refCache
+ *
+ * @param bytes 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) {
Preconditions.checkArgument(attribute instanceof PathAttributes, "Attribute parameter is not a PathAttribute object.");
+++ /dev/null
-/*
- * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
- */
-package org.opendaylight.protocol.bgp.parser.impl.message.update;
-
-import com.google.common.annotations.VisibleForTesting;
-import com.google.common.primitives.UnsignedBytes;
-import io.netty.buffer.ByteBuf;
-import java.util.Arrays;
-import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
-import org.opendaylight.protocol.bgp.parser.BGPError;
-import org.opendaylight.protocol.util.ByteArray;
-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.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.types.rev130919.Community;
-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.extended.community.AsSpecificExtendedCommunityCaseBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.Inet4SpecificExtendedCommunityCaseBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.OpaqueExtendedCommunityCaseBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.RouteOriginExtendedCommunityCaseBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.RouteTargetExtendedCommunityCaseBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.as.specific.extended.community._case.AsSpecificExtendedCommunityBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.inet4.specific.extended.community._case.Inet4SpecificExtendedCommunityBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.opaque.extended.community._case.OpaqueExtendedCommunityBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.route.origin.extended.community._case.RouteOriginExtendedCommunityBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.route.target.extended.community._case.RouteTargetExtendedCommunityBuilder;
-
-/**
- * Parser for Extended Communities Path Attribute.
- */
-public final class CommunitiesParser {
-
- public static final int COMMUNITY_LENGTH = 4;
-
- private static final int AS_LOCAL_ADMIN_LENGTH = 4;
-
- private static final int INET_LOCAL_ADMIN_LENGTH = 2;
-
- private static final short AS_TYPE_TRANS = 0;
-
- private static final short AS_TYPE_NON_TRANS = 40;
-
- private static final short INET_TYPE_TRANS = 1;
-
- private static final short INET_TYPE_NON_TRANS = 41;
-
- private static final short OPAQUE_TYPE_TRANS = 3;
-
- private static final short OPAQUE_TYPE_NON_TRANS = 43;
-
- private static final short ROUTE_TYPE_ONLY = 2;
-
- private static final short ROUTE_TARGET_SUBTYPE = 2;
-
- private static final short ROUTE_ORIGIN_SUBTYPE = 3;
-
- 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 CommunitiesParser() {
-
- }
-
- /**
- * Parse known Community, if unknown, a new one will be created.
- *
- * @param refCache
- *
- * @param bytes byte array to be parsed
- * @return new Community
- * @throws BGPDocumentedException
- */
- 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());
- }
-
- /**
- * Parse Extended Community according to their type.
- *
- * @param bytes byte array to be parsed
- * @return new Specific Extended Community
- * @throws BGPDocumentedException if the type is not recognized
- */
- @VisibleForTesting
- public static ExtendedCommunities parseExtendedCommunity(final ReferenceCache refCache, final ByteBuf buffer)
- throws BGPDocumentedException {
- final int type = UnsignedBytes.toInt(buffer.readByte());
- final int subType = UnsignedBytes.toInt(buffer.readByte());
-
- final ExtendedCommunitiesBuilder comm = new ExtendedCommunitiesBuilder();
- switch (type) {
- case AS_TYPE_TRANS:
- comm.setCommType(AS_TYPE_TRANS);
- if (subType == ROUTE_TARGET_SUBTYPE) {
- comm.setCommSubType(ROUTE_TARGET_SUBTYPE).setExtendedCommunity(
- new RouteTargetExtendedCommunityCaseBuilder().setRouteTargetExtendedCommunity(
- new RouteTargetExtendedCommunityBuilder().setGlobalAdministrator(
- new ShortAsNumber((long) buffer.readUnsignedShort())).setLocalAdministrator(
- ByteArray.readBytes(buffer, AS_LOCAL_ADMIN_LENGTH)).build()).build());
- } else if (subType == ROUTE_ORIGIN_SUBTYPE) {
- comm.setCommSubType(ROUTE_ORIGIN_SUBTYPE).setExtendedCommunity(
- new RouteOriginExtendedCommunityCaseBuilder().setRouteOriginExtendedCommunity(
- new RouteOriginExtendedCommunityBuilder().setGlobalAdministrator(
- new ShortAsNumber((long) buffer.readUnsignedShort())).setLocalAdministrator(
- ByteArray.readBytes(buffer, AS_LOCAL_ADMIN_LENGTH)).build()).build());
- } else {
- comm.setExtendedCommunity(new AsSpecificExtendedCommunityCaseBuilder().setAsSpecificExtendedCommunity(
- new AsSpecificExtendedCommunityBuilder().setTransitive(false).setGlobalAdministrator(
- new ShortAsNumber((long) buffer.readUnsignedShort())).setLocalAdministrator(
- ByteArray.readBytes(buffer, AS_LOCAL_ADMIN_LENGTH)).build()).build());
- }
- break;
- case AS_TYPE_NON_TRANS:
- comm.setCommType(AS_TYPE_NON_TRANS).setExtendedCommunity(
- new AsSpecificExtendedCommunityCaseBuilder().setAsSpecificExtendedCommunity(
- new AsSpecificExtendedCommunityBuilder().setTransitive(true).setGlobalAdministrator(
- new ShortAsNumber((long) buffer.readUnsignedShort())).setLocalAdministrator(
- ByteArray.readBytes(buffer, AS_LOCAL_ADMIN_LENGTH)).build()).build());
- break;
- case ROUTE_TYPE_ONLY:
- comm.setCommType(ROUTE_TYPE_ONLY);
- if (subType == ROUTE_TARGET_SUBTYPE) {
- comm.setCommSubType(ROUTE_TARGET_SUBTYPE).setExtendedCommunity(
- new RouteTargetExtendedCommunityCaseBuilder().setRouteTargetExtendedCommunity(
- new RouteTargetExtendedCommunityBuilder().setGlobalAdministrator(
- new ShortAsNumber((long) buffer.readUnsignedShort())).setLocalAdministrator(
- ByteArray.readBytes(buffer, AS_LOCAL_ADMIN_LENGTH)).build()).build());
- } else if (subType == ROUTE_ORIGIN_SUBTYPE) {
- comm.setCommSubType(ROUTE_ORIGIN_SUBTYPE).setExtendedCommunity(
- new RouteOriginExtendedCommunityCaseBuilder().setRouteOriginExtendedCommunity(
- new RouteOriginExtendedCommunityBuilder().setGlobalAdministrator(
- new ShortAsNumber((long) buffer.readUnsignedShort())).setLocalAdministrator(
- ByteArray.readBytes(buffer, AS_LOCAL_ADMIN_LENGTH)).build()).build());
- } else {
- throw new BGPDocumentedException("Could not parse Extended Community subtype: " + subType, BGPError.OPT_ATTR_ERROR);
- }
- break;
- case INET_TYPE_TRANS:
- comm.setCommType(INET_TYPE_TRANS);
- if (subType == ROUTE_TARGET_SUBTYPE) {
- comm.setCommSubType(ROUTE_TARGET_SUBTYPE).setExtendedCommunity(
- new RouteTargetExtendedCommunityCaseBuilder().setRouteTargetExtendedCommunity(
- new RouteTargetExtendedCommunityBuilder().setGlobalAdministrator(
- new ShortAsNumber((long) buffer.readUnsignedShort())).setLocalAdministrator(
- ByteArray.readBytes(buffer, AS_LOCAL_ADMIN_LENGTH)).build()).build());
- } else if (subType == ROUTE_ORIGIN_SUBTYPE) {
- comm.setCommSubType(ROUTE_ORIGIN_SUBTYPE).setExtendedCommunity(
- new RouteOriginExtendedCommunityCaseBuilder().setRouteOriginExtendedCommunity(
- new RouteOriginExtendedCommunityBuilder().setGlobalAdministrator(
- new ShortAsNumber((long) buffer.readUnsignedShort())).setLocalAdministrator(
- ByteArray.readBytes(buffer, AS_LOCAL_ADMIN_LENGTH)).build()).build());
- } else {
- comm.setExtendedCommunity(new Inet4SpecificExtendedCommunityCaseBuilder().setInet4SpecificExtendedCommunity(
- new Inet4SpecificExtendedCommunityBuilder().setTransitive(false).setGlobalAdministrator(
- Ipv4Util.addressForBytes(ByteArray.readBytes(buffer, Ipv4Util.IP4_LENGTH))).setLocalAdministrator(
- ByteArray.readBytes(buffer, INET_LOCAL_ADMIN_LENGTH)).build()).build());
- }
- break;
- case INET_TYPE_NON_TRANS:
- comm.setCommType(INET_TYPE_NON_TRANS).setExtendedCommunity(
- new Inet4SpecificExtendedCommunityCaseBuilder().setInet4SpecificExtendedCommunity(
- new Inet4SpecificExtendedCommunityBuilder().setTransitive(true).setGlobalAdministrator(
- Ipv4Util.addressForBytes(ByteArray.readBytes(buffer, Ipv4Util.IP4_LENGTH))).setLocalAdministrator(
- ByteArray.readBytes(buffer, INET_LOCAL_ADMIN_LENGTH)).build()).build());
- break;
- case OPAQUE_TYPE_TRANS:
- comm.setCommType(OPAQUE_TYPE_TRANS).setExtendedCommunity(
- new OpaqueExtendedCommunityCaseBuilder().setOpaqueExtendedCommunity(
- new OpaqueExtendedCommunityBuilder().setTransitive(false).setValue(ByteArray.readAllBytes(buffer)).build()).build());
- break;
- case OPAQUE_TYPE_NON_TRANS:
- comm.setCommType(OPAQUE_TYPE_NON_TRANS).setExtendedCommunity(
- new OpaqueExtendedCommunityCaseBuilder().setOpaqueExtendedCommunity(
- new OpaqueExtendedCommunityBuilder().setTransitive(true).setValue(ByteArray.readAllBytes(buffer)).build()).build());
- break;
- default:
- throw new BGPDocumentedException("Could not parse Extended Community type: " + type, BGPError.OPT_ATTR_ERROR);
- }
-
- return comm.build();
- }
-}
*/
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.primitives.UnsignedBytes;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import java.util.ArrayList;
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.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.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;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.AsSpecificExtendedCommunityCaseBuilder;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.Inet4SpecificExtendedCommunityCase;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.Inet4SpecificExtendedCommunityCaseBuilder;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.OpaqueExtendedCommunityCase;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.OpaqueExtendedCommunityCaseBuilder;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.RouteOriginExtendedCommunityCase;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.RouteOriginExtendedCommunityCaseBuilder;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.RouteTargetExtendedCommunityCase;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.RouteTargetExtendedCommunityCaseBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.as.specific.extended.community._case.AsSpecificExtendedCommunity;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.as.specific.extended.community._case.AsSpecificExtendedCommunityBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.inet4.specific.extended.community._case.Inet4SpecificExtendedCommunity;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.inet4.specific.extended.community._case.Inet4SpecificExtendedCommunityBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.opaque.extended.community._case.OpaqueExtendedCommunity;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.opaque.extended.community._case.OpaqueExtendedCommunityBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.route.origin.extended.community._case.RouteOriginExtendedCommunity;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.route.origin.extended.community._case.RouteOriginExtendedCommunityBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.route.target.extended.community._case.RouteTargetExtendedCommunity;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.route.target.extended.community._case.RouteTargetExtendedCommunityBuilder;
import org.opendaylight.yangtools.yang.binding.DataObject;
public final class ExtendedCommunitiesAttributeParser implements AttributeParser,AttributeSerializer {
private static final int EXTENDED_COMMUNITY_LENGTH = 8;
+ private static final int AS_LOCAL_ADMIN_LENGTH = 4;
+
+ private static final int INET_LOCAL_ADMIN_LENGTH = 2;
+
+ private static final short AS_TYPE_TRANS = 0;
+
+ private static final short AS_TYPE_NON_TRANS = 40;
+
+ private static final short INET_TYPE_TRANS = 1;
+
+ private static final short INET_TYPE_NON_TRANS = 41;
+
+ private static final short OPAQUE_TYPE_TRANS = 3;
+
+ private static final short OPAQUE_TYPE_NON_TRANS = 43;
+
+ private static final short ROUTE_TYPE_ONLY = 2;
+
+ private static final short ROUTE_TARGET_SUBTYPE = 2;
+
+ private static final short ROUTE_ORIGIN_SUBTYPE = 3;
+
private final ReferenceCache refCache;
public ExtendedCommunitiesAttributeParser(final ReferenceCache refCache) {
public void parseAttribute(final ByteBuf buffer, final PathAttributesBuilder builder) throws BGPDocumentedException {
final List<ExtendedCommunities> set = new ArrayList<>();
while (buffer.isReadable()) {
- final ExtendedCommunities comm = CommunitiesParser.parseExtendedCommunity(this.refCache, buffer.slice(buffer.readerIndex(), EXTENDED_COMMUNITY_LENGTH));
+ final ExtendedCommunities comm = parseExtendedCommunity(this.refCache, buffer.slice(buffer.readerIndex(), EXTENDED_COMMUNITY_LENGTH));
buffer.skipBytes(EXTENDED_COMMUNITY_LENGTH);
set.add(comm);
}
builder.setExtendedCommunities(set);
}
+ /**
+ * Parse Extended Community according to their type.
+ *
+ * @param bytes byte array to be parsed
+ * @return new Specific Extended Community
+ * @throws BGPDocumentedException if the type is not recognized
+ */
+ @VisibleForTesting
+ public static ExtendedCommunities parseExtendedCommunity(final ReferenceCache refCache, final ByteBuf buffer)
+ throws BGPDocumentedException {
+ final ExtendedCommunitiesBuilder comm = new ExtendedCommunitiesBuilder();
+ comm.setCommType((short) UnsignedBytes.toInt(buffer.readByte()));
+ comm.setCommSubType((short) UnsignedBytes.toInt(buffer.readByte()));
+ ExtendedCommunity c = null;
+ switch (comm.getCommType()) {
+ case AS_TYPE_TRANS:
+ ShortAsNumber as = new ShortAsNumber((long) buffer.readUnsignedShort());
+ byte[] 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 {
+ c = new AsSpecificExtendedCommunityCaseBuilder().setAsSpecificExtendedCommunity(
+ new AsSpecificExtendedCommunityBuilder().setTransitive(false).setGlobalAdministrator(as).setLocalAdministrator(value).build()).build();
+ }
+ break;
+ case AS_TYPE_NON_TRANS:
+ as = new ShortAsNumber((long) buffer.readUnsignedShort());
+ 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);
+ }
+ break;
+ case INET_TYPE_TRANS:
+ if (comm.getCommSubType() == ROUTE_TARGET_SUBTYPE) {
+ c = new RouteTargetExtendedCommunityCaseBuilder().setRouteTargetExtendedCommunity(
+ new RouteTargetExtendedCommunityBuilder().setGlobalAdministrator(new ShortAsNumber((long) buffer.readUnsignedShort()))
+ .setLocalAdministrator(ByteArray.readBytes(buffer, AS_LOCAL_ADMIN_LENGTH)).build()).build();
+ } else if (comm.getCommSubType() == ROUTE_ORIGIN_SUBTYPE) {
+ c = new RouteOriginExtendedCommunityCaseBuilder().setRouteOriginExtendedCommunity(
+ new RouteOriginExtendedCommunityBuilder().setGlobalAdministrator(new ShortAsNumber((long) buffer.readUnsignedShort()))
+ .setLocalAdministrator(ByteArray.readBytes(buffer, AS_LOCAL_ADMIN_LENGTH)).build()).build();
+ } else {
+ c = new Inet4SpecificExtendedCommunityCaseBuilder().setInet4SpecificExtendedCommunity(
+ new Inet4SpecificExtendedCommunityBuilder().setTransitive(false).setGlobalAdministrator(
+ Ipv4Util.addressForBytes(ByteArray.readBytes(buffer, Ipv4Util.IP4_LENGTH))).setLocalAdministrator(
+ ByteArray.readBytes(buffer, INET_LOCAL_ADMIN_LENGTH)).build()).build();
+ }
+ break;
+ case INET_TYPE_NON_TRANS:
+ c = new Inet4SpecificExtendedCommunityCaseBuilder().setInet4SpecificExtendedCommunity(
+ new Inet4SpecificExtendedCommunityBuilder().setTransitive(true).setGlobalAdministrator(
+ Ipv4Util.addressForBytes(ByteArray.readBytes(buffer, Ipv4Util.IP4_LENGTH))).setLocalAdministrator(
+ ByteArray.readBytes(buffer, INET_LOCAL_ADMIN_LENGTH)).build()).build();
+ break;
+ case OPAQUE_TYPE_TRANS:
+ c = new OpaqueExtendedCommunityCaseBuilder().setOpaqueExtendedCommunity(
+ new OpaqueExtendedCommunityBuilder().setTransitive(false).setValue(ByteArray.readAllBytes(buffer)).build()).build();
+ break;
+ case OPAQUE_TYPE_NON_TRANS:
+ c = new OpaqueExtendedCommunityCaseBuilder().setOpaqueExtendedCommunity(
+ new OpaqueExtendedCommunityBuilder().setTransitive(true).setValue(ByteArray.readAllBytes(buffer)).build()).build();
+ break;
+ default:
+ throw new BGPDocumentedException("Could not parse Extended Community type: " + comm.getCommType(), BGPError.OPT_ATTR_ERROR);
+ }
+ return comm.setExtendedCommunity(c).build();
+ }
+
@Override
public void serializeAttribute(final DataObject attribute, final ByteBuf byteAggregator) {
Preconditions.checkArgument(attribute instanceof PathAttributes, "Attribute parameter is not a PathAttribute object.");
- final PathAttributes pathAttributes = (PathAttributes) attribute;
- final List<ExtendedCommunities> communitiesList = pathAttributes.getExtendedCommunities();
+ final List<ExtendedCommunities> communitiesList = ((PathAttributes) attribute).getExtendedCommunities();
if (communitiesList == null || communitiesList.isEmpty()) {
return;
}
final ByteBuf extendedCommunitiesBuffer = Unpooled.buffer();
for (final ExtendedCommunities extendedCommunities : communitiesList) {
- if (extendedCommunities.getCommSubType() != null) {
- extendedCommunitiesBuffer.writeShort(extendedCommunities.getCommSubType());
- }
- if (extendedCommunities.getExtendedCommunity() instanceof AsSpecificExtendedCommunityCase) {
- final AsSpecificExtendedCommunityCase asSpecificExtendedCommunity = (AsSpecificExtendedCommunityCase) extendedCommunities.getExtendedCommunity();
-
- //TODO resolve types correctly
- extendedCommunitiesBuffer.writeByte(0);
- extendedCommunitiesBuffer.writeByte(1);
-
- extendedCommunitiesBuffer.writeShort(asSpecificExtendedCommunity.getAsSpecificExtendedCommunity().getGlobalAdministrator().getValue().shortValue());
- extendedCommunitiesBuffer.writeBytes(asSpecificExtendedCommunity.getAsSpecificExtendedCommunity().getLocalAdministrator());
- }
- if (extendedCommunities.getExtendedCommunity() instanceof Inet4SpecificExtendedCommunityCase) {
- final Inet4SpecificExtendedCommunityCase inet4SpecificExtendedCommunity = (Inet4SpecificExtendedCommunityCase) extendedCommunities.getExtendedCommunity();
-
- //TODO resolve types correctly
- extendedCommunitiesBuffer.writeByte(1);
- extendedCommunitiesBuffer.writeByte(4);
-
- extendedCommunitiesBuffer.writeBytes(Ipv4Util.bytesForAddress(inet4SpecificExtendedCommunity.getInet4SpecificExtendedCommunity().getGlobalAdministrator()));
- extendedCommunitiesBuffer.writeBytes(inet4SpecificExtendedCommunity.getInet4SpecificExtendedCommunity().getLocalAdministrator());
- }
- if (extendedCommunities.getExtendedCommunity() instanceof OpaqueExtendedCommunityCase) {
- final OpaqueExtendedCommunityCase opaqueExtendedCommunity = (OpaqueExtendedCommunityCase) extendedCommunities.getExtendedCommunity();
- //TODO resolve types correctly
- extendedCommunitiesBuffer.writeByte(3);
- extendedCommunitiesBuffer.writeByte(4);
-
- extendedCommunitiesBuffer.writeBytes(opaqueExtendedCommunity.getOpaqueExtendedCommunity().getValue());
- }
- if (extendedCommunities.getExtendedCommunity() instanceof RouteTargetExtendedCommunityCase) {
- final RouteTargetExtendedCommunityCase routeTargetExtendedCommunity = (RouteTargetExtendedCommunityCase) extendedCommunities.getExtendedCommunity();
- //TODO how to determine, which numbering space global administrator number is originated from
- extendedCommunitiesBuffer.writeByte(0);
- extendedCommunitiesBuffer.writeByte(2);
-
- extendedCommunitiesBuffer.writeShort(routeTargetExtendedCommunity.getRouteTargetExtendedCommunity().getGlobalAdministrator().getValue().shortValue());
- extendedCommunitiesBuffer.writeBytes(routeTargetExtendedCommunity.getRouteTargetExtendedCommunity().getLocalAdministrator());
- }
- if (extendedCommunities.getExtendedCommunity() instanceof RouteOriginExtendedCommunityCase) {
- final RouteOriginExtendedCommunityCase routeOriginExtendedCommunity = (RouteOriginExtendedCommunityCase) extendedCommunities.getExtendedCommunity();
- //TODO how to determine, which numbering space global administrator number is originated from
- extendedCommunitiesBuffer.writeByte(2);
- extendedCommunitiesBuffer.writeByte(3);
- extendedCommunitiesBuffer.writeShort(routeOriginExtendedCommunity.getRouteOriginExtendedCommunity().getGlobalAdministrator().getValue().shortValue());
- extendedCommunitiesBuffer.writeBytes(routeOriginExtendedCommunity.getRouteOriginExtendedCommunity().getLocalAdministrator());
- }
+ serializeExtendedCommunity(extendedCommunities, extendedCommunitiesBuffer);
AttributeUtil.formatAttribute(AttributeUtil.OPTIONAL | AttributeUtil.TRANSITIVE, TYPE, extendedCommunitiesBuffer, byteAggregator);
}
}
+
+ @VisibleForTesting
+ public static void serializeExtendedCommunity(final ExtendedCommunities extendedCommunities, final ByteBuf extendedCommunitiesBuffer) {
+ if (extendedCommunities.getCommType() != null) {
+ extendedCommunitiesBuffer.writeByte(extendedCommunities.getCommType());
+ }
+ if (extendedCommunities.getCommSubType() != null) {
+ extendedCommunitiesBuffer.writeByte(extendedCommunities.getCommSubType());
+ }
+ final ExtendedCommunity ex = extendedCommunities.getExtendedCommunity();
+ if (ex instanceof AsSpecificExtendedCommunityCase) {
+ final AsSpecificExtendedCommunity asSpecificExtendedCommunity = ((AsSpecificExtendedCommunityCase) ex).getAsSpecificExtendedCommunity();
+ extendedCommunitiesBuffer.writeShort(asSpecificExtendedCommunity.getGlobalAdministrator().getValue().shortValue());
+ extendedCommunitiesBuffer.writeBytes(asSpecificExtendedCommunity.getLocalAdministrator());
+ }
+ else if (ex instanceof Inet4SpecificExtendedCommunityCase) {
+ final Inet4SpecificExtendedCommunity inet4SpecificExtendedCommunity = ((Inet4SpecificExtendedCommunityCase) ex).getInet4SpecificExtendedCommunity();
+ extendedCommunitiesBuffer.writeBytes(Ipv4Util.bytesForAddress(inet4SpecificExtendedCommunity.getGlobalAdministrator()));
+ extendedCommunitiesBuffer.writeBytes(inet4SpecificExtendedCommunity.getLocalAdministrator());
+ }
+ else if (ex instanceof OpaqueExtendedCommunityCase) {
+ final OpaqueExtendedCommunity opaqueExtendedCommunity = ((OpaqueExtendedCommunityCase) ex).getOpaqueExtendedCommunity();
+ extendedCommunitiesBuffer.writeBytes(opaqueExtendedCommunity.getValue());
+ }
+ else if (ex instanceof RouteTargetExtendedCommunityCase) {
+ final RouteTargetExtendedCommunity routeTargetExtendedCommunity = ((RouteTargetExtendedCommunityCase) ex).getRouteTargetExtendedCommunity();
+ extendedCommunitiesBuffer.writeShort(routeTargetExtendedCommunity.getGlobalAdministrator().getValue().shortValue());
+ extendedCommunitiesBuffer.writeBytes(routeTargetExtendedCommunity.getLocalAdministrator());
+ }
+ else if (ex instanceof RouteOriginExtendedCommunityCase) {
+ final RouteOriginExtendedCommunity routeOriginExtendedCommunity = ((RouteOriginExtendedCommunityCase) ex).getRouteOriginExtendedCommunity();
+ extendedCommunitiesBuffer.writeShort(routeOriginExtendedCommunity.getGlobalAdministrator().getValue().shortValue());
+ extendedCommunitiesBuffer.writeBytes(routeOriginExtendedCommunity.getLocalAdministrator());
+ }
+ }
}
new Ipv4NextHopBuilder().setGlobal(new Ipv4Address("3.3.3.3")).build()).build();
final List<ExtendedCommunities> comms = Lists.newArrayList();
- comms.add(new ExtendedCommunitiesBuilder().setCommType((short) 1).setExtendedCommunity(
+ comms.add(new ExtendedCommunitiesBuilder().setCommType((short) 1).setCommSubType((short) 4).setExtendedCommunity(
new Inet4SpecificExtendedCommunityCaseBuilder().setInet4SpecificExtendedCommunity(
new Inet4SpecificExtendedCommunityBuilder().setTransitive(false).setGlobalAdministrator(
new Ipv4Address("192.168.1.0")).setLocalAdministrator(new byte[] { 0x12, 0x34 }).build()).build()).build());
import static org.junit.Assert.fail;
import com.google.common.collect.Maps;
-
+import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
-
import java.util.Map;
-
import org.junit.Test;
import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
import org.opendaylight.protocol.bgp.parser.BgpTableTypeImpl;
-import org.opendaylight.protocol.bgp.parser.impl.message.update.CommunitiesParser;
+import org.opendaylight.protocol.bgp.parser.impl.message.update.ExtendedCommunitiesAttributeParser;
import org.opendaylight.protocol.bgp.parser.spi.MessageRegistry;
import org.opendaylight.protocol.bgp.parser.spi.pojo.ServiceLoaderBGPExtensionProviderContext;
+import org.opendaylight.protocol.util.ByteArray;
import org.opendaylight.protocol.util.NoopReferenceCache;
import org.opendaylight.protocol.util.ReferenceCache;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
}
@Test
- public void testCommunitiesParser() {
+ public void testExtendedCommunitiesParser() {
ExtendedCommunities as = null;
try {
- as = CommunitiesParser.parseExtendedCommunity(this.ref, Unpooled.copiedBuffer(new byte[] { 0, 5, 0, 54, 0, 0, 1, 76 }));
+ as = ExtendedCommunitiesAttributeParser.parseExtendedCommunity(this.ref, Unpooled.copiedBuffer(new byte[] { 0, 5, 0, 54, 0, 0, 1, 76 }));
} catch (final BGPDocumentedException e1) {
fail("Not expected exception: " + e1);
}
result.getAsSpecificExtendedCommunity().getLocalAdministrator());
assertEquals(0, as.getCommType().intValue());
+ final ByteBuf serializedBuffer = Unpooled.buffer();
+ ExtendedCommunitiesAttributeParser.serializeExtendedCommunity(as, serializedBuffer);
+ assertArrayEquals(new byte[] { 0, 5, 0, 54, 0, 0, 1, 76 }, ByteArray.readAllBytes(serializedBuffer));
+
try {
- as = CommunitiesParser.parseExtendedCommunity(this.ref, Unpooled.copiedBuffer(new byte[] { 40, 5, 0, 54, 0, 0, 1, 76 }));
+ as = ExtendedCommunitiesAttributeParser.parseExtendedCommunity(this.ref, Unpooled.copiedBuffer(new byte[] { 40, 5, 0, 54, 0, 0, 1, 76 }));
} catch (final BGPDocumentedException e1) {
fail("Not expected exception: " + e1);
}
assertEquals(expected.getAsSpecificExtendedCommunity().isTransitive(), result.getAsSpecificExtendedCommunity().isTransitive());
assertEquals(40, as.getCommType().intValue());
+ serializedBuffer.clear();
+ ExtendedCommunitiesAttributeParser.serializeExtendedCommunity(as, serializedBuffer);
+ assertArrayEquals(new byte[] { 40, 5, 0, 54, 0, 0, 1, 76 }, ByteArray.readAllBytes(serializedBuffer));
+
ExtendedCommunities rtc = null;
try {
- rtc = CommunitiesParser.parseExtendedCommunity(this.ref, Unpooled.copiedBuffer(new byte[] { 1, 2, 0, 35, 4, 2, 8, 7 }));
+ rtc = ExtendedCommunitiesAttributeParser.parseExtendedCommunity(this.ref, Unpooled.copiedBuffer(new byte[] { 1, 2, 0, 35, 4, 2, 8, 7 }));
} catch (final BGPDocumentedException e1) {
fail("Not expected exception: " + e1);
}
assertEquals(1, rtc.getCommType().intValue());
assertEquals(2, rtc.getCommSubType().intValue());
+ serializedBuffer.clear();
+ ExtendedCommunitiesAttributeParser.serializeExtendedCommunity(rtc, serializedBuffer);
+ assertArrayEquals(new byte[] { 1, 2, 0, 35, 4, 2, 8, 7 }, ByteArray.readAllBytes(serializedBuffer));
+
ExtendedCommunities roc = null;
try {
- roc = CommunitiesParser.parseExtendedCommunity(this.ref, Unpooled.copiedBuffer(new byte[] { 0, 3, 0, 24, 4, 2, 8, 7 }));
+ roc = ExtendedCommunitiesAttributeParser.parseExtendedCommunity(this.ref, Unpooled.copiedBuffer(new byte[] { 0, 3, 0, 24, 4, 2, 8, 7 }));
} catch (final BGPDocumentedException e1) {
fail("Not expected exception: " + e1);
}
assertEquals(0, roc.getCommType().intValue());
assertEquals(3, roc.getCommSubType().intValue());
+ serializedBuffer.clear();
+ ExtendedCommunitiesAttributeParser.serializeExtendedCommunity(roc, serializedBuffer);
+ assertArrayEquals(new byte[] { 0, 3, 0, 24, 4, 2, 8, 7 }, ByteArray.readAllBytes(serializedBuffer));
+
ExtendedCommunities sec = null;
try {
- sec = CommunitiesParser.parseExtendedCommunity(this.ref, Unpooled.copiedBuffer(new byte[] { 41, 6, 12, 51, 2, 5, 21, 45 }));
+ sec = ExtendedCommunitiesAttributeParser.parseExtendedCommunity(this.ref, Unpooled.copiedBuffer(new byte[] { 41, 6, 12, 51, 2, 5, 21, 45 }));
} catch (final BGPDocumentedException e1) {
fail("Not expected exception: " + e1);
}
iresult.getInet4SpecificExtendedCommunity().getLocalAdministrator());
assertEquals(41, sec.getCommType().intValue());
+ serializedBuffer.clear();
+ ExtendedCommunitiesAttributeParser.serializeExtendedCommunity(sec, serializedBuffer);
+ assertArrayEquals(new byte[] { 41, 6, 12, 51, 2, 5, 21, 45 }, ByteArray.readAllBytes(serializedBuffer));
+
ExtendedCommunities oec = null;
try {
- oec = CommunitiesParser.parseExtendedCommunity(this.ref, Unpooled.copiedBuffer(new byte[] { 3, 6, 21, 45, 5, 4, 3, 1 }));
+ oec = ExtendedCommunitiesAttributeParser.parseExtendedCommunity(this.ref, Unpooled.copiedBuffer(new byte[] { 3, 6, 21, 45, 5, 4, 3, 1 }));
} catch (final BGPDocumentedException e1) {
fail("Not expected exception: " + e1);
}
assertArrayEquals(oeexpected.getOpaqueExtendedCommunity().getValue(), oeresult.getOpaqueExtendedCommunity().getValue());
assertEquals(3, oec.getCommType().intValue());
+ serializedBuffer.clear();
+ ExtendedCommunitiesAttributeParser.serializeExtendedCommunity(oec, serializedBuffer);
+ assertArrayEquals(new byte[] { 3, 6, 21, 45, 5, 4, 3, 1 }, ByteArray.readAllBytes(serializedBuffer));
+
try {
- oec = CommunitiesParser.parseExtendedCommunity(this.ref, Unpooled.copiedBuffer(new byte[] { 43, 6, 21, 45, 5, 4, 3, 1 }));
+ oec = ExtendedCommunitiesAttributeParser.parseExtendedCommunity(this.ref, Unpooled.copiedBuffer(new byte[] { 43, 6, 21, 45, 5, 4, 3, 1 }));
} catch (final BGPDocumentedException e1) {
fail("Not expected exception: " + e1);
}
assertArrayEquals(oeexpected1.getOpaqueExtendedCommunity().getValue(), oeresult1.getOpaqueExtendedCommunity().getValue());
assertEquals(43, oec.getCommType().intValue());
+ serializedBuffer.clear();
+ ExtendedCommunitiesAttributeParser.serializeExtendedCommunity(oec, serializedBuffer);
+ assertArrayEquals(new byte[] { 43, 6, 21, 45, 5, 4, 3, 1 }, ByteArray.readAllBytes(serializedBuffer));
+
try {
- CommunitiesParser.parseExtendedCommunity(this.ref, Unpooled.copiedBuffer(new byte[] { 11, 11, 21, 45, 5, 4, 3, 1 }));
+ ExtendedCommunitiesAttributeParser.parseExtendedCommunity(this.ref, Unpooled.copiedBuffer(new byte[] { 11, 11, 21, 45, 5, 4, 3, 1 }));
fail("Exception should have occured.");
} catch (final BGPDocumentedException e) {
assertEquals("Could not parse Extended Community type: 11", e.getMessage());