Map identities to proper objects
[mdsal.git] / binding / mdsal-binding-java-api-generator / src / main / java / org / opendaylight / mdsal / binding / java / api / generator / UnionTemplate.xtend
index 05b8a23e9d1f0b7daeb6bc43ec1261bf9d008995..600bb69cd2910f40d25d63795e668c580ea7eb60 100644 (file)
@@ -7,23 +7,27 @@
  */
 package org.opendaylight.mdsal.binding.java.api.generator
 
-import static org.opendaylight.mdsal.binding.model.util.Types.BOOLEAN;
-import static org.opendaylight.mdsal.binding.model.util.Types.BYTE_ARRAY;
-import static org.opendaylight.mdsal.binding.model.util.Types.STRING;
-import static org.opendaylight.mdsal.binding.model.util.Types.getOuterClassName;
+import static org.opendaylight.mdsal.binding.model.ri.BaseYangTypes.BINARY_TYPE
+import static org.opendaylight.mdsal.binding.model.ri.BaseYangTypes.BOOLEAN_TYPE
+import static org.opendaylight.mdsal.binding.model.ri.BaseYangTypes.EMPTY_TYPE
+import static org.opendaylight.mdsal.binding.model.ri.BaseYangTypes.STRING_TYPE
+import static org.opendaylight.mdsal.binding.model.ri.Types.STRING
+import static org.opendaylight.mdsal.binding.model.ri.Types.getOuterClassName
+import static org.opendaylight.mdsal.binding.spec.naming.BindingMapping.BINDING_CONTRACT_IMPLEMENTED_INTERFACE_NAME
+import static org.opendaylight.mdsal.binding.spec.naming.BindingMapping.BUILDER_SUFFIX
+import static extension org.opendaylight.mdsal.binding.model.ri.BindingTypes.isBitsType
+import static extension org.opendaylight.mdsal.binding.model.ri.BindingTypes.isIdentityType
 
-import java.util.Arrays
 import java.util.Base64;
-import org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject
+import org.gaul.modernizer_maven_annotations.SuppressModernizer
 import org.opendaylight.mdsal.binding.model.api.Enumeration
+import org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject
 import org.opendaylight.mdsal.binding.model.api.Type
-import org.opendaylight.mdsal.binding.model.util.Types
-import org.opendaylight.yangtools.yang.common.Empty
-import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition
 
 /**
  * Template for generating JAVA class.
  */
+@SuppressModernizer
 class UnionTemplate extends ClassTemplate {
     /**
      * Creates instance of this class with concrete <code>genType</code>.
@@ -67,7 +71,7 @@ class UnionTemplate extends ClassTemplate {
             public «type.name»(«propertyAndTopParentProperties.asArgumentsDeclaration») {
                 super(«parentProperties.asArguments»);
                 «IF restrictions !== null»
-                    «checkArgument(property, restrictions, actualType, property.fieldName.toString
+                    «checkArgument(property, restrictions, actualType, property.fieldName)»
                 «ENDIF»
                 this.«property.fieldName» = «property.fieldName»;
                 «FOR other : finalProperties»
@@ -81,10 +85,10 @@ class UnionTemplate extends ClassTemplate {
 
     def typeBuilder() {
         val outerCls = getOuterClassName(type);
-        if(outerCls !== null) {
-            return outerCls + type.name + "Builder"
+        if (outerCls !== null) {
+            return outerCls + type.name + BUILDER_SUFFIX
         }
-        return type.name + "Builder"
+        return type.name + BUILDER_SUFFIX
     }
 
     private def unionConstructorsParentProperties() '''
@@ -102,49 +106,52 @@ class UnionTemplate extends ClassTemplate {
          *
          * @return String representation of this union's value.
          */
-        public «String.importedName» stringValue() {
+        public «STRING.importedName» stringValue() {
             «FOR property : finalProperties»
                 «val field = property.fieldName»
             if («field» != null) {
                 «val propRet = property.returnType»
-                «IF STRING.equals(propRet)»
+                «IF STRING_TYPE.equals(propRet)»
                     ««« type string
                 return «field»;
                 «ELSEIF "org.opendaylight.yangtools.yang.binding.InstanceIdentifier".equals(propRet.fullyQualifiedName)»
                     ««« type instance-identifier
                 return «field».toString();
-                «ELSEIF BYTE_ARRAY.equals(propRet)»
+                «ELSEIF BINARY_TYPE.equals(propRet)»
                     ««« type binary
-                return new «String.importedName»(«field»);
-                «ELSEIF propRet.fullyQualifiedName.startsWith("java.lang") || propRet instanceof Enumeration
-                        || propRet.fullyQualifiedName.startsWith("java.math")»
-                    ««« type int*, uint, decimal64 or enumeration*
+                return new «STRING.importedName»(«field»);
+                «ELSEIF propRet.fullyQualifiedName.startsWith("java.lang") || propRet instanceof Enumeration»
+                    ««« type int* or enumeration*
                 return «field».toString();
+                «ELSEIF "org.opendaylight.yangtools.yang.common".equals(propRet.packageName)
+                        && (propRet.name.startsWith("Uint") || "Decimal64".equals(propRet.name))»
+                    ««« type uint*, decimal64
+                return «field».toCanonicalString();
                 «ELSEIF propRet instanceof GeneratedTransferObject && (propRet as GeneratedTransferObject).unionType»
                     ««« union type
                 return «field».stringValue();
-                «ELSEIF BOOLEAN.equals(propRet.typedefReturnType)»
+                «ELSEIF BOOLEAN_TYPE.equals(propRet.typedefReturnType)»
                     ««« generated boolean typedef
                 return «field».isValue().toString();
-                «ELSEIF BYTE_ARRAY.equals(propRet.typedefReturnType)»
+                «ELSEIF BINARY_TYPE.equals(propRet.typedefReturnType)»
                     ««« generated byte[] typedef
                 return «Base64.importedName».getEncoder().encodeToString(«field».getValue());
-                «ELSEIF Constants.EMPTY.equals(propRet) || Constants.EMPTY.equals(propRet.typedefReturnType)»
+                «ELSEIF EMPTY_TYPE.equals(propRet) || EMPTY_TYPE.equals(propRet.typedefReturnType)»
                     ««« generated empty typedef
                 return "";
-                «ELSEIF propRet instanceof GeneratedTransferObject // Is it a GeneratedTransferObject
-                        && (propRet as GeneratedTransferObject).typedef  // Is it a typedef
-                        && (propRet as GeneratedTransferObject).baseType instanceof BitsTypeDefinition»
+                «ELSEIF propRet.isBitsType»
                     ««« generated bits typedef
-                return «Arrays.importedName».toString(«field».getValue());
+                return «JU_ARRAYS.importedName».toString(«field».getValue());
+                «ELSEIF propRet.isIdentityType»
+                    ««« generated identity
+                return «field».«BINDING_CONTRACT_IMPLEMENTED_INTERFACE_NAME»().toString();
                 «ELSE»
                     ««« generated type
                 return «field».getValue().toString();
                 «ENDIF»
             }
             «ENDFOR»
-
-            throw new IllegalStateException("No value assinged");
+            throw new IllegalStateException("No value assigned");
         }
     '''
 
@@ -174,13 +181,13 @@ class UnionTemplate extends ClassTemplate {
                 super(source);
             «ENDIF»
             «FOR p : properties»
-                «IF p.returnType.importedName.contains("[]")»
-                this.«p.fieldName» = source.«p.fieldName» == null ? null : source.«p.fieldName».clone();
+                «val fieldName = p.fieldName»
+                «IF p.returnType.name.endsWith("[]")»
+                this.«fieldName» = source.«fieldName» == null ? null : source.«fieldName».clone();
                 «ELSE»
-                this.«p.fieldName» = source.«p.fieldName»;
+                this.«fieldName» = source.«fieldName»;
                 «ENDIF»
             «ENDFOR»
         }
     '''
-
 }