Introduce ReadOperations.exists()
[mdsal.git] / binding / mdsal-binding-api / src / main / java / org / opendaylight / mdsal / binding / api / ReadOperations.java
1 /*
2  * Copyright (c) 2018 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.mdsal.binding.api;
9
10 import com.google.common.util.concurrent.FluentFuture;
11 import com.google.common.util.concurrent.MoreExecutors;
12 import java.util.Optional;
13 import org.eclipse.jdt.annotation.NonNull;
14 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
15 import org.opendaylight.mdsal.common.api.ReadFailedException;
16 import org.opendaylight.yangtools.yang.binding.DataObject;
17 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
18
19 /**
20  * Read-like operations supported by {@link ReadTransaction} and {@link ReadWriteTransaction}. This interface defines
21  * the operations without a tie-in with lifecycle management.
22  */
23 public interface ReadOperations {
24     /**
25      * Reads data from the provided logical data store located at the provided path.
26      *
27      * <p>
28      * If the target is a subtree, then the whole subtree is read (and will be accessible from the returned data
29      * object).
30      *
31      * @param store Logical data store from which read should occur.
32      * @param path Path which uniquely identifies subtree which client want to read
33      * @return a FluentFuture containing the result of the read. The Future blocks until the operation is complete. Once
34      *         complete:
35      *         <ul>
36      *         <li>If the data at the supplied path exists, the Future returns an Optional object containing the data.
37      *         </li>
38      *         <li>If the data at the supplied path does not exist, the Future returns Optional.empty().</li>
39      *         <li>If the read of the data fails, the Future will fail with a {@link ReadFailedException} or
40      *         an exception derived from ReadFailedException.</li>
41      *         </ul>
42      * @throws NullPointerException if any of the arguments is null
43      * @throws IllegalArgumentException if the path is {@link InstanceIdentifier#isWildcarded()}
44      */
45     <T extends DataObject> @NonNull FluentFuture<Optional<T>> read(@NonNull LogicalDatastoreType store,
46             @NonNull InstanceIdentifier<T> path);
47
48     /**
49      * Determines if data data exists in the provided logical data store located at the provided path.
50      *
51      * <p>
52      * Default implementation just delegates to {@link #read(LogicalDatastoreType, InstanceIdentifier)}. Implementations
53      * are recommended to override with a more efficient implementation.
54      *
55      * @param store Logical data store from which read should occur.
56      * @param path Path which uniquely identifies subtree which client want to read
57      * @return a FluentFuture containing the result of the check. The Future blocks until the operation is complete.
58      *         Once complete:
59      *         <ul>
60      *         <li>If the data at the supplied path exists, the Future returns {@link Boolean#TRUE}.
61      *         </li>
62      *         <li>If the data at the supplied path does not exist, the Future returns {@link Boolean#FALSE}.</li>
63      *         <li>If the check fails, the Future will fail with a {@link ReadFailedException} or an exception derived
64      *             from ReadFailedException.</li>
65      *         </ul>
66      * @throws NullPointerException if any of the arguments is null
67      * @throws IllegalArgumentException if the path is {@link InstanceIdentifier#isWildcarded()} and the implementation
68      *                                  does not support evaluating wildcards.
69      */
70     default @NonNull FluentFuture<Boolean> exists(final @NonNull LogicalDatastoreType store,
71             final @NonNull InstanceIdentifier<?> path) {
72         return read(store, path).transform(Optional::isPresent, MoreExecutors.directExecutor());
73     }
74 }