Search in sources :

Example 1 with MethodDefinition

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

the class RuntimeRegistratorFtlTemplate method create.

// TODO Move to factory
/**
 * Get registrator and n registration ftls where n is equal to total number
 * of runtime beans in hierarchy.
 */
public static Map<String, FtlTemplate> create(RuntimeBeanEntry rootRB) {
    checkArgument(rootRB.isRoot(), "RuntimeBeanEntry must be root");
    String registratorName = getJavaNameOfRuntimeRegistrator(rootRB);
    List<MethodDefinition> methods = new ArrayList<>();
    Field rootRuntimeBeanRegistratorField = new Field(Collections.singletonList(Modifier.FINAL), RootRuntimeBeanRegistrator.class.getName(), "rootRuntimeBeanRegistrator");
    List<Field> constructorParameters = Lists.newArrayList(rootRuntimeBeanRegistratorField);
    String constructorBody = constructConstructorBody(constructorParameters);
    MethodDefinition constructor = MethodDefinition.createConstructor(registratorName, constructorParameters, constructorBody);
    methods.add(constructor);
    LinkedHashMap<String, RuntimeRegistratorFtlTemplate> RuntimeRegistratorFtlTemplates = createRegistrationHierarchy(rootRB, Collections.emptySet());
    RuntimeRegistratorFtlTemplate rootFtlFile = RuntimeRegistratorFtlTemplates.values().iterator().next();
    {
        // add register(rootruntimemxbean)
        String fullyQualifiedNameOfMXBean = FullyQualifiedNameHelper.getFullyQualifiedName(rootRB.getPackageName(), rootRB.getJavaNameOfRuntimeMXBean());
        String childRegistratorFQN = rootFtlFile.getFullyQualifiedName();
        Field rbParameter = new Field(fullyQualifiedNameOfMXBean, "rb");
        String registerBody = format("%s %s = this.%s.registerRoot(%s);\n" + "return new %s(%2$s);\n", HierarchicalRuntimeBeanRegistration.class.getCanonicalName(), hierachicalRegistration.getName(), rootRuntimeBeanRegistratorField.getName(), rbParameter.getName(), rootFtlFile.getFullyQualifiedName());
        MethodDefinition registerMethod = new MethodDefinition(childRegistratorFQN, "register", Collections.singletonList(rbParameter), registerBody);
        methods.add(registerMethod);
    }
    MethodDefinition closeRegistrator = createCloseMethodToCloseField(rootRuntimeBeanRegistratorField);
    methods.add(closeRegistrator);
    // TODO add header
    GeneralClassTemplate registrator = new GeneralClassTemplate(null, rootRB.getPackageName(), registratorName, Collections.emptyList(), Collections.singletonList(Closeable.class.getCanonicalName()), constructorParameters, methods);
    checkState(!RuntimeRegistratorFtlTemplates.containsKey(registrator.getTypeDeclaration().getName()), "Name conflict: " + registrator.getTypeDeclaration().getName());
    Map<String, FtlTemplate> result = new HashMap<>();
    result.putAll(RuntimeRegistratorFtlTemplates);
    result.put(registrator.getTypeDeclaration().getName(), registrator);
    return result;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) RootRuntimeBeanRegistrator(org.opendaylight.controller.config.api.runtime.RootRuntimeBeanRegistrator) Field(org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model.Field) MethodDefinition(org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model.MethodDefinition)

Example 2 with MethodDefinition

use of org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model.MethodDefinition 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)

Example 3 with MethodDefinition

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

the class RuntimeRegistratorFtlTemplate method createCloseMethodToCloseField.

private static MethodDefinition createCloseMethodToCloseField(Field field) {
    String body = field.getName() + ".close();";
    // TODO Thrown exception breaks build
    // return new MethodDefinition(Collections.<String> emptyList(), "void",
    // "close", Collections.<Field> emptyList(),
    // Arrays.asList(IOException.class.getCanonicalName()),
    // Collections.<Annotation> emptyList(), body);
    List<Annotation> annotations = Lists.newArrayList(new Annotation("Override", Collections.emptyList()));
    return new MethodDefinition(Collections.emptyList(), "void", "close", Collections.emptyList(), Collections.emptyList(), annotations, body);
}
Also used : MethodDefinition(org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model.MethodDefinition) Annotation(org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model.Annotation)

Aggregations

MethodDefinition (org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model.MethodDefinition)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 Field (org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model.Field)2 HashSet (java.util.HashSet)1 Map (java.util.Map)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 RootRuntimeBeanRegistrator (org.opendaylight.controller.config.api.runtime.RootRuntimeBeanRegistrator)1 RuntimeBeanEntry (org.opendaylight.controller.config.yangjmxgenerator.RuntimeBeanEntry)1 Annotation (org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model.Annotation)1