Search in sources :

Example 1 with DataValidationFailedException

use of org.opendaylight.mdsal.common.api.DataValidationFailedException in project controller by opendaylight.

the class CarEntryDataTreeCommitCohort method canCommit.

@Override
public CheckedFuture<PostCanCommitStep, DataValidationFailedException> canCommit(final Object txId, final DOMDataTreeCandidate candidate, final SchemaContext ctx) {
    // Simple data validation - verify the year, if present, is >= 1990
    final DataTreeCandidateNode rootNode = candidate.getRootNode();
    final Optional<NormalizedNode<?, ?>> dataAfter = rootNode.getDataAfter();
    LOG.info("In canCommit: modificationType: {}, dataBefore: {}, dataAfter: {}", rootNode.getModificationType(), rootNode.getDataBefore(), dataAfter);
    // MapEntryNode but we verify anyway.
    if (dataAfter.isPresent()) {
        final NormalizedNode<?, ?> normalizedNode = dataAfter.get();
        Verify.verify(normalizedNode instanceof DataContainerNode, "Expected type DataContainerNode, actual was %s", normalizedNode.getClass());
        DataContainerNode<?> entryNode = (DataContainerNode<?>) normalizedNode;
        final Optional<DataContainerChild<? extends PathArgument, ?>> possibleYear = entryNode.getChild(YEAR_NODE_ID);
        if (possibleYear.isPresent()) {
            final Number year = (Number) possibleYear.get().getValue();
            LOG.info("year is {}", year);
            if (!(year.longValue() >= 1990)) {
                return Futures.immediateFailedCheckedFuture(new DataValidationFailedException(DOMDataTreeIdentifier.class, candidate.getRootPath(), String.format("Invalid year %d - year must be >= 1990", year)));
            }
        }
    }
    // remaining 3PC stages (pre-commit and commit).
    return PostCanCommitStep.NOOP_SUCCESS_FUTURE;
}
Also used : DataValidationFailedException(org.opendaylight.mdsal.common.api.DataValidationFailedException) DataContainerChild(org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild) DOMDataTreeIdentifier(org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier) DataTreeCandidateNode(org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode) DataContainerNode(org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode) PathArgument(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument) NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode)

Aggregations

DataValidationFailedException (org.opendaylight.mdsal.common.api.DataValidationFailedException)1 DOMDataTreeIdentifier (org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier)1 PathArgument (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument)1 DataContainerChild (org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild)1 DataContainerNode (org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode)1 NormalizedNode (org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode)1 DataTreeCandidateNode (org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode)1