1170828ce94946a023c2b5c437a860f91e2244f5
[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 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 SUBTREE_MODIFIED:
144                 return DataObjectModification.ModificationType.SUBTREE_MODIFIED;
145             case DELETE:
146                 return DataObjectModification.ModificationType.DELETE;
147
148             default:
149                 // TODO: Should we lie about modification type instead of exception?
150                 throw new IllegalStateException("Unsupported DOM Modification type " + domData.getModificationType());
151         }
152     }
153
154     @Override
155     public Collection<DataObjectModification<? extends DataObject>> getModifiedChildren() {
156         if (childNodesCache == null) {
157             childNodesCache = from(codec, domData.getChildNodes());
158         }
159         return childNodesCache;
160     }
161
162     @Override
163     public DataObjectModification<? extends DataObject> getModifiedChild(final PathArgument arg) {
164         final List<YangInstanceIdentifier.PathArgument> domArgumentList = new ArrayList<>();
165         final BindingCodecTreeNode<?> childCodec = codec.bindingPathArgumentChild(arg, domArgumentList);
166         final Iterator<YangInstanceIdentifier.PathArgument> toEnter = domArgumentList.iterator();
167         DataTreeCandidateNode current = domData;
168         while (toEnter.hasNext() && current != null) {
169             current = current.getModifiedChild(toEnter.next());
170         }
171         if (current != null) {
172             return create(childCodec, current);
173         }
174         return null;
175     }
176
177     @Override
178     @SuppressWarnings("unchecked")
179     public <C extends Identifiable<K> & ChildOf<? super T>, K extends Identifier<C>> DataObjectModification<C> getModifiedChildListItem(
180             final Class<C> listItem, final K listKey) {
181         return (DataObjectModification<C>) getModifiedChild(new InstanceIdentifier.IdentifiableItem<>(listItem, listKey));
182     }
183
184     @Override
185     @SuppressWarnings("unchecked")
186     public <C extends ChildOf<? super T>> DataObjectModification<C> getModifiedChildContainer(final Class<C> arg) {
187         return (DataObjectModification<C>) getModifiedChild(new InstanceIdentifier.Item<>(arg));
188     }
189
190     @Override
191     @SuppressWarnings("unchecked")
192     public <C extends Augmentation<T> & DataObject> DataObjectModification<C> getModifiedAugmentation(
193             final Class<C> augmentation) {
194         return (DataObjectModification<C>) getModifiedChild(new InstanceIdentifier.Item<>(augmentation));
195     }
196
197     private T deserialize(final Optional<NormalizedNode<?, ?>> dataAfter) {
198         if (dataAfter.isPresent()) {
199             return codec.deserialize(dataAfter.get());
200         }
201         return null;
202     }
203 }