import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
+import java.net.URI;
import java.util.ArrayList;
-import java.util.Calendar;
+import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
+import java.util.Iterator;
import java.util.List;
import java.util.Map;
+import java.util.NoSuchElementException;
import java.util.Set;
import java.util.TreeMap;
import org.antlr.v4.runtime.tree.ParseTreeWalker;
import org.opendaylight.controller.antlrv4.code.gen.YangLexer;
import org.opendaylight.controller.antlrv4.code.gen.YangParser;
-import org.opendaylight.controller.model.api.type.BinaryTypeDefinition;
-import org.opendaylight.controller.model.api.type.BitsTypeDefinition;
-import org.opendaylight.controller.model.api.type.BitsTypeDefinition.Bit;
-import org.opendaylight.controller.model.api.type.DecimalTypeDefinition;
-import org.opendaylight.controller.model.api.type.InstanceIdentifierTypeDefinition;
-import org.opendaylight.controller.model.api.type.IntegerTypeDefinition;
-import org.opendaylight.controller.model.api.type.LengthConstraint;
-import org.opendaylight.controller.model.api.type.PatternConstraint;
-import org.opendaylight.controller.model.api.type.RangeConstraint;
-import org.opendaylight.controller.model.api.type.StringTypeDefinition;
-import org.opendaylight.controller.model.util.BaseConstraints;
-import org.opendaylight.controller.model.util.BinaryType;
-import org.opendaylight.controller.model.util.BitsType;
-import org.opendaylight.controller.model.util.StringType;
-import org.opendaylight.controller.model.util.UnknownType;
-import org.opendaylight.controller.model.util.YangTypesConverter;
import org.opendaylight.controller.yang.common.QName;
-import org.opendaylight.controller.yang.model.api.AugmentationSchema;
import org.opendaylight.controller.yang.model.api.DataSchemaNode;
import org.opendaylight.controller.yang.model.api.ExtensionDefinition;
import org.opendaylight.controller.yang.model.api.Module;
import org.opendaylight.controller.yang.model.api.ModuleImport;
+import org.opendaylight.controller.yang.model.api.MustDefinition;
import org.opendaylight.controller.yang.model.api.NotificationDefinition;
import org.opendaylight.controller.yang.model.api.RpcDefinition;
import org.opendaylight.controller.yang.model.api.SchemaContext;
import org.opendaylight.controller.yang.model.api.SchemaPath;
import org.opendaylight.controller.yang.model.api.TypeDefinition;
+import org.opendaylight.controller.yang.model.api.type.BinaryTypeDefinition;
+import org.opendaylight.controller.yang.model.api.type.DecimalTypeDefinition;
+import org.opendaylight.controller.yang.model.api.type.IntegerTypeDefinition;
+import org.opendaylight.controller.yang.model.api.type.LengthConstraint;
+import org.opendaylight.controller.yang.model.api.type.PatternConstraint;
+import org.opendaylight.controller.yang.model.api.type.RangeConstraint;
+import org.opendaylight.controller.yang.model.api.type.StringTypeDefinition;
import org.opendaylight.controller.yang.model.parser.api.YangModelParser;
import org.opendaylight.controller.yang.model.parser.builder.api.AugmentationSchemaBuilder;
import org.opendaylight.controller.yang.model.parser.builder.api.AugmentationTargetBuilder;
+import org.opendaylight.controller.yang.model.parser.builder.api.Builder;
import org.opendaylight.controller.yang.model.parser.builder.api.ChildNodeBuilder;
import org.opendaylight.controller.yang.model.parser.builder.api.DataSchemaNodeBuilder;
+import org.opendaylight.controller.yang.model.parser.builder.api.GroupingBuilder;
import org.opendaylight.controller.yang.model.parser.builder.api.TypeAwareBuilder;
import org.opendaylight.controller.yang.model.parser.builder.api.TypeDefinitionBuilder;
+import org.opendaylight.controller.yang.model.parser.builder.api.UsesNodeBuilder;
+import org.opendaylight.controller.yang.model.parser.builder.impl.AnyXmlBuilder;
+import org.opendaylight.controller.yang.model.parser.builder.impl.ChoiceBuilder;
+import org.opendaylight.controller.yang.model.parser.builder.impl.ContainerSchemaNodeBuilder;
+import org.opendaylight.controller.yang.model.parser.builder.impl.IdentitySchemaNodeBuilder;
+import org.opendaylight.controller.yang.model.parser.builder.impl.IdentityrefTypeBuilder;
+import org.opendaylight.controller.yang.model.parser.builder.impl.LeafListSchemaNodeBuilder;
+import org.opendaylight.controller.yang.model.parser.builder.impl.LeafSchemaNodeBuilder;
+import org.opendaylight.controller.yang.model.parser.builder.impl.ListSchemaNodeBuilder;
import org.opendaylight.controller.yang.model.parser.builder.impl.ModuleBuilder;
+import org.opendaylight.controller.yang.model.parser.builder.impl.TypedefBuilder;
import org.opendaylight.controller.yang.model.parser.builder.impl.UnionTypeBuilder;
+import org.opendaylight.controller.yang.model.parser.builder.impl.UnknownSchemaNodeBuilder;
+import org.opendaylight.controller.yang.model.parser.util.ModuleDependencySort;
+import org.opendaylight.controller.yang.model.parser.util.ModuleDependencySort.ModuleSimple;
+import org.opendaylight.controller.yang.model.parser.util.ParserUtils;
+import org.opendaylight.controller.yang.model.parser.util.RefineHolder;
+import org.opendaylight.controller.yang.model.parser.util.TypeConstraints;
+import org.opendaylight.controller.yang.model.parser.util.YangParseException;
+import org.opendaylight.controller.yang.model.util.ExtendedType;
+import org.opendaylight.controller.yang.model.util.IdentityrefType;
+import org.opendaylight.controller.yang.model.util.UnknownType;
+import org.opendaylight.controller.yang.model.validator.YangModelBasicValidator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
.getLogger(YangModelParserImpl.class);
@Override
- public Module parseYangModel(String yangFile) {
- final Map<String, TreeMap<Date, ModuleBuilder>> modules = resolveModuleBuildersFromStreams(yangFile);
- Set<Module> result = build(modules);
- return result.iterator().next();
- }
-
- @Override
- public Set<Module> parseYangModels(String... yangFiles) {
- final Map<String, TreeMap<Date, ModuleBuilder>> modules = resolveModuleBuildersFromStreams(yangFiles);
- Set<Module> result = build(modules);
- return result;
+ public Set<Module> parseYangModels(final List<File> yangFiles) {
+ if (yangFiles != null) {
+ final List<InputStream> inputStreams = new ArrayList<InputStream>();
+
+ for (final File yangFile : yangFiles) {
+ try {
+ inputStreams.add(new FileInputStream(yangFile));
+ } catch (FileNotFoundException e) {
+ logger.warn("Exception while reading yang file: "
+ + yangFile.getName(), e);
+ }
+ }
+ final Map<String, TreeMap<Date, ModuleBuilder>> modules = resolveModuleBuilders(inputStreams);
+ return build(modules);
+ }
+ return Collections.emptySet();
}
@Override
public Set<Module> parseYangModelsFromStreams(
- InputStream... yangModelStreams) {
- final Map<String, TreeMap<Date, ModuleBuilder>> modules = resolveModuleBuildersFromStreams(yangModelStreams);
- Set<Module> result = build(modules);
- return result;
+ final List<InputStream> yangModelStreams) {
+ final Map<String, TreeMap<Date, ModuleBuilder>> modules = resolveModuleBuilders(yangModelStreams);
+ return build(modules);
}
@Override
- public SchemaContext resolveSchemaContext(Set<Module> modules) {
+ public SchemaContext resolveSchemaContext(final Set<Module> modules) {
return new SchemaContextImpl(modules);
}
- private Map<String, TreeMap<Date, ModuleBuilder>> resolveModuleBuildersFromStreams(
- String... yangFiles) {
- InputStream[] streams = new InputStream[yangFiles.length];
- for(int i = 0; i < yangFiles.length; i++) {
- final String yangFileName = yangFiles[i];
- final File yangFile = new File(yangFileName);
- FileInputStream inStream = null;
- try {
- inStream = new FileInputStream(yangFile);
- } catch(FileNotFoundException e) {
- logger.warn("Exception while reading yang stream: " + inStream, e);
- }
- streams[i] = inStream;
- }
- return resolveModuleBuildersFromStreams(streams);
- }
-
- private Map<String, TreeMap<Date, ModuleBuilder>> resolveModuleBuildersFromStreams(
- InputStream... yangFiles) {
+ private Map<String, TreeMap<Date, ModuleBuilder>> resolveModuleBuilders(
+ final List<InputStream> yangFileStreams) {
final Map<String, TreeMap<Date, ModuleBuilder>> modules = new HashMap<String, TreeMap<Date, ModuleBuilder>>();
-
- final YangModelParserListenerImpl yangModelParser = new YangModelParserListenerImpl();
final ParseTreeWalker walker = new ParseTreeWalker();
+ final List<ParseTree> trees = parseStreams(yangFileStreams);
+ final ModuleBuilder[] builders = new ModuleBuilder[trees.size()];
- List<ParseTree> trees = parseStreams(yangFiles);
-
- ModuleBuilder[] builders = new ModuleBuilder[trees.size()];
+ // validate yang
+ new YangModelBasicValidator(walker).validate(trees);
+ YangModelParserListenerImpl yangModelParser = null;
for (int i = 0; i < trees.size(); i++) {
+ yangModelParser = new YangModelParserListenerImpl();
walker.walk(yangModelParser, trees.get(i));
builders[i] = yangModelParser.getModuleBuilder();
}
+ // module dependency graph sorted
+ List<ModuleSimple> sorted = new ModuleDependencySort(builders).sort();
+
for (ModuleBuilder builder : builders) {
final String builderName = builder.getName();
Date builderRevision = builder.getRevision();
if (builderRevision == null) {
- builderRevision = createEpochTime();
+ builderRevision = new Date(0L);
}
-
TreeMap<Date, ModuleBuilder> builderByRevision = modules
.get(builderName);
if (builderByRevision == null) {
builderByRevision = new TreeMap<Date, ModuleBuilder>();
}
builderByRevision.put(builderRevision, builder);
-
modules.put(builderName, builderByRevision);
}
return modules;
}
- private List<ParseTree> parseStreams(InputStream... yangStreams) {
- List<ParseTree> trees = new ArrayList<ParseTree>();
+ private List<ParseTree> parseStreams(final List<InputStream> yangStreams) {
+ final List<ParseTree> trees = new ArrayList<ParseTree>();
for (InputStream yangStream : yangStreams) {
trees.add(parseStream(yangStream));
}
return trees;
}
- private ParseTree parseStream(InputStream yangStream) {
+ private ParseTree parseStream(final InputStream yangStream) {
ParseTree result = null;
try {
final ANTLRInputStream input = new ANTLRInputStream(yangStream);
return result;
}
- private Set<Module> build(Map<String, TreeMap<Date, ModuleBuilder>> modules) {
- // first validate
+ private Set<Module> build(
+ final Map<String, TreeMap<Date, ModuleBuilder>> modules) {
+ // fix unresolved nodes
for (Map.Entry<String, TreeMap<Date, ModuleBuilder>> entry : modules
.entrySet()) {
for (Map.Entry<Date, ModuleBuilder> childEntry : entry.getValue()
.entrySet()) {
- ModuleBuilder moduleBuilder = childEntry.getValue();
- validateBuilder(modules, moduleBuilder);
+ final ModuleBuilder moduleBuilder = childEntry.getValue();
+ fixUnresolvedNodes(modules, moduleBuilder);
}
}
- // then build
+ resolveAugments(modules);
+
+ // build
final Set<Module> result = new HashSet<Module>();
for (Map.Entry<String, TreeMap<Date, ModuleBuilder>> entry : modules
.entrySet()) {
final Map<Date, Module> modulesByRevision = new HashMap<Date, Module>();
for (Map.Entry<Date, ModuleBuilder> childEntry : entry.getValue()
.entrySet()) {
- ModuleBuilder moduleBuilder = childEntry.getValue();
- modulesByRevision.put(childEntry.getKey(),
- moduleBuilder.build());
- result.add(moduleBuilder.build());
+ final ModuleBuilder moduleBuilder = childEntry.getValue();
+ final Module module = moduleBuilder.build();
+ modulesByRevision.put(childEntry.getKey(), module);
+ result.add(module);
}
}
-
return result;
}
- private void validateBuilder(
- Map<String, TreeMap<Date, ModuleBuilder>> modules,
- ModuleBuilder builder) {
- resolveTypedefs(modules, builder);
- resolveAugments(modules, builder);
+ private void fixUnresolvedNodes(
+ final Map<String, TreeMap<Date, ModuleBuilder>> modules,
+ final ModuleBuilder builder) {
+ resolveDirtyNodes(modules, builder);
+ resolveIdentities(modules, builder);
+ resolveUses(modules, builder);
+ resolveUnknownNodes(modules, builder);
}
/**
* @param module
* current module
*/
- private void resolveTypedefs(
- Map<String, TreeMap<Date, ModuleBuilder>> modules,
- ModuleBuilder module) {
- Map<List<String>, TypeAwareBuilder> dirtyNodes = module
+ private void resolveDirtyNodes(
+ final Map<String, TreeMap<Date, ModuleBuilder>> modules,
+ final ModuleBuilder module) {
+ final Map<List<String>, TypeAwareBuilder> dirtyNodes = module
.getDirtyNodes();
- if (dirtyNodes.size() == 0) {
- return;
- } else {
+ if (!dirtyNodes.isEmpty()) {
for (Map.Entry<List<String>, TypeAwareBuilder> entry : dirtyNodes
.entrySet()) {
- TypeAwareBuilder typeToResolve = entry.getValue();
- if (typeToResolve instanceof UnionTypeBuilder) {
- resolveUnionTypeBuilder(modules, module,
- (UnionTypeBuilder) typeToResolve);
+ final TypeAwareBuilder nodeToResolve = entry.getValue();
+ // different handling for union types
+ if (nodeToResolve instanceof UnionTypeBuilder) {
+ final UnionTypeBuilder union = (UnionTypeBuilder) nodeToResolve;
+ final List<TypeDefinition<?>> unionTypes = union.getTypes();
+ final List<UnknownType> toRemove = new ArrayList<UnknownType>();
+ for (TypeDefinition<?> td : unionTypes) {
+ if (td instanceof UnknownType) {
+ final UnknownType unknownType = (UnknownType) td;
+ final TypeDefinitionBuilder resolvedType = resolveTypeUnion(
+ nodeToResolve, unknownType, modules, module);
+ union.setType(resolvedType);
+ toRemove.add(unknownType);
+ }
+ }
+ unionTypes.removeAll(toRemove);
+ } else if (nodeToResolve.getTypedef() instanceof IdentityrefTypeBuilder) {
+ IdentityrefTypeBuilder idref = (IdentityrefTypeBuilder) nodeToResolve
+ .getTypedef();
+ nodeToResolve.setType(new IdentityrefType(findFullQName(
+ modules, module, idref), idref.getPath()));
} else {
- UnknownType ut = (UnknownType) typeToResolve.getType();
- TypeDefinition<?> resolvedType = findTargetType(ut,
- modules, module);
- typeToResolve.setType(resolvedType);
+ final TypeDefinitionBuilder resolvedType = resolveType(
+ nodeToResolve, modules, module);
+ nodeToResolve.setType(resolvedType);
}
}
}
}
- private UnionTypeBuilder resolveUnionTypeBuilder(
- Map<String, TreeMap<Date, ModuleBuilder>> modules,
- ModuleBuilder builder, UnionTypeBuilder unionTypeBuilderToResolve) {
+ private TypeDefinitionBuilder resolveType(
+ final TypeAwareBuilder typeToResolve,
+ final Map<String, TreeMap<Date, ModuleBuilder>> modules,
+ final ModuleBuilder builder) {
+ final TypeConstraints constraints = new TypeConstraints();
+
+ final TypeDefinitionBuilder targetType = getTypedefBuilder(
+ typeToResolve, modules, builder);
+ final TypeConstraints tConstraints = findConstraints(typeToResolve,
+ constraints, modules, builder);
+ targetType.setRanges(tConstraints.getRange());
+ targetType.setLengths(tConstraints.getLength());
+ targetType.setPatterns(tConstraints.getPatterns());
+ targetType.setFractionDigits(tConstraints.getFractionDigits());
+
+ return targetType;
+ }
+
+ private TypeDefinitionBuilder resolveTypeUnion(
+ final TypeAwareBuilder typeToResolve,
+ final UnknownType unknownType,
+ final Map<String, TreeMap<Date, ModuleBuilder>> modules,
+ final ModuleBuilder builder) {
+ final TypeConstraints constraints = new TypeConstraints();
+
+ final TypeDefinitionBuilder targetType = getUnionBuilder(typeToResolve,
+ unknownType, modules, builder);
+ final TypeConstraints tConstraints = findConstraints(typeToResolve,
+ constraints, modules, builder);
+ targetType.setRanges(tConstraints.getRange());
+ targetType.setLengths(tConstraints.getLength());
+ targetType.setPatterns(tConstraints.getPatterns());
+ targetType.setFractionDigits(tConstraints.getFractionDigits());
+
+ return targetType;
+ }
- List<TypeDefinition<?>> resolvedTypes = new ArrayList<TypeDefinition<?>>();
- List<TypeDefinition<?>> typesToRemove = new ArrayList<TypeDefinition<?>>();
+ private TypeDefinitionBuilder getTypedefBuilder(
+ final TypeAwareBuilder nodeToResolve,
+ final Map<String, TreeMap<Date, ModuleBuilder>> modules,
+ final ModuleBuilder builder) {
- for (TypeDefinition<?> td : unionTypeBuilderToResolve.getTypes()) {
- if (td instanceof UnknownType) {
- TypeDefinition<?> resolvedType = findTargetType(
- (UnknownType) td, modules, builder);
- resolvedTypes.add(resolvedType);
- typesToRemove.add(td);
- }
+ final TypeDefinition<?> nodeToResolveBase = nodeToResolve.getType();
+ if (nodeToResolveBase != null
+ && !(nodeToResolveBase instanceof UnknownType)) {
+ return (TypeDefinitionBuilder) nodeToResolve;
}
- List<TypeDefinition<?>> unionTypeBuilderTypes = unionTypeBuilderToResolve
- .getTypes();
- unionTypeBuilderTypes.addAll(resolvedTypes);
- unionTypeBuilderTypes.removeAll(typesToRemove);
+ final UnknownType unknownType = (UnknownType) nodeToResolve.getType();
+ final QName unknownTypeQName = unknownType.getQName();
- return unionTypeBuilderToResolve;
+ // search for module which contains referenced typedef
+ final ModuleBuilder dependentModule = findDependentModule(modules,
+ builder, unknownTypeQName.getPrefix(), nodeToResolve.getLine());
+ final TypeDefinitionBuilder lookedUpBuilder = findTypedefBuilderByName(
+ dependentModule, unknownTypeQName.getLocalName(),
+ builder.getName(), nodeToResolve.getLine());
+
+ final TypeDefinitionBuilder lookedUpBuilderCopy = copyTypedefBuilder(
+ lookedUpBuilder, nodeToResolve instanceof TypeDefinitionBuilder);
+ final TypeDefinitionBuilder resolvedCopy = resolveCopiedBuilder(
+ lookedUpBuilderCopy, modules, dependentModule);
+ return resolvedCopy;
}
- private TypeDefinition<?> findTargetType(UnknownType ut,
- Map<String, TreeMap<Date, ModuleBuilder>> modules,
- ModuleBuilder builder) {
-
- Map<TypeDefinitionBuilder, TypeConstraints> foundedTypeDefinitionBuilder = findTypeDefinitionBuilderWithConstraints(
- modules, ut, builder);
- TypeDefinitionBuilder targetType = foundedTypeDefinitionBuilder
- .entrySet().iterator().next().getKey();
- TypeConstraints constraints = foundedTypeDefinitionBuilder.entrySet()
- .iterator().next().getValue();
-
- TypeDefinition<?> targetTypeBaseType = targetType.getBaseType();
- String targetTypeBaseTypeName = targetTypeBaseType.getQName()
- .getLocalName();
-
- // RANGE
- List<RangeConstraint> ranges = ut.getRangeStatements();
- resolveRanges(ranges, targetType, modules, builder);
-
- // LENGTH
- List<LengthConstraint> lengths = ut.getLengthStatements();
- resolveLengths(lengths, targetType, modules, builder);
-
- // PATTERN
- List<PatternConstraint> patterns = ut.getPatterns();
-
- // Fraction Digits
- Integer fractionDigits = ut.getFractionDigits();
-
- // MERGE CONSTRAINTS (enumeration and leafref omitted
- // because
- // they have no restrictions)
- if (targetTypeBaseType instanceof DecimalTypeDefinition) {
- List<RangeConstraint> fullRanges = new ArrayList<RangeConstraint>();
- fullRanges.addAll(constraints.getRanges());
- fullRanges.addAll(ranges);
- Integer fd = fractionDigits == null ? constraints
- .getFractionDigits() : fractionDigits;
- targetTypeBaseType = YangTypesConverter
- .javaTypeForBaseYangDecimal64Type(fullRanges, fd);
- } else if (targetTypeBaseType instanceof IntegerTypeDefinition) {
- List<RangeConstraint> fullRanges = new ArrayList<RangeConstraint>();
- fullRanges.addAll(constraints.getRanges());
- fullRanges.addAll(ranges);
- if (targetTypeBaseTypeName.startsWith("int")) {
- targetTypeBaseType = YangTypesConverter
- .javaTypeForBaseYangSignedIntegerType(
- targetTypeBaseTypeName, fullRanges);
- } else {
- targetTypeBaseType = YangTypesConverter
- .javaTypeForBaseYangUnsignedIntegerType(
- targetTypeBaseTypeName, fullRanges);
- }
- } else if (targetTypeBaseType instanceof StringTypeDefinition) {
- List<LengthConstraint> fullLengths = new ArrayList<LengthConstraint>();
- fullLengths.addAll(constraints.getLengths());
- fullLengths.addAll(lengths);
- List<PatternConstraint> fullPatterns = new ArrayList<PatternConstraint>();
- fullPatterns.addAll(constraints.getPatterns());
- fullPatterns.addAll(patterns);
- targetTypeBaseType = new StringType(fullLengths, fullPatterns);
- } else if (targetTypeBaseType instanceof BitsTypeDefinition) {
- BitsTypeDefinition bitsType = (BitsTypeDefinition) targetTypeBaseType;
- List<Bit> bits = bitsType.getBits();
- targetTypeBaseType = new BitsType(bits);
- } else if (targetTypeBaseType instanceof BinaryTypeDefinition) {
- targetTypeBaseType = new BinaryType(null, lengths, null);
- } else if (targetTypeBaseTypeName.equals("instance-identifier")) {
- // TODO: instance-identifier
- /*
- * boolean requireInstance = isRequireInstance(typeBody); type = new
- * InstanceIdentifier(null, requireInstance);
- */
- }
-
- return targetTypeBaseType;
- }
+ private TypeDefinitionBuilder getUnionBuilder(
+ final TypeAwareBuilder nodeToResolve,
+ final UnknownType unknownType,
+ final Map<String, TreeMap<Date, ModuleBuilder>> modules,
+ final ModuleBuilder module) {
+
+ final TypeDefinition<?> baseTypeToResolve = nodeToResolve.getType();
+ if (baseTypeToResolve != null
+ && !(baseTypeToResolve instanceof UnknownType)) {
+ return (TypeDefinitionBuilder) nodeToResolve;
+ }
- private TypeDefinitionBuilder findTypeDefinitionBuilder(
- Map<String, TreeMap<Date, ModuleBuilder>> modules,
- UnknownType unknownType, ModuleBuilder builder) {
- Map<TypeDefinitionBuilder, TypeConstraints> result = findTypeDefinitionBuilderWithConstraints(
- modules, unknownType, builder);
- return result.entrySet().iterator().next().getKey();
+ final QName unknownTypeQName = unknownType.getQName();
+ // search for module which contains referenced typedef
+ final ModuleBuilder dependentModule = findDependentModule(modules,
+ module, unknownTypeQName.getPrefix(), nodeToResolve.getLine());
+ final TypeDefinitionBuilder lookedUpBuilder = findTypedefBuilderByName(
+ dependentModule, unknownTypeQName.getLocalName(),
+ module.getName(), nodeToResolve.getLine());
+
+ final TypeDefinitionBuilder lookedUpBuilderCopy = copyTypedefBuilder(
+ lookedUpBuilder, nodeToResolve instanceof TypeDefinitionBuilder);
+ final TypeDefinitionBuilder resolvedCopy = resolveCopiedBuilder(
+ lookedUpBuilderCopy, modules, dependentModule);
+ return resolvedCopy;
}
- private Map<TypeDefinitionBuilder, TypeConstraints> findTypeDefinitionBuilderWithConstraints(
- Map<String, TreeMap<Date, ModuleBuilder>> modules,
- UnknownType unknownType, ModuleBuilder builder) {
- return findTypeDefinitionBuilderWithConstraints(new TypeConstraints(),
- modules, unknownType, builder);
+ private TypeDefinitionBuilder copyTypedefBuilder(
+ final TypeDefinitionBuilder old, final boolean seekByTypedefBuilder) {
+ if (old instanceof UnionTypeBuilder) {
+ final UnionTypeBuilder oldUnion = (UnionTypeBuilder) old;
+ final UnionTypeBuilder newUnion = new UnionTypeBuilder(
+ oldUnion.getActualPath(), oldUnion.getNamespace(),
+ oldUnion.getRevision(), old.getLine());
+ for (TypeDefinition<?> td : oldUnion.getTypes()) {
+ newUnion.setType(td);
+ }
+ for (TypeDefinitionBuilder tdb : oldUnion.getTypedefs()) {
+ newUnion.setType(copyTypedefBuilder(tdb, true));
+ }
+ return newUnion;
+ }
+
+ final QName oldName = old.getQName();
+ final QName newName = new QName(oldName.getNamespace(),
+ oldName.getRevision(), oldName.getPrefix(),
+ oldName.getLocalName());
+ final TypeDefinitionBuilder tdb = new TypedefBuilder(newName,
+ old.getLine());
+
+ tdb.setRanges(old.getRanges());
+ tdb.setLengths(old.getLengths());
+ tdb.setPatterns(old.getPatterns());
+ tdb.setFractionDigits(old.getFractionDigits());
+ tdb.setPath(old.getPath());
+
+ final TypeDefinition<?> oldType = old.getType();
+ if (oldType == null) {
+ tdb.setType(old.getTypedef());
+ } else {
+ tdb.setType(oldType);
+ }
+
+ if (!seekByTypedefBuilder) {
+ tdb.setDescription(old.getDescription());
+ tdb.setReference(old.getReference());
+ tdb.setStatus(old.getStatus());
+ tdb.setDefaultValue(old.getDefaultValue());
+ tdb.setUnits(old.getUnits());
+ }
+ return tdb;
}
- /**
- * Traverse through all referenced types chain until base YANG type is
- * founded.
- *
- * @param constraints current type constraints
- * @param modules all available modules
- * @param unknownType unknown type
- * @param builder current module
- * @return map, where key is type referenced and value is its constraints
- */
- private Map<TypeDefinitionBuilder, TypeConstraints> findTypeDefinitionBuilderWithConstraints(
- TypeConstraints constraints,
- Map<String, TreeMap<Date, ModuleBuilder>> modules,
- UnknownType unknownType, ModuleBuilder builder) {
- Map<TypeDefinitionBuilder, TypeConstraints> result = new HashMap<TypeDefinitionBuilder, TypeConstraints>();
+ private TypeDefinitionBuilder resolveCopiedBuilder(
+ final TypeDefinitionBuilder copy,
+ final Map<String, TreeMap<Date, ModuleBuilder>> modules,
+ final ModuleBuilder builder) {
+
+ if (copy instanceof UnionTypeBuilder) {
+ final UnionTypeBuilder union = (UnionTypeBuilder) copy;
+ final List<TypeDefinition<?>> unionTypes = union.getTypes();
+ final List<UnknownType> toRemove = new ArrayList<UnknownType>();
+ for (TypeDefinition<?> td : unionTypes) {
+ if (td instanceof UnknownType) {
+ final UnknownType unknownType = (UnknownType) td;
+ final TypeDefinitionBuilder resolvedType = resolveTypeUnion(
+ union, unknownType, modules, builder);
+ union.setType(resolvedType);
+ toRemove.add(unknownType);
+ }
+ }
+ unionTypes.removeAll(toRemove);
- // TypeDefinition<?> unknownType = typeBuilder.getType();
- QName unknownTypeQName = unknownType.getQName();
- String unknownTypeName = unknownTypeQName.getLocalName();
- String unknownTypePrefix = unknownTypeQName.getPrefix();
+ return union;
+ }
- // search for module which contains referenced typedef
- ModuleBuilder dependentModuleBuilder;
- if (unknownTypePrefix.equals(builder.getPrefix())) {
- dependentModuleBuilder = builder;
+ final TypeDefinition<?> base = copy.getType();
+ final TypeDefinitionBuilder baseTdb = copy.getTypedef();
+ if (base != null && !(base instanceof UnknownType)) {
+ return copy;
+ } else if (base instanceof UnknownType) {
+ final UnknownType unknownType = (UnknownType) base;
+ final QName unknownTypeQName = unknownType.getQName();
+ final String unknownTypePrefix = unknownTypeQName.getPrefix();
+ final ModuleBuilder dependentModule = findDependentModule(modules,
+ builder, unknownTypePrefix, copy.getLine());
+ final TypeDefinitionBuilder utBuilder = getTypedefBuilder(copy,
+ modules, dependentModule);
+ copy.setType(utBuilder);
+ return copy;
+ } else if (base == null && baseTdb != null) {
+ // make a copy of baseTypeDef and call again
+ final TypeDefinitionBuilder baseTdbCopy = copyTypedefBuilder(
+ baseTdb, true);
+ final TypeDefinitionBuilder baseTdbCopyResolved = resolveCopiedBuilder(
+ baseTdbCopy, modules, builder);
+ copy.setType(baseTdbCopyResolved);
+ return copy;
} else {
- ModuleImport dependentModuleImport = getModuleImport(builder,
- unknownTypePrefix);
- String dependentModuleName = dependentModuleImport.getModuleName();
- Date dependentModuleRevision = dependentModuleImport.getRevision();
- TreeMap<Date, ModuleBuilder> moduleBuildersByRevision = modules
- .get(dependentModuleName);
- if (dependentModuleRevision == null) {
- dependentModuleBuilder = moduleBuildersByRevision.lastEntry()
- .getValue();
- } else {
- dependentModuleBuilder = moduleBuildersByRevision
- .get(dependentModuleRevision);
- }
+ throw new IllegalStateException("Failed to resolve type "
+ + copy.getQName().getLocalName());
}
+ }
- // pull all typedef statements from dependent module...
- final Set<TypeDefinitionBuilder> typedefs = dependentModuleBuilder
- .getModuleTypedefs();
- // and search for referenced typedef
- TypeDefinitionBuilder lookedUpBuilder = null;
- for (TypeDefinitionBuilder tdb : typedefs) {
- QName qname = tdb.getQName();
- if (qname.getLocalName().equals(unknownTypeName)) {
- lookedUpBuilder = tdb;
- break;
- }
+ private TypeDefinitionBuilder findTypedefBuilder(
+ final QName unknownTypeQName,
+ final Map<String, TreeMap<Date, ModuleBuilder>> modules,
+ final ModuleBuilder builder, int line) {
+ // search for module which contains referenced typedef
+ final ModuleBuilder dependentModule = findDependentModule(modules,
+ builder, unknownTypeQName.getPrefix(), line);
+ final TypeDefinitionBuilder lookedUpBuilder = findTypedefBuilderByName(
+ dependentModule, unknownTypeQName.getLocalName(),
+ builder.getName(), line);
+ return copyTypedefBuilder(lookedUpBuilder, true);
+ }
+
+ private TypeConstraints findConstraints(
+ final TypeAwareBuilder nodeToResolve,
+ final TypeConstraints constraints,
+ final Map<String, TreeMap<Date, ModuleBuilder>> modules,
+ final ModuleBuilder builder) {
+ // union type cannot be restricted
+ if (nodeToResolve instanceof UnionTypeBuilder) {
+ return constraints;
}
// if referenced type is UnknownType again, search recursively with
// current constraints
- TypeDefinition<?> referencedType = lookedUpBuilder.getBaseType();
- if (referencedType instanceof UnknownType) {
- UnknownType unknown = (UnknownType) lookedUpBuilder.getBaseType();
-
- final List<RangeConstraint> ranges = unknown.getRangeStatements();
+ final TypeDefinition<?> referencedType = nodeToResolve.getType();
+ List<RangeConstraint> ranges = Collections.emptyList();
+ List<LengthConstraint> lengths = Collections.emptyList();
+ List<PatternConstraint> patterns = Collections.emptyList();
+ Integer fractionDigits = null;
+ if (referencedType == null) {
+ final TypeDefinitionBuilder tdb = (TypeDefinitionBuilder) nodeToResolve;
+ ranges = tdb.getRanges();
constraints.addRanges(ranges);
- final List<LengthConstraint> lengths = unknown
- .getLengthStatements();
+ lengths = tdb.getLengths();
constraints.addLengths(lengths);
- final List<PatternConstraint> patterns = unknown.getPatterns();
+ patterns = tdb.getPatterns();
constraints.addPatterns(patterns);
- return findTypeDefinitionBuilderWithConstraints(constraints,
- modules, unknown, dependentModuleBuilder);
+ fractionDigits = tdb.getFractionDigits();
+ constraints.setFractionDigits(fractionDigits);
+ return constraints;
+ } else if (referencedType instanceof ExtendedType) {
+ final ExtendedType ext = (ExtendedType) referencedType;
+ ranges = ext.getRanges();
+ constraints.addRanges(ranges);
+ lengths = ext.getLengths();
+ constraints.addLengths(lengths);
+ patterns = ext.getPatterns();
+ constraints.addPatterns(patterns);
+ fractionDigits = ext.getFractionDigits();
+ constraints.setFractionDigits(fractionDigits);
+ return findConstraints(
+ findTypedefBuilder(ext.getQName(), modules, builder,
+ nodeToResolve.getLine()), constraints, modules,
+ builder);
+ } else if (referencedType instanceof UnknownType) {
+ final UnknownType unknown = (UnknownType) referencedType;
+ ranges = unknown.getRangeStatements();
+ constraints.addRanges(ranges);
+ lengths = unknown.getLengthStatements();
+ constraints.addLengths(lengths);
+ patterns = unknown.getPatterns();
+ constraints.addPatterns(patterns);
+ fractionDigits = unknown.getFractionDigits();
+ constraints.setFractionDigits(fractionDigits);
+
+ String unknownTypePrefix = unknown.getQName().getPrefix();
+ if (unknownTypePrefix == null || "".equals(unknownTypePrefix)) {
+ unknownTypePrefix = builder.getPrefix();
+ }
+ final ModuleBuilder dependentModule = findDependentModule(modules,
+ builder, unknown.getQName().getPrefix(),
+ nodeToResolve.getLine());
+ final TypeDefinitionBuilder utBuilder = findTypedefBuilder(
+ unknown.getQName(), modules, builder,
+ nodeToResolve.getLine());
+ return findConstraints(utBuilder, constraints, modules,
+ dependentModule);
} else {
- // pull restriction from this base type and add them to
- // 'constraints'
- if (referencedType instanceof DecimalTypeDefinition) {
- constraints.addRanges(((DecimalTypeDefinition) referencedType)
- .getRangeStatements());
- constraints
- .setFractionDigits(((DecimalTypeDefinition) referencedType)
- .getFractionDigits());
- } else if (referencedType instanceof IntegerTypeDefinition) {
- constraints.addRanges(((IntegerTypeDefinition) referencedType)
- .getRangeStatements());
- } else if (referencedType instanceof StringTypeDefinition) {
- constraints.addPatterns(((StringTypeDefinition) referencedType)
- .getPatterns());
- } else if (referencedType instanceof BinaryTypeDefinition) {
- constraints.addLengths(((BinaryTypeDefinition) referencedType)
- .getLengthConstraints());
- } else if (referencedType instanceof InstanceIdentifierTypeDefinition) {
- // TODO: instance-identifier
+ // HANDLE BASE YANG TYPE
+ mergeConstraints(referencedType, constraints);
+ return constraints;
+ }
+ }
+
+ /**
+ * Go through all typedef statements from given module and search for one
+ * with given name
+ *
+ * @param typedefs
+ * typedef statements to search
+ * @param name
+ * name of searched typedef
+ * @return typedef with name equals to given name
+ */
+ private TypeDefinitionBuilder findTypedefBuilderByName(
+ final ModuleBuilder dependentModule, final String name,
+ final String currentModuleName, final int line) {
+ TypeDefinitionBuilder result = null;
+ final Set<TypeDefinitionBuilder> typedefs = dependentModule
+ .getModuleTypedefs();
+ for (TypeDefinitionBuilder td : typedefs) {
+ if (td.getQName().getLocalName().equals(name)) {
+ result = td;
+ break;
}
+ }
+ if (result == null) {
+ throw new YangParseException(currentModuleName, line,
+ "Target module '" + dependentModule.getName()
+ + "' does not contain typedef '" + name + "'.");
+ }
+ return result;
+ }
- result.put(lookedUpBuilder, constraints);
- return result;
+ /**
+ * Pull restriction from referenced type and add them to given constraints
+ *
+ * @param referencedType
+ * @param constraints
+ */
+ private void mergeConstraints(final TypeDefinition<?> referencedType,
+ final TypeConstraints constraints) {
+
+ if (referencedType instanceof DecimalTypeDefinition) {
+ constraints.addRanges(((DecimalTypeDefinition) referencedType)
+ .getRangeStatements());
+ constraints
+ .setFractionDigits(((DecimalTypeDefinition) referencedType)
+ .getFractionDigits());
+ } else if (referencedType instanceof IntegerTypeDefinition) {
+ constraints.addRanges(((IntegerTypeDefinition) referencedType)
+ .getRangeStatements());
+ } else if (referencedType instanceof StringTypeDefinition) {
+ constraints.addPatterns(((StringTypeDefinition) referencedType)
+ .getPatterns());
+ constraints.addLengths(((StringTypeDefinition) referencedType)
+ .getLengthStatements());
+ } else if (referencedType instanceof BinaryTypeDefinition) {
+ constraints.addLengths(((BinaryTypeDefinition) referencedType)
+ .getLengthConstraints());
}
}
/**
- * Go through all augmentation definitions and resolve them. This means find
- * referenced node and add child nodes to it.
+ * Go through all augmentation definitions and resolve them. This method
+ * also finds referenced node and add child nodes to it.
*
* @param modules
* all available modules
- * @param module
- * current module
*/
private void resolveAugments(
- Map<String, TreeMap<Date, ModuleBuilder>> modules,
- ModuleBuilder module) {
- Set<AugmentationSchemaBuilder> augmentBuilders = module
- .getAddedAugments();
-
- Set<AugmentationSchema> augments = new HashSet<AugmentationSchema>();
- for (AugmentationSchemaBuilder augmentBuilder : augmentBuilders) {
- SchemaPath augmentTargetSchemaPath = augmentBuilder.getTargetPath();
- String prefix = null;
- List<String> augmentTargetPath = new ArrayList<String>();
- for (QName pathPart : augmentTargetSchemaPath.getPath()) {
- prefix = pathPart.getPrefix();
- augmentTargetPath.add(pathPart.getLocalName());
+ final Map<String, TreeMap<Date, ModuleBuilder>> modules) {
+ final List<ModuleBuilder> allModulesList = new ArrayList<ModuleBuilder>();
+ final Set<ModuleBuilder> allModulesSet = new HashSet<ModuleBuilder>();
+ for (Map.Entry<String, TreeMap<Date, ModuleBuilder>> entry : modules
+ .entrySet()) {
+ for (Map.Entry<Date, ModuleBuilder> inner : entry.getValue()
+ .entrySet()) {
+ allModulesList.add(inner.getValue());
+ allModulesSet.add(inner.getValue());
}
- ModuleImport dependentModuleImport = getModuleImport(module,
- prefix);
- String dependentModuleName = dependentModuleImport.getModuleName();
- augmentTargetPath.add(0, dependentModuleName);
-
- Date dependentModuleRevision = dependentModuleImport.getRevision();
+ }
- TreeMap<Date, ModuleBuilder> moduleBuildersByRevision = modules
- .get(dependentModuleName);
- ModuleBuilder dependentModule;
- if (dependentModuleRevision == null) {
- dependentModule = moduleBuildersByRevision.lastEntry()
- .getValue();
- } else {
- dependentModule = moduleBuildersByRevision
- .get(dependentModuleRevision);
+ for (int i = 0; i < allModulesList.size(); i++) {
+ final ModuleBuilder module = allModulesList.get(i);
+ // try to resolve augments in module
+ resolveAugment(modules, module);
+ // while all augments are not resolved
+ final Iterator<ModuleBuilder> allModulesIterator = allModulesSet
+ .iterator();
+ while (!(module.getAugmentsResolved() == module.getAddedAugments()
+ .size())) {
+ ModuleBuilder nextModule = null;
+ // try resolve other module augments
+ try {
+ nextModule = allModulesIterator.next();
+ resolveAugment(modules, nextModule);
+ } catch (NoSuchElementException e) {
+ throw new YangParseException(
+ "Failed to resolve augments in module '"
+ + module.getName() + "'.", e);
+ }
+ // then try to resolve first module again
+ resolveAugment(modules, module);
}
-
- AugmentationTargetBuilder augmentTarget = (AugmentationTargetBuilder) dependentModule
- .getNode(augmentTargetPath);
- AugmentationSchema result = augmentBuilder.build();
- augmentTarget.addAugmentation(result);
- fillAugmentTarget(augmentBuilder, (ChildNodeBuilder) augmentTarget);
- augments.add(result);
}
- module.setAugmentations(augments);
}
/**
- * Add all augment's child nodes to given target.
*
- * @param augment
- * @param target
+ * @param modules
+ * all available modules
+ * @param module
+ * current module
*/
- private void fillAugmentTarget(AugmentationSchemaBuilder augment,
- ChildNodeBuilder target) {
- for (DataSchemaNodeBuilder builder : augment.getChildNodes()) {
- builder.setAugmenting(true);
- target.addChildNode(builder);
+ private void resolveAugment(
+ final Map<String, TreeMap<Date, ModuleBuilder>> modules,
+ final ModuleBuilder module) {
+ if (module.getAugmentsResolved() < module.getAddedAugments().size()) {
+ for (AugmentationSchemaBuilder augmentBuilder : module
+ .getAddedAugments()) {
+ final SchemaPath augmentTargetSchemaPath = augmentBuilder
+ .getTargetPath();
+ final List<QName> path = augmentTargetSchemaPath.getPath();
+
+ int i = 0;
+ final QName qname = path.get(i);
+ String prefix = qname.getPrefix();
+ if (prefix == null) {
+ prefix = module.getPrefix();
+ }
+
+ DataSchemaNodeBuilder currentParent = null;
+ final ModuleBuilder dependentModule = findDependentModule(
+ modules, module, prefix, augmentBuilder.getLine());
+ for (DataSchemaNodeBuilder child : dependentModule
+ .getChildNodes()) {
+ final QName childQName = child.getQName();
+ if (childQName.getLocalName().equals(qname.getLocalName())) {
+ currentParent = child;
+ i++;
+ break;
+ }
+ }
+
+ for (; i < path.size(); i++) {
+ final QName currentQName = path.get(i);
+ DataSchemaNodeBuilder newParent = null;
+ for (DataSchemaNodeBuilder child : ((ChildNodeBuilder) currentParent)
+ .getChildNodes()) {
+ final QName childQName = child.getQName();
+ if (childQName.getLocalName().equals(
+ currentQName.getLocalName())) {
+ newParent = child;
+ break;
+ }
+ }
+ if (newParent == null) {
+ break; // node not found, quit search
+ } else {
+ currentParent = newParent;
+ }
+ }
+
+ final QName currentQName = currentParent.getQName();
+ final QName lastAugmentPathElement = path.get(path.size() - 1);
+ if (currentQName.getLocalName().equals(
+ lastAugmentPathElement.getLocalName())) {
+ ParserUtils.fillAugmentTarget(augmentBuilder,
+ (ChildNodeBuilder) currentParent);
+ ((AugmentationTargetBuilder) currentParent)
+ .addAugmentation(augmentBuilder);
+ module.augmentResolved();
+ }
+ }
}
}
/**
- * Get module import referenced by given prefix.
+ * Go through identity statements defined in current module and resolve
+ * their 'base' statement if present.
*
- * @param builder
- * module to search
- * @param prefix
- * prefix associated with import
- * @return ModuleImport based on given prefix
+ * @param modules
+ * all modules
+ * @param module
+ * module being resolved
*/
- private ModuleImport getModuleImport(ModuleBuilder builder, String prefix) {
- ModuleImport moduleImport = null;
- for (ModuleImport mi : builder.getModuleImports()) {
- if (mi.getPrefix().equals(prefix)) {
- moduleImport = mi;
- break;
+ private void resolveIdentities(
+ final Map<String, TreeMap<Date, ModuleBuilder>> modules,
+ final ModuleBuilder module) {
+ final Set<IdentitySchemaNodeBuilder> identities = module
+ .getAddedIdentities();
+ for (IdentitySchemaNodeBuilder identity : identities) {
+ final String baseIdentityName = identity.getBaseIdentityName();
+ if (baseIdentityName != null) {
+ String baseIdentityPrefix = null;
+ String baseIdentityLocalName = null;
+ if (baseIdentityName.contains(":")) {
+ final String[] splitted = baseIdentityName.split(":");
+ baseIdentityPrefix = splitted[0];
+ baseIdentityLocalName = splitted[1];
+ } else {
+ baseIdentityPrefix = module.getPrefix();
+ baseIdentityLocalName = baseIdentityName;
+ }
+ final ModuleBuilder dependentModule = findDependentModule(
+ modules, module, baseIdentityPrefix, identity.getLine());
+
+ final Set<IdentitySchemaNodeBuilder> dependentModuleIdentities = dependentModule
+ .getAddedIdentities();
+ for (IdentitySchemaNodeBuilder idBuilder : dependentModuleIdentities) {
+ if (idBuilder.getQName().getLocalName()
+ .equals(baseIdentityLocalName)) {
+ identity.setBaseIdentity(idBuilder);
+ }
+ }
}
}
- return moduleImport;
}
/**
- * Helper method for resolving special 'min' or 'max' values in range
- * constraint
+ * Go through uses statements defined in current module and resolve their
+ * refine statements.
*
- * @param ranges ranges to resolve
- * @param targetType target type
- * @param modules all available modules
- * @param builder current module
+ * @param modules
+ * all modules
+ * @param module
+ * module being resolved
*/
- private void resolveRanges(List<RangeConstraint> ranges,
- TypeDefinitionBuilder targetType,
- Map<String, TreeMap<Date, ModuleBuilder>> modules,
- ModuleBuilder builder) {
- if (ranges != null && ranges.size() > 0) {
- Long min = (Long) ranges.get(0).getMin();
- Long max = (Long) ranges.get(ranges.size() - 1).getMax();
- // if range contains one of the special values 'min' or 'max'
- if (min.equals(Long.MIN_VALUE) || max.equals(Long.MAX_VALUE)) {
- Long[] values = parseRangeConstraint(targetType, modules,
- builder);
- if (min.equals(Long.MIN_VALUE)) {
- min = values[0];
- RangeConstraint oldFirst = ranges.get(0);
- RangeConstraint newFirst = BaseConstraints.rangeConstraint(
- min, oldFirst.getMax(), oldFirst.getDescription(),
- oldFirst.getReference());
- ranges.set(0, newFirst);
- }
- if (max.equals(Long.MAX_VALUE)) {
- max = values[1];
- RangeConstraint oldLast = ranges.get(ranges.size() - 1);
- RangeConstraint newLast = BaseConstraints.rangeConstraint(
- oldLast.getMin(), max, oldLast.getDescription(),
- oldLast.getReference());
- ranges.set(ranges.size() - 1, newLast);
+ private void resolveUses(
+ final Map<String, TreeMap<Date, ModuleBuilder>> modules,
+ final ModuleBuilder module) {
+ final Map<List<String>, UsesNodeBuilder> moduleUses = module
+ .getAddedUsesNodes();
+ for (Map.Entry<List<String>, UsesNodeBuilder> entry : moduleUses
+ .entrySet()) {
+ final List<String> key = entry.getKey();
+ final UsesNodeBuilder usesNode = entry.getValue();
+
+ final String groupingName = key.get(key.size() - 1);
+
+ for (RefineHolder refine : usesNode.getRefines()) {
+ // refine statements
+ final String defaultStr = refine.getDefaultStr();
+ final Boolean mandatory = refine.isMandatory();
+ final MustDefinition must = refine.getMust();
+ final Boolean presence = refine.isPresence();
+ final Integer min = refine.getMinElements();
+ final Integer max = refine.getMaxElements();
+ final List<UnknownSchemaNodeBuilder> unknownNodes = refine
+ .getUnknownNodes();
+
+ Builder refineTarget = getRefineTargetBuilder(groupingName,
+ refine, modules, module);
+ if (refineTarget instanceof LeafSchemaNodeBuilder) {
+ final LeafSchemaNodeBuilder leaf = (LeafSchemaNodeBuilder) refineTarget;
+ if (defaultStr != null && !("".equals(defaultStr))) {
+ leaf.setDefaultStr(defaultStr);
+ }
+ if (mandatory != null) {
+ leaf.getConstraints().setMandatory(mandatory);
+ }
+ if (must != null) {
+ leaf.getConstraints().addMustDefinition(must);
+ }
+ if (unknownNodes != null) {
+ for (UnknownSchemaNodeBuilder unknown : unknownNodes) {
+ leaf.addUnknownSchemaNode(unknown);
+ }
+ }
+ usesNode.addRefineNode(leaf);
+ } else if (refineTarget instanceof ContainerSchemaNodeBuilder) {
+ final ContainerSchemaNodeBuilder container = (ContainerSchemaNodeBuilder) refineTarget;
+ if (presence != null) {
+ container.setPresence(presence);
+ }
+ if (must != null) {
+ container.getConstraints().addMustDefinition(must);
+ }
+ if (unknownNodes != null) {
+ for (UnknownSchemaNodeBuilder unknown : unknownNodes) {
+ container.addUnknownSchemaNode(unknown);
+ }
+ }
+ usesNode.addRefineNode(container);
+ } else if (refineTarget instanceof ListSchemaNodeBuilder) {
+ final ListSchemaNodeBuilder list = (ListSchemaNodeBuilder) refineTarget;
+ if (must != null) {
+ list.getConstraints().addMustDefinition(must);
+ }
+ if (min != null) {
+ list.getConstraints().setMinElements(min);
+ }
+ if (max != null) {
+ list.getConstraints().setMaxElements(max);
+ }
+ if (unknownNodes != null) {
+ for (UnknownSchemaNodeBuilder unknown : unknownNodes) {
+ list.addUnknownSchemaNode(unknown);
+ }
+ }
+ } else if (refineTarget instanceof LeafListSchemaNodeBuilder) {
+ final LeafListSchemaNodeBuilder leafList = (LeafListSchemaNodeBuilder) getRefineTargetBuilder(
+ groupingName, refine, modules, module);
+ if (must != null) {
+ leafList.getConstraints().addMustDefinition(must);
+ }
+ if (min != null) {
+ leafList.getConstraints().setMinElements(min);
+ }
+ if (max != null) {
+ leafList.getConstraints().setMaxElements(max);
+ }
+ if (unknownNodes != null) {
+ for (UnknownSchemaNodeBuilder unknown : unknownNodes) {
+ leafList.addUnknownSchemaNode(unknown);
+ }
+ }
+ } else if (refineTarget instanceof ChoiceBuilder) {
+ final ChoiceBuilder choice = (ChoiceBuilder) refineTarget;
+ if (defaultStr != null) {
+ choice.setDefaultCase(defaultStr);
+ }
+ if (mandatory != null) {
+ choice.getConstraints().setMandatory(mandatory);
+ }
+ if (unknownNodes != null) {
+ for (UnknownSchemaNodeBuilder unknown : unknownNodes) {
+ choice.addUnknownSchemaNode(unknown);
+ }
+ }
+ } else if (refineTarget instanceof AnyXmlBuilder) {
+ final AnyXmlBuilder anyXml = (AnyXmlBuilder) refineTarget;
+ if (mandatory != null) {
+ anyXml.getConstraints().setMandatory(mandatory);
+ }
+ if (must != null) {
+ anyXml.getConstraints().addMustDefinition(must);
+ }
+ if (unknownNodes != null) {
+ for (UnknownSchemaNodeBuilder unknown : unknownNodes) {
+ anyXml.addUnknownSchemaNode(unknown);
+ }
+ }
}
}
}
}
/**
- * Helper method for resolving special 'min' or 'max' values in length
- * constraint
+ * Find original builder of refine node and return copy of this builder.
*
- * @param lengths lengths to resolve
- * @param targetType target type
- * @param modules all available modules
- * @param builder current module
+ * @param groupingPath
+ * path to grouping which contains node to refine
+ * @param refine
+ * refine object containing informations about refine
+ * @param modules
+ * all loaded modules
+ * @param module
+ * current module
+ * @return copy of Builder object of node to be refined if it is present in
+ * grouping, null otherwise
*/
- private void resolveLengths(List<LengthConstraint> lengths,
- TypeDefinitionBuilder targetType,
- Map<String, TreeMap<Date, ModuleBuilder>> modules,
- ModuleBuilder builder) {
- if (lengths != null && lengths.size() > 0) {
- Long min = lengths.get(0).getMin();
- Long max = lengths.get(lengths.size() - 1).getMax();
- // if length contains one of the special values 'min' or 'max'
- if (min.equals(Long.MIN_VALUE) || max.equals(Long.MAX_VALUE)) {
- Long[] values = parseRangeConstraint(targetType, modules,
- builder);
- if (min.equals(Long.MIN_VALUE)) {
- min = values[0];
- LengthConstraint oldFirst = lengths.get(0);
- LengthConstraint newFirst = BaseConstraints
- .lengthConstraint(min, oldFirst.getMax(),
- oldFirst.getDescription(),
- oldFirst.getReference());
- lengths.set(0, newFirst);
- }
- if (max.equals(Long.MAX_VALUE)) {
- max = values[1];
- LengthConstraint oldLast = lengths.get(lengths.size() - 1);
- LengthConstraint newLast = BaseConstraints
- .lengthConstraint(oldLast.getMin(), max,
- oldLast.getDescription(),
- oldLast.getReference());
- lengths.set(lengths.size() - 1, newLast);
+ private Builder getRefineTargetBuilder(final String groupingPath,
+ final RefineHolder refine,
+ final Map<String, TreeMap<Date, ModuleBuilder>> modules,
+ final ModuleBuilder module) {
+ Builder result = null;
+ final Builder lookedUpBuilder = findRefineTargetBuilder(groupingPath,
+ refine, modules, module);
+ if (lookedUpBuilder instanceof LeafSchemaNodeBuilder) {
+ result = ParserUtils
+ .copyLeafBuilder((LeafSchemaNodeBuilder) lookedUpBuilder);
+ } else if (lookedUpBuilder instanceof ContainerSchemaNodeBuilder) {
+ result = ParserUtils
+ .copyContainerBuilder((ContainerSchemaNodeBuilder) lookedUpBuilder);
+ } else if (lookedUpBuilder instanceof ListSchemaNodeBuilder) {
+ result = ParserUtils
+ .copyListBuilder((ListSchemaNodeBuilder) lookedUpBuilder);
+ } else if (lookedUpBuilder instanceof LeafListSchemaNodeBuilder) {
+ result = ParserUtils
+ .copyLeafListBuilder((LeafListSchemaNodeBuilder) lookedUpBuilder);
+ } else if (lookedUpBuilder instanceof ChoiceBuilder) {
+ result = ParserUtils
+ .copyChoiceBuilder((ChoiceBuilder) lookedUpBuilder);
+ } else if (lookedUpBuilder instanceof AnyXmlBuilder) {
+ result = ParserUtils
+ .copyAnyXmlBuilder((AnyXmlBuilder) lookedUpBuilder);
+ } else {
+ throw new YangParseException(module.getName(), refine.getLine(),
+ "Target '" + refine.getName() + "' can not be refined");
+ }
+ return result;
+ }
+
+ /**
+ * Find builder of refine node.
+ *
+ * @param groupingPath
+ * path to grouping which contains node to refine
+ * @param refineNodeName
+ * name of node to be refined
+ * @param modules
+ * all loaded modules
+ * @param module
+ * current module
+ * @return Builder object of refine node if it is present in grouping, null
+ * otherwise
+ */
+ private Builder findRefineTargetBuilder(final String groupingPath,
+ final RefineHolder refine,
+ final Map<String, TreeMap<Date, ModuleBuilder>> modules,
+ final ModuleBuilder module) {
+ final SchemaPath path = ParserUtils.parseUsesPath(groupingPath);
+ final List<String> builderPath = new ArrayList<String>();
+ String prefix = null;
+ for (QName qname : path.getPath()) {
+ builderPath.add(qname.getLocalName());
+ prefix = qname.getPrefix();
+ }
+ if (prefix == null) {
+ prefix = module.getPrefix();
+ }
+
+ final ModuleBuilder dependentModule = findDependentModule(modules,
+ module, prefix, refine.getLine());
+ builderPath.add(0, "grouping");
+ builderPath.add(0, dependentModule.getName());
+ final GroupingBuilder builder = (GroupingBuilder) dependentModule
+ .getNode(builderPath);
+
+ return builder.getChildNode(refine.getName());
+ }
+
+ private QName findFullQName(
+ final Map<String, TreeMap<Date, ModuleBuilder>> modules,
+ final ModuleBuilder module, final IdentityrefTypeBuilder idref) {
+ QName result = null;
+ String baseString = idref.getBaseString();
+ if (baseString.contains(":")) {
+ String[] splittedBase = baseString.split(":");
+ if (splittedBase.length > 2) {
+ throw new YangParseException(module.getName(), idref.getLine(),
+ "Failed to parse identityref base: " + baseString);
+ }
+ String prefix = splittedBase[0];
+ String name = splittedBase[1];
+ ModuleBuilder dependentModule = findDependentModule(modules,
+ module, prefix, idref.getLine());
+ result = new QName(dependentModule.getNamespace(),
+ dependentModule.getRevision(), prefix, name);
+ } else {
+ result = new QName(module.getNamespace(), module.getRevision(),
+ module.getPrefix(), baseString);
+ }
+ return result;
+ }
+
+ private void resolveUnknownNodes(
+ final Map<String, TreeMap<Date, ModuleBuilder>> modules,
+ final ModuleBuilder module) {
+ for (UnknownSchemaNodeBuilder usnb : module.getAddedUnknownNodes()) {
+ QName nodeType = usnb.getNodeType();
+ if (nodeType.getNamespace() == null
+ || nodeType.getRevision() == null) {
+ try {
+ ModuleBuilder dependentModule = findDependentModule(
+ modules, module, nodeType.getPrefix(),
+ usnb.getLine());
+ QName newNodeType = new QName(
+ dependentModule.getNamespace(),
+ dependentModule.getRevision(),
+ nodeType.getPrefix(), nodeType.getLocalName());
+ usnb.setNodeType(newNodeType);
+ } catch (YangParseException e) {
+ logger.debug(module.getName(), usnb.getLine(),
+ "Failed to find unknown node type: " + nodeType);
}
}
}
}
- private Long[] parseRangeConstraint(TypeDefinitionBuilder targetType,
- Map<String, TreeMap<Date, ModuleBuilder>> modules,
- ModuleBuilder builder) {
- TypeDefinition<?> targetBaseType = targetType.getBaseType();
-
- if (targetBaseType instanceof IntegerTypeDefinition) {
- IntegerTypeDefinition itd = (IntegerTypeDefinition) targetBaseType;
- List<RangeConstraint> ranges = itd.getRangeStatements();
- Long min = (Long) ranges.get(0).getMin();
- Long max = (Long) ranges.get(ranges.size() - 1).getMax();
- return new Long[] { min, max };
- } else if (targetBaseType instanceof DecimalTypeDefinition) {
- DecimalTypeDefinition dtd = (DecimalTypeDefinition) targetBaseType;
- List<RangeConstraint> ranges = dtd.getRangeStatements();
- Long min = (Long) ranges.get(0).getMin();
- Long max = (Long) ranges.get(ranges.size() - 1).getMax();
- return new Long[] { min, max };
+ /**
+ * Find dependent module based on given prefix
+ *
+ * @param modules
+ * all available modules
+ * @param module
+ * current module
+ * @param prefix
+ * target module prefix
+ * @return
+ */
+ private ModuleBuilder findDependentModule(
+ final Map<String, TreeMap<Date, ModuleBuilder>> modules,
+ final ModuleBuilder module, final String prefix, final int line) {
+ ModuleBuilder dependentModule = null;
+ Date dependentModuleRevision = null;
+
+ if (prefix.equals(module.getPrefix())) {
+ dependentModule = module;
} else {
- return parseRangeConstraint(
- findTypeDefinitionBuilder(modules,
- (UnknownType) targetBaseType, builder), modules,
- builder);
+ final ModuleImport dependentModuleImport = ParserUtils
+ .getModuleImport(module, prefix);
+ if (dependentModuleImport == null) {
+ throw new YangParseException(module.getName(), line,
+ "No import found with prefix '" + prefix + "'.");
+ }
+ final String dependentModuleName = dependentModuleImport
+ .getModuleName();
+ dependentModuleRevision = dependentModuleImport.getRevision();
+
+ final TreeMap<Date, ModuleBuilder> moduleBuildersByRevision = modules
+ .get(dependentModuleName);
+ if (moduleBuildersByRevision == null) {
+ throw new YangParseException(module.getName(), line,
+ "Failed to find dependent module '"
+ + dependentModuleName + "'.");
+ }
+ if (dependentModuleRevision == null) {
+ dependentModule = moduleBuildersByRevision.lastEntry()
+ .getValue();
+ } else {
+ dependentModule = moduleBuildersByRevision
+ .get(dependentModuleRevision);
+ }
}
- }
- private Date createEpochTime() {
- Calendar c = Calendar.getInstance();
- c.setTimeInMillis(0);
- return c.getTime();
+ if (dependentModule == null) {
+ throw new YangParseException(module.getName(), line,
+ "Failed to find dependent module with prefix '" + prefix
+ + "' and revision '" + dependentModuleRevision
+ + "'.");
+ }
+ return dependentModule;
}
private static class SchemaContextImpl implements SchemaContext {
private final Set<Module> modules;
- private SchemaContextImpl(Set<Module> modules) {
+ private SchemaContextImpl(final Set<Module> modules) {
this.modules = modules;
}
}
return extensions;
}
- }
-
- private static class TypeConstraints {
- private final List<RangeConstraint> ranges = new ArrayList<RangeConstraint>();
- private final List<LengthConstraint> lengths = new ArrayList<LengthConstraint>();
- private final List<PatternConstraint> patterns = new ArrayList<PatternConstraint>();
- private Integer fractionDigits;
-
- public List<RangeConstraint> getRanges() {
- return ranges;
- }
-
- public void addRanges(List<RangeConstraint> ranges) {
- this.ranges.addAll(0, ranges);
- }
-
- public List<LengthConstraint> getLengths() {
- return lengths;
- }
- public void addLengths(List<LengthConstraint> lengths) {
- this.lengths.addAll(0, lengths);
- }
-
- public List<PatternConstraint> getPatterns() {
- return patterns;
- }
-
- public void addPatterns(List<PatternConstraint> patterns) {
- this.patterns.addAll(0, patterns);
- }
-
- public Integer getFractionDigits() {
- return fractionDigits;
+ @Override
+ public Module findModuleByName(final String name, final Date revision) {
+ if ((name != null) && (revision != null)) {
+ for (final Module module : modules) {
+ if (module.getName().equals(name)
+ && module.getRevision().equals(revision)) {
+ return module;
+ }
+ }
+ }
+ return null;
}
- public void setFractionDigits(Integer fractionDigits) {
- if (fractionDigits != null) {
- this.fractionDigits = fractionDigits;
+ @Override
+ public Module findModuleByNamespace(final URI namespace) {
+ if (namespace != null) {
+ for (final Module module : modules) {
+ if (module.getNamespace().equals(namespace)) {
+ return module;
+ }
+ }
}
+ return null;
}
}