Bug 2933: Implemented DataTreeChangeService
[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.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;
27
28 /**
29  * Lazily translated {@link DataObjectModification} based on {@link DataTreeCandidateNode}.
30  *
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.
34  *
35  * @param <T> Type of Binding Data Object
36  */
37 class LazyDataObjectModification<T extends DataObject> implements DataObjectModification<T> {
38
39     private final static Logger LOG = LoggerFactory.getLogger(LazyDataObjectModification.class);
40
41     private final BindingCodecTreeNode<T> codec;
42     private final DataTreeCandidateNode domData;
43     private final PathArgument identifier;
44     private Collection<DataObjectModification<? extends DataObject>> childNodesCache;
45
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());
50     }
51
52     static DataObjectModification<? extends DataObject> create(final BindingCodecTreeNode<?> codec,
53             final DataTreeCandidateNode domData) {
54         return new LazyDataObjectModification<>(codec,domData);
55     }
56
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);
61         return result;
62     }
63
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) {
69                 /*
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
72                  *  debug log.
73                  */
74                 try {
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);
81                     } else {
82                         LOG.debug("Binding representation for DOM node {} was not found",domChildNode,e);
83                     }
84                 }
85             }
86         }
87     }
88
89
90     private static void populateList(final List<DataObjectModification<? extends DataObject>> result,
91             final BindingStructuralType type, final BindingCodecTreeNode<?> childCodec,
92             final DataTreeCandidateNode domChildNode) {
93         switch (type) {
94             case INVISIBLE_LIST:
95                 // We use parent codec intentionally.
96                 populateListWithSingleCodec(result, childCodec, domChildNode.getChildNodes());
97                 break;
98             case INVISIBLE_CONTAINER:
99                 populateList(result, childCodec, domChildNode.getChildNodes());
100                 break;
101             case UNKNOWN:
102             case VISIBLE_CONTAINER:
103                 result.add(create(childCodec, domChildNode));
104             default:
105                 break;
106         }
107     }
108
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));
113         }
114     }
115
116     @Override
117     public T getDataAfter() {
118         return deserialize(domData.getDataAfter());
119     }
120
121     @Override
122     public Class<T> getDataType() {
123         return codec.getBindingClass();
124     }
125
126     @Override
127     public PathArgument getIdentifier() {
128         return identifier;
129     }
130
131     @Override
132     public DataObjectModification.ModificationType getModificationType() {
133         switch(domData.getModificationType()) {
134             case WRITE:
135                 return DataObjectModification.ModificationType.WRITE;
136             case SUBTREE_MODIFIED:
137                 return DataObjectModification.ModificationType.SUBTREE_MODIFIED;
138             case DELETE:
139                 return DataObjectModification.ModificationType.DELETE;
140
141             default:
142                 // TODO: Should we lie about modification type instead of exception?
143                 throw new IllegalStateException("Unsupported DOM Modification type " + domData.getModificationType());
144         }
145     }
146
147     @Override
148     public Collection<DataObjectModification<? extends DataObject>> getModifiedChildren() {
149         if(childNodesCache == null) {
150             childNodesCache = from(codec,domData.getChildNodes());
151         }
152         return childNodesCache;
153     }
154
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());
162         }
163         if (current != null) {
164             return create(childCodec, current);
165         }
166         return null;
167     }
168
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));
172     }
173
174     private T deserialize(final Optional<NormalizedNode<?, ?>> dataAfter) {
175         if(dataAfter.isPresent()) {
176             return codec.deserialize(dataAfter.get());
177         }
178         return null;
179     }
180 }