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