Fix checkstyle violations in sal-binding-broker
[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.adapter.BindingStructuralType;
18 import org.opendaylight.mdsal.binding.dom.codec.api.BindingCodecTreeNode;
19 import org.opendaylight.yangtools.yang.binding.Augmentation;
20 import org.opendaylight.yangtools.yang.binding.ChildOf;
21 import org.opendaylight.yangtools.yang.binding.DataObject;
22 import org.opendaylight.yangtools.yang.binding.Identifiable;
23 import org.opendaylight.yangtools.yang.binding.Identifier;
24 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
25 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.PathArgument;
26 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
27 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
28 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 /**
33  * Lazily translated {@link DataObjectModification} based on {@link DataTreeCandidateNode}.
34  *
35  * {@link LazyDataObjectModification} represents Data tree change event,
36  * but whole tree is not translated or resolved eagerly, but only child nodes
37  * which are directly accessed by user of data object modification.
38  *
39  * @param <T> Type of Binding Data Object
40  */
41 final class LazyDataObjectModification<T extends DataObject> implements DataObjectModification<T> {
42
43     private static final Logger LOG = LoggerFactory.getLogger(LazyDataObjectModification.class);
44
45     private final BindingCodecTreeNode<T> codec;
46     private final DataTreeCandidateNode domData;
47     private final PathArgument identifier;
48
49     private volatile Collection<DataObjectModification<? extends DataObject>> childNodesCache;
50     private volatile ModificationType modificationType;
51
52     private LazyDataObjectModification(final BindingCodecTreeNode<T> codec, final DataTreeCandidateNode domData) {
53         this.codec = Preconditions.checkNotNull(codec);
54         this.domData = Preconditions.checkNotNull(domData);
55         this.identifier = codec.deserializePathArgument(domData.getIdentifier());
56     }
57
58     static <T extends DataObject> DataObjectModification<T> create(final BindingCodecTreeNode<T> codec,
59             final DataTreeCandidateNode domData) {
60         return new LazyDataObjectModification<>(codec,domData);
61     }
62
63     private static Collection<DataObjectModification<? extends DataObject>> from(
64             final BindingCodecTreeNode<?> parentCodec, final Collection<DataTreeCandidateNode> domChildNodes) {
65         final List<DataObjectModification<? extends DataObject>> result = new ArrayList<>(domChildNodes.size());
66         populateList(result, parentCodec, domChildNodes);
67         return result;
68     }
69
70     private static void populateList(final List<DataObjectModification<? extends DataObject>> result,
71             final BindingCodecTreeNode<?> parentCodec, final Collection<DataTreeCandidateNode> domChildNodes) {
72         for (final DataTreeCandidateNode domChildNode : domChildNodes) {
73             final BindingStructuralType type = BindingStructuralType.from(domChildNode);
74             if (type != BindingStructuralType.NOT_ADDRESSABLE) {
75                 /*
76                  *  Even if type is UNKNOWN, from perspective of BindingStructuralType
77                  *  we try to load codec for it. We will use that type to further specify
78                  *  debug log.
79                  */
80                 try {
81                     final BindingCodecTreeNode<?> childCodec =
82                             parentCodec.yangPathArgumentChild(domChildNode.getIdentifier());
83                     populateList(result,type, childCodec, domChildNode);
84                 } catch (final IllegalArgumentException e) {
85                     if (type == BindingStructuralType.UNKNOWN) {
86                         LOG.debug("Unable to deserialize unknown DOM node {}",domChildNode,e);
87                     } else {
88                         LOG.debug("Binding representation for DOM node {} was not found",domChildNode,e);
89                     }
90                 }
91             }
92         }
93     }
94
95     private static void populateList(final List<DataObjectModification<? extends DataObject>> result,
96             final BindingStructuralType type, final BindingCodecTreeNode<?> childCodec,
97             final DataTreeCandidateNode domChildNode) {
98         switch (type) {
99             case INVISIBLE_LIST:
100                 // We use parent codec intentionally.
101                 populateListWithSingleCodec(result, childCodec, domChildNode.getChildNodes());
102                 break;
103             case INVISIBLE_CONTAINER:
104                 populateList(result, childCodec, domChildNode.getChildNodes());
105                 break;
106             case UNKNOWN:
107             case VISIBLE_CONTAINER:
108                 result.add(create(childCodec, domChildNode));
109                 break;
110             default:
111                 break;
112         }
113     }
114
115     private static void populateListWithSingleCodec(final List<DataObjectModification<? extends DataObject>> result,
116             final BindingCodecTreeNode<?> codec, final Collection<DataTreeCandidateNode> childNodes) {
117         for (final DataTreeCandidateNode child : childNodes) {
118             result.add(create(codec, child));
119         }
120     }
121
122     @Override
123     public T getDataBefore() {
124         return deserialize(domData.getDataBefore());
125     }
126
127     @Override
128     public T getDataAfter() {
129         return deserialize(domData.getDataAfter());
130     }
131
132     @Override
133     public Class<T> getDataType() {
134         return codec.getBindingClass();
135     }
136
137     @Override
138     public PathArgument getIdentifier() {
139         return identifier;
140     }
141
142     @Override
143     public ModificationType getModificationType() {
144         ModificationType localType = modificationType;
145         if (localType != null) {
146             return localType;
147         }
148
149         switch (domData.getModificationType()) {
150             case APPEARED:
151             case WRITE:
152                 localType = ModificationType.WRITE;
153                 break;
154             case DISAPPEARED:
155             case DELETE:
156                 localType = ModificationType.DELETE;
157                 break;
158             case SUBTREE_MODIFIED:
159                 localType = resolveSubtreeModificationType();
160                 break;
161             default:
162                 // TODO: Should we lie about modification type instead of exception?
163                 throw new IllegalStateException("Unsupported DOM Modification type " + domData.getModificationType());
164         }
165
166         modificationType = localType;
167         return localType;
168     }
169
170     private ModificationType resolveSubtreeModificationType() {
171         switch (codec.getChildAddressabilitySummary()) {
172             case ADDRESSABLE:
173                 // All children are addressable, it is safe to report SUBTREE_MODIFIED
174                 return ModificationType.SUBTREE_MODIFIED;
175             case UNADDRESSABLE:
176                 // All children are non-addressable, report WRITE
177                 return ModificationType.WRITE;
178             case MIXED:
179                 // This case is not completely trivial, as we may have NOT_ADDRESSABLE nodes underneath us. If that
180                 // is the case, we need to turn this modification into a WRITE operation, so that the user is able
181                 // to observe those nodes being introduced. This is not efficient, but unfortunately unavoidable,
182                 // as we cannot accurately represent such changes.
183                 for (DataTreeCandidateNode child : domData.getChildNodes()) {
184                     if (BindingStructuralType.recursiveFrom(child) == BindingStructuralType.NOT_ADDRESSABLE) {
185                         // We have a non-addressable child, turn this modification into a write
186                         return ModificationType.WRITE;
187                     }
188                 }
189
190                 // No unaddressable children found, proceed in addressed mode
191                 return ModificationType.SUBTREE_MODIFIED;
192             default:
193                 throw new IllegalStateException("Unsupported child addressability summary "
194                         + codec.getChildAddressabilitySummary());
195         }
196     }
197
198     @Override
199     public Collection<DataObjectModification<? extends DataObject>> getModifiedChildren() {
200         Collection<DataObjectModification<? extends DataObject>> local = childNodesCache;
201         if (local == null) {
202             childNodesCache = local = from(codec, domData.getChildNodes());
203         }
204         return local;
205     }
206
207     @Override
208     public DataObjectModification<? extends DataObject> getModifiedChild(final PathArgument arg) {
209         final List<YangInstanceIdentifier.PathArgument> domArgumentList = new ArrayList<>();
210         final BindingCodecTreeNode<?> childCodec = codec.bindingPathArgumentChild(arg, domArgumentList);
211         final Iterator<YangInstanceIdentifier.PathArgument> toEnter = domArgumentList.iterator();
212         DataTreeCandidateNode current = domData;
213         while (toEnter.hasNext() && current != null) {
214             current = current.getModifiedChild(toEnter.next());
215         }
216         if (current != null) {
217             return create(childCodec, current);
218         }
219         return null;
220     }
221
222     @Override
223     @SuppressWarnings("unchecked")
224     public <C extends Identifiable<K> & ChildOf<? super T>, K extends Identifier<C>> DataObjectModification<C>
225             getModifiedChildListItem(final Class<C> listItem, final K listKey) {
226         return (DataObjectModification<C>) getModifiedChild(new InstanceIdentifier.IdentifiableItem<>(
227                 listItem, listKey));
228     }
229
230     @Override
231     @SuppressWarnings("unchecked")
232     public <C extends ChildOf<? super T>> DataObjectModification<C> getModifiedChildContainer(final Class<C> arg) {
233         return (DataObjectModification<C>) getModifiedChild(new InstanceIdentifier.Item<>(arg));
234     }
235
236     @Override
237     @SuppressWarnings("unchecked")
238     public <C extends Augmentation<T> & DataObject> DataObjectModification<C> getModifiedAugmentation(
239             final Class<C> augmentation) {
240         return (DataObjectModification<C>) getModifiedChild(new InstanceIdentifier.Item<>(augmentation));
241     }
242
243     private T deserialize(final Optional<NormalizedNode<?, ?>> dataAfter) {
244         if (dataAfter.isPresent()) {
245             return codec.deserialize(dataAfter.get());
246         }
247         return null;
248     }
249
250     @Override
251     public String toString() {
252         return getClass().getSimpleName() + "{identifier = " + identifier + ", domData = " + domData + "}";
253     }
254 }