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() {
38 * @param qname - schema node to find
39 * @param dataSchemaNode - iterable of schemaNodes to look through
40 * @return - schema node with newest revision or absent if no schema node with matching qname is found
42 public static final Optional<DataSchemaNode> findFirstSchema(final QName qname, final Iterable<DataSchemaNode> dataSchemaNode) {
43 DataSchemaNode sNode = null;
44 if (dataSchemaNode != null && qname != null) {
45 for (DataSchemaNode dsn : dataSchemaNode) {
46 if (qname.isEqualWithoutRevision(dsn.getQName())) {
47 if (sNode == null || sNode.getQName().compareTo(dsn.getQName()) < 0) {
50 } else if (dsn instanceof ChoiceSchemaNode) {
51 for (ChoiceCaseNode choiceCase : ((ChoiceSchemaNode) dsn).getCases()) {
52 Optional<DataSchemaNode> foundDsn = findFirstSchema(qname, choiceCase.getChildNodes());
53 if (foundDsn != null && foundDsn.isPresent()) {
60 return Optional.fromNullable(sNode);
63 public static DataSchemaNode findSchemaForChild(final DataNodeContainer schema, final QName qname) {
64 return findSchemaForChild(schema, qname, schema.getChildNodes());
67 public static DataSchemaNode findSchemaForChild(final DataNodeContainer schema, final QName qname, final Iterable<DataSchemaNode> childNodes) {
68 Optional<DataSchemaNode> childSchema = findFirstSchema(qname, childNodes);
69 Preconditions.checkState(childSchema.isPresent(),
70 "Unknown child(ren) node(s) detected, identified by: %s, in: %s", qname, schema);
71 return childSchema.get();
74 public static AugmentationSchema findSchemaForAugment(final AugmentationTarget schema, final Set<QName> qNames) {
75 Optional<AugmentationSchema> schemaForAugment = findAugment(schema, qNames);
76 Preconditions.checkState(schemaForAugment.isPresent(), "Unknown augmentation node detected, identified by: %s, in: %s",
78 return schemaForAugment.get();
81 public static AugmentationSchema findSchemaForAugment(final ChoiceSchemaNode schema, final Set<QName> qNames) {
82 Optional<AugmentationSchema> schemaForAugment = Optional.absent();
84 for (ChoiceCaseNode choiceCaseNode : schema.getCases()) {
85 schemaForAugment = findAugment(choiceCaseNode, qNames);
86 if(schemaForAugment.isPresent()) {
91 Preconditions.checkState(schemaForAugment.isPresent(), "Unknown augmentation node detected, identified by: %s, in: %s",
93 return schemaForAugment.get();
96 private static Optional<AugmentationSchema> findAugment(final AugmentationTarget schema, final Set<QName> qNames) {
97 for (AugmentationSchema augment : schema.getAvailableAugmentations()) {
99 HashSet<QName> qNamesFromAugment = Sets.newHashSet(Collections2.transform(augment.getChildNodes(), new Function<DataSchemaNode, QName>() {
101 public QName apply(final @Nonnull DataSchemaNode input) {
102 Preconditions.checkNotNull(input);
103 return input.getQName();
107 if(qNamesFromAugment.equals(qNames)) {
108 return Optional.of(augment);
112 return Optional.absent();
115 public static DataSchemaNode findSchemaForChild(final ChoiceSchemaNode schema, final QName childPartialQName) {
116 for (ChoiceCaseNode choiceCaseNode : schema.getCases()) {
117 Optional<DataSchemaNode> childSchema = findFirstSchema(childPartialQName, choiceCaseNode.getChildNodes());
118 if (childSchema.isPresent()) {
119 return childSchema.get();
124 throw new IllegalStateException(String.format("Unknown child(ren) node(s) detected, identified by: %s, in: %s",
125 childPartialQName, schema));
129 * Recursively find all child nodes that come from choices.
131 * @return Map with all child nodes, to their most top augmentation
133 public static Map<QName, ChoiceSchemaNode> mapChildElementsFromChoices(final DataNodeContainer schema) {
134 return mapChildElementsFromChoices(schema, schema.getChildNodes());
137 private static Map<QName, ChoiceSchemaNode> mapChildElementsFromChoices(final DataNodeContainer schema, final Iterable<DataSchemaNode> childNodes) {
138 Map<QName, ChoiceSchemaNode> mappedChoices = Maps.newLinkedHashMap();
140 for (final DataSchemaNode childSchema : childNodes) {
141 if (childSchema instanceof ChoiceSchemaNode) {
143 if (isFromAugment(schema, childSchema)) {
147 for (ChoiceCaseNode choiceCaseNode : ((ChoiceSchemaNode) childSchema).getCases()) {
149 for (QName qName : getChildNodesRecursive(choiceCaseNode)) {
150 mappedChoices.put(qName, (ChoiceSchemaNode) childSchema);
156 return mappedChoices;
159 private static boolean isFromAugment(final DataNodeContainer schema, final DataSchemaNode childSchema) {
160 if (!(schema instanceof AugmentationTarget)) {
164 for (AugmentationSchema augmentationSchema : ((AugmentationTarget) schema).getAvailableAugmentations()) {
165 if(augmentationSchema.getDataChildByName(childSchema.getQName()) != null) {
174 * Recursively find all child nodes that come from augmentations.
176 * @return Map with all child nodes, to their most top augmentation
178 public static Map<QName, AugmentationSchema> mapChildElementsFromAugments(final AugmentationTarget schema) {
180 Map<QName, AugmentationSchema> childNodesToAugmentation = Maps.newLinkedHashMap();
182 // Find QNames of augmented child nodes
183 Map<QName, AugmentationSchema> augments = Maps.newHashMap();
184 for (final AugmentationSchema augmentationSchema : schema.getAvailableAugmentations()) {
185 for (DataSchemaNode dataSchemaNode : augmentationSchema.getChildNodes()) {
186 augments.put(dataSchemaNode.getQName(), augmentationSchema);
190 // Augmented nodes have to be looked up directly in augmentationTarget
191 // because nodes from augment do not contain nodes from other augmentations
192 if (schema instanceof DataNodeContainer) {
194 for (DataSchemaNode child : ((DataNodeContainer) schema).getChildNodes()) {
195 // If is not augmented child, continue
196 if (!(augments.containsKey(child.getQName()))) {
200 AugmentationSchema mostTopAugmentation = augments.get(child.getQName());
202 // recursively add all child nodes in case of augment, case and choice
203 if (child instanceof AugmentationSchema || child instanceof ChoiceCaseNode) {
204 for (QName qName : getChildNodesRecursive((DataNodeContainer) child)) {
205 childNodesToAugmentation.put(qName, mostTopAugmentation);
207 } else if (child instanceof ChoiceSchemaNode) {
208 for (ChoiceCaseNode choiceCaseNode : ((ChoiceSchemaNode) child).getCases()) {
209 for (QName qName : getChildNodesRecursive(choiceCaseNode)) {
210 childNodesToAugmentation.put(qName, mostTopAugmentation);
214 childNodesToAugmentation.put(child.getQName(), mostTopAugmentation);
219 // Choice Node has to map child nodes from all its cases
220 if (schema instanceof ChoiceSchemaNode) {
221 for (ChoiceCaseNode choiceCaseNode : ((ChoiceSchemaNode) schema).getCases()) {
222 if (!(augments.containsKey(choiceCaseNode.getQName()))) {
226 for (QName qName : getChildNodesRecursive(choiceCaseNode)) {
227 childNodesToAugmentation.put(qName, augments.get(choiceCaseNode.getQName()));
232 return childNodesToAugmentation;
236 * Recursively list all child nodes.
238 * In case of choice, augment and cases, step in.
240 public static Set<QName> getChildNodesRecursive(final DataNodeContainer nodeContainer) {
241 Set<QName> allChildNodes = Sets.newHashSet();
243 for (DataSchemaNode childSchema : nodeContainer.getChildNodes()) {
244 if(childSchema instanceof ChoiceSchemaNode) {
245 for (ChoiceCaseNode choiceCaseNode : ((ChoiceSchemaNode) childSchema).getCases()) {
246 allChildNodes.addAll(getChildNodesRecursive(choiceCaseNode));
248 } else if(childSchema instanceof AugmentationSchema || childSchema instanceof ChoiceCaseNode) {
249 allChildNodes.addAll(getChildNodesRecursive((DataNodeContainer) childSchema));
252 allChildNodes.add(childSchema.getQName());
256 return allChildNodes;
260 * Retrieves real schemas for augmented child node.
262 * Schema of the same child node from augment, and directly from target is not the same.
263 * Schema of child node from augment is incomplete, therefore its useless for XML/NormalizedNode translation.
266 public static Set<DataSchemaNode> getRealSchemasForAugment(final AugmentationTarget targetSchema, final AugmentationSchema augmentSchema) {
267 if (!(targetSchema.getAvailableAugmentations().contains(augmentSchema))) {
268 return Collections.emptySet();
271 Set<DataSchemaNode> realChildNodes = Sets.newHashSet();
273 if(targetSchema instanceof DataNodeContainer) {
274 realChildNodes = getRealSchemasForAugment((DataNodeContainer)targetSchema, augmentSchema);
275 } else if(targetSchema instanceof ChoiceSchemaNode) {
276 for (DataSchemaNode dataSchemaNode : augmentSchema.getChildNodes()) {
277 for (ChoiceCaseNode choiceCaseNode : ((ChoiceSchemaNode) targetSchema).getCases()) {
278 if(getChildNodesRecursive(choiceCaseNode).contains(dataSchemaNode.getQName())) {
279 realChildNodes.add(choiceCaseNode.getDataChildByName(dataSchemaNode.getQName()));
285 return realChildNodes;
288 public static Set<DataSchemaNode> getRealSchemasForAugment(final DataNodeContainer targetSchema,
289 final AugmentationSchema augmentSchema) {
290 Set<DataSchemaNode> realChildNodes = Sets.newHashSet();
291 for (DataSchemaNode dataSchemaNode : augmentSchema.getChildNodes()) {
292 DataSchemaNode realChild = targetSchema.getDataChildByName(dataSchemaNode.getQName());
293 realChildNodes.add(realChild);
295 return realChildNodes;
298 public static Optional<ChoiceCaseNode> detectCase(final ChoiceSchemaNode schema, final DataContainerChild<?, ?> child) {
299 for (ChoiceCaseNode choiceCaseNode : schema.getCases()) {
300 if (child instanceof AugmentationNode
301 && belongsToCaseAugment(choiceCaseNode,
302 (YangInstanceIdentifier.AugmentationIdentifier) child.getIdentifier())) {
303 return Optional.of(choiceCaseNode);
304 } else if (choiceCaseNode.getDataChildByName(child.getNodeType()) != null) {
305 return Optional.of(choiceCaseNode);
309 return Optional.absent();
312 public static boolean belongsToCaseAugment(final ChoiceCaseNode caseNode, final YangInstanceIdentifier.AugmentationIdentifier childToProcess) {
313 for (AugmentationSchema augmentationSchema : caseNode.getAvailableAugmentations()) {
315 Set<QName> currentAugmentChildNodes = Sets.newHashSet();
316 for (DataSchemaNode dataSchemaNode : augmentationSchema.getChildNodes()) {
317 currentAugmentChildNodes.add(dataSchemaNode.getQName());
320 if(childToProcess.getPossibleChildNames().equals(currentAugmentChildNodes)){
328 public static YangInstanceIdentifier.AugmentationIdentifier getNodeIdentifierForAugmentation(final AugmentationSchema schema) {
329 return new YangInstanceIdentifier.AugmentationIdentifier(getChildQNames(schema));
332 public static Set<QName> getChildQNames(final AugmentationSchema schema) {
333 Set<QName> qnames = Sets.newHashSet();
335 for (DataSchemaNode dataSchemaNode : schema.getChildNodes()) {
336 qnames.add(dataSchemaNode.getQName());