yang.data.impl.schema.tree clean-up
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / tree / AbstractDataTreeCandidateNode.java
1 /*
2  * Copyright (c) 2015 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.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;
23
24 abstract class AbstractDataTreeCandidateNode implements DataTreeCandidateNode {
25     private static final Function<NormalizedNode<?, ?>, DataTreeCandidateNode> TO_DELETED_NODE = new Function<NormalizedNode<?, ?>, DataTreeCandidateNode>() {
26         @Override
27         public DataTreeCandidateNode apply(final NormalizedNode<?, ?> input) {
28             return AbstractRecursiveCandidateNode.deleteNode(input);
29         }
30     };
31     private static final Function<NormalizedNode<?, ?>, DataTreeCandidateNode> TO_WRITTEN_NODE = new Function<NormalizedNode<?, ?>, DataTreeCandidateNode>() {
32         @Override
33         public DataTreeCandidateNode apply(final NormalizedNode<?, ?> input) {
34             return AbstractRecursiveCandidateNode.writeNode(input);
35         }
36     };
37
38     private static Optional<NormalizedNode<?, ?>> getChild(final NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>> container, final PathArgument identifier) {
39         if (container != null) {
40             return container.getChild(identifier);
41         } else {
42             return Optional.absent();
43         }
44     }
45
46     static DataTreeCandidateNode deltaChild(
47             final NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>> oldData,
48             final NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>> newData, final PathArgument identifier) {
49
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());
56             } else {
57                 return TO_DELETED_NODE.apply(oldChild);
58             }
59         } else {
60             if (maybeNewChild.isPresent()) {
61                 return TO_WRITTEN_NODE.apply(maybeNewChild.get());
62             } else {
63                 return null;
64             }
65         }
66     }
67
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);
74         }
75         if (oldData == null) {
76             return Collections2.transform(newData.getValue(), TO_WRITTEN_NODE);
77         }
78
79         /*
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().
82          *
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.
86          */
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());
91
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);
96             } else {
97                 node = AbstractRecursiveCandidateNode.writeNode(child);
98             }
99
100             result.add(node);
101         }
102
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));
107             }
108         }
109
110         return result;
111     }
112
113     private final NormalizedNodeContainer<?, PathArgument, NormalizedNode<?,?>> data;
114
115     protected AbstractDataTreeCandidateNode(final NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>> data) {
116         this.data = Preconditions.checkNotNull(data);
117     }
118
119     protected final Optional<NormalizedNode<?, ?>> dataOptional() {
120         return Optional.<NormalizedNode<?, ?>>of(data);
121     }
122
123     @Override
124     @Nonnull
125     public final PathArgument getIdentifier() {
126         return data.getIdentifier();
127     }
128
129     protected final NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>> getData() {
130         return data;
131     }
132 }