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;
13 import org.opendaylight.yangtools.yang.common.QName;
14 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
15 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
16 import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationSchemaBuilder;
17 import org.opendaylight.yangtools.yang.parser.builder.api.Builder;
18 import org.opendaylight.yangtools.yang.parser.builder.api.DataSchemaNodeBuilder;
19 import org.opendaylight.yangtools.yang.parser.builder.api.GroupingBuilder;
20 import org.opendaylight.yangtools.yang.parser.builder.api.SchemaNodeBuilder;
21 import org.opendaylight.yangtools.yang.parser.builder.api.TypeDefinitionBuilder;
22 import org.opendaylight.yangtools.yang.parser.builder.api.UsesNodeBuilder;
23 import org.opendaylight.yangtools.yang.parser.builder.impl.AnyXmlBuilder;
24 import org.opendaylight.yangtools.yang.parser.builder.impl.AugmentationSchemaBuilderImpl;
25 import org.opendaylight.yangtools.yang.parser.builder.impl.ChoiceBuilder;
26 import org.opendaylight.yangtools.yang.parser.builder.impl.ChoiceCaseBuilder;
27 import org.opendaylight.yangtools.yang.parser.builder.impl.ConstraintsBuilder;
28 import org.opendaylight.yangtools.yang.parser.builder.impl.ContainerSchemaNodeBuilder;
29 import org.opendaylight.yangtools.yang.parser.builder.impl.GroupingBuilderImpl;
30 import org.opendaylight.yangtools.yang.parser.builder.impl.IdentityrefTypeBuilder;
31 import org.opendaylight.yangtools.yang.parser.builder.impl.LeafListSchemaNodeBuilder;
32 import org.opendaylight.yangtools.yang.parser.builder.impl.LeafSchemaNodeBuilder;
33 import org.opendaylight.yangtools.yang.parser.builder.impl.ListSchemaNodeBuilder;
34 import org.opendaylight.yangtools.yang.parser.builder.impl.ModuleBuilder;
35 import org.opendaylight.yangtools.yang.parser.builder.impl.TypeDefinitionBuilderImpl;
36 import org.opendaylight.yangtools.yang.parser.builder.impl.UnionTypeBuilder;
37 import org.opendaylight.yangtools.yang.parser.builder.impl.UnknownSchemaNodeBuilder;
38 import org.opendaylight.yangtools.yang.parser.builder.impl.UsesNodeBuilderImpl;
40 public final class CopyUtils {
46 * Create copy of DataSchemaNodeBuilder with new parent. If updateQName is
47 * true, qname of node will be corrected based on new parent.
54 * flag to indicate if qname should be updated based on new
56 * @return copy of given builder
58 public static DataSchemaNodeBuilder copy(final DataSchemaNodeBuilder old, final Builder newParent, final boolean updateQName) {
59 if (old instanceof AnyXmlBuilder) {
60 return copy((AnyXmlBuilder) old, newParent, updateQName);
61 } else if (old instanceof ChoiceBuilder) {
62 return copy((ChoiceBuilder) old, newParent, updateQName);
63 } else if (old instanceof ContainerSchemaNodeBuilder) {
64 return copy((ContainerSchemaNodeBuilder) old, newParent, updateQName);
65 } else if (old instanceof LeafSchemaNodeBuilder) {
66 return copy((LeafSchemaNodeBuilder) old, newParent, updateQName);
67 } else if (old instanceof LeafListSchemaNodeBuilder) {
68 return copy((LeafListSchemaNodeBuilder) old, newParent, updateQName);
69 } else if (old instanceof ListSchemaNodeBuilder) {
70 return copy((ListSchemaNodeBuilder) old, newParent, updateQName);
71 } else if (old instanceof ChoiceCaseBuilder) {
72 return copy((ChoiceCaseBuilder) old, newParent, updateQName);
74 throw new YangParseException(old.getModuleName(), old.getLine(),
75 "Failed to copy node: Unknown type of DataSchemaNode: " + old);
79 private static AnyXmlBuilder copy(final AnyXmlBuilder old, final Builder newParent, final boolean updateQName) {
80 DataBean data = getdata(old, newParent, updateQName);
81 QName newQName = data.qname;
82 SchemaPath newSchemaPath = data.schemaPath;
84 AnyXmlBuilder copy = new AnyXmlBuilder(newParent.getModuleName(), newParent.getLine(), newQName, newSchemaPath);
85 copyConstraints(copy.getConstraints(), old.getConstraints());
86 copy.setParent(newParent);
87 copy.setDescription(old.getDescription());
88 copy.setReference(old.getReference());
89 copy.setStatus(old.getStatus());
90 copy.setAugmenting(old.isAugmenting());
91 copy.setAddedByUses(old.isAddedByUses());
92 copy.setConfiguration(old.isConfiguration());
93 for (UnknownSchemaNodeBuilder un : old.getUnknownNodes()) {
94 copy.addUnknownNodeBuilder((copy(un, copy, updateQName)));
100 private static ChoiceBuilder copy(final ChoiceBuilder old, final Builder newParent, final boolean updateQName) {
101 DataBean data = getdata(old, newParent, updateQName);
102 QName newQName = data.qname;
103 SchemaPath newSchemaPath = data.schemaPath;
105 ChoiceBuilder copy = new ChoiceBuilder(newParent.getModuleName(), newParent.getLine(), newQName, newSchemaPath);
106 copyConstraints(copy.getConstraints(), old.getConstraints());
107 copy.setParent(newParent);
108 copy.setDescription(old.getDescription());
109 copy.setReference(old.getReference());
110 copy.setStatus(old.getStatus());
111 copy.setAugmenting(old.isAugmenting());
112 copy.setAddedByUses(old.isAddedByUses());
113 copy.setConfiguration(old.isConfiguration());
114 for (ChoiceCaseBuilder childNode : old.getCases()) {
115 copy.addCase(copy(childNode, copy, updateQName));
117 for (AugmentationSchemaBuilder augment : old.getAugmentationBuilders()) {
118 copy.addAugmentation(copyAugment(augment, copy));
120 for (UnknownSchemaNodeBuilder un : old.getUnknownNodes()) {
121 copy.addUnknownNodeBuilder((copy(un, copy, updateQName)));
127 private static ChoiceCaseBuilder copy(final ChoiceCaseBuilder old, final Builder newParent, final boolean updateQName) {
128 DataBean data = getdata(old, newParent, updateQName);
129 QName newQName = data.qname;
130 SchemaPath newSchemaPath = data.schemaPath;
132 ChoiceCaseBuilder copy = new ChoiceCaseBuilder(newParent.getModuleName(), newParent.getLine(), newQName, newSchemaPath);
133 copyConstraints(copy.getConstraints(), old.getConstraints());
134 copy.setParent(newParent);
135 copy.setDescription(old.getDescription());
136 copy.setReference(old.getReference());
137 copy.setStatus(old.getStatus());
138 copy.setAugmenting(old.isAugmenting());
139 for (DataSchemaNodeBuilder childNode : old.getChildNodeBuilders()) {
140 copy.addChildNode(copy(childNode, copy, updateQName));
142 copy.getGroupings().addAll(old.getGroupings());
143 for (GroupingBuilder grouping : old.getGroupingBuilders()) {
144 copy.addGrouping(copy(grouping, copy, updateQName));
146 for (TypeDefinitionBuilder tdb : old.getTypeDefinitionBuilders()) {
147 copy.addTypedef(copy(tdb, copy, updateQName));
149 for (UsesNodeBuilder oldUses : old.getUsesNodeBuilders()) {
150 copy.addUsesNode(copyUses(oldUses, copy));
152 for (UnknownSchemaNodeBuilder un : old.getUnknownNodes()) {
153 copy.addUnknownNodeBuilder((copy(un, copy, updateQName)));
159 private static ContainerSchemaNodeBuilder copy(final ContainerSchemaNodeBuilder old, final Builder newParent,
160 final boolean updateQName) {
161 DataBean data = getdata(old, newParent, updateQName);
162 QName newQName = data.qname;
163 SchemaPath newSchemaPath = data.schemaPath;
165 ContainerSchemaNodeBuilder copy = new ContainerSchemaNodeBuilder(newParent.getModuleName(),
166 newParent.getLine(), newQName, newSchemaPath);
167 copyConstraints(copy.getConstraints(), old.getConstraints());
168 copy.setParent(newParent);
169 copy.setDescription(old.getDescription());
170 copy.setReference(old.getReference());
171 copy.setStatus(old.getStatus());
172 copy.setPresence(old.isPresence());
173 copy.setAugmenting(old.isAugmenting());
174 copy.setAddedByUses(old.isAddedByUses());
175 copy.setConfiguration(old.isConfiguration());
176 for (DataSchemaNodeBuilder childNode : old.getChildNodeBuilders()) {
177 copy.addChildNode(copy(childNode, copy, updateQName));
179 copy.getGroupings().addAll(old.getGroupings());
180 for (GroupingBuilder grouping : old.getGroupingBuilders()) {
181 copy.addGrouping(copy(grouping, copy, updateQName));
183 for (TypeDefinitionBuilder tdb : old.getTypeDefinitionBuilders()) {
184 copy.addTypedef(copy(tdb, copy, updateQName));
186 for (UsesNodeBuilder oldUses : old.getUsesNodeBuilders()) {
187 copy.addUsesNode(copyUses(oldUses, copy));
189 for (AugmentationSchemaBuilder augment : old.getAugmentationBuilders()) {
190 copy.addAugmentation(copyAugment(augment, copy));
192 for (UnknownSchemaNodeBuilder un : old.getUnknownNodes()) {
193 copy.addUnknownNodeBuilder((copy(un, copy, updateQName)));
199 private static LeafSchemaNodeBuilder copy(final LeafSchemaNodeBuilder old, final Builder newParent, final boolean updateQName) {
200 DataBean data = getdata(old, newParent, updateQName);
201 QName newQName = data.qname;
202 SchemaPath newSchemaPath = data.schemaPath;
204 LeafSchemaNodeBuilder copy = new LeafSchemaNodeBuilder(newParent.getModuleName(), newParent.getLine(),
205 newQName, newSchemaPath);
206 copyConstraints(copy.getConstraints(), old.getConstraints());
207 copy.setParent(newParent);
208 copy.setDescription(old.getDescription());
209 copy.setReference(old.getReference());
210 copy.setStatus(old.getStatus());
211 copy.setAugmenting(old.isAugmenting());
212 copy.setAddedByUses(old.isAddedByUses());
213 copy.setConfiguration(old.isConfiguration());
214 for (UnknownSchemaNodeBuilder un : old.getUnknownNodes()) {
215 copy.addUnknownNodeBuilder((copy(un, copy, updateQName)));
218 if (old.getType() == null) {
219 copy.setTypedef(copy(old.getTypedef(), copy, updateQName));
221 copy.setType(old.getType());
224 copy.setDefaultStr(old.getDefaultStr());
225 copy.setUnits(old.getUnits());
230 public static LeafListSchemaNodeBuilder copy(final LeafListSchemaNodeBuilder old, final Builder newParent, final boolean updateQName) {
231 DataBean data = getdata(old, newParent, updateQName);
232 QName newQName = data.qname;
233 SchemaPath newSchemaPath = data.schemaPath;
235 LeafListSchemaNodeBuilder copy = new LeafListSchemaNodeBuilder(newParent.getModuleName(), newParent.getLine(),
236 newQName, newSchemaPath);
237 copyConstraints(copy.getConstraints(), old.getConstraints());
238 copy.setParent(newParent);
239 copy.setDescription(old.getDescription());
240 copy.setReference(old.getReference());
241 copy.setStatus(old.getStatus());
242 copy.setAugmenting(old.isAugmenting());
243 copy.setAddedByUses(old.isAddedByUses());
244 copy.setConfiguration(old.isConfiguration());
245 for (UnknownSchemaNodeBuilder un : old.getUnknownNodes()) {
246 copy.addUnknownNodeBuilder((copy(un, copy, updateQName)));
249 if (old.getType() == null) {
250 copy.setTypedef(copy(old.getTypedef(), copy, updateQName));
252 copy.setType(old.getType());
255 copy.setUserOrdered(old.isUserOrdered());
260 private static ListSchemaNodeBuilder copy(final ListSchemaNodeBuilder old, final Builder newParent, final boolean updateQName) {
261 DataBean data = getdata(old, newParent, updateQName);
262 QName newQName = data.qname;
263 SchemaPath newSchemaPath = data.schemaPath;
265 ListSchemaNodeBuilder copy = new ListSchemaNodeBuilder(newParent.getModuleName(), newParent.getLine(),
266 newQName, newSchemaPath);
267 copyConstraints(copy.getConstraints(), old.getConstraints());
268 copy.setParent(newParent);
269 copy.setDescription(old.getDescription());
270 copy.setReference(old.getReference());
271 copy.setStatus(old.getStatus());
272 copy.setAugmenting(old.isAugmenting());
273 copy.setAddedByUses(old.isAddedByUses());
274 copy.setConfiguration(old.isConfiguration());
275 for (DataSchemaNodeBuilder childNode : old.getChildNodeBuilders()) {
276 copy.addChildNode(copy(childNode, copy, updateQName));
278 copy.getGroupings().addAll(old.getGroupings());
279 for (GroupingBuilder grouping : old.getGroupingBuilders()) {
280 copy.addGrouping(copy(grouping, copy, updateQName));
282 for (TypeDefinitionBuilder tdb : old.getTypeDefinitionBuilders()) {
283 copy.addTypedef(copy(tdb, copy, updateQName));
285 for (UsesNodeBuilder oldUses : old.getUsesNodeBuilders()) {
286 copy.addUsesNode(copyUses(oldUses, copy));
288 for (AugmentationSchemaBuilder augment : old.getAugmentationBuilders()) {
289 copy.addAugmentation(copyAugment(augment, copy));
291 for (UnknownSchemaNodeBuilder un : old.getUnknownNodes()) {
292 copy.addUnknownNodeBuilder((copy(un, copy, updateQName)));
295 copy.setUserOrdered(old.isUserOrdered());
296 copy.setKeys(old.getKeys());
301 public static GroupingBuilder copy(final GroupingBuilder old, final Builder newParent, final boolean updateQName) {
302 DataBean data = getdata(old, newParent, updateQName);
303 QName newQName = data.qname;
304 SchemaPath newSchemaPath = data.schemaPath;
306 GroupingBuilderImpl copy = new GroupingBuilderImpl(newParent.getModuleName(), newParent.getLine(), newQName, newSchemaPath);
307 copy.setParent(newParent);
308 copy.setDescription(old.getDescription());
309 copy.setReference(old.getReference());
310 copy.setStatus(old.getStatus());
311 copy.setAddedByUses(old.isAddedByUses());
312 for (DataSchemaNodeBuilder childNode : old.getChildNodeBuilders()) {
313 copy.addChildNode(copy(childNode, copy, updateQName));
315 copy.getGroupings().addAll(old.getGroupings());
316 for (GroupingBuilder grouping : old.getGroupingBuilders()) {
317 copy.addGrouping(copy(grouping, copy, updateQName));
319 for (TypeDefinitionBuilder tdb : old.getTypeDefinitionBuilders()) {
320 copy.addTypedef(copy(tdb, copy, updateQName));
322 for (UsesNodeBuilder oldUses : old.getUsesNodeBuilders()) {
323 copy.addUsesNode(copyUses(oldUses, copy));
325 for (UnknownSchemaNodeBuilder un : old.getUnknownNodes()) {
326 copy.addUnknownNodeBuilder((copy(un, copy, updateQName)));
332 public static TypeDefinitionBuilder copy(final TypeDefinitionBuilder old, final Builder newParent, final boolean updateQName) {
333 DataBean data = getdata(old, newParent, updateQName);
334 QName newQName = data.qname;
335 SchemaPath newSchemaPath = data.schemaPath;
336 TypeDefinitionBuilder type;
338 if (old instanceof UnionTypeBuilder) {
339 UnionTypeBuilder oldUnion = (UnionTypeBuilder) old;
340 type = new UnionTypeBuilder(newParent.getModuleName(), newParent.getLine());
341 type.setParent(newParent);
342 for (TypeDefinition<?> td : oldUnion.getTypes()) {
345 for (TypeDefinitionBuilder tdb : oldUnion.getTypedefs()) {
346 type.setTypedef(copy(tdb, type, updateQName));
348 } else if (old instanceof IdentityrefTypeBuilder) {
349 type = new IdentityrefTypeBuilder(newParent.getModuleName(), newParent.getLine(),
350 ((IdentityrefTypeBuilder) old).getBaseString(), newSchemaPath);
351 type.setParent(newParent);
353 type = new TypeDefinitionBuilderImpl(old.getModuleName(), newParent.getLine(), newQName, old.getPath());
354 type.setParent(newParent);
356 if (old.getType() == null) {
357 type.setTypedef(copy(old.getTypedef(), type, updateQName));
359 type.setType(old.getType());
362 for (UnknownSchemaNodeBuilder un : old.getUnknownNodes()) {
363 type.addUnknownNodeBuilder((copy(un, type, updateQName)));
366 type.setRanges(old.getRanges());
367 type.setLengths(old.getLengths());
368 type.setPatterns(old.getPatterns());
369 type.setFractionDigits(old.getFractionDigits());
370 type.setDescription(old.getDescription());
371 type.setReference(old.getReference());
372 type.setStatus(old.getStatus());
373 type.setUnits(old.getUnits());
374 type.setDefaultValue(old.getDefaultValue());
375 type.setAddedByUses(old.isAddedByUses());
381 private static ConstraintsBuilder copyConstraints(final ConstraintsBuilder newConstraints, final ConstraintsBuilder old) {
382 newConstraints.getMustDefinitions().addAll(old.getMustDefinitions());
383 newConstraints.addWhenCondition(old.getWhenCondition());
384 newConstraints.setMandatory(old.isMandatory());
385 newConstraints.setMinElements(old.getMinElements());
386 newConstraints.setMaxElements(old.getMaxElements());
387 return newConstraints;
390 private static UsesNodeBuilder copyUses(final UsesNodeBuilder old, final Builder newParent) {
391 UsesNodeBuilder copy = new UsesNodeBuilderImpl(newParent.getModuleName(), newParent.getLine(),
392 old.getGroupingPathAsString());
393 copy.setParent(newParent);
394 copy.setGroupingDefinition(old.getGroupingDefinition());
395 copy.setGrouping(old.getGroupingBuilder());
396 copy.setAddedByUses(old.isAddedByUses());
397 copy.getAugmentations().addAll(old.getAugmentations());
398 copy.getRefineNodes().addAll(old.getRefineNodes());
399 copy.getRefines().addAll(old.getRefines());
400 copy.setAugmenting(old.isAugmenting());
404 private static AugmentationSchemaBuilder copyAugment(final AugmentationSchemaBuilder old, final Builder newParent) {
405 AugmentationSchemaBuilderImpl copy = new AugmentationSchemaBuilderImpl(newParent.getModuleName(),
406 newParent.getLine(), old.getTargetPathAsString(), old.getOrder());
407 copy.setParent(newParent);
409 copy.setDescription(old.getDescription());
410 copy.setReference(old.getReference());
411 copy.setStatus(old.getStatus());
412 copy.addWhenCondition(old.getWhenCondition());
413 copy.setTargetNodeSchemaPath(old.getTargetNodeSchemaPath());
414 for (DataSchemaNodeBuilder childNode : old.getChildNodeBuilders()) {
415 copy.addChildNode(copy(childNode, copy, false));
417 for (UsesNodeBuilder oldUses : old.getUsesNodeBuilders()) {
418 copy.addUsesNode(copyUses(oldUses, copy));
420 for (UnknownSchemaNodeBuilder un : old.getUnknownNodes()) {
421 copy.addUnknownNodeBuilder((copy(un, copy, false)));
427 public static UnknownSchemaNodeBuilder copy(final UnknownSchemaNodeBuilder old, final Builder newParent, final boolean updateQName) {
428 DataBean data = getdata(old, newParent, updateQName);
429 QName newQName = data.qname;
430 SchemaPath newSchemaPath = data.schemaPath;
432 UnknownSchemaNodeBuilder c = new UnknownSchemaNodeBuilder(newParent.getModuleName(), newParent.getLine(),
433 newQName, newSchemaPath);
435 c.setNodeType(old.getNodeType());
436 c.setNodeParameter(old.getNodeParameter());
437 c.setParent(newParent);
438 c.setDescription(old.getDescription());
439 c.setReference(old.getReference());
440 c.setStatus(old.getStatus());
441 c.setAddedByUses(old.isAddedByUses());
442 for (UnknownSchemaNodeBuilder un : old.getUnknownNodes()) {
443 c.addUnknownNodeBuilder((copy(un, c, updateQName)));
445 c.setExtensionBuilder(old.getExtensionBuilder());
446 c.setExtensionDefinition(old.getExtensionDefinition());
451 private static DataBean getdata(final SchemaNodeBuilder old, final Builder newParent, final boolean updateQName) {
452 List<QName> newPath = null;
453 QName newQName = null;
454 if (newParent instanceof ModuleBuilder) {
455 ModuleBuilder parent = (ModuleBuilder) newParent;
457 newQName = new QName(parent.getNamespace(), parent.getRevision(), parent.getPrefix(), old.getQName()
459 newPath = Collections.singletonList(newQName);
461 newQName = old.getQName();
462 newPath = Collections.singletonList(newQName);
464 } else if (newParent instanceof AugmentationSchemaBuilder) {
465 AugmentationSchemaBuilder augment = (AugmentationSchemaBuilder) newParent;
466 ModuleBuilder parent = ParserUtils.getParentModule(newParent);
468 newQName = new QName(parent.getNamespace(), parent.getRevision(), parent.getPrefix(), old.getQName()
470 newPath = new ArrayList<>(augment.getTargetNodeSchemaPath().getPath());
471 newPath.add(newQName);
473 newQName = old.getQName();
474 newPath = new ArrayList<>(augment.getTargetNodeSchemaPath().getPath());
475 newPath.add(newQName);
478 } else if (newParent instanceof SchemaNodeBuilder) {
479 SchemaNodeBuilder parent = (SchemaNodeBuilder) newParent;
480 QName parentQName = parent.getQName();
482 newQName = new QName(parentQName.getNamespace(), parentQName.getRevision(), parentQName.getPrefix(),
483 old.getQName().getLocalName());
484 newPath = new ArrayList<>(parent.getPath().getPath());
485 newPath.add(newQName);
487 newQName = old.getQName();
488 newPath = new ArrayList<>(parent.getPath().getPath());
489 newPath.add(newQName);
493 SchemaPath newSchemaPath = SchemaPath.create(newPath, true);
494 return new DataBean(newQName, newSchemaPath);
497 private static final class DataBean {
498 private final QName qname;
499 private final SchemaPath schemaPath;
501 private DataBean(final QName qname, final SchemaPath schemaPath) {
503 this.schemaPath = schemaPath;