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%2FJavaAttribute.java;h=3e20e4a55ad47f58e538a33594d1f5d4f0e70d32;hb=711bd1804e4b3362337be7ed0955f94e234dc7b3;hp=8f516ef1812f0a929e73dc42708850fd026fe9e9;hpb=9fb64948564e252018f9b1e13e7cea2c92f991aa;p=controller.git diff --git a/opendaylight/config/yang-jmx-generator/src/main/java/org/opendaylight/controller/config/yangjmxgenerator/attribute/JavaAttribute.java b/opendaylight/config/yang-jmx-generator/src/main/java/org/opendaylight/controller/config/yangjmxgenerator/attribute/JavaAttribute.java index 8f516ef181..3e20e4a55a 100644 --- a/opendaylight/config/yang-jmx-generator/src/main/java/org/opendaylight/controller/config/yangjmxgenerator/attribute/JavaAttribute.java +++ b/opendaylight/config/yang-jmx-generator/src/main/java/org/opendaylight/controller/config/yangjmxgenerator/attribute/JavaAttribute.java @@ -7,25 +7,33 @@ */ package org.opendaylight.controller.config.yangjmxgenerator.attribute; -import javax.management.openmbean.ArrayType; -import javax.management.openmbean.OpenDataException; -import javax.management.openmbean.OpenType; -import javax.management.openmbean.SimpleType; - import org.opendaylight.controller.config.yangjmxgenerator.TypeProviderWrapper; import org.opendaylight.yangtools.sal.binding.model.api.Type; import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode; import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode; +import org.opendaylight.yangtools.yang.model.api.TypeDefinition; + +import javax.management.openmbean.ArrayType; +import javax.management.openmbean.CompositeType; +import javax.management.openmbean.OpenDataException; +import javax.management.openmbean.OpenType; +import javax.management.openmbean.SimpleType; public class JavaAttribute extends AbstractAttribute implements TypedAttribute { + private final Type type; - private final String nullableDescription, nullableDefault; + private final String nullableDescription, nullableDefault, nullableDefaultWrappedForCode; + private final TypeProviderWrapper typeProviderWrapper; + private final TypeDefinition typeDefinition; public JavaAttribute(LeafSchemaNode leaf, TypeProviderWrapper typeProviderWrapper) { super(leaf); this.type = typeProviderWrapper.getType(leaf); + this.typeDefinition = leaf.getType(); + this.typeProviderWrapper = typeProviderWrapper; this.nullableDefault = leaf.getDefault(); + this.nullableDefaultWrappedForCode = leaf.getDefault() == null ? null : typeProviderWrapper.getDefault(leaf); this.nullableDescription = leaf.getDescription(); } @@ -33,10 +41,30 @@ public class JavaAttribute extends AbstractAttribute implements TypedAttribute { TypeProviderWrapper typeProviderWrapper) { super(leaf); this.type = typeProviderWrapper.getType(leaf); - this.nullableDefault = null; + this.typeDefinition = leaf.getType(); + this.typeProviderWrapper = typeProviderWrapper; + this.nullableDefault = nullableDefaultWrappedForCode = null; this.nullableDescription = leaf.getDescription(); } + public TypeDefinition getTypeDefinition() { + return typeDefinition; + } + + /** + * Returns the most base type + */ + private TypeDefinition getBaseType(TypeProviderWrapper typeProviderWrapper, TypeDefinition baseType) { + while(baseType.getBaseType()!=null) { + baseType = baseType.getBaseType(); + } + return baseType; + } + + public String getNullableDefaultWrappedForCode() { + return nullableDefaultWrappedForCode; + } + @Override public Type getType() { return type; @@ -97,27 +125,60 @@ public class JavaAttribute extends AbstractAttribute implements TypedAttribute { @Override public OpenType getOpenType() { - // If is array => arrayType - if (isArray(getType())) { - String innerTypeFullyQName = getInnerType(getType()); - SimpleType innerSimpleType = SimpleTypeResolver - .getSimpleType(innerTypeFullyQName); - try { - ArrayType arrayType = isPrimitive(innerTypeFullyQName) ? new ArrayType<>( - innerSimpleType, true) : new ArrayType<>(1, - innerSimpleType); - return arrayType; - } catch (OpenDataException e) { - throw new RuntimeException("Unable to create " - + ArrayType.class + " with inner element of type " - + innerSimpleType, e); - } + TypeDefinition baseTypeDefinition = getBaseType(typeProviderWrapper, typeDefinition); + Type baseType = typeProviderWrapper.getType(baseTypeDefinition, baseTypeDefinition); + + if (isArray()) { + return getArrayType(); + } else if (isEnum(baseType)) { + return getSimpleType(baseType); + } else if (isDerivedType(baseType)) { + return getCompositeType(baseType, baseTypeDefinition); } - // else simple type - SimpleType simpleType = SimpleTypeResolver.getSimpleType(getType()); + + return getSimpleType(getType()); + } + + private boolean isEnum(Type baseType) { + return baseType.getFullyQualifiedName().equals(Enum.class.getName()); + } + + private OpenType getSimpleType(Type type) { + SimpleType simpleType = SimpleTypeResolver.getSimpleType(type); return simpleType; } + private OpenType getCompositeType(Type baseType, TypeDefinition baseTypeDefinition) { + + SimpleType innerItemType = SimpleTypeResolver.getSimpleType(baseType); + String innerItemName = typeProviderWrapper.getJMXParamForBaseType(baseTypeDefinition); + + String[] itemNames = new String[]{innerItemName}; + String description = getNullableDescription() == null ? getAttributeYangName() : getNullableDescription(); + + OpenType[] itemTypes = new OpenType[]{innerItemType}; + try { + return new CompositeType(getUpperCaseCammelCase(), description, itemNames, itemNames, itemTypes); + } catch (OpenDataException e) { + throw new RuntimeException("Unable to create " + CompositeType.class + " with inner element of type " + + itemTypes, e); + } + + } + + private OpenType getArrayType() { + String innerTypeFullyQName = getInnerType(getType()); + SimpleType innerSimpleType = SimpleTypeResolver.getSimpleType(innerTypeFullyQName); + try { + ArrayType arrayType = isPrimitive(innerTypeFullyQName) ? new ArrayType<>(innerSimpleType, true) + : new ArrayType<>(1, innerSimpleType); + return arrayType; + } catch (OpenDataException e) { + throw new RuntimeException("Unable to create " + ArrayType.class + " with inner element of type " + + innerSimpleType, e); + } + } + // TODO verify private boolean isPrimitive(String innerTypeFullyQName) { if (innerTypeFullyQName.contains(".")) @@ -126,13 +187,17 @@ public class JavaAttribute extends AbstractAttribute implements TypedAttribute { return true; } + private boolean isArray() { + return type.getName().endsWith("[]"); + } + + private boolean isDerivedType(Type baseType) { + return baseType.equals(getType()) == false; + } + private static String getInnerType(Type type) { String fullyQualifiedName = type.getFullyQualifiedName(); return fullyQualifiedName.substring(0, fullyQualifiedName.length() - 2); } - private static boolean isArray(Type type) { - return type.getName().endsWith("[]"); - } - }