Bug 1435: Introduced DOM Data Commit Cohort & Validation APIs
[mdsal.git] / dom / mdsal-dom-api / src / main / java / org / opendaylight / mdsal / dom / api / DOMDataTreeCommitCohort.java
1 /*
2  * Copyright (c) 2015 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.dom.api;
9
10 import com.google.common.annotations.Beta;
11 import com.google.common.util.concurrent.CheckedFuture;
12 import javax.annotation.Nonnull;
13 import org.opendaylight.mdsal.common.api.DataValidationFailedException;
14 import org.opendaylight.mdsal.common.api.PostCanCommitStep;
15 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
16 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
17
18 /**
19  *
20  * Commit cohort participating in commit of data modification, which can validate data tree
21  * modifications, with option to reject supplied modification, and with callbacks describing state
22  * of commit.
23  *
24  * <h2>Performance implications</h2>
25  *
26  * {@link DOMDataTreeCommitCohort}s are hooked up into commit of data tree changes and MAY
27  * negatively affect performance of data broker / store.
28  *
29  * Implementations of this interface are discouraged, unless you really need ability to veto data
30  * tree changes, or to provide external state change in sync with visibility of commited data.
31  *
32  *
33  * <h2>Implementation requirements</h2>
34  *
35  * <h3>Correctness assumptions</h3> Implementation SHOULD use only {@link DataTreeCandidate} and
36  * provided {@link SchemaContext} for validation purposes.
37  *
38  * Use of any other external mutable state is discouraged, implementation MUST NOT use any
39  * transaction related APIs on same data broker / data store instance during invocation of
40  * callbacks, except ones provided as argument. Note that this MAY BE enforced by some
41  * implementations of {@link DOMDataBroker} or DOMDataCommitCoordinator
42  *
43  * Note that this may be enforced by some implementations of {@link DOMDataTreeCommitCohortRegistry}
44  * and such calls may fail.
45  *
46  * <h3>Correct model usage</h3> If implementation is performing YANG-model driven validation
47  * implementation SHOULD use provided schema context.
48  *
49  * Any other instance of {@link SchemaContext} obtained by other means, may not be valid for
50  * associated DataTreeCandidate and it may lead to incorrect validation or processing of provided
51  * data.
52  *
53  * <h3>DataTreeCandidate assumptions</h3> Implementation SHOULD NOT make any assumptions on
54  * {@link DataTreeCandidate} being successfully committed until associated
55  * {@link PostCanCommitStep#preCommit()} and
56  * {@link org.opendaylight.mdsal.common.api.PostPreCommitStep#commit()} callback was invoked.
57  *
58  *
59  * <h2>Usage patterns</h2>
60  *
61  * <h3>Data Tree Validator</h3>
62  *
63  * Validator is implementation, which only validates {@link DataTreeCandidate} and does not retain
64  * any state derived from edited data - does not care if {@link DataTreeCandidate} was rejected
65  * afterwards or transaction was cancelled.
66  *
67  * Implementation may opt-out from receiving {@code preCommit()}, {@code commit()}, {@code abort()}
68  * callbacks by returning {@link PostCanCommitStep#NOOP}.
69  *
70  * TODO: Provide example and describe more usage patterns
71  *
72  * @author Tony Tkacik &lt;ttkacik@cisco.com&gt;
73  *
74  */
75 @Beta
76 public interface DOMDataTreeCommitCohort {
77
78     /**
79      * Validates supplied data tree candidate and associates cohort-specific steps with data broker
80      * transaction.
81      *
82      * If {@link DataValidationFailedException} is thrown by implementation, commit of supplied data
83      * will be prevented, with the DataBroker transaction providing the thrown exception as the
84      * cause of failure.
85      *
86      * Note the implementations are expected to do validation and processing asynchronous.
87      *
88      * Implementations SHOULD do processing fast, and are discouraged SHOULD NOT block on any
89      * external resources.
90      *
91      * Implementation MUST NOT access any data transaction related APIs during invocation of
92      * callback. Note that this may be enforced by some implementations of
93      * {@link DOMDataTreeCommitCohortRegistry} and such calls may fail.
94      *
95      * Implementation MAY opt-out from implementing other steps by returning
96      * {@link PostCanCommitStep#NOOP}. Otherwise implementation MUST return instance of
97      * {@link PostCanCommitStep}, which will be used to invoke
98      * {@link org.opendaylight.mdsal.common.api.PostPreCommitStep#commit()} or
99      * {@link PostCanCommitStep#abort()} based on accepting data by data broker and or other commit
100      * cohorts.
101      *
102      * @param txId Transaction identifier. SHOULD be used only for reporting and correlation.
103      *        Implementation MUST NOT use {@code txId} for validation.
104      * @param candidate Data Tree candidate to be validated and committed.
105      * @param ctx Schema Context to which Data Tree candidate should conform.
106      * @return Checked future which will successfully complete with user-supplied implementation of
107      *         {@link PostCanCommitStep} if data are valid, or failed check future with
108      *         {@link DataValidationFailedException} if and only if provided
109      *         {@link DOMDataTreeCandidate} did not pass validation. Users are encouraged to use
110      *         more specific subclasses of this exception to provide additional information about
111      *         validation failure reason.
112      */
113     @Nonnull
114     CheckedFuture<PostCanCommitStep, DataValidationFailedException> canCommit(@Nonnull Object txId,
115             @Nonnull DOMDataTreeCandidate candidate, @Nonnull SchemaContext ctx);
116 }