Adjust to DOMDataTreeCommitCohort API change
[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.FluentFuture;
12 import java.util.Collection;
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.util.concurrent.FluentFutures;
21 import org.opendaylight.yangtools.yang.common.QName;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
24 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
25 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
26 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
27 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
28 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 /**
33  * Example implementation of a DOMDataTreeCommitCohort that validates car entry data.
34  *
35  * @author Thomas Pantelis
36  */
37 public class CarEntryDataTreeCommitCohort implements DOMDataTreeCommitCohort {
38     private static final Logger LOG = LoggerFactory.getLogger(CarEntryDataTreeCommitCohort.class);
39
40     private static final QName YEAR_QNAME = QName.create(Cars.QNAME, "year").intern();
41     private static final NodeIdentifier YEAR_NODE_ID = new NodeIdentifier(YEAR_QNAME);
42
43     @Override
44     public FluentFuture<PostCanCommitStep> canCommit(Object txId, SchemaContext ctx,
45             Collection<DOMDataTreeCandidate> candidates) {
46
47         for (DOMDataTreeCandidate candidate : candidates) {
48             // Simple data validation - verify the year, if present, is >= 1990
49
50             final DataTreeCandidateNode rootNode = candidate.getRootNode();
51             final Optional<NormalizedNode<?, ?>> dataAfter = rootNode.getDataAfter();
52
53             LOG.info("In canCommit: modificationType: {}, dataBefore: {}, dataAfter: {}",
54                     rootNode.getModificationType(), rootNode.getDataBefore(), dataAfter);
55
56             // Note: we don't want to process DELETE modifications but we don't need to explicitly check the
57             // ModificationType because dataAfter will not be present. Also dataAfter *should* always contain a
58             // MapEntryNode but we verify anyway.
59             if (dataAfter.isPresent()) {
60                 final NormalizedNode<?, ?> normalizedNode = dataAfter.get();
61                 Verify.verify(normalizedNode instanceof DataContainerNode,
62                         "Expected type DataContainerNode, actual was %s", normalizedNode.getClass());
63                 DataContainerNode<?> entryNode = (DataContainerNode<?>) normalizedNode;
64                 final Optional<DataContainerChild<? extends PathArgument, ?>> possibleYear =
65                         entryNode.getChild(YEAR_NODE_ID);
66                 if (possibleYear.isPresent()) {
67                     final Number year = (Number) possibleYear.get().getValue();
68
69                     LOG.info("year is {}", year);
70
71                     if (!(year.longValue() >= 1990)) {
72                         return FluentFutures.immediateFailedFluentFuture(new DataValidationFailedException(
73                                 DOMDataTreeIdentifier.class, candidate.getRootPath(),
74                                 String.format("Invalid year %d - year must be >= 1990", year)));
75                     }
76                 }
77             }
78         }
79
80         // Return the noop PostCanCommitStep as we're only validating input data and not participating in the
81         // remaining 3PC stages (pre-commit and commit).
82         return PostCanCommitStep.NOOP_SUCCESSFUL_FUTURE;
83     }
84 }

©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.