Speed up annotations a bit
[mdsal.git] / binding / mdsal-binding-generator-impl / src / main / java / org / opendaylight / mdsal / binding / yang / types / AbstractTypeProvider.java
index f2a93739828d9276be1b91dab2e4d844b7c791e3..8e53b58a023e886310f32f272d4fe00910ec5057 100644 (file)
@@ -34,6 +34,7 @@ import java.util.Set;
 import java.util.TreeMap;
 import java.util.regex.Pattern;
 import org.opendaylight.mdsal.binding.generator.spi.TypeProvider;
+import org.opendaylight.mdsal.binding.generator.util.BaseYangTypesProvider;
 import org.opendaylight.mdsal.binding.model.api.AccessModifier;
 import org.opendaylight.mdsal.binding.model.api.ConcreteType;
 import org.opendaylight.mdsal.binding.model.api.Enumeration;
@@ -48,7 +49,9 @@ import org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedTOBuilder;
 import org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedTypeBuilder;
 import org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedTypeBuilderBase;
 import org.opendaylight.mdsal.binding.model.api.type.builder.MethodSignatureBuilder;
+import org.opendaylight.mdsal.binding.model.util.BaseYangTypes;
 import org.opendaylight.mdsal.binding.model.util.BindingGeneratorUtil;
+import org.opendaylight.mdsal.binding.model.util.BindingTypes;
 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;
@@ -97,6 +100,7 @@ import org.slf4j.LoggerFactory;
 public abstract class AbstractTypeProvider implements TypeProvider {
     private static final Logger LOG = LoggerFactory.getLogger(AbstractTypeProvider.class);
     private static final Pattern GROUPS_PATTERN = Pattern.compile("\\[(.*?)\\]");
+    private static final JavaTypeName DEPRECATED_ANNOTATION = JavaTypeName.create(Deprecated.class);
 
     // 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.
@@ -186,7 +190,7 @@ public abstract class AbstractTypeProvider implements TypeProvider {
             // 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,
+                final Type ret = BaseYangTypesProvider.INSTANCE.javaTypeForSchemaDefinitionType(typeDefinition,
                     parentNode, restrictions, lenientRelativeLeafrefs);
                 if (ret != null) {
                     return ret;
@@ -224,6 +228,17 @@ public abstract class AbstractTypeProvider implements TypeProvider {
         return returnType;
     }
 
+    public SchemaNode getTargetForLeafref(final LeafrefTypeDefinition leafrefType, final SchemaNode parentNode) {
+        final PathExpression xpath = leafrefType.getPathStatement();
+        Preconditions.checkArgument(xpath != null, "The Path Statement for Leafref Type Definition cannot be NULL!");
+
+        final Module module = findParentModule(schemaContext, parentNode);
+        Preconditions.checkArgument(module != null, "Failed to find module for parent %s", parentNode);
+
+        return xpath.isAbsolute() ? findDataTreeSchemaNode(schemaContext, module.getQNameModule(), xpath)
+                : findDataSchemaNodeForRelativeXPath(schemaContext, module, parentNode, xpath);
+    }
+
     private GeneratedTransferObject shadedTOWithRestrictions(final GeneratedTransferObject gto,
             final Restrictions restrictions) {
         final GeneratedTOBuilder gtob = newGeneratedTOBuilder(gto.getIdentifier());
@@ -330,8 +345,8 @@ public abstract class AbstractTypeProvider implements TypeProvider {
                         returnType = genTOs.get(typedefName);
                     }
                     if (returnType == null) {
-                        returnType = BaseYangTypes.BASE_YANG_TYPES_PROVIDER.javaTypeForSchemaDefinitionType(
-                                baseTypeDef, typeDefinition, r, lenient);
+                        returnType = BaseYangTypesProvider.INSTANCE.javaTypeForSchemaDefinitionType(baseTypeDef,
+                            typeDefinition, r, lenient);
                     }
                 }
             }
@@ -560,7 +575,7 @@ public abstract class AbstractTypeProvider implements TypeProvider {
             returnType = Types.listTypeFor(referencedTypes.get(dataNode.getPath()));
         }
         if (returnType == null) {
-            returnType = resolveTypeFromDataSchemaNode(dataNode);
+            returnType = resolveTypeFromDataSchemaNode(dataNode, inGrouping);
         }
         Preconditions.checkArgument(returnType != null, "Failed to find leafref target: %s in module %s (%s)",
                 strXPath, this.getParentModule(parentNode).getName(), parentNode.getQName().getModule(), this);
@@ -716,16 +731,16 @@ public abstract class AbstractTypeProvider implements TypeProvider {
      * @param dataNode contains information about YANG type
      * @return JAVA <code>Type</code> representation of <code>dataNode</code>
      */
-    private Type resolveTypeFromDataSchemaNode(final SchemaNode dataNode) {
+    private Type resolveTypeFromDataSchemaNode(final SchemaNode dataNode, final boolean inGrouping) {
         Type returnType = null;
         if (dataNode != null) {
             if (dataNode instanceof LeafSchemaNode) {
                 final LeafSchemaNode leaf = (LeafSchemaNode) dataNode;
                 final TypeDefinition<?> type = CompatUtils.compatType(leaf);
-                returnType = javaTypeForSchemaDefinitionType(type, leaf);
+                returnType = javaTypeForSchemaDefinitionType(type, leaf, inGrouping);
             } else if (dataNode instanceof LeafListSchemaNode) {
                 final LeafListSchemaNode leafList = (LeafListSchemaNode) dataNode;
-                returnType = javaTypeForSchemaDefinitionType(leafList.getType(), leafList);
+                returnType = javaTypeForSchemaDefinitionType(leafList.getType(), leafList, inGrouping);
             }
         }
         return returnType;
@@ -861,14 +876,15 @@ public abstract class AbstractTypeProvider implements TypeProvider {
 
         final GeneratedTOBuilder genTOBuilder = typedefToTransferObject(basePackageName, typedef, moduleName);
         genTOBuilder.setRestrictions(BindingGeneratorUtil.getRestrictions(typedef));
-        final GeneratedPropertyBuilder genPropBuilder = genTOBuilder.addProperty("value");
+        final GeneratedPropertyBuilder genPropBuilder = genTOBuilder.addProperty(TypeConstants.VALUE_PROP);
         genPropBuilder.setReturnType(javaType);
+
         genTOBuilder.addEqualsIdentity(genPropBuilder);
         genTOBuilder.addHashIdentity(genPropBuilder);
         genTOBuilder.addToStringProperty(genPropBuilder);
-        genTOBuilder.addImplementsType(TYPE_OBJECT);
+        genTOBuilder.addImplementsType(BindingTypes.scalarTypeObject(javaType));
         if (typedef.getStatus() == Status.DEPRECATED) {
-            genTOBuilder.addAnnotation("java.lang", "Deprecated");
+            genTOBuilder.addAnnotation(DEPRECATED_ANNOTATION);
         }
         if (javaType instanceof ConcreteType && "String".equals(javaType.getName()) && typedef.getBaseType() != null) {
             addStringRegExAsConstant(genTOBuilder, resolveRegExpressionsFromTypedef(typedef));
@@ -1011,8 +1027,8 @@ 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, BindingGeneratorUtil.getRestrictions(unionSubtype));
+            final Type javaType = BaseYangTypesProvider.INSTANCE.javaTypeForSchemaDefinitionType(baseType, parentNode,
+                BindingGeneratorUtil.getRestrictions(unionSubtype));
             if (javaType != null) {
                 updateUnionTypeAsProperty(parentUnionGenTOBuilder, javaType, unionTypeName);
             }
@@ -1211,7 +1227,7 @@ public abstract class AbstractTypeProvider implements TypeProvider {
         addStringRegExAsConstant(genTOBuilder, resolveRegExpressionsFromTypedef(typedef));
 
         if (typedef.getStatus() == Status.DEPRECATED) {
-            genTOBuilder.addAnnotation("java.lang", "Deprecated");
+            genTOBuilder.addAnnotation(DEPRECATED_ANNOTATION);
         }
 
         if (baseTypeDefForExtendedType(innerExtendedType) instanceof UnionTypeDefinition) {
@@ -1591,18 +1607,18 @@ public abstract class AbstractTypeProvider implements TypeProvider {
     }
 
     private static String union(final String className, final String defaultValue, final LeafSchemaNode node) {
-        final StringBuilder sb = new StringBuilder();
-        sb.append("new ");
-        sb.append(className);
-        sb.append("(\"");
-        sb.append(defaultValue);
-        sb.append("\".toCharArray())");
-        return sb.toString();
+        return new StringBuilder()
+                .append("new ")
+                .append(className)
+                .append("(\"")
+                .append(defaultValue)
+                .append("\".toCharArray())")
+                .toString();
     }
 
     @Override
     public String getConstructorPropertyName(final SchemaNode node) {
-        return node instanceof TypeDefinition<?> ? "value" : "";
+        return node instanceof TypeDefinition<?> ? TypeConstants.VALUE_PROP : "";
     }
 
     @Override