Added Support for conversion yang binary type to java type; 77/477/1
authorlsedlak <lsedlak@cisco.com>
Thu, 13 Jun 2013 13:30:01 +0000 (15:30 +0200)
committerlsedlak <lsedlak@cisco.com>
Thu, 13 Jun 2013 14:54:32 +0000 (16:54 +0200)
Added support for generation of primitive java types;
Added support for conversion yang binary type to java byte array;

Updated toString method in AbstractBaseType in case that primitive type does not contain package name;

Added documentation into TypeProvider javaTypeForSchemaDefinitionType method;
Updated GeneratorUtil methods to not generate fully qualified names or imports in case of generating of java primitive types;

Signed-off-by: Lukas Sedlak <lsedlak@cisco.com>
opendaylight/sal/yang-prototype/code-generator/binding-generator-impl/src/main/java/org/opendaylight/controller/sal/binding/yang/types/BaseYangTypes.java
opendaylight/sal/yang-prototype/code-generator/binding-generator-impl/src/main/java/org/opendaylight/controller/sal/binding/yang/types/TypeProviderImpl.java
opendaylight/sal/yang-prototype/code-generator/binding-generator-impl/src/test/java/org/opendaylight/controller/sal/binding/generator/impl/AugmentRleativeXPathTest.java
opendaylight/sal/yang-prototype/code-generator/binding-generator-impl/src/test/java/org/opendaylight/controller/sal/binding/generator/impl/BinaryTypeTest.java [new file with mode: 0644]
opendaylight/sal/yang-prototype/code-generator/binding-generator-impl/src/test/java/org/opendaylight/controller/sal/binding/generator/impl/UnionTypeDefTest.java
opendaylight/sal/yang-prototype/code-generator/binding-generator-impl/src/test/resources/binary-type-test-models/binary-type-test.yang [new file with mode: 0644]
opendaylight/sal/yang-prototype/code-generator/binding-generator-impl/src/test/resources/binary-type-test-models/binary-types@2013-06-13.yang [new file with mode: 0644]
opendaylight/sal/yang-prototype/code-generator/binding-generator-spi/src/main/java/org/opendaylight/controller/sal/binding/generator/spi/TypeProvider.java
opendaylight/sal/yang-prototype/code-generator/binding-generator-util/src/main/java/org/opendaylight/controller/binding/generator/util/AbstractBaseType.java
opendaylight/sal/yang-prototype/code-generator/binding-generator-util/src/main/java/org/opendaylight/controller/binding/generator/util/Types.java
opendaylight/sal/yang-prototype/code-generator/binding-java-api-generator/src/main/java/org/opendaylight/controller/sal/java/api/generator/GeneratorUtil.java

index fca2477640c8022892a7a528acc325964b86acf0..4a74ce6a9eca339893092f47776f40901f613bbe 100644 (file)
@@ -32,6 +32,7 @@ public final class BaseYangTypes {
     public static final Type UINT16_TYPE = Types.typeForClass(Integer.class);
     public static final Type UINT32_TYPE = Types.typeForClass(Long.class);
     public static final Type UINT64_TYPE = Types.typeForClass(BigInteger.class);
     public static final Type UINT16_TYPE = Types.typeForClass(Integer.class);
     public static final Type UINT32_TYPE = Types.typeForClass(Long.class);
     public static final Type UINT64_TYPE = Types.typeForClass(BigInteger.class);
+    public static final Type BINARY_TYPE = Types.primitiveType("byte[]");
 
     static {
         typeMap.put("boolean", BOOLEAN_TYPE);
 
     static {
         typeMap.put("boolean", BOOLEAN_TYPE);
@@ -46,6 +47,7 @@ public final class BaseYangTypes {
         typeMap.put("uint16", UINT16_TYPE);
         typeMap.put("uint32", UINT32_TYPE);
         typeMap.put("uint64", UINT64_TYPE);
         typeMap.put("uint16", UINT16_TYPE);
         typeMap.put("uint32", UINT32_TYPE);
         typeMap.put("uint64", UINT64_TYPE);
+        typeMap.put("binary", BINARY_TYPE);
     }
 
     public static final TypeProvider BASE_YANG_TYPES_PROVIDER = new TypeProvider() {
     }
 
     public static final TypeProvider BASE_YANG_TYPES_PROVIDER = new TypeProvider() {
index b3739ec5c44d5812a708139603cd4104ff738d67..03824efed06ed868709cf696e96d231e74a57c12 100644 (file)
@@ -87,12 +87,10 @@ public final class TypeProviderImpl implements TypeProvider {
             throw new IllegalArgumentException("Type Definition cannot be " +
                     "NULL!");
         }
             throw new IllegalArgumentException("Type Definition cannot be " +
                     "NULL!");
         }
-
         if (typeDefinition.getQName() == null) {
             throw new IllegalArgumentException("Type Definition cannot have " +
                     "non specified QName (QName cannot be NULL!)");
         }
         if (typeDefinition.getQName() == null) {
             throw new IllegalArgumentException("Type Definition cannot have " +
                     "non specified QName (QName cannot be NULL!)");
         }
-
         if (typeDefinition.getQName().getLocalName() == null) {
             throw new IllegalArgumentException("Type Definitions Local Name " +
                     "cannot be NULL!");
         if (typeDefinition.getQName().getLocalName() == null) {
             throw new IllegalArgumentException("Type Definitions Local Name " +
                     "cannot be NULL!");
@@ -136,6 +134,12 @@ public final class TypeProviderImpl implements TypeProvider {
                         .javaTypeForSchemaDefinitionType(typeDefinition);
             }
         }
                         .javaTypeForSchemaDefinitionType(typeDefinition);
             }
         }
+        //TODO: add throw exception when we will be able to resolve ALL yang
+        // types!
+//        if (returnType == null) {
+//            throw new IllegalArgumentException("Type Provider can't resolve " +
+//                    "type for specified Type Definition " + typedefName);
+//        }
         return returnType;
     }
 
         return returnType;
     }
 
index 185d95b35dbe9dbb3d248b3ea528e7d4e4aa32da..aac64e8ed17eb7a991fce7ccaf29f79d9edfa9ec 100644 (file)
@@ -53,5 +53,7 @@ public class AugmentRleativeXPathTest {
 
         assertNotNull("genTypes is null", genTypes);
         assertFalse("genTypes is empty", genTypes.isEmpty());
 
         assertNotNull("genTypes is null", genTypes);
         assertFalse("genTypes is empty", genTypes.isEmpty());
+
+        //TODO: implement test
     }
 }
     }
 }
diff --git a/opendaylight/sal/yang-prototype/code-generator/binding-generator-impl/src/test/java/org/opendaylight/controller/sal/binding/generator/impl/BinaryTypeTest.java b/opendaylight/sal/yang-prototype/code-generator/binding-generator-impl/src/test/java/org/opendaylight/controller/sal/binding/generator/impl/BinaryTypeTest.java
new file mode 100644 (file)
index 0000000..a3d2d27
--- /dev/null
@@ -0,0 +1,57 @@
+/*
+* Copyright (c) 2013 Cisco Systems, Inc. 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.controller.sal.binding.generator.impl;
+
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.opendaylight.controller.sal.binding.generator.api.BindingGenerator;
+import org.opendaylight.controller.sal.binding.model.api.Type;
+import org.opendaylight.controller.yang.model.api.Module;
+import org.opendaylight.controller.yang.model.api.SchemaContext;
+import org.opendaylight.controller.yang.model.parser.api.YangModelParser;
+import org.opendaylight.controller.yang.parser.impl.YangParserImpl;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Set;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+
+public class BinaryTypeTest {
+    private final static List<File> yangModels = new ArrayList<>();
+    private final static String yangModelsFolder = AugmentedTypeTest.class
+            .getResource("/binary-type-test-models").getPath();
+
+    @BeforeClass
+    public static void loadTestResources() {
+        final File augFolder = new File(yangModelsFolder);
+        for (final File fileEntry : augFolder.listFiles()) {
+            if (fileEntry.isFile()) {
+                yangModels.add(fileEntry);
+            }
+        }
+    }
+
+    @Test
+    public void binaryTypeTest() {
+        final YangModelParser parser = new YangParserImpl();
+        final Set<Module> modules = parser.parseYangModels(yangModels);
+        final SchemaContext context = parser.resolveSchemaContext(modules);
+
+        assertNotNull("context is null", context);
+        final BindingGenerator bindingGen = new BindingGeneratorImpl();
+        final List<Type> genTypes = bindingGen.generateTypes(context);
+
+        assertNotNull("genTypes is null", genTypes);
+        assertFalse("genTypes is empty", genTypes.isEmpty());
+
+        //TODO: implement test
+    }
+}
index 31092b0c027f14294d6a1e00e8fbe45c349e525b..7ff628325e05a0bbcf0402fae67cb8ed5f34970a 100644 (file)
@@ -52,5 +52,7 @@ public class UnionTypeDefTest {
 
         assertNotNull("genTypes is null", genTypes);
         assertFalse("genTypes is empty", genTypes.isEmpty());
 
         assertNotNull("genTypes is null", genTypes);
         assertFalse("genTypes is empty", genTypes.isEmpty());
+
+        //TODO: implement test
     }
 }
     }
 }
diff --git a/opendaylight/sal/yang-prototype/code-generator/binding-generator-impl/src/test/resources/binary-type-test-models/binary-type-test.yang b/opendaylight/sal/yang-prototype/code-generator/binding-generator-impl/src/test/resources/binary-type-test-models/binary-type-test.yang
new file mode 100644 (file)
index 0000000..f0cb123
--- /dev/null
@@ -0,0 +1,57 @@
+module binary-type-test {
+    yang-version 1;
+    namespace "urn:binary:types:model";
+    prefix "btt";
+
+    import binary-types {
+        prefix "bin";
+        revision-date 2013-06-13;
+    }
+
+    organization "OPEN DAYLIGHT";
+    contact "http://www.opendaylight.org/";
+
+    description
+        "Simple test to test imported binary types and resolving of binary
+        type in leaf statement.";
+
+    revision "2013-06-13" {
+        reference "NO REF";
+    }
+
+    typedef binary-type {
+        type binary {
+            length 128;
+        }
+    }
+
+    container container-foo {
+        leaf binary-leaf {
+            type binary {
+                length 128;
+            }
+        }
+
+        list binary-list {
+            key "binary-key";
+
+            leaf binary-key {
+                type btt:binary-type;
+            }
+
+            leaf imported-simple-binary {
+                type bin:simple-binary;
+            }
+        }
+
+        leaf imported-restrict-binary {
+            type bin:restricted-binary;
+        }
+
+        leaf-list binary-list {
+            type binary {
+                length 256;
+            }
+        }
+    }
+}
\ No newline at end of file
diff --git a/opendaylight/sal/yang-prototype/code-generator/binding-generator-impl/src/test/resources/binary-type-test-models/binary-types@2013-06-13.yang b/opendaylight/sal/yang-prototype/code-generator/binding-generator-impl/src/test/resources/binary-type-test-models/binary-types@2013-06-13.yang
new file mode 100644 (file)
index 0000000..df55441
--- /dev/null
@@ -0,0 +1,38 @@
+ module binary-types {
+
+   namespace "urn:binary:types";
+   prefix "bin";
+
+   organization "OPEN DAYLIGHT";
+
+   contact "http://www.opendaylight.org/";
+
+   description "Stand alone binary types declaration file for testing
+   purposes only.";
+
+   revision 2013-06-13 {
+     description
+      "Initial revision.";
+     reference
+      "NO REFERENCE";
+   }
+
+   /*** collection of protocol field related types ***/
+
+   typedef simple-binary {
+     type binary;
+   }
+
+   typedef restricted-binary {
+     type binary {
+        length 24;
+     }
+   }
+
+   typedef composite-binary {
+     type union {
+        type bin:simple-binary;
+        type bin:restricted-binary;
+     }
+   }
+ }
index 3c00dee98ea30e95cb4547ffbb1b29cc5f2386f0..93be5a667ca23307002dc7ad73ff1234a94a91a8 100644 (file)
@@ -15,5 +15,21 @@ public interface TypeProvider {
     @Deprecated\r
     Type javaTypeForYangType(String type);\r
 \r
     @Deprecated\r
     Type javaTypeForYangType(String type);\r
 \r
+    /**\r
+     * Resolve of yang Type Definition to it's java counter part.\r
+     * If the Type Definition contains one of yang primitive types the method\r
+     * will return java.lang. counterpart. (For example if yang type is int32\r
+     * the java counterpart is java.lang.Integer). In case that Type\r
+     * Definition contains extended type defined via yang typedef statement\r
+     * the method SHOULD return Generated Type or Generated Transfer Object\r
+     * if that Type is correctly referenced to resolved imported yang module.\r
+     * The method will return <cdoe>null</cdoe> value in situations that\r
+     * TypeDefinition can't be resolved (either due missing yang import or\r
+     * incorrectly specified type).\r
+     *\r
+     *\r
+     * @param type Type Definition to resolve from\r
+     * @return Resolved Type\r
+     */\r
     Type javaTypeForSchemaDefinitionType(final TypeDefinition<?> type);\r
 }\r
     Type javaTypeForSchemaDefinitionType(final TypeDefinition<?> type);\r
 }\r
index 6683230d028ef8c419b05413d0828a31fc84f772..a9d1cf5227c4efb575debadfb52eb288f64032b7 100644 (file)
@@ -63,7 +63,9 @@ public class AbstractBaseType implements Type {
 \r
     @Override\r
     public String toString() {\r
 \r
     @Override\r
     public String toString() {\r
-\r
+        if (packageName.isEmpty()) {\r
+            return "Type (" + name + ")";\r
+        }\r
         return "Type (" + packageName + "." + name + ")";\r
     }\r
 \r
         return "Type (" + packageName + "." + name + ")";\r
     }\r
 \r
index 2966246601ed3c8c0fe2ede8b1720a3dde8aed50..26be94351c67b9db724d7fd27a44abfbf26c9051 100644 (file)
@@ -22,17 +22,18 @@ public final class Types {
     private static final Type SET_TYPE = typeForClass(Set.class);\r
     private static final Type LIST_TYPE = typeForClass(List.class);\r
     private static final Type MAP_TYPE = typeForClass(Map.class);\r
     private static final Type SET_TYPE = typeForClass(Set.class);\r
     private static final Type LIST_TYPE = typeForClass(List.class);\r
     private static final Type MAP_TYPE = typeForClass(Map.class);\r
-    \r
     public static final Type DATA_OBJECT = typeForClass(DataObject.class);\r
     public static final Type DATA_OBJECT = typeForClass(DataObject.class);\r
-    \r
-    private Types() {\r
-    }\r
 \r
     public static ConcreteType voidType() {\r
         return new ConcreteTypeImpl(Void.class.getPackage().getName(),\r
                 Void.class.getSimpleName());\r
     }\r
 \r
 \r
     public static ConcreteType voidType() {\r
         return new ConcreteTypeImpl(Void.class.getPackage().getName(),\r
                 Void.class.getSimpleName());\r
     }\r
 \r
+    public static final Type primitiveType(final String primitiveType) {\r
+        return new ConcreteTypeImpl("", primitiveType);\r
+    }\r
+\r
+\r
     /**\r
      * Returns an instance of {@link ConcreteType} describing the class\r
      * \r
     /**\r
      * Returns an instance of {@link ConcreteType} describing the class\r
      * \r
index a059c13b069712b76447d60c4708d3b2f7eb96ad..bb4bbbaf789191e29ff6e87a446595f84ee5bd5c 100644 (file)
@@ -528,7 +528,12 @@ public final class GeneratorUtil {
                        if (packageName.startsWith("java.lang")) {
                                builder.append(type.getName());
                        } else {
                        if (packageName.startsWith("java.lang")) {
                                builder.append(type.getName());
                        } else {
-                               builder.append(packageName + "." + type.getName());
+                if (!packageName.isEmpty()) {
+                    builder.append(packageName + "." + type.getName());
+                } else {
+                    builder.append(type.getName());
+                }
+
                        }
                        if (type instanceof ParameterizedType) {
                                ParameterizedType pType = (ParameterizedType) type;
                        }
                        if (type instanceof ParameterizedType) {
                                ParameterizedType pType = (ParameterizedType) type;
@@ -645,7 +650,8 @@ public final class GeneratorUtil {
                        String genTypePkg) {
                String typeName = type.getName();
                String typePkg = type.getPackageName();
                        String genTypePkg) {
                String typeName = type.getName();
                String typePkg = type.getPackageName();
-               if (typePkg.startsWith("java.lang") || typePkg.equals(genTypePkg)) {
+               if (typePkg.startsWith("java.lang") || typePkg.equals(genTypePkg) ||
+                typePkg.isEmpty()) {
                        return;
                }
                LinkedHashMap<String, Integer> packages = importedTypes.get(typeName);
                        return;
                }
                LinkedHashMap<String, Integer> packages = importedTypes.get(typeName);