Bug 576 Fixes of critical issues for YT components
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / SchemaUtils.java
1 /*
2  * Copyright (c) 2013 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.yangtools.yang.data.impl.schema;
9
10
11 import com.google.common.base.Function;
12 import com.google.common.base.Optional;
13 import com.google.common.base.Preconditions;
14 import com.google.common.collect.Collections2;
15 import com.google.common.collect.Maps;
16 import com.google.common.collect.Sets;
17 import java.util.Collections;
18 import java.util.HashSet;
19 import java.util.Map;
20 import java.util.Set;
21 import javax.annotation.Nonnull;
22 import org.opendaylight.yangtools.yang.common.QName;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
24 import org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode;
25 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
26 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
27 import org.opendaylight.yangtools.yang.model.api.AugmentationTarget;
28 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
29 import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
30 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
31 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
32
33 public final class SchemaUtils {
34
35     private SchemaUtils() {
36     }
37
38     public static final Optional<DataSchemaNode> findFirstSchema(final QName qname, final Iterable<DataSchemaNode> dataSchemaNode) {
39         if (dataSchemaNode != null && qname != null) {
40             for (DataSchemaNode dsn : dataSchemaNode) {
41                 if (qname.isEqualWithoutRevision(dsn.getQName())) {
42                     return Optional.<DataSchemaNode> of(dsn);
43                 } else if (dsn instanceof ChoiceNode) {
44                     for (ChoiceCaseNode choiceCase : ((ChoiceNode) dsn).getCases()) {
45                         Optional<DataSchemaNode> foundDsn = findFirstSchema(qname, choiceCase.getChildNodes());
46                         if (foundDsn != null && foundDsn.isPresent()) {
47                             return foundDsn;
48                         }
49                     }
50                 }
51             }
52         }
53         return Optional.absent();
54     }
55
56     public static DataSchemaNode findSchemaForChild(final DataNodeContainer schema, final QName qname) {
57         return findSchemaForChild(schema, qname, schema.getChildNodes());
58     }
59
60     public static DataSchemaNode findSchemaForChild(final DataNodeContainer schema, final QName qname, final Iterable<DataSchemaNode> childNodes) {
61         Optional<DataSchemaNode> childSchema = findFirstSchema(qname, childNodes);
62         Preconditions.checkState(childSchema.isPresent(),
63                 "Unknown child(ren) node(s) detected, identified by: %s, in: %s", qname, schema);
64         return childSchema.get();
65     }
66
67     public static AugmentationSchema findSchemaForAugment(final AugmentationTarget schema, final Set<QName> qNames) {
68         Optional<AugmentationSchema> schemaForAugment = findAugment(schema, qNames);
69         Preconditions.checkState(schemaForAugment.isPresent(), "Unknown augmentation node detected, identified by: %s, in: %s",
70                 qNames, schema);
71         return schemaForAugment.get();
72     }
73
74     public static AugmentationSchema findSchemaForAugment(final ChoiceNode schema, final Set<QName> qNames) {
75         Optional<AugmentationSchema> schemaForAugment = Optional.absent();
76
77         for (ChoiceCaseNode choiceCaseNode : schema.getCases()) {
78             schemaForAugment = findAugment(choiceCaseNode, qNames);
79             if(schemaForAugment.isPresent()) {
80                 break;
81             }
82         }
83
84         Preconditions.checkState(schemaForAugment.isPresent(), "Unknown augmentation node detected, identified by: %s, in: %s",
85                 qNames, schema);
86         return schemaForAugment.get();
87     }
88
89     private static Optional<AugmentationSchema> findAugment(final AugmentationTarget schema, final Set<QName> qNames) {
90         for (AugmentationSchema augment : schema.getAvailableAugmentations()) {
91
92             HashSet<QName> qNamesFromAugment = Sets.newHashSet(Collections2.transform(augment.getChildNodes(), new Function<DataSchemaNode, QName>() {
93                 @Override
94                 public QName apply(final @Nonnull DataSchemaNode input) {
95                     Preconditions.checkNotNull(input);
96                     return input.getQName();
97                 }
98             }));
99
100             if(qNamesFromAugment.equals(qNames)) {
101                 return Optional.of(augment);
102             }
103         }
104
105         return Optional.absent();
106     }
107
108     public static DataSchemaNode findSchemaForChild(final ChoiceNode schema, final QName childPartialQName) {
109         for (ChoiceCaseNode choiceCaseNode : schema.getCases()) {
110             Optional<DataSchemaNode> childSchema = findFirstSchema(childPartialQName, choiceCaseNode.getChildNodes());
111             if (childSchema.isPresent()) {
112                 return childSchema.get();
113             }
114         }
115
116
117         throw new IllegalStateException(String.format("Unknown child(ren) node(s) detected, identified by: %s, in: %s",
118                 childPartialQName, schema));
119     }
120
121     /**
122      * Recursively find all child nodes that come from choices.
123      *
124      * @return Map with all child nodes, to their most top augmentation
125      */
126     public static Map<QName, ChoiceNode> mapChildElementsFromChoices(final DataNodeContainer schema) {
127         return mapChildElementsFromChoices(schema, schema.getChildNodes());
128     }
129
130     private static Map<QName, ChoiceNode> mapChildElementsFromChoices(final DataNodeContainer schema, final Iterable<DataSchemaNode> childNodes) {
131         Map<QName, ChoiceNode> mappedChoices = Maps.newLinkedHashMap();
132
133         for (final DataSchemaNode childSchema : childNodes) {
134             if(childSchema instanceof ChoiceNode) {
135
136                 if(isFromAugment(schema, childSchema)) {
137                     continue;
138                 }
139
140                 for (ChoiceCaseNode choiceCaseNode : ((ChoiceNode) childSchema).getCases()) {
141
142                     for (QName qName : getChildNodesRecursive(choiceCaseNode)) {
143                         mappedChoices.put(qName, (ChoiceNode) childSchema);
144                     }
145                 }
146             }
147         }
148
149         return mappedChoices;
150     }
151
152     private static boolean isFromAugment(final DataNodeContainer schema, final DataSchemaNode childSchema) {
153         if (!(schema instanceof AugmentationTarget)) {
154             return false;
155         }
156
157         for (AugmentationSchema augmentationSchema : ((AugmentationTarget) schema).getAvailableAugmentations()) {
158             if(augmentationSchema.getDataChildByName(childSchema.getQName()) != null) {
159                 return true;
160             }
161         }
162
163         return false;
164     }
165
166     /**
167      * Recursively find all child nodes that come from augmentations.
168      *
169      * @return Map with all child nodes, to their most top augmentation
170      */
171     public static Map<QName, AugmentationSchema> mapChildElementsFromAugments(final AugmentationTarget schema) {
172
173         Map<QName, AugmentationSchema> childNodesToAugmentation = Maps.newLinkedHashMap();
174
175         // Find QNames of augmented child nodes
176         Map<QName, AugmentationSchema> augments = Maps.newHashMap();
177         for (final AugmentationSchema augmentationSchema : schema.getAvailableAugmentations()) {
178             for (DataSchemaNode dataSchemaNode : augmentationSchema.getChildNodes()) {
179                 augments.put(dataSchemaNode.getQName(), augmentationSchema);
180             }
181         }
182
183         // Augmented nodes have to be looked up directly in augmentationTarget
184         // because nodes from augment do not contain nodes from other augmentations
185         if (schema instanceof DataNodeContainer) {
186
187             for (DataSchemaNode child : ((DataNodeContainer) schema).getChildNodes()) {
188                 // If is not augmented child, continue
189                 if (!(augments.containsKey(child.getQName()))) {
190                     continue;
191                 }
192
193                 AugmentationSchema mostTopAugmentation = augments.get(child.getQName());
194
195                 // recursively add all child nodes in case of augment, case and choice
196                 if (child instanceof AugmentationSchema || child instanceof ChoiceCaseNode) {
197                     for (QName qName : getChildNodesRecursive((DataNodeContainer) child)) {
198                         childNodesToAugmentation.put(qName, mostTopAugmentation);
199                     }
200                 } else if (child instanceof ChoiceNode) {
201                     for (ChoiceCaseNode choiceCaseNode : ((ChoiceNode) child).getCases()) {
202                         for (QName qName : getChildNodesRecursive(choiceCaseNode)) {
203                             childNodesToAugmentation.put(qName, mostTopAugmentation);
204                         }
205                     }
206                 } else {
207                     childNodesToAugmentation.put(child.getQName(), mostTopAugmentation);
208                 }
209             }
210         }
211
212         // Choice Node has to map child nodes from all its cases
213         if (schema instanceof ChoiceNode) {
214             for (ChoiceCaseNode choiceCaseNode : ((ChoiceNode) schema).getCases()) {
215                 if (!(augments.containsKey(choiceCaseNode.getQName()))) {
216                     continue;
217                 }
218
219                 for (QName qName : getChildNodesRecursive(choiceCaseNode)) {
220                     childNodesToAugmentation.put(qName, augments.get(choiceCaseNode.getQName()));
221                 }
222             }
223         }
224
225         return childNodesToAugmentation;
226     }
227
228     /**
229      * Recursively list all child nodes.
230      *
231      * In case of choice, augment and cases, step in.
232      */
233     public static Set<QName> getChildNodesRecursive(final DataNodeContainer nodeContainer) {
234         Set<QName> allChildNodes = Sets.newHashSet();
235
236         for (DataSchemaNode childSchema : nodeContainer.getChildNodes()) {
237             if(childSchema instanceof ChoiceNode) {
238                 for (ChoiceCaseNode choiceCaseNode : ((ChoiceNode) childSchema).getCases()) {
239                     allChildNodes.addAll(getChildNodesRecursive(choiceCaseNode));
240                 }
241             } else if(childSchema instanceof AugmentationSchema || childSchema instanceof ChoiceCaseNode) {
242                 allChildNodes.addAll(getChildNodesRecursive((DataNodeContainer) childSchema));
243             }
244             else {
245                 allChildNodes.add(childSchema.getQName());
246             }
247         }
248
249         return allChildNodes;
250     }
251
252     /**
253      * Retrieves real schemas for augmented child node.
254      *
255      * Schema of the same child node from augment, and directly from target is not the same.
256      * Schema of child node from augment is incomplete, therefore its useless for xml <-> normalizedNode translation.
257      *
258      */
259     public static Set<DataSchemaNode> getRealSchemasForAugment(final AugmentationTarget targetSchema, final AugmentationSchema augmentSchema) {
260         if (!(targetSchema.getAvailableAugmentations().contains(augmentSchema))) {
261             return Collections.emptySet();
262         }
263
264         Set<DataSchemaNode> realChildNodes = Sets.newHashSet();
265
266         if(targetSchema instanceof DataNodeContainer) {
267             realChildNodes = getRealSchemasForAugment((DataNodeContainer)targetSchema, augmentSchema);
268         } else if(targetSchema instanceof ChoiceNode) {
269             for (DataSchemaNode dataSchemaNode : augmentSchema.getChildNodes()) {
270                 for (ChoiceCaseNode choiceCaseNode : ((ChoiceNode) targetSchema).getCases()) {
271                     if(getChildNodesRecursive(choiceCaseNode).contains(dataSchemaNode.getQName())) {
272                         realChildNodes.add(choiceCaseNode.getDataChildByName(dataSchemaNode.getQName()));
273                     }
274                 }
275             }
276         }
277
278         return realChildNodes;
279     }
280
281     public static Set<DataSchemaNode> getRealSchemasForAugment(final DataNodeContainer targetSchema,
282             final AugmentationSchema augmentSchema) {
283         Set<DataSchemaNode> realChildNodes = Sets.newHashSet();
284         for (DataSchemaNode dataSchemaNode : augmentSchema.getChildNodes()) {
285             DataSchemaNode realChild = targetSchema.getDataChildByName(dataSchemaNode.getQName());
286             realChildNodes.add(realChild);
287         }
288         return realChildNodes;
289     }
290
291     public static Optional<ChoiceCaseNode> detectCase(final ChoiceNode schema, final DataContainerChild<?, ?> child) {
292         for (ChoiceCaseNode choiceCaseNode : schema.getCases()) {
293             if (child instanceof AugmentationNode
294                     && belongsToCaseAugment(choiceCaseNode,
295                             (YangInstanceIdentifier.AugmentationIdentifier) child.getIdentifier())) {
296                 return Optional.of(choiceCaseNode);
297             } else if (choiceCaseNode.getDataChildByName(child.getNodeType()) != null) {
298                 return Optional.of(choiceCaseNode);
299             }
300         }
301
302         return Optional.absent();
303     }
304
305     public static boolean belongsToCaseAugment(final ChoiceCaseNode caseNode, final YangInstanceIdentifier.AugmentationIdentifier childToProcess) {
306         for (AugmentationSchema augmentationSchema : caseNode.getAvailableAugmentations()) {
307
308             Set<QName> currentAugmentChildNodes = Sets.newHashSet();
309             for (DataSchemaNode dataSchemaNode : augmentationSchema.getChildNodes()) {
310                 currentAugmentChildNodes.add(dataSchemaNode.getQName());
311             }
312
313             if(childToProcess.getPossibleChildNames().equals(currentAugmentChildNodes)){
314                 return true;
315             }
316         }
317
318         return false;
319     }
320
321     public static YangInstanceIdentifier.AugmentationIdentifier getNodeIdentifierForAugmentation(final AugmentationSchema schema) {
322         return new YangInstanceIdentifier.AugmentationIdentifier(getChildQNames(schema));
323     }
324
325     public static Set<QName> getChildQNames(final AugmentationSchema schema) {
326         Set<QName> qnames = Sets.newHashSet();
327
328         for (DataSchemaNode dataSchemaNode : schema.getChildNodes()) {
329             qnames.add(dataSchemaNode.getQName());
330         }
331
332         return qnames;
333     }
334 }