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 com.google.common.base.Optional;
11 import com.google.common.base.Preconditions;
12 import javax.annotation.Nonnull;
13 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
14 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateTip;
15 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
16 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeTip;
17 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
18 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNode;
20 abstract class AbstractDataTreeTip implements DataTreeTip {
22 * Return the current root node of this tip.
24 * @return Current tip root node, may not be null.
26 @Nonnull protected abstract TreeNode getTipRoot();
29 public final void validate(final DataTreeModification modification) throws DataValidationFailedException {
30 Preconditions.checkArgument(modification instanceof InMemoryDataTreeModification,
31 "Invalid modification class %s", modification.getClass());
32 final InMemoryDataTreeModification m = (InMemoryDataTreeModification)modification;
33 Preconditions.checkArgument(m.isSealed(), "Attempted to verify unsealed modification %s", m);
35 m.getStrategy().checkApplicable(YangInstanceIdentifier.EMPTY, m.getRootModification(),
36 Optional.of(getTipRoot()), m.getVersion());
40 public final DataTreeCandidateTip prepare(final DataTreeModification modification) {
41 Preconditions.checkArgument(modification instanceof InMemoryDataTreeModification,
42 "Invalid modification class %s", modification.getClass());
43 final InMemoryDataTreeModification m = (InMemoryDataTreeModification)modification;
44 Preconditions.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<TreeNode> newRoot = m.getStrategy().apply(m.getRootModification(),
54 Optional.of(currentRoot), m.getVersion());
55 Preconditions.checkState(newRoot.isPresent(), "Apply strategy failed to produce root node for modification %s",
57 return new InMemoryDataTreeCandidate(YangInstanceIdentifier.EMPTY, root, currentRoot, newRoot.get());