X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fconfig%2Fyang-jmx-generator%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fconfig%2Fyangjmxgenerator%2Fattribute%2FJavaAttribute.java;h=fac4d5743229501fd7827269a8c810ac6b6f54a1;hp=8f516ef1812f0a929e73dc42708850fd026fe9e9;hb=ebd9aba9e844229de613a2b60b5d21119fcee968;hpb=9fb64948564e252018f9b1e13e7cea2c92f991aa 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..fac4d57432 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,42 @@ */ 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 com.google.common.base.Preconditions; +import org.opendaylight.controller.config.api.IdentityAttributeRef; 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 org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition; +import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition; + +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; +import java.util.Arrays; +import java.util.List; public class JavaAttribute extends AbstractAttribute implements TypedAttribute { + + public static final String DESCRIPTION_OF_VALUE_ATTRIBUTE_FOR_UNION = "valueOfArtificialUnionProperty"; + 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 +50,35 @@ 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 boolean isUnion() { + TypeDefinition base = getBaseType(typeProviderWrapper, typeDefinition); + return base instanceof UnionTypeDefinition; + } + + 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 +139,141 @@ 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 (isUnion()) { + return getCompositeTypeForUnion(baseTypeDefinition); + } else if (isDerivedType(baseType, getType())) { + return getCompositeType(baseType, baseTypeDefinition); + } else if (isIdentityRef()) { + return getCompositeTypeForIdentity(); + } + + return getSimpleType(getType()); + } + + public boolean isIdentityRef() { + return typeDefinition instanceof IdentityrefTypeDefinition; + } + + private OpenType getCompositeTypeForUnion(TypeDefinition baseTypeDefinition) { + Preconditions.checkArgument(baseTypeDefinition instanceof UnionTypeDefinition, + "Expected %s instance but was %s", UnionTypeDefinition.class, baseTypeDefinition); + + List> types = ((UnionTypeDefinition) baseTypeDefinition).getTypes(); + + String[] itemNames = new String[types.size()+1]; + OpenType[] itemTypes = new OpenType[itemNames.length]; + + addArtificialPropertyToUnionCompositeType(baseTypeDefinition, itemNames, itemTypes); + + String description = getNullableDescription() == null ? getAttributeYangName() : getNullableDescription(); + + int i = 1; + for (TypeDefinition innerTypeDefinition : types) { + + Type innerType = typeProviderWrapper.getType(innerTypeDefinition, innerTypeDefinition); + + TypeDefinition baseInnerTypeDefinition = getBaseType(typeProviderWrapper, innerTypeDefinition); + Type innerTypeBaseType = typeProviderWrapper.getType(baseInnerTypeDefinition, baseInnerTypeDefinition); + + OpenType innerCompositeType; + + if(isDerivedType(innerTypeBaseType, innerType)) { + innerCompositeType = baseInnerTypeDefinition instanceof UnionTypeDefinition ? + getCompositeTypeForUnion(baseInnerTypeDefinition) : + getCompositeType(innerTypeBaseType, baseInnerTypeDefinition); + } else { + innerCompositeType = SimpleTypeResolver.getSimpleType(innerType); } + + itemNames[i] = typeProviderWrapper.getJMXParamForUnionInnerType(innerTypeDefinition); + itemTypes[i++] = innerCompositeType; + } + + String[] descriptions = Arrays.copyOf(itemNames, itemNames.length); + descriptions[0] = DESCRIPTION_OF_VALUE_ATTRIBUTE_FOR_UNION; + + try { + return new CompositeType(getUpperCaseCammelCase(), description, itemNames, descriptions, itemTypes); + } catch (OpenDataException e) { + throw new RuntimeException("Unable to create " + CompositeType.class + " with inner elements " + + Arrays.toString(itemTypes), e); } - // else simple type - SimpleType simpleType = SimpleTypeResolver.getSimpleType(getType()); + } + + public static final Class TYPE_OF_ARTIFICIAL_UNION_PROPERTY = char.class; + + private void addArtificialPropertyToUnionCompositeType(TypeDefinition baseTypeDefinition, String[] itemNames, OpenType[] itemTypes) { + String artificialPropertyName = typeProviderWrapper.getJMXParamForBaseType(baseTypeDefinition); + itemNames[0] = artificialPropertyName; + + OpenType artificialPropertyType = getArrayOpenTypeForSimpleType(TYPE_OF_ARTIFICIAL_UNION_PROPERTY.getName(), + SimpleTypeResolver.getSimpleType(TYPE_OF_ARTIFICIAL_UNION_PROPERTY.getName())); + itemTypes[0] = artificialPropertyType; + } + + 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); + } + } + + public OpenType getCompositeTypeForIdentity() { + String[] itemNames = new String[]{IdentityAttributeRef.QNAME_ATTR_NAME}; + String description = getNullableDescription() == null ? getAttributeYangName() : getNullableDescription(); + OpenType[] itemTypes = new OpenType[]{SimpleType.STRING}; + + 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); + return getArrayOpenTypeForSimpleType(innerTypeFullyQName, innerSimpleType); + } + + private OpenType getArrayOpenTypeForSimpleType(String innerTypeFullyQName, SimpleType innerSimpleType) { + 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 +282,17 @@ public class JavaAttribute extends AbstractAttribute implements TypedAttribute { return true; } + private boolean isArray() { + return type.getName().endsWith("[]"); + } + + private boolean isDerivedType(Type baseType, Type currentType) { + return baseType.equals(currentType) == 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("[]"); - } - }