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 fca2477..4a74ce6 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 BINARY_TYPE = Types.primitiveType("byte[]");
 
     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("binary", BINARY_TYPE);
     }
 
     public static final TypeProvider BASE_YANG_TYPES_PROVIDER = new TypeProvider() {
index b3739ec..03824ef 100644 (file)
@@ -87,12 +87,10 @@ public final class TypeProviderImpl implements TypeProvider {
             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().getLocalName() == null) {
             throw new IllegalArgumentException("Type Definitions Local Name " +
                     "cannot be NULL!");
@@ -136,6 +134,12 @@ public final class TypeProviderImpl implements TypeProvider {
                         .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;
     }
 
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
+    }
+}
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 3c00dee..93be5a6 100644 (file)
@@ -15,5 +15,21 @@ public interface TypeProvider {
     @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
index 2966246..26be943 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
-    \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
+    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
index a059c13..bb4bbba 100644 (file)
@@ -528,7 +528,12 @@ public final class GeneratorUtil {
                        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;
@@ -645,7 +650,8 @@ public final class GeneratorUtil {
                        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);

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.