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.model.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.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;
34 public final class ParserUtils {
36 private ParserUtils() {
40 * Get module import referenced by given prefix.
45 * prefix associated with import
46 * @return ModuleImport based on given prefix
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)) {
62 * @param usesPath as String
63 * @return SchemaPath from given String
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>();
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]);
76 name = new QName(null, null, splittedElement[0],
82 return new SchemaPath(path, absolute);
85 public static LeafSchemaNodeBuilder copyLeafBuilder(
86 final LeafSchemaNodeBuilder old) {
87 final LeafSchemaNodeBuilder copy = new LeafSchemaNodeBuilder(
89 final TypeDefinition<?> type = old.getType();
92 copy.setType(old.getTypedef());
96 copy.setPath(old.getPath());
97 copyConstraints(old, copy);
98 for (UnknownSchemaNodeBuilder unknown : old.getUnknownNodes()) {
99 copy.addUnknownSchemaNode(unknown);
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());
111 public static ContainerSchemaNodeBuilder copyContainerBuilder(
112 final ContainerSchemaNodeBuilder old) {
113 final ContainerSchemaNodeBuilder copy = new ContainerSchemaNodeBuilder(
115 copy.setPath(old.getPath());
116 copyConstraints(old, copy);
117 for (UnknownSchemaNodeBuilder unknown : old.getUnknownNodes()) {
118 copy.addUnknownSchemaNode(unknown);
120 for (DataSchemaNodeBuilder child : old.getChildNodes()) {
121 copy.addChildNode(child);
123 for (GroupingBuilder grouping : old.getGroupings()) {
124 copy.addGrouping(grouping);
126 for (TypeDefinitionBuilder typedef : old.getTypedefs()) {
127 copy.addTypedef(typedef);
129 for (AugmentationSchemaBuilder augment : old.getAugmentations()) {
130 copy.addAugmentation(augment);
132 for (UsesNodeBuilder use : old.getUsesNodes()) {
133 copy.addUsesNode(use);
135 for (UnknownSchemaNodeBuilder unknown : old.getUnknownNodes()) {
136 copy.addUnknownSchemaNode(unknown);
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());
147 public static ListSchemaNodeBuilder copyListBuilder(
148 final ListSchemaNodeBuilder old) {
149 final ListSchemaNodeBuilder copy = new ListSchemaNodeBuilder(
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.setUserOrdered(old.isUserOrdered());
183 public static LeafListSchemaNodeBuilder copyLeafListBuilder(
184 final LeafListSchemaNodeBuilder old) {
185 final LeafListSchemaNodeBuilder copy = new LeafListSchemaNodeBuilder(
187 copy.setPath(old.getPath());
188 copyConstraints(old, copy);
189 final TypeDefinition<?> type = old.getType();
191 copy.setType(old.getTypedef());
195 for (UnknownSchemaNodeBuilder unknown : old.getUnknownNodes()) {
196 copy.addUnknownSchemaNode(unknown);
198 for (UnknownSchemaNodeBuilder unknown : old.getUnknownNodes()) {
199 copy.addUnknownSchemaNode(unknown);
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());
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);
217 for (UnknownSchemaNodeBuilder unknown : old.getUnknownNodes()) {
218 copy.addUnknownSchemaNode(unknown);
220 for (TypeDefinitionBuilder typedef : old.getTypedefs()) {
221 copy.addTypedef(typedef);
223 for (UsesNodeBuilder use : old.getUsesNodes()) {
224 copy.addUsesNode(use);
226 for (UnknownSchemaNodeBuilder unknown : old.getUnknownNodes()) {
227 copy.addUnknownSchemaNode(unknown);
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());
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);
245 copy.setDescription(old.getDescription());
246 copy.setReference(old.getReference());
247 copy.setStatus(old.getStatus());
248 copy.setConfiguration(old.isConfiguration());
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);
260 newConstraints.setMandatory(oldConstraints.isMandatory());
261 newConstraints.setMinElements(oldConstraints.getMinElements());
262 newConstraints.setMaxElements(oldConstraints.getMaxElements());