BUG-6647 Increase code coverage and clean up I
[bgpcep.git] / bgp / parser-spi / src / main / java / org / opendaylight / protocol / bgp / parser / spi / PathIdUtil.java
index 76e4d61a6a3f067d6baab06283d730abd7a681e1..f4aedc34f4a653cacd3028723d043342010121c5 100644 (file)
@@ -8,21 +8,23 @@
 
 package org.opendaylight.protocol.bgp.parser.spi;
 
+import com.google.common.base.Optional;
 import com.google.common.base.Preconditions;
 import com.google.common.collect.ImmutableMap;
 import io.netty.buffer.ByteBuf;
-import javax.annotation.Nullable;
 import org.opendaylight.protocol.util.ByteBufWriteUtil;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.PathId;
 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;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
+import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
 
 public final class PathIdUtil {
+    public static final long NON_PATH_ID = 0;
 
     private PathIdUtil() {
         throw new UnsupportedOperationException();
@@ -57,9 +59,8 @@ public final class PathIdUtil {
      *
      * @param data Data containing the path Id
      * @param pathNii Path Id NodeIdentifier specific per each Rib support
-     * @return The path identifier from data change, in case its not provided or supported return null
+     * @return The path identifier from data change
      */
-    @Nullable
     public static Long extractPathId(final NormalizedNode<?, ?> data, final NodeIdentifier pathNii) {
         final NormalizedNode<?, ?> pathId = NormalizedNodes.findNode(data, pathNii).orNull();
         if (pathId == null) {
@@ -75,14 +76,28 @@ public final class PathIdUtil {
      * @param routeId Route Id value
      * @param routeQname route QName provided per each RibSupport
      * @param pathidQname Path Id QName provided per each RibSupport
-     * @param prefixQname Prefix QName provided per each RibSupport
-     * @return
+     * @param routeKeyQname Prefix QName provided per each RibSupport
+     * @return Route Key Nid
      */
-    public static PathArgument createNiiKey(final long pathId, final PathArgument routeId, final QName routeQname, final QName pathidQname,
-        final QName prefixQname) {
-        final String prefix = (String) (((NodeIdentifierWithPredicates) routeId).getKeyValues()).get(prefixQname);
-        final ImmutableMap<QName, Object> keyValues = ImmutableMap.of(pathidQname, pathId, prefixQname, prefix);
+    public static PathArgument createNidKey(final long pathId, final PathArgument routeId, final QName routeQname, final QName pathidQname,
+        final QName routeKeyQname) {
+        return createNodeIdentifierWithPredicates(routeQname, pathidQname, pathId, routeKeyQname, getObjectKey(routeId, routeKeyQname));
+    }
+
+    /**
+     * Get route key object ( prefgit stat  ix / key-value/ .. )
+     *
+     * @param routeId PathArgument containing the key
+     * @param routeKeyQname routeKey Qname
+     * @return key
+     */
+    public static Object getObjectKey(final PathArgument routeId, final QName routeKeyQname) {
+        return (((NodeIdentifierWithPredicates) routeId).getKeyValues()).get(routeKeyQname);
+    }
 
+    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);
     }
 
@@ -97,4 +112,22 @@ public final class PathIdUtil {
         final Long pathIdVal = PathIdUtil.extractPathId(routesCont, pathIdNii);
         return pathIdVal == null ? null : new PathId(pathIdVal);
     }
+
+    /**
+     * 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
+     * @return Route Key Nid
+     */
+    public static NodeIdentifierWithPredicates createNidKey(final QName routeQname, final QName routeKeyQname, final QName pathIdQname,
+        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;
+        return createNodeIdentifierWithPredicates(routeQname, pathIdQname, pathId, routeKeyQname, routeKeyValue);
+    }
 }