X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fconfig%2Fyang-jmx-generator-plugin%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fconfig%2Fyangjmxgenerator%2Fplugin%2Fftl%2FTemplateFactory.java;h=115bb85b618020b61282c83c3c692c9dafca6251;hb=616a88111ea9603f0d6f93c7462e6dab39644fcf;hp=641fb25c1a350e74cbe9135d93f1a61dc24a3e1a;hpb=9fb64948564e252018f9b1e13e7cea2c92f991aa;p=controller.git diff --git a/opendaylight/config/yang-jmx-generator-plugin/src/main/java/org/opendaylight/controller/config/yangjmxgenerator/plugin/ftl/TemplateFactory.java b/opendaylight/config/yang-jmx-generator-plugin/src/main/java/org/opendaylight/controller/config/yangjmxgenerator/plugin/ftl/TemplateFactory.java index 641fb25c1a..115bb85b61 100644 --- a/opendaylight/config/yang-jmx-generator-plugin/src/main/java/org/opendaylight/controller/config/yangjmxgenerator/plugin/ftl/TemplateFactory.java +++ b/opendaylight/config/yang-jmx-generator-plugin/src/main/java/org/opendaylight/controller/config/yangjmxgenerator/plugin/ftl/TemplateFactory.java @@ -7,15 +7,11 @@ */ package org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; - +import com.google.common.base.Function; +import com.google.common.base.Preconditions; +import com.google.common.collect.Collections2; +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; import org.opendaylight.controller.config.api.RuntimeBeanRegistratorAwareModule; import org.opendaylight.controller.config.api.annotations.AbstractServiceInterface; import org.opendaylight.controller.config.api.runtime.RuntimeBean; @@ -25,13 +21,14 @@ import org.opendaylight.controller.config.yangjmxgenerator.ModuleMXBeanEntry; import org.opendaylight.controller.config.yangjmxgenerator.RuntimeBeanEntry; import org.opendaylight.controller.config.yangjmxgenerator.RuntimeBeanEntry.Rpc; import org.opendaylight.controller.config.yangjmxgenerator.ServiceInterfaceEntry; +import org.opendaylight.controller.config.yangjmxgenerator.attribute.AbstractDependencyAttribute; import org.opendaylight.controller.config.yangjmxgenerator.attribute.AttributeIfc; -import org.opendaylight.controller.config.yangjmxgenerator.attribute.DependencyAttribute; -import org.opendaylight.controller.config.yangjmxgenerator.attribute.DependencyAttribute.Dependency; +import org.opendaylight.controller.config.yangjmxgenerator.attribute.Dependency; import org.opendaylight.controller.config.yangjmxgenerator.attribute.JavaAttribute; import org.opendaylight.controller.config.yangjmxgenerator.attribute.ListAttribute; import org.opendaylight.controller.config.yangjmxgenerator.attribute.TOAttribute; import org.opendaylight.controller.config.yangjmxgenerator.attribute.TypedAttribute; +import org.opendaylight.controller.config.yangjmxgenerator.attribute.VoidAttribute; import org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model.Annotation; import org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model.Annotation.Parameter; import org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model.Constructor; @@ -42,12 +39,18 @@ import org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model.Meth import org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model.ModuleField; import org.opendaylight.controller.config.yangjmxgenerator.plugin.util.FullyQualifiedNameHelper; import org.opendaylight.yangtools.binding.generator.util.BindingGeneratorUtil; +import org.opendaylight.yangtools.sal.binding.model.api.ParameterizedType; import org.opendaylight.yangtools.sal.binding.model.api.Type; -import com.google.common.base.Function; -import com.google.common.collect.Collections2; -import com.google.common.collect.Lists; -import com.google.common.collect.Maps; +import javax.management.openmbean.SimpleType; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; public class TemplateFactory { @@ -107,40 +110,18 @@ public class TemplateFactory { List extendedInterfaces = Arrays.asList(RuntimeBean.class .getCanonicalName()); List methods = new ArrayList<>(); + // convert attributes to getters for (AttributeIfc attributeIfc : entry.getAttributes()) { - String returnType = null; - if (attributeIfc instanceof TypedAttribute) { - returnType = ((TypedAttribute) attributeIfc).getType() - .getFullyQualifiedName(); - } else if (attributeIfc instanceof TOAttribute) { - String fullyQualifiedName = FullyQualifiedNameHelper - .getFullyQualifiedName(entry.getPackageName(), - attributeIfc.getUpperCaseCammelCase()); - - returnType = fullyQualifiedName; - } else if (attributeIfc instanceof ListAttribute) { - AttributeIfc innerAttr = ((ListAttribute) attributeIfc) - .getInnerAttribute(); - - String innerTpe = innerAttr instanceof TypedAttribute ? ((TypedAttribute) innerAttr) - .getType().getFullyQualifiedName() - : FullyQualifiedNameHelper.getFullyQualifiedName( - entry.getPackageName(), - attributeIfc.getUpperCaseCammelCase()); - - returnType = "java.util.List<" + innerTpe + ">"; - } else { - throw new UnsupportedOperationException( - "Attribute not supported: " - + attributeIfc.getClass()); - } + String returnType; + returnType = getReturnType(attributeIfc); String getterName = "get" + attributeIfc.getUpperCaseCammelCase(); MethodDeclaration getter = new MethodDeclaration(returnType, getterName, Collections. emptyList()); methods.add(getter); } + // add rpc methods for (Rpc rpc : entry.getRpcs()) { // convert JavaAttribute parameters into fields @@ -148,11 +129,11 @@ public class TemplateFactory { for (JavaAttribute ja : rpc.getParameters()) { Field field = new Field(Collections. emptyList(), ja.getType().getFullyQualifiedName(), - ja.getLowerCaseCammelCase()); + ja.getLowerCaseCammelCase(), ja.getNullableDefaultWrappedForCode()); fields.add(field); } MethodDeclaration operation = new MethodDeclaration( - rpc.getReturnType(), rpc.getName(), fields); + getReturnType(rpc.getReturnType()), rpc.getName(), fields); methods.add(operation); } @@ -170,6 +151,45 @@ public class TemplateFactory { return result; } + // FIXME: put into Type.toString + static String serializeType(Type type) { + if (type instanceof ParameterizedType){ + ParameterizedType parameterizedType = (ParameterizedType) type; + StringBuffer sb = new StringBuffer(); + sb.append(parameterizedType.getRawType().getFullyQualifiedName()); + sb.append("<"); + boolean first = true; + for(Type parameter: parameterizedType.getActualTypeArguments()) { + if (first) { + first = false; + } else { + sb.append(","); + } + sb.append(serializeType(parameter)); + } + sb.append(">"); + return sb.toString(); + } else { + return type.getFullyQualifiedName(); + } + } + + + private static String getReturnType(AttributeIfc attributeIfc) { + String returnType; + if (attributeIfc instanceof TypedAttribute) { + Type type = ((TypedAttribute) attributeIfc).getType(); + returnType = serializeType(type); + } else if (attributeIfc == VoidAttribute.getInstance()) { + return "void"; + } else { + throw new UnsupportedOperationException( + "Attribute not supported: " + + attributeIfc.getClass()); + } + return returnType; + } + public static GeneralInterfaceTemplate serviceInterfaceFromSie( ServiceInterfaceEntry sie) { @@ -280,8 +300,7 @@ public class TemplateFactory { public static GeneralInterfaceTemplate mXBeanInterfaceTemplateFromMbe( ModuleMXBeanEntry mbe) { MXBeanInterfaceAttributesProcessor attrProcessor = new MXBeanInterfaceAttributesProcessor(); - attrProcessor.processAttributes(mbe.getAttributes(), - mbe.getPackageName()); + attrProcessor.processAttributes(mbe.getAttributes()); GeneralInterfaceTemplate ifcTemplate = new GeneralInterfaceTemplate( getHeaderFromEntry(mbe), mbe.getPackageName(), mbe.getMXBeanInterfaceName(), Lists. newArrayList(), @@ -294,7 +313,7 @@ public class TemplateFactory { ModuleMXBeanEntry mbe) { Map retVal = Maps.newHashMap(); TOAttributesProcessor processor = new TOAttributesProcessor(); - processor.processAttributes(mbe.getAttributes(), mbe.getPackageName()); + processor.processAttributes(mbe.getAttributes()); for (org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.TemplateFactory.TOAttributesProcessor.TOInternal to : processor .getTOs()) { List constructors = Lists.newArrayList(); @@ -315,8 +334,25 @@ public class TemplateFactory { RuntimeBeanEntry rbe) { Map retVal = Maps.newHashMap(); TOAttributesProcessor processor = new TOAttributesProcessor(); - processor.processAttributes(rbe.getYangPropertiesToTypesMap(), - rbe.getPackageName()); + Map yangPropertiesToTypesMap = Maps.newHashMap(rbe.getYangPropertiesToTypesMap()); + + // Add TOs from output parameters + for (Rpc rpc : rbe.getRpcs()) { + AttributeIfc returnType = rpc.getReturnType(); + + if (returnType == VoidAttribute.getInstance()) + continue; + if (returnType instanceof JavaAttribute) + continue; + if (returnType instanceof ListAttribute && returnType.getOpenType() instanceof SimpleType) + continue; + + Preconditions.checkState(yangPropertiesToTypesMap.containsKey(returnType.getAttributeYangName()) == false, + "Duplicate TO %s for %s", returnType.getAttributeYangName(), rbe); + yangPropertiesToTypesMap.put(returnType.getAttributeYangName(), returnType); + } + + processor.processAttributes(yangPropertiesToTypesMap); for (org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.TemplateFactory.TOAttributesProcessor.TOInternal to : processor .getTOs()) { List constructors = Lists.newArrayList(); @@ -343,36 +379,29 @@ public class TemplateFactory { private final List tos = Lists.newArrayList(); - void processAttributes(Map attributes, - String packageName) { + void processAttributes(Map attributes) { for (Entry attrEntry : attributes.entrySet()) { AttributeIfc attributeIfc = attrEntry.getValue(); if (attributeIfc instanceof TOAttribute) { - createTOInternal(packageName, attributeIfc); + createTOInternal((TOAttribute) attributeIfc); } if (attributeIfc instanceof ListAttribute) { AttributeIfc innerAttr = ((ListAttribute) attributeIfc) .getInnerAttribute(); if (innerAttr instanceof TOAttribute) { - createTOInternal(packageName, innerAttr); + createTOInternal((TOAttribute) innerAttr); } } } } - private void createTOInternal(String packageName, - AttributeIfc attributeIfc) { - String fullyQualifiedName = FullyQualifiedNameHelper - .getFullyQualifiedName(packageName, attributeIfc.getUpperCaseCammelCase()); + private void createTOInternal(TOAttribute toAttribute) { - String type = fullyQualifiedName; - String name = attributeIfc.getUpperCaseCammelCase(); - Map attrs = ((TOAttribute) attributeIfc) - .getCapitalizedPropertiesToTypesMap(); - // recursive processing - processAttributes(attrs, packageName); + Map attrs = toAttribute.getCapitalizedPropertiesToTypesMap(); + // recursive processing of TO's attributes + processAttributes(attrs); - tos.add(new TOInternal(type, name, attrs, packageName)); + tos.add(new TOInternal(toAttribute.getType(), attrs)); } List getTOs() { @@ -380,20 +409,22 @@ public class TemplateFactory { } private static class TOInternal { - private final String type, name; + private final String fullyQualifiedName, name; private List fields; private List methods; - public TOInternal(String type, String name, + public TOInternal(Type type, Map attrs) { + this(type.getFullyQualifiedName(), type.getName(), attrs, type.getPackageName()); + } + + public TOInternal(String fullyQualifiedName, String name, Map attrs, String packageName) { - super(); - this.type = type; + this.fullyQualifiedName = fullyQualifiedName; this.name = name; processAttrs(attrs, packageName); } - private void processAttrs(Map attrs, - String packageName) { + private void processAttrs(Map attrs, String packageName) { fields = Lists.newArrayList(); methods = Lists.newArrayList(); @@ -402,26 +433,18 @@ public class TemplateFactory { String varName = BindingGeneratorUtil .parseToValidParamName(attrEntry.getKey()); - String fullyQualifiedName = null; + String fullyQualifiedName, nullableDefault = null; if (attrEntry.getValue() instanceof TypedAttribute) { - Type innerType = ((TypedAttribute) attrEntry.getValue()) - .getType(); - fullyQualifiedName = innerType.getFullyQualifiedName(); - } else if (attrEntry.getValue() instanceof ListAttribute) { - AttributeIfc innerAttr = ((ListAttribute) attrEntry - .getValue()).getInnerAttribute(); - - String innerTpe = innerAttr instanceof TypedAttribute ? ((TypedAttribute) innerAttr) - .getType().getFullyQualifiedName() - : FullyQualifiedNameHelper - .getFullyQualifiedName(packageName, attrEntry.getValue().getUpperCaseCammelCase()); - - fullyQualifiedName = "java.util.List<" + innerTpe + ">"; - } else + Type type = ((TypedAttribute) attrEntry.getValue()).getType(); + fullyQualifiedName = serializeType(type); + if(attrEntry.getValue() instanceof JavaAttribute) { + nullableDefault = ((JavaAttribute)attrEntry.getValue()).getNullableDefaultWrappedForCode(); + } + } else { fullyQualifiedName = FullyQualifiedNameHelper .getFullyQualifiedName(packageName, attrEntry.getValue().getUpperCaseCammelCase()); - - fields.add(new Field(fullyQualifiedName, varName)); + } + fields.add(new Field(fullyQualifiedName, varName, nullableDefault)); String getterName = "get" + innerName; MethodDefinition getter = new MethodDefinition( @@ -441,7 +464,7 @@ public class TemplateFactory { } String getType() { - return type; + return fullyQualifiedName; } String getName() { @@ -459,38 +482,16 @@ public class TemplateFactory { } private static class MXBeanInterfaceAttributesProcessor { - private static final String STRING_FULLY_QUALIFIED_NAME = "java.util.List"; private final List methods = Lists.newArrayList(); - void processAttributes(Map attributes, - String packageName) { + void processAttributes(Map attributes) { for (Entry attrEntry : attributes.entrySet()) { String returnType; AttributeIfc attributeIfc = attrEntry.getValue(); if (attributeIfc instanceof TypedAttribute) { - returnType = ((TypedAttribute) attributeIfc).getType() - .getFullyQualifiedName(); - } else if (attributeIfc instanceof TOAttribute) { - String fullyQualifiedName = FullyQualifiedNameHelper - .getFullyQualifiedName(packageName, attributeIfc.getUpperCaseCammelCase()); - - returnType = fullyQualifiedName; - } else if (attributeIfc instanceof ListAttribute) { - String fullyQualifiedName = null; - - AttributeIfc innerAttr = ((ListAttribute) attributeIfc) - .getInnerAttribute(); - if (innerAttr instanceof JavaAttribute) { - fullyQualifiedName = ((JavaAttribute) innerAttr) - .getType().getFullyQualifiedName(); - } else if (innerAttr instanceof TOAttribute) { - fullyQualifiedName = FullyQualifiedNameHelper - .getFullyQualifiedName(packageName, innerAttr.getUpperCaseCammelCase()); - } - - returnType = STRING_FULLY_QUALIFIED_NAME.concat("<") - .concat(fullyQualifiedName).concat(">"); + TypedAttribute typedAttribute = (TypedAttribute) attributeIfc; + returnType = serializeType(typedAttribute.getType()); } else { throw new UnsupportedOperationException( "Attribute not supported: " @@ -533,23 +534,25 @@ public class TemplateFactory { String packageName) { for (Entry attrEntry : attributes.entrySet()) { String type; + String nullableDefaultWrapped = null; AttributeIfc attributeIfc = attrEntry.getValue(); if (attributeIfc instanceof TypedAttribute) { - type = ((TypedAttribute) attributeIfc).getType() - .getFullyQualifiedName(); + TypedAttribute typedAttribute = (TypedAttribute) attributeIfc; + type = serializeType(typedAttribute.getType()); } else if (attributeIfc instanceof TOAttribute) { String fullyQualifiedName = FullyQualifiedNameHelper .getFullyQualifiedName(packageName, attributeIfc.getUpperCaseCammelCase()); type = fullyQualifiedName; - } else if (attributeIfc instanceof ListAttribute) { + } else if (attributeIfc instanceof ListAttribute) { //FIXME: listAttribute might extend TypedAttribute String fullyQualifiedName = null; AttributeIfc innerAttr = ((ListAttribute) attributeIfc) .getInnerAttribute(); if (innerAttr instanceof JavaAttribute) { fullyQualifiedName = ((JavaAttribute) innerAttr) .getType().getFullyQualifiedName(); + nullableDefaultWrapped = ((JavaAttribute) innerAttr).getNullableDefaultWrappedForCode(); } else if (innerAttr instanceof TOAttribute) { fullyQualifiedName = FullyQualifiedNameHelper .getFullyQualifiedName(packageName, innerAttr.getUpperCaseCammelCase()); @@ -565,7 +568,7 @@ public class TemplateFactory { } fields.add(new Field(type, attributeIfc - .getUpperCaseCammelCase())); + .getUpperCaseCammelCase(), nullableDefaultWrapped)); } } @@ -584,12 +587,16 @@ public class TemplateFactory { void processAttributes(Map attributes, String packageName) { for (Entry attrEntry : attributes.entrySet()) { - String type; + String type, nullableDefaultWrapped = null; AttributeIfc attributeIfc = attrEntry.getValue(); if (attributeIfc instanceof TypedAttribute) { - type = ((TypedAttribute) attributeIfc).getType() - .getFullyQualifiedName(); + TypedAttribute typedAttribute = (TypedAttribute) attributeIfc; + type = serializeType(typedAttribute.getType()); + if (attributeIfc instanceof JavaAttribute) { + nullableDefaultWrapped = ((JavaAttribute) attributeIfc).getNullableDefaultWrappedForCode(); + } + } else if (attributeIfc instanceof TOAttribute) { String fullyQualifiedName = FullyQualifiedNameHelper .getFullyQualifiedName(packageName, attributeIfc.getUpperCaseCammelCase()); @@ -602,6 +609,7 @@ public class TemplateFactory { if (innerAttr instanceof JavaAttribute) { fullyQualifiedName = ((JavaAttribute) innerAttr) .getType().getFullyQualifiedName(); + nullableDefaultWrapped = ((JavaAttribute) innerAttr).getNullableDefaultWrappedForCode(); } else if (innerAttr instanceof TOAttribute) { fullyQualifiedName = FullyQualifiedNameHelper .getFullyQualifiedName(packageName, innerAttr.getUpperCaseCammelCase()); @@ -622,9 +630,9 @@ public class TemplateFactory { List annotations = Lists .newArrayList(overrideAnnotation); - if (attributeIfc instanceof DependencyAttribute) { + if (attributeIfc instanceof AbstractDependencyAttribute) { isDependency = true; - dependency = ((DependencyAttribute) attributeIfc) + dependency = ((AbstractDependencyAttribute) attributeIfc) .getDependency(); annotations.add(Annotation .createRequireIfcAnnotation(dependency.getSie())); @@ -633,8 +641,7 @@ public class TemplateFactory { String varName = BindingGeneratorUtil .parseToValidParamName(attrEntry.getKey()); moduleFields.add(new ModuleField(type, varName, attributeIfc - .getUpperCaseCammelCase(), attributeIfc - .getNullableDefault(), isDependency, dependency)); + .getUpperCaseCammelCase(), nullableDefaultWrapped, isDependency, dependency)); String getterName = "get" + attributeIfc.getUpperCaseCammelCase();