Search in sources :

Example 1 with RootRuntimeBeanRegistrator

use of org.opendaylight.controller.config.api.runtime.RootRuntimeBeanRegistrator 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)

Aggregations

ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 RootRuntimeBeanRegistrator (org.opendaylight.controller.config.api.runtime.RootRuntimeBeanRegistrator)1 Field (org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model.Field)1 MethodDefinition (org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model.MethodDefinition)1