Populate data/ hierarchy
[yangtools.git] / data / 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 static com.google.common.base.Preconditions.checkArgument;
11 import static com.google.common.base.Preconditions.checkState;
12
13 import java.util.Optional;
14 import org.eclipse.jdt.annotation.NonNull;
15 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
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.spi.tree.TreeNode;
21
22 abstract class AbstractDataTreeTip implements DataTreeTip {
23     /**
24      * Return the current root node of this tip.
25      *
26      * @return Current tip root node, may not be null.
27      */
28     protected abstract @NonNull TreeNode getTipRoot();
29
30     abstract @NonNull YangInstanceIdentifier getRootPath();
31
32     @Override
33     public final void validate(final DataTreeModification modification) throws DataValidationFailedException {
34         final InMemoryDataTreeModification m = checkedCast(modification);
35         checkArgument(m.isSealed(), "Attempted to verify unsealed modification %s", m);
36
37         m.getStrategy().checkApplicable(new ModificationPath(getRootPath()), m.getRootModification(),
38             Optional.of(getTipRoot()), m.getVersion());
39     }
40
41     @Override
42     public final DataTreeCandidateTip prepare(final DataTreeModification modification) {
43         final InMemoryDataTreeModification m = checkedCast(modification);
44         checkArgument(m.isSealed(), "Attempted to prepare unsealed modification %s", m);
45
46         final ModifiedNode root = m.getRootModification();
47
48         final TreeNode currentRoot = getTipRoot();
49         if (root.getOperation() == LogicalOperation.NONE) {
50             return new NoopDataTreeCandidate(YangInstanceIdentifier.empty(), root, currentRoot);
51         }
52
53         final Optional<? extends TreeNode> newRoot = m.getStrategy().apply(m.getRootModification(),
54             Optional.of(currentRoot), m.getVersion());
55         checkState(newRoot.isPresent(), "Apply strategy failed to produce root node for modification %s", modification);
56         return new InMemoryDataTreeCandidate(YangInstanceIdentifier.empty(), root, currentRoot, newRoot.get());
57     }
58
59     private static InMemoryDataTreeModification checkedCast(final DataTreeModification mod) {
60         checkArgument(mod instanceof InMemoryDataTreeModification, "Invalid modification class %s", mod.getClass());
61         return (InMemoryDataTreeModification)mod;
62     }
63 }