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.controller.md.sal.binding.impl;
10 import static com.google.common.base.Preconditions.checkArgument;
12 import com.google.common.base.Optional;
13 import com.google.common.util.concurrent.FluentFuture;
14 import java.util.Map.Entry;
15 import org.gaul.modernizer_maven_annotations.SuppressModernizer;
16 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
17 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
18 import org.opendaylight.mdsal.common.api.CommitInfo;
19 import org.opendaylight.yangtools.yang.binding.DataObject;
20 import org.opendaylight.yangtools.yang.binding.Identifiable;
21 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
23 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
26 * Abstract Base Transaction for transactions which are backed by {@link DOMDataWriteTransaction}.
28 @Deprecated(forRemoval = true)
29 public abstract class AbstractWriteTransaction<T extends DOMDataWriteTransaction> extends
30 AbstractForwardedTransaction<T> {
32 protected AbstractWriteTransaction(final T delegate, final BindingToNormalizedNodeCodec codec) {
33 super(delegate, codec);
36 public final <U extends DataObject> void put(final LogicalDatastoreType store,
37 final InstanceIdentifier<U> path, final U data, final boolean createParents) {
38 checkArgument(!path.isWildcarded(), "Cannot put data into wildcarded path %s", path);
40 final Entry<YangInstanceIdentifier, NormalizedNode<?, ?>> normalized = getCodec().toNormalizedNode(path, data);
42 ensureParentsByMerge(store, normalized.getKey(), path);
44 ensureListParentIfNeeded(store,path,normalized);
47 getDelegate().put(store, normalized.getKey(), normalized.getValue());
50 public final <U extends DataObject> void merge(final LogicalDatastoreType store,
51 final InstanceIdentifier<U> path, final U data,final boolean createParents) {
52 checkArgument(!path.isWildcarded(), "Cannot merge data into wildcarded path %s", path);
54 final Entry<YangInstanceIdentifier, NormalizedNode<?, ?>> normalized = getCodec().toNormalizedNode(path, data);
56 ensureParentsByMerge(store, normalized.getKey(), path);
58 ensureListParentIfNeeded(store,path,normalized);
61 getDelegate().merge(store, normalized.getKey(), normalized.getValue());
65 * Ensures list parent if item is list, otherwise noop.
68 * One of properties of binding specification is that it is imposible
69 * to represent list as a whole and thus it is impossible to write
70 * empty variation of MapNode without creating parent node, with
74 * This actually makes writes such as
76 * put("Nodes", new NodesBuilder().build());
77 * put("Nodes/Node[key]", new NodeBuilder().setKey("key").build());
79 * To result in three DOM operations:
81 * put("/nodes",domNodes);
82 * merge("/nodes/node",domNodeList);
83 * put("/nodes/node/node[key]",domNode);
87 * In order to allow that to be inserted if necessary, if we know
88 * item is list item, we will try to merge empty MapNode or OrderedNodeMap
89 * to ensure list exists.
91 * @param store Data Store type
92 * @param path Path to data (Binding Aware)
93 * @param normalized Normalized version of data to be written
95 private void ensureListParentIfNeeded(final LogicalDatastoreType store, final InstanceIdentifier<?> path,
96 final Entry<YangInstanceIdentifier, NormalizedNode<?, ?>> normalized) {
97 if (Identifiable.class.isAssignableFrom(path.getTargetType())) {
98 YangInstanceIdentifier parentMapPath = normalized.getKey().getParent();
99 checkArgument(parentMapPath != null, "Map path %s does not have a parent", path);
101 NormalizedNode<?, ?> emptyParent = getCodec().getDefaultNodeFor(parentMapPath);
102 getDelegate().merge(store, parentMapPath, emptyParent);
109 * @deprecated Use {@link YangInstanceIdentifier#getParent()} instead.
113 protected static Optional<YangInstanceIdentifier> getParent(final YangInstanceIdentifier child) {
114 return Optional.fromNullable(child.getParent());
118 * Subclasses of this class are required to implement creation of parent
119 * nodes based on behaviour of their underlying transaction.
121 protected abstract void ensureParentsByMerge(LogicalDatastoreType store,
122 YangInstanceIdentifier key, InstanceIdentifier<?> path);
124 protected final void doDelete(final LogicalDatastoreType store,
125 final InstanceIdentifier<?> path) {
126 checkArgument(!path.isWildcarded(), "Cannot delete wildcarded path %s", path);
128 final YangInstanceIdentifier normalized = getCodec().toYangInstanceIdentifierBlocking(path);
129 getDelegate().delete(store, normalized);
132 protected final FluentFuture<? extends CommitInfo> doCommit() {
133 return getDelegate().commit();
136 protected final boolean doCancel() {
137 return getDelegate().cancel();