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