Use ImmutableOffsetMapTemplate for route keys 65/82965/2
authorRobert Varga <robert.varga@pantheon.tech>
Mon, 8 Jul 2019 12:30:47 +0000 (14:30 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Mon, 8 Jul 2019 15:29:44 +0000 (17:29 +0200)
We are sharing the same key layout for all entries, hence it is
wasteful to instantiate a full ImmutableMap for each of them. Use
pre-computed ImmutableOffsetMapTemplate.ordered() to efficiently
instantiate identifiers.

Change-Id: Id3424f95e7e7ad3a6e7ed75715d8505645a45e69
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
(cherry picked from commit 8161baf409d425f87bb05b21b569a85c62ecd552)

15 files changed:
bgp/extensions/evpn/src/main/java/org/opendaylight/protocol/bgp/evpn/impl/EvpnRibSupport.java
bgp/extensions/flowspec/src/main/java/org/opendaylight/protocol/bgp/flowspec/AbstractFlowspecRIBSupport.java
bgp/extensions/inet/src/main/java/org/opendaylight/protocol/bgp/inet/AbstractIPRibSupport.java
bgp/extensions/l3vpn/src/main/java/org/opendaylight/protocol/bgp/l3vpn/mcast/L3VpnMcastIpv4RIBSupport.java
bgp/extensions/l3vpn/src/main/java/org/opendaylight/protocol/bgp/l3vpn/mcast/L3VpnMcastIpv6RIBSupport.java
bgp/extensions/l3vpn/src/main/java/org/opendaylight/protocol/bgp/l3vpn/unicast/AbstractVpnRIBSupport.java
bgp/extensions/labeled-unicast/src/main/java/org/opendaylight/protocol/bgp/labeled/unicast/AbstractLabeledUnicastRIBSupport.java
bgp/extensions/linkstate/src/main/java/org/opendaylight/protocol/bgp/linkstate/impl/LinkstateRIBSupport.java
bgp/extensions/mvpn/src/main/java/org/opendaylight/protocol/bgp/mvpn/impl/MvpnIpv4RIBSupport.java
bgp/extensions/mvpn/src/main/java/org/opendaylight/protocol/bgp/mvpn/impl/MvpnIpv6RIBSupport.java
bgp/extensions/route-target/src/main/java/org/opendaylight/protocol/bgp/route/targetcontrain/impl/RouteTargetConstrainRIBSupport.java
bgp/parser-spi/pom.xml
bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/PathIdUtil.java
bgp/rib-spi/src/main/java/org/opendaylight/protocol/bgp/rib/spi/AbstractRIBSupport.java
bgp/rib-spi/src/test/java/org/opendaylight/protocol/bgp/rib/spi/RIBSupportTest.java

index 308ad0fd0529dd06fac41cd422cbb8ecf939d290..845b77212ce8ccfc05e5646a5e7ed4deef2e32d3 100644 (file)
@@ -133,8 +133,8 @@ final class EvpnRibSupport extends AbstractRIBSupport<EvpnRoutesCase, EvpnRoutes
         EvpnNlriParser.serializeNlri(Collections.singletonList(dest), buffer);
         final Optional<DataContainerChild<? extends PathArgument, ?>> maybePathIdLeaf =
                 evpn.getChild(routePathIdNid());
-        return PathIdUtil.createNidKey(routeQName(), routeKeyQName(),
-                pathIdQName(), ByteArray.encodeBase64(buffer), maybePathIdLeaf);
+        return PathIdUtil.createNidKey(routeQName(), routeKeyTemplate(),
+                ByteArray.encodeBase64(buffer), maybePathIdLeaf);
     }
 
     @Override
index de964f9fb588837030973cdc92e750f26911b7a4..92d2e973a7233f3edd0530c7f2682203d611fc96 100644 (file)
@@ -96,8 +96,8 @@ public abstract class AbstractFlowspecRIBSupport<
         final Optional<DataContainerChild<? extends PathArgument, ?>> maybePathIdLeaf
                 = destination.getChild(routePathIdNid());
         final String routeKeyValue = this.nlriParser.stringNlri(destination);
-        final NodeIdentifierWithPredicates routeKey = PathIdUtil.createNidKey(routeQName(), routeKeyQName(),
-                pathIdQName(), routeKeyValue, maybePathIdLeaf);
+        final NodeIdentifierWithPredicates routeKey = PathIdUtil.createNidKey(routeQName(), routeKeyTemplate(),
+                routeKeyValue, maybePathIdLeaf);
         function.apply(tx, base, routeKey, destination, attributes);
 
         return Collections.singletonList(routeKey);
index fdd2cef0cc1585f751805159cc6df2281bf11ef4..6c0d075ad96ada017ea56f20920f3c24d51e85de 100644 (file)
@@ -122,6 +122,6 @@ abstract class AbstractIPRibSupport<
                 prefixes.getChild(routePathIdNid());
         Preconditions.checkState(maybePrefixLeaf.isPresent());
         final Object prefixValue = maybePrefixLeaf.get().getValue();
-        return PathIdUtil.createNidKey(routeQName(), routeKeyQName(), pathIdQName(), prefixValue, maybePathIdLeaf);
+        return PathIdUtil.createNidKey(routeQName(), routeKeyTemplate(), prefixValue, maybePathIdLeaf);
     }
 }
index 926ad44d6a0776db851564ef4e3a1403bc6c8371..62cc0ab8932429b970a4de4a2e2d8f1e9a340277 100644 (file)
@@ -110,8 +110,8 @@ public final class L3VpnMcastIpv4RIBSupport
         L3vpnMcastNlriSerializer.serializeNlri(Collections.singletonList(dest), buffer);
         final Optional<DataContainerChild<? extends PathArgument, ?>> maybePathIdLeaf =
                 l3vpn.getChild(routePathIdNid());
-        return PathIdUtil.createNidKey(routeQName(), routeKeyQName(),
-                pathIdQName(), ByteArray.encodeBase64(buffer), maybePathIdLeaf);
+        return PathIdUtil.createNidKey(routeQName(), routeKeyTemplate(),
+                ByteArray.encodeBase64(buffer), maybePathIdLeaf);
     }
 
     @Override
index 5ad8cc26b8a9fcc46975f3760570ce11a6183de2..fe46b6b8a17b7192fb881cd0b3adb2f6aafb6e5e 100644 (file)
@@ -110,8 +110,8 @@ public final class L3VpnMcastIpv6RIBSupport
         L3vpnMcastNlriSerializer.serializeNlri(Collections.singletonList(dest), buffer);
         final Optional<DataContainerChild<? extends PathArgument, ?>> maybePathIdLeaf =
                 l3vpn.getChild(routePathIdNid());
-        return PathIdUtil.createNidKey(routeQName(), routeKeyQName(),
-                pathIdQName(), ByteArray.encodeBase64(buffer), maybePathIdLeaf);
+        return PathIdUtil.createNidKey(routeQName(), routeKeyTemplate(),
+                ByteArray.encodeBase64(buffer), maybePathIdLeaf);
     }
 
     @Override
index 44b1daf96ad189bde567152304cac5821f7026b8..63fe2cc547c72bdc57cc72c873583d27830f5a61 100644 (file)
@@ -167,8 +167,8 @@ public abstract class AbstractVpnRIBSupport<C extends Routes & DataObject, S ext
 
         final Optional<DataContainerChild<? extends PathArgument, ?>> maybePathIdLeaf =
                 l3vpn.getChild(routePathIdNid());
-        return PathIdUtil.createNidKey(routeQName(), routeKeyQName(),
-                pathIdQName(), ByteArray.encodeBase64(buffer), maybePathIdLeaf);
+        return PathIdUtil.createNidKey(routeQName(), routeKeyTemplate(),
+                ByteArray.encodeBase64(buffer), maybePathIdLeaf);
     }
 
     @Override
index 847bf49290554e12fd4051046f446074eebe3481..3ac665aeceb48386bbe6eca3aec434f837caee1a 100644 (file)
@@ -130,7 +130,7 @@ abstract class AbstractLabeledUnicastRIBSupport<
         final String routeKeyValue = ByteArray.encodeBase64(buffer);
         final Optional<DataContainerChild<? extends PathArgument, ?>> maybePathIdLeaf
                 = labeledUnicast.getChild(routePathIdNid());
-        return PathIdUtil.createNidKey(routeQName(), routeKeyQName(), pathIdQName(), routeKeyValue, maybePathIdLeaf);
+        return PathIdUtil.createNidKey(routeQName(), routeKeyTemplate(), routeKeyValue, maybePathIdLeaf);
     }
 
     /**
index 71bf22c3058b532f7790fc2843ea9ca4aeeb4600..7d4b2aa9b64048e2a3c41102b7ec98de7da979aa 100644 (file)
@@ -88,8 +88,8 @@ public final class LinkstateRIBSupport
         SimpleNlriTypeRegistry.getInstance().serializeNlriType(cLinkstateDestination, buffer);
         final Optional<DataContainerChild<? extends PathArgument, ?>> maybePathIdLeaf =
                 linkstate.getChild(routePathIdNid());
-        return PathIdUtil.createNidKey(routeQName(), routeKeyQName(),
-                pathIdQName(), ByteArray.encodeBase64(buffer), maybePathIdLeaf);
+        return PathIdUtil.createNidKey(routeQName(), routeKeyTemplate(),
+                ByteArray.encodeBase64(buffer), maybePathIdLeaf);
     }
 
     private static List<CLinkstateDestination> extractRoutes(final Collection<MapEntryNode> routes) {
index 7e681ef76d856e1211a96927a89821b52515da32..d46dd6513b59ef08f2d0628090131b376c0c1172 100644 (file)
@@ -114,8 +114,8 @@ final class MvpnIpv4RIBSupport extends AbstractMvpnRIBSupport<MvpnRoutesIpv4Case
         Ipv4NlriHandler.serializeNlri(Collections.singletonList(dest), buffer);
         final Optional<DataContainerChild<? extends PathArgument, ?>> maybePathIdLeaf =
                 mvpn.getChild(routePathIdNid());
-        return PathIdUtil.createNidKey(routeQName(), routeKeyQName(),
-                pathIdQName(), ByteArray.encodeBase64(buffer), maybePathIdLeaf);
+        return PathIdUtil.createNidKey(routeQName(), routeKeyTemplate(),
+                ByteArray.encodeBase64(buffer), maybePathIdLeaf);
     }
 
     @Override
index 32bee63b3a4447575589a7fad44735cf850dd564..3e4b898d000b4f99d87e4004cb15c5ec2cd6fc19 100644 (file)
@@ -115,8 +115,8 @@ final class MvpnIpv6RIBSupport extends AbstractMvpnRIBSupport<MvpnRoutesIpv6Case
         Ipv6NlriHandler.serializeNlri(Collections.singletonList(dest), buffer);
         final Optional<DataContainerChild<? extends PathArgument, ?>> maybePathIdLeaf =
                 mvpn.getChild(routePathIdNid());
-        return PathIdUtil.createNidKey(routeQName(), routeKeyQName(),
-                pathIdQName(), ByteArray.encodeBase64(buffer), maybePathIdLeaf);
+        return PathIdUtil.createNidKey(routeQName(), routeKeyTemplate(),
+                ByteArray.encodeBase64(buffer), maybePathIdLeaf);
     }
 
     @Override
index f0b1deab7cafc57c4aa11a0ab0a6983543a5a781..c4cbd1ea1a6d67c0f4a8343742c22dbb63ac3dfa 100644 (file)
@@ -189,8 +189,8 @@ public final class RouteTargetConstrainRIBSupport
                 .serializeRouteTargetConstrain(dest.getRouteTargetConstrainChoice()));
         final Optional<DataContainerChild<? extends PathArgument, ?>> maybePathIdLeaf =
                 routeTarget.getChild(routePathIdNid());
-        return PathIdUtil.createNidKey(routeQName(), routeKeyQName(),
-                pathIdQName(), ByteArray.encodeBase64(buffer), maybePathIdLeaf);
+        return PathIdUtil.createNidKey(routeQName(), routeKeyTemplate(),
+                ByteArray.encodeBase64(buffer), maybePathIdLeaf);
     }
 
     @Override
index 4edbb3c4d2cc7e64dd1feaf87bc9ee4a420b2db1..fe18f70f9d2e885840a2dfb9e505b3387df5c227 100644 (file)
             <groupId>org.opendaylight.yangtools</groupId>
             <artifactId>concepts</artifactId>
         </dependency>
+        <dependency>
+            <groupId>org.opendaylight.yangtools</groupId>
+            <artifactId>util</artifactId>
+        </dependency>
         <dependency>
             <groupId>org.opendaylight.yangtools</groupId>
             <artifactId>yang-common</artifactId>
index 67848ca66c181734cdf20e7deb99bf60c89d8746..670cc936b08b51b83d1b59e44a0a4ff08b86eb43 100644 (file)
@@ -9,11 +9,11 @@
 package org.opendaylight.protocol.bgp.parser.spi;
 
 import com.google.common.base.Preconditions;
-import com.google.common.collect.ImmutableMap;
 import io.netty.buffer.ByteBuf;
 import java.util.Optional;
 import org.opendaylight.protocol.util.ByteBufWriteUtil;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.PathId;
+import org.opendaylight.yangtools.util.ImmutableOffsetMapTemplate;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
@@ -66,13 +66,6 @@ public final class PathIdUtil {
         return (Long) NormalizedNodes.findNode(data, pathNii).map(NormalizedNode::getValue).orElse(null);
     }
 
-    public static NodeIdentifierWithPredicates createNodeIdentifierWithPredicates(final QName routeQname,
-            final QName pathidQname, final Object pathId,
-            final QName routeKeyQname, final Object keyObject) {
-        final ImmutableMap<QName, Object> keyValues = ImmutableMap.of(pathidQname, pathId, routeKeyQname, keyObject);
-        return new NodeIdentifierWithPredicates(routeQname, keyValues);
-    }
-
     /**
      * Build Path Id.
      *
@@ -90,20 +83,19 @@ public final class PathIdUtil {
      * Build Route Key for supporting mp.
      * Key is composed by 2 elements (route-key + path Id).
      *
-     * @param routeQname      route Qname
-     * @param routeKeyQname   route key Qname
-     * @param pathIdQname     path Id Qname
-     * @param routeKeyValue   route key value
-     * @param maybePathIdLeaf path id container, it might me supported or not, in that case default 0 value will
-     *                        be
-     *                        assigned
+     * @param routeQName       route QName
+     * @param routeKeyTemplate route key template
+     * @param routeKeyValue    route key value
+     * @param maybePathIdLeaf  path id container, it might me supported or not, in that case default 0 value will
+     *                         be assigned
      * @return Route Key Nid
      */
-    public static NodeIdentifierWithPredicates createNidKey(final QName routeQname, final QName routeKeyQname,
-            final QName pathIdQname, final Object routeKeyValue,
+    public static NodeIdentifierWithPredicates createNidKey(final QName routeQName,
+            final ImmutableOffsetMapTemplate<QName> routeKeyTemplate, final Object routeKeyValue,
             final Optional<DataContainerChild<? extends PathArgument, ?>> maybePathIdLeaf) {
         // FIXME: a cache here would mean we instantiate the same identifier for each route making comparison quicker.
         final Object pathId = maybePathIdLeaf.isPresent() ? maybePathIdLeaf.get().getValue() : NON_PATH_ID_VALUE;
-        return createNodeIdentifierWithPredicates(routeQname, pathIdQname, pathId, routeKeyQname, routeKeyValue);
+        return new NodeIdentifierWithPredicates(routeQName,
+            routeKeyTemplate.instantiateWithValues(pathId, routeKeyValue));
     }
 }
index 09ad5be727164cca759fdc8d15cecea16dd69204..739169c1d055c36df406ed42600dd771cf2a94f3 100644 (file)
@@ -59,6 +59,7 @@ 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.rev180329.RouteDistinguisherBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.SubsequentAddressFamily;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.next.hop.CNextHop;
+import org.opendaylight.yangtools.util.ImmutableOffsetMapTemplate;
 import org.opendaylight.yangtools.yang.binding.ChildOf;
 import org.opendaylight.yangtools.yang.binding.ChoiceIn;
 import org.opendaylight.yangtools.yang.binding.DataObject;
@@ -120,15 +121,14 @@ public abstract class AbstractRIBSupport<
     private final Class<? extends AddressFamily> afiClass;
     private final Class<? extends SubsequentAddressFamily> safiClass;
     private final NodeIdentifier destinationNid;
-    private final QName pathIdQname;
     private final NodeIdentifier pathIdNid;
-    private final QName routeKeyQname;
     private final NodeIdentifier prefixTypeNid;
     private final NodeIdentifier rdNid;
     protected final BindingNormalizedNodeSerializer mappingService;
     protected final YangInstanceIdentifier routeDefaultYii;
     private final TablesKey tk;
     private final ImmutableList<PathArgument> relativeRoutesPath;
+    private final ImmutableOffsetMapTemplate<QName> routeKeyTemplate;
 
     /**
      * Default constructor. Requires the QName of the container augmented under the routes choice
@@ -170,9 +170,7 @@ public abstract class AbstractRIBSupport<
         this.afiClass = afiClass;
         this.safiClass = safiClass;
         this.destinationNid = NodeIdentifier.create(destContainerQname);
-        this.pathIdQname = QName.create(routeQName(), "path-id").intern();
-        this.pathIdNid = NodeIdentifier.create(this.pathIdQname);
-        this.routeKeyQname = QName.create(routeQName(), ROUTE_KEY).intern();
+        this.pathIdNid = NodeIdentifier.create(QName.create(routeQName(), "path-id").intern());
         this.prefixTypeNid = NodeIdentifier.create(QName.create(destContainerQname, "prefix").intern());
         this.rdNid = NodeIdentifier.create(QName.create(destContainerQname, "route-distinguisher").intern());
         this.routeDefaultYii =
@@ -188,6 +186,8 @@ public abstract class AbstractRIBSupport<
                         .node(this.routesListIdentifier)
                         .node(this.routesListIdentifier).build();
         this.relativeRoutesPath = ImmutableList.of(routesContainerIdentifier, routesListIdentifier);
+        this.routeKeyTemplate = ImmutableOffsetMapTemplate.ordered(
+            ImmutableList.of(this.pathIdNid.getNodeType(), QName.create(routeQName(), ROUTE_KEY).intern()));
     }
 
     @Override
@@ -511,12 +511,8 @@ public abstract class AbstractRIBSupport<
         return this.pathIdNid;
     }
 
-    protected final QName pathIdQName() {
-        return this.pathIdQname;
-    }
-
-    protected final QName routeKeyQName() {
-        return this.routeKeyQname;
+    protected final ImmutableOffsetMapTemplate<QName> routeKeyTemplate() {
+        return this.routeKeyTemplate;
     }
 
     protected final String extractPrefix(final DataContainerNode<? extends PathArgument> route) {
index ef803b9cbe5095d4fd7b432701575eaafb021cb4..fe7b499aed4f43fe471289c2ea5172db92da682b 100644 (file)
@@ -159,29 +159,24 @@ public class RIBSupportTest extends AbstractConcurrentDataBrokerTest {
         return customizer;
     }
 
-    @Test
-    public void pathIdQName() {
-        assertEquals(PATH_ID_QNAME,this.ribSupportTestImp.pathIdQName());
-    }
-
     @Test
     public void routesCaseClass() {
-        assertEquals(Ipv4RoutesCase.class,this.ribSupportTestImp.routesCaseClass());
+        assertEquals(Ipv4RoutesCase.class, this.ribSupportTestImp.routesCaseClass());
     }
 
     @Test
     public void routesContainerClass() {
-        assertEquals(Ipv4Routes.class,this.ribSupportTestImp.routesContainerClass());
+        assertEquals(Ipv4Routes.class, this.ribSupportTestImp.routesContainerClass());
     }
 
     @Test
     public void routesListClass() {
-        assertEquals(Ipv4Route.class,this.ribSupportTestImp.routesListClass());
+        assertEquals(Ipv4Route.class, this.ribSupportTestImp.routesListClass());
     }
 
     @Test
     public void routeQName() {
-        assertEquals(Ipv4Route.QNAME,this.ribSupportTestImp.routeQName());
+        assertEquals(Ipv4Route.QNAME, this.ribSupportTestImp.routeQName());
     }
 
     @Test