Search in sources :

Example 1 with GeneratedPropertyBuilder

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

the class AbstractGeneratedTypeBuilder method addProperty.

@Override
public GeneratedPropertyBuilder addProperty(final String name) {
    checkArgument(name != null, "Parameter name can't be null");
    checkArgument(!containsProperty(name), "This generated type already contains property with the same name.");
    final GeneratedPropertyBuilder builder = new GeneratedPropertyBuilderImpl(name);
    builder.setAccessModifier(AccessModifier.PUBLIC);
    this.properties = LazyCollections.lazyAdd(this.properties, builder);
    return builder;
}
Also used : GeneratedPropertyBuilder(org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedPropertyBuilder)

Example 2 with GeneratedPropertyBuilder

use of org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedPropertyBuilder 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 3 with GeneratedPropertyBuilder

use of org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedPropertyBuilder 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 4 with GeneratedPropertyBuilder

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

the class Generator method addSerialVersionUID.

static final void addSerialVersionUID(final GeneratedTOBuilder gto) {
    final GeneratedPropertyBuilder prop = new GeneratedPropertyBuilderImpl("serialVersionUID");
    prop.setValue(Long.toString(SerialVersionHelper.computeDefaultSUID(gto)));
    gto.setSUID(prop);
}
Also used : GeneratedPropertyBuilder(org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedPropertyBuilder) GeneratedPropertyBuilderImpl(org.opendaylight.mdsal.binding.model.ri.generated.type.builder.GeneratedPropertyBuilderImpl)

Example 5 with GeneratedPropertyBuilder

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

the class SerialVersionHelper method computeDefaultSUID.

public static long computeDefaultSUID(final GeneratedTypeBuilderBase<?> to) {
    final ByteArrayOutputStream bout = new ByteArrayOutputStream();
    try (DataOutputStream dout = new DataOutputStream(bout)) {
        dout.writeUTF(to.getName());
        dout.writeInt(to.isAbstract() ? 3 : 7);
        for (final Type ifc : sortedCollection(SUID_NAME_COMPARATOR, filteredImplementsTypes(to))) {
            dout.writeUTF(ifc.getFullyQualifiedName());
        }
        for (final GeneratedPropertyBuilder gp : sortedCollection(SUID_MEMBER_COMPARATOR, to.getProperties())) {
            dout.writeUTF(gp.getName());
        }
        for (final MethodSignatureBuilder m : sortedCollection(SUID_MEMBER_COMPARATOR, to.getMethodDefinitions())) {
            if (!m.getAccessModifier().equals(AccessModifier.PRIVATE)) {
                dout.writeUTF(m.getName());
                dout.write(m.getAccessModifier().ordinal());
            }
        }
        dout.flush();
    } catch (final IOException e) {
        throw new IllegalStateException("Failed to hash object " + to, e);
    }
    final byte[] hashBytes = SHA1_MD.get().digest(bout.toByteArray());
    long hash = 0;
    for (int i = Math.min(hashBytes.length, 8) - 1; i >= 0; i--) {
        hash = hash << 8 | hashBytes[i] & 0xFF;
    }
    return hash;
}
Also used : Type(org.opendaylight.mdsal.binding.model.api.Type) ConcreteType(org.opendaylight.mdsal.binding.model.api.ConcreteType) GeneratedPropertyBuilder(org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedPropertyBuilder) DataOutputStream(java.io.DataOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) MethodSignatureBuilder(org.opendaylight.mdsal.binding.model.api.type.builder.MethodSignatureBuilder)

Aggregations

GeneratedPropertyBuilder (org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedPropertyBuilder)13 GeneratedTOBuilder (org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedTOBuilder)8 Test (org.junit.Test)5 GeneratedTransferObject (org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject)5 NonNull (org.eclipse.jdt.annotation.NonNull)3 ConcreteType (org.opendaylight.mdsal.binding.model.api.ConcreteType)3 GeneratedPropertyBuilderImpl (org.opendaylight.mdsal.binding.model.ri.generated.type.builder.GeneratedPropertyBuilderImpl)3 ArrayList (java.util.ArrayList)2 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)2 GeneratedProperty (org.opendaylight.mdsal.binding.model.api.GeneratedProperty)2 GeneratedType (org.opendaylight.mdsal.binding.model.api.GeneratedType)2 Type (org.opendaylight.mdsal.binding.model.api.Type)2 MethodSignatureBuilder (org.opendaylight.mdsal.binding.model.api.type.builder.MethodSignatureBuilder)2 CodegenGeneratedTOBuilder (org.opendaylight.mdsal.binding.model.ri.generated.type.builder.CodegenGeneratedTOBuilder)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