Use reference cache for Communities and ASes 23/5123/1
authorRobert Varga <rovarga@cisco.com>
Wed, 5 Feb 2014 03:01:01 +0000 (04:01 +0100)
committerRobert Varga <rovarga@cisco.com>
Wed, 5 Feb 2014 03:01:01 +0000 (04:01 +0100)
These two type of objects get reused over and over again, so it makes
sense to actually use refcaching on them.

Change-Id: I9c63ab23cdf0480055e7a868a73107c4ac9926ff
Signed-off-by: Robert Varga <rovarga@cisco.com>
bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/BGPActivator.java
bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/update/AggregatorAttributeParser.java
bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/update/AsPathAttributeParser.java
bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/update/AsPathSegmentParser.java
bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/update/CommunitiesAttributeParser.java
bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/update/CommunitiesParser.java
bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/update/CommunityUtil.java
bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/update/ExtendedCommunitiesAttributeParser.java
bgp/parser-impl/src/test/java/org/opendaylight/protocol/bgp/parser/impl/BGPParserTest.java
bgp/parser-impl/src/test/java/org/opendaylight/protocol/bgp/parser/impl/CommunityTest.java
bgp/parser-impl/src/test/java/org/opendaylight/protocol/bgp/parser/impl/ComplementaryTest.java

index 972105b56a97c32d05cd515ba9c26e2fea60a7bd..890ca5e1958bfef0fab826b8a3f5aa4acd71ed95 100644 (file)
@@ -74,18 +74,18 @@ public final class BGPActivator extends AbstractBGPExtensionProviderActivator {
 
                final AttributeRegistry attrReg = context.getAttributeRegistry();
                regs.add(context.registerAttributeParser(OriginAttributeParser.TYPE, new OriginAttributeParser()));
-               regs.add(context.registerAttributeParser(AsPathAttributeParser.TYPE, new AsPathAttributeParser()));
+               regs.add(context.registerAttributeParser(AsPathAttributeParser.TYPE, new AsPathAttributeParser(context.getReferenceCache())));
                regs.add(context.registerAttributeParser(NextHopAttributeParser.TYPE, new NextHopAttributeParser()));
                regs.add(context.registerAttributeParser(MultiExitDiscriminatorAttributeParser.TYPE, new MultiExitDiscriminatorAttributeParser()));
                regs.add(context.registerAttributeParser(LocalPreferenceAttributeParser.TYPE, new LocalPreferenceAttributeParser()));
                regs.add(context.registerAttributeParser(AtomicAggregateAttributeParser.TYPE, new AtomicAggregateAttributeParser()));
-               regs.add(context.registerAttributeParser(AggregatorAttributeParser.TYPE, new AggregatorAttributeParser()));
-               regs.add(context.registerAttributeParser(CommunitiesAttributeParser.TYPE, new CommunitiesAttributeParser()));
+               regs.add(context.registerAttributeParser(AggregatorAttributeParser.TYPE, new AggregatorAttributeParser(context.getReferenceCache())));
+               regs.add(context.registerAttributeParser(CommunitiesAttributeParser.TYPE, new CommunitiesAttributeParser(context.getReferenceCache())));
                regs.add(context.registerAttributeParser(OriginatorIdAttributeParser.TYPE, new OriginatorIdAttributeParser()));
                regs.add(context.registerAttributeParser(ClusterIdAttributeParser.TYPE, new ClusterIdAttributeParser()));
                regs.add(context.registerAttributeParser(MPReachAttributeParser.TYPE, new MPReachAttributeParser(nlriReg)));
                regs.add(context.registerAttributeParser(MPUnreachAttributeParser.TYPE, new MPUnreachAttributeParser(nlriReg)));
-               regs.add(context.registerAttributeParser(ExtendedCommunitiesAttributeParser.TYPE, new ExtendedCommunitiesAttributeParser()));
+               regs.add(context.registerAttributeParser(ExtendedCommunitiesAttributeParser.TYPE, new ExtendedCommunitiesAttributeParser(context.getReferenceCache())));
                regs.add(context.registerAttributeParser(AS4AggregatorAttributeParser.TYPE, new AS4AggregatorAttributeParser()));
                regs.add(context.registerAttributeParser(AS4PathAttributeParser.TYPE, new AS4PathAttributeParser()));
 
index 54faef9f5719504b07dd4bc2b86bc19c97c71caa..022b9579d664ac52421ae78c93602c023bd59145 100644 (file)
@@ -10,23 +10,32 @@ package org.opendaylight.protocol.bgp.parser.impl.message.update;
 import org.opendaylight.protocol.bgp.parser.spi.AttributeParser;
 import org.opendaylight.protocol.concepts.Ipv4Util;
 import org.opendaylight.protocol.util.ByteArray;
+import org.opendaylight.protocol.util.ReferenceCache;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.Aggregator;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.AggregatorBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.PathAttributesBuilder;
 
+import com.google.common.base.Preconditions;
+
 public final class AggregatorAttributeParser implements AttributeParser {
        public static final int TYPE = 7;
 
+       private final ReferenceCache refCache;
+
+       public AggregatorAttributeParser(final ReferenceCache refCache) {
+               this.refCache = Preconditions.checkNotNull(refCache);
+       }
+
        /**
         * Parse AGGREGATOR from bytes
         * 
         * @param bytes byte array to be parsed
         * @return {@link Aggregator} BGP Aggregator
         */
-       private static Aggregator parseAggregator(final byte[] bytes) {
-               final AsNumber asNumber = new AsNumber(ByteArray.bytesToLong(ByteArray.subByte(bytes, 0, AsPathSegmentParser.AS_NUMBER_LENGTH)));
+       private Aggregator parseAggregator(final byte[] bytes) {
+               final AsNumber asNumber = refCache.getSharedReference(new AsNumber(ByteArray.bytesToLong(ByteArray.subByte(bytes, 0, AsPathSegmentParser.AS_NUMBER_LENGTH))));
                final Ipv4Address address = Ipv4Util.addressForBytes(ByteArray.subByte(bytes, AsPathSegmentParser.AS_NUMBER_LENGTH, 4));
                return new AggregatorBuilder().setAsNumber(asNumber).setNetworkAddress(address).build();
        }
index 9ba416d5976286ccdafecc2561b6c64c75374c0a..4a8f993413dcc9207772142235ce1c1d5a0b2199 100644 (file)
@@ -15,6 +15,7 @@ import org.opendaylight.protocol.bgp.parser.BGPParsingException;
 import org.opendaylight.protocol.bgp.parser.impl.message.update.AsPathSegmentParser.SegmentType;
 import org.opendaylight.protocol.bgp.parser.spi.AttributeParser;
 import org.opendaylight.protocol.util.ByteArray;
+import org.opendaylight.protocol.util.ReferenceCache;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.AsPath;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.AsPathBuilder;
@@ -27,11 +28,17 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.type
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.as.path.segment.c.segment.a.list._case.a.list.AsSequence;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.as.path.segment.c.segment.a.set._case.ASetBuilder;
 
+import com.google.common.base.Preconditions;
 import com.google.common.collect.Lists;
 import com.google.common.primitives.UnsignedBytes;
 
 public final class AsPathAttributeParser implements AttributeParser {
        public static final int TYPE = 2;
+       private final ReferenceCache refCache;
+
+       public AsPathAttributeParser(final ReferenceCache refCache) {
+               this.refCache = Preconditions.checkNotNull(refCache);
+       }
 
        /**
         * Parses AS_PATH from bytes.
@@ -41,7 +48,7 @@ public final class AsPathAttributeParser implements AttributeParser {
         * @throws BGPDocumentedException if there is no AS_SEQUENCE present (mandatory)
         * @throws BGPParsingException
         */
-       private static AsPath parseAsPath(final byte[] bytes) throws BGPDocumentedException, BGPParsingException {
+       private static AsPath parseAsPath(final ReferenceCache refCache, final byte[] bytes) throws BGPDocumentedException, BGPParsingException {
                int byteOffset = 0;
                final List<Segments> ases = Lists.newArrayList();
                boolean isSequence = false;
@@ -57,13 +64,13 @@ public final class AsPathAttributeParser implements AttributeParser {
                        byteOffset += AsPathSegmentParser.LENGTH_SIZE;
 
                        if (segmentType == SegmentType.AS_SEQUENCE) {
-                               final List<AsSequence> numbers = AsPathSegmentParser.parseAsSequence(count,
+                               final List<AsSequence> numbers = AsPathSegmentParser.parseAsSequence(refCache, count,
                                                ByteArray.subByte(bytes, byteOffset, count * AsPathSegmentParser.AS_NUMBER_LENGTH));
                                ases.add(new SegmentsBuilder().setCSegment(
                                                new AListCaseBuilder().setAList(new AListBuilder().setAsSequence(numbers).build()).build()).build());
                                isSequence = true;
                        } else {
-                               final List<AsNumber> list = AsPathSegmentParser.parseAsSet(count,
+                               final List<AsNumber> list = AsPathSegmentParser.parseAsSet(refCache, count,
                                                ByteArray.subByte(bytes, byteOffset, count * AsPathSegmentParser.AS_NUMBER_LENGTH));
                                ases.add(new SegmentsBuilder().setCSegment(new ASetCaseBuilder().setASet(new ASetBuilder().setAsSet(list).build()).build()).build());
 
@@ -79,6 +86,6 @@ public final class AsPathAttributeParser implements AttributeParser {
 
        @Override
        public void parseAttribute(final byte[] bytes, final PathAttributesBuilder builder) throws BGPDocumentedException, BGPParsingException {
-               builder.setAsPath(parseAsPath(bytes));
+               builder.setAsPath(parseAsPath(refCache, bytes));
        }
 }
\ No newline at end of file
index fa9caede622aba7b0cffd84e973d6d4194bf280b..b856db25a8d92e30c5953494c090cb88b44a0827 100644 (file)
@@ -12,6 +12,7 @@ import java.util.ArrayList;
 import java.util.List;
 
 import org.opendaylight.protocol.util.ByteArray;
+import org.opendaylight.protocol.util.ReferenceCache;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.as.path.segment.c.segment.a.list._case.a.list.AsSequence;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.as.path.segment.c.segment.a.list._case.a.list.AsSequenceBuilder;
@@ -53,22 +54,26 @@ public final class AsPathSegmentParser {
                }
        }
 
-       static List<AsSequence> parseAsSequence(final int count, final byte[] bytes) {
+       static List<AsSequence> parseAsSequence(final ReferenceCache refCache, final int count, final byte[] bytes) {
                final List<AsSequence> coll = new ArrayList<>();
                int byteOffset = 0;
                for (int i = 0; i < count; i++) {
-                       coll.add(new AsSequenceBuilder().setAs(
-                                       new AsNumber(ByteArray.bytesToLong(ByteArray.subByte(bytes, byteOffset, AS_NUMBER_LENGTH)))).build());
+                       coll.add(
+                                       refCache.getSharedReference(
+                                                       new AsSequenceBuilder().setAs(
+                                                                       refCache.getSharedReference(
+                                                                                       new AsNumber(ByteArray.bytesToLong(ByteArray.subByte(bytes, byteOffset, AS_NUMBER_LENGTH))))).build()));
                        byteOffset += AS_NUMBER_LENGTH;
                }
                return coll;
        }
 
-       static List<AsNumber> parseAsSet(final int count, final byte[] bytes) {
+       static List<AsNumber> parseAsSet(final ReferenceCache refCache, final int count, final byte[] bytes) {
                final List<AsNumber> coll = new ArrayList<>();
                int byteOffset = 0;
                for (int i = 0; i < count; i++) {
-                       coll.add(new AsNumber(ByteArray.bytesToLong(ByteArray.subByte(bytes, byteOffset, AS_NUMBER_LENGTH))));
+                       coll.add(refCache.getSharedReference(
+                                       new AsNumber(ByteArray.bytesToLong(ByteArray.subByte(bytes, byteOffset, AS_NUMBER_LENGTH)))));
                        byteOffset += AS_NUMBER_LENGTH;
                }
                return coll;
index dd45dfc4a010654b1ee45b753d30c158d58e1387..9e8359094df847c7d82feafa5dc30a18bc87c80e 100644 (file)
@@ -12,20 +12,28 @@ import java.util.List;
 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
 import org.opendaylight.protocol.bgp.parser.spi.AttributeParser;
 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 com.google.common.base.Preconditions;
 import com.google.common.collect.Lists;
 
 public final class CommunitiesAttributeParser implements AttributeParser {
        public static final int TYPE = 8;
 
+       private final ReferenceCache refCache;
+
+       public CommunitiesAttributeParser(final ReferenceCache refCache) {
+               this.refCache = Preconditions.checkNotNull(refCache);
+       }
+
        @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(ByteArray.subByte(bytes, i, CommunitiesParser.COMMUNITY_LENGTH)));
+                       set.add((Communities) CommunitiesParser.parseCommunity(refCache, ByteArray.subByte(bytes, i, CommunitiesParser.COMMUNITY_LENGTH)));
                        i += CommunitiesParser.COMMUNITY_LENGTH;
                }
 
index 48a68c6abcb0ce55ec4307a4a00c409962b3c454..ff360410d864dc66593971a21ed23dd7a73695f6 100644 (file)
@@ -14,6 +14,7 @@ import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
 import org.opendaylight.protocol.bgp.parser.BGPError;
 import org.opendaylight.protocol.concepts.Ipv4Util;
 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.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;
@@ -77,12 +78,13 @@ public final class 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 byte[] bytes) throws BGPDocumentedException {
+       static Community parseCommunity(final ReferenceCache refCache, final byte[] bytes) throws BGPDocumentedException {
                if (bytes.length != COMMUNITY_LENGTH) {
                        throw new BGPDocumentedException("Community with wrong length: " + bytes.length, BGPError.OPT_ATTR_ERROR);
                }
@@ -93,7 +95,7 @@ public final class CommunitiesParser {
                } else if (Arrays.equals(bytes, NO_EXPORT_SUBCONFED)) {
                        return CommunityUtil.NO_EXPORT_SUBCONFED;
                }
-               return CommunityUtil.create((ByteArray.bytesToLong(Arrays.copyOfRange(bytes, 0, AS_NUMBER_LENGTH))),
+               return CommunityUtil.create(refCache, ByteArray.bytesToLong(Arrays.copyOfRange(bytes, 0, AS_NUMBER_LENGTH)),
                                ByteArray.bytesToInt(Arrays.copyOfRange(bytes, AS_NUMBER_LENGTH, AS_NUMBER_LENGTH + AS_NUMBER_LENGTH)));
        }
 
@@ -105,7 +107,7 @@ public final class CommunitiesParser {
         * @throws BGPDocumentedException if the type is not recognized
         */
        @VisibleForTesting
-       public static ExtendedCommunities parseExtendedCommunity(final byte[] bytes) throws BGPDocumentedException {
+       public static ExtendedCommunities parseExtendedCommunity(final ReferenceCache refCache, final byte[] bytes) throws BGPDocumentedException {
                final int type = UnsignedBytes.toInt(bytes[0]);
                final int subType = UnsignedBytes.toInt(bytes[1]);
                final byte[] value = ByteArray.subByte(bytes, TYPE_LENGTH, bytes.length - TYPE_LENGTH);
index 1f399095c8332fa287e6ae94901b16067ec79b8b..7e0a37f0dc9093586aecf866fb68a29c97f08960 100644 (file)
@@ -7,10 +7,14 @@
  */
 package org.opendaylight.protocol.bgp.parser.impl.message.update;
 
+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;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.CommunitiesBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Community;
 
+import com.google.common.base.Preconditions;
+
 /**
  * Object representation of a RFC1997 Community tag. Communities are a way for the advertising entity to attach semantic
  * meaning/policy to advertised objects.
@@ -21,48 +25,76 @@ public final class CommunityUtil {
         * advertised outside a BGP confederation boundary (a stand-alone autonomous system that is not part of a
         * confederation should be considered a confederation itself).
         */
-       public static final Community NO_EXPORT = CommunityUtil.create(0xFFFF, 0xFF01);
+       public static final Community NO_EXPORT =
+                       CommunityUtil.create(NoopReferenceCache.getInstance(), 0xFFFF, 0xFF01);
        /**
         * NO_ADVERTISE community. All routes received carrying a communities attribute containing this value MUST NOT be
         * advertised to other BGP peers.
         */
-       public static final Community NO_ADVERTISE = CommunityUtil.create(0xFFFF, 0xFF02);
+       public static final Community NO_ADVERTISE =
+                       CommunityUtil.create(NoopReferenceCache.getInstance(), 0xFFFF, 0xFF02);
        /**
         * NO_EXPORT_SUBCONFED community. All routes received carrying a communities attribute containing this value MUST
         * NOT be advertised to external BGP peers (this includes peers in other members autonomous systems inside a BGP
         * confederation).
         */
-       public static final Community NO_EXPORT_SUBCONFED = CommunityUtil.create(0xFFFF, 0xFF03);
+       public static final Community NO_EXPORT_SUBCONFED =
+                       CommunityUtil.create(NoopReferenceCache.getInstance(), 0xFFFF, 0xFF03);
+
+       private final ReferenceCache refCache;
 
-       private CommunityUtil() {
+       public CommunityUtil(final ReferenceCache refCache) {
+               this.refCache = Preconditions.checkNotNull(refCache);
+       }
+
+       /**
+        * Creates a new Community given AS number value and semantics using generated CommunitiesBuilder.
+        * 
+        * @param asn long
+        * @param semantics long
+        * @return new Community
+        */
+       public Community create(final long asn, final int semantics) {
+               return create(refCache, asn, semantics);
+       }
 
+       /**
+        * Creates a Community from its String representation.
+        * 
+        * @param string String representation of a community
+        * @return new Community
+        */
+       public Community valueOf(final String string) {
+               return valueOf(refCache, string);
        }
 
        /**
         * Creates a new Community given AS number value and semantics using generated CommunitiesBuilder.
         * 
+        * @param refCache reference cache to use
         * @param asn long
         * @param semantics long
         * @return new Community
         */
-       public static Community create(final long asn, final int semantics) {
+       public static Community create(final ReferenceCache refCache, final long asn, final int semantics) {
                final CommunitiesBuilder builder = new CommunitiesBuilder();
-               builder.setAsNumber(new AsNumber(asn));
-               builder.setSemantics(semantics);
-               return builder.build();
+               builder.setAsNumber(refCache.getSharedReference(new AsNumber(asn)));
+               builder.setSemantics(refCache.getSharedReference(semantics));
+               return refCache.getSharedReference(builder.build());
        }
 
        /**
         * Creates a Community from its String representation.
         * 
+        * @param refCache reference cache to use
         * @param string String representation of a community
         * @return new Community
         */
-       public static Community valueOf(final String string) {
+       public static Community valueOf(final ReferenceCache refCache, final String string) {
                final String[] parts = string.split(":", 2);
                final CommunitiesBuilder builder = new CommunitiesBuilder();
-               builder.setAsNumber(new AsNumber(Long.valueOf(parts[0])));
-               builder.setSemantics(Integer.valueOf(parts[1]));
-               return builder.build();
+               builder.setAsNumber(refCache.getSharedReference(new AsNumber(Long.valueOf(parts[0]))));
+               builder.setSemantics(refCache.getSharedReference(Integer.valueOf(parts[1])));
+               return refCache.getSharedReference(builder.build());
        }
 }
index a997f6fc890dacabc27492136609a9a7d3b33fad..26f97398b4aadced75562a638762d853c073f988 100644 (file)
@@ -12,20 +12,28 @@ import java.util.List;
 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
 import org.opendaylight.protocol.bgp.parser.spi.AttributeParser;
 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.ExtendedCommunities;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.PathAttributesBuilder;
 
+import com.google.common.base.Preconditions;
 import com.google.common.collect.Lists;
 
 public final class ExtendedCommunitiesAttributeParser implements AttributeParser {
        public static final int TYPE = 16;
 
+       private final ReferenceCache refCache;
+
+       public ExtendedCommunitiesAttributeParser(final ReferenceCache refCache) {
+               this.refCache = Preconditions.checkNotNull(refCache);
+       }
+
        @Override
        public void parseAttribute(final byte[] bytes, final PathAttributesBuilder builder) throws BGPDocumentedException {
                final List<ExtendedCommunities> set = Lists.newArrayList();
                int i = 0;
                while (i < bytes.length) {
-                       final ExtendedCommunities comm = CommunitiesParser.parseExtendedCommunity(ByteArray.subByte(bytes, i,
+                       final ExtendedCommunities comm = CommunitiesParser.parseExtendedCommunity(refCache, ByteArray.subByte(bytes, i,
                                        CommunitiesParser.EXTENDED_COMMUNITY_LENGTH));
                        i += CommunitiesParser.EXTENDED_COMMUNITY_LENGTH;
                        set.add(comm);
index 61d0f63c2937fa2fb4436983b65240b90264bc88..15d5722b3a22a022ded5073077c763ec6ead9188 100644 (file)
@@ -28,6 +28,7 @@ import org.opendaylight.protocol.bgp.parser.spi.MessageRegistry;
 import org.opendaylight.protocol.bgp.parser.spi.MessageUtil;
 import org.opendaylight.protocol.bgp.parser.spi.pojo.ServiceLoaderBGPExtensionProviderContext;
 import org.opendaylight.protocol.util.ByteArray;
+import org.opendaylight.protocol.util.NoopReferenceCache;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
@@ -224,7 +225,7 @@ public class BGPParserTest {
                comms.add((Communities) CommunityUtil.NO_EXPORT);
                comms.add((Communities) CommunityUtil.NO_ADVERTISE);
                comms.add((Communities) CommunityUtil.NO_EXPORT_SUBCONFED);
-               comms.add((Communities) CommunityUtil.create(0xFFFF, 0xFF10));
+               comms.add((Communities) CommunityUtil.create(NoopReferenceCache.getInstance(), 0xFFFF, 0xFF10));
 
                final UpdateBuilder builder = new UpdateBuilder();
 
index e748c592dd7dce8b9f84151938f3eb3807b91dc0..b3c157d2c35db910aa099b0e422808cbef06db0e 100644 (file)
@@ -13,14 +13,16 @@ import static org.junit.Assert.fail;
 
 import org.junit.Test;
 import org.opendaylight.protocol.bgp.parser.impl.message.update.CommunityUtil;
+import org.opendaylight.protocol.util.NoopReferenceCache;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Community;
 
 public class CommunityTest {
+       final CommunityUtil util = new CommunityUtil(NoopReferenceCache.getInstance());
 
        @Test
        public void testCommunity() {
-               CommunityUtil.create(10, 222);
-               final Community c = CommunityUtil.create(12, 12);
+               util.create(10, 222);
+               final Community c = util.create(12, 12);
                assertEquals(12, c.getAsNumber().getValue().intValue());
                assertEquals(12, c.getSemantics().intValue());
        }
@@ -28,13 +30,13 @@ public class CommunityTest {
        @Test
        public void testOverflows() {
                try {
-                       CommunityUtil.create(10, -2);
+                       util.create(10, -2);
                        fail("Semantics under range.");
                } catch (final IllegalArgumentException e) {
                        assertEquals("Invalid range: -2, expected: [[0‥65535]].", e.getMessage());
                }
                try {
-                       CommunityUtil.create(10, 65536);
+                       util.create(10, 65536);
                        fail("Semantics above range.");
                } catch (final IllegalArgumentException e) {
                        assertEquals("Invalid range: 65536, expected: [[0‥65535]].", e.getMessage());
@@ -43,13 +45,13 @@ public class CommunityTest {
 
        @Test
        public void testToString() {
-               final Community c = CommunityUtil.create(10, 222);
+               final Community c = util.create(10, 222);
                assertNotNull(c.toString());
        }
 
        @Test
        public void testValueOf() {
-               final Community comm = CommunityUtil.valueOf("12:50");
+               final Community comm = util.valueOf("12:50");
                assertEquals(12, comm.getAsNumber().getValue().intValue());
                assertEquals(50, comm.getSemantics().intValue());
        }
index f9431937a3d6fe0a28525e3d47de184253920147..bcb01816ed7a1f738e26052894a3c7b71e2194ca 100644 (file)
@@ -22,6 +22,8 @@ import org.opendaylight.protocol.bgp.parser.impl.message.update.CommunitiesParse
 import org.opendaylight.protocol.bgp.parser.spi.MessageRegistry;
 import org.opendaylight.protocol.bgp.parser.spi.pojo.ServiceLoaderBGPExtensionProviderContext;
 import org.opendaylight.protocol.util.ByteList;
+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;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.LinkstateAddressFamily;
@@ -59,6 +61,8 @@ import com.google.common.collect.Maps;
 
 public class ComplementaryTest {
 
+       private final ReferenceCache ref = NoopReferenceCache.getInstance();
+
        @Test
        public void testBGPParameter() {
 
@@ -124,7 +128,7 @@ public class ComplementaryTest {
        public void testCommunitiesParser() {
                ExtendedCommunities as = null;
                try {
-                       as = CommunitiesParser.parseExtendedCommunity(new byte[] { 0, 5, 0, 54, 0, 0, 1, 76 });
+                       as = CommunitiesParser.parseExtendedCommunity(ref, new byte[] { 0, 5, 0, 54, 0, 0, 1, 76 });
                } catch (final BGPDocumentedException e1) {
                        fail("Not expected exception: " + e1);
                }
@@ -140,7 +144,7 @@ public class ComplementaryTest {
                assertEquals(0, as.getCommType().intValue());
 
                try {
-                       as = CommunitiesParser.parseExtendedCommunity(new byte[] { 40, 5, 0, 54, 0, 0, 1, 76 });
+                       as = CommunitiesParser.parseExtendedCommunity(ref, new byte[] { 40, 5, 0, 54, 0, 0, 1, 76 });
                } catch (final BGPDocumentedException e1) {
                        fail("Not expected exception: " + e1);
                }
@@ -153,7 +157,7 @@ public class ComplementaryTest {
 
                ExtendedCommunities rtc = null;
                try {
-                       rtc = CommunitiesParser.parseExtendedCommunity(new byte[] { 1, 2, 0, 35, 4, 2, 8, 7 });
+                       rtc = CommunitiesParser.parseExtendedCommunity(ref, new byte[] { 1, 2, 0, 35, 4, 2, 8, 7 });
                } catch (final BGPDocumentedException e1) {
                        fail("Not expected exception: " + e1);
                }
@@ -170,7 +174,7 @@ public class ComplementaryTest {
 
                ExtendedCommunities roc = null;
                try {
-                       roc = CommunitiesParser.parseExtendedCommunity(new byte[] { 0, 3, 0, 24, 4, 2, 8, 7 });
+                       roc = CommunitiesParser.parseExtendedCommunity(ref, new byte[] { 0, 3, 0, 24, 4, 2, 8, 7 });
                } catch (final BGPDocumentedException e1) {
                        fail("Not expected exception: " + e1);
                }
@@ -187,7 +191,7 @@ public class ComplementaryTest {
 
                ExtendedCommunities sec = null;
                try {
-                       sec = CommunitiesParser.parseExtendedCommunity(new byte[] { 41, 6, 12, 51, 2, 5, 21, 45 });
+                       sec = CommunitiesParser.parseExtendedCommunity(ref, new byte[] { 41, 6, 12, 51, 2, 5, 21, 45 });
                } catch (final BGPDocumentedException e1) {
                        fail("Not expected exception: " + e1);
                }
@@ -205,7 +209,7 @@ public class ComplementaryTest {
 
                ExtendedCommunities oec = null;
                try {
-                       oec = CommunitiesParser.parseExtendedCommunity(new byte[] { 3, 6, 21, 45, 5, 4, 3, 1 });
+                       oec = CommunitiesParser.parseExtendedCommunity(ref, new byte[] { 3, 6, 21, 45, 5, 4, 3, 1 });
                } catch (final BGPDocumentedException e1) {
                        fail("Not expected exception: " + e1);
                }
@@ -217,7 +221,7 @@ public class ComplementaryTest {
                assertEquals(3, oec.getCommType().intValue());
 
                try {
-                       oec = CommunitiesParser.parseExtendedCommunity(new byte[] { 43, 6, 21, 45, 5, 4, 3, 1 });
+                       oec = CommunitiesParser.parseExtendedCommunity(ref, new byte[] { 43, 6, 21, 45, 5, 4, 3, 1 });
                } catch (final BGPDocumentedException e1) {
                        fail("Not expected exception: " + e1);
                }
@@ -229,7 +233,7 @@ public class ComplementaryTest {
                assertEquals(43, oec.getCommType().intValue());
 
                try {
-                       CommunitiesParser.parseExtendedCommunity(new byte[] { 11, 11, 21, 45, 5, 4, 3, 1 });
+                       CommunitiesParser.parseExtendedCommunity(ref, 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());