Merge "Fixed union serialization and deserialization"
authorEd Warnicke <eaw@cisco.com>
Fri, 13 Dec 2013 19:54:40 +0000 (19:54 +0000)
committerGerrit Code Review <gerrit@opendaylight.org>
Fri, 13 Dec 2013 19:54:40 +0000 (19:54 +0000)
opendaylight/md-sal/sal-binding-broker/pom.xml
opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/sal/binding/dom/serializer/impl/TransformerGenerator.xtend
opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/sal/binding/impl/util/ClassLoaderUtils.java
opendaylight/md-sal/sal-binding-broker/src/test/java/org/opendaylight/controller/sal/binding/test/bugfix/UnionSerializationTest.java [new file with mode: 0644]

index f9f72094f816d8166c0067f7e1e1aff0829a2aac..1b0f78384f1a6c166b46fed6f4f89152e276c77c 100644 (file)
             <artifactId>ietf-inet-types</artifactId>
             <version>2010.09.24.2-SNAPSHOT</version>
         </dependency>
+        <dependency>
+        <groupId>org.opendaylight.yangtools.model</groupId>
+        <artifactId>ietf-topology-l3-unicast-igp</artifactId>
+        <version>2013.10.21.0-SNAPSHOT</version>
+        <scope>test</scope>
+        </dependency>
         <dependency>
                <groupId>org.opendaylight.controller.model</groupId>
                <artifactId>model-flow-base</artifactId>
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»''';
     }
 
index d230fd17f9475c24bd920094a233734291593901..e0eb10299256a9d6ff48e0bb305f15c57fee1848 100644 (file)
@@ -56,6 +56,8 @@ public final class ClassLoaderUtils {
     public static Class<?> loadClassWithTCCL(String name) throws ClassNotFoundException {
         if ("byte[]".equals(name)) {
             return byte[].class;
+        } else if("char[]".equals(name)) {
+            return char[].class;
         }
         try {
             return Thread.currentThread().getContextClassLoader().loadClass(name);
@@ -78,8 +80,7 @@ public final class ClassLoaderUtils {
         try {
             return loadClassWithTCCL(fullyQualifiedName);
         } catch (ClassNotFoundException e) {
-
+            return null;
         }
-        return null;
     }
 }
\ No newline at end of file
diff --git a/opendaylight/md-sal/sal-binding-broker/src/test/java/org/opendaylight/controller/sal/binding/test/bugfix/UnionSerializationTest.java b/opendaylight/md-sal/sal-binding-broker/src/test/java/org/opendaylight/controller/sal/binding/test/bugfix/UnionSerializationTest.java
new file mode 100644 (file)
index 0000000..2057619
--- /dev/null
@@ -0,0 +1,45 @@
+package org.opendaylight.controller.sal.binding.test.bugfix;
+
+import org.junit.Test;
+import org.opendaylight.controller.sal.binding.test.AbstractDataServiceTest;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpPrefix;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
+import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.igp.node.attributes.igp.node.attributes.Prefix;
+import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.igp.node.attributes.igp.node.attributes.PrefixBuilder;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.CompositeNode;
+
+
+
+
+
+
+
+
+import static org.junit.Assert.*;
+
+public class UnionSerializationTest extends AbstractDataServiceTest {
+    
+    public static final String PREFIX_STRING = "192.168.0.1/32";
+    
+    
+    @Test
+    public void testPrefixSerialization() throws Exception {
+        
+        Ipv4Prefix ipv4prefix = new Ipv4Prefix(PREFIX_STRING);
+        IpPrefix ipPrefix = new IpPrefix(ipv4prefix);
+        Prefix prefix = new PrefixBuilder().setPrefix(ipPrefix).build();
+        
+        CompositeNode serialized = testContext.getBindingToDomMappingService().toDataDom(prefix);
+        assertNotNull(serialized);
+        assertNotNull(serialized.getFirstSimpleByName(Prefix.QNAME));
+        assertEquals(PREFIX_STRING, serialized.getFirstSimpleByName(Prefix.QNAME).getValue());
+        
+        Prefix deserialized = (Prefix) testContext.getBindingToDomMappingService().dataObjectFromDataDom(InstanceIdentifier.builder().node(Prefix.class).build(), serialized);
+        assertNotNull(deserialized);
+        assertNotNull(deserialized.getPrefix());
+        assertNotNull(deserialized.getPrefix().getIpv4Prefix());
+        assertEquals(PREFIX_STRING, deserialized.getPrefix().getIpv4Prefix().getValue());
+    }
+
+}