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