Remove TypeProvider.javaTypeForYangType()
[mdsal.git] / binding / mdsal-binding-generator-impl / src / main / java / org / opendaylight / mdsal / binding / yang / types / AbstractTypeProvider.java
index d0dbc593e875ae7f224aa2095cb24bccdcce055e..74a65c8c39e964c3823a626f327fbd91f15be600 100644 (file)
@@ -7,6 +7,7 @@
  */
 package org.opendaylight.mdsal.binding.yang.types;
 
+import static java.util.Objects.requireNonNull;
 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;
@@ -52,7 +53,7 @@ import org.opendaylight.mdsal.binding.model.util.TypeConstants;
 import org.opendaylight.mdsal.binding.model.util.Types;
 import org.opendaylight.mdsal.binding.model.util.generated.type.builder.AbstractEnumerationBuilder;
 import org.opendaylight.mdsal.binding.model.util.generated.type.builder.GeneratedPropertyBuilderImpl;
-import org.opendaylight.yangtools.yang.binding.BindingMapping;
+import org.opendaylight.mdsal.binding.spec.naming.BindingMapping;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.common.Revision;
 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
@@ -113,19 +114,22 @@ public abstract class AbstractTypeProvider implements TypeProvider {
      */
     private final Map<SchemaPath, Type> referencedTypes = new HashMap<>();
     private final Map<Module, Set<Type>> additionalTypes = new HashMap<>();
+    private final Map<SchemaNode, JavaTypeName> renames;
 
     /**
      * Creates new instance of class <code>TypeProviderImpl</code>.
      *
      * @param schemaContext
      *            contains the schema data red from YANG files
+     * @param renames
      * @throws IllegalArgumentException
      *             if <code>schemaContext</code> equal null.
      */
-    AbstractTypeProvider(final SchemaContext schemaContext) {
+    AbstractTypeProvider(final SchemaContext schemaContext, final Map<SchemaNode, JavaTypeName> renames) {
         Preconditions.checkArgument(schemaContext != null, "Schema Context cannot be null!");
 
         this.schemaContext = schemaContext;
+        this.renames = requireNonNull(renames);
         resolveTypeDefsFromContext();
     }
 
@@ -154,21 +158,6 @@ public abstract class AbstractTypeProvider implements TypeProvider {
         return additionalTypes;
     }
 
-    /**
-     *
-     * Converts basic YANG type <code>type</code> to JAVA <code>Type</code>.
-     *
-     * @param type
-     *            string with YANG name of type
-     * @return JAVA <code>Type</code> for YANG type <code>type</code>
-     * @see TypeProvider#javaTypeForYangType(String)
-     */
-    @Override
-    @Deprecated
-    public Type javaTypeForYangType(final String type) {
-        return BaseYangTypes.BASE_YANG_TYPES_PROVIDER.javaTypeForYangType(type);
-    }
-
     @Override
     public Type javaTypeForSchemaDefinitionType(final TypeDefinition<?> typeDefinition, final SchemaNode parentNode) {
         return javaTypeForSchemaDefinitionType(typeDefinition, parentNode, null);
@@ -216,7 +205,7 @@ public abstract class AbstractTypeProvider implements TypeProvider {
             }
 
             // FIXME: it looks as though we could be using the same codepath as above...
-            ret = BaseYangTypes.BASE_YANG_TYPES_PROVIDER.javaTypeForYangType(typeDefinition.getQName().getLocalName());
+            ret = BaseYangTypes.javaTypeForYangType(typeDefinition.getQName().getLocalName());
             if (ret == null) {
                 LOG.debug("Failed to resolve Java type for {}", typeDefinition);
             }
@@ -394,8 +383,7 @@ public abstract class AbstractTypeProvider implements TypeProvider {
         final String basePackageName = BindingMapping.getRootPackageName(module.getQNameModule());
         final JavaTypeName identifier = JavaTypeName.create(BindingGeneratorUtil.packageNameForGeneratedType(basePackageName,
             identity.getPath()), BindingMapping.getClassName(identity.getQName()));
-        final Type paramType = Types.wildcardTypeFor(identifier);
-        return Types.parameterizedTypeFor(Types.typeForClass(Class.class), paramType);
+        return Types.classType(Types.wildcardTypeFor(identifier));
     }
 
     /**
@@ -550,15 +538,17 @@ public abstract class AbstractTypeProvider implements TypeProvider {
                 Preconditions.checkArgument(dataNode != null, "Failed to find leafref target: %s in module %s (%s)",
                         strXPath, this.getParentModule(parentNode).getName(), parentNode.getQName().getModule());
 
+                // FIXME: this block seems to be some weird magic hack. Analyze and refactor it.
                 if (leafContainsEnumDefinition(dataNode)) {
                     returnType = referencedTypes.get(dataNode.getPath());
                 } else if (leafListContainsEnumDefinition(dataNode)) {
                     returnType = Types.listTypeFor(referencedTypes.get(dataNode.getPath()));
-                } else {
+                }
+                if (returnType == null) {
                     returnType = resolveTypeFromDataSchemaNode(dataNode);
                 }
             } else {
-                returnType = Types.typeForClass(Object.class);
+                returnType = Types.objectType();
             }
         }
         Preconditions.checkArgument(returnType != null, "Failed to find leafref target: %s in module %s (%s)",
@@ -895,11 +885,10 @@ public abstract class AbstractTypeProvider implements TypeProvider {
     private 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";
 
         final GeneratedTOBuilder genTOBuilder = typedefToTransferObject(basePackageName, typedef, moduleName);
         genTOBuilder.setRestrictions(BindingGeneratorUtil.getRestrictions(typedef));
-        final GeneratedPropertyBuilder genPropBuilder = genTOBuilder.addProperty(propertyName);
+        final GeneratedPropertyBuilder genPropBuilder = genTOBuilder.addProperty("value");
         genPropBuilder.setReturnType(javaType);
         genTOBuilder.addEqualsIdentity(genPropBuilder);
         genTOBuilder.addHashIdentity(genPropBuilder);
@@ -932,8 +921,6 @@ public abstract class AbstractTypeProvider implements TypeProvider {
 
         final GeneratedTOBuilder resultTOBuilder = builders.remove(0);
         builders.forEach(resultTOBuilder::addEnclosingTransferObject);
-
-        resultTOBuilder.addProperty("value").setReturnType(Types.CHAR_ARRAY);
         return resultTOBuilder;
     }
 
@@ -964,12 +951,11 @@ public abstract class AbstractTypeProvider implements TypeProvider {
         final Module module = findParentModule(schemaContext, parentNode);
 
         final GeneratedTOBuilder unionGenTOBuilder = newGeneratedTOBuilder(typeName);
+        unionGenTOBuilder.setIsUnion(true);
         unionGenTOBuilder.setSchemaPath(typedef.getPath());
         unionGenTOBuilder.setModuleName(module.getName());
         addCodegenInformation(unionGenTOBuilder, typedef);
-
         generatedTOBuilders.add(unionGenTOBuilder);
-        unionGenTOBuilder.setIsUnion(true);
 
         // Pattern string is the key, XSD regex is the value. The reason for this choice is that the pattern carries
         // also negation information and hence guarantees uniqueness.
@@ -1029,7 +1015,7 @@ public abstract class AbstractTypeProvider implements TypeProvider {
         final GeneratedPropertyBuilder propertyBuilder;
         propertyBuilder = parentUnionGenTOBuilder.addProperty(BindingMapping.getPropertyName(
             newTOBuilderName.simpleName()));
-        propertyBuilder.setReturnType(subUnionGenTOBUilders.get(0));
+        propertyBuilder.setReturnType(subUnionGenTOBUilders.get(0).build());
         parentUnionGenTOBuilder.addEqualsIdentity(propertyBuilder);
         parentUnionGenTOBuilder.addToStringProperty(propertyBuilder);
 
@@ -1067,7 +1053,7 @@ public abstract class AbstractTypeProvider implements TypeProvider {
         final TypeDefinition<?> baseType = baseTypeDefForExtendedType(unionSubtype);
         if (unionTypeName.equals(baseType.getQName().getLocalName())) {
             final Type javaType = BaseYangTypes.BASE_YANG_TYPES_PROVIDER.javaTypeForSchemaDefinitionType(baseType,
-                parentNode);
+                parentNode, BindingGeneratorUtil.getRestrictions(unionSubtype));
             if (javaType != null) {
                 updateUnionTypeAsProperty(parentUnionGenTOBuilder, javaType, unionTypeName);
             }
@@ -1171,9 +1157,14 @@ public abstract class AbstractTypeProvider implements TypeProvider {
      */
     private GeneratedTOBuilder typedefToTransferObject(final String basePackageName,
             final TypeDefinition<?> typedef, final String moduleName) {
-        final GeneratedTOBuilder newType = newGeneratedTOBuilder(JavaTypeName.create(
-            BindingGeneratorUtil.packageNameForGeneratedType(basePackageName, typedef.getPath()),
-            BindingMapping.getClassName(typedef.getQName().getLocalName())));
+        JavaTypeName name = renames.get(typedef);
+        if (name == null) {
+            name = JavaTypeName.create(
+                BindingGeneratorUtil.packageNameForGeneratedType(basePackageName, typedef.getPath()),
+                BindingMapping.getClassName(typedef.getQName().getLocalName()));
+        }
+
+        final GeneratedTOBuilder newType = newGeneratedTOBuilder(name);
         newType.setSchemaPath(typedef.getPath());
         newType.setModuleName(moduleName);
         addCodegenInformation(newType, typedef);
@@ -1347,11 +1338,8 @@ public abstract class AbstractTypeProvider implements TypeProvider {
         final Map<Integer, List<TypeDefinition<?>>> typeDefinitionsDepths = new TreeMap<>();
         for (TypeDefinition<?> unsortedTypeDefinition : unsortedTypeDefinitions) {
             final Integer depth = getTypeDefinitionDepth(unsortedTypeDefinition);
-            List<TypeDefinition<?>> typeDefinitionsConcreteDepth = typeDefinitionsDepths.get(depth);
-            if (typeDefinitionsConcreteDepth == null) {
-                typeDefinitionsConcreteDepth = new ArrayList<>();
-                typeDefinitionsDepths.put(depth, typeDefinitionsConcreteDepth);
-            }
+            List<TypeDefinition<?>> typeDefinitionsConcreteDepth =
+                typeDefinitionsDepths.computeIfAbsent(depth, k -> new ArrayList<>());
             typeDefinitionsConcreteDepth.add(unsortedTypeDefinition);
         }
 
@@ -1576,11 +1564,11 @@ public abstract class AbstractTypeProvider implements TypeProvider {
         return sb.toString();
     }
 
-    private static final Comparator<Bit> BIT_NAME_COMPARATOR = (o1, o2) -> o1.getName().compareTo(o2.getName());
+    private static final Comparator<Bit> BIT_NAME_COMPARATOR = Comparator.comparing(Bit::getName);
 
     private static String bitsToDef(final BitsTypeDefinition type, final String className, final String defaultValue, final boolean isExt) {
         final List<Bit> bits = new ArrayList<>(type.getBits());
-        Collections.sort(bits, BIT_NAME_COMPARATOR);
+        bits.sort(BIT_NAME_COMPARATOR);
         final StringBuilder sb = new StringBuilder();
         if (!isExt) {
             sb.append("new ");
@@ -1655,7 +1643,7 @@ public abstract class AbstractTypeProvider implements TypeProvider {
                 }
                 if (module == null) {
                     final List<Module> modulesList = new ArrayList<>(modules);
-                    Collections.sort(modulesList, (o1, o2) -> Revision.compare(o1.getRevision(), o2.getRevision()));
+                    modulesList.sort((o1, o2) -> Revision.compare(o1.getRevision(), o2.getRevision()));
                     module = modulesList.get(0);
                 }
             } else {