BUG-1513: introduce ChoiceSchemaNode
[mdsal.git] / code-generator / binding-generator-impl / src / main / java / org / opendaylight / yangtools / sal / binding / generator / impl / BindingGeneratorImpl.java
index b98c48c9844f0b5067aeae72cd1894b9efd43b9c..b6e3598ce1fc21126ce2e9f19be1e7f5f29efb30 100644 (file)
@@ -12,7 +12,6 @@ import static com.google.common.base.Preconditions.checkNotNull;
 import static com.google.common.base.Preconditions.checkState;
 import static org.opendaylight.yangtools.binding.generator.util.BindingGeneratorUtil.computeDefaultSUID;
 import static org.opendaylight.yangtools.binding.generator.util.BindingGeneratorUtil.packageNameForGeneratedType;
-import static org.opendaylight.yangtools.binding.generator.util.BindingGeneratorUtil.parseToValidParamName;
 import static org.opendaylight.yangtools.binding.generator.util.BindingTypes.DATA_OBJECT;
 import static org.opendaylight.yangtools.binding.generator.util.BindingTypes.DATA_ROOT;
 import static org.opendaylight.yangtools.binding.generator.util.BindingTypes.IDENTIFIABLE;
@@ -26,7 +25,7 @@ import static org.opendaylight.yangtools.binding.generator.util.Types.typeForCla
 import static org.opendaylight.yangtools.yang.model.util.SchemaContextUtil.findDataSchemaNode;
 import static org.opendaylight.yangtools.yang.model.util.SchemaContextUtil.findNodeInSchemaContext;
 import static org.opendaylight.yangtools.yang.model.util.SchemaContextUtil.findParentModule;
-
+import com.google.common.base.Preconditions;
 import com.google.common.base.Splitter;
 import com.google.common.collect.Iterables;
 import com.google.common.collect.Sets;
@@ -72,7 +71,7 @@ import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.common.RpcResult;
 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
-import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
+import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
@@ -211,7 +210,7 @@ public class BindingGeneratorImpl implements BindingGenerator {
      *         <ul>
      *         <li>are generated from <code>context</code> schema nodes and</li>
      *         <li>are also part of some of the module in <code>modules</code>
-     *         set</li>.
+     *         set.</li>
      *         </ul>
      * @throws IllegalArgumentException
      *             <ul>
@@ -296,7 +295,9 @@ public class BindingGeneratorImpl implements BindingGenerator {
                 final Type type = ((TypeProviderImpl) typeProvider).generatedTypeForExtendedDefinitionType(typedef,
                         typedef);
                 if (type != null) {
-                    genCtx.get(module).addTypedefType(typedef.getPath(), type);
+                    final ModuleContext ctx = genCtx.get(module);
+                    ctx.addTypedefType(typedef.getPath(), type);
+                    ctx.addTypeToSchema(type,typedef);
                 }
             }
         }
@@ -318,61 +319,23 @@ public class BindingGeneratorImpl implements BindingGenerator {
             genCtx.get(module).addChildNodeType(node, genType);
             groupingsToGenTypes(module, ((DataNodeContainer) node).getGroupings());
             processUsesAugments((DataNodeContainer) node, module);
-            if (node.isAddedByUses() || node.isAugmenting())
-                genType.setSuitableForBoxing(false);
         }
         return genType;
     }
 
-    private boolean hasWhenOrMustConstraints(final SchemaNode node) {
-        boolean hasWhenCondition;
-        boolean hasMustConstraints;
-
-        if (node instanceof ContainerSchemaNode) {
-            ContainerSchemaNode contNode = (ContainerSchemaNode)node;
-            hasWhenCondition = contNode.getConstraints().getWhenCondition() != null;
-            hasMustConstraints = !isNullOrEmpty(contNode.getConstraints().getMustConstraints());
-
-            if (hasWhenCondition || hasMustConstraints)
-                return true;
-        }
-        return false;
-    }
-
     private void containerToGenType(final Module module, final String basePackageName,
             final GeneratedTypeBuilder parent, final GeneratedTypeBuilder childOf, final ContainerSchemaNode node) {
         final GeneratedTypeBuilder genType = processDataSchemaNode(module, basePackageName, childOf, node);
-
         if (genType != null) {
             constructGetter(parent, node.getQName().getLocalName(), node.getDescription(), genType);
             resolveDataSchemaNodes(module, basePackageName, genType, genType, node.getChildNodes());
-
-            final String parentName = parent.getName();
-            final String childOfName = childOf.getName();
-
-            if (parent != null && !parent.getName().contains("Data"))
-                genType.setParentType(parent);
-            genType.setSuitableForBoxing(hasOnlyOneChild(node) && !hasWhenOrMustConstraints(node));
-
-            if (parentName.equals(childOfName))
-                genType.setSuitableForBoxing(false);
         }
     }
 
-    private boolean hasOnlyOneChild(final ContainerSchemaNode contNode) {
-        if (!isNullOrEmpty(contNode.getChildNodes()) && contNode.getChildNodes().size() == 1)
-            return true;
-        return false;
-    }
-
     private void listToGenType(final Module module, final String basePackageName, final GeneratedTypeBuilder parent,
             final GeneratedTypeBuilder childOf, final ListSchemaNode node) {
         final GeneratedTypeBuilder genType = processDataSchemaNode(module, basePackageName, childOf, node);
-
         if (genType != null) {
-            if (!parent.getName().equals(childOf) && !parent.getName().contains("Data")) {
-                genType.setParentType(parent);
-            }
             constructGetter(parent, node.getQName().getLocalName(), node.getDescription(), Types.listTypeFor(genType));
 
             final List<String> listKeys = listKeys(node);
@@ -519,7 +482,7 @@ public class BindingGeneratorImpl implements BindingGenerator {
         for (RpcDefinition rpc : rpcDefinitions) {
             if (rpc != null) {
                 final String rpcName = BindingMapping.getClassName(rpc.getQName());
-                final String rpcMethodName = parseToValidParamName(rpcName);
+                final String rpcMethodName = BindingMapping.getPropertyName(rpcName);
                 final String rpcComment = rpc.getDescription();
                 final MethodSignatureBuilder method = interfaceBuilder.addMethod(rpcMethodName);
                 final ContainerSchemaNode input = rpc.getInput();
@@ -861,7 +824,7 @@ public class BindingGeneratorImpl implements BindingGenerator {
             throw new NullPointerException("Target type not yet generated: " + targetSchemaNode);
         }
 
-        if (!(targetSchemaNode instanceof ChoiceNode)) {
+        if (!(targetSchemaNode instanceof ChoiceSchemaNode)) {
             String packageName = augmentPackageName;
             final Type targetType = new ReferencedTypeImpl(targetTypeBuilder.getPackageName(),
                     targetTypeBuilder.getName());
@@ -869,7 +832,7 @@ public class BindingGeneratorImpl implements BindingGenerator {
 
         } else {
             generateTypesFromAugmentedChoiceCases(module, augmentPackageName, targetTypeBuilder.toInstance(),
-                    (ChoiceNode) targetSchemaNode, augSchema.getChildNodes());
+                    (ChoiceSchemaNode) targetSchemaNode, augSchema.getChildNodes());
         }
     }
 
@@ -894,9 +857,8 @@ public class BindingGeneratorImpl implements BindingGenerator {
         if (targetTypeBuilder == null) {
             throw new NullPointerException("Target type not yet generated: " + targetSchemaNode);
         }
-        targetTypeBuilder.setSuitableForBoxing(false);
 
-        if (!(targetSchemaNode instanceof ChoiceNode)) {
+        if (!(targetSchemaNode instanceof ChoiceSchemaNode)) {
             String packageName = augmentPackageName;
             if (usesNodeParent instanceof SchemaNode) {
                 packageName = packageNameForGeneratedType(augmentPackageName, ((SchemaNode) usesNodeParent).getPath(),
@@ -906,7 +868,7 @@ public class BindingGeneratorImpl implements BindingGenerator {
                     augSchema);
         } else {
             generateTypesFromAugmentedChoiceCases(module, augmentPackageName, targetTypeBuilder.toInstance(),
-                    (ChoiceNode) targetSchemaNode, augSchema.getChildNodes());
+                    (ChoiceSchemaNode) targetSchemaNode, augSchema.getChildNodes());
         }
     }
 
@@ -933,8 +895,8 @@ public class BindingGeneratorImpl implements BindingGenerator {
             // with same name and different namespace
             if (result instanceof DataNodeContainer) {
                 result = ((DataNodeContainer) result).getDataChildByName(node.getLocalName());
-            } else if (result instanceof ChoiceNode) {
-                result = ((ChoiceNode) result).getCaseNodeByName(node.getLocalName());
+            } else if (result instanceof ChoiceSchemaNode) {
+                result = ((ChoiceSchemaNode) result).getCaseNodeByName(node.getLocalName());
             }
         }
         if (result == null) {
@@ -1151,8 +1113,8 @@ public class BindingGeneratorImpl implements BindingGenerator {
                 containerToGenType(module, basePackageName, typeBuilder, childOf, (ContainerSchemaNode) node);
             } else if (node instanceof ListSchemaNode) {
                 listToGenType(module, basePackageName, typeBuilder, childOf, (ListSchemaNode) node);
-            } else if (node instanceof ChoiceNode) {
-                choiceToGeneratedType(module, basePackageName, typeBuilder, (ChoiceNode) node);
+            } else if (node instanceof ChoiceSchemaNode) {
+                choiceToGeneratedType(module, basePackageName, typeBuilder, (ChoiceSchemaNode) node);
             } else {
                 // TODO: anyxml not yet supported
                 LOG.debug("Unable to add schema node {} as method in {}: unsupported type of node.", node.getClass(),
@@ -1185,7 +1147,7 @@ public class BindingGeneratorImpl implements BindingGenerator {
      *             </ul>
      */
     private void choiceToGeneratedType(final Module module, final String basePackageName,
-            final GeneratedTypeBuilder parent, final ChoiceNode choiceNode) {
+            final GeneratedTypeBuilder parent, final ChoiceSchemaNode choiceNode) {
         checkArgument(basePackageName != null, "Base Package Name cannot be NULL.");
         checkArgument(choiceNode != null, "Choice Schema Node cannot be NULL.");
 
@@ -1195,7 +1157,6 @@ public class BindingGeneratorImpl implements BindingGenerator {
             constructGetter(parent, choiceNode.getQName().getLocalName(), choiceNode.getDescription(),
                     choiceTypeBuilder);
             choiceTypeBuilder.addImplementsType(typeForClass(DataContainer.class));
-            choiceTypeBuilder.setParentType(parent);
             genCtx.get(module).addChildNodeType(choiceNode, choiceTypeBuilder);
             generateTypesFromChoiceCases(module, basePackageName, choiceTypeBuilder.toInstance(), choiceNode);
         }
@@ -1229,7 +1190,7 @@ public class BindingGeneratorImpl implements BindingGenerator {
      *             </ul>
      */
     private void generateTypesFromChoiceCases(final Module module, final String basePackageName,
-            final Type refChoiceType, final ChoiceNode choiceNode) {
+            final Type refChoiceType, final ChoiceSchemaNode choiceNode) {
         checkArgument(basePackageName != null, "Base Package Name cannot be NULL.");
         checkArgument(refChoiceType != null, "Referenced Choice Type cannot be NULL.");
         checkArgument(choiceNode != null, "ChoiceNode cannot be NULL.");
@@ -1248,9 +1209,8 @@ public class BindingGeneratorImpl implements BindingGenerator {
                 genCtx.get(module).addChoiceToCaseMapping(refChoiceType, caseTypeBuilder, caseNode);
                 final Iterable<DataSchemaNode> caseChildNodes = caseNode.getChildNodes();
                 if (caseChildNodes != null) {
-                    Object parentNode = null;
                     final SchemaPath nodeSp = choiceNode.getPath();
-                    parentNode = findDataSchemaNode(schemaContext, nodeSp.getParent());
+                    final Object parentNode = findDataSchemaNode(schemaContext, nodeSp.getParent());
 
                     SchemaNode parent;
                     if (parentNode instanceof AugmentationSchema) {
@@ -1273,6 +1233,7 @@ public class BindingGeneratorImpl implements BindingGenerator {
                         final SchemaPath sp = choiceNode.getPath();
                         parent = findDataSchemaNode(schemaContext, sp.getParent());
                     }
+                    Preconditions.checkState(parent != null, "Could not find Choice node parent "+choiceNode.getPath().getParent());
                     GeneratedTypeBuilder childOfType = findChildNodeByPath(parent.getPath());
                     if (childOfType == null) {
                         childOfType = findGroupingByPath(parent.getPath());
@@ -1280,7 +1241,6 @@ public class BindingGeneratorImpl implements BindingGenerator {
                     resolveDataSchemaNodes(module, basePackageName, caseTypeBuilder, childOfType, caseChildNodes);
                 }
             }
-
             processUsesAugments(caseNode, module);
         }
     }
@@ -1313,7 +1273,7 @@ public class BindingGeneratorImpl implements BindingGenerator {
      *             </ul>
      */
     private void generateTypesFromAugmentedChoiceCases(final Module module, final String basePackageName,
-            final Type targetType, final ChoiceNode targetNode, final Iterable<DataSchemaNode> augmentedNodes) {
+            final Type targetType, final ChoiceSchemaNode targetNode, final Iterable<DataSchemaNode> augmentedNodes) {
         checkArgument(basePackageName != null, "Base Package Name cannot be NULL.");
         checkArgument(targetType != null, "Referenced Choice Type cannot be NULL.");
         checkArgument(augmentedNodes != null, "Set of Choice Case Nodes cannot be NULL.");
@@ -1540,7 +1500,7 @@ public class BindingGeneratorImpl implements BindingGenerator {
         }
         final String leafName = leaf.getQName().getLocalName();
         final String leafDesc = leaf.getDescription();
-        final GeneratedPropertyBuilder propBuilder = toBuilder.addProperty(parseToValidParamName(leafName));
+        final GeneratedPropertyBuilder propBuilder = toBuilder.addProperty(BindingMapping.getPropertyName(leafName));
         propBuilder.setReadOnly(isReadOnly);
         propBuilder.setReturnType(returnType);
         propBuilder.setComment(leafDesc);
@@ -1648,10 +1608,10 @@ public class BindingGeneratorImpl implements BindingGenerator {
      * <code>schemaNode</code>.
      *
      * The new builder always implements
-     * {@link org.opendaylight.yangtools.yang.binding.DataObject DataObject}.<br />
+     * {@link org.opendaylight.yangtools.yang.binding.DataObject DataObject}.<br>
      * If <code>schemaNode</code> is instance of GroupingDefinition it also
      * implements {@link org.opendaylight.yangtools.yang.binding.Augmentable
-     * Augmentable}.<br />
+     * Augmentable}.<br>
      * If <code>schemaNode</code> is instance of
      * {@link org.opendaylight.yangtools.yang.model.api.DataNodeContainer
      * DataNodeContainer} it can also implement nodes which are specified in
@@ -1861,8 +1821,8 @@ public class BindingGeneratorImpl implements BindingGenerator {
                 resolveLeafListSchemaNode(typeBuilder, (LeafListSchemaNode) schemaNode);
             } else if (schemaNode instanceof ContainerSchemaNode) {
                 containerToGenType(module, basePackageName, typeBuilder, typeBuilder, (ContainerSchemaNode) schemaNode);
-            } else if (schemaNode instanceof ChoiceNode) {
-                choiceToGeneratedType(module, basePackageName, typeBuilder, (ChoiceNode) schemaNode);
+            } else if (schemaNode instanceof ChoiceSchemaNode) {
+                choiceToGeneratedType(module, basePackageName, typeBuilder, (ChoiceSchemaNode) schemaNode);
             } else if (schemaNode instanceof ListSchemaNode) {
                 listToGenType(module, basePackageName, typeBuilder, typeBuilder, (ListSchemaNode) schemaNode);
             }
@@ -1967,7 +1927,7 @@ public class BindingGeneratorImpl implements BindingGenerator {
             }
 
             final GeneratedPropertyBuilder genPropBuilder = resultTOBuilder.addProperty("value");
-            genPropBuilder.setReturnType(Types.primitiveType("char[]", null));
+            genPropBuilder.setReturnType(Types.CHAR_ARRAY);
             resultTOBuilder.addEqualsIdentity(genPropBuilder);
             resultTOBuilder.addHashIdentity(genPropBuilder);
             resultTOBuilder.addToStringProperty(genPropBuilder);
@@ -2042,7 +2002,7 @@ public class BindingGeneratorImpl implements BindingGenerator {
             }
         }
         sb.append(NEW_LINE);
-        sb.append("<br />(Source path: <i>");
+        sb.append("<br>(Source path: <i>");
         sb.append(moduleSourcePath);
         sb.append("</i>):");
         sb.append(NEW_LINE);
@@ -2070,18 +2030,12 @@ public class BindingGeneratorImpl implements BindingGenerator {
         if (verboseClassComments) {
             final Module module = findParentModule(schemaContext, schemaNode);
             final StringBuilder linkToBuilderClass = new StringBuilder();
-            final StringBuilder linkToKeyClass = new StringBuilder();
             final String[] namespace = Iterables.toArray(BSDOT_SPLITTER.split(fullyQualifiedName), String.class);
             String className = namespace[namespace.length - 1];
 
             if (hasBuilderClass(schemaNode)) {
                 linkToBuilderClass.append(className);
                 linkToBuilderClass.append("Builder");
-
-                if (schemaNode instanceof ListSchemaNode) {
-                    linkToKeyClass.append(className);
-                    linkToKeyClass.append("Key");
-                }
             }
 
             sb.append("<p>");
@@ -2089,7 +2043,7 @@ public class BindingGeneratorImpl implements BindingGenerator {
             sb.append(module.getName());
             sb.append("</b>");
             sb.append(NEW_LINE);
-            sb.append("<br />(Source path: <i>");
+            sb.append("<br>(Source path: <i>");
             sb.append(module.getModuleSourcePath());
             sb.append("</i>):");
             sb.append(NEW_LINE);
@@ -2111,11 +2065,16 @@ public class BindingGeneratorImpl implements BindingGenerator {
                 sb.append(NEW_LINE);
                 sb.append("@see ");
                 sb.append(linkToBuilderClass);
+                sb.append(NEW_LINE);
                 if (schemaNode instanceof ListSchemaNode) {
-                    sb.append("@see ");
-                    sb.append(linkToKeyClass);
+                    final List<QName> keyDef = ((ListSchemaNode)schemaNode).getKeyDefinition();
+                    if (keyDef != null && !keyDef.isEmpty()) {
+                        sb.append("@see ");
+                        sb.append(className);
+                        sb.append("Key");
+                    }
+                   sb.append(NEW_LINE);
                 }
-                sb.append(NEW_LINE);
             }
         }
 
@@ -2149,7 +2108,7 @@ public class BindingGeneratorImpl implements BindingGenerator {
             sb.append(module.getName());
             sb.append("</b>");
             sb.append(NEW_LINE);
-            sb.append("<br />Source path: <i>");
+            sb.append("<br>Source path: <i>");
             sb.append(module.getModuleSourcePath());
             sb.append("</i>):");
             sb.append(NEW_LINE);