BUG 2908 - Unable to read data from netconf northbound loopback connection.
[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     /**
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
41      */
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) {
48                         sNode = dsn;
49                     }
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()) {
54                             return foundDsn;
55                         }
56                     }
57                 }
58             }
59         }
60         return Optional.fromNullable(sNode);
61     }
62
63     public static DataSchemaNode findSchemaForChild(final DataNodeContainer schema, final QName qname) {
64         return findSchemaForChild(schema, qname, schema.getChildNodes());
65     }
66
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();
72     }
73
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",
77                 qNames, schema);
78         return schemaForAugment.get();
79     }
80
81     public static AugmentationSchema findSchemaForAugment(final ChoiceSchemaNode schema, final Set<QName> qNames) {
82         Optional<AugmentationSchema> schemaForAugment = Optional.absent();
83
84         for (ChoiceCaseNode choiceCaseNode : schema.getCases()) {
85             schemaForAugment = findAugment(choiceCaseNode, qNames);
86             if(schemaForAugment.isPresent()) {
87                 break;
88             }
89         }
90
91         Preconditions.checkState(schemaForAugment.isPresent(), "Unknown augmentation node detected, identified by: %s, in: %s",
92                 qNames, schema);
93         return schemaForAugment.get();
94     }
95
96     private static Optional<AugmentationSchema> findAugment(final AugmentationTarget schema, final Set<QName> qNames) {
97         for (AugmentationSchema augment : schema.getAvailableAugmentations()) {
98
99             HashSet<QName> qNamesFromAugment = Sets.newHashSet(Collections2.transform(augment.getChildNodes(), new Function<DataSchemaNode, QName>() {
100                 @Override
101                 public QName apply(final @Nonnull DataSchemaNode input) {
102                     Preconditions.checkNotNull(input);
103                     return input.getQName();
104                 }
105             }));
106
107             if(qNamesFromAugment.equals(qNames)) {
108                 return Optional.of(augment);
109             }
110         }
111
112         return Optional.absent();
113     }
114
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();
120             }
121         }
122
123
124         throw new IllegalStateException(String.format("Unknown child(ren) node(s) detected, identified by: %s, in: %s",
125                 childPartialQName, schema));
126     }
127
128     /**
129      * Recursively find all child nodes that come from choices.
130      *
131      * @return Map with all child nodes, to their most top augmentation
132      */
133     public static Map<QName, ChoiceSchemaNode> mapChildElementsFromChoices(final DataNodeContainer schema) {
134         return mapChildElementsFromChoices(schema, schema.getChildNodes());
135     }
136
137     private static Map<QName, ChoiceSchemaNode> mapChildElementsFromChoices(final DataNodeContainer schema, final Iterable<DataSchemaNode> childNodes) {
138         Map<QName, ChoiceSchemaNode> mappedChoices = Maps.newLinkedHashMap();
139
140         for (final DataSchemaNode childSchema : childNodes) {
141             if (childSchema instanceof ChoiceSchemaNode) {
142
143                 if (isFromAugment(schema, childSchema)) {
144                     continue;
145                 }
146
147                 for (ChoiceCaseNode choiceCaseNode : ((ChoiceSchemaNode) childSchema).getCases()) {
148
149                     for (QName qName : getChildNodesRecursive(choiceCaseNode)) {
150                         mappedChoices.put(qName, (ChoiceSchemaNode) childSchema);
151                     }
152                 }
153             }
154         }
155
156         return mappedChoices;
157     }
158
159     private static boolean isFromAugment(final DataNodeContainer schema, final DataSchemaNode childSchema) {
160         if (!(schema instanceof AugmentationTarget)) {
161             return false;
162         }
163
164         for (AugmentationSchema augmentationSchema : ((AugmentationTarget) schema).getAvailableAugmentations()) {
165             if(augmentationSchema.getDataChildByName(childSchema.getQName()) != null) {
166                 return true;
167             }
168         }
169
170         return false;
171     }
172
173     /**
174      * Recursively find all child nodes that come from augmentations.
175      *
176      * @return Map with all child nodes, to their most top augmentation
177      */
178     public static Map<QName, AugmentationSchema> mapChildElementsFromAugments(final AugmentationTarget schema) {
179
180         Map<QName, AugmentationSchema> childNodesToAugmentation = Maps.newLinkedHashMap();
181
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);
187             }
188         }
189
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) {
193
194             for (DataSchemaNode child : ((DataNodeContainer) schema).getChildNodes()) {
195                 // If is not augmented child, continue
196                 if (!(augments.containsKey(child.getQName()))) {
197                     continue;
198                 }
199
200                 AugmentationSchema mostTopAugmentation = augments.get(child.getQName());
201
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);
206                     }
207                 } else if (child instanceof ChoiceSchemaNode) {
208                     for (ChoiceCaseNode choiceCaseNode : ((ChoiceSchemaNode) child).getCases()) {
209                         for (QName qName : getChildNodesRecursive(choiceCaseNode)) {
210                             childNodesToAugmentation.put(qName, mostTopAugmentation);
211                         }
212                     }
213                 } else {
214                     childNodesToAugmentation.put(child.getQName(), mostTopAugmentation);
215                 }
216             }
217         }
218
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()))) {
223                     continue;
224                 }
225
226                 for (QName qName : getChildNodesRecursive(choiceCaseNode)) {
227                     childNodesToAugmentation.put(qName, augments.get(choiceCaseNode.getQName()));
228                 }
229             }
230         }
231
232         return childNodesToAugmentation;
233     }
234
235     /**
236      * Recursively list all child nodes.
237      *
238      * In case of choice, augment and cases, step in.
239      */
240     public static Set<QName> getChildNodesRecursive(final DataNodeContainer nodeContainer) {
241         Set<QName> allChildNodes = Sets.newHashSet();
242
243         for (DataSchemaNode childSchema : nodeContainer.getChildNodes()) {
244             if(childSchema instanceof ChoiceSchemaNode) {
245                 for (ChoiceCaseNode choiceCaseNode : ((ChoiceSchemaNode) childSchema).getCases()) {
246                     allChildNodes.addAll(getChildNodesRecursive(choiceCaseNode));
247                 }
248             } else if(childSchema instanceof AugmentationSchema || childSchema instanceof ChoiceCaseNode) {
249                 allChildNodes.addAll(getChildNodesRecursive((DataNodeContainer) childSchema));
250             }
251             else {
252                 allChildNodes.add(childSchema.getQName());
253             }
254         }
255
256         return allChildNodes;
257     }
258
259     /**
260      * Retrieves real schemas for augmented child node.
261      *
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.
264      *
265      */
266     public static Set<DataSchemaNode> getRealSchemasForAugment(final AugmentationTarget targetSchema, final AugmentationSchema augmentSchema) {
267         if (!(targetSchema.getAvailableAugmentations().contains(augmentSchema))) {
268             return Collections.emptySet();
269         }
270
271         Set<DataSchemaNode> realChildNodes = Sets.newHashSet();
272
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()));
280                     }
281                 }
282             }
283         }
284
285         return realChildNodes;
286     }
287
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);
294         }
295         return realChildNodes;
296     }
297
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);
306             }
307         }
308
309         return Optional.absent();
310     }
311
312     public static boolean belongsToCaseAugment(final ChoiceCaseNode caseNode, final YangInstanceIdentifier.AugmentationIdentifier childToProcess) {
313         for (AugmentationSchema augmentationSchema : caseNode.getAvailableAugmentations()) {
314
315             Set<QName> currentAugmentChildNodes = Sets.newHashSet();
316             for (DataSchemaNode dataSchemaNode : augmentationSchema.getChildNodes()) {
317                 currentAugmentChildNodes.add(dataSchemaNode.getQName());
318             }
319
320             if(childToProcess.getPossibleChildNames().equals(currentAugmentChildNodes)){
321                 return true;
322             }
323         }
324
325         return false;
326     }
327
328     public static YangInstanceIdentifier.AugmentationIdentifier getNodeIdentifierForAugmentation(final AugmentationSchema schema) {
329         return new YangInstanceIdentifier.AugmentationIdentifier(getChildQNames(schema));
330     }
331
332     public static Set<QName> getChildQNames(final AugmentationSchema schema) {
333         Set<QName> qnames = Sets.newHashSet();
334
335         for (DataSchemaNode dataSchemaNode : schema.getChildNodes()) {
336             qnames.add(dataSchemaNode.getQName());
337         }
338
339         return qnames;
340     }
341 }