2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.mdsal.binding.dom.adapter;
10 import static com.google.common.base.Preconditions.checkArgument;
12 import com.google.common.util.concurrent.FluentFuture;
13 import java.util.Map.Entry;
14 import java.util.Optional;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.opendaylight.mdsal.common.api.CommitInfo;
17 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
18 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction;
19 import org.opendaylight.yangtools.yang.binding.DataObject;
20 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
22 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
25 * Abstract Base Transaction for transactions which are backed by
26 * {@link DOMDataTreeWriteTransaction}.
28 public abstract class AbstractWriteTransaction<T extends DOMDataTreeWriteTransaction> extends
29 AbstractForwardedTransaction<T> {
31 protected AbstractWriteTransaction(final T delegate, final BindingToNormalizedNodeCodec codec) {
32 super(delegate, codec);
35 public final <U extends DataObject> void put(final LogicalDatastoreType store, final InstanceIdentifier<U> path,
36 final U data, final boolean createParents) {
37 checkArgument(!path.isWildcarded(), "Cannot put data into wildcarded path %s", path);
39 final Entry<YangInstanceIdentifier, NormalizedNode<?, ?>> normalized = getCodec().toNormalizedNode(path, data);
41 ensureParentsByMerge(store, normalized.getKey(), path);
44 getDelegate().put(store, normalized.getKey(), normalized.getValue());
47 public final <U extends DataObject> void merge(final LogicalDatastoreType store, final InstanceIdentifier<U> path,
48 final U data,final boolean createParents) {
49 checkArgument(!path.isWildcarded(), "Cannot merge data into wildcarded path %s", path);
51 final Entry<YangInstanceIdentifier, NormalizedNode<?, ?>> normalized = getCodec().toNormalizedNode(path, data);
53 ensureParentsByMerge(store, normalized.getKey(), path);
56 getDelegate().merge(store, normalized.getKey(), normalized.getValue());
60 * Use {@link YangInstanceIdentifier#getParent()} instead.
63 protected static Optional<YangInstanceIdentifier> getParent(final YangInstanceIdentifier child) {
64 return Optional.ofNullable(child.getParent());
68 * Subclasses of this class are required to implement creation of parent nodes based on behaviour of their
69 * underlying transaction.
71 * @param store an instance of LogicalDatastoreType
72 * @param domPath an instance of YangInstanceIdentifier
73 * @param path an instance of InstanceIdentifier
75 protected final void ensureParentsByMerge(final LogicalDatastoreType store, final YangInstanceIdentifier domPath,
76 final InstanceIdentifier<?> path) {
77 final YangInstanceIdentifier parentPath = domPath.getParent();
78 if (parentPath != null) {
79 final NormalizedNode<?, ?> parentNode = getCodec().instanceIdentifierToNode(parentPath);
80 getDelegate().merge(store, YangInstanceIdentifier.create(parentNode.getIdentifier()), parentNode);
84 protected final void doDelete(final LogicalDatastoreType store, final InstanceIdentifier<?> path) {
85 checkArgument(!path.isWildcarded(), "Cannot delete wildcarded path %s", path);
87 final YangInstanceIdentifier normalized = getCodec().toYangInstanceIdentifierBlocking(path);
88 getDelegate().delete(store, normalized);
91 protected final @NonNull FluentFuture<? extends @NonNull CommitInfo> doCommit() {
92 return getDelegate().commit();
95 protected final boolean doCancel() {
96 return getDelegate().cancel();