X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fsal%2Fyang-prototype%2Fyang%2Fyang-model-util%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fyang%2Fmodel%2Futil%2FSchemaContextUtil.java;h=13c2ba7cb55455800e763c754f53883d8e0986eb;hb=ff1b4a79cca00743a00c3b0b1100bd0ab2b2fb31;hp=39865a9ddf4e00eb3a9977687dfdfaa6411a7484;hpb=fb99d767417ba5a92c24412aa15ddb56f1f292a3;p=controller.git diff --git a/opendaylight/sal/yang-prototype/yang/yang-model-util/src/main/java/org/opendaylight/controller/yang/model/util/SchemaContextUtil.java b/opendaylight/sal/yang-prototype/yang/yang-model-util/src/main/java/org/opendaylight/controller/yang/model/util/SchemaContextUtil.java index 39865a9ddf..13c2ba7cb5 100644 --- a/opendaylight/sal/yang-prototype/yang/yang-model-util/src/main/java/org/opendaylight/controller/yang/model/util/SchemaContextUtil.java +++ b/opendaylight/sal/yang-prototype/yang/yang-model-util/src/main/java/org/opendaylight/controller/yang/model/util/SchemaContextUtil.java @@ -24,6 +24,7 @@ import org.opendaylight.controller.yang.model.api.RevisionAwareXPath; import org.opendaylight.controller.yang.model.api.SchemaContext; import org.opendaylight.controller.yang.model.api.SchemaNode; import org.opendaylight.controller.yang.model.api.SchemaPath; +import org.opendaylight.controller.yang.model.api.TypeDefinition; public final class SchemaContextUtil { @@ -32,7 +33,7 @@ public final class SchemaContextUtil { public static DataSchemaNode findDataSchemaNode(final SchemaContext context, final SchemaPath schemaPath) { if (schemaPath != null) { final Module module = resolveModuleFromSchemaPath(context, schemaPath); - final Queue prefixedPath = new LinkedList(schemaPath.getPath()); + final Queue prefixedPath = new LinkedList<>(schemaPath.getPath()); if ((module != null) && (prefixedPath != null)) { return findSchemaNodeForGivenPath(context, module, prefixedPath); @@ -54,7 +55,7 @@ public final class SchemaContextUtil { final Queue qnamedPath = xpathToQNamePath(context, module, strXPath); if (qnamedPath != null) { - final DataSchemaNode dataNode = findSchemaNodeForGivenPath(context, + final DataSchemaNode dataNode = findSchemaNodeForGivenPath(context, module, qnamedPath); return dataNode; } @@ -64,7 +65,7 @@ public final class SchemaContextUtil { return null; } - public static DataSchemaNode findDataSchemaNodeForRelativeXPath(final SchemaContext context, + public static DataSchemaNode findDataSchemaNodeForRelativeXPath(final SchemaContext context, final Module module, final SchemaNode actualSchemaNode, final RevisionAwareXPath relativeXPath) { if ((actualSchemaNode != null) && (relativeXPath != null) @@ -72,11 +73,11 @@ public final class SchemaContextUtil { final SchemaPath actualNodePath = actualSchemaNode.getPath(); if (actualNodePath != null) { - final Queue qnamePath = resolveRelativeXPath(context, module, + final Queue qnamePath = resolveRelativeXPath(context, module, relativeXPath, actualNodePath); if (qnamePath != null) { - final DataSchemaNode dataNode = findSchemaNodeForGivenPath(context, + final DataSchemaNode dataNode = findSchemaNodeForGivenPath(context, module, qnamePath); return dataNode; } @@ -86,13 +87,41 @@ public final class SchemaContextUtil { return null; } - public static Module resolveModuleFromSchemaPath(final SchemaContext context, final SchemaPath schemaPath) { + private static Module resolveModuleFromSchemaPath(final SchemaContext + context, final SchemaPath schemaPath) { if ((schemaPath != null) && (schemaPath.getPath() != null)) { - final QName qname = schemaPath.getPath().get(0); + final List path = schemaPath.getPath(); + if (!path.isEmpty()) { + final QName qname = path.get(path.size() - 1); - if ((qname != null) && (qname.getNamespace() != null)) { - return context.findModuleByNamespace(qname.getNamespace()); + if ((qname != null) && (qname.getNamespace() != null)) { + return context.findModuleByNamespace(qname.getNamespace()); + } + } + } + return null; + } + + public static Module findParentModuleForTypeDefinition( + final SchemaContext context, final TypeDefinition type) { + final SchemaPath schemaPath = type.getPath(); + if ((schemaPath != null) && (schemaPath.getPath() != null)) { + if(type instanceof ExtendedType) { + List path = schemaPath.getPath(); + final QName qname = path.get(path.size() - 1); + + if ((qname != null) && (qname.getNamespace() != null)) { + return context.findModuleByNamespace(qname.getNamespace()); + } + } else { + List path = schemaPath.getPath(); + final QName qname = path.get(path.size() - 2); + + if ((qname != null) && (qname.getNamespace() != null)) { + return context.findModuleByNamespace(qname.getNamespace()); + } } + } return null; } @@ -116,7 +145,7 @@ public final class SchemaContextUtil { "The Schema Path MUST contain at least ONE QName which defines namespace and Local name" + "of path."); } - final QName qname = qnamedPath.get(0); + final QName qname = qnamedPath.get(qnamedPath.size() - 1); return context.findModuleByNamespace(qname.getNamespace()); } @@ -133,7 +162,7 @@ public final class SchemaContextUtil { childNodeQName = qnamedPath.peek(); if (childNodeQName != null) { final URI childNodeNamespace = childNodeQName.getNamespace(); - + schemaNode = nextNode.getDataChildByName(childNodeQName); if (schemaNode != null) { if (schemaNode instanceof ContainerSchemaNode) { @@ -160,7 +189,7 @@ public final class SchemaContextUtil { private static Queue xpathToQNamePath(final SchemaContext context, final Module parentModule, final String xpath) { - final Queue path = new LinkedList(); + final Queue path = new LinkedList<>(); if (xpath != null) { final String[] prefixedPath = xpath.split("/"); @@ -214,7 +243,7 @@ public final class SchemaContextUtil { private static Queue resolveRelativeXPath(final SchemaContext context, final Module module, final RevisionAwareXPath relativeXPath, final SchemaPath leafrefSchemaPath) { - final Queue absolutePath = new LinkedList(); + final Queue absolutePath = new LinkedList<>(); if ((module != null) && (relativeXPath != null) && !relativeXPath.isAbsolute() && (leafrefSchemaPath != null)) { @@ -229,7 +258,7 @@ public final class SchemaContextUtil { } final List path = leafrefSchemaPath.getPath(); if (path != null) { - int lenght = path.size() - colCount; + int lenght = path.size() - colCount - 1; for (int i = 0; i < lenght; ++i) { absolutePath.add(path.get(i)); }