Search in sources :

Example 1 with Field

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

the class RuntimeRegistratorFtlTemplate method constructConstructorBody.

private static String constructConstructorBody(List<Field> constructorParameters) {
    StringBuilder constructorBody = new StringBuilder();
    for (Field field : constructorParameters) {
        constructorBody.append("this.");
        constructorBody.append(field.getName());
        constructorBody.append("=");
        constructorBody.append(field.getName());
        constructorBody.append(";\n");
    }
    return constructorBody.toString();
}
Also used : Field(org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model.Field)

Example 2 with Field

use of org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model.Field 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 3 with Field

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

the class TemplateFactory method getTOAndMXInterfaceFtlFiles.

/**
 * Get map of file name as key, FtlFile instance representing runtime mx
 * bean as value that should be persisted from this instance.
 */
public static Map<String, FtlTemplate> getTOAndMXInterfaceFtlFiles(final RuntimeBeanEntry entry) {
    final Map<String, FtlTemplate> result = new HashMap<>();
    {
        // create GeneralInterfaceFtlFile for runtime MXBean. Attributes will
        // be transformed to getter methods
        final String mxBeanTypeName = entry.getJavaNameOfRuntimeMXBean();
        final List<String> extendedInterfaces = Collections.singletonList(RuntimeBean.class.getCanonicalName());
        final List<MethodDeclaration> methods = new ArrayList<>();
        // convert attributes to getters
        for (final AttributeIfc attributeIfc : entry.getAttributes()) {
            String returnType;
            returnType = getReturnType(attributeIfc);
            final String getterName = "get" + attributeIfc.getUpperCaseCammelCase();
            final MethodDeclaration getter = new MethodDeclaration(returnType, getterName, Collections.<Field>emptyList());
            methods.add(getter);
        }
        // add rpc methods
        for (final Rpc rpc : entry.getRpcs()) {
            // convert JavaAttribute parameters into fields
            final List<Field> fields = new ArrayList<>();
            for (final JavaAttribute ja : rpc.getParameters()) {
                final Field field = new Field(Collections.emptyList(), ja.getType().getFullyQualifiedName(), ja.getLowerCaseCammelCase(), ja.getNullableDefaultWrappedForCode());
                fields.add(field);
            }
            final MethodDeclaration operation = new MethodDeclaration(getReturnType(rpc.getReturnType()), rpc.getName(), fields);
            methods.add(operation);
        }
        // FIXME header
        final GeneralInterfaceTemplate runtimeMxBeanIfc = new GeneralInterfaceTemplate(null, entry.getPackageName(), mxBeanTypeName, extendedInterfaces, methods);
        result.put(runtimeMxBeanIfc.getTypeDeclaration().getName() + ".java", runtimeMxBeanIfc);
    }
    result.putAll(TemplateFactory.tOsFromRbe(entry));
    return result;
}
Also used : ModuleField(org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model.ModuleField) Field(org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model.Field) IdentityRefModuleField(org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model.IdentityRefModuleField) Rpc(org.opendaylight.controller.config.yangjmxgenerator.RuntimeBeanEntry.Rpc) HashMap(java.util.HashMap) MethodDeclaration(org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model.MethodDeclaration) AttributeIfc(org.opendaylight.controller.config.yangjmxgenerator.attribute.AttributeIfc) ArrayList(java.util.ArrayList) List(java.util.List) JavaAttribute(org.opendaylight.controller.config.yangjmxgenerator.attribute.JavaAttribute)

Example 4 with Field

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

the class AbsFactoryGeneratedObjectFactory method getCreateModule.

private static String getCreateModule(FullyQualifiedName moduleFQN, List<Field> moduleFields) {
    StringBuilder result = new StringBuilder("\n" + "@Override\n");
    result.append(format("public %s createModule(String instanceName, %s dependencyResolver, %s old, %s bundleContext) " + "throws Exception {\n", Module.class.getCanonicalName(), DependencyResolver.class.getCanonicalName(), DynamicMBeanWithInstance.class.getCanonicalName(), BUNDLE_CONTEXT)).append(format("%s oldModule;\n", moduleFQN)).append("try {\n").append(format("oldModule = (%s) old.getModule();\n", moduleFQN)).append("} catch(Exception e) {\n" + "return handleChangedClass(dependencyResolver, old, bundleContext);\n" + "}\n").append(format("%s module = instantiateModule(instanceName, dependencyResolver, oldModule, old" + ".getInstance(), bundleContext);\n", moduleFQN));
    for (Field field : moduleFields) {
        result.append(format("module.set%s(oldModule.get%1$s());\n", field.getName()));
    }
    result.append("\n" + "return module;\n" + "}\n");
    return result.toString();
}
Also used : Field(org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model.Field) Module(org.opendaylight.controller.config.spi.Module) DynamicMBeanWithInstance(org.opendaylight.controller.config.api.DynamicMBeanWithInstance) DependencyResolver(org.opendaylight.controller.config.api.DependencyResolver)

Example 5 with Field

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

the class AbsFactoryGeneratedObjectFactory method genCodeToCopyAttributes.

private String genCodeToCopyAttributes(List<Field> moduleFields) {
    StringBuilder sb = new StringBuilder("\n");
    for (Field field : moduleFields) {
        sb.append(format("newModule.set%1$s( (%2$s) oldModuleClass.getMethod(\"get%1$s\").invoke(oldModule));\n", field.getName(), field.getType()));
    }
    sb.append('\n');
    return sb.toString();
}
Also used : Field(org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model.Field)

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