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