Fixed union serialization and deserialization
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / main / java / org / opendaylight / controller / sal / binding / dom / serializer / impl / TransformerGenerator.xtend
index 87c3286f95db83e113a753207516ab40ade98c89..4271ef9c1a9bb5824f04673cbcf1e86f466c7c52 100644 (file)
@@ -827,6 +827,11 @@ class TransformerGenerator {
                 val ret = ctCls.toClassImpl(inputType.classLoader, inputType.protectionDomain)
                 return ret as Class<? extends BindingCodec<Map<QName,Object>, Object>>;
             }
+            if (returnType.name == 'char[]') {
+                val ctCls = createUnionImplementation(inputType, typeSpec);
+                val ret = ctCls.toClassImpl(inputType.classLoader, inputType.protectionDomain)
+                return ret as Class<? extends BindingCodec<Map<QName,Object>, Object>>;
+            }
 
             val ctCls = createClass(typeSpec.codecClassName) [
                 //staticField(Map,"AUGMENTATION_SERIALIZERS");
@@ -897,6 +902,73 @@ class TransformerGenerator {
 
     }
 
+    def createUnionImplementation(Class<?> inputType, GeneratedTransferObject typeSpec) {
+        return createClass(typeSpec.codecClassName) [
+            val properties = typeSpec.allProperties;
+            //staticField(Map,"AUGMENTATION_SERIALIZERS");
+            if (inputType.isYangBindingAvailable) {
+                implementsType(BINDING_CODEC)
+                staticField(it, INSTANCE_IDENTIFIER_CODEC, BindingCodec)
+                staticField(it, IDENTITYREF_CODEC, BindingCodec)
+                implementsType(BindingDeserializer.asCtClass)
+            }
+            method(Object, "toDomValue", Object) [
+                modifiers = PUBLIC + FINAL + STATIC
+                val ctSpec = inputType.asCtClass;
+                bodyChecked = '''
+                    {
+                        //System.out.println("«inputType.simpleName»#toDomValue: "+$1);
+                        
+                        if($1 == null) {
+                            return null;
+                        }
+                        «typeSpec.resolvedName» _value = («typeSpec.resolvedName») $1;
+                        «FOR property : properties.entrySet»
+                            «IF property.key != "getValue"»
+                                «property.value.resolvedName» «property.key» = («property.value.resolvedName») _value.«property.key»();
+                                if(«property.key» != null) { 
+                                    return «serializeValue(property.value, property.key)»;
+                                }
+                            «ENDIF»
+                        «ENDFOR»
+                        
+                        return null;
+                    }
+                '''
+            ]
+            method(Object, "serialize", Object) [
+                bodyChecked = '''
+                    {
+                        return toDomValue($1);
+                    }
+                '''
+            ]
+            method(Object, "fromDomValue", Object) [
+                modifiers = PUBLIC + FINAL + STATIC
+                bodyChecked = '''
+                    {
+                        //System.out.println("«inputType.simpleName»#fromDomValue: "+$1);
+                        
+                        if($1 == null) {
+                            return null;
+                        }
+                        if($1 instanceof String) {
+                            String _simpleValue = (String) $1;
+                            return new «typeSpec.resolvedName»(_simpleValue.toCharArray());
+                        }
+                        return null;
+                    }
+                '''
+            ]
+            method(Object, "deserialize", Object) [
+                bodyChecked = '''{
+                            return fromDomValue($1);
+                    }
+                    '''
+            ]
+        ]
+    }
+
     def boolean isYangBindingAvailable(Class<?> class1) {
         try {
             val bindingCodecClass = class1.classLoader.loadClass(BINDING_CODEC.name);
@@ -1221,6 +1293,9 @@ class TransformerGenerator {
         } else if (CLASS_TYPE.equals(signature)) {
             return '''(«QName.resolvedName») «IDENTITYREF_CODEC».serialize(«property»)'''
         }
+        if ("char[]" == signature.name) {
+            return '''new String(«property»)''';
+        }
         return '''«property»''';
     }