Adjust to yangtools-2.0.0 changes
[controller.git] / opendaylight / md-sal / samples / clustering-test-app / provider / src / main / java / org / opendaylight / controller / clustering / it / provider / CarEntryDataTreeCommitCohort.java
1 /*
2  * Copyright (c) 2017 Brocade Communications 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.controller.clustering.it.provider;
9
10 import com.google.common.base.Verify;
11 import com.google.common.util.concurrent.CheckedFuture;
12 import com.google.common.util.concurrent.Futures;
13 import java.util.Optional;
14 import org.opendaylight.mdsal.common.api.DataValidationFailedException;
15 import org.opendaylight.mdsal.common.api.PostCanCommitStep;
16 import org.opendaylight.mdsal.dom.api.DOMDataTreeCandidate;
17 import org.opendaylight.mdsal.dom.api.DOMDataTreeCommitCohort;
18 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.Cars;
20 import org.opendaylight.yangtools.yang.common.QName;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
23 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
24 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
25 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
26 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
27 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 /**
32  * Example implementation of a DOMDataTreeCommitCohort that validates car entry data.
33  *
34  * @author Thomas Pantelis
35  */
36 public class CarEntryDataTreeCommitCohort implements DOMDataTreeCommitCohort {
37     private static final Logger LOG = LoggerFactory.getLogger(CarEntryDataTreeCommitCohort.class);
38
39     private static final QName YEAR_QNAME = QName.create(Cars.QNAME, "year").intern();
40     private static final NodeIdentifier YEAR_NODE_ID = new NodeIdentifier(YEAR_QNAME);
41
42     @Override
43     public CheckedFuture<PostCanCommitStep, DataValidationFailedException> canCommit(final Object txId,
44             final DOMDataTreeCandidate candidate, final SchemaContext ctx) {
45
46         // Simple data validation - verify the year, if present, is >= 1990
47
48         final DataTreeCandidateNode rootNode = candidate.getRootNode();
49         final Optional<NormalizedNode<?, ?>> dataAfter = rootNode.getDataAfter();
50
51         LOG.info("In canCommit: modificationType: {}, dataBefore: {}, dataAfter: {}", rootNode.getModificationType(),
52                 rootNode.getDataBefore(), dataAfter);
53
54         // Note: we don't want to process DELETE modifications but we don't need to explicitly check the
55         // ModificationType because dataAfter will not be present. Also dataAfter *should* always contain a
56         // MapEntryNode but we verify anyway.
57         if (dataAfter.isPresent()) {
58             final NormalizedNode<?, ?> normalizedNode = dataAfter.get();
59             Verify.verify(normalizedNode instanceof DataContainerNode, "Expected type DataContainerNode, actual was %s",
60                     normalizedNode.getClass());
61             DataContainerNode<?> entryNode = (DataContainerNode<?>) normalizedNode;
62             final Optional<DataContainerChild<? extends PathArgument, ?>> possibleYear =
63                     entryNode.getChild(YEAR_NODE_ID);
64             if (possibleYear.isPresent()) {
65                 final Number year = (Number) possibleYear.get().getValue();
66
67                 LOG.info("year is {}", year);
68
69                 if (!(year.longValue() >= 1990)) {
70                     return Futures.immediateFailedCheckedFuture(new DataValidationFailedException(
71                             DOMDataTreeIdentifier.class, candidate.getRootPath(),
72                                 String.format("Invalid year %d - year must be >= 1990", year)));
73                 }
74             }
75         }
76
77         // Return the noop PostCanCommitStep as we're only validating input data and not participating in the
78         // remaining 3PC stages (pre-commit and commit).
79         return PostCanCommitStep.NOOP_SUCCESS_FUTURE;
80     }
81 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.