*/
package org.opendaylight.yangtools.sal.binding.yang.types;
+import static org.opendaylight.mdsal.binding.generator.util.BindingGeneratorUtil.encodeAngleBrackets;
import static org.opendaylight.yangtools.yang.model.util.SchemaContextUtil.findDataSchemaNode;
import static org.opendaylight.yangtools.yang.model.util.SchemaContextUtil.findDataSchemaNodeForRelativeXPath;
import static org.opendaylight.yangtools.yang.model.util.SchemaContextUtil.findParentModule;
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
+import com.google.common.collect.ImmutableList;
import com.google.common.collect.Sets;
import com.google.common.io.BaseEncoding;
import java.io.Serializable;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.lang3.StringEscapeUtils;
-import org.opendaylight.yangtools.binding.generator.util.BindingGeneratorUtil;
-import org.opendaylight.yangtools.binding.generator.util.TypeConstants;
-import org.opendaylight.yangtools.binding.generator.util.Types;
-import org.opendaylight.yangtools.binding.generator.util.generated.type.builder.EnumerationBuilderImpl;
-import org.opendaylight.yangtools.binding.generator.util.generated.type.builder.GeneratedPropertyBuilderImpl;
-import org.opendaylight.yangtools.binding.generator.util.generated.type.builder.GeneratedTOBuilderImpl;
-import org.opendaylight.yangtools.sal.binding.generator.spi.TypeProvider;
-import org.opendaylight.yangtools.sal.binding.model.api.AccessModifier;
-import org.opendaylight.yangtools.sal.binding.model.api.ConcreteType;
-import org.opendaylight.yangtools.sal.binding.model.api.Enumeration;
-import org.opendaylight.yangtools.sal.binding.model.api.GeneratedProperty;
-import org.opendaylight.yangtools.sal.binding.model.api.GeneratedTransferObject;
-import org.opendaylight.yangtools.sal.binding.model.api.Restrictions;
-import org.opendaylight.yangtools.sal.binding.model.api.Type;
-import org.opendaylight.yangtools.sal.binding.model.api.type.builder.EnumBuilder;
-import org.opendaylight.yangtools.sal.binding.model.api.type.builder.GeneratedPropertyBuilder;
-import org.opendaylight.yangtools.sal.binding.model.api.type.builder.GeneratedTOBuilder;
-import org.opendaylight.yangtools.sal.binding.model.api.type.builder.GeneratedTypeBuilderBase;
-import org.opendaylight.yangtools.sal.binding.model.api.type.builder.MethodSignatureBuilder;
+import org.opendaylight.mdsal.binding.generator.spi.TypeProvider;
+import org.opendaylight.mdsal.binding.generator.util.BindingGeneratorUtil;
+import org.opendaylight.mdsal.binding.generator.util.TypeConstants;
+import org.opendaylight.mdsal.binding.generator.util.Types;
+import org.opendaylight.mdsal.binding.generator.util.generated.type.builder.EnumerationBuilderImpl;
+import org.opendaylight.mdsal.binding.generator.util.generated.type.builder.GeneratedPropertyBuilderImpl;
+import org.opendaylight.mdsal.binding.generator.util.generated.type.builder.GeneratedTOBuilderImpl;
+import org.opendaylight.mdsal.binding.model.api.AccessModifier;
+import org.opendaylight.mdsal.binding.model.api.ConcreteType;
+import org.opendaylight.mdsal.binding.model.api.Enumeration;
+import org.opendaylight.mdsal.binding.model.api.GeneratedProperty;
+import org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject;
+import org.opendaylight.mdsal.binding.model.api.Restrictions;
+import org.opendaylight.mdsal.binding.model.api.Type;
+import org.opendaylight.mdsal.binding.model.api.type.builder.EnumBuilder;
+import org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedPropertyBuilder;
+import org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedTOBuilder;
+import org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedTypeBuilderBase;
+import org.opendaylight.mdsal.binding.model.api.type.builder.MethodSignatureBuilder;
import org.opendaylight.yangtools.yang.binding.BindingMapping;
import org.opendaylight.yangtools.yang.common.QName;
import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
import org.opendaylight.yangtools.yang.model.api.SchemaContext;
import org.opendaylight.yangtools.yang.model.api.SchemaNode;
import org.opendaylight.yangtools.yang.model.api.SchemaPath;
+import org.opendaylight.yangtools.yang.model.api.Status;
import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
import org.opendaylight.yangtools.yang.model.api.type.BinaryTypeDefinition;
import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint;
import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition;
import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
-import org.opendaylight.yangtools.yang.model.util.EnumerationType;
-import org.opendaylight.yangtools.yang.model.util.ExtendedType;
-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.Int8;
import org.opendaylight.yangtools.yang.model.util.RevisionAwareXPathImpl;
import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
-import org.opendaylight.yangtools.yang.model.util.StringType;
-import org.opendaylight.yangtools.yang.model.util.Uint16;
-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.UnionType;
+import org.opendaylight.yangtools.yang.model.util.type.BaseTypes;
+import org.opendaylight.yangtools.yang.model.util.type.CompatUtils;
import org.opendaylight.yangtools.yang.parser.util.YangValidationException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
public final class TypeProviderImpl implements TypeProvider {
+ private static final Logger LOG = LoggerFactory.getLogger(TypeProviderImpl.class);
private static final Pattern NUMBERS_PATTERN = Pattern.compile("[0-9]+\\z");
+ // Backwards compatibility: Union types used to be instantiated in YANG namespace, which is no longer
+ // the case, as unions are emitted to their correct schema path.
+ private static final SchemaPath UNION_PATH = SchemaPath.create(true,
+ org.opendaylight.yangtools.yang.model.util.BaseTypes.UNION_QNAME);
+
/**
* Contains the schema data red from YANG files.
*/
* </ul>
*/
@Override
- public Type javaTypeForSchemaDefinitionType(final TypeDefinition<?> typeDefinition, final SchemaNode parentNode,
- final Restrictions r) {
- Type returnType;
+ public Type javaTypeForSchemaDefinitionType(final TypeDefinition<?> typeDefinition, final SchemaNode parentNode, final Restrictions r) {
Preconditions.checkArgument(typeDefinition != null, "Type Definition cannot be NULL!");
Preconditions.checkArgument(typeDefinition.getQName() != null,
"Type Definition cannot have non specified QName (QName cannot be NULL!)");
String typedefName = typeDefinition.getQName().getLocalName();
Preconditions.checkArgument(typedefName != null, "Type Definitions Local Name cannot be NULL!");
- if (typeDefinition instanceof ExtendedType) {
- returnType = javaTypeForExtendedType(typeDefinition);
- if (r != null && returnType instanceof GeneratedTransferObject) {
- GeneratedTransferObject gto = (GeneratedTransferObject) returnType;
- Module module = findParentModule(schemaContext, parentNode);
- String basePackageName = BindingMapping.getRootPackageName(module.getQNameModule());
- String packageName = BindingGeneratorUtil.packageNameForGeneratedType(basePackageName,
- typeDefinition.getPath());
- String genTOName = BindingMapping.getClassName(typedefName);
- String name = packageName + "." + genTOName;
- if (!(returnType.getFullyQualifiedName().equals(name))) {
- returnType = shadedTOWithRestrictions(gto, r);
+ // Deal with base types
+ if (typeDefinition.getBaseType() == null) {
+ // We have to deal with differing handling of decimal64. The old parser used a fixed Decimal64 type
+ // and generated an enclosing ExtendedType to hold any range constraints. The new parser instantiates
+ // a base type which holds these constraints.
+ if (typeDefinition instanceof DecimalTypeDefinition) {
+ final Type ret = BaseYangTypes.BASE_YANG_TYPES_PROVIDER.javaTypeForSchemaDefinitionType(typeDefinition, parentNode, r);
+ if (ret != null) {
+ return ret;
}
}
- } else {
- returnType = javaTypeForLeafrefOrIdentityRef(typeDefinition, parentNode);
- if (returnType == null) {
- returnType = BaseYangTypes.BASE_YANG_TYPES_PROVIDER.javaTypeForYangType(typeDefinition.getQName()
- .getLocalName());
+
+ // Deal with leafrefs/identityrefs
+ Type ret = javaTypeForLeafrefOrIdentityRef(typeDefinition, parentNode);
+ if (ret != null) {
+ return ret;
+ }
+
+ // FIXME: it looks as though we could be using the same codepath as above...
+ ret = BaseYangTypes.BASE_YANG_TYPES_PROVIDER.javaTypeForYangType(typeDefinition.getQName().getLocalName());
+ if (ret == null) {
+ LOG.debug("Failed to resolve Java type for {}", typeDefinition);
+ }
+
+ return ret;
+ }
+
+ Type returnType = javaTypeForExtendedType(typeDefinition);
+ if (r != null && returnType instanceof GeneratedTransferObject) {
+ GeneratedTransferObject gto = (GeneratedTransferObject) returnType;
+ Module module = findParentModule(schemaContext, parentNode);
+ String basePackageName = BindingMapping.getRootPackageName(module.getQNameModule());
+ String packageName = BindingGeneratorUtil.packageNameForGeneratedType(basePackageName, typeDefinition.getPath());
+ String genTOName = BindingMapping.getClassName(typedefName);
+ String name = packageName + "." + genTOName;
+ if (!(returnType.getFullyQualifiedName().equals(name))) {
+ returnType = shadedTOWithRestrictions(gto, r);
}
}
return returnType;
* <li>if name of <code>typeDefinition</code></li>
* </ul>
*/
- public Type generatedTypeForExtendedDefinitionType(final TypeDefinition<?> typeDefinition,
- final SchemaNode parentNode) {
- Type returnType = null;
+ public Type generatedTypeForExtendedDefinitionType(final TypeDefinition<?> typeDefinition, final SchemaNode parentNode) {
Preconditions.checkArgument(typeDefinition != null, "Type Definition cannot be NULL!");
if (typeDefinition.getQName() == null) {
throw new IllegalArgumentException(
Preconditions.checkArgument(typeDefinition.getQName().getLocalName() != null,
"Type Definitions Local Name cannot be NULL!");
- final String typedefName = typeDefinition.getQName().getLocalName();
- if (typeDefinition instanceof ExtendedType) {
- final TypeDefinition<?> baseTypeDef = baseTypeDefForExtendedType(typeDefinition);
-
- if (!(baseTypeDef instanceof LeafrefTypeDefinition) && !(baseTypeDef instanceof IdentityrefTypeDefinition)) {
- final Module module = findParentModule(schemaContext, parentNode);
+ final TypeDefinition<?> baseTypeDef = baseTypeDefForExtendedType(typeDefinition);
+ if (!(baseTypeDef instanceof LeafrefTypeDefinition) && !(baseTypeDef instanceof IdentityrefTypeDefinition)) {
+ final Module module = findParentModule(schemaContext, parentNode);
- if (module != null) {
- final Map<Date, Map<String, Type>> modulesByDate = genTypeDefsContextMap.get(module.getName());
- final Map<String, Type> genTOs = modulesByDate.get(module.getRevision());
- if (genTOs != null) {
- returnType = genTOs.get(typedefName);
- }
+ if (module != null) {
+ final Map<Date, Map<String, Type>> modulesByDate = genTypeDefsContextMap.get(module.getName());
+ final Map<String, Type> genTOs = modulesByDate.get(module.getRevision());
+ if (genTOs != null) {
+ return genTOs.get(typeDefinition.getQName().getLocalName());
}
}
}
- return returnType;
+ return null;
}
/**
if (strXPath != null) {
if (strXPath.indexOf('[') == -1) {
final Module module = findParentModule(schemaContext, parentNode);
- if (module != null) {
- final SchemaNode dataNode;
- if (xpath.isAbsolute()) {
- dataNode = findDataSchemaNode(schemaContext, module, xpath);
- } else {
- dataNode = findDataSchemaNodeForRelativeXPath(schemaContext, module, parentNode, xpath);
- }
+ Preconditions.checkArgument(module != null, "Failed to find module for parent %s", parentNode);
- if (leafContainsEnumDefinition(dataNode)) {
- returnType = referencedTypes.get(dataNode.getPath());
- } else if (leafListContainsEnumDefinition(dataNode)) {
- returnType = Types.listTypeFor(referencedTypes.get(dataNode.getPath()));
- } else {
- returnType = resolveTypeFromDataSchemaNode(dataNode);
- }
+ final SchemaNode dataNode;
+ if (xpath.isAbsolute()) {
+ dataNode = findDataSchemaNode(schemaContext, module, xpath);
+ } else {
+ dataNode = findDataSchemaNodeForRelativeXPath(schemaContext, module, parentNode, xpath);
+ }
+ Preconditions.checkArgument(dataNode != null, "Failed to find leafref target: %s in module %s (%s)",
+ strXPath, this.getParentModule(parentNode).getName(), parentNode.getQName().getModule());
+
+ if (leafContainsEnumDefinition(dataNode)) {
+ returnType = referencedTypes.get(dataNode.getPath());
+ } else if (leafListContainsEnumDefinition(dataNode)) {
+ returnType = Types.listTypeFor(referencedTypes.get(dataNode.getPath()));
+ } else {
+ returnType = resolveTypeFromDataSchemaNode(dataNode);
}
} else {
returnType = Types.typeForClass(Object.class);
}
}
- if (returnType == null) {
- throw new IllegalArgumentException("Failed to find leafref target: " + strXPath);
- }
+ Preconditions.checkArgument(returnType != null, "Failed to find leafref target: %s in module %s (%s)",
+ strXPath, this.getParentModule(parentNode).getName(), parentNode.getQName().getModule(), this);
return returnType;
}
private static boolean leafContainsEnumDefinition(final SchemaNode dataNode) {
if (dataNode instanceof LeafSchemaNode) {
final LeafSchemaNode leaf = (LeafSchemaNode) dataNode;
- if (leaf.getType() instanceof EnumTypeDefinition) {
+ if (CompatUtils.compatLeafType(leaf) instanceof EnumTypeDefinition) {
return true;
}
}
/**
* Converts <code>enumTypeDef</code> to
- * {@link org.opendaylight.yangtools.sal.binding.model.api.Enumeration
+ * {@link Enumeration
* enumeration}.
*
* @param enumTypeDef
* <li>if name of <code>enumTypeDef</code> equal null</li>
* </ul>
*/
- private Enumeration provideTypeForEnum(final EnumTypeDefinition enumTypeDef, final String enumName,
- final SchemaNode parentNode) {
+ private Enumeration provideTypeForEnum(final EnumTypeDefinition enumTypeDef, final String enumName, final SchemaNode parentNode) {
Preconditions.checkArgument(enumTypeDef != null, "EnumTypeDefinition reference cannot be NULL!");
Preconditions.checkArgument(enumTypeDef.getValues() != null,
"EnumTypeDefinition MUST contain at least ONE value definition!");
final String basePackageName = BindingMapping.getRootPackageName(module.getQNameModule());
final EnumerationBuilderImpl enumBuilder = new EnumerationBuilderImpl(basePackageName, enumerationName);
- enumBuilder.setDescription(enumTypeDef.getDescription());
+ final String enumTypedefDescription = encodeAngleBrackets(enumTypeDef.getDescription());
+ enumBuilder.setDescription(enumTypedefDescription);
enumBuilder.setReference(enumTypeDef.getReference());
enumBuilder.setModuleName(module.getName());
enumBuilder.setSchemaPath(enumTypeDef.getPath().getPathFromRoot());
* </ul>
*
*/
- private static Enumeration addInnerEnumerationToTypeBuilder(final EnumTypeDefinition enumTypeDef, final String enumName,
- final GeneratedTypeBuilderBase<?> typeBuilder) {
+ private static Enumeration addInnerEnumerationToTypeBuilder(final EnumTypeDefinition enumTypeDef, final String enumName, final GeneratedTypeBuilderBase<?> typeBuilder) {
Preconditions.checkArgument(enumTypeDef != null, "EnumTypeDefinition reference cannot be NULL!");
Preconditions.checkArgument(enumTypeDef.getValues() != null,
"EnumTypeDefinition MUST contain at least ONE value definition!");
final String enumerationName = BindingMapping.getClassName(enumName);
final EnumBuilder enumBuilder = typeBuilder.addEnumeration(enumerationName);
- enumBuilder.setDescription(enumTypeDef.getDescription());
+ final String enumTypedefDescription = encodeAngleBrackets(enumTypeDef.getDescription());
+ enumBuilder.setDescription(enumTypedefDescription);
enumBuilder.updateEnumPairsFromEnumTypeDef(enumTypeDef);
return enumBuilder.toInstance(enumBuilder);
}
if (dataNode != null) {
if (dataNode instanceof LeafSchemaNode) {
final LeafSchemaNode leaf = (LeafSchemaNode) dataNode;
- returnType = javaTypeForSchemaDefinitionType(leaf.getType(), leaf);
+ final TypeDefinition<?> type = CompatUtils.compatLeafType(leaf);
+ returnType = javaTypeForSchemaDefinitionType(type, leaf);
} else if (dataNode instanceof LeafListSchemaNode) {
final LeafListSchemaNode leafList = (LeafListSchemaNode) dataNode;
returnType = javaTypeForSchemaDefinitionType(leafList.getType(), leafList);
* <code>modulName</code> or <code>typedef</code> or Q name of
* <code>typedef</code> equals <code>null</code>
*/
- private Type typedefToGeneratedType(final String basePackageName, final Module module,
- final TypeDefinition<?> typedef) {
+ private Type typedefToGeneratedType(final String basePackageName, final Module module, final TypeDefinition<?> typedef) {
final String moduleName = module.getName();
final Date moduleRevision = module.getRevision();
if ((basePackageName != null) && (moduleName != null) && (typedef != null) && (typedef.getQName() != null)) {
if (!(innerTypeDefinition instanceof LeafrefTypeDefinition)
&& !(innerTypeDefinition instanceof IdentityrefTypeDefinition)) {
Type returnType = null;
- if (innerTypeDefinition instanceof ExtendedType) {
- ExtendedType innerExtendedType = (ExtendedType) innerTypeDefinition;
- returnType = provideGeneratedTOFromExtendedType(typedef, innerExtendedType, basePackageName,
- module.getName());
+ if (innerTypeDefinition.getBaseType() != null) {
+ returnType = provideGeneratedTOFromExtendedType(typedef, innerTypeDefinition, basePackageName, module.getName());
} else if (innerTypeDefinition instanceof UnionTypeDefinition) {
final GeneratedTOBuilder genTOBuilder = provideGeneratedTOBuilderForUnionTypeDef(basePackageName,
(UnionTypeDefinition) innerTypeDefinition, typedefName, typedef);
makeSerializable((GeneratedTOBuilderImpl) genTOBuilder);
returnType = genTOBuilder.toInstance();
} else {
- final Type javaType = BaseYangTypes.BASE_YANG_TYPES_PROVIDER.javaTypeForSchemaDefinitionType(
- innerTypeDefinition, typedef);
+ final Type javaType = javaTypeForSchemaDefinitionType(innerTypeDefinition, typedef);
returnType = wrapJavaTypeIntoTO(basePackageName, typedef, javaType, module.getName());
}
if (returnType != null) {
* JAVA <code>Type</code> to which is <code>typedef</code> mapped
* @return generated transfer object which represent<code>javaType</code>
*/
- private GeneratedTransferObject wrapJavaTypeIntoTO(final String basePackageName, final TypeDefinition<?> typedef,
- final Type javaType, final String moduleName) {
+ private static GeneratedTransferObject wrapJavaTypeIntoTO(final String basePackageName, final TypeDefinition<?> typedef, final Type javaType, final String moduleName) {
Preconditions.checkNotNull(javaType, "javaType cannot be null");
final String propertyName = "value";
genTOBuilder.addEqualsIdentity(genPropBuilder);
genTOBuilder.addHashIdentity(genPropBuilder);
genTOBuilder.addToStringProperty(genPropBuilder);
- if (javaType instanceof ConcreteType && "String".equals(javaType.getName()) && typedef instanceof ExtendedType) {
- final List<String> regExps = resolveRegExpressionsFromTypedef((ExtendedType) typedef);
+ if (typedef.getStatus() == Status.DEPRECATED) {
+ genTOBuilder.addAnnotation("", "Deprecated");
+ }
+ if (javaType instanceof ConcreteType && "String".equals(javaType.getName()) && typedef.getBaseType() != null) {
+ final List<String> regExps = resolveRegExpressionsFromTypedef(typedef);
addStringRegExAsConstant(genTOBuilder, regExps);
}
addUnitsToGenTO(genTOBuilder, typedef.getUnits());
*/
public GeneratedTOBuilder provideGeneratedTOBuilderForUnionTypeDef(final String basePackageName,
final UnionTypeDefinition typedef, final String typeDefName, final SchemaNode parentNode) {
- final List<GeneratedTOBuilder> genTOBuilders = provideGeneratedTOBuildersForUnionTypeDef(basePackageName,
+ final List<GeneratedTOBuilder> builders = provideGeneratedTOBuildersForUnionTypeDef(basePackageName,
typedef, typeDefName, parentNode);
- GeneratedTOBuilder resultTOBuilder = null;
- if (genTOBuilders.isEmpty()) {
- throw new IllegalStateException("No GeneratedTOBuilder objects generated from union " + typedef);
- }
+ Preconditions.checkState(!builders.isEmpty(), "No GeneratedTOBuilder objects generated from union %s", typedef);
- resultTOBuilder = genTOBuilders.remove(0);
- for (GeneratedTOBuilder genTOBuilder : genTOBuilders) {
+ final GeneratedTOBuilder resultTOBuilder = builders.remove(0);
+ for (GeneratedTOBuilder genTOBuilder : builders) {
resultTOBuilder.addEnclosingTransferObject(genTOBuilder);
}
- final GeneratedPropertyBuilder genPropBuilder = resultTOBuilder.addProperty("value");
- genPropBuilder.setReturnType(Types.CHAR_ARRAY);
- resultTOBuilder.addEqualsIdentity(genPropBuilder);
- resultTOBuilder.addHashIdentity(genPropBuilder);
- resultTOBuilder.addToStringProperty(genPropBuilder);
-
+ resultTOBuilder.addProperty("value").setReturnType(Types.CHAR_ARRAY);
return resultTOBuilder;
}
* <li>if Qname of <code>typedef</code> is null</li>
* </ul>
*/
- public List<GeneratedTOBuilder> provideGeneratedTOBuildersForUnionTypeDef(final String basePackageName,
- final UnionTypeDefinition typedef, final String typeDefName, final SchemaNode parentNode) {
+ public List<GeneratedTOBuilder> provideGeneratedTOBuildersForUnionTypeDef(final String basePackageName, final UnionTypeDefinition typedef, final String typeDefName, final SchemaNode parentNode) {
Preconditions.checkNotNull(basePackageName, "Base Package Name cannot be NULL!");
Preconditions.checkNotNull(typedef, "Type Definition cannot be NULL!");
Preconditions.checkNotNull(typedef.getQName(), "Type definition QName cannot be NULL!");
if (typeDefName != null && !typeDefName.isEmpty()) {
final String typeName = BindingMapping.getClassName(typeDefName);
unionGenTOBuilder = new GeneratedTOBuilderImpl(basePackageName, typeName);
- unionGenTOBuilder.setDescription(typedef.getDescription());
+ final String typedefDescription = encodeAngleBrackets(typedef.getDescription());
+ unionGenTOBuilder.setDescription(typedefDescription);
unionGenTOBuilder.setReference(typedef.getReference());
unionGenTOBuilder.setSchemaPath(typedef.getPath().getPathFromRoot());
unionGenTOBuilder.setModuleName(module.getName());
final List<String> regularExpressions = new ArrayList<String>();
for (final TypeDefinition<?> unionType : unionTypes) {
final String unionTypeName = unionType.getQName().getLocalName();
- if (unionType instanceof UnionType) {
- generatedTOBuilders.addAll(resolveUnionSubtypeAsUnion(unionGenTOBuilder, (UnionType) unionType,
- basePackageName, parentNode));
- } else if (unionType instanceof ExtendedType) {
- resolveExtendedSubtypeAsUnion(unionGenTOBuilder, (ExtendedType) unionType, regularExpressions,
+ if (unionType.getBaseType() != null) {
+ resolveExtendedSubtypeAsUnion(unionGenTOBuilder, unionType, regularExpressions,
parentNode);
+ } else if (unionType instanceof UnionTypeDefinition) {
+ generatedTOBuilders.addAll(resolveUnionSubtypeAsUnion(unionGenTOBuilder, (UnionTypeDefinition) unionType,
+ basePackageName, parentNode));
} else if (unionType instanceof EnumTypeDefinition) {
final Enumeration enumeration = addInnerEnumerationToTypeBuilder((EnumTypeDefinition) unionType,
unionTypeName, unionGenTOBuilder);
updateUnionTypeAsProperty(unionGenTOBuilder, enumeration, unionTypeName);
} else {
- final Type javaType = BaseYangTypes.BASE_YANG_TYPES_PROVIDER.javaTypeForSchemaDefinitionType(unionType,
- parentNode);
+ final Type javaType = javaTypeForSchemaDefinitionType(unionType, parentNode);
updateUnionTypeAsProperty(unionGenTOBuilder, javaType, unionTypeName);
}
}
* bigger one due to recursive call of
* <code>provideGeneratedTOBuildersForUnionTypeDef</code> method.
*/
- private List<GeneratedTOBuilder> resolveUnionSubtypeAsUnion(final GeneratedTOBuilder parentUnionGenTOBuilder,
- final UnionTypeDefinition unionSubtype, final String basePackageName, final SchemaNode parentNode) {
+ private List<GeneratedTOBuilder> resolveUnionSubtypeAsUnion(final GeneratedTOBuilder parentUnionGenTOBuilder, final UnionTypeDefinition unionSubtype, final String basePackageName, final SchemaNode parentNode) {
final String newTOBuilderName = provideAvailableNameForGenTOBuilder(parentUnionGenTOBuilder.getName());
final List<GeneratedTOBuilder> subUnionGenTOBUilders = provideGeneratedTOBuildersForUnionTypeDef(
basePackageName, unionSubtype, newTOBuilderName, parentNode);
* parent Schema Node for Extended Subtype
*
*/
- private void resolveExtendedSubtypeAsUnion(final GeneratedTOBuilder parentUnionGenTOBuilder,
- final ExtendedType unionSubtype, final List<String> regularExpressions, final SchemaNode parentNode) {
+ private void resolveExtendedSubtypeAsUnion(final GeneratedTOBuilder parentUnionGenTOBuilder, final TypeDefinition<?> unionSubtype, final List<String> regularExpressions, final SchemaNode parentNode) {
final String unionTypeName = unionSubtype.getQName().getLocalName();
final Type genTO = findGenTO(unionTypeName, unionSubtype);
if (genTO != null) {
updateUnionTypeAsProperty(parentUnionGenTOBuilder, javaType, unionTypeName);
}
}
- if (baseType instanceof StringType) {
+ if (baseType instanceof StringTypeDefinition) {
regularExpressions.addAll(resolveRegExpressionsFromTypedef(unionSubtype));
}
}
* generated TO builder which is converted to generated TO and
* stored
*/
- private void storeGenTO(final TypeDefinition<?> newTypeDef, final GeneratedTOBuilder genTOBuilder,
- final SchemaNode parentNode) {
- if (!(newTypeDef instanceof UnionType)) {
-
+ private void storeGenTO(final TypeDefinition<?> newTypeDef, final GeneratedTOBuilder genTOBuilder, final SchemaNode parentNode) {
+ if (!(newTypeDef instanceof UnionTypeDefinition)) {
final Module parentModule = findParentModule(schemaContext, parentNode);
if (parentModule != null && parentModule.getName() != null) {
Map<Date, Map<String, Type>> modulesByDate = genTypeDefsContextMap.get(parentModule.getName());
* string with name of property which should be added to
* <code>unionGentransObject</code>
*/
- private static void updateUnionTypeAsProperty(final GeneratedTOBuilder unionGenTransObject, final Type type,
- final String propertyName) {
+ private static void updateUnionTypeAsProperty(final GeneratedTOBuilder unionGenTransObject, final Type type, final String propertyName) {
if (unionGenTransObject != null && type != null && !unionGenTransObject.containsProperty(propertyName)) {
final GeneratedPropertyBuilder propBuilder = unionGenTransObject
.addProperty(BindingMapping.getPropertyName(propertyName));
* @return generated TO builder which contains data from
* <code>typedef</code> and <code>basePackageName</code>
*/
- private static GeneratedTOBuilderImpl typedefToTransferObject(final String basePackageName,
- final TypeDefinition<?> typedef, final String moduleName) {
+ private static GeneratedTOBuilderImpl typedefToTransferObject(final String basePackageName, final TypeDefinition<?> typedef, final String moduleName) {
final String packageName = BindingGeneratorUtil.packageNameForGeneratedType(basePackageName, typedef.getPath());
final String typeDefTOName = typedef.getQName().getLocalName();
if ((packageName != null) && (typeDefTOName != null)) {
final String genTOName = BindingMapping.getClassName(typeDefTOName);
final GeneratedTOBuilderImpl newType = new GeneratedTOBuilderImpl(packageName, genTOName);
+ final String typedefDescription = encodeAngleBrackets(typedef.getDescription());
- newType.setDescription(typedef.getDescription());
+ newType.setDescription(typedefDescription);
newType.setReference(typedef.getReference());
newType.setSchemaPath(typedef.getPath().getPathFromRoot());
newType.setModuleName(moduleName);
* <li>if <code>basePackageName</code> equals null</li>
* </ul>
*/
- public GeneratedTOBuilder provideGeneratedTOBuilderForBitsTypeDefinition(final String basePackageName,
- final TypeDefinition<?> typeDef, final String typeDefName, final String moduleName) {
+ public GeneratedTOBuilder provideGeneratedTOBuilderForBitsTypeDefinition(final String basePackageName, final TypeDefinition<?> typeDef, final String typeDefName, final String moduleName) {
Preconditions.checkArgument(typeDef != null, "typeDef cannot be NULL!");
Preconditions.checkArgument(basePackageName != null, "Base Package Name cannot be NULL!");
final String typeName = BindingMapping.getClassName(typeDefName);
final GeneratedTOBuilderImpl genTOBuilder = new GeneratedTOBuilderImpl(basePackageName, typeName);
+ final String typedefDescription = encodeAngleBrackets(typeDef.getDescription());
- genTOBuilder.setDescription(typeDef.getDescription());
+ genTOBuilder.setDescription(typedefDescription);
genTOBuilder.setReference(typeDef.getReference());
genTOBuilder.setSchemaPath(typeDef.getPath().getPathFromRoot());
genTOBuilder.setModuleName(moduleName);
* if <code>typedef</code> equals null
*
*/
- private List<String> resolveRegExpressionsFromTypedef(final ExtendedType typedef) {
- final List<String> regExps = new ArrayList<String>();
+ private static List<String> resolveRegExpressionsFromTypedef(final TypeDefinition<?> typedef) {
Preconditions.checkArgument(typedef != null, "typedef can't be null");
- final TypeDefinition<?> strTypeDef = baseTypeDefForExtendedType(typedef);
- if (strTypeDef instanceof StringType) {
- final List<PatternConstraint> patternConstraints = typedef.getPatternConstraints();
- if (!patternConstraints.isEmpty()) {
- String regEx;
- String modifiedRegEx;
- for (PatternConstraint patternConstraint : patternConstraints) {
- regEx = patternConstraint.getRegularExpression();
- modifiedRegEx = StringEscapeUtils.escapeJava(regEx);
- regExps.add(modifiedRegEx);
- }
- }
+
+ final List<PatternConstraint> patternConstraints;
+ if (typedef instanceof StringTypeDefinition) {
+ // FIXME: run diff against base
+ patternConstraints = ((StringTypeDefinition) typedef).getPatternConstraints();
+ } else {
+ patternConstraints = ImmutableList.of();
}
+
+ final List<String> regExps = new ArrayList<>(patternConstraints.size());
+ for (PatternConstraint patternConstraint : patternConstraints) {
+ final String regEx = patternConstraint.getRegularExpression();
+ final String modifiedRegEx = StringEscapeUtils.escapeJava(regEx);
+ regExps.add(modifiedRegEx);
+ }
+
return regExps;
}
* <li>if <code>typedefName</code> equals null</li>
* </ul>
*/
- private GeneratedTransferObject provideGeneratedTOFromExtendedType(final TypeDefinition<?> typedef,
- final ExtendedType innerExtendedType, final String basePackageName, final String moduleName) {
+ private GeneratedTransferObject provideGeneratedTOFromExtendedType(final TypeDefinition<?> typedef, final TypeDefinition<?> innerExtendedType, final String basePackageName, final String moduleName) {
Preconditions.checkArgument(innerExtendedType != null, "Extended type cannot be NULL!");
Preconditions.checkArgument(basePackageName != null, "String with base package name cannot be NULL!");
final String classTypedefName = BindingMapping.getClassName(typedefName);
final String innerTypeDef = innerExtendedType.getQName().getLocalName();
final GeneratedTOBuilderImpl genTOBuilder = new GeneratedTOBuilderImpl(basePackageName, classTypedefName);
+ final String typedefDescription = encodeAngleBrackets(typedef.getDescription());
- genTOBuilder.setDescription(typedef.getDescription());
+ genTOBuilder.setDescription(typedefDescription);
genTOBuilder.setReference(typedef.getReference());
genTOBuilder.setSchemaPath(typedef.getPath().getPathFromRoot());
genTOBuilder.setModuleName(moduleName);
genTOBuilder.setTypedef(true);
Restrictions r = BindingGeneratorUtil.getRestrictions(typedef);
genTOBuilder.setRestrictions(r);
+ if (typedef.getStatus() == Status.DEPRECATED) {
+ genTOBuilder.addAnnotation("", "Deprecated");
+ }
if (baseTypeDefForExtendedType(innerExtendedType) instanceof UnionTypeDefinition) {
genTOBuilder.setIsUnion(true);
* definition to the base type
*/
private static int getTypeDefinitionDepth(final TypeDefinition<?> typeDefinition) {
- // FIXME: rewrite this in a non-recursive manner, without ExtendedType and UnionType
+ // FIXME: rewrite this in a non-recursive manner
if (typeDefinition == null) {
return 1;
}
- int depth = 1;
TypeDefinition<?> baseType = typeDefinition.getBaseType();
+ if (baseType == null) {
+ return 1;
+ }
- if (baseType instanceof ExtendedType) {
- depth = depth + getTypeDefinitionDepth(typeDefinition.getBaseType());
- } else if (baseType instanceof UnionType) {
- List<TypeDefinition<?>> childTypeDefinitions = ((UnionType) baseType).getTypes();
+ int depth = 1;
+ if (baseType.getBaseType() != null) {
+ depth = depth + getTypeDefinitionDepth(baseType);
+ } else if (baseType instanceof UnionTypeDefinition) {
+ List<TypeDefinition<?>> childTypeDefinitions = ((UnionTypeDefinition) baseType).getTypes();
int maxChildDepth = 0;
int childDepth = 1;
for (TypeDefinition<?> childTypeDefinition : childTypeDefinitions) {
}
public String getTypeDefaultConstruction(final LeafSchemaNode node, final String defaultValue) {
- TypeDefinition<?> type = node.getType();
+ TypeDefinition<?> type = CompatUtils.compatLeafType(node);
QName typeQName = type.getQName();
TypeDefinition<?> base = baseTypeDefForExtendedType(type);
Preconditions.checkNotNull(type, "Cannot provide default construction for null type of %s", node);
parentName = BindingMapping.getClassName(parent.getName());
className = packageName + "." + parentName + "." + BindingMapping.getClassName(node.getQName());
}
- result = bitsToDef((BitsTypeDefinition) base, className, defaultValue, type instanceof ExtendedType);
+ result = bitsToDef((BitsTypeDefinition) base, className, defaultValue, type.getBaseType() != null);
} else if (base instanceof BooleanTypeDefinition) {
- result = typeToDef(Boolean.class, defaultValue);
+ result = typeToBooleanDef(defaultValue);
} else if (base instanceof DecimalTypeDefinition) {
result = typeToDef(BigDecimal.class, defaultValue);
} else if (base instanceof EmptyTypeDefinition) {
- result = typeToDef(Boolean.class, defaultValue);
+ result = typeToBooleanDef(defaultValue);
} else if (base instanceof EnumTypeDefinition) {
char[] defValArray = defaultValue.toCharArray();
char first = Character.toUpperCase(defaultValue.charAt(0));
defValArray[0] = first;
String newDefVal = new String(defValArray);
String className;
- if (type instanceof ExtendedType) {
+ if (type.getBaseType() != null) {
Module m = getParentModule(type);
String basePackageName = BindingMapping.getRootPackageName(m.getQNameModule());
String packageName = BindingGeneratorUtil.packageNameForGeneratedType(basePackageName, type.getPath());
throw new UnsupportedOperationException("Cannot get default construction for identityref type");
} else if (base instanceof InstanceIdentifierTypeDefinition) {
throw new UnsupportedOperationException("Cannot get default construction for instance-identifier type");
- } else if (base instanceof Int8) {
- result = typeToDef(Byte.class, defaultValue);
- } else if (base instanceof Int16) {
- result = typeToDef(Short.class, defaultValue);
- } else if (base instanceof Int32) {
- result = typeToDef(Integer.class, defaultValue);
- } else if (base instanceof Int64) {
- result = typeToDef(Long.class, defaultValue);
+ } else if (BaseTypes.isInt8(base)) {
+ result = typeToValueOfDef(Byte.class, defaultValue);
+ } else if (BaseTypes.isInt16(base)) {
+ result = typeToValueOfDef(Short.class, defaultValue);
+ } else if (BaseTypes.isInt32(base)) {
+ result = typeToValueOfDef(Integer.class, defaultValue);
+ } else if (BaseTypes.isInt64(base)) {
+ result = typeToValueOfDef(Long.class, defaultValue);
} else if (base instanceof LeafrefTypeDefinition) {
result = leafrefToDef(node, (LeafrefTypeDefinition) base, defaultValue);
} else if (base instanceof StringTypeDefinition) {
result = "\"" + defaultValue + "\"";
- } else if (base instanceof Uint8) {
- result = typeToDef(Short.class, defaultValue);
- } else if (base instanceof Uint16) {
- result = typeToDef(Integer.class, defaultValue);
- } else if (base instanceof Uint32) {
- result = typeToDef(Long.class, defaultValue);
- } else if (base instanceof Uint64) {
- result = typeToDef(BigInteger.class, defaultValue);
+ } else if (BaseTypes.isUint8(base)) {
+ result = typeToValueOfDef(Short.class, defaultValue);
+ } else if (BaseTypes.isUint16(base)) {
+ result = typeToValueOfDef(Integer.class, defaultValue);
+ } else if (BaseTypes.isUint32(base)) {
+ result = typeToValueOfDef(Long.class, defaultValue);
+ } else if (BaseTypes.isUint64(base)) {
+ switch (defaultValue) {
+ case "0":
+ result = "java.math.BigInteger.ZERO";
+ break;
+ case "1":
+ result = "java.math.BigInteger.ONE";
+ break;
+ case "10":
+ result = "java.math.BigInteger.TEN";
+ break;
+ default:
+ result = typeToDef(BigInteger.class, defaultValue);
+ }
} else if (base instanceof UnionTypeDefinition) {
result = unionToDef(node);
} else {
}
sb.append(result);
- if (type instanceof ExtendedType && !(base instanceof LeafrefTypeDefinition)
- && !(base instanceof EnumerationType) && !(base instanceof UnionTypeDefinition)) {
+ if (type.getBaseType() != null && !(base instanceof LeafrefTypeDefinition)
+ && !(base instanceof EnumTypeDefinition) && !(base instanceof UnionTypeDefinition)) {
Module m = getParentModule(type);
String basePackageName = BindingMapping.getRootPackageName(m.getQNameModule());
String packageName = BindingGeneratorUtil.packageNameForGeneratedType(basePackageName, type.getPath());
return "new " + clazz.getName() + "(\"" + defaultValue + "\")";
}
+ private static String typeToValueOfDef(final Class<?> clazz, final String defaultValue) {
+ return clazz.getName() + ".valueOf(\"" + defaultValue + "\")";
+ }
+
+ private static String typeToBooleanDef(final String defaultValue) {
+ switch (defaultValue) {
+ case "false":
+ return "java.lang.Boolean.FALSE";
+ case "true":
+ return "java.lang.Boolean.TRUE";
+ default:
+ return typeToValueOfDef(Boolean.class, defaultValue);
+ }
+ }
+
private static String binaryToDef(final String defaultValue) {
StringBuilder sb = new StringBuilder();
BaseEncoding en = BaseEncoding.base64();
return sb.toString();
}
- private static final Comparator<Bit> BIT_NAME_COMPARATOR = new Comparator<Bit>() {
- @Override
- public int compare(final Bit o1, final Bit o2) {
- return o1.getName().compareTo(o2.getName());
- }
- };
+ private static final Comparator<Bit> BIT_NAME_COMPARATOR = (o1, o2) -> o1.getName().compareTo(o2.getName());
- private static String bitsToDef(final BitsTypeDefinition type, final String className, final String defaultValue,
- final boolean isExt) {
+ private static String bitsToDef(final BitsTypeDefinition type, final String className, final String defaultValue, final boolean isExt) {
List<Bit> bits = new ArrayList<>(type.getBits());
Collections.sort(bits, BIT_NAME_COMPARATOR);
StringBuilder sb = new StringBuilder();
}
private String unionToDef(final LeafSchemaNode node) {
+ final TypeDefinition<?> type = CompatUtils.compatLeafType(node);
String parentName;
String className;
- if (node.getType() instanceof ExtendedType) {
- ExtendedType type = (ExtendedType) node.getType();
+ if (type.getBaseType() != null) {
QName typeQName = type.getQName();
Module module = null;
Set<Module> modules = schemaContext.findModuleByNamespace(typeQName.getNamespace());
}
if (module == null) {
List<Module> modulesList = new ArrayList<>(modules);
- Collections.sort(modulesList, new Comparator<Module>() {
- @Override
- public int compare(final Module o1, final Module o2) {
- return o1.getRevision().compareTo(o2.getRevision());
- }
- });
+ Collections.sort(modulesList, (o1, o2) -> o1.getRevision().compareTo(o2.getRevision()));
module = modulesList.get(0);
}
} else {
Date revision = first.getRevision();
Module parentModule = schemaContext.findModuleByNamespaceAndRevision(namespace, revision);
String basePackageName = BindingMapping.getRootPackageName(parentModule.getQNameModule());
- String packageName = BindingGeneratorUtil.packageNameForGeneratedType(basePackageName,
- node.getType().getPath());
+ String packageName = BindingGeneratorUtil.packageNameForGeneratedType(basePackageName, UNION_PATH);
className = packageName + "." + BindingMapping.getClassName(node.getQName());
}
}