fa88a29981335c1f34c834decbc0f2bb366e53f7
[controller.git] / opendaylight / sal / yang-prototype / code-generator / yang-model-parser-impl / src / main / java / org / opendaylight / controller / yang / model / 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.model.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.model.parser.builder.api.AugmentationSchemaBuilder;
19 import org.opendaylight.controller.yang.model.parser.builder.api.ChildNodeBuilder;
20 import org.opendaylight.controller.yang.model.parser.builder.api.DataSchemaNodeBuilder;
21 import org.opendaylight.controller.yang.model.parser.builder.api.GroupingBuilder;
22 import org.opendaylight.controller.yang.model.parser.builder.api.TypeDefinitionBuilder;
23 import org.opendaylight.controller.yang.model.parser.builder.api.UsesNodeBuilder;
24 import org.opendaylight.controller.yang.model.parser.builder.impl.AnyXmlBuilder;
25 import org.opendaylight.controller.yang.model.parser.builder.impl.ChoiceBuilder;
26 import org.opendaylight.controller.yang.model.parser.builder.impl.ChoiceCaseBuilder;
27 import org.opendaylight.controller.yang.model.parser.builder.impl.ConstraintsBuilder;
28 import org.opendaylight.controller.yang.model.parser.builder.impl.ContainerSchemaNodeBuilder;
29 import org.opendaylight.controller.yang.model.parser.builder.impl.LeafListSchemaNodeBuilder;
30 import org.opendaylight.controller.yang.model.parser.builder.impl.LeafSchemaNodeBuilder;
31 import org.opendaylight.controller.yang.model.parser.builder.impl.ListSchemaNodeBuilder;
32 import org.opendaylight.controller.yang.model.parser.builder.impl.ModuleBuilder;
33 import org.opendaylight.controller.yang.model.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             target.addChildNode(builder);
100         }
101     }
102
103     public static LeafSchemaNodeBuilder copyLeafBuilder(
104             final LeafSchemaNodeBuilder old) {
105         final LeafSchemaNodeBuilder copy = new LeafSchemaNodeBuilder(
106                 old.getQName());
107         final TypeDefinition<?> type = old.getType();
108
109         if (type == null) {
110             copy.setType(old.getTypedef());
111         } else {
112             copy.setType(type);
113         }
114         copy.setPath(old.getPath());
115         copyConstraints(old, copy);
116         for (UnknownSchemaNodeBuilder unknown : old.getUnknownNodes()) {
117             copy.addUnknownSchemaNode(unknown);
118         }
119         copy.setDescription(old.getDescription());
120         copy.setReference(old.getReference());
121         copy.setStatus(old.getStatus());
122         copy.setAugmenting(old.isAugmenting());
123         copy.setConfiguration(old.isConfiguration());
124         copy.setDefaultStr(old.getDefaultStr());
125         copy.setUnits(old.getUnits());
126         return copy;
127     }
128
129     public static ContainerSchemaNodeBuilder copyContainerBuilder(
130             final ContainerSchemaNodeBuilder old) {
131         final ContainerSchemaNodeBuilder copy = new ContainerSchemaNodeBuilder(
132                 old.getQName());
133         copy.setPath(old.getPath());
134         copyConstraints(old, copy);
135         for (UnknownSchemaNodeBuilder unknown : old.getUnknownNodes()) {
136             copy.addUnknownSchemaNode(unknown);
137         }
138         for (DataSchemaNodeBuilder child : old.getChildNodes()) {
139             copy.addChildNode(child);
140         }
141         for (GroupingBuilder grouping : old.getGroupings()) {
142             copy.addGrouping(grouping);
143         }
144         for (TypeDefinitionBuilder typedef : old.getTypedefs()) {
145             copy.addTypedef(typedef);
146         }
147         for (AugmentationSchemaBuilder augment : old.getAugmentations()) {
148             copy.addAugmentation(augment);
149         }
150         for (UsesNodeBuilder use : old.getUsesNodes()) {
151             copy.addUsesNode(use);
152         }
153         for (UnknownSchemaNodeBuilder unknown : old.getUnknownNodes()) {
154             copy.addUnknownSchemaNode(unknown);
155         }
156         copy.setDescription(old.getDescription());
157         copy.setReference(old.getReference());
158         copy.setStatus(old.getStatus());
159         copy.setAugmenting(old.isAugmenting());
160         copy.setConfiguration(old.isConfiguration());
161         copy.setPresence(old.isPresence());
162         return copy;
163     }
164
165     public static ListSchemaNodeBuilder copyListBuilder(
166             final ListSchemaNodeBuilder old) {
167         final ListSchemaNodeBuilder copy = new ListSchemaNodeBuilder(
168                 old.getQName());
169         copy.setPath(old.getPath());
170         copyConstraints(old, copy);
171         for (UnknownSchemaNodeBuilder unknown : old.getUnknownNodes()) {
172             copy.addUnknownSchemaNode(unknown);
173         }
174         for (DataSchemaNodeBuilder child : old.getChildNodes()) {
175             copy.addChildNode(child);
176         }
177         for (GroupingBuilder grouping : old.getGroupings()) {
178             copy.addGrouping(grouping);
179         }
180         for (TypeDefinitionBuilder typedef : old.getTypedefs()) {
181             copy.addTypedef(typedef);
182         }
183         for (AugmentationSchemaBuilder augment : old.getAugmentations()) {
184             copy.addAugmentation(augment);
185         }
186         for (UsesNodeBuilder use : old.getUsesNodes()) {
187             copy.addUsesNode(use);
188         }
189         for (UnknownSchemaNodeBuilder unknown : old.getUnknownNodes()) {
190             copy.addUnknownSchemaNode(unknown);
191         }
192         copy.setDescription(old.getDescription());
193         copy.setReference(old.getReference());
194         copy.setStatus(old.getStatus());
195         copy.setAugmenting(old.isAugmenting());
196         copy.setConfiguration(old.isConfiguration());
197         copy.setUserOrdered(old.isUserOrdered());
198         return copy;
199     }
200
201     public static LeafListSchemaNodeBuilder copyLeafListBuilder(
202             final LeafListSchemaNodeBuilder old) {
203         final LeafListSchemaNodeBuilder copy = new LeafListSchemaNodeBuilder(
204                 old.getQName());
205         copy.setPath(old.getPath());
206         copyConstraints(old, copy);
207         final TypeDefinition<?> type = old.getType();
208         if (type == null) {
209             copy.setType(old.getTypedef());
210         } else {
211             copy.setType(type);
212         }
213         for (UnknownSchemaNodeBuilder unknown : old.getUnknownNodes()) {
214             copy.addUnknownSchemaNode(unknown);
215         }
216         for (UnknownSchemaNodeBuilder unknown : old.getUnknownNodes()) {
217             copy.addUnknownSchemaNode(unknown);
218         }
219         copy.setDescription(old.getDescription());
220         copy.setReference(old.getReference());
221         copy.setStatus(old.getStatus());
222         copy.setAugmenting(old.isAugmenting());
223         copy.setConfiguration(old.isConfiguration());
224         copy.setUserOrdered(old.isUserOrdered());
225         return copy;
226     }
227
228     public static ChoiceBuilder copyChoiceBuilder(final ChoiceBuilder old) {
229         final ChoiceBuilder copy = new ChoiceBuilder(old.getQName());
230         copy.setPath(old.getPath());
231         copyConstraints(old, copy);
232         for (ChoiceCaseBuilder caseBuilder : old.getCases()) {
233             copy.addChildNode(caseBuilder);
234         }
235         for (UnknownSchemaNodeBuilder unknown : old.getUnknownNodes()) {
236             copy.addUnknownSchemaNode(unknown);
237         }
238         for (TypeDefinitionBuilder typedef : old.getTypedefs()) {
239             copy.addTypedef(typedef);
240         }
241         for (UsesNodeBuilder use : old.getUsesNodes()) {
242             copy.addUsesNode(use);
243         }
244         for (UnknownSchemaNodeBuilder unknown : old.getUnknownNodes()) {
245             copy.addUnknownSchemaNode(unknown);
246         }
247         copy.setDefaultCase(old.getDefaultCase());
248         copy.setDescription(old.getDescription());
249         copy.setReference(old.getReference());
250         copy.setStatus(old.getStatus());
251         copy.setAugmenting(old.isAugmenting());
252         copy.setConfiguration(old.isConfiguration());
253         return copy;
254     }
255
256     public static AnyXmlBuilder copyAnyXmlBuilder(final AnyXmlBuilder old) {
257         final AnyXmlBuilder copy = new AnyXmlBuilder(old.getQName());
258         copy.setPath(old.getPath());
259         copyConstraints(old, copy);
260         for (UnknownSchemaNodeBuilder unknown : old.getUnknownNodes()) {
261             copy.addUnknownSchemaNode(unknown);
262         }
263         copy.setDescription(old.getDescription());
264         copy.setReference(old.getReference());
265         copy.setStatus(old.getStatus());
266         copy.setConfiguration(old.isConfiguration());
267         return copy;
268     }
269
270     private static void copyConstraints(final DataSchemaNodeBuilder oldBuilder,
271             final DataSchemaNodeBuilder newBuilder) {
272         final ConstraintsBuilder oldConstraints = oldBuilder.getConstraints();
273         final ConstraintsBuilder newConstraints = newBuilder.getConstraints();
274         newConstraints.addWhenCondition(oldConstraints.getWhenCondition());
275         for (MustDefinition must : oldConstraints.getMustDefinitions()) {
276             newConstraints.addMustDefinition(must);
277         }
278         newConstraints.setMandatory(oldConstraints.isMandatory());
279         newConstraints.setMinElements(oldConstraints.getMinElements());
280         newConstraints.setMaxElements(oldConstraints.getMaxElements());
281     }
282
283 }