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 java.util.AbstractMap.SimpleImmutableEntry;
16 import java.util.Collection;
17 import java.util.Map.Entry;
18 import java.util.concurrent.ExecutionException;
19 import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
20 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
21 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
22 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadTransaction;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
24 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
26 final class BatchedExistenceCheck {
27 private static final AtomicIntegerFieldUpdater<BatchedExistenceCheck> UPDATER =
28 AtomicIntegerFieldUpdater.newUpdater(BatchedExistenceCheck.class, "outstanding");
30 private final SettableFuture<Entry<YangInstanceIdentifier, ReadFailedException>> future = SettableFuture.create();
32 @SuppressWarnings("unused")
33 private volatile int outstanding;
35 private BatchedExistenceCheck(final int total) {
36 this.outstanding = total;
39 static BatchedExistenceCheck start(final DOMDataReadTransaction readTx,
40 final LogicalDatastoreType datastore, final YangInstanceIdentifier parentPath,
41 final Collection<? extends NormalizedNode<?, ?>> children) {
42 final BatchedExistenceCheck ret = new BatchedExistenceCheck(children.size());
43 for (NormalizedNode<?, ?> child : children) {
44 final YangInstanceIdentifier path = parentPath.node(child.getIdentifier());
45 final ListenableFuture<Boolean> f = readTx.exists(datastore, path);
46 Futures.addCallback(f, new FutureCallback<Boolean>() {
48 public void onSuccess(final Boolean result) {
49 ret.complete(path, result.booleanValue());
53 public void onFailure(final Throwable throwable) {
55 if (throwable instanceof Exception) {
56 e = (Exception) throwable;
58 e = new ExecutionException(throwable);
61 ret.complete(path, ReadFailedException.MAPPER.apply(e));
63 }, MoreExecutors.directExecutor());
69 Entry<YangInstanceIdentifier, ReadFailedException> getFailure() throws InterruptedException {
72 } catch (ExecutionException e) {
73 // This should never happen
74 throw new IllegalStateException(e);
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 private void complete(final YangInstanceIdentifier childPath, final ReadFailedException cause) {
88 UPDATER.decrementAndGet(this);
89 future.set(new SimpleImmutableEntry<>(childPath, cause));