a2f8b1c405efec641372c02e886734de79d64fd9
[bgpcep.git] / bgp / rib-impl / src / main / java / org / opendaylight / protocol / bgp / rib / impl / config / BGPClusterSingletonService.java
1 /*
2  * Copyright (c) 2017 AT&T Intellectual Property. 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.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;
15
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;
24 import java.util.Map;
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;
57
58 public final class BGPClusterSingletonService implements ClusterSingletonService, AutoCloseable {
59
60     private static final Logger LOG = LoggerFactory.getLogger(BGPClusterSingletonService.class);
61
62     private static final long TIMEOUT_NS = TimeUnit.SECONDS.toNanos(5);
63     private final InstanceIdentifier<Bgp> bgpIid;
64     @GuardedBy("this")
65     private final Map<InstanceIdentifier<Neighbor>, PeerBean> peers = new HashMap<>();
66     @GuardedBy("this")
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;
81
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;
97         this.bgpIid = bgpIid;
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);
111     }
112
113     @Override
114     public synchronized void instantiateServiceInstance() {
115         if (this.ribImpl != null) {
116             this.ribImpl.instantiateServiceInstance();
117             this.peers.values().forEach(PeerBean::instantiateServiceInstance);
118         }
119         this.instantiated.set(true);
120         LOG.info("BGPClusterSingletonService {} instantiated", this.serviceGroupIdentifier.getValue());
121     }
122
123     @Override
124     public synchronized ListenableFuture<? extends CommitInfo> closeServiceInstance() {
125         LOG.info("BGPClusterSingletonService {} close service instance", this.serviceGroupIdentifier.getValue());
126         this.instantiated.set(false);
127
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();
131
132         final ListenableFuture<List<CommitInfo>> futureResult = Futures.allAsList(futurePeerCloseList);
133         Futures.addCallback(futureResult, new FutureCallback<List<? extends CommitInfo>>() {
134             @Override
135             public void onSuccess(final List<? extends CommitInfo> result) {
136                 done.setFuture(Futures.transform(BGPClusterSingletonService.this.ribImpl.closeServiceInstance(),
137                     input -> null, MoreExecutors.directExecutor()));
138             }
139
140             @Override
141             public void onFailure(final Throwable throwable) {
142                 LOG.warn("Failed removing peers {}", throwable);
143             }
144         }, MoreExecutors.directExecutor());
145         return done;
146     }
147
148     @Nonnull
149     @Override
150     public ServiceGroupIdentifier getIdentifier() {
151         return this.serviceGroupIdentifier;
152     }
153
154     synchronized void onGlobalChanged(final DataObjectModification<Global> dataObjectModification) {
155         switch (dataObjectModification.getModificationType()) {
156             case DELETE:
157                 LOG.debug("Removing RIB instance: {}", this.bgpIid);
158                 if (this.ribImpl != null) {
159                     LOG.debug("RIB instance removed {}", this.ribImpl);
160                     closeAllBindedPeers();
161                     closeRibService();
162                     this.ribImpl = null;
163                 }
164                 break;
165             case SUBTREE_MODIFIED:
166             case WRITE:
167                 final Global global = dataObjectModification.getDataAfter();
168                 if (this.ribImpl == null) {
169                     onGlobalCreated(global);
170                 } else if (!this.ribImpl.isGlobalEqual(global)) {
171                     onGlobalUpdated(global);
172                 }
173                 break;
174             default:
175                 break;
176         }
177     }
178
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,
182                 schemaService);
183         initiateRibInstance(global, this.ribImpl);
184         LOG.debug("RIB instance created: {}", this.ribImpl);
185     }
186
187     private synchronized void onGlobalUpdated(final Global global) {
188         LOG.debug("Modifying RIB instance with configuration: {}", global);
189         final List<PeerBean> closedPeers = closeAllBindedPeers();
190         closeRibService();
191         initiateRibInstance(global, this.ribImpl);
192         for (final PeerBean peer : closedPeers) {
193             peer.restart(this.ribImpl, this.bgpIid, this.peerGroupLoader, this.tableTypeRegistry);
194         }
195         if (this.instantiated.get()) {
196             closedPeers.forEach(PeerBean::instantiateServiceInstance);
197         }
198         LOG.debug("RIB instance created: {}", this.ribImpl);
199     }
200
201     private void closeRibService() {
202         try {
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);
206         }
207         this.ribImpl.close();
208     }
209
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();
215         }
216     }
217
218     private synchronized List<PeerBean> closeAllBindedPeers() {
219         final List<PeerBean> filtered = new ArrayList<>();
220         this.peers.forEach((key, peer) -> {
221             try {
222                 peer.closeServiceInstance().get();
223             } catch (final Exception e) {
224                 LOG.error("Peer instance failed to close service instance", e);
225             }
226             peer.close();
227             filtered.add(peer);
228         });
229         return filtered;
230     }
231
232     @Override
233     public void close() {
234         LOG.info("BGPClusterSingletonService {} close", this.serviceGroupIdentifier.getValue());
235         this.peers.values().iterator().forEachRemaining(PeerBean::close);
236         this.ribImpl.close();
237         this.peers.clear();
238         this.ribImpl = null;
239     }
240
241     synchronized void onNeighborsChanged(final DataObjectModification<Neighbors> dataObjectModification) {
242         for (final DataObjectModification<? extends DataObject> neighborModification :
243                 dataObjectModification.getModifiedChildren()) {
244             switch (neighborModification.getModificationType()) {
245                 case DELETE:
246                     onNeighborRemoved((Neighbor) neighborModification.getDataBefore());
247                     break;
248                 case SUBTREE_MODIFIED:
249                 case WRITE:
250                     onNeighborModified((Neighbor) neighborModification.getDataAfter());
251                     break;
252                 default:
253                     break;
254             }
255         }
256     }
257
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);
265         }
266     }
267
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();
273         } else {
274             bgpPeer = new BgpPeer(this.rpcRegistry);
275         }
276         final InstanceIdentifier<Neighbor> neighborInstanceIdentifier =
277                 getNeighborInstanceIdentifier(this.bgpIid, neighbor.key());
278         initiatePeerInstance(neighborInstanceIdentifier, neighbor, bgpPeer);
279         this.peers.put(neighborInstanceIdentifier, bgpPeer);
280
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());
284     }
285
286     private static Optional<String> getPeerGroupName(final Config config) {
287         if (config == null) {
288             return Optional.empty();
289         }
290         final NeighborPeerGroupConfig aug = config.augmentation(NeighborPeerGroupConfig.class);
291         if (aug == null || aug.getPeerGroup() == null) {
292             return Optional.empty();
293         }
294         final String peerGroupName = aug.getPeerGroup();
295         if (peerGroupName.equals(APPLICATION_PEER_GROUP_NAME)) {
296             return APPLICATION_PEER_GROUP_NAME_OPT;
297         }
298         return Optional.of(StringUtils.substringBetween(peerGroupName, "=\"", "\""));
299     }
300
301     private synchronized void onNeighborUpdated(final PeerBean bgpPeer, final Neighbor neighbor) {
302         LOG.debug("Updating Peer instance with configuration: {}", neighbor);
303         closePeer(bgpPeer);
304
305         final InstanceIdentifier<Neighbor> neighborInstanceIdentifier
306                 = getNeighborInstanceIdentifier(this.bgpIid, neighbor.key());
307         initiatePeerInstance(neighborInstanceIdentifier, neighbor, bgpPeer);
308         LOG.debug("Peer instance updated {}", bgpPeer);
309     }
310
311     private static void closePeer(final PeerBean bgpPeer) {
312         if (bgpPeer != null) {
313             try {
314                 bgpPeer.closeServiceInstance().get();
315                 bgpPeer.close();
316                 LOG.debug("Peer instance closed {}", bgpPeer);
317             } catch (final Exception e) {
318                 LOG.error("Peer instance failed to close service instance", e);
319             }
320         }
321     }
322
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()));
326
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;
331         }));
332         closePeer(bgpPeer);
333     }
334
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);
339         }
340         if (this.instantiated.get()) {
341             bgpPeer.instantiateServiceInstance();
342         }
343     }
344
345     synchronized void restartNeighbors(final String peerGroupName) {
346         final List<PeerBean> peerGroup = this.peersGroups.get(peerGroupName);
347         if (peerGroup == null) {
348             return;
349         }
350         for (final PeerBean peer : peerGroup) {
351             try {
352                 peer.closeServiceInstance().get();
353             } catch (final Exception e) {
354                 LOG.error("Peer instance failed to close service instance", e);
355             }
356             peer.restart(this.ribImpl, this.bgpIid, this.peerGroupLoader, this.tableTypeRegistry);
357             peer.instantiateServiceInstance();
358         }
359     }
360 }