Search in sources :

Example 1 with GeneratedTOBuilder

use of org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedTOBuilder in project mdsal by opendaylight.

the class AnnotationBuilderTest method generatedPropertyAnnotationTest.

@Test
public void generatedPropertyAnnotationTest() {
    final GeneratedTOBuilder genTOBuilder = new CodegenGeneratedTOBuilder(JavaTypeName.create("org.opendaylight.controller", "AnnotInterface"));
    final GeneratedPropertyBuilder propertyBuilder = genTOBuilder.addProperty("simpleProperty");
    propertyBuilder.setReturnType(Types.typeForClass(Integer.class));
    final AnnotationTypeBuilder annotManAttr = propertyBuilder.addAnnotation("org.springframework.jmx.export.annotation", "ManagedAttribute");
    annotManAttr.addParameter("description", "\"The Name Attribute\"");
    annotManAttr.addParameter("currencyTimeLimit", "20");
    annotManAttr.addParameter("defaultValue", "\"bar\"");
    annotManAttr.addParameter("persistPolicy", "\"OnUpdate\"");
    final AnnotationTypeBuilder annotManProp = propertyBuilder.addAnnotation("org.springframework.jmx.export.annotation", "ManagedOperation");
    final List<String> typeValues = new ArrayList<>();
    typeValues.add("\"val1\"");
    typeValues.add("\"val2\"");
    typeValues.add("\"val3\"");
    annotManProp.addParameters("types", typeValues);
    final GeneratedTransferObject genTransObj = genTOBuilder.build();
    assertNotNull(genTransObj);
    assertNotNull(genTransObj.getAnnotations());
    assertNotNull(genTransObj.getProperties());
    assertNotNull(genTransObj.getProperties().get(0));
    assertNotNull(genTransObj.getProperties().get(0).getAnnotations());
    final List<AnnotationType> annotations = genTransObj.getProperties().get(0).getAnnotations();
    assertEquals(2, annotations.size());
    int annotCount = 0;
    for (final AnnotationType annotation : annotations) {
        if (annotation.getPackageName().equals("org.springframework.jmx.export.annotation") && annotation.getName().equals("ManagedAttribute")) {
            annotCount++;
            assertEquals(4, annotation.getParameters().size());
            assertNotNull(annotation.getParameter("description"));
            assertNotNull(annotation.getParameter("currencyTimeLimit"));
            assertNotNull(annotation.getParameter("defaultValue"));
            assertNotNull(annotation.getParameter("persistPolicy"));
            assertEquals("\"The Name Attribute\"", annotation.getParameter("description").getValue());
            assertEquals("20", annotation.getParameter("currencyTimeLimit").getValue());
            assertEquals("\"bar\"", annotation.getParameter("defaultValue").getValue());
            assertEquals("\"OnUpdate\"", annotation.getParameter("persistPolicy").getValue());
        }
        if (annotation.getPackageName().equals("org.springframework.jmx.export.annotation") && annotation.getName().equals("ManagedOperation")) {
            annotCount++;
            assertEquals(1, annotation.getParameters().size());
            assertNotNull(annotation.getParameter("types"));
            assertEquals(3, annotation.getParameter("types").getValues().size());
        }
    }
    assertEquals(2, annotCount);
}
Also used : AnnotationTypeBuilder(org.opendaylight.mdsal.binding.model.api.type.builder.AnnotationTypeBuilder) GeneratedPropertyBuilder(org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedPropertyBuilder) GeneratedTOBuilder(org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedTOBuilder) ArrayList(java.util.ArrayList) GeneratedTransferObject(org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject) AnnotationType(org.opendaylight.mdsal.binding.model.api.AnnotationType) Test(org.junit.Test)

Example 2 with GeneratedTOBuilder

use of org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedTOBuilder in project mdsal by opendaylight.

the class GeneratedTOBuilderTest method testToString.

@Test
public void testToString() {
    final GeneratedTOBuilder genTypeBuilder = new CodegenGeneratedTOBuilder(JavaTypeName.create("org.opendaylight.controller", "AnnotClassCache"));
    assertThat(genTypeBuilder.toString(), startsWith("CodegenGeneratedTOBuilder{identifier=org.opendaylight.controller.AnnotClassCache"));
}
Also used : GeneratedTOBuilder(org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedTOBuilder) Test(org.junit.Test)

Example 3 with GeneratedTOBuilder

use of org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedTOBuilder in project mdsal by opendaylight.

the class GeneratedTypeBuilderTest method addEnclosingTransferObjectTest.

@Test
public void addEnclosingTransferObjectTest() {
    GeneratedTypeBuilder generatedTypeBuilder = new CodegenGeneratedTypeBuilder(JavaTypeName.create("my.package", "MyName"));
    GeneratedTOBuilder enclosingTransferObject = new CodegenGeneratedTOBuilder(generatedTypeBuilder.getIdentifier().createEnclosed("myTOName"));
    GeneratedTOBuilder enclosingTransferObject2 = new CodegenGeneratedTOBuilder(generatedTypeBuilder.getIdentifier().createEnclosed("myTOName2"));
    GeneratedTOBuilder enclosingTransferObject3 = new CodegenGeneratedTOBuilder(generatedTypeBuilder.getIdentifier().createEnclosed("myTOName3"));
    generatedTypeBuilder.addEnclosingTransferObject(enclosingTransferObject.build());
    generatedTypeBuilder.addEnclosingTransferObject(enclosingTransferObject2.build());
    generatedTypeBuilder.addEnclosingTransferObject(enclosingTransferObject3.build());
    GeneratedType instance = generatedTypeBuilder.build();
    List<GeneratedType> enclosedTypes = instance.getEnclosedTypes();
    assertEquals(3, enclosedTypes.size());
    assertTrue(enclosedTypes.contains(enclosingTransferObject.build()));
    assertTrue(enclosedTypes.contains(enclosingTransferObject2.build()));
    assertTrue(enclosedTypes.contains(enclosingTransferObject3.build()));
    assertFalse(enclosedTypes.contains(new CodegenGeneratedTOBuilder(generatedTypeBuilder.getIdentifier().createEnclosed("myTOName4")).build()));
}
Also used : GeneratedTypeBuilder(org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedTypeBuilder) GeneratedType(org.opendaylight.mdsal.binding.model.api.GeneratedType) GeneratedTOBuilder(org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedTOBuilder) Test(org.junit.Test)

Example 4 with GeneratedTOBuilder

use of org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedTOBuilder in project mdsal by opendaylight.

the class AbstractTypeObjectGenerator method createSimple.

@NonNull
private static GeneratedType createSimple(final TypeBuilderFactory builderFactory, final JavaTypeName typeName, final ModuleGenerator module, final Type javaType, final TypeDefinition<?> typedef) {
    final String moduleName = module.statement().argument().getLocalName();
    final GeneratedTOBuilder builder = builderFactory.newGeneratedTOBuilder(typeName);
    builder.setTypedef(true);
    builder.addImplementsType(BindingTypes.scalarTypeObject(javaType));
    final GeneratedPropertyBuilder genPropBuilder = builder.addProperty(TypeConstants.VALUE_PROP);
    genPropBuilder.setReturnType(javaType);
    builder.addEqualsIdentity(genPropBuilder);
    builder.addHashIdentity(genPropBuilder);
    builder.addToStringProperty(genPropBuilder);
    builder.setRestrictions(BindingGeneratorUtil.getRestrictions(typedef));
    // builder.setSchemaPath(typedef.getPath());
    builder.setModuleName(moduleName);
    builderFactory.addCodegenInformation(typedef, builder);
    annotateDeprecatedIfNecessary(typedef, builder);
    if (javaType instanceof ConcreteType && // FIXME: This looks very suspicious: we should by checking for Types.STRING
    "String".equals(javaType.getName()) && typedef.getBaseType() != null) {
        addStringRegExAsConstant(builder, resolveRegExpressions(typedef));
    }
    addUnits(builder, typedef);
    makeSerializable(builder);
    return builder.build();
}
Also used : GeneratedPropertyBuilder(org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedPropertyBuilder) GeneratedTOBuilder(org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedTOBuilder) ConcreteType(org.opendaylight.mdsal.binding.model.api.ConcreteType) NonNull(org.eclipse.jdt.annotation.NonNull)

Example 5 with GeneratedTOBuilder

use of org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedTOBuilder in project mdsal by opendaylight.

the class AbstractTypeObjectGenerator method restrictType.

@NonNull
private static Type restrictType(@NonNull final Type baseType, final Restrictions restrictions, final TypeBuilderFactory builderFactory) {
    if (restrictions == null || restrictions.isEmpty()) {
        // No additional restrictions, return base type
        return baseType;
    }
    if (!(baseType instanceof GeneratedTransferObject)) {
        // This is a simple Java type, just wrap it with new restrictions
        return Types.restrictedType(baseType, restrictions);
    }
    // Base type is a GTO, we need to re-adjust it with new restrictions
    final GeneratedTransferObject gto = (GeneratedTransferObject) baseType;
    final GeneratedTOBuilder builder = builderFactory.newGeneratedTOBuilder(gto.getIdentifier());
    final GeneratedTransferObject parent = gto.getSuperType();
    if (parent != null) {
        builder.setExtendsType(parent);
    }
    builder.setRestrictions(restrictions);
    for (GeneratedProperty gp : gto.getProperties()) {
        builder.addProperty(gp.getName()).setValue(gp.getValue()).setReadOnly(gp.isReadOnly()).setAccessModifier(gp.getAccessModifier()).setReturnType(gp.getReturnType()).setFinal(gp.isFinal()).setStatic(gp.isStatic());
    }
    return builder.build();
}
Also used : GeneratedTOBuilder(org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedTOBuilder) GeneratedTransferObject(org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject) GeneratedProperty(org.opendaylight.mdsal.binding.model.api.GeneratedProperty) NonNull(org.eclipse.jdt.annotation.NonNull)

Aggregations

GeneratedTOBuilder (org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedTOBuilder)14 Test (org.junit.Test)8 GeneratedTransferObject (org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject)8 GeneratedPropertyBuilder (org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedPropertyBuilder)8 NonNull (org.eclipse.jdt.annotation.NonNull)4 ArrayList (java.util.ArrayList)3 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)2 AnnotationType (org.opendaylight.mdsal.binding.model.api.AnnotationType)2 ConcreteType (org.opendaylight.mdsal.binding.model.api.ConcreteType)2 GeneratedProperty (org.opendaylight.mdsal.binding.model.api.GeneratedProperty)2 GeneratedType (org.opendaylight.mdsal.binding.model.api.GeneratedType)2 AnnotationTypeBuilder (org.opendaylight.mdsal.binding.model.api.type.builder.AnnotationTypeBuilder)2 QName (org.opendaylight.yangtools.yang.common.QName)2 BitsTypeDefinition (org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition)2 Bit (org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition.Bit)2 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)1 Verify.verify (com.google.common.base.Verify.verify)1 Verify.verifyNotNull (com.google.common.base.Verify.verifyNotNull)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 Maps (com.google.common.collect.Maps)1