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.yangtools.binding.data.codec.api.BindingCodecTreeNode;
18 import org.opendaylight.yangtools.yang.binding.ChildOf;
19 import org.opendaylight.yangtools.yang.binding.DataObject;
20 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
21 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.PathArgument;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
23 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
24 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
29 * Lazily translated {@link DataObjectModification} based on {@link DataTreeCandidateNode}.
31 * {@link LazyDataObjectModification} represents Data tree change event,
32 * but whole tree is not translated or resolved eagerly, but only child nodes
33 * which are directly accessed by user of data object modification.
35 * @param <T> Type of Binding Data Object
37 class LazyDataObjectModification<T extends DataObject> implements DataObjectModification<T> {
39 private final static Logger LOG = LoggerFactory.getLogger(LazyDataObjectModification.class);
41 private final BindingCodecTreeNode<T> codec;
42 private final DataTreeCandidateNode domData;
43 private final PathArgument identifier;
44 private Collection<DataObjectModification<? extends DataObject>> childNodesCache;
46 private LazyDataObjectModification(final BindingCodecTreeNode<T> codec, final DataTreeCandidateNode domData) {
47 this.codec = Preconditions.checkNotNull(codec);
48 this.domData = Preconditions.checkNotNull(domData);
49 this.identifier = codec.deserializePathArgument(domData.getIdentifier());
52 static DataObjectModification<? extends DataObject> create(final BindingCodecTreeNode<?> codec,
53 final DataTreeCandidateNode domData) {
54 return new LazyDataObjectModification<>(codec,domData);
57 static Collection<DataObjectModification<? extends DataObject>> from(final BindingCodecTreeNode<?> parentCodec,
58 final Collection<DataTreeCandidateNode> domChildNodes) {
59 final ArrayList<DataObjectModification<? extends DataObject>> result = new ArrayList<>(domChildNodes.size());
60 populateList(result, parentCodec, domChildNodes);
64 private static void populateList(final List<DataObjectModification<? extends DataObject>> result,
65 final BindingCodecTreeNode<?> parentCodec, final Collection<DataTreeCandidateNode> domChildNodes) {
66 for (final DataTreeCandidateNode domChildNode : domChildNodes) {
67 final BindingStructuralType type = BindingStructuralType.from(domChildNode);
68 if (type != BindingStructuralType.NOT_ADDRESSABLE) {
70 * Even if type is UNKNOWN, from perspective of BindingStructuralType
71 * we try to load codec for it. We will use that type to further specify
75 final BindingCodecTreeNode<?> childCodec =
76 parentCodec.yangPathArgumentChild(domChildNode.getIdentifier());
77 populateList(result,type, childCodec, domChildNode);
78 } catch (final IllegalArgumentException e) {
79 if(type == BindingStructuralType.UNKNOWN) {
80 LOG.debug("Unable to deserialize unknown DOM node {}",domChildNode,e);
82 LOG.debug("Binding representation for DOM node {} was not found",domChildNode,e);
90 private static void populateList(final List<DataObjectModification<? extends DataObject>> result,
91 final BindingStructuralType type, final BindingCodecTreeNode<?> childCodec,
92 final DataTreeCandidateNode domChildNode) {
95 // We use parent codec intentionally.
96 populateListWithSingleCodec(result, childCodec, domChildNode.getChildNodes());
98 case INVISIBLE_CONTAINER:
99 populateList(result, childCodec, domChildNode.getChildNodes());
102 case VISIBLE_CONTAINER:
103 result.add(create(childCodec, domChildNode));
109 private static void populateListWithSingleCodec(final List<DataObjectModification<? extends DataObject>> result,
110 final BindingCodecTreeNode<?> codec, final Collection<DataTreeCandidateNode> childNodes) {
111 for (final DataTreeCandidateNode child : childNodes) {
112 result.add(create(codec, child));
117 public T getDataAfter() {
118 return deserialize(domData.getDataAfter());
122 public Class<T> getDataType() {
123 return codec.getBindingClass();
127 public PathArgument getIdentifier() {
132 public DataObjectModification.ModificationType getModificationType() {
133 switch(domData.getModificationType()) {
135 return DataObjectModification.ModificationType.WRITE;
136 case SUBTREE_MODIFIED:
137 return DataObjectModification.ModificationType.SUBTREE_MODIFIED;
139 return DataObjectModification.ModificationType.DELETE;
142 // TODO: Should we lie about modification type instead of exception?
143 throw new IllegalStateException("Unsupported DOM Modification type " + domData.getModificationType());
148 public Collection<DataObjectModification<? extends DataObject>> getModifiedChildren() {
149 if(childNodesCache == null) {
150 childNodesCache = from(codec,domData.getChildNodes());
152 return childNodesCache;
155 public DataObjectModification<? extends DataObject> getModifiedChild(final PathArgument arg) {
156 final List<YangInstanceIdentifier.PathArgument> domArgumentList = new ArrayList<>();
157 final BindingCodecTreeNode<?> childCodec = codec.bindingPathArgumentChild(arg, domArgumentList);
158 final Iterator<YangInstanceIdentifier.PathArgument> toEnter = domArgumentList.iterator();
159 DataTreeCandidateNode current = domData;
160 while (toEnter.hasNext() && current != null) {
161 current = current.getModifiedChild(toEnter.next());
163 if (current != null) {
164 return create(childCodec, current);
169 @SuppressWarnings("unchecked")
170 public <C extends ChildOf<T>> DataObjectModification<C> getModifiedChild(final Class<C> arg) {
171 return (DataObjectModification<C>) getModifiedChild(new InstanceIdentifier.Item<>(arg));
174 private T deserialize(final Optional<NormalizedNode<?, ?>> dataAfter) {
175 if(dataAfter.isPresent()) {
176 return codec.deserialize(dataAfter.get());