Do not use RibSupportUtils in AdjRibInWriter 49/106649/2
authorRobert Varga <robert.varga@pantheon.tech>
Fri, 23 Jun 2023 10:49:16 +0000 (12:49 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Fri, 23 Jun 2023 11:34:38 +0000 (13:34 +0200)
RIBSupport has everything we need, there is no point in using
RibSupportUtils for this task.

JIRA: BGPCEP-1016
Change-Id: If692d5b1d84d7596f8af3c13930f420bed901fd3
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/AdjRibInWriter.java
bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/RibSupportUtils.java
bgp/rib-impl/src/test/java/org/opendaylight/protocol/bgp/rib/impl/AdjRibsInWriterTest.java
bgp/rib-impl/src/test/java/org/opendaylight/protocol/bgp/rib/impl/RibSupportUtilsTest.java

index 9dd74d02a690e2e9026c77b12dc1b612f548318a..e6ae77c681e93970be79e1365b7b2c1c4b0f72b7 100644 (file)
@@ -26,7 +26,6 @@ import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
-import java.util.Map.Entry;
 import java.util.Optional;
 import java.util.Set;
 import java.util.concurrent.CountDownLatch;
@@ -181,25 +180,22 @@ final class AdjRibInWriter {
         final Builder<TablesKey, TableContext> tb = ImmutableMap.builder();
         for (final TablesKey tableKey : tableTypes) {
             final RIBSupportContext rs = registry.getRIBSupportContext(tableKey);
-            // TODO: Use returned value once Instance Identifier builder allows for it.
-            final NodeIdentifierWithPredicates instanceIdentifierKey = RibSupportUtils.toYangTablesKey(tableKey);
             if (rs == null) {
                 LOG.warn("No support for table type {}, skipping it", tableKey);
                 continue;
             }
-            installAdjRibsOutTables(newPeerPath, rs, instanceIdentifierKey, tableKey,
-                    addPathTablesType.get(tableKey), tx);
-            installAdjRibInTables(newPeerPath, tableKey, rs, instanceIdentifierKey, tx, tb);
+            installAdjRibsOutTables(newPeerPath, rs, addPathTablesType.get(tableKey), tx);
+            installAdjRibInTables(newPeerPath, tableKey, rs, tx, tb);
         }
         return tb.build();
     }
 
     private static void installAdjRibInTables(final YangInstanceIdentifier newPeerPath, final TablesKey tableKey,
-            final RIBSupportContext rs, final NodeIdentifierWithPredicates instanceIdentifierKey,
-            final DOMDataTreeWriteTransaction tx, final Builder<TablesKey, TableContext> tb) {
+            final RIBSupportContext rs, final DOMDataTreeWriteTransaction tx,
+            final Builder<TablesKey, TableContext> tb) {
         // We will use table keys very often, make sure they are optimized
         final InstanceIdentifierBuilder idb = YangInstanceIdentifier.builder(newPeerPath
-                .node(EMPTY_ADJRIBIN.getIdentifier()).node(TABLES_NID)).node(instanceIdentifierKey);
+                .node(EMPTY_ADJRIBIN.getIdentifier()).node(TABLES_NID)).node(rs.getRibSupport().tablesKey());
 
         final TableContext ctx = new TableContext(rs, idb.build());
         ctx.createEmptyTableStructure(tx);
@@ -211,19 +207,19 @@ final class AdjRibInWriter {
     }
 
     private static void installAdjRibsOutTables(final YangInstanceIdentifier newPeerPath, final RIBSupportContext rs,
-            final NodeIdentifierWithPredicates instanceIdentifierKey, final TablesKey tableKey,
             final SendReceive sendReceive, final DOMDataTreeWriteTransaction tx) {
-        final NodeIdentifierWithPredicates supTablesKey = RibSupportUtils.toYangKey(SupportedTables.QNAME, tableKey);
+        final var tableKey = rs.getRibSupport().tablesKey();
+        final var supTablesKey = NodeIdentifierWithPredicates.of(SupportedTables.QNAME, tableKey.asMap());
         final var tt = Builders.mapEntryBuilder().withNodeIdentifier(supTablesKey);
-        for (final Entry<QName, Object> e : supTablesKey.entrySet()) {
+        for (var e : supTablesKey.entrySet()) {
             tt.withChild(ImmutableNodes.leafNode(e.getKey(), e.getValue()));
         }
         if (sendReceive != null) {
             tt.withChild(ImmutableNodes.leafNode(SEND_RECEIVE, sendReceive.getName()));
         }
         tx.put(LogicalDatastoreType.OPERATIONAL, newPeerPath.node(PEER_TABLES).node(supTablesKey), tt.build());
-        rs.createEmptyTableStructure(tx, newPeerPath.node(EMPTY_ADJRIBOUT.getIdentifier())
-                .node(TABLES_NID).node(instanceIdentifierKey));
+        rs.createEmptyTableStructure(tx,
+            newPeerPath.node(EMPTY_ADJRIBOUT.getIdentifier()).node(TABLES_NID).node(tableKey));
     }
 
     private void createEmptyPeerStructure(final PeerId newPeerId,
index 06d0952648304826bbe027b7adf75b394de962c6..db7038df8240316bbb4580387050d2ef6e8449f1 100644 (file)
@@ -11,8 +11,6 @@ import com.google.common.collect.ImmutableList;
 import org.opendaylight.mdsal.binding.spec.reflect.BindingReflections;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.Tables;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.TablesKey;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.AddressFamily;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.SubsequentAddressFamily;
 import org.opendaylight.yangtools.util.ImmutableOffsetMapTemplate;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
@@ -33,31 +31,7 @@ final class RibSupportUtils {
      * @return NodeIdentifierWithPredicates of {@link Tables} for specified AFI, SAFI combination.
      */
     static NodeIdentifierWithPredicates toYangTablesKey(final TablesKey tablesKey) {
-        return toYangKey(Tables.QNAME, tablesKey.getAfi(), tablesKey.getSafi());
-    }
-
-    /**
-     * Creates Yang Instance Identifier path argument from supplied AFI and SAFI.
-     *
-     * @param id   QNAME representing node
-     * @param afi  Class representing AFI
-     * @param safi Class representing SAFI
-     * @return NodeIdentifierWithPredicates of 'id' for specified AFI, SAFI combination.
-     */
-    static NodeIdentifierWithPredicates toYangKey(final QName id, final AddressFamily afi,
-            final SubsequentAddressFamily safi) {
-        return NodeIdentifierWithPredicates.of(id, AFI_SAFI_TEMPLATE.instantiateWithValues(
-            BindingReflections.getQName(afi), BindingReflections.getQName(safi)));
-    }
-
-    /**
-     * Creates Yang Instance Identifier path argument from supplied {@link TablesKey}.
-     *
-     * @param id QNAME representing node
-     * @param tablesKey  Tables key representing table.
-     * @return NodeIdentifierWithPredicates of 'id' for specified AFI, SAFI combination.
-     */
-    static NodeIdentifierWithPredicates toYangKey(final QName id, final TablesKey tablesKey) {
-        return toYangKey(id, tablesKey.getAfi(), tablesKey.getSafi());
+        return NodeIdentifierWithPredicates.of(Tables.QNAME, AFI_SAFI_TEMPLATE.instantiateWithValues(
+            BindingReflections.getQName(tablesKey.getAfi()), BindingReflections.getQName(tablesKey.getSafi())));
     }
 }
index 974ef7fd1b99a163743a0003e756473276cb159e..f2cadeceb8778a0721912b259e6bc08369bc239a 100644 (file)
@@ -23,8 +23,9 @@ import java.util.Map;
 import java.util.Set;
 import org.junit.Before;
 import org.junit.Test;
+import org.junit.runner.RunWith;
 import org.mockito.Mock;
-import org.mockito.MockitoAnnotations;
+import org.mockito.junit.MockitoJUnitRunner;
 import org.opendaylight.mdsal.common.api.CommitInfo;
 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction;
@@ -35,24 +36,33 @@ import org.opendaylight.protocol.bgp.rib.impl.spi.RIBSupportContextRegistry;
 import org.opendaylight.protocol.bgp.rib.spi.IdentifierUtils;
 import org.opendaylight.protocol.bgp.rib.spi.RIBNormalizedNodes;
 import org.opendaylight.protocol.bgp.rib.spi.RIBQNames;
+import org.opendaylight.protocol.bgp.rib.spi.RIBSupport;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.SendReceive;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.PeerId;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.PeerRole;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.Rib;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.bgp.rib.rib.Peer;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.bgp.rib.rib.peer.SupportedTables;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.Tables;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.TablesKey;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.Ipv4AddressFamily;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.UnicastSubsequentAddressFamily;
+import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
 
+@RunWith(MockitoJUnitRunner.StrictStubs.class)
 public class AdjRibsInWriterTest {
-
     private static final TablesKey K4 = new TablesKey(Ipv4AddressFamily.VALUE, UnicastSubsequentAddressFamily.VALUE);
+    private static final NodeIdentifierWithPredicates DOM_K4 = NodeIdentifierWithPredicates.of(Tables.QNAME,
+        Map.of(QName.create(Tables.QNAME, "afi"), Ipv4AddressFamily.QNAME,
+            QName.create(Tables.QNAME, "safi"), UnicastSubsequentAddressFamily.QNAME));
     private static final Map<TablesKey, SendReceive> ADD_PATH_TABLE_MAPS = Map.of(K4, SendReceive.Both);
+
     private final Set<TablesKey> tableTypes = Set.of(K4);
     private final String peerIp = "12.34.56.78";
+
     @Mock
     private DOMTransactionChain chain;
     @Mock
@@ -62,13 +72,14 @@ public class AdjRibsInWriterTest {
     @Mock
     private RIBSupportContext context;
     @Mock
+    private RIBSupport<?, ?> support;
+    @Mock
     private PeerTransactionChain ptc;
+
     private AdjRibInWriter writer;
 
     @Before
     public void setUp() {
-        MockitoAnnotations.initMocks(this);
-        doReturn("MockedTrans").when(tx).toString();
         doReturn(tx).when(chain).newWriteOnlyTransaction();
         doReturn(CommitInfo.emptyFluentFuture()).when(tx).commit();
         doNothing().when(tx).put(eq(LogicalDatastoreType.OPERATIONAL),
@@ -78,6 +89,8 @@ public class AdjRibsInWriterTest {
         doReturn(context).when(registry).getRIBSupportContext(any(TablesKey.class));
         doReturn(chain).when(ptc).getDomChain();
         doNothing().when(context).createEmptyTableStructure(eq(tx), any(YangInstanceIdentifier.class));
+        doReturn(support).when(context).getRibSupport();
+        doReturn(DOM_K4).when(support).tablesKey();
     }
 
     @Test
@@ -91,13 +104,13 @@ public class AdjRibsInWriterTest {
         verifyPeerSkeletonInsertedCorrectly(peerPath);
         // verify supported tables were inserted for ipv4
         verify(tx).put(eq(LogicalDatastoreType.OPERATIONAL), eq(peerPath.node(SupportedTables.QNAME)
-                .node(RibSupportUtils.toYangKey(SupportedTables.QNAME, K4))), any(NormalizedNode.class));
+            .node(NodeIdentifierWithPredicates.of(SupportedTables.QNAME, DOM_K4.asMap()))), any(NormalizedNode.class));
         verifyUptodateSetToFalse(peerPath);
     }
 
     private void verifyUptodateSetToFalse(final YangInstanceIdentifier peerPath) {
         final YangInstanceIdentifier path = peerPath.node(ADJRIBIN_NID)
-                .node(TABLES_NID).node(RibSupportUtils.toYangTablesKey(K4))
+                .node(TABLES_NID).node(DOM_K4)
                 .node(ATTRIBUTES_NID).node(UPTODATE_NID);
         verify(tx).merge(eq(LogicalDatastoreType.OPERATIONAL), eq(path),
                 eq(RIBNormalizedNodes.ATTRIBUTES_UPTODATE_FALSE));
index cb10ef1ae84f211237362909f6fd19a9902ef845..3c208aa776c1f2d0ea444aa1dcfd57241aac3f96 100644 (file)
@@ -11,10 +11,8 @@ import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
 
-import com.google.common.collect.ImmutableMap;
 import java.util.Map;
 import org.junit.Test;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.bgp.rib.rib.peer.SupportedTables;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.Tables;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.TablesKey;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.Ipv4AddressFamily;
@@ -23,46 +21,14 @@ import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
 
 public class RibSupportUtilsTest {
-    private static final NodeIdentifierWithPredicates NII;
-    private static final NodeIdentifierWithPredicates NII_PATH;
-    private static final TablesKey TABLE_KEY = new TablesKey(Ipv4AddressFamily.VALUE,
-            UnicastSubsequentAddressFamily.VALUE);
-
-    static {
-        final QName afi = QName.create("urn:opendaylight:params:xml:ns:yang:bgp-rib?revision=2018-03-29", "afi");
-        final QName safi = QName.create("urn:opendaylight:params:xml:ns:yang:bgp-rib?revision=2018-03-29", "safi");
-        NII = NodeIdentifierWithPredicates.of(SupportedTables.QNAME, ImmutableMap.of(afi, Ipv4AddressFamily.QNAME,
-                safi, UnicastSubsequentAddressFamily.QNAME));
-        NII_PATH = NodeIdentifierWithPredicates.of(SupportedTables.QNAME, ImmutableMap.of(
-                QName.create("urn:opendaylight:params:xml:ns:yang:bgp-multiprotocol?revision=2018-03-29", "afi"),
-                Ipv4AddressFamily.QNAME,
-                QName.create("urn:opendaylight:params:xml:ns:yang:bgp-multiprotocol?revision=2018-03-29", "safi"),
-                UnicastSubsequentAddressFamily.QNAME));
-    }
-
     @Test
     public void testYangTablesKey() {
-        final NodeIdentifierWithPredicates p = RibSupportUtils.toYangTablesKey(TABLE_KEY);
+        final NodeIdentifierWithPredicates p = RibSupportUtils.toYangTablesKey(new TablesKey(Ipv4AddressFamily.VALUE,
+            UnicastSubsequentAddressFamily.VALUE));
         final Map<QName, Object> m = p.asMap();
         assertFalse(m.isEmpty());
         assertEquals(Tables.QNAME, p.getNodeType());
         assertTrue(m.containsValue(Ipv4AddressFamily.QNAME));
         assertTrue(m.containsValue(UnicastSubsequentAddressFamily.QNAME));
     }
-
-    @Test
-    public void testYangKey() {
-        final NodeIdentifierWithPredicates p = RibSupportUtils.toYangKey(SupportedTables.QNAME, TABLE_KEY);
-        final Map<QName, Object> m = p.asMap();
-        assertFalse(m.isEmpty());
-        assertEquals(SupportedTables.QNAME, p.getNodeType());
-        assertTrue(m.containsValue(Ipv4AddressFamily.QNAME));
-        assertTrue(m.containsValue(UnicastSubsequentAddressFamily.QNAME));
-    }
-
-    @Test
-    public void toYangKey() {
-        final NodeIdentifierWithPredicates result = RibSupportUtils.toYangKey(SupportedTables.QNAME, TABLE_KEY);
-        assertEquals(NII.toString(), result.toString());
-    }
 }