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();
}
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;
}
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;
}
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();
}
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();
}
Aggregations