Move a few more tests
[mdsal.git] / binding / mdsal-binding-generator / src / main / java / org / opendaylight / mdsal / binding / yang / types / AbstractTypeProvider.java
index 73fa9bf7bb0dd8e79f662393542c796b6be11b69..3da39a1fd8c309a882935b896468eac231b895ff 100644 (file)
@@ -70,7 +70,7 @@ import org.opendaylight.yangtools.yang.model.spi.ModuleDependencySort;
 
 // FIXME: remove this class
 @Deprecated(forRemoval = true)
-abstract class AbstractTypeProvider implements TypeProvider {
+abstract class AbstractTypeProvider {
     private static final JavaTypeName DEPRECATED_ANNOTATION = JavaTypeName.create(Deprecated.class);
     private static final CharMatcher DASH_COLON_MATCHER = CharMatcher.anyOf("-:");
 
@@ -81,11 +81,6 @@ abstract class AbstractTypeProvider implements TypeProvider {
 
     private final Map<String, Map<Optional<Revision>, Map<String, GeneratedType>>> genTypeDefsContextMap =
         new HashMap<>();
-
-    /**
-     * The map which maps schema paths to JAVA <code>Type</code>.
-     */
-    private final Map<SchemaPath, Type> referencedTypes = new HashMap<>();
     private final Map<Module, Set<GeneratedType>> additionalTypes = new HashMap<>();
 
     /**
@@ -101,33 +96,32 @@ abstract class AbstractTypeProvider implements TypeProvider {
         resolveTypeDefsFromContext();
     }
 
-    /**
-     * Puts <code>refType</code> to map with key <code>refTypePath</code>.
-     *
-     * @param refTypePath schema path used as the map key
-     * @param refType type which represents the map value
-     * @throws IllegalArgumentException
-     *             <ul>
-     *             <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) {
-        Preconditions.checkArgument(refTypePath != null,
-                "Path reference of Enumeration Type Definition cannot be NULL!");
-        Preconditions.checkArgument(refType != null, "Reference to Enumeration Type cannot be NULL!");
-        referencedTypes.put(refTypePath, refType);
-    }
-
     public Map<Module, Set<GeneratedType>> getAdditionalTypes() {
         return additionalTypes;
     }
 
-    @Override
-    public Type javaTypeForSchemaDefinitionType(final TypeDefinition<?> typeDefinition, final SchemaNode parentNode,
-            final boolean lenientRelativeLeafrefs) {
-        return javaTypeForSchemaDefinitionType(typeDefinition, parentNode, null, lenientRelativeLeafrefs);
+    /**
+     * Resolve of YANG Type Definition to it's java counter part. If the Type Definition contains one of YANG primitive
+     * types the method will return {@code java.lang.} counterpart. (For example if YANG type is int32 the Java
+     * counterpart is {@link Integer}). In case that Type Definition contains extended type defined via YANG typedef
+     * statement the method SHOULD return Generated Type or Generated Transfer Object if that Type is correctly
+     * referenced to resolved imported YANG module.
+     *
+     * <p>
+     * The method will return <code>null</code> value in situations that TypeDefinition can't be resolved (either due
+     * to missing YANG import or incorrectly specified type).
+     *
+     * <p>
+     * {@code leafref} resolution for relative paths has two models of operation: lenient and strict. This is needed to
+     * handle the case where a grouping leaf's path points outside of the grouping tree. In such a case we cannot
+     * completely determine the correct type and need to fallback to {@link Object}.
+     *
+     * @param type Type Definition to resolve from
+     * @param lenientRelativeLeafrefs treat relative leafrefs leniently
+     * @return Resolved Type
+     */
+    public Type javaTypeForSchemaDefinitionType(final TypeDefinition<?> type, final SchemaNode parentNode) {
+        return javaTypeForSchemaDefinitionType(type, parentNode, null);
     }
 
     /**
@@ -141,9 +135,8 @@ abstract class AbstractTypeProvider implements TypeProvider {
      *             <li>if name of <code>typeDefinition</code> equal null</li>
      *             </ul>
      */
-    @Override
     public Type javaTypeForSchemaDefinitionType(final TypeDefinition<?> typeDefinition, final SchemaNode parentNode,
-            final Restrictions restrictions, final boolean lenientRelativeLeafrefs) {
+            final Restrictions restrictions) {
         throw new UnsupportedOperationException();
     }
 
@@ -437,8 +430,8 @@ abstract class AbstractTypeProvider implements TypeProvider {
 
             // Define a corresponding union builder. Typedefs are always anchored at a Java package root,
             // so we are placing the builder alongside the union.
-            final GeneratedTOBuilder unionBuilder = newGeneratedTOBuilder(
-                JavaTypeName.create(genTOBuilder.getPackageName(), genTOBuilder.getName() + "Builder"));
+            final GeneratedTOBuilder unionBuilder = newGeneratedTOBuilder(JavaTypeName.create(
+                genTOBuilder.getPackageName(), genTOBuilder.getName() + BindingMapping.BUILDER_SUFFIX));
             unionBuilder.setIsUnionBuilder(true);
             final MethodSignatureBuilder method = unionBuilder.addMethod("getDefaultInstance");
             method.setReturnType(returnType);
@@ -645,7 +638,7 @@ abstract class AbstractTypeProvider implements TypeProvider {
 
         final TypeDefinition<?> baseType = baseTypeDefForExtendedType(unionSubtype);
         if (unionTypeName.equals(baseType.getQName().getLocalName())) {
-            final Type javaType = BaseYangTypesProvider.INSTANCE.javaTypeForSchemaDefinitionType(baseType, parentNode,
+            final Type javaType = baseJavaTypeForSchema(baseType, parentNode,
                 BindingGeneratorUtil.getRestrictions(unionSubtype));
             if (javaType != null) {
                 updateUnionTypeAsProperty(parentUnionGenTOBuilder, javaType, unionTypeName);
@@ -670,6 +663,13 @@ abstract class AbstractTypeProvider implements TypeProvider {
         }
     }
 
+    private static Type baseJavaTypeForSchema(final TypeDefinition<?> type, final SchemaNode parentNode,
+            final Restrictions restrictions) {
+        final String typeName = type.getQName().getLocalName();
+        final Type mapped = BaseYangTypes.javaTypeForYangType(typeName);
+        return mapped == null || restrictions == null ? mapped : Types.restrictedType(mapped, restrictions);
+    }
+
     /**
      * Searches for generated TO for <code>searchedTypeDef</code> type  definition
      * in {@link #genTypeDefsContextMap genTypeDefsContextMap}.