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.controller.yang.parser.util;
10 import java.util.ArrayList;
11 import java.util.List;
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;
35 public final class ParserUtils {
37 private ParserUtils() {
41 * Get module import referenced by given prefix.
46 * prefix associated with import
47 * @return ModuleImport based on given prefix
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)) {
66 * @return SchemaPath from given String
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>();
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]);
79 name = new QName(null, null, splittedElement[0],
85 return new SchemaPath(path, absolute);
89 * Add all augment's child nodes to given target.
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);
104 private static void correctPath(final ChildNodeBuilder node,
105 final SchemaPath parentSchemaPath) {
106 for(DataSchemaNodeBuilder builder : node.getChildNodes()) {
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));
114 if(builder instanceof ChildNodeBuilder) {
115 ChildNodeBuilder cnb = (ChildNodeBuilder)builder;
116 correctPath(cnb, builder.getPath());
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();
128 copy.setType(old.getTypedef());
132 copy.setPath(old.getPath());
133 copyConstraints(old, copy);
134 for (UnknownSchemaNodeBuilder unknown : old.getUnknownNodes()) {
135 copy.addUnknownSchemaNode(unknown);
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());
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);
156 for (DataSchemaNodeBuilder child : old.getChildNodes()) {
157 copy.addChildNode(child);
159 for (GroupingBuilder grouping : old.getGroupings()) {
160 copy.addGrouping(grouping);
162 for (TypeDefinitionBuilder typedef : old.getTypedefs()) {
163 copy.addTypedef(typedef);
165 for (AugmentationSchemaBuilder augment : old.getAugmentations()) {
166 copy.addAugmentation(augment);
168 for (UsesNodeBuilder use : old.getUsesNodes()) {
169 copy.addUsesNode(use);
171 for (UnknownSchemaNodeBuilder unknown : old.getUnknownNodes()) {
172 copy.addUnknownSchemaNode(unknown);
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());
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);
192 for (DataSchemaNodeBuilder child : old.getChildNodes()) {
193 copy.addChildNode(child);
195 for (GroupingBuilder grouping : old.getGroupings()) {
196 copy.addGrouping(grouping);
198 for (TypeDefinitionBuilder typedef : old.getTypedefs()) {
199 copy.addTypedef(typedef);
201 for (AugmentationSchemaBuilder augment : old.getAugmentations()) {
202 copy.addAugmentation(augment);
204 for (UsesNodeBuilder use : old.getUsesNodes()) {
205 copy.addUsesNode(use);
207 for (UnknownSchemaNodeBuilder unknown : old.getUnknownNodes()) {
208 copy.addUnknownSchemaNode(unknown);
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());
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();
227 copy.setType(old.getTypedef());
231 for (UnknownSchemaNodeBuilder unknown : old.getUnknownNodes()) {
232 copy.addUnknownSchemaNode(unknown);
234 for (UnknownSchemaNodeBuilder unknown : old.getUnknownNodes()) {
235 copy.addUnknownSchemaNode(unknown);
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());
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);
253 for (UnknownSchemaNodeBuilder unknown : old.getUnknownNodes()) {
254 copy.addUnknownSchemaNode(unknown);
256 for (TypeDefinitionBuilder typedef : old.getTypedefs()) {
257 copy.addTypedef(typedef);
259 for (UsesNodeBuilder use : old.getUsesNodes()) {
260 copy.addUsesNode(use);
262 for (UnknownSchemaNodeBuilder unknown : old.getUnknownNodes()) {
263 copy.addUnknownSchemaNode(unknown);
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());
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);
281 copy.setDescription(old.getDescription());
282 copy.setReference(old.getReference());
283 copy.setStatus(old.getStatus());
284 copy.setConfiguration(old.isConfiguration());
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);
296 newConstraints.setMandatory(oldConstraints.isMandatory());
297 newConstraints.setMinElements(oldConstraints.getMinElements());
298 newConstraints.setMaxElements(oldConstraints.getMaxElements());