2 * Copyright (c) 2017 Pantheon Technologies, s.r.o. and others. 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
8 package org.opendaylight.restconf.nb.rfc8040.rests.transactions;
10 import com.google.common.util.concurrent.FutureCallback;
11 import com.google.common.util.concurrent.MoreExecutors;
12 import com.google.common.util.concurrent.SettableFuture;
13 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
14 import java.util.AbstractMap.SimpleImmutableEntry;
15 import java.util.Collection;
16 import java.util.Map.Entry;
17 import java.util.concurrent.ExecutionException;
18 import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
19 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
20 import org.opendaylight.mdsal.common.api.ReadFailedException;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
22 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
24 final class BatchedExistenceCheck {
25 private static final AtomicIntegerFieldUpdater<BatchedExistenceCheck> UPDATER =
26 AtomicIntegerFieldUpdater.newUpdater(BatchedExistenceCheck.class, "outstanding");
28 private final SettableFuture<Entry<YangInstanceIdentifier, ReadFailedException>> future = SettableFuture.create();
30 @SuppressWarnings("unused")
31 private volatile int outstanding;
33 private BatchedExistenceCheck(final int total) {
34 this.outstanding = total;
37 static BatchedExistenceCheck start(final RestconfStrategy read,
38 final LogicalDatastoreType datastore, final YangInstanceIdentifier parentPath,
39 final Collection<? extends NormalizedNode<?, ?>> children) {
40 final BatchedExistenceCheck ret = new BatchedExistenceCheck(children.size());
41 for (NormalizedNode<?, ?> child : children) {
42 final YangInstanceIdentifier path = parentPath.node(child.getIdentifier());
43 read.exists(datastore, path).addCallback(new FutureCallback<Boolean>() {
45 public void onSuccess(final Boolean result) {
46 ret.complete(path, result);
50 @SuppressFBWarnings("BC_UNCONFIRMED_CAST_OF_RETURN_VALUE")
51 public void onFailure(final Throwable throwable) {
53 if (throwable instanceof Exception) {
54 e = (Exception) throwable;
56 e = new ExecutionException(throwable);
59 ret.complete(path, ReadFailedException.MAPPER.apply(e));
61 }, MoreExecutors.directExecutor());
67 Entry<YangInstanceIdentifier, ReadFailedException> getFailure() throws InterruptedException {
70 } catch (ExecutionException e) {
71 // This should never happen
72 throw new IllegalStateException(e);
76 @SuppressFBWarnings(value = "UPM_UNCALLED_PRIVATE_METHOD",
77 justification = "https://github.com/spotbugs/spotbugs/issues/811")
78 private void complete(final YangInstanceIdentifier childPath, final boolean present) {
79 final int count = UPDATER.decrementAndGet(this);
81 future.set(new SimpleImmutableEntry<>(childPath, null));
82 } else if (count == 0) {
87 @SuppressFBWarnings(value = "UPM_UNCALLED_PRIVATE_METHOD",
88 justification = "https://github.com/spotbugs/spotbugs/issues/811")
89 private void complete(final YangInstanceIdentifier childPath, final ReadFailedException cause) {
90 UPDATER.decrementAndGet(this);
91 future.set(new SimpleImmutableEntry<>(childPath, cause));