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.Function;
11 import com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
13 import com.google.common.collect.Collections2;
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.Collections;
17 import javax.annotation.Nonnull;
18 import javax.annotation.Nullable;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
20 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
21 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
22 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
24 abstract class AbstractDataTreeCandidateNode implements DataTreeCandidateNode {
25 private static final Function<NormalizedNode<?, ?>, DataTreeCandidateNode> TO_DELETED_NODE = new Function<NormalizedNode<?, ?>, DataTreeCandidateNode>() {
27 public DataTreeCandidateNode apply(final NormalizedNode<?, ?> input) {
28 return AbstractRecursiveCandidateNode.deleteNode(input);
31 private static final Function<NormalizedNode<?, ?>, DataTreeCandidateNode> TO_WRITTEN_NODE = new Function<NormalizedNode<?, ?>, DataTreeCandidateNode>() {
33 public DataTreeCandidateNode apply(final NormalizedNode<?, ?> input) {
34 return AbstractRecursiveCandidateNode.writeNode(input);
38 private static Optional<NormalizedNode<?, ?>> getChild(final NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>> container, final PathArgument identifier) {
39 if (container != null) {
40 return container.getChild(identifier);
42 return Optional.absent();
46 static DataTreeCandidateNode deltaChild(
47 final NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>> oldData,
48 final NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>> newData, final PathArgument identifier) {
50 final Optional<NormalizedNode<?, ?>> maybeNewChild = getChild(newData, identifier);
51 final Optional<NormalizedNode<?, ?>> maybeOldChild = getChild(oldData, identifier);
52 if (maybeOldChild.isPresent()) {
53 final NormalizedNode<?, ?> oldChild = maybeOldChild.get();
54 if (maybeNewChild.isPresent()) {
55 return AbstractRecursiveCandidateNode.replaceNode(oldChild, maybeNewChild.get());
57 return TO_DELETED_NODE.apply(oldChild);
60 if (maybeNewChild.isPresent()) {
61 return TO_WRITTEN_NODE.apply(maybeNewChild.get());
68 static Collection<DataTreeCandidateNode> deltaChildren(@Nullable final NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>> oldData,
69 @Nullable final NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>> newData) {
70 Preconditions.checkArgument(newData != null || oldData != null,
71 "No old or new data, modification type should be NONE and deltaChildren() mustn't be called.");
72 if (newData == null) {
73 return Collections2.transform(oldData.getValue(), TO_DELETED_NODE);
75 if (oldData == null) {
76 return Collections2.transform(newData.getValue(), TO_WRITTEN_NODE);
80 * This is slightly inefficient, as it requires N*F(M)+M*F(N) lookup operations, where
81 * F is dependent on the implementation of NormalizedNodeContainer.getChild().
83 * We build the return collection by iterating over new data and looking each child up
84 * in old data. Based on that we construct replaced/written nodes. We then proceed to
85 * iterate over old data and looking up each child in new data.
87 final Collection<DataTreeCandidateNode> result = new ArrayList<>();
88 for (NormalizedNode<?, ?> child : newData.getValue()) {
89 final DataTreeCandidateNode node;
90 final Optional<NormalizedNode<?, ?>> maybeOldChild = oldData.getChild(child.getIdentifier());
92 if (maybeOldChild.isPresent()) {
93 // This does not find children which have not in fact been modified, as doing that
94 // reliably would require us running a full equals() on the two nodes.
95 node = AbstractRecursiveCandidateNode.replaceNode(maybeOldChild.get(), child);
97 node = AbstractRecursiveCandidateNode.writeNode(child);
103 // Process removals next, looking into new data to see if we processed it
104 for (NormalizedNode<?, ?> child : oldData.getValue()) {
105 if (!newData.getChild(child.getIdentifier()).isPresent()) {
106 result.add(AbstractRecursiveCandidateNode.deleteNode(child));
113 private final NormalizedNodeContainer<?, PathArgument, NormalizedNode<?,?>> data;
115 protected AbstractDataTreeCandidateNode(final NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>> data) {
116 this.data = Preconditions.checkNotNull(data);
119 protected final Optional<NormalizedNode<?, ?>> dataOptional() {
120 return Optional.<NormalizedNode<?, ?>>of(data);
125 public final PathArgument getIdentifier() {
126 return data.getIdentifier();
129 protected final NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>> getData() {