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 javax.annotation.Nullable;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
18 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
19 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
20 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
22 abstract class AbstractDataTreeCandidateNode implements DataTreeCandidateNode {
23 private static final Function<NormalizedNode<?, ?>, DataTreeCandidateNode> TO_DELETED_NODE = new Function<NormalizedNode<?, ?>, DataTreeCandidateNode>() {
25 public DataTreeCandidateNode apply(final NormalizedNode<?, ?> input) {
26 return AbstractRecursiveCandidateNode.deleteNode(input);
29 private static final Function<NormalizedNode<?, ?>, DataTreeCandidateNode> TO_WRITTEN_NODE = new Function<NormalizedNode<?, ?>, DataTreeCandidateNode>() {
31 public DataTreeCandidateNode apply(final NormalizedNode<?, ?> input) {
32 return AbstractRecursiveCandidateNode.writeNode(input);
36 private static Optional<NormalizedNode<?, ?>> getChild(final NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>> container, final PathArgument identifier) {
37 if (container != null) {
38 return container.getChild(identifier);
40 return Optional.absent();
44 static DataTreeCandidateNode deltaChild(
45 final NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>> oldData,
46 final NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>> newData, final PathArgument identifier) {
48 final Optional<NormalizedNode<?, ?>> maybeNewChild = getChild(newData, identifier);
49 final Optional<NormalizedNode<?, ?>> maybeOldChild = getChild(oldData, identifier);
50 if (maybeOldChild.isPresent()) {
51 final NormalizedNode<?, ?> oldChild = maybeOldChild.get();
52 if (maybeNewChild.isPresent()) {
53 return AbstractRecursiveCandidateNode.replaceNode(oldChild, maybeNewChild.get());
55 return TO_DELETED_NODE.apply(oldChild);
58 if (maybeNewChild.isPresent()) {
59 return TO_WRITTEN_NODE.apply(maybeNewChild.get());
66 static Collection<DataTreeCandidateNode> deltaChildren(@Nullable final NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>> oldData,
67 @Nullable final NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>> newData) {
68 if (newData == null) {
69 return Collections2.transform(oldData.getValue(), TO_DELETED_NODE);
71 if (oldData == null) {
72 return Collections2.transform(newData.getValue(), TO_WRITTEN_NODE);
76 * This is slightly inefficient, as it requires N*F(M)+M*F(N) lookup operations, where
77 * F is dependent on the implementation of NormalizedNodeContainer.getChild().
79 * We build the return collection by iterating over new data and looking each child up
80 * in old data. Based on that we construct replaced/written nodes. We then proceed to
81 * iterate over old data and looking up each child in new data.
83 final Collection<DataTreeCandidateNode> result = new ArrayList<>();
84 for (NormalizedNode<?, ?> child : newData.getValue()) {
85 final DataTreeCandidateNode node;
86 final Optional<NormalizedNode<?, ?>> maybeOldChild = oldData.getChild(child.getIdentifier());
88 if (maybeOldChild.isPresent()) {
89 // This does not find children which have not in fact been modified, as doing that
90 // reliably would require us running a full equals() on the two nodes.
91 node = AbstractRecursiveCandidateNode.replaceNode(maybeOldChild.get(), child);
93 node = AbstractRecursiveCandidateNode.writeNode(child);
99 // Process removals next, looking into new data to see if we processed it
100 for (NormalizedNode<?, ?> child : oldData.getValue()) {
101 if (!newData.getChild(child.getIdentifier()).isPresent()) {
102 result.add(AbstractRecursiveCandidateNode.deleteNode(child));
109 private final NormalizedNodeContainer<?, PathArgument, NormalizedNode<?,?>> data;
111 protected AbstractDataTreeCandidateNode(final NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>> data) {
112 this.data = Preconditions.checkNotNull(data);
115 protected final Optional<NormalizedNode<?, ?>> dataOptional() {
116 return Optional.<NormalizedNode<?, ?>>of(data);
120 public final PathArgument getIdentifier() {
121 return data.getIdentifier();
124 protected final NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>> getData() {