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.Optional;
11 import com.google.common.base.Preconditions;
12 import com.google.common.collect.Collections2;
13 import java.util.ArrayList;
14 import java.util.Collection;
15 import javax.annotation.Nonnull;
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 Optional<NormalizedNode<?, ?>> getChild(
24 final NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>> container,
25 final PathArgument identifier) {
26 if (container != null) {
27 return container.getChild(identifier);
29 return Optional.absent();
33 static DataTreeCandidateNode deltaChild(
34 final NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>> oldData,
35 final NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>> newData, final PathArgument identifier) {
37 final Optional<NormalizedNode<?, ?>> maybeNewChild = getChild(newData, identifier);
38 final Optional<NormalizedNode<?, ?>> maybeOldChild = getChild(oldData, identifier);
39 if (maybeOldChild.isPresent()) {
40 final NormalizedNode<?, ?> oldChild = maybeOldChild.get();
41 if (maybeNewChild.isPresent()) {
42 return AbstractRecursiveCandidateNode.replaceNode(oldChild, maybeNewChild.get());
44 return AbstractRecursiveCandidateNode.deleteNode(oldChild);
47 if (maybeNewChild.isPresent()) {
48 return AbstractRecursiveCandidateNode.writeNode(maybeNewChild.get());
55 static Collection<DataTreeCandidateNode> deltaChildren(@Nullable final NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>> oldData,
56 @Nullable final NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>> newData) {
57 Preconditions.checkArgument(newData != null || oldData != null,
58 "No old or new data, modification type should be NONE and deltaChildren() mustn't be called.");
59 if (newData == null) {
60 return Collections2.transform(oldData.getValue(), AbstractRecursiveCandidateNode::deleteNode);
62 if (oldData == null) {
63 return Collections2.transform(newData.getValue(), AbstractRecursiveCandidateNode::writeNode);
67 * This is slightly inefficient, as it requires N*F(M)+M*F(N) lookup operations, where
68 * F is dependent on the implementation of NormalizedNodeContainer.getChild().
70 * We build the return collection by iterating over new data and looking each child up
71 * in old data. Based on that we construct replaced/written nodes. We then proceed to
72 * iterate over old data and looking up each child in new data.
74 final Collection<DataTreeCandidateNode> result = new ArrayList<>();
75 for (NormalizedNode<?, ?> child : newData.getValue()) {
76 final DataTreeCandidateNode node;
77 final Optional<NormalizedNode<?, ?>> maybeOldChild = oldData.getChild(child.getIdentifier());
79 if (maybeOldChild.isPresent()) {
80 // This does not find children which have not in fact been modified, as doing that
81 // reliably would require us running a full equals() on the two nodes.
82 node = AbstractRecursiveCandidateNode.replaceNode(maybeOldChild.get(), child);
84 node = AbstractRecursiveCandidateNode.writeNode(child);
90 // Process removals next, looking into new data to see if we processed it
91 for (NormalizedNode<?, ?> child : oldData.getValue()) {
92 if (!newData.getChild(child.getIdentifier()).isPresent()) {
93 result.add(AbstractRecursiveCandidateNode.deleteNode(child));
100 private final NormalizedNodeContainer<?, PathArgument, NormalizedNode<?,?>> data;
102 protected AbstractDataTreeCandidateNode(final NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>> data) {
103 this.data = Preconditions.checkNotNull(data);
106 protected final Optional<NormalizedNode<?, ?>> dataOptional() {
107 return Optional.of(data);
112 public final PathArgument getIdentifier() {
113 return data.getIdentifier();
116 protected final NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>> getData() {
121 public String toString() {
122 return this.getClass().getSimpleName() + "{data = " + this.data + "}";