2 * Copyright (c) 2017 Pantheon Technologies s.r.o. 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.mdsal.binding.javav2.dom.codec.modification;
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;
34 * Lazily translated {@link TreeNodeModification} based on {@link DataTreeCandidateNode}.
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.
41 * Type of Binding Tree Node
44 final class LazyTreeNodeModification<T extends TreeNode> implements TreeNodeModification<T> {
46 private static final Logger LOG = LoggerFactory.getLogger(LazyTreeNodeModification.class);
48 private final BindingTreeNodeCodec<T> codec;
49 private final DataTreeCandidateNode domData;
50 private final TreeArgument<?> identifier;
51 private Collection<TreeNodeModification<? extends TreeNode>> childNodesCache;
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());
59 static <T extends TreeNode> TreeNodeModification<T> create(final BindingTreeNodeCodec<T> codec,
60 final DataTreeCandidateNode domData) {
61 return new LazyTreeNodeModification<>(codec, domData);
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);
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) {
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.
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);
88 LOG.debug("Binding representation for DOM node {} was not found", domChildNode, e);
95 private static void populateList(final List<TreeNodeModification<? extends TreeNode>> result,
96 final BindingStructuralType type, final BindingTreeNodeCodec<?> childCodec,
97 final DataTreeCandidateNode domChildNode) {
100 // We use parent codec intentionally.
101 populateListWithSingleCodec(result, childCodec, domChildNode.getChildNodes());
103 case INVISIBLE_CONTAINER:
104 populateList(result, childCodec, domChildNode.getChildNodes());
107 case VISIBLE_CONTAINER:
108 result.add(create(childCodec, domChildNode));
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));
123 public T getDataBefore() {
124 return deserialize(domData.getDataBefore());
129 public T getDataAfter() {
130 return deserialize(domData.getDataAfter());
135 public Class<T> getDataType() {
136 return codec.getBindingClass();
141 public TreeArgument<?> getIdentifier() {
147 public TreeNodeModification.ModificationType getModificationType() {
148 switch (domData.getModificationType()) {
151 return TreeNodeModification.ModificationType.WRITE;
152 case SUBTREE_MODIFIED:
153 return TreeNodeModification.ModificationType.SUBTREE_MODIFIED;
156 return TreeNodeModification.ModificationType.DELETE;
159 // TODO: Should we lie about modification type instead of exception?
160 throw new IllegalStateException("Unsupported DOM Modification type " + domData.getModificationType());
166 public Collection<TreeNodeModification<? extends TreeNode>> getModifiedChildren() {
167 if (childNodesCache == null) {
168 childNodesCache = from(codec, domData.getChildNodes());
170 return childNodesCache;
173 @SuppressWarnings("unchecked")
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);
186 @SuppressWarnings("rawtypes")
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());
197 if (current != null) {
198 return create(childCodec, current);
203 @SuppressWarnings({ "unchecked", "rawtypes" })
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));
211 @SuppressWarnings({ "unchecked", "rawtypes" })
214 public <C extends TreeChildNode<? super T, ?>> TreeNodeModification<C>
215 getModifiedChildContainer(@Nonnull final Class<C> child) {
216 return (TreeNodeModification<C>) getModifiedChild(new Item(child));
219 @SuppressWarnings({ "unchecked", "rawtypes" })
222 public <C extends Augmentation<T> & TreeNode> TreeNodeModification<C>
223 getModifiedAugmentation(@Nonnull final Class<C> augmentation) {
224 return (TreeNodeModification<C>) getModifiedChild(new Item(augmentation));
227 private T deserialize(final Optional<NormalizedNode<?, ?>> dataAfter) {
228 if (dataAfter.isPresent()) {
229 return codec.deserialize(dataAfter.get());