Hide BindingRpcFutureAware
[mdsal.git] / binding / mdsal-binding-dom-adapter / src / main / java / org / opendaylight / mdsal / binding / dom / adapter / AbstractForwardedTransaction.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. 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.dom.adapter;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static com.google.common.base.Preconditions.checkState;
12 import static java.util.Objects.requireNonNull;
13
14 import com.google.common.util.concurrent.FluentFuture;
15 import com.google.common.util.concurrent.MoreExecutors;
16 import java.util.Optional;
17 import org.eclipse.jdt.annotation.NonNull;
18 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
19 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadOperations;
20 import org.opendaylight.mdsal.dom.api.DOMDataTreeTransaction;
21 import org.opendaylight.yangtools.concepts.Delegator;
22 import org.opendaylight.yangtools.concepts.Identifiable;
23 import org.opendaylight.yangtools.yang.binding.DataObject;
24 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
26
27 abstract class AbstractForwardedTransaction<T extends DOMDataTreeTransaction> implements Delegator<T>,
28         Identifiable<Object> {
29
30     private final @NonNull AdapterContext adapterContext;
31     private final @NonNull T delegate;
32
33     AbstractForwardedTransaction(final AdapterContext adapterContext, final T delegateTx) {
34         this.adapterContext = requireNonNull(adapterContext, "Codec must not be null");
35         this.delegate = requireNonNull(delegateTx, "Delegate must not be null");
36     }
37
38     @Override
39     public final Object getIdentifier() {
40         return delegate.getIdentifier();
41     }
42
43     @Override
44     public final T getDelegate() {
45         return delegate;
46     }
47
48     protected final <S extends DOMDataTreeTransaction> S getDelegateChecked(final Class<S> txType) {
49         checkState(txType.isInstance(delegate));
50         return txType.cast(delegate);
51     }
52
53     protected final AdapterContext adapterContext() {
54         return adapterContext;
55     }
56
57     protected final <D extends DataObject> @NonNull FluentFuture<Optional<D>> doRead(
58             final DOMDataTreeReadOperations readOps, final LogicalDatastoreType store,
59             final InstanceIdentifier<D> path) {
60         checkArgument(!path.isWildcarded(), "Invalid read of wildcarded path %s", path);
61
62         final CurrentAdapterSerializer codec = adapterContext.currentSerializer();
63         final YangInstanceIdentifier domPath = codec.toYangInstanceIdentifier(path);
64
65         return readOps.read(store, domPath)
66                 .transform(optData -> optData.map(domData -> (D) codec.fromNormalizedNode(domPath, domData).getValue()),
67                     MoreExecutors.directExecutor());
68     }
69
70     protected final @NonNull FluentFuture<Boolean> doExists(final DOMDataTreeReadOperations readOps,
71             final LogicalDatastoreType store, final InstanceIdentifier<?> path) {
72         checkArgument(!path.isWildcarded(), "Invalid exists of wildcarded path %s", path);
73         return readOps.exists(store, adapterContext.currentSerializer().toYangInstanceIdentifier(path));
74     }
75 }