From: Martin Vitez Date: Thu, 4 Jul 2013 11:50:25 +0000 (+0200) Subject: Added more tests for yang parser. Updated current tests. X-Git-Tag: releasepom-0.1.0~306^2 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=commitdiff_plain;h=c04f8eba6fd7e6939e022071deba887bbbddffee Added more tests for yang parser. Updated current tests. Improved parsing of deviation statement. Added getDataChildByName method to DataNodeContainerBuilder. Fixed resolving of pattern constraint. Renamed YangModelBuilderUtil -> ParserListenerUtils. Renamed some test files. Signed-off-by: Martin Vitez --- diff --git a/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/parser/builder/api/AbstractDataNodeContainerBuilder.java b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/parser/builder/api/AbstractDataNodeContainerBuilder.java index 6e804a199d..116f82e8d8 100644 --- a/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/parser/builder/api/AbstractDataNodeContainerBuilder.java +++ b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/parser/builder/api/AbstractDataNodeContainerBuilder.java @@ -64,6 +64,16 @@ public abstract class AbstractDataNodeContainerBuilder implements DataNodeContai return addedChildNodes; } + @Override + public DataSchemaNodeBuilder getDataChildByName(final String name) { + for(DataSchemaNodeBuilder child : addedChildNodes) { + if(child.getQName().getLocalName().equals(name)) { + return child; + } + } + return null; + } + @Override public void addChildNode(DataSchemaNodeBuilder childNode) { addedChildNodes.add(childNode); diff --git a/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/parser/builder/api/DataNodeContainerBuilder.java b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/parser/builder/api/DataNodeContainerBuilder.java index 13b7e4874e..4271776d63 100644 --- a/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/parser/builder/api/DataNodeContainerBuilder.java +++ b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/parser/builder/api/DataNodeContainerBuilder.java @@ -28,6 +28,8 @@ public interface DataNodeContainerBuilder extends Builder { Set getChildNodeBuilders(); + DataSchemaNodeBuilder getDataChildByName(String name); + void addChildNode(DataSchemaNodeBuilder childNode); Set getGroupings(); diff --git a/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/parser/builder/impl/AugmentationSchemaBuilderImpl.java b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/parser/builder/impl/AugmentationSchemaBuilderImpl.java index b534ba4f60..d3082e8824 100644 --- a/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/parser/builder/impl/AugmentationSchemaBuilderImpl.java +++ b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/parser/builder/impl/AugmentationSchemaBuilderImpl.java @@ -34,7 +34,7 @@ import org.opendaylight.controller.yang.parser.builder.api.GroupingBuilder; import org.opendaylight.controller.yang.parser.builder.api.TypeDefinitionBuilder; import org.opendaylight.controller.yang.parser.builder.api.UsesNodeBuilder; import org.opendaylight.controller.yang.parser.util.Comparators; -import org.opendaylight.controller.yang.parser.util.YangModelBuilderUtil; +import org.opendaylight.controller.yang.parser.util.ParserListenerUtils; import org.opendaylight.controller.yang.parser.util.YangParseException; public final class AugmentationSchemaBuilderImpl implements AugmentationSchemaBuilder { @@ -61,7 +61,7 @@ public final class AugmentationSchemaBuilderImpl implements AugmentationSchemaBu AugmentationSchemaBuilderImpl(final int line, final String augmentTargetStr) { this.augmentTargetStr = augmentTargetStr; this.line = line; - final SchemaPath targetPath = YangModelBuilderUtil.parseAugmentPath(augmentTargetStr); + final SchemaPath targetPath = ParserListenerUtils.parseAugmentPath(augmentTargetStr); dirtyAugmentTarget = targetPath; instance = new AugmentationSchemaImpl(targetPath); } @@ -91,6 +91,16 @@ public final class AugmentationSchemaBuilderImpl implements AugmentationSchemaBu return Collections.emptySet(); } + @Override + public DataSchemaNodeBuilder getDataChildByName(final String name) { + for(DataSchemaNodeBuilder child : childNodes) { + if(child.getQName().getLocalName().equals(name)) { + return child; + } + } + return null; + } + @Override public Set getChildNodeBuilders() { return childNodes; diff --git a/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/parser/builder/impl/DeviationBuilder.java b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/parser/builder/impl/DeviationBuilder.java index 9b52d61c88..aee5587a36 100644 --- a/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/parser/builder/impl/DeviationBuilder.java +++ b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/parser/builder/impl/DeviationBuilder.java @@ -17,31 +17,48 @@ import org.opendaylight.controller.yang.model.api.SchemaPath; import org.opendaylight.controller.yang.model.api.UnknownSchemaNode; import org.opendaylight.controller.yang.parser.builder.api.Builder; import org.opendaylight.controller.yang.parser.util.Comparators; -import org.opendaylight.controller.yang.parser.util.YangModelBuilderUtil; +import org.opendaylight.controller.yang.parser.util.ParserListenerUtils; import org.opendaylight.controller.yang.parser.util.YangParseException; public final class DeviationBuilder implements Builder { private final int line; private Builder parent; + private boolean isBuilt; private final DeviationImpl instance; + + private SchemaPath targetPath; + private String reference; private final List addedUnknownNodes = new ArrayList(); - DeviationBuilder(final String targetPathStr, final int line) { + DeviationBuilder(final int line, final String targetPathStr) { + if(!targetPathStr.startsWith("/")) { + throw new YangParseException(line, "Deviation argument string must be an absolute schema node identifier."); + } this.line = line; - final SchemaPath targetPath = YangModelBuilderUtil - .parseAugmentPath(targetPathStr); - instance = new DeviationImpl(targetPath); + this.targetPath = ParserListenerUtils.parseAugmentPath(targetPathStr); + instance = new DeviationImpl(); } @Override public Deviation build() { - // UNKNOWN NODES - List unknownNodes = new ArrayList(); - for (UnknownSchemaNodeBuilder b : addedUnknownNodes) { - unknownNodes.add(b.build()); + if(targetPath == null) { + throw new YangParseException(line, "Unresolved deviation target"); + } + + if(!isBuilt) { + instance.setTargetPath(targetPath); + instance.setReference(reference); + + // UNKNOWN NODES + List unknownNodes = new ArrayList(); + for (UnknownSchemaNodeBuilder b : addedUnknownNodes) { + unknownNodes.add(b.build()); + } + Collections.sort(unknownNodes, Comparators.SCHEMA_NODE_COMP); + instance.setUnknownSchemaNodes(unknownNodes); + + isBuilt = true; } - Collections.sort(unknownNodes, Comparators.SCHEMA_NODE_COMP); - instance.setUnknownSchemaNodes(unknownNodes); return instance; } @@ -66,6 +83,14 @@ public final class DeviationBuilder implements Builder { addedUnknownNodes.add(unknownNode); } + public SchemaPath getTargetPath() { + return targetPath; + } + + public void setTargetPath(final SchemaPath targetPath) { + this.targetPath = targetPath; + } + public void setDeviate(final String deviate) { if ("not-supported".equals(deviate)) { instance.setDeviate(Deviate.NOT_SUPPORTED); @@ -82,17 +107,16 @@ public final class DeviationBuilder implements Builder { } public void setReference(final String reference) { - instance.setReference(reference); + this.reference = reference; } private final class DeviationImpl implements Deviation { - private final SchemaPath targetPath; + private SchemaPath targetPath; private Deviate deviate; private String reference; private List unknownNodes = Collections.emptyList(); - private DeviationImpl(final SchemaPath targetPath) { - this.targetPath = targetPath; + private DeviationImpl() { } @Override @@ -100,6 +124,10 @@ public final class DeviationBuilder implements Builder { return targetPath; } + private void setTargetPath(final SchemaPath targetPath) { + this.targetPath = targetPath; + } + @Override public Deviate getDeviate() { return deviate; diff --git a/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/parser/builder/impl/GroupingBuilderImpl.java b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/parser/builder/impl/GroupingBuilderImpl.java index 0f7179e791..0099b4f54b 100644 --- a/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/parser/builder/impl/GroupingBuilderImpl.java +++ b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/parser/builder/impl/GroupingBuilderImpl.java @@ -258,6 +258,16 @@ public final class GroupingBuilderImpl implements GroupingBuilder { return addedChildNodes; } + @Override + public DataSchemaNodeBuilder getDataChildByName(final String name) { + for(DataSchemaNodeBuilder child : addedChildNodes) { + if(child.getQName().getLocalName().equals(name)) { + return child; + } + } + return null; + } + public void setChildNodes(final Set childNodes) { this.childNodes = childNodes; } diff --git a/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/parser/builder/impl/ModuleBuilder.java b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/parser/builder/impl/ModuleBuilder.java index 57e87b33ad..484461e4e3 100644 --- a/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/parser/builder/impl/ModuleBuilder.java +++ b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/parser/builder/impl/ModuleBuilder.java @@ -81,7 +81,7 @@ public class ModuleBuilder implements DataNodeContainerBuilder { private final Set addedNotifications = new HashSet(); private final Set addedIdentities = new HashSet(); private final Set addedFeatures = new HashSet(); - private final Map, DeviationBuilder> addedDeviations = new HashMap, DeviationBuilder>(); + private final Set addedDeviations = new HashSet(); private final Set addedTypedefs = new HashSet(); private final Map, UnionTypeBuilder> addedUnionTypes = new HashMap, UnionTypeBuilder>(); private final List addedExtensions = new ArrayList(); @@ -165,8 +165,8 @@ public class ModuleBuilder implements DataNodeContainerBuilder { // DEVIATIONS final Set deviations = new HashSet(); - for (Map.Entry, DeviationBuilder> entry : addedDeviations.entrySet()) { - deviations.add(entry.getValue().build()); + for (DeviationBuilder entry : addedDeviations) { + deviations.add(entry.build()); } instance.setDeviations(deviations); @@ -265,6 +265,16 @@ public class ModuleBuilder implements DataNodeContainerBuilder { return childNodes; } + @Override + public DataSchemaNodeBuilder getDataChildByName(final String name) { + for(DataSchemaNodeBuilder child : childNodes) { + if(child.getQName().getLocalName().equals(name)) { + return child; + } + } + return null; + } + public Map, TypeAwareBuilder> getDirtyNodes() { return dirtyNodes; } @@ -281,6 +291,10 @@ public class ModuleBuilder implements DataNodeContainerBuilder { return allUsesNodes; } + public Set getDeviations() { + return addedDeviations; + } + public List getUnknownNodes() { List result = new ArrayList(); for (List entry : addedUnknownNodes.values()) { @@ -754,16 +768,14 @@ public class ModuleBuilder implements DataNodeContainerBuilder { } } - public DeviationBuilder addDeviation(final String targetPath, final List parentPath, final int line) { + public DeviationBuilder addDeviation(final int line, final String targetPath) { Builder parent = getActualNode(); if (parent != null) { throw new YangParseException(name, line, "deviation can be defined only in module or submodule"); } - final List pathToDeviation = new ArrayList(parentPath); - pathToDeviation.add(targetPath); - final DeviationBuilder builder = new DeviationBuilder(targetPath, line); - addedDeviations.put(pathToDeviation, builder); + final DeviationBuilder builder = new DeviationBuilder(line, targetPath); + addedDeviations.add(builder); return builder; } diff --git a/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/parser/impl/YangParserImpl.java b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/parser/impl/YangParserImpl.java index 6adc29b227..712c84fad1 100644 --- a/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/parser/impl/YangParserImpl.java +++ b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/parser/impl/YangParserImpl.java @@ -39,6 +39,7 @@ import org.opendaylight.controller.yang.common.QName; import org.opendaylight.controller.yang.model.api.AnyXmlSchemaNode; import org.opendaylight.controller.yang.model.api.ChoiceNode; import org.opendaylight.controller.yang.model.api.ContainerSchemaNode; +import org.opendaylight.controller.yang.model.api.DataNodeContainer; import org.opendaylight.controller.yang.model.api.DataSchemaNode; import org.opendaylight.controller.yang.model.api.GroupingDefinition; import org.opendaylight.controller.yang.model.api.IdentitySchemaNode; @@ -47,6 +48,7 @@ import org.opendaylight.controller.yang.model.api.LeafSchemaNode; import org.opendaylight.controller.yang.model.api.ListSchemaNode; import org.opendaylight.controller.yang.model.api.Module; 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; import org.opendaylight.controller.yang.model.api.UnknownSchemaNode; @@ -68,6 +70,7 @@ import org.opendaylight.controller.yang.parser.builder.api.UsesNodeBuilder; import org.opendaylight.controller.yang.parser.builder.impl.AnyXmlBuilder; import org.opendaylight.controller.yang.parser.builder.impl.ChoiceBuilder; import org.opendaylight.controller.yang.parser.builder.impl.ContainerSchemaNodeBuilder; +import org.opendaylight.controller.yang.parser.builder.impl.DeviationBuilder; import org.opendaylight.controller.yang.parser.builder.impl.GroupingBuilderImpl; import org.opendaylight.controller.yang.parser.builder.impl.IdentitySchemaNodeBuilder; import org.opendaylight.controller.yang.parser.builder.impl.IdentityrefTypeBuilder; @@ -305,6 +308,7 @@ public final class YangParserImpl implements YangModelParser { } } resolveAugments(modules); + resolveDeviations(modules); // build // LinkedHashMap MUST be used otherwise the values will not maintain @@ -333,6 +337,7 @@ public final class YangParserImpl implements YangModelParser { } } resolveAugmentsWithContext(modules, context); + resolveDeviationsWithContext(modules, context); // build // LinkedHashMap MUST be used otherwise the values will not maintain @@ -1218,7 +1223,7 @@ public final class YangParserImpl implements YangModelParser { } private void resolveUnknownNodesWithContext(final Map> modules, - final ModuleBuilder module, SchemaContext context) { + final ModuleBuilder module, final SchemaContext context) { for (UnknownSchemaNodeBuilder unknownNodeBuilder : module.getUnknownNodes()) { QName nodeType = unknownNodeBuilder.getNodeType(); if (nodeType.getNamespace() == null || nodeType.getRevision() == null) { @@ -1246,4 +1251,115 @@ public final class YangParserImpl implements YangModelParser { } } + private void resolveDeviations(final Map> modules) { + for (Map.Entry> entry : modules.entrySet()) { + for (Map.Entry inner : entry.getValue().entrySet()) { + ModuleBuilder b = inner.getValue(); + resolveDeviation(modules, b); + } + } + } + + private void resolveDeviation(final Map> modules, final ModuleBuilder module) { + for (DeviationBuilder dev : module.getDeviations()) { + int line = dev.getLine(); + SchemaPath targetPath = dev.getTargetPath(); + List path = targetPath.getPath(); + QName q0 = path.get(0); + String prefix = q0.getPrefix(); + if (prefix == null) { + prefix = module.getPrefix(); + } + + ModuleBuilder dependentModuleBuilder = findDependentModuleBuilder(modules, module, prefix, line); + processDeviation(dev, dependentModuleBuilder, path, module); + } + } + + private void resolveDeviationsWithContext(final Map> modules, + final SchemaContext context) { + for (Map.Entry> entry : modules.entrySet()) { + for (Map.Entry inner : entry.getValue().entrySet()) { + ModuleBuilder b = inner.getValue(); + resolveDeviationWithContext(modules, b, context); + } + } + } + + private void resolveDeviationWithContext(final Map> modules, + final ModuleBuilder module, final SchemaContext context) { + for (DeviationBuilder dev : module.getDeviations()) { + int line = dev.getLine(); + SchemaPath targetPath = dev.getTargetPath(); + List path = targetPath.getPath(); + QName q0 = path.get(0); + String prefix = q0.getPrefix(); + if (prefix == null) { + prefix = module.getPrefix(); + } + String name = null; + + ModuleBuilder dependentModuleBuilder = findDependentModuleBuilder(modules, module, prefix, line); + if (dependentModuleBuilder == null) { + Module dependentModule = findModuleFromContext(context, module, prefix, line); + Object currentParent = dependentModule; + + for (int i = 0; i < path.size(); i++) { + if (currentParent == null) { + throw new YangParseException(module.getName(), line, "Failed to find deviation target."); + } + QName q = path.get(i); + name = q.getLocalName(); + if (currentParent instanceof DataNodeContainer) { + currentParent = ((DataNodeContainer) currentParent).getDataChildByName(name); + } + } + + if (currentParent == null) { + throw new YangParseException(module.getName(), line, "Failed to find deviation target."); + } + if (currentParent instanceof SchemaNode) { + dev.setTargetPath(((SchemaNode) currentParent).getPath()); + } + + } else { + processDeviation(dev, dependentModuleBuilder, path, module); + } + } + } + + /** + * Correct deviation target path in deviation builder. + * + * @param dev + * deviation + * @param dependentModuleBuilder + * module containing deviation target + * @param path + * current deviation target path + * @param module + * current module + */ + private void processDeviation(final DeviationBuilder dev, final ModuleBuilder dependentModuleBuilder, + final List path, final ModuleBuilder module) { + final int line = dev.getLine(); + Builder currentParent = dependentModuleBuilder; + + for (int i = 0; i < path.size(); i++) { + if (currentParent == null) { + throw new YangParseException(module.getName(), line, "Failed to find deviation target."); + } + QName q = path.get(i); + String name = q.getLocalName(); + if (currentParent instanceof DataNodeContainerBuilder) { + currentParent = ((DataNodeContainerBuilder) currentParent).getDataChildByName(name); + } + } + + if (currentParent == null || !(currentParent instanceof SchemaNodeBuilder)) { + throw new YangParseException(module.getName(), line, "Failed to find deviation target."); + } + dev.setTargetPath(((SchemaNodeBuilder) currentParent).getPath()); + } + } diff --git a/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/parser/impl/YangParserListenerImpl.java b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/parser/impl/YangParserListenerImpl.java index f806c94a30..73462b22da 100644 --- a/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/parser/impl/YangParserListenerImpl.java +++ b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/parser/impl/YangParserListenerImpl.java @@ -7,7 +7,7 @@ */ package org.opendaylight.controller.yang.parser.impl; -import static org.opendaylight.controller.yang.parser.util.YangModelBuilderUtil.*; +import static org.opendaylight.controller.yang.parser.util.ParserListenerUtils.*; import java.net.URI; import java.text.DateFormat; @@ -331,6 +331,7 @@ public final class YangParserListenerImpl extends YangParserBaseListener { builder.setPath(createActualSchemaPath(actualPath, namespace, revision, yangModelPrefix)); parseSchemaNodeArgs(ctx, builder); builder.setUnits(parseUnits(ctx)); + builder.setDefaultValue(parseDefault(ctx)); } @Override @@ -854,7 +855,7 @@ public final class YangParserListenerImpl extends YangParserBaseListener { String reference = null; String deviate = null; - DeviationBuilder builder = moduleBuilder.addDeviation(targetPath, actualPath, line); + DeviationBuilder builder = moduleBuilder.addDeviation(line, targetPath); moduleBuilder.enterNode(builder); actualPath.push(targetPath); diff --git a/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/parser/util/YangModelBuilderUtil.java b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/parser/util/ParserListenerUtils.java similarity index 98% rename from opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/parser/util/YangModelBuilderUtil.java rename to opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/parser/util/ParserListenerUtils.java index ada2d2cfd8..d913f5b87f 100644 --- a/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/parser/util/YangModelBuilderUtil.java +++ b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/parser/util/ParserListenerUtils.java @@ -118,10 +118,10 @@ import org.opendaylight.controller.yang.parser.builder.impl.UnionTypeBuilder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public final class YangModelBuilderUtil { - private static final Logger logger = LoggerFactory.getLogger(YangModelBuilderUtil.class); +public final class ParserListenerUtils { + private static final Logger logger = LoggerFactory.getLogger(ParserListenerUtils.class); - private YangModelBuilderUtil() { + private ParserListenerUtils() { } /** @@ -216,6 +216,26 @@ public final class YangModelBuilderUtil { return units; } + /** + * Parse given tree and returns default statement as string. + * + * @param ctx + * context to parse + * @return value of default statement as string or null if there is no + * default statement + */ + public static String parseDefault(final ParseTree ctx) { + String defaultValue = null; + for (int i = 0; i < ctx.getChildCount(); i++) { + ParseTree child = ctx.getChild(i); + if (child instanceof Default_stmtContext) { + defaultValue = stringFromNode(child); + break; + } + } + return defaultValue; + } + /** * Create SchemaPath from actualPath and names. * @@ -926,8 +946,8 @@ public final class YangModelBuilderUtil { ChoiceCaseBuilder choiceCase = (ChoiceCaseBuilder) parent; Builder choice = choiceCase.getParent(); Boolean parentConfig = null; - if(choice instanceof ChoiceBuilder) { - parentConfig = ((ChoiceBuilder)choice).isConfiguration(); + if (choice instanceof ChoiceBuilder) { + parentConfig = ((ChoiceBuilder) choice).isConfiguration(); } else { parentConfig = true; } @@ -1019,17 +1039,14 @@ public final class YangModelBuilderUtil { TypeDefinition baseType = unknownType.build(); TypeDefinition result = null; QName qname = new QName(namespace, revision, prefix, typeName); - ExtendedType.Builder typeBuilder = null; - SchemaPath schemaPath = createTypeSchemaPath(actualPath, namespace, revision, prefix, typeName, false, false); - typeBuilder = new ExtendedType.Builder(qname, baseType, "", "", schemaPath); + ExtendedType.Builder typeBuilder = new ExtendedType.Builder(qname, baseType, null, null, schemaPath); typeBuilder.ranges(rangeStatements); typeBuilder.lengths(lengthStatements); typeBuilder.patterns(patternStatements); typeBuilder.fractionDigits(fractionDigits); - result = typeBuilder.build(); return result; diff --git a/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/parser/util/TypeConstraints.java b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/parser/util/TypeConstraints.java index 17307b46ee..4d2c5b5823 100644 --- a/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/parser/util/TypeConstraints.java +++ b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/parser/util/TypeConstraints.java @@ -24,7 +24,7 @@ public final class TypeConstraints { private final int line; private final List> ranges = new ArrayList>(); private final List> lengths = new ArrayList>(); - private final List patterns = new ArrayList(); + private final List> patterns = new ArrayList>(); private final List fractionDigits = new ArrayList(); public TypeConstraints(final String moduleName, final int line) { @@ -195,11 +195,14 @@ public final class TypeConstraints { } public List getPatterns() { - return patterns; + if(patterns.isEmpty()) { + return Collections.emptyList(); + } + return patterns.get(0); } public void addPatterns(final List patterns) { - this.patterns.addAll(patterns); + this.patterns.add(patterns); } public Integer getFractionDigits() { diff --git a/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/validator/ValidationUtil.java b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/validator/ValidationUtil.java index d697201f21..cb5af9f349 100644 --- a/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/validator/ValidationUtil.java +++ b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/validator/ValidationUtil.java @@ -16,7 +16,7 @@ import java.util.Set; import org.antlr.v4.runtime.tree.ParseTree; import org.opendaylight.controller.antlrv4.code.gen.YangParser.Module_stmtContext; import org.opendaylight.controller.antlrv4.code.gen.YangParser.Submodule_stmtContext; -import org.opendaylight.controller.yang.parser.util.YangModelBuilderUtil; +import org.opendaylight.controller.yang.parser.util.ParserListenerUtils; import org.opendaylight.controller.yang.parser.util.YangValidationException; /** @@ -60,7 +60,7 @@ final class ValidationUtil { } static String getName(ParseTree child) { - return YangModelBuilderUtil.stringFromNode(child); + return ParserListenerUtils.stringFromNode(child); } static String f(String base, Object... args) { diff --git a/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/test/java/org/opendaylight/controller/yang/parser/impl/AugmentTest.java b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/test/java/org/opendaylight/controller/yang/parser/impl/AugmentTest.java new file mode 100644 index 0000000000..d6f635650f --- /dev/null +++ b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/test/java/org/opendaylight/controller/yang/parser/impl/AugmentTest.java @@ -0,0 +1,360 @@ +/* + * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ +package org.opendaylight.controller.yang.parser.impl; + +import static org.junit.Assert.*; + +import java.io.FileNotFoundException; +import java.net.URI; +import java.text.DateFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Arrays; +import java.util.Date; +import java.util.List; +import java.util.Set; + +import org.junit.Before; +import org.junit.Test; +import org.opendaylight.controller.yang.common.QName; +import org.opendaylight.controller.yang.model.api.AugmentationSchema; +import org.opendaylight.controller.yang.model.api.ChoiceCaseNode; +import org.opendaylight.controller.yang.model.api.ChoiceNode; +import org.opendaylight.controller.yang.model.api.ContainerSchemaNode; +import org.opendaylight.controller.yang.model.api.DataSchemaNode; +import org.opendaylight.controller.yang.model.api.LeafSchemaNode; +import org.opendaylight.controller.yang.model.api.ListSchemaNode; +import org.opendaylight.controller.yang.model.api.Module; +import org.opendaylight.controller.yang.model.api.SchemaPath; +import org.opendaylight.controller.yang.model.api.TypeDefinition; +import org.opendaylight.controller.yang.model.util.ExtendedType; +import org.opendaylight.controller.yang.model.util.Leafref; + +import com.google.common.collect.Lists; + +public class AugmentTest { + + private final URI types1NS = URI.create("urn:simple.nodes.test"); + private final URI types2NS = URI.create("urn:simple.types.test"); + private final URI types3NS = URI.create("urn:custom.nodes.test"); + private Date types1Rev; + private Date types2Rev; + private Date types3Rev; + private final String t1 = "n"; + private final String t2 = "t"; + private final String t3 = "c"; + private QName q0; + private QName q1; + private QName q2; + + private final DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); + private Set modules; + + @Before + public void init() throws FileNotFoundException, ParseException { + types1Rev = simpleDateFormat.parse("2013-02-27"); + types2Rev = simpleDateFormat.parse("2013-07-03"); + types3Rev = simpleDateFormat.parse("2013-02-27"); + + q0 = new QName(types2NS, types2Rev, t2, "interfaces"); + q1 = new QName(types2NS, types2Rev, t2, "ifEntry"); + q2 = new QName(types3NS, types3Rev, t3, "augment-holder"); + + modules = TestUtils.loadModules(getClass().getResource("/model").getPath()); + assertEquals(3, modules.size()); + } + + @Test + public void testAugmentParsing() { + SchemaPath expectedPath = null; + QName[] qnames = null; + + // testfile1 + Module module1 = TestUtils.findModule(modules, "nodes"); + Set augmentations = module1.getAugmentations(); + assertEquals(1, augmentations.size()); + AugmentationSchema augment = augmentations.iterator().next(); + + Set augmentChildren = augment.getChildNodes(); + assertEquals(5, augmentChildren.size()); + for(DataSchemaNode dsn : augmentChildren) { + assertTrue(dsn.isAugmenting()); + } + + LeafSchemaNode ds0ChannelNumber = (LeafSchemaNode) augment.getDataChildByName("ds0ChannelNumber"); + LeafSchemaNode interfaceId = (LeafSchemaNode) augment.getDataChildByName("interface-id"); + LeafSchemaNode myType = (LeafSchemaNode) augment.getDataChildByName("my-type"); + ContainerSchemaNode schemas = (ContainerSchemaNode) augment.getDataChildByName("schemas"); + ChoiceNode odl = (ChoiceNode)augment.getDataChildByName("odl"); + + assertNotNull(ds0ChannelNumber); + assertNotNull(interfaceId); + assertNotNull(myType); + assertNotNull(schemas); + assertNotNull(odl); + + qnames = new QName[4]; + qnames[0] = q0; + qnames[1] = q1; + qnames[2] = q2; + + // leaf ds0ChannelNumber + qnames[3] = new QName(types1NS, types1Rev, t1, "ds0ChannelNumber"); + expectedPath = new SchemaPath(Lists.newArrayList(qnames), true); + assertEquals(expectedPath, ds0ChannelNumber.getPath()); + + // leaf interface-id + qnames[3] = new QName(types1NS, types1Rev, t1, "interface-id"); + expectedPath = new SchemaPath(Lists.newArrayList(qnames), true); + assertEquals(expectedPath, interfaceId.getPath()); + + // leaf my-type + qnames[3] = new QName(types1NS, types1Rev, t1, "my-type"); + expectedPath = new SchemaPath(Lists.newArrayList(qnames), true); + assertEquals(expectedPath, myType.getPath()); + + // container schemas + qnames[3] = new QName(types1NS, types1Rev, t1, "schemas"); + expectedPath = new SchemaPath(Lists.newArrayList(qnames), true); + assertEquals(expectedPath, schemas.getPath()); + + // choice odl + qnames[3] = new QName(types1NS, types1Rev, t1, "odl"); + expectedPath = new SchemaPath(Lists.newArrayList(qnames), true); + assertEquals(expectedPath, odl.getPath()); + + + // testfile3 + Module module3 = TestUtils.findModule(modules, "custom"); + augmentations = module3.getAugmentations(); + assertEquals(3, augmentations.size()); + AugmentationSchema augment1 = null; + AugmentationSchema augment2 = null; + AugmentationSchema augment3 = null; + for (AugmentationSchema as : augmentations) { + if("if:ifType='ds0'".equals(as.getWhenCondition().toString())) { + augment1 = as; + } else if("if:ifType='ds2'".equals(as.getWhenCondition().toString())) { + augment2 = as; + } else if ("if:leafType='ds1'".equals(as.getWhenCondition().toString())) { + augment3 = as; + } + } + assertNotNull(augment1); + assertNotNull(augment2); + assertNotNull(augment3); + + assertEquals(1, augment1.getChildNodes().size()); + ContainerSchemaNode augmentHolder = (ContainerSchemaNode) augment1.getDataChildByName("augment-holder"); + assertTrue(augmentHolder.isAugmenting()); + + assertEquals(1, augment2.getChildNodes().size()); + ContainerSchemaNode augmentHolder2 = (ContainerSchemaNode) augment2.getDataChildByName("augment-holder2"); + assertTrue(augmentHolder2.isAugmenting()); + + assertEquals(1, augment3.getChildNodes().size()); + LeafSchemaNode linkleaf = (LeafSchemaNode) augment3.getDataChildByName("linkleaf"); + assertTrue(linkleaf.isAugmenting()); + } + + @Test + public void testAugmentResolving() throws ParseException { + SchemaPath expectedPath = null; + QName[] qnames = null; + + Module module2 = TestUtils.findModule(modules, "types"); + ContainerSchemaNode interfaces = (ContainerSchemaNode) module2.getDataChildByName("interfaces"); + ListSchemaNode ifEntry = (ListSchemaNode) interfaces.getDataChildByName("ifEntry"); + ContainerSchemaNode augmentedContainer = (ContainerSchemaNode) ifEntry.getDataChildByName("augment-holder"); + + // testfile1.yang + // augment "/data:interfaces/data:ifEntry/t3:augment-holder" + LeafSchemaNode ds0ChannelNumber = (LeafSchemaNode) augmentedContainer.getDataChildByName("ds0ChannelNumber"); + LeafSchemaNode interfaceId = (LeafSchemaNode) augmentedContainer.getDataChildByName("interface-id"); + LeafSchemaNode myType = (LeafSchemaNode) augmentedContainer.getDataChildByName("my-type"); + ContainerSchemaNode schemas = (ContainerSchemaNode) augmentedContainer.getDataChildByName("schemas"); + ChoiceNode odl = (ChoiceNode)augmentedContainer.getDataChildByName("odl"); + + assertNotNull(ds0ChannelNumber); + assertNotNull(interfaceId); + assertNotNull(myType); + assertNotNull(schemas); + assertNotNull(odl); + + qnames = new QName[4]; + qnames[0] = q0; + qnames[1] = q1; + qnames[2] = q2; + + // leaf ds0ChannelNumber + qnames[3] = new QName(types1NS, types1Rev, t1, "ds0ChannelNumber"); + expectedPath = new SchemaPath(Lists.newArrayList(qnames), true); + assertEquals(expectedPath, ds0ChannelNumber.getPath()); + + // leaf interface-id + qnames[3] = new QName(types1NS, types1Rev, t1, "interface-id"); + expectedPath = new SchemaPath(Lists.newArrayList(qnames), true); + assertEquals(expectedPath, interfaceId.getPath()); + + // leaf my-type + qnames[3] = new QName(types1NS, types1Rev, t1, "my-type"); + expectedPath = new SchemaPath(Lists.newArrayList(qnames), true); + assertEquals(expectedPath, myType.getPath()); + + // container schemas + qnames[3] = new QName(types1NS, types1Rev, t1, "schemas"); + expectedPath = new SchemaPath(Lists.newArrayList(qnames), true); + assertEquals(expectedPath, schemas.getPath()); + + // choice odl + qnames[3] = new QName(types1NS, types1Rev, t1, "odl"); + expectedPath = new SchemaPath(Lists.newArrayList(qnames), true); + assertEquals(expectedPath, odl.getPath()); + + // testfile3.yang + // augment "/data:interfaces/data:ifEntry/t3:augment-holder/t1:schemas" + LeafSchemaNode linkleaf = (LeafSchemaNode) schemas.getDataChildByName("linkleaf"); + assertNotNull(linkleaf); + + qnames = new QName[5]; + qnames[0] = q0; + qnames[1] = q1; + qnames[2] = q2; + qnames[3] = new QName(types1NS, types1Rev, t1, "schemas"); + qnames[4] = new QName(types3NS, types3Rev, t3, "linkleaf"); + expectedPath = new SchemaPath(Arrays.asList(qnames), true); + assertEquals(expectedPath, linkleaf.getPath()); + } + + @Test + public void testAugmentChoice() throws ParseException { + SchemaPath expectedPath = null; + QName[] qnames = null; + + Module module2 = TestUtils.findModule(modules, "types"); + ContainerSchemaNode interfaces = (ContainerSchemaNode) module2.getDataChildByName("interfaces"); + ListSchemaNode ifEntry = (ListSchemaNode) interfaces.getDataChildByName("ifEntry"); + ContainerSchemaNode augmentedContainer = (ContainerSchemaNode) ifEntry.getDataChildByName("augment-holder"); + + // testfile1.yang + // augment "/data:interfaces/data:ifEntry/t3:augment-holder" + ChoiceNode odl = (ChoiceNode)augmentedContainer.getDataChildByName("odl"); + assertNotNull(odl); + Set cases = odl.getCases(); + assertEquals(4, cases.size()); + + ChoiceCaseNode id = null; + ChoiceCaseNode node1 = null; + ChoiceCaseNode node2 = null; + ChoiceCaseNode node3 = null; + + for(ChoiceCaseNode ccn : cases) { + if("id".equals(ccn.getQName().getLocalName())) { + id = ccn; + } else if("node1".equals(ccn.getQName().getLocalName())) { + node1 = ccn; + } else if("node2".equals(ccn.getQName().getLocalName())) { + node2 = ccn; + } else if("node3".equals(ccn.getQName().getLocalName())) { + node3 = ccn; + } + } + + assertNotNull(id); + assertNotNull(node1); + assertNotNull(node2); + assertNotNull(node3); + + qnames = new QName[5]; + qnames[0] = q0; + qnames[1] = q1; + qnames[2] = q2; + qnames[3] = new QName(types1NS, types1Rev, t1, "odl"); + + // case id + qnames[4] = new QName(types1NS, types1Rev, t1, "id"); + expectedPath = new SchemaPath(Lists.newArrayList(qnames), true); + assertEquals(expectedPath, id.getPath()); + Set idChildren = id.getChildNodes(); + assertEquals(1, idChildren.size()); + + // case node1 + qnames[4] = new QName(types1NS, types1Rev, t1, "node1"); + expectedPath = new SchemaPath(Lists.newArrayList(qnames), true); + assertEquals(expectedPath, node1.getPath()); + Set node1Children = node1.getChildNodes(); + assertTrue(node1Children.isEmpty()); + + // case node2 + qnames[4] = new QName(types1NS, types1Rev, t1, "node2"); + expectedPath = new SchemaPath(Lists.newArrayList(qnames), true); + assertEquals(expectedPath, node2.getPath()); + Set node2Children = node2.getChildNodes(); + assertTrue(node2Children.isEmpty()); + + // case node3 + qnames[4] = new QName(types1NS, types1Rev, t1, "node3"); + expectedPath = new SchemaPath(Lists.newArrayList(qnames), true); + assertEquals(expectedPath, node3.getPath()); + Set node3Children = node3.getChildNodes(); + assertEquals(1, node3Children.size()); + + // test cases + qnames = new QName[6]; + qnames[0] = q0; + qnames[1] = q1; + qnames[2] = q2; + qnames[3] = new QName(types1NS, types1Rev, t1, "odl"); + + // case id child + qnames[4] = new QName(types1NS, types1Rev, t1, "id"); + qnames[5] = new QName(types1NS, types1Rev, t1, "id"); + LeafSchemaNode caseIdChild = (LeafSchemaNode)idChildren.iterator().next(); + assertNotNull(caseIdChild); + expectedPath = new SchemaPath(Lists.newArrayList(qnames), true); + assertEquals(expectedPath, caseIdChild.getPath()); + + // case node3 child + qnames[4] = new QName(types1NS, types1Rev, t1, "node3"); + qnames[5] = new QName(types1NS, types1Rev, t1, "node3"); + ContainerSchemaNode caseNode3Child = (ContainerSchemaNode)node3Children.iterator().next(); + assertNotNull(caseNode3Child); + expectedPath = new SchemaPath(Lists.newArrayList(qnames), true); + assertEquals(expectedPath, caseNode3Child.getPath()); + } + + @Test + public void testAugmentNodesTypeSchemaPath() throws Exception { + Module testModule = TestUtils.findModule(modules, "nodes"); + Set augments = testModule.getAugmentations(); + assertEquals(1, augments.size()); + AugmentationSchema augment = augments.iterator().next(); + + LeafSchemaNode ifcId = (LeafSchemaNode) augment.getDataChildByName("interface-id"); + Leafref ifcIdType = (Leafref) ifcId.getType(); + SchemaPath ifcIdTypeSchemaPath = ifcIdType.getPath(); + List ifcIdTypePath = ifcIdTypeSchemaPath.getPath(); + + Date expectedDate = simpleDateFormat.parse("2013-02-27"); + + QName q3 = new QName(types1NS, expectedDate, "data", "interface-id"); + assertEquals(q0, ifcIdTypePath.get(0)); + assertEquals(q1, ifcIdTypePath.get(1)); + assertEquals(q2, ifcIdTypePath.get(2)); + assertEquals(q3, ifcIdTypePath.get(3)); + + LeafSchemaNode myType = (LeafSchemaNode) augment.getDataChildByName("my-type"); + ExtendedType leafType = (ExtendedType) myType.getType(); + + testModule = TestUtils.findModule(modules, "types"); + TypeDefinition typedef = TestUtils.findTypedef(testModule.getTypeDefinitions(), "int32-ext2"); + + assertEquals(typedef, leafType); + } + +} diff --git a/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/test/java/org/opendaylight/controller/yang/parser/impl/GroupingTest.java b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/test/java/org/opendaylight/controller/yang/parser/impl/GroupingTest.java index 2442f51da3..88d7543520 100644 --- a/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/test/java/org/opendaylight/controller/yang/parser/impl/GroupingTest.java +++ b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/test/java/org/opendaylight/controller/yang/parser/impl/GroupingTest.java @@ -43,7 +43,7 @@ public class GroupingTest { @Test public void testRefine() { - Module testModule = TestUtils.findModule(modules, "types2"); + Module testModule = TestUtils.findModule(modules, "nodes"); ContainerSchemaNode peer = (ContainerSchemaNode) testModule.getDataChildByName("peer"); ContainerSchemaNode destination = (ContainerSchemaNode) peer.getDataChildByName("destination"); @@ -119,7 +119,7 @@ public class GroupingTest { @Test public void testGrouping() { - Module testModule = TestUtils.findModule(modules, "types2"); + Module testModule = TestUtils.findModule(modules, "custom"); Set groupings = testModule.getGroupings(); assertEquals(1, groupings.size()); GroupingDefinition grouping = groupings.iterator().next(); @@ -132,13 +132,15 @@ public class GroupingTest { // suffix _u = added by uses // suffix _g = defined in grouping - Module testModule = TestUtils.findModule(modules, "types2"); + Module testModule = TestUtils.findModule(modules, "custom"); // get grouping Set groupings = testModule.getGroupings(); assertEquals(1, groupings.size()); GroupingDefinition grouping = groupings.iterator().next(); + testModule = TestUtils.findModule(modules, "nodes"); + // get node containing uses ContainerSchemaNode peer = (ContainerSchemaNode) testModule.getDataChildByName("peer"); ContainerSchemaNode destination = (ContainerSchemaNode) peer.getDataChildByName("destination"); diff --git a/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/test/java/org/opendaylight/controller/yang/parser/impl/YangParserTest.java b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/test/java/org/opendaylight/controller/yang/parser/impl/YangParserTest.java index 3eff948d8b..f2478e741d 100644 --- a/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/test/java/org/opendaylight/controller/yang/parser/impl/YangParserTest.java +++ b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/test/java/org/opendaylight/controller/yang/parser/impl/YangParserTest.java @@ -34,7 +34,6 @@ import org.opendaylight.controller.yang.model.api.Deviation.Deviate; import org.opendaylight.controller.yang.model.api.ExtensionDefinition; import org.opendaylight.controller.yang.model.api.FeatureDefinition; import org.opendaylight.controller.yang.model.api.GroupingDefinition; -import org.opendaylight.controller.yang.model.api.LeafListSchemaNode; import org.opendaylight.controller.yang.model.api.LeafSchemaNode; import org.opendaylight.controller.yang.model.api.ListSchemaNode; import org.opendaylight.controller.yang.model.api.Module; @@ -52,42 +51,51 @@ import org.opendaylight.controller.yang.model.util.Decimal64; import org.opendaylight.controller.yang.model.util.ExtendedType; import org.opendaylight.controller.yang.model.util.Int16; import org.opendaylight.controller.yang.model.util.Int32; -import org.opendaylight.controller.yang.model.util.Int8; -import org.opendaylight.controller.yang.model.util.Leafref; import org.opendaylight.controller.yang.model.util.StringType; import org.opendaylight.controller.yang.model.util.Uint32; import org.opendaylight.controller.yang.model.util.UnionType; public class YangParserTest { + + private final URI nodesNS = URI.create("urn:simple.nodes.test"); + private final URI typesNS = URI.create("urn:simple.types.test"); + private final URI customNS = URI.create("urn:custom.nodes.test"); + private Date nodesRev; + private Date typesRev; + private Date customRev; + private final DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); private Set modules; @Before - public void init() throws FileNotFoundException { + public void init() throws FileNotFoundException, ParseException { + nodesRev = simpleDateFormat.parse("2013-02-27"); + typesRev = simpleDateFormat.parse("2013-07-03"); + customRev = simpleDateFormat.parse("2013-02-27"); + modules = TestUtils.loadModules(getClass().getResource("/model").getPath()); assertEquals(3, modules.size()); } @Test - public void testHeaders() { - Module test = TestUtils.findModule(modules, "types1"); + public void testHeaders() throws ParseException { + Module test = TestUtils.findModule(modules, "nodes"); - assertEquals("types1", test.getName()); + assertEquals("nodes", test.getName()); assertEquals("1", test.getYangVersion()); - URI expectedNamespace = URI.create("urn:simple.container.demo"); - assertEquals(expectedNamespace, test.getNamespace()); - assertEquals("t1", test.getPrefix()); + assertEquals(nodesNS, test.getNamespace()); + assertEquals("n", test.getPrefix()); Set imports = test.getImports(); assertEquals(2, imports.size()); - ModuleImport import2 = TestUtils.findImport(imports, "data"); - assertEquals("types2", import2.getModuleName()); - assertEquals(TestUtils.createDate("2013-02-27"), import2.getRevision()); + ModuleImport import2 = TestUtils.findImport(imports, "t"); + assertEquals("types", import2.getModuleName()); + assertEquals(typesRev, import2.getRevision()); - ModuleImport import3 = TestUtils.findImport(imports, "t3"); - assertEquals("types3", import3.getModuleName()); - assertEquals(TestUtils.createDate("2013-02-27"), import3.getRevision()); + ModuleImport import3 = TestUtils.findImport(imports, "c"); + assertEquals("custom", import3.getModuleName()); + assertEquals(customRev, import3.getRevision()); assertEquals("opendaylight", test.getOrganization()); assertEquals("http://www.opendaylight.org/", test.getContact()); @@ -98,12 +106,10 @@ public class YangParserTest { @Test public void testOrderingTypedef() { - Module test = TestUtils.findModule(modules, "types2"); + Module test = TestUtils.findModule(modules, "types"); Set> typedefs = test.getTypeDefinitions(); - String[] expectedOrder = new String[] { "my-base-int32-type", "my-custom-string", "my-decimal-type", - "my-decimal-type-ext", "my-int-type", "my-int-type-ext", "my-int-type2", "my-string-type", - "my-string-type-ext", "my-string-type2", "my-type1", "my-union", "my-union-ext", "nested-union1", - "nested-union2" }; + String[] expectedOrder = new String[] { "int32-ext1", "int32-ext2", "my-decimal-type", "my-union", + "my-union-ext", "nested-union2", "string-ext1", "string-ext2", "string-ext3", "string-ext4" }; String[] actualOrder = new String[typedefs.size()]; int i = 0; @@ -116,23 +122,31 @@ public class YangParserTest { @Test public void testOrderingChildNodes() { - Module test = TestUtils.findModule(modules, "types2"); - Set childNodes = test.getChildNodes(); - String[] expectedOrder = new String[] { "count", "if-name", "interfaces", "name", "nested-type-leaf", "peer", - "system" }; - String[] actualOrder = new String[childNodes.size()]; + Module test = TestUtils.findModule(modules, "nodes"); + AugmentationSchema augment1 = null; + for (AugmentationSchema as : test.getAugmentations()) { + if ("if:ifType='ds0'".equals(as.getWhenCondition().toString())) { + augment1 = as; + break; + } + } + assertNotNull(augment1); + + String[] expectedOrder = new String[] { "ds0ChannelNumber", "interface-id", "my-type", "odl", "schemas" }; + String[] actualOrder = new String[expectedOrder.length]; int i = 0; - for (DataSchemaNode child : childNodes) { - actualOrder[i] = child.getQName().getLocalName(); + for (DataSchemaNode augmentChild : augment1.getChildNodes()) { + actualOrder[i] = augmentChild.getQName().getLocalName(); i++; } + assertArrayEquals(expectedOrder, actualOrder); } @Test public void testOrderingNestedChildNodes() { - Module test = TestUtils.findModule(modules, "types2"); + Module test = TestUtils.findModule(modules, "custom"); Set groupings = test.getGroupings(); assertEquals(1, groupings.size()); GroupingDefinition target = groupings.iterator().next(); @@ -151,17 +165,15 @@ public class YangParserTest { @Test public void testParseContainer() { - Module test = TestUtils.findModule(modules, "types2"); - URI expectedNamespace = URI.create("urn:simple.types.data.demo"); - String expectedPrefix = "t2"; - Date expectedRevision = TestUtils.createDate("2013-02-27"); + Module test = TestUtils.findModule(modules, "types"); + URI expectedNamespace = URI.create("urn:simple.types.test"); + String expectedPrefix = "t"; ContainerSchemaNode interfaces = (ContainerSchemaNode) test.getDataChildByName("interfaces"); // test SchemaNode args - QName expectedQName = new QName(expectedNamespace, expectedRevision, expectedPrefix, "interfaces"); + QName expectedQName = new QName(expectedNamespace, typesRev, expectedPrefix, "interfaces"); assertEquals(expectedQName, interfaces.getQName()); - SchemaPath expectedPath = TestUtils.createPath(true, expectedNamespace, expectedRevision, expectedPrefix, - "interfaces"); + SchemaPath expectedPath = TestUtils.createPath(true, expectedNamespace, typesRev, expectedPrefix, "interfaces"); assertEquals(expectedPath, interfaces.getPath()); assertNull(interfaces.getDescription()); assertNull(interfaces.getReference()); @@ -192,19 +204,18 @@ public class YangParserTest { @Test public void testParseList() { - Module test = TestUtils.findModule(modules, "types2"); - URI expectedNamespace = URI.create("urn:simple.types.data.demo"); - String expectedPrefix = "t2"; - Date expectedRevision = TestUtils.createDate("2013-02-27"); + Module test = TestUtils.findModule(modules, "types"); + URI expectedNamespace = URI.create("urn:simple.types.test"); + String expectedPrefix = "t"; ContainerSchemaNode interfaces = (ContainerSchemaNode) test.getDataChildByName("interfaces"); ListSchemaNode ifEntry = (ListSchemaNode) interfaces.getDataChildByName("ifEntry"); // test SchemaNode args - QName expectedQName = new QName(expectedNamespace, expectedRevision, expectedPrefix, "ifEntry"); + QName expectedQName = new QName(expectedNamespace, typesRev, expectedPrefix, "ifEntry"); assertEquals(expectedQName, ifEntry.getQName()); - SchemaPath expectedPath = TestUtils.createPath(true, expectedNamespace, expectedRevision, expectedPrefix, - "interfaces", "ifEntry"); + SchemaPath expectedPath = TestUtils.createPath(true, expectedNamespace, typesRev, expectedPrefix, "interfaces", + "ifEntry"); assertEquals(expectedPath, ifEntry.getPath()); assertNull(ifEntry.getDescription()); assertNull(ifEntry.getReference()); @@ -224,7 +235,7 @@ public class YangParserTest { assertEquals(2, availableAugmentations.size()); // test ListSchemaNode args List expectedKey = new ArrayList(); - expectedKey.add(new QName(expectedNamespace, expectedRevision, expectedPrefix, "ifIndex")); + expectedKey.add(new QName(expectedNamespace, typesRev, expectedPrefix, "ifIndex")); assertEquals(expectedKey, ifEntry.getKeyDefinition()); assertFalse(ifEntry.isUserOrdered()); // test DataNodeContainer args @@ -239,237 +250,228 @@ public class YangParserTest { assertTrue(ifMtu.getType() instanceof Int32); } - @Test - public void testParseLeaf() throws ParseException { - Module test = TestUtils.findModule(modules, "types2"); - - // leaf if-name - LeafSchemaNode ifName = (LeafSchemaNode) test.getDataChildByName("if-name"); - Leafref ifNameType = (Leafref) ifName.getType(); - QName qname = ifNameType.getQName(); - - URI baseYangTypeNS = URI.create("urn:ietf:params:xml:ns:yang:1"); - assertEquals(baseYangTypeNS, qname.getNamespace()); - assertNull(qname.getRevision()); - assertEquals("", qname.getPrefix()); - assertEquals("leafref", qname.getLocalName()); - - // leaf name - LeafSchemaNode name = (LeafSchemaNode) test.getDataChildByName("name"); - StringType nameType = (StringType) name.getType(); - QName nameQName = nameType.getQName(); - - assertEquals(baseYangTypeNS, nameQName.getNamespace()); - assertNull(nameQName.getRevision()); - assertEquals("", nameQName.getPrefix()); - assertEquals("string", nameQName.getLocalName()); - - // leaf count - LeafSchemaNode count = (LeafSchemaNode) test.getDataChildByName("count"); - ExtendedType countType = (ExtendedType) count.getType(); - QName countTypeQName = countType.getQName(); - - URI expectedNS = URI.create("urn:simple.types.data.demo"); - Date expectedDate = simpleDateFormat.parse("2013-02-27"); - assertEquals(expectedNS, countTypeQName.getNamespace()); - assertEquals(expectedDate, countTypeQName.getRevision()); - assertEquals("t2", countTypeQName.getPrefix()); - assertEquals("int8", countTypeQName.getLocalName()); - - Int8 countTypeBase = (Int8) countType.getBaseType(); - QName countTypeBaseQName = countTypeBase.getQName(); - - assertEquals(baseYangTypeNS, countTypeBaseQName.getNamespace()); - assertNull(countTypeBaseQName.getRevision()); - assertEquals("", countTypeBaseQName.getPrefix()); - assertEquals("int8", countTypeBaseQName.getLocalName()); - } - - @Test - public void testAugmentResolving() { - // testfile1 - Module module1 = TestUtils.findModule(modules, "types1"); - - Set module1Augmentations = module1.getAugmentations(); - AugmentationSchema augment1 = module1Augmentations.iterator().next(); - LeafSchemaNode augmentedLeafDefinition = (LeafSchemaNode) augment1.getDataChildByName("ds0ChannelNumber"); - assertTrue(augmentedLeafDefinition.isAugmenting()); - - // testfile2 - Module module2 = TestUtils.findModule(modules, "types2"); - - ContainerSchemaNode interfaces = (ContainerSchemaNode) module2.getDataChildByName("interfaces"); - ListSchemaNode ifEntry = (ListSchemaNode) interfaces.getDataChildByName("ifEntry"); - ContainerSchemaNode augmentedContainer = (ContainerSchemaNode) ifEntry.getDataChildByName("augment-holder"); - - ContainerSchemaNode schemas = (ContainerSchemaNode) augmentedContainer.getDataChildByName("schemas"); - LeafSchemaNode linkleaf = (LeafSchemaNode) schemas.getDataChildByName("linkleaf"); - assertNotNull(linkleaf); - - // augmentation defined in testfile1 and augmentation returned from - // augmented container have to be same - Set augmentedContainerAugments = augmentedContainer.getAvailableAugmentations(); - AugmentationSchema augmentDefinition = augmentedContainerAugments.iterator().next(); - assertEquals(augment1, augmentDefinition); - - LeafSchemaNode augmentedLeaf = (LeafSchemaNode) augmentedContainer.getDataChildByName("ds0ChannelNumber"); - assertTrue(augmentedLeaf.isAugmenting()); - assertEquals(augmentedLeafDefinition, augmentedLeaf); - - Set ifEntryAugments = ifEntry.getAvailableAugmentations(); - assertEquals(2, ifEntryAugments.size()); - - // testfile3 - Module module3 = TestUtils.findModule(modules, "types3"); - - Set module3Augmentations = module3.getAugmentations(); - assertEquals(3, module3Augmentations.size()); - AugmentationSchema augment3 = null; - for (AugmentationSchema as : module3Augmentations) { - if ("if:ifType='ds0'".equals(as.getWhenCondition().toString())) { - augment3 = as; - } - } - ContainerSchemaNode augmentedContainerDefinition = (ContainerSchemaNode) augment3 - .getDataChildByName("augment-holder"); - assertTrue(augmentedContainerDefinition.isAugmenting()); - - // check - assertEquals(augmentedContainer, augmentedContainerDefinition); - assertEquals(augmentedLeaf, augmentedLeafDefinition); - } - - @Test - public void testAugmentTarget() { - Module test = TestUtils.findModule(modules, "types2"); - - ContainerSchemaNode interfaces = (ContainerSchemaNode) test.getDataChildByName("interfaces"); - ListSchemaNode ifEntry = (ListSchemaNode) interfaces.getDataChildByName("ifEntry"); - Set augmentations = ifEntry.getAvailableAugmentations(); - assertEquals(2, augmentations.size()); - - AugmentationSchema augment = null; - for (AugmentationSchema as : augmentations) { - if ("if:ifType='ds0'".equals(as.getWhenCondition().toString())) { - augment = as; - } - } - ContainerSchemaNode augmentHolder = (ContainerSchemaNode) augment.getDataChildByName("augment-holder"); - assertNotNull(augmentHolder); - assertTrue(augmentHolder.isAugmenting()); - QName augmentHolderQName = augmentHolder.getQName(); - assertEquals("augment-holder", augmentHolderQName.getLocalName()); - assertEquals("t3", augmentHolderQName.getPrefix()); - assertEquals("Description for augment holder", augmentHolder.getDescription()); - } - @Test public void testTypedefRangesResolving() throws ParseException { - Module testModule = TestUtils.findModule(modules, "types1"); + Module testModule = TestUtils.findModule(modules, "nodes"); + LeafSchemaNode int32Leaf = (LeafSchemaNode) testModule.getDataChildByName("int32-leaf"); - LeafSchemaNode testleaf = (LeafSchemaNode) testModule.getDataChildByName("testleaf"); - ExtendedType leafType = (ExtendedType) testleaf.getType(); + ExtendedType leafType = (ExtendedType) int32Leaf.getType(); QName leafTypeQName = leafType.getQName(); - assertEquals("my-type1", leafTypeQName.getLocalName()); - assertEquals("t1", leafTypeQName.getPrefix()); - assertEquals(URI.create("urn:simple.container.demo"), leafTypeQName.getNamespace()); - Date expectedDate = simpleDateFormat.parse("2013-02-27"); - assertEquals(expectedDate, leafTypeQName.getRevision()); - assertEquals(1, leafType.getRanges().size()); - - ExtendedType baseType = (ExtendedType) leafType.getBaseType(); - QName baseTypeQName = baseType.getQName(); - assertEquals("my-type1", baseTypeQName.getLocalName()); - assertEquals("t2", baseTypeQName.getPrefix()); - assertEquals(URI.create("urn:simple.types.data.demo"), baseTypeQName.getNamespace()); - assertEquals(expectedDate, baseTypeQName.getRevision()); - assertEquals(2, baseType.getRanges().size()); - + assertEquals("int32-ext2", leafTypeQName.getLocalName()); + assertEquals("n", leafTypeQName.getPrefix()); + assertEquals(nodesNS, leafTypeQName.getNamespace()); + assertEquals(nodesRev, leafTypeQName.getRevision()); + assertNull(leafType.getUnits()); + assertNull(leafType.getDefaultValue()); + assertTrue(leafType.getLengths().isEmpty()); + assertTrue(leafType.getPatterns().isEmpty()); List ranges = leafType.getRanges(); assertEquals(1, ranges.size()); RangeConstraint range = ranges.get(0); assertEquals(12L, range.getMin()); assertEquals(20L, range.getMax()); + + ExtendedType baseType = (ExtendedType) leafType.getBaseType(); + QName baseTypeQName = baseType.getQName(); + assertEquals("int32-ext2", baseTypeQName.getLocalName()); + assertEquals("t", baseTypeQName.getPrefix()); + assertEquals(typesNS, baseTypeQName.getNamespace()); + assertEquals(typesRev, baseTypeQName.getRevision()); + assertEquals("mile", baseType.getUnits()); + assertEquals("11", baseType.getDefaultValue()); + assertTrue(leafType.getLengths().isEmpty()); + assertTrue(leafType.getPatterns().isEmpty()); + List baseTypeRanges = baseType.getRanges(); + assertEquals(2, baseTypeRanges.size()); + RangeConstraint baseTypeRange1 = baseTypeRanges.get(0); + assertEquals(3L, baseTypeRange1.getMin()); + assertEquals(9L, baseTypeRange1.getMax()); + RangeConstraint baseTypeRange2 = baseTypeRanges.get(1); + assertEquals(11L, baseTypeRange2.getMin()); + assertEquals(20L, baseTypeRange2.getMax()); + + ExtendedType base = (ExtendedType) baseType.getBaseType(); + QName baseQName = base.getQName(); + assertEquals("int32-ext1", baseQName.getLocalName()); + assertEquals("t", baseQName.getPrefix()); + assertEquals(typesNS, baseQName.getNamespace()); + assertEquals(typesRev, baseQName.getRevision()); + assertNull(base.getUnits()); + assertNull(base.getDefaultValue()); + assertTrue(leafType.getLengths().isEmpty()); + assertTrue(leafType.getPatterns().isEmpty()); + List baseRanges = base.getRanges(); + assertEquals(1, baseRanges.size()); + RangeConstraint baseRange = baseRanges.get(0); + assertEquals(2L, baseRange.getMin()); + assertEquals(20L, baseRange.getMax()); + + assertTrue(base.getBaseType() instanceof Int32); } @Test public void testTypedefPatternsResolving() { - Module testModule = TestUtils.findModule(modules, "types1"); - - LeafSchemaNode testleaf = (LeafSchemaNode) testModule.getDataChildByName("test-string-leaf"); - ExtendedType testleafType = (ExtendedType) testleaf.getType(); - QName testleafTypeQName = testleafType.getQName(); - assertEquals("my-string-type-ext", testleafTypeQName.getLocalName()); - assertEquals("t2", testleafTypeQName.getPrefix()); - - List patterns = testleafType.getPatterns(); + Module testModule = TestUtils.findModule(modules, "nodes"); + LeafSchemaNode stringleaf = (LeafSchemaNode) testModule.getDataChildByName("string-leaf"); + + ExtendedType type = (ExtendedType) stringleaf.getType(); + QName typeQName = type.getQName(); + assertEquals("string-ext4", typeQName.getLocalName()); + assertEquals("t", typeQName.getPrefix()); + assertEquals(typesNS, typeQName.getNamespace()); + assertEquals(typesRev, typeQName.getRevision()); + assertNull(type.getUnits()); + assertNull(type.getDefaultValue()); + List patterns = type.getPatterns(); assertEquals(1, patterns.size()); PatternConstraint pattern = patterns.iterator().next(); assertEquals("[e-z]*", pattern.getRegularExpression()); - - ExtendedType baseType = (ExtendedType) testleafType.getBaseType(); - assertEquals("my-string-type2", baseType.getQName().getLocalName()); - - patterns = baseType.getPatterns(); + assertTrue(type.getLengths().isEmpty()); + assertTrue(type.getRanges().isEmpty()); + + ExtendedType baseType1 = (ExtendedType) type.getBaseType(); + QName baseType1QName = baseType1.getQName(); + assertEquals("string-ext3", baseType1QName.getLocalName()); + assertEquals("t", baseType1QName.getPrefix()); + assertEquals(typesNS, baseType1QName.getNamespace()); + assertEquals(typesRev, baseType1QName.getRevision()); + assertNull(baseType1.getUnits()); + assertNull(baseType1.getDefaultValue()); + patterns = baseType1.getPatterns(); assertEquals(1, patterns.size()); pattern = patterns.iterator().next(); assertEquals("[b-u]*", pattern.getRegularExpression()); - - List lengths = testleafType.getLengths(); - assertTrue(lengths.isEmpty()); + assertTrue(baseType1.getLengths().isEmpty()); + assertTrue(baseType1.getRanges().isEmpty()); + + ExtendedType baseType2 = (ExtendedType) baseType1.getBaseType(); + QName baseType2QName = baseType2.getQName(); + assertEquals("string-ext2", baseType2QName.getLocalName()); + assertEquals("t", baseType2QName.getPrefix()); + assertEquals(typesNS, baseType2QName.getNamespace()); + assertEquals(typesRev, baseType2QName.getRevision()); + assertNull(baseType2.getUnits()); + assertNull(baseType2.getDefaultValue()); + assertTrue(baseType2.getPatterns().isEmpty()); + List baseType2Lengths = baseType2.getLengths(); + assertEquals(1, baseType2Lengths.size()); + LengthConstraint length = baseType2Lengths.get(0); + assertEquals(6L, length.getMin()); + assertEquals(10L, length.getMax()); + assertTrue(baseType2.getRanges().isEmpty()); + + ExtendedType baseType3 = (ExtendedType) baseType2.getBaseType(); + QName baseType3QName = baseType3.getQName(); + assertEquals("string-ext1", baseType3QName.getLocalName()); + assertEquals("t", baseType3QName.getPrefix()); + assertEquals(typesNS, baseType3QName.getNamespace()); + assertEquals(typesRev, baseType3QName.getRevision()); + assertNull(baseType3.getUnits()); + assertNull(baseType3.getDefaultValue()); + patterns = baseType3.getPatterns(); + assertEquals(1, patterns.size()); + pattern = patterns.iterator().next(); + assertEquals("[a-k]*", pattern.getRegularExpression()); + List baseType3Lengths = baseType3.getLengths(); + assertEquals(1, baseType3Lengths.size()); + length = baseType3Lengths.get(0); + assertEquals(5L, length.getMin()); + assertEquals(11L, length.getMax()); + assertTrue(baseType3.getRanges().isEmpty()); + + assertTrue(baseType3.getBaseType() instanceof StringType); } @Test public void testTypedefLengthsResolving() { - Module testModule = TestUtils.findModule(modules, "types1"); - - LeafSchemaNode testleaf = (LeafSchemaNode) testModule.getDataChildByName("leaf-with-length"); - ExtendedType testleafType = (ExtendedType) testleaf.getType(); - assertEquals("my-string-type", testleafType.getQName().getLocalName()); - - List lengths = testleafType.getLengths(); - assertEquals(1, lengths.size()); - - LengthConstraint length = lengths.get(0); + Module testModule = TestUtils.findModule(modules, "nodes"); + + LeafSchemaNode lengthLeaf = (LeafSchemaNode) testModule.getDataChildByName("length-leaf"); + ExtendedType type = (ExtendedType) lengthLeaf.getType(); + + QName typeQName = type.getQName(); + assertEquals("string-ext2", typeQName.getLocalName()); + assertEquals("n", typeQName.getPrefix()); + assertEquals(nodesNS, typeQName.getNamespace()); + assertEquals(nodesRev, typeQName.getRevision()); + assertNull(type.getUnits()); + assertNull(type.getDefaultValue()); + assertTrue(type.getPatterns().isEmpty()); + List typeLengths = type.getLengths(); + assertEquals(1, typeLengths.size()); + LengthConstraint length = typeLengths.get(0); assertEquals(7L, length.getMin()); assertEquals(10L, length.getMax()); - } - - @Test - public void testTypeDef() { - Module testModule = TestUtils.findModule(modules, "types2"); - - LeafSchemaNode testleaf = (LeafSchemaNode) testModule.getDataChildByName("nested-type-leaf"); - ExtendedType testleafType = (ExtendedType) testleaf.getType(); - assertEquals("my-type1", testleafType.getQName().getLocalName()); - - ExtendedType baseType = (ExtendedType) testleafType.getBaseType(); - assertEquals("my-base-int32-type", baseType.getQName().getLocalName()); - - Int32 int32Type = (Int32) baseType.getBaseType(); - QName qname = int32Type.getQName(); - assertEquals(URI.create("urn:ietf:params:xml:ns:yang:1"), qname.getNamespace()); - assertNull(qname.getRevision()); - assertEquals("", qname.getPrefix()); - assertEquals("int32", qname.getLocalName()); - List ranges = baseType.getRanges(); - assertEquals(1, ranges.size()); - RangeConstraint range = ranges.get(0); - assertEquals(2L, range.getMin()); - assertEquals(20L, range.getMax()); + assertTrue(type.getRanges().isEmpty()); + + ExtendedType baseType1 = (ExtendedType) type.getBaseType(); + QName baseType1QName = baseType1.getQName(); + assertEquals("string-ext2", baseType1QName.getLocalName()); + assertEquals("t", baseType1QName.getPrefix()); + assertEquals(typesNS, baseType1QName.getNamespace()); + assertEquals(typesRev, baseType1QName.getRevision()); + assertNull(baseType1.getUnits()); + assertNull(baseType1.getDefaultValue()); + assertTrue(baseType1.getPatterns().isEmpty()); + List baseType2Lengths = baseType1.getLengths(); + assertEquals(1, baseType2Lengths.size()); + length = baseType2Lengths.get(0); + assertEquals(6L, length.getMin()); + assertEquals(10L, length.getMax()); + assertTrue(baseType1.getRanges().isEmpty()); + + ExtendedType baseType2 = (ExtendedType) baseType1.getBaseType(); + QName baseType2QName = baseType2.getQName(); + assertEquals("string-ext1", baseType2QName.getLocalName()); + assertEquals("t", baseType2QName.getPrefix()); + assertEquals(typesNS, baseType2QName.getNamespace()); + assertEquals(typesRev, baseType2QName.getRevision()); + assertNull(baseType2.getUnits()); + assertNull(baseType2.getDefaultValue()); + List patterns = baseType2.getPatterns(); + assertEquals(1, patterns.size()); + PatternConstraint pattern = patterns.iterator().next(); + assertEquals("[a-k]*", pattern.getRegularExpression()); + List baseType3Lengths = baseType2.getLengths(); + assertEquals(1, baseType3Lengths.size()); + length = baseType3Lengths.get(0); + assertEquals(5L, length.getMin()); + assertEquals(11L, length.getMax()); + assertTrue(baseType2.getRanges().isEmpty()); + + assertTrue(baseType2.getBaseType() instanceof StringType); } @Test public void testTypedefDecimal1() { - Module testModule = TestUtils.findModule(modules, "types1"); + Module testModule = TestUtils.findModule(modules, "nodes"); + LeafSchemaNode testleaf = (LeafSchemaNode) testModule.getDataChildByName("decimal-leaf"); - LeafSchemaNode testleaf = (LeafSchemaNode) testModule.getDataChildByName("test-decimal-leaf"); ExtendedType type = (ExtendedType) testleaf.getType(); + QName typeQName = type.getQName(); + assertEquals("my-decimal-type", typeQName.getLocalName()); + assertEquals("n", typeQName.getPrefix()); + assertEquals(nodesNS, typeQName.getNamespace()); + assertEquals(nodesRev, typeQName.getRevision()); + assertNull(type.getUnits()); + assertNull(type.getDefaultValue()); assertEquals(4, (int) type.getFractionDigits()); + assertTrue(type.getLengths().isEmpty()); + assertTrue(type.getPatterns().isEmpty()); + assertTrue(type.getRanges().isEmpty()); ExtendedType typeBase = (ExtendedType) type.getBaseType(); - assertEquals("my-decimal-type", typeBase.getQName().getLocalName()); + QName typeBaseQName = typeBase.getQName(); + assertEquals("my-decimal-type", typeBaseQName.getLocalName()); + assertEquals("t", typeBaseQName.getPrefix()); + assertEquals(typesNS, typeBaseQName.getNamespace()); + assertEquals(typesRev, typeBaseQName.getRevision()); + assertNull(typeBase.getUnits()); + assertNull(typeBase.getDefaultValue()); assertNull(typeBase.getFractionDigits()); + assertTrue(typeBase.getLengths().isEmpty()); + assertTrue(typeBase.getPatterns().isEmpty()); + assertTrue(typeBase.getRanges().isEmpty()); Decimal64 decimal = (Decimal64) typeBase.getBaseType(); assertEquals(6, (int) decimal.getFractionDigits()); @@ -477,147 +479,195 @@ public class YangParserTest { @Test public void testTypedefDecimal2() { - Module testModule = TestUtils.findModule(modules, "types1"); + Module testModule = TestUtils.findModule(modules, "nodes"); + LeafSchemaNode testleaf = (LeafSchemaNode) testModule.getDataChildByName("decimal-leaf2"); - LeafSchemaNode testleaf = (LeafSchemaNode) testModule.getDataChildByName("test-decimal-leaf2"); - TypeDefinition baseType = testleaf.getType().getBaseType(); - assertTrue(testleaf.getType().getBaseType() instanceof Decimal64); - Decimal64 baseTypeCast = (Decimal64) baseType; - assertEquals(5, (int) baseTypeCast.getFractionDigits()); + ExtendedType type = (ExtendedType) testleaf.getType(); + QName typeQName = type.getQName(); + assertEquals("my-decimal-type", typeQName.getLocalName()); + assertEquals("t", typeQName.getPrefix()); + assertEquals(typesNS, typeQName.getNamespace()); + assertEquals(typesRev, typeQName.getRevision()); + assertNull(type.getUnits()); + assertNull(type.getDefaultValue()); + assertNull(type.getFractionDigits()); + assertTrue(type.getLengths().isEmpty()); + assertTrue(type.getPatterns().isEmpty()); + assertTrue(type.getRanges().isEmpty()); + + Decimal64 baseTypeDecimal = (Decimal64) type.getBaseType(); + assertEquals(6, (int) baseTypeDecimal.getFractionDigits()); } @Test public void testTypedefUnion() { - Module testModule = TestUtils.findModule(modules, "types1"); - - LeafSchemaNode testleaf = (LeafSchemaNode) testModule.getDataChildByName("union-leaf"); - ExtendedType testleafType = (ExtendedType) testleaf.getType(); - assertEquals("my-union-ext", testleafType.getQName().getLocalName()); - - ExtendedType baseType = (ExtendedType) testleafType.getBaseType(); - assertEquals("my-union", baseType.getQName().getLocalName()); - - UnionType unionBase = (UnionType) baseType.getBaseType(); + Module testModule = TestUtils.findModule(modules, "nodes"); + LeafSchemaNode unionleaf = (LeafSchemaNode) testModule.getDataChildByName("union-leaf"); + + ExtendedType type = (ExtendedType) unionleaf.getType(); + QName typeQName = type.getQName(); + assertEquals("my-union-ext", typeQName.getLocalName()); + assertEquals("t", typeQName.getPrefix()); + assertEquals(typesNS, typeQName.getNamespace()); + assertEquals(typesRev, typeQName.getRevision()); + assertNull(type.getUnits()); + assertNull(type.getDefaultValue()); + assertNull(type.getFractionDigits()); + assertTrue(type.getLengths().isEmpty()); + assertTrue(type.getPatterns().isEmpty()); + assertTrue(type.getRanges().isEmpty()); + + ExtendedType baseType = (ExtendedType) type.getBaseType(); + QName baseTypeQName = baseType.getQName(); + assertEquals("my-union", baseTypeQName.getLocalName()); + assertEquals("t", baseTypeQName.getPrefix()); + assertEquals(typesNS, baseTypeQName.getNamespace()); + assertEquals(typesRev, baseTypeQName.getRevision()); + assertNull(baseType.getUnits()); + assertNull(baseType.getDefaultValue()); + assertNull(baseType.getFractionDigits()); + assertTrue(baseType.getLengths().isEmpty()); + assertTrue(baseType.getPatterns().isEmpty()); + assertTrue(baseType.getRanges().isEmpty()); + + UnionType unionType = (UnionType) baseType.getBaseType(); + List> unionTypes = unionType.getTypes(); + assertEquals(2, unionTypes.size()); - List> unionTypes = unionBase.getTypes(); ExtendedType unionType1 = (ExtendedType) unionTypes.get(0); + QName unionType1QName = baseType.getQName(); + assertEquals("my-union", unionType1QName.getLocalName()); + assertEquals("t", unionType1QName.getPrefix()); + assertEquals(typesNS, unionType1QName.getNamespace()); + assertEquals(typesRev, unionType1QName.getRevision()); + assertNull(unionType1.getUnits()); + assertNull(unionType1.getDefaultValue()); + assertNull(unionType1.getFractionDigits()); + assertTrue(unionType1.getLengths().isEmpty()); + assertTrue(unionType1.getPatterns().isEmpty()); List ranges = unionType1.getRanges(); assertEquals(1, ranges.size()); RangeConstraint range = ranges.get(0); assertEquals(1L, range.getMin()); assertEquals(100L, range.getMax()); - assertTrue(unionType1.getBaseType() instanceof Int16); - assertTrue(unionTypes.get(1) instanceof Int32); - } - - @Test - public void testNestedUnionResolving1() { - Module testModule = TestUtils.findModule(modules, "types1"); - - LeafSchemaNode testleaf = (LeafSchemaNode) testModule.getDataChildByName("nested-union-leaf"); - - ExtendedType nestedUnion1 = (ExtendedType) testleaf.getType(); - assertEquals("nested-union1", nestedUnion1.getQName().getLocalName()); - - ExtendedType nestedUnion2 = (ExtendedType) nestedUnion1.getBaseType(); - assertEquals("nested-union2", nestedUnion2.getQName().getLocalName()); - - UnionType unionType1 = (UnionType) nestedUnion2.getBaseType(); - List> unionTypes = unionType1.getTypes(); - assertEquals(2, unionTypes.size()); - assertTrue(unionTypes.get(0) instanceof StringType); - assertTrue(unionTypes.get(1) instanceof ExtendedType); - - ExtendedType extendedUnion = (ExtendedType) unionTypes.get(1); - ExtendedType extendedUnionBase = (ExtendedType) extendedUnion.getBaseType(); - assertEquals("my-union", extendedUnionBase.getQName().getLocalName()); - UnionType extendedTargetUnion = (UnionType) extendedUnionBase.getBaseType(); - List> extendedTargetTypes = extendedTargetUnion.getTypes(); - assertTrue(extendedTargetTypes.get(0).getBaseType() instanceof Int16); - assertTrue(extendedTargetTypes.get(1) instanceof Int32); - - ExtendedType int16 = (ExtendedType) extendedTargetTypes.get(0); - assertTrue(int16.getBaseType() instanceof Int16); - List ranges = int16.getRanges(); - assertEquals(1, ranges.size()); - RangeConstraint range = ranges.get(0); - assertEquals(1L, range.getMin()); - assertEquals(100L, range.getMax()); + assertTrue(unionTypes.get(1) instanceof Int32); } @Test - public void testNestedUnionResolving2() { - Module testModule = TestUtils.findModule(modules, "types1"); - + public void testNestedUnionResolving() { + Module testModule = TestUtils.findModule(modules, "nodes"); LeafSchemaNode testleaf = (LeafSchemaNode) testModule.getDataChildByName("custom-union-leaf"); - ExtendedType testleafType = (ExtendedType) testleaf.getType(); - QName testleafTypeQName = testleafType.getQName(); - assertEquals(URI.create("urn:simple.container.demo.test"), testleafTypeQName.getNamespace()); - assertEquals(TestUtils.createDate("2013-02-27"), testleafTypeQName.getRevision()); - assertEquals("t3", testleafTypeQName.getPrefix()); + ExtendedType type = (ExtendedType) testleaf.getType(); + QName testleafTypeQName = type.getQName(); + assertEquals(customNS, testleafTypeQName.getNamespace()); + assertEquals(customRev, testleafTypeQName.getRevision()); + assertEquals("c", testleafTypeQName.getPrefix()); assertEquals("union1", testleafTypeQName.getLocalName()); + assertNull(type.getUnits()); + assertNull(type.getDefaultValue()); + assertNull(type.getFractionDigits()); + assertTrue(type.getLengths().isEmpty()); + assertTrue(type.getPatterns().isEmpty()); + assertTrue(type.getRanges().isEmpty()); - ExtendedType union2 = (ExtendedType) testleafType.getBaseType(); - QName union2QName = union2.getQName(); - assertEquals(URI.create("urn:simple.container.demo.test"), union2QName.getNamespace()); - assertEquals(TestUtils.createDate("2013-02-27"), union2QName.getRevision()); - assertEquals("t3", union2QName.getPrefix()); - assertEquals("union2", union2QName.getLocalName()); + ExtendedType typeBase = (ExtendedType) type.getBaseType(); + QName typeBaseQName = typeBase.getQName(); + assertEquals(customNS, typeBaseQName.getNamespace()); + assertEquals(customRev, typeBaseQName.getRevision()); + assertEquals("c", typeBaseQName.getPrefix()); + assertEquals("union2", typeBaseQName.getLocalName()); + assertNull(typeBase.getUnits()); + assertNull(typeBase.getDefaultValue()); + assertNull(typeBase.getFractionDigits()); + assertTrue(typeBase.getLengths().isEmpty()); + assertTrue(typeBase.getPatterns().isEmpty()); + assertTrue(typeBase.getRanges().isEmpty()); - UnionType union2Base = (UnionType) union2.getBaseType(); - List> unionTypes = union2Base.getTypes(); + UnionType union = (UnionType) typeBase.getBaseType(); + List> unionTypes = union.getTypes(); assertEquals(2, unionTypes.size()); assertTrue(unionTypes.get(0) instanceof Int32); assertTrue(unionTypes.get(1) instanceof ExtendedType); - ExtendedType nestedUnion2 = (ExtendedType) unionTypes.get(1); - QName nestedUnion2QName = nestedUnion2.getQName(); - assertEquals(URI.create("urn:simple.types.data.demo"), nestedUnion2QName.getNamespace()); - assertEquals(TestUtils.createDate("2013-02-27"), nestedUnion2QName.getRevision()); - assertEquals("t2", nestedUnion2QName.getPrefix()); - assertEquals("nested-union2", nestedUnion2QName.getLocalName()); - - UnionType nestedUnion2Base = (UnionType) nestedUnion2.getBaseType(); - List> nestedUnion2Types = nestedUnion2Base.getTypes(); + ExtendedType unionType1 = (ExtendedType) unionTypes.get(1); + QName uniontType1QName = unionType1.getQName(); + assertEquals(typesNS, uniontType1QName.getNamespace()); + assertEquals(typesRev, uniontType1QName.getRevision()); + assertEquals("t", uniontType1QName.getPrefix()); + assertEquals("nested-union2", uniontType1QName.getLocalName()); + assertNull(unionType1.getUnits()); + assertNull(unionType1.getDefaultValue()); + assertNull(unionType1.getFractionDigits()); + assertTrue(unionType1.getLengths().isEmpty()); + assertTrue(unionType1.getPatterns().isEmpty()); + assertTrue(unionType1.getRanges().isEmpty()); + + UnionType nestedUnion = (UnionType) unionType1.getBaseType(); + List> nestedUnion2Types = nestedUnion.getTypes(); assertEquals(2, nestedUnion2Types.size()); assertTrue(nestedUnion2Types.get(0) instanceof StringType); assertTrue(nestedUnion2Types.get(1) instanceof ExtendedType); ExtendedType myUnionExt = (ExtendedType) nestedUnion2Types.get(1); QName myUnionExtQName = myUnionExt.getQName(); - assertEquals(URI.create("urn:simple.types.data.demo"), myUnionExtQName.getNamespace()); - assertEquals(TestUtils.createDate("2013-02-27"), myUnionExtQName.getRevision()); - assertEquals("t2", myUnionExtQName.getPrefix()); + assertEquals(typesNS, myUnionExtQName.getNamespace()); + assertEquals(typesRev, myUnionExtQName.getRevision()); + assertEquals("t", myUnionExtQName.getPrefix()); assertEquals("my-union-ext", myUnionExtQName.getLocalName()); + assertNull(myUnionExt.getUnits()); + assertNull(myUnionExt.getDefaultValue()); + assertNull(myUnionExt.getFractionDigits()); + assertTrue(myUnionExt.getLengths().isEmpty()); + assertTrue(myUnionExt.getPatterns().isEmpty()); + assertTrue(myUnionExt.getRanges().isEmpty()); ExtendedType myUnion = (ExtendedType) myUnionExt.getBaseType(); QName myUnionQName = myUnion.getQName(); - assertEquals(URI.create("urn:simple.types.data.demo"), myUnionQName.getNamespace()); - assertEquals(TestUtils.createDate("2013-02-27"), myUnionQName.getRevision()); - assertEquals("t2", myUnionQName.getPrefix()); + assertEquals(typesNS, myUnionQName.getNamespace()); + assertEquals(typesRev, myUnionQName.getRevision()); + assertEquals("t", myUnionQName.getPrefix()); assertEquals("my-union", myUnionQName.getLocalName()); + assertNull(myUnion.getUnits()); + assertNull(myUnion.getDefaultValue()); + assertNull(myUnion.getFractionDigits()); + assertTrue(myUnion.getLengths().isEmpty()); + assertTrue(myUnion.getPatterns().isEmpty()); + assertTrue(myUnion.getRanges().isEmpty()); UnionType myUnionBase = (UnionType) myUnion.getBaseType(); List> myUnionBaseTypes = myUnionBase.getTypes(); assertEquals(2, myUnionBaseTypes.size()); - assertTrue(myUnionBaseTypes.get(0).getBaseType() instanceof Int16); + assertTrue(myUnionBaseTypes.get(0) instanceof ExtendedType); assertTrue(myUnionBaseTypes.get(1) instanceof Int32); - ExtendedType int16 = (ExtendedType) myUnionBaseTypes.get(0); - List ranges = int16.getRanges(); + + ExtendedType int16Ext = (ExtendedType) myUnionBaseTypes.get(0); + QName int16ExtQName = int16Ext.getQName(); + assertEquals(typesNS, int16ExtQName.getNamespace()); + assertEquals(typesRev, int16ExtQName.getRevision()); + assertEquals("t", int16ExtQName.getPrefix()); + assertEquals("int16", int16ExtQName.getLocalName()); + assertNull(int16Ext.getUnits()); + assertNull(int16Ext.getDefaultValue()); + assertNull(int16Ext.getFractionDigits()); + assertTrue(int16Ext.getLengths().isEmpty()); + assertTrue(int16Ext.getPatterns().isEmpty()); + List ranges = int16Ext.getRanges(); assertEquals(1, ranges.size()); RangeConstraint range = ranges.get(0); assertEquals(1L, range.getMin()); assertEquals(100L, range.getMax()); + + assertTrue(int16Ext.getBaseType() instanceof Int16); } @Test public void testChoice() { - Module testModule = TestUtils.findModule(modules, "types1"); - ContainerSchemaNode peer = (ContainerSchemaNode) testModule.getDataChildByName("transfer"); - ChoiceNode how = (ChoiceNode) peer.getDataChildByName("how"); + Module testModule = TestUtils.findModule(modules, "nodes"); + ContainerSchemaNode transfer = (ContainerSchemaNode) testModule.getDataChildByName("transfer"); + ChoiceNode how = (ChoiceNode) transfer.getDataChildByName("how"); Set cases = how.getCases(); assertEquals(5, cases.size()); ChoiceCaseNode input = null; @@ -637,26 +687,52 @@ public class YangParserTest { @Test public void testAnyXml() { - Module testModule = TestUtils.findModule(modules, "types1"); + Module testModule = TestUtils.findModule(modules, "nodes"); AnyXmlSchemaNode data = (AnyXmlSchemaNode) testModule.getDataChildByName("data"); - assertNotNull(data); + assertNotNull("anyxml data not found", data); + + // test SchemaNode args + QName qname = data.getQName(); + assertEquals("data", qname.getLocalName()); + assertEquals("n", qname.getPrefix()); + assertEquals(nodesNS, qname.getNamespace()); + assertEquals(nodesRev, qname.getRevision()); + assertTrue(data.getDescription().contains("Copy of the source typesstore subset that matched")); + assertNull(data.getReference()); + assertEquals(Status.OBSOLETE, data.getStatus()); + assertEquals(0, data.getUnknownSchemaNodes().size()); + // test DataSchemaNode args + assertFalse(data.isAugmenting()); + assertTrue(data.isConfiguration()); + ConstraintDefinition constraints = data.getConstraints(); + assertNull(constraints.getWhenCondition()); + assertEquals(0, constraints.getMustConstraints().size()); + assertFalse(constraints.isMandatory()); + assertNull(constraints.getMinElements()); + assertNull(constraints.getMaxElements()); } @Test public void testDeviation() { - Module testModule = TestUtils.findModule(modules, "types1"); + Module testModule = TestUtils.findModule(modules, "nodes"); Set deviations = testModule.getDeviations(); assertEquals(1, deviations.size()); - Deviation dev = deviations.iterator().next(); - SchemaPath expectedPath = TestUtils.createPath(true, null, null, "data", "system", "user"); + + assertEquals("system/user ref", dev.getReference()); + + List path = new ArrayList(); + path.add(new QName(typesNS, typesRev, "t", "interfaces")); + path.add(new QName(typesNS, typesRev, "t", "ifEntry")); + SchemaPath expectedPath = new SchemaPath(path, true); + assertEquals(expectedPath, dev.getTargetPath()); assertEquals(Deviate.ADD, dev.getDeviate()); } @Test public void testUnknownNode() { - Module testModule = TestUtils.findModule(modules, "types3"); + Module testModule = TestUtils.findModule(modules, "custom"); ContainerSchemaNode network = (ContainerSchemaNode) testModule.getDataChildByName("network"); List unknownNodes = network.getUnknownSchemaNodes(); assertEquals(1, unknownNodes.size()); @@ -667,37 +743,34 @@ public class YangParserTest { @Test public void testFeature() { - Module testModule = TestUtils.findModule(modules, "types3"); + Module testModule = TestUtils.findModule(modules, "custom"); Set features = testModule.getFeatures(); assertEquals(1, features.size()); } @Test public void testExtension() { - Module testModule = TestUtils.findModule(modules, "types3"); + Module testModule = TestUtils.findModule(modules, "custom"); List extensions = testModule.getExtensionSchemaNodes(); assertEquals(1, extensions.size()); ExtensionDefinition extension = extensions.get(0); assertEquals("name", extension.getArgument()); - assertFalse(extension.isYinElement()); + assertTrue(extension.isYinElement()); } @Test public void testNotification() { - Module testModule = TestUtils.findModule(modules, "types3"); - URI expectedNamespace = URI.create("urn:simple.container.demo.test"); - String expectedPrefix = "t3"; - Date expectedRevision = TestUtils.createDate("2013-02-27"); + Module testModule = TestUtils.findModule(modules, "custom"); + String expectedPrefix = "c"; Set notifications = testModule.getNotifications(); assertEquals(1, notifications.size()); NotificationDefinition notification = notifications.iterator().next(); // test SchemaNode args - QName expectedQName = new QName(expectedNamespace, expectedRevision, expectedPrefix, "event"); + QName expectedQName = new QName(customNS, customRev, expectedPrefix, "event"); assertEquals(expectedQName, notification.getQName()); - SchemaPath expectedPath = TestUtils.createPath(true, expectedNamespace, expectedRevision, expectedPrefix, - "event"); + SchemaPath expectedPath = TestUtils.createPath(true, customNS, customRev, expectedPrefix, "event"); assertEquals(expectedPath, notification.getPath()); assertNull(notification.getDescription()); assertNull(notification.getReference()); @@ -719,7 +792,7 @@ public class YangParserTest { @Test public void testRpc() { - Module testModule = TestUtils.findModule(modules, "types3"); + Module testModule = TestUtils.findModule(modules, "custom"); Set rpcs = testModule.getRpcs(); assertEquals(1, rpcs.size()); @@ -735,66 +808,19 @@ public class YangParserTest { assertNotNull(output.getDataChildByName("data")); } - @Test - public void testAugmentNodesTypesSchemaPath() throws Exception { - Module testModule = TestUtils.findModule(modules, "types1"); - Set augments = testModule.getAugmentations(); - assertEquals(1, augments.size()); - AugmentationSchema augment = augments.iterator().next(); - - LeafSchemaNode ifcId = (LeafSchemaNode) augment.getDataChildByName("interface-id"); - Leafref ifcIdType = (Leafref) ifcId.getType(); - SchemaPath ifcIdTypeSchemaPath = ifcIdType.getPath(); - List ifcIdTypePath = ifcIdTypeSchemaPath.getPath(); - - URI types1URI = URI.create("urn:simple.container.demo"); - URI types2URI = URI.create("urn:simple.types.data.demo"); - URI types3URI = URI.create("urn:simple.container.demo.test"); - Date expectedDate = simpleDateFormat.parse("2013-02-27"); - - QName q0 = new QName(types2URI, expectedDate, "data", "interfaces"); - QName q1 = new QName(types2URI, expectedDate, "data", "ifEntry"); - QName q2 = new QName(types3URI, expectedDate, "data", "augment-holder"); - QName q3 = new QName(types1URI, expectedDate, "data", "interface-id"); - assertEquals(q0, ifcIdTypePath.get(0)); - assertEquals(q1, ifcIdTypePath.get(1)); - assertEquals(q2, ifcIdTypePath.get(2)); - assertEquals(q3, ifcIdTypePath.get(3)); - - LeafListSchemaNode higherLayer = (LeafListSchemaNode) augment.getDataChildByName("higher-layer-if"); - Leafref higherLayerType = (Leafref) higherLayer.getType(); - SchemaPath higherLayerTypeSchemaPath = higherLayerType.getPath(); - List higherLayerTypePath = higherLayerTypeSchemaPath.getPath(); - assertEquals(q0, higherLayerTypePath.get(0)); - assertEquals(q1, higherLayerTypePath.get(1)); - assertEquals(q2, higherLayerTypePath.get(2)); - q3 = new QName(types1URI, expectedDate, "data", "higher-layer-if"); - assertEquals(q3, higherLayerTypePath.get(3)); - - LeafSchemaNode myType = (LeafSchemaNode) augment.getDataChildByName("my-type"); - ExtendedType leafType = (ExtendedType) myType.getType(); - - testModule = TestUtils.findModule(modules, "types2"); - TypeDefinition typedef = TestUtils.findTypedef(testModule.getTypeDefinitions(), "my-type1"); - - assertEquals(typedef, leafType); - } - @Test public void testTypePath() throws ParseException { - Module test = TestUtils.findModule(modules, "types2"); + Module test = TestUtils.findModule(modules, "types"); Set> types = test.getTypeDefinitions(); // my-base-int32-type - ExtendedType int32Typedef = (ExtendedType) TestUtils.findTypedef(types, "my-base-int32-type"); + ExtendedType int32Typedef = (ExtendedType) TestUtils.findTypedef(types, "int32-ext1"); QName int32TypedefQName = int32Typedef.getQName(); - URI expectedNS = URI.create("urn:simple.types.data.demo"); - Date expectedDate = simpleDateFormat.parse("2013-02-27"); - assertEquals(expectedNS, int32TypedefQName.getNamespace()); - assertEquals(expectedDate, int32TypedefQName.getRevision()); - assertEquals("t2", int32TypedefQName.getPrefix()); - assertEquals("my-base-int32-type", int32TypedefQName.getLocalName()); + assertEquals(typesNS, int32TypedefQName.getNamespace()); + assertEquals(typesRev, int32TypedefQName.getRevision()); + assertEquals("t", int32TypedefQName.getPrefix()); + assertEquals("int32-ext1", int32TypedefQName.getLocalName()); SchemaPath typeSchemaPath = int32Typedef.getPath(); List typePath = typeSchemaPath.getPath(); @@ -818,18 +844,16 @@ public class YangParserTest { @Test public void testTypePath2() throws ParseException { - Module test = TestUtils.findModule(modules, "types2"); + Module test = TestUtils.findModule(modules, "types"); Set> types = test.getTypeDefinitions(); // my-base-int32-type ExtendedType myDecType = (ExtendedType) TestUtils.findTypedef(types, "my-decimal-type"); QName myDecTypeQName = myDecType.getQName(); - URI expectedNS = URI.create("urn:simple.types.data.demo"); - Date expectedDate = simpleDateFormat.parse("2013-02-27"); - assertEquals(expectedNS, myDecTypeQName.getNamespace()); - assertEquals(expectedDate, myDecTypeQName.getRevision()); - assertEquals("t2", myDecTypeQName.getPrefix()); + assertEquals(typesNS, myDecTypeQName.getNamespace()); + assertEquals(typesRev, myDecTypeQName.getRevision()); + assertEquals("t", myDecTypeQName.getPrefix()); assertEquals("my-decimal-type", myDecTypeQName.getLocalName()); SchemaPath typeSchemaPath = myDecType.getPath(); diff --git a/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/test/java/org/opendaylight/controller/yang/parser/impl/YangParserWithContextTest.java b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/test/java/org/opendaylight/controller/yang/parser/impl/YangParserWithContextTest.java index be7b7ef7aa..f029a33f02 100644 --- a/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/test/java/org/opendaylight/controller/yang/parser/impl/YangParserWithContextTest.java +++ b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/test/java/org/opendaylight/controller/yang/parser/impl/YangParserWithContextTest.java @@ -15,6 +15,7 @@ import java.net.URI; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.ArrayList; +import java.util.Date; import java.util.List; import java.util.Map; import java.util.Set; @@ -25,6 +26,8 @@ import org.opendaylight.controller.yang.model.api.AnyXmlSchemaNode; import org.opendaylight.controller.yang.model.api.ChoiceNode; import org.opendaylight.controller.yang.model.api.ContainerSchemaNode; import org.opendaylight.controller.yang.model.api.DataSchemaNode; +import org.opendaylight.controller.yang.model.api.Deviation; +import org.opendaylight.controller.yang.model.api.Deviation.Deviate; import org.opendaylight.controller.yang.model.api.GroupingDefinition; import org.opendaylight.controller.yang.model.api.IdentitySchemaNode; import org.opendaylight.controller.yang.model.api.LeafSchemaNode; @@ -77,7 +80,7 @@ public class YangParserWithContextTest { assertEquals("inet", qname.getPrefix()); assertEquals("port-number", qname.getLocalName()); - ExtendedType dscpExt = (ExtendedType)TestUtils.findTypedef(module.getTypeDefinitions(), "dscp-ext"); + ExtendedType dscpExt = (ExtendedType) TestUtils.findTypedef(module.getTypeDefinitions(), "dscp-ext"); List ranges = dscpExt.getRanges(); assertEquals(1, ranges.size()); RangeConstraint range = ranges.get(0); @@ -88,8 +91,10 @@ public class YangParserWithContextTest { @Test public void testUsesFromContext() throws Exception { SchemaContext context = null; - try (InputStream stream = new FileInputStream(getClass().getResource("/model/testfile2.yang").getPath())) { - context = parser.resolveSchemaContext(TestUtils.loadModules(Lists.newArrayList(stream))); + try (InputStream stream1 = new FileInputStream(getClass().getResource("/model/custom.yang").getPath()); + InputStream stream2 = new FileInputStream(getClass().getResource("/model/types.yang").getPath()); + InputStream stream3 = new FileInputStream(getClass().getResource("/model/nodes.yang").getPath())) { + context = parser.resolveSchemaContext(TestUtils.loadModules(Lists.newArrayList(stream1, stream2, stream3))); } Module testModule = null; try (InputStream stream = new FileInputStream(getClass().getResource("/context-test/test2.yang").getPath())) { @@ -101,62 +106,62 @@ public class YangParserWithContextTest { // suffix _g = defined in grouping from context // get grouping - Module contextModule = context.findModuleByNamespace(URI.create("urn:simple.types.data.demo")); + Module contextModule = context.findModuleByNamespace(URI.create("urn:custom.nodes.test")); assertNotNull(contextModule); Set groupings = contextModule.getGroupings(); assertEquals(1, groupings.size()); GroupingDefinition grouping = groupings.iterator().next(); // get node containing uses - ContainerSchemaNode peer = (ContainerSchemaNode)testModule.getDataChildByName("peer"); - ContainerSchemaNode destination = (ContainerSchemaNode)peer.getDataChildByName("destination"); + ContainerSchemaNode peer = (ContainerSchemaNode) testModule.getDataChildByName("peer"); + ContainerSchemaNode destination = (ContainerSchemaNode) peer.getDataChildByName("destination"); // check uses Set uses = destination.getUses(); assertEquals(1, uses.size()); // check uses process - AnyXmlSchemaNode data_u = (AnyXmlSchemaNode)destination.getDataChildByName("data"); + AnyXmlSchemaNode data_u = (AnyXmlSchemaNode) destination.getDataChildByName("data"); assertNotNull(data_u); assertTrue(data_u.isAddedByUses()); - AnyXmlSchemaNode data_g = (AnyXmlSchemaNode)grouping.getDataChildByName("data"); + AnyXmlSchemaNode data_g = (AnyXmlSchemaNode) grouping.getDataChildByName("data"); assertNotNull(data_g); assertFalse(data_g.isAddedByUses()); assertFalse(data_u.equals(data_g)); - ChoiceNode how_u = (ChoiceNode)destination.getDataChildByName("how"); + ChoiceNode how_u = (ChoiceNode) destination.getDataChildByName("how"); assertNotNull(how_u); assertTrue(how_u.isAddedByUses()); - ChoiceNode how_g = (ChoiceNode)grouping.getDataChildByName("how"); + ChoiceNode how_g = (ChoiceNode) grouping.getDataChildByName("how"); assertNotNull(how_g); assertFalse(how_g.isAddedByUses()); assertFalse(how_u.equals(how_g)); - LeafSchemaNode address_u = (LeafSchemaNode)destination.getDataChildByName("address"); + LeafSchemaNode address_u = (LeafSchemaNode) destination.getDataChildByName("address"); assertNotNull(address_u); assertTrue(address_u.isAddedByUses()); - LeafSchemaNode address_g = (LeafSchemaNode)grouping.getDataChildByName("address"); + LeafSchemaNode address_g = (LeafSchemaNode) grouping.getDataChildByName("address"); assertNotNull(address_g); assertFalse(address_g.isAddedByUses()); assertFalse(address_u.equals(address_g)); - ContainerSchemaNode port_u = (ContainerSchemaNode)destination.getDataChildByName("port"); + ContainerSchemaNode port_u = (ContainerSchemaNode) destination.getDataChildByName("port"); assertNotNull(port_u); assertTrue(port_u.isAddedByUses()); - ContainerSchemaNode port_g = (ContainerSchemaNode)grouping.getDataChildByName("port"); + ContainerSchemaNode port_g = (ContainerSchemaNode) grouping.getDataChildByName("port"); assertNotNull(port_g); assertFalse(port_g.isAddedByUses()); assertFalse(port_u.equals(port_g)); - ListSchemaNode addresses_u = (ListSchemaNode)destination.getDataChildByName("addresses"); + ListSchemaNode addresses_u = (ListSchemaNode) destination.getDataChildByName("addresses"); assertNotNull(addresses_u); assertTrue(addresses_u.isAddedByUses()); - ListSchemaNode addresses_g = (ListSchemaNode)grouping.getDataChildByName("addresses"); + ListSchemaNode addresses_g = (ListSchemaNode) grouping.getDataChildByName("addresses"); assertNotNull(addresses_g); assertFalse(addresses_g.isAddedByUses()); assertFalse(addresses_u.equals(addresses_g)); @@ -189,8 +194,10 @@ public class YangParserWithContextTest { @Test public void testUsesRefineFromContext() throws Exception { SchemaContext context = null; - try (InputStream stream = new FileInputStream(getClass().getResource("/model/testfile2.yang").getPath())) { - context = parser.resolveSchemaContext(TestUtils.loadModules(Lists.newArrayList(stream))); + try (InputStream stream1 = new FileInputStream(getClass().getResource("/model/custom.yang").getPath()); + InputStream stream2 = new FileInputStream(getClass().getResource("/model/types.yang").getPath()); + InputStream stream3 = new FileInputStream(getClass().getResource("/model/nodes.yang").getPath())) { + context = parser.resolveSchemaContext(TestUtils.loadModules(Lists.newArrayList(stream1, stream2, stream3))); } Module module = null; try (InputStream stream = new FileInputStream(getClass().getResource("/context-test/test2.yang").getPath())) { @@ -206,7 +213,7 @@ public class YangParserWithContextTest { // test grouping path List path = new ArrayList(); - QName qname = new QName(URI.create("urn:simple.types.data.demo"), simpleDateFormat.parse("2013-02-27"), "t2", + QName qname = new QName(URI.create("urn:custom.nodes.test"), simpleDateFormat.parse("2013-02-27"), "c", "target"); path.add(qname); SchemaPath expectedPath = new SchemaPath(path, true); @@ -314,7 +321,8 @@ public class YangParserWithContextTest { @Test public void testUnknownNodes() throws Exception { SchemaContext context = null; - try (InputStream stream = new FileInputStream(getClass().getResource("/types/custom-types-test@2012-4-4.yang").getPath())) { + try (InputStream stream = new FileInputStream(getClass().getResource("/types/custom-types-test@2012-4-4.yang") + .getPath())) { context = parser.resolveSchemaContext(TestUtils.loadModules(Lists.newArrayList(stream))); } @@ -382,4 +390,43 @@ public class YangParserWithContextTest { assertTrue(ifEntry == ifEntryAfterAugment); } + @Test + public void testDeviation() throws Exception { + // load first module + SchemaContext context = null; + String resource = "/model/types.yang"; + + try (InputStream stream = new FileInputStream(getClass().getResource(resource).getPath())) { + context = parser.resolveSchemaContext(TestUtils.loadModules(Lists.newArrayList(stream))); + } + + // load another modules and parse them against already existing context + Set modules = null; + try (InputStream stream = new FileInputStream(getClass().getResource("/context-test/deviation-test.yang") + .getPath())) { + List input = Lists.newArrayList(stream); + modules = TestUtils.loadModulesWithContext(input, context); + } + assertNotNull(modules); + + // test deviation + Module testModule = TestUtils.findModule(modules, "deviation-test"); + Set deviations = testModule.getDeviations(); + assertEquals(1, deviations.size()); + Deviation dev = deviations.iterator().next(); + + assertEquals("system/user ref", dev.getReference()); + + URI expectedNS = URI.create("urn:simple.types.test"); + DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); + Date expectedRev = simpleDateFormat.parse("2013-07-03"); + List path = new ArrayList(); + path.add(new QName(expectedNS, expectedRev, "t", "interfaces")); + path.add(new QName(expectedNS, expectedRev, "t", "ifEntry")); + SchemaPath expectedPath = new SchemaPath(path, true); + + assertEquals(expectedPath, dev.getTargetPath()); + assertEquals(Deviate.ADD, dev.getDeviate()); + } + } diff --git a/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/test/resources/context-test/deviation-test.yang b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/test/resources/context-test/deviation-test.yang new file mode 100644 index 0000000000..eafcb56f89 --- /dev/null +++ b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/test/resources/context-test/deviation-test.yang @@ -0,0 +1,27 @@ +module deviation-test { + yang-version 1; + namespace "urn:simple.deviation.test"; + prefix "dev"; + + import types { + prefix "t"; + revision-date 2013-07-03; + } + + organization "opendaylight"; + contact "http://www.opendaylight.org/"; + + revision "2013-02-27" { + reference " WILL BE DEFINED LATER"; + } + + + deviation /t:interfaces/t:ifEntry { + deviate add { + default "admin"; // new users are 'admin' by default + config "true"; + } + reference "system/user ref"; + } + +} diff --git a/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/test/resources/context-test/test2.yang b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/test/resources/context-test/test2.yang index 74704d5c22..4cff19259e 100644 --- a/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/test/resources/context-test/test2.yang +++ b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/test/resources/context-test/test2.yang @@ -4,7 +4,7 @@ module test2 { namespace "urn:simple.demo.test2"; prefix "t2"; - import types2 { + import custom { prefix "data"; } diff --git a/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/test/resources/model/testfile3.yang b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/test/resources/model/custom.yang similarity index 63% rename from opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/test/resources/model/testfile3.yang rename to opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/test/resources/model/custom.yang index c507da55e5..d8d6054b59 100644 --- a/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/test/resources/model/testfile3.yang +++ b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/test/resources/model/custom.yang @@ -1,11 +1,11 @@ -module types3 { +module custom { yang-version 1; - namespace "urn:simple.container.demo.test"; - prefix "t3"; + namespace "urn:custom.nodes.test"; + prefix "c"; - import types2 { - prefix "data"; - revision-date 2013-02-27; + import types { + prefix "types"; + revision-date 2013-07-03; } organization "opendaylight"; @@ -22,25 +22,25 @@ module types3 { typedef union2 { type union { type int32; - type data:nested-union2; + type types:nested-union2; } } - augment "/data:interfaces/data:ifEntry" { + augment "/types:interfaces/types:ifEntry" { when "if:ifType='ds0'"; container augment-holder { description "Description for augment holder"; } } - augment "/data:interfaces/data:ifEntry" { + augment "/types:interfaces/types:ifEntry" { when "if:ifType='ds2'"; container augment-holder2 { description "Description for augment holder"; } } - augment "/data:interfaces/data:ifEntry/t3:augment-holder/t1:schemas" { + augment "/types:interfaces/types:ifEntry/t3:augment-holder/t1:schemas" { when "if:leafType='ds1'"; leaf linkleaf { type binary; @@ -68,10 +68,10 @@ module types3 { extension c-define { description - "Takes as argument a name string. - Makes the code generator use the given name in the - #define."; - argument "name"; + "Takes as argument a name string. Makes the code generator use the given name in the #define."; + argument "name" { + yin-element "true"; + } } notification event { @@ -145,4 +145,61 @@ module types3 { } } + grouping target { + anyxml data { + config true; + description "Copy of the source datastore subset."; + mandatory false; + must "test-condition-text"; + reference "test-no-reference"; + status "obsolete"; + when "test-when-text"; + } + choice how { + description "test choice description"; + default interval; + case interval { + leaf interval { + type uint16; + default 30; + units minutes; + } + } + case daily { + leaf daily { + type empty; + } + leaf time-of-day { + type string; + units 24-hour-clock; + default 1am; + } + } + } + leaf address { + type string; + description "Target IP address"; + } + container port { + description "Target port container"; + } + list addresses { + key "id"; + leaf id { + type int8; + } + } + grouping target-inner { + description "target-inner default description"; + leaf inner-grouping-id { + type int8; + } + } + typedef group-type { + type types:my-decimal-type; + } + + opendaylight; + } + } diff --git a/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/test/resources/model/nodes.yang b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/test/resources/model/nodes.yang new file mode 100644 index 0000000000..e27dd0b9c0 --- /dev/null +++ b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/test/resources/model/nodes.yang @@ -0,0 +1,192 @@ +module nodes { + yang-version 1; + namespace "urn:simple.nodes.test"; + prefix "n"; + + import types { + prefix "t"; + revision-date 2013-07-03; + } + + import custom { + prefix "c"; + revision-date 2013-02-27; + } + + organization "opendaylight"; + contact "http://www.opendaylight.org/"; + + revision "2013-02-27" { + reference " WILL BE DEFINED LATER"; + } + + leaf int32-leaf { + type t:int32-ext2 { + range "12..max"; + } + } + + leaf string-leaf { + type t:string-ext4; + } + + leaf length-leaf { + type t:string-ext2 { + length "7..max"; + } + } + + leaf decimal-leaf { + type t:my-decimal-type { + fraction-digits 4; + } + } + + leaf decimal-leaf2 { + type t:my-decimal-type; + } + + container ext { + types:c-define "MY_INTERFACES"; + } + + leaf union-leaf { + type t:my-union-ext; + } + + deviation /t:interfaces/t:ifEntry { + deviate add { + default "admin"; // new users are 'admin' by default + config "true"; + } + reference "system/user ref"; + } + + leaf custom-union-leaf { + type c:union1; + } + + container transfer { + choice how { + default interval; + container input { + } + list output { + leaf id { + type string; + } + } + case interval { + leaf interval { + type uint16; + default 30; + units minutes; + } + } + case daily { + leaf daily { + type empty; + } + leaf time-of-day { + type string; + units 24-hour-clock; + default 1am; + } + } + case manual { + leaf manual { + type empty; + } + } + } + } + + anyxml data { + description + "Copy of the source typesstore subset that matched + the filter criteria (if any). An empty types container + indicates that the request did not produce any results."; + status obsolete; + } + + augment "/t:interfaces/t:ifEntry/c:augment-holder" { + when "if:ifType='ds0'"; + leaf ds0ChannelNumber { + type string; + } + leaf interface-id { + type leafref { + path "/if:interfaces/if:interface/if:name"; + } + } + leaf my-type { + type t:int32-ext2; + } + container schemas { + } + choice odl { + leaf id { + type int8; + } + case node1 { + description "node1"; + } + case node2 { + description "node2"; + } + container node3 { + description "node3"; + } + } + } + + container mycont { + container innercont { + typedef mytype { + type string; + } + leaf myleaf { + type mytype; + } + } + } + + container peer { + container destination { + uses c:target { + refine address { + default "1.2.3.4"; + description "IP address of target node"; + reference "address reference added by refine"; + config false; + mandatory true; + must "ifType != 'ethernet' or " + + "(ifType = 'ethernet' and ifMTU = 1500)" { + error-message "An ethernet MTU must be 1500"; + } + } + refine port { + description "description of port defined by refine"; + reference "port reference added by refine"; + config false; + presence "presence is required"; + } + refine addresses { + description "description of addresses defined by refine"; + reference "addresses reference added by refine"; + config false; + min-elements 2; + max-elements 12; + } + refine target-inner { + description "new target-inner grouping description"; + } + refine group-type { + description "new group-type description"; + reference "new group-type reference"; + } + } + } + } + +} diff --git a/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/test/resources/model/testfile1.yang b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/test/resources/model/testfile1.yang deleted file mode 100644 index 47e649112a..0000000000 --- a/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/test/resources/model/testfile1.yang +++ /dev/null @@ -1,157 +0,0 @@ -module types1 { - yang-version 1; - namespace "urn:simple.container.demo"; - prefix "t1"; - - import types2 { - prefix "data"; - revision-date 2013-02-27; - } - - import types3 { - prefix "t3"; - revision-date 2013-02-27; - } - - organization "opendaylight"; - contact "http://www.opendaylight.org/"; - - revision "2013-02-27" { - reference " WILL BE DEFINED LATER"; - } - - leaf testleaf { - type data:my-type1 { - range "12..max"; - } - } - - leaf test-string-leaf { - type data:my-string-type-ext; - } - - leaf leaf-with-length { - type data:my-string-type { - length "7..max"; - } - } - - leaf test-int-leaf { - type data:my-int-type-ext; - } - - leaf test-decimal-leaf { - type data:my-decimal-type { - fraction-digits 4; - } - } - - leaf test-decimal-leaf2 { - type data:my-decimal-type-ext; - } - - container ext { - data:c-define "MY_INTERFACES"; - } - - leaf union-leaf { - type data:my-union-ext; - } - - deviation /data:system/data:user { - deviate add { - default "admin"; // new users are 'admin' by default - config "true"; - } - } - - leaf nested-union-leaf { - type data:nested-union1; - } - - leaf custom-union-leaf { - type t3:union1; - } - - container transfer { - choice how { - default interval; - container input { - } - list output { - leaf id { - type string; - } - } - case interval { - leaf interval { - type uint16; - default 30; - units minutes; - } - } - case daily { - leaf daily { - type empty; - } - leaf time-of-day { - type string; - units 24-hour-clock; - default 1am; - } - } - case manual { - leaf manual { - type empty; - } - } - } - } - - anyxml data { - description - "Copy of the source datastore subset that matched - the filter criteria (if any). An empty data container - indicates that the request did not produce any results."; - } - - augment "/data:interfaces/data:ifEntry/t3:augment-holder" { - when "if:ifType='ds0'"; - leaf ds0ChannelNumber { - type string; - } - leaf interface-id { - type leafref { - path "/if:interfaces/if:interface/if:name"; - } - } - leaf-list higher-layer-if { - type leafref { - path "/if:interfaces/if:interface/if:higher-layer-if"; - } - } - leaf my-type { - type data:my-type1; - } - container schemas { - } - choice odl { - leaf id { - type int8; - } - } - } - - container mycont { - container innercont { - typedef mytype { - type string; - } - - leaf myleaf { - type mytype; - } - } - } - -} diff --git a/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/test/resources/model/testfile2.yang b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/test/resources/model/testfile2.yang deleted file mode 100644 index b25f65e4a4..0000000000 --- a/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/test/resources/model/testfile2.yang +++ /dev/null @@ -1,257 +0,0 @@ -module types2 { - yang-version 1; - namespace "urn:simple.types.data.demo"; - prefix "t2"; - - organization "opendaylight"; - contact "http://www.opendaylight.org/"; - - description "This is types-data test description"; - - revision "2013-02-27" { - reference " WILL BE DEFINED LATER"; - } - - typedef my-base-int32-type { - type int32 { - range "2..20"; - } - } - - typedef my-type1 { - type my-base-int32-type { - range "3..9|11..max"; - } - units "mile"; - default "11"; - } - - typedef my-custom-string { - type string { - pattern "[a-k]*"; - length "5..11"; - } - } - - typedef my-string-type { - type my-custom-string { - length "6..10"; - } - } - - typedef my-string-type2 { - type my-string-type { - pattern "[b-u]*"; - } - } - - typedef my-string-type-ext { - type my-string-type2 { - pattern "[e-z]*"; - } - } - - typedef my-int-type { - type int32 { - range "10..20"; - } - } - - typedef my-int-type2 { - type my-int-type { - range "12..18"; - } - } - - typedef my-int-type-ext { - type my-int-type2 { - range "14..16"; - } - } - - typedef my-decimal-type { - type decimal64 { - fraction-digits 6; - } - } - - typedef my-decimal-type-ext { - type decimal64 { - fraction-digits 5; - } - } - - typedef my-union { - type union { - type int16 { - range "1..100"; - } - type int32; - } - } - - typedef my-union-ext { - type my-union; - } - - typedef nested-union1 { - type nested-union2; - } - - typedef nested-union2 { - type union { - type my-union-ext; - type string; - } - } - - leaf if-name { - type leafref { - path "/interface/name"; - } - } - - leaf name { - type string; - } - - leaf count { - type int8 { - range "1..10"; - } - } - - leaf nested-type-leaf { - type my-type1; - } - - extension c-define { - description - "Takes as argument a name string. - Makes the code generator use the given name in the - #define."; - argument "name" { - yin-element "true"; - } - } - - container system { - leaf user { - type string; - } - } - - grouping target { - anyxml data { - config true; - description "Copy of the source datastore subset."; - mandatory false; - must "test-condition-text"; - reference "test-no-reference"; - status "obsolete"; - when "test-when-text"; - } - choice how { - description "test choice description"; - default interval; - case interval { - leaf interval { - type uint16; - default 30; - units minutes; - } - } - case daily { - leaf daily { - type empty; - } - leaf time-of-day { - type string; - units 24-hour-clock; - default 1am; - } - } - } - leaf address { - type string; - description "Target IP address"; - } - container port { - description "Target port container"; - } - list addresses { - key "id"; - leaf id { - type int8; - } - } - grouping target-inner { - description "target-inner default description"; - leaf inner-grouping-id { - type int8; - } - } - typedef group-type { - type my-decimal-type; - } - - opendaylight; - } - - container peer { - container destination { - uses target { - refine address { - default "1.2.3.4"; - description "IP address of target node"; - reference "address reference added by refine"; - config false; - mandatory true; - must "ifType != 'ethernet' or " + - "(ifType = 'ethernet' and ifMTU = 1500)" { - error-message "An ethernet MTU must be 1500"; - } - } - refine port { - description "description of port defined by refine"; - reference "port reference added by refine"; - config false; - presence "presence is required"; - } - refine addresses { - description "description of addresses defined by refine"; - reference "addresses reference added by refine"; - config false; - min-elements 2; - max-elements 12; - } - refine target-inner { - description "new target-inner grouping description"; - } - refine group-type { - description "new group-type description"; - reference "new group-type reference"; - } - } - } - } - - container interfaces { - list ifEntry { - key "ifIndex"; - - leaf ifIndex { - type uint32; - units minutes; - } - - leaf ifMtu { - type int32; - } - - min-elements 1; - max-elements 11; - } - } - -} diff --git a/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/test/resources/model/types.yang b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/test/resources/model/types.yang new file mode 100644 index 0000000000..d9d5064383 --- /dev/null +++ b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/test/resources/model/types.yang @@ -0,0 +1,97 @@ +module types { + yang-version 1; + namespace "urn:simple.types.test"; + prefix "t"; + + organization "opendaylight"; + contact "http://www.opendaylight.org/"; + description "This is types-data test description"; + + revision "2013-07-03" { + reference " WILL BE DEFINED LATER"; + } + + typedef int32-ext1 { + type int32 { + range "2..20"; + } + } + + typedef int32-ext2 { + type int32-ext1 { + range "3..9|11..max"; + } + units "mile"; + default "11"; + } + + typedef string-ext1 { + type string { + pattern "[a-k]*"; + length "5..11"; + } + } + + typedef string-ext2 { + type string-ext1 { + length "6..10"; + } + } + + typedef string-ext3 { + type string-ext2 { + pattern "[b-u]*"; + } + } + + typedef string-ext4 { + type string-ext3 { + pattern "[e-z]*"; + } + } + + typedef my-decimal-type { + type decimal64 { + fraction-digits 6; + } + } + + typedef my-union { + type union { + type int16 { + range "1..100"; + } + type int32; + } + } + + typedef my-union-ext { + type my-union; + } + + typedef nested-union2 { + type union { + type my-union-ext; + type string; + } + } + + container interfaces { + list ifEntry { + key "ifIndex"; + + leaf ifIndex { + type uint32; + units minutes; + } + + leaf ifMtu { + type int32; + } + + min-elements 1; + max-elements 11; + } + } + +} diff --git a/opendaylight/sal/yang-prototype/yang/yang-model-util/src/main/java/org/opendaylight/controller/yang/model/util/ExtendedType.java b/opendaylight/sal/yang-prototype/yang/yang-model-util/src/main/java/org/opendaylight/controller/yang/model/util/ExtendedType.java index b9729102c9..6070c1c6ac 100644 --- a/opendaylight/sal/yang-prototype/yang/yang-model-util/src/main/java/org/opendaylight/controller/yang/model/util/ExtendedType.java +++ b/opendaylight/sal/yang-prototype/yang/yang-model-util/src/main/java/org/opendaylight/controller/yang/model/util/ExtendedType.java @@ -51,7 +51,7 @@ public class ExtendedType implements TypeDefinition> { private List unknownSchemaNodes = Collections .emptyList(); private Status status = Status.CURRENT; - private String units = ""; + private String units = null; private Object defaultValue = null; private boolean addedByUses;