Fix YANG snippet escaping
[mdsal.git] / binding / mdsal-binding-java-api-generator / src / main / java / org / opendaylight / mdsal / binding / java / api / generator / UnionTemplate.xtend
index 84e9d78f665b750f94a0cb63371878b1cff1b442..9808196a298ebfc278cdf795a305dd625b03326d 100644 (file)
@@ -8,19 +8,21 @@
 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 com.google.common.io.BaseEncoding
 import java.util.Arrays
+import java.util.Base64;
 import org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject
 import org.opendaylight.mdsal.binding.model.api.Enumeration
+import org.opendaylight.mdsal.binding.model.api.Type
 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition
 
 /**
  * Template for generating JAVA class.
  */
 class UnionTemplate extends ClassTemplate {
-
     /**
      * Creates instance of this class with concrete <code>genType</code>.
      *
@@ -103,13 +105,13 @@ class UnionTemplate extends ClassTemplate {
                 «val field = property.fieldName»
             if («field» != null) {
                 «val propRet = property.returnType»
-                «IF "java.lang.String".equals(propRet.fullyQualifiedName
+                «IF STRING.equals(propRet
                     ««« type string
                 return «field»;
                 «ELSEIF "org.opendaylight.yangtools.yang.binding.InstanceIdentifier".equals(propRet.fullyQualifiedName)»
                     ««« type instance-identifier
                 return «field».toString();
-                «ELSEIF "byte[]".equals(propRet.name
+                «ELSEIF BYTE_ARRAY.equals(propRet
                     ««« type binary
                 return new «String.importedName»(«field»);
                 «ELSEIF propRet.fullyQualifiedName.startsWith("java.lang") || propRet instanceof Enumeration
@@ -119,24 +121,15 @@ class UnionTemplate extends ClassTemplate {
                 «ELSEIF propRet instanceof GeneratedTransferObject && (propRet as GeneratedTransferObject).unionType»
                     ««« union type
                 return «field».stringValue();
-                «ELSEIF propRet instanceof GeneratedTransferObject // Is it a GeneratedTransferObject
-                        && (propRet as GeneratedTransferObject).typedef  // Is it a typedef
-                        && (propRet as GeneratedTransferObject).properties !== null
-                        && !(propRet as GeneratedTransferObject).properties.empty
-                        && ((propRet as GeneratedTransferObject).properties.size == 1)
-                        && (propRet as GeneratedTransferObject).properties.get(0).name.equals("value")
-                        && BOOLEAN.equals((propRet as GeneratedTransferObject).properties.get(0).returnType) // And the property value is of type boolean»
+                «ELSEIF BOOLEAN.equals(propRet.typedefReturnType)»
                     ««« generated boolean typedef
                 return «field».isValue().toString();
-                «ELSEIF propRet instanceof GeneratedTransferObject // Is it a GeneratedTransferObject
-                        && (propRet as GeneratedTransferObject).typedef  // Is it a typedef
-                        && (propRet as GeneratedTransferObject).properties !== null
-                        && !(propRet as GeneratedTransferObject).properties.empty
-                        && ((propRet as GeneratedTransferObject).properties.size == 1)
-                        && (propRet as GeneratedTransferObject).properties.get(0).name.equals("value")
-                        && "byte[]".equals((propRet as GeneratedTransferObject).properties.get(0).returnType.name)»
+                «ELSEIF BYTE_ARRAY.equals(propRet.typedefReturnType)»
                     ««« generated byte[] typedef
-                return «BaseEncoding.importedName».base64().encode(«field».getValue());
+                return «Base64.importedName».getEncoder().encodeToString(«field».getValue());
+                «ELSEIF Constants.EMPTY.equals(propRet) || Constants.EMPTY.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»
@@ -153,6 +146,21 @@ class UnionTemplate extends ClassTemplate {
         }
     '''
 
+    private static def Type typedefReturnType(Type type) {
+        if (!(type instanceof GeneratedTransferObject)) {
+            return null
+        }
+        val gto = type as GeneratedTransferObject
+        if (!gto.typedef || gto.properties === null || gto.properties.size != 1) {
+            return null
+        }
+        val prop = gto.properties.get(0)
+        if (prop.name.equals("value")) {
+            return prop.returnType
+        }
+        return null
+    }
+
     override protected copyConstructor() '''
         /**
          * Creates a copy from Source Object.
@@ -164,10 +172,11 @@ 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»
         }