2 * Copyright (c) 2017 AT&T Intellectual Property. All rights reserved.
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
9 package org.opendaylight.protocol.bgp.rib.impl.config;
11 import static org.opendaylight.protocol.bgp.rib.impl.config.OpenConfigMappingUtil.APPLICATION_PEER_GROUP_NAME;
12 import static org.opendaylight.protocol.bgp.rib.impl.config.OpenConfigMappingUtil.APPLICATION_PEER_GROUP_NAME_OPT;
13 import static org.opendaylight.protocol.bgp.rib.impl.config.OpenConfigMappingUtil.getNeighborInstanceIdentifier;
14 import static org.opendaylight.protocol.bgp.rib.impl.config.OpenConfigMappingUtil.getRibInstanceName;
16 import com.google.common.util.concurrent.FutureCallback;
17 import com.google.common.util.concurrent.Futures;
18 import com.google.common.util.concurrent.ListenableFuture;
19 import com.google.common.util.concurrent.MoreExecutors;
20 import com.google.common.util.concurrent.SettableFuture;
21 import java.util.ArrayList;
22 import java.util.HashMap;
23 import java.util.List;
25 import java.util.Optional;
26 import java.util.concurrent.TimeUnit;
27 import java.util.concurrent.atomic.AtomicBoolean;
28 import java.util.stream.Collectors;
29 import javax.annotation.Nonnull;
30 import javax.annotation.concurrent.GuardedBy;
31 import org.apache.commons.lang3.StringUtils;
32 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
33 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
34 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
35 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
36 import org.opendaylight.mdsal.binding.dom.codec.api.BindingCodecTreeFactory;
37 import org.opendaylight.mdsal.common.api.CommitInfo;
38 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
39 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonService;
40 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
41 import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
42 import org.opendaylight.protocol.bgp.openconfig.routing.policy.spi.BGPRibRoutingPolicyFactory;
43 import org.opendaylight.protocol.bgp.openconfig.spi.BGPTableTypeRegistryConsumer;
44 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPDispatcher;
45 import org.opendaylight.protocol.bgp.rib.spi.RIBExtensionConsumerContext;
46 import org.opendaylight.protocol.bgp.rib.spi.util.ClusterSingletonServiceRegistrationHelper;
47 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.neighbor.group.Config;
48 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.neighbors.Neighbor;
49 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.top.Bgp;
50 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.top.bgp.Global;
51 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.top.bgp.Neighbors;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.openconfig.extensions.rev180329.NeighborPeerGroupConfig;
53 import org.opendaylight.yangtools.yang.binding.DataObject;
54 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
55 import org.slf4j.Logger;
56 import org.slf4j.LoggerFactory;
58 public final class BGPClusterSingletonService implements ClusterSingletonService, AutoCloseable {
60 private static final Logger LOG = LoggerFactory.getLogger(BGPClusterSingletonService.class);
62 private static final long TIMEOUT_NS = TimeUnit.SECONDS.toNanos(5);
63 private final InstanceIdentifier<Bgp> bgpIid;
65 private final Map<InstanceIdentifier<Neighbor>, PeerBean> peers = new HashMap<>();
67 private final Map<String, List<PeerBean>> peersGroups = new HashMap<>();
68 private final BGPTableTypeRegistryConsumer tableTypeRegistry;
69 private final ServiceGroupIdentifier serviceGroupIdentifier;
70 private final AtomicBoolean instantiated = new AtomicBoolean(false);
71 private final PeerGroupConfigLoader peerGroupLoader;
72 private RibImpl ribImpl;
73 private final RIBExtensionConsumerContext ribExtensionContext;
74 private final BGPDispatcher dispatcher;
75 private final BGPRibRoutingPolicyFactory policyFactory;
76 private final BindingCodecTreeFactory codecFactory;
77 private final DOMDataBroker domBroker;
78 private final DataBroker dataBroker;
79 private final DOMSchemaService schemaService;
80 private final RpcProviderRegistry rpcRegistry;
82 BGPClusterSingletonService(
83 @Nonnull final PeerGroupConfigLoader peerGroupLoader,
84 @Nonnull final ClusterSingletonServiceProvider provider,
85 @Nonnull final BGPTableTypeRegistryConsumer tableTypeRegistry,
86 @Nonnull final InstanceIdentifier<Bgp> bgpIid,
87 @Nonnull final RIBExtensionConsumerContext ribExtensionContext,
88 @Nonnull final BGPDispatcher dispatcher,
89 @Nonnull final BGPRibRoutingPolicyFactory policyFactory,
90 @Nonnull final BindingCodecTreeFactory codecFactory,
91 @Nonnull final DOMDataBroker domBroker,
92 @Nonnull final DataBroker dataBroker,
93 @Nonnull final DOMSchemaService schemaService,
94 @Nonnull final RpcProviderRegistry rpcRegistry) {
95 this.peerGroupLoader = peerGroupLoader;
96 this.tableTypeRegistry = tableTypeRegistry;
98 this.ribExtensionContext = ribExtensionContext;
99 this.dispatcher = dispatcher;
100 this.policyFactory = policyFactory;
101 this.codecFactory = codecFactory;
102 this.domBroker = domBroker;
103 this.dataBroker = dataBroker;
104 this.schemaService = schemaService;
105 this.rpcRegistry = rpcRegistry;
106 final String ribInstanceName = getRibInstanceName(bgpIid);
107 this.serviceGroupIdentifier = ServiceGroupIdentifier.create(ribInstanceName + "-service-group");
108 LOG.info("BGPClusterSingletonService {} registered", this.serviceGroupIdentifier.getValue());
109 ClusterSingletonServiceRegistrationHelper
110 .registerSingletonService(provider, this);
114 public synchronized void instantiateServiceInstance() {
115 if (this.ribImpl != null) {
116 this.ribImpl.instantiateServiceInstance();
117 this.peers.values().forEach(PeerBean::instantiateServiceInstance);
119 this.instantiated.set(true);
120 LOG.info("BGPClusterSingletonService {} instantiated", this.serviceGroupIdentifier.getValue());
124 public synchronized ListenableFuture<? extends CommitInfo> closeServiceInstance() {
125 LOG.info("BGPClusterSingletonService {} close service instance", this.serviceGroupIdentifier.getValue());
126 this.instantiated.set(false);
128 final List<ListenableFuture<? extends CommitInfo>> futurePeerCloseList = this.peers.values().stream()
129 .map(PeerBean::closeServiceInstance).collect(Collectors.toList());
130 final SettableFuture<? extends CommitInfo> done = SettableFuture.create();
132 final ListenableFuture<List<CommitInfo>> futureResult = Futures.allAsList(futurePeerCloseList);
133 Futures.addCallback(futureResult, new FutureCallback<List<? extends CommitInfo>>() {
135 public void onSuccess(final List<? extends CommitInfo> result) {
136 done.setFuture(Futures.transform(BGPClusterSingletonService.this.ribImpl.closeServiceInstance(),
137 input -> null, MoreExecutors.directExecutor()));
141 public void onFailure(final Throwable throwable) {
142 LOG.warn("Failed removing peers {}", throwable);
144 }, MoreExecutors.directExecutor());
150 public ServiceGroupIdentifier getIdentifier() {
151 return this.serviceGroupIdentifier;
154 synchronized void onGlobalChanged(final DataObjectModification<Global> dataObjectModification) {
155 switch (dataObjectModification.getModificationType()) {
157 LOG.debug("Removing RIB instance: {}", this.bgpIid);
158 if (this.ribImpl != null) {
159 LOG.debug("RIB instance removed {}", this.ribImpl);
160 closeAllBindedPeers();
165 case SUBTREE_MODIFIED:
167 final Global global = dataObjectModification.getDataAfter();
168 if (this.ribImpl == null) {
169 onGlobalCreated(global);
170 } else if (!this.ribImpl.isGlobalEqual(global)) {
171 onGlobalUpdated(global);
179 private synchronized void onGlobalCreated(final Global global) {
180 LOG.debug("Creating RIB instance with configuration: {}", global);
181 this.ribImpl = new RibImpl(ribExtensionContext, dispatcher, policyFactory, codecFactory, domBroker, dataBroker,
183 initiateRibInstance(global, this.ribImpl);
184 LOG.debug("RIB instance created: {}", this.ribImpl);
187 private synchronized void onGlobalUpdated(final Global global) {
188 LOG.debug("Modifying RIB instance with configuration: {}", global);
189 final List<PeerBean> closedPeers = closeAllBindedPeers();
191 initiateRibInstance(global, this.ribImpl);
192 for (final PeerBean peer : closedPeers) {
193 peer.restart(this.ribImpl, this.bgpIid, this.peerGroupLoader, this.tableTypeRegistry);
195 if (this.instantiated.get()) {
196 closedPeers.forEach(PeerBean::instantiateServiceInstance);
198 LOG.debug("RIB instance created: {}", this.ribImpl);
201 private void closeRibService() {
203 this.ribImpl.closeServiceInstance().get(TIMEOUT_NS, TimeUnit.NANOSECONDS);
204 } catch (final Exception e) {
205 LOG.error("RIB instance failed to close service instance", e);
207 this.ribImpl.close();
210 private synchronized void initiateRibInstance(final Global global, final RibImpl ribImpl) {
211 final String ribInstanceName = getRibInstanceName(this.bgpIid);
212 ribImpl.start(global, ribInstanceName, this.tableTypeRegistry);
213 if (this.instantiated.get()) {
214 this.ribImpl.instantiateServiceInstance();
218 private synchronized List<PeerBean> closeAllBindedPeers() {
219 final List<PeerBean> filtered = new ArrayList<>();
220 this.peers.forEach((key, peer) -> {
222 peer.closeServiceInstance().get();
223 } catch (final Exception e) {
224 LOG.error("Peer instance failed to close service instance", e);
233 public void close() {
234 LOG.info("BGPClusterSingletonService {} close", this.serviceGroupIdentifier.getValue());
235 this.peers.values().iterator().forEachRemaining(PeerBean::close);
236 this.ribImpl.close();
241 synchronized void onNeighborsChanged(final DataObjectModification<Neighbors> dataObjectModification) {
242 for (final DataObjectModification<? extends DataObject> neighborModification :
243 dataObjectModification.getModifiedChildren()) {
244 switch (neighborModification.getModificationType()) {
246 onNeighborRemoved((Neighbor) neighborModification.getDataBefore());
248 case SUBTREE_MODIFIED:
250 onNeighborModified((Neighbor) neighborModification.getDataAfter());
258 private synchronized void onNeighborModified(final Neighbor neighbor) {
259 //restart peer instance with a new configuration
260 final PeerBean bgpPeer = this.peers.get(getNeighborInstanceIdentifier(this.bgpIid, neighbor.key()));
261 if (bgpPeer == null) {
262 onNeighborCreated(neighbor);
263 } else if (!bgpPeer.containsEqualConfiguration(neighbor)) {
264 onNeighborUpdated(bgpPeer, neighbor);
268 private synchronized void onNeighborCreated(final Neighbor neighbor) {
269 LOG.debug("Creating Peer instance with configuration: {}", neighbor);
270 final PeerBean bgpPeer;
271 if (OpenConfigMappingUtil.isApplicationPeer(neighbor)) {
272 bgpPeer = new AppPeer();
274 bgpPeer = new BgpPeer(this.rpcRegistry);
276 final InstanceIdentifier<Neighbor> neighborInstanceIdentifier =
277 getNeighborInstanceIdentifier(this.bgpIid, neighbor.key());
278 initiatePeerInstance(neighborInstanceIdentifier, neighbor, bgpPeer);
279 this.peers.put(neighborInstanceIdentifier, bgpPeer);
281 final Optional<String> peerGroupName = getPeerGroupName(neighbor.getConfig());
282 peerGroupName.ifPresent(s -> this.peersGroups.computeIfAbsent(s, k -> new ArrayList<>()).add(bgpPeer));
283 LOG.debug("Peer instance created {}", neighbor.key().getNeighborAddress());
286 private static Optional<String> getPeerGroupName(final Config config) {
287 if (config == null) {
288 return Optional.empty();
290 final NeighborPeerGroupConfig aug = config.augmentation(NeighborPeerGroupConfig.class);
291 if (aug == null || aug.getPeerGroup() == null) {
292 return Optional.empty();
294 final String peerGroupName = aug.getPeerGroup();
295 if (peerGroupName.equals(APPLICATION_PEER_GROUP_NAME)) {
296 return APPLICATION_PEER_GROUP_NAME_OPT;
298 return Optional.of(StringUtils.substringBetween(peerGroupName, "=\"", "\""));
301 private synchronized void onNeighborUpdated(final PeerBean bgpPeer, final Neighbor neighbor) {
302 LOG.debug("Updating Peer instance with configuration: {}", neighbor);
305 final InstanceIdentifier<Neighbor> neighborInstanceIdentifier
306 = getNeighborInstanceIdentifier(this.bgpIid, neighbor.key());
307 initiatePeerInstance(neighborInstanceIdentifier, neighbor, bgpPeer);
308 LOG.debug("Peer instance updated {}", bgpPeer);
311 private static void closePeer(final PeerBean bgpPeer) {
312 if (bgpPeer != null) {
314 bgpPeer.closeServiceInstance().get();
316 LOG.debug("Peer instance closed {}", bgpPeer);
317 } catch (final Exception e) {
318 LOG.error("Peer instance failed to close service instance", e);
323 private synchronized void onNeighborRemoved(final Neighbor neighbor) {
324 LOG.debug("Removing Peer instance: {}", neighbor);
325 final PeerBean bgpPeer = this.peers.remove(getNeighborInstanceIdentifier(this.bgpIid, neighbor.key()));
327 final Optional<String> groupName = getPeerGroupName(neighbor.getConfig());
328 groupName.ifPresent(s -> this.peersGroups.computeIfPresent(s, (k, peers) -> {
329 peers.remove(bgpPeer);
330 return peers.isEmpty() ? null : peers;
335 private synchronized void initiatePeerInstance(final InstanceIdentifier<Neighbor> neighborIdentifier,
336 final Neighbor neighbor, final PeerBean bgpPeer) {
337 if (this.ribImpl != null) {
338 bgpPeer.start(this.ribImpl, neighbor, this.bgpIid, this.peerGroupLoader, this.tableTypeRegistry);
340 if (this.instantiated.get()) {
341 bgpPeer.instantiateServiceInstance();
345 synchronized void restartNeighbors(final String peerGroupName) {
346 final List<PeerBean> peerGroup = this.peersGroups.get(peerGroupName);
347 if (peerGroup == null) {
350 for (final PeerBean peer : peerGroup) {
352 peer.closeServiceInstance().get();
353 } catch (final Exception e) {
354 LOG.error("Peer instance failed to close service instance", e);
356 peer.restart(this.ribImpl, this.bgpIid, this.peerGroupLoader, this.tableTypeRegistry);
357 peer.instantiateServiceInstance();