import com.google.common.base.MoreObjects.ToStringHelper;
import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
+import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
+import com.google.common.util.concurrent.CheckedFuture;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
-import java.util.ArrayList;
+import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
+import javax.annotation.concurrent.GuardedBy;
import javax.annotation.concurrent.ThreadSafe;
-import org.opendaylight.controller.config.yang.bgp.rib.impl.RIBImplRuntimeRegistration;
-import org.opendaylight.controller.config.yang.bgp.rib.impl.RIBImplRuntimeRegistrator;
import org.opendaylight.controller.md.sal.common.api.data.AsyncTransaction;
import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
import org.opendaylight.controller.md.sal.common.api.data.TransactionChain;
import org.opendaylight.protocol.bgp.rib.impl.spi.RIBSupportContextRegistry;
import org.opendaylight.protocol.bgp.rib.impl.stats.rib.impl.BGPRenderStats;
import org.opendaylight.protocol.bgp.rib.impl.stats.rib.impl.RIBImplRuntimeMXBeanImpl;
-import org.opendaylight.protocol.bgp.rib.spi.CacheDisconnectedPeers;
+import org.opendaylight.protocol.bgp.rib.spi.ExportPolicyPeerTracker;
import org.opendaylight.protocol.bgp.rib.spi.RIBExtensionConsumerContext;
import org.opendaylight.protocol.bgp.rib.spi.RibSupportUtils;
+import org.opendaylight.protocol.bgp.rib.spi.util.ClusterSingletonServiceRegistrationHelper;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.BgpTableType;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.BgpRib;
private static final Logger LOG = LoggerFactory.getLogger(RIBImpl.class);
private static final QName RIB_ID_QNAME = QName.create(Rib.QNAME, "id").intern();
private static final ContainerNode EMPTY_TABLE_ATTRIBUTES = ImmutableNodes.containerNode(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.tables.Attributes.QNAME);
+ private static final int MAX_REGISTRATION_ATTEMPTS = 10;
+ private static final int SLEEP_TIME = MAX_REGISTRATION_ATTEMPTS;
private final BGPDispatcher dispatcher;
- private final DOMTransactionChain domChain;
private final AsNumber localAs;
private final BgpId bgpIdentifier;
private final Set<BgpTableType> localTables;
private final CodecsRegistryImpl codecsRegistry;
private final ServiceGroupIdentifier serviceGroupIdentifier;
private final ClusterSingletonServiceProvider provider;
- private final PolicyDatabase policyDatabase;
private final BgpDeployer.WriteConfiguration configurationWriter;
private ClusterSingletonServiceRegistration registration;
private final DOMDataBrokerExtension service;
- private final List<LocRibWriter> locRibs = new ArrayList<>();
+ private final Map<TransactionChain, LocRibWriter> txChainToLocRibWriter = new HashMap<>();
private final BGPConfigModuleTracker configModuleTracker;
private final BGPOpenConfigProvider openConfigProvider;
- private final CacheDisconnectedPeers cacheDisconnectedPeers;
private final Map<TablesKey, PathSelectionMode> bestPathSelectionStrategies;
private final ImportPolicyPeerTracker importPolicyPeerTracker;
private final RIBImplRuntimeMXBeanImpl renderStats;
private final RibId ribId;
+ private final Map<TablesKey, ExportPolicyPeerTracker> exportPolicyPeerTrackerMap;
+
+ private DOMTransactionChain domChain;
+ @GuardedBy("this")
+ private boolean isServiceInstantiated;
public RIBImpl(final ClusterSingletonServiceProvider provider, final RibId ribId, final AsNumber localAs, final BgpId localBgpId,
final ClusterIdentifier clusterId, final RIBExtensionConsumerContext extensions, final BGPDispatcher dispatcher,
final BindingCodecTreeFactory codecFactory, final DOMDataBroker domDataBroker, final List<BgpTableType> localTables,
@Nonnull final Map<TablesKey, PathSelectionMode> bestPathSelectionStrategies, final GeneratedClassLoadingStrategy classStrategy,
- final BGPConfigModuleTracker moduleTracker, final BGPOpenConfigProvider openConfigProvider, final BgpDeployer.WriteConfiguration configurationWriter) {
+ final BGPConfigModuleTracker moduleTracker, final BGPOpenConfigProvider openConfigProvider,
+ final BgpDeployer.WriteConfiguration configurationWriter) {
super(InstanceIdentifier.create(BgpRib.class).child(Rib.class, new RibKey(Preconditions.checkNotNull(ribId))));
- this.domChain = domDataBroker.createTransactionChain(this);
this.localAs = Preconditions.checkNotNull(localAs);
this.bgpIdentifier = Preconditions.checkNotNull(localBgpId);
this.dispatcher = Preconditions.checkNotNull(dispatcher);
this.yangRibId = yangRibIdBuilder.nodeWithKey(Rib.QNAME, RIB_ID_QNAME, ribId.getValue()).build();
this.configModuleTracker = moduleTracker;
this.openConfigProvider = openConfigProvider;
- this.cacheDisconnectedPeers = new CacheDisconnectedPeersImpl();
this.bestPathSelectionStrategies = Preconditions.checkNotNull(bestPathSelectionStrategies);
final ClusterIdentifier cId = (clusterId == null) ? new ClusterIdentifier(localBgpId) : clusterId;
this.renderStats = new RIBImplRuntimeMXBeanImpl(localBgpId, ribId, localAs, cId);
this.ribId = ribId;
- this.policyDatabase = new PolicyDatabase(this.localAs.getValue(), localBgpId, cId);
- this.importPolicyPeerTracker = new ImportPolicyPeerTrackerImpl( this.policyDatabase);
- this.serviceGroupIdentifier = ServiceGroupIdentifier.create(this.ribId + "-service-group");
+ final PolicyDatabase policyDatabase = new PolicyDatabase(this.localAs.getValue(), localBgpId, cId);
+ this.importPolicyPeerTracker = new ImportPolicyPeerTrackerImpl(policyDatabase);
+ this.serviceGroupIdentifier = ServiceGroupIdentifier.create(this.ribId.getValue() + "-service-group");
Preconditions.checkNotNull(provider, "ClusterSingletonServiceProvider is null");
this.provider = provider;
- LOG.info("RIB Singleton Service {} registered", this.getIdentifier());
- this.registration = registerClusterSingletonService(this);
this.configurationWriter = configurationWriter;
+
+ final ImmutableMap.Builder<TablesKey, ExportPolicyPeerTracker> exportPolicies = new ImmutableMap.Builder<>();
+ for (final BgpTableType t : this.localTables) {
+ final TablesKey key = new TablesKey(t.getAfi(), t.getSafi());
+ this.localTablesKeys.add(key);
+ exportPolicies.put(key, new ExportPolicyPeerTrackerImpl(policyDatabase, key));
+ }
+ this.exportPolicyPeerTrackerMap = exportPolicies.build();
+
+ LOG.info("RIB Singleton Service {} registered, RIB {}", getIdentifier().getValue(), this.ribId.getValue());
+ //this need to be always the last step
+ this.registration = registerClusterSingletonService(this);
}
public RIBImpl(final ClusterSingletonServiceProvider provider, final RibId ribId, final AsNumber localAs, final BgpId localBgpId, @Nullable final ClusterIdentifier clusterId,
final DOMDataBroker domDataBroker, final List<BgpTableType> localTables, final Map<TablesKey, PathSelectionMode> bestPathSelectionstrategies,
final GeneratedClassLoadingStrategy classStrategy, final BgpDeployer.WriteConfiguration configurationWriter) {
this(provider, ribId, localAs, localBgpId, clusterId, extensions, dispatcher, codecFactory,
- domDataBroker, localTables, bestPathSelectionstrategies, classStrategy, null, null, configurationWriter);
+ domDataBroker, localTables, bestPathSelectionstrategies, classStrategy, null, null, configurationWriter);
}
- private void startLocRib(final TablesKey key, final PolicyDatabase pd) {
+ private void startLocRib(final TablesKey key) {
LOG.debug("Creating LocRib table for {}", key);
// create locRibWriter for each table
final DOMDataWriteTransaction tx = this.domChain.newWriteOnlyTransaction();
} catch (final TransactionCommitFailedException e1) {
LOG.error("Failed to initiate LocRIB for key {}", key, e1);
}
+ createLocRibWriter(key);
+ }
+ private synchronized void createLocRibWriter(final TablesKey key) {
+ LOG.debug("Creating LocRIB writer for key {}", key);
+ final DOMTransactionChain txChain = createPeerChain(this);
PathSelectionMode pathSelectionStrategy = this.bestPathSelectionStrategies.get(key);
if (pathSelectionStrategy == null) {
pathSelectionStrategy = BasePathSelectionModeFactory.createBestPathSelectionStrategy();
}
- this.locRibs.add(LocRibWriter.create(this.ribContextRegistry, key, createPeerChain(this), getYangRibId(), this.localAs, getService(), pd,
- this.cacheDisconnectedPeers, pathSelectionStrategy, this.renderStats.getLocRibRouteCounter().init(key)));
+ final LocRibWriter locRibWriter = LocRibWriter.create(this.ribContextRegistry, key, txChain,
+ getYangRibId(), this.localAs, getService(), this.exportPolicyPeerTrackerMap.get(key),
+ pathSelectionStrategy, this.renderStats.getLocRibRouteCounter().init(key));
+ this.txChainToLocRibWriter.put(txChain, locRibWriter);
}
@Override
@Override
public synchronized void close() throws Exception {
- this.domChain.close();
- if (registration != null) {
- registration.close();
- registration = null;
+ if (this.registration != null) {
+ this.registration.close();
+ this.registration = null;
}
}
return this.bgpIdentifier;
}
+ @Nonnull
@Override
public Set<? extends BgpTableType> getLocalTables() {
return this.localTables;
}
@Override
- public void onTransactionChainFailed(final TransactionChain<?, ?> chain, final AsyncTransaction<?, ?> transaction, final Throwable cause) {
+ public synchronized void onTransactionChainFailed(final TransactionChain<?, ?> chain, final AsyncTransaction<?, ?> transaction, final Throwable cause) {
LOG.error("Broken chain in RIB {} transaction {}", getInstanceIdentifier(), transaction != null ? transaction.getIdentifier() : null, cause);
+ if (this.txChainToLocRibWriter.containsKey(chain)) {
+ final LocRibWriter locRibWriter = this.txChainToLocRibWriter.remove(chain);
+ final DOMTransactionChain newChain = createPeerChain(this);
+ locRibWriter.restart(newChain);
+ this.txChainToLocRibWriter.put(newChain, locRibWriter);
+ }
}
@Override
}
@Override
- public CacheDisconnectedPeers getCacheDisconnectedPeers() {
- return this.cacheDisconnectedPeers;
+ public ImportPolicyPeerTracker getImportPolicyPeerTracker() {
+ return this.importPolicyPeerTracker;
}
@Override
- public ImportPolicyPeerTracker getImportPolicyPeerTracker() {
- return this.importPolicyPeerTracker;
+ public ExportPolicyPeerTracker getExportPolicyPeerTracker(final TablesKey tablesKey) {
+ return this.exportPolicyPeerTrackerMap.get(tablesKey);
}
@Override
- public void instantiateServiceInstance() {
+ public synchronized void instantiateServiceInstance() {
+ this.isServiceInstantiated = true;
+ this.domChain = this.domDataBroker.createTransactionChain(this);
if(this.configurationWriter != null) {
this.configurationWriter.apply();
}
- LOG.info("RIB Singleton Service {} instantiated", this.getIdentifier());
+ LOG.info("RIB Singleton Service {} instantiated, RIB {}", getIdentifier().getValue(), this.ribId.getValue());
LOG.debug("Instantiating RIB table {} at {}", this.ribId , this.yangRibId);
final ContainerNode bgpRib = Builders.containerBuilder().withNodeIdentifier(new NodeIdentifier(BgpRib.QNAME))
LOG.debug("Effective RIB created.");
- for (final BgpTableType t : this.localTables) {
- final TablesKey key = new TablesKey(t.getAfi(), t.getSafi());
- this.localTablesKeys.add(key);
- startLocRib(key, policyDatabase);
- }
-
+ this.localTablesKeys.forEach(this::startLocRib);
if (this.configModuleTracker != null) {
this.configModuleTracker.onInstanceCreate();
}
}
@Override
- public ListenableFuture<Void> closeServiceInstance() {
- LOG.info("Close RIB Singleton Service {}", this.getIdentifier());
- for (final LocRibWriter locRib : this.locRibs) {
- try {
- locRib.close();
- } catch (final Exception e) {
- LOG.warn("Could not close LocalRib reference: {}", locRib, e);
- }
- }
- try {
- final DOMDataWriteTransaction t = this.domChain.newWriteOnlyTransaction();
- t.delete(LogicalDatastoreType.OPERATIONAL, getYangRibId());
- t.submit().checkedGet();
- } catch (final TransactionCommitFailedException e) {
- LOG.warn("Failed to remove RIB instance {} from DS.", getYangRibId(), e);
+ public synchronized ListenableFuture<Void> closeServiceInstance() {
+ if(!this.isServiceInstantiated) {
+ LOG.trace("RIB Singleton Service {} already closed, RIB {}", getIdentifier().getValue(),
+ this.ribId.getValue());
+ return Futures.immediateFuture(null);
}
+ LOG.info("Close RIB Singleton Service {}, RIB {}", getIdentifier().getValue(), this.ribId.getValue());
+ this.isServiceInstantiated = false;
+
+ this.txChainToLocRibWriter.values().forEach(LocRibWriter::close);
+ this.txChainToLocRibWriter.clear();
+
this.renderStats.getLocRibRouteCounter().resetAll();
if (this.configModuleTracker != null) {
this.configModuleTracker.onInstanceClose();
}
- return Futures.immediateFuture(null);
+
+ final DOMDataWriteTransaction t = this.domChain.newWriteOnlyTransaction();
+ t.delete(LogicalDatastoreType.OPERATIONAL, getYangRibId());
+ final CheckedFuture<Void, TransactionCommitFailedException> cleanFuture = t.submit();
+
+ this.domChain.close();
+
+ return cleanFuture;
}
@Override
@Override
public ClusterSingletonServiceRegistration registerClusterSingletonService(final ClusterSingletonService clusterSingletonService) {
- return this.provider.registerClusterSingletonService(clusterSingletonService);
+ return ClusterSingletonServiceRegistrationHelper.registerSingletonService(this.provider, clusterSingletonService, MAX_REGISTRATION_ATTEMPTS,
+ SLEEP_TIME);
}
@Override