132e02256c5e3c824aed6ecbdab2f6fbad97033f
[netconf.git] / restconf / restconf-nb-bierman02 / src / main / java / org / opendaylight / netconf / sal / restconf / impl / 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.netconf.sal.restconf.impl;
9
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.controller.md.sal.common.api.data.LogicalDatastoreType;
23 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
24 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadTransaction;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
26 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
27
28 final class BatchedExistenceCheck {
29     private static final AtomicIntegerFieldUpdater<BatchedExistenceCheck> UPDATER =
30             AtomicIntegerFieldUpdater.newUpdater(BatchedExistenceCheck.class, "outstanding");
31
32     private final SettableFuture<Entry<YangInstanceIdentifier, ReadFailedException>> future = SettableFuture.create();
33
34     @SuppressWarnings("unused")
35     private volatile int outstanding;
36
37     private BatchedExistenceCheck(final int total) {
38         this.outstanding = total;
39     }
40
41     static BatchedExistenceCheck start(final DOMDataReadTransaction 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>() {
49                 @Override
50                 public void onSuccess(@Nonnull final Boolean result) {
51                     ret.complete(path, result.booleanValue());
52                 }
53
54                 @Override
55                 @SuppressFBWarnings("BC_UNCONFIRMED_CAST_OF_RETURN_VALUE")
56                 public void onFailure(final Throwable throwable) {
57                     final Exception e;
58                     if (throwable instanceof Exception) {
59                         e = (Exception) throwable;
60                     } else {
61                         e = new ExecutionException(throwable);
62                     }
63
64                     ret.complete(path, ReadFailedException.MAPPER.apply(e));
65                 }
66             }, MoreExecutors.directExecutor());
67         }
68
69         return ret;
70     }
71
72     Entry<YangInstanceIdentifier, ReadFailedException> getFailure() throws InterruptedException {
73         try {
74             return future.get();
75         } catch (ExecutionException e) {
76             // This should never happen
77             throw new IllegalStateException(e);
78         }
79     }
80
81     private void complete(final YangInstanceIdentifier childPath, final boolean present) {
82         final int count = UPDATER.decrementAndGet(this);
83         if (present) {
84             future.set(new SimpleImmutableEntry<>(childPath, null));
85         } else if (count == 0) {
86             future.set(null);
87         }
88     }
89
90     private void complete(final YangInstanceIdentifier childPath, final ReadFailedException cause) {
91         UPDATER.decrementAndGet(this);
92         future.set(new SimpleImmutableEntry<>(childPath, cause));
93     }
94 }