Bug 2933: Make DataTreeModification and Listeners type-safe
[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.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.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 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     static Collection<DataObjectModification<? extends DataObject>> from(final BindingCodecTreeNode<?> parentCodec,
61             final Collection<DataTreeCandidateNode> domChildNodes) {
62         final ArrayList<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
93     private static void populateList(final List<DataObjectModification<? extends DataObject>> result,
94             final BindingStructuralType type, final BindingCodecTreeNode<?> childCodec,
95             final DataTreeCandidateNode domChildNode) {
96         switch (type) {
97             case INVISIBLE_LIST:
98                 // We use parent codec intentionally.
99                 populateListWithSingleCodec(result, childCodec, domChildNode.getChildNodes());
100                 break;
101             case INVISIBLE_CONTAINER:
102                 populateList(result, childCodec, domChildNode.getChildNodes());
103                 break;
104             case UNKNOWN:
105             case VISIBLE_CONTAINER:
106                 result.add(create(childCodec, domChildNode));
107             default:
108                 break;
109         }
110     }
111
112     private static void populateListWithSingleCodec(final List<DataObjectModification<? extends DataObject>> result,
113             final BindingCodecTreeNode<?> codec, final Collection<DataTreeCandidateNode> childNodes) {
114         for (final DataTreeCandidateNode child : childNodes) {
115             result.add(create(codec, child));
116         }
117     }
118
119     @Override
120     public T getDataAfter() {
121         return deserialize(domData.getDataAfter());
122     }
123
124     @Override
125     public Class<T> getDataType() {
126         return codec.getBindingClass();
127     }
128
129     @Override
130     public PathArgument getIdentifier() {
131         return identifier;
132     }
133
134     @Override
135     public DataObjectModification.ModificationType getModificationType() {
136         switch(domData.getModificationType()) {
137             case WRITE:
138                 return DataObjectModification.ModificationType.WRITE;
139             case SUBTREE_MODIFIED:
140                 return DataObjectModification.ModificationType.SUBTREE_MODIFIED;
141             case DELETE:
142                 return DataObjectModification.ModificationType.DELETE;
143
144             default:
145                 // TODO: Should we lie about modification type instead of exception?
146                 throw new IllegalStateException("Unsupported DOM Modification type " + domData.getModificationType());
147         }
148     }
149
150     @Override
151     public Collection<DataObjectModification<? extends DataObject>> getModifiedChildren() {
152         if(childNodesCache == null) {
153             childNodesCache = from(codec,domData.getChildNodes());
154         }
155         return childNodesCache;
156     }
157
158     @Override
159     public DataObjectModification<? extends DataObject> getModifiedChild(final PathArgument arg) {
160         final List<YangInstanceIdentifier.PathArgument> domArgumentList = new ArrayList<>();
161         final BindingCodecTreeNode<?> childCodec = codec.bindingPathArgumentChild(arg, domArgumentList);
162         final Iterator<YangInstanceIdentifier.PathArgument> toEnter = domArgumentList.iterator();
163         DataTreeCandidateNode current = domData;
164         while (toEnter.hasNext() && current != null) {
165             current = current.getModifiedChild(toEnter.next());
166         }
167         if (current != null) {
168             return create(childCodec, current);
169         }
170         return null;
171     }
172
173     @Override
174     @SuppressWarnings("unchecked")
175     public <C extends Identifiable<K> & ChildOf<? super T>, K extends Identifier<C>> DataObjectModification<C> getModifiedChildListItem(
176             final Class<C> listItem, final K listKey) {
177         return (DataObjectModification<C>) getModifiedChild(new InstanceIdentifier.IdentifiableItem<>(listItem, listKey));
178     }
179
180     @Override
181     @SuppressWarnings("unchecked")
182     public <C extends ChildOf<? super T>> DataObjectModification<C> getModifiedChildContainer(final Class<C> arg) {
183         return (DataObjectModification<C>) getModifiedChild(new InstanceIdentifier.Item<>(arg));
184     }
185
186     @Override
187     @SuppressWarnings("unchecked")
188     public <C extends Augmentation<T> & DataObject> DataObjectModification<C> getModifiedAugmentation(
189             final Class<C> augmentation) {
190         return (DataObjectModification<C>) getModifiedChild(new InstanceIdentifier.Item<>(augmentation));
191     }
192
193     private T deserialize(final Optional<NormalizedNode<?, ?>> dataAfter) {
194         if(dataAfter.isPresent()) {
195             return codec.deserialize(dataAfter.get());
196         }
197         return null;
198     }
199 }