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;
17 import java.util.Collections;
18 import java.util.HashSet;
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;
32 public final class SchemaUtils {
34 private SchemaUtils() {
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()) {
52 return Optional.absent();
55 public static DataSchemaNode findSchemaForChild(final DataNodeContainer schema, final QName qname) {
56 return findSchemaForChild(schema, qname, schema.getChildNodes());
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();
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",
70 return schemaForAugment.get();
73 public static AugmentationSchema findSchemaForAugment(final ChoiceNode schema, final Set<QName> qNames) {
74 Optional<AugmentationSchema> schemaForAugment = Optional.absent();
76 for (ChoiceCaseNode choiceCaseNode : schema.getCases()) {
77 schemaForAugment = findAugment(choiceCaseNode, qNames);
78 if(schemaForAugment.isPresent()) {
83 Preconditions.checkState(schemaForAugment.isPresent(), "Unknown augmentation node detected, identified by: %s, in: %s",
85 return schemaForAugment.get();
88 private static Optional<AugmentationSchema> findAugment(final AugmentationTarget schema, final Set<QName> qNames) {
89 for (AugmentationSchema augment : schema.getAvailableAugmentations()) {
91 HashSet<QName> qNamesFromAugment = Sets.newHashSet(Collections2.transform(augment.getChildNodes(), new Function<DataSchemaNode, QName>() {
93 public QName apply(final DataSchemaNode input) {
94 return input.getQName();
98 if(qNamesFromAugment.equals(qNames)) {
99 return Optional.of(augment);
103 return Optional.absent();
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();
115 throw new IllegalStateException(String.format("Unknown child(ren) node(s) detected, identified by: %s, in: %s",
116 childPartialQName, schema));
120 * Recursively find all child nodes that come from choices.
122 * @return Map with all child nodes, to their most top augmentation
124 public static Map<QName, ChoiceNode> mapChildElementsFromChoices(final DataNodeContainer schema) {
125 return mapChildElementsFromChoices(schema, schema.getChildNodes());
128 private static Map<QName, ChoiceNode> mapChildElementsFromChoices(final DataNodeContainer schema, final Iterable<DataSchemaNode> childNodes) {
129 Map<QName, ChoiceNode> mappedChoices = Maps.newLinkedHashMap();
131 for (final DataSchemaNode childSchema : childNodes) {
132 if(childSchema instanceof ChoiceNode) {
134 if(isFromAugment(schema, childSchema)) {
138 for (ChoiceCaseNode choiceCaseNode : ((ChoiceNode) childSchema).getCases()) {
140 for (QName qName : getChildNodesRecursive(choiceCaseNode)) {
141 mappedChoices.put(qName, (ChoiceNode) childSchema);
147 return mappedChoices;
150 private static boolean isFromAugment(final DataNodeContainer schema, final DataSchemaNode childSchema) {
151 if (!(schema instanceof AugmentationTarget)) {
155 for (AugmentationSchema augmentationSchema : ((AugmentationTarget) schema).getAvailableAugmentations()) {
156 if(augmentationSchema.getDataChildByName(childSchema.getQName()) != null) {
165 * Recursively find all child nodes that come from augmentations.
167 * @return Map with all child nodes, to their most top augmentation
169 public static Map<QName, AugmentationSchema> mapChildElementsFromAugments(final AugmentationTarget schema) {
171 Map<QName, AugmentationSchema> childNodesToAugmentation = Maps.newLinkedHashMap();
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);
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) {
185 for (DataSchemaNode child : ((DataNodeContainer) schema).getChildNodes()) {
186 // If is not augmented child, continue
187 if (!(augments.containsKey(child.getQName()))) {
191 AugmentationSchema mostTopAugmentation = augments.get(child.getQName());
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);
198 } else if (child instanceof ChoiceNode) {
199 for (ChoiceCaseNode choiceCaseNode : ((ChoiceNode) child).getCases()) {
200 for (QName qName : getChildNodesRecursive(choiceCaseNode)) {
201 childNodesToAugmentation.put(qName, mostTopAugmentation);
205 childNodesToAugmentation.put(child.getQName(), mostTopAugmentation);
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()))) {
217 for (QName qName : getChildNodesRecursive(choiceCaseNode)) {
218 childNodesToAugmentation.put(qName, augments.get(choiceCaseNode.getQName()));
223 return childNodesToAugmentation;
227 * Recursively list all child nodes.
229 * In case of choice, augment and cases, step in.
231 public static Set<QName> getChildNodesRecursive(final DataNodeContainer nodeContainer) {
232 Set<QName> allChildNodes = Sets.newHashSet();
234 for (DataSchemaNode childSchema : nodeContainer.getChildNodes()) {
235 if(childSchema instanceof ChoiceNode) {
236 for (ChoiceCaseNode choiceCaseNode : ((ChoiceNode) childSchema).getCases()) {
237 allChildNodes.addAll(getChildNodesRecursive(choiceCaseNode));
239 } else if(childSchema instanceof AugmentationSchema || childSchema instanceof ChoiceCaseNode) {
240 allChildNodes.addAll(getChildNodesRecursive((DataNodeContainer) childSchema));
243 allChildNodes.add(childSchema.getQName());
247 return allChildNodes;
251 * Retrieves real schemas for augmented child node.
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.
257 public static Set<DataSchemaNode> getRealSchemasForAugment(final AugmentationTarget targetSchema, final AugmentationSchema augmentSchema) {
258 if (!(targetSchema.getAvailableAugmentations().contains(augmentSchema))) {
259 return Collections.emptySet();
262 Set<DataSchemaNode> realChildNodes = Sets.newHashSet();
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()));
276 return realChildNodes;
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);
286 return realChildNodes;
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);
300 return Optional.absent();
303 public static boolean belongsToCaseAugment(final ChoiceCaseNode caseNode, final YangInstanceIdentifier.AugmentationIdentifier childToProcess) {
304 for (AugmentationSchema augmentationSchema : caseNode.getAvailableAugmentations()) {
306 Set<QName> currentAugmentChildNodes = Sets.newHashSet();
307 for (DataSchemaNode dataSchemaNode : augmentationSchema.getChildNodes()) {
308 currentAugmentChildNodes.add(dataSchemaNode.getQName());
311 if(childToProcess.getPossibleChildNames().equals(currentAugmentChildNodes)){
319 public static YangInstanceIdentifier.AugmentationIdentifier getNodeIdentifierForAugmentation(final AugmentationSchema schema) {
320 return new YangInstanceIdentifier.AugmentationIdentifier(getChildQNames(schema));
323 public static Set<QName> getChildQNames(final AugmentationSchema schema) {
324 Set<QName> qnames = Sets.newHashSet();
326 for (DataSchemaNode dataSchemaNode : schema.getChildNodes()) {
327 qnames.add(dataSchemaNode.getQName());