import com.google.common.base.Optional;
import com.google.common.base.Splitter;
import com.google.common.collect.Lists;
-
import java.math.BigDecimal;
import java.math.BigInteger;
-import java.net.URI;
import java.util.ArrayList;
import java.util.Collections;
-import java.util.Date;
import java.util.Iterator;
import java.util.List;
-import java.util.Stack;
-
import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.tree.ParseTree;
import org.antlr.v4.runtime.tree.TerminalNode;
import org.opendaylight.yangtools.yang.model.util.Decimal64;
import org.opendaylight.yangtools.yang.model.util.EnumerationType;
import org.opendaylight.yangtools.yang.model.util.ExtendedType;
-import org.opendaylight.yangtools.yang.model.util.InstanceIdentifier;
+import org.opendaylight.yangtools.yang.model.util.InstanceIdentifierType;
import org.opendaylight.yangtools.yang.model.util.Int16;
import org.opendaylight.yangtools.yang.model.util.Int32;
import org.opendaylight.yangtools.yang.model.util.Int64;
import org.opendaylight.yangtools.yang.model.util.Uint32;
import org.opendaylight.yangtools.yang.model.util.Uint64;
import org.opendaylight.yangtools.yang.model.util.Uint8;
-import org.opendaylight.yangtools.yang.model.util.UnknownType;
import org.opendaylight.yangtools.yang.parser.builder.api.Builder;
import org.opendaylight.yangtools.yang.parser.builder.api.ConstraintsBuilder;
import org.opendaylight.yangtools.yang.parser.builder.api.DataSchemaNodeBuilder;
import org.opendaylight.yangtools.yang.parser.builder.api.RefineBuilder;
import org.opendaylight.yangtools.yang.parser.builder.api.SchemaNodeBuilder;
+import org.opendaylight.yangtools.yang.parser.builder.api.TypeAwareBuilder;
import org.opendaylight.yangtools.yang.parser.builder.api.TypeDefinitionBuilder;
import org.opendaylight.yangtools.yang.parser.builder.impl.ChoiceCaseBuilder;
+import org.opendaylight.yangtools.yang.parser.builder.impl.ModuleBuilder;
import org.opendaylight.yangtools.yang.parser.builder.impl.RefineHolderImpl;
+import org.opendaylight.yangtools.yang.parser.builder.impl.TypeDefinitionBuilderImpl;
import org.opendaylight.yangtools.yang.parser.builder.impl.UnionTypeBuilder;
import org.opendaylight.yangtools.yang.parser.util.TypeConstraints;
import org.opendaylight.yangtools.yang.parser.util.UnknownBoundaryNumber;
return null;
}
- /**
- * Create SchemaPath from actualPath and new node name.
- *
- * @param actualPath
- * current position in model
- * @return SchemaPath object
- */
- public static SchemaPath createActualSchemaPath(final Stack<QName> actualPath) {
- return SchemaPath.create(actualPath, true);
- }
-
/**
* Create java.util.List of key node names.
*
* @return List of EnumPair object parsed from given context
*/
private static List<EnumTypeDefinition.EnumPair> getEnumConstants(final Type_body_stmtsContext ctx,
- final Stack<QName> path, final String moduleName) {
+ final SchemaPath path, final String moduleName) {
List<EnumTypeDefinition.EnumPair> enumConstants = new ArrayList<>();
for (int i = 0; i < ctx.getChildCount(); i++) {
* @return EnumPair object parsed from given context
*/
private static EnumTypeDefinition.EnumPair createEnumPair(final Enum_stmtContext ctx, final int highestValue,
- final Stack<QName> actualPath, final String moduleName) {
+ final SchemaPath actualPath, final String moduleName) {
final String name = stringFromNode(ctx);
SchemaPath path = createTypePath(actualPath, name);
Integer value = null;
* @return PatternConstraint object
*/
private static PatternConstraint parsePatternConstraint(final Pattern_stmtContext ctx) {
- String description = null;
- String reference = null;
+ Optional<String> description = Optional.absent();
+ Optional<String> reference = Optional.absent();
for (int i = 0; i < ctx.getChildCount(); i++) {
ParseTree child = ctx.getChild(i);
if (child instanceof Description_stmtContext) {
- description = stringFromNode(child);
+ description = Optional.of(stringFromNode(child));
} else if (child instanceof Reference_stmtContext) {
- reference = stringFromNode(child);
+ reference = Optional.of(stringFromNode(child));
}
}
String pattern = parsePatternString(ctx);
- return BaseConstraints.patternConstraint(pattern, description, reference);
+ return BaseConstraints.newPatternConstraint(pattern, description, reference);
}
/**
* current module name
* @return List of Bit objects created from this context
*/
- private static List<BitsTypeDefinition.Bit> getBits(final Type_body_stmtsContext ctx, final Stack<QName> actualPath,
+ private static List<BitsTypeDefinition.Bit> getBits(final Type_body_stmtsContext ctx, final SchemaPath actualPath,
final String moduleName) {
final List<BitsTypeDefinition.Bit> bits = new ArrayList<>();
for (int j = 0; j < ctx.getChildCount(); j++) {
* @return Bit object parsed from this context
*/
private static BitsTypeDefinition.Bit parseBit(final Bit_stmtContext ctx, final long highestPosition,
- final Stack<QName> actualPath, final String moduleName) {
+ final SchemaPath actualPath, final String moduleName) {
String name = stringFromNode(ctx);
Long position = null;
}
/**
- * Parse type body and create UnknownType definition.
+ * Parse unknown type with body.
*
- * @param typedefQName
- * qname of current type
- * @param ctx
+ * @param typeBody
* type body
- * @param actualPath
- * actual path in model
- * @param namespace
- * module namespace
- * @param revision
- * module revision
- * @param prefix
- * module prefix
* @param parent
* current node parent
- * @return UnknownType object with constraints from parsed type body
+ * @param prefixedQName
+ * type qname with prefix
+ * @param moduleBuilder
+ * current module builder
+ * @param moduleQName
+ * current module qname
+ * @param actualPath
+ * actual path in model
*/
- public static TypeDefinition<?> parseUnknownTypeWithBody(final QName typedefQName,
- final Type_body_stmtsContext ctx, final Stack<QName> actualPath, final URI namespace, final Date revision,
- final String prefix, final Builder parent) {
- String moduleName = parent.getModuleName();
- String typeName = typedefQName.getLocalName();
-
- UnknownType.Builder unknownType = new UnknownType.Builder(typedefQName);
-
- if (ctx != null) {
- List<RangeConstraint> rangeStatements = getRangeConstraints(ctx, moduleName);
- List<LengthConstraint> lengthStatements = getLengthConstraints(ctx, moduleName);
- List<PatternConstraint> patternStatements = getPatternConstraint(ctx);
- Integer fractionDigits = getFractionDigits(ctx, moduleName);
-
- if (parent instanceof TypeDefinitionBuilder) {
- TypeDefinitionBuilder typedef = (TypeDefinitionBuilder) parent;
- typedef.setRanges(rangeStatements);
- typedef.setLengths(lengthStatements);
- typedef.setPatterns(patternStatements);
- typedef.setFractionDigits(fractionDigits);
- return unknownType.build();
- } else {
- TypeDefinition<?> baseType = unknownType.build();
- QName qname = new QName(namespace, revision, prefix, typeName);
- SchemaPath schemaPath = createTypePath(actualPath, typeName);
+ public static void parseUnknownTypeWithBody(Type_body_stmtsContext typeBody, TypeAwareBuilder parent,
+ QName prefixedQName, ModuleBuilder moduleBuilder, QName moduleQName, SchemaPath actualPath) {
+ final int line = typeBody.getStart().getLine();
- ExtendedType.Builder typeBuilder = new ExtendedType.Builder(qname, baseType, null, null, schemaPath);
- typeBuilder.ranges(rangeStatements);
- typeBuilder.lengths(lengthStatements);
- typeBuilder.patterns(patternStatements);
- typeBuilder.fractionDigits(fractionDigits);
+ List<RangeConstraint> rangeStatements = getRangeConstraints(typeBody, moduleBuilder.getName());
+ List<LengthConstraint> lengthStatements = getLengthConstraints(typeBody, moduleBuilder.getName());
+ List<PatternConstraint> patternStatements = getPatternConstraint(typeBody);
+ Integer fractionDigits = getFractionDigits(typeBody, moduleBuilder.getName());
- return typeBuilder.build();
- }
+ if (parent instanceof TypeDefinitionBuilder && !(parent instanceof UnionTypeBuilder)) {
+ TypeDefinitionBuilder typedef = (TypeDefinitionBuilder) parent;
+ typedef.setRanges(rangeStatements);
+ typedef.setLengths(lengthStatements);
+ typedef.setPatterns(patternStatements);
+ typedef.setFractionDigits(fractionDigits);
+ typedef.setTypeQName(prefixedQName);
+ // add parent node of this type statement to dirty nodes
+ moduleBuilder.markActualNodeDirty();
+ } else {
+ QName qname = QName.create(moduleQName, prefixedQName.getLocalName());
+ SchemaPath schemaPath = createTypePath(actualPath, prefixedQName.getLocalName());
+ TypeDefinitionBuilder typeBuilder = new TypeDefinitionBuilderImpl(moduleBuilder.getName(), line, qname, schemaPath);
+ typeBuilder.setRanges(rangeStatements);
+ typeBuilder.setLengths(lengthStatements);
+ typeBuilder.setPatterns(patternStatements);
+ typeBuilder.setFractionDigits(fractionDigits);
+ typeBuilder.setTypeQName(prefixedQName);
+ parent.setTypedef(typeBuilder);
+ moduleBuilder.getDirtyNodes().add(typeBuilder);
}
-
- return unknownType.build();
}
/**
* @return TypeDefinition object based on parsed values.
*/
public static TypeDefinition<?> parseTypeWithBody(final String typeName, final Type_body_stmtsContext typeBody,
- final Stack<QName> actualPath, final URI namespace, final Date revision, final String prefix,
- final Builder parent) {
+ final SchemaPath actualPath, final QName moduleQName, final Builder parent) {
final String moduleName = parent.getModuleName();
final int line = typeBody.getStart().getLine();
constraints.addRanges(rangeStatements);
SchemaPath baseTypePath = createBaseTypePath(actualPath, typeName);
- SchemaPath extBaseTypePath = createExtendedBaseTypePath(actualPath, namespace, revision, prefix, typeName);
+ SchemaPath extBaseTypePath = createExtendedBaseTypePath(actualPath, moduleQName, typeName);
if (parent instanceof TypeDefinitionBuilder && !(parent instanceof UnionTypeBuilder)) {
extBaseTypePath = baseTypePath;
if ("decimal64".equals(typeName)) {
if (rangeStatements.isEmpty()) {
try {
- return new Decimal64(baseTypePath, fractionDigits);
+ return Decimal64.create(baseTypePath, fractionDigits);
} catch(Exception e) {
throw new YangParseException(moduleName, line, e.getMessage());
}
}
- Decimal64 decimalType = new Decimal64(extBaseTypePath, fractionDigits);
+ Decimal64 decimalType = Decimal64.create(extBaseTypePath, fractionDigits);
constraints.addRanges(decimalType.getRangeConstraints());
baseType = decimalType;
} else if (typeName.startsWith("int")) {
constraints.addLengths(binaryType.getLengthConstraints());
baseType = binaryType;
} else if ("instance-identifier".equals(typeName)) {
- return InstanceIdentifier.create(isRequireInstance(typeBody));
+ return InstanceIdentifierType.create(isRequireInstance(typeBody));
}
if (parent instanceof TypeDefinitionBuilder && !(parent instanceof UnionTypeBuilder)) {
return baseType;
}
- List<QName> path = new ArrayList<>(actualPath);
- path.add(new QName(namespace, revision, prefix, typeName));
- SchemaPath schemaPath = SchemaPath.create(path, true);
-
- QName qname = schemaPath.getPath().get(schemaPath.getPath().size() - 1);
- ExtendedType.Builder typeBuilder = new ExtendedType.Builder(qname, baseType, "", "", schemaPath);
+ QName qname = QName.create(moduleQName, typeName);
+ SchemaPath schemaPath = actualPath.createChild(qname);
+ final Optional<String> opt = Optional.of("");
+ ExtendedType.Builder typeBuilder = ExtendedType.builder(qname, baseType, opt, opt, schemaPath);
typeBuilder.ranges(constraints.getRange());
typeBuilder.lengths(constraints.getLength());
return typeBuilder.build();
}
- private static SchemaPath createTypePath(final Stack<QName> actual, final String typeName) {
- QName last = actual.peek();
- QName typeQName = QName.create(last, typeName);
- List<QName> path = new ArrayList<>(actual);
- path.add(typeQName);
- return SchemaPath.create(path, true);
+ private static SchemaPath createTypePath(final SchemaPath actual, final String typeName) {
+ QName last = actual.getLastComponent();
+ return actual.createChild(QName.create(last, typeName));
}
- private static SchemaPath createBaseTypePath(final Stack<QName> actual, final String typeName) {
- List<QName> path = new ArrayList<>(actual);
- path.add(BaseTypes.constructQName(typeName));
- return SchemaPath.create(path, true);
+ private static SchemaPath createBaseTypePath(final SchemaPath actual, final String typeName) {
+ return actual.createChild(BaseTypes.constructQName(typeName));
}
- private static SchemaPath createExtendedBaseTypePath(final Stack<QName> actual, final URI namespace, final Date revision,
- final String prefix, final String typeName) {
- QName extTypeName = new QName(namespace, revision, prefix, typeName);
- QName baseTypeName = BaseTypes.constructQName(typeName);
- List<QName> path = new ArrayList<>(actual);
- path.add(extTypeName);
- path.add(baseTypeName);
- return SchemaPath.create(path, true);
+ private static SchemaPath createExtendedBaseTypePath(final SchemaPath actual, final QName moduleQName, final String typeName) {
+ return actual.createChild(
+ QName.create(moduleQName, typeName),
+ BaseTypes.constructQName(typeName));
}
/**
*/
private static MustDefinition parseMust(final YangParser.Must_stmtContext ctx) {
StringBuilder mustText = new StringBuilder();
- String description = null;
- String reference = null;
- String errorAppTag = null;
- String errorMessage = null;
+ Optional<String> description = Optional.absent();
+ Optional<String> reference = Optional.absent();
+ Optional<String> errorAppTag = Optional.absent();
+ Optional<String> errorMessage = Optional.absent();
for (int i = 0; i < ctx.getChildCount(); ++i) {
ParseTree child = ctx.getChild(i);
if (child instanceof StringContext) {
}
}
} else if (child instanceof Description_stmtContext) {
- description = stringFromNode(child);
+ description = Optional.of(stringFromNode(child));
} else if (child instanceof Reference_stmtContext) {
- reference = stringFromNode(child);
+ reference = Optional.of(stringFromNode(child));
} else if (child instanceof Error_app_tag_stmtContext) {
- errorAppTag = stringFromNode(child);
+ errorAppTag = Optional.of(stringFromNode(child));
} else if (child instanceof Error_message_stmtContext) {
- errorMessage = stringFromNode(child);
+ errorMessage = Optional.of(stringFromNode(child));
}
}
- return new MustDefinitionImpl(mustText.toString(), description, reference, errorAppTag, errorMessage);
+ return MustDefinitionImpl.create(mustText.toString(), description, reference, errorAppTag, errorMessage);
}
/**
for (int i = 0; i < ctx.getChildCount(); i++) {
ParseTree maxArg = ctx.getChild(i);
if (maxArg instanceof Max_value_argContext) {
- result = Integer.valueOf(stringFromNode(maxArg));
+ String maxValue = stringFromNode(maxArg);
+ if ("unbounded".equals(maxValue)) {
+ result = Integer.MAX_VALUE;
+ } else {
+ result = Integer.valueOf(maxValue);
+ }
}
}
if (result == null) {