Fixed resolving of schema path and qname for nodes added by augmentation.
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / util / CopyUtils.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.parser.util;
9
10 import java.util.ArrayList;
11 import java.util.Collections;
12 import java.util.HashSet;
13 import java.util.List;
14 import java.util.Set;
15
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode;
18 import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
19 import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
20 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
21 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
23 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.MustDefinition;
27 import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath;
28 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
29 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
30 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
31 import org.opendaylight.yangtools.yang.model.util.ExtendedType;
32 import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationSchemaBuilder;
33 import org.opendaylight.yangtools.yang.parser.builder.api.Builder;
34 import org.opendaylight.yangtools.yang.parser.builder.api.DataSchemaNodeBuilder;
35 import org.opendaylight.yangtools.yang.parser.builder.api.GroupingBuilder;
36 import org.opendaylight.yangtools.yang.parser.builder.api.SchemaNodeBuilder;
37 import org.opendaylight.yangtools.yang.parser.builder.api.TypeDefinitionBuilder;
38 import org.opendaylight.yangtools.yang.parser.builder.api.UsesNodeBuilder;
39 import org.opendaylight.yangtools.yang.parser.builder.impl.AnyXmlBuilder;
40 import org.opendaylight.yangtools.yang.parser.builder.impl.AugmentationSchemaBuilderImpl;
41 import org.opendaylight.yangtools.yang.parser.builder.impl.ChoiceBuilder;
42 import org.opendaylight.yangtools.yang.parser.builder.impl.ChoiceCaseBuilder;
43 import org.opendaylight.yangtools.yang.parser.builder.impl.ConstraintsBuilder;
44 import org.opendaylight.yangtools.yang.parser.builder.impl.ContainerSchemaNodeBuilder;
45 import org.opendaylight.yangtools.yang.parser.builder.impl.GroupingBuilderImpl;
46 import org.opendaylight.yangtools.yang.parser.builder.impl.IdentityrefTypeBuilder;
47 import org.opendaylight.yangtools.yang.parser.builder.impl.LeafListSchemaNodeBuilder;
48 import org.opendaylight.yangtools.yang.parser.builder.impl.LeafSchemaNodeBuilder;
49 import org.opendaylight.yangtools.yang.parser.builder.impl.ListSchemaNodeBuilder;
50 import org.opendaylight.yangtools.yang.parser.builder.impl.ModuleBuilder;
51 import org.opendaylight.yangtools.yang.parser.builder.impl.TypeDefinitionBuilderImpl;
52 import org.opendaylight.yangtools.yang.parser.builder.impl.UnionTypeBuilder;
53 import org.opendaylight.yangtools.yang.parser.builder.impl.UnknownSchemaNodeBuilder;
54 import org.opendaylight.yangtools.yang.parser.builder.impl.UsesNodeBuilderImpl;
55
56 public final class CopyUtils {
57
58     private CopyUtils() {
59     }
60
61     /**
62      * Create copy of DataSchemaNodeBuilder with new parent. If updateQName is
63      * true, qname of node will be corrected based on new parent.
64      *
65      * @param old
66      * @param newParent
67      * @param updateQName
68      * @return
69      */
70     public static DataSchemaNodeBuilder copy(DataSchemaNodeBuilder old, Builder newParent, boolean updateQName) {
71         if (old instanceof AnyXmlBuilder) {
72             return copy((AnyXmlBuilder) old, newParent, updateQName);
73         } else if (old instanceof ChoiceBuilder) {
74             return copy((ChoiceBuilder) old, newParent, updateQName);
75         } else if (old instanceof ContainerSchemaNodeBuilder) {
76             return copy((ContainerSchemaNodeBuilder) old, newParent, updateQName);
77         } else if (old instanceof LeafSchemaNodeBuilder) {
78             return copy((LeafSchemaNodeBuilder) old, newParent, updateQName);
79         } else if (old instanceof LeafListSchemaNodeBuilder) {
80             return copy((LeafListSchemaNodeBuilder) old, newParent, updateQName);
81         } else if (old instanceof ListSchemaNodeBuilder) {
82             return copy((ListSchemaNodeBuilder) old, newParent, updateQName);
83         } else if (old instanceof ChoiceCaseBuilder) {
84             return copy((ChoiceCaseBuilder) old, newParent, updateQName);
85         } else {
86             throw new YangParseException(old.getModuleName(), old.getLine(),
87                     "Failed to copy node: Unknown type of DataSchemaNode: " + old);
88         }
89     }
90
91     private static AnyXmlBuilder copy(AnyXmlBuilder old, Builder newParent, boolean updateQName) {
92         DataBean data = getdata(old, newParent, updateQName);
93         QName newQName = data.qname;
94         SchemaPath newSchemaPath = data.schemaPath;
95
96         AnyXmlBuilder copy = new AnyXmlBuilder(newParent.getModuleName(), newParent.getLine(), newQName, newSchemaPath);
97         copyConstraints(copy.getConstraints(), old.getConstraints());
98         copy.setParent(newParent);
99         copy.setPath(newSchemaPath);
100         copy.setDescription(old.getDescription());
101         copy.setReference(old.getReference());
102         copy.setStatus(old.getStatus());
103         copy.setAugmenting(old.isAugmenting());
104         copy.setAddedByUses(old.isAddedByUses());
105         copy.setConfiguration(old.isConfiguration());
106         for (UnknownSchemaNodeBuilder un : old.getUnknownNodeBuilders()) {
107             copy.addUnknownNodeBuilder((copy(un, copy, updateQName)));
108         }
109
110         return copy;
111     }
112
113     private static ChoiceBuilder copy(ChoiceBuilder old, Builder newParent, boolean updateQName) {
114         DataBean data = getdata(old, newParent, updateQName);
115         QName newQName = data.qname;
116         SchemaPath newSchemaPath = data.schemaPath;
117
118         ChoiceBuilder copy = new ChoiceBuilder(newParent.getModuleName(), newParent.getLine(), newQName);
119         copyConstraints(copy.getConstraints(), old.getConstraints());
120         copy.setParent(newParent);
121         copy.setPath(newSchemaPath);
122         copy.setDescription(old.getDescription());
123         copy.setReference(old.getReference());
124         copy.setStatus(old.getStatus());
125         copy.setAugmenting(old.isAugmenting());
126         copy.setAddedByUses(old.isAddedByUses());
127         copy.setConfiguration(old.isConfiguration());
128         for (ChoiceCaseBuilder childNode : old.getCases()) {
129             copy.addCase(copy(childNode, copy, updateQName));
130         }
131         for (AugmentationSchemaBuilder augment : old.getAugmentations()) {
132             copy.addAugmentation(copyAugment(augment, copy));
133         }
134         for (UnknownSchemaNodeBuilder un : old.getUnknownNodeBuilders()) {
135             copy.addUnknownNodeBuilder((copy(un, copy, updateQName)));
136         }
137
138         return copy;
139     }
140
141     private static ChoiceCaseBuilder copy(ChoiceCaseBuilder old, Builder newParent, boolean updateQName) {
142         DataBean data = getdata(old, newParent, updateQName);
143         QName newQName = data.qname;
144         SchemaPath newSchemaPath = data.schemaPath;
145
146         ChoiceCaseBuilder copy = new ChoiceCaseBuilder(newParent.getModuleName(), newParent.getLine(), newQName);
147         copyConstraints(copy.getConstraints(), old.getConstraints());
148         copy.setParent(newParent);
149         copy.setPath(newSchemaPath);
150         copy.setDescription(old.getDescription());
151         copy.setReference(old.getReference());
152         copy.setStatus(old.getStatus());
153         copy.setAugmenting(old.isAugmenting());
154         copy.getChildNodes().addAll(old.getChildNodes());
155         for (DataSchemaNodeBuilder childNode : old.getChildNodeBuilders()) {
156             copy.addChildNode(copy(childNode, copy, updateQName));
157         }
158         copy.getGroupings().addAll(old.getGroupings());
159         for (GroupingBuilder grouping : old.getGroupingBuilders()) {
160             copy.addGrouping(copy(grouping, copy, updateQName));
161         }
162         for (TypeDefinitionBuilder tdb : old.getTypeDefinitionBuilders()) {
163             copy.addTypedef(copy(tdb, copy, updateQName));
164         }
165         for (UsesNodeBuilder oldUses : old.getUsesNodes()) {
166             copy.addUsesNode(copyUses(oldUses, copy));
167         }
168         for (UnknownSchemaNodeBuilder un : old.getUnknownNodeBuilders()) {
169             copy.addUnknownNodeBuilder((copy(un, copy, updateQName)));
170         }
171
172         return copy;
173     }
174
175     private static ContainerSchemaNodeBuilder copy(ContainerSchemaNodeBuilder old, Builder newParent,
176             boolean updateQName) {
177         DataBean data = getdata(old, newParent, updateQName);
178         QName newQName = data.qname;
179         SchemaPath newSchemaPath = data.schemaPath;
180
181         ContainerSchemaNodeBuilder copy = new ContainerSchemaNodeBuilder(newParent.getModuleName(),
182                 newParent.getLine(), newQName, newSchemaPath);
183         copyConstraints(copy.getConstraints(), old.getConstraints());
184         copy.setParent(newParent);
185         copy.setPath(newSchemaPath);
186         copy.setDescription(old.getDescription());
187         copy.setReference(old.getReference());
188         copy.setStatus(old.getStatus());
189         copy.setPresence(old.isPresence());
190         copy.setAugmenting(old.isAugmenting());
191         copy.setAddedByUses(old.isAddedByUses());
192         copy.setConfiguration(old.isConfiguration());
193         copy.setChildNodes(old.getChildNodes());
194         for (DataSchemaNodeBuilder childNode : old.getChildNodeBuilders()) {
195             copy.addChildNode(copy(childNode, copy, updateQName));
196         }
197         copy.getGroupings().addAll(old.getGroupings());
198         for (GroupingBuilder grouping : old.getGroupingBuilders()) {
199             copy.addGrouping(copy(grouping, copy, updateQName));
200         }
201         for (TypeDefinitionBuilder tdb : old.getTypeDefinitionBuilders()) {
202             copy.addTypedef(copy(tdb, copy, updateQName));
203         }
204         for (UsesNodeBuilder oldUses : old.getUsesNodes()) {
205             copy.addUsesNode(copyUses(oldUses, copy));
206         }
207         for (AugmentationSchemaBuilder augment : old.getAugmentations()) {
208             copy.addAugmentation(copyAugment(augment, copy));
209         }
210         for (UnknownSchemaNodeBuilder un : old.getUnknownNodeBuilders()) {
211             copy.addUnknownNodeBuilder((copy(un, copy, updateQName)));
212         }
213
214         return copy;
215     }
216
217     private static LeafSchemaNodeBuilder copy(LeafSchemaNodeBuilder old, Builder newParent, boolean updateQName) {
218         DataBean data = getdata(old, newParent, updateQName);
219         QName newQName = data.qname;
220         SchemaPath newSchemaPath = data.schemaPath;
221
222         LeafSchemaNodeBuilder copy = new LeafSchemaNodeBuilder(newParent.getModuleName(), newParent.getLine(),
223                 newQName, newSchemaPath);
224         copyConstraints(copy.getConstraints(), old.getConstraints());
225         copy.setParent(newParent);
226         copy.setPath(newSchemaPath);
227         copy.setDescription(old.getDescription());
228         copy.setReference(old.getReference());
229         copy.setStatus(old.getStatus());
230         copy.setAugmenting(old.isAugmenting());
231         copy.setAddedByUses(old.isAddedByUses());
232         copy.setConfiguration(old.isConfiguration());
233         for (UnknownSchemaNodeBuilder un : old.getUnknownNodeBuilders()) {
234             copy.addUnknownNodeBuilder((copy(un, copy, updateQName)));
235         }
236
237         if (old.getType() == null) {
238             copy.setTypedef(copy(old.getTypedef(), copy, updateQName));
239         } else {
240             copy.setType(old.getType());
241         }
242
243         copy.setDefaultStr(old.getDefaultStr());
244         copy.setUnits(old.getUnits());
245
246         return copy;
247     }
248
249     public static LeafListSchemaNodeBuilder copy(LeafListSchemaNodeBuilder old, Builder newParent, boolean updateQName) {
250         DataBean data = getdata(old, newParent, updateQName);
251         QName newQName = data.qname;
252         SchemaPath newSchemaPath = data.schemaPath;
253
254         LeafListSchemaNodeBuilder copy = new LeafListSchemaNodeBuilder(newParent.getModuleName(), newParent.getLine(),
255                 newQName, newSchemaPath);
256         copyConstraints(copy.getConstraints(), old.getConstraints());
257         copy.setParent(newParent);
258         copy.setPath(newSchemaPath);
259         copy.setDescription(old.getDescription());
260         copy.setReference(old.getReference());
261         copy.setStatus(old.getStatus());
262         copy.setAugmenting(old.isAugmenting());
263         copy.setAddedByUses(old.isAddedByUses());
264         copy.setConfiguration(old.isConfiguration());
265         for (UnknownSchemaNodeBuilder un : old.getUnknownNodeBuilders()) {
266             copy.addUnknownNodeBuilder((copy(un, copy, updateQName)));
267         }
268
269         if (old.getType() == null) {
270             copy.setTypedef(copy(old.getTypedef(), copy, updateQName));
271         } else {
272             copy.setType(old.getType());
273         }
274
275         copy.setUserOrdered(old.isUserOrdered());
276
277         return copy;
278     }
279
280     private static ListSchemaNodeBuilder copy(ListSchemaNodeBuilder old, Builder newParent, boolean updateQName) {
281         DataBean data = getdata(old, newParent, updateQName);
282         QName newQName = data.qname;
283         SchemaPath newSchemaPath = data.schemaPath;
284
285         ListSchemaNodeBuilder copy = new ListSchemaNodeBuilder(newParent.getModuleName(), newParent.getLine(),
286                 newQName, newSchemaPath);
287         copyConstraints(copy.getConstraints(), old.getConstraints());
288         copy.setParent(newParent);
289         copy.setPath(newSchemaPath);
290         copy.setDescription(old.getDescription());
291         copy.setReference(old.getReference());
292         copy.setStatus(old.getStatus());
293         copy.setAugmenting(old.isAugmenting());
294         copy.setAddedByUses(old.isAddedByUses());
295         copy.setConfiguration(old.isConfiguration());
296         copy.setChildNodes(old.getChildNodes());
297         for (DataSchemaNodeBuilder childNode : old.getChildNodeBuilders()) {
298             copy.addChildNode(copy(childNode, copy, updateQName));
299         }
300         copy.getGroupings().addAll(old.getGroupings());
301         for (GroupingBuilder grouping : old.getGroupingBuilders()) {
302             copy.addGrouping(copy(grouping, copy, updateQName));
303         }
304         for (TypeDefinitionBuilder tdb : old.getTypeDefinitionBuilders()) {
305             copy.addTypedef(copy(tdb, copy, updateQName));
306         }
307         for (UsesNodeBuilder oldUses : old.getUsesNodes()) {
308             copy.addUsesNode(copyUses(oldUses, copy));
309         }
310         for (AugmentationSchemaBuilder augment : old.getAugmentations()) {
311             copy.addAugmentation(copyAugment(augment, copy));
312         }
313         for (UnknownSchemaNodeBuilder un : old.getUnknownNodeBuilders()) {
314             copy.addUnknownNodeBuilder((copy(un, copy, updateQName)));
315         }
316
317         copy.setUserOrdered(old.isUserOrdered());
318         copy.setKeyDefinition(old.getKeyDefinition());
319
320         return copy;
321     }
322
323     public static GroupingBuilder copy(GroupingBuilder old, Builder newParent, boolean updateQName) {
324         DataBean data = getdata(old, newParent, updateQName);
325         QName newQName = data.qname;
326         SchemaPath newSchemaPath = data.schemaPath;
327
328         GroupingBuilderImpl copy = new GroupingBuilderImpl(newParent.getModuleName(), newParent.getLine(), newQName);
329         copy.setParent(newParent);
330         copy.setPath(newSchemaPath);
331         copy.setDescription(old.getDescription());
332         copy.setReference(old.getReference());
333         copy.setStatus(old.getStatus());
334         copy.setAddedByUses(old.isAddedByUses());
335         copy.setChildNodes(old.getChildNodes());
336         for (DataSchemaNodeBuilder childNode : old.getChildNodeBuilders()) {
337             copy.addChildNode(copy(childNode, copy, updateQName));
338         }
339         copy.getGroupings().addAll(old.getGroupings());
340         for (GroupingBuilder grouping : old.getGroupingBuilders()) {
341             copy.addGrouping(copy(grouping, copy, updateQName));
342         }
343         for (TypeDefinitionBuilder tdb : old.getTypeDefinitionBuilders()) {
344             copy.addTypedef(copy(tdb, copy, updateQName));
345         }
346         for (UsesNodeBuilder oldUses : old.getUsesNodes()) {
347             copy.addUsesNode(copyUses(oldUses, copy));
348         }
349         for (UnknownSchemaNodeBuilder un : old.getUnknownNodeBuilders()) {
350             copy.addUnknownNodeBuilder((copy(un, copy, updateQName)));
351         }
352
353         return copy;
354     }
355
356     public static TypeDefinitionBuilder copy(TypeDefinitionBuilder old, Builder newParent, boolean updateQName) {
357         DataBean data = getdata(old, newParent, updateQName);
358         QName newQName = data.qname;
359         SchemaPath newSchemaPath = data.schemaPath;
360         TypeDefinitionBuilder type = null;
361
362         if (old instanceof UnionTypeBuilder) {
363             UnionTypeBuilder oldUnion = (UnionTypeBuilder) old;
364             type = new UnionTypeBuilder(newParent.getModuleName(), newParent.getLine());
365             type.setParent(newParent);
366             for (TypeDefinition<?> td : oldUnion.getTypes()) {
367                 type.setType(td);
368             }
369             for (TypeDefinitionBuilder tdb : oldUnion.getTypedefs()) {
370                 type.setTypedef(copy(tdb, type, updateQName));
371             }
372         } else if (old instanceof IdentityrefTypeBuilder) {
373             type = new IdentityrefTypeBuilder(newParent.getModuleName(), newParent.getLine(),
374                     ((IdentityrefTypeBuilder) old).getBaseString(), newSchemaPath);
375             type.setParent(newParent);
376             type.setPath(newSchemaPath);
377         } else {
378             type = new TypeDefinitionBuilderImpl(old.getModuleName(), newParent.getLine(), newQName);
379             type.setParent(newParent);
380             // TODO
381             //type.setPath(newSchemaPath);
382             type.setPath(old.getPath());
383
384             if (old.getType() == null) {
385                 type.setTypedef(copy(old.getTypedef(), type, updateQName));
386             } else {
387                 type.setType(old.getType());
388             }
389
390             for (UnknownSchemaNodeBuilder un : old.getUnknownNodeBuilders()) {
391                 type.addUnknownNodeBuilder((copy(un, type, updateQName)));
392             }
393
394             type.setRanges(old.getRanges());
395             type.setLengths(old.getLengths());
396             type.setPatterns(old.getPatterns());
397             type.setFractionDigits(old.getFractionDigits());
398             type.setDescription(old.getDescription());
399             type.setReference(old.getReference());
400             type.setStatus(old.getStatus());
401             type.setUnits(old.getUnits());
402             type.setDefaultValue(old.getDefaultValue());
403             type.setAddedByUses(old.isAddedByUses());
404         }
405
406         return type;
407     }
408
409     private static ConstraintsBuilder copyConstraints(ConstraintsBuilder newConstraints, ConstraintsBuilder old) {
410         newConstraints.getMustDefinitions().addAll(old.getMustDefinitions());
411         newConstraints.addWhenCondition(old.getWhenCondition());
412         newConstraints.setMandatory(old.isMandatory());
413         newConstraints.setMinElements(old.getMinElements());
414         newConstraints.setMaxElements(old.getMaxElements());
415         return newConstraints;
416     }
417
418     static UsesNodeBuilder copyUses(UsesNodeBuilder old, Builder newParent) {
419         UsesNodeBuilder copy = new UsesNodeBuilderImpl(newParent.getModuleName(), newParent.getLine(),
420                 old.getGroupingPathAsString(), true);
421         copy.setParent(newParent);
422         copy.setGroupingDefinition(old.getGroupingDefinition());
423         copy.setGrouping(old.getGroupingBuilder());
424         copy.setAddedByUses(old.isAddedByUses());
425         copy.getAugmentations().addAll(old.getAugmentations());
426         copy.getRefineNodes().addAll(old.getRefineNodes());
427         copy.getRefines().addAll(old.getRefines());
428         copy.setAugmenting(old.isAugmenting());
429         copy.setParentAugment(old.getParentAugment());
430
431         // target child nodes
432         Set<DataSchemaNodeBuilder> newTargetChildren = new HashSet<>();
433         for(DataSchemaNodeBuilder dnb : old.getTargetChildren()) {
434             newTargetChildren.add(copy(dnb, newParent, true));
435         }
436         copy.getTargetChildren().addAll(newTargetChildren);
437
438         // target typedefs
439         Set<TypeDefinitionBuilder> newTargetTypedefs = new HashSet<>();
440         for(TypeDefinitionBuilder tdb : old.getTargetTypedefs()) {
441             newTargetTypedefs.add(copy(tdb, newParent, true));
442         }
443         copy.getTargetTypedefs().addAll(newTargetTypedefs);
444
445         // target groupings
446         Set<GroupingBuilder> newTargetGroupings = new HashSet<>();
447         for(GroupingBuilder gb : old.getTargetGroupings()) {
448             newTargetGroupings.add(copy(gb, newParent, true));
449         }
450         copy.getTargetGroupings().addAll(newTargetGroupings);
451
452         // target unknown nodes
453         Set<UnknownSchemaNodeBuilder> newTargetUnknownNodes = new HashSet<>();
454         for(UnknownSchemaNodeBuilder unb : old.getTargetUnknownNodes()) {
455             newTargetUnknownNodes.add(copy(unb, newParent, true));
456         }
457         copy.getTargetUnknownNodes().addAll(newTargetUnknownNodes);
458
459         // add new uses to collection of uses in module
460         ModuleBuilder module = ParserUtils.getParentModule(newParent);
461         module.getAllUsesNodes().add(copy);
462
463         return copy;
464     }
465
466     private static AugmentationSchemaBuilder copyAugment(AugmentationSchemaBuilder old, Builder newParent) {
467         AugmentationSchemaBuilderImpl copy = new AugmentationSchemaBuilderImpl(newParent.getModuleName(),
468                 newParent.getLine(), old.getTargetPathAsString());
469         copy.setParent(newParent);
470
471         copy.setDescription(old.getDescription());
472         copy.setReference(old.getReference());
473         copy.setStatus(old.getStatus());
474         copy.addWhenCondition(old.getWhenCondition());
475         copy.setChildNodes(old.getChildNodes());
476         for (DataSchemaNodeBuilder childNode : old.getChildNodeBuilders()) {
477             copy.addChildNode(copy(childNode, copy, false));
478         }
479         for (UsesNodeBuilder oldUses : old.getUsesNodes()) {
480             copy.addUsesNode(copyUses(oldUses, copy));
481         }
482         for (UnknownSchemaNodeBuilder un : old.getUnknownNodeBuilders()) {
483             copy.addUnknownNodeBuilder((copy(un, copy, false)));
484         }
485
486         return copy;
487     }
488
489     static UnknownSchemaNodeBuilder copy(UnknownSchemaNodeBuilder old, Builder newParent, boolean updateQName) {
490         DataBean data = getdata(old, newParent, updateQName);
491         QName newQName = data.qname;
492         SchemaPath newSchemaPath = data.schemaPath;
493
494         UnknownSchemaNodeBuilder c = new UnknownSchemaNodeBuilder(newParent.getModuleName(), newParent.getLine(),
495                 newQName);
496
497         c.setParent(newParent);
498         c.setPath(newSchemaPath);
499         c.setDescription(old.getDescription());
500         c.setReference(old.getReference());
501         c.setStatus(old.getStatus());
502         c.setAddedByUses(old.isAddedByUses());
503         for (UnknownSchemaNodeBuilder un : old.getUnknownNodeBuilders()) {
504             c.addUnknownNodeBuilder((copy(un, c, updateQName)));
505         }
506
507         return c;
508     }
509
510     private static DataBean getdata(SchemaNodeBuilder old, Builder newParent, boolean updateQName) {
511         List<QName> newPath = null;
512         QName newQName = null;
513         if (newParent instanceof ModuleBuilder) {
514             ModuleBuilder parent = (ModuleBuilder) newParent;
515             if (updateQName) {
516                 newQName = new QName(parent.getNamespace(), parent.getRevision(), parent.getPrefix(), old.getQName()
517                         .getLocalName());
518                 newPath = Collections.singletonList(newQName);
519             } else {
520                 newQName = old.getQName();
521                 newPath = Collections.singletonList(newQName);
522             }
523         } else if (newParent instanceof AugmentationSchemaBuilder) {
524             AugmentationSchemaBuilder augment = (AugmentationSchemaBuilder)newParent;
525             ModuleBuilder parent = ParserUtils.getParentModule(newParent);
526             if (updateQName) {
527                 newQName = new QName(parent.getNamespace(), parent.getRevision(), parent.getPrefix(), old.getQName()
528                         .getLocalName());
529                 newPath = new ArrayList<>(augment.getTargetPath().getPath());
530                 newPath.add(newQName);
531             } else {
532                 newQName = old.getQName();
533                 newPath = new ArrayList<>(augment.getTargetPath().getPath());
534                 newPath.add(newQName);
535             }
536
537         } else if (newParent instanceof SchemaNodeBuilder) {
538             SchemaNodeBuilder parent = (SchemaNodeBuilder) newParent;
539             QName parentQName = parent.getQName();
540             if (updateQName) {
541                 newQName = new QName(parentQName.getNamespace(), parentQName.getRevision(), parentQName.getPrefix(),
542                         old.getQName().getLocalName());
543                 newPath = new ArrayList<>(parent.getPath().getPath());
544                 newPath.add(newQName);
545             } else {
546                 newQName = old.getQName();
547                 newPath = new ArrayList<>(parent.getPath().getPath());
548                 newPath.add(newQName);
549             }
550         }
551
552         SchemaPath newSchemaPath = new SchemaPath(newPath, true);
553         return new DataBean(newQName, newSchemaPath);
554     }
555
556     private static final class DataBean {
557         private QName qname;
558         private SchemaPath schemaPath;
559
560         private DataBean(QName qname, SchemaPath schemaPath) {
561             this.qname = qname;
562             this.schemaPath = schemaPath;
563         }
564     }
565
566     /**
567      * Create AnyXmlBuilder from given AnyXmlSchemaNode.
568      *
569      * @param anyxml
570      * @param qname
571      * @param moduleName
572      *            current module name
573      * @param line
574      *            current line in module
575      * @return anyxml builder based on given anyxml node
576      */
577     public static AnyXmlBuilder createAnyXml(AnyXmlSchemaNode anyxml, QName qname, String moduleName, int line) {
578         final AnyXmlBuilder builder = new AnyXmlBuilder(moduleName, line, qname, anyxml.getPath());
579         convertDataSchemaNode(anyxml, builder);
580         builder.setConfiguration(anyxml.isConfiguration());
581         builder.setUnknownNodes(anyxml.getUnknownSchemaNodes());
582         return builder;
583     }
584
585     /**
586      * Create GroupingBuilder from given GroupingDefinition.
587      *
588      * @param grouping
589      * @param qname
590      * @param moduleName
591      *            current module name
592      * @param line
593      *            current line in module
594      * @return grouping builder based on given grouping node
595      */
596     public static GroupingBuilder createGrouping(GroupingDefinition grouping, QName qname, String moduleName, int line) {
597         final GroupingBuilderImpl builder = new GroupingBuilderImpl(moduleName, line, qname);
598         builder.setPath(grouping.getPath());
599         builder.setChildNodes(grouping.getChildNodes());
600         builder.setGroupings(grouping.getGroupings());
601         builder.setTypedefs(grouping.getTypeDefinitions());
602         builder.setUsesnodes(grouping.getUses());
603         builder.setUnknownNodes(grouping.getUnknownSchemaNodes());
604         builder.setDescription(grouping.getDescription());
605         builder.setReference(grouping.getReference());
606         builder.setStatus(grouping.getStatus());
607         return builder;
608     }
609
610     /**
611      * Create TypeDefinitionBuilder from given ExtendedType.
612      *
613      * @param typedef
614      * @param qname
615      * @param moduleName
616      *            current module name
617      * @param line
618      *            current line in module
619      * @return typedef builder based on given typedef node
620      */
621     public static TypeDefinitionBuilder createTypedef(ExtendedType typedef, QName qname, String moduleName, int line) {
622         final TypeDefinitionBuilderImpl builder = new TypeDefinitionBuilderImpl(moduleName, line, qname);
623         builder.setPath(typedef.getPath());
624         builder.setDefaultValue(typedef.getDefaultValue());
625         builder.setUnits(typedef.getUnits());
626         builder.setDescription(typedef.getDescription());
627         builder.setReference(typedef.getReference());
628         builder.setStatus(typedef.getStatus());
629         builder.setRanges(typedef.getRanges());
630         builder.setLengths(typedef.getLengths());
631         builder.setPatterns(typedef.getPatterns());
632         builder.setFractionDigits(typedef.getFractionDigits());
633         final TypeDefinition<?> type = typedef.getBaseType();
634         builder.setType(type);
635         builder.setUnits(typedef.getUnits());
636         builder.setUnknownNodes(typedef.getUnknownSchemaNodes());
637         return builder;
638     }
639
640     /**
641      * Create UnknownSchemaNodeBuilder from given UnknownSchemaNode.
642      *
643      * @param unknownNode
644      * @param qname
645      * @param moduleName
646      *            current module name
647      * @param line
648      *            current line in module
649      * @return unknown node builder based on given unknown node
650      */
651     public static UnknownSchemaNodeBuilder createUnknownSchemaNode(UnknownSchemaNode unknownNode, QName qname,
652             String moduleName, int line) {
653         final UnknownSchemaNodeBuilder builder = new UnknownSchemaNodeBuilder(moduleName, line, qname);
654         builder.setPath(unknownNode.getPath());
655         builder.setUnknownNodes(unknownNode.getUnknownSchemaNodes());
656         builder.setDescription(unknownNode.getDescription());
657         builder.setReference(unknownNode.getReference());
658         builder.setStatus(unknownNode.getStatus());
659         builder.setAddedByUses(unknownNode.isAddedByUses());
660         builder.setNodeType(unknownNode.getNodeType());
661         builder.setNodeParameter(unknownNode.getNodeParameter());
662         return builder;
663     }
664
665     /**
666      * Create LeafSchemaNodeBuilder from given LeafSchemaNode.
667      *
668      * @param leaf
669      *            leaf from which to create builder
670      * @param qname
671      * @param moduleName
672      *            current module name
673      * @param line
674      *            line in module
675      * @return leaf builder based on given leaf node
676      */
677     public static LeafSchemaNodeBuilder createLeafBuilder(LeafSchemaNode leaf, QName qname, String moduleName, int line) {
678         final LeafSchemaNodeBuilder builder = new LeafSchemaNodeBuilder(moduleName, line, qname, leaf.getPath());
679         convertDataSchemaNode(leaf, builder);
680         builder.setConfiguration(leaf.isConfiguration());
681         final TypeDefinition<?> type = leaf.getType();
682         builder.setType(type);
683         builder.setPath(leaf.getPath());
684         builder.setUnknownNodes(leaf.getUnknownSchemaNodes());
685         builder.setDefaultStr(leaf.getDefault());
686         builder.setUnits(leaf.getUnits());
687         return builder;
688     }
689
690     /**
691      * Create ContainerSchemaNodeBuilder from given ContainerSchemaNode.
692      *
693      * @param container
694      * @param qname
695      * @param moduleName
696      *            current module name
697      * @param line
698      *            current line in module
699      * @return container builder based on given container node
700      */
701     public static ContainerSchemaNodeBuilder createContainer(ContainerSchemaNode container, QName qname,
702             String moduleName, int line) {
703         final ContainerSchemaNodeBuilder builder = new ContainerSchemaNodeBuilder(moduleName, line, qname,
704                 container.getPath());
705         convertDataSchemaNode(container, builder);
706         builder.setConfiguration(container.isConfiguration());
707         builder.setUnknownNodes(container.getUnknownSchemaNodes());
708         builder.setChildNodes(container.getChildNodes());
709         builder.setGroupings(container.getGroupings());
710         builder.setTypedefs(container.getTypeDefinitions());
711         builder.setAugmentations(container.getAvailableAugmentations());
712         builder.setUsesnodes(container.getUses());
713         builder.setPresence(container.isPresenceContainer());
714         return builder;
715     }
716
717     /**
718      * Create ListSchemaNodeBuilder from given ListSchemaNode.
719      *
720      * @param list
721      * @param qname
722      * @param moduleName
723      *            current module name
724      * @param line
725      *            current line in module
726      * @return list builder based on given list node
727      */
728     public static ListSchemaNodeBuilder createList(ListSchemaNode list, QName qname, String moduleName, int line) {
729         ListSchemaNodeBuilder builder = new ListSchemaNodeBuilder(moduleName, line, qname, list.getPath());
730         convertDataSchemaNode(list, builder);
731         builder.setConfiguration(list.isConfiguration());
732         builder.setUnknownNodes(list.getUnknownSchemaNodes());
733         builder.setTypedefs(list.getTypeDefinitions());
734         builder.setChildNodes(list.getChildNodes());
735         builder.setGroupings(list.getGroupings());
736         builder.setAugmentations(list.getAvailableAugmentations());
737         builder.setUsesnodes(list.getUses());
738         builder.setUserOrdered(builder.isUserOrdered());
739         return builder;
740     }
741
742     /**
743      * Create LeafListSchemaNodeBuilder from given LeafListSchemaNode.
744      *
745      * @param leafList
746      * @param qname
747      * @param moduleName
748      *            current module name
749      * @param line
750      *            current line in module
751      * @return leaf-list builder based on given leaf-list node
752      */
753     public static LeafListSchemaNodeBuilder createLeafList(LeafListSchemaNode leafList, QName qname, String moduleName,
754             int line) {
755         final LeafListSchemaNodeBuilder builder = new LeafListSchemaNodeBuilder(moduleName, line, qname,
756                 leafList.getPath());
757         convertDataSchemaNode(leafList, builder);
758         builder.setConfiguration(leafList.isConfiguration());
759         builder.setType(leafList.getType());
760         builder.setUnknownNodes(leafList.getUnknownSchemaNodes());
761         builder.setUserOrdered(leafList.isUserOrdered());
762         return builder;
763     }
764
765     /**
766      * Create ChoiceBuilder from given ChoiceNode.
767      *
768      * @param choice
769      * @param qname
770      * @param moduleName
771      *            current module name
772      * @param line
773      *            current line in module
774      * @return choice builder based on given choice node
775      */
776     public static ChoiceBuilder createChoice(ChoiceNode choice, QName qname, String moduleName, int line) {
777         final ChoiceBuilder builder = new ChoiceBuilder(moduleName, line, qname);
778         convertDataSchemaNode(choice, builder);
779         builder.setConfiguration(choice.isConfiguration());
780         builder.setCases(choice.getCases());
781         builder.setUnknownNodes(choice.getUnknownSchemaNodes());
782         builder.setDefaultCase(choice.getDefaultCase());
783         return builder;
784     }
785
786     /**
787      * Set DataSchemaNode arguments to builder object
788      *
789      * @param node
790      *            node from which arguments should be read
791      * @param builder
792      *            builder to which arguments should be set
793      */
794     private static void convertDataSchemaNode(DataSchemaNode node, DataSchemaNodeBuilder builder) {
795         builder.setPath(node.getPath());
796         builder.setDescription(node.getDescription());
797         builder.setReference(node.getReference());
798         builder.setStatus(node.getStatus());
799         builder.setAugmenting(node.isAugmenting());
800         copyConstraintsFromDefinition(node.getConstraints(), builder.getConstraints());
801     }
802
803     /**
804      * Copy constraints from constraints definition to constraints builder.
805      *
806      * @param nodeConstraints
807      *            definition from which constraints will be copied
808      * @param constraints
809      *            builder to which constraints will be added
810      */
811     private static void copyConstraintsFromDefinition(final ConstraintDefinition nodeConstraints,
812             final ConstraintsBuilder constraints) {
813         final RevisionAwareXPath when = nodeConstraints.getWhenCondition();
814         final Set<MustDefinition> must = nodeConstraints.getMustConstraints();
815
816         if (when != null) {
817             constraints.addWhenCondition(when.toString());
818         }
819         if (must != null) {
820             for (MustDefinition md : must) {
821                 constraints.addMustDefinition(md);
822             }
823         }
824         constraints.setMandatory(nodeConstraints.isMandatory());
825         constraints.setMinElements(nodeConstraints.getMinElements());
826         constraints.setMaxElements(nodeConstraints.getMaxElements());
827     }
828
829 }