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=1e55b2444919fd67f5f46dd2114e8610e3d9e07f;hb=ff1b4a79cca00743a00c3b0b1100bd0ab2b2fb31;hp=873e3842db0097cb17ec4d1fb6dbd5edd039451b;hpb=d0f1a6162437a9e003e87c7a8b36ea875f8de984;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 873e3842db..1e55b24449 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,18 +76,24 @@ 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; import org.opendaylight.controller.yang.model.util.BitsType; import org.opendaylight.controller.yang.model.util.Decimal64; import org.opendaylight.controller.yang.model.util.EnumerationType; +import org.opendaylight.controller.yang.model.util.ExtendedType; import org.opendaylight.controller.yang.model.util.InstanceIdentifier; import org.opendaylight.controller.yang.model.util.Int16; import org.opendaylight.controller.yang.model.util.Int32; @@ -101,8 +107,12 @@ 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; @@ -962,19 +972,48 @@ public final class YangModelBuilderUtil { * @return UnknownType object with constraints from parsed type body */ public static TypeDefinition parseUnknownTypeBody(QName typedefQName, - Type_body_stmtsContext ctx) { + 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(); } @@ -998,8 +1037,8 @@ public final class YangModelBuilderUtil { public static TypeDefinition parseTypeBody(final String typeName, final Type_body_stmtsContext typeBody, final List actualPath, final URI namespace, - final Date revision, final String prefix) { - TypeDefinition type = null; + final Date revision, final String prefix, Builder parent) { + TypeDefinition baseType = null; List rangeStatements = getRangeConstraints(typeBody); Integer fractionDigits = getFractionDigits(typeBody); @@ -1008,62 +1047,143 @@ public final class YangModelBuilderUtil { List enumConstants = getEnumConstants( typeBody, actualPath, namespace, revision, prefix); + TypeConstraints constraints = new TypeConstraints(); + 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); + if ("decimal64".equals(typeName)) { - type = new Decimal64(actualPath, namespace, revision, - fractionDigits); + if (rangeStatements.isEmpty()) { + return new Decimal64(baseTypePathFinal, 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)) { - type = new Int8(actualPath, namespace, revision, - rangeStatements, null, null); + intType = new Int8(baseTypePath); } else if ("int16".equals(typeName)) { - type = new Int16(actualPath, namespace, revision, - rangeStatements, null, null); + intType = new Int16(baseTypePath); } else if ("int32".equals(typeName)) { - type = new Int32(actualPath, namespace, revision, - rangeStatements, null, null); + intType = new Int32(baseTypePath); } else if ("int64".equals(typeName)) { - type = new Int64(actualPath, namespace, revision, - rangeStatements, null, null); + intType = new Int64(baseTypePath); } + constraints.addRanges(intType.getRangeStatements()); + baseType = intType; } else if (typeName.startsWith("uint")) { + UnsignedIntegerTypeDefinition uintType = null; if ("uint8".equals(typeName)) { - type = new Uint8(actualPath, namespace, revision, - rangeStatements, null, null); + uintType = new Uint8(baseTypePath); } else if ("uint16".equals(typeName)) { - type = new Uint16(actualPath, namespace, revision, - rangeStatements, null, null); + uintType = new Uint16(baseTypePath); } else if ("uint32".equals(typeName)) { - type = new Uint32(actualPath, namespace, revision, - rangeStatements, null, null); + uintType = new Uint32(baseTypePath); } else if ("uint64".equals(typeName)) { - type = new Uint64(actualPath, namespace, revision, - rangeStatements, null, null); + uintType = new Uint64(baseTypePath); } + constraints.addRanges(uintType.getRangeStatements()); + baseType = uintType; } else if ("enumeration".equals(typeName)) { - type = new EnumerationType(actualPath, namespace, revision, - enumConstants); + return new EnumerationType(baseTypePathFinal, enumConstants); } else if ("string".equals(typeName)) { - type = new StringType(actualPath, namespace, revision, - lengthStatements, patternStatements); + StringTypeDefinition stringType = new StringType(baseTypePath); + constraints.addLengths(stringType.getLengthStatements()); + baseType = stringType; } else if ("bits".equals(typeName)) { - type = new BitsType(actualPath, namespace, revision, 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); - type = new Leafref(actualPath, namespace, revision, xpath); + return new Leafref(baseTypePathFinal, xpath); } else if ("binary".equals(typeName)) { - List bytes = Collections.emptyList(); - type = new BinaryType(actualPath, namespace, revision, bytes, - lengthStatements, null); + BinaryTypeDefinition binaryType = new BinaryType(baseTypePath); + constraints.addLengths(binaryType.getLengthConstraints()); + baseType = binaryType; } else if ("instance-identifier".equals(typeName)) { boolean requireInstance = isRequireInstance(typeBody); - type = new InstanceIdentifier(actualPath, namespace, revision, - 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); + + typeBuilder.ranges(constraints.getRange()); + typeBuilder.lengths(constraints.getLength()); + typeBuilder.patterns(constraints.getPatterns()); + typeBuilder.fractionDigits(constraints.getFractionDigits()); + + result = typeBuilder.build(); + return result; + } + + /** + * Create SchemaPath object from given path list with namespace, revision + * and prefix based on given values. + * + * @param actualPath + * current position in model + * @param namespace + * @param revision + * @param prefix + * @param typeName + * @param isBaseYangType + * if this is base yang type + * @param isBaseYangTypeFinal + * 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) { + List typePath = new ArrayList(actualPath); + if (isBaseYangType && !isBaseYangTypeFinal) { + typePath.add(typeName); } - return type; + + final List path = new ArrayList(); + QName qname; + // start from index 1 -> module name omited + for (int i = 1; i < typePath.size(); i++) { + qname = new QName(namespace, revision, prefix, typePath.get(i)); + path.add(qname); + } + QName typeQName; + if (isBaseYangType) { + typeQName = new QName(BaseTypes.BaseTypesNamespace, typeName); + } else { + typeQName = new QName(namespace, revision, prefix, typeName); + } + path.add(typeQName); + return new SchemaPath(path, true); } /**