Merge "Added support for annotations in generated APIs."
[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.DataSchemaNodeBuilder;
20 import org.opendaylight.controller.yang.model.parser.builder.api.GroupingBuilder;
21 import org.opendaylight.controller.yang.model.parser.builder.api.TypeDefinitionBuilder;
22 import org.opendaylight.controller.yang.model.parser.builder.api.UsesNodeBuilder;
23 import org.opendaylight.controller.yang.model.parser.builder.impl.AnyXmlBuilder;
24 import org.opendaylight.controller.yang.model.parser.builder.impl.ChoiceBuilder;
25 import org.opendaylight.controller.yang.model.parser.builder.impl.ChoiceCaseBuilder;
26 import org.opendaylight.controller.yang.model.parser.builder.impl.ConstraintsBuilder;
27 import org.opendaylight.controller.yang.model.parser.builder.impl.ContainerSchemaNodeBuilder;
28 import org.opendaylight.controller.yang.model.parser.builder.impl.LeafListSchemaNodeBuilder;
29 import org.opendaylight.controller.yang.model.parser.builder.impl.LeafSchemaNodeBuilder;
30 import org.opendaylight.controller.yang.model.parser.builder.impl.ListSchemaNodeBuilder;
31 import org.opendaylight.controller.yang.model.parser.builder.impl.ModuleBuilder;
32 import org.opendaylight.controller.yang.model.parser.builder.impl.UnknownSchemaNodeBuilder;
33
34 public final class ParserUtils {
35
36     private ParserUtils() {
37     }
38
39     /**
40      * Get module import referenced by given prefix.
41      *
42      * @param builder
43      *            module to search
44      * @param prefix
45      *            prefix associated with import
46      * @return ModuleImport based on given prefix
47      */
48     public static ModuleImport getModuleImport(final ModuleBuilder builder,
49             final String prefix) {
50         ModuleImport moduleImport = null;
51         for (ModuleImport mi : builder.getModuleImports()) {
52             if (mi.getPrefix().equals(prefix)) {
53                 moduleImport = mi;
54                 break;
55             }
56         }
57         return moduleImport;
58     }
59
60     /**
61      * Parse uses path.
62      * @param usesPath as String
63      * @return SchemaPath from given String
64      */
65     public static SchemaPath parseUsesPath(final String usesPath) {
66         final boolean absolute = usesPath.startsWith("/");
67         final String[] splittedPath = usesPath.split("/");
68         final List<QName> path = new ArrayList<QName>();
69         QName name;
70         for (String pathElement : splittedPath) {
71             if (pathElement.length() > 0) {
72                 final String[] splittedElement = pathElement.split(":");
73                 if (splittedElement.length == 1) {
74                     name = new QName(null, null, null, splittedElement[0]);
75                 } else {
76                     name = new QName(null, null, splittedElement[0],
77                             splittedElement[1]);
78                 }
79                 path.add(name);
80             }
81         }
82         return new SchemaPath(path, absolute);
83     }
84
85     public static LeafSchemaNodeBuilder copyLeafBuilder(
86             final LeafSchemaNodeBuilder old) {
87         final LeafSchemaNodeBuilder copy = new LeafSchemaNodeBuilder(
88                 old.getQName());
89         final TypeDefinition<?> type = old.getType();
90
91         if (type == null) {
92             copy.setType(old.getTypedef());
93         } else {
94             copy.setType(type);
95         }
96         copy.setPath(old.getPath());
97         copyConstraints(old, copy);
98         for (UnknownSchemaNodeBuilder unknown : old.getUnknownNodes()) {
99             copy.addUnknownSchemaNode(unknown);
100         }
101         copy.setDescription(old.getDescription());
102         copy.setReference(old.getReference());
103         copy.setStatus(old.getStatus());
104         copy.setAugmenting(old.isAugmenting());
105         copy.setConfiguration(old.isConfiguration());
106         copy.setDefaultStr(old.getDefaultStr());
107         copy.setUnits(old.getUnits());
108         return copy;
109     }
110
111     public static ContainerSchemaNodeBuilder copyContainerBuilder(
112             final ContainerSchemaNodeBuilder old) {
113         final ContainerSchemaNodeBuilder copy = new ContainerSchemaNodeBuilder(
114                 old.getQName());
115         copy.setPath(old.getPath());
116         copyConstraints(old, copy);
117         for (UnknownSchemaNodeBuilder unknown : old.getUnknownNodes()) {
118             copy.addUnknownSchemaNode(unknown);
119         }
120         for (DataSchemaNodeBuilder child : old.getChildNodes()) {
121             copy.addChildNode(child);
122         }
123         for (GroupingBuilder grouping : old.getGroupings()) {
124             copy.addGrouping(grouping);
125         }
126         for (TypeDefinitionBuilder typedef : old.getTypedefs()) {
127             copy.addTypedef(typedef);
128         }
129         for (AugmentationSchemaBuilder augment : old.getAugmentations()) {
130             copy.addAugmentation(augment);
131         }
132         for (UsesNodeBuilder use : old.getUsesNodes()) {
133             copy.addUsesNode(use);
134         }
135         for (UnknownSchemaNodeBuilder unknown : old.getUnknownNodes()) {
136             copy.addUnknownSchemaNode(unknown);
137         }
138         copy.setDescription(old.getDescription());
139         copy.setReference(old.getReference());
140         copy.setStatus(old.getStatus());
141         copy.setAugmenting(old.isAugmenting());
142         copy.setConfiguration(old.isConfiguration());
143         copy.setPresence(old.isPresence());
144         return copy;
145     }
146
147     public static ListSchemaNodeBuilder copyListBuilder(
148             final ListSchemaNodeBuilder old) {
149         final ListSchemaNodeBuilder copy = new ListSchemaNodeBuilder(
150                 old.getQName());
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.setUserOrdered(old.isUserOrdered());
180         return copy;
181     }
182
183     public static LeafListSchemaNodeBuilder copyLeafListBuilder(
184             final LeafListSchemaNodeBuilder old) {
185         final LeafListSchemaNodeBuilder copy = new LeafListSchemaNodeBuilder(
186                 old.getQName());
187         copy.setPath(old.getPath());
188         copyConstraints(old, copy);
189         final TypeDefinition<?> type = old.getType();
190         if (type == null) {
191             copy.setType(old.getTypedef());
192         } else {
193             copy.setType(type);
194         }
195         for (UnknownSchemaNodeBuilder unknown : old.getUnknownNodes()) {
196             copy.addUnknownSchemaNode(unknown);
197         }
198         for (UnknownSchemaNodeBuilder unknown : old.getUnknownNodes()) {
199             copy.addUnknownSchemaNode(unknown);
200         }
201         copy.setDescription(old.getDescription());
202         copy.setReference(old.getReference());
203         copy.setStatus(old.getStatus());
204         copy.setAugmenting(old.isAugmenting());
205         copy.setConfiguration(old.isConfiguration());
206         copy.setUserOrdered(old.isUserOrdered());
207         return copy;
208     }
209
210     public static ChoiceBuilder copyChoiceBuilder(final ChoiceBuilder old) {
211         final ChoiceBuilder copy = new ChoiceBuilder(old.getQName());
212         copy.setPath(old.getPath());
213         copyConstraints(old, copy);
214         for (ChoiceCaseBuilder caseBuilder : old.getCases()) {
215             copy.addChildNode(caseBuilder);
216         }
217         for (UnknownSchemaNodeBuilder unknown : old.getUnknownNodes()) {
218             copy.addUnknownSchemaNode(unknown);
219         }
220         for (TypeDefinitionBuilder typedef : old.getTypedefs()) {
221             copy.addTypedef(typedef);
222         }
223         for (UsesNodeBuilder use : old.getUsesNodes()) {
224             copy.addUsesNode(use);
225         }
226         for (UnknownSchemaNodeBuilder unknown : old.getUnknownNodes()) {
227             copy.addUnknownSchemaNode(unknown);
228         }
229         copy.setDefaultCase(old.getDefaultCase());
230         copy.setDescription(old.getDescription());
231         copy.setReference(old.getReference());
232         copy.setStatus(old.getStatus());
233         copy.setAugmenting(old.isAugmenting());
234         copy.setConfiguration(old.isConfiguration());
235         return copy;
236     }
237
238     public static AnyXmlBuilder copyAnyXmlBuilder(final AnyXmlBuilder old) {
239         final AnyXmlBuilder copy = new AnyXmlBuilder(old.getQName());
240         copy.setPath(old.getPath());
241         copyConstraints(old, copy);
242         for (UnknownSchemaNodeBuilder unknown : old.getUnknownNodes()) {
243             copy.addUnknownSchemaNode(unknown);
244         }
245         copy.setDescription(old.getDescription());
246         copy.setReference(old.getReference());
247         copy.setStatus(old.getStatus());
248         copy.setConfiguration(old.isConfiguration());
249         return copy;
250     }
251
252     private static void copyConstraints(final DataSchemaNodeBuilder oldBuilder,
253             final DataSchemaNodeBuilder newBuilder) {
254         final ConstraintsBuilder oldConstraints = oldBuilder.getConstraints();
255         final ConstraintsBuilder newConstraints = newBuilder.getConstraints();
256         newConstraints.addWhenCondition(oldConstraints.getWhenCondition());
257         for (MustDefinition must : oldConstraints.getMustDefinitions()) {
258             newConstraints.addMustDefinition(must);
259         }
260         newConstraints.setMandatory(oldConstraints.isMandatory());
261         newConstraints.setMinElements(oldConstraints.getMinElements());
262         newConstraints.setMaxElements(oldConstraints.getMaxElements());
263     }
264
265 }