X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;ds=sidebyside;f=yang%2Fyang-parser-impl%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fyangtools%2Fyang%2Fparser%2Fbuilder%2Fimpl%2FBuilderUtils.java;h=1c202f61004c5d7ac84a57532571897da679cf80;hb=d417276b1c26ab5029b381657fd0be810970e572;hp=975b683dd64e93e6aa962069a9c4c2eeb0838747;hpb=9d60f0f7441f27336385642333ffb2cb51ac1c38;p=yangtools.git diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/builder/impl/BuilderUtils.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/builder/impl/BuilderUtils.java index 975b683dd6..1c202f6100 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/builder/impl/BuilderUtils.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/builder/impl/BuilderUtils.java @@ -29,6 +29,7 @@ import java.util.Iterator; import java.util.LinkedHashSet; import java.util.List; import java.util.Map; +import java.util.NavigableMap; import java.util.Set; import java.util.TreeMap; import org.antlr.v4.runtime.tree.ParseTree; @@ -83,6 +84,7 @@ public final class BuilderUtils { private static final Date NULL_DATE = new Date(0L); private static final String INPUT = "input"; private static final String OUTPUT = "output"; + private static final String CHILD_NOT_FOUND_IN_NODE_STR = "Child {} not found in node {}"; private BuilderUtils() { } @@ -119,22 +121,6 @@ public final class BuilderUtils { }); } - /** - * Create new SchemaPath from given path and qname. - * - * @param schemaPath - * base path - * @param qname - * one or more qnames added to base path - * @return new SchemaPath from given path and qname - * - * @deprecated Use {@link SchemaPath#createChild(QName...)} instead. - */ - @Deprecated - public static SchemaPath createSchemaPath(final SchemaPath schemaPath, final QName... qname) { - return schemaPath.createChild(qname); - } - /** * Find dependent module based on given prefix * @@ -148,7 +134,7 @@ public final class BuilderUtils { * current line in yang model * @return module builder if found, null otherwise */ - public static ModuleBuilder findModuleFromBuilders(final Map> modules, + public static ModuleBuilder findModuleFromBuilders(final Map> modules, final ModuleBuilder module, final String prefix, final int line) { ModuleBuilder dependentModule; Date dependentModuleRevision; @@ -165,7 +151,7 @@ public final class BuilderUtils { String dependentModuleName = dependentModuleImport.getModuleName(); dependentModuleRevision = dependentModuleImport.getRevision(); - TreeMap moduleBuildersByRevision = modules.get(dependentModuleName); + NavigableMap moduleBuildersByRevision = modules.get(dependentModuleName); if (moduleBuildersByRevision == null) { return null; } @@ -178,15 +164,13 @@ public final class BuilderUtils { return dependentModule; } - public static ModuleBuilder findModuleFromBuilders(ModuleImport imp, Iterable modules) { + public static ModuleBuilder findModuleFromBuilders(final ModuleImport imp, final Iterable modules) { String name = imp.getModuleName(); Date revision = imp.getRevision(); - TreeMap map = new TreeMap<>(); + NavigableMap map = new TreeMap<>(); for (ModuleBuilder module : modules) { - if (module != null) { - if (module.getName().equals(name)) { - map.put(module.getRevision(), module); - } + if (module != null && module.getName().equals(name)) { + map.put(module.getRevision(), module); } } if (map.isEmpty()) { @@ -216,7 +200,7 @@ public final class BuilderUtils { */ public static Module findModuleFromContext(final SchemaContext context, final ModuleBuilder currentModule, final String prefix, final int line) { - TreeMap modulesByRevision = new TreeMap<>(); + NavigableMap modulesByRevision = new TreeMap<>(); ModuleImport dependentModuleImport = currentModule.getImport(prefix); if (dependentModuleImport == null) { @@ -343,7 +327,7 @@ public final class BuilderUtils { /** * Set addedByUses flag to true for node and all its child nodes. * - * @param node + * @param node grouping member node */ public static void setNodeAddedByUses(final GroupingMember node) { node.setAddedByUses(true); @@ -360,6 +344,41 @@ public final class BuilderUtils { } } + /** + * Find builder of schema node under parent builder (including under + * AugmentationSchemaBuilder). + * + * @param path + * - path of target schema node builder + * @param parent + * - base data node container builder under which the target + * schema node builder should be found + * @return builder of schema node + */ + public static SchemaNodeBuilder findTargetNode(final Iterable path, + final DataNodeContainerBuilder parent) { + + Preconditions.checkNotNull(parent); + Preconditions.checkNotNull(path); + + SchemaNodeBuilder foundNode = null; + + final Iterator pathIterator = path.iterator(); + if (pathIterator.hasNext()) { + String name = pathIterator.next().getLocalName(); + foundNode = parent.getDataChildByName(name); + if (foundNode == null) { + foundNode = findUnknownNode(name, parent); + } + } + + if (pathIterator.hasNext() && foundNode != null) { + return findSchemaNode(Iterables.skip(path, 1), foundNode); + } else { + return foundNode; + } + } + public static SchemaNodeBuilder findSchemaNode(final Iterable path, final SchemaNodeBuilder parentNode) { SchemaNodeBuilder node = null; SchemaNodeBuilder parent = parentNode; @@ -378,9 +397,9 @@ public final class BuilderUtils { node = findUnknownNode(name, parent); } } else if (parent instanceof RpcDefinitionBuilder) { - if ("input".equals(name)) { + if (INPUT.equals(name)) { node = ((RpcDefinitionBuilder) parent).getInput(); - } else if ("output".equals(name)) { + } else if (OUTPUT.equals(name)) { node = ((RpcDefinitionBuilder) parent).getOutput(); } else { if (node == null) { @@ -457,9 +476,8 @@ public final class BuilderUtils { return castOptional(SchemaNodeBuilder.class, findCaseInChoice((ChoiceBuilder) parent, child)); } else if (parent instanceof RpcDefinitionBuilder) { return castOptional(SchemaNodeBuilder.class, findContainerInRpc((RpcDefinitionBuilder) parent, child)); - } else { - LOG.trace("Child {} not found in node {}", child, parent); + LOG.trace(CHILD_NOT_FOUND_IN_NODE_STR, child, parent); return Optional.absent(); } } @@ -504,7 +522,7 @@ public final class BuilderUtils { final QName child) { if (INPUT.equals(child.getLocalName())) { if (parent.getInput() == null) { - QName qname = QName.create(parent.getQName().getModule(), "input"); + QName qname = QName.create(parent.getQName().getModule(), INPUT); final ContainerSchemaNodeBuilder inputBuilder = new ContainerSchemaNodeBuilder(parent.getModuleName(), parent.getLine(), qname, parent.getPath().createChild(qname)); inputBuilder.setParent(parent); @@ -514,7 +532,7 @@ public final class BuilderUtils { return Optional.of(parent.getInput()); } else if (OUTPUT.equals(child.getLocalName())) { if (parent.getOutput() == null) { - QName qname = QName.create(parent.getQName().getModule(), "output"); + QName qname = QName.create(parent.getQName().getModule(), OUTPUT); final ContainerSchemaNodeBuilder outputBuilder = new ContainerSchemaNodeBuilder(parent.getModuleName(), parent.getLine(), qname, parent.getPath().createChild(qname)); outputBuilder.setParent(parent); @@ -523,7 +541,7 @@ public final class BuilderUtils { } return Optional.of(parent.getOutput()); } - LOG.trace("Child {} not found in node {}", child, parent); + LOG.trace(CHILD_NOT_FOUND_IN_NODE_STR, child, parent); return Optional.absent(); } @@ -544,7 +562,7 @@ public final class BuilderUtils { return Optional.of(caze); } } - LOG.trace("Child {} not found in node {}", child, parent); + LOG.trace(CHILD_NOT_FOUND_IN_NODE_STR, child, parent); return Optional.absent(); } @@ -565,7 +583,7 @@ public final class BuilderUtils { return Optional.of(childNode); } } - LOG.trace("Child {} not found in node {}", child, parent); + LOG.trace(CHILD_NOT_FOUND_IN_NODE_STR, child, parent); return Optional.absent(); } @@ -625,7 +643,7 @@ public final class BuilderUtils { return Optional. of(childNode); } } - LOG.trace("Child {} not found in node {}", child, builder); + LOG.trace(CHILD_NOT_FOUND_IN_NODE_STR, child, builder); return Optional.absent(); } @@ -694,7 +712,7 @@ public final class BuilderUtils { /** * Get module in which this node is defined. * - * @param node + * @param node node * @return builder of module where this node is defined */ public static ModuleBuilder getParentModule(final Builder node) { @@ -730,19 +748,19 @@ public final class BuilderUtils { final SchemaPath schemaPath = parentPath.createChild(qname); if (node instanceof AnyXmlSchemaNode) { - return new AnyXmlBuilder(moduleName, line, qname, schemaPath, ((AnyXmlSchemaNode) node)); + return new AnyXmlBuilder(moduleName, line, qname, schemaPath, (AnyXmlSchemaNode) node); } else if (node instanceof ChoiceSchemaNode) { - return new ChoiceBuilder(moduleName, line, qname, schemaPath, ((ChoiceSchemaNode) node)); + return new ChoiceBuilder(moduleName, line, qname, schemaPath, (ChoiceSchemaNode) node); } else if (node instanceof ContainerSchemaNode) { - return new ContainerSchemaNodeBuilder(moduleName, line, qname, schemaPath, ((ContainerSchemaNode) node)); + return new ContainerSchemaNodeBuilder(moduleName, line, qname, schemaPath, (ContainerSchemaNode) node); } else if (node instanceof LeafSchemaNode) { - return new LeafSchemaNodeBuilder(moduleName, line, qname, schemaPath, ((LeafSchemaNode) node)); + return new LeafSchemaNodeBuilder(moduleName, line, qname, schemaPath, (LeafSchemaNode) node); } else if (node instanceof LeafListSchemaNode) { - return new LeafListSchemaNodeBuilder(moduleName, line, qname, schemaPath, ((LeafListSchemaNode) node)); + return new LeafListSchemaNodeBuilder(moduleName, line, qname, schemaPath, (LeafListSchemaNode) node); } else if (node instanceof ListSchemaNode) { - return new ListSchemaNodeBuilder(moduleName, line, qname, schemaPath, ((ListSchemaNode) node)); + return new ListSchemaNodeBuilder(moduleName, line, qname, schemaPath, (ListSchemaNode) node); } else if (node instanceof ChoiceCaseNode) { - return new ChoiceCaseBuilder(moduleName, line, qname, schemaPath, ((ChoiceCaseNode) node)); + return new ChoiceCaseBuilder(moduleName, line, qname, schemaPath, (ChoiceCaseNode) node); } else { throw new YangParseException(moduleName, line, "Failed to copy node: Unknown type of DataSchemaNode: " + node); @@ -767,7 +785,7 @@ public final class BuilderUtils { for (TypeDefinition node : nodes) { QName qname = QName.create(parentQName, node.getQName().getLocalName()); SchemaPath schemaPath = parentPath.createChild(qname); - result.add(new TypeDefinitionBuilderImpl(moduleName, line, qname, schemaPath, ((ExtendedType) node))); + result.add(new TypeDefinitionBuilderImpl(moduleName, line, qname, schemaPath, (ExtendedType) node)); } return result; } @@ -806,8 +824,8 @@ public final class BuilderUtils { } } - public static ModuleBuilder findModule(final QName qname, final Map> modules) { - TreeMap map = modules.get(qname.getNamespace()); + public static ModuleBuilder findModule(final QName qname, final Map> modules) { + NavigableMap map = modules.get(qname.getNamespace()); if (map == null) { return null; } @@ -817,9 +835,9 @@ public final class BuilderUtils { return map.get(qname.getRevision()); } - public static Map> createYangNamespaceContext( + public static Map> createYangNamespaceContext( final Collection modules, final Optional context) { - Map> namespaceContext = new HashMap<>(); + Map> namespaceContext = new HashMap<>(); Set submodules = new HashSet<>(); // first read ParseTree collection and separate modules and submodules for (ParseTree module : modules) { @@ -858,7 +876,7 @@ public final class BuilderUtils { } } // update namespaceContext - TreeMap revToNs = namespaceContext.get(moduleName); + NavigableMap revToNs = namespaceContext.get(moduleName); if (revToNs == null) { revToNs = new TreeMap<>(); revToNs.put(rev, namespace); @@ -872,7 +890,7 @@ public final class BuilderUtils { // from SchemaContext if (context.isPresent()) { for (Module module : context.get().getModules()) { - TreeMap revToNs = namespaceContext.get(module.getName()); + NavigableMap revToNs = namespaceContext.get(module.getName()); if (revToNs == null) { revToNs = new TreeMap<>(); revToNs.put(module.getRevision(), module.getNamespace()); @@ -892,13 +910,13 @@ public final class BuilderUtils { ParseTree belongsCtx = subHeaderCtx.getChild(j); if (belongsCtx instanceof Belongs_to_stmtContext) { final String belongsTo = ParserListenerUtils.stringFromNode(belongsCtx); - TreeMap ns = namespaceContext.get(belongsTo); + NavigableMap ns = namespaceContext.get(belongsTo); if (ns == null) { throw new YangParseException(moduleName, submodule.getStart().getLine(), String.format( "Unresolved belongs-to statement: %s", belongsTo)); } // submodule get namespace and revision from module - TreeMap subNs = new TreeMap<>(); + NavigableMap subNs = new TreeMap<>(); subNs.put(ns.firstKey(), ns.firstEntry().getValue()); namespaceContext.put(moduleName, subNs); }