X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=binding%2Fmdsal-binding-generator-impl%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fyangtools%2Fsal%2Fbinding%2Fyang%2Ftypes%2FTypeProviderImpl.java;h=397557611591c7132ce56f4ab5b342f750716b5a;hb=refs%2Fchanges%2F97%2F39897%2F12;hp=3aedd9d2f0c66d0d4445ce31e97a05a6b59efe02;hpb=c91cd85aadc9859a9c50d63487834d7a4ee14a0c;p=mdsal.git diff --git a/binding/mdsal-binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/yang/types/TypeProviderImpl.java b/binding/mdsal-binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/yang/types/TypeProviderImpl.java index 3aedd9d2f0..3975576115 100644 --- a/binding/mdsal-binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/yang/types/TypeProviderImpl.java +++ b/binding/mdsal-binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/yang/types/TypeProviderImpl.java @@ -7,10 +7,13 @@ */ package org.opendaylight.yangtools.sal.binding.yang.types; +import static org.opendaylight.yangtools.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; @@ -62,6 +65,7 @@ import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath; 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; @@ -76,25 +80,23 @@ import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition; 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. */ @@ -188,34 +190,50 @@ public final class TypeProviderImpl implements TypeProvider { * */ @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; @@ -394,9 +412,7 @@ public final class TypeProviderImpl implements TypeProvider { *
  • if name of typeDefinition
  • * */ - 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( @@ -405,28 +421,24 @@ public final class TypeProviderImpl implements TypeProvider { 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> modulesByDate = genTypeDefsContextMap.get(module.getName()); - final Map genTOs = modulesByDate.get(module.getRevision()); - if (genTOs != null) { - returnType = genTOs.get(typedefName); - } + if (module != null) { + final Map> modulesByDate = genTypeDefsContextMap.get(module.getName()); + final Map genTOs = modulesByDate.get(module.getRevision()); + if (genTOs != null) { + return genTOs.get(typeDefinition.getQName().getLocalName()); } } } - return returnType; + return null; } /** * Gets base type definition for extendTypeDef. The method is - * recursivelly called until non ExtendedType type is found. + * recursively called until non ExtendedType type is found. * * @param extendTypeDef * type definition for which is the base type definition sought @@ -434,17 +446,15 @@ public final class TypeProviderImpl implements TypeProvider { * @throws IllegalArgumentException * if extendTypeDef equal null */ - private TypeDefinition baseTypeDefForExtendedType(final TypeDefinition extendTypeDef) { + private static TypeDefinition baseTypeDefForExtendedType(final TypeDefinition extendTypeDef) { Preconditions.checkArgument(extendTypeDef != null, "Type Definition reference cannot be NULL!"); - final TypeDefinition baseTypeDef = extendTypeDef.getBaseType(); - if (baseTypeDef == null) { - return extendTypeDef; - } else if (baseTypeDef instanceof ExtendedType) { - return baseTypeDefForExtendedType(baseTypeDef); - } else { - return baseTypeDef; + + TypeDefinition ret = extendTypeDef; + while (ret.getBaseType() != null) { + ret = ret.getBaseType(); } + return ret; } /** @@ -477,29 +487,30 @@ public final class TypeProviderImpl implements TypeProvider { 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; } @@ -519,7 +530,7 @@ public final class TypeProviderImpl implements TypeProvider { 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; } } @@ -552,7 +563,7 @@ public final class TypeProviderImpl implements TypeProvider { /** * Converts enumTypeDef to - * {@link org.opendaylight.yangtools.sal.binding.model.api.Enumeration + * {@link Enumeration * enumeration}. * * @param enumTypeDef @@ -569,8 +580,7 @@ public final class TypeProviderImpl implements TypeProvider { *
  • if name of enumTypeDef equal null
  • * */ - 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!"); @@ -584,7 +594,8 @@ public final class TypeProviderImpl implements TypeProvider { 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()); @@ -615,8 +626,7 @@ public final class TypeProviderImpl implements TypeProvider { * * */ - 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!"); @@ -628,7 +638,8 @@ public final class TypeProviderImpl implements TypeProvider { 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); } @@ -645,7 +656,8 @@ public final class TypeProviderImpl implements TypeProvider { 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); @@ -719,8 +731,7 @@ public final class TypeProviderImpl implements TypeProvider { * modulName or typedef or Q name of * typedef equals null */ - 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)) { @@ -729,10 +740,8 @@ public final class TypeProviderImpl implements TypeProvider { 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); @@ -771,8 +780,7 @@ public final class TypeProviderImpl implements TypeProvider { 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) { @@ -803,8 +811,7 @@ public final class TypeProviderImpl implements TypeProvider { * JAVA Type to which is typedef mapped * @return generated transfer object which representjavaType */ - 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"; @@ -815,8 +822,11 @@ public final class TypeProviderImpl implements TypeProvider { genTOBuilder.addEqualsIdentity(genPropBuilder); genTOBuilder.addHashIdentity(genPropBuilder); genTOBuilder.addToStringProperty(genPropBuilder); - if (javaType instanceof ConcreteType && "String".equals(javaType.getName()) && typedef instanceof ExtendedType) { - final List 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 regExps = resolveRegExpressionsFromTypedef(typedef); addStringRegExAsConstant(genTOBuilder, regExps); } addUnitsToGenTO(genTOBuilder, typedef.getUnits()); @@ -841,24 +851,16 @@ public final class TypeProviderImpl implements TypeProvider { */ public GeneratedTOBuilder provideGeneratedTOBuilderForUnionTypeDef(final String basePackageName, final UnionTypeDefinition typedef, final String typeDefName, final SchemaNode parentNode) { - final List genTOBuilders = provideGeneratedTOBuildersForUnionTypeDef(basePackageName, + final List 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; } @@ -882,8 +884,7 @@ public final class TypeProviderImpl implements TypeProvider { *
  • if Qname of typedef is null
  • * */ - public List provideGeneratedTOBuildersForUnionTypeDef(final String basePackageName, - final UnionTypeDefinition typedef, final String typeDefName, final SchemaNode parentNode) { + public List 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!"); @@ -896,7 +897,8 @@ public final class TypeProviderImpl implements TypeProvider { 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()); @@ -909,19 +911,18 @@ public final class TypeProviderImpl implements TypeProvider { final List regularExpressions = new ArrayList(); 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); } } @@ -957,8 +958,7 @@ public final class TypeProviderImpl implements TypeProvider { * bigger one due to recursive call of * provideGeneratedTOBuildersForUnionTypeDef method. */ - private List resolveUnionSubtypeAsUnion(final GeneratedTOBuilder parentUnionGenTOBuilder, - final UnionTypeDefinition unionSubtype, final String basePackageName, final SchemaNode parentNode) { + private List resolveUnionSubtypeAsUnion(final GeneratedTOBuilder parentUnionGenTOBuilder, final UnionTypeDefinition unionSubtype, final String basePackageName, final SchemaNode parentNode) { final String newTOBuilderName = provideAvailableNameForGenTOBuilder(parentUnionGenTOBuilder.getName()); final List subUnionGenTOBUilders = provideGeneratedTOBuildersForUnionTypeDef( basePackageName, unionSubtype, newTOBuilderName, parentNode); @@ -991,8 +991,7 @@ public final class TypeProviderImpl implements TypeProvider { * parent Schema Node for Extended Subtype * */ - private void resolveExtendedSubtypeAsUnion(final GeneratedTOBuilder parentUnionGenTOBuilder, - final ExtendedType unionSubtype, final List regularExpressions, final SchemaNode parentNode) { + private void resolveExtendedSubtypeAsUnion(final GeneratedTOBuilder parentUnionGenTOBuilder, final TypeDefinition unionSubtype, final List regularExpressions, final SchemaNode parentNode) { final String unionTypeName = unionSubtype.getQName().getLocalName(); final Type genTO = findGenTO(unionTypeName, unionSubtype); if (genTO != null) { @@ -1006,7 +1005,7 @@ public final class TypeProviderImpl implements TypeProvider { updateUnionTypeAsProperty(parentUnionGenTOBuilder, javaType, unionTypeName); } } - if (baseType instanceof StringType) { + if (baseType instanceof StringTypeDefinition) { regularExpressions.addAll(resolveRegExpressionsFromTypedef(unionSubtype)); } } @@ -1044,10 +1043,8 @@ public final class TypeProviderImpl implements TypeProvider { * 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> modulesByDate = genTypeDefsContextMap.get(parentModule.getName()); @@ -1070,8 +1067,7 @@ public final class TypeProviderImpl implements TypeProvider { * string with name of property which should be added to * unionGentransObject */ - 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)); @@ -1093,8 +1089,7 @@ public final class TypeProviderImpl implements TypeProvider { * @return generated TO builder which contains data from * typedef and basePackageName */ - 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(); @@ -1102,8 +1097,9 @@ public final class TypeProviderImpl implements TypeProvider { 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); @@ -1133,8 +1129,7 @@ public final class TypeProviderImpl implements TypeProvider { *
  • if basePackageName equals null
  • * */ - 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!"); @@ -1144,8 +1139,9 @@ public final class TypeProviderImpl implements TypeProvider { 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); @@ -1180,22 +1176,24 @@ public final class TypeProviderImpl implements TypeProvider { * if typedef equals null * */ - private List resolveRegExpressionsFromTypedef(final ExtendedType typedef) { - final List regExps = new ArrayList(); + private static List resolveRegExpressionsFromTypedef(final TypeDefinition typedef) { Preconditions.checkArgument(typedef != null, "typedef can't be null"); - final TypeDefinition strTypeDef = baseTypeDefForExtendedType(typedef); - if (strTypeDef instanceof StringType) { - final List 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 patternConstraints; + if (typedef instanceof StringTypeDefinition) { + // FIXME: run diff against base + patternConstraints = ((StringTypeDefinition) typedef).getPatternConstraints(); + } else { + patternConstraints = ImmutableList.of(); + } + + final List 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; } @@ -1255,8 +1253,7 @@ public final class TypeProviderImpl implements TypeProvider { *
  • if typedefName equals null
  • * */ - 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!"); @@ -1264,14 +1261,18 @@ public final class TypeProviderImpl implements TypeProvider { 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); @@ -1358,16 +1359,20 @@ public final class TypeProviderImpl implements TypeProvider { * definition to the base type */ private static int getTypeDefinitionDepth(final TypeDefinition typeDefinition) { + // 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> childTypeDefinitions = ((UnionType) baseType).getTypes(); + int depth = 1; + if (baseType.getBaseType() != null) { + depth = depth + getTypeDefinitionDepth(baseType); + } else if (baseType instanceof UnionTypeDefinition) { + List> childTypeDefinitions = ((UnionTypeDefinition) baseType).getTypes(); int maxChildDepth = 0; int childDepth = 1; for (TypeDefinition childTypeDefinition : childTypeDefinitions) { @@ -1401,7 +1406,7 @@ public final class TypeProviderImpl implements TypeProvider { } public static void addUnitsToGenTO(final GeneratedTOBuilder to, final String units) { - if (units != null && !units.isEmpty()) { + if (!Strings.isNullOrEmpty(units)) { to.addConstant(Types.STRING, "_UNITS", "\"" + units + "\""); GeneratedPropertyBuilder prop = new GeneratedPropertyBuilderImpl("UNITS"); prop.setReturnType(Types.STRING); @@ -1415,7 +1420,7 @@ public final class TypeProviderImpl implements TypeProvider { } 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); @@ -1442,7 +1447,7 @@ public final class TypeProviderImpl implements TypeProvider { 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); } else if (base instanceof DecimalTypeDefinition) { @@ -1455,7 +1460,7 @@ public final class TypeProviderImpl implements TypeProvider { 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()); @@ -1471,25 +1476,25 @@ public final class TypeProviderImpl implements TypeProvider { 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) { + } else if (BaseTypes.isInt8(base)) { result = typeToDef(Byte.class, defaultValue); - } else if (base instanceof Int16) { + } else if (BaseTypes.isInt16(base)) { result = typeToDef(Short.class, defaultValue); - } else if (base instanceof Int32) { + } else if (BaseTypes.isInt32(base)) { result = typeToDef(Integer.class, defaultValue); - } else if (base instanceof Int64) { + } else if (BaseTypes.isInt64(base)) { result = typeToDef(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) { + } else if (BaseTypes.isUint8(base)) { result = typeToDef(Short.class, defaultValue); - } else if (base instanceof Uint16) { + } else if (BaseTypes.isUint16(base)) { result = typeToDef(Integer.class, defaultValue); - } else if (base instanceof Uint32) { + } else if (BaseTypes.isUint32(base)) { result = typeToDef(Long.class, defaultValue); - } else if (base instanceof Uint64) { + } else if (BaseTypes.isUint64(base)) { result = typeToDef(BigInteger.class, defaultValue); } else if (base instanceof UnionTypeDefinition) { result = unionToDef(node); @@ -1498,8 +1503,8 @@ public final class TypeProviderImpl implements TypeProvider { } 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()); @@ -1530,15 +1535,11 @@ public final class TypeProviderImpl implements TypeProvider { return sb.toString(); } - private static String bitsToDef(final BitsTypeDefinition type, final String className, final String defaultValue, - final boolean isExt) { + private static final Comparator 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) { List bits = new ArrayList<>(type.getBits()); - Collections.sort(bits, new Comparator() { - @Override - public int compare(final Bit o1, final Bit o2) { - return o1.getName().compareTo(o2.getName()); - } - }); + Collections.sort(bits, BIT_NAME_COMPARATOR); StringBuilder sb = new StringBuilder(); if (!isExt) { sb.append("new "); @@ -1598,11 +1599,11 @@ public final class TypeProviderImpl implements TypeProvider { } 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 modules = schemaContext.findModuleByNamespace(typeQName.getNamespace()); @@ -1615,12 +1616,7 @@ public final class TypeProviderImpl implements TypeProvider { } if (module == null) { List modulesList = new ArrayList<>(modules); - Collections.sort(modulesList, new Comparator() { - @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 { @@ -1644,8 +1640,7 @@ public final class TypeProviderImpl implements TypeProvider { 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()); } }