BUG-5685: Register BGP Peer Cluster Singleton Service
[bgpcep.git] / bgp / rib-impl / src / main / java / org / opendaylight / protocol / bgp / rib / impl / config / BgpPeer.java
index 7159c704b41c9f5ad4996c4d126618d5dfb2f4a7..38b41787f61fbf7801a9313737a5050e3e8e7cdf 100644 (file)
@@ -14,19 +14,27 @@ import static org.opendaylight.protocol.bgp.rib.impl.config.OpenConfigMappingUti
 import com.google.common.base.Optional;
 import com.google.common.base.Preconditions;
 import com.google.common.collect.Iterables;
+import com.google.common.util.concurrent.Futures;
+import com.google.common.util.concurrent.ListenableFuture;
 import io.netty.util.concurrent.Future;
+import java.net.InetSocketAddress;
 import java.util.ArrayList;
 import java.util.List;
 import org.opendaylight.controller.config.yang.bgp.rib.impl.BGPPeerRuntimeMXBean;
 import org.opendaylight.controller.config.yang.bgp.rib.impl.BgpPeerState;
 import org.opendaylight.controller.config.yang.bgp.rib.impl.BgpSessionState;
 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
+import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonService;
+import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
+import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
 import org.opendaylight.protocol.bgp.openconfig.spi.BGPOpenConfigMappingService;
 import org.opendaylight.protocol.bgp.parser.BgpExtendedMessageUtil;
 import org.opendaylight.protocol.bgp.parser.spi.MultiprotocolCapabilitiesUtil;
 import org.opendaylight.protocol.bgp.rib.impl.BGPPeer;
+import org.opendaylight.protocol.bgp.rib.impl.spi.BGPDispatcher;
 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPPeerRegistry;
 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionPreferences;
+import org.opendaylight.protocol.bgp.rib.impl.spi.BgpDeployer.WriteConfiguration;
 import org.opendaylight.protocol.bgp.rib.impl.spi.RIB;
 import org.opendaylight.protocol.concepts.KeyMapping;
 import org.opendaylight.protocol.util.Ipv4Util;
@@ -55,12 +63,9 @@ public class BgpPeer implements PeerBean, BGPPeerRuntimeMXBean {
 
     private final RpcProviderRegistry rpcRegistry;
     private final BGPPeerRegistry peerRegistry;
-    private BGPPeer bgpPeer;
-
-    private Future<Void> connection;
-
     private ServiceRegistration<?> serviceRegistration;
     private Neighbor currentConfiguration;
+    private BgpPeerSingletonService bgpPeerSingletonService;
 
     public BgpPeer(final RpcProviderRegistry rpcRegistry, final BGPPeerRegistry peerRegistry) {
         this.rpcRegistry = rpcRegistry;
@@ -68,49 +73,39 @@ 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.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(neighborAddress, this.bgpPeer, prefs);
-        if (OpenConfigMappingUtil.isActive(neighbor)) {
-            this.connection = rib.getDispatcher().createReconnectingClient(
-                    Ipv4Util.toInetSocketAddress(neighborAddress, OpenConfigMappingUtil.getPort(neighbor)), this.peerRegistry,
-                    OpenConfigMappingUtil.getRetryTimer(neighbor), Optional.fromNullable(key));
-        }
-
+    public void start(final RIB rib, final Neighbor neighbor, final BGPOpenConfigMappingService mappingService,
+        final WriteConfiguration configurationWriter) {
+        Preconditions.checkState(this.bgpPeerSingletonService == null, "Previous peer instance {} was not closed.");
+        this.bgpPeerSingletonService = new BgpPeerSingletonService(rib, neighbor, mappingService, configurationWriter);
+        this.currentConfiguration = neighbor;
     }
 
     @Override
     public void restart(final RIB rib, final BGPOpenConfigMappingService mappingService) {
         Preconditions.checkState(this.currentConfiguration != null);
-        start(rib, this.currentConfiguration, mappingService);
+        start(rib, this.currentConfiguration, mappingService, null);
     }
 
     @Override
     public void close() {
-        if (this.bgpPeer != null) {
-            if (this.connection != null) {
-                this.connection.cancel(true);
-                this.connection = null;
-            }
-            this.bgpPeer.close();
-            this.bgpPeer = null;
-            this.peerRegistry.removePeer(this.currentConfiguration.getNeighborAddress());
-            this.currentConfiguration = null;
-            if (this.serviceRegistration != null) {
-                this.serviceRegistration.unregister();
-                this.serviceRegistration = null;
-            }
+        try {
+            this.bgpPeerSingletonService.close();
+            this.bgpPeerSingletonService = null;
+        } catch (final Exception e) {
+            LOG.warn("Failed to close peer instance", e);
+        }
+        this.currentConfiguration = null;
+        if (this.serviceRegistration != null) {
+            this.serviceRegistration.unregister();
+            this.serviceRegistration = null;
         }
     }
 
+    @Override
+    public Boolean containsEqualConfiguration(final Neighbor neighbor) {
+        return this.currentConfiguration.equals(neighbor);
+    }
+
     private static List<BgpParameters> getBgpParameters(final Neighbor neighbor, final RIB rib,
             final BGPOpenConfigMappingService mappingService) {
         final List<BgpParameters> tlvs = new ArrayList<>();
@@ -152,28 +147,103 @@ public class BgpPeer implements PeerBean, BGPPeerRuntimeMXBean {
 
     @Override
     public BgpPeerState getBgpPeerState() {
-        return this.bgpPeer.getBgpPeerState();
+        return this.bgpPeerSingletonService.getPeer().getBgpPeerState();
     }
 
     @Override
     public BgpSessionState getBgpSessionState() {
-        return this.bgpPeer.getBgpSessionState();
+        return this.bgpPeerSingletonService.getPeer().getBgpSessionState();
     }
 
     @Override
-    public void resetStats() {
-        this.bgpPeer.resetStats();
-
+    public void resetSession() {
+        this.bgpPeerSingletonService.getPeer().resetSession();
     }
 
     @Override
-    public void resetSession() {
-        this.bgpPeer.resetSession();
-
+    public void resetStats() {
+        this.bgpPeerSingletonService.getPeer().resetStats();
     }
 
-    public void setServiceRegistration(final ServiceRegistration<?> serviceRegistration) {
+    void setServiceRegistration(final ServiceRegistration<?> serviceRegistration) {
         this.serviceRegistration = serviceRegistration;
     }
 
+    private final class BgpPeerSingletonService implements ClusterSingletonService, AutoCloseable {
+        private final ServiceGroupIdentifier serviceGroupIdentifier;
+        private final boolean activeConnection;
+        private final BGPDispatcher dispatcher;
+        private final InetSocketAddress inetAddress;
+        private final int retryTimer;
+        private final Optional<KeyMapping> key;
+        private final WriteConfiguration configurationWriter;
+        private ClusterSingletonServiceRegistration registration;
+        private final BGPPeer bgpPeer;
+        private final IpAddress neighborAddress;
+        private final BGPSessionPreferences prefs;
+        private Future<Void> connection;
+
+        private BgpPeerSingletonService(final RIB rib, final Neighbor neighbor, final BGPOpenConfigMappingService mappingService,
+            final WriteConfiguration configurationWriter) {
+            this.neighborAddress = neighbor.getNeighborAddress();
+            this.bgpPeer = new BGPPeer(Ipv4Util.toStringIP(this.neighborAddress), rib, mappingService.toPeerRole(neighbor), rpcRegistry);
+            final List<BgpParameters> bgpParameters = getBgpParameters(neighbor, rib, mappingService);
+            final KeyMapping key = OpenConfigMappingUtil.getNeighborKey(neighbor);
+            this.prefs = new BGPSessionPreferences(rib.getLocalAs(), getHoldTimer(neighbor), rib.getBgpIdentifier(), getPeerAs(neighbor, rib),
+                bgpParameters, getPassword(key));
+            this.activeConnection = OpenConfigMappingUtil.isActive(neighbor);
+            this.dispatcher = rib.getDispatcher();
+            this.inetAddress = Ipv4Util.toInetSocketAddress(this.neighborAddress, OpenConfigMappingUtil.getPort(neighbor));
+            this.retryTimer = OpenConfigMappingUtil.getRetryTimer(neighbor);
+            this.key = Optional.fromNullable(key);
+            this.serviceGroupIdentifier = rib.getRibIServiceGroupIdentifier();
+            LOG.info("Peer Singleton Service {} registered", this.serviceGroupIdentifier);
+            this.registration = rib.registerClusterSingletonService(this);
+            this.configurationWriter = configurationWriter;
+        }
+
+        @Override
+        public void close() throws Exception {
+            if (this.registration != null) {
+                this.registration.close();
+                this.registration = null;
+            }
+        }
+
+        @Override
+        public void instantiateServiceInstance() {
+            if(this.configurationWriter != null) {
+                this.configurationWriter.apply();
+            }
+            LOG.info("Peer Singleton Service {} instantiated", getIdentifier());
+            this.bgpPeer.instantiateServiceInstance();
+            peerRegistry.addPeer(this.neighborAddress, this.bgpPeer, prefs);
+            if (this.activeConnection) {
+                this.connection = this.dispatcher.createReconnectingClient(this.inetAddress, peerRegistry, this.retryTimer, this.key);
+            }
+        }
+
+        @Override
+        public ListenableFuture<Void> closeServiceInstance() {
+            LOG.info("Close RIB Singleton Service {}", this.getIdentifier());
+            if (this.connection != null) {
+                this.connection.cancel(true);
+                this.connection = null;
+            }
+            this.bgpPeer.close();
+            if(currentConfiguration != null) {
+                peerRegistry.removePeer(currentConfiguration.getNeighborAddress());
+            }
+            return Futures.immediateFuture(null);
+        }
+
+        @Override
+        public ServiceGroupIdentifier getIdentifier() {
+            return this.serviceGroupIdentifier;
+        }
+
+        BGPPeerRuntimeMXBean getPeer() {
+            return this.bgpPeer;
+        }
+    }
 }