X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fconfig%2Fyang-jmx-generator%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fconfig%2Fyangjmxgenerator%2Fattribute%2FTOAttribute.java;h=d9698cc7e247bbfeae52b92e62856eb8d2aebbab;hb=f43b01b81319959b1907e3e04537f5169e7f33d8;hp=96656338df130f30ef569fbb54537727ff9a0b3a;hpb=bab3649670c33b3b12a49c59fc1c5cabcbcb924e;p=controller.git diff --git a/opendaylight/config/yang-jmx-generator/src/main/java/org/opendaylight/controller/config/yangjmxgenerator/attribute/TOAttribute.java b/opendaylight/config/yang-jmx-generator/src/main/java/org/opendaylight/controller/config/yangjmxgenerator/attribute/TOAttribute.java index 96656338df..d9698cc7e2 100644 --- a/opendaylight/config/yang-jmx-generator/src/main/java/org/opendaylight/controller/config/yangjmxgenerator/attribute/TOAttribute.java +++ b/opendaylight/config/yang-jmx-generator/src/main/java/org/opendaylight/controller/config/yangjmxgenerator/attribute/TOAttribute.java @@ -11,8 +11,19 @@ import com.google.common.base.Function; import com.google.common.collect.Collections2; import com.google.common.collect.Maps; import com.google.common.collect.Sets; -import org.opendaylight.controller.config.yangjmxgenerator.ModuleMXBeanEntry; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; +import javax.management.openmbean.CompositeType; +import javax.management.openmbean.OpenDataException; +import javax.management.openmbean.OpenType; import org.opendaylight.controller.config.yangjmxgenerator.TypeProviderWrapper; +import org.opendaylight.mdsal.binding.model.api.JavaTypeName; +import org.opendaylight.mdsal.binding.model.api.Type; +import org.opendaylight.mdsal.binding.model.util.ReferencedTypeImpl; import org.opendaylight.yangtools.yang.model.api.AugmentationTarget; import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode; import org.opendaylight.yangtools.yang.model.api.DataNodeContainer; @@ -21,19 +32,12 @@ import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode; import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode; import org.opendaylight.yangtools.yang.model.api.ListSchemaNode; -import javax.management.openmbean.CompositeType; -import javax.management.openmbean.OpenDataException; -import javax.management.openmbean.OpenType; -import java.util.HashMap; -import java.util.Map; -import java.util.Map.Entry; -import java.util.Set; - -public class TOAttribute extends AbstractAttribute { +public class TOAttribute extends AbstractAttribute implements TypedAttribute { private final String nullableDescription, nullableDefault; private final Map yangNameToAttributeMap; private final Map attributeNameMap; + private final String packageName; private static final Set> ALLOWED_CHILDREN = Sets .newHashSet(); @@ -45,132 +49,134 @@ public class TOAttribute extends AbstractAttribute { } public static TOAttribute create( - T containerSchemaNode, TypeProviderWrapper typeProviderWrapper) { + final T containerSchemaNode, final TypeProviderWrapper typeProviderWrapper, final String packageName) { // Transfer Object: get the leaves - Map map = new HashMap<>(); - Map attributeNameMap = new HashMap<>(); - for (DataSchemaNode dataSchemaNode : containerSchemaNode - .getChildNodes()) { + final Map map = new HashMap<>(); + final Map attributeNameMap = new HashMap<>(); + for (final DataSchemaNode dataSchemaNode : containerSchemaNode.getChildNodes()) { try { - String yangName = dataSchemaNode.getQName().getLocalName(); - map.put(yangName, - createInnerAttribute(dataSchemaNode, - typeProviderWrapper)); - } catch (IllegalArgumentException e) { + final String yangName = dataSchemaNode.getQName().getLocalName(); + map.put(yangName, createInnerAttribute(dataSchemaNode, typeProviderWrapper, packageName)); + } catch (final IllegalArgumentException e) { throw new IllegalStateException("Unable to create TO", e); } } return new TOAttribute(containerSchemaNode, map, attributeNameMap, - containerSchemaNode.getDescription()); + containerSchemaNode.getDescription().orElse(null), packageName); } private static AttributeIfc createInnerAttribute( - DataSchemaNode dataSchemaNode, - TypeProviderWrapper typeProviderWrapper) { - Class type = isAllowedType(dataSchemaNode); - - if (type.equals(LeafSchemaNode.class)) - return new JavaAttribute((LeafSchemaNode) dataSchemaNode, - typeProviderWrapper); - else if (type.equals(ListSchemaNode.class)) - return ListAttribute.create((ListSchemaNode) dataSchemaNode, - typeProviderWrapper); - else if (type.equals(LeafListSchemaNode.class)) - return ListAttribute.create((LeafListSchemaNode) dataSchemaNode, - typeProviderWrapper); - else if (type.equals(ContainerSchemaNode.class)) - return TOAttribute.create((ContainerSchemaNode) dataSchemaNode, - typeProviderWrapper); + final DataSchemaNode dataSchemaNode, + final TypeProviderWrapper typeProviderWrapper, final String packageName) { + final Class type = isAllowedType(dataSchemaNode); + + if (type.equals(LeafSchemaNode.class)) { + return new JavaAttribute((LeafSchemaNode) dataSchemaNode, typeProviderWrapper); + } else if (type.equals(ListSchemaNode.class)) { + return ListAttribute.create((ListSchemaNode) dataSchemaNode, typeProviderWrapper, packageName); + } else if (type.equals(LeafListSchemaNode.class)) { + return ListAttribute.create((LeafListSchemaNode) dataSchemaNode, typeProviderWrapper); + } else if (type.equals(ContainerSchemaNode.class)) { + return TOAttribute.create((ContainerSchemaNode) dataSchemaNode, typeProviderWrapper, packageName); + } throw new IllegalStateException("This should never happen"); } private static Class isAllowedType( - DataSchemaNode dataSchemaNode) { - for (Class allowedType : ALLOWED_CHILDREN) { - if (allowedType.isAssignableFrom(dataSchemaNode.getClass()) == true) + final DataSchemaNode dataSchemaNode) { + for (final Class allowedType : ALLOWED_CHILDREN) { + if (allowedType.isAssignableFrom(dataSchemaNode.getClass())) { return allowedType; + } } throw new IllegalArgumentException("Illegal child node for TO: " + dataSchemaNode.getClass() + " allowed node types: " + ALLOWED_CHILDREN); } - private TOAttribute(DataSchemaNode attrNode, - Map transferObject, - Map attributeNameMap, String nullableDescription) { + private TOAttribute(final DataSchemaNode attrNode, + final Map transferObject, + final Map attributeNameMap, final String nullableDescription, final String packageName) { super(attrNode); - yangNameToAttributeMap = transferObject; + this.yangNameToAttributeMap = transferObject; this.attributeNameMap = attributeNameMap; this.nullableDescription = nullableDescription; - nullableDefault = null; + this.nullableDefault = null; + this.packageName = packageName; } public Map getAttributeNameMap() { - return attributeNameMap; + return this.attributeNameMap; } public Map getCapitalizedPropertiesToTypesMap() { - Map capitalizedPropertiesToTypesMap = Maps + final Map capitalizedPropertiesToTypesMap = Maps .newHashMap(); - for (Entry entry : yangNameToAttributeMap + for (final Entry entry : this.yangNameToAttributeMap .entrySet()) { capitalizedPropertiesToTypesMap.put( - ModuleMXBeanEntry.convertToJavaName(entry.getKey(), true), + TypeProviderWrapper.convertToJavaName(entry.getKey(), true), entry.getValue()); } return capitalizedPropertiesToTypesMap; } public Map getJmxPropertiesToTypesMap() { - Map jmxPropertiesToTypesMap = Maps.newHashMap(); - for (Entry entry : yangNameToAttributeMap + final Map jmxPropertiesToTypesMap = Maps.newHashMap(); + for (final Entry entry : this.yangNameToAttributeMap .entrySet()) { jmxPropertiesToTypesMap.put( - ModuleMXBeanEntry.convertToJavaName(entry.getKey(), false), + TypeProviderWrapper.convertToJavaName(entry.getKey(), false), entry.getValue()); } return jmxPropertiesToTypesMap; } public Map getYangPropertiesToTypesMap() { - return yangNameToAttributeMap; + return this.yangNameToAttributeMap; } @Override public String getNullableDescription() { - return nullableDescription; + return this.nullableDescription; } @Override public String getNullableDefault() { - return nullableDefault; + return this.nullableDefault; } @Override - public boolean equals(Object o) { - if (this == o) + public boolean equals(final Object o) { + if (this == o) { return true; - if (o == null || getClass() != o.getClass()) + } + if (o == null || getClass() != o.getClass()) { return false; - if (!super.equals(o)) + } + if (!super.equals(o)) { return false; + } - TOAttribute that = (TOAttribute) o; + final TOAttribute that = (TOAttribute) o; - if (nullableDefault != null ? !nullableDefault - .equals(that.nullableDefault) : that.nullableDefault != null) + if (this.nullableDefault != null ? !this.nullableDefault + .equals(that.nullableDefault) : that.nullableDefault != null) { return false; - if (nullableDescription != null ? !nullableDescription + } + if (this.nullableDescription != null ? !this.nullableDescription .equals(that.nullableDescription) - : that.nullableDescription != null) + : that.nullableDescription != null) { return false; - if (yangNameToAttributeMap != null ? !yangNameToAttributeMap + } + if (this.yangNameToAttributeMap != null ? !this.yangNameToAttributeMap .equals(that.yangNameToAttributeMap) - : that.yangNameToAttributeMap != null) + : that.yangNameToAttributeMap != null) { return false; + } return true; } @@ -180,13 +186,13 @@ public class TOAttribute extends AbstractAttribute { int result = super.hashCode(); result = 31 * result - + (nullableDescription != null ? nullableDescription.hashCode() + + (this.nullableDescription != null ? this.nullableDescription.hashCode() : 0); result = 31 * result - + (nullableDefault != null ? nullableDefault.hashCode() : 0); + + (this.nullableDefault != null ? this.nullableDefault.hashCode() : 0); result = 31 * result - + (yangNameToAttributeMap != null ? yangNameToAttributeMap + + (this.yangNameToAttributeMap != null ? this.yangNameToAttributeMap .hashCode() : 0); return result; } @@ -194,48 +200,56 @@ public class TOAttribute extends AbstractAttribute { @Override public String toString() { return "TOAttribute{" + getAttributeYangName() + "," + "to=" - + yangNameToAttributeMap + '}'; + + this.yangNameToAttributeMap + '}'; + } + + @Override + public Type getType() { + // TODO: ReferencedTypeImpl from Types + return new ReferencedTypeImpl(JavaTypeName.create(this.packageName, getUpperCaseCammelCase())); } @Override public CompositeType getOpenType() { - String description = getNullableDescription() == null ? getAttributeYangName() - : getNullableDescription(); - final String[] itemNames = new String[yangNameToAttributeMap.keySet() - .size()]; - String[] itemDescriptions = itemNames; - FunctionImpl functionImpl = new FunctionImpl(itemNames); - Map jmxPropertiesToTypesMap = getJmxPropertiesToTypesMap(); - OpenType[] itemTypes = Collections2.transform( + final String description = getNullableDescription() == null ? getAttributeYangName() : getNullableDescription(); + + final FunctionImpl functionImpl = new FunctionImpl(); + final Map jmxPropertiesToTypesMap = getJmxPropertiesToTypesMap(); + final OpenType[] itemTypes = Collections2.transform( jmxPropertiesToTypesMap.entrySet(), functionImpl).toArray( new OpenType[] {}); + final String[] itemNames = functionImpl.getItemNames(); try { // TODO add package name to create fully qualified name for this // type - CompositeType compositeType = new CompositeType( + final CompositeType compositeType = new CompositeType( getUpperCaseCammelCase(), description, itemNames, - itemDescriptions, itemTypes); + itemNames, itemTypes); return compositeType; - } catch (OpenDataException e) { + } catch (final OpenDataException e) { throw new RuntimeException("Unable to create CompositeType for " + this, e); } } - private static final class FunctionImpl implements - Function, OpenType> { - private final String[] itemNames; - int i = 0; + public String getPackageName() { + return this.packageName; + } - private FunctionImpl(String[] itemNames) { - this.itemNames = itemNames; - } +} - @Override - public OpenType apply(Entry input) { - AttributeIfc innerType = input.getValue(); - itemNames[i++] = input.getKey(); - return innerType.getOpenType(); - } +class FunctionImpl implements + Function, OpenType> { + private final List itemNames = new ArrayList<>(); + + @Override + public OpenType apply(final Entry input) { + final AttributeIfc innerType = input.getValue(); + this.itemNames.add(input.getKey()); + return innerType.getOpenType(); + } + + public String[] getItemNames(){ + return this.itemNames.toArray(new String[this.itemNames.size()]); } }