2 * Copyright (c) 2015, 2016 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.Nonnull;
17 import javax.annotation.Nullable;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
19 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
20 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
21 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
23 abstract class AbstractDataTreeCandidateNode implements DataTreeCandidateNode {
24 private static final Function<NormalizedNode<?, ?>, DataTreeCandidateNode> TO_DELETED_NODE = new Function<NormalizedNode<?, ?>, DataTreeCandidateNode>() {
26 public DataTreeCandidateNode apply(final NormalizedNode<?, ?> input) {
27 return AbstractRecursiveCandidateNode.deleteNode(input);
30 private static final Function<NormalizedNode<?, ?>, DataTreeCandidateNode> TO_WRITTEN_NODE = new Function<NormalizedNode<?, ?>, DataTreeCandidateNode>() {
32 public DataTreeCandidateNode apply(final NormalizedNode<?, ?> input) {
33 return AbstractRecursiveCandidateNode.writeNode(input);
37 private static Optional<NormalizedNode<?, ?>> getChild(final NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>> container, final PathArgument identifier) {
38 if (container != null) {
39 return container.getChild(identifier);
41 return Optional.absent();
45 static DataTreeCandidateNode deltaChild(
46 final NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>> oldData,
47 final NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>> newData, final PathArgument identifier) {
49 final Optional<NormalizedNode<?, ?>> maybeNewChild = getChild(newData, identifier);
50 final Optional<NormalizedNode<?, ?>> maybeOldChild = getChild(oldData, identifier);
51 if (maybeOldChild.isPresent()) {
52 final NormalizedNode<?, ?> oldChild = maybeOldChild.get();
53 if (maybeNewChild.isPresent()) {
54 return AbstractRecursiveCandidateNode.replaceNode(oldChild, maybeNewChild.get());
56 return TO_DELETED_NODE.apply(oldChild);
59 if (maybeNewChild.isPresent()) {
60 return TO_WRITTEN_NODE.apply(maybeNewChild.get());
67 static Collection<DataTreeCandidateNode> deltaChildren(@Nullable final NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>> oldData,
68 @Nullable final NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>> newData) {
69 Preconditions.checkArgument(newData != null || oldData != null,
70 "No old or new data, modification type should be NONE and deltaChildren() mustn't be called.");
71 if (newData == null) {
72 return Collections2.transform(oldData.getValue(), TO_DELETED_NODE);
74 if (oldData == null) {
75 return Collections2.transform(newData.getValue(), TO_WRITTEN_NODE);
79 * This is slightly inefficient, as it requires N*F(M)+M*F(N) lookup operations, where
80 * F is dependent on the implementation of NormalizedNodeContainer.getChild().
82 * We build the return collection by iterating over new data and looking each child up
83 * in old data. Based on that we construct replaced/written nodes. We then proceed to
84 * iterate over old data and looking up each child in new data.
86 final Collection<DataTreeCandidateNode> result = new ArrayList<>();
87 for (NormalizedNode<?, ?> child : newData.getValue()) {
88 final DataTreeCandidateNode node;
89 final Optional<NormalizedNode<?, ?>> maybeOldChild = oldData.getChild(child.getIdentifier());
91 if (maybeOldChild.isPresent()) {
92 // This does not find children which have not in fact been modified, as doing that
93 // reliably would require us running a full equals() on the two nodes.
94 node = AbstractRecursiveCandidateNode.replaceNode(maybeOldChild.get(), child);
96 node = AbstractRecursiveCandidateNode.writeNode(child);
102 // Process removals next, looking into new data to see if we processed it
103 for (NormalizedNode<?, ?> child : oldData.getValue()) {
104 if (!newData.getChild(child.getIdentifier()).isPresent()) {
105 result.add(AbstractRecursiveCandidateNode.deleteNode(child));
112 private final NormalizedNodeContainer<?, PathArgument, NormalizedNode<?,?>> data;
114 protected AbstractDataTreeCandidateNode(final NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>> data) {
115 this.data = Preconditions.checkNotNull(data);
118 protected final Optional<NormalizedNode<?, ?>> dataOptional() {
119 return Optional.of(data);
124 public final PathArgument getIdentifier() {
125 return data.getIdentifier();
128 protected final NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>> getData() {
133 public String toString() {
134 return this.getClass().getSimpleName() + "{data = " + this.data + "}";