Merge "Bug 2362: Make sure JSON and XML codecs are not losing constraints."
[yangtools.git] / yang / 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 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;
21
22 abstract class AbstractDataTreeTip implements DataTreeTip {
23     private static final YangInstanceIdentifier PUBLIC_ROOT_PATH = YangInstanceIdentifier.create(Collections.<PathArgument>emptyList());
24
25     /**
26      * Return the current root node of this tip.
27      *
28      * @return Current tip root node, may not be null.
29      */
30     @Nonnull protected abstract TreeNode getTipRoot();
31
32     @Override
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
37         m.getStrategy().checkApplicable(PUBLIC_ROOT_PATH, m.getRootModification(), Optional.of(getTipRoot()));
38     }
39
40     @Override
41     public final DataTreeCandidateTip prepare(final DataTreeModification modification) {
42         Preconditions.checkArgument(modification instanceof InMemoryDataTreeModification, "Invalid modification class %s", modification.getClass());
43
44         final InMemoryDataTreeModification m = (InMemoryDataTreeModification)modification;
45         final ModifiedNode root = m.getRootModification();
46
47         final TreeNode currentRoot = getTipRoot();
48         if (root.getOperation() == LogicalOperation.NONE) {
49             return new NoopDataTreeCandidate(PUBLIC_ROOT_PATH, root, currentRoot);
50         }
51
52         final Optional<TreeNode> newRoot = m.getStrategy().apply(m.getRootModification(),
53             Optional.<TreeNode>of(currentRoot), m.getVersion());
54         Preconditions.checkState(newRoot.isPresent(), "Apply strategy failed to produce root node");
55         return new InMemoryDataTreeCandidate(PUBLIC_ROOT_PATH, root, currentRoot, newRoot.get());
56     }
57 }