25c339cc7757f9f0340e16d49c08f3a2cc1dac0f
[netconf.git] / restconf / restconf-nb / src / main / java / org / opendaylight / restconf / nb / rfc8040 / rests / transactions / BatchedExistenceCheck.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies, s.r.o. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.restconf.nb.rfc8040.rests.transactions;
9
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.mdsal.dom.api.DOMDataTreeReadOperations;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
23 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
24
25 final class BatchedExistenceCheck {
26     private static final AtomicIntegerFieldUpdater<BatchedExistenceCheck> UPDATER =
27         AtomicIntegerFieldUpdater.newUpdater(BatchedExistenceCheck.class, "outstanding");
28
29     private final SettableFuture<Entry<YangInstanceIdentifier, ReadFailedException>> future = SettableFuture.create();
30
31     @SuppressWarnings("unused")
32     private volatile int outstanding;
33
34     private BatchedExistenceCheck(final int total) {
35         outstanding = total;
36     }
37
38     static BatchedExistenceCheck start(final DOMDataTreeReadOperations tx, final LogicalDatastoreType datastore,
39             final YangInstanceIdentifier parentPath, final Collection<? extends NormalizedNode> children) {
40         final var ret = new BatchedExistenceCheck(children.size());
41         for (var child : children) {
42             final var path = parentPath.node(child.name());
43             tx.exists(datastore, path).addCallback(new FutureCallback<Boolean>() {
44                 @Override
45                 public void onSuccess(final Boolean result) {
46                     ret.complete(path, result);
47                 }
48
49                 @Override
50                 @SuppressFBWarnings("BC_UNCONFIRMED_CAST_OF_RETURN_VALUE")
51                 public void onFailure(final Throwable throwable) {
52                     final Exception e;
53                     if (throwable instanceof Exception) {
54                         e = (Exception) throwable;
55                     } else {
56                         e = new ExecutionException(throwable);
57                     }
58
59                     ret.complete(path, ReadFailedException.MAPPER.apply(e));
60                 }
61             }, MoreExecutors.directExecutor());
62         }
63         return ret;
64     }
65
66     Entry<YangInstanceIdentifier, ReadFailedException> getFailure() throws InterruptedException {
67         try {
68             return future.get();
69         } catch (ExecutionException e) {
70             // This should never happen
71             throw new IllegalStateException(e);
72         }
73     }
74
75     private void complete(final YangInstanceIdentifier childPath, final boolean present) {
76         final int count = UPDATER.decrementAndGet(this);
77         if (present) {
78             future.set(new SimpleImmutableEntry<>(childPath, null));
79         } else if (count == 0) {
80             future.set(null);
81         }
82     }
83
84     private void complete(final YangInstanceIdentifier childPath, final ReadFailedException cause) {
85         UPDATER.decrementAndGet(this);
86         future.set(new SimpleImmutableEntry<>(childPath, cause));
87     }
88 }