import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
+import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.opendaylight.yangtools.antlrv4.code.gen.YangParser;
import org.opendaylight.yangtools.yang.common.QName;
import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
+import org.opendaylight.yangtools.yang.model.api.ExtensionDefinition;
import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
import org.opendaylight.yangtools.yang.model.api.Module;
import org.opendaylight.yangtools.yang.parser.builder.api.TypeDefinitionBuilder;
import org.opendaylight.yangtools.yang.parser.builder.api.UsesNodeBuilder;
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.IdentitySchemaNodeBuilder;
import org.opendaylight.yangtools.yang.parser.builder.impl.IdentityrefTypeBuilder;
import org.opendaylight.yangtools.yang.parser.builder.impl.ModuleBuilder;
@Override
public Set<Module> parseYangModels(final List<File> yangFiles, final SchemaContext context) {
- // TODO
- throw new YangParseException("Not yet implemented");
- // if (yangFiles != null) {
- // final Map<InputStream, File> inputStreams = Maps.newHashMap();
- //
- // for (final File yangFile : yangFiles) {
- // try {
- // inputStreams.put(new FileInputStream(yangFile), yangFile);
- // } catch (FileNotFoundException e) {
- // LOG.warn("Exception while reading yang file: " + yangFile.getName(),
- // e);
- // }
- // }
- //
- // Map<ModuleBuilder, InputStream> builderToStreamMap =
- // Maps.newHashMap();
- //
- // final Map<String, TreeMap<Date, ModuleBuilder>> modules =
- // resolveModuleBuilders(
- // Lists.newArrayList(inputStreams.keySet()), builderToStreamMap);
- //
- // for (InputStream is : inputStreams.keySet()) {
- // try {
- // is.close();
- // } catch (IOException e) {
- // LOG.debug("Failed to close stream.");
- // }
- // }
- //
- // return new LinkedHashSet<Module>(buildWithContext(modules,
- // context).values());
- // }
- // return Collections.emptySet();
+ if (yangFiles != null) {
+ final Map<InputStream, File> inputStreams = Maps.newHashMap();
+
+ for (final File yangFile : yangFiles) {
+ try {
+ inputStreams.put(new FileInputStream(yangFile), yangFile);
+ } catch (FileNotFoundException e) {
+ LOG.warn("Exception while reading yang file: " + yangFile.getName(), e);
+ }
+ }
+
+ Map<ModuleBuilder, InputStream> builderToStreamMap = Maps.newHashMap();
+
+ final Map<String, TreeMap<Date, ModuleBuilder>> modules = resolveModuleBuilders(
+ Lists.newArrayList(inputStreams.keySet()), builderToStreamMap);
+
+ for (InputStream is : inputStreams.keySet()) {
+ try {
+ is.close();
+ } catch (IOException e) {
+ LOG.debug("Failed to close stream.");
+ }
+ }
+
+ return new LinkedHashSet<Module>(buildWithContext(modules, context).values());
+ }
+ return Collections.emptySet();
}
@Override
@Override
public Set<Module> parseYangModelsFromStreams(final List<InputStream> yangModelStreams, SchemaContext context) {
- // TODO
- throw new YangParseException("Not yet implemented");
- // if (yangModelStreams != null) {
- // Map<ModuleBuilder, InputStream> builderToStreamMap =
- // Maps.newHashMap();
- // final Map<String, TreeMap<Date, ModuleBuilder>> modules =
- // resolveModuleBuildersWithContext(
- // yangModelStreams, builderToStreamMap, context);
- // return new LinkedHashSet<Module>(buildWithContext(modules,
- // context).values());
- // }
- // return Collections.emptySet();
+ if (yangModelStreams != null) {
+ Map<ModuleBuilder, InputStream> builderToStreamMap = Maps.newHashMap();
+ final Map<String, TreeMap<Date, ModuleBuilder>> modules = resolveModuleBuildersWithContext(
+ yangModelStreams, builderToStreamMap, context);
+ return new LinkedHashSet<Module>(buildWithContext(modules, context).values());
+ }
+ return Collections.emptySet();
}
@Override
final SchemaContext context) {
final ModuleBuilder[] builders = parseModuleBuilders(yangFileStreams, streamToBuilderMap);
- // Linked Hash Map MUST be used because Linked Hash Map preserves ORDER
- // of items stored in map.
+ // LinkedHashMap must be used to preserve order
final LinkedHashMap<String, TreeMap<Date, ModuleBuilder>> modules = new LinkedHashMap<String, TreeMap<Date, ModuleBuilder>>();
// module dependency graph sorted
fixUnresolvedNodes(modules, moduleBuilder);
}
}
-
finishResolveDirtyNodes(modules);
resolveAugments(modules);
resolveUses(modules);
fixUnresolvedNodesWithContext(modules, moduleBuilder, context);
}
}
- // TODO finishResolveDirtyNodesWithContext(modules, context)
+ finishResolveDirtyNodes(modules);
resolveAugmentsWithContext(modules, context);
resolveUsesWithContext(modules, context);
resolveDeviationsWithContext(modules, context);
nodeToResolve.getType());
nodeToResolve.setType(idRef);
} else {
- TypeDefinitionBuilder tdb = CopyUtils.copy(nodeToResolve.getTypedef(), nodeToResolve, false);
- ParserUtils.correctTypeAwareNodePath(tdb);
- nodeToResolve.setTypedef(tdb);
+ if (nodeToResolve.getType() == null) {
+ TypeDefinitionBuilder tdb = CopyUtils.copy(nodeToResolve.getTypedef(), nodeToResolve, false);
+ ParserUtils.correctTypeAwareNodePath(tdb);
+ nodeToResolve.setTypedef(tdb);
+ } else {
+ TypeDefinition<?> td = ParserUtils.createCorrectTypeDefinition(nodeToResolve.getPath(),
+ nodeToResolve.getType());
+ nodeToResolve.setType(td);
+ }
}
}
}
*
* @param modules
* all loaded modules
- * @param augmentBuilder
+ * @param augment
* augment to resolve
* @param context
* SchemaContext containing already resolved modules
* @return true if target node found, false otherwise
*/
private boolean resolveAugmentWithContext(final Map<String, TreeMap<Date, ModuleBuilder>> modules,
- final AugmentationSchemaBuilder augmentBuilder, final SchemaContext context) {
- if (augmentBuilder.isResolved()) {
+ final AugmentationSchemaBuilder augment, final SchemaContext context) {
+ if (augment.isResolved()) {
return true;
}
- int line = augmentBuilder.getLine();
- ModuleBuilder module = getParentModule(augmentBuilder);
- List<QName> path = augmentBuilder.getTargetPath().getPath();
+ int line = augment.getLine();
+ ModuleBuilder module = getParentModule(augment);
+ List<QName> path = augment.getTargetPath().getPath();
final QName firstNameInPath = path.get(0);
String prefix = firstNameInPath.getPrefix();
if (prefix == null) {
prefix = module.getPrefix();
}
- Builder augmentParent = augmentBuilder.getParent();
+ Builder augmentParent = augment.getParent();
Builder currentParent = null;
if (augmentParent instanceof ModuleBuilder) {
currentParent = augmentParent.getParent();
} else {
// augment can be defined only under module or uses
- throw new YangParseException(augmentBuilder.getModuleName(), augmentBuilder.getLine(),
+ throw new YangParseException(augment.getModuleName(), augment.getLine(),
"Error in augment parsing: Unresolved parent of augment: " + augmentParent);
}
if (currentParent == null) {
- return processAugmentationOnContext(augmentBuilder, path, module, prefix, context);
+ return processAugmentationOnContext(augment, path, module, prefix, context);
} else {
- return processAugmentation(augmentBuilder, currentParent, path);
+ return processAugmentation(augment, currentParent, path);
}
}
*
* @param modules
* all loaded modules
+ * @param context
+ * SchemaContext containing already resolved modules or null if
+ * context is not available
*/
private void findUsesTargets(final Map<String, TreeMap<Date, ModuleBuilder>> modules, final SchemaContext context) {
final List<UsesNodeBuilder> allUses = new ArrayList<>();
for (UsesNodeBuilder usesNode : allModulesUses) {
GroupingUtils.fixUsesNodesPath(usesNode);
}
+
+ for (UsesNodeBuilder usesNode : allModulesUses) {
+ if (usesNode.isCopy()) {
+ usesNode.getParent().getUsesNodes().remove(usesNode);
+ }
+ }
}
/**
+ * Copy data from uses target, update uses parent and perform refinement.
+ * Augmentations have to be resolved already.
*
* @param modules
* all loaded modules
private void resolveUnknownNodes(final Map<String, TreeMap<Date, ModuleBuilder>> modules, final ModuleBuilder module) {
for (UnknownSchemaNodeBuilder usnb : module.getAllUnknownNodes()) {
QName nodeType = usnb.getNodeType();
- if (nodeType.getNamespace() == null || nodeType.getRevision() == null) {
- try {
- ModuleBuilder dependentModule = findDependentModuleBuilder(modules, module, nodeType.getPrefix(),
- usnb.getLine());
- QName newNodeType = new QName(dependentModule.getNamespace(), dependentModule.getRevision(),
- nodeType.getPrefix(), nodeType.getLocalName());
- usnb.setNodeType(newNodeType);
- } catch (YangParseException e) {
- LOG.debug(module.getName(), usnb.getLine(), "Failed to find unknown node type: " + nodeType);
+ try {
+ ModuleBuilder dependentModule = findDependentModuleBuilder(modules, module, nodeType.getPrefix(),
+ usnb.getLine());
+ for (ExtensionBuilder extension : dependentModule.getExtensions()) {
+ if (extension.getQName().getLocalName().equals(nodeType.getLocalName())) {
+ usnb.setExtensionBuilder(extension);
+ break;
+ }
}
+ } catch (YangParseException e) {
+ LOG.debug(module.getName(), usnb.getLine(), "Failed to find unknown node extension definition: "
+ + nodeType);
}
}
}
private void resolveUnknownNodesWithContext(final Map<String, TreeMap<Date, ModuleBuilder>> modules,
final ModuleBuilder module, final SchemaContext context) {
- for (UnknownSchemaNodeBuilder unknownNodeBuilder : module.getAllUnknownNodes()) {
- QName nodeType = unknownNodeBuilder.getNodeType();
- if (nodeType.getNamespace() == null || nodeType.getRevision() == null) {
- try {
- ModuleBuilder dependentModuleBuilder = findDependentModuleBuilder(modules, module,
- nodeType.getPrefix(), unknownNodeBuilder.getLine());
-
- QName newNodeType = null;
- if (dependentModuleBuilder == null) {
- Module dependentModule = findModuleFromContext(context, module, nodeType.getPrefix(),
- unknownNodeBuilder.getLine());
- newNodeType = new QName(dependentModule.getNamespace(), dependentModule.getRevision(),
- nodeType.getPrefix(), nodeType.getLocalName());
- } else {
- newNodeType = new QName(dependentModuleBuilder.getNamespace(),
- dependentModuleBuilder.getRevision(), nodeType.getPrefix(), nodeType.getLocalName());
- }
+ for (UnknownSchemaNodeBuilder usnb : module.getAllUnknownNodes()) {
+ QName nodeType = usnb.getNodeType();
+ try {
+ ModuleBuilder dependentModuleBuilder = findDependentModuleBuilder(modules, module,
+ nodeType.getPrefix(), usnb.getLine());
- unknownNodeBuilder.setNodeType(newNodeType);
- } catch (YangParseException e) {
- LOG.debug(module.getName(), unknownNodeBuilder.getLine(), "Failed to find unknown node type: "
- + nodeType);
+ if (dependentModuleBuilder == null) {
+ Module dependentModule = findModuleFromContext(context, module, nodeType.getPrefix(),
+ usnb.getLine());
+ for (ExtensionDefinition e : dependentModule.getExtensionSchemaNodes()) {
+ if (e.getQName().getLocalName().equals(nodeType.getLocalName())) {
+ usnb.setExtensionDefinition(e);
+ break;
+ }
+ }
+ } else {
+ for (ExtensionBuilder extension : dependentModuleBuilder.getExtensions()) {
+ if (extension.getQName().getLocalName().equals(nodeType.getLocalName())) {
+ usnb.setExtensionBuilder(extension);
+ break;
+ }
+ }
}
+
+ } catch (YangParseException e) {
+ LOG.debug(module.getName(), usnb.getLine(), "Failed to find unknown node extension definition: "
+ + nodeType);
}
+
}
}
return copy;
}
- public static GroupingBuilder copyGroupingWithoutDeep(GroupingBuilder old, Builder newParent, boolean updateQName) {
- DataBean data = getdata(old, newParent, updateQName);
- QName newQName = data.qname;
- SchemaPath newSchemaPath = data.schemaPath;
-
- GroupingBuilderImpl copy = new GroupingBuilderImpl(newParent.getModuleName(), newParent.getLine(), newQName);
- copy.setParent(newParent);
- copy.setPath(newSchemaPath);
- copy.setDescription(old.getDescription());
- copy.setReference(old.getReference());
- copy.setStatus(old.getStatus());
- copy.setAddedByUses(old.isAddedByUses());
- copy.setChildNodes(old.getChildNodes());
- for (DataSchemaNodeBuilder childNode : old.getChildNodeBuilders()) {
- copy.addChildNode(copy(childNode, copy, updateQName));
- }
- copy.getGroupings().addAll(old.getGroupings());
- for (GroupingBuilder grouping : old.getGroupingBuilders()) {
- copy.addGrouping(copy(grouping, copy, updateQName));
- }
- for (TypeDefinitionBuilder tdb : old.getTypeDefinitionBuilders()) {
- copy.addTypedef(copy(tdb, copy, updateQName));
- }
- for (UsesNodeBuilder oldUses : old.getUsesNodes()) {
- copy.addUsesNode(copyUses(oldUses, copy));
-
- }
- for (UnknownSchemaNodeBuilder un : old.getUnknownNodeBuilders()) {
- copy.addUnknownNodeBuilder((copy(un, copy, updateQName)));
- }
-
- return copy;
- }
-
public static TypeDefinitionBuilder copy(TypeDefinitionBuilder old, Builder newParent, boolean updateQName) {
DataBean data = getdata(old, newParent, updateQName);
QName newQName = data.qname;
static UsesNodeBuilder copyUses(UsesNodeBuilder old, Builder newParent) {
UsesNodeBuilder copy = new UsesNodeBuilderImpl(newParent.getModuleName(), newParent.getLine(),
- old.getGroupingName());
+ old.getGroupingName(), true);
copy.setParent(newParent);
copy.setGroupingPath(old.getGroupingPath());
// TODO grouping vs grouping path?
// add new uses to collection of uses in module
ModuleBuilder module = ParserUtils.getParentModule(newParent);
- module.addUsesNode(copy);
+ module.getAllUsesNodes().add(copy);
return copy;
}
newPath = new ArrayList<>(parent.getPath().getPath());
newPath.add(newQName);
} else {
- if(old == null) {
- System.out.println();
- }
newQName = old.getQName();
newPath = new ArrayList<>(parent.getPath().getPath());
newPath.add(newQName);
// child nodes
Set<DataSchemaNodeBuilder> currentChildNodes = parent.getChildNodeBuilders();
- Set<DataSchemaNodeBuilder> toRemove = new HashSet<>();
- Set<DataSchemaNodeBuilder> toAdd = new HashSet<>();
for (DataSchemaNodeBuilder child : currentChildNodes) {
if (child instanceof GroupingMember) {
GroupingMember gm = (GroupingMember) child;
if (gm.isAddedByUses()) {
- toRemove.add(child);
- DataSchemaNodeBuilder copy = CopyUtils.copy(child, parent, true);
- ParserUtils.correctNodePath(copy, parent.getPath());
- toAdd.add(copy);
+ ParserUtils.correctNodePath(child, parent.getPath());
}
}
}
- currentChildNodes.removeAll(toRemove);
- currentChildNodes.addAll(toAdd);
// groupings
Set<GroupingBuilder> currentGroupings = parent.getGroupingBuilders();
- Set<GroupingBuilder> toRemoveG = new HashSet<>();
- Set<GroupingBuilder> toAddG = new HashSet<>();
for (GroupingBuilder child : currentGroupings) {
if (child.isAddedByUses()) {
- toRemoveG.add(child);
- GroupingBuilder copy = CopyUtils.copy(child, parent, true);
- ParserUtils.correctNodePath(copy, parent.getPath());
- toAddG.add(copy);
+ ParserUtils.correctNodePath(child, parent.getPath());
}
}
- currentGroupings.removeAll(toRemoveG);
- currentGroupings.addAll(toAddG);
// typedefs
Set<TypeDefinitionBuilder> currentTypedefs = parent.getTypeDefinitionBuilders();
- Set<TypeDefinitionBuilder> toRemoveTD = new HashSet<>();
- Set<TypeDefinitionBuilder> toAddTD = new HashSet<>();
for (TypeDefinitionBuilder child : currentTypedefs) {
if (child.isAddedByUses()) {
- toRemoveTD.add(child);
- TypeDefinitionBuilder copy = CopyUtils.copy(child, parent, true);
- ParserUtils.correctNodePath(copy, parent.getPath());
- toAddTD.add(copy);
+ ParserUtils.correctNodePath(child, parent.getPath());
}
}
- currentTypedefs.removeAll(toRemoveTD);
- currentTypedefs.addAll(toAddTD);
// unknown nodes
List<UnknownSchemaNodeBuilder> currentUN = parent.getUnknownNodeBuilders();
- List<UnknownSchemaNodeBuilder> toRemoveUN = new ArrayList<>();
- List<UnknownSchemaNodeBuilder> toAddUN = new ArrayList<>();
for (UnknownSchemaNodeBuilder un : currentUN) {
if (un.isAddedByUses()) {
- toRemoveUN.add(un);
- UnknownSchemaNodeBuilder copy = CopyUtils.copy(un, parent, true);
- ParserUtils.correctNodePath(copy, parent.getPath());
- toAddUN.add(copy);
+ ParserUtils.correctNodePath(un, parent.getPath());
}
}
- currentUN.removeAll(toRemoveUN);
- currentUN.addAll(toAddUN);
}
/**