Bug 5061 - introduce peer restarting 69/44069/2
authorMilos Fabian <milfabia@cisco.com>
Tue, 26 Jul 2016 14:04:52 +0000 (16:04 +0200)
committerMilos Fabian <milfabia@cisco.com>
Tue, 16 Aug 2016 14:27:47 +0000 (14:27 +0000)
When RIB configuration is modified all related
peer are supposed to be restart with modified RIB.
Introduce such function for BgpPeer/App, preserve
current Neighbor configuration internally.

Change-Id: I608a564a956f255bdb89603e736dd78cd60fef62
Signed-off-by: Milos Fabian <milfabia@cisco.com>
(cherry picked from commit 19a3a5142a0f34d1eba29250c9787e69f3d2cbbf)

bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/config/AppPeer.java
bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/config/BgpDeployerImpl.java
bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/config/BgpPeer.java
bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/config/PeerBean.java

index bd66144458c97b6b4a45045557dce5346aa8878d..0481d9d719eae8bcc9d79e61c7f40ef0e71c695a 100644 (file)
@@ -8,6 +8,7 @@
 
 package org.opendaylight.protocol.bgp.rib.impl.config;
 
+import com.google.common.base.Preconditions;
 import com.google.common.base.Strings;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeIdentifier;
@@ -29,9 +30,11 @@ public class AppPeer implements PeerBean {
 
     private ApplicationPeer applicationPeer;
     private ListenerRegistration<ApplicationPeer> registration;
+    private Neighbor currentConfiguration;
 
     @Override
     public void start(final RIB rib, final Neighbor neighbor, final BGPOpenConfigMappingService mappingService) {
+        this.currentConfiguration = Preconditions.checkNotNull(neighbor);
         final ApplicationRibId appRibId = createAppRibId(neighbor);
         this.applicationPeer = new ApplicationPeer(appRibId, neighbor.getNeighborAddress().getIpv4Address(), rib);
         final YangInstanceIdentifier yangIId = YangInstanceIdentifier.builder().node(ApplicationRib.QNAME)
@@ -40,6 +43,12 @@ public class AppPeer implements PeerBean {
                 this.applicationPeer);
     }
 
+    @Override
+    public void restart(final RIB rib, final BGPOpenConfigMappingService mappingService) {
+        Preconditions.checkState(this.currentConfiguration != null);
+        start(rib, this.currentConfiguration, mappingService);
+    }
+
     @Override
     public void close() {
         if (this.applicationPeer != null) {
index 12741051b11451063919dc1ba39ec3441550e4cf..0804d72c58d5ce3cb842f170d400d7735c5ae839 100644 (file)
@@ -17,11 +17,14 @@ import com.google.common.util.concurrent.CheckedFuture;
 import com.google.common.util.concurrent.FutureCallback;
 import com.google.common.util.concurrent.Futures;
 import com.google.common.util.concurrent.ListenableFuture;
+import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Dictionary;
 import java.util.HashMap;
 import java.util.Hashtable;
+import java.util.List;
 import java.util.Map;
+import java.util.Map.Entry;
 import javax.annotation.concurrent.GuardedBy;
 import org.opendaylight.controller.md.sal.binding.api.ClusteredDataTreeChangeListener;
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
@@ -165,12 +168,26 @@ public final class BgpDeployerImpl implements BgpDeployer, ClusteredDataTreeChan
             //if not exists, create a new instance
             onGlobalCreated(rootIdentifier, global, null);
         } else if (!ribImpl.isGlobalEqual(global)) {
+            final List<PeerBean> closedPeers = closeAllBindedPeers(rootIdentifier);
             ribImpl.close();
             initiateRibInstance(rootIdentifier, global, ribImpl, null);
+            closedPeers.forEach(peer -> peer.restart(ribImpl, this.mappingService));
         }
         LOG.debug("RIB instance modified {}", ribImpl);
     }
 
+    private List<PeerBean> closeAllBindedPeers(final InstanceIdentifier<Bgp> rootIdentifier) {
+        final List<PeerBean> filtered = new ArrayList<>();
+        for (final Entry<InstanceIdentifier<Neighbor>, PeerBean> entry : this.peers.entrySet()) {
+            if (entry.getKey().contains(rootIdentifier)) {
+                final PeerBean peer = entry.getValue();
+                peer.close();
+                filtered.add(peer);
+            }
+        }
+        return filtered;
+    }
+
     @Override
     public synchronized void onGlobalCreated(final InstanceIdentifier<Bgp> rootIdentifier, final Global global, final WriteConfiguration
         configurationWriter) {
index a80b5de4b944710459b2ab024bc1263e2d1a0bac..19279574d999d1650c8b96cc2395761a6a14160b 100644 (file)
@@ -61,11 +61,10 @@ public class BgpPeer implements PeerBean, BGPPeerRuntimeMXBean {
     private final BGPPeerRegistry peerRegistry;
     private BGPPeer bgpPeer;
 
-    private IpAddress neighborAddress;
-
     private Future<Void> connection;
 
     private ServiceRegistration<?> serviceRegistration;
+    private Neighbor currentConfiguration;
 
     public BgpPeer(final RpcProviderRegistry rpcRegistry, final BGPPeerRegistry peerRegistry) {
         this.rpcRegistry = rpcRegistry;
@@ -75,22 +74,29 @@ public class BgpPeer implements PeerBean, BGPPeerRuntimeMXBean {
     @Override
     public void start(final RIB rib, final Neighbor neighbor, final BGPOpenConfigMappingService mappingService) {
         Preconditions.checkState(this.bgpPeer == null, "Previous peer instance {} was not closed.");
-        this.neighborAddress = neighbor.getNeighborAddress();
-        this.bgpPeer = new BGPPeer(Ipv4Util.toStringIP(this.neighborAddress), rib,
+        this.currentConfiguration = Preconditions.checkNotNull(neighbor);
+        final IpAddress neighborAddress = neighbor.getNeighborAddress();
+        this.bgpPeer = new BGPPeer(Ipv4Util.toStringIP(neighborAddress), rib,
                 mappingService.toPeerRole(neighbor), this.rpcRegistry);
         final List<BgpParameters> bgpParameters = getBgpParameters(neighbor, rib, mappingService);
         final KeyMapping key = OpenConfigMappingUtil.getNeighborKey(neighbor);
         final BGPSessionPreferences prefs = new BGPSessionPreferences(rib.getLocalAs(),
                 getHoldTimer(neighbor), rib.getBgpIdentifier(), getPeerAs(neighbor, rib), bgpParameters, getPassword(key));
-        this.peerRegistry.addPeer(this.neighborAddress, this.bgpPeer, prefs);
+        this.peerRegistry.addPeer(neighborAddress, this.bgpPeer, prefs);
         if (OpenConfigMappingUtil.isActive(neighbor)) {
             this.connection = rib.getDispatcher().createReconnectingClient(
-                    Ipv4Util.toInetSocketAddress(this.neighborAddress, PORT), this.peerRegistry,
+                    Ipv4Util.toInetSocketAddress(neighborAddress, PORT), this.peerRegistry,
                     OpenConfigMappingUtil.getRetryTimer(neighbor), Optional.fromNullable(key));
         }
 
     }
 
+    @Override
+    public void restart(final RIB rib, final BGPOpenConfigMappingService mappingService) {
+        Preconditions.checkState(this.currentConfiguration != null);
+        start(rib, this.currentConfiguration, mappingService);
+    }
+
     @Override
     public void close() {
         if (this.bgpPeer != null) {
@@ -100,8 +106,8 @@ public class BgpPeer implements PeerBean, BGPPeerRuntimeMXBean {
             }
             this.bgpPeer.close();
             this.bgpPeer = null;
-            this.peerRegistry.removePeer(this.neighborAddress);
-            this.neighborAddress = null;
+            this.peerRegistry.removePeer(this.currentConfiguration.getNeighborAddress());
+            this.currentConfiguration = null;
             if (this.serviceRegistration != null) {
                 this.serviceRegistration.unregister();
                 this.serviceRegistration = null;
index 64034e0fb27c602da1c3a81820c6898618a8f9ad..2ebf98c524c2aa1006d6939c0b08676c8378be4f 100644 (file)
@@ -20,6 +20,8 @@ public interface PeerBean extends AutoCloseable {
 
     void start(RIB rib, Neighbor neighbor, BGPOpenConfigMappingService mappingService);
 
+    void restart(RIB rib, BGPOpenConfigMappingService mappingService);
+
     @Override
     void close();