Do not issue empty list merges
[mdsal.git] / binding / mdsal-binding-dom-adapter / src / main / java / org / opendaylight / mdsal / binding / dom / adapter / AbstractWriteTransaction.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
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;
23
24 /**
25  * Abstract Base Transaction for transactions which are backed by
26  * {@link DOMDataTreeWriteTransaction}.
27  */
28 public abstract class AbstractWriteTransaction<T extends DOMDataTreeWriteTransaction> extends
29         AbstractForwardedTransaction<T> {
30
31     protected AbstractWriteTransaction(final T delegate, final BindingToNormalizedNodeCodec codec) {
32         super(delegate, codec);
33     }
34
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);
38
39         final Entry<YangInstanceIdentifier, NormalizedNode<?, ?>> normalized = getCodec().toNormalizedNode(path, data);
40         if (createParents) {
41             ensureParentsByMerge(store, normalized.getKey(), path);
42         }
43
44         getDelegate().put(store, normalized.getKey(), normalized.getValue());
45     }
46
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);
50
51         final Entry<YangInstanceIdentifier, NormalizedNode<?, ?>> normalized = getCodec().toNormalizedNode(path, data);
52         if (createParents) {
53             ensureParentsByMerge(store, normalized.getKey(), path);
54         }
55
56         getDelegate().merge(store, normalized.getKey(), normalized.getValue());
57     }
58
59     /**
60      * Use {@link YangInstanceIdentifier#getParent()} instead.
61      */
62     @Deprecated
63     protected static Optional<YangInstanceIdentifier> getParent(final YangInstanceIdentifier child) {
64         return Optional.ofNullable(child.getParent());
65     }
66
67     /**
68      * Subclasses of this class are required to implement creation of parent nodes based on behaviour of their
69      * underlying transaction.
70      *
71      * @param store an instance of LogicalDatastoreType
72      * @param domPath an instance of YangInstanceIdentifier
73      * @param path an instance of InstanceIdentifier
74      */
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);
81         }
82     }
83
84     protected final void doDelete(final LogicalDatastoreType store, final InstanceIdentifier<?> path) {
85         checkArgument(!path.isWildcarded(), "Cannot delete wildcarded path %s", path);
86
87         final YangInstanceIdentifier normalized = getCodec().toYangInstanceIdentifierBlocking(path);
88         getDelegate().delete(store, normalized);
89     }
90
91     protected final @NonNull FluentFuture<? extends @NonNull CommitInfo> doCommit() {
92         return getDelegate().commit();
93     }
94
95     protected final boolean doCancel() {
96         return getDelegate().cancel();
97     }
98 }