Fix for bug 24.
[controller.git] / opendaylight / sal / yang-prototype / code-generator / yang-model-parser-impl / src / main / java / org / opendaylight / controller / yang / parser / util / ParserUtils.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.controller.yang.parser.util;
9
10 import java.util.ArrayList;
11 import java.util.List;
12
13 import org.opendaylight.controller.yang.common.QName;
14 import org.opendaylight.controller.yang.model.api.ModuleImport;
15 import org.opendaylight.controller.yang.model.api.MustDefinition;
16 import org.opendaylight.controller.yang.model.api.SchemaPath;
17 import org.opendaylight.controller.yang.model.api.TypeDefinition;
18 import org.opendaylight.controller.yang.parser.builder.api.AugmentationSchemaBuilder;
19 import org.opendaylight.controller.yang.parser.builder.api.ChildNodeBuilder;
20 import org.opendaylight.controller.yang.parser.builder.api.DataSchemaNodeBuilder;
21 import org.opendaylight.controller.yang.parser.builder.api.GroupingBuilder;
22 import org.opendaylight.controller.yang.parser.builder.api.TypeDefinitionBuilder;
23 import org.opendaylight.controller.yang.parser.builder.api.UsesNodeBuilder;
24 import org.opendaylight.controller.yang.parser.builder.impl.AnyXmlBuilder;
25 import org.opendaylight.controller.yang.parser.builder.impl.ChoiceBuilder;
26 import org.opendaylight.controller.yang.parser.builder.impl.ChoiceCaseBuilder;
27 import org.opendaylight.controller.yang.parser.builder.impl.ConstraintsBuilder;
28 import org.opendaylight.controller.yang.parser.builder.impl.ContainerSchemaNodeBuilder;
29 import org.opendaylight.controller.yang.parser.builder.impl.LeafListSchemaNodeBuilder;
30 import org.opendaylight.controller.yang.parser.builder.impl.LeafSchemaNodeBuilder;
31 import org.opendaylight.controller.yang.parser.builder.impl.ListSchemaNodeBuilder;
32 import org.opendaylight.controller.yang.parser.builder.impl.ModuleBuilder;
33 import org.opendaylight.controller.yang.parser.builder.impl.UnknownSchemaNodeBuilder;
34
35 public final class ParserUtils {
36
37     private ParserUtils() {
38     }
39
40     /**
41      * Get module import referenced by given prefix.
42      *
43      * @param builder
44      *            module to search
45      * @param prefix
46      *            prefix associated with import
47      * @return ModuleImport based on given prefix
48      */
49     public static ModuleImport getModuleImport(final ModuleBuilder builder,
50             final String prefix) {
51         ModuleImport moduleImport = null;
52         for (ModuleImport mi : builder.getModuleImports()) {
53             if (mi.getPrefix().equals(prefix)) {
54                 moduleImport = mi;
55                 break;
56             }
57         }
58         return moduleImport;
59     }
60
61     /**
62      * Parse uses path.
63      *
64      * @param usesPath
65      *            as String
66      * @return SchemaPath from given String
67      */
68     public static SchemaPath parseUsesPath(final String usesPath) {
69         final boolean absolute = usesPath.startsWith("/");
70         final String[] splittedPath = usesPath.split("/");
71         final List<QName> path = new ArrayList<QName>();
72         QName name;
73         for (String pathElement : splittedPath) {
74             if (pathElement.length() > 0) {
75                 final String[] splittedElement = pathElement.split(":");
76                 if (splittedElement.length == 1) {
77                     name = new QName(null, null, null, splittedElement[0]);
78                 } else {
79                     name = new QName(null, null, splittedElement[0],
80                             splittedElement[1]);
81                 }
82                 path.add(name);
83             }
84         }
85         return new SchemaPath(path, absolute);
86     }
87
88     /**
89      * Add all augment's child nodes to given target.
90      *
91      * @param augment
92      * @param target
93      */
94     public static void fillAugmentTarget(
95             final AugmentationSchemaBuilder augment,
96             final ChildNodeBuilder target) {
97         for (DataSchemaNodeBuilder builder : augment.getChildNodes()) {
98             builder.setAugmenting(true);
99             correctPath(augment, target.getPath());
100             target.addChildNode(builder);
101         }
102     }
103
104     private static void correctPath(final ChildNodeBuilder node,
105             final SchemaPath parentSchemaPath) {
106         for(DataSchemaNodeBuilder builder : node.getChildNodes()) {
107
108             // add correct path
109             SchemaPath targetNodeSchemaPath = parentSchemaPath;
110             List<QName> targetNodePath = new ArrayList<QName>(targetNodeSchemaPath.getPath());
111             targetNodePath.add(builder.getQName());
112             builder.setPath(new SchemaPath(targetNodePath, true));
113
114             if(builder instanceof ChildNodeBuilder) {
115                 ChildNodeBuilder cnb = (ChildNodeBuilder)builder;
116                 correctPath(cnb, builder.getPath());
117             }
118         }
119     }
120
121     public static LeafSchemaNodeBuilder copyLeafBuilder(
122             final LeafSchemaNodeBuilder old) {
123         final LeafSchemaNodeBuilder copy = new LeafSchemaNodeBuilder(
124                 old.getQName(), old.getLine());
125         final TypeDefinition<?> type = old.getType();
126
127         if (type == null) {
128             copy.setType(old.getTypedef());
129         } else {
130             copy.setType(type);
131         }
132         copy.setPath(old.getPath());
133         copyConstraints(old, copy);
134         for (UnknownSchemaNodeBuilder unknown : old.getUnknownNodes()) {
135             copy.addUnknownSchemaNode(unknown);
136         }
137         copy.setDescription(old.getDescription());
138         copy.setReference(old.getReference());
139         copy.setStatus(old.getStatus());
140         copy.setAugmenting(old.isAugmenting());
141         copy.setConfiguration(old.isConfiguration());
142         copy.setDefaultStr(old.getDefaultStr());
143         copy.setUnits(old.getUnits());
144         return copy;
145     }
146
147     public static ContainerSchemaNodeBuilder copyContainerBuilder(
148             final ContainerSchemaNodeBuilder old) {
149         final ContainerSchemaNodeBuilder copy = new ContainerSchemaNodeBuilder(
150                 old.getQName(), old.getLine());
151         copy.setPath(old.getPath());
152         copyConstraints(old, copy);
153         for (UnknownSchemaNodeBuilder unknown : old.getUnknownNodes()) {
154             copy.addUnknownSchemaNode(unknown);
155         }
156         for (DataSchemaNodeBuilder child : old.getChildNodes()) {
157             copy.addChildNode(child);
158         }
159         for (GroupingBuilder grouping : old.getGroupings()) {
160             copy.addGrouping(grouping);
161         }
162         for (TypeDefinitionBuilder typedef : old.getTypedefs()) {
163             copy.addTypedef(typedef);
164         }
165         for (AugmentationSchemaBuilder augment : old.getAugmentations()) {
166             copy.addAugmentation(augment);
167         }
168         for (UsesNodeBuilder use : old.getUsesNodes()) {
169             copy.addUsesNode(use);
170         }
171         for (UnknownSchemaNodeBuilder unknown : old.getUnknownNodes()) {
172             copy.addUnknownSchemaNode(unknown);
173         }
174         copy.setDescription(old.getDescription());
175         copy.setReference(old.getReference());
176         copy.setStatus(old.getStatus());
177         copy.setAugmenting(old.isAugmenting());
178         copy.setConfiguration(old.isConfiguration());
179         copy.setPresence(old.isPresence());
180         return copy;
181     }
182
183     public static ListSchemaNodeBuilder copyListBuilder(
184             final ListSchemaNodeBuilder old) {
185         final ListSchemaNodeBuilder copy = new ListSchemaNodeBuilder(
186                 old.getQName(), old.getLine());
187         copy.setPath(old.getPath());
188         copyConstraints(old, copy);
189         for (UnknownSchemaNodeBuilder unknown : old.getUnknownNodes()) {
190             copy.addUnknownSchemaNode(unknown);
191         }
192         for (DataSchemaNodeBuilder child : old.getChildNodes()) {
193             copy.addChildNode(child);
194         }
195         for (GroupingBuilder grouping : old.getGroupings()) {
196             copy.addGrouping(grouping);
197         }
198         for (TypeDefinitionBuilder typedef : old.getTypedefs()) {
199             copy.addTypedef(typedef);
200         }
201         for (AugmentationSchemaBuilder augment : old.getAugmentations()) {
202             copy.addAugmentation(augment);
203         }
204         for (UsesNodeBuilder use : old.getUsesNodes()) {
205             copy.addUsesNode(use);
206         }
207         for (UnknownSchemaNodeBuilder unknown : old.getUnknownNodes()) {
208             copy.addUnknownSchemaNode(unknown);
209         }
210         copy.setDescription(old.getDescription());
211         copy.setReference(old.getReference());
212         copy.setStatus(old.getStatus());
213         copy.setAugmenting(old.isAugmenting());
214         copy.setConfiguration(old.isConfiguration());
215         copy.setUserOrdered(old.isUserOrdered());
216         return copy;
217     }
218
219     public static LeafListSchemaNodeBuilder copyLeafListBuilder(
220             final LeafListSchemaNodeBuilder old) {
221         final LeafListSchemaNodeBuilder copy = new LeafListSchemaNodeBuilder(
222                 old.getQName(), old.getLine());
223         copy.setPath(old.getPath());
224         copyConstraints(old, copy);
225         final TypeDefinition<?> type = old.getType();
226         if (type == null) {
227             copy.setType(old.getTypedef());
228         } else {
229             copy.setType(type);
230         }
231         for (UnknownSchemaNodeBuilder unknown : old.getUnknownNodes()) {
232             copy.addUnknownSchemaNode(unknown);
233         }
234         for (UnknownSchemaNodeBuilder unknown : old.getUnknownNodes()) {
235             copy.addUnknownSchemaNode(unknown);
236         }
237         copy.setDescription(old.getDescription());
238         copy.setReference(old.getReference());
239         copy.setStatus(old.getStatus());
240         copy.setAugmenting(old.isAugmenting());
241         copy.setConfiguration(old.isConfiguration());
242         copy.setUserOrdered(old.isUserOrdered());
243         return copy;
244     }
245
246     public static ChoiceBuilder copyChoiceBuilder(final ChoiceBuilder old) {
247         final ChoiceBuilder copy = new ChoiceBuilder(old.getQName(), old.getLine());
248         copy.setPath(old.getPath());
249         copyConstraints(old, copy);
250         for (ChoiceCaseBuilder caseBuilder : old.getCases()) {
251             copy.addChildNode(caseBuilder);
252         }
253         for (UnknownSchemaNodeBuilder unknown : old.getUnknownNodes()) {
254             copy.addUnknownSchemaNode(unknown);
255         }
256         for (TypeDefinitionBuilder typedef : old.getTypedefs()) {
257             copy.addTypedef(typedef);
258         }
259         for (UsesNodeBuilder use : old.getUsesNodes()) {
260             copy.addUsesNode(use);
261         }
262         for (UnknownSchemaNodeBuilder unknown : old.getUnknownNodes()) {
263             copy.addUnknownSchemaNode(unknown);
264         }
265         copy.setDefaultCase(old.getDefaultCase());
266         copy.setDescription(old.getDescription());
267         copy.setReference(old.getReference());
268         copy.setStatus(old.getStatus());
269         copy.setAugmenting(old.isAugmenting());
270         copy.setConfiguration(old.isConfiguration());
271         return copy;
272     }
273
274     public static AnyXmlBuilder copyAnyXmlBuilder(final AnyXmlBuilder old) {
275         final AnyXmlBuilder copy = new AnyXmlBuilder(old.getQName(), old.getLine());
276         copy.setPath(old.getPath());
277         copyConstraints(old, copy);
278         for (UnknownSchemaNodeBuilder unknown : old.getUnknownNodes()) {
279             copy.addUnknownSchemaNode(unknown);
280         }
281         copy.setDescription(old.getDescription());
282         copy.setReference(old.getReference());
283         copy.setStatus(old.getStatus());
284         copy.setConfiguration(old.isConfiguration());
285         return copy;
286     }
287
288     private static void copyConstraints(final DataSchemaNodeBuilder oldBuilder,
289             final DataSchemaNodeBuilder newBuilder) {
290         final ConstraintsBuilder oldConstraints = oldBuilder.getConstraints();
291         final ConstraintsBuilder newConstraints = newBuilder.getConstraints();
292         newConstraints.addWhenCondition(oldConstraints.getWhenCondition());
293         for (MustDefinition must : oldConstraints.getMustDefinitions()) {
294             newConstraints.addMustDefinition(must);
295         }
296         newConstraints.setMandatory(oldConstraints.isMandatory());
297         newConstraints.setMinElements(oldConstraints.getMinElements());
298         newConstraints.setMaxElements(oldConstraints.getMaxElements());
299     }
300
301 }