BUG-6437: Fix race condition when closing singleton group I
[bgpcep.git] / bgp / rib-impl / src / main / java / org / opendaylight / protocol / bgp / rib / impl / config / BgpPeer.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.protocol.bgp.rib.impl.config;
10
11 import static org.opendaylight.protocol.bgp.rib.impl.config.OpenConfigMappingUtil.getHoldTimer;
12 import static org.opendaylight.protocol.bgp.rib.impl.config.OpenConfigMappingUtil.getPeerAs;
13
14 import com.google.common.base.Optional;
15 import com.google.common.base.Preconditions;
16 import com.google.common.collect.Iterables;
17 import com.google.common.util.concurrent.Futures;
18 import com.google.common.util.concurrent.ListenableFuture;
19 import io.netty.util.concurrent.Future;
20 import java.net.InetSocketAddress;
21 import java.util.ArrayList;
22 import java.util.List;
23 import java.util.Objects;
24 import org.opendaylight.controller.config.yang.bgp.rib.impl.BGPPeerRuntimeMXBean;
25 import org.opendaylight.controller.config.yang.bgp.rib.impl.BgpPeerState;
26 import org.opendaylight.controller.config.yang.bgp.rib.impl.BgpSessionState;
27 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
28 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonService;
29 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
30 import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
31 import org.opendaylight.protocol.bgp.openconfig.spi.BGPOpenConfigMappingService;
32 import org.opendaylight.protocol.bgp.parser.BgpExtendedMessageUtil;
33 import org.opendaylight.protocol.bgp.parser.spi.MultiprotocolCapabilitiesUtil;
34 import org.opendaylight.protocol.bgp.rib.impl.BGPPeer;
35 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPDispatcher;
36 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPPeerRegistry;
37 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionPreferences;
38 import org.opendaylight.protocol.bgp.rib.impl.spi.BgpDeployer.WriteConfiguration;
39 import org.opendaylight.protocol.bgp.rib.impl.spi.RIB;
40 import org.opendaylight.protocol.concepts.KeyMapping;
41 import org.opendaylight.protocol.util.Ipv4Util;
42 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.multiprotocol.rev151009.bgp.common.afi.safi.list.AfiSafi;
43 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.neighbor.group.AfiSafis;
44 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.neighbor.group.Config;
45 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.neighbors.Neighbor;
46 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.BgpParameters;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.BgpParametersBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.bgp.parameters.OptionalCapabilities;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.bgp.parameters.OptionalCapabilitiesBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.bgp.parameters.optional.capabilities.CParametersBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.bgp.parameters.optional.capabilities.c.parameters.As4BytesCapabilityBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.BgpTableType;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.CParameters1;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.CParameters1Builder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.mp.capabilities.AddPathCapabilityBuilder;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.mp.capabilities.MultiprotocolCapabilityBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.mp.capabilities.add.path.capability.AddressFamilies;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.openconfig.extensions.rev160614.AfiSafi2;
60 import org.osgi.framework.ServiceRegistration;
61 import org.slf4j.Logger;
62 import org.slf4j.LoggerFactory;
63
64
65 public class BgpPeer implements PeerBean, BGPPeerRuntimeMXBean {
66
67     private static final Logger LOG = LoggerFactory.getLogger(BgpPeer.class);
68
69     private final RpcProviderRegistry rpcRegistry;
70     private final BGPPeerRegistry peerRegistry;
71     private ServiceRegistration<?> serviceRegistration;
72     private Neighbor currentConfiguration;
73     private BgpPeerSingletonService bgpPeerSingletonService;
74
75     public BgpPeer(final RpcProviderRegistry rpcRegistry, final BGPPeerRegistry peerRegistry) {
76         this.rpcRegistry = rpcRegistry;
77         this.peerRegistry = peerRegistry;
78     }
79
80     @Override
81     public void start(final RIB rib, final Neighbor neighbor, final BGPOpenConfigMappingService mappingService,
82         final WriteConfiguration configurationWriter) {
83         Preconditions.checkState(this.bgpPeerSingletonService == null, "Previous peer instance {} was not closed.");
84         this.bgpPeerSingletonService = new BgpPeerSingletonService(rib, neighbor, mappingService, configurationWriter);
85         this.currentConfiguration = neighbor;
86     }
87
88     @Override
89     public void restart(final RIB rib, final BGPOpenConfigMappingService mappingService) {
90         Preconditions.checkState(this.currentConfiguration != null);
91         start(rib, this.currentConfiguration, mappingService, null);
92     }
93
94     @Override
95     public void close() {
96         try {
97             this.bgpPeerSingletonService.close();
98             this.bgpPeerSingletonService = null;
99         } catch (final Exception e) {
100             LOG.warn("Failed to close peer instance", e);
101         }
102         this.currentConfiguration = null;
103         if (this.serviceRegistration != null) {
104             this.serviceRegistration.unregister();
105             this.serviceRegistration = null;
106         }
107     }
108
109     @Override
110     public Boolean containsEqualConfiguration(final Neighbor neighbor) {
111         final AfiSafis actAfiSafi = this.currentConfiguration.getAfiSafis();
112         final AfiSafis extAfiSafi = neighbor.getAfiSafis();
113         final List<AfiSafi> actualSafi = actAfiSafi.getAfiSafi();
114         final List<AfiSafi> extSafi = extAfiSafi.getAfiSafi();
115         return actualSafi.containsAll(extSafi) && extSafi.containsAll(actualSafi)
116         && Objects.equals(this.currentConfiguration.getConfig(), neighbor.getConfig())
117         && Objects.equals(this.currentConfiguration.getNeighborAddress(), neighbor.getNeighborAddress())
118         && Objects.equals(this.currentConfiguration.getAddPaths(),neighbor.getAddPaths())
119         && Objects.equals(this.currentConfiguration.getApplyPolicy(), neighbor.getApplyPolicy())
120         && Objects.equals(this.currentConfiguration.getAsPathOptions(), neighbor.getAsPathOptions())
121         && Objects.equals(this.currentConfiguration.getEbgpMultihop(), neighbor.getEbgpMultihop())
122         && Objects.equals(this.currentConfiguration.getGracefulRestart(), neighbor.getGracefulRestart())
123         && Objects.equals(this.currentConfiguration.getErrorHandling(), neighbor.getErrorHandling())
124         && Objects.equals(this.currentConfiguration.getLoggingOptions(), neighbor.getLoggingOptions())
125         && Objects.equals(this.currentConfiguration.getRouteReflector(), neighbor.getRouteReflector())
126         && Objects.equals(this.currentConfiguration.getState(), neighbor.getState())
127         && Objects.equals(this.currentConfiguration.getTimers(), neighbor.getTimers())
128         && Objects.equals(this.currentConfiguration.getTransport(), neighbor.getTransport());
129     }
130
131     private static List<BgpParameters> getBgpParameters(final Neighbor neighbor, final RIB rib,
132             final BGPOpenConfigMappingService mappingService) {
133         final List<BgpParameters> tlvs = new ArrayList<>();
134         final List<OptionalCapabilities> caps = new ArrayList<>();
135         caps.add(new OptionalCapabilitiesBuilder().setCParameters(new CParametersBuilder().setAs4BytesCapability(
136                 new As4BytesCapabilityBuilder().setAsNumber(rib.getLocalAs()).build()).build()).build());
137
138         caps.add(new OptionalCapabilitiesBuilder().setCParameters(BgpExtendedMessageUtil.EXTENDED_MESSAGE_CAPABILITY).build());
139         caps.add(new OptionalCapabilitiesBuilder().setCParameters(MultiprotocolCapabilitiesUtil.RR_CAPABILITY).build());
140
141         final List<AddressFamilies> addPathCapability = mappingService.toAddPathCapability(neighbor.getAfiSafis().getAfiSafi());
142         if (!addPathCapability.isEmpty()) {
143             caps.add(new OptionalCapabilitiesBuilder().setCParameters(new CParametersBuilder().addAugmentation(CParameters1.class,
144                     new CParameters1Builder().setAddPathCapability(
145                             new AddPathCapabilityBuilder().setAddressFamilies(addPathCapability).build()).build()).build()).build());
146         }
147
148         final List<BgpTableType> tableTypes = mappingService.toTableTypes(neighbor.getAfiSafis().getAfiSafi());
149         for (final BgpTableType tableType : tableTypes) {
150             if (!rib.getLocalTables().contains(tableType)) {
151                 LOG.info("RIB instance does not list {} in its local tables. Incoming data will be dropped.", tableType);
152             }
153
154             caps.add(new OptionalCapabilitiesBuilder().setCParameters(
155                     new CParametersBuilder().addAugmentation(CParameters1.class,
156                             new CParameters1Builder().setMultiprotocolCapability(
157                                     new MultiprotocolCapabilityBuilder(tableType).build()).build()).build()).build());
158         }
159         tlvs.add(new BgpParametersBuilder().setOptionalCapabilities(caps).build());
160         return tlvs;
161     }
162
163     private static Optional<byte[]> getPassword(final KeyMapping key) {
164         if (key != null) {
165             return Optional.of(Iterables.getOnlyElement(key.values()));
166         }
167         return Optional.absent();
168     }
169
170     @Override
171     public BgpPeerState getBgpPeerState() {
172         return this.bgpPeerSingletonService.getPeer().getBgpPeerState();
173     }
174
175     @Override
176     public BgpSessionState getBgpSessionState() {
177         return this.bgpPeerSingletonService.getPeer().getBgpSessionState();
178     }
179
180     @Override
181     public void resetSession() {
182         this.bgpPeerSingletonService.getPeer().resetSession();
183     }
184
185     @Override
186     public void resetStats() {
187         this.bgpPeerSingletonService.getPeer().resetStats();
188     }
189
190     void setServiceRegistration(final ServiceRegistration<?> serviceRegistration) {
191         this.serviceRegistration = serviceRegistration;
192     }
193
194     private final class BgpPeerSingletonService implements ClusterSingletonService, AutoCloseable {
195         private final ServiceGroupIdentifier serviceGroupIdentifier;
196         private final boolean activeConnection;
197         private final BGPDispatcher dispatcher;
198         private final InetSocketAddress inetAddress;
199         private final int retryTimer;
200         private final Optional<KeyMapping> key;
201         private final WriteConfiguration configurationWriter;
202         private ClusterSingletonServiceRegistration registration;
203         private final BGPPeer bgpPeer;
204         private final IpAddress neighborAddress;
205         private final BGPSessionPreferences prefs;
206         private Future<Void> connection;
207
208         private BgpPeerSingletonService(final RIB rib, final Neighbor neighbor, final BGPOpenConfigMappingService mappingService,
209             final WriteConfiguration configurationWriter) {
210             this.neighborAddress = neighbor.getNeighborAddress();
211             this.bgpPeer = new BGPPeer(Ipv4Util.toStringIP(this.neighborAddress), rib, mappingService.toPeerRole(neighbor), rpcRegistry);
212             final List<BgpParameters> bgpParameters = getBgpParameters(neighbor, rib, mappingService);
213             final KeyMapping key = OpenConfigMappingUtil.getNeighborKey(neighbor);
214             this.prefs = new BGPSessionPreferences(rib.getLocalAs(), getHoldTimer(neighbor), rib.getBgpIdentifier(), getPeerAs(neighbor, rib),
215                 bgpParameters, getPassword(key));
216             this.activeConnection = OpenConfigMappingUtil.isActive(neighbor);
217             this.dispatcher = rib.getDispatcher();
218             this.inetAddress = Ipv4Util.toInetSocketAddress(this.neighborAddress, OpenConfigMappingUtil.getPort(neighbor));
219             this.retryTimer = OpenConfigMappingUtil.getRetryTimer(neighbor);
220             this.key = Optional.fromNullable(key);
221             this.configurationWriter = configurationWriter;
222             this.serviceGroupIdentifier = rib.getRibIServiceGroupIdentifier();
223             LOG.info("Peer Singleton Service {} registered", this.serviceGroupIdentifier);
224             //this need to be always the last step
225             this.registration = rib.registerClusterSingletonService(this);
226         }
227
228         @Override
229         public void close() throws Exception {
230             if (this.registration != null) {
231                 this.registration.close();
232                 this.registration = null;
233             }
234         }
235
236         @Override
237         public void instantiateServiceInstance() {
238             if(this.configurationWriter != null) {
239                 this.configurationWriter.apply();
240             }
241             LOG.info("Peer Singleton Service {} instantiated", getIdentifier());
242             this.bgpPeer.instantiateServiceInstance();
243             peerRegistry.addPeer(this.neighborAddress, this.bgpPeer, prefs);
244             if (this.activeConnection) {
245                 this.connection = this.dispatcher.createReconnectingClient(this.inetAddress, peerRegistry, this.retryTimer, this.key);
246             }
247         }
248
249         @Override
250         public ListenableFuture<Void> closeServiceInstance() {
251             LOG.info("Close Peer Singleton Service {}", this.getIdentifier());
252             if (this.connection != null) {
253                 this.connection.cancel(true);
254                 this.connection = null;
255             }
256             this.bgpPeer.close();
257             if(currentConfiguration != null) {
258                 peerRegistry.removePeer(currentConfiguration.getNeighborAddress());
259             }
260             return Futures.immediateFuture(null);
261         }
262
263         @Override
264         public ServiceGroupIdentifier getIdentifier() {
265             return this.serviceGroupIdentifier;
266         }
267
268         BGPPeerRuntimeMXBean getPeer() {
269             return this.bgpPeer;
270         }
271     }
272 }