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.collect.Iterables;
12 import com.google.common.util.concurrent.CheckedFuture;
13 import com.google.common.util.concurrent.FluentFuture;
14 import com.google.common.util.concurrent.Futures;
15 import com.google.common.util.concurrent.ListenableFuture;
16 import com.google.common.util.concurrent.MoreExecutors;
17 import java.util.ArrayList;
18 import java.util.Collection;
19 import java.util.List;
20 import javax.annotation.Nonnull;
21 import org.opendaylight.mdsal.common.api.DataValidationFailedException;
22 import org.opendaylight.mdsal.common.api.MappingCheckedFuture;
23 import org.opendaylight.mdsal.common.api.PostCanCommitStep;
24 import org.opendaylight.yangtools.util.concurrent.ExceptionMapper;
25 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
26 import org.slf4j.LoggerFactory;
29 * Commit cohort participating in commit of data modification, which can validate data tree
30 * modifications, with option to reject supplied modification, and with callbacks describing state
33 * <h2>Performance implications</h2>
34 * {@link DOMDataTreeCommitCohort}s are hooked up into commit of data tree changes and MAY
35 * negatively affect performance of data broker / store.
36 * Implementations of this interface are discouraged, unless you really need ability to veto data
37 * tree changes, or to provide external state change in sync with visibility of committed data.
39 * <h2>Implementation requirements</h2>
40 * <h3>Correctness assumptions</h3> Implementation SHOULD use only the {@link DOMDataTreeCandidate} instances and
41 * provided {@link SchemaContext} for validation purposes.
42 * Use of any other external mutable state is discouraged, implementation MUST NOT use any
43 * transaction related APIs on same data broker / data store instance during invocation of
44 * callbacks, except ones provided as argument. Note that this MAY BE enforced by some
45 * implementations of {@link DOMDataBroker} or DOMDataCommitCoordinator
46 * Note that this may be enforced by some implementations of {@link DOMDataTreeCommitCohortRegistry}
47 * and such calls may fail.
48 * <h3>Correct model usage</h3> If implementation is performing YANG-model driven validation
49 * implementation SHOULD use provided schema context.
50 * Any other instance of {@link SchemaContext} obtained by other means, may not be valid for the
51 * associated DOMDataTreeCandidates and it may lead to incorrect validation or processing of provided
53 * <h3>DataTreeCandidate assumptions</h3> Implementation SHOULD NOT make any assumptions on a
54 * {@link DOMDataTreeCandidate} being successfully committed until associated
55 * {@link PostCanCommitStep#preCommit()} and
56 * {@link org.opendaylight.mdsal.common.api.PostPreCommitStep#commit()} callback was invoked.
57 * <h2>Usage patterns</h2>
58 * <h3>Data Tree Validator</h3>
59 * Validator is implementation, which only validates {@link DOMDataTreeCandidate} instances and does not
60 * retain any state derived from edited data - does not care if a {@link DOMDataTreeCandidate} was
61 * rejected afterwards or transaction was cancelled.
62 * Implementation may opt-out from receiving {@code preCommit()}, {@code commit()}, {@code abort()}
63 * callbacks by returning {@link PostCanCommitStep#NOOP}.
68 // TODO: Provide example and describe more usage patterns
70 public interface DOMDataTreeCommitCohort {
73 * DO NOT implement or invoke this method. It is deprecated in favor of
74 * {@link #canCommit(Object, Collection, SchemaContext)} and only exists for backwards compatibility. The
75 * default implementation returns {@link PostCanCommitStep#NOOP_SUCCESS_FUTURE} and is invoked by the
76 * default implementation of {@link #canCommit(Object, Collection, SchemaContext)}.
78 * @deprecated Implement and invoke {@link #canCommit(Object, Collection, SchemaContext)} instead.
82 default CheckedFuture<PostCanCommitStep, DataValidationFailedException> canCommit(@Nonnull final Object txId,
83 @Nonnull final DOMDataTreeCandidate candidate, @Nonnull final SchemaContext ctx) {
84 LoggerFactory.getLogger(getClass()).error(
85 "The default implementation of DOMDataTreeCommitCohort#canCommit(Object, DOMDataTreeCandidate, "
86 + "SchemaContext) was invoked on {}", getClass());
87 return PostCanCommitStep.NOOP_SUCCESS_FUTURE;
91 * DO NOT implement or invoke this method. It is deprecated in favor of
92 * {@link #canCommit(Object, SchemaContext, Collection)} which returns a {@link FluentFuture} instead of the
93 * deprecated {@link CheckedFuture} which is scheduled for remkoval in a future guava release. The
94 * default implementation calls {@link #canCommit(Object, SchemaContext, Collection)}.
96 * @deprecated Implement and invoke {@link #canCommit(Object, SchemaContext, Collection)} instead.
100 default CheckedFuture<PostCanCommitStep, DataValidationFailedException> canCommit(@Nonnull final Object txId,
101 @Nonnull final Collection<DOMDataTreeCandidate> candidates, @Nonnull final SchemaContext ctx) {
102 LoggerFactory.getLogger(getClass()).error(
103 "The default implementation of DOMDataTreeCommitCohort#canCommit(Object, Collection, "
104 + "SchemaContext) was invoked on {}", getClass());
106 return MappingCheckedFuture.create(canCommit(txId, ctx, candidates), new DataValidationFailedExceptionMapper(
107 "canCommit", Iterables.getLast(candidates).getRootPath()));
111 * Validates the supplied data tree modifications and associates the cohort-specific steps with data broker
115 * If {@link DataValidationFailedException} is thrown by implementation, the commit of the supplied data
116 * will be prevented, with the DataBroker transaction providing the thrown exception as the cause of failure.
119 * Note the implementations are expected to do validation and processing asynchronous. Implementations SHOULD do
120 * processing fast, and are discouraged from blocking on any external resources. Implementation MUST NOT access
121 * any data transaction related APIs during invocation of the callback. Note that this may be enforced by some
122 * implementations of {@link DOMDataTreeCommitCohortRegistry} and such calls may fail.
125 * Implementation MAY opt-out from implementing other steps by returning
126 * {@link PostCanCommitStep#NOOP}. Otherwise implementation MUST return instance of
127 * {@link PostCanCommitStep}, which will be used to invoke
128 * {@link org.opendaylight.mdsal.common.api.PostPreCommitStep#commit()} or
129 * {@link PostCanCommitStep#abort()} based on accepting data by data broker and or other commit cohorts.
131 * @param txId Transaction identifier. SHOULD be used only for reporting and correlation.
132 * Implementation MUST NOT use {@code txId} for validation.
133 * @param candidates Data Tree candidates to be validated and committed.
134 * @param ctx Schema Context to which Data Tree candidate should conform.
135 * @return a FluentFuture which will successfully complete with the user-supplied implementation of
136 * {@link PostCanCommitStep} if all candidates are valid, or a failed future with a
137 * {@link DataValidationFailedException} if and only if a provided
138 * {@link DOMDataTreeCandidate} instance did not pass validation. Users are encouraged to use
139 * more specific subclasses of this exception to provide additional information about
140 * validation failure reason.
143 default FluentFuture<PostCanCommitStep> canCommit(@Nonnull final Object txId,
144 @Nonnull final SchemaContext ctx, @Nonnull final Collection<DOMDataTreeCandidate> candidates) {
145 LoggerFactory.getLogger(getClass()).warn("DOMDataTreeCommitCohort implementation {} should override "
146 + "canCommit(Object, SchemaContext, Collection)", getClass());
148 // For backwards compatibility, the default implementation is to invoke the deprecated
149 // canCommit(Object, DOMDataTreeCandidate, SchemaContext) method for each DOMDataTreeCandidate and return the
150 // last PostCanCommitStep.
151 List<ListenableFuture<PostCanCommitStep>> futures = new ArrayList<>();
152 for (DOMDataTreeCandidate candidate : candidates) {
153 futures.add(canCommit(txId, candidate, ctx));
156 final ListenableFuture<PostCanCommitStep> resultFuture = Futures.transform(Futures.allAsList(futures),
157 input -> input.get(input.size() - 1), MoreExecutors.directExecutor());
158 return FluentFuture.from(resultFuture);
162 * An ExceptionMapper that translates an Exception to a DataValidationFailedException.
164 class DataValidationFailedExceptionMapper extends ExceptionMapper<DataValidationFailedException> {
165 private final DOMDataTreeIdentifier failedTreeId;
167 public DataValidationFailedExceptionMapper(final String opName, final DOMDataTreeIdentifier failedTreeId) {
168 super(opName, DataValidationFailedException.class);
169 this.failedTreeId = failedTreeId;
173 protected DataValidationFailedException newWithCause(final String message, final Throwable cause) {
174 return new DataValidationFailedException(DOMDataTreeIdentifier.class, failedTreeId, message, cause);