X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=yang%2Fyang-parser-impl%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fyangtools%2Fyang%2Fparser%2Fbuilder%2Fimpl%2FBuilderUtils.java;h=759e779eed26e29a079b7e37681515022e8a160a;hb=0082208025b5bb78e648cf20a31ac78b8b9e204c;hp=4a61d7b0f6d0eef9458a16fbc1c3becd97d3976c;hpb=0eb60011b52e4e56c62b47a36eb334f2c3b3ad6a;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 4a61d7b0f6..759e779eed 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 @@ -20,8 +20,6 @@ import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.net.URI; -import java.text.DateFormat; -import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Collection; import java.util.Date; @@ -35,10 +33,13 @@ import java.util.Set; import java.util.TreeMap; import org.antlr.v4.runtime.tree.ParseTree; import org.apache.commons.io.IOUtils; +import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Belongs_to_stmtContext; import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Module_header_stmtsContext; import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Module_stmtContext; import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Namespace_stmtContext; import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Revision_stmtsContext; +import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Submodule_header_stmtsContext; +import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Submodule_stmtContext; import org.opendaylight.yangtools.yang.common.QName; import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode; import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode; @@ -78,9 +79,7 @@ import org.slf4j.LoggerFactory; public final class BuilderUtils { - private static final DateFormat SIMPLE_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd"); private static final Logger LOG = LoggerFactory.getLogger(BuilderUtils.class); - private static final Splitter SLASH_SPLITTER = Splitter.on('/').omitEmptyStrings(); private static final Splitter COLON_SPLITTER = Splitter.on(':'); private static final Date NULL_DATE = new Date(0L); private static final String INPUT = "input"; @@ -473,7 +472,7 @@ public final class BuilderUtils { * Class to be checked * @param optional * Original value - * @return + * @return Optional object with type argument casted as cls */ private static Optional castOptional(final Class cls, final Optional optional) { if (optional.isPresent()) { @@ -635,10 +634,9 @@ public final class BuilderUtils { * Find augment target node and perform augmentation. * * @param augment + * augment builder to process * @param firstNodeParent * parent of first node in path - * @param path - * path to augment target * @return true if augmentation process succeed, false otherwise */ public static boolean processAugmentation(final AugmentationSchemaBuilder augment, @@ -822,11 +820,17 @@ public final class BuilderUtils { public static Map> createYangNamespaceContext( final Collection modules, final Optional context) { - Map> map = new HashMap<>(); + Map> namespaceContext = new HashMap<>(); + Set submodules = new HashSet<>(); + // first read ParseTree collection and separate modules and submodules for (ParseTree module : modules) { for (int i = 0; i < module.getChildCount(); i++) { ParseTree moduleTree = module.getChild(i); - if (moduleTree instanceof Module_stmtContext) { + if (moduleTree instanceof Submodule_stmtContext) { + // put submodule context to separate collection + submodules.add((Submodule_stmtContext) moduleTree); + } else if (moduleTree instanceof Module_stmtContext) { + // get name, revision and namespace from module Module_stmtContext moduleCtx = (Module_stmtContext) moduleTree; final String moduleName = ParserListenerUtils.stringFromNode(moduleCtx); Date rev = null; @@ -854,28 +858,56 @@ public final class BuilderUtils { } } } - TreeMap revToNs = map.get(moduleName); + // update namespaceContext + TreeMap revToNs = namespaceContext.get(moduleName); if (revToNs == null) { revToNs = new TreeMap<>(); revToNs.put(rev, namespace); - map.put(moduleName, revToNs); + namespaceContext.put(moduleName, revToNs); } revToNs.put(rev, namespace); } } } + // after all ParseTree-s are parsed update namespaceContext with modules + // from SchemaContext if (context.isPresent()) { for (Module module : context.get().getModules()) { - TreeMap revToNs = map.get(module.getName()); + TreeMap revToNs = namespaceContext.get(module.getName()); if (revToNs == null) { revToNs = new TreeMap<>(); revToNs.put(module.getRevision(), module.getNamespace()); - map.put(module.getName(), revToNs); + namespaceContext.put(module.getName(), revToNs); } revToNs.put(module.getRevision(), module.getNamespace()); } } - return map; + // when all modules are processed, traverse submodules and update + // namespaceContext with mapping for submodules + for (Submodule_stmtContext submodule : submodules) { + final String moduleName = ParserListenerUtils.stringFromNode(submodule); + for (int i = 0; i < submodule.getChildCount(); i++) { + ParseTree subHeaderCtx = submodule.getChild(i); + if (subHeaderCtx instanceof Submodule_header_stmtsContext) { + for (int j = 0; j < subHeaderCtx.getChildCount(); j++) { + ParseTree belongsCtx = subHeaderCtx.getChild(j); + if (belongsCtx instanceof Belongs_to_stmtContext) { + final String belongsTo = ParserListenerUtils.stringFromNode(belongsCtx); + TreeMap 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<>(); + subNs.put(ns.firstKey(), ns.firstEntry().getValue()); + namespaceContext.put(moduleName, subNs); + } + } + } + } + } + return namespaceContext; } }