BUG-2383 : wired cluster id with effective RIB
[bgpcep.git] / bgp / rib-impl / src / main / java / org / opendaylight / protocol / bgp / rib / impl / LocRibWriter.java
index c36ed77d0f048cdcccecd9be6d8d6ee1304c6563..db497f4c3e21f29ac2abbec03a10ac201f9e784c 100644 (file)
@@ -10,19 +10,24 @@ package org.opendaylight.protocol.bgp.rib.impl;
 import com.google.common.base.Preconditions;
 import com.google.common.primitives.UnsignedInteger;
 import java.util.Collection;
-import java.util.EnumMap;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.Map.Entry;
+import javax.annotation.Nonnull;
 import javax.annotation.concurrent.NotThreadSafe;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeListener;
+import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeService;
+import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeIdentifier;
 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
 import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
 import org.opendaylight.protocol.bgp.rib.spi.RIBSupport;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
 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.LocRib;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
+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.ContainerNode;
@@ -31,22 +36,33 @@ import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
 
 // FIXME: instantiate for each table, listen on wildcard peer and routes
 @NotThreadSafe
-final class LocRibWriter implements DOMDataTreeChangeListener {
+final class LocRibWriter implements AutoCloseable, DOMDataTreeChangeListener {
     private final Map<PathArgument, RouteEntry> routeEntries = new HashMap<>();
     private final YangInstanceIdentifier target;
     private final DOMTransactionChain chain;
-    private final RIBSupport ribSupport;
+    private final ExportPolicyPeerTracker peerPolicyTracker;
+    private final NodeIdentifier attributesIdentifier;
     private final Long ourAs;
 
-    // FIXME: these maps need to be populated
-    private final Map<PeerRole, Map<PeerId, YangInstanceIdentifier>> peersToUpdate = new EnumMap<>(PeerRole.class);
-    private final Map<PeerId, PeerRole> peers = new HashMap<>();
-
-    LocRibWriter(final RIBSupport ribSupport, final DOMTransactionChain chain, final YangInstanceIdentifier target, final Long ourAs) {
+    LocRibWriter(final RIBSupport ribSupport, final DOMTransactionChain chain, final YangInstanceIdentifier target, final Long ourAs,
+        final DOMDataTreeChangeService service, final PolicyDatabase pd) {
         this.chain = Preconditions.checkNotNull(chain);
         this.target = Preconditions.checkNotNull(target);
         this.ourAs = Preconditions.checkNotNull(ourAs);
-        this.ribSupport = Preconditions.checkNotNull(ribSupport);
+        this.attributesIdentifier = ribSupport.routeAttributesIdentifier();
+        this.peerPolicyTracker = new ExportPolicyPeerTracker(service, target, pd);
+
+        service.registerDataTreeChangeListener(new DOMDataTreeIdentifier(LogicalDatastoreType.OPERATIONAL, target.node(LocRib.QNAME)), this);
+    }
+
+    public static LocRibWriter create(@Nonnull final RIBSupport ribSupport, @Nonnull final DOMTransactionChain chain, @Nonnull final YangInstanceIdentifier target,
+        @Nonnull final AsNumber ourAs, @Nonnull final DOMDataTreeChangeService service, @Nonnull final PolicyDatabase pd) {
+        return new LocRibWriter(ribSupport, chain, target, ourAs.getValue(), service, pd);
+    }
+
+    @Override
+    public void close() {
+        this.peerPolicyTracker.close();
     }
 
     @Override
@@ -56,40 +72,38 @@ final class LocRibWriter implements DOMDataTreeChangeListener {
          * calculations when multiple peers have changed a particular entry.
          */
         final Map<RouteUpdateKey, RouteEntry> toUpdate = new HashMap<>();
-        for (DataTreeCandidate tc : changes) {
+        for (final DataTreeCandidate tc : changes) {
             final YangInstanceIdentifier path = tc.getRootPath();
             final PathArgument routeId = path.getLastPathArgument();
             final NodeIdentifierWithPredicates peerKey = IdentifierUtils.peerKey(path);
             final PeerId peerId = IdentifierUtils.peerId(peerKey);
             final UnsignedInteger routerId = RouterIds.routerIdForPeerId(peerId);
 
-            RouteEntry entry = routeEntries.get(routeId);
+            RouteEntry entry = this.routeEntries.get(routeId);
             if (tc.getRootNode().getDataAfter().isPresent()) {
                 if (entry == null) {
                     entry = new RouteEntry();
-                    routeEntries.put(routeId, entry);
+                    this.routeEntries.put(routeId, entry);
                 }
 
                 entry.addRoute(routerId, (ContainerNode) tc.getRootNode().getDataAfter().get());
-            } else if (entry != null) {
-                if (entry.removeRoute(routerId)) {
-                    routeEntries.remove(routeId);
-                    entry = null;
-                }
+            } else if (entry != null && entry.removeRoute(routerId)) {
+                this.routeEntries.remove(routeId);
+                entry = null;
             }
 
             toUpdate.put(new RouteUpdateKey(peerId, routeId), entry);
         }
 
-        final DOMDataWriteTransaction tx = chain.newWriteOnlyTransaction();
+        final DOMDataWriteTransaction tx = this.chain.newWriteOnlyTransaction();
 
         // Now walk all updated entries
-        for (Entry<RouteUpdateKey, RouteEntry> e : toUpdate.entrySet()) {
+        for (final Entry<RouteUpdateKey, RouteEntry> e : toUpdate.entrySet()) {
             final RouteEntry entry = e.getValue();
             final NormalizedNode<?, ?> value;
 
             if (entry != null) {
-                if (!entry.selectBest(ourAs)) {
+                if (!entry.selectBest(this.ourAs)) {
                     // Best path has not changed, no need to do anything else. Proceed to next route.
                     continue;
                 }
@@ -100,9 +114,9 @@ final class LocRibWriter implements DOMDataTreeChangeListener {
             }
 
             if (value != null) {
-                tx.put(LogicalDatastoreType.OPERATIONAL, target.node(e.getKey().getRouteId()), value);
+                tx.put(LogicalDatastoreType.OPERATIONAL, this.target.node(e.getKey().getRouteId()), value);
             } else {
-                tx.delete(LogicalDatastoreType.OPERATIONAL, target.node(e.getKey().getRouteId()));
+                tx.delete(LogicalDatastoreType.OPERATIONAL, this.target.node(e.getKey().getRouteId()));
             }
 
             /*
@@ -114,25 +128,23 @@ final class LocRibWriter implements DOMDataTreeChangeListener {
              * if we have two eBGP peers, for example, there is no reason why we should perform the translation
              * multiple times.
              */
-            for (Entry<PeerRole, AbstractExportPolicy> pe : AbstractExportPolicy.POLICIES.entrySet()) {
-                final Map<PeerId, YangInstanceIdentifier> toPeers = peersToUpdate.get(pe.getKey());
-                if (toPeers == null || toPeers.isEmpty()) {
-                    continue;
-                }
-
-                final ContainerNode attributes = null;
-                final PeerId peerId = e.getKey().getPeerId();
-                final ContainerNode effectiveAttributes = pe.getValue().effectiveAttributes(peers.get(peerId), attributes);
-
-                for (Entry<PeerId, YangInstanceIdentifier> pid : toPeers.entrySet()) {
-                    // This points to adj-rib-out for a particlar peer/table combination
-                    final YangInstanceIdentifier routeTarget = pid.getValue().node(e.getKey().getRouteId());
-
-                    if (effectiveAttributes != null && value != null && !peerId.equals(pid.getKey())) {
-                        tx.put(LogicalDatastoreType.OPERATIONAL, routeTarget, value);
-                        tx.put(LogicalDatastoreType.OPERATIONAL, routeTarget.node(ribSupport.routeAttributes()), effectiveAttributes);
-                    } else {
-                        tx.delete(LogicalDatastoreType.OPERATIONAL, routeTarget);
+            for (final PeerRole role : PeerRole.values()) {
+                final PeerExportGroup peerGroup = this.peerPolicyTracker.getPeerGroup(role);
+                if (peerGroup != null) {
+                    final ContainerNode attributes = null;
+                    final PeerId peerId = e.getKey().getPeerId();
+                    final ContainerNode effectiveAttributes = peerGroup.effectiveAttributes(peerId, attributes);
+
+                    for (final Entry<PeerId, YangInstanceIdentifier> pid : peerGroup.getPeers()) {
+                        // This points to adj-rib-out for a particular peer/table combination
+                        final YangInstanceIdentifier routeTarget = pid.getValue().node(e.getKey().getRouteId());
+
+                        if (effectiveAttributes != null && value != null && !peerId.equals(pid.getKey())) {
+                            tx.put(LogicalDatastoreType.OPERATIONAL, routeTarget, value);
+                            tx.put(LogicalDatastoreType.OPERATIONAL, routeTarget.node(this.attributesIdentifier), effectiveAttributes);
+                        } else {
+                            tx.delete(LogicalDatastoreType.OPERATIONAL, routeTarget);
+                        }
                     }
                 }
             }