BUG-4684: validate changes against effective state
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / tree / AbstractDataTreeTip.java
1 /*
2  * Copyright (c) 2015 Cisco 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.yangtools.yang.data.impl.schema.tree;
9
10 import com.google.common.base.Optional;
11 import com.google.common.base.Preconditions;
12 import java.util.Collections;
13 import javax.annotation.Nonnull;
14 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
15 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
16 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateTip;
17 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
18 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeTip;
19 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
20 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNode;
21
22 abstract class AbstractDataTreeTip implements DataTreeTip {
23     private static final YangInstanceIdentifier PUBLIC_ROOT_PATH = YangInstanceIdentifier.create(Collections.<PathArgument>emptyList());
24
25     /**
26      * Return the current root node of this tip.
27      *
28      * @return Current tip root node, may not be null.
29      */
30     @Nonnull protected abstract TreeNode getTipRoot();
31
32     @Override
33     public final void validate(final DataTreeModification modification) throws DataValidationFailedException {
34         Preconditions.checkArgument(modification instanceof InMemoryDataTreeModification, "Invalid modification class %s", modification.getClass());
35         final InMemoryDataTreeModification m = (InMemoryDataTreeModification)modification;
36         Preconditions.checkArgument(m.isSealed(), "Attempted to verify unsealed modification %s", m);
37
38         m.getStrategy().checkApplicable(PUBLIC_ROOT_PATH, m.getRootModification(), Optional.of(getTipRoot()), m.getVersion());
39     }
40
41     @Override
42     public final DataTreeCandidateTip prepare(final DataTreeModification modification) {
43         Preconditions.checkArgument(modification instanceof InMemoryDataTreeModification, "Invalid modification class %s", modification.getClass());
44         final InMemoryDataTreeModification m = (InMemoryDataTreeModification)modification;
45         Preconditions.checkArgument(m.isSealed(), "Attempted to prepare unsealed modification %s", m);
46
47         final ModifiedNode root = m.getRootModification();
48
49         final TreeNode currentRoot = getTipRoot();
50         if (root.getOperation() == LogicalOperation.NONE) {
51             return new NoopDataTreeCandidate(PUBLIC_ROOT_PATH, root, currentRoot);
52         }
53
54         final Optional<TreeNode> newRoot = m.getStrategy().apply(m.getRootModification(),
55             Optional.<TreeNode>of(currentRoot), m.getVersion());
56         Preconditions.checkState(newRoot.isPresent(), "Apply strategy failed to produce root node for modification %s", modification);
57         return new InMemoryDataTreeCandidate(PUBLIC_ROOT_PATH, root, currentRoot, newRoot.get());
58     }
59 }