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.tree.impl;
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.tree.api.DataTreeCandidateTip;
17 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeModification;
18 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeTip;
19 import org.opendaylight.yangtools.yang.data.tree.api.DataValidationFailedException;
20 import org.opendaylight.yangtools.yang.data.tree.impl.node.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.of(), root, currentRoot);
53 final var newRoot = m.getStrategy().apply(m.getRootModification(), Optional.of(currentRoot), m.getVersion());
54 checkState(newRoot.isPresent(), "Apply strategy failed to produce root node for modification %s", modification);
55 return new InMemoryDataTreeCandidate(YangInstanceIdentifier.of(), root, currentRoot, newRoot.orElseThrow());
58 private static InMemoryDataTreeModification checkedCast(final DataTreeModification mod) {
59 checkArgument(mod instanceof InMemoryDataTreeModification, "Invalid modification class %s", mod.getClass());
60 return (InMemoryDataTreeModification)mod;