import com.google.common.util.concurrent.ListenableFuture;
import java.util.Collection;
import java.util.Map;
-import java.util.concurrent.ExecutionException;
import org.eclipse.jdt.annotation.NonNull;
import org.opendaylight.mdsal.common.api.CommitInfo;
import org.opendaylight.mdsal.common.api.ReadFailedException;
@Override
public void delete(final YangInstanceIdentifier path) {
- final var existsFuture = verifyNotNull(rwTx).exists(CONFIGURATION, path);
- final boolean exists;
- try {
- exists = existsFuture.get();
- } catch (ExecutionException e) {
- throw new RestconfDocumentedException("Failed to access " + path, e);
- } catch (InterruptedException e) {
- Thread.currentThread().interrupt();
- throw new RestconfDocumentedException("Interrupted while accessing " + path, e);
- }
-
- if (!exists) {
+ if (!TransactionUtil.syncAccess(verifyNotNull(rwTx).exists(CONFIGURATION, path), path)) {
LOG.trace("Operation via Restconf was not executed because data at {} does not exist", path);
throw new RestconfDocumentedException("Data does not exist", ErrorType.PROTOCOL, ErrorTag.DATA_MISSING,
path);
import com.google.common.util.concurrent.FluentFuture;
import com.google.common.util.concurrent.ListenableFuture;
import java.net.URI;
-import java.util.concurrent.ExecutionException;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import org.opendaylight.mdsal.common.api.CommitInfo;
*/
public static void checkItemDoesNotExists(final ListenableFuture<Boolean> existsFuture,
final YangInstanceIdentifier path) {
- final boolean exists;
- try {
- exists = existsFuture.get();
- } catch (ExecutionException e) {
- throw new RestconfDocumentedException("Failed to access " + path, e);
- } catch (InterruptedException e) {
- Thread.currentThread().interrupt();
- throw new RestconfDocumentedException("Interrupted while accessing " + path, e);
- }
-
- if (exists) {
+ if (TransactionUtil.syncAccess(existsFuture, path)) {
LOG.trace("Operation via Restconf was not executed because data at {} already exists", path);
- throw new RestconfDocumentedException(
- "Data already exists", ErrorType.PROTOCOL, ErrorTag.DATA_EXISTS, path);
+ throw new RestconfDocumentedException("Data already exists", ErrorType.PROTOCOL, ErrorTag.DATA_EXISTS,
+ path);
}
}
}
package org.opendaylight.restconf.nb.rfc8040.rests.utils;
import com.google.common.util.concurrent.ListenableFuture;
-import java.util.concurrent.ExecutionException;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import org.opendaylight.mdsal.common.api.CommitInfo;
*/
public static Response putData(final YangInstanceIdentifier path, final NormalizedNode data,
final EffectiveModelContext schemaContext, final RestconfStrategy strategy, final WriteDataParams params) {
- final var existsFuture = strategy.exists(LogicalDatastoreType.CONFIGURATION, path);
- final boolean exists;
- try {
- exists = existsFuture.get();
- } catch (ExecutionException e) {
- throw new RestconfDocumentedException("Failed to access " + path, e);
- } catch (InterruptedException e) {
- Thread.currentThread().interrupt();
- throw new RestconfDocumentedException("Interrupted while accessing " + path, e);
- }
-
+ final var exists = TransactionUtil.syncAccess(strategy.exists(LogicalDatastoreType.CONFIGURATION, path), path);
TransactionUtil.syncCommit(submitData(path, schemaContext, strategy, data, params), PUT_TX_TYPE, path);
// TODO: Status.CREATED implies a location...
return exists ? Response.noContent().build() : Response.status(Status.CREATED).build();
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Optional;
-import java.util.concurrent.ExecutionException;
import java.util.function.Function;
import java.util.stream.Collectors;
import org.eclipse.jdt.annotation.NonNull;
private static @Nullable NormalizedNode extractReadData(final YangInstanceIdentifier path,
final ListenableFuture<Optional<NormalizedNode>> dataFuture) {
- try {
- return dataFuture.get().orElse(null);
- } catch (ExecutionException e) {
- throw new RestconfDocumentedException("Failed to access " + path, e);
- } catch (InterruptedException e) {
- Thread.currentThread().interrupt();
- throw new RestconfDocumentedException("Interrupted while accessing " + path, e);
- }
+ return TransactionUtil.syncAccess(dataFuture, path).orElse(null);
}
/**
ImmutableNodes.fromInstanceId(schemaContext, YangInstanceIdentifier.of(normalizedPathWithoutChildArgs)));
}
+ /**
+ * Synchronize access to a path resource, translating any failure to a {@link RestconfDocumentedException}.
+ *
+ * @param <T> The type being accessed
+ * @param future Access future
+ * @param path Path being accessed
+ * @return The accessed value
+ * @throws RestconfDocumentedException if commit fails
+ */
+ public static <T> T syncAccess(final ListenableFuture<T> future, final YangInstanceIdentifier path) {
+ try {
+ return future.get();
+ } catch (ExecutionException e) {
+ throw new RestconfDocumentedException("Failed to access " + path, e);
+ } catch (InterruptedException e) {
+ Thread.currentThread().interrupt();
+ throw new RestconfDocumentedException("Interrupted while accessing " + path, e);
+ }
+ }
+
/**
* Synchronize commit future, translating any failure to a {@link RestconfDocumentedException}.
*