Adjust for AnyXmlNode changing its name
[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
26 abstract class AbstractForwardedTransaction<T extends DOMDataTreeTransaction> implements Delegator<T>,
27         Identifiable<Object> {
28
29     private final @NonNull BindingToNormalizedNodeCodec codec;
30     private final @NonNull T delegate;
31
32     AbstractForwardedTransaction(final T delegateTx, final BindingToNormalizedNodeCodec codec) {
33         this.delegate = requireNonNull(delegateTx, "Delegate must not be null");
34         this.codec = requireNonNull(codec, "Codec must not be null");
35     }
36
37     @Override
38     public final Object getIdentifier() {
39         return delegate.getIdentifier();
40     }
41
42     @Override
43     public final T getDelegate() {
44         return delegate;
45     }
46
47     protected final <S extends DOMDataTreeTransaction> S getDelegateChecked(final Class<S> txType) {
48         checkState(txType.isInstance(delegate));
49         return txType.cast(delegate);
50     }
51
52     protected final BindingToNormalizedNodeCodec getCodec() {
53         return codec;
54     }
55
56     protected final <D extends DataObject> @NonNull FluentFuture<Optional<D>> doRead(
57             final DOMDataTreeReadOperations readOps, final LogicalDatastoreType store,
58             final InstanceIdentifier<D> path) {
59         checkArgument(!path.isWildcarded(), "Invalid read of wildcarded path %s", path);
60
61         return readOps.read(store, codec.toYangInstanceIdentifierBlocking(path))
62                 .transform(codec.getCodecRegistry().deserializeFunction(path)::apply, MoreExecutors.directExecutor());
63     }
64
65     protected final @NonNull FluentFuture<Boolean> doExists(final DOMDataTreeReadOperations readOps,
66             final LogicalDatastoreType store, final InstanceIdentifier<?> path) {
67         checkArgument(!path.isWildcarded(), "Invalid exists of wildcarded path %s", path);
68         return readOps.exists(store, codec.toYangInstanceIdentifierBlocking(path));
69     }
70 }