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 java.util.stream.Collectors;
17 import java.util.stream.Stream;
18 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
19 import org.opendaylight.mdsal.binding.dom.adapter.BindingStructuralType;
20 import org.opendaylight.mdsal.binding.dom.codec.api.BindingCodecTreeNode;
21 import org.opendaylight.yangtools.yang.binding.Augmentation;
22 import org.opendaylight.yangtools.yang.binding.ChildOf;
23 import org.opendaylight.yangtools.yang.binding.ChoiceIn;
24 import org.opendaylight.yangtools.yang.binding.DataObject;
25 import org.opendaylight.yangtools.yang.binding.Identifiable;
26 import org.opendaylight.yangtools.yang.binding.Identifier;
27 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.IdentifiableItem;
28 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.Item;
29 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.PathArgument;
30 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
31 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
32 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
37 * Lazily translated {@link DataObjectModification} based on {@link DataTreeCandidateNode}.
39 * {@link LazyDataObjectModification} represents Data tree change event,
40 * but whole tree is not translated or resolved eagerly, but only child nodes
41 * which are directly accessed by user of data object modification.
43 * @param <T> Type of Binding Data Object
45 final class LazyDataObjectModification<T extends DataObject> implements DataObjectModification<T> {
47 private static final Logger LOG = LoggerFactory.getLogger(LazyDataObjectModification.class);
49 private final BindingCodecTreeNode<T> codec;
50 private final DataTreeCandidateNode domData;
51 private final PathArgument identifier;
53 private volatile Collection<LazyDataObjectModification<? extends DataObject>> childNodesCache;
54 private volatile ModificationType modificationType;
56 private LazyDataObjectModification(final BindingCodecTreeNode<T> codec, final DataTreeCandidateNode domData) {
57 this.codec = Preconditions.checkNotNull(codec);
58 this.domData = Preconditions.checkNotNull(domData);
59 this.identifier = codec.deserializePathArgument(domData.getIdentifier());
62 static <T extends DataObject> LazyDataObjectModification<T> create(final BindingCodecTreeNode<T> codec,
63 final DataTreeCandidateNode domData) {
64 return new LazyDataObjectModification<>(codec,domData);
67 private static Collection<LazyDataObjectModification<? extends DataObject>> from(
68 final BindingCodecTreeNode<?> parentCodec, final Collection<DataTreeCandidateNode> domChildNodes) {
69 final List<LazyDataObjectModification<? extends DataObject>> result = new ArrayList<>(domChildNodes.size());
70 populateList(result, parentCodec, domChildNodes);
74 private static void populateList(final List<LazyDataObjectModification<? extends DataObject>> result,
75 final BindingCodecTreeNode<?> parentCodec, final Collection<DataTreeCandidateNode> domChildNodes) {
76 for (final DataTreeCandidateNode domChildNode : domChildNodes) {
77 final BindingStructuralType type = BindingStructuralType.from(domChildNode);
78 if (type != BindingStructuralType.NOT_ADDRESSABLE) {
80 * Even if type is UNKNOWN, from perspective of BindingStructuralType
81 * we try to load codec for it. We will use that type to further specify
85 final BindingCodecTreeNode<?> childCodec =
86 parentCodec.yangPathArgumentChild(domChildNode.getIdentifier());
87 populateList(result,type, childCodec, domChildNode);
88 } catch (final IllegalArgumentException e) {
89 if (type == BindingStructuralType.UNKNOWN) {
90 LOG.debug("Unable to deserialize unknown DOM node {}",domChildNode,e);
92 LOG.debug("Binding representation for DOM node {} was not found",domChildNode,e);
99 private static void populateList(final List<LazyDataObjectModification<? extends DataObject>> result,
100 final BindingStructuralType type, final BindingCodecTreeNode<?> childCodec,
101 final DataTreeCandidateNode domChildNode) {
104 // We use parent codec intentionally.
105 populateListWithSingleCodec(result, childCodec, domChildNode.getChildNodes());
107 case INVISIBLE_CONTAINER:
108 populateList(result, childCodec, domChildNode.getChildNodes());
111 case VISIBLE_CONTAINER:
112 result.add(create(childCodec, domChildNode));
119 private static void populateListWithSingleCodec(final List<LazyDataObjectModification<? extends DataObject>> result,
120 final BindingCodecTreeNode<?> codec, final Collection<DataTreeCandidateNode> childNodes) {
121 for (final DataTreeCandidateNode child : childNodes) {
122 result.add(create(codec, child));
127 public T getDataBefore() {
128 return deserialize(domData.getDataBefore());
132 public T getDataAfter() {
133 return deserialize(domData.getDataAfter());
137 public Class<T> getDataType() {
138 return codec.getBindingClass();
142 public PathArgument getIdentifier() {
147 public ModificationType getModificationType() {
148 ModificationType localType = modificationType;
149 if (localType != null) {
153 switch (domData.getModificationType()) {
156 localType = ModificationType.WRITE;
160 localType = ModificationType.DELETE;
162 case SUBTREE_MODIFIED:
163 localType = resolveSubtreeModificationType();
166 // TODO: Should we lie about modification type instead of exception?
167 throw new IllegalStateException("Unsupported DOM Modification type " + domData.getModificationType());
170 modificationType = localType;
174 private ModificationType resolveSubtreeModificationType() {
175 switch (codec.getChildAddressabilitySummary()) {
177 // All children are addressable, it is safe to report SUBTREE_MODIFIED
178 return ModificationType.SUBTREE_MODIFIED;
180 // All children are non-addressable, report WRITE
181 return ModificationType.WRITE;
183 // This case is not completely trivial, as we may have NOT_ADDRESSABLE nodes underneath us. If that
184 // is the case, we need to turn this modification into a WRITE operation, so that the user is able
185 // to observe those nodes being introduced. This is not efficient, but unfortunately unavoidable,
186 // as we cannot accurately represent such changes.
187 for (DataTreeCandidateNode child : domData.getChildNodes()) {
188 if (BindingStructuralType.recursiveFrom(child) == BindingStructuralType.NOT_ADDRESSABLE) {
189 // We have a non-addressable child, turn this modification into a write
190 return ModificationType.WRITE;
194 // No unaddressable children found, proceed in addressed mode
195 return ModificationType.SUBTREE_MODIFIED;
197 throw new IllegalStateException("Unsupported child addressability summary "
198 + codec.getChildAddressabilitySummary());
203 public Collection<LazyDataObjectModification<? extends DataObject>> getModifiedChildren() {
204 Collection<LazyDataObjectModification<? extends DataObject>> local = childNodesCache;
206 childNodesCache = local = from(codec, domData.getChildNodes());
212 public <H extends ChoiceIn<? super T> & DataObject, C extends ChildOf<? super H>>
213 Collection<DataObjectModification<C>> getModifiedChildren(final Class<H> caseType,
214 final Class<C> childType) {
215 return streamModifiedChildren(childType)
216 .filter(child -> caseType.equals(child.identifier.getCaseType().orElse(null)))
217 .collect(Collectors.toList());
220 @SuppressWarnings("unchecked")
221 private <C extends DataObject> Stream<LazyDataObjectModification<C>> streamModifiedChildren(
222 final Class<C> childType) {
223 return getModifiedChildren().stream()
224 .filter(child -> childType.isAssignableFrom(child.getDataType()))
225 .map(child -> (LazyDataObjectModification<C>) child);
229 public DataObjectModification<? extends DataObject> getModifiedChild(final PathArgument arg) {
230 final List<YangInstanceIdentifier.PathArgument> domArgumentList = new ArrayList<>();
231 final BindingCodecTreeNode<?> childCodec = codec.bindingPathArgumentChild(arg, domArgumentList);
232 final Iterator<YangInstanceIdentifier.PathArgument> toEnter = domArgumentList.iterator();
233 DataTreeCandidateNode current = domData;
234 while (toEnter.hasNext() && current != null) {
235 current = current.getModifiedChild(toEnter.next());
237 if (current != null) {
238 return create(childCodec, current);
244 @SuppressWarnings("unchecked")
245 public <C extends Identifiable<K> & ChildOf<? super T>, K extends Identifier<C>> DataObjectModification<C>
246 getModifiedChildListItem(final Class<C> listItem, final K listKey) {
247 return (DataObjectModification<C>) getModifiedChild(IdentifiableItem.of(listItem, listKey));
251 @SuppressWarnings("unchecked")
252 public <H extends ChoiceIn<? super T> & DataObject, C extends Identifiable<K> & ChildOf<? super H>,
253 K extends Identifier<C>> DataObjectModification<C> getModifiedChildListItem(final Class<H> caseType,
254 final Class<C> listItem, final K listKey) {
255 return (DataObjectModification<C>) getModifiedChild(IdentifiableItem.of(caseType, listItem, listKey));
259 @SuppressWarnings("unchecked")
260 public <C extends ChildOf<? super T>> DataObjectModification<C> getModifiedChildContainer(final Class<C> child) {
261 return (DataObjectModification<C>) getModifiedChild(Item.of(child));
265 @SuppressWarnings("unchecked")
266 public <H extends ChoiceIn<? super T> & DataObject, C extends ChildOf<? super H>> DataObjectModification<C>
267 getModifiedChildContainer(final Class<H> caseType, final Class<C> child) {
268 return (DataObjectModification<C>) getModifiedChild(Item.of(caseType, child));
272 @SuppressWarnings("unchecked")
273 public <C extends Augmentation<T> & DataObject> DataObjectModification<C> getModifiedAugmentation(
274 final Class<C> augmentation) {
275 return (DataObjectModification<C>) getModifiedChild(Item.of(augmentation));
278 private T deserialize(final Optional<NormalizedNode<?, ?>> dataAfter) {
279 return dataAfter.map(codec::deserialize).orElse(null);
283 public String toString() {
284 return getClass().getSimpleName() + "{identifier = " + identifier + ", domData = " + domData + "}";