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.netconf.sal.restconf.impl;
10 import com.google.common.util.concurrent.FutureCallback;
11 import com.google.common.util.concurrent.Futures;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import com.google.common.util.concurrent.MoreExecutors;
14 import com.google.common.util.concurrent.SettableFuture;
15 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
16 import java.util.AbstractMap.SimpleImmutableEntry;
17 import java.util.Collection;
18 import java.util.Map.Entry;
19 import java.util.concurrent.ExecutionException;
20 import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
21 import javax.annotation.Nonnull;
22 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
23 import org.opendaylight.mdsal.common.api.ReadFailedException;
24 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadTransaction;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
26 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
28 final class BatchedExistenceCheck {
29 private static final AtomicIntegerFieldUpdater<BatchedExistenceCheck> UPDATER =
30 AtomicIntegerFieldUpdater.newUpdater(BatchedExistenceCheck.class, "outstanding");
32 private final SettableFuture<Entry<YangInstanceIdentifier, ReadFailedException>> future = SettableFuture.create();
34 @SuppressWarnings("unused")
35 private volatile int outstanding;
37 private BatchedExistenceCheck(final int total) {
38 this.outstanding = total;
41 static BatchedExistenceCheck start(final DOMDataTreeReadTransaction readTx,
42 final LogicalDatastoreType datastore, final YangInstanceIdentifier parentPath,
43 final Collection<? extends NormalizedNode<?, ?>> children) {
44 final BatchedExistenceCheck ret = new BatchedExistenceCheck(children.size());
45 for (NormalizedNode<?, ?> child : children) {
46 final YangInstanceIdentifier path = parentPath.node(child.getIdentifier());
47 final ListenableFuture<Boolean> f = readTx.exists(datastore, path);
48 Futures.addCallback(f, new FutureCallback<Boolean>() {
50 public void onSuccess(@Nonnull final Boolean result) {
51 ret.complete(path, result.booleanValue());
55 @SuppressFBWarnings("BC_UNCONFIRMED_CAST_OF_RETURN_VALUE")
56 public void onFailure(final Throwable throwable) {
58 if (throwable instanceof Exception) {
59 e = (Exception) throwable;
61 e = new ExecutionException(throwable);
64 ret.complete(path, ReadFailedException.MAPPER.apply(e));
66 }, MoreExecutors.directExecutor());
72 Entry<YangInstanceIdentifier, ReadFailedException> getFailure() throws InterruptedException {
75 } catch (ExecutionException e) {
76 // This should never happen
77 throw new IllegalStateException(e);
81 private void complete(final YangInstanceIdentifier childPath, final boolean present) {
82 final int count = UPDATER.decrementAndGet(this);
84 future.set(new SimpleImmutableEntry<>(childPath, null));
85 } else if (count == 0) {
90 private void complete(final YangInstanceIdentifier childPath, final ReadFailedException cause) {
91 UPDATER.decrementAndGet(this);
92 future.set(new SimpleImmutableEntry<>(childPath, cause));