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.SettableFuture;
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.controller.md.sal.common.api.data.LogicalDatastoreType;
20 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
21 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadTransaction;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
23 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
25 final class BatchedExistenceCheck {
26 private static final AtomicIntegerFieldUpdater<BatchedExistenceCheck> UPDATER = AtomicIntegerFieldUpdater.newUpdater(
27 BatchedExistenceCheck.class, "outstanding");
29 private final SettableFuture<Entry<YangInstanceIdentifier, ReadFailedException>> future = SettableFuture.create();
31 @SuppressWarnings("unused")
32 private volatile int outstanding;
34 private BatchedExistenceCheck(final int total) {
35 this.outstanding = total;
38 static BatchedExistenceCheck start(final DOMDataReadTransaction readTx,
39 final LogicalDatastoreType datastore, final YangInstanceIdentifier parentPath,
40 final Collection<? extends NormalizedNode<?, ?>> children) {
41 final BatchedExistenceCheck ret = new BatchedExistenceCheck(children.size());
42 for (NormalizedNode<?, ?> child : children) {
43 final YangInstanceIdentifier path = parentPath.node(child.getIdentifier());
44 final ListenableFuture<Boolean> f = readTx.exists(datastore, path);
45 Futures.addCallback(f, new FutureCallback<Boolean>() {
47 public void onSuccess(final Boolean result) {
48 ret.complete(path, result.booleanValue());
52 public void onFailure(final Throwable t) {
54 if (t instanceof Exception) {
57 e = new ExecutionException(t);
60 ret.complete(path, ReadFailedException.MAPPER.apply(e));
68 Entry<YangInstanceIdentifier, ReadFailedException> getFailure() throws InterruptedException {
71 } catch (ExecutionException e) {
72 // This should never happen
73 throw new IllegalStateException(e);
77 private void complete(final YangInstanceIdentifier childPath, final boolean present) {
78 final int count = UPDATER.decrementAndGet(this);
80 future.set(new SimpleImmutableEntry<>(childPath, null));
81 } else if (count == 0) {
86 private void complete(final YangInstanceIdentifier childPath, final ReadFailedException cause) {
87 UPDATER.decrementAndGet(this);
88 future.set(new SimpleImmutableEntry<>(childPath, cause));