X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;ds=sidebyside;f=opendaylight%2Fsal%2Fyang-prototype%2Fcode-generator%2Fbinding-generator-impl%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fsal%2Fbinding%2Fyang%2Ftypes%2FTypeProviderImpl.java;h=9410ffe1de5dc9825325bfc97b076aa32f079a94;hb=a0485f8ee40e67bb60d70644838c01b2d9f5301e;hp=b3739ec5c44d5812a708139603cd4104ff738d67;hpb=37ff82351675cc5c279dfe88c6daf10cbbf9f48b;p=controller.git diff --git a/opendaylight/sal/yang-prototype/code-generator/binding-generator-impl/src/main/java/org/opendaylight/controller/sal/binding/yang/types/TypeProviderImpl.java b/opendaylight/sal/yang-prototype/code-generator/binding-generator-impl/src/main/java/org/opendaylight/controller/sal/binding/yang/types/TypeProviderImpl.java index b3739ec5c4..9410ffe1de 100644 --- a/opendaylight/sal/yang-prototype/code-generator/binding-generator-impl/src/main/java/org/opendaylight/controller/sal/binding/yang/types/TypeProviderImpl.java +++ b/opendaylight/sal/yang-prototype/code-generator/binding-generator-impl/src/main/java/org/opendaylight/controller/sal/binding/yang/types/TypeProviderImpl.java @@ -19,12 +19,11 @@ import org.opendaylight.controller.sal.binding.model.api.type.builder.EnumBuilde import org.opendaylight.controller.sal.binding.model.api.type.builder.GeneratedPropertyBuilder; import org.opendaylight.controller.sal.binding.model.api.type.builder.GeneratedTOBuilder; import org.opendaylight.controller.sal.binding.model.api.type.builder.GeneratedTypeBuilder; +import org.opendaylight.controller.yang.common.QName; import org.opendaylight.controller.yang.model.api.*; -import org.opendaylight.controller.yang.model.api.type.EnumTypeDefinition; +import org.opendaylight.controller.yang.model.api.type.*; +import org.opendaylight.controller.yang.model.api.type.BitsTypeDefinition.Bit; import org.opendaylight.controller.yang.model.api.type.EnumTypeDefinition.EnumPair; -import org.opendaylight.controller.yang.model.api.type.IdentityrefTypeDefinition; -import org.opendaylight.controller.yang.model.api.type.LeafrefTypeDefinition; -import org.opendaylight.controller.yang.model.api.type.UnionTypeDefinition; import org.opendaylight.controller.yang.model.util.ExtendedType; import java.util.HashMap; @@ -55,13 +54,13 @@ public final class TypeProviderImpl implements TypeProvider { public void putReferencedType(final SchemaPath refTypePath, final Type refType) { if (refTypePath == null) { - throw new IllegalArgumentException("Path reference of " + - "Enumeration Type Definition cannot be NULL!"); + throw new IllegalArgumentException("Path reference of " + + "Enumeration Type Definition cannot be NULL!"); } if (refType == null) { - throw new IllegalArgumentException("Reference to Enumeration " + - "Type cannot be NULL!"); + throw new IllegalArgumentException("Reference to Enumeration " + + "Type cannot be NULL!"); } referencedTypes.put(refTypePath, refType); } @@ -84,18 +83,13 @@ public final class TypeProviderImpl implements TypeProvider { final TypeDefinition typeDefinition) { Type returnType = null; if (typeDefinition == null) { - throw new IllegalArgumentException("Type Definition cannot be " + - "NULL!"); + throw new IllegalArgumentException("Type Definition cannot be NULL!"); } - if (typeDefinition.getQName() == null) { - throw new IllegalArgumentException("Type Definition cannot have " + - "non specified QName (QName cannot be NULL!)"); + throw new IllegalArgumentException("Type Definition cannot have non specified QName (QName cannot be NULL!)"); } - if (typeDefinition.getQName().getLocalName() == null) { - throw new IllegalArgumentException("Type Definitions Local Name " + - "cannot be NULL!"); + throw new IllegalArgumentException("Type Definitions Local Name cannot be NULL!"); } final String typedefName = typeDefinition.getQName().getLocalName(); if (typeDefinition instanceof ExtendedType) { @@ -105,14 +99,15 @@ public final class TypeProviderImpl implements TypeProvider { final LeafrefTypeDefinition leafref = (LeafrefTypeDefinition) baseTypeDef; returnType = provideTypeForLeafref(leafref); } else if (baseTypeDef instanceof IdentityrefTypeDefinition) { - + final IdentityrefTypeDefinition idref = (IdentityrefTypeDefinition) baseTypeDef; + returnType = returnTypeForIdentityref(idref); } else if (baseTypeDef instanceof EnumTypeDefinition) { final EnumTypeDefinition enumTypeDef = (EnumTypeDefinition) baseTypeDef; returnType = resolveEnumFromTypeDefinition(enumTypeDef, typedefName); } else { - final Module module = findParentModuleForTypeDefinition(schemaContext, - typeDefinition); + final Module module = findParentModuleForTypeDefinition( + schemaContext, typeDefinition); if (module != null) { final Map genTOs = genTypeDefsContextMap .get(module.getName()); @@ -130,12 +125,42 @@ public final class TypeProviderImpl implements TypeProvider { final LeafrefTypeDefinition leafref = (LeafrefTypeDefinition) typeDefinition; returnType = provideTypeForLeafref(leafref); } else if (typeDefinition instanceof IdentityrefTypeDefinition) { - + final IdentityrefTypeDefinition idref = (IdentityrefTypeDefinition) typeDefinition; + returnType = returnTypeForIdentityref(idref); } else { returnType = BaseYangTypes.BASE_YANG_TYPES_PROVIDER .javaTypeForSchemaDefinitionType(typeDefinition); } } + // TODO: add throw exception when we will be able to resolve ALL yang + // types! + // if (returnType == null) { + // throw new IllegalArgumentException("Type Provider can't resolve " + + // "type for specified Type Definition " + typedefName); + // } + return returnType; + } + + private Type returnTypeForIdentityref(IdentityrefTypeDefinition idref) { + QName baseIdQName = idref.getIdentity(); + Module module = schemaContext.findModuleByNamespace(baseIdQName.getNamespace()); + IdentitySchemaNode identity = null; + for (IdentitySchemaNode id : module.getIdentities()) { + if (id.getQName().equals(baseIdQName)) { + identity = id; + } + } + if (identity == null) { + throw new IllegalArgumentException("Target identity '" + baseIdQName + "' do not exists"); + } + + final String basePackageName = moduleNamespaceToPackageName(module); + final String packageName = packageNameForGeneratedType(basePackageName, identity.getPath()); + final String genTypeName = parseToClassName(identity.getQName().getLocalName()); + + Type baseType = Types.typeForClass(Class.class); + Type paramType = Types.wildcardTypeFor(packageName, genTypeName); + Type returnType = Types.parameterizedTypeFor(baseType, paramType); return returnType; } @@ -143,16 +168,13 @@ public final class TypeProviderImpl implements TypeProvider { final TypeDefinition typeDefinition) { Type returnType = null; if (typeDefinition == null) { - throw new IllegalArgumentException("Type Definition cannot be " + - "NULL!"); + throw new IllegalArgumentException("Type Definition cannot be NULL!"); } if (typeDefinition.getQName() == null) { - throw new IllegalArgumentException("Type Definition cannot have " + - "non specified QName (QName cannot be NULL!)"); + throw new IllegalArgumentException("Type Definition cannot have non specified QName (QName cannot be NULL!)"); } if (typeDefinition.getQName() == null) { - throw new IllegalArgumentException("Type Definitions Local Name " + - "cannot be NULL!"); + throw new IllegalArgumentException("Type Definitions Local Name cannot be NULL!"); } final String typedefName = typeDefinition.getQName().getLocalName(); @@ -161,8 +183,8 @@ public final class TypeProviderImpl implements TypeProvider { if (!(baseTypeDef instanceof LeafrefTypeDefinition) && !(baseTypeDef instanceof IdentityrefTypeDefinition)) { - final Module module = findParentModuleForTypeDefinition(schemaContext, - typeDefinition); + final Module module = findParentModuleForTypeDefinition( + schemaContext, typeDefinition); if (module != null) { final Map genTOs = genTypeDefsContextMap @@ -179,8 +201,7 @@ public final class TypeProviderImpl implements TypeProvider { private TypeDefinition baseTypeDefForExtendedType( final TypeDefinition extendTypeDef) { if (extendTypeDef == null) { - throw new IllegalArgumentException("Type Definiition reference " + - "cannot be NULL!"); + throw new IllegalArgumentException("Type Definiition reference cannot be NULL!"); } final TypeDefinition baseTypeDef = extendTypeDef.getBaseType(); if (baseTypeDef instanceof ExtendedType) { @@ -194,13 +215,11 @@ public final class TypeProviderImpl implements TypeProvider { public Type provideTypeForLeafref(final LeafrefTypeDefinition leafrefType) { Type returnType = null; if (leafrefType == null) { - throw new IllegalArgumentException("Leafref Type Definition " + - "reference cannot be NULL!"); + throw new IllegalArgumentException("Leafref Type Definition reference cannot be NULL!"); } if (leafrefType.getPathStatement() == null) { - throw new IllegalArgumentException("The Path Statement for " + - "Leafref Type Definition cannot be NULL!"); + throw new IllegalArgumentException("The Path Statement for Leafref Type Definition cannot be NULL!"); } final RevisionAwareXPath xpath = leafrefType.getPathStatement(); @@ -210,22 +229,23 @@ public final class TypeProviderImpl implements TypeProvider { if (strXPath.matches(".*//[.* | .*//].*")) { returnType = Types.typeForClass(Object.class); } else { - final Module module = findParentModuleForTypeDefinition(schemaContext, leafrefType); + final Module module = findParentModuleForTypeDefinition( + schemaContext, leafrefType); if (module != null) { final DataSchemaNode dataNode; if (xpath.isAbsolute()) { dataNode = findDataSchemaNode(schemaContext, module, xpath); } else { - dataNode = findDataSchemaNodeForRelativeXPath(schemaContext, - module, leafrefType, xpath); + dataNode = findDataSchemaNodeForRelativeXPath( + schemaContext, module, leafrefType, xpath); } if (leafContainsEnumDefinition(dataNode)) { returnType = referencedTypes.get(dataNode.getPath()); } else if (leafListContainsEnumDefinition(dataNode)) { - returnType = Types.listTypeFor(referencedTypes.get( - dataNode.getPath())); + returnType = Types.listTypeFor(referencedTypes + .get(dataNode.getPath())); } else { returnType = resolveTypeFromDataSchemaNode(dataNode); } @@ -245,8 +265,7 @@ public final class TypeProviderImpl implements TypeProvider { return false; } - private boolean leafListContainsEnumDefinition( - final DataSchemaNode dataNode) { + private boolean leafListContainsEnumDefinition(final DataSchemaNode dataNode) { if (dataNode instanceof LeafListSchemaNode) { final LeafListSchemaNode leafList = (LeafListSchemaNode) dataNode; if (leafList.getType() instanceof EnumTypeDefinition) { @@ -259,25 +278,22 @@ public final class TypeProviderImpl implements TypeProvider { private Enumeration resolveEnumFromTypeDefinition( final EnumTypeDefinition enumTypeDef, final String enumName) { if (enumTypeDef == null) { - throw new IllegalArgumentException("EnumTypeDefinition reference " + - "cannot be NULL!"); + throw new IllegalArgumentException("EnumTypeDefinition reference cannot be NULL!"); } if (enumTypeDef.getValues() == null) { - throw new IllegalArgumentException("EnumTypeDefinition MUST " + - "contain at least ONE value definition!"); + throw new IllegalArgumentException("EnumTypeDefinition MUST contain at least ONE value definition!"); } if (enumTypeDef.getQName() == null) { - throw new IllegalArgumentException("EnumTypeDefinition MUST " + - "contain NON-NULL QName!"); + throw new IllegalArgumentException("EnumTypeDefinition MUST contain NON-NULL QName!"); } if (enumTypeDef.getQName().getLocalName() == null) { - throw new IllegalArgumentException("Local Name in " + - "EnumTypeDefinition QName cannot be NULL!"); + throw new IllegalArgumentException("Local Name in EnumTypeDefinition QName cannot be NULL!"); } final String enumerationName = parseToClassName(enumName); - Module module = findParentModuleForTypeDefinition(schemaContext, enumTypeDef); + Module module = findParentModuleForTypeDefinition(schemaContext, + enumTypeDef); final String basePackageName = moduleNamespaceToPackageName(module); final EnumBuilder enumBuilder = new EnumerationBuilderImpl( @@ -290,24 +306,19 @@ public final class TypeProviderImpl implements TypeProvider { final EnumTypeDefinition enumTypeDef, final String enumName, final GeneratedTypeBuilder typeBuilder) { if (enumTypeDef == null) { - throw new IllegalArgumentException("EnumTypeDefinition reference " + - "cannot be NULL!"); + throw new IllegalArgumentException("EnumTypeDefinition reference cannot be NULL!"); } if (enumTypeDef.getValues() == null) { - throw new IllegalArgumentException("EnumTypeDefinition MUST " + - "contain at least ONE value definition!"); + throw new IllegalArgumentException("EnumTypeDefinition MUST contain at least ONE value definition!"); } if (enumTypeDef.getQName() == null) { - throw new IllegalArgumentException("EnumTypeDefinition MUST " + - "contain NON-NULL QName!"); + throw new IllegalArgumentException("EnumTypeDefinition MUST contain NON-NULL QName!"); } if (enumTypeDef.getQName().getLocalName() == null) { - throw new IllegalArgumentException("Local Name in " + - "EnumTypeDefinition QName cannot be NULL!"); + throw new IllegalArgumentException("Local Name in EnumTypeDefinition QName cannot be NULL!"); } if (typeBuilder == null) { - throw new IllegalArgumentException("Generated Type Builder " + - "reference cannot be NULL!"); + throw new IllegalArgumentException("Generated Type Builder reference cannot be NULL!"); } final String enumerationName = parseToClassName(enumName); @@ -320,8 +331,7 @@ public final class TypeProviderImpl implements TypeProvider { } private void updateEnumPairsFromEnumTypeDef( - final EnumTypeDefinition enumTypeDef, - final EnumBuilder enumBuilder) { + final EnumTypeDefinition enumTypeDef, final EnumBuilder enumBuilder) { if (enumBuilder != null) { final List enums = enumTypeDef.getValues(); if (enums != null) { @@ -360,8 +370,7 @@ public final class TypeProviderImpl implements TypeProvider { private void resolveTypeDefsFromContext() { final Set modules = schemaContext.getModules(); if (modules == null) { - throw new IllegalArgumentException("Sef of Modules cannot be " + - "NULL!"); + throw new IllegalArgumentException("Sef of Modules cannot be NULL!"); } for (final Module module : modules) { if (module == null) { @@ -383,7 +392,8 @@ public final class TypeProviderImpl implements TypeProvider { final List extUnions = UnionDependencySort .sort(typeDefinitions); for (final ExtendedType extUnionType : extUnions) { - addUnionGeneratedTypeDefinition(basePackageName, extUnionType); + addUnionGeneratedTypeDefinition(basePackageName, + extUnionType); } } } @@ -394,7 +404,6 @@ public final class TypeProviderImpl implements TypeProvider { if ((basePackageName != null) && (moduleName != null) && (typedef != null) && (typedef.getQName() != null)) { - final String typedefName = typedef.getQName().getLocalName(); final TypeDefinition baseTypeDefinition = baseTypeDefForExtendedType(typedef); if (!(baseTypeDefinition instanceof LeafrefTypeDefinition) @@ -405,6 +414,11 @@ public final class TypeProviderImpl implements TypeProvider { returnType = resolveEnumFromTypeDefinition(enumTypeDef, typedefName); + } else if (baseTypeDefinition instanceof BitsTypeDefinition) { + final BitsTypeDefinition bitsTypeDefinition = (BitsTypeDefinition) baseTypeDefinition; + returnType = bitsTypedefToTransferObject(bitsTypeDefinition, + basePackageName, typedefName); + } else { final Type javaType = BaseYangTypes.BASE_YANG_TYPES_PROVIDER .javaTypeForSchemaDefinitionType(baseTypeDefinition); @@ -413,8 +427,8 @@ public final class TypeProviderImpl implements TypeProvider { javaType); } if (returnType != null) { - final Map typeMap = genTypeDefsContextMap.get - (moduleName); + final Map typeMap = genTypeDefsContextMap + .get(moduleName); if (typeMap != null) { typeMap.put(typedefName, returnType); } @@ -438,7 +452,7 @@ public final class TypeProviderImpl implements TypeProvider { final GeneratedPropertyBuilder genPropBuilder = genTOBuilder .addProperty(propertyName); - genPropBuilder.addReturnType(javaType); + genPropBuilder.setReturnType(javaType); genTOBuilder.addEqualsIdentity(genPropBuilder); genTOBuilder.addHashIdentity(genPropBuilder); genTOBuilder.addToStringProperty(genPropBuilder); @@ -450,26 +464,22 @@ public final class TypeProviderImpl implements TypeProvider { private void addUnionGeneratedTypeDefinition(final String basePackageName, final TypeDefinition typedef) { if (basePackageName == null) { - throw new IllegalArgumentException("Base Package Name cannot be " + - "NULL!"); + throw new IllegalArgumentException("Base Package Name cannot be NULL!"); } if (typedef == null) { - throw new IllegalArgumentException("Type Definition cannot be " + - "NULL!"); + throw new IllegalArgumentException("Type Definition cannot be NULL!"); } if (typedef.getQName() == null) { - throw new IllegalArgumentException("Type Definition cannot have " + - "non specified QName (QName cannot be NULL!)"); + throw new IllegalArgumentException("Type Definition cannot have non specified QName (QName cannot be NULL!)"); } final TypeDefinition baseTypeDefinition = typedef.getBaseType(); if ((baseTypeDefinition != null) && (baseTypeDefinition instanceof UnionTypeDefinition)) { final UnionTypeDefinition unionTypeDef = (UnionTypeDefinition) baseTypeDefinition; - final List> unionTypes = unionTypeDef - .getTypes(); - final Module parentModule = findParentModuleForTypeDefinition(schemaContext, - typedef); + final List> unionTypes = unionTypeDef.getTypes(); + final Module parentModule = findParentModuleForTypeDefinition( + schemaContext, typedef); Map genTOsMap = null; if (parentModule != null && parentModule.getName() != null) { @@ -480,17 +490,17 @@ public final class TypeProviderImpl implements TypeProvider { basePackageName, typedef); if ((unionTypes != null) && (unionGenTransObject != null)) { for (final TypeDefinition unionType : unionTypes) { - final String typeName = unionType.getQName() - .getLocalName(); + final String typeName = unionType.getQName().getLocalName(); if (unionType instanceof ExtendedType) { - final Module unionTypeModule = findParentModuleForTypeDefinition(schemaContext, - unionType); - if (unionTypeModule != null && unionTypeModule.getName() != null) { + final Module unionTypeModule = findParentModuleForTypeDefinition( + schemaContext, unionType); + if (unionTypeModule != null + && unionTypeModule.getName() != null) { final Map innerGenTOs = genTypeDefsContextMap .get(unionTypeModule.getName()); - final GeneratedTransferObject genTransferObject = - (GeneratedTransferObject) innerGenTOs.get(typeName); + final GeneratedTransferObject genTransferObject = (GeneratedTransferObject) innerGenTOs + .get(typeName); if (genTransferObject != null) { updateUnionTypeAsProperty(unionGenTransObject, genTransferObject, @@ -498,8 +508,7 @@ public final class TypeProviderImpl implements TypeProvider { } } } else if (unionType instanceof EnumTypeDefinition) { - final EnumBuilder - enumBuilder = resolveInnerEnumFromTypeDefinition( + final EnumBuilder enumBuilder = resolveInnerEnumFromTypeDefinition( (EnumTypeDefinition) unionType, typeName, unionGenTransObject); final Type enumRefType = new ReferencedTypeImpl( @@ -526,10 +535,9 @@ public final class TypeProviderImpl implements TypeProvider { final GeneratedTOBuilder unionGenTransObject, final Type type, final String propertyName) { if (unionGenTransObject != null && type != null) { - final GeneratedPropertyBuilder propBuilder = - unionGenTransObject.addProperty(parseToValidParamName( - propertyName)); - propBuilder.addReturnType(type); + final GeneratedPropertyBuilder propBuilder = unionGenTransObject + .addProperty(parseToValidParamName(propertyName)); + propBuilder.setReturnType(type); propBuilder.setReadOnly(false); if (!(type instanceof Enumeration)) { @@ -557,4 +565,35 @@ public final class TypeProviderImpl implements TypeProvider { } return null; } + + private GeneratedTransferObject bitsTypedefToTransferObject( + final BitsTypeDefinition bitsTypeDefinition, final String basePackageName, final String typedefName) { + + if (bitsTypeDefinition == null) { + throw new IllegalArgumentException("Bits TypeDefinition cannot be NULL!"); + } + if (basePackageName == null) { + throw new IllegalArgumentException("Base Package Name cannot be NULL!"); + } + if (typedefName == null) { + throw new IllegalArgumentException("Type Definition Local Name cannot be NULL!"); + } + + final String typeDefName = parseToClassName(typedefName); + final GeneratedTOBuilder genTOBuilder = new GeneratedTOBuilderImpl(basePackageName, typeDefName); + + final List bitList = bitsTypeDefinition.getBits(); + GeneratedPropertyBuilder genPropertyBuilder; + for (final Bit bit : bitList) { + String name = bit.getName(); + genPropertyBuilder = genTOBuilder.addProperty(parseToValidParamName(name)); + genPropertyBuilder.setReadOnly(false); + genPropertyBuilder.setReturnType(BaseYangTypes.BOOLEAN_TYPE); + + genTOBuilder.addEqualsIdentity(genPropertyBuilder); + genTOBuilder.addHashIdentity(genPropertyBuilder); + genTOBuilder.addToStringProperty(genPropertyBuilder); + } + return genTOBuilder.toInstance(); + } }