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.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.controller.md.sal.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}.
34 * {@link LazyDataObjectModification} represents Data tree change event,
35 * but whole tree is not translated or resolved eagerly, but only child nodes
36 * which are directly accessed by user of data object modification.
38 * @param <T> Type of Binding Data Object
40 final class LazyDataObjectModification<T extends DataObject> implements DataObjectModification<T> {
42 private final static Logger LOG = LoggerFactory.getLogger(LazyDataObjectModification.class);
44 private final BindingCodecTreeNode<T> codec;
45 private final DataTreeCandidateNode domData;
46 private final PathArgument identifier;
47 private Collection<DataObjectModification<? extends DataObject>> childNodesCache;
49 private LazyDataObjectModification(final BindingCodecTreeNode<T> codec, final DataTreeCandidateNode domData) {
50 this.codec = Preconditions.checkNotNull(codec);
51 this.domData = Preconditions.checkNotNull(domData);
52 this.identifier = codec.deserializePathArgument(domData.getIdentifier());
55 static <T extends DataObject> DataObjectModification<T> create(final BindingCodecTreeNode<T> codec,
56 final DataTreeCandidateNode domData) {
57 return new LazyDataObjectModification<>(codec,domData);
60 private static Collection<DataObjectModification<? extends DataObject>> from(final BindingCodecTreeNode<?> parentCodec,
61 final Collection<DataTreeCandidateNode> domChildNodes) {
62 final List<DataObjectModification<? extends DataObject>> result = new ArrayList<>(domChildNodes.size());
63 populateList(result, parentCodec, domChildNodes);
67 private static void populateList(final List<DataObjectModification<? extends DataObject>> result,
68 final BindingCodecTreeNode<?> parentCodec, final Collection<DataTreeCandidateNode> domChildNodes) {
69 for (final DataTreeCandidateNode domChildNode : domChildNodes) {
70 final BindingStructuralType type = BindingStructuralType.from(domChildNode);
71 if (type != BindingStructuralType.NOT_ADDRESSABLE) {
73 * Even if type is UNKNOWN, from perspective of BindingStructuralType
74 * we try to load codec for it. We will use that type to further specify
78 final BindingCodecTreeNode<?> childCodec =
79 parentCodec.yangPathArgumentChild(domChildNode.getIdentifier());
80 populateList(result,type, childCodec, domChildNode);
81 } catch (final IllegalArgumentException e) {
82 if (type == BindingStructuralType.UNKNOWN) {
83 LOG.debug("Unable to deserialize unknown DOM node {}",domChildNode,e);
85 LOG.debug("Binding representation for DOM node {} was not found",domChildNode,e);
92 private static void populateList(final List<DataObjectModification<? extends DataObject>> result,
93 final BindingStructuralType type, final BindingCodecTreeNode<?> childCodec,
94 final DataTreeCandidateNode domChildNode) {
97 // We use parent codec intentionally.
98 populateListWithSingleCodec(result, childCodec, domChildNode.getChildNodes());
100 case INVISIBLE_CONTAINER:
101 populateList(result, childCodec, domChildNode.getChildNodes());
104 case VISIBLE_CONTAINER:
105 result.add(create(childCodec, domChildNode));
111 private static void populateListWithSingleCodec(final List<DataObjectModification<? extends DataObject>> result,
112 final BindingCodecTreeNode<?> codec, final Collection<DataTreeCandidateNode> childNodes) {
113 for (final DataTreeCandidateNode child : childNodes) {
114 result.add(create(codec, child));
119 public T getDataBefore() {
120 return deserialize(domData.getDataBefore());
124 public T getDataAfter() {
125 return deserialize(domData.getDataAfter());
129 public Class<T> getDataType() {
130 return codec.getBindingClass();
134 public PathArgument getIdentifier() {
139 public DataObjectModification.ModificationType getModificationType() {
140 switch(domData.getModificationType()) {
143 return DataObjectModification.ModificationType.WRITE;
144 case SUBTREE_MODIFIED:
145 return DataObjectModification.ModificationType.SUBTREE_MODIFIED;
148 return DataObjectModification.ModificationType.DELETE;
150 // TODO: Should we lie about modification type instead of exception?
151 throw new IllegalStateException("Unsupported DOM Modification type " + domData.getModificationType());
156 public Collection<DataObjectModification<? extends DataObject>> getModifiedChildren() {
157 if (childNodesCache == null) {
158 childNodesCache = from(codec, domData.getChildNodes());
160 return childNodesCache;
164 public DataObjectModification<? extends DataObject> getModifiedChild(final PathArgument arg) {
165 final List<YangInstanceIdentifier.PathArgument> domArgumentList = new ArrayList<>();
166 final BindingCodecTreeNode<?> childCodec = codec.bindingPathArgumentChild(arg, domArgumentList);
167 final Iterator<YangInstanceIdentifier.PathArgument> toEnter = domArgumentList.iterator();
168 DataTreeCandidateNode current = domData;
169 while (toEnter.hasNext() && current != null) {
170 current = current.getModifiedChild(toEnter.next());
172 if (current != null) {
173 return create(childCodec, current);
179 @SuppressWarnings("unchecked")
180 public <C extends Identifiable<K> & ChildOf<? super T>, K extends Identifier<C>> DataObjectModification<C> getModifiedChildListItem(
181 final Class<C> listItem, final K listKey) {
182 return (DataObjectModification<C>) getModifiedChild(new InstanceIdentifier.IdentifiableItem<>(listItem, listKey));
186 @SuppressWarnings("unchecked")
187 public <C extends ChildOf<? super T>> DataObjectModification<C> getModifiedChildContainer(final Class<C> arg) {
188 return (DataObjectModification<C>) getModifiedChild(new InstanceIdentifier.Item<>(arg));
192 @SuppressWarnings("unchecked")
193 public <C extends Augmentation<T> & DataObject> DataObjectModification<C> getModifiedAugmentation(
194 final Class<C> augmentation) {
195 return (DataObjectModification<C>) getModifiedChild(new InstanceIdentifier.Item<>(augmentation));
198 private T deserialize(final Optional<NormalizedNode<?, ?>> dataAfter) {
199 if (dataAfter.isPresent()) {
200 return codec.deserialize(dataAfter.get());
206 public String toString() {
207 return getClass().getSimpleName() + "{identifier = " + identifier + ", domData = " + domData + "}";