*/
package org.opendaylight.controller.blueprint.ext;
-import com.google.common.base.Optional;
+import com.google.common.util.concurrent.FluentFuture;
import com.google.common.util.concurrent.FutureCallback;
-import com.google.common.util.concurrent.Futures;
-import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.MoreExecutors;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.Collection;
import java.util.Objects;
+import java.util.Optional;
import java.util.concurrent.atomic.AtomicBoolean;
-import javax.annotation.Nonnull;
-import javax.annotation.Nullable;
-import javax.xml.parsers.ParserConfigurationException;
import javax.xml.stream.XMLStreamException;
import org.apache.aries.blueprint.services.ExtendedBlueprintContainer;
+import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.jdt.annotation.Nullable;
import org.opendaylight.controller.blueprint.ext.DataStoreAppConfigDefaultXMLReader.ConfigURLProvider;
-import org.opendaylight.controller.md.sal.binding.api.ClusteredDataTreeChangeListener;
-import org.opendaylight.controller.md.sal.binding.api.DataBroker;
-import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
-import org.opendaylight.controller.md.sal.binding.api.DataObjectModification.ModificationType;
-import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
-import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
-import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
-import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
+import org.opendaylight.mdsal.binding.api.DataBroker;
+import org.opendaylight.mdsal.binding.api.DataObjectModification;
+import org.opendaylight.mdsal.binding.api.DataObjectModification.ModificationType;
+import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
+import org.opendaylight.mdsal.binding.api.DataTreeModification;
+import org.opendaylight.mdsal.binding.api.ReadTransaction;
import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
+import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
import org.opendaylight.mdsal.dom.api.DOMSchemaService;
-import org.opendaylight.yangtools.concepts.ListenerRegistration;
+import org.opendaylight.yangtools.concepts.Registration;
+import org.opendaylight.yangtools.yang.binding.ChildOf;
import org.opendaylight.yangtools.yang.binding.DataObject;
import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
-import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+import org.opendaylight.yangtools.yang.model.api.SchemaTreeInference;
import org.osgi.service.blueprint.container.ComponentDefinitionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
private final AtomicBoolean readingInitialAppConfig = new AtomicBoolean(true);
private volatile BindingContext bindingContext;
- private volatile ListenerRegistration<?> appConfigChangeListenerReg;
+ private volatile Registration appConfigChangeListenerReg;
private volatile DataObject currentAppConfig;
// Note: the BindingNormalizedNodeSerializer interface is annotated as deprecated because there's an
// project are still used - conversion to the mdsal binding classes hasn't occurred yet.
private volatile BindingNormalizedNodeSerializer bindingSerializer;
- public DataStoreAppConfigMetadata(@Nonnull final String id, @Nonnull final String appConfigBindingClassName,
- @Nullable final String appConfigListKeyValue, @Nullable final String defaultAppConfigFileName,
- @Nonnull final UpdateStrategy updateStrategyValue, @Nullable final Element defaultAppConfigElement) {
+ public DataStoreAppConfigMetadata(final String id, final @NonNull String appConfigBindingClassName,
+ final @Nullable String appConfigListKeyValue, final @Nullable String defaultAppConfigFileName,
+ final @NonNull UpdateStrategy updateStrategyValue, final @Nullable Element defaultAppConfigElement) {
super(id);
this.defaultAppConfigElement = defaultAppConfigElement;
this.defaultAppConfigFileName = defaultAppConfigFileName;
this.appConfigBindingClassName = appConfigBindingClassName;
this.appConfigListKeyValue = appConfigListKeyValue;
- this.appConfigUpdateStrategy = updateStrategyValue;
+ appConfigUpdateStrategy = updateStrategyValue;
}
@Override
Class<DataObject> appConfigBindingClass;
try {
Class<?> bindingClass = container.getBundleContext().getBundle().loadClass(appConfigBindingClassName);
- if (!DataObject.class.isAssignableFrom(bindingClass)) {
+ if (!ChildOf.class.isAssignableFrom(bindingClass)) {
throw new ComponentDefinitionException(String.format(
"%s: Specified app config binding class %s does not extend %s",
- logName(), appConfigBindingClassName, DataObject.class.getName()));
+ logName(), appConfigBindingClassName, ChildOf.class.getName()));
}
appConfigBindingClass = (Class<DataObject>) bindingClass;
setDependencyDesc("Initial app config " + bindingContext.appConfigBindingClass.getSimpleName());
- // We register a DTCL to get updates and also read the app config data from the data store. If
- // the app config data is present then both the read and initial DTCN update will return it. If the
- // the data isn't present, we won't get an initial DTCN update so the read will indicate the data
- // isn't present.
-
- DataTreeIdentifier<DataObject> dataTreeId = new DataTreeIdentifier<>(LogicalDatastoreType.CONFIGURATION,
- bindingContext.appConfigPath);
- appConfigChangeListenerReg = dataBroker.registerDataTreeChangeListener(dataTreeId,
- (ClusteredDataTreeChangeListener<DataObject>) this::onAppConfigChanged);
+ // We register a DTCL to get updates and also read the app config data from the data store. If the app config
+ // data is present then both the read and initial DTCN update will return it. If the the data isn't present, we
+ // will not get an initial DTCN update so the read will indicate the data is not present.
+ appConfigChangeListenerReg = dataBroker.registerTreeChangeListener(
+ DataTreeIdentifier.of(LogicalDatastoreType.CONFIGURATION, bindingContext.appConfigPath),
+ this::onAppConfigChanged);
readInitialAppConfig(dataBroker);
}
private void readInitialAppConfig(final DataBroker dataBroker) {
- final ReadOnlyTransaction readOnlyTx = dataBroker.newReadOnlyTransaction();
- ListenableFuture<Optional<DataObject>> future = readOnlyTx.read(
- LogicalDatastoreType.CONFIGURATION, bindingContext.appConfigPath);
- Futures.addCallback(future, new FutureCallback<Optional<DataObject>>() {
+ final FluentFuture<Optional<DataObject>> future;
+ try (ReadTransaction readOnlyTx = dataBroker.newReadOnlyTransaction()) {
+ future = readOnlyTx.read(LogicalDatastoreType.CONFIGURATION, bindingContext.appConfigPath);
+ }
+
+ future.addCallback(new FutureCallback<Optional<DataObject>>() {
@Override
public void onSuccess(final Optional<DataObject> possibleAppConfig) {
LOG.debug("{}: Read of app config {} succeeded: {}", logName(), bindingContext
.appConfigBindingClass.getName(), possibleAppConfig);
- readOnlyTx.close();
setInitialAppConfig(possibleAppConfig);
}
@Override
public void onFailure(final Throwable failure) {
- readOnlyTx.close();
-
// We may have gotten the app config via the data tree change listener so only retry if not.
if (readingInitialAppConfig.get()) {
LOG.warn("{}: Read of app config {} failed - retrying", logName(),
private void onAppConfigChanged(final Collection<DataTreeModification<DataObject>> changes) {
for (DataTreeModification<DataObject> change: changes) {
DataObjectModification<DataObject> changeRoot = change.getRootNode();
- ModificationType type = changeRoot.getModificationType();
+ ModificationType type = changeRoot.modificationType();
LOG.debug("{}: onAppConfigChanged: {}, {}", logName(), type, change.getRootPath());
if (type == ModificationType.SUBTREE_MODIFIED || type == ModificationType.WRITE) {
- DataObject newAppConfig = changeRoot.getDataAfter();
+ DataObject newAppConfig = changeRoot.dataAfter();
LOG.debug("New app config instance: {}, previous: {}", newAppConfig, currentAppConfig);
if (result) {
DataObject localAppConfig;
if (possibleAppConfig.isPresent()) {
- localAppConfig = possibleAppConfig.get();
+ localAppConfig = possibleAppConfig.orElseThrow();
} else {
// No app config data is present so create an empty instance via the bindingSerializer service.
// This will also return default values for leafs that haven't been explicitly set.
appConfigFile.getAbsolutePath());
if (!appConfigFile.exists()) {
- return Optional.absent();
+ return Optional.empty();
}
LOG.debug("{}: Found file {}", logName(), appConfigFile.getAbsolutePath());
DataStoreAppConfigDefaultXMLReader<?> reader = new DataStoreAppConfigDefaultXMLReader<>(logName(),
defaultAppConfigFileName, getOSGiService(DOMSchemaService.class), bindingSerializer, bindingContext,
inputStreamProvider);
- return reader.createDefaultInstance((schemaContext, dataSchema) -> {
+ return reader.createDefaultInstance(dataSchema -> {
// Fallback if file cannot be read, try XML from Config
- NormalizedNode<?, ?> dataNode = parsePossibleDefaultAppConfigElement(schemaContext, dataSchema);
+ NormalizedNode dataNode = parsePossibleDefaultAppConfigElement(dataSchema);
if (dataNode == null) {
// or, as last resort, defaults from the model
return bindingContext.newDefaultNode(dataSchema);
}
});
- } catch (final ConfigXMLReaderException | IOException | SAXException | XMLStreamException
- | ParserConfigurationException | URISyntaxException e) {
+ } catch (ConfigXMLReaderException | IOException | SAXException | XMLStreamException | URISyntaxException e) {
if (e.getCause() == null) {
setFailureMessage(e.getMessage());
} else {
}
}
- @Nullable
- private NormalizedNode<?, ?> parsePossibleDefaultAppConfigElement(final SchemaContext schemaContext,
- final DataSchemaNode dataSchema) throws URISyntaxException, IOException, ParserConfigurationException,
- SAXException, XMLStreamException {
+ private @Nullable NormalizedNode parsePossibleDefaultAppConfigElement(final SchemaTreeInference dataSchema)
+ throws URISyntaxException, IOException, SAXException, XMLStreamException {
if (defaultAppConfigElement == null) {
return null;
}
LOG.debug("{}: Got app config schema: {}", logName(), dataSchema);
- NormalizedNode<?, ?> dataNode = bindingContext.parseDataElement(defaultAppConfigElement, dataSchema,
- schemaContext);
+ NormalizedNode dataNode = bindingContext.parseDataElement(defaultAppConfigElement, dataSchema);
LOG.debug("{}: Parsed data node: {}", logName(), dataNode);