Adjust to yangtools-2.0.0 changes
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / main / java / org / opendaylight / controller / md / sal / binding / impl / LazyDataObjectModification.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.md.sal.binding.impl;
9
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.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;
30
31 /**
32  * Lazily translated {@link DataObjectModification} based on {@link DataTreeCandidateNode}.
33  *
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.
37  *
38  * @param <T> Type of Binding Data Object
39  */
40 final class LazyDataObjectModification<T extends DataObject> implements DataObjectModification<T> {
41
42     private final static Logger LOG = LoggerFactory.getLogger(LazyDataObjectModification.class);
43
44     private final BindingCodecTreeNode<T> codec;
45     private final DataTreeCandidateNode domData;
46     private final PathArgument identifier;
47     private Collection<DataObjectModification<? extends DataObject>> childNodesCache;
48
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());
53     }
54
55     static <T extends DataObject> DataObjectModification<T> create(final BindingCodecTreeNode<T> codec,
56             final DataTreeCandidateNode domData) {
57         return new LazyDataObjectModification<>(codec,domData);
58     }
59
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);
64         return result;
65     }
66
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) {
72                 /*
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
75                  *  debug log.
76                  */
77                 try {
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);
84                     } else {
85                         LOG.debug("Binding representation for DOM node {} was not found",domChildNode,e);
86                     }
87                 }
88             }
89         }
90     }
91
92     private static void populateList(final List<DataObjectModification<? extends DataObject>> result,
93             final BindingStructuralType type, final BindingCodecTreeNode<?> childCodec,
94             final DataTreeCandidateNode domChildNode) {
95         switch (type) {
96             case INVISIBLE_LIST:
97                 // We use parent codec intentionally.
98                 populateListWithSingleCodec(result, childCodec, domChildNode.getChildNodes());
99                 break;
100             case INVISIBLE_CONTAINER:
101                 populateList(result, childCodec, domChildNode.getChildNodes());
102                 break;
103             case UNKNOWN:
104             case VISIBLE_CONTAINER:
105                 result.add(create(childCodec, domChildNode));
106             default:
107                 break;
108         }
109     }
110
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));
115         }
116     }
117
118     @Override
119     public T getDataBefore() {
120         return deserialize(domData.getDataBefore());
121     }
122
123     @Override
124     public T getDataAfter() {
125         return deserialize(domData.getDataAfter());
126     }
127
128     @Override
129     public Class<T> getDataType() {
130         return codec.getBindingClass();
131     }
132
133     @Override
134     public PathArgument getIdentifier() {
135         return identifier;
136     }
137
138     @Override
139     public DataObjectModification.ModificationType getModificationType() {
140         switch(domData.getModificationType()) {
141             case WRITE:
142                 return DataObjectModification.ModificationType.WRITE;
143             case APPEARED:
144             case SUBTREE_MODIFIED:
145                 return DataObjectModification.ModificationType.SUBTREE_MODIFIED;
146             case DISAPPEARED:
147             case DELETE:
148                 return DataObjectModification.ModificationType.DELETE;
149             default:
150                 // TODO: Should we lie about modification type instead of exception?
151                 throw new IllegalStateException("Unsupported DOM Modification type " + domData.getModificationType());
152         }
153     }
154
155     @Override
156     public Collection<DataObjectModification<? extends DataObject>> getModifiedChildren() {
157         if (childNodesCache == null) {
158             childNodesCache = from(codec, domData.getChildNodes());
159         }
160         return childNodesCache;
161     }
162
163     @Override
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());
171         }
172         if (current != null) {
173             return create(childCodec, current);
174         }
175         return null;
176     }
177
178     @Override
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));
183     }
184
185     @Override
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));
189     }
190
191     @Override
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));
196     }
197
198     private T deserialize(final Optional<NormalizedNode<?, ?>> dataAfter) {
199         if (dataAfter.isPresent()) {
200             return codec.deserialize(dataAfter.get());
201         }
202         return null;
203     }
204
205     @Override
206     public String toString() {
207         return getClass().getSimpleName() + "{identifier = " + identifier + ", domData = " + domData + "}";
208     }
209 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.