f36caa0d2dc46a965b1644b16f0359dc3f40627f
[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 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;
25
26 final class BatchedExistenceCheck {
27     private static final AtomicIntegerFieldUpdater<BatchedExistenceCheck> UPDATER =
28             AtomicIntegerFieldUpdater.newUpdater(BatchedExistenceCheck.class, "outstanding");
29
30     private final SettableFuture<Entry<YangInstanceIdentifier, ReadFailedException>> future = SettableFuture.create();
31
32     @SuppressWarnings("unused")
33     private volatile int outstanding;
34
35     private BatchedExistenceCheck(final int total) {
36         this.outstanding = total;
37     }
38
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>() {
47                 @Override
48                 public void onSuccess(final Boolean result) {
49                     ret.complete(path, result.booleanValue());
50                 }
51
52                 @Override
53                 public void onFailure(final Throwable throwable) {
54                     final Exception e;
55                     if (throwable instanceof Exception) {
56                         e = (Exception) throwable;
57                     } else {
58                         e = new ExecutionException(throwable);
59                     }
60
61                     ret.complete(path, ReadFailedException.MAPPER.apply(e));
62                 }
63             }, MoreExecutors.directExecutor());
64         }
65
66         return ret;
67     }
68
69     Entry<YangInstanceIdentifier, ReadFailedException> getFailure() throws InterruptedException {
70         try {
71             return future.get();
72         } catch (ExecutionException e) {
73             // This should never happen
74             throw new IllegalStateException(e);
75         }
76     }
77
78     private void complete(final YangInstanceIdentifier childPath, final boolean present) {
79         final int count = UPDATER.decrementAndGet(this);
80         if (present) {
81             future.set(new SimpleImmutableEntry<>(childPath, null));
82         } else if (count == 0) {
83             future.set(null);
84         }
85     }
86
87     private void complete(final YangInstanceIdentifier childPath, final ReadFailedException cause) {
88         UPDATER.decrementAndGet(this);
89         future.set(new SimpleImmutableEntry<>(childPath, cause));
90     }
91 }