use of org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model.ModuleField in project controller by opendaylight.
the class TemplateFactory method abstractModuleTemplateFromMbe.
public static AbstractModuleTemplate abstractModuleTemplateFromMbe(final ModuleMXBeanEntry mbe) {
final AbstractModuleAttributesProcessor attrProcessor = new AbstractModuleAttributesProcessor(mbe.getAttributes());
final List<ModuleField> moduleFields = attrProcessor.getModuleFields();
final List<String> implementedIfcs = Lists.newArrayList(mbe.getFullyQualifiedName(mbe.getMXBeanInterfaceName()));
for (final String implementedService : mbe.getProvidedServices().keySet()) {
implementedIfcs.add(implementedService);
}
boolean generateRuntime = false;
String registratorFullyQualifiedName = null;
if (mbe.getRuntimeBeans() != null && !mbe.getRuntimeBeans().isEmpty()) {
generateRuntime = true;
final RuntimeBeanEntry rootEntry = RuntimeRegistratorFtlTemplate.findRoot(mbe.getRuntimeBeans());
registratorFullyQualifiedName = rootEntry.getPackageName().concat(".").concat(RuntimeRegistratorFtlTemplate.getJavaNameOfRuntimeRegistrator(rootEntry));
implementedIfcs.add(RuntimeBeanRegistratorAwareModule.class.getCanonicalName());
}
final List<String> extendedClasses = Collections.singletonList(AbstractModule.class.getCanonicalName() + "<" + mbe.getAbstractModuleName() + ">");
final AbstractModuleTemplate abstractModuleTemplate = new AbstractModuleTemplate(getHeaderFromEntry(mbe), mbe.getPackageName(), mbe.getAbstractModuleName(), extendedClasses, implementedIfcs, moduleFields, attrProcessor.getMethods(), generateRuntime, registratorFullyQualifiedName);
if (mbe.getNullableDescription() != null) {
abstractModuleTemplate.getAnnotations().add(Annotation.createDescriptionAnnotation(mbe.getNullableDescription()));
}
return abstractModuleTemplate;
}
use of org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model.ModuleField in project controller by opendaylight.
the class AbsModuleGeneratedObjectFactory method toGeneratedObject.
public GeneratedObject toGeneratedObject(FullyQualifiedName abstractFQN, Optional<String> copyright, Optional<String> header, Optional<String> classJavaDoc, Optional<FullyQualifiedName> extended, List<FullyQualifiedName> implementedInterfaces, List<ModuleField> moduleFields, Optional<FullyQualifiedName> maybeRegistratorType, List<? extends Method> methods, QName yangModuleQName) {
JavaFileInputBuilder b = new JavaFileInputBuilder();
Annotation moduleQNameAnnotation = Annotation.createModuleQNameANnotation(yangModuleQName);
b.addClassAnnotation(moduleQNameAnnotation);
b.setFqn(abstractFQN);
b.setTypeName(TypeName.absClassType);
b.setCopyright(copyright);
b.setHeader(header);
b.setClassJavaDoc(classJavaDoc);
for (FullyQualifiedName implemented : implementedInterfaces) {
b.addImplementsFQN(implemented);
}
if (extended.isPresent()) {
b.addExtendsFQN(extended.get());
}
if (classJavaDoc.isPresent()) {
b.addClassAnnotation(format("@%s(value=\"%s\")", Description.class.getCanonicalName(), classJavaDoc.get()));
}
// add logger:
b.addToBody(getLoggerDefinition(abstractFQN));
b.addToBody("//attributes start");
for (ModuleField moduleField : moduleFields) {
b.addToBody(moduleField.toString() + "\n");
}
b.addToBody("//attributes end");
b.addToBody(getNewConstructor(abstractFQN));
b.addToBody(getCopyFromOldConstructor(abstractFQN));
b.addToBody(getRuntimeRegistratorCode(maybeRegistratorType));
b.addToBody(getValidationMethods(moduleFields));
b.addToBody(getCachesOfResolvedDependencies(moduleFields));
b.addToBody(getCachesOfResolvedIdentityRefs(moduleFields));
b.addToBody(getResolveDependencies(moduleFields));
b.addToBody(getReuseLogic(moduleFields, abstractFQN));
b.addToBody(getEqualsAndHashCode(abstractFQN));
b.addToBody(getMethods(methods));
b.addToBody(getGetLogger());
return new GeneratedObjectBuilder(b.build()).toGeneratedObject();
}
use of org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model.ModuleField in project controller by opendaylight.
the class AbsModuleGeneratedObjectFactory method getResolveDependencies.
private static String getResolveDependencies(final List<ModuleField> moduleFields) {
// loop through dependent fields, use dependency resolver to instantiate dependencies. Do it in loop in case field represents list of dependencies.
Map<ModuleField, String> resolveDependenciesMap = new HashMap<>();
for (ModuleField moduleField : moduleFields) {
if (moduleField.isDependent()) {
String str;
String osgi = moduleField.getDependency().getSie().getExportedOsgiClassName();
if (moduleField.isList()) {
str = format("%sDependency = new java.util.ArrayList<%s>();\n" + "for(javax.management.ObjectName dep : %1$s) {\n" + "%1$sDependency.add(dependencyResolver.resolveInstance(%2$s.class, dep, %1$sJmxAttribute));\n" + "}\n", moduleField.getName(), osgi);
} else {
str = format("%1$sDependency = dependencyResolver.resolveInstance(%2$s.class, %1$s, %1$sJmxAttribute);\n", moduleField.getName(), osgi);
}
resolveDependenciesMap.put(moduleField, str);
}
}
StringBuilder result = new StringBuilder("\n" + "protected final void resolveDependencies() {\n");
// wrap each field resolvation statement with if !=null when dependency is not mandatory
for (Map.Entry<ModuleField, String> entry : resolveDependenciesMap.entrySet()) {
if (!entry.getKey().getDependency().isMandatory()) {
checkState(entry.getValue().endsWith(";\n"));
result.append(format("if (%s!=null) {\n%s}\n", entry.getKey().getName(), entry.getValue()));
} else {
result.append(entry.getValue());
}
}
// add code to inject dependency resolver to fields that support it
for (ModuleField moduleField : moduleFields) {
if (moduleField.isNeedsDepResolver()) {
result.append(format("if (%s!=null){\n", moduleField.getName()));
if (moduleField.isList()) {
result.append(format("for(%s candidate : %s) {\n" + "candidate.injectDependencyResolver(dependencyResolver);\n" + "}\n", moduleField.getGenericInnerType(), moduleField.getName()));
} else {
result.append(format("%s.injectDependencyResolver(dependencyResolver);\n", moduleField.getName()));
}
result.append("}\n");
}
}
// identity refs need to be injected with dependencyResolver and base class
for (ModuleField moduleField : moduleFields) {
if (moduleField.isIdentityRef()) {
result.append(format("if (%s!=null) {", moduleField.getName()));
result.append(format("set%s(%s.resolveIdentity(dependencyResolver, %s.class));", moduleField.getAttributeName(), moduleField.getName(), ((IdentityRefModuleField) moduleField).getIdentityBaseClass()));
result.append("}\n");
}
}
result.append("}\n");
return result.toString();
}
use of org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model.ModuleField in project controller by opendaylight.
the class AbsModuleGeneratedObjectFactory method getReuseLogic.
private static String getReuseLogic(List<ModuleField> moduleFields, FullyQualifiedName abstractFQN) {
StringBuilder result = new StringBuilder("\n" + format("public boolean canReuseInstance(%s oldModule){\n", abstractFQN.getTypeName()) + "// allow reusing of old instance if no parameters was changed\n" + "return isSame(oldModule);\n" + "}\n" + "\n" + format("public %s reuseInstance(%1$s oldInstance){\n", AutoCloseable.class.getCanonicalName()) + "// implement if instance reuse should be supported." + "Override canReuseInstance to change the criteria.\n" + "return oldInstance;\n" + "}\n");
// isSame method that detects changed fields
result.append("\n").append(format("public boolean isSame(%s other) {\n", abstractFQN.getTypeName())).append("if (other == null) {\n").append("throw new IllegalArgumentException(\"Parameter 'other' is null\");\n").append("}\n");
// loop through fields, do deep equals on each field
for (ModuleField moduleField : moduleFields) {
result.append(format("if (!java.util.Objects.deepEquals(%s, other.%1$s)) {\n" + "return false;\n" + "}\n", moduleField.getName()));
if (moduleField.isListOfDependencies()) {
result.append(format("for (int idx = 0; idx < %1$s.size(); idx++) {\n" + "if (!dependencyResolver.canReuseDependency(%1$s.get(idx), %1$sJmxAttribute)) {\n" + "return false;\n" + "}\n" + "}\n", moduleField.getName()));
} else if (moduleField.isDependent()) {
result.append(format(// would return false
"if(%1$s!= null) {\n" + "// reference to dependency must be reusable as well\n" + "if (!dependencyResolver.canReuseDependency(%1$s, %1$sJmxAttribute)) {\n" + "return false;\n" + "}\n" + "}\n", moduleField.getName()));
}
}
result.append("\n" + "return true;\n" + "}\n");
return result.toString();
}
use of org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model.ModuleField in project controller by opendaylight.
the class AbsModuleGeneratedObjectFactory method getValidationMethods.
private static String getValidationMethods(List<ModuleField> moduleFields) {
StringBuilder result = new StringBuilder("\n" + "@Override\n" + "public void validate() {\n");
// validate each mandatory dependency
for (ModuleField moduleField : moduleFields) {
if (moduleField.isDependent()) {
if (moduleField.isList()) {
result.append(format("for(javax.management.ObjectName dep : %s) {\n", moduleField.getName())).append(format(" dependencyResolver.validateDependency(%s.class, dep, %sJmxAttribute);" + "\n", moduleField.getDependency().getSie().getFullyQualifiedName(), moduleField.getName())).append("}\n");
} else {
if (!moduleField.getDependency().isMandatory()) {
result.append(format("if(%s != null) {\n", moduleField.getName()));
}
result.append(format("dependencyResolver.validateDependency(%s.class, %s, %sJmxAttribute);\n", moduleField.getDependency().getSie().getFullyQualifiedName(), moduleField.getName(), moduleField.getName()));
if (!moduleField.getDependency().isMandatory()) {
result.append("}\n");
}
}
}
}
result.append("\n" + "customValidation();\n" + "}\n" + "\n" + "protected void customValidation() {\n" + "}\n");
return result.toString();
}
Aggregations