Binding generator v2 - fix IdentifiableItem and Item 16/62116/19
authorJie Han <han.jie@zte.com.cn>
Tue, 22 Aug 2017 01:48:14 +0000 (09:48 +0800)
committerRobert Varga <nite@hq.sk>
Fri, 26 Jan 2018 11:20:55 +0000 (11:20 +0000)
- Support generated interface of List implements
  kind of TreeChildNode<ParentClass,IdentifiableItem<Class,KeyClass>>
- Other interface implements
  kind of TreeChildNode<ParentClass,Item<Class>>
- checkstyle
Change-Id: Ia0c5805e20c1ecb75a507aeaa02ed165b14b4986
Signed-off-by: Jie Han <han.jie@zte.com.cn>
binding2/mdsal-binding2-generator-impl/src/main/java/org/opendaylight/mdsal/binding/javav2/generator/impl/GenHelperUtil.java
binding2/mdsal-binding2-generator-impl/src/main/java/org/opendaylight/mdsal/binding/javav2/generator/impl/RpcActionGenHelper.java
binding2/mdsal-binding2-java-api-generator/src/main/java/org/opendaylight/mdsal/binding/javav2/java/api/generator/renderers/BuilderRenderer.java
binding2/mdsal-binding2-java-api-generator/src/main/twirl/org/opendaylight/mdsal/binding/javav2/java/api/generator/builderTemplate.scala.txt

index 9d9fa5cde8c1b9b7551d9ea8caa58a6d437fc224..3b46518e7c81b5620023d3a89397625b19307b99 100644 (file)
@@ -531,14 +531,12 @@ final class GenHelperUtil {
             if (parent == null) {
                 it.addImplementsType(BindingTypes.TREE_NODE);
             } else {
-                if (parent instanceof ListSchemaNode) {
+                if (!(schemaNode instanceof ListSchemaNode) ||
+                        ((ListSchemaNode) schemaNode).getKeyDefinition().isEmpty()) {
                     it.addImplementsType(parameterizedTypeFor(BindingTypes.TREE_CHILD_NODE, parent, parameterizedTypeFor
-                            (BindingTypes.IDENTIFIABLE_ITEM, parent)));
-                } else {
-                    it.addImplementsType(parameterizedTypeFor(BindingTypes.TREE_CHILD_NODE, parent, parameterizedTypeFor
-                            (BindingTypes.ITEM, parent)));
-                    it.addImplementsType(parameterizedTypeFor(BindingTypes.INSTANTIABLE, it));
+                            (BindingTypes.ITEM, it)));
                 }
+                it.addImplementsType(parameterizedTypeFor(BindingTypes.INSTANTIABLE, it));
             }
 
             if (!(schemaNode instanceof GroupingDefinition)) {
@@ -787,6 +785,9 @@ final class GenHelperUtil {
                     final Type identifiableMarker = Types.parameterizedTypeFor(IDENTIFIABLE, genTOBuilder);
                     genTOBuilder.addImplementsType(IDENTIFIER);
                     genType.addImplementsType(identifiableMarker);
+                    genType.addImplementsType(parameterizedTypeFor(BindingTypes.TREE_CHILD_NODE, parent, parameterizedTypeFor
+                        (BindingTypes.IDENTIFIABLE_ITEM, genType, genTOBuilder)));
+
                 }
 
                 for (final DataSchemaNode schemaNode : node.getChildNodes()) {
@@ -802,7 +803,6 @@ final class GenHelperUtil {
                     final GeneratedPropertyBuilder prop = new GeneratedPropertyBuilderImpl("serialVersionUID");
                     prop.setValue(Long.toString(computeDefaultSUID(genTOBuilder)));
                     genTOBuilder.setSUID(prop);
-
                     typeBuildersToGenTypes(module, genType, genTOBuilder.toInstance(), genCtx, namespaceType);
                     genCtx.get(module).addGeneratedTOBuilder(node.getPath(), genTOBuilder);
                 }
index ba6a18ec6f4f91c7f937992c8c3eaf5aa66411a3..b3c2479e36adb135756b9d15942789c055924746 100644 (file)
@@ -311,7 +311,7 @@ final class RpcActionGenHelper {
                 operationName, "", verboseClassComments, genTypeBuilders, namespaceType, genCtx.get(module));
         addImplementedInterfaceFromUses(operationNode, nodeType, genCtx);
         nodeType.addImplementsType(parameterizedTypeFor(BindingTypes.TREE_CHILD_NODE, parent, parameterizedTypeFor
-                (BindingTypes.ITEM, parent)));
+                (BindingTypes.ITEM, nodeType)));
         if (isInput) {
             nodeType.addImplementsType(parameterizedTypeFor(INPUT, nodeType));
         } else {
index ec373f9e4788f85aba9c49a35134d2dae8e241a9..07ae4bd5c9b63f40dc04b7634128aa4bf52b5571 100644 (file)
@@ -44,6 +44,7 @@ import org.opendaylight.mdsal.binding.javav2.model.api.GeneratedTypeForBuilder;
 import org.opendaylight.mdsal.binding.javav2.model.api.MethodSignature;
 import org.opendaylight.mdsal.binding.javav2.model.api.ParameterizedType;
 import org.opendaylight.mdsal.binding.javav2.model.api.Type;
+import org.opendaylight.mdsal.binding.javav2.spec.base.IdentifiableItem;
 import org.opendaylight.mdsal.binding.javav2.spec.base.Instantiable;
 import org.opendaylight.mdsal.binding.javav2.spec.base.Item;
 import org.opendaylight.mdsal.binding.javav2.spec.base.TreeNode;
@@ -133,14 +134,16 @@ public class BuilderRenderer extends BaseRenderer {
      */
     private GeneratedProperty propertyFromGetter(final MethodSignature method) {
         Preconditions.checkArgument(method != null, "Method cannot be NULL");
-        Preconditions.checkArgument(!Strings.isNullOrEmpty(method.getName()), "Method name cannot be NULL or empty");
-        Preconditions.checkArgument(method.getReturnType() != null, "Method return type reference cannot be NULL");
+        Preconditions.checkArgument(!Strings.isNullOrEmpty(method.getName()),
+            "Method name cannot be NULL or empty");
+        Preconditions.checkArgument(method.getReturnType() != null,
+            "Method return type reference cannot be NULL");
         final String prefix = Types.BOOLEAN.equals(method.getReturnType()) ? "is" : "get";
         if (method.getName().startsWith(prefix)) {
             final String fieldName = toFirstLower(method.getName().substring(prefix.length()));
-            final GeneratedTOBuilderImpl tmpGenTO = new GeneratedTOBuilderImpl("foo", "foo", true);
-            tmpGenTO.addProperty(fieldName)
-                    .setReturnType(method.getReturnType());
+            final GeneratedTOBuilderImpl tmpGenTO =
+                new GeneratedTOBuilderImpl("foo", "foo", true);
+            tmpGenTO.addProperty(fieldName).setReturnType(method.getReturnType());
             return tmpGenTO.toInstance().getProperties().get(0);
         }
         return null;
@@ -187,13 +190,15 @@ public class BuilderRenderer extends BaseRenderer {
                             final String name = getName(fullyQualifiedName);
                             final GeneratedTOBuilderImpl generatedTOBuilder = new GeneratedTOBuilderImpl(aPackage,
                                     name, true);
-                            final ReferencedTypeImpl referencedType = new ReferencedTypeImpl(aPackage, name, true,
-                                    null);
+                            final ReferencedTypeImpl referencedType = new ReferencedTypeImpl(aPackage, name,
+                                true, null);
                             final ReferencedTypeImpl generic = new ReferencedTypeImpl(getType().getPackageName(),
                                     getType().getName(), true, null);
-                            final ParameterizedType parametrizedReturnType = Types.parameterizedTypeFor(referencedType, generic);
+                            final ParameterizedType parametrizedReturnType =
+                                Types.parameterizedTypeFor(referencedType, generic);
                             generatedTOBuilder.addMethod(method.getName()).setReturnType(parametrizedReturnType);
-                            augmentField = propertyFromGetter(generatedTOBuilder.toInstance().getMethodDefinitions().get(0));
+                            augmentField = propertyFromGetter(generatedTOBuilder.toInstance().getMethodDefinitions()
+                                .get(0));
                             importedNames.put("map", importedName(Map.class));
                             importedNames.put("hashMap", importedName(HashMap.class));
                             importedNames.put("class", importedName(Class.class));
@@ -281,6 +286,7 @@ public class BuilderRenderer extends BaseRenderer {
         importedNames.put("treeNode", importedName(TreeNode.class));
         importedNames.put("instantiable", importedName(Instantiable.class));
         importedNames.put("item", importedName(Item.class));
+        importedNames.put("identifiableItem", importedName(IdentifiableItem.class));
         if (getType().getParentType() != null) {
             importedNames.put("parent", importedName(getType().getParentType()));
             parentTypeForBuilderName = getType().getParentType().getFullyQualifiedName();
@@ -292,8 +298,16 @@ public class BuilderRenderer extends BaseRenderer {
         }
 
         boolean childTreeNode = false;
+        boolean childTreeNodeIdent = false;
+        String keyTypeName = null;
         if (getType().getImplements().contains(BindingTypes.TREE_CHILD_NODE)) {
             childTreeNode = true;
+            if (getType().getImplements().contains(BindingTypes.IDENTIFIABLE)) {
+                childTreeNodeIdent = true;
+                final ParameterizedType pType = (ParameterizedType) getType().getImplements().get(getType()
+                    .getImplements().indexOf(BindingTypes.IDENTIFIABLE));
+                keyTypeName = pType.getActualTypeArguments()[0].getName();
+            }
         }
 
         importedNames.put("augmentation", importedName(Augmentation.class));
@@ -304,14 +318,14 @@ public class BuilderRenderer extends BaseRenderer {
         List<String> getterMethods = new ArrayList<>(Collections2.transform(properties, this::getterMethod));
 
         return builderTemplate.render(getType(), properties, importedNames, importedNamesForProperties, augmentField,
-            copyConstructorHelper, getterMethods, parentTypeForBuilderName, childTreeNode, instantiable)
-                .body();
+            copyConstructorHelper, getterMethods, parentTypeForBuilderName, childTreeNode, childTreeNodeIdent,
+            keyTypeName, instantiable).body();
     }
 
     private String generateListForCopyConstructor() {
         final List allProps = new ArrayList<>(properties);
-        final boolean isList = implementsIfc(getType(), Types.parameterizedTypeFor(Types.typeForClass(Identifiable.class),
-                getType()));
+        final boolean isList = implementsIfc(getType(),
+            Types.parameterizedTypeFor(Types.typeForClass(Identifiable.class), getType()));
         final Type keyType = getKey(getType());
         if (isList && keyType != null) {
             final List<GeneratedProperty> keyProps = ((GeneratedTransferObject) keyType).getProperties();
index b1540681ed83f5332b7b09167b84d6eb4c5500d2..9a79a4e135a16bbcb8593a2a10e53c9f9f6cb183 100644 (file)
@@ -30,7 +30,8 @@
 
 @(genType: GeneratedType, properties: Set[GeneratedProperty], importedNames: Map[String, String],
 ImportedNamesWithProperties: Map[GeneratedProperty, String], augmentField: GeneratedProperty, copyConstructorHelper: String,
-getterMethods: List[String], parentTypeForBuilderName: String, childTreeNode: Boolean, instantiable: Boolean)
+getterMethods: List[String], parentTypeForBuilderName: String, childTreeNode: Boolean, childTreeNodeIdent: Boolean,
+keyTypeName: String, instantiable: Boolean)
 @if(genType != null) {
 @{wrapToDocumentation(formatDataForJavaDocBuilder(importedNames.get("genType")))}
 public class @{genType.getName}Builder implements @{getSimpleNameForBuilder} <@{importedNames.get("genType")}> {
@@ -406,12 +407,18 @@ public class @{genType.getName}Builder implements @{getSimpleNameForBuilder} <@{
 }
 
 @generateImplementedMethods() = {
-    @if(childTreeNode) {
+    @if(childTreeNodeIdent) {
         @@Override
-        public @{importedNames.get("item")}<@{parentTypeForBuilderName}> treeIdentifier() {
-            //TODO implement
-            return null;
+        public @{importedNames.get("identifiableItem")}<@{genType.getName()}, @{keyTypeName}> treeIdentifier() {
+            return new @{importedNames.get("identifiableItem")}(@{importedNames.get("genType")}.class,_identifier);
         }
+    } else {
+      @if(childTreeNode) {
+          @@Override
+          public @{importedNames.get("item")}<@{genType.getName()}> treeIdentifier() {
+               return new @{importedNames.get("item")}(@{importedNames.get("genType")}.class);
+          }
+      }
     }
 
     @if(augmentField != null) {