BUG-2453 (De)Serialize enum values as defined in yang
[controller.git] / opendaylight / config / yang-jmx-generator / src / main / java / org / opendaylight / controller / config / yangjmxgenerator / attribute / JavaAttribute.java
index fac4d5743229501fd7827269a8c810ac6b6f54a1..d830e97678db5d7f4f5b26c8db70daf212472d48 100644 (file)
@@ -8,23 +8,23 @@
 package org.opendaylight.controller.config.yangjmxgenerator.attribute;
 
 import com.google.common.base.Preconditions;
+import java.util.Arrays;
+import java.util.List;
+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 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.EnumTypeDefinition;
 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";
@@ -61,6 +61,11 @@ public class JavaAttribute extends AbstractAttribute implements TypedAttribute {
         return base instanceof UnionTypeDefinition;
     }
 
+    public boolean isEnum() {
+        TypeDefinition<?> base = getBaseType(typeProviderWrapper, typeDefinition);
+        return base instanceof EnumTypeDefinition;
+    }
+
     public TypeDefinition<?> getTypeDefinition() {
         return typeDefinition;
     }
@@ -96,24 +101,30 @@ public class JavaAttribute extends AbstractAttribute implements TypedAttribute {
 
     @Override
     public boolean equals(Object o) {
-        if (this == 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;
+        }
 
         JavaAttribute that = (JavaAttribute) o;
 
         if (nullableDefault != null ? !nullableDefault
-                .equals(that.nullableDefault) : that.nullableDefault != null)
+                .equals(that.nullableDefault) : that.nullableDefault != null) {
             return false;
+        }
         if (nullableDescription != null ? !nullableDescription
                 .equals(that.nullableDescription)
-                : that.nullableDescription != null)
+                : that.nullableDescription != null) {
             return false;
-        if (type != null ? !type.equals(that.type) : that.type != null)
+        }
+        if (type != null ? !type.equals(that.type) : that.type != null) {
             return false;
+        }
 
         return true;
     }
@@ -144,8 +155,8 @@ public class JavaAttribute extends AbstractAttribute implements TypedAttribute {
 
         if (isArray()) {
             return getArrayType();
-        } else if (isEnum(baseType)) {
-            return getSimpleType(baseType);
+        } else if (isEnum()) {
+            return getEnumType(baseTypeDefinition);
         } else if (isUnion()) {
             return getCompositeTypeForUnion(baseTypeDefinition);
         } else if (isDerivedType(baseType, getType())) {
@@ -157,6 +168,18 @@ public class JavaAttribute extends AbstractAttribute implements TypedAttribute {
         return getSimpleType(getType());
     }
 
+    private OpenType<?> getEnumType(final TypeDefinition<?> baseType) {
+        final String fullyQualifiedName = typeProviderWrapper.getType(node, getTypeDefinition()).getFullyQualifiedName();
+        final String[] items = {"instance"};
+        String description = getNullableDescription() == null ? getAttributeYangName() : getNullableDescription();
+
+        try {
+            return new CompositeType(fullyQualifiedName, description, items, items, new OpenType[]{SimpleType.STRING});
+        } catch (OpenDataException e) {
+            throw new RuntimeException("Unable to create enum type" + fullyQualifiedName + " as open type", e);
+        }
+    }
+
     public boolean isIdentityRef() {
         return typeDefinition instanceof IdentityrefTypeDefinition;
     }
@@ -196,7 +219,7 @@ public class JavaAttribute extends AbstractAttribute implements TypedAttribute {
             itemTypes[i++] = innerCompositeType;
         }
 
-        String[] descriptions = Arrays.copyOf(itemNames, itemNames.length);
+        String[] descriptions = itemNames.clone();
         descriptions[0] = DESCRIPTION_OF_VALUE_ATTRIBUTE_FOR_UNION;
 
         try {
@@ -218,16 +241,12 @@ public class JavaAttribute extends AbstractAttribute implements TypedAttribute {
         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) {
+    private OpenType<?> getCompositeType(Type baseType, TypeDefinition<?> baseTypeDefinition) {
 
         SimpleType<?> innerItemType = SimpleTypeResolver.getSimpleType(baseType);
         String innerItemName = typeProviderWrapper.getJMXParamForBaseType(baseTypeDefinition);
@@ -276,8 +295,9 @@ public class JavaAttribute extends AbstractAttribute implements TypedAttribute {
 
     // TODO verify
     private boolean isPrimitive(String innerTypeFullyQName) {
-        if (innerTypeFullyQName.contains("."))
+        if (innerTypeFullyQName.contains(".")) {
             return false;
+        }
 
         return true;
     }