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;
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;
* @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<String> 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<RangeConstraint> rangeStatements = getRangeConstraints(ctx);
List<LengthConstraint> lengthStatements = getLengthConstraints(ctx);
List<PatternConstraint> 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();
}
public static TypeDefinition<?> parseTypeBody(final String typeName,
final Type_body_stmtsContext typeBody,
final List<String> 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<RangeConstraint> rangeStatements = getRangeConstraints(typeBody);
Integer fractionDigits = getFractionDigits(typeBody);
List<EnumTypeDefinition.EnumPair> enumConstants = getEnumConstants(
typeBody, actualPath, namespace, revision, prefix);
- SchemaPath schemaPath = createActualSchemaPath(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(schemaPath, 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(schemaPath, rangeStatements, null, null);
+ intType = new Int8(baseTypePath);
} else if ("int16".equals(typeName)) {
- type = new Int16(schemaPath, rangeStatements, null, null);
+ intType = new Int16(baseTypePath);
} else if ("int32".equals(typeName)) {
- type = new Int32(schemaPath, rangeStatements, null, null);
+ intType = new Int32(baseTypePath);
} else if ("int64".equals(typeName)) {
- type = new Int64(schemaPath, 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(schemaPath, rangeStatements, null, null);
+ uintType = new Uint8(baseTypePath);
} else if ("uint16".equals(typeName)) {
- type = new Uint16(schemaPath, rangeStatements, null, null);
+ uintType = new Uint16(baseTypePath);
} else if ("uint32".equals(typeName)) {
- type = new Uint32(schemaPath, rangeStatements, null, null);
+ uintType = new Uint32(baseTypePath);
} else if ("uint64".equals(typeName)) {
- type = new Uint64(schemaPath, rangeStatements, null, null);
+ uintType = new Uint64(baseTypePath);
}
+ constraints.addRanges(uintType.getRangeStatements());
+ baseType = uintType;
} else if ("enumeration".equals(typeName)) {
- type = new EnumerationType(schemaPath, enumConstants);
+ return new EnumerationType(baseTypePathFinal, enumConstants);
} else if ("string".equals(typeName)) {
- type = new StringType(schemaPath, lengthStatements,
- patternStatements);
+ StringTypeDefinition stringType = new StringType(baseTypePath);
+ constraints.addLengths(stringType.getLengthStatements());
+ baseType = stringType;
} else if ("bits".equals(typeName)) {
- type = new BitsType(schemaPath, 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(schemaPath, xpath);
+ return new Leafref(baseTypePathFinal, xpath);
} else if ("binary".equals(typeName)) {
- List<Byte> bytes = Collections.emptyList();
- type = new BinaryType(schemaPath, 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(schemaPath, 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<String> actualPath, final URI namespace,
+ final Date revision, final String prefix, final String typeName,
+ final boolean isBaseYangType, final boolean isBaseYangTypeFinal) {
+ List<String> typePath = new ArrayList<String>(actualPath);
+ if (isBaseYangType && !isBaseYangTypeFinal) {
+ typePath.add(typeName);
}
- return type;
+
+ final List<QName> path = new ArrayList<QName>();
+ 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);
}
/**