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;
10 import com.google.common.base.Function;
11 import com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
13 import com.google.common.collect.Collections2;
14 import com.google.common.collect.Maps;
15 import com.google.common.collect.Sets;
16 import java.util.Collections;
17 import java.util.HashSet;
20 import javax.annotation.Nonnull;
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.ChoiceSchemaNode;
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 ChoiceSchemaNode) {
43 for (ChoiceCaseNode choiceCase : ((ChoiceSchemaNode) 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 ChoiceSchemaNode 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 @Nonnull DataSchemaNode input) {
94 Preconditions.checkNotNull(input);
95 return input.getQName();
99 if(qNamesFromAugment.equals(qNames)) {
100 return Optional.of(augment);
104 return Optional.absent();
107 public static DataSchemaNode findSchemaForChild(final ChoiceSchemaNode schema, final QName childPartialQName) {
108 for (ChoiceCaseNode choiceCaseNode : schema.getCases()) {
109 Optional<DataSchemaNode> childSchema = findFirstSchema(childPartialQName, choiceCaseNode.getChildNodes());
110 if (childSchema.isPresent()) {
111 return childSchema.get();
116 throw new IllegalStateException(String.format("Unknown child(ren) node(s) detected, identified by: %s, in: %s",
117 childPartialQName, schema));
121 * Recursively find all child nodes that come from choices.
123 * @return Map with all child nodes, to their most top augmentation
125 public static Map<QName, ChoiceSchemaNode> mapChildElementsFromChoices(final DataNodeContainer schema) {
126 return mapChildElementsFromChoices(schema, schema.getChildNodes());
129 private static Map<QName, ChoiceSchemaNode> mapChildElementsFromChoices(final DataNodeContainer schema, final Iterable<DataSchemaNode> childNodes) {
130 Map<QName, ChoiceSchemaNode> mappedChoices = Maps.newLinkedHashMap();
132 for (final DataSchemaNode childSchema : childNodes) {
133 if (childSchema instanceof ChoiceSchemaNode) {
135 if (isFromAugment(schema, childSchema)) {
139 for (ChoiceCaseNode choiceCaseNode : ((ChoiceSchemaNode) childSchema).getCases()) {
141 for (QName qName : getChildNodesRecursive(choiceCaseNode)) {
142 mappedChoices.put(qName, (ChoiceSchemaNode) childSchema);
148 return mappedChoices;
151 private static boolean isFromAugment(final DataNodeContainer schema, final DataSchemaNode childSchema) {
152 if (!(schema instanceof AugmentationTarget)) {
156 for (AugmentationSchema augmentationSchema : ((AugmentationTarget) schema).getAvailableAugmentations()) {
157 if(augmentationSchema.getDataChildByName(childSchema.getQName()) != null) {
166 * Recursively find all child nodes that come from augmentations.
168 * @return Map with all child nodes, to their most top augmentation
170 public static Map<QName, AugmentationSchema> mapChildElementsFromAugments(final AugmentationTarget schema) {
172 Map<QName, AugmentationSchema> childNodesToAugmentation = Maps.newLinkedHashMap();
174 // Find QNames of augmented child nodes
175 Map<QName, AugmentationSchema> augments = Maps.newHashMap();
176 for (final AugmentationSchema augmentationSchema : schema.getAvailableAugmentations()) {
177 for (DataSchemaNode dataSchemaNode : augmentationSchema.getChildNodes()) {
178 augments.put(dataSchemaNode.getQName(), augmentationSchema);
182 // Augmented nodes have to be looked up directly in augmentationTarget
183 // because nodes from augment do not contain nodes from other augmentations
184 if (schema instanceof DataNodeContainer) {
186 for (DataSchemaNode child : ((DataNodeContainer) schema).getChildNodes()) {
187 // If is not augmented child, continue
188 if (!(augments.containsKey(child.getQName()))) {
192 AugmentationSchema mostTopAugmentation = augments.get(child.getQName());
194 // recursively add all child nodes in case of augment, case and choice
195 if (child instanceof AugmentationSchema || child instanceof ChoiceCaseNode) {
196 for (QName qName : getChildNodesRecursive((DataNodeContainer) child)) {
197 childNodesToAugmentation.put(qName, mostTopAugmentation);
199 } else if (child instanceof ChoiceSchemaNode) {
200 for (ChoiceCaseNode choiceCaseNode : ((ChoiceSchemaNode) child).getCases()) {
201 for (QName qName : getChildNodesRecursive(choiceCaseNode)) {
202 childNodesToAugmentation.put(qName, mostTopAugmentation);
206 childNodesToAugmentation.put(child.getQName(), mostTopAugmentation);
211 // Choice Node has to map child nodes from all its cases
212 if (schema instanceof ChoiceSchemaNode) {
213 for (ChoiceCaseNode choiceCaseNode : ((ChoiceSchemaNode) schema).getCases()) {
214 if (!(augments.containsKey(choiceCaseNode.getQName()))) {
218 for (QName qName : getChildNodesRecursive(choiceCaseNode)) {
219 childNodesToAugmentation.put(qName, augments.get(choiceCaseNode.getQName()));
224 return childNodesToAugmentation;
228 * Recursively list all child nodes.
230 * In case of choice, augment and cases, step in.
232 public static Set<QName> getChildNodesRecursive(final DataNodeContainer nodeContainer) {
233 Set<QName> allChildNodes = Sets.newHashSet();
235 for (DataSchemaNode childSchema : nodeContainer.getChildNodes()) {
236 if(childSchema instanceof ChoiceSchemaNode) {
237 for (ChoiceCaseNode choiceCaseNode : ((ChoiceSchemaNode) childSchema).getCases()) {
238 allChildNodes.addAll(getChildNodesRecursive(choiceCaseNode));
240 } else if(childSchema instanceof AugmentationSchema || childSchema instanceof ChoiceCaseNode) {
241 allChildNodes.addAll(getChildNodesRecursive((DataNodeContainer) childSchema));
244 allChildNodes.add(childSchema.getQName());
248 return allChildNodes;
252 * Retrieves real schemas for augmented child node.
254 * Schema of the same child node from augment, and directly from target is not the same.
255 * Schema of child node from augment is incomplete, therefore its useless for XML/NormalizedNode translation.
258 public static Set<DataSchemaNode> getRealSchemasForAugment(final AugmentationTarget targetSchema, final AugmentationSchema augmentSchema) {
259 if (!(targetSchema.getAvailableAugmentations().contains(augmentSchema))) {
260 return Collections.emptySet();
263 Set<DataSchemaNode> realChildNodes = Sets.newHashSet();
265 if(targetSchema instanceof DataNodeContainer) {
266 realChildNodes = getRealSchemasForAugment((DataNodeContainer)targetSchema, augmentSchema);
267 } else if(targetSchema instanceof ChoiceSchemaNode) {
268 for (DataSchemaNode dataSchemaNode : augmentSchema.getChildNodes()) {
269 for (ChoiceCaseNode choiceCaseNode : ((ChoiceSchemaNode) targetSchema).getCases()) {
270 if(getChildNodesRecursive(choiceCaseNode).contains(dataSchemaNode.getQName())) {
271 realChildNodes.add(choiceCaseNode.getDataChildByName(dataSchemaNode.getQName()));
277 return realChildNodes;
280 public static Set<DataSchemaNode> getRealSchemasForAugment(final DataNodeContainer targetSchema,
281 final AugmentationSchema augmentSchema) {
282 Set<DataSchemaNode> realChildNodes = Sets.newHashSet();
283 for (DataSchemaNode dataSchemaNode : augmentSchema.getChildNodes()) {
284 DataSchemaNode realChild = targetSchema.getDataChildByName(dataSchemaNode.getQName());
285 realChildNodes.add(realChild);
287 return realChildNodes;
290 public static Optional<ChoiceCaseNode> detectCase(final ChoiceSchemaNode schema, final DataContainerChild<?, ?> child) {
291 for (ChoiceCaseNode choiceCaseNode : schema.getCases()) {
292 if (child instanceof AugmentationNode
293 && belongsToCaseAugment(choiceCaseNode,
294 (YangInstanceIdentifier.AugmentationIdentifier) child.getIdentifier())) {
295 return Optional.of(choiceCaseNode);
296 } else if (choiceCaseNode.getDataChildByName(child.getNodeType()) != null) {
297 return Optional.of(choiceCaseNode);
301 return Optional.absent();
304 public static boolean belongsToCaseAugment(final ChoiceCaseNode caseNode, final YangInstanceIdentifier.AugmentationIdentifier childToProcess) {
305 for (AugmentationSchema augmentationSchema : caseNode.getAvailableAugmentations()) {
307 Set<QName> currentAugmentChildNodes = Sets.newHashSet();
308 for (DataSchemaNode dataSchemaNode : augmentationSchema.getChildNodes()) {
309 currentAugmentChildNodes.add(dataSchemaNode.getQName());
312 if(childToProcess.getPossibleChildNames().equals(currentAugmentChildNodes)){
320 public static YangInstanceIdentifier.AugmentationIdentifier getNodeIdentifierForAugmentation(final AugmentationSchema schema) {
321 return new YangInstanceIdentifier.AugmentationIdentifier(getChildQNames(schema));
324 public static Set<QName> getChildQNames(final AugmentationSchema schema) {
325 Set<QName> qnames = Sets.newHashSet();
327 for (DataSchemaNode dataSchemaNode : schema.getChildNodes()) {
328 qnames.add(dataSchemaNode.getQName());