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.parser.util;
10 import java.util.ArrayList;
11 import java.util.Collections;
12 import java.util.List;
14 import org.opendaylight.yangtools.yang.common.QName;
15 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
16 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
17 import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationSchemaBuilder;
18 import org.opendaylight.yangtools.yang.parser.builder.api.Builder;
19 import org.opendaylight.yangtools.yang.parser.builder.api.DataSchemaNodeBuilder;
20 import org.opendaylight.yangtools.yang.parser.builder.api.GroupingBuilder;
21 import org.opendaylight.yangtools.yang.parser.builder.api.SchemaNodeBuilder;
22 import org.opendaylight.yangtools.yang.parser.builder.api.TypeDefinitionBuilder;
23 import org.opendaylight.yangtools.yang.parser.builder.api.UsesNodeBuilder;
24 import org.opendaylight.yangtools.yang.parser.builder.impl.AnyXmlBuilder;
25 import org.opendaylight.yangtools.yang.parser.builder.impl.AugmentationSchemaBuilderImpl;
26 import org.opendaylight.yangtools.yang.parser.builder.impl.ChoiceBuilder;
27 import org.opendaylight.yangtools.yang.parser.builder.impl.ChoiceCaseBuilder;
28 import org.opendaylight.yangtools.yang.parser.builder.impl.ConstraintsBuilder;
29 import org.opendaylight.yangtools.yang.parser.builder.impl.ContainerSchemaNodeBuilder;
30 import org.opendaylight.yangtools.yang.parser.builder.impl.GroupingBuilderImpl;
31 import org.opendaylight.yangtools.yang.parser.builder.impl.IdentityrefTypeBuilder;
32 import org.opendaylight.yangtools.yang.parser.builder.impl.LeafListSchemaNodeBuilder;
33 import org.opendaylight.yangtools.yang.parser.builder.impl.LeafSchemaNodeBuilder;
34 import org.opendaylight.yangtools.yang.parser.builder.impl.ListSchemaNodeBuilder;
35 import org.opendaylight.yangtools.yang.parser.builder.impl.ModuleBuilder;
36 import org.opendaylight.yangtools.yang.parser.builder.impl.TypeDefinitionBuilderImpl;
37 import org.opendaylight.yangtools.yang.parser.builder.impl.UnionTypeBuilder;
38 import org.opendaylight.yangtools.yang.parser.builder.impl.UnknownSchemaNodeBuilder;
39 import org.opendaylight.yangtools.yang.parser.builder.impl.UsesNodeBuilderImpl;
41 public final class CopyUtils {
47 * Create copy of DataSchemaNodeBuilder with new parent. If updateQName is
48 * true, qname of node will be corrected based on new parent.
55 * flag to indicate if qname should be updated based on new
57 * @return copy of given builder
59 public static DataSchemaNodeBuilder copy(final DataSchemaNodeBuilder old, final Builder newParent, final boolean updateQName) {
60 if (old instanceof AnyXmlBuilder) {
61 return copy((AnyXmlBuilder) old, newParent, updateQName);
62 } else if (old instanceof ChoiceBuilder) {
63 return copy((ChoiceBuilder) old, newParent, updateQName);
64 } else if (old instanceof ContainerSchemaNodeBuilder) {
65 return copy((ContainerSchemaNodeBuilder) old, newParent, updateQName);
66 } else if (old instanceof LeafSchemaNodeBuilder) {
67 return copy((LeafSchemaNodeBuilder) old, newParent, updateQName);
68 } else if (old instanceof LeafListSchemaNodeBuilder) {
69 return copy((LeafListSchemaNodeBuilder) old, newParent, updateQName);
70 } else if (old instanceof ListSchemaNodeBuilder) {
71 return copy((ListSchemaNodeBuilder) old, newParent, updateQName);
72 } else if (old instanceof ChoiceCaseBuilder) {
73 return copy((ChoiceCaseBuilder) old, newParent, updateQName);
75 throw new YangParseException(old.getModuleName(), old.getLine(),
76 "Failed to copy node: Unknown type of DataSchemaNode: " + old);
80 private static AnyXmlBuilder copy(final AnyXmlBuilder old, final Builder newParent, final boolean updateQName) {
81 DataBean data = getdata(old, newParent, updateQName);
82 QName newQName = data.qname;
83 SchemaPath newSchemaPath = data.schemaPath;
85 AnyXmlBuilder copy = new AnyXmlBuilder(newParent.getModuleName(), newParent.getLine(), newQName, newSchemaPath);
86 copyConstraints(copy.getConstraints(), old.getConstraints());
87 copy.setParent(newParent);
88 copy.setDescription(old.getDescription());
89 copy.setReference(old.getReference());
90 copy.setStatus(old.getStatus());
91 copy.setAugmenting(old.isAugmenting());
92 copy.setAddedByUses(old.isAddedByUses());
93 copy.setConfiguration(old.isConfiguration());
94 for (UnknownSchemaNodeBuilder un : old.getUnknownNodes()) {
95 copy.addUnknownNodeBuilder((copy(un, copy, updateQName)));
101 private static ChoiceBuilder copy(final ChoiceBuilder old, final Builder newParent, final boolean updateQName) {
102 DataBean data = getdata(old, newParent, updateQName);
103 QName newQName = data.qname;
104 SchemaPath newSchemaPath = data.schemaPath;
106 ChoiceBuilder copy = new ChoiceBuilder(newParent.getModuleName(), newParent.getLine(), newQName, newSchemaPath);
107 copyConstraints(copy.getConstraints(), old.getConstraints());
108 copy.setParent(newParent);
109 copy.setDescription(old.getDescription());
110 copy.setReference(old.getReference());
111 copy.setStatus(old.getStatus());
112 copy.setAugmenting(old.isAugmenting());
113 copy.setAddedByUses(old.isAddedByUses());
114 copy.setConfiguration(old.isConfiguration());
115 for (ChoiceCaseBuilder childNode : old.getCases()) {
116 copy.addCase(copy(childNode, copy, updateQName));
118 for (AugmentationSchemaBuilder augment : old.getAugmentationBuilders()) {
119 copy.addAugmentation(copyAugment(augment, copy));
121 for (UnknownSchemaNodeBuilder un : old.getUnknownNodes()) {
122 copy.addUnknownNodeBuilder((copy(un, copy, updateQName)));
128 private static ChoiceCaseBuilder copy(final ChoiceCaseBuilder old, final Builder newParent, final boolean updateQName) {
129 DataBean data = getdata(old, newParent, updateQName);
130 QName newQName = data.qname;
131 SchemaPath newSchemaPath = data.schemaPath;
133 ChoiceCaseBuilder copy = new ChoiceCaseBuilder(newParent.getModuleName(), newParent.getLine(), newQName, newSchemaPath);
134 copyConstraints(copy.getConstraints(), old.getConstraints());
135 copy.setParent(newParent);
136 copy.setDescription(old.getDescription());
137 copy.setReference(old.getReference());
138 copy.setStatus(old.getStatus());
139 copy.setAugmenting(old.isAugmenting());
140 for (DataSchemaNodeBuilder childNode : old.getChildNodeBuilders()) {
141 copy.addChildNode(copy(childNode, copy, updateQName));
143 copy.getGroupings().addAll(old.getGroupings());
144 for (GroupingBuilder grouping : old.getGroupingBuilders()) {
145 copy.addGrouping(copy(grouping, copy, updateQName));
147 for (TypeDefinitionBuilder tdb : old.getTypeDefinitionBuilders()) {
148 copy.addTypedef(copy(tdb, copy, updateQName));
150 for (UsesNodeBuilder oldUses : old.getUsesNodeBuilders()) {
151 copy.addUsesNode(copyUses(oldUses, copy));
153 for (UnknownSchemaNodeBuilder un : old.getUnknownNodes()) {
154 copy.addUnknownNodeBuilder((copy(un, copy, updateQName)));
160 private static ContainerSchemaNodeBuilder copy(final ContainerSchemaNodeBuilder old, final Builder newParent,
161 final boolean updateQName) {
162 DataBean data = getdata(old, newParent, updateQName);
163 QName newQName = data.qname;
164 SchemaPath newSchemaPath = data.schemaPath;
166 ContainerSchemaNodeBuilder copy = new ContainerSchemaNodeBuilder(newParent.getModuleName(),
167 newParent.getLine(), newQName, newSchemaPath);
168 copyConstraints(copy.getConstraints(), old.getConstraints());
169 copy.setParent(newParent);
170 copy.setDescription(old.getDescription());
171 copy.setReference(old.getReference());
172 copy.setStatus(old.getStatus());
173 copy.setPresence(old.isPresence());
174 copy.setAugmenting(old.isAugmenting());
175 copy.setAddedByUses(old.isAddedByUses());
176 copy.setConfiguration(old.isConfiguration());
177 for (DataSchemaNodeBuilder childNode : old.getChildNodeBuilders()) {
178 copy.addChildNode(copy(childNode, copy, updateQName));
180 copy.getGroupings().addAll(old.getGroupings());
181 for (GroupingBuilder grouping : old.getGroupingBuilders()) {
182 copy.addGrouping(copy(grouping, copy, updateQName));
184 for (TypeDefinitionBuilder tdb : old.getTypeDefinitionBuilders()) {
185 copy.addTypedef(copy(tdb, copy, updateQName));
187 for (UsesNodeBuilder oldUses : old.getUsesNodeBuilders()) {
188 copy.addUsesNode(copyUses(oldUses, copy));
190 for (AugmentationSchemaBuilder augment : old.getAugmentationBuilders()) {
191 copy.addAugmentation(copyAugment(augment, copy));
193 for (UnknownSchemaNodeBuilder un : old.getUnknownNodes()) {
194 copy.addUnknownNodeBuilder((copy(un, copy, updateQName)));
200 private static LeafSchemaNodeBuilder copy(final LeafSchemaNodeBuilder old, final Builder newParent, final boolean updateQName) {
201 DataBean data = getdata(old, newParent, updateQName);
202 QName newQName = data.qname;
203 SchemaPath newSchemaPath = data.schemaPath;
205 LeafSchemaNodeBuilder copy = new LeafSchemaNodeBuilder(newParent.getModuleName(), newParent.getLine(),
206 newQName, newSchemaPath);
207 copyConstraints(copy.getConstraints(), old.getConstraints());
208 copy.setParent(newParent);
209 copy.setDescription(old.getDescription());
210 copy.setReference(old.getReference());
211 copy.setStatus(old.getStatus());
212 copy.setAugmenting(old.isAugmenting());
213 copy.setAddedByUses(old.isAddedByUses());
214 copy.setConfiguration(old.isConfiguration());
215 for (UnknownSchemaNodeBuilder un : old.getUnknownNodes()) {
216 copy.addUnknownNodeBuilder((copy(un, copy, updateQName)));
219 if (old.getType() == null) {
220 copy.setTypedef(copy(old.getTypedef(), copy, updateQName));
222 copy.setType(old.getType());
225 copy.setDefaultStr(old.getDefaultStr());
226 copy.setUnits(old.getUnits());
231 public static LeafListSchemaNodeBuilder copy(final LeafListSchemaNodeBuilder old, final Builder newParent, final boolean updateQName) {
232 DataBean data = getdata(old, newParent, updateQName);
233 QName newQName = data.qname;
234 SchemaPath newSchemaPath = data.schemaPath;
236 LeafListSchemaNodeBuilder copy = new LeafListSchemaNodeBuilder(newParent.getModuleName(), newParent.getLine(),
237 newQName, newSchemaPath);
238 copyConstraints(copy.getConstraints(), old.getConstraints());
239 copy.setParent(newParent);
240 copy.setDescription(old.getDescription());
241 copy.setReference(old.getReference());
242 copy.setStatus(old.getStatus());
243 copy.setAugmenting(old.isAugmenting());
244 copy.setAddedByUses(old.isAddedByUses());
245 copy.setConfiguration(old.isConfiguration());
246 for (UnknownSchemaNodeBuilder un : old.getUnknownNodes()) {
247 copy.addUnknownNodeBuilder((copy(un, copy, updateQName)));
250 if (old.getType() == null) {
251 copy.setTypedef(copy(old.getTypedef(), copy, updateQName));
253 copy.setType(old.getType());
256 copy.setUserOrdered(old.isUserOrdered());
261 private static ListSchemaNodeBuilder copy(final ListSchemaNodeBuilder old, final Builder newParent, final boolean updateQName) {
262 DataBean data = getdata(old, newParent, updateQName);
263 QName newQName = data.qname;
264 SchemaPath newSchemaPath = data.schemaPath;
266 ListSchemaNodeBuilder copy = new ListSchemaNodeBuilder(newParent.getModuleName(), newParent.getLine(),
267 newQName, newSchemaPath);
268 copyConstraints(copy.getConstraints(), old.getConstraints());
269 copy.setParent(newParent);
270 copy.setDescription(old.getDescription());
271 copy.setReference(old.getReference());
272 copy.setStatus(old.getStatus());
273 copy.setAugmenting(old.isAugmenting());
274 copy.setAddedByUses(old.isAddedByUses());
275 copy.setConfiguration(old.isConfiguration());
276 for (DataSchemaNodeBuilder childNode : old.getChildNodeBuilders()) {
277 copy.addChildNode(copy(childNode, copy, updateQName));
279 copy.getGroupings().addAll(old.getGroupings());
280 for (GroupingBuilder grouping : old.getGroupingBuilders()) {
281 copy.addGrouping(copy(grouping, copy, updateQName));
283 for (TypeDefinitionBuilder tdb : old.getTypeDefinitionBuilders()) {
284 copy.addTypedef(copy(tdb, copy, updateQName));
286 for (UsesNodeBuilder oldUses : old.getUsesNodeBuilders()) {
287 copy.addUsesNode(copyUses(oldUses, copy));
289 for (AugmentationSchemaBuilder augment : old.getAugmentationBuilders()) {
290 copy.addAugmentation(copyAugment(augment, copy));
292 for (UnknownSchemaNodeBuilder un : old.getUnknownNodes()) {
293 copy.addUnknownNodeBuilder((copy(un, copy, updateQName)));
296 copy.setUserOrdered(old.isUserOrdered());
297 copy.setKeys(old.getKeys());
302 public static GroupingBuilder copy(final GroupingBuilder old, final Builder newParent, final boolean updateQName) {
303 DataBean data = getdata(old, newParent, updateQName);
304 QName newQName = data.qname;
305 SchemaPath newSchemaPath = data.schemaPath;
307 GroupingBuilderImpl copy = new GroupingBuilderImpl(newParent.getModuleName(), newParent.getLine(), newQName, newSchemaPath);
308 copy.setParent(newParent);
309 copy.setDescription(old.getDescription());
310 copy.setReference(old.getReference());
311 copy.setStatus(old.getStatus());
312 copy.setAddedByUses(old.isAddedByUses());
313 for (DataSchemaNodeBuilder childNode : old.getChildNodeBuilders()) {
314 copy.addChildNode(copy(childNode, copy, updateQName));
316 copy.getGroupings().addAll(old.getGroupings());
317 for (GroupingBuilder grouping : old.getGroupingBuilders()) {
318 copy.addGrouping(copy(grouping, copy, updateQName));
320 for (TypeDefinitionBuilder tdb : old.getTypeDefinitionBuilders()) {
321 copy.addTypedef(copy(tdb, copy, updateQName));
323 for (UsesNodeBuilder oldUses : old.getUsesNodeBuilders()) {
324 copy.addUsesNode(copyUses(oldUses, copy));
326 for (UnknownSchemaNodeBuilder un : old.getUnknownNodes()) {
327 copy.addUnknownNodeBuilder((copy(un, copy, updateQName)));
333 public static TypeDefinitionBuilder copy(final TypeDefinitionBuilder old, final Builder newParent, final boolean updateQName) {
334 DataBean data = getdata(old, newParent, updateQName);
335 QName newQName = data.qname;
336 SchemaPath newSchemaPath = data.schemaPath;
337 TypeDefinitionBuilder type;
339 if (old instanceof UnionTypeBuilder) {
340 UnionTypeBuilder oldUnion = (UnionTypeBuilder) old;
341 type = new UnionTypeBuilder(newParent.getModuleName(), newParent.getLine());
342 type.setParent(newParent);
343 for (TypeDefinition<?> td : oldUnion.getTypes()) {
346 for (TypeDefinitionBuilder tdb : oldUnion.getTypedefs()) {
347 type.setTypedef(copy(tdb, type, updateQName));
349 } else if (old instanceof IdentityrefTypeBuilder) {
350 type = new IdentityrefTypeBuilder(newParent.getModuleName(), newParent.getLine(),
351 ((IdentityrefTypeBuilder) old).getBaseString(), newSchemaPath);
352 type.setParent(newParent);
354 type = new TypeDefinitionBuilderImpl(old.getModuleName(), newParent.getLine(), newQName, old.getPath());
355 type.setParent(newParent);
357 if (old.getType() == null) {
358 type.setTypedef(copy(old.getTypedef(), type, updateQName));
360 type.setType(old.getType());
363 for (UnknownSchemaNodeBuilder un : old.getUnknownNodes()) {
364 type.addUnknownNodeBuilder((copy(un, type, updateQName)));
367 type.setRanges(old.getRanges());
368 type.setLengths(old.getLengths());
369 type.setPatterns(old.getPatterns());
370 type.setFractionDigits(old.getFractionDigits());
371 type.setDescription(old.getDescription());
372 type.setReference(old.getReference());
373 type.setStatus(old.getStatus());
374 type.setUnits(old.getUnits());
375 type.setDefaultValue(old.getDefaultValue());
376 type.setAddedByUses(old.isAddedByUses());
382 private static ConstraintsBuilder copyConstraints(final ConstraintsBuilder newConstraints, final ConstraintsBuilder old) {
383 newConstraints.getMustDefinitions().addAll(old.getMustDefinitions());
384 newConstraints.addWhenCondition(old.getWhenCondition());
385 newConstraints.setMandatory(old.isMandatory());
386 newConstraints.setMinElements(old.getMinElements());
387 newConstraints.setMaxElements(old.getMaxElements());
388 return newConstraints;
391 private static UsesNodeBuilder copyUses(final UsesNodeBuilder old, final Builder newParent) {
392 UsesNodeBuilder copy = new UsesNodeBuilderImpl(newParent.getModuleName(), newParent.getLine(),
393 old.getGroupingPathAsString());
394 copy.setParent(newParent);
395 copy.setGroupingDefinition(old.getGroupingDefinition());
396 copy.setGrouping(old.getGroupingBuilder());
397 copy.setAddedByUses(old.isAddedByUses());
398 copy.getAugmentations().addAll(old.getAugmentations());
399 copy.getRefineNodes().addAll(old.getRefineNodes());
400 copy.getRefines().addAll(old.getRefines());
401 copy.setAugmenting(old.isAugmenting());
405 private static AugmentationSchemaBuilder copyAugment(final AugmentationSchemaBuilder old, final Builder newParent) {
406 AugmentationSchemaBuilderImpl copy = new AugmentationSchemaBuilderImpl(newParent.getModuleName(),
407 newParent.getLine(), old.getTargetPathAsString());
408 copy.setParent(newParent);
410 copy.setDescription(old.getDescription());
411 copy.setReference(old.getReference());
412 copy.setStatus(old.getStatus());
413 copy.addWhenCondition(old.getWhenCondition());
414 copy.setTargetNodeSchemaPath(old.getTargetNodeSchemaPath());
415 for (DataSchemaNodeBuilder childNode : old.getChildNodeBuilders()) {
416 copy.addChildNode(copy(childNode, copy, false));
418 for (UsesNodeBuilder oldUses : old.getUsesNodeBuilders()) {
419 copy.addUsesNode(copyUses(oldUses, copy));
421 for (UnknownSchemaNodeBuilder un : old.getUnknownNodes()) {
422 copy.addUnknownNodeBuilder((copy(un, copy, false)));
428 public static UnknownSchemaNodeBuilder copy(final UnknownSchemaNodeBuilder old, final Builder newParent, final boolean updateQName) {
429 DataBean data = getdata(old, newParent, updateQName);
430 QName newQName = data.qname;
431 SchemaPath newSchemaPath = data.schemaPath;
433 UnknownSchemaNodeBuilder c = new UnknownSchemaNodeBuilder(newParent.getModuleName(), newParent.getLine(),
434 newQName, newSchemaPath);
436 c.setNodeType(old.getNodeType());
437 c.setNodeParameter(old.getNodeParameter());
438 c.setParent(newParent);
439 c.setDescription(old.getDescription());
440 c.setReference(old.getReference());
441 c.setStatus(old.getStatus());
442 c.setAddedByUses(old.isAddedByUses());
443 for (UnknownSchemaNodeBuilder un : old.getUnknownNodes()) {
444 c.addUnknownNodeBuilder((copy(un, c, updateQName)));
446 c.setExtensionBuilder(old.getExtensionBuilder());
447 c.setExtensionDefinition(old.getExtensionDefinition());
452 private static DataBean getdata(final SchemaNodeBuilder old, final Builder newParent, final boolean updateQName) {
453 List<QName> newPath = null;
454 QName newQName = null;
455 if (newParent instanceof ModuleBuilder) {
456 ModuleBuilder parent = (ModuleBuilder) newParent;
458 newQName = new QName(parent.getNamespace(), parent.getRevision(), parent.getPrefix(), old.getQName()
460 newPath = Collections.singletonList(newQName);
462 newQName = old.getQName();
463 newPath = Collections.singletonList(newQName);
465 } else if (newParent instanceof AugmentationSchemaBuilder) {
466 AugmentationSchemaBuilder augment = (AugmentationSchemaBuilder) newParent;
467 ModuleBuilder parent = ParserUtils.getParentModule(newParent);
469 newQName = new QName(parent.getNamespace(), parent.getRevision(), parent.getPrefix(), old.getQName()
471 newPath = new ArrayList<>(augment.getTargetNodeSchemaPath().getPath());
472 newPath.add(newQName);
474 newQName = old.getQName();
475 newPath = new ArrayList<>(augment.getTargetNodeSchemaPath().getPath());
476 newPath.add(newQName);
479 } else if (newParent instanceof SchemaNodeBuilder) {
480 SchemaNodeBuilder parent = (SchemaNodeBuilder) newParent;
481 QName parentQName = parent.getQName();
483 newQName = new QName(parentQName.getNamespace(), parentQName.getRevision(), parentQName.getPrefix(),
484 old.getQName().getLocalName());
485 newPath = new ArrayList<>(parent.getPath().getPath());
486 newPath.add(newQName);
488 newQName = old.getQName();
489 newPath = new ArrayList<>(parent.getPath().getPath());
490 newPath.add(newQName);
494 SchemaPath newSchemaPath = SchemaPath.create(newPath, true);
495 return new DataBean(newQName, newSchemaPath);
498 private static final class DataBean {
499 private final QName qname;
500 private final SchemaPath schemaPath;
502 private DataBean(final QName qname, final SchemaPath schemaPath) {
504 this.schemaPath = schemaPath;