Binding generator v2 - Identities support 22/58222/2
authorMartin Ciglan <martin.ciglan@pantheon.tech>
Thu, 1 Jun 2017 12:20:10 +0000 (14:20 +0200)
committerRobert Varga <nite@hq.sk>
Tue, 6 Jun 2017 09:37:41 +0000 (09:37 +0000)
- naming conflict prevention added

Change-Id: Iac43a3ed45ed13b113fd48a2dce3829a9ce7c21a
Signed-off-by: Martin Ciglan <martin.ciglan@pantheon.tech>
(cherry picked from commit ccc1939cf81b78524c29bf649c8d4e04742fd849)

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/ModuleToGenType.java
binding2/mdsal-binding2-generator-impl/src/test/resources/identity/identity-import.yang [new file with mode: 0644]
binding2/mdsal-binding2-generator-impl/src/test/resources/identity/identity.yang [new file with mode: 0644]
binding2/mdsal-binding2-generator-impl/src/test/resources/identity/identity2.yang [new file with mode: 0644]
binding2/mdsal-binding2-java-api-generator/src/main/java/org/opendaylight/mdsal/binding/javav2/java/api/generator/renderers/ClassRenderer.java
binding2/mdsal-binding2-java-api-generator/src/main/twirl/org/opendaylight/mdsal/binding/javav2/java/api/generator/classTemplate.scala.txt
binding2/mdsal-binding2-spec/src/main/java/org/opendaylight/mdsal/binding/javav2/spec/base/BaseIdentity.java [new file with mode: 0644]

index e8d7a2e39a641fcb4752b07d974b6d16534733a1..9afe6650cb3118e9b6070b3b1a3ab7f78941327f 100644 (file)
@@ -33,6 +33,7 @@ import java.util.Collection;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 import org.opendaylight.mdsal.binding.javav2.generator.spi.TypeProvider;
 import org.opendaylight.mdsal.binding.javav2.generator.util.BindingGeneratorUtil;
 import org.opendaylight.mdsal.binding.javav2.generator.util.BindingTypes;
@@ -40,6 +41,7 @@ import org.opendaylight.mdsal.binding.javav2.generator.util.JavaIdentifier;
 import org.opendaylight.mdsal.binding.javav2.generator.util.JavaIdentifierNormalizer;
 import org.opendaylight.mdsal.binding.javav2.generator.util.Types;
 import org.opendaylight.mdsal.binding.javav2.generator.util.generated.type.builder.GeneratedPropertyBuilderImpl;
+import org.opendaylight.mdsal.binding.javav2.generator.util.generated.type.builder.GeneratedTOBuilderImpl;
 import org.opendaylight.mdsal.binding.javav2.generator.util.generated.type.builder.GeneratedTypeBuilderImpl;
 import org.opendaylight.mdsal.binding.javav2.generator.yang.types.GroupingDefinitionDependencySort;
 import org.opendaylight.mdsal.binding.javav2.generator.yang.types.TypeProviderImpl;
@@ -52,6 +54,7 @@ import org.opendaylight.mdsal.binding.javav2.model.api.type.builder.EnumBuilder;
 import org.opendaylight.mdsal.binding.javav2.model.api.type.builder.GeneratedPropertyBuilder;
 import org.opendaylight.mdsal.binding.javav2.model.api.type.builder.GeneratedTOBuilder;
 import org.opendaylight.mdsal.binding.javav2.model.api.type.builder.GeneratedTypeBuilder;
+import org.opendaylight.mdsal.binding.javav2.spec.base.BaseIdentity;
 import org.opendaylight.mdsal.binding.javav2.spec.base.TreeNode;
 import org.opendaylight.mdsal.binding.javav2.spec.runtime.BindingNamespaceType;
 import org.opendaylight.mdsal.binding.javav2.spec.structural.Augmentable;
@@ -62,6 +65,7 @@ import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
+import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.Module;
@@ -873,4 +877,80 @@ final class GenHelperUtil {
                 typeProvider);
         return genCtx;
     }
+
+    /**
+     * //TODO: add information about multiple base identities in YANG 1.1
+     * Converts the <b>identity</b> object to GeneratedType. Firstly it is
+     * created transport object builder. If identity contains base identity then
+     * reference to base identity is added to superior identity as its extend.
+     * If identity doesn't contain base identity then only reference to abstract
+     * class {@link org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode
+     * BaseIdentity} is added
+     *
+     * @param module
+     *            current module
+     * @param basePackageName
+     *            string contains the module package name
+     * @param identity
+     *            IdentitySchemaNode which contains data about identity
+     * @param schemaContext
+     *            SchemaContext which is used to get package and name
+     *            information about base of identity
+     * @param genCtx generated context
+     * @return returns generated context
+     */
+    static Map<Module, ModuleContext> identityToGenType(final Module module, final String basePackageName,
+            final IdentitySchemaNode identity, final SchemaContext schemaContext, Map<Module, ModuleContext> genCtx,
+            boolean verboseClassComments, final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders,
+            final TypeProvider typeProvider, Map<QName, GeneratedTOBuilderImpl> generatedIdentities) {
+
+        final String packageName = BindingGeneratorUtil.packageNameForGeneratedType(basePackageName, identity.getPath(),
+            BindingNamespaceType.Identity);
+        final GeneratedTOBuilderImpl newType = new GeneratedTOBuilderImpl(packageName,
+                identity.getQName().getLocalName());
+
+        final Set<IdentitySchemaNode> baseIdentities = identity.getBaseIdentities();
+        if (baseIdentities.size() == 0) {
+            //no base - abstract
+            final GeneratedTOBuilderImpl gto = new GeneratedTOBuilderImpl(BaseIdentity.class.getPackage().getName(),
+                BaseIdentity.class.getSimpleName());
+            newType.setExtendsType(gto.toInstance());
+            generatedIdentities.put(identity.getQName(), newType);
+        } else {
+            //one base - inheritance
+            final IdentitySchemaNode baseIdentity = baseIdentities.iterator().next();
+            final Module baseIdentityParentModule = SchemaContextUtil.findParentModule(schemaContext, baseIdentity);
+            final String returnTypePkgName = new StringBuilder(BindingMapping.getRootPackageName
+                    (baseIdentityParentModule))
+                    .append('.')
+                    .append(BindingNamespaceType.Identity.getPackagePrefix())
+                    .toString();
+
+            final GeneratedTOBuilderImpl existingIdentityGto = generatedIdentities.get(baseIdentity.getQName());
+            if (existingIdentityGto != null) {
+                newType.setExtendsType(existingIdentityGto.toInstance());
+            } else {
+                final GeneratedTOBuilderImpl gto = new GeneratedTOBuilderImpl(returnTypePkgName,
+                        baseIdentity.getQName().getLocalName());
+                newType.setExtendsType(gto.toInstance());
+                generatedIdentities.put(baseIdentity.getQName(), gto);
+            }
+
+            //FIXME: more bases - possible composition, multiple inheritance not possible
+        }
+        generatedIdentities.put(identity.getQName(), newType);
+
+        newType.setAbstract(true);
+        newType.addComment(identity.getDescription());
+        newType.setDescription(createDescription(identity, newType.getFullyQualifiedName(), schemaContext,
+                verboseClassComments));
+        newType.setReference(identity.getReference());
+        newType.setModuleName(module.getName());
+        newType.setSchemaPath((List) identity.getPath().getPathFromRoot());
+
+        qNameConstant(newType, BindingMapping.QNAME_STATIC_FIELD_NAME, identity.getQName());
+
+        genCtx.get(module).addIdentityType(identity.getQName(), newType);
+        return genCtx;
+    }
 }
index 23d360c97c0a2164e4bc4dbefe1eb7346ca9b578..99d1108384db88a1ff4a0ab0d27404e2c1d30488 100644 (file)
@@ -19,15 +19,19 @@ import com.google.common.annotations.Beta;
 import com.google.common.base.Preconditions;
 import com.google.common.collect.ImmutableSet;
 import java.util.Collection;
+import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 import org.opendaylight.mdsal.binding.javav2.generator.spi.TypeProvider;
+import org.opendaylight.mdsal.binding.javav2.generator.util.generated.type.builder.GeneratedTOBuilderImpl;
 import org.opendaylight.mdsal.binding.javav2.generator.yang.types.TypeProviderImpl;
 import org.opendaylight.mdsal.binding.javav2.model.api.Type;
 import org.opendaylight.mdsal.binding.javav2.model.api.type.builder.GeneratedTypeBuilder;
 import org.opendaylight.mdsal.binding.javav2.util.BindingMapping;
+import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
 import org.opendaylight.yangtools.yang.model.api.Module;
 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
 import org.opendaylight.yangtools.yang.model.api.NotificationNodeContainer;
@@ -52,10 +56,9 @@ final class ModuleToGenType {
                 genTypeBuilders, typeProvider);
         genCtx = actionsAndRPCMethodsToGenType(module, genCtx, schemaContext, verboseClassComments,
                 genTypeBuilders, typeProvider);
+        genCtx = allIdentitiesToGenTypes(module, schemaContext, genCtx, verboseClassComments,  genTypeBuilders, typeProvider);
         genCtx = notificationsToGenType(module, genCtx, schemaContext, genTypeBuilders, verboseClassComments, typeProvider);
 
-        //TODO: call generate for other entities (identities)
-
         if (!module.getChildNodes().isEmpty()) {
             final GeneratedTypeBuilder moduleType = GenHelperUtil.moduleToDataType(module, genCtx, verboseClassComments);
             genCtx.get(module).addModuleNode(moduleType);
@@ -113,6 +116,38 @@ final class ModuleToGenType {
         return genCtx;
     }
 
+    /**
+     * Converts all <b>identities</b> of the module to the list of
+     * <code>Type</code> objects.
+     *
+     * @param module
+     *            module from which is obtained set of all identity objects to
+     *            iterate over them
+     * @param schemaContext
+     *            schema context only used as input parameter for method
+     *            {@link GenHelperUtil#identityToGenType(Module, String, IdentitySchemaNode, SchemaContext, Map, boolean, Map, TypeProvider, Map)}
+     * @param genCtx generated context
+     * @return returns generated context
+     *
+     */
+    private static Map<Module, ModuleContext> allIdentitiesToGenTypes(final Module module,
+            final SchemaContext schemaContext, Map<Module, ModuleContext> genCtx, boolean verboseClassComments,
+            final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders, final TypeProvider typeProvider) {
+
+        final Set<IdentitySchemaNode> schemaIdentities = module.getIdentities();
+        final String basePackageName = BindingMapping.getRootPackageName(module);
+
+        if (schemaIdentities != null && !schemaIdentities.isEmpty()) {
+            Map<QName, GeneratedTOBuilderImpl> generatedIdentities = new HashMap<>();
+            for (final IdentitySchemaNode identity : schemaIdentities) {
+                GenHelperUtil.identityToGenType(module, basePackageName, identity, schemaContext, genCtx,
+                    verboseClassComments, genTypeBuilders, typeProvider, generatedIdentities);
+            }
+        }
+
+        return genCtx;
+    }
+
     /**
      * Converts all <b>notifications</b> of the module to the list of
      * <code>Type</code> objects. In addition are to this list added containers
diff --git a/binding2/mdsal-binding2-generator-impl/src/test/resources/identity/identity-import.yang b/binding2/mdsal-binding2-generator-impl/src/test/resources/identity/identity-import.yang
new file mode 100644 (file)
index 0000000..50ff359
--- /dev/null
@@ -0,0 +1,11 @@
+module identity-import {
+  yang-version 1.1;
+  namespace "identity:import";
+
+  prefix "ideimp";
+
+  revision 2017-06-02;
+
+  identity iden1 {
+  }
+}
\ No newline at end of file
diff --git a/binding2/mdsal-binding2-generator-impl/src/test/resources/identity/identity.yang b/binding2/mdsal-binding2-generator-impl/src/test/resources/identity/identity.yang
new file mode 100644 (file)
index 0000000..1a3a5fa
--- /dev/null
@@ -0,0 +1,27 @@
+module identity-module {
+  yang-version 1.1;
+  namespace "identity:module";
+
+  prefix "idemod";
+
+  revision 2017-06-01;
+
+  import identity-import {
+    prefix "ideimp";
+  }
+
+  identity iden1 {
+  }
+
+  identity iden2 {
+    base "idemod:iden1";
+  }
+
+  identity iden3 {
+    base "ideimp:iden1";
+  }
+
+  identity iden4 {
+    base "idemod:iden2";
+  }
+}
\ No newline at end of file
diff --git a/binding2/mdsal-binding2-generator-impl/src/test/resources/identity/identity2.yang b/binding2/mdsal-binding2-generator-impl/src/test/resources/identity/identity2.yang
new file mode 100644 (file)
index 0000000..686244d
--- /dev/null
@@ -0,0 +1,15 @@
+module identity2-module {
+  yang-version 1.1;
+  namespace "identity2:module";
+
+  prefix "ide2mod";
+
+  revision 2017-06-02;
+
+  identity iden1 {
+  }
+
+  identity iden2 {
+    base "ide2mod:iden1";
+  }
+}
\ No newline at end of file
index dc28c0a591a2d1c9f4b5ad7f8237a431b343b5c9..75055bc4e6abddadcb9dd7ea7f6d24ecc492b66b 100644 (file)
@@ -240,7 +240,8 @@ public class ClassRenderer extends BaseRenderer {
         }
         final String fields = sb2.toString();
         importedNames.put("baseEncoding", importedName(BaseEncoding.class));
-        importedNames.put("defProp", importedName(((GeneratedProperty)((List) allProperties).get(0)).getReturnType()));
+        if (!allProperties.isEmpty())
+            importedNames.put("defProp", importedName(((GeneratedProperty)((List) allProperties).get(0)).getReturnType()));
 
         final StringBuilder sb3 = new StringBuilder();
         for (GeneratedProperty property : properties) {
index bcd86c2bf5e9071005d8cd59979f567b696c8ef6..4d7d46026c80ec6a73c9479d174a1bfa1f002cd4 100644 (file)
@@ -63,8 +63,8 @@ fields: String, allProperties: List[GeneratedProperty], propertyMethod: String,
 }
 
 @generateClassDeclaration(isInnerCLass: Boolean) = {
-public @{if (isInnerCLass) " static final "
-else if(genType.isAbstract) " abstract "
+public @{if (isInnerCLass) "static final "
+else if(genType.isAbstract) "abstract "
 else " "
 }
 class @{genType.getName}
diff --git a/binding2/mdsal-binding2-spec/src/main/java/org/opendaylight/mdsal/binding/javav2/spec/base/BaseIdentity.java b/binding2/mdsal-binding2-spec/src/main/java/org/opendaylight/mdsal/binding/javav2/spec/base/BaseIdentity.java
new file mode 100644 (file)
index 0000000..ebf3a56
--- /dev/null
@@ -0,0 +1,16 @@
+/*
+ * Copyright (c) 2017 Pantheon Technologies s.r.o. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+
+package org.opendaylight.mdsal.binding.javav2.spec.base;
+
+/**
+ * Base Identity abstract class
+ *
+ */
+public abstract class BaseIdentity {
+}
\ No newline at end of file