2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
\r
4 * This program and the accompanying materials are made available under the
\r
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
\r
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
\r
8 package org.opendaylight.controller.sal.binding.yang.types;
\r
10 import java.util.LinkedList;
\r
11 import java.util.List;
\r
12 import java.util.Queue;
\r
13 import java.util.Set;
\r
15 import org.opendaylight.controller.binding.generator.util.Types;
\r
16 import org.opendaylight.controller.sal.binding.generator.spi.TypeProvider;
\r
17 import org.opendaylight.controller.sal.binding.model.api.Type;
\r
18 import org.opendaylight.controller.yang.common.QName;
\r
19 import org.opendaylight.controller.yang.model.api.ContainerSchemaNode;
\r
20 import org.opendaylight.controller.yang.model.api.DataNodeContainer;
\r
21 import org.opendaylight.controller.yang.model.api.DataSchemaNode;
\r
22 import org.opendaylight.controller.yang.model.api.LeafListSchemaNode;
\r
23 import org.opendaylight.controller.yang.model.api.LeafSchemaNode;
\r
24 import org.opendaylight.controller.yang.model.api.ListSchemaNode;
\r
25 import org.opendaylight.controller.yang.model.api.Module;
\r
26 import org.opendaylight.controller.yang.model.api.ModuleImport;
\r
27 import org.opendaylight.controller.yang.model.api.RevisionAwareXPath;
\r
28 import org.opendaylight.controller.yang.model.api.SchemaContext;
\r
29 import org.opendaylight.controller.yang.model.api.SchemaPath;
\r
30 import org.opendaylight.controller.yang.model.api.TypeDefinition;
\r
31 import org.opendaylight.controller.yang.model.api.type.IdentityrefTypeDefinition;
\r
32 import org.opendaylight.controller.yang.model.api.type.LeafrefTypeDefinition;
\r
33 import org.opendaylight.controller.yang.model.util.Leafref;
\r
35 public class TypeProviderImpl implements TypeProvider {
\r
37 private SchemaContext schemaContext;
\r
39 public TypeProviderImpl(SchemaContext schemaContext) {
\r
40 this.schemaContext = schemaContext;
\r
46 * @see org.opendaylight.controller.yang.model.type.provider.TypeProvider#
\r
47 * javaTypeForYangType(java.lang.String)
\r
50 public Type javaTypeForYangType(String type) {
\r
51 Type t = BaseYangTypes.BASE_YANG_TYPES_PROVIDER
\r
52 .javaTypeForYangType(type);
\r
57 public Type javaTypeForSchemaDefinitionType(final TypeDefinition<?> type) {
\r
58 Type returnType = null;
\r
60 if (type instanceof Leafref) {
\r
61 final LeafrefTypeDefinition leafref = (LeafrefTypeDefinition) type;
\r
62 returnType = provideTypeForLeafref(leafref);
\r
63 } else if (type instanceof IdentityrefTypeDefinition) {
\r
66 returnType = BaseYangTypes.BASE_YANG_TYPES_PROVIDER
\r
67 .javaTypeForSchemaDefinitionType(type);
\r
73 public Type provideTypeForLeafref(final LeafrefTypeDefinition leafrefType) {
\r
74 Type returnType = null;
\r
75 if ((leafrefType != null) && (leafrefType.getPathStatement() != null)) {
\r
76 final RevisionAwareXPath xpath = leafrefType.getPathStatement();
\r
77 final String strXPath = xpath.toString();
\r
79 if (strXPath != null) {
\r
80 if (strXPath.matches(".*//[.* | .*//].*")) {
\r
81 returnType = Types.typeForClass(Object.class);
\r
83 final Module module = resolveModuleFromSchemaContext(leafrefType
\r
85 if (module != null) {
\r
86 Queue<String> leafrefPath;
\r
87 if (!xpath.isAbsolute()) {
\r
88 leafrefPath = resolveRelativeXPath(xpath,
\r
89 leafrefType.getPath());
\r
91 leafrefPath = xpathToPrefixedPath(strXPath, module.getName());
\r
94 if (leafrefPath != null) {
\r
95 final DataSchemaNode dataNode = findSchemaNodeForGivenPath(
\r
96 module, leafrefPath);
\r
97 returnType = resolveTypeFromDataSchemaNode(dataNode);
\r
106 private Type resolveTypeFromDataSchemaNode(final DataSchemaNode dataNode) {
\r
107 Type returnType = null;
\r
108 if (dataNode != null) {
\r
109 if (dataNode instanceof LeafSchemaNode) {
\r
110 final LeafSchemaNode leaf = (LeafSchemaNode) dataNode;
\r
111 returnType = javaTypeForSchemaDefinitionType(leaf.getType());
\r
112 } else if (dataNode instanceof LeafListSchemaNode) {
\r
113 final LeafListSchemaNode leafList = (LeafListSchemaNode) dataNode;
\r
114 returnType = javaTypeForSchemaDefinitionType(leafList.getType());
\r
121 * Search which starts from root of Module.
\r
124 * @param prefixedPath
\r
127 private DataSchemaNode findSchemaNodeForGivenPath(final Module module,
\r
128 final Queue<String> prefixedPath) {
\r
129 if ((module != null) && (prefixedPath != null)) {
\r
130 final String modulePrefix = module.getPrefix();
\r
131 String childNodeName = prefixedPath.poll();
\r
132 DataNodeContainer nextContainer = null;
\r
134 if ((childNodeName != null)
\r
135 && childNodeName.equals(module.getName())) {
\r
136 nextContainer = module;
\r
139 DataSchemaNode schemaNode = null;
\r
140 while ((nextContainer != null) && (prefixedPath.size() > 0)) {
\r
141 childNodeName = prefixedPath.poll();
\r
142 if (childNodeName.contains(":")) {
\r
143 final String[] prefixedChildNode = childNodeName.split(":");
\r
144 if ((modulePrefix != null)
\r
145 && modulePrefix.equals(prefixedChildNode[0])) {
\r
147 childNodeName = prefixedChildNode[1];
\r
149 final Module nextModule = resolveModuleForPrefix(
\r
150 prefixedChildNode[0], module);
\r
151 final Queue<String> nextModulePrefixedPath = new LinkedList<String>();
\r
153 nextModulePrefixedPath.add(nextModule.getName());
\r
154 nextModulePrefixedPath.add(childNodeName);
\r
155 nextModulePrefixedPath.addAll(prefixedPath);
\r
156 prefixedPath.clear();
\r
158 schemaNode = findSchemaNodeForGivenPath(nextModule,
\r
159 nextModulePrefixedPath);
\r
165 schemaNode = nextContainer.getDataChildByName(childNodeName);
\r
166 if (schemaNode instanceof ContainerSchemaNode) {
\r
167 nextContainer = (ContainerSchemaNode) schemaNode;
\r
168 } else if (schemaNode instanceof ListSchemaNode) {
\r
169 nextContainer = (ListSchemaNode) schemaNode;
\r
179 private Module resolveModuleFromSchemaContext(final SchemaPath schemaPath) {
\r
180 final Set<Module> modules = schemaContext.getModules();
\r
181 final String moduleName = resolveModuleName(schemaPath);
\r
182 if ((moduleName != null) && (modules != null)) {
\r
183 for (final Module module : modules) {
\r
184 if (module.getName().equals(moduleName)) {
\r
192 private String resolveModuleName(final SchemaPath schemaPath) {
\r
193 if ((schemaPath != null) && (schemaPath.getPath() != null)) {
\r
194 final QName qname = schemaPath.getPath().get(0);
\r
195 if ((qname != null) && (qname.getLocalName() != null)) {
\r
196 return qname.getLocalName();
\r
202 private Queue<String> xpathToPrefixedPath(final String xpath, final String moduleName) {
\r
203 final Queue<String> retQueue = new LinkedList<String>();
\r
204 if ((xpath != null) && (moduleName != null)) {
\r
205 final String[] prefixedPath = xpath.split("/");
\r
207 retQueue.add(moduleName);
\r
208 if (prefixedPath != null) {
\r
209 for (int i = 0; i < prefixedPath.length; ++i) {
\r
210 if (!prefixedPath[i].isEmpty()) {
\r
211 retQueue.add(prefixedPath[i]);
\r
219 private Module resolveModuleForPrefix(final String prefix,
\r
220 final Module parent) {
\r
221 if ((prefix != null) && (parent != null)) {
\r
222 final Set<ModuleImport> imports = parent.getImports();
\r
224 if (imports != null) {
\r
225 for (final ModuleImport impModule : imports) {
\r
226 final String impModPrefix = impModule.getPrefix();
\r
227 if ((impModPrefix != null) && prefix.equals(impModPrefix)) {
\r
228 return resolveModuleFromContext(prefix,
\r
229 impModule.getModuleName());
\r
237 private Module resolveModuleFromContext(final String prefix,
\r
238 final String moduleName) {
\r
239 final Set<Module> modules = schemaContext.getModules();
\r
241 if ((prefix != null) && (moduleName != null) && (modules != null)) {
\r
242 for (Module module : modules) {
\r
243 if ((module != null) && prefix.equals(module.getPrefix())
\r
244 && moduleName.equals(module.getName())) {
\r
252 private Queue<String> resolveRelativeXPath(
\r
253 final RevisionAwareXPath relativeXPath,
\r
254 final SchemaPath leafrefSchemaPath) {
\r
255 final Queue<String> absolutePath = new LinkedList<String>();
\r
257 if ((relativeXPath != null) && !relativeXPath.isAbsolute()
\r
258 && (leafrefSchemaPath != null)) {
\r
259 final String strXPath = relativeXPath.toString();
\r
260 if (strXPath != null) {
\r
261 final String[] xpaths = strXPath.split("/");
\r
263 if (xpaths != null) {
\r
265 while (xpaths[colCount].contains("..")) {
\r
268 final List<QName> path = leafrefSchemaPath.getPath();
\r
269 if (path != null) {
\r
270 int lenght = path.size() - colCount;
\r
271 for (int i = 0; i < lenght; ++i) {
\r
272 absolutePath.add(path.get(i).getLocalName());
\r
274 for (int i = colCount; i < xpaths.length; ++i) {
\r
275 absolutePath.add(xpaths[i]);
\r
281 return absolutePath;
\r