2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.data.impl.schema.tree;
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static com.google.common.base.Preconditions.checkState;
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.api.schema.tree.spi.TreeNode;
22 abstract class AbstractDataTreeTip implements DataTreeTip {
24 * Return the current root node of this tip.
26 * @return Current tip root node, may not be null.
28 protected abstract @NonNull TreeNode getTipRoot();
30 abstract @NonNull YangInstanceIdentifier getRootPath();
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);
37 m.getStrategy().checkApplicable(new ModificationPath(getRootPath()), m.getRootModification(),
38 Optional.of(getTipRoot()), m.getVersion());
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);
46 final ModifiedNode root = m.getRootModification();
48 final TreeNode currentRoot = getTipRoot();
49 if (root.getOperation() == LogicalOperation.NONE) {
50 return new NoopDataTreeCandidate(YangInstanceIdentifier.empty(), root, currentRoot);
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());
59 private static InMemoryDataTreeModification checkedCast(final DataTreeModification mod) {
60 checkArgument(mod instanceof InMemoryDataTreeModification, "Invalid modification class %s", mod.getClass());
61 return (InMemoryDataTreeModification)mod;