+ 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;
+ }
+ }