2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.data.impl.schema;
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;
18 import java.util.Collections;
19 import java.util.HashSet;
23 import org.opendaylight.yangtools.yang.common.QName;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
25 import org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode;
26 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
27 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
28 import org.opendaylight.yangtools.yang.model.api.AugmentationTarget;
29 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
30 import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
31 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
32 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
34 public final class SchemaUtils {
36 private SchemaUtils() {
39 public static final Optional<DataSchemaNode> findFirstSchema(final QName qname, final Iterable<DataSchemaNode> dataSchemaNode) {
40 if (dataSchemaNode != null && qname != null) {
41 for (DataSchemaNode dsn : dataSchemaNode) {
42 if (qname.isEqualWithoutRevision(dsn.getQName())) {
43 return Optional.<DataSchemaNode> of(dsn);
44 } else if (dsn instanceof ChoiceNode) {
45 for (ChoiceCaseNode choiceCase : ((ChoiceNode) dsn).getCases()) {
46 Optional<DataSchemaNode> foundDsn = findFirstSchema(qname, choiceCase.getChildNodes());
47 if (foundDsn != null && foundDsn.isPresent()) {
54 return Optional.absent();
57 public static DataSchemaNode findSchemaForChild(final DataNodeContainer schema, final QName qname) {
58 return findSchemaForChild(schema, qname, schema.getChildNodes());
61 public static DataSchemaNode findSchemaForChild(final DataNodeContainer schema, final QName qname, final Iterable<DataSchemaNode> childNodes) {
62 Optional<DataSchemaNode> childSchema = findFirstSchema(qname, childNodes);
63 Preconditions.checkState(childSchema.isPresent(),
64 "Unknown child(ren) node(s) detected, identified by: %s, in: %s", qname, schema);
65 return childSchema.get();
68 public static AugmentationSchema findSchemaForAugment(final AugmentationTarget schema, final Set<QName> qNames) {
69 Optional<AugmentationSchema> schemaForAugment = findAugment(schema, qNames);
70 Preconditions.checkState(schemaForAugment.isPresent(), "Unknown augmentation node detected, identified by: %s, in: %s",
72 return schemaForAugment.get();
75 public static AugmentationSchema findSchemaForAugment(final ChoiceNode schema, final Set<QName> qNames) {
76 Optional<AugmentationSchema> schemaForAugment = Optional.absent();
78 for (ChoiceCaseNode choiceCaseNode : schema.getCases()) {
79 schemaForAugment = findAugment(choiceCaseNode, qNames);
80 if(schemaForAugment.isPresent()) {
85 Preconditions.checkState(schemaForAugment.isPresent(), "Unknown augmentation node detected, identified by: %s, in: %s",
87 return schemaForAugment.get();
90 private static Optional<AugmentationSchema> findAugment(final AugmentationTarget schema, final Set<QName> qNames) {
91 for (AugmentationSchema augment : schema.getAvailableAugmentations()) {
93 HashSet<QName> qNamesFromAugment = Sets.newHashSet(Collections2.transform(augment.getChildNodes(), new Function<DataSchemaNode, QName>() {
95 public QName apply(final DataSchemaNode input) {
96 return input.getQName();
100 if(qNamesFromAugment.equals(qNames)) {
101 return Optional.of(augment);
105 return Optional.absent();
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();
117 throw new IllegalStateException(String.format("Unknown child(ren) node(s) detected, identified by: %s, in: %s",
118 childPartialQName, schema));
122 * Recursively find all child nodes that come from choices.
124 * @return Map with all child nodes, to their most top augmentation
126 public static Map<QName, ChoiceNode> mapChildElementsFromChoices(final DataNodeContainer schema) {
127 return mapChildElementsFromChoices(schema, schema.getChildNodes());
130 private static Map<QName, ChoiceNode> mapChildElementsFromChoices(final DataNodeContainer schema, final Iterable<DataSchemaNode> childNodes) {
131 Map<QName, ChoiceNode> mappedChoices = Maps.newLinkedHashMap();
133 for (final DataSchemaNode childSchema : childNodes) {
134 if(childSchema instanceof ChoiceNode) {
136 if(isFromAugment(schema, childSchema)) {
140 for (ChoiceCaseNode choiceCaseNode : ((ChoiceNode) childSchema).getCases()) {
142 for (QName qName : getChildNodesRecursive(choiceCaseNode)) {
143 mappedChoices.put(qName, (ChoiceNode) childSchema);
149 return mappedChoices;
152 private static boolean isFromAugment(final DataNodeContainer schema, final DataSchemaNode childSchema) {
153 if(schema instanceof AugmentationTarget == false) {
157 for (AugmentationSchema augmentationSchema : ((AugmentationTarget) schema).getAvailableAugmentations()) {
158 if(augmentationSchema.getDataChildByName(childSchema.getQName()) != null) {
167 * Recursively find all child nodes that come from augmentations.
169 * @return Map with all child nodes, to their most top augmentation
171 public static Map<QName, AugmentationSchema> mapChildElementsFromAugments(final AugmentationTarget schema) {
173 Map<QName, AugmentationSchema> childNodesToAugmentation = Maps.newLinkedHashMap();
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);
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) {
187 for (DataSchemaNode child : ((DataNodeContainer) schema).getChildNodes()) {
188 // If is not augmented child, continue
189 if (augments.containsKey(child.getQName()) == false) {
193 AugmentationSchema mostTopAugmentation = augments.get(child.getQName());
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);
200 } else if (child instanceof ChoiceNode) {
201 for (ChoiceCaseNode choiceCaseNode : ((ChoiceNode) child).getCases()) {
202 for (QName qName : getChildNodesRecursive(choiceCaseNode)) {
203 childNodesToAugmentation.put(qName, mostTopAugmentation);
207 childNodesToAugmentation.put(child.getQName(), mostTopAugmentation);
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()) == false) {
219 for (QName qName : getChildNodesRecursive(choiceCaseNode)) {
220 childNodesToAugmentation.put(qName, augments.get(choiceCaseNode.getQName()));
225 return childNodesToAugmentation;
229 * Recursively list all child nodes.
231 * In case of choice, augment and cases, step in.
233 public static Set<QName> getChildNodesRecursive(final DataNodeContainer nodeContainer) {
234 Set<QName> allChildNodes = Sets.newHashSet();
236 for (DataSchemaNode childSchema : nodeContainer.getChildNodes()) {
237 if(childSchema instanceof ChoiceNode) {
238 for (ChoiceCaseNode choiceCaseNode : ((ChoiceNode) childSchema).getCases()) {
239 allChildNodes.addAll(getChildNodesRecursive(choiceCaseNode));
241 } else if(childSchema instanceof AugmentationSchema || childSchema instanceof ChoiceCaseNode) {
242 allChildNodes.addAll(getChildNodesRecursive((DataNodeContainer) childSchema));
245 allChildNodes.add(childSchema.getQName());
249 return allChildNodes;
253 * Retrieves real schemas for augmented child node.
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.
259 public static Set<DataSchemaNode> getRealSchemasForAugment(final AugmentationTarget targetSchema, final AugmentationSchema augmentSchema) {
260 if(targetSchema.getAvailableAugmentations().contains(augmentSchema) == false) {
261 return Collections.emptySet();
264 Set<DataSchemaNode> realChildNodes = Sets.newHashSet();
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()));
278 return realChildNodes;
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);
288 return realChildNodes;
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);
302 return Optional.absent();
305 public static boolean belongsToCaseAugment(final ChoiceCaseNode caseNode, final YangInstanceIdentifier.AugmentationIdentifier childToProcess) {
306 for (AugmentationSchema augmentationSchema : caseNode.getAvailableAugmentations()) {
308 Set<QName> currentAugmentChildNodes = Sets.newHashSet();
309 for (DataSchemaNode dataSchemaNode : augmentationSchema.getChildNodes()) {
310 currentAugmentChildNodes.add(dataSchemaNode.getQName());
313 if(childToProcess.getPossibleChildNames().equals(currentAugmentChildNodes)){
321 public static YangInstanceIdentifier.AugmentationIdentifier getNodeIdentifierForAugmentation(final AugmentationSchema schema) {
322 return new YangInstanceIdentifier.AugmentationIdentifier(getChildQNames(schema));
325 public static Set<QName> getChildQNames(final AugmentationSchema schema) {
326 Set<QName> qnames = Sets.newHashSet();
328 for (DataSchemaNode dataSchemaNode : schema.getChildNodes()) {
329 qnames.add(dataSchemaNode.getQName());