2 * Copyright (c) 2015 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.mdsal.binding.dom.adapter;
10 import com.google.common.base.Optional;
11 import com.google.common.base.Preconditions;
12 import java.util.ArrayList;
13 import java.util.Collection;
14 import java.util.Iterator;
15 import java.util.List;
16 import org.opendaylight.mdsal.binding.api.DataObjectModification;
17 import org.opendaylight.mdsal.binding.dom.codec.api.BindingCodecTreeNode;
18 import org.opendaylight.yangtools.yang.binding.Augmentation;
19 import org.opendaylight.yangtools.yang.binding.ChildOf;
20 import org.opendaylight.yangtools.yang.binding.DataObject;
21 import org.opendaylight.yangtools.yang.binding.Identifiable;
22 import org.opendaylight.yangtools.yang.binding.Identifier;
23 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
24 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.PathArgument;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
26 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
27 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
32 * Lazily translated {@link DataObjectModification} based on {@link DataTreeCandidateNode}.
35 * {@link LazyDataObjectModification} represents Data tree change event,
36 * but whole tree is not translated or resolved eagerly, but only child nodes
37 * which are directly accessed by user of data object modification.
39 * @param <T> Type of Binding Data Object
41 final class LazyDataObjectModification<T extends DataObject> implements DataObjectModification<T> {
43 private static final Logger LOG = LoggerFactory.getLogger(LazyDataObjectModification.class);
45 private final BindingCodecTreeNode<T> codec;
46 private final DataTreeCandidateNode domData;
47 private final PathArgument identifier;
48 private Collection<DataObjectModification<? extends DataObject>> childNodesCache;
50 private LazyDataObjectModification(final BindingCodecTreeNode<T> codec, final DataTreeCandidateNode domData) {
51 this.codec = Preconditions.checkNotNull(codec);
52 this.domData = Preconditions.checkNotNull(domData);
53 this.identifier = codec.deserializePathArgument(domData.getIdentifier());
56 static <T extends DataObject> DataObjectModification<T> create(final BindingCodecTreeNode<T> codec,
57 final DataTreeCandidateNode domData) {
58 return new LazyDataObjectModification<>(codec,domData);
61 private static Collection<DataObjectModification<? extends DataObject>> from(final BindingCodecTreeNode<?>
62 parentCodec, final Collection<DataTreeCandidateNode> domChildNodes) {
63 final List<DataObjectModification<? extends DataObject>> result = new ArrayList<>(domChildNodes.size());
64 populateList(result, parentCodec, domChildNodes);
68 private static void populateList(final List<DataObjectModification<? extends DataObject>> result,
69 final BindingCodecTreeNode<?> parentCodec, final Collection<DataTreeCandidateNode> domChildNodes) {
70 for (final DataTreeCandidateNode domChildNode : domChildNodes) {
71 final BindingStructuralType type = BindingStructuralType.from(domChildNode);
72 if (type != BindingStructuralType.NOT_ADDRESSABLE) {
74 * Even if type is UNKNOWN, from perspective of BindingStructuralType
75 * we try to load codec for it. We will use that type to further specify
79 final BindingCodecTreeNode<?> childCodec =
80 parentCodec.yangPathArgumentChild(domChildNode.getIdentifier());
81 populateList(result,type, childCodec, domChildNode);
82 } catch (final IllegalArgumentException e) {
83 if (type == BindingStructuralType.UNKNOWN) {
84 LOG.debug("Unable to deserialize unknown DOM node {}",domChildNode,e);
86 LOG.debug("Binding representation for DOM node {} was not found",domChildNode,e);
93 private static void populateList(final List<DataObjectModification<? extends DataObject>> result,
94 final BindingStructuralType type, final BindingCodecTreeNode<?> childCodec,
95 final DataTreeCandidateNode domChildNode) {
98 // We use parent codec intentionally.
99 populateListWithSingleCodec(result, childCodec, domChildNode.getChildNodes());
101 case INVISIBLE_CONTAINER:
102 populateList(result, childCodec, domChildNode.getChildNodes());
105 case VISIBLE_CONTAINER:
106 result.add(create(childCodec, domChildNode));
112 private static void populateListWithSingleCodec(final List<DataObjectModification<? extends DataObject>> result,
113 final BindingCodecTreeNode<?> codec, final Collection<DataTreeCandidateNode> childNodes) {
114 for (final DataTreeCandidateNode child : childNodes) {
115 result.add(create(codec, child));
120 public T getDataBefore() {
121 return deserialize(domData.getDataBefore());
125 public T getDataAfter() {
126 return deserialize(domData.getDataAfter());
130 public Class<T> getDataType() {
131 return codec.getBindingClass();
135 public PathArgument getIdentifier() {
140 public DataObjectModification.ModificationType getModificationType() {
141 switch (domData.getModificationType()) {
144 return DataObjectModification.ModificationType.WRITE;
145 case SUBTREE_MODIFIED:
146 return DataObjectModification.ModificationType.SUBTREE_MODIFIED;
149 return DataObjectModification.ModificationType.DELETE;
152 // TODO: Should we lie about modification type instead of exception?
153 throw new IllegalStateException("Unsupported DOM Modification type " + domData.getModificationType());
158 public Collection<DataObjectModification<? extends DataObject>> getModifiedChildren() {
159 if (childNodesCache == null) {
160 childNodesCache = from(codec, domData.getChildNodes());
162 return childNodesCache;
165 @SuppressWarnings("unchecked")
167 public <C extends ChildOf<? super T>> Collection<DataObjectModification<C>>
168 getModifiedChildren(Class<C> childType) {
169 List<DataObjectModification<C>> children = new ArrayList<>();
170 for (DataObjectModification<? extends DataObject> potential : getModifiedChildren()) {
171 if (childType.isAssignableFrom(potential.getDataType())) {
172 children.add((DataObjectModification<C>) potential);
179 public DataObjectModification<? extends DataObject> getModifiedChild(final PathArgument arg) {
180 final List<YangInstanceIdentifier.PathArgument> domArgumentList = new ArrayList<>();
181 final BindingCodecTreeNode<?> childCodec = codec.bindingPathArgumentChild(arg, domArgumentList);
182 final Iterator<YangInstanceIdentifier.PathArgument> toEnter = domArgumentList.iterator();
183 DataTreeCandidateNode current = domData;
184 while (toEnter.hasNext() && current != null) {
185 current = current.getModifiedChild(toEnter.next());
187 if (current != null) {
188 return create(childCodec, current);
194 @SuppressWarnings("unchecked")
195 public <C extends Identifiable<K> & ChildOf<? super T>, K extends Identifier<C>> DataObjectModification<C>
196 getModifiedChildListItem(final Class<C> listItem, final K listKey) {
197 return (DataObjectModification<C>) getModifiedChild(new InstanceIdentifier.IdentifiableItem<>(
202 @SuppressWarnings("unchecked")
203 public <C extends ChildOf<? super T>> DataObjectModification<C> getModifiedChildContainer(final Class<C> arg) {
204 return (DataObjectModification<C>) getModifiedChild(new InstanceIdentifier.Item<>(arg));
208 @SuppressWarnings("unchecked")
209 public <C extends Augmentation<T> & DataObject> DataObjectModification<C> getModifiedAugmentation(
210 final Class<C> augmentation) {
211 return (DataObjectModification<C>) getModifiedChild(new InstanceIdentifier.Item<>(augmentation));
214 private T deserialize(final Optional<NormalizedNode<?, ?>> dataAfter) {
215 if (dataAfter.isPresent()) {
216 return codec.deserialize(dataAfter.get());