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.InstanceIdentifier;
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 Set<DataSchemaNode> dataSchemaNode) {
40 if (dataSchemaNode != null && !dataSchemaNode.isEmpty() && 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 Set<DataSchemaNode> childNodes = schema.getChildNodes();
59 return findSchemaForChild(schema, qname, childNodes);
62 public static DataSchemaNode findSchemaForChild(final DataNodeContainer schema, final QName qname, final Set<DataSchemaNode> childNodes) {
63 Optional<DataSchemaNode> childSchema = findFirstSchema(qname, childNodes);
64 Preconditions.checkState(childSchema.isPresent(),
65 "Unknown child(ren) node(s) detected, identified by: %s, in: %s", qname, schema);
66 return childSchema.get();
69 public static AugmentationSchema findSchemaForAugment(final AugmentationTarget schema, final Set<QName> qNames) {
70 Optional<AugmentationSchema> schemaForAugment = findAugment(schema, qNames);
71 Preconditions.checkState(schemaForAugment.isPresent(), "Unknown augmentation node detected, identified by: %s, in: %s",
73 return schemaForAugment.get();
76 public static AugmentationSchema findSchemaForAugment(final ChoiceNode schema, final Set<QName> qNames) {
77 Optional<AugmentationSchema> schemaForAugment = Optional.absent();
79 for (ChoiceCaseNode choiceCaseNode : schema.getCases()) {
80 schemaForAugment = findAugment(choiceCaseNode, qNames);
81 if(schemaForAugment.isPresent()) {
86 Preconditions.checkState(schemaForAugment.isPresent(), "Unknown augmentation node detected, identified by: %s, in: %s",
88 return schemaForAugment.get();
91 private static Optional<AugmentationSchema> findAugment(final AugmentationTarget schema, final Set<QName> qNames) {
92 for (AugmentationSchema augment : schema.getAvailableAugmentations()) {
94 HashSet<QName> qNamesFromAugment = Sets.newHashSet(Collections2.transform(augment.getChildNodes(), new Function<DataSchemaNode, QName>() {
96 public QName apply(final DataSchemaNode input) {
97 return input.getQName();
101 if(qNamesFromAugment.equals(qNames)) {
102 return Optional.of(augment);
106 return Optional.absent();
109 public static DataSchemaNode findSchemaForChild(final ChoiceNode schema, final QName childPartialQName) {
110 for (ChoiceCaseNode choiceCaseNode : schema.getCases()) {
111 Optional<DataSchemaNode> childSchema = findFirstSchema(childPartialQName, choiceCaseNode.getChildNodes());
112 if (childSchema.isPresent()) {
113 return childSchema.get();
118 throw new IllegalStateException(String.format("Unknown child(ren) node(s) detected, identified by: %s, in: %s",
119 childPartialQName, schema));
123 * Recursively find all child nodes that come from choices.
125 * @return Map with all child nodes, to their most top augmentation
127 public static Map<QName, ChoiceNode> mapChildElementsFromChoices(final DataNodeContainer schema) {
128 Set<DataSchemaNode> childNodes = schema.getChildNodes();
130 return mapChildElementsFromChoices(schema, childNodes);
133 private static Map<QName, ChoiceNode> mapChildElementsFromChoices(final DataNodeContainer schema, final Set<DataSchemaNode> childNodes) {
134 Map<QName, ChoiceNode> mappedChoices = Maps.newLinkedHashMap();
136 for (final DataSchemaNode childSchema : childNodes) {
137 if(childSchema instanceof ChoiceNode) {
139 if(isFromAugment(schema, childSchema)) {
143 for (ChoiceCaseNode choiceCaseNode : ((ChoiceNode) childSchema).getCases()) {
145 for (QName qName : getChildNodesRecursive(choiceCaseNode)) {
146 mappedChoices.put(qName, (ChoiceNode) childSchema);
152 return mappedChoices;
155 private static boolean isFromAugment(final DataNodeContainer schema, final DataSchemaNode childSchema) {
156 if(schema instanceof AugmentationTarget == false) {
160 for (AugmentationSchema augmentationSchema : ((AugmentationTarget) schema).getAvailableAugmentations()) {
161 if(augmentationSchema.getDataChildByName(childSchema.getQName()) != null) {
170 * Recursively find all child nodes that come from augmentations.
172 * @return Map with all child nodes, to their most top augmentation
174 public static Map<QName, AugmentationSchema> mapChildElementsFromAugments(final AugmentationTarget schema) {
176 Map<QName, AugmentationSchema> childNodesToAugmentation = Maps.newLinkedHashMap();
178 // Find QNames of augmented child nodes
179 Map<QName, AugmentationSchema> augments = Maps.newHashMap();
180 for (final AugmentationSchema augmentationSchema : schema.getAvailableAugmentations()) {
181 for (DataSchemaNode dataSchemaNode : augmentationSchema.getChildNodes()) {
182 augments.put(dataSchemaNode.getQName(), augmentationSchema);
186 // Augmented nodes have to be looked up directly in augmentationTarget
187 // because nodes from augment do not contain nodes from other augmentations
188 if (schema instanceof DataNodeContainer) {
190 for (DataSchemaNode child : ((DataNodeContainer) schema).getChildNodes()) {
191 // If is not augmented child, continue
192 if (augments.containsKey(child.getQName()) == false) {
196 AugmentationSchema mostTopAugmentation = augments.get(child.getQName());
198 // recursively add all child nodes in case of augment, case and choice
199 if (child instanceof AugmentationSchema || child instanceof ChoiceCaseNode) {
200 for (QName qName : getChildNodesRecursive((DataNodeContainer) child)) {
201 childNodesToAugmentation.put(qName, mostTopAugmentation);
203 } else if (child instanceof ChoiceNode) {
204 for (ChoiceCaseNode choiceCaseNode : ((ChoiceNode) child).getCases()) {
205 for (QName qName : getChildNodesRecursive(choiceCaseNode)) {
206 childNodesToAugmentation.put(qName, mostTopAugmentation);
210 childNodesToAugmentation.put(child.getQName(), mostTopAugmentation);
215 // Choice Node has to map child nodes from all its cases
216 if (schema instanceof ChoiceNode) {
217 for (ChoiceCaseNode choiceCaseNode : ((ChoiceNode) schema).getCases()) {
218 if (augments.containsKey(choiceCaseNode.getQName()) == false) {
222 for (QName qName : getChildNodesRecursive(choiceCaseNode)) {
223 childNodesToAugmentation.put(qName, augments.get(choiceCaseNode.getQName()));
228 return childNodesToAugmentation;
232 * Recursively list all child nodes.
234 * In case of choice, augment and cases, step in.
236 public static Set<QName> getChildNodesRecursive(final DataNodeContainer nodeContainer) {
237 Set<QName> allChildNodes = Sets.newHashSet();
239 for (DataSchemaNode childSchema : nodeContainer.getChildNodes()) {
240 if(childSchema instanceof ChoiceNode) {
241 for (ChoiceCaseNode choiceCaseNode : ((ChoiceNode) childSchema).getCases()) {
242 allChildNodes.addAll(getChildNodesRecursive(choiceCaseNode));
244 } else if(childSchema instanceof AugmentationSchema || childSchema instanceof ChoiceCaseNode) {
245 allChildNodes.addAll(getChildNodesRecursive((DataNodeContainer) childSchema));
248 allChildNodes.add(childSchema.getQName());
252 return allChildNodes;
256 * Retrieves real schemas for augmented child node.
258 * Schema of the same child node from augment, and directly from target is not the same.
259 * Schema of child node from augment is incomplete, therefore its useless for xml <-> normalizedNode translation.
262 public static Set<DataSchemaNode> getRealSchemasForAugment(final AugmentationTarget targetSchema, final AugmentationSchema augmentSchema) {
263 if(targetSchema.getAvailableAugmentations().contains(augmentSchema) == false) {
264 return Collections.emptySet();
267 Set<DataSchemaNode> realChildNodes = Sets.newHashSet();
269 if(targetSchema instanceof DataNodeContainer) {
270 realChildNodes = getRealSchemasForAugment((DataNodeContainer)targetSchema, augmentSchema);
271 } else if(targetSchema instanceof ChoiceNode) {
272 for (DataSchemaNode dataSchemaNode : augmentSchema.getChildNodes()) {
273 for (ChoiceCaseNode choiceCaseNode : ((ChoiceNode) targetSchema).getCases()) {
274 if(getChildNodesRecursive(choiceCaseNode).contains(dataSchemaNode.getQName())) {
275 realChildNodes.add(choiceCaseNode.getDataChildByName(dataSchemaNode.getQName()));
281 return realChildNodes;
284 public static Set<DataSchemaNode> getRealSchemasForAugment(final DataNodeContainer targetSchema,
285 final AugmentationSchema augmentSchema) {
286 Set<DataSchemaNode> realChildNodes = Sets.newHashSet();
287 for (DataSchemaNode dataSchemaNode : augmentSchema.getChildNodes()) {
288 DataSchemaNode realChild = targetSchema.getDataChildByName(dataSchemaNode.getQName());
289 realChildNodes.add(realChild);
291 return realChildNodes;
294 public static Optional<ChoiceCaseNode> detectCase(final ChoiceNode schema, final DataContainerChild<?, ?> child) {
295 for (ChoiceCaseNode choiceCaseNode : schema.getCases()) {
296 if (child instanceof AugmentationNode
297 && belongsToCaseAugment(choiceCaseNode,
298 (InstanceIdentifier.AugmentationIdentifier) child.getIdentifier())) {
299 return Optional.of(choiceCaseNode);
300 } else if (choiceCaseNode.getDataChildByName(child.getNodeType()) != null) {
301 return Optional.of(choiceCaseNode);
305 return Optional.absent();
308 public static boolean belongsToCaseAugment(final ChoiceCaseNode caseNode, final InstanceIdentifier.AugmentationIdentifier childToProcess) {
309 for (AugmentationSchema augmentationSchema : caseNode.getAvailableAugmentations()) {
311 Set<QName> currentAugmentChildNodes = Sets.newHashSet();
312 for (DataSchemaNode dataSchemaNode : augmentationSchema.getChildNodes()) {
313 currentAugmentChildNodes.add(dataSchemaNode.getQName());
316 if(childToProcess.getPossibleChildNames().equals(currentAugmentChildNodes)){
324 public static InstanceIdentifier.AugmentationIdentifier getNodeIdentifierForAugmentation(final AugmentationSchema schema) {
325 return new InstanceIdentifier.AugmentationIdentifier(getChildQNames(schema));
328 public static Set<QName> getChildQNames(final AugmentationSchema schema) {
329 Set<QName> qnames = Sets.newHashSet();
331 for (DataSchemaNode dataSchemaNode : schema.getChildNodes()) {
332 qnames.add(dataSchemaNode.getQName());