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.controller.md.sal.binding.impl;
10 import com.google.common.base.Preconditions;
11 import java.util.ArrayList;
12 import java.util.Collection;
13 import java.util.Iterator;
14 import java.util.List;
15 import java.util.Optional;
16 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
17 import org.opendaylight.mdsal.binding.dom.adapter.BindingStructuralType;
18 import org.opendaylight.mdsal.binding.dom.codec.api.BindingCodecTreeNode;
19 import org.opendaylight.yangtools.yang.binding.Augmentation;
20 import org.opendaylight.yangtools.yang.binding.ChildOf;
21 import org.opendaylight.yangtools.yang.binding.DataObject;
22 import org.opendaylight.yangtools.yang.binding.Identifiable;
23 import org.opendaylight.yangtools.yang.binding.Identifier;
24 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.IdentifiableItem;
25 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.Item;
26 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.PathArgument;
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 DataObjectModification} based on {@link DataTreeCandidateNode}.
36 * {@link LazyDataObjectModification} represents Data tree change event,
37 * but whole tree is not translated or resolved eagerly, but only child nodes
38 * which are directly accessed by user of data object modification.
40 * @param <T> Type of Binding Data Object
42 final class LazyDataObjectModification<T extends DataObject> implements DataObjectModification<T> {
44 private static final Logger LOG = LoggerFactory.getLogger(LazyDataObjectModification.class);
46 private final BindingCodecTreeNode<T> codec;
47 private final DataTreeCandidateNode domData;
48 private final PathArgument identifier;
50 private volatile Collection<DataObjectModification<? extends DataObject>> childNodesCache;
51 private volatile ModificationType modificationType;
53 private LazyDataObjectModification(final BindingCodecTreeNode<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 DataObject> DataObjectModification<T> create(final BindingCodecTreeNode<T> codec,
60 final DataTreeCandidateNode domData) {
61 return new LazyDataObjectModification<>(codec,domData);
64 private static Collection<DataObjectModification<? extends DataObject>> from(
65 final BindingCodecTreeNode<?> parentCodec, final Collection<DataTreeCandidateNode> domChildNodes) {
66 final List<DataObjectModification<? extends DataObject>> result = new ArrayList<>(domChildNodes.size());
67 populateList(result, parentCodec, domChildNodes);
71 private static void populateList(final List<DataObjectModification<? extends DataObject>> result,
72 final BindingCodecTreeNode<?> 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
78 * we try to load codec for it. We will use that type to further specify
82 final BindingCodecTreeNode<?> childCodec =
83 parentCodec.yangPathArgumentChild(domChildNode.getIdentifier());
84 populateList(result,type, childCodec, domChildNode);
85 } catch (final IllegalArgumentException e) {
86 if (type == BindingStructuralType.UNKNOWN) {
87 LOG.debug("Unable to deserialize unknown DOM node {}",domChildNode,e);
89 LOG.debug("Binding representation for DOM node {} was not found",domChildNode,e);
96 private static void populateList(final List<DataObjectModification<? extends DataObject>> result,
97 final BindingStructuralType type, final BindingCodecTreeNode<?> childCodec,
98 final DataTreeCandidateNode domChildNode) {
101 // We use parent codec intentionally.
102 populateListWithSingleCodec(result, childCodec, domChildNode.getChildNodes());
104 case INVISIBLE_CONTAINER:
105 populateList(result, childCodec, domChildNode.getChildNodes());
108 case VISIBLE_CONTAINER:
109 result.add(create(childCodec, domChildNode));
116 private static void populateListWithSingleCodec(final List<DataObjectModification<? extends DataObject>> result,
117 final BindingCodecTreeNode<?> codec, final Collection<DataTreeCandidateNode> childNodes) {
118 for (final DataTreeCandidateNode child : childNodes) {
119 result.add(create(codec, child));
124 public T getDataBefore() {
125 return deserialize(domData.getDataBefore());
129 public T getDataAfter() {
130 return deserialize(domData.getDataAfter());
134 public Class<T> getDataType() {
135 return codec.getBindingClass();
139 public PathArgument getIdentifier() {
144 public ModificationType getModificationType() {
145 ModificationType localType = modificationType;
146 if (localType != null) {
150 switch (domData.getModificationType()) {
153 localType = ModificationType.WRITE;
157 localType = ModificationType.DELETE;
159 case SUBTREE_MODIFIED:
160 localType = resolveSubtreeModificationType();
163 // TODO: Should we lie about modification type instead of exception?
164 throw new IllegalStateException("Unsupported DOM Modification type " + domData.getModificationType());
167 modificationType = localType;
171 private ModificationType resolveSubtreeModificationType() {
172 switch (codec.getChildAddressabilitySummary()) {
174 // All children are addressable, it is safe to report SUBTREE_MODIFIED
175 return ModificationType.SUBTREE_MODIFIED;
177 // All children are non-addressable, report WRITE
178 return ModificationType.WRITE;
180 // This case is not completely trivial, as we may have NOT_ADDRESSABLE nodes underneath us. If that
181 // is the case, we need to turn this modification into a WRITE operation, so that the user is able
182 // to observe those nodes being introduced. This is not efficient, but unfortunately unavoidable,
183 // as we cannot accurately represent such changes.
184 for (DataTreeCandidateNode child : domData.getChildNodes()) {
185 if (BindingStructuralType.recursiveFrom(child) == BindingStructuralType.NOT_ADDRESSABLE) {
186 // We have a non-addressable child, turn this modification into a write
187 return ModificationType.WRITE;
191 // No unaddressable children found, proceed in addressed mode
192 return ModificationType.SUBTREE_MODIFIED;
194 throw new IllegalStateException("Unsupported child addressability summary "
195 + codec.getChildAddressabilitySummary());
200 public Collection<DataObjectModification<? extends DataObject>> getModifiedChildren() {
201 Collection<DataObjectModification<? extends DataObject>> local = childNodesCache;
203 childNodesCache = local = from(codec, domData.getChildNodes());
209 public DataObjectModification<? extends DataObject> getModifiedChild(final PathArgument arg) {
210 final List<YangInstanceIdentifier.PathArgument> domArgumentList = new ArrayList<>();
211 final BindingCodecTreeNode<?> childCodec = codec.bindingPathArgumentChild(arg, domArgumentList);
212 final Iterator<YangInstanceIdentifier.PathArgument> toEnter = domArgumentList.iterator();
213 DataTreeCandidateNode current = domData;
214 while (toEnter.hasNext() && current != null) {
215 current = current.getModifiedChild(toEnter.next());
217 if (current != null) {
218 return create(childCodec, current);
224 @SuppressWarnings("unchecked")
225 public <C extends Identifiable<K> & ChildOf<? super T>, K extends Identifier<C>> DataObjectModification<C>
226 getModifiedChildListItem(final Class<C> listItem, final K listKey) {
227 return (DataObjectModification<C>) getModifiedChild(IdentifiableItem.of(listItem, listKey));
231 @SuppressWarnings("unchecked")
232 public <C extends ChildOf<? super T>> DataObjectModification<C> getModifiedChildContainer(final Class<C> arg) {
233 return (DataObjectModification<C>) getModifiedChild(Item.of(arg));
237 @SuppressWarnings("unchecked")
238 public <C extends Augmentation<T> & DataObject> DataObjectModification<C> getModifiedAugmentation(
239 final Class<C> augmentation) {
240 return (DataObjectModification<C>) getModifiedChild(Item.of(augmentation));
243 private T deserialize(final Optional<NormalizedNode<?, ?>> dataAfter) {
244 if (dataAfter.isPresent()) {
245 return codec.deserialize(dataAfter.get());
251 public String toString() {
252 return getClass().getSimpleName() + "{identifier = " + identifier + ", domData = " + domData + "}";