Refactored base yang-java types.
[yangtools.git] / code-generator / binding-generator-impl / src / main / java / org / opendaylight / yangtools / sal / binding / yang / types / TypeProviderImpl.java
index 4e6d2632e2f999531c8eeb12a59d0145d76f5a32..a119d4f155c40de1f2d099e98e7f73598c094dd0 100644 (file)
@@ -18,7 +18,6 @@ import java.util.Set;
 import java.util.TreeMap;
 
 import org.apache.commons.lang.StringEscapeUtils;
-import org.opendaylight.yangtools.binding.generator.util.ReferencedTypeImpl;
 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;
@@ -62,7 +61,8 @@ public final class TypeProviderImpl implements TypeProvider {
 
     /**
      * The outter map maps module names to the map of the types for the module.
-     * The inner map maps the name of the concrete type to the JAVA <code>Type</code> (usually it is generated TO).
+     * The inner map maps the name of the concrete type to the JAVA
+     * <code>Type</code> (usually it is generated TO).
      */
     private Map<String, Map<String, Type>> genTypeDefsContextMap;
 
@@ -73,7 +73,7 @@ public final class TypeProviderImpl implements TypeProvider {
 
     /**
      * Creates new instance of class <code>TypeProviderImpl</code>.
-     * 
+     *
      * @param schemaContext
      *            contains the schema data red from YANG files
      * @throws IllegalArgumentException
@@ -92,7 +92,7 @@ public final class TypeProviderImpl implements TypeProvider {
 
     /**
      * Puts <code>refType</code> to map with key <code>refTypePath</code>
-     * 
+     *
      * @param refTypePath
      *            schema path used as the map key
      * @param refType
@@ -102,7 +102,7 @@ public final class TypeProviderImpl implements TypeProvider {
      *             <li>if <code>refTypePath</code> equal null</li>
      *             <li>if <code>refType</code> equal null</li>
      *             </ul>
-     * 
+     *
      */
     public void putReferencedType(final SchemaPath refTypePath, final Type refType) {
         if (refTypePath == null) {
@@ -116,9 +116,9 @@ public final class TypeProviderImpl implements TypeProvider {
     }
 
     /**
-     * 
+     *
      * Converts basic YANG type <code>type</code> to JAVA <code>Type</code>.
-     * 
+     *
      * @param type
      *            string with YANG name of type
      * @returns JAVA <code>Type</code> for YANG type <code>type</code>
@@ -134,7 +134,7 @@ public final class TypeProviderImpl implements TypeProvider {
     /**
      * Converts schema definition type <code>typeDefinition</code> to JAVA
      * <code>Type</code>
-     * 
+     *
      * @param typeDefinition
      *            type definition which is converted to JAVA type
      * @throws IllegalArgumentException
@@ -207,11 +207,11 @@ public final class TypeProviderImpl implements TypeProvider {
      * Seeks for identity reference <code>idref</code> the JAVA
      * <code>type</code>.<br />
      * <br />
-     * 
+     *
      * <i>Example:<br />
      * If identy which is referenced via <code>idref</code> has name <b>Idn</b>
      * then returning type is <b>{@code Class<? extends Idn>}</b></i>
-     * 
+     *
      * @param idref
      *            identityref type definition for which JAVA <code>Type</code>
      *            is sought
@@ -243,7 +243,7 @@ public final class TypeProviderImpl implements TypeProvider {
 
     /**
      * Converts <code>typeDefinition</code> to concrete JAVA <code>Type</code>.
-     * 
+     *
      * @param typeDefinition
      *            type definition which should be converted to JAVA
      *            <code>Type</code>
@@ -290,7 +290,7 @@ public final class TypeProviderImpl implements TypeProvider {
     /**
      * Gets base type definition for <code>extendTypeDef</code>. The method is
      * recursivelly called until non <code>ExtendedType</code> type is found.
-     * 
+     *
      * @param extendTypeDef
      *            type definition for which is the base type definition sought
      * @return type definition which is base type for <code>extendTypeDef</code>
@@ -312,10 +312,10 @@ public final class TypeProviderImpl implements TypeProvider {
 
     /**
      * Converts <code>leafrefType</code> to JAVA <code>Type</code>.
-     * 
+     *
      * The path of <code>leafrefType</code> is followed to find referenced node
      * and its <code>Type</code> is returned.
-     * 
+     *
      * @param leafrefType
      *            leafref type definition for which is the type sought
      * @return JAVA <code>Type</code> of data schema node which is referenced in
@@ -325,7 +325,7 @@ public final class TypeProviderImpl implements TypeProvider {
      *             <li>if <code>leafrefType</code> equal null</li>
      *             <li>if path statement of <code>leafrefType</code> equal null</li>
      *             </ul>
-     * 
+     *
      */
     public Type provideTypeForLeafref(final LeafrefTypeDefinition leafrefType) {
         Type returnType = null;
@@ -369,7 +369,7 @@ public final class TypeProviderImpl implements TypeProvider {
     /**
      * Checks if <code>dataNode</code> is <code>LeafSchemaNode</code> and if it
      * so then checks if it is of type <code>EnumTypeDefinition</code>.
-     * 
+     *
      * @param dataNode
      *            data schema node for which is checked if it is leaf and if it
      *            is of enum type
@@ -392,7 +392,7 @@ public final class TypeProviderImpl implements TypeProvider {
     /**
      * Checks if <code>dataNode</code> is <code>LeafListSchemaNode</code> and if
      * it so then checks if it is of type <code>EnumTypeDefinition</code>.
-     * 
+     *
      * @param dataNode
      *            data schema node for which is checked if it is leaflist and if
      *            it is of enum type
@@ -417,7 +417,7 @@ public final class TypeProviderImpl implements TypeProvider {
      * Converts <code>enumTypeDef</code> to
      * {@link org.opendaylight.yangtools.sal.binding.model.api.Enumeration
      * enumeration}.
-     * 
+     *
      * @param enumTypeDef
      *            enumeration type definition which is converted to enumeration
      * @param enumName
@@ -459,7 +459,7 @@ public final class TypeProviderImpl implements TypeProvider {
     /**
      * Adds enumeration to <code>typeBuilder</code>. The enumeration data are
      * taken from <code>enumTypeDef</code>.
-     * 
+     *
      * @param enumTypeDef
      *            enumeration type definition is source of enumeration data for
      *            <code>typeBuilder</code>
@@ -477,7 +477,7 @@ public final class TypeProviderImpl implements TypeProvider {
      *             <li>if name of <code>enumTypeDef</code> equal null</li>
      *             <li>if name of <code>typeBuilder</code> equal null</li>
      *             </ul>
-     * 
+     *
      */
     private Enumeration addInnerEnumerationToTypeBuilder(final EnumTypeDefinition enumTypeDef, final String enumName,
             final GeneratedTypeBuilder typeBuilder) {
@@ -507,7 +507,7 @@ public final class TypeProviderImpl implements TypeProvider {
     /**
      * Updates <code>enumBuilder</code> with data from <code>enumTypeDef</code>.
      * Specifically this data represents list of value-name pairs.
-     * 
+     *
      * @param enumTypeDef
      *            enum type definition as source of enum data for
      *            <code>enumBuilder</code>
@@ -538,7 +538,7 @@ public final class TypeProviderImpl implements TypeProvider {
 
     /**
      * Converts <code>dataNode</code> to JAVA <code>Type</code>.
-     * 
+     *
      * @param dataNode
      *            contains information about YANG type
      * @return JAVA <code>Type</code> representation of <code>dataNode</code>
@@ -560,7 +560,7 @@ public final class TypeProviderImpl implements TypeProvider {
     /**
      * Passes through all modules and through all its type definitions and
      * convert it to generated types.
-     * 
+     *
      * The modules are firstly sorted by mutual dependencies. The modules are
      * sequentially passed. All type definitions of a module are at the
      * beginning sorted so that type definition with less amount of references
@@ -569,7 +569,7 @@ public final class TypeProviderImpl implements TypeProvider {
      * {@link TypeProviderImpl#genTypeDefsContextMap genTypeDefsContextMap}
      * which map current module name to the map which maps type names to
      * returned types (generated types).
-     * 
+     *
      */
     private void resolveTypeDefsFromContext() {
         final Set<Module> modules = schemaContext.getModules();
@@ -605,7 +605,7 @@ public final class TypeProviderImpl implements TypeProvider {
     }
 
     /**
-     * 
+     *
      * @param basePackageName
      *            string with name of package to which the module belongs
      * @param moduleName
@@ -632,8 +632,9 @@ public final class TypeProviderImpl implements TypeProvider {
                     ExtendedType innerExtendedType = (ExtendedType) innerTypeDefinition;
                     returnType = provideGeneratedTOFromExtendedType(innerExtendedType, basePackageName, typedefName);
                 } else if (innerTypeDefinition instanceof UnionTypeDefinition) {
+                    final Module parentModule = findParentModuleForTypeDefinition(schemaContext, typedef);
                     final GeneratedTOBuilder genTOBuilder = provideGeneratedTOBuilderForUnionTypeDefinition(
-                            basePackageName, typedef, typedefName);
+                            basePackageName, typedef, typedefName, parentModule);
                     returnType = genTOBuilder.toInstance();
                 } else if (innerTypeDefinition instanceof EnumTypeDefinition) {
                     final EnumTypeDefinition enumTypeDef = (EnumTypeDefinition) innerTypeDefinition;
@@ -665,7 +666,7 @@ public final class TypeProviderImpl implements TypeProvider {
 
     /**
      * Wraps base YANG type to generated TO.
-     * 
+     *
      * @param basePackageName
      *            string with name of package to which the module belongs
      * @param typedef
@@ -703,7 +704,7 @@ public final class TypeProviderImpl implements TypeProvider {
      * Converts <code>typedef</code> to generated TO with
      * <code>typeDefName</code>. Every union type from <code>typedef</code> is
      * added to generated TO builder as property.
-     * 
+     *
      * @param basePackageName
      *            string with name of package to which the module belongs
      * @param typedef
@@ -720,7 +721,7 @@ public final class TypeProviderImpl implements TypeProvider {
      *             </ul>
      */
     public GeneratedTOBuilder provideGeneratedTOBuilderForUnionTypeDefinition(final String basePackageName,
-            final TypeDefinition<?> typedef, String typeDefName) {
+            final TypeDefinition<?> typedef, final String typeDefName, final Module parentModule) {
         if (basePackageName == null) {
             throw new IllegalArgumentException("Base Package Name cannot be NULL!");
         }
@@ -791,7 +792,8 @@ public final class TypeProviderImpl implements TypeProvider {
             }
 
             Map<String, Type> genTOsMap = null;
-            final Module parentModule = findParentModuleForTypeDefinition(schemaContext, typedef);
+            // final Module parentModule =
+            // findParentModuleForTypeDefinition(schemaContext, typedef);
             if (parentModule != null && parentModule.getName() != null) {
                 genTOsMap = genTypeDefsContextMap.get(parentModule.getName());
                 genTOsMap.put(typedef.getQName().getLocalName(), unionGenTransObject.toInstance());
@@ -805,7 +807,7 @@ public final class TypeProviderImpl implements TypeProvider {
     /**
      * Adds a new property with the name <code>propertyName</code> and with type
      * <code>type</code> to <code>unonGenTransObject</code>.
-     * 
+     *
      * @param unionGenTransObject
      *            generated TO to which should be property added
      * @param type
@@ -832,7 +834,7 @@ public final class TypeProviderImpl implements TypeProvider {
 
     /**
      * Converts <code>typedef</code> to the generated TO builder.
-     * 
+     *
      * @param basePackageName
      *            string with name of package to which the module belongs
      * @param typedef
@@ -857,10 +859,10 @@ public final class TypeProviderImpl implements TypeProvider {
     /**
      * Converts <code>typeDef</code> which should be of the type
      * <code>BitsTypeDefinition</code> to <code>GeneratedTOBuilder</code>.
-     * 
+     *
      * All the bits of the typeDef are added to returning generated TO as
      * properties.
-     * 
+     *
      * @param basePackageName
      *            string with name of package to which the module belongs
      * @param typeDef
@@ -911,13 +913,13 @@ public final class TypeProviderImpl implements TypeProvider {
     /**
      * Converts the pattern constraints from <code>typedef</code> to the list of
      * the strings which represents these constraints.
-     * 
+     *
      * @param typedef
      *            extended type in which are the pattern constraints sought
      * @return list of strings which represents the constraint patterns
      * @throws IllegalArgumentException
      *             if <code>typedef</code> equals null
-     * 
+     *
      */
     private List<String> resolveRegExpressionsFromTypedef(ExtendedType typedef) {
         final List<String> regExps = new ArrayList<String>();
@@ -941,10 +943,10 @@ public final class TypeProviderImpl implements TypeProvider {
     }
 
     /**
-     * 
+     *
      * Adds to the <code>genTOBuilder</code> the constant which contains regular
      * expressions from the <code>regularExpressions</code>
-     * 
+     *
      * @param genTOBuilder
      *            generated TO builder to which are
      *            <code>regular expressions</code> added
@@ -973,11 +975,11 @@ public final class TypeProviderImpl implements TypeProvider {
      * <code>innerExtendedType</code>, about the package name
      * <code>typedefName</code> and about the generated TO name
      * <code>typedefName</code>.
-     * 
+     *
      * It is supposed that <code>innerExtendedType</code> is already present in
      * {@link TypeProviderImpl#genTypeDefsContextMap genTypeDefsContextMap} to
      * be possible set it as extended type for the returning generated TO.
-     * 
+     *
      * @param innerExtendedType
      *            extended type which is part of some other extended type
      * @param basePackageName
@@ -1033,7 +1035,7 @@ public final class TypeProviderImpl implements TypeProvider {
      * equal depth. In next step are lists from this map concatenated to one
      * list in ascending order according to their depth. All type definitions
      * are in the list behind all type definitions on which depends.
-     * 
+     *
      * @param unsortedTypeDefinitions
      *            list of type definitions which should be sorted by depth
      * @return list of type definitions sorted according their each other
@@ -1067,7 +1069,7 @@ public final class TypeProviderImpl implements TypeProvider {
     /**
      * Returns how many immersion is necessary to get from the type definition
      * to the base type.
-     * 
+     *
      * @param typeDefinition
      *            type definition for which is depth sought.
      * @return number of immersions which are necessary to get from the type