77a32c25d53685fb4d62a0c681803b3226071512
[mdsal.git] / binding2 / mdsal-binding2-dom-codec / src / main / java / org / opendaylight / mdsal / binding / javav2 / dom / codec / modification / LazyTreeNodeModification.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies s.r.o. 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.mdsal.binding.javav2.dom.codec.modification;
9
10 import com.google.common.annotations.Beta;
11 import com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
13 import java.util.ArrayList;
14 import java.util.Collection;
15 import java.util.Iterator;
16 import java.util.List;
17 import javax.annotation.Nonnull;
18 import javax.annotation.Nullable;
19 import org.opendaylight.mdsal.binding.javav2.api.TreeNodeModification;
20 import org.opendaylight.mdsal.binding.javav2.dom.codec.api.BindingTreeNodeCodec;
21 import org.opendaylight.mdsal.binding.javav2.spec.base.IdentifiableItem;
22 import org.opendaylight.mdsal.binding.javav2.spec.base.Item;
23 import org.opendaylight.mdsal.binding.javav2.spec.base.TreeArgument;
24 import org.opendaylight.mdsal.binding.javav2.spec.base.TreeNode;
25 import org.opendaylight.mdsal.binding.javav2.spec.structural.Augmentation;
26 import org.opendaylight.mdsal.binding.javav2.spec.structural.TreeChildNode;
27 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
28 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
29 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 /**
34  * Lazily translated {@link TreeNodeModification} based on {@link DataTreeCandidateNode}.
35  *
36  * <p>
37  * {@link LazyTreeNodeModification} represents Data tree change event, but whole tree is not translated or
38  * resolved eagerly, but only child nodes which are directly accessed by user of tree node modification.
39  *
40  * @param <T>
41  *            Type of Binding Tree Node
42  */
43 @Beta
44 final class LazyTreeNodeModification<T extends TreeNode> implements TreeNodeModification<T> {
45
46     private static final Logger LOG = LoggerFactory.getLogger(LazyTreeNodeModification.class);
47
48     private final BindingTreeNodeCodec<T> codec;
49     private final DataTreeCandidateNode domData;
50     private final TreeArgument<?> identifier;
51     private Collection<TreeNodeModification<? extends TreeNode>> childNodesCache;
52
53     private LazyTreeNodeModification(final BindingTreeNodeCodec<T> codec, final DataTreeCandidateNode domData) {
54         this.codec = Preconditions.checkNotNull(codec);
55         this.domData = Preconditions.checkNotNull(domData);
56         this.identifier = codec.deserializePathArgument(domData.getIdentifier());
57     }
58
59     static <T extends TreeNode> TreeNodeModification<T> create(final BindingTreeNodeCodec<T> codec,
60             final DataTreeCandidateNode domData) {
61         return new LazyTreeNodeModification<>(codec, domData);
62     }
63
64     private static Collection<TreeNodeModification<? extends TreeNode>> from(final BindingTreeNodeCodec<?> parentCodec,
65             final Collection<DataTreeCandidateNode> domChildNodes) {
66         final List<TreeNodeModification<? extends TreeNode>> result = new ArrayList<>(domChildNodes.size());
67         populateList(result, parentCodec, domChildNodes);
68         return result;
69     }
70
71     private static void populateList(final List<TreeNodeModification<? extends TreeNode>> result,
72             final BindingTreeNodeCodec<?> parentCodec, final Collection<DataTreeCandidateNode> domChildNodes) {
73         for (final DataTreeCandidateNode domChildNode : domChildNodes) {
74             final BindingStructuralType type = BindingStructuralType.from(domChildNode);
75             if (type != BindingStructuralType.NOT_ADDRESSABLE) {
76                 /*
77                  * Even if type is UNKNOWN, from perspective of BindingStructuralType we try to load codec for
78                  * it. We will use that type to further specify debug log.
79                  */
80                 try {
81                     final BindingTreeNodeCodec<?> childCodec =
82                             parentCodec.yangPathArgumentChild(domChildNode.getIdentifier());
83                     populateList(result, type, childCodec, domChildNode);
84                 } catch (final IllegalArgumentException e) {
85                     if (type == BindingStructuralType.UNKNOWN) {
86                         LOG.debug("Unable to deserialize unknown DOM node {}", domChildNode, e);
87                     } else {
88                         LOG.debug("Binding representation for DOM node {} was not found", domChildNode, e);
89                     }
90                 }
91             }
92         }
93     }
94
95     private static void populateList(final List<TreeNodeModification<? extends TreeNode>> result,
96             final BindingStructuralType type, final BindingTreeNodeCodec<?> childCodec,
97             final DataTreeCandidateNode domChildNode) {
98         switch (type) {
99             case INVISIBLE_LIST:
100                 // We use parent codec intentionally.
101                 populateListWithSingleCodec(result, childCodec, domChildNode.getChildNodes());
102                 break;
103             case INVISIBLE_CONTAINER:
104                 populateList(result, childCodec, domChildNode.getChildNodes());
105                 break;
106             case UNKNOWN:
107             case VISIBLE_CONTAINER:
108                 result.add(create(childCodec, domChildNode));
109                 break;
110             default:
111         }
112     }
113
114     private static void populateListWithSingleCodec(final List<TreeNodeModification<? extends TreeNode>> result,
115             final BindingTreeNodeCodec<?> codec, final Collection<DataTreeCandidateNode> childNodes) {
116         for (final DataTreeCandidateNode child : childNodes) {
117             result.add(create(codec, child));
118         }
119     }
120
121     @Nullable
122     @Override
123     public T getDataBefore() {
124         return deserialize(domData.getDataBefore());
125     }
126
127     @Nullable
128     @Override
129     public T getDataAfter() {
130         return deserialize(domData.getDataAfter());
131     }
132
133     @Nonnull
134     @Override
135     public Class<T> getDataType() {
136         return codec.getBindingClass();
137     }
138
139     @Nonnull
140     @Override
141     public TreeArgument<?> getIdentifier() {
142         return identifier;
143     }
144
145     @Nonnull
146     @Override
147     public TreeNodeModification.ModificationType getModificationType() {
148         switch (domData.getModificationType()) {
149             case APPEARED:
150             case WRITE:
151                 return TreeNodeModification.ModificationType.WRITE;
152             case SUBTREE_MODIFIED:
153                 return TreeNodeModification.ModificationType.SUBTREE_MODIFIED;
154             case DISAPPEARED:
155             case DELETE:
156                 return TreeNodeModification.ModificationType.DELETE;
157
158             default:
159                 // TODO: Should we lie about modification type instead of exception?
160                 throw new IllegalStateException("Unsupported DOM Modification type " + domData.getModificationType());
161         }
162     }
163
164     @Nonnull
165     @Override
166     public Collection<TreeNodeModification<? extends TreeNode>> getModifiedChildren() {
167         if (childNodesCache == null) {
168             childNodesCache = from(codec, domData.getChildNodes());
169         }
170         return childNodesCache;
171     }
172
173     @SuppressWarnings("unchecked")
174     @Override
175     public <C extends TreeChildNode<? super T, ?>> Collection<TreeNodeModification<C>>
176             getModifiedChildren(@Nonnull final Class<C> childType) {
177         final List<TreeNodeModification<C>> children = new ArrayList<>();
178         for (final TreeNodeModification<? extends TreeNode> potential : getModifiedChildren()) {
179             if (childType.isAssignableFrom(potential.getDataType())) {
180                 children.add((TreeNodeModification<C>) potential);
181             }
182         }
183         return children;
184     }
185
186     @SuppressWarnings("rawtypes")
187     @Nullable
188     @Override
189     public TreeNodeModification<? extends TreeNode> getModifiedChild(final TreeArgument childArgument) {
190         final List<YangInstanceIdentifier.PathArgument> domArgumentList = new ArrayList<>();
191         final BindingTreeNodeCodec<?> childCodec = codec.bindingPathArgumentChild(childArgument, domArgumentList);
192         final Iterator<YangInstanceIdentifier.PathArgument> toEnter = domArgumentList.iterator();
193         DataTreeCandidateNode current = domData;
194         while (toEnter.hasNext() && current != null) {
195             current = current.getModifiedChild(toEnter.next());
196         }
197         if (current != null) {
198             return create(childCodec, current);
199         }
200         return null;
201     }
202
203     @SuppressWarnings({ "unchecked", "rawtypes" })
204     @Override
205     public <C extends IdentifiableItem<T, K> & TreeChildNode<? super T, ?>, K extends IdentifiableItem<T, K>>
206             TreeNodeModification<C>
207             getModifiedChildListItem(@Nonnull final Class<C> listItem, @Nonnull final K listKey) {
208         return (TreeNodeModification) getModifiedChild(new IdentifiableItem(listItem, listKey));
209     }
210
211     @SuppressWarnings({ "unchecked", "rawtypes" })
212     @Nullable
213     @Override
214     public <C extends TreeChildNode<? super T, ?>> TreeNodeModification<C>
215             getModifiedChildContainer(@Nonnull final Class<C> child) {
216         return (TreeNodeModification<C>) getModifiedChild(new Item(child));
217     }
218
219     @SuppressWarnings({ "unchecked", "rawtypes" })
220     @Nullable
221     @Override
222     public <C extends Augmentation<T> & TreeNode> TreeNodeModification<C>
223             getModifiedAugmentation(@Nonnull final Class<C> augmentation) {
224         return (TreeNodeModification<C>) getModifiedChild(new Item(augmentation));
225     }
226
227     private T deserialize(final Optional<NormalizedNode<?, ?>> dataAfter) {
228         if (dataAfter.isPresent()) {
229             return codec.deserialize(dataAfter.get());
230         }
231         return null;
232     }
233 }
234