Rename ValueNodeModificationStrategy
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / SchemaUtils.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.data.impl.schema;
9
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;
18 import java.util.Map;
19 import java.util.Set;
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;
31
32 public final class SchemaUtils {
33
34     private SchemaUtils() {
35     }
36
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()) {
46                             return foundDsn;
47                         }
48                     }
49                 }
50             }
51         }
52         return Optional.absent();
53     }
54
55     public static DataSchemaNode findSchemaForChild(final DataNodeContainer schema, final QName qname) {
56         return findSchemaForChild(schema, qname, schema.getChildNodes());
57     }
58
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();
64     }
65
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",
69                 qNames, schema);
70         return schemaForAugment.get();
71     }
72
73     public static AugmentationSchema findSchemaForAugment(final ChoiceSchemaNode schema, final Set<QName> qNames) {
74         Optional<AugmentationSchema> schemaForAugment = Optional.absent();
75
76         for (ChoiceCaseNode choiceCaseNode : schema.getCases()) {
77             schemaForAugment = findAugment(choiceCaseNode, qNames);
78             if(schemaForAugment.isPresent()) {
79                 break;
80             }
81         }
82
83         Preconditions.checkState(schemaForAugment.isPresent(), "Unknown augmentation node detected, identified by: %s, in: %s",
84                 qNames, schema);
85         return schemaForAugment.get();
86     }
87
88     private static Optional<AugmentationSchema> findAugment(final AugmentationTarget schema, final Set<QName> qNames) {
89         for (AugmentationSchema augment : schema.getAvailableAugmentations()) {
90
91             HashSet<QName> qNamesFromAugment = Sets.newHashSet(Collections2.transform(augment.getChildNodes(), new Function<DataSchemaNode, QName>() {
92                 @Override
93                 public QName apply(final @Nonnull DataSchemaNode input) {
94                     Preconditions.checkNotNull(input);
95                     return input.getQName();
96                 }
97             }));
98
99             if(qNamesFromAugment.equals(qNames)) {
100                 return Optional.of(augment);
101             }
102         }
103
104         return Optional.absent();
105     }
106
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();
112             }
113         }
114
115
116         throw new IllegalStateException(String.format("Unknown child(ren) node(s) detected, identified by: %s, in: %s",
117                 childPartialQName, schema));
118     }
119
120     /**
121      * Recursively find all child nodes that come from choices.
122      *
123      * @return Map with all child nodes, to their most top augmentation
124      */
125     public static Map<QName, ChoiceSchemaNode> mapChildElementsFromChoices(final DataNodeContainer schema) {
126         return mapChildElementsFromChoices(schema, schema.getChildNodes());
127     }
128
129     private static Map<QName, ChoiceSchemaNode> mapChildElementsFromChoices(final DataNodeContainer schema, final Iterable<DataSchemaNode> childNodes) {
130         Map<QName, ChoiceSchemaNode> mappedChoices = Maps.newLinkedHashMap();
131
132         for (final DataSchemaNode childSchema : childNodes) {
133             if (childSchema instanceof ChoiceSchemaNode) {
134
135                 if (isFromAugment(schema, childSchema)) {
136                     continue;
137                 }
138
139                 for (ChoiceCaseNode choiceCaseNode : ((ChoiceSchemaNode) childSchema).getCases()) {
140
141                     for (QName qName : getChildNodesRecursive(choiceCaseNode)) {
142                         mappedChoices.put(qName, (ChoiceSchemaNode) childSchema);
143                     }
144                 }
145             }
146         }
147
148         return mappedChoices;
149     }
150
151     private static boolean isFromAugment(final DataNodeContainer schema, final DataSchemaNode childSchema) {
152         if (!(schema instanceof AugmentationTarget)) {
153             return false;
154         }
155
156         for (AugmentationSchema augmentationSchema : ((AugmentationTarget) schema).getAvailableAugmentations()) {
157             if(augmentationSchema.getDataChildByName(childSchema.getQName()) != null) {
158                 return true;
159             }
160         }
161
162         return false;
163     }
164
165     /**
166      * Recursively find all child nodes that come from augmentations.
167      *
168      * @return Map with all child nodes, to their most top augmentation
169      */
170     public static Map<QName, AugmentationSchema> mapChildElementsFromAugments(final AugmentationTarget schema) {
171
172         Map<QName, AugmentationSchema> childNodesToAugmentation = Maps.newLinkedHashMap();
173
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);
179             }
180         }
181
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) {
185
186             for (DataSchemaNode child : ((DataNodeContainer) schema).getChildNodes()) {
187                 // If is not augmented child, continue
188                 if (!(augments.containsKey(child.getQName()))) {
189                     continue;
190                 }
191
192                 AugmentationSchema mostTopAugmentation = augments.get(child.getQName());
193
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);
198                     }
199                 } else if (child instanceof ChoiceSchemaNode) {
200                     for (ChoiceCaseNode choiceCaseNode : ((ChoiceSchemaNode) child).getCases()) {
201                         for (QName qName : getChildNodesRecursive(choiceCaseNode)) {
202                             childNodesToAugmentation.put(qName, mostTopAugmentation);
203                         }
204                     }
205                 } else {
206                     childNodesToAugmentation.put(child.getQName(), mostTopAugmentation);
207                 }
208             }
209         }
210
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()))) {
215                     continue;
216                 }
217
218                 for (QName qName : getChildNodesRecursive(choiceCaseNode)) {
219                     childNodesToAugmentation.put(qName, augments.get(choiceCaseNode.getQName()));
220                 }
221             }
222         }
223
224         return childNodesToAugmentation;
225     }
226
227     /**
228      * Recursively list all child nodes.
229      *
230      * In case of choice, augment and cases, step in.
231      */
232     public static Set<QName> getChildNodesRecursive(final DataNodeContainer nodeContainer) {
233         Set<QName> allChildNodes = Sets.newHashSet();
234
235         for (DataSchemaNode childSchema : nodeContainer.getChildNodes()) {
236             if(childSchema instanceof ChoiceSchemaNode) {
237                 for (ChoiceCaseNode choiceCaseNode : ((ChoiceSchemaNode) childSchema).getCases()) {
238                     allChildNodes.addAll(getChildNodesRecursive(choiceCaseNode));
239                 }
240             } else if(childSchema instanceof AugmentationSchema || childSchema instanceof ChoiceCaseNode) {
241                 allChildNodes.addAll(getChildNodesRecursive((DataNodeContainer) childSchema));
242             }
243             else {
244                 allChildNodes.add(childSchema.getQName());
245             }
246         }
247
248         return allChildNodes;
249     }
250
251     /**
252      * Retrieves real schemas for augmented child node.
253      *
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.
256      *
257      */
258     public static Set<DataSchemaNode> getRealSchemasForAugment(final AugmentationTarget targetSchema, final AugmentationSchema augmentSchema) {
259         if (!(targetSchema.getAvailableAugmentations().contains(augmentSchema))) {
260             return Collections.emptySet();
261         }
262
263         Set<DataSchemaNode> realChildNodes = Sets.newHashSet();
264
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()));
272                     }
273                 }
274             }
275         }
276
277         return realChildNodes;
278     }
279
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);
286         }
287         return realChildNodes;
288     }
289
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);
298             }
299         }
300
301         return Optional.absent();
302     }
303
304     public static boolean belongsToCaseAugment(final ChoiceCaseNode caseNode, final YangInstanceIdentifier.AugmentationIdentifier childToProcess) {
305         for (AugmentationSchema augmentationSchema : caseNode.getAvailableAugmentations()) {
306
307             Set<QName> currentAugmentChildNodes = Sets.newHashSet();
308             for (DataSchemaNode dataSchemaNode : augmentationSchema.getChildNodes()) {
309                 currentAugmentChildNodes.add(dataSchemaNode.getQName());
310             }
311
312             if(childToProcess.getPossibleChildNames().equals(currentAugmentChildNodes)){
313                 return true;
314             }
315         }
316
317         return false;
318     }
319
320     public static YangInstanceIdentifier.AugmentationIdentifier getNodeIdentifierForAugmentation(final AugmentationSchema schema) {
321         return new YangInstanceIdentifier.AugmentationIdentifier(getChildQNames(schema));
322     }
323
324     public static Set<QName> getChildQNames(final AugmentationSchema schema) {
325         Set<QName> qnames = Sets.newHashSet();
326
327         for (DataSchemaNode dataSchemaNode : schema.getChildNodes()) {
328             qnames.add(dataSchemaNode.getQName());
329         }
330
331         return qnames;
332     }
333 }