Update WriteOperations/TypedTransaction API design
[mdsal.git] / binding / mdsal-binding-api / src / main / java / org / opendaylight / mdsal / binding / api / WriteOperations.java
1 /*
2  * Copyright (c) 2018 Pantheon Technologies, s.r.o. 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.api;
9
10 import com.google.common.annotations.Beta;
11 import org.eclipse.jdt.annotation.NonNull;
12 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
13 import org.opendaylight.yangtools.yang.binding.DataObject;
14 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
15
16 /**
17  * Write-like operations supported by {@link WriteTransaction} and {@link ReadWriteTransaction}. This interface defines
18  * the operations without a tie-in with lifecycle management.
19  */
20 public interface WriteOperations {
21     /**
22      * Stores a piece of data at the specified path. This acts as an add / replace operation, which is to say that
23      * whole subtree will be replaced by the specified data.
24      *
25      * <p>
26      * If you need to make sure that a parent object exists but you do not want modify its pre-existing state by using
27      * put, consider using {@link #merge} instead.
28      *
29      * @param store the logical data store which should be modified
30      * @param path the data object path
31      * @param data the data object to be written to the specified path
32      * @throws IllegalStateException if the transaction has already been submitted
33      * @throws NullPointerException if any of the arguments is null
34      */
35     <T extends DataObject> void put(@NonNull LogicalDatastoreType store, @NonNull InstanceIdentifier<T> path,
36             @NonNull T data);
37
38     /**
39      * Stores a piece of data at the specified path. This acts as an add / replace operation, which is to say that whole
40      * subtree will be replaced by the specified data.
41      *
42      * <p>
43      * If you need to make sure that a parent object exists but you do not want modify its pre-existing state by using
44      * put, consider using {@link #merge} instead.
45      *
46      * <b>WARNING:</b> Using this method may introduce garbage in data store, or recreate nodes, which were deleted by
47      *                 a previous transaction. It is not necessary in most scenarios and has a significantly higher cost
48      *                 than {@link #put(LogicalDatastoreType, InstanceIdentifier, DataObject)} and should only be used
49      *                 when absolutely necessary.
50      *
51      * @param store the logical data store which should be modified
52      * @param path the data object path
53      * @param data the data object to be written to the specified path
54      * @param createMissingParents if {@link #CREATE_MISSING_PARENTS}, any missing parent nodes will be automatically
55      *                             created using a merge operation. <b>WARNING:</b> using this option is not needed
56      *                             in most scenarios and has a significant performance cost and should be avoided
57      *                             whenever possible.
58      * @throws IllegalStateException if the transaction has already been submitted
59      * @throws NullPointerException if any of the arguments is null
60      * @deprecated Use {@link #put(LogicalDatastoreType, InstanceIdentifier, DataObject)} or
61      *             {@link #mergeParentStructurePut(LogicalDatastoreType, InstanceIdentifier, DataObject)}
62      *             instead.
63      */
64     @Deprecated
65     <T extends DataObject> void put(@NonNull LogicalDatastoreType store, @NonNull InstanceIdentifier<T> path,
66             @NonNull T data, boolean createMissingParents);
67
68     /**
69      * Stores a piece of data at the specified path. This acts as an add / replace operation, which is to say that whole
70      * subtree will be replaced by the specified data. Unlike
71      * {@link #put(LogicalDatastoreType, InstanceIdentifier, DataObject)}, this method will attempt to create
72      * semantically-significant parent nodes, like list entries and presence containers, as indicated by {@code path}.
73      *
74      * <p>
75      * If you need to make sure that a parent object exists but you do not want modify its pre-existing state by using
76      * put, consider using {@link #merge} instead.
77      *
78      * <p>
79      * <b>WARNING:</b> Using this method may introduce garbage in data store, or recreate nodes, which were deleted by
80      *                 a previous transaction. It also has a significantly higher cost than
81      *                 {@link #put(LogicalDatastoreType, InstanceIdentifier, DataObject)} and should only be used when
82      *                 absolutely necessary.
83      *
84      * @param store the logical data store which should be modified
85      * @param path the data object path
86      * @param data the data object to be written to the specified path
87      * @throws IllegalStateException if the transaction has already been submitted
88      * @throws NullPointerException if any of the arguments is null
89      */
90     // FIXME: 4.0.0: make this method non-default
91     // TODO: can we come up with a better name?
92     @Beta
93     default <T extends DataObject> void mergeParentStructurePut(@NonNull final LogicalDatastoreType store,
94             @NonNull final InstanceIdentifier<T> path, @NonNull final T data) {
95         put(store, path, data, CREATE_MISSING_PARENTS);
96     }
97
98     /**
99      * Merges a piece of data with the existing data at a specified path. Any pre-existing data which is not explicitly
100      * overwritten will be preserved. This means that if you store a container, its child lists will be merged.
101      *
102      * <p>
103      * If you require an explicit replace operation, use {@link #put} instead.
104      *
105      * @param store the logical data store which should be modified
106      * @param path the data object path
107      * @param data the data object to be merged to the specified path
108      * @throws IllegalStateException if the transaction has already been submitted
109      * @throws NullPointerException if any of the arguments is null
110      */
111     <T extends DataObject> void merge(@NonNull LogicalDatastoreType store, @NonNull InstanceIdentifier<T> path,
112             @NonNull T data);
113
114     /**
115      * Merges a piece of data with the existing data at a specified path. Any pre-existing data which is not explicitly
116      * overwritten will be preserved. This means that if you store a container, its child lists will be merged.
117      *
118      * <p>
119      * If you require an explicit replace operation, use {@link #put} instead.
120      *
121      * @param store the logical data store which should be modified
122      * @param path the data object path
123      * @param data the data object to be merged to the specified path
124      * @param createMissingParents if {@link #CREATE_MISSING_PARENTS}, any missing parent nodes will be automatically
125      *                             created using a merge operation. <b>WARNING:</b> using this option is not needed
126      *                             in most scenarios and has a significant performance cost and should be avoided
127      *                             whenever possible.
128      * @throws IllegalStateException if the transaction has already been submitted
129      * @throws NullPointerException if any of the arguments is null
130      * @deprecated Use {@link #merge(LogicalDatastoreType, InstanceIdentifier, DataObject)} or
131      *             {@link #mergeParentStructureMerge(LogicalDatastoreType, InstanceIdentifier, DataObject)}
132      *             instead.
133      */
134     @Deprecated
135     <T extends DataObject> void merge(@NonNull LogicalDatastoreType store, @NonNull InstanceIdentifier<T> path,
136             @NonNull T data, boolean createMissingParents);
137
138     /**
139      * Merges a piece of data with the existing data at a specified path. Any pre-existing data which is not explicitly
140      * overwritten will be preserved. This means that if you store a container, its child lists will be merged. Unlike
141      * {@link #merge(LogicalDatastoreType, InstanceIdentifier, DataObject)}, this method will attempt to create
142      * semantically-significant parent nodes, like list entries and presence containers, as indicated by {@code path}.
143      *
144      * <p>
145      * If you require an explicit replace operation, use {@link #put} instead.
146      *
147      * <p>
148      * <b>WARNING:</b> Using this method may introduce garbage in data store, or recreate nodes, which were deleted by
149      *                 a previous transaction. It is not necessary in most scenarios and has a significantly higher cost
150      *                 than {@link #merge(LogicalDatastoreType, InstanceIdentifier, DataObject)}. It should only be used
151      *                 when absolutely necessary.
152      *
153      * @param store the logical data store which should be modified
154      * @param path the data object path
155      * @param data the data object to be merged to the specified path
156      * @throws IllegalStateException if the transaction has already been submitted
157      * @throws NullPointerException if any of the arguments is null
158      */
159     // FIXME: 4.0.0: make this method non-default
160     // TODO: can we come up with a better name?
161     @Beta
162     default <T extends DataObject> void mergeParentStructureMerge(@NonNull final LogicalDatastoreType store,
163             @NonNull final InstanceIdentifier<T> path, @NonNull final T data) {
164         merge(store, path, data, CREATE_MISSING_PARENTS);
165     }
166
167     /**
168      * Removes a piece of data from specified path. This operation does not fail if the specified path does not exist.
169      *
170      * @param store Logical data store which should be modified
171      * @param path Data object path
172      * @throws IllegalStateException if the transaction was committed or canceled.
173      */
174     void delete(@NonNull LogicalDatastoreType store, @NonNull InstanceIdentifier<?> path);
175
176     /**
177      * Flag value indicating that missing parents should be created.
178      *
179      * <p>
180      * <b>WARNING:</b> Using this flag may introduce garbage in data store, or recreate nodes, which were deleted by
181      *                 a previous transaction. It is not necessary in most scenarios and also has a significantly higher
182      *                 cost than {@link #FAIL_ON_MISSING_PARENTS} and should only be used when absolutely necessary.
183      *
184      * @deprecated To be removed with {@link #merge(LogicalDatastoreType, InstanceIdentifier, DataObject, boolean)}
185      *             and {@link #put(LogicalDatastoreType, InstanceIdentifier, DataObject, boolean)}.
186      */
187     @Deprecated
188     boolean CREATE_MISSING_PARENTS = true;
189
190     /**
191      * Flag value indicating that missing parents should cause an error.
192      *
193      * @deprecated To be removed with {@link #merge(LogicalDatastoreType, InstanceIdentifier, DataObject, boolean)}
194      *             and {@link #put(LogicalDatastoreType, InstanceIdentifier, DataObject, boolean)}.
195      */
196     @Deprecated
197     boolean FAIL_ON_MISSING_PARENTS = false;
198 }