package org.opendaylight.yangtools.yang.parser.impl;
import static com.google.common.base.Preconditions.checkNotNull;
-import static org.opendaylight.yangtools.yang.parser.util.ParserUtils.fillAugmentTarget;
-import static org.opendaylight.yangtools.yang.parser.util.ParserUtils.findBaseIdentity;
-import static org.opendaylight.yangtools.yang.parser.util.ParserUtils.findModuleFromBuilders;
-import static org.opendaylight.yangtools.yang.parser.util.ParserUtils.findModuleFromContext;
-import static org.opendaylight.yangtools.yang.parser.util.ParserUtils.findSchemaNode;
-import static org.opendaylight.yangtools.yang.parser.util.ParserUtils.findSchemaNodeInModule;
-import static org.opendaylight.yangtools.yang.parser.util.ParserUtils.processAugmentation;
-import static org.opendaylight.yangtools.yang.parser.util.ParserUtils.setNodeAddedByUses;
-import static org.opendaylight.yangtools.yang.parser.util.ParserUtils.wrapChildNode;
-import static org.opendaylight.yangtools.yang.parser.util.ParserUtils.wrapChildNodes;
-import static org.opendaylight.yangtools.yang.parser.util.ParserUtils.wrapGroupings;
-import static org.opendaylight.yangtools.yang.parser.util.ParserUtils.wrapTypedefs;
-import static org.opendaylight.yangtools.yang.parser.util.ParserUtils.wrapUnknownNodes;
-import static org.opendaylight.yangtools.yang.parser.util.TypeUtils.resolveType;
-import static org.opendaylight.yangtools.yang.parser.util.TypeUtils.resolveTypeUnion;
-import static org.opendaylight.yangtools.yang.parser.util.TypeUtils.resolveTypeUnionWithContext;
-import static org.opendaylight.yangtools.yang.parser.util.TypeUtils.resolveTypeWithContext;
+import static org.opendaylight.yangtools.yang.parser.builder.impl.BuilderUtils.fillAugmentTarget;
+import static org.opendaylight.yangtools.yang.parser.builder.impl.BuilderUtils.findBaseIdentity;
+import static org.opendaylight.yangtools.yang.parser.builder.impl.BuilderUtils.findModuleFromBuilders;
+import static org.opendaylight.yangtools.yang.parser.builder.impl.BuilderUtils.findModuleFromContext;
+import static org.opendaylight.yangtools.yang.parser.builder.impl.BuilderUtils.findSchemaNode;
+import static org.opendaylight.yangtools.yang.parser.builder.impl.BuilderUtils.findSchemaNodeInModule;
+import static org.opendaylight.yangtools.yang.parser.builder.impl.BuilderUtils.processAugmentation;
+import static org.opendaylight.yangtools.yang.parser.builder.impl.BuilderUtils.setNodeAddedByUses;
+import static org.opendaylight.yangtools.yang.parser.builder.impl.BuilderUtils.wrapChildNode;
+import static org.opendaylight.yangtools.yang.parser.builder.impl.BuilderUtils.wrapChildNodes;
+import static org.opendaylight.yangtools.yang.parser.builder.impl.BuilderUtils.wrapGroupings;
+import static org.opendaylight.yangtools.yang.parser.builder.impl.BuilderUtils.wrapTypedefs;
+import static org.opendaylight.yangtools.yang.parser.builder.impl.BuilderUtils.wrapUnknownNodes;
+import static org.opendaylight.yangtools.yang.parser.builder.impl.TypeUtils.resolveType;
+import static org.opendaylight.yangtools.yang.parser.builder.impl.TypeUtils.resolveTypeUnion;
+import static org.opendaylight.yangtools.yang.parser.builder.impl.TypeUtils.resolveTypeUnionWithContext;
+import static org.opendaylight.yangtools.yang.parser.builder.impl.TypeUtils.resolveTypeWithContext;
+
+import com.google.common.base.Optional;
+import com.google.common.base.Preconditions;
+import com.google.common.base.Splitter;
+import com.google.common.collect.HashBiMap;
+import com.google.common.io.ByteSource;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
-import java.net.URI;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
+import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.TreeMap;
+import javax.annotation.concurrent.Immutable;
+
import org.antlr.v4.runtime.ANTLRInputStream;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.tree.ParseTree;
import org.opendaylight.yangtools.yang.parser.builder.api.Builder;
import org.opendaylight.yangtools.yang.parser.builder.api.DataNodeContainerBuilder;
import org.opendaylight.yangtools.yang.parser.builder.api.DataSchemaNodeBuilder;
+import org.opendaylight.yangtools.yang.parser.builder.api.ExtensionBuilder;
import org.opendaylight.yangtools.yang.parser.builder.api.GroupingBuilder;
import org.opendaylight.yangtools.yang.parser.builder.api.SchemaNodeBuilder;
import org.opendaylight.yangtools.yang.parser.builder.api.TypeAwareBuilder;
import org.opendaylight.yangtools.yang.parser.builder.api.TypeDefinitionBuilder;
+import org.opendaylight.yangtools.yang.parser.builder.api.UnknownSchemaNodeBuilder;
import org.opendaylight.yangtools.yang.parser.builder.api.UsesNodeBuilder;
+import org.opendaylight.yangtools.yang.parser.builder.impl.BuilderUtils;
import org.opendaylight.yangtools.yang.parser.builder.impl.ChoiceBuilder;
import org.opendaylight.yangtools.yang.parser.builder.impl.ChoiceCaseBuilder;
import org.opendaylight.yangtools.yang.parser.builder.impl.DeviationBuilder;
-import org.opendaylight.yangtools.yang.parser.builder.impl.ExtensionBuilder;
+import org.opendaylight.yangtools.yang.parser.builder.impl.GroupingUtils;
import org.opendaylight.yangtools.yang.parser.builder.impl.IdentitySchemaNodeBuilder;
import org.opendaylight.yangtools.yang.parser.builder.impl.IdentityrefTypeBuilder;
import org.opendaylight.yangtools.yang.parser.builder.impl.ModuleBuilder;
-import org.opendaylight.yangtools.yang.parser.builder.impl.ModuleBuilder.ModuleImpl;
+import org.opendaylight.yangtools.yang.parser.builder.impl.ModuleImpl;
import org.opendaylight.yangtools.yang.parser.builder.impl.UnionTypeBuilder;
-import org.opendaylight.yangtools.yang.parser.builder.impl.UnknownSchemaNodeBuilder;
-import org.opendaylight.yangtools.yang.parser.util.Comparators;
-import org.opendaylight.yangtools.yang.parser.util.GroupingSort;
-import org.opendaylight.yangtools.yang.parser.util.GroupingUtils;
+import org.opendaylight.yangtools.yang.parser.builder.impl.UnknownSchemaNodeBuilderImpl;
+import org.opendaylight.yangtools.yang.parser.builder.util.Comparators;
import org.opendaylight.yangtools.yang.parser.util.ModuleDependencySort;
import org.opendaylight.yangtools.yang.parser.util.NamedByteArrayInputStream;
import org.opendaylight.yangtools.yang.parser.util.NamedFileInputStream;
import org.opendaylight.yangtools.yang.parser.util.NamedInputStream;
-import org.opendaylight.yangtools.yang.parser.util.ParserUtils;
import org.opendaylight.yangtools.yang.parser.util.YangParseException;
-import org.opendaylight.yangtools.yang.validator.YangModelBasicValidator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import com.google.common.base.Optional;
-import com.google.common.base.Preconditions;
-import com.google.common.collect.HashBiMap;
-import com.google.common.io.ByteSource;
-
+@Immutable
public final class YangParserImpl implements YangContextParser {
private static final Logger LOG = LoggerFactory.getLogger(YangParserImpl.class);
-
private static final String FAIL_DEVIATION_TARGET = "Failed to find deviation target.";
+ private static final Splitter COLON_SPLITTER = Splitter.on(':');
+ private static final YangParserImpl INSTANCE = new YangParserImpl();
+
+ public static YangParserImpl getInstance() {
+ return INSTANCE;
+ }
@Override
@Deprecated
final String[] fileList = checkNotNull(directory.list(), directory + " not found or is not a directory");
Map<ByteSource, File> sourceToFile = new LinkedHashMap<>();
- ByteSource mainFileSource = ParserUtils.fileToByteSource(yangFile);
+ ByteSource mainFileSource = BuilderUtils.fileToByteSource(yangFile);
sourceToFile.put(mainFileSource, yangFile);
for (String fileName : fileList) {
}
File dependency = new File(directory, fileName);
if (dependency.isFile()) {
- sourceToFile.put(ParserUtils.fileToByteSource(dependency), dependency);
+ sourceToFile.put(BuilderUtils.fileToByteSource(dependency), dependency);
}
}
return resolveSchemaContext(Collections.<Module> emptySet());
}
- Collection<ByteSource> sources = ParserUtils.filesToByteSources(yangFiles);
- SchemaContext result = parseSources(sources, context);
- return result;
+ Collection<ByteSource> sources = BuilderUtils.filesToByteSources(yangFiles);
+ return parseSources(sources, context);
}
@Override
@Deprecated
public Set<Module> parseYangModelsFromStreams(final List<InputStream> yangModelStreams) {
try {
- Collection<ByteSource> sources = ParserUtils.streamsToByteSources(yangModelStreams);
+ Collection<ByteSource> sources = BuilderUtils.streamsToByteSources(yangModelStreams);
return parseSources(sources).getModules();
} catch (IOException | YangSyntaxErrorException e) {
throw new YangParseException("Failed to parse yang data", e);
@Deprecated
public Set<Module> parseYangModelsFromStreams(final List<InputStream> yangModelStreams, final SchemaContext context) {
try {
- Collection<ByteSource> sources = ParserUtils.streamsToByteSources(yangModelStreams);
+ Collection<ByteSource> sources = BuilderUtils.streamsToByteSources(yangModelStreams);
return parseSources(sources, context).getModules();
} catch (IOException | YangSyntaxErrorException e) {
throw new YangParseException("Failed to parse yang data", e);
Map<ByteSource, ModuleBuilder> sourceToBuilder = resolveSources(sources);
// sort and check for duplicates
List<ModuleBuilder> sorted = ModuleDependencySort.sort(sourceToBuilder.values());
- ParserUtils.setSourceToBuilder(sourceToBuilder);
Map<String, TreeMap<Date, ModuleBuilder>> modules = orderModules(sorted);
Map<ModuleBuilder, Module> builderToModule = build(modules);
Map<ModuleBuilder, ByteSource> builderToSource = HashBiMap.create(sourceToBuilder).inverse();
* parsed from stream
* @throws YangSyntaxErrorException
*/
- private Map<ByteSource, ModuleBuilder> resolveSources(final Collection<ByteSource> streams) throws IOException,
- YangSyntaxErrorException {
+ // TODO: remove ByteSource result after removing YangModelParser
+ private Map<ByteSource, ModuleBuilder> resolveSources(final Collection<ByteSource> streams)
+ throws IOException, YangSyntaxErrorException {
Map<ByteSource, ModuleBuilder> builders = parseSourcesToBuilders(streams);
- Map<ByteSource, ModuleBuilder> result = resolveSubmodules(builders);
- return result;
+ return resolveSubmodules(builders);
}
private Map<ByteSource, ModuleBuilder> parseSourcesToBuilders(final Collection<ByteSource> sources)
YangParserListenerImpl yangModelParser;
for (Map.Entry<ByteSource, ParseTree> entry : sourceToTree.entrySet()) {
ByteSource source = entry.getKey();
- String path = null;
- InputStream stream = source.openStream();
- if (stream instanceof NamedInputStream) {
- path = stream.toString();
- }
- try {
- stream.close();
- } catch (IOException e) {
- LOG.warn("Failed to close stream {}", stream);
+ String path = null; // TODO refactor to Optional
+ //TODO refactor so that path can be retrieved without opening stream: NamedInputStream -> NamedByteSource ?
+ try(InputStream stream = source.openStream()) {
+ if (stream instanceof NamedInputStream) {
+ path = stream.toString();
+ }
}
-
yangModelParser = new YangParserListenerImpl(path);
walker.walk(yangModelParser, entry.getValue());
ModuleBuilder moduleBuilder = yangModelParser.getModuleBuilder();
+ moduleBuilder.setSource(source);
sourceToBuilder.put(source, moduleBuilder);
}
-
- ParserUtils.setSourceToBuilder(sourceToBuilder);
return sourceToBuilder;
}
}
}
+ /**
+ * Mini parser: This parsing context does not validate full YANG module, only
+ * parses header up to the revisions and imports.
+ * @see org.opendaylight.yangtools.yang.parser.impl.util.YangModelDependencyInfo
+ */
public static YangContext parseStreamWithoutErrorListeners(final InputStream yangStream) {
YangContext result = null;
try {
*/
private void setCorrectAugmentTargetPath(final Map<String, TreeMap<Date, ModuleBuilder>> modules,
final AugmentationSchemaBuilder augment, final SchemaContext context) {
- ModuleBuilder module = ParserUtils.getParentModule(augment);
+ ModuleBuilder module = BuilderUtils.getParentModule(augment);
SchemaPath oldSchemaPath = augment.getTargetPath();
- List<QName> oldPath = oldSchemaPath.getPath();
List<QName> newPath = new ArrayList<>();
Builder parent = augment.getParent();
DataNodeContainerBuilder usesParent = ((UsesNodeBuilder) parent).getParent();
newPath.addAll(usesParent.getPath().getPath());
- URI ns;
- Date revision;
- String prefix;
- QName baseQName = usesParent.getQName();
+ final QName baseQName = usesParent.getQName();
+ final QNameModule qnm;
+ final String prefix;
if (baseQName == null) {
- ModuleBuilder m = ParserUtils.getParentModule(usesParent);
- ns = m.getNamespace();
- revision = m.getRevision();
+ ModuleBuilder m = BuilderUtils.getParentModule(usesParent);
+ qnm = m.getQNameModule();
prefix = m.getPrefix();
} else {
- ns = baseQName.getNamespace();
- revision = baseQName.getRevision();
+ qnm = baseQName.getModule();
prefix = baseQName.getPrefix();
}
- final QNameModule qm = QNameModule.create(ns, revision);
- for (QName qn : oldSchemaPath.getPath()) {
- newPath.add(QName.create(qm, prefix, qn.getLocalName()));
+ for (QName qn : oldSchemaPath.getPathFromRoot()) {
+ newPath.add(QName.create(qnm, prefix, qn.getLocalName()));
}
} else {
+ Iterable<QName> oldPath = oldSchemaPath.getPathFromRoot();
for (QName qn : oldPath) {
- URI ns = module.getNamespace();
- Date rev = module.getRevision();
+ QNameModule qnm = module.getQNameModule();
String localPrefix = qn.getPrefix();
- if (localPrefix != null && !("".equals(localPrefix))) {
- ModuleBuilder currentModule = ParserUtils.findModuleFromBuilders(modules, module, localPrefix,
+ if (localPrefix != null && !localPrefix.isEmpty()) {
+ ModuleBuilder currentModule = BuilderUtils.findModuleFromBuilders(modules, module, localPrefix,
augment.getLine());
if (currentModule == null) {
- Module m = ParserUtils.findModuleFromContext(context, module, localPrefix, augment.getLine());
+ Module m = BuilderUtils.findModuleFromContext(context, module, localPrefix, augment.getLine());
if (m == null) {
throw new YangParseException(module.getName(), augment.getLine(), "Module with prefix "
+ localPrefix + " not found.");
}
- ns = m.getNamespace();
- rev = m.getRevision();
+ qnm = m.getQNameModule();
} else {
- ns = currentModule.getNamespace();
- rev = currentModule.getRevision();
+ qnm = currentModule.getQNameModule();
}
}
- newPath.add(new QName(ns, rev, localPrefix, qn.getLocalName()));
+ newPath.add(new QName(qnm.getNamespace(), qnm.getRevision(), localPrefix, qn.getLocalName()));
}
}
augment.setTargetNodeSchemaPath(SchemaPath.create(newPath, true));
private void checkAugmentMandatoryNodes(final Collection<AugmentationSchemaBuilder> augments) {
for (AugmentationSchemaBuilder augment : augments) {
String augmentPrefix = augment.getTargetPath().getPathFromRoot().iterator().next().getPrefix();
- ModuleBuilder module = ParserUtils.getParentModule(augment);
+ ModuleBuilder module = BuilderUtils.getParentModule(augment);
String modulePrefix = module.getPrefix();
if (augmentPrefix == null || augmentPrefix.isEmpty() || augmentPrefix.equals(modulePrefix)) {
*/
private ModuleBuilder findTargetModule(final QName qname, final ModuleBuilder module,
final Map<String, TreeMap<Date, ModuleBuilder>> modules, final SchemaContext context, final int line) {
- ModuleBuilder targetModule = null;
+ ModuleBuilder targetModule;
String prefix = qname.getPrefix();
- if (prefix == null || prefix.equals("")) {
+ if (prefix == null || prefix.isEmpty()) {
targetModule = module;
} else {
targetModule = findModuleFromBuilders(modules, module, qname.getPrefix(), line);
private ModuleBuilder findTargetModule(final String prefix, final ModuleBuilder module,
final Map<String, TreeMap<Date, ModuleBuilder>> modules, final SchemaContext context, final int line) {
- ModuleBuilder targetModule = null;
+ ModuleBuilder targetModule;
if (prefix == null || prefix.equals("")) {
targetModule = module;
if (baseIdentityName != null) {
IdentitySchemaNodeBuilder result = null;
- if (baseIdentityName.contains(":")) {
- String[] splittedBase = baseIdentityName.split(":");
- if (splittedBase.length > 2) {
+ if (baseIdentityName.indexOf(':') != -1) {
+ final Iterator<String> split = COLON_SPLITTER.split(baseIdentityName).iterator();
+ final String prefix = split.next();
+ final String name = split.next();
+ if (split.hasNext()) {
throw new YangParseException(module.getName(), line,
"Failed to parse identityref base: " + baseIdentityName);
}
- String prefix = splittedBase[0];
- String name = splittedBase[1];
+
ModuleBuilder dependentModule = findTargetModule(prefix, module, modules, context, line);
if (dependentModule != null) {
- result = ParserUtils.findIdentity(dependentModule.getAddedIdentities(), name);
+ result = BuilderUtils.findIdentity(dependentModule.getAddedIdentities(), name);
}
} else {
- result = ParserUtils.findIdentity(module.getAddedIdentities(), baseIdentityName);
+ result = BuilderUtils.findIdentity(module.getAddedIdentities(), baseIdentityName);
}
identity.setBaseIdentity(result);
}
}
}
for (UsesNodeBuilder usesNode : allUses) {
- ModuleBuilder module = ParserUtils.getParentModule(usesNode);
+ ModuleBuilder module = BuilderUtils.getParentModule(usesNode);
final GroupingBuilder targetGroupingBuilder = GroupingUtils.getTargetGroupingFromModules(usesNode, modules,
module);
if (targetGroupingBuilder == null) {
final SchemaContext context) {
if (!usesNode.isResolved()) {
DataNodeContainerBuilder parent = usesNode.getParent();
- ModuleBuilder module = ParserUtils.getParentModule(parent);
+ ModuleBuilder module = BuilderUtils.getParentModule(parent);
GroupingBuilder target = GroupingUtils.getTargetGroupingFromModules(usesNode, modules, module);
if (target == null) {
resolveUsesWithContext(usesNode);
private void resolveUsesWithContext(final UsesNodeBuilder usesNode) {
final int line = usesNode.getLine();
DataNodeContainerBuilder parent = usesNode.getParent();
- ModuleBuilder module = ParserUtils.getParentModule(parent);
+ ModuleBuilder module = BuilderUtils.getParentModule(parent);
SchemaPath parentPath;
- URI ns = null;
- Date rev = null;
- String pref = null;
+
+ final QName parentQName;
if (parent instanceof AugmentationSchemaBuilder || parent instanceof ModuleBuilder) {
- ns = module.getNamespace();
- rev = module.getRevision();
- pref = module.getPrefix();
+ parentQName = QName.create(module.getQNameModule(), module.getPrefix(), "dummy");
if (parent instanceof AugmentationSchemaBuilder) {
parentPath = ((AugmentationSchemaBuilder) parent).getTargetNodeSchemaPath();
} else {
- parentPath = ((ModuleBuilder) parent).getPath();
+ parentPath = parent.getPath();
}
} else {
- ns = ((DataSchemaNodeBuilder) parent).getQName().getNamespace();
- rev = ((DataSchemaNodeBuilder) parent).getQName().getRevision();
- pref = ((DataSchemaNodeBuilder) parent).getQName().getPrefix();
- parentPath = ((DataSchemaNodeBuilder) parent).getPath();
+ parentQName = parent.getQName();
+ parentPath = parent.getPath();
}
GroupingDefinition gd = usesNode.getGroupingDefinition();
Set<DataSchemaNodeBuilder> childNodes = wrapChildNodes(module.getModuleName(), line, gd.getChildNodes(),
- parentPath, ns, rev, pref);
+ parentPath, parentQName);
parent.getChildNodeBuilders().addAll(childNodes);
for (DataSchemaNodeBuilder childNode : childNodes) {
setNodeAddedByUses(childNode);
}
- Set<TypeDefinitionBuilder> typedefs = wrapTypedefs(module.getModuleName(), line, gd, parentPath, ns, rev, pref);
+ Set<TypeDefinitionBuilder> typedefs = wrapTypedefs(module.getModuleName(), line, gd, parentPath, parentQName);
parent.getTypeDefinitionBuilders().addAll(typedefs);
for (TypeDefinitionBuilder typedef : typedefs) {
setNodeAddedByUses(typedef);
}
Set<GroupingBuilder> groupings = wrapGroupings(module.getModuleName(), line, usesNode.getGroupingDefinition()
- .getGroupings(), parentPath, ns, rev, pref);
+ .getGroupings(), parentPath, parentQName);
parent.getGroupingBuilders().addAll(groupings);
for (GroupingBuilder gb : groupings) {
setNodeAddedByUses(gb);
}
- List<UnknownSchemaNodeBuilder> unknownNodes = wrapUnknownNodes(module.getModuleName(), line,
- gd.getUnknownSchemaNodes(), parentPath, ns, rev, pref);
+ List<UnknownSchemaNodeBuilderImpl> unknownNodes = wrapUnknownNodes(module.getModuleName(), line,
+ gd.getUnknownSchemaNodes(), parentPath, parentQName);
parent.getUnknownNodes().addAll(unknownNodes);
for (UnknownSchemaNodeBuilder un : unknownNodes) {
un.setAddedByUses(true);