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 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;
22 abstract class AbstractDataTreeTip implements DataTreeTip {
23 private static final YangInstanceIdentifier PUBLIC_ROOT_PATH = YangInstanceIdentifier.create(Collections.<PathArgument>emptyList());
26 * Return the current root node of this tip.
28 * @return Current tip root node, may not be null.
30 @Nonnull protected abstract TreeNode getTipRoot();
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);
38 m.getStrategy().checkApplicable(PUBLIC_ROOT_PATH, m.getRootModification(), Optional.of(getTipRoot()));
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);
47 final ModifiedNode root = m.getRootModification();
49 final TreeNode currentRoot = getTipRoot();
50 if (root.getOperation() == LogicalOperation.NONE) {
51 return new NoopDataTreeCandidate(PUBLIC_ROOT_PATH, root, currentRoot);
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());