From 951731766152e865c55c8907f1f066aa581862c5 Mon Sep 17 00:00:00 2001 From: lsedlak Date: Thu, 13 Jun 2013 15:30:01 +0200 Subject: [PATCH] Added Support for conversion yang binary type to java type; 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 --- .../sal/binding/yang/types/BaseYangTypes.java | 2 + .../binding/yang/types/TypeProviderImpl.java | 8 ++- .../impl/AugmentRleativeXPathTest.java | 2 + .../generator/impl/BinaryTypeTest.java | 57 +++++++++++++++++++ .../generator/impl/UnionTypeDefTest.java | 2 + .../binary-type-test.yang | 57 +++++++++++++++++++ .../binary-types@2013-06-13.yang | 38 +++++++++++++ .../binding/generator/spi/TypeProvider.java | 16 ++++++ .../generator/util/AbstractBaseType.java | 4 +- .../binding/generator/util/Types.java | 9 +-- .../sal/java/api/generator/GeneratorUtil.java | 10 +++- 11 files changed, 196 insertions(+), 9 deletions(-) create mode 100644 opendaylight/sal/yang-prototype/code-generator/binding-generator-impl/src/test/java/org/opendaylight/controller/sal/binding/generator/impl/BinaryTypeTest.java create mode 100644 opendaylight/sal/yang-prototype/code-generator/binding-generator-impl/src/test/resources/binary-type-test-models/binary-type-test.yang create mode 100644 opendaylight/sal/yang-prototype/code-generator/binding-generator-impl/src/test/resources/binary-type-test-models/binary-types@2013-06-13.yang diff --git a/opendaylight/sal/yang-prototype/code-generator/binding-generator-impl/src/main/java/org/opendaylight/controller/sal/binding/yang/types/BaseYangTypes.java b/opendaylight/sal/yang-prototype/code-generator/binding-generator-impl/src/main/java/org/opendaylight/controller/sal/binding/yang/types/BaseYangTypes.java index fca2477640..4a74ce6a9e 100644 --- a/opendaylight/sal/yang-prototype/code-generator/binding-generator-impl/src/main/java/org/opendaylight/controller/sal/binding/yang/types/BaseYangTypes.java +++ b/opendaylight/sal/yang-prototype/code-generator/binding-generator-impl/src/main/java/org/opendaylight/controller/sal/binding/yang/types/BaseYangTypes.java @@ -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() { diff --git a/opendaylight/sal/yang-prototype/code-generator/binding-generator-impl/src/main/java/org/opendaylight/controller/sal/binding/yang/types/TypeProviderImpl.java b/opendaylight/sal/yang-prototype/code-generator/binding-generator-impl/src/main/java/org/opendaylight/controller/sal/binding/yang/types/TypeProviderImpl.java index b3739ec5c4..03824efed0 100644 --- a/opendaylight/sal/yang-prototype/code-generator/binding-generator-impl/src/main/java/org/opendaylight/controller/sal/binding/yang/types/TypeProviderImpl.java +++ b/opendaylight/sal/yang-prototype/code-generator/binding-generator-impl/src/main/java/org/opendaylight/controller/sal/binding/yang/types/TypeProviderImpl.java @@ -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/AugmentRleativeXPathTest.java b/opendaylight/sal/yang-prototype/code-generator/binding-generator-impl/src/test/java/org/opendaylight/controller/sal/binding/generator/impl/AugmentRleativeXPathTest.java index 185d95b35d..aac64e8ed1 100644 --- a/opendaylight/sal/yang-prototype/code-generator/binding-generator-impl/src/test/java/org/opendaylight/controller/sal/binding/generator/impl/AugmentRleativeXPathTest.java +++ b/opendaylight/sal/yang-prototype/code-generator/binding-generator-impl/src/test/java/org/opendaylight/controller/sal/binding/generator/impl/AugmentRleativeXPathTest.java @@ -53,5 +53,7 @@ public class AugmentRleativeXPathTest { 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 index 0000000000..a3d2d27a93 --- /dev/null +++ b/opendaylight/sal/yang-prototype/code-generator/binding-generator-impl/src/test/java/org/opendaylight/controller/sal/binding/generator/impl/BinaryTypeTest.java @@ -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 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 modules = parser.parseYangModels(yangModels); + final SchemaContext context = parser.resolveSchemaContext(modules); + + assertNotNull("context is null", context); + final BindingGenerator bindingGen = new BindingGeneratorImpl(); + final List 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/java/org/opendaylight/controller/sal/binding/generator/impl/UnionTypeDefTest.java b/opendaylight/sal/yang-prototype/code-generator/binding-generator-impl/src/test/java/org/opendaylight/controller/sal/binding/generator/impl/UnionTypeDefTest.java index 31092b0c02..7ff628325e 100644 --- a/opendaylight/sal/yang-prototype/code-generator/binding-generator-impl/src/test/java/org/opendaylight/controller/sal/binding/generator/impl/UnionTypeDefTest.java +++ b/opendaylight/sal/yang-prototype/code-generator/binding-generator-impl/src/test/java/org/opendaylight/controller/sal/binding/generator/impl/UnionTypeDefTest.java @@ -52,5 +52,7 @@ public class UnionTypeDefTest { 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 index 0000000000..f0cb1236ae --- /dev/null +++ b/opendaylight/sal/yang-prototype/code-generator/binding-generator-impl/src/test/resources/binary-type-test-models/binary-type-test.yang @@ -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 index 0000000000..df554419f3 --- /dev/null +++ b/opendaylight/sal/yang-prototype/code-generator/binding-generator-impl/src/test/resources/binary-type-test-models/binary-types@2013-06-13.yang @@ -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; + } + } + } diff --git a/opendaylight/sal/yang-prototype/code-generator/binding-generator-spi/src/main/java/org/opendaylight/controller/sal/binding/generator/spi/TypeProvider.java b/opendaylight/sal/yang-prototype/code-generator/binding-generator-spi/src/main/java/org/opendaylight/controller/sal/binding/generator/spi/TypeProvider.java index 3c00dee98e..93be5a667c 100644 --- a/opendaylight/sal/yang-prototype/code-generator/binding-generator-spi/src/main/java/org/opendaylight/controller/sal/binding/generator/spi/TypeProvider.java +++ b/opendaylight/sal/yang-prototype/code-generator/binding-generator-spi/src/main/java/org/opendaylight/controller/sal/binding/generator/spi/TypeProvider.java @@ -15,5 +15,21 @@ public interface TypeProvider { @Deprecated Type javaTypeForYangType(String type); + /** + * Resolve of yang Type Definition to it's java counter part. + * If the Type Definition contains one of yang primitive types the method + * will return java.lang. counterpart. (For example if yang type is int32 + * the java counterpart is java.lang.Integer). In case that Type + * Definition contains extended type defined via yang typedef statement + * the method SHOULD return Generated Type or Generated Transfer Object + * if that Type is correctly referenced to resolved imported yang module. + * The method will return null value in situations that + * TypeDefinition can't be resolved (either due missing yang import or + * incorrectly specified type). + * + * + * @param type Type Definition to resolve from + * @return Resolved Type + */ Type javaTypeForSchemaDefinitionType(final TypeDefinition type); } diff --git a/opendaylight/sal/yang-prototype/code-generator/binding-generator-util/src/main/java/org/opendaylight/controller/binding/generator/util/AbstractBaseType.java b/opendaylight/sal/yang-prototype/code-generator/binding-generator-util/src/main/java/org/opendaylight/controller/binding/generator/util/AbstractBaseType.java index 6683230d02..a9d1cf5227 100644 --- a/opendaylight/sal/yang-prototype/code-generator/binding-generator-util/src/main/java/org/opendaylight/controller/binding/generator/util/AbstractBaseType.java +++ b/opendaylight/sal/yang-prototype/code-generator/binding-generator-util/src/main/java/org/opendaylight/controller/binding/generator/util/AbstractBaseType.java @@ -63,7 +63,9 @@ public class AbstractBaseType implements Type { @Override public String toString() { - + if (packageName.isEmpty()) { + return "Type (" + name + ")"; + } return "Type (" + packageName + "." + name + ")"; } diff --git a/opendaylight/sal/yang-prototype/code-generator/binding-generator-util/src/main/java/org/opendaylight/controller/binding/generator/util/Types.java b/opendaylight/sal/yang-prototype/code-generator/binding-generator-util/src/main/java/org/opendaylight/controller/binding/generator/util/Types.java index 2966246601..26be94351c 100644 --- a/opendaylight/sal/yang-prototype/code-generator/binding-generator-util/src/main/java/org/opendaylight/controller/binding/generator/util/Types.java +++ b/opendaylight/sal/yang-prototype/code-generator/binding-generator-util/src/main/java/org/opendaylight/controller/binding/generator/util/Types.java @@ -22,17 +22,18 @@ public final class Types { private static final Type SET_TYPE = typeForClass(Set.class); private static final Type LIST_TYPE = typeForClass(List.class); private static final Type MAP_TYPE = typeForClass(Map.class); - public static final Type DATA_OBJECT = typeForClass(DataObject.class); - - private Types() { - } public static ConcreteType voidType() { return new ConcreteTypeImpl(Void.class.getPackage().getName(), Void.class.getSimpleName()); } + public static final Type primitiveType(final String primitiveType) { + return new ConcreteTypeImpl("", primitiveType); + } + + /** * Returns an instance of {@link ConcreteType} describing the class * diff --git a/opendaylight/sal/yang-prototype/code-generator/binding-java-api-generator/src/main/java/org/opendaylight/controller/sal/java/api/generator/GeneratorUtil.java b/opendaylight/sal/yang-prototype/code-generator/binding-java-api-generator/src/main/java/org/opendaylight/controller/sal/java/api/generator/GeneratorUtil.java index a059c13b06..bb4bbbaf78 100644 --- a/opendaylight/sal/yang-prototype/code-generator/binding-java-api-generator/src/main/java/org/opendaylight/controller/sal/java/api/generator/GeneratorUtil.java +++ b/opendaylight/sal/yang-prototype/code-generator/binding-java-api-generator/src/main/java/org/opendaylight/controller/sal/java/api/generator/GeneratorUtil.java @@ -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 packages = importedTypes.get(typeName); -- 2.36.6