f7bea78824c56a9442a062d6b6be39ace089a542
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / tree / AbstractDataTreeCandidateNode.java
1 /*
2  * Copyright (c) 2015, 2016 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 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;
21
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);
28         } else {
29             return Optional.absent();
30         }
31     }
32
33     static DataTreeCandidateNode deltaChild(
34             final NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>> oldData,
35             final NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>> newData, final PathArgument identifier) {
36
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());
43             } else {
44                 return AbstractRecursiveCandidateNode.deleteNode(oldChild);
45             }
46         } else {
47             if (maybeNewChild.isPresent()) {
48                 return AbstractRecursiveCandidateNode.writeNode(maybeNewChild.get());
49             } else {
50                 return null;
51             }
52         }
53     }
54
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);
61         }
62         if (oldData == null) {
63             return Collections2.transform(newData.getValue(), AbstractRecursiveCandidateNode::writeNode);
64         }
65
66         /*
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().
69          *
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.
73          */
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());
78
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);
83             } else {
84                 node = AbstractRecursiveCandidateNode.writeNode(child);
85             }
86
87             result.add(node);
88         }
89
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));
94             }
95         }
96
97         return result;
98     }
99
100     private final NormalizedNodeContainer<?, PathArgument, NormalizedNode<?,?>> data;
101
102     protected AbstractDataTreeCandidateNode(final NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>> data) {
103         this.data = Preconditions.checkNotNull(data);
104     }
105
106     protected final Optional<NormalizedNode<?, ?>> dataOptional() {
107         return Optional.of(data);
108     }
109
110     @Override
111     @Nonnull
112     public final PathArgument getIdentifier() {
113         return data.getIdentifier();
114     }
115
116     protected final NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>> getData() {
117         return data;
118     }
119
120     @Override
121     public String toString() {
122         return this.getClass().getSimpleName() + "{data = " + this.data + "}";
123     }
124 }