X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=yang%2Fyang-model-util%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fyangtools%2Fyang%2Fmodel%2Futil%2FSchemaContextUtil.java;h=b10633a2ea20e8d503fd886e92fe7dbad4335a61;hb=6b5d20f6513bc3e6e5db4a2058ee81308edaa9c8;hp=10594211833df3fb15a86fad6ba32dd091bf4112;hpb=d201a3088bb18099fbfd8d71dd982244eb9b1d9a;p=yangtools.git diff --git a/yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/SchemaContextUtil.java b/yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/SchemaContextUtil.java index 1059421183..b10633a2ea 100644 --- a/yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/SchemaContextUtil.java +++ b/yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/SchemaContextUtil.java @@ -8,12 +8,13 @@ package org.opendaylight.yangtools.yang.model.util; import com.google.common.annotations.Beta; +import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Function; import com.google.common.base.Optional; import com.google.common.base.Preconditions; import com.google.common.base.Splitter; import com.google.common.collect.Iterables; -import java.util.Arrays; +import java.util.Collections; import java.util.Iterator; import java.util.LinkedList; import java.util.List; @@ -469,7 +470,7 @@ public final class SchemaContextUtil { Preconditions.checkArgument(parentModule != null, "Parent Module reference cannot be NULL"); Preconditions.checkArgument(xpath != null, "XPath string reference cannot be NULL"); - final List path = new LinkedList(); + final List path = new LinkedList<>(); for (final String pathComponent : SLASH_SPLITTER.split(xpath)) { if (!pathComponent.isEmpty()) { path.add(stringPathPartToQName(context, parentModule, pathComponent)); @@ -630,23 +631,25 @@ public final class SchemaContextUtil { RevisionAwareXPath pathStatement = typeDefinition.getPathStatement(); pathStatement = new RevisionAwareXPathImpl(stripConditionsFromXPathString(pathStatement), pathStatement.isAbsolute()); - SchemaNode baseSchema = schema; - while (baseSchema instanceof DerivableSchemaNode) { - final Optional basePotential = ((DerivableSchemaNode) baseSchema).getOriginal(); - if (basePotential.isPresent()) { - baseSchema = basePotential.get(); - } else { - break; + final DataSchemaNode dataSchemaNode; + if (pathStatement.isAbsolute()) { + SchemaNode baseSchema = schema; + while (baseSchema instanceof DerivableSchemaNode) { + final Optional basePotential = ((DerivableSchemaNode) baseSchema).getOriginal(); + if (basePotential.isPresent()) { + baseSchema = basePotential.get(); + } else { + break; + } } - } - Module parentModule = findParentModuleByType(schemaContext, baseSchema); - - final DataSchemaNode dataSchemaNode; - if(pathStatement.isAbsolute()) { - dataSchemaNode = (DataSchemaNode) SchemaContextUtil.findDataSchemaNode(schemaContext, parentModule, pathStatement); + Module parentModule = findParentModuleOfReferencingType(schemaContext, baseSchema); + dataSchemaNode = (DataSchemaNode) SchemaContextUtil.findDataSchemaNode(schemaContext, parentModule, + pathStatement); } else { - dataSchemaNode = (DataSchemaNode) SchemaContextUtil.findDataSchemaNodeForRelativeXPath(schemaContext, parentModule, baseSchema, pathStatement); + Module parentModule = findParentModule(schemaContext, schema); + dataSchemaNode = (DataSchemaNode) SchemaContextUtil.findDataSchemaNodeForRelativeXPath(schemaContext, + parentModule, schema, pathStatement); } // FIXME this is just to preserve backwards compatibility since yangtools do not mind wrong leafref xpaths @@ -665,7 +668,41 @@ public final class SchemaContextUtil { } } + private static Module findParentModuleOfReferencingType(final SchemaContext schemaContext, + final SchemaNode schemaNode) { + Preconditions.checkArgument(schemaContext != null, "Schema Context reference cannot be NULL!"); + Preconditions.checkArgument(schemaNode != null, "Schema Node cannot be NULL!"); + TypeDefinition nodeType = null; + + if (schemaNode instanceof LeafSchemaNode) { + nodeType = ((LeafSchemaNode) schemaNode).getType(); + } else if (schemaNode instanceof LeafListSchemaNode) { + nodeType = ((LeafListSchemaNode) schemaNode).getType(); + } + + if (nodeType instanceof ExtendedType) { + while (nodeType.getBaseType() instanceof ExtendedType) { + nodeType = nodeType.getBaseType(); + } + + QNameModule typeDefModuleQname = nodeType.getQName().getModule(); + return schemaContext.findModuleByNamespaceAndRevision(typeDefModuleQname.getNamespace(), + typeDefModuleQname.getRevision()); + } else if (nodeType.getBaseType() != null) { + while (nodeType.getBaseType() != null) { + nodeType = nodeType.getBaseType(); + } + + final QNameModule typeDefModuleQname = nodeType.getQName().getModule(); + return schemaContext.findModuleByNamespaceAndRevision(typeDefModuleQname.getNamespace(), + typeDefModuleQname.getRevision()); + } + + return SchemaContextUtil.findParentModule(schemaContext, schemaNode); + } + /** + * @deprecated due to expensive lookup * Returns parent Yang Module for specified Schema Context in which Schema * Node is declared. If Schema Node is of type 'ExtendedType' it tries to find parent module * in which the type was originally declared (needed for correct leafref path resolution).
@@ -684,6 +721,7 @@ public final class SchemaContextUtil { * Schema Node is NOT present, the method will returns * null */ + @Deprecated public static Module findParentModuleByType(final SchemaContext schemaContext, final SchemaNode schemaNode) { Preconditions.checkArgument(schemaContext != null, "Schema Context reference cannot be NULL!"); Preconditions.checkArgument(schemaNode != null, "Schema Node cannot be NULL!"); @@ -695,8 +733,8 @@ public final class SchemaContextUtil { nodeType = ((LeafListSchemaNode) schemaNode).getType(); } - if (nodeType instanceof ExtendedType) { - while (nodeType.getBaseType() instanceof ExtendedType) { + if (!BaseTypes.isYangBuildInType(nodeType) && nodeType.getBaseType() != null) { + while (nodeType.getBaseType() != null && !BaseTypes.isYangBuildInType(nodeType.getBaseType())) { nodeType = nodeType.getBaseType(); } @@ -739,7 +777,7 @@ public final class SchemaContextUtil { } } - private static final Pattern STRIP_PATTERN = Pattern.compile("\\[.*\\]"); + private static final Pattern STRIP_PATTERN = Pattern.compile("\\[[^\\[\\]]*\\]"); /** * Removes conditions from xPath pointed to target node. @@ -749,7 +787,8 @@ public final class SchemaContextUtil { * @return string representation of xPath without conditions * */ - private static String stripConditionsFromXPathString(final RevisionAwareXPath pathStatement) { + @VisibleForTesting + static String stripConditionsFromXPathString(final RevisionAwareXPath pathStatement) { return STRIP_PATTERN.matcher(pathStatement.toString()).replaceAll(""); } @@ -760,7 +799,7 @@ public final class SchemaContextUtil { * a node representing LeafSchemaNode * @return concrete type definition of node value */ - private static TypeDefinition typeDefinition(final LeafSchemaNode node) { + private static TypeDefinition typeDefinition(final LeafSchemaNode node) { TypeDefinition baseType = node.getType(); while (baseType.getBaseType() != null) { baseType = baseType.getBaseType(); @@ -775,7 +814,7 @@ public final class SchemaContextUtil { * a node representing LeafListSchemaNode * @return concrete type definition of node value */ - private static TypeDefinition typeDefinition(final LeafListSchemaNode node) { + private static TypeDefinition typeDefinition(final LeafListSchemaNode node) { TypeDefinition baseType = node.getType(); while (baseType.getBaseType() != null) { baseType = baseType.getBaseType(); @@ -790,13 +829,13 @@ public final class SchemaContextUtil { * a node representing DataSchemaNode * @return concrete type definition of node value */ - private static TypeDefinition typeDefinition(final DataSchemaNode node) { + private static TypeDefinition typeDefinition(final DataSchemaNode node) { if (node instanceof LeafListSchemaNode) { return typeDefinition((LeafListSchemaNode) node); } else if (node instanceof LeafSchemaNode) { return typeDefinition((LeafSchemaNode) node); } else { - throw new IllegalArgumentException("Unhandled parameter types: " + Arrays. asList(node).toString()); + throw new IllegalArgumentException("Unhandled parameter types: " + Collections.singletonList(node).toString()); } } }