X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fsal%2Fyang-prototype%2Fcode-generator%2Fyang-model-parser-impl%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fyang%2Fparser%2Futil%2FYangModelBuilderUtil.java;h=5423e486db13a9101eff84d9f849fb40ad5ec054;hb=f0b551cfbf794966124da7475935c27c3e7311c0;hp=f58ee740cfe4a274840098716be91a430265e70d;hpb=6341181f02c8ebd4a2b77716d310c5a151eb556a;p=controller.git 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/YangModelBuilderUtil.java index f58ee740cf..5423e486db 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/YangModelBuilderUtil.java @@ -76,13 +76,17 @@ import org.opendaylight.controller.yang.model.api.SchemaPath; import org.opendaylight.controller.yang.model.api.Status; import org.opendaylight.controller.yang.model.api.TypeDefinition; import org.opendaylight.controller.yang.model.api.UnknownSchemaNode; +import org.opendaylight.controller.yang.model.api.type.BinaryTypeDefinition; import org.opendaylight.controller.yang.model.api.type.BitsTypeDefinition; import org.opendaylight.controller.yang.model.api.type.BitsTypeDefinition.Bit; import org.opendaylight.controller.yang.model.api.type.EnumTypeDefinition; import org.opendaylight.controller.yang.model.api.type.EnumTypeDefinition.EnumPair; +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.api.type.UnsignedIntegerTypeDefinition; import org.opendaylight.controller.yang.model.util.BaseConstraints; import org.opendaylight.controller.yang.model.util.BaseTypes; import org.opendaylight.controller.yang.model.util.BinaryType; @@ -103,15 +107,18 @@ import org.opendaylight.controller.yang.model.util.Uint32; import org.opendaylight.controller.yang.model.util.Uint64; import org.opendaylight.controller.yang.model.util.Uint8; import org.opendaylight.controller.yang.model.util.UnknownType; +import org.opendaylight.controller.yang.parser.builder.api.Builder; import org.opendaylight.controller.yang.parser.builder.api.SchemaNodeBuilder; +import org.opendaylight.controller.yang.parser.builder.api.TypeDefinitionBuilder; import org.opendaylight.controller.yang.parser.builder.impl.ConstraintsBuilder; +import org.opendaylight.controller.yang.parser.builder.impl.ModuleBuilder; +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); + private static final Logger logger = LoggerFactory.getLogger(YangModelBuilderUtil.class); private YangModelBuilderUtil() { } @@ -127,8 +134,7 @@ public final class YangModelBuilderUtil { final String result = ""; for (int i = 0; i < treeNode.getChildCount(); ++i) { if (treeNode.getChild(i) instanceof StringContext) { - final StringContext context = (StringContext) treeNode - .getChild(i); + final StringContext context = (StringContext) treeNode.getChild(i); if (context != null) { return context.getChild(0).getText().replace("\"", ""); } @@ -146,8 +152,7 @@ public final class YangModelBuilderUtil { * @param builder * builder to fill in with parsed statements */ - public static void parseSchemaNodeArgs(final ParseTree ctx, - final SchemaNodeBuilder builder) { + public static void parseSchemaNodeArgs(final ParseTree ctx, final SchemaNodeBuilder builder) { for (int i = 0; i < ctx.getChildCount(); i++) { final ParseTree child = ctx.getChild(i); if (child instanceof Description_stmtContext) { @@ -221,8 +226,7 @@ public final class YangModelBuilderUtil { * @param prefix * @return SchemaPath object. */ - public static SchemaPath createActualSchemaPath( - final List actualPath, final URI namespace, + public static SchemaPath createActualSchemaPath(final List actualPath, final URI namespace, final Date revision, final String prefix) { final List path = new ArrayList(); QName qname; @@ -252,8 +256,7 @@ public final class YangModelBuilderUtil { if (splittedElement.length == 1) { name = new QName(null, null, null, splittedElement[0]); } else { - name = new QName(null, null, splittedElement[0], - splittedElement[1]); + name = new QName(null, null, splittedElement[0], splittedElement[1]); } path.add(name); } @@ -275,8 +278,8 @@ public final class YangModelBuilderUtil { * current prefix * @return YANG list key as java.util.List of QName objects */ - public static List createListKey(final String keyDefinition, - final URI namespace, final Date revision, final String prefix) { + public static List createListKey(final String keyDefinition, final URI namespace, final Date revision, + final String prefix) { List key = new ArrayList(); String[] splittedKey = keyDefinition.split(" "); @@ -302,9 +305,8 @@ public final class YangModelBuilderUtil { * @param prefix * @return List of EnumPair object parsed from given context */ - private static List getEnumConstants( - final Type_body_stmtsContext ctx, final List path, - final URI namespace, final Date revision, final String prefix) { + private static List getEnumConstants(final Type_body_stmtsContext ctx, + final List path, final URI namespace, final Date revision, final String prefix) { List enumConstants = new ArrayList(); for (int j = 0; j < ctx.getChildCount(); j++) { @@ -314,9 +316,8 @@ public final class YangModelBuilderUtil { for (int k = 0; k < enumSpecChild.getChildCount(); k++) { ParseTree enumChild = enumSpecChild.getChild(k); if (enumChild instanceof Enum_stmtContext) { - EnumPair enumPair = createEnumPair( - (Enum_stmtContext) enumChild, highestValue, - path, namespace, revision, prefix); + EnumPair enumPair = createEnumPair((Enum_stmtContext) enumChild, highestValue, path, namespace, + revision, prefix); if (enumPair.getValue() > highestValue) { highestValue = enumPair.getValue(); } @@ -342,10 +343,8 @@ public final class YangModelBuilderUtil { * @param prefix * @return EnumPair object parsed from given context */ - private static EnumTypeDefinition.EnumPair createEnumPair( - final Enum_stmtContext ctx, final int highestValue, - final List path, final URI namespace, final Date revision, - final String prefix) { + private static EnumTypeDefinition.EnumPair createEnumPair(final Enum_stmtContext ctx, final int highestValue, + final List path, final URI namespace, final Date revision, final String prefix) { final String name = stringFromNode(ctx); final QName qname = new QName(namespace, revision, prefix, name); Integer value = null; @@ -375,18 +374,13 @@ public final class YangModelBuilderUtil { value = highestValue + 1; } if (value < -2147483648 || value > 2147483647) { - throw new YangParseException( - ctx.getStart().getLine(), - "Error on enum '" - + name - + "': the enum value MUST be in the range from -2147483648 to 2147483647, but was: " - + value); + throw new YangParseException(ctx.getStart().getLine(), "Error on enum '" + name + + "': the enum value MUST be in the range from -2147483648 to 2147483647, but was: " + value); } EnumPairImpl result = new EnumPairImpl(); result.qname = qname; - result.path = createActualSchemaPath(enumPairPath, namespace, revision, - prefix); + result.path = createActualSchemaPath(enumPairPath, namespace, revision, prefix); result.description = description; result.reference = reference; result.status = status; @@ -404,8 +398,7 @@ public final class YangModelBuilderUtil { private String description; private String reference; private Status status; - private List extensionSchemaNodes = Collections - .emptyList(); + private List extensionSchemaNodes = Collections.emptyList(); private String name; private Integer value; @@ -455,10 +448,7 @@ public final class YangModelBuilderUtil { int result = 1; result = prime * result + ((qname == null) ? 0 : qname.hashCode()); result = prime * result + ((path == null) ? 0 : path.hashCode()); - result = prime - * result - + ((extensionSchemaNodes == null) ? 0 - : extensionSchemaNodes.hashCode()); + result = prime * result + ((extensionSchemaNodes == null) ? 0 : extensionSchemaNodes.hashCode()); result = prime * result + ((name == null) ? 0 : name.hashCode()); result = prime * result + ((value == null) ? 0 : value.hashCode()); return result; @@ -516,8 +506,7 @@ public final class YangModelBuilderUtil { @Override public String toString() { - return EnumTypeDefinition.EnumPair.class.getSimpleName() + "[name=" - + name + ", value=" + value + "]"; + return EnumTypeDefinition.EnumPair.class.getSimpleName() + "[name=" + name + ", value=" + value + "]"; } } @@ -528,8 +517,7 @@ public final class YangModelBuilderUtil { * type body context to parse * @return List of RangeConstraint created from this context */ - private static List getRangeConstraints( - final Type_body_stmtsContext ctx) { + private static List getRangeConstraints(final Type_body_stmtsContext ctx) { List rangeConstraints = Collections.emptyList(); outer: for (int j = 0; j < ctx.getChildCount(); j++) { ParseTree numRestrChild = ctx.getChild(j); @@ -553,8 +541,7 @@ public final class YangModelBuilderUtil { * range context to parse * @return List of RangeConstraints parsed from this context */ - private static List parseRangeConstraints( - final Range_stmtContext ctx) { + private static List parseRangeConstraints(final Range_stmtContext ctx) { final int line = ctx.getStart().getLine(); List rangeConstraints = new ArrayList(); String description = null; @@ -577,14 +564,12 @@ public final class YangModelBuilderUtil { Number min; Number max; if (splittedRangeDef.length == 1) { - min = max = parseNumberConstraintValue(splittedRangeDef[0], - line); + min = max = parseNumberConstraintValue(splittedRangeDef[0], line); } else { min = parseNumberConstraintValue(splittedRangeDef[0], line); max = parseNumberConstraintValue(splittedRangeDef[1], line); } - RangeConstraint range = BaseConstraints.rangeConstraint(min, max, - description, reference); + RangeConstraint range = BaseConstraints.rangeConstraint(min, max, description, reference); rangeConstraints.add(range); } @@ -598,8 +583,7 @@ public final class YangModelBuilderUtil { * type body context to parse * @return List of LengthConstraint created from this context */ - private static List getLengthConstraints( - final Type_body_stmtsContext ctx) { + private static List getLengthConstraints(final Type_body_stmtsContext ctx) { List lengthConstraints = Collections.emptyList(); outer: for (int j = 0; j < ctx.getChildCount(); j++) { ParseTree stringRestrChild = ctx.getChild(j); @@ -623,8 +607,7 @@ public final class YangModelBuilderUtil { * length context to parse * @return List of LengthConstraints parsed from this context */ - private static List parseLengthConstraints( - final Length_stmtContext ctx) { + private static List parseLengthConstraints(final Length_stmtContext ctx) { final int line = ctx.getStart().getLine(); List lengthConstraints = new ArrayList(); String description = null; @@ -647,14 +630,12 @@ public final class YangModelBuilderUtil { Number min; Number max; if (splittedRangeDef.length == 1) { - min = max = parseNumberConstraintValue(splittedRangeDef[0], - line); + min = max = parseNumberConstraintValue(splittedRangeDef[0], line); } else { min = parseNumberConstraintValue(splittedRangeDef[0], line); max = parseNumberConstraintValue(splittedRangeDef[1], line); } - LengthConstraint range = BaseConstraints.lengthConstraint(min, max, - description, reference); + LengthConstraint range = BaseConstraints.lengthConstraint(min, max, description, reference); lengthConstraints.add(range); } @@ -667,8 +648,7 @@ public final class YangModelBuilderUtil { * @return wrapper object of primitive java type or UnknownBoundaryNumber if * type is one of special YANG values 'min' or 'max' */ - private static Number parseNumberConstraintValue(final String value, - final int line) { + private static Number parseNumberConstraintValue(final String value, final int line) { Number result = null; if ("min".equals(value) || "max".equals(value)) { result = new UnknownBoundaryNumber(value); @@ -676,8 +656,7 @@ public final class YangModelBuilderUtil { try { result = Long.valueOf(value); } catch (NumberFormatException e) { - throw new YangParseException(line, - "Unable to parse range value '" + value + "'.", e); + throw new YangParseException(line, "Unable to parse range value '" + value + "'.", e); } } return result; @@ -690,20 +669,16 @@ public final class YangModelBuilderUtil { * type body * @return list of pattern constraints */ - private static List getPatternConstraint( - final Type_body_stmtsContext ctx) { + private static List getPatternConstraint(final Type_body_stmtsContext ctx) { List patterns = new ArrayList(); - outer: for (int j = 0; j < ctx.getChildCount(); j++) { + for (int j = 0; j < ctx.getChildCount(); j++) { ParseTree stringRestrChild = ctx.getChild(j); if (stringRestrChild instanceof String_restrictionsContext) { for (int k = 0; k < stringRestrChild.getChildCount(); k++) { ParseTree lengthChild = stringRestrChild.getChild(k); if (lengthChild instanceof Pattern_stmtContext) { patterns.add(parsePatternConstraint((Pattern_stmtContext) lengthChild)); - if (k == lengthChild.getChildCount() - 1) { - break outer; - } } } } @@ -718,8 +693,7 @@ public final class YangModelBuilderUtil { * pattern context * @return PatternConstraint object */ - private static PatternConstraint parsePatternConstraint( - final Pattern_stmtContext ctx) { + private static PatternConstraint parsePatternConstraint(final Pattern_stmtContext ctx) { String description = null; String reference = null; for (int i = 0; i < ctx.getChildCount(); i++) { @@ -731,8 +705,7 @@ public final class YangModelBuilderUtil { } } String pattern = patternStringFromNode(ctx); - return BaseConstraints.patternConstraint(pattern, description, - reference); + return BaseConstraints.patternConstraint(pattern, description, reference); } /** @@ -750,8 +723,7 @@ public final class YangModelBuilderUtil { for (int j = 0; j < child.getChildCount(); j++) { if (j % 2 == 0) { String patternToken = child.getChild(j).getText(); - result.append(patternToken.substring(1, - patternToken.length() - 1)); + result.append(patternToken.substring(1, patternToken.length() - 1)); } } } @@ -785,8 +757,7 @@ public final class YangModelBuilderUtil { * decimal64 context * @return fraction-digits value as Integer */ - private static Integer parseFractionDigits( - Decimal64_specificationContext ctx) { + private static Integer parseFractionDigits(Decimal64_specificationContext ctx) { Integer result = null; for (int k = 0; k < ctx.getChildCount(); k++) { ParseTree fdChild = ctx.getChild(k); @@ -795,9 +766,8 @@ public final class YangModelBuilderUtil { try { result = Integer.valueOf(value); } catch (NumberFormatException e) { - throw new YangParseException(ctx.getStart().getLine(), - "Unable to parse fraction digits value '" + value - + "'.", e); + throw new YangParseException(ctx.getStart().getLine(), "Unable to parse fraction digits value '" + + value + "'.", e); } } } @@ -817,9 +787,8 @@ public final class YangModelBuilderUtil { * @param prefix * @return List of Bit objects created from this context */ - private static List getBits( - Type_body_stmtsContext ctx, List actualPath, URI namespace, - Date revision, String prefix) { + private static List getBits(Type_body_stmtsContext ctx, List actualPath, + URI namespace, Date revision, String prefix) { final List bits = new ArrayList(); for (int j = 0; j < ctx.getChildCount(); j++) { ParseTree bitsSpecChild = ctx.getChild(j); @@ -828,8 +797,7 @@ public final class YangModelBuilderUtil { for (int k = 0; k < bitsSpecChild.getChildCount(); k++) { ParseTree bitChild = bitsSpecChild.getChild(k); if (bitChild instanceof Bit_stmtContext) { - Bit bit = parseBit((Bit_stmtContext) bitChild, - highestPosition, actualPath, namespace, + Bit bit = parseBit((Bit_stmtContext) bitChild, highestPosition, actualPath, namespace, revision, prefix); if (bit.getPosition() > highestPosition) { highestPosition = bit.getPosition(); @@ -856,9 +824,8 @@ public final class YangModelBuilderUtil { * @param prefix * @return Bit object parsed from this context */ - private static BitsTypeDefinition.Bit parseBit(final Bit_stmtContext ctx, - long highestPosition, List actualPath, final URI namespace, - final Date revision, final String prefix) { + private static BitsTypeDefinition.Bit parseBit(final Bit_stmtContext ctx, long highestPosition, + List actualPath, final URI namespace, final Date revision, final String prefix) { String name = stringFromNode(ctx); final QName qname = new QName(namespace, revision, prefix, name); Long position = null; @@ -871,8 +838,7 @@ public final class YangModelBuilderUtil { bitPath.addAll(actualPath); bitPath.add(name); - SchemaPath schemaPath = createActualSchemaPath(bitPath, namespace, - revision, prefix); + SchemaPath schemaPath = createActualSchemaPath(bitPath, namespace, revision, prefix); for (int i = 0; i < ctx.getChildCount(); i++) { ParseTree child = ctx.getChild(i); @@ -892,16 +858,12 @@ public final class YangModelBuilderUtil { position = highestPosition + 1; } if (position < 0 || position > 4294967295L) { - throw new YangParseException( - ctx.getStart().getLine(), - "Error on bit '" - + name - + "': the position value MUST be in the range 0 to 4294967295"); + throw new YangParseException(ctx.getStart().getLine(), "Error on bit '" + name + + "': the position value MUST be in the range 0 to 4294967295"); } final List unknownNodes = Collections.emptyList(); - return new BitImpl(position, qname, schemaPath, description, reference, - status, unknownNodes); + return new BitImpl(position, qname, schemaPath, description, reference, status, unknownNodes); } /** @@ -963,20 +925,47 @@ public final class YangModelBuilderUtil { * type body * @return UnknownType object with constraints from parsed type body */ - public static TypeDefinition parseUnknownTypeBody(QName typedefQName, - Type_body_stmtsContext ctx) { + public static TypeDefinition parseUnknownTypeBody(QName typedefQName, Type_body_stmtsContext ctx, + final List actualPath, final URI namespace, final Date revision, final String prefix, + Builder parent, ModuleBuilder moduleBuilder) { + String typeName = typedefQName.getLocalName(); + UnknownType.Builder unknownType = new UnknownType.Builder(typedefQName); + if (ctx != null) { List rangeStatements = getRangeConstraints(ctx); List lengthStatements = getLengthConstraints(ctx); List patternStatements = getPatternConstraint(ctx); Integer fractionDigits = getFractionDigits(ctx); - unknownType.rangeStatements(rangeStatements); - unknownType.lengthStatements(lengthStatements); - unknownType.patterns(patternStatements); - unknownType.fractionDigits(fractionDigits); + 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(); + 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); + + typeBuilder.ranges(rangeStatements); + typeBuilder.lengths(lengthStatements); + typeBuilder.patterns(patternStatements); + typeBuilder.fractionDigits(fractionDigits); + + result = typeBuilder.build(); + + return result; + } } + return unknownType.build(); } @@ -997,83 +986,103 @@ public final class YangModelBuilderUtil { * current prefix * @return TypeDefinition object based on parsed values. */ - public static TypeDefinition parseTypeBody(final String typeName, - final Type_body_stmtsContext typeBody, - final List actualPath, final URI namespace, - final Date revision, final String prefix) { + public static TypeDefinition parseTypeBody(final String moduleName, final String typeName, + final Type_body_stmtsContext typeBody, final List actualPath, final URI namespace, + final Date revision, final String prefix, Builder parent) { TypeDefinition baseType = null; List rangeStatements = getRangeConstraints(typeBody); Integer fractionDigits = getFractionDigits(typeBody); List lengthStatements = getLengthConstraints(typeBody); List patternStatements = getPatternConstraint(typeBody); - List enumConstants = getEnumConstants( - typeBody, actualPath, namespace, revision, prefix); + List enumConstants = getEnumConstants(typeBody, actualPath, namespace, revision, + prefix); + + TypeConstraints constraints = new TypeConstraints(moduleName, typeBody.getStart().getLine()); + constraints.addFractionDigits(fractionDigits); + constraints.addLengths(lengthStatements); + constraints.addPatterns(patternStatements); + constraints.addRanges(rangeStatements); - SchemaPath baseTypePathFinal = createTypeSchemaPath(actualPath, - namespace, revision, prefix, typeName, true, true); - SchemaPath baseTypePath = createTypeSchemaPath(actualPath, - namespace, revision, prefix, typeName, true, false); + SchemaPath baseTypePathFinal = createTypeSchemaPath(actualPath, namespace, revision, prefix, typeName, true, + true); + SchemaPath baseTypePath = createTypeSchemaPath(actualPath, namespace, revision, prefix, typeName, true, false); if ("decimal64".equals(typeName)) { if (rangeStatements.isEmpty()) { return new Decimal64(baseTypePathFinal, fractionDigits); } - baseType = new Decimal64(baseTypePath, fractionDigits); + Decimal64 decimalType = new Decimal64(baseTypePath, fractionDigits); + constraints.addRanges(decimalType.getRangeStatements()); + baseType = decimalType; } else if (typeName.startsWith("int")) { + IntegerTypeDefinition intType = null; if ("int8".equals(typeName)) { - baseType = new Int8(baseTypePath); + intType = new Int8(baseTypePath); } else if ("int16".equals(typeName)) { - baseType = new Int16(baseTypePath); + intType = new Int16(baseTypePath); } else if ("int32".equals(typeName)) { - baseType = new Int32(baseTypePath); + intType = new Int32(baseTypePath); } else if ("int64".equals(typeName)) { - baseType = new Int64(baseTypePath); + intType = new Int64(baseTypePath); } + constraints.addRanges(intType.getRangeStatements()); + baseType = intType; } else if (typeName.startsWith("uint")) { + UnsignedIntegerTypeDefinition uintType = null; if ("uint8".equals(typeName)) { - baseType = new Uint8(baseTypePath); + uintType = new Uint8(baseTypePath); } else if ("uint16".equals(typeName)) { - baseType = new Uint16(baseTypePath); + uintType = new Uint16(baseTypePath); } else if ("uint32".equals(typeName)) { - baseType = new Uint32(baseTypePath); + uintType = new Uint32(baseTypePath); } else if ("uint64".equals(typeName)) { - baseType = new Uint64(baseTypePath); + uintType = new Uint64(baseTypePath); } + constraints.addRanges(uintType.getRangeStatements()); + baseType = uintType; } else if ("enumeration".equals(typeName)) { return new EnumerationType(baseTypePathFinal, enumConstants); } else if ("string".equals(typeName)) { - baseType = new StringType(baseTypePath); + StringTypeDefinition stringType = new StringType(baseTypePath); + constraints.addLengths(stringType.getLengthStatements()); + baseType = stringType; } else if ("bits".equals(typeName)) { - return new BitsType(baseTypePathFinal, getBits(typeBody, - actualPath, namespace, revision, prefix)); + return new BitsType(baseTypePathFinal, getBits(typeBody, actualPath, namespace, revision, prefix)); } else if ("leafref".equals(typeName)) { final String path = parseLeafrefPath(typeBody); final boolean absolute = path.startsWith("/"); - RevisionAwareXPath xpath = new RevisionAwareXPathImpl(path, - absolute); + RevisionAwareXPath xpath = new RevisionAwareXPathImpl(path, absolute); return new Leafref(baseTypePathFinal, xpath); } else if ("binary".equals(typeName)) { - baseType = new BinaryType(baseTypePath); + BinaryTypeDefinition binaryType = new BinaryType(baseTypePath); + constraints.addLengths(binaryType.getLengthConstraints()); + baseType = binaryType; } else if ("instance-identifier".equals(typeName)) { boolean requireInstance = isRequireInstance(typeBody); - baseType = new InstanceIdentifier(baseTypePath, null, - requireInstance); + baseType = new InstanceIdentifier(baseTypePath, null, requireInstance); + } + + if (parent instanceof TypeDefinitionBuilder && !(parent instanceof UnionTypeBuilder)) { + TypeDefinitionBuilder typedef = (TypeDefinitionBuilder) parent; + typedef.setRanges(constraints.getRange()); + typedef.setLengths(constraints.getLength()); + typedef.setPatterns(constraints.getPatterns()); + typedef.setFractionDigits(constraints.getFractionDigits()); + return baseType; } 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); + SchemaPath schemaPath = createTypeSchemaPath(actualPath, namespace, revision, prefix, typeName, false, false); + typeBuilder = new ExtendedType.Builder(qname, baseType, "", "", schemaPath); - typeBuilder.ranges(rangeStatements); - typeBuilder.lengths(lengthStatements); - typeBuilder.patterns(patternStatements); - typeBuilder.fractionDigits(fractionDigits); + typeBuilder.ranges(constraints.getRange()); + typeBuilder.lengths(constraints.getLength()); + typeBuilder.patterns(constraints.getPatterns()); + typeBuilder.fractionDigits(constraints.getFractionDigits()); result = typeBuilder.build(); return result; @@ -1095,10 +1104,9 @@ public final class YangModelBuilderUtil { * if this is base yang type without restrictions * @return SchemaPath object. */ - private static SchemaPath createTypeSchemaPath( - final List actualPath, final URI namespace, - final Date revision, final String prefix, final String typeName, - final boolean isBaseYangType, final boolean isBaseYangTypeFinal) { + private static SchemaPath createTypeSchemaPath(final List actualPath, final URI namespace, + final Date revision, final String prefix, final String typeName, final boolean isBaseYangType, + final boolean isBaseYangTypeFinal) { List typePath = new ArrayList(actualPath); if (isBaseYangType && !isBaseYangTypeFinal) { typePath.add(typeName); @@ -1214,8 +1222,7 @@ public final class YangModelBuilderUtil { for (int j = 0; j < context.getChildCount(); j++) { String mustPart = context.getChild(j).getText(); if (j == 0) { - mustText.append(mustPart.substring(0, - mustPart.length() - 1)); + mustText.append(mustPart.substring(0, mustPart.length() - 1)); continue; } if (j % 2 == 0) { @@ -1234,8 +1241,8 @@ public final class YangModelBuilderUtil { } } - MustDefinition must = new MustDefinitionImpl(mustText.toString(), - description, reference, errorAppTag, errorMessage); + MustDefinition must = new MustDefinitionImpl(mustText.toString(), description, reference, errorAppTag, + errorMessage); return must; } @@ -1247,8 +1254,7 @@ public final class YangModelBuilderUtil { * @param constraints * ConstraintsBuilder to fill */ - public static void parseConstraints(final ParseTree ctx, - final ConstraintsBuilder constraints) { + public static void parseConstraints(final ParseTree ctx, final ConstraintsBuilder constraints) { for (int i = 0; i < ctx.getChildCount(); ++i) { final ParseTree childNode = ctx.getChild(i); if (childNode instanceof Max_elements_stmtContext) { @@ -1264,8 +1270,7 @@ public final class YangModelBuilderUtil { for (int j = 0; j < childNode.getChildCount(); j++) { ParseTree mandatoryTree = ctx.getChild(j); if (mandatoryTree instanceof Mandatory_argContext) { - Boolean mandatory = Boolean - .valueOf(stringFromNode(mandatoryTree)); + Boolean mandatory = Boolean.valueOf(stringFromNode(mandatoryTree)); constraints.setMandatory(mandatory); } } @@ -1289,8 +1294,7 @@ public final class YangModelBuilderUtil { } return result; } catch (Exception e) { - throw new YangParseException(ctx.getStart().getLine(), - "Failed to parse min-elements.", e); + throw new YangParseException(ctx.getStart().getLine(), "Failed to parse min-elements.", e); } } @@ -1308,8 +1312,7 @@ public final class YangModelBuilderUtil { } return result; } catch (Exception e) { - throw new YangParseException(ctx.getStart().getLine(), - "Failed to parse max-elements.", e); + throw new YangParseException(ctx.getStart().getLine(), "Failed to parse max-elements.", e); } } @@ -1353,8 +1356,8 @@ public final class YangModelBuilderUtil { * if this is one of YANG type which MUST contain additional * informations in its body */ - public static void checkMissingBody(final String typeName, - final String moduleName, final int line) throws YangParseException { + public static void checkMissingBody(final String typeName, final String moduleName, final int line) + throws YangParseException { if ("decimal64".equals(typeName)) { throw new YangParseException(moduleName, line, "The 'fraction-digits' statement MUST be present if the type is 'decimal64'."); @@ -1365,8 +1368,7 @@ public final class YangModelBuilderUtil { throw new YangParseException(moduleName, line, "The 'path' statement MUST be present if the type is 'leafref'."); } else if ("bits".equals(typeName)) { - throw new YangParseException(moduleName, line, - "The 'bit' statement MUST be present if the type is 'bits'."); + throw new YangParseException(moduleName, line, "The 'bit' statement MUST be present if the type is 'bits'."); } else if ("enumeration".equals(typeName)) { throw new YangParseException(moduleName, line, "The 'enum' statement MUST be present if the type is 'enumeration'."); @@ -1384,8 +1386,7 @@ public final class YangModelBuilderUtil { */ public static RefineHolder parseRefine(Refine_stmtContext refineCtx) { final String refineTarget = stringFromNode(refineCtx); - final RefineHolder refine = new RefineHolder(refineTarget, refineCtx - .getStart().getLine()); + final RefineHolder refine = new RefineHolder(refineTarget, refineCtx.getStart().getLine()); for (int j = 0; j < refineCtx.getChildCount(); j++) { ParseTree refinePom = refineCtx.getChild(j); if (refinePom instanceof Refine_pomContext) { @@ -1394,23 +1395,17 @@ public final class YangModelBuilderUtil { parseRefineDefault(refine, refineStmt); if (refineStmt instanceof Refine_leaf_stmtsContext) { - parseRefine(refine, - (Refine_leaf_stmtsContext) refineStmt); + parseRefine(refine, (Refine_leaf_stmtsContext) refineStmt); } else if (refineStmt instanceof Refine_container_stmtsContext) { - parseRefine(refine, - (Refine_container_stmtsContext) refineStmt); + parseRefine(refine, (Refine_container_stmtsContext) refineStmt); } else if (refineStmt instanceof Refine_list_stmtsContext) { - parseRefine(refine, - (Refine_list_stmtsContext) refineStmt); + parseRefine(refine, (Refine_list_stmtsContext) refineStmt); } else if (refineStmt instanceof Refine_leaf_list_stmtsContext) { - parseRefine(refine, - (Refine_leaf_list_stmtsContext) refineStmt); + parseRefine(refine, (Refine_leaf_list_stmtsContext) refineStmt); } else if (refineStmt instanceof Refine_choice_stmtsContext) { - parseRefine(refine, - (Refine_choice_stmtsContext) refineStmt); + parseRefine(refine, (Refine_choice_stmtsContext) refineStmt); } else if (refineStmt instanceof Refine_anyxml_stmtsContext) { - parseRefine(refine, - (Refine_anyxml_stmtsContext) refineStmt); + parseRefine(refine, (Refine_anyxml_stmtsContext) refineStmt); } } } @@ -1418,8 +1413,7 @@ public final class YangModelBuilderUtil { return refine; } - private static void parseRefineDefault(RefineHolder refine, - ParseTree refineStmt) { + private static void parseRefineDefault(RefineHolder refine, ParseTree refineStmt) { for (int i = 0; i < refineStmt.getChildCount(); i++) { ParseTree refineArg = refineStmt.getChild(i); if (refineArg instanceof Description_stmtContext) { @@ -1435,8 +1429,7 @@ public final class YangModelBuilderUtil { } } - private static RefineHolder parseRefine(RefineHolder refine, - Refine_leaf_stmtsContext refineStmt) { + private static RefineHolder parseRefine(RefineHolder refine, Refine_leaf_stmtsContext refineStmt) { for (int i = 0; i < refineStmt.getChildCount(); i++) { ParseTree refineArg = refineStmt.getChild(i); if (refineArg instanceof Default_stmtContext) { @@ -1446,8 +1439,7 @@ public final class YangModelBuilderUtil { for (int j = 0; j < refineArg.getChildCount(); j++) { ParseTree mandatoryTree = refineArg.getChild(j); if (mandatoryTree instanceof Mandatory_argContext) { - Boolean mandatory = Boolean - .valueOf(stringFromNode(mandatoryTree)); + Boolean mandatory = Boolean.valueOf(stringFromNode(mandatoryTree)); refine.setMandatory(mandatory); } } @@ -1460,8 +1452,7 @@ public final class YangModelBuilderUtil { return refine; } - private static RefineHolder parseRefine(RefineHolder refine, - Refine_container_stmtsContext refineStmt) { + private static RefineHolder parseRefine(RefineHolder refine, Refine_container_stmtsContext refineStmt) { for (int m = 0; m < refineStmt.getChildCount(); m++) { ParseTree refineArg = refineStmt.getChild(m); if (refineArg instanceof Must_stmtContext) { @@ -1474,8 +1465,7 @@ public final class YangModelBuilderUtil { return refine; } - private static RefineHolder parseRefine(RefineHolder refine, - Refine_list_stmtsContext refineStmt) { + private static RefineHolder parseRefine(RefineHolder refine, Refine_list_stmtsContext refineStmt) { for (int m = 0; m < refineStmt.getChildCount(); m++) { ParseTree refineArg = refineStmt.getChild(m); if (refineArg instanceof Must_stmtContext) { @@ -1492,8 +1482,7 @@ public final class YangModelBuilderUtil { return refine; } - private static RefineHolder parseRefine(RefineHolder refine, - Refine_leaf_list_stmtsContext refineStmt) { + private static RefineHolder parseRefine(RefineHolder refine, Refine_leaf_list_stmtsContext refineStmt) { for (int m = 0; m < refineStmt.getChildCount(); m++) { ParseTree refineArg = refineStmt.getChild(m); if (refineArg instanceof Must_stmtContext) { @@ -1510,8 +1499,7 @@ public final class YangModelBuilderUtil { return refine; } - private static RefineHolder parseRefine(RefineHolder refine, - Refine_choice_stmtsContext refineStmt) { + private static RefineHolder parseRefine(RefineHolder refine, Refine_choice_stmtsContext refineStmt) { for (int i = 0; i < refineStmt.getChildCount(); i++) { ParseTree refineArg = refineStmt.getChild(i); if (refineArg instanceof Default_stmtContext) { @@ -1521,8 +1509,7 @@ public final class YangModelBuilderUtil { for (int j = 0; j < refineArg.getChildCount(); j++) { ParseTree mandatoryTree = refineArg.getChild(j); if (mandatoryTree instanceof Mandatory_argContext) { - Boolean mandatory = Boolean - .valueOf(stringFromNode(mandatoryTree)); + Boolean mandatory = Boolean.valueOf(stringFromNode(mandatoryTree)); refine.setMandatory(mandatory); } } @@ -1531,8 +1518,7 @@ public final class YangModelBuilderUtil { return refine; } - private static RefineHolder parseRefine(RefineHolder refine, - Refine_anyxml_stmtsContext refineStmt) { + private static RefineHolder parseRefine(RefineHolder refine, Refine_anyxml_stmtsContext refineStmt) { for (int i = 0; i < refineStmt.getChildCount(); i++) { ParseTree refineArg = refineStmt.getChild(i); if (refineArg instanceof Must_stmtContext) { @@ -1542,8 +1528,7 @@ public final class YangModelBuilderUtil { for (int j = 0; j < refineArg.getChildCount(); j++) { ParseTree mandatoryTree = refineArg.getChild(j); if (mandatoryTree instanceof Mandatory_argContext) { - Boolean mandatory = Boolean - .valueOf(stringFromNode(mandatoryTree)); + Boolean mandatory = Boolean.valueOf(stringFromNode(mandatoryTree)); refine.setMandatory(mandatory); } }