Lookup leaf key methods in parents
[mdsal.git] / binding / mdsal-binding-generator-impl / src / main / java / org / opendaylight / mdsal / binding / generator / impl / BindingGeneratorImpl.java
index 6f64e70d158796339f4d1a79701a507cd813c35f..3c70087d56c1c48097cc988826ab027fdde5b8b8 100644 (file)
@@ -210,7 +210,6 @@ public class BindingGeneratorImpl implements BindingGenerator {
         checkArgument(context != null, "Schema Context reference cannot be NULL.");
         checkState(context.getModules() != null, "Schema Context does not contain defined modules.");
         schemaContext = context;
-        typeProvider = new TypeProviderImpl(context);
         final Set<Module> modules = context.getModules();
         return generateTypes(context, modules);
     }
@@ -311,7 +310,7 @@ public class BindingGeneratorImpl implements BindingGenerator {
         checkArgument(module.getName() != null, "Module name cannot be NULL.");
         final DataNodeIterator it = new DataNodeIterator(module);
         final List<TypeDefinition<?>> typeDefinitions = it.allTypedefs();
-        checkState(typeDefinitions != null, "Type Definitions for module «module.name» cannot be NULL.");
+        checkState(typeDefinitions != null, "Type Definitions for module %s cannot be NULL.", module.getName());
 
         for (final TypeDefinition<?> typedef : typeDefinitions) {
             if (typedef != null) {
@@ -516,6 +515,7 @@ public class BindingGeneratorImpl implements BindingGenerator {
                 //in case of implicit RPC input (StatementSource.CONTEXT),
                 // stay compatible (no input argument generated)
                 if (input != null && isExplicitStatement(input)) {
+                    processUsesAugments(input, module);
                     final GeneratedTypeBuilder inType = addRawInterfaceDefinition(basePackageName, input, rpcName);
                     addImplementedInterfaceFromUses(input, inType);
                     inType.addImplementsType(DATA_OBJECT);
@@ -531,6 +531,7 @@ public class BindingGeneratorImpl implements BindingGenerator {
                 //in case of implicit RPC output (StatementSource.CONTEXT),
                 //stay compatible (Future<RpcResult<Void>> return type generated)
                 if (output != null && isExplicitStatement(output)) {
+                    processUsesAugments(output, module);
                     final GeneratedTypeBuilder outType = addRawInterfaceDefinition(basePackageName, output, rpcName);
                     addImplementedInterfaceFromUses(output, outType);
                     outType.addImplementsType(DATA_OBJECT);
@@ -550,9 +551,9 @@ public class BindingGeneratorImpl implements BindingGenerator {
         genCtx.get(module).addTopLevelNodeType(interfaceBuilder);
     }
 
-    private static boolean isExplicitStatement(ContainerSchemaNode node) {
+    private static boolean isExplicitStatement(final ContainerSchemaNode node) {
         return node instanceof EffectiveStatement
-                && ((EffectiveStatement) node).getDeclared().getStatementSource() == StatementSource.DECLARATION;
+                && ((EffectiveStatement<?, ?>) node).getDeclared().getStatementSource() == StatementSource.DECLARATION;
     }
 
     /**
@@ -724,8 +725,6 @@ public class BindingGeneratorImpl implements BindingGenerator {
      *            GroupingDefinition which contains data about grouping
      * @param module
      *            current module
-     * @return GeneratedType which is generated from grouping (object of type
-     *         <code>GroupingDefinition</code>)
      */
     private void groupingToGenType(final String basePackageName, final GroupingDefinition grouping, final Module module) {
         final String packageName = packageNameForGeneratedType(basePackageName, grouping.getPath());
@@ -944,17 +943,15 @@ public class BindingGeneratorImpl implements BindingGenerator {
                 // The original node is required, but we have only the copy of
                 // the original node.
                 // Maybe this indicates a bug in Yang parser.
-                throw new IllegalStateException(
-                        "Failed to generate code for augment in "
-                                + parentUsesNode);
-            } else {
-                return resultDataSchemaNode;
+                throw new IllegalStateException("Failed to generate code for augment in " + parentUsesNode);
             }
-        } else {
-            throw new IllegalStateException(
-                    "Target node of uses-augment statement must be DataSchemaNode. Failed to generate code for augment in "
-                            + parentUsesNode);
+
+            return resultDataSchemaNode;
         }
+
+        throw new IllegalStateException(
+            "Target node of uses-augment statement must be DataSchemaNode. Failed to generate code for augment in "
+                    + parentUsesNode);
     }
 
     /**
@@ -1112,7 +1109,7 @@ public class BindingGeneratorImpl implements BindingGenerator {
     private GeneratedTypeBuilder augSchemaNodeToMethods(final Module module, final String basePackageName,
             final GeneratedTypeBuilder typeBuilder, final GeneratedTypeBuilder childOf,
             final Iterable<DataSchemaNode> schemaNodes) {
-        if ((schemaNodes != null) && (typeBuilder != null)) {
+        if (schemaNodes != null && typeBuilder != null) {
             for (final DataSchemaNode schemaNode : schemaNodes) {
                 if (!schemaNode.isAugmenting()) {
                     addSchemaNodeToBuilderAsMethod(basePackageName, schemaNode, typeBuilder, childOf, module);
@@ -1219,7 +1216,6 @@ public class BindingGeneratorImpl implements BindingGenerator {
      *            type which represents superior <i>case</i>
      * @param choiceNode
      *            choice case node which is mapped to generated type
-     * @return list of generated types for <code>caseNodes</code>.
      * @throws IllegalArgumentException
      *             <ul>
      *             <li>if <code>basePackageName</code> equals null</li>
@@ -1306,8 +1302,6 @@ public class BindingGeneratorImpl implements BindingGenerator {
      * @param augmentedNodes
      *            set of choice case nodes for which is checked if are/aren't
      *            added to choice through augmentation
-     * @return list of generated types which represents augmented cases of
-     *         choice <code>refChoiceType</code>
      * @throws IllegalArgumentException
      *             <ul>
      *             <li>if <code>basePackageName</code> is null</li>
@@ -1434,6 +1428,8 @@ public class BindingGeneratorImpl implements BindingGenerator {
                 GeneratedTOBuilder genTOBuilder = addTOToTypeBuilder(typeDef, typeBuilder, leaf, parentModule);
                 if (genTOBuilder != null) {
                     returnType = createReturnTypeForUnion(genTOBuilder, typeDef, typeBuilder, parentModule);
+                    // Store the inner type within the union so that we can find the reference for it
+                    genCtx.get(module).addInnerTypedefType(typeDef.getPath(), returnType);
                 }
             } else if (typeDef instanceof BitsTypeDefinition) {
                 GeneratedTOBuilder genTOBuilder = addTOToTypeBuilder(typeDef, typeBuilder, leaf, parentModule);
@@ -1475,7 +1471,7 @@ public class BindingGeneratorImpl implements BindingGenerator {
     private static TypeDefinition<?> getBaseOrDeclaredType(final TypeDefinition<?> typeDef) {
         // Returns DerivedType in case of new parser.
         final TypeDefinition<?> baseType = typeDef.getBaseType();
-        return (baseType != null && baseType.getBaseType() != null) ? baseType : typeDef;
+        return baseType != null && baseType.getBaseType() != null ? baseType : typeDef;
     }
 
     private void processContextRefExtension(final LeafSchemaNode leaf, final MethodSignatureBuilder getter,
@@ -1544,13 +1540,16 @@ public class BindingGeneratorImpl implements BindingGenerator {
             Type returnType;
             final TypeDefinition<?> typeDef = CompatUtils.compatLeafType(leaf);
             if (typeDef instanceof UnionTypeDefinition) {
-                // GeneratedType for this type definition should be already
-                // created
+                // GeneratedType for this type definition should have be already created
                 final QName qname = typeDef.getQName();
                 final Module unionModule = schemaContext.findModuleByNamespaceAndRevision(qname.getNamespace(),
                         qname.getRevision());
                 final ModuleContext mc = genCtx.get(unionModule);
                 returnType = mc.getTypedefs().get(typeDef.getPath());
+                if (returnType == null) {
+                    // This may still be an inner type, try to find it
+                    returnType = mc.getInnerType(typeDef.getPath());
+                }
             } else if (typeDef instanceof EnumTypeDefinition && typeDef.getBaseType() == null) {
                 // Annonymous enumeration (already generated, since it is inherited via uses).
                 LeafSchemaNode originalLeaf = (LeafSchemaNode) SchemaNodeUtils.getRootOriginalIfPossible(leaf);
@@ -1929,7 +1928,7 @@ public class BindingGeneratorImpl implements BindingGenerator {
         if (schemaNode instanceof LeafSchemaNode) {
             final LeafSchemaNode leaf = (LeafSchemaNode) schemaNode;
             final String leafName = leaf.getQName().getLocalName();
-            final Type type = resolveLeafSchemaNodeAsMethod(typeBuilder, leaf,module);
+            Type type = resolveLeafSchemaNodeAsMethod(typeBuilder, leaf, module);
             if (listKeys.contains(leafName)) {
                 if (type == null) {
                     resolveLeafSchemaNodeAsProperty(genTOBuilder, leaf, true, module);
@@ -1997,7 +1996,7 @@ public class BindingGeneratorImpl implements BindingGenerator {
      */
     private static GeneratedTOBuilder resolveListKeyTOBuilder(final String packageName, final ListSchemaNode list) {
         GeneratedTOBuilder genTOBuilder = null;
-        if ((list.getKeyDefinition() != null) && (!list.getKeyDefinition().isEmpty())) {
+        if (list.getKeyDefinition() != null && !list.getKeyDefinition().isEmpty()) {
             final String listName = list.getQName().getLocalName() + "Key";
             final String genTOName = BindingMapping.getClassName(listName);
             genTOBuilder = new GeneratedTOBuilderImpl(packageName, genTOName);
@@ -2033,7 +2032,7 @@ public class BindingGeneratorImpl implements BindingGenerator {
         final String packageName = typeBuilder.getFullyQualifiedName();
         if (typeDef instanceof UnionTypeDefinition) {
             final List<GeneratedTOBuilder> types = ((TypeProviderImpl) typeProvider)
-                    .provideGeneratedTOBuildersForUnionTypeDef(packageName, ((UnionTypeDefinition) typeDef),
+                    .provideGeneratedTOBuildersForUnionTypeDef(packageName, (UnionTypeDefinition) typeDef,
                             classNameFromLeaf, leaf);
             genTOBuilders.addAll(types);
 
@@ -2053,7 +2052,7 @@ public class BindingGeneratorImpl implements BindingGenerator {
             resultTOBuilder.addToStringProperty(genPropBuilder);
 
         } else if (typeDef instanceof BitsTypeDefinition) {
-            genTOBuilders.add((((TypeProviderImpl) typeProvider)).provideGeneratedTOBuilderForBitsTypeDefinition(
+            genTOBuilders.add(((TypeProviderImpl) typeProvider).provideGeneratedTOBuilderForBitsTypeDefinition(
                     packageName, typeDef, classNameFromLeaf, parentModule.getName()));
         }
         if (!genTOBuilders.isEmpty()) {