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 static com.google.common.base.Verify.verify;
11 import static java.util.Objects.requireNonNull;
12 import static org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType.UNMODIFIED;
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.Iterator;
17 import java.util.List;
18 import java.util.Optional;
19 import java.util.stream.Collectors;
20 import java.util.stream.Stream;
21 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
22 import org.opendaylight.mdsal.binding.dom.adapter.BindingStructuralType;
23 import org.opendaylight.mdsal.binding.dom.codec.api.BindingCodecTreeNode;
24 import org.opendaylight.mdsal.binding.dom.codec.api.BindingDataObjectCodecTreeNode;
25 import org.opendaylight.yangtools.yang.binding.Augmentation;
26 import org.opendaylight.yangtools.yang.binding.ChildOf;
27 import org.opendaylight.yangtools.yang.binding.ChoiceIn;
28 import org.opendaylight.yangtools.yang.binding.DataObject;
29 import org.opendaylight.yangtools.yang.binding.Identifiable;
30 import org.opendaylight.yangtools.yang.binding.Identifier;
31 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.IdentifiableItem;
32 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.Item;
33 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.PathArgument;
34 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
35 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
36 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
41 * Lazily translated {@link DataObjectModification} based on {@link DataTreeCandidateNode}.
43 * {@link LazyDataObjectModification} represents Data tree change event,
44 * but whole tree is not translated or resolved eagerly, but only child nodes
45 * which are directly accessed by user of data object modification.
47 * @param <T> Type of Binding Data Object
50 final class LazyDataObjectModification<T extends DataObject> implements DataObjectModification<T> {
52 private static final Logger LOG = LoggerFactory.getLogger(LazyDataObjectModification.class);
54 private final BindingDataObjectCodecTreeNode<T> codec;
55 private final DataTreeCandidateNode domData;
56 private final PathArgument identifier;
58 private volatile Collection<LazyDataObjectModification<? extends DataObject>> childNodesCache;
59 private volatile ModificationType modificationType;
61 private LazyDataObjectModification(final BindingDataObjectCodecTreeNode<T> codec,
62 final DataTreeCandidateNode domData) {
63 this.codec = requireNonNull(codec);
64 this.domData = requireNonNull(domData);
65 this.identifier = codec.deserializePathArgument(domData.getIdentifier());
68 static <T extends DataObject> LazyDataObjectModification<T> create(final BindingDataObjectCodecTreeNode<T> codec,
69 final DataTreeCandidateNode domData) {
70 return new LazyDataObjectModification<>(codec, domData);
73 private static Collection<LazyDataObjectModification<? extends DataObject>> from(
74 final BindingDataObjectCodecTreeNode<?> parentCodec,
75 final Collection<DataTreeCandidateNode> domChildNodes) {
76 final List<LazyDataObjectModification<? extends DataObject>> result = new ArrayList<>(domChildNodes.size());
77 populateList(result, parentCodec, domChildNodes);
81 private static void populateList(final List<LazyDataObjectModification<? extends DataObject>> result,
82 final BindingDataObjectCodecTreeNode<?> parentCodec,
83 final Collection<DataTreeCandidateNode> domChildNodes) {
84 for (final DataTreeCandidateNode domChildNode : domChildNodes) {
85 if (domChildNode.getModificationType() != UNMODIFIED) {
86 final BindingStructuralType type = BindingStructuralType.from(domChildNode);
87 if (type != BindingStructuralType.NOT_ADDRESSABLE) {
89 * Even if type is UNKNOWN, from perspective of BindingStructuralType
90 * we try to load codec for it. We will use that type to further specify
94 final BindingCodecTreeNode childCodec = parentCodec.yangPathArgumentChild(
95 domChildNode.getIdentifier());
96 verify(childCodec instanceof BindingDataObjectCodecTreeNode, "Unhandled codec %s for type %s",
98 populateList(result, type, (BindingDataObjectCodecTreeNode<?>) childCodec, domChildNode);
99 } catch (final IllegalArgumentException e) {
100 if (type == BindingStructuralType.UNKNOWN) {
101 LOG.debug("Unable to deserialize unknown DOM node {}", domChildNode, e);
103 LOG.debug("Binding representation for DOM node {} was not found", domChildNode, e);
111 private static void populateList(final List<LazyDataObjectModification<? extends DataObject>> result,
112 final BindingStructuralType type, final BindingDataObjectCodecTreeNode<?> childCodec,
113 final DataTreeCandidateNode domChildNode) {
116 // We use parent codec intentionally.
117 populateListWithSingleCodec(result, childCodec, domChildNode.getChildNodes());
119 case INVISIBLE_CONTAINER:
120 populateList(result, childCodec, domChildNode.getChildNodes());
123 case VISIBLE_CONTAINER:
124 result.add(create(childCodec, domChildNode));
131 private static void populateListWithSingleCodec(final List<LazyDataObjectModification<? extends DataObject>> result,
132 final BindingDataObjectCodecTreeNode<?> codec, final Collection<DataTreeCandidateNode> childNodes) {
133 for (final DataTreeCandidateNode child : childNodes) {
134 if (child.getModificationType() != UNMODIFIED) {
135 result.add(create(codec, child));
141 public T getDataBefore() {
142 return deserialize(domData.getDataBefore());
146 public T getDataAfter() {
147 return deserialize(domData.getDataAfter());
151 public Class<T> getDataType() {
152 return codec.getBindingClass();
156 public PathArgument getIdentifier() {
161 public ModificationType getModificationType() {
162 ModificationType localType = modificationType;
163 if (localType != null) {
167 switch (domData.getModificationType()) {
170 localType = ModificationType.WRITE;
174 localType = ModificationType.DELETE;
176 case SUBTREE_MODIFIED:
177 localType = resolveSubtreeModificationType();
180 // TODO: Should we lie about modification type instead of exception?
181 throw new IllegalStateException("Unsupported DOM Modification type " + domData.getModificationType());
184 modificationType = localType;
188 private ModificationType resolveSubtreeModificationType() {
189 switch (codec.getChildAddressabilitySummary()) {
191 // All children are addressable, it is safe to report SUBTREE_MODIFIED
192 return ModificationType.SUBTREE_MODIFIED;
194 // All children are non-addressable, report WRITE
195 return ModificationType.WRITE;
197 // This case is not completely trivial, as we may have NOT_ADDRESSABLE nodes underneath us. If that
198 // is the case, we need to turn this modification into a WRITE operation, so that the user is able
199 // to observe those nodes being introduced. This is not efficient, but unfortunately unavoidable,
200 // as we cannot accurately represent such changes.
201 for (DataTreeCandidateNode child : domData.getChildNodes()) {
202 if (BindingStructuralType.recursiveFrom(child) == BindingStructuralType.NOT_ADDRESSABLE) {
203 // We have a non-addressable child, turn this modification into a write
204 return ModificationType.WRITE;
208 // No unaddressable children found, proceed in addressed mode
209 return ModificationType.SUBTREE_MODIFIED;
211 throw new IllegalStateException("Unsupported child addressability summary "
212 + codec.getChildAddressabilitySummary());
217 public Collection<LazyDataObjectModification<? extends DataObject>> getModifiedChildren() {
218 Collection<LazyDataObjectModification<? extends DataObject>> local = childNodesCache;
220 childNodesCache = local = from(codec, domData.getChildNodes());
226 public <H extends ChoiceIn<? super T> & DataObject, C extends ChildOf<? super H>>
227 Collection<DataObjectModification<C>> getModifiedChildren(final Class<H> caseType,
228 final Class<C> childType) {
229 return streamModifiedChildren(childType)
230 .filter(child -> caseType.equals(child.identifier.getCaseType().orElse(null)))
231 .collect(Collectors.toList());
234 @SuppressWarnings("unchecked")
235 private <C extends DataObject> Stream<LazyDataObjectModification<C>> streamModifiedChildren(
236 final Class<C> childType) {
237 return getModifiedChildren().stream()
238 .filter(child -> childType.isAssignableFrom(child.getDataType()))
239 .map(child -> (LazyDataObjectModification<C>) child);
243 public DataObjectModification<? extends DataObject> getModifiedChild(final PathArgument arg) {
244 final List<YangInstanceIdentifier.PathArgument> domArgumentList = new ArrayList<>();
245 final BindingDataObjectCodecTreeNode<?> childCodec = codec.bindingPathArgumentChild(arg, domArgumentList);
246 final Iterator<YangInstanceIdentifier.PathArgument> toEnter = domArgumentList.iterator();
247 DataTreeCandidateNode current = domData;
248 while (toEnter.hasNext() && current != null) {
249 current = current.getModifiedChild(toEnter.next()).orElse(null);
251 return current != null && current.getModificationType() != UNMODIFIED ? create(childCodec, current) : null;
255 @SuppressWarnings("unchecked")
256 public <C extends Identifiable<K> & ChildOf<? super T>, K extends Identifier<C>> DataObjectModification<C>
257 getModifiedChildListItem(final Class<C> listItem, final K listKey) {
258 return (DataObjectModification<C>) getModifiedChild(IdentifiableItem.of(listItem, listKey));
262 @SuppressWarnings("unchecked")
263 public <H extends ChoiceIn<? super T> & DataObject, C extends Identifiable<K> & ChildOf<? super H>,
264 K extends Identifier<C>> DataObjectModification<C> getModifiedChildListItem(final Class<H> caseType,
265 final Class<C> listItem, final K listKey) {
266 return (DataObjectModification<C>) getModifiedChild(IdentifiableItem.of(caseType, listItem, listKey));
270 @SuppressWarnings("unchecked")
271 public <C extends ChildOf<? super T>> DataObjectModification<C> getModifiedChildContainer(final Class<C> child) {
272 return (DataObjectModification<C>) getModifiedChild(Item.of(child));
276 @SuppressWarnings("unchecked")
277 public <H extends ChoiceIn<? super T> & DataObject, C extends ChildOf<? super H>> DataObjectModification<C>
278 getModifiedChildContainer(final Class<H> caseType, final Class<C> child) {
279 return (DataObjectModification<C>) getModifiedChild(Item.of(caseType, child));
283 @SuppressWarnings("unchecked")
284 public <C extends Augmentation<T> & DataObject> DataObjectModification<C> getModifiedAugmentation(
285 final Class<C> augmentation) {
286 return (DataObjectModification<C>) getModifiedChild(Item.of(augmentation));
289 private T deserialize(final Optional<NormalizedNode<?, ?>> dataAfter) {
290 return dataAfter.map(codec::deserialize).orElse(null);
294 public String toString() {
295 return getClass().getSimpleName() + "{identifier = " + identifier + ", domData = " + domData + "}";