BUG-4714 : No routes from loc-rib are advertised
[bgpcep.git] / bgp / rib-impl / src / main / java / org / opendaylight / protocol / bgp / rib / impl / ExportPolicyPeerTracker.java
index 561981e4f47f7ba14182533d4685b93ace8fcf1f..69b67656105e96992c3415dc111a6f8aa11a4741 100644 (file)
@@ -11,6 +11,7 @@ import com.google.common.base.Function;
 import com.google.common.base.Preconditions;
 import com.google.common.collect.ArrayListMultimap;
 import com.google.common.collect.Collections2;
+import com.google.common.collect.HashMultimap;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.Multimap;
@@ -21,11 +22,14 @@ import java.util.EnumMap;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.Map.Entry;
-import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeService;
+import org.opendaylight.protocol.bgp.rib.spi.RibSupportUtils;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.PeerId;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.PeerRole;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.bgp.rib.rib.peer.SupportedTables;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.TablesKey;
 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.tree.DataTreeCandidateNode;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -43,11 +47,11 @@ final class ExportPolicyPeerTracker extends AbstractPeerRoleTracker {
     };
 
     private final Map<YangInstanceIdentifier, PeerRole> peerRoles = new HashMap<>();
+    private final HashMultimap<PeerId, NodeIdentifierWithPredicates> peerTables = HashMultimap.create();
     private volatile Map<PeerRole, PeerExportGroup> groups = Collections.emptyMap();
     private final PolicyDatabase policyDatabase;
 
-    ExportPolicyPeerTracker(final DOMDataTreeChangeService service, final YangInstanceIdentifier ribId, final PolicyDatabase policyDatabase) {
-        super(service, ribId);
+    ExportPolicyPeerTracker(final PolicyDatabase policyDatabase) {
         this.policyDatabase = Preconditions.checkNotNull(policyDatabase);
     }
 
@@ -59,20 +63,20 @@ final class ExportPolicyPeerTracker extends AbstractPeerRoleTracker {
         // Index things nicely for easy access
         final Multimap<PeerRole, YangInstanceIdentifier> roleToIds = ArrayListMultimap.create(PeerRole.values().length, 2);
         final Map<PeerId, PeerRole> idToRole = new HashMap<>();
-        for (Entry<YangInstanceIdentifier, PeerRole> e : peerPathRoles.entrySet()) {
+        for (final Entry<YangInstanceIdentifier, PeerRole> e : peerPathRoles.entrySet()) {
             roleToIds.put(e.getValue(), e.getKey());
             idToRole.put(IdentifierUtils.peerId((NodeIdentifierWithPredicates) e.getKey().getLastPathArgument()), e.getValue());
         }
 
         // Optimized immutable copy, reused for all PeerGroups
-        final Map<PeerId, PeerRole> peerRoles = ImmutableMap.copyOf(idToRole);
+        final Map<PeerId, PeerRole> allPeerRoles = ImmutableMap.copyOf(idToRole);
 
         final Map<PeerRole, PeerExportGroup> ret = new EnumMap<>(PeerRole.class);
-        for (Entry<PeerRole, Collection<YangInstanceIdentifier>> e : roleToIds.asMap().entrySet()) {
-            final AbstractExportPolicy policy = policyDatabase.exportPolicyForRole(e.getKey());
+        for (final Entry<PeerRole, Collection<YangInstanceIdentifier>> e : roleToIds.asMap().entrySet()) {
+            final AbstractExportPolicy policy = this.policyDatabase.exportPolicyForRole(e.getKey());
             final Collection<Entry<PeerId, YangInstanceIdentifier>> peers = ImmutableList.copyOf(Collections2.transform(e.getValue(), GENERATE_PEERID));
 
-            ret.put(e.getKey(), new PeerExportGroup(peers, peerRoles, policy));
+            ret.put(e.getKey(), new PeerExportGroup(peers, allPeerRoles, policy));
         }
 
         return ret;
@@ -86,18 +90,40 @@ final class ExportPolicyPeerTracker extends AbstractPeerRoleTracker {
          */
         final PeerRole oldRole;
         if (role != null) {
-            oldRole = peerRoles.put(peerPath, role);
+            oldRole = this.peerRoles.put(peerPath, role);
         } else {
-            oldRole = peerRoles.remove(peerPath);
+            oldRole = this.peerRoles.remove(peerPath);
         }
 
         if (role != oldRole) {
             LOG.debug("Peer {} changed role from {} to {}", peerPath, oldRole, role);
-            groups = createGroups(peerRoles);
+            this.groups = createGroups(this.peerRoles);
         }
     }
 
     PeerExportGroup getPeerGroup(final PeerRole role) {
-        return groups.get(Preconditions.checkNotNull(role));
+        return this.groups.get(Preconditions.checkNotNull(role));
+    }
+
+    boolean onTablesChanged(final PeerId peerId, final DataTreeCandidateNode node) {
+        if (node.getDataAfter().isPresent()) {
+            final NodeIdentifierWithPredicates value = (NodeIdentifierWithPredicates) node.getDataAfter().get().getIdentifier();
+            final boolean added = this.peerTables.put(peerId, value);
+            if (added) {
+                LOG.debug("Supported table {} added to peer {}", value, peerId);
+            }
+            return added;
+        } else {
+            LOG.debug("Removed tables {} from peer {}", this.peerTables.removeAll(peerId), peerId);
+        }
+        return false;
+    }
+
+    boolean isTableSupported(final PeerId peerId, final TablesKey tablesKey) {
+        return this.peerTables.get(peerId).contains(RibSupportUtils.toYangKey(SupportedTables.QNAME, tablesKey));
+    }
+
+    public PeerRole getRole(final YangInstanceIdentifier peerId) {
+        return this.peerRoles.get(peerId);
     }
 }
\ No newline at end of file