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.util;
10 import java.util.LinkedList;
11 import java.util.List;
12 import java.util.Queue;
15 import org.opendaylight.controller.yang.common.QName;
16 import org.opendaylight.controller.yang.model.api.ContainerSchemaNode;
17 import org.opendaylight.controller.yang.model.api.DataNodeContainer;
18 import org.opendaylight.controller.yang.model.api.DataSchemaNode;
19 import org.opendaylight.controller.yang.model.api.ListSchemaNode;
20 import org.opendaylight.controller.yang.model.api.Module;
21 import org.opendaylight.controller.yang.model.api.ModuleImport;
22 import org.opendaylight.controller.yang.model.api.RevisionAwareXPath;
23 import org.opendaylight.controller.yang.model.api.SchemaContext;
24 import org.opendaylight.controller.yang.model.api.SchemaNode;
25 import org.opendaylight.controller.yang.model.api.SchemaPath;
27 public final class SchemaContextUtil {
29 private final SchemaContext context;
31 public SchemaContextUtil(final SchemaContext context) {
32 this.context = context;
35 public DataSchemaNode findDataSchemaNode(final Module module,
36 final RevisionAwareXPath nonCondXPath) {
37 if (nonCondXPath != null) {
38 final String strXPath = nonCondXPath.toString();
40 if (strXPath != null) {
41 if (strXPath.matches(".*//[.* | .*//].*")) {
42 // TODO: function to escape conditions in path
44 if (nonCondXPath.isAbsolute()) {
45 final Queue<String> queuedPath = xpathToQueuedPath(strXPath);
46 if (queuedPath != null) {
47 final DataSchemaNode dataNode = findSchemaNodeForGivenPath(
57 public DataSchemaNode findDataSchemaNodeForRelativeXPath(
58 final Module module, final SchemaNode actualSchemaNode,
59 final RevisionAwareXPath relativeXPath) {
60 if ((actualSchemaNode != null) && (relativeXPath != null)
61 && !relativeXPath.isAbsolute()) {
63 final SchemaPath actualNodePath = actualSchemaNode.getPath();
64 if (actualNodePath != null) {
65 final Queue<String> queuedPath = resolveRelativeXPath(
66 relativeXPath, actualNodePath);
68 if (queuedPath != null) {
69 final DataSchemaNode dataNode = findSchemaNodeForGivenPath(
79 public Module resolveModuleFromSchemaPath(final SchemaPath schemaPath) {
80 if ((schemaPath != null) && (schemaPath.getPath() != null)) {
81 final QName qname = schemaPath.getPath().get(0);
83 if ((qname != null) && (qname.getNamespace() != null)) {
85 .findModuleByNamespace(qname.getNamespace());
92 * Search which starts from root of Module.
98 private DataSchemaNode findSchemaNodeForGivenPath(final Module module,
99 final Queue<String> prefixedPath) {
100 if ((module != null) && (prefixedPath != null)) {
101 DataNodeContainer nextContainer = module;
102 final String modulePrefix = module.getPrefix();
104 String childNodeName = null;
105 DataSchemaNode schemaNode = null;
106 while ((nextContainer != null) && (prefixedPath.size() > 0)) {
107 childNodeName = prefixedPath.poll();
108 if (childNodeName.contains(":")) {
109 final String[] prefixedChildNode = childNodeName.split(":");
110 if ((modulePrefix != null)
111 && modulePrefix.equals(prefixedChildNode[0])) {
113 childNodeName = prefixedChildNode[1];
115 final Module nextModule = resolveModuleForPrefix(
116 prefixedChildNode[0], module);
117 final Queue<String> nextModulePrefixedPath = new LinkedList<String>();
118 nextModulePrefixedPath.add(childNodeName);
119 nextModulePrefixedPath.addAll(prefixedPath);
120 prefixedPath.clear();
122 schemaNode = findSchemaNodeForGivenPath(nextModule,
123 nextModulePrefixedPath);
129 schemaNode = nextContainer.getDataChildByName(childNodeName);
130 if (schemaNode instanceof ContainerSchemaNode) {
131 nextContainer = (ContainerSchemaNode) schemaNode;
132 } else if (schemaNode instanceof ListSchemaNode) {
133 nextContainer = (ListSchemaNode) schemaNode;
142 private Module resolveModuleForPrefix(final String prefix,
143 final Module parent) {
144 if ((prefix != null) && (parent != null)) {
145 final Set<ModuleImport> imports = parent.getImports();
147 if (imports != null) {
148 for (final ModuleImport impModule : imports) {
149 final String impModPrefix = impModule.getPrefix();
150 if ((impModPrefix != null) && prefix.equals(impModPrefix)) {
151 return resolveModuleFromContext(prefix,
152 impModule.getModuleName());
160 private Module resolveModuleFromContext(final String prefix,
161 final String moduleName) {
162 final Set<Module> modules = context.getModules();
164 if ((prefix != null) && (moduleName != null) && (modules != null)) {
165 for (Module module : modules) {
166 if ((module != null) && prefix.equals(module.getPrefix())
167 && moduleName.equals(module.getName())) {
175 private Queue<String> xpathToQueuedPath(final String xpath) {
176 final Queue<String> retQueue = new LinkedList<String>();
177 if ((xpath != null)) {
178 final String[] prefixedPath = xpath.split("/");
180 if (prefixedPath != null) {
181 for (int i = 0; i < prefixedPath.length; ++i) {
182 if (!prefixedPath[i].isEmpty()) {
183 retQueue.add(prefixedPath[i]);
191 private Queue<String> resolveRelativeXPath(
192 final RevisionAwareXPath relativeXPath,
193 final SchemaPath leafrefSchemaPath) {
194 final Queue<String> absolutePath = new LinkedList<String>();
196 if ((relativeXPath != null) && !relativeXPath.isAbsolute()
197 && (leafrefSchemaPath != null)) {
198 final String strXPath = relativeXPath.toString();
199 if (strXPath != null) {
200 final String[] xpaths = strXPath.split("/");
202 if (xpaths != null) {
204 while (xpaths[colCount].contains("..")) {
207 final List<QName> path = leafrefSchemaPath.getPath();
209 int lenght = path.size() - colCount;
210 for (int i = 0; i < lenght; ++i) {
211 absolutePath.add(path.get(i).getLocalName());
213 for (int i = colCount; i < xpaths.length; ++i) {
214 absolutePath.add(xpaths[i]);