Search in sources :

Example 6 with Field

use of org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model.Field in project controller by opendaylight.

the class GenericGeneratedObjectFactory method toGeneratedObject.

public GeneratedObject toGeneratedObject(FtlTemplate template, Optional<String> copyright) {
    JavaFileInputBuilder b = new JavaFileInputBuilder();
    b.setHeader(template.getHeaderString());
    b.setFqn(new FullyQualifiedName(template.getPackageName(), template.getTypeDeclaration().getName()));
    b.setClassJavaDoc(template.getMaybeJavadoc());
    for (Annotation annotation : template.getAnnotations()) {
        b.addClassAnnotation(annotation);
    }
    // type declaration
    for (String extended : template.getTypeDeclaration().getExtended()) {
        b.addExtendsFQN(FullyQualifiedName.fromString(extended));
    }
    for (String implemented : template.getTypeDeclaration().getImplemented()) {
        b.addImplementsFQN(FullyQualifiedName.fromString(implemented));
    }
    b.setCopyright(copyright);
    b.setTypeName(template.getTypeDeclaration().toTypeName());
    // fields
    for (Field field : template.getFields()) {
        b.addToBody(field.toString());
    }
    // constructors
    for (Constructor constructor : template.getConstructors()) {
        b.addToBody(constructor.toString());
    }
    // methods
    for (Method method : template.getMethods()) {
        b.addToBody(method.toString());
    }
    return new GeneratedObjectBuilder(b.build()).toGeneratedObject();
}
Also used : Field(org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model.Field) Constructor(org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model.Constructor) GeneratedObjectBuilder(org.opendaylight.controller.config.yangjmxgenerator.plugin.java.GeneratedObjectBuilder) FullyQualifiedName(org.opendaylight.controller.config.yangjmxgenerator.plugin.java.FullyQualifiedName) Method(org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model.Method) Annotation(org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model.Annotation) JavaFileInputBuilder(org.opendaylight.controller.config.yangjmxgenerator.plugin.java.JavaFileInputBuilder)

Example 7 with Field

use of org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model.Field in project controller by opendaylight.

the class RuntimeRegistratorFtlTemplate method createRegistrationHierarchy.

// TODO move to factory + RuntimeBeanEntry
/**
 * Create ftls representing registrations. First registration is represents
 * parent.
 *
 * @return map containing java class name as key, instance representing the
 *         java file as value.
 */
private static LinkedHashMap<String, RuntimeRegistratorFtlTemplate> createRegistrationHierarchy(RuntimeBeanEntry parent, Set<String> occupiedKeys) {
    LinkedHashMap<String, RuntimeRegistratorFtlTemplate> unorderedResult = new LinkedHashMap<>();
    List<MethodDefinition> methods = new ArrayList<>();
    // leaf list>: key or counter
    if (occupiedKeys.contains(parent.getJavaNamePrefix())) {
        throw new IllegalArgumentException("Name conflict in runtime bean hierarchy - java name found more than " + "once. Consider using java-name extension. Conflicting name: " + parent.getJavaNamePrefix());
    }
    Set<String> currentOccupiedKeys = new HashSet<>(occupiedKeys);
    currentOccupiedKeys.add(parent.getJavaNamePrefix());
    Field registratorsMapField = new Field(Collections.singletonList(Modifier.FINAL), TypeHelper.getGenericType(Map.class, String.class, AtomicInteger.class), "unkeyedMap", "new " + TypeHelper.getGenericType(HashMap.class, String.class, AtomicInteger.class) + "()");
    // create register methods for children
    for (RuntimeBeanEntry child : parent.getChildren()) {
        checkArgument(parent.getPackageName().equals(child.getPackageName()), "Invalid package name");
        // call itself recursively to generate child
        // registrators/registrations
        LinkedHashMap<String, RuntimeRegistratorFtlTemplate> childRegistratorMap = createRegistrationHierarchy(child, currentOccupiedKeys);
        for (Entry<String, RuntimeRegistratorFtlTemplate> entry : childRegistratorMap.entrySet()) {
            if (unorderedResult.containsKey(entry.getKey())) {
                throw new IllegalStateException("Conflicting name found while generating runtime registration:" + entry.getKey());
            }
            unorderedResult.put(entry.getKey(), entry.getValue());
        }
        if (!childRegistratorMap.isEmpty()) {
            // first entry is the direct descendant according to the create
            // contract
            RuntimeRegistratorFtlTemplate childRegistrator = childRegistratorMap.values().iterator().next();
            StringBuilder body = new StringBuilder();
            String key, value;
            key = child.getJavaNamePrefix();
            body.append(format("String key = \"%s\"; //TODO: check for conflicts\n", key));
            Optional<String> childKeyJavaName = child.getKeyJavaName();
            if (childKeyJavaName.isPresent()) {
                value = "bean.get" + childKeyJavaName.get() + "()";
                value = "String.valueOf(" + value + ")";
            } else {
                body.append("java.util.concurrent.atomic.AtomicInteger counter = unkeyedMap.get(key);\n" + "if (counter==null){\n" + "counter = new java.util.concurrent.atomic.AtomicInteger();\n" + "unkeyedMap.put(key, counter);\n" + "}\n");
                value = "String.valueOf(counter.incrementAndGet())";
            }
            body.append(format("String value = %s;\n", value));
            body.append(format("%s r = %s.register(key, value, bean);\n", HierarchicalRuntimeBeanRegistration.class.getCanonicalName(), hierachicalRegistration.getName()));
            body.append(format("return new %s(r);", childRegistrator.getFullyQualifiedName()));
            Field param = new Field(Collections.singletonList(Modifier.FINAL), child.getJavaNameOfRuntimeMXBean(), "bean");
            MethodDefinition register = new MethodDefinition(Collections.singletonList(Modifier.SYNCHRONIZED), childRegistrator.getFullyQualifiedName(), "register", Collections.singletonList(param), Collections.emptyList(), Collections.emptyList(), body.toString());
            methods.add(register);
        }
    }
    // create parent registration
    String createdName = getJavaNameOfRuntimeRegistration(parent.getJavaNamePrefix());
    List<Field> constructorParameters = Collections.singletonList(hierachicalRegistration);
    String constructorBody = constructConstructorBody(constructorParameters);
    MethodDefinition constructor = MethodDefinition.createConstructor(createdName, constructorParameters, constructorBody);
    MethodDefinition closeRegistrator = createCloseMethodToCloseField(hierachicalRegistration);
    methods.add(closeRegistrator);
    methods.add(constructor);
    List<Field> privateFields = Lists.newArrayList(registratorsMapField);
    privateFields.addAll(constructorParameters);
    RuntimeRegistratorFtlTemplate created = new RuntimeRegistratorFtlTemplate(parent, createdName, privateFields, methods);
    LinkedHashMap<String, RuntimeRegistratorFtlTemplate> result = new LinkedHashMap<>();
    result.put(created.getTypeDeclaration().getName(), created);
    checkState(!unorderedResult.containsKey(created.getTypeDeclaration().getName()), "Naming conflict: " + created.getTypeDeclaration().getName());
    result.putAll(unorderedResult);
    return result;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) RuntimeBeanEntry(org.opendaylight.controller.config.yangjmxgenerator.RuntimeBeanEntry) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) Field(org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model.Field) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MethodDefinition(org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model.MethodDefinition) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Aggregations

Field (org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model.Field)7 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 LinkedHashMap (java.util.LinkedHashMap)2 MethodDefinition (org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model.MethodDefinition)2 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 DependencyResolver (org.opendaylight.controller.config.api.DependencyResolver)1 DynamicMBeanWithInstance (org.opendaylight.controller.config.api.DynamicMBeanWithInstance)1 RootRuntimeBeanRegistrator (org.opendaylight.controller.config.api.runtime.RootRuntimeBeanRegistrator)1 Module (org.opendaylight.controller.config.spi.Module)1 RuntimeBeanEntry (org.opendaylight.controller.config.yangjmxgenerator.RuntimeBeanEntry)1 Rpc (org.opendaylight.controller.config.yangjmxgenerator.RuntimeBeanEntry.Rpc)1 AttributeIfc (org.opendaylight.controller.config.yangjmxgenerator.attribute.AttributeIfc)1 JavaAttribute (org.opendaylight.controller.config.yangjmxgenerator.attribute.JavaAttribute)1 Annotation (org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model.Annotation)1 Constructor (org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model.Constructor)1 IdentityRefModuleField (org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model.IdentityRefModuleField)1