Bump upstreams to 2022.09
[bgpcep.git] / bgp / rib-impl / src / main / java / org / opendaylight / protocol / bgp / rib / impl / LocRibWriter.java
index 7b8991e5ec7dcda23e5e2dfca3e00f2f31ab1ad6..170e1b955519a39596030f338e2cc3db4056cd5f 100644 (file)
@@ -7,7 +7,15 @@
  */
 package org.opendaylight.protocol.bgp.rib.impl;
 
+import static com.google.common.base.Verify.verify;
 import static java.util.Objects.requireNonNull;
+import static org.opendaylight.protocol.bgp.rib.spi.RIBNodeIdentifiers.ATTRIBUTES_NID;
+import static org.opendaylight.protocol.bgp.rib.spi.RIBNodeIdentifiers.EFFRIBIN_NID;
+import static org.opendaylight.protocol.bgp.rib.spi.RIBNodeIdentifiers.LOCRIB_NID;
+import static org.opendaylight.protocol.bgp.rib.spi.RIBNodeIdentifiers.PEER_NID;
+import static org.opendaylight.protocol.bgp.rib.spi.RIBNodeIdentifiers.ROUTES_NID;
+import static org.opendaylight.protocol.bgp.rib.spi.RIBNodeIdentifiers.TABLES_NID;
+import static org.opendaylight.protocol.bgp.rib.spi.RIBNodeIdentifiers.UPTODATE_NID;
 
 import com.google.common.util.concurrent.FutureCallback;
 import com.google.common.util.concurrent.MoreExecutors;
@@ -19,24 +27,24 @@ import java.util.Map;
 import java.util.Map.Entry;
 import java.util.Set;
 import java.util.concurrent.atomic.LongAdder;
-import javax.annotation.Nonnull;
-import javax.annotation.concurrent.GuardedBy;
-import javax.annotation.concurrent.NotThreadSafe;
-import org.opendaylight.controller.md.sal.binding.api.BindingTransactionChain;
-import org.opendaylight.controller.md.sal.binding.api.ClusteredDataTreeChangeListener;
-import org.opendaylight.controller.md.sal.binding.api.DataBroker;
-import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
-import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
-import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
-import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
-import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
+import org.checkerframework.checker.lock.qual.GuardedBy;
+import org.eclipse.jdt.annotation.NonNull;
 import org.opendaylight.mdsal.common.api.CommitInfo;
+import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
+import org.opendaylight.mdsal.dom.api.ClusteredDOMDataTreeChangeListener;
+import org.opendaylight.mdsal.dom.api.DOMDataTreeChangeService;
+import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
+import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteOperations;
+import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction;
+import org.opendaylight.mdsal.dom.api.DOMTransactionChain;
 import org.opendaylight.protocol.bgp.mode.api.PathSelectionMode;
 import org.opendaylight.protocol.bgp.mode.api.RouteEntry;
 import org.opendaylight.protocol.bgp.rib.impl.spi.RibOutRefresh;
 import org.opendaylight.protocol.bgp.rib.impl.state.rib.TotalPathsCounter;
 import org.opendaylight.protocol.bgp.rib.impl.state.rib.TotalPrefixesCounter;
 import org.opendaylight.protocol.bgp.rib.spi.BGPPeerTracker;
+import org.opendaylight.protocol.bgp.rib.spi.IdentifierUtils;
+import org.opendaylight.protocol.bgp.rib.spi.RIBNormalizedNodes;
 import org.opendaylight.protocol.bgp.rib.spi.RIBSupport;
 import org.opendaylight.protocol.bgp.rib.spi.RouterId;
 import org.opendaylight.protocol.bgp.rib.spi.entry.ActualBestPathRoutes;
@@ -45,99 +53,94 @@ import org.opendaylight.protocol.bgp.rib.spi.entry.StaleBestPathRoute;
 import org.opendaylight.protocol.bgp.rib.spi.policy.BGPRibRoutingPolicy;
 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.types.rev151009.AfiSafiType;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.PathId;
 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.Route;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.bgp.rib.Rib;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.bgp.rib.RibKey;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.bgp.rib.rib.LocRib;
-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.PeerKey;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.bgp.rib.rib.peer.EffectiveRibIn;
 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.rib.rev180329.rib.tables.Attributes;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.tables.AttributesBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.tables.Routes;
 import org.opendaylight.yangtools.concepts.ListenerRegistration;
 import org.opendaylight.yangtools.yang.binding.ChildOf;
 import org.opendaylight.yangtools.yang.binding.ChoiceIn;
 import org.opendaylight.yangtools.yang.binding.DataObject;
-import org.opendaylight.yangtools.yang.binding.Identifiable;
-import org.opendaylight.yangtools.yang.binding.Identifier;
-import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
-import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
+import org.opendaylight.yangtools.yang.common.Uint32;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
+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.MapEntryNode;
+import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
+import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
+import org.opendaylight.yangtools.yang.data.tree.api.DataTreeCandidate;
+import org.opendaylight.yangtools.yang.data.tree.api.DataTreeCandidateNode;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-@NotThreadSafe
-final class LocRibWriter<C extends Routes & DataObject & ChoiceIn<Tables>, S extends ChildOf<? super C>,
-        R extends Route & ChildOf<? super S> & Identifiable<I>, I extends Identifier<R>>
+// This class is NOT thread-safe
+final class LocRibWriter<C extends Routes & DataObject & ChoiceIn<Tables>, S extends ChildOf<? super C>>
         implements AutoCloseable, RibOutRefresh, TotalPrefixesCounter, TotalPathsCounter,
-        ClusteredDataTreeChangeListener<Tables> {
+        ClusteredDOMDataTreeChangeListener {
 
     private static final Logger LOG = LoggerFactory.getLogger(LocRibWriter.class);
 
-    private final Map<String, RouteEntry<C, S, R, I>> routeEntries = new HashMap<>();
+    private final Map<String, RouteEntry<C, S>> routeEntries = new HashMap<>();
     private final long ourAs;
-    private final RIBSupport<C, S, R, I> ribSupport;
-    private final DataBroker dataBroker;
+    private final RIBSupport<C, S> ribSupport;
+    private final DOMDataTreeChangeService dataBroker;
     private final PathSelectionMode pathSelectionMode;
     private final LongAdder totalPathsCounter = new LongAdder();
     private final LongAdder totalPrefixesCounter = new LongAdder();
     private final RouteEntryDependenciesContainerImpl entryDep;
     private final BGPPeerTracker peerTracker;
-    private final KeyedInstanceIdentifier<Rib, RibKey> ribIId;
-    private final KeyedInstanceIdentifier<Tables, TablesKey> locRibTableIID;
+    private final YangInstanceIdentifier ribIId;
+    private final YangInstanceIdentifier locRibTableIID;
 
-    private BindingTransactionChain chain;
+    private DOMTransactionChain chain;
     @GuardedBy("this")
     private ListenerRegistration<?> reg;
 
-    private LocRibWriter(final RIBSupport<C, S, R, I> ribSupport,
-            final BindingTransactionChain chain,
-            final KeyedInstanceIdentifier<Rib, RibKey> ribIId,
-            final Long ourAs,
-            final DataBroker dataBroker,
+    private LocRibWriter(final RIBSupport<C, S> ribSupport,
+            final DOMTransactionChain chain,
+            final YangInstanceIdentifier ribIId,
+            final Uint32 ourAs,
+            final DOMDataTreeChangeService dataBroker,
             final BGPRibRoutingPolicy ribPolicies,
             final BGPPeerTracker peerTracker,
-            final Class<? extends AfiSafiType> afiSafiType,
+            final AfiSafiType afiSafiType,
             final PathSelectionMode pathSelectionMode) {
         this.chain = requireNonNull(chain);
         this.ribIId = requireNonNull(ribIId);
         this.ribSupport = requireNonNull(ribSupport);
-        this.locRibTableIID = ribIId.child(LocRib.class).child(Tables.class, ribSupport.getTablesKey());
-        this.ourAs = ourAs;
+
+        this.locRibTableIID = ribIId.node(LOCRIB_NID).node(TABLES_NID).node(ribSupport.emptyTable().getIdentifier())
+            .toOptimized();
+        this.ourAs = ourAs.toJava();
         this.dataBroker = requireNonNull(dataBroker);
         this.peerTracker = peerTracker;
         this.pathSelectionMode = pathSelectionMode;
 
         this.entryDep = new RouteEntryDependenciesContainerImpl(this.ribSupport, this.peerTracker, ribPolicies,
                 afiSafiType, this.locRibTableIID);
-        init();
     }
 
-    public static <C extends Routes & DataObject & ChoiceIn<Tables>, S extends ChildOf<? super C>,
-                R extends Route & ChildOf<? super S> & Identifiable<I>, I extends Identifier<R>>
-                LocRibWriter<C, S, R, I> create(
-            @Nonnull final RIBSupport<C, S, R, I> ribSupport,
-            @Nonnull final Class<? extends AfiSafiType> afiSafiType,
-            @Nonnull final BindingTransactionChain chain,
-            @Nonnull final KeyedInstanceIdentifier<Rib, RibKey> ribIId,
-            @Nonnull final AsNumber ourAs,
-            @Nonnull final DataBroker dataBroker,
+    public static <C extends Routes & DataObject & ChoiceIn<Tables>, S extends ChildOf<? super C>>
+                LocRibWriter<C, S> create(
+            final @NonNull RIBSupport<C, S> ribSupport,
+            final @NonNull AfiSafiType afiSafiType,
+            final @NonNull DOMTransactionChain chain,
+            final @NonNull YangInstanceIdentifier ribIId,
+            final @NonNull AsNumber ourAs,
+            final @NonNull DOMDataTreeChangeService dataBroker,
             final BGPRibRoutingPolicy ribPolicies,
-            @Nonnull final BGPPeerTracker peerTracker,
-            @Nonnull final PathSelectionMode pathSelectionStrategy) {
-        return new LocRibWriter<>(ribSupport, chain, ribIId, ourAs.getValue(), dataBroker, ribPolicies,
-                peerTracker, afiSafiType, pathSelectionStrategy);
+            final @NonNull BGPPeerTracker peerTracker,
+            final @NonNull PathSelectionMode pathSelectionStrategy) {
+        final LocRibWriter<C, S> ret = new LocRibWriter<>(ribSupport, chain, ribIId, ourAs.getValue(), dataBroker,
+            ribPolicies, peerTracker, afiSafiType, pathSelectionStrategy);
+        ret.init();
+        return ret;
     }
 
     private synchronized void init() {
-        final WriteTransaction tx = this.chain.newWriteOnlyTransaction();
-        tx.merge(LogicalDatastoreType.OPERATIONAL,
-                this.locRibTableIID.builder().child(Attributes.class).build(),
-                new AttributesBuilder().setUptodate(true).build());
+        final DOMDataTreeWriteTransaction tx = this.chain.newWriteOnlyTransaction();
+        tx.put(LogicalDatastoreType.OPERATIONAL, this.locRibTableIID.node(ATTRIBUTES_NID).node(UPTODATE_NID),
+                RIBNormalizedNodes.ATTRIBUTES_UPTODATE_TRUE);
         tx.commit().addCallback(new FutureCallback<CommitInfo>() {
             @Override
             public void onSuccess(final CommitInfo result) {
@@ -150,10 +153,9 @@ final class LocRibWriter<C extends Routes & DataObject & ChoiceIn<Tables>, S ext
             }
         }, MoreExecutors.directExecutor());
 
-        final InstanceIdentifier<Tables> tableId = this.ribIId.builder().child(Peer.class)
-                .child(EffectiveRibIn.class).child(Tables.class, getTableKey()).build();
-        this.reg = this.dataBroker.registerDataTreeChangeListener(
-                new DataTreeIdentifier<>(LogicalDatastoreType.OPERATIONAL, tableId), this);
+        this.reg = this.dataBroker.registerDataTreeChangeListener(new DOMDataTreeIdentifier(
+            LogicalDatastoreType.OPERATIONAL, ribIId.node(PEER_NID).node(PEER_NID).node(EFFRIBIN_NID).node(TABLES_NID)
+                .node(locRibTableIID.getLastPathArgument())), this);
     }
 
     /**
@@ -161,7 +163,7 @@ final class LocRibWriter<C extends Routes & DataObject & ChoiceIn<Tables>, S ext
      *
      * @param newChain new transaction chain
      */
-    synchronized void restart(@Nonnull final BindingTransactionChain newChain) {
+    synchronized void restart(final @NonNull DOMTransactionChain newChain) {
         requireNonNull(newChain);
         close();
         this.chain = newChain;
@@ -180,15 +182,19 @@ final class LocRibWriter<C extends Routes & DataObject & ChoiceIn<Tables>, S ext
         }
     }
 
-    @Nonnull
-    private RouteEntry<C, S, R, I> createEntry(final String routeId) {
-        final RouteEntry<C, S, R, I> ret = this.pathSelectionMode.createRouteEntry();
+    private @NonNull RouteEntry<C, S> createEntry(final String routeId) {
+        final RouteEntry<C, S> ret = this.pathSelectionMode.createRouteEntry();
         this.routeEntries.put(routeId, ret);
         this.totalPrefixesCounter.increment();
         LOG.trace("Created new entry for {}", routeId);
         return ret;
     }
 
+    @Override
+    public synchronized void onInitialData() {
+        // FIXME: we need to do something
+    }
+
     /**
      * We use two-stage processing here in hopes that we avoid duplicate
      * calculations when multiple peers have changed a particular entry.
@@ -196,15 +202,16 @@ final class LocRibWriter<C extends Routes & DataObject & ChoiceIn<Tables>, S ext
      * @param changes on supported table
      */
     @Override
-    public synchronized void onDataTreeChanged(final Collection<DataTreeModification<Tables>> changes) {
+    @SuppressWarnings("checkstyle:illegalCatch")
+    public synchronized void onDataTreeChanged(final List<DataTreeCandidate> changes) {
         if (this.chain == null) {
             LOG.trace("Chain closed, ignoring received data change {} to LocRib {}", changes, this);
             return;
         }
         LOG.trace("Received data change {} to LocRib {}", changes, this);
-        final WriteTransaction tx = this.chain.newWriteOnlyTransaction();
+        final DOMDataTreeWriteTransaction tx = this.chain.newWriteOnlyTransaction();
         try {
-            final Map<RouteUpdateKey, RouteEntry<C, S, R, I>> toUpdate = update(tx, changes);
+            final Map<RouteUpdateKey, RouteEntry<C, S>> toUpdate = update(tx, changes);
 
             if (!toUpdate.isEmpty()) {
                 walkThrough(tx, toUpdate.entrySet());
@@ -227,26 +234,24 @@ final class LocRibWriter<C extends Routes & DataObject & ChoiceIn<Tables>, S ext
     }
 
     @SuppressWarnings("unchecked")
-    private Map<RouteUpdateKey, RouteEntry<C, S, R, I>> update(final WriteTransaction tx,
-            final Collection<DataTreeModification<Tables>> changes) {
-        final Map<RouteUpdateKey, RouteEntry<C, S, R, I>> ret = new HashMap<>();
-        for (final DataTreeModification<Tables> tc : changes) {
-            final DataObjectModification<Tables> table = tc.getRootNode();
-            final DataTreeIdentifier<Tables> rootPath = tc.getRootPath();
-            final KeyedInstanceIdentifier<Peer, PeerKey> peerKIid = (KeyedInstanceIdentifier<Peer, PeerKey>)
-                    rootPath.getRootIdentifier().firstIdentifierOf(Peer.class);
-            final RouterId peerUuid = RouterId.forPeerId(peerKIid.getKey().getPeerId());
+    private Map<RouteUpdateKey, RouteEntry<C, S>> update(final DOMDataTreeWriteOperations tx,
+            final Collection<DataTreeCandidate> changes) {
+        final Map<RouteUpdateKey, RouteEntry<C, S>> ret = new HashMap<>();
+        for (final DataTreeCandidate tc : changes) {
+            final DataTreeCandidateNode table = tc.getRootNode();
+            final RouterId peerUuid = RouterId.forPeerId(IdentifierUtils.peerKeyToPeerId(tc.getRootPath()));
+
             /*
             Initialize Peer with routes under loc rib
              */
-            if (!this.routeEntries.isEmpty() && table.getDataBefore() == null) {
+            if (!this.routeEntries.isEmpty() && table.getDataBefore().isEmpty()) {
                 final org.opendaylight.protocol.bgp.rib.spi.Peer toPeer
-                        = this.peerTracker.getPeer(peerKIid.getKey().getPeerId());
+                        = this.peerTracker.getPeer(peerUuid.getPeerId());
                 if (toPeer != null && toPeer.supportsTable(this.entryDep.getLocalTablesKey())) {
                     LOG.debug("Peer {} table has been created, inserting existent routes", toPeer.getPeerId());
-                    final List<ActualBestPathRoutes<C, S, R, I>> routesToStore = new ArrayList<>();
-                    for (final Entry<String, RouteEntry<C, S, R, I>> entry : this.routeEntries.entrySet()) {
-                        final List<ActualBestPathRoutes<C, S, R, I>> filteredRoute = entry.getValue()
+                    final List<ActualBestPathRoutes<C, S>> routesToStore = new ArrayList<>();
+                    for (final Entry<String, RouteEntry<C, S>> entry : this.routeEntries.entrySet()) {
+                        final List<ActualBestPathRoutes<C, S>> filteredRoute = entry.getValue()
                                 .actualBestPaths(this.ribSupport, new RouteEntryInfoImpl(toPeer, entry.getKey()));
                         routesToStore.addAll(filteredRoute);
                     }
@@ -261,42 +266,37 @@ final class LocRibWriter<C extends Routes & DataObject & ChoiceIn<Tables>, S ext
         return ret;
     }
 
-    private void updateNodes(final DataObjectModification<Tables> table, final RouterId peerUuid,
-            final WriteTransaction tx, final Map<RouteUpdateKey, RouteEntry<C, S, R, I>> routes) {
-        final DataObjectModification<Attributes> attUpdate = table.getModifiedChildContainer(Attributes.class);
-        if (attUpdate != null) {
-            final Attributes newAttValue = attUpdate.getDataAfter();
-            if (newAttValue != null) {
-                LOG.trace("Uptodate found for {}", newAttValue);
-                tx.put(LogicalDatastoreType.OPERATIONAL, this.locRibTableIID.child(Attributes.class), newAttValue);
-            }
-        }
+    private void updateNodes(final DataTreeCandidateNode table, final RouterId peerUuid,
+            final DOMDataTreeWriteOperations tx, final Map<RouteUpdateKey, RouteEntry<C, S>> routes) {
+        table.getModifiedChild(ATTRIBUTES_NID).flatMap(DataTreeCandidateNode::getDataAfter).ifPresent(newAttValue -> {
+            LOG.trace("Uptodate found for {}", newAttValue);
+            tx.put(LogicalDatastoreType.OPERATIONAL, this.locRibTableIID.node(ATTRIBUTES_NID), newAttValue);
+        });
 
-        final DataObjectModification<S> routesChangesContainer
-                = table.getModifiedChildContainer(ribSupport.routesCaseClass(), ribSupport.routesContainerClass());
-        if (routesChangesContainer != null) {
-            updateRoutesEntries(routesChangesContainer.getModifiedChildren(), peerUuid, routes);
-        }
+        table.getModifiedChild(ROUTES_NID).ifPresent(modifiedRoutes -> {
+            updateRoutesEntries(ribSupport.changedRoutes(modifiedRoutes), peerUuid, routes);
+        });
     }
 
-    private void updateRoutesEntries(final Collection<? extends DataObjectModification<?>> collection,
-            final RouterId routerId, final Map<RouteUpdateKey, RouteEntry<C, S, R, I>> routes) {
-        for (final DataObjectModification<? extends DataObject> route : collection) {
-            if (!(route.getIdentifier() instanceof InstanceIdentifier.IdentifiableItem)) {
-                LOG.debug("Route {} already deleted", route.getIdentifier());
+    private void updateRoutesEntries(final Collection<DataTreeCandidateNode> collection,
+            final RouterId routerId, final Map<RouteUpdateKey, RouteEntry<C, S>> routes) {
+        for (final DataTreeCandidateNode route : collection) {
+            final PathArgument routeArg = route.getIdentifier();
+            if (!(routeArg instanceof NodeIdentifierWithPredicates routeId)) {
+                LOG.debug("Route {} already deleted", routeArg);
                 return;
             }
-            final I routeListKey = (I) ((InstanceIdentifier.IdentifiableItem) route.getIdentifier()).getKey();
-            final String routeKey = ribSupport.extractRouteKey(routeListKey);
-            final PathId pathId = ribSupport.extractPathId(routeListKey);
 
-            RouteEntry<C, S, R, I> entry;
+            final String routeKey = ribSupport.extractRouteKey(routeId);
+            final Uint32 pathId = ribSupport.extractPathId(routeId);
+
+            RouteEntry<C, S> entry;
             switch (route.getModificationType()) {
                 case DELETE:
                     entry = this.routeEntries.get(routeKey);
                     if (entry != null) {
                         this.totalPathsCounter.decrement();
-                        if (entry.removeRoute(routerId, pathId.getValue())) {
+                        if (entry.removeRoute(routerId, pathId)) {
                             this.routeEntries.remove(routeKey);
                             this.totalPrefixesCounter.decrement();
                             LOG.trace("Removed route from {}", routerId);
@@ -305,13 +305,14 @@ final class LocRibWriter<C extends Routes & DataObject & ChoiceIn<Tables>, S ext
                     break;
                 case SUBTREE_MODIFIED:
                 case WRITE:
-                    final R newRoute = (R) route.getDataAfter();
                     entry = this.routeEntries.get(routeKey);
                     if (entry == null) {
                         entry = createEntry(routeKey);
                     }
 
-                    entry.addRoute(routerId, pathId.getValue(), newRoute);
+                    final NormalizedNode routeAfter = route.getDataAfter().get();
+                    verify(routeAfter instanceof MapEntryNode, "Unexpected route %s", routeAfter);
+                    entry.addRoute(routerId, pathId, (MapEntryNode) routeAfter);
                     this.totalPathsCounter.increment();
                     break;
                 default:
@@ -324,15 +325,15 @@ final class LocRibWriter<C extends Routes & DataObject & ChoiceIn<Tables>, S ext
         }
     }
 
-    private void walkThrough(final WriteTransaction tx,
-            final Set<Entry<RouteUpdateKey, RouteEntry<C, S, R, I>>> toUpdate) {
-        final List<StaleBestPathRoute<C, S, R, I>> staleRoutes = new ArrayList<>();
-        final List<AdvertizedRoute<C, S, R, I>> newRoutes = new ArrayList<>();
-        for (final Entry<RouteUpdateKey, RouteEntry<C, S, R, I>> e : toUpdate) {
+    private void walkThrough(final DOMDataTreeWriteOperations tx,
+            final Set<Entry<RouteUpdateKey, RouteEntry<C, S>>> toUpdate) {
+        final List<StaleBestPathRoute> staleRoutes = new ArrayList<>();
+        final List<AdvertizedRoute<C, S>> newRoutes = new ArrayList<>();
+        for (final Entry<RouteUpdateKey, RouteEntry<C, S>> e : toUpdate) {
             LOG.trace("Walking through {}", e);
-            final RouteEntry<C, S, R, I> entry = e.getValue();
+            final RouteEntry<C, S> entry = e.getValue();
 
-            if (!entry.selectBest(this.ourAs)) {
+            if (!entry.selectBest(this.ribSupport, this.ourAs)) {
                 LOG.trace("Best path has not changed, continuing");
                 continue;
             }
@@ -341,29 +342,31 @@ final class LocRibWriter<C extends Routes & DataObject & ChoiceIn<Tables>, S ext
             newRoutes.addAll(entry.newBestPaths(this.ribSupport, e.getKey().getRouteId()));
         }
         updateLocRib(newRoutes, staleRoutes, tx);
-        this.peerTracker.getNonInternalPeers().parallelStream().forEach(
-            toPeer -> toPeer.refreshRibOut(this.entryDep, staleRoutes, newRoutes));
+        this.peerTracker.getNonInternalPeers().parallelStream()
+                .filter(toPeer -> toPeer.supportsTable(this.entryDep.getLocalTablesKey()))
+                .forEach(toPeer -> toPeer.refreshRibOut(this.entryDep, staleRoutes, newRoutes));
     }
 
-    private void updateLocRib(final List<AdvertizedRoute<C, S, R, I>> newRoutes,
-            final List<StaleBestPathRoute<C, S, R, I>> staleRoutes,
-            final WriteTransaction tx) {
-        final KeyedInstanceIdentifier<Tables, TablesKey> locRibTarget = this.entryDep.getLocRibTableTarget();
+    private void updateLocRib(final List<AdvertizedRoute<C, S>> newRoutes, final List<StaleBestPathRoute> staleRoutes,
+            final DOMDataTreeWriteOperations tx) {
+        final YangInstanceIdentifier locRibTarget = this.entryDep.getLocRibTableTarget();
 
-        for (final StaleBestPathRoute<C, S, R, I> staleContainer : staleRoutes) {
-            for (final I routeId : staleContainer.getStaleRouteKeyIdentifiers()) {
-                final InstanceIdentifier<R> routeTarget = ribSupport.createRouteIdentifier(locRibTarget, routeId);
+        for (final StaleBestPathRoute staleContainer : staleRoutes) {
+            for (final NodeIdentifierWithPredicates routeId : staleContainer.getStaleRouteKeyIdentifiers()) {
+                final YangInstanceIdentifier routeTarget = ribSupport.createRouteIdentifier(locRibTarget, routeId);
                 LOG.debug("Delete route from LocRib {}", routeTarget);
                 tx.delete(LogicalDatastoreType.OPERATIONAL, routeTarget);
             }
         }
 
-        for (final AdvertizedRoute<C,S,R,I> advRoute : newRoutes) {
-            final R route = advRoute.getRoute();
-            final I iid = advRoute.getAddPathRouteKeyIdentifier();
-            final InstanceIdentifier<R> locRibRouteTarget
-                    = this.ribSupport.createRouteIdentifier(locRibTarget, iid);
-            LOG.debug("Write route to LocRib {}", route);
+        for (final AdvertizedRoute<C, S> advRoute : newRoutes) {
+            final MapEntryNode route = advRoute.getRoute();
+            final NodeIdentifierWithPredicates iid = advRoute.getAddPathRouteKeyIdentifier();
+            final YangInstanceIdentifier locRibRouteTarget = this.ribSupport.createRouteIdentifier(locRibTarget, iid);
+            LOG.debug("Write LocRib route {}", locRibRouteTarget);
+            if (LOG.isTraceEnabled()) {
+                LOG.trace("Write route to LocRib {}", NormalizedNodes.toStringTree(route));
+            }
             tx.put(LogicalDatastoreType.OPERATIONAL, locRibRouteTarget, route);
         }
     }
@@ -387,9 +390,9 @@ final class LocRibWriter<C extends Routes & DataObject & ChoiceIn<Tables>, S ext
         final org.opendaylight.protocol.bgp.rib.spi.Peer toPeer = this.peerTracker.getPeer(peerId);
         if (toPeer != null && toPeer.supportsTable(this.entryDep.getLocalTablesKey())) {
             LOG.debug("Peer {} table has been created, inserting existent routes", toPeer.getPeerId());
-            final List<ActualBestPathRoutes<C, S, R, I>> routesToStore = new ArrayList<>();
-            for (final Entry<String, RouteEntry<C, S, R, I>> entry : this.routeEntries.entrySet()) {
-                final List<ActualBestPathRoutes<C, S, R, I>> filteredRoute = entry.getValue()
+            final List<ActualBestPathRoutes<C, S>> routesToStore = new ArrayList<>();
+            for (final Entry<String, RouteEntry<C, S>> entry : this.routeEntries.entrySet()) {
+                final List<ActualBestPathRoutes<C, S>> filteredRoute = entry.getValue()
                         .actualBestPaths(this.ribSupport, new RouteEntryInfoImpl(toPeer, entry.getKey()));
                 routesToStore.addAll(filteredRoute);
             }