Add support for enums as configuration attributes in config and netconf subsystem. 02/3202/3
authorMaros Marsalek <mmarsale@cisco.com>
Thu, 28 Nov 2013 12:54:50 +0000 (13:54 +0100)
committerMaros Marsalek <mmarsale@cisco.com>
Thu, 28 Nov 2013 15:23:40 +0000 (16:23 +0100)
Change-Id: Ieb6054e2136a2eaf834105f9c1cff6db8668d507
Signed-off-by: Maros Marsalek <mmarsale@cisco.com>
opendaylight/config/yang-jmx-generator/src/main/java/org/opendaylight/controller/config/yangjmxgenerator/attribute/JavaAttribute.java
opendaylight/config/yang-jmx-generator/src/main/java/org/opendaylight/controller/config/yangjmxgenerator/attribute/SimpleTypeResolver.java
opendaylight/config/yang-test/src/main/yang/config-test-impl.yang
opendaylight/netconf/config-netconf-connector/src/test/java/org/opendaylight/controller/netconf/confignetconfconnector/NetconfMappingTest.java
opendaylight/netconf/netconf-util/src/test/resources/netconfMessages/editConfig.xml

index 73c7e22..325ca9e 100644 (file)
@@ -121,15 +121,21 @@ public class JavaAttribute extends AbstractAttribute implements TypedAttribute {
 
         if (isArray()) {
             return getArrayType();
+        } else if (isEnum(baseType)) {
+            return getSimpleType(baseType);
         } else if (isDerivedType(baseType)) {
             return getCompositeType(baseType, baseTypeDefinition);
         }
 
-        return getSimpleType();
+        return getSimpleType(getType());
     }
 
-    private OpenType<?> getSimpleType() {
-        SimpleType<?> simpleType = SimpleTypeResolver.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;
     }
 
index 61ad74d..87b55f3 100644 (file)
@@ -39,26 +39,21 @@ public class SimpleTypeResolver {
         return expectedSimpleType;
     }
 
-    private static final Map<String, SimpleType<?>> JAVA_TYPE_TO_SIMPLE_TYPE = Maps
-            .newHashMap();
+    private static final Map<String, SimpleType<?>> JAVA_TYPE_TO_SIMPLE_TYPE = Maps.newHashMap();
     static {
         // TODO add all
-        JAVA_TYPE_TO_SIMPLE_TYPE.put(Integer.class.getName(),
-                SimpleType.INTEGER);
+        JAVA_TYPE_TO_SIMPLE_TYPE.put(Integer.class.getName(), SimpleType.INTEGER);
         JAVA_TYPE_TO_SIMPLE_TYPE.put(int.class.getName(), SimpleType.INTEGER);
         JAVA_TYPE_TO_SIMPLE_TYPE.put(Short.class.getName(), SimpleType.SHORT);
         JAVA_TYPE_TO_SIMPLE_TYPE.put(short.class.getName(), SimpleType.SHORT);
         JAVA_TYPE_TO_SIMPLE_TYPE.put(Long.class.getName(), SimpleType.LONG);
         JAVA_TYPE_TO_SIMPLE_TYPE.put(long.class.getName(), SimpleType.LONG);
         JAVA_TYPE_TO_SIMPLE_TYPE.put(String.class.getName(), SimpleType.STRING);
-        JAVA_TYPE_TO_SIMPLE_TYPE.put(Boolean.class.getName(),
-                SimpleType.BOOLEAN);
-        JAVA_TYPE_TO_SIMPLE_TYPE.put(boolean.class.getName(),
-                SimpleType.BOOLEAN);
-        JAVA_TYPE_TO_SIMPLE_TYPE.put(BigInteger.class.getName(),
-                SimpleType.BIGINTEGER);
-        JAVA_TYPE_TO_SIMPLE_TYPE.put(BigDecimal.class.getName(),
-                SimpleType.BIGDECIMAL);
+        JAVA_TYPE_TO_SIMPLE_TYPE.put(Enum.class.getName(), SimpleType.STRING);
+        JAVA_TYPE_TO_SIMPLE_TYPE.put(Boolean.class.getName(), SimpleType.BOOLEAN);
+        JAVA_TYPE_TO_SIMPLE_TYPE.put(boolean.class.getName(), SimpleType.BOOLEAN);
+        JAVA_TYPE_TO_SIMPLE_TYPE.put(BigInteger.class.getName(), SimpleType.BIGINTEGER);
+        JAVA_TYPE_TO_SIMPLE_TYPE.put(BigDecimal.class.getName(), SimpleType.BIGDECIMAL);
         JAVA_TYPE_TO_SIMPLE_TYPE.put(Byte.class.getName(), SimpleType.BYTE);
         JAVA_TYPE_TO_SIMPLE_TYPE.put(byte.class.getName(), SimpleType.BYTE);
         JAVA_TYPE_TO_SIMPLE_TYPE.put(Date.class.getName(), SimpleType.DATE);
index 977aa6a..df636fd 100644 (file)
@@ -115,12 +115,10 @@ module config-test-impl {
                 type tt:extend-twice;
             }
 
-/* TODO Add support for enums
-
             leaf extended-enum {
                 type tt:extend-enum;
             }
-*/
+
            container dto-c {
                 leaf simple-arg {
                     type uint32;
index eabc2b9..c57817e 100644 (file)
@@ -150,10 +150,11 @@ public class NetconfMappingTest extends AbstractConfigTest {
         // check after edit
         commit();
         Element response = getConfigRunning();
-        System.err.println(XmlUtil.toString(response));
+
         checkBinaryLeafEdited(response);
         checkTypeConfigAttribute(response);
         checkTypedefs(response);
+        checkEnum(response);
 
         edit("netconfMessages/editConfig_remove.xml");
 
@@ -385,6 +386,26 @@ public class NetconfMappingTest extends AbstractConfigTest {
         assertEquals(1, children.getLength());
     }
 
+    private void checkEnum(final Element response) {
+        XmlElement modulesElement = XmlElement.fromDomElement(response).getOnlyChildElement("data")
+                .getOnlyChildElement("modules");
+
+        String enumName = "extended-enum";
+        String enumContent = "TWO";
+
+        for (XmlElement moduleElement : modulesElement.getChildElements("module")) {
+            String name = moduleElement.getOnlyChildElement("name").getTextContent();
+            if(name.equals("test1")) {
+                XmlElement enumAttr = moduleElement.getOnlyChildElement(enumName);
+                assertEquals(enumContent, enumAttr.getTextContent());
+
+                return;
+            }
+        }
+
+        fail("Enum attribute " + enumName + ":" + enumContent + " not present in " + XmlUtil.toString(response));
+    }
+
     private void checkTypeConfigAttribute(Element response) {
 
         XmlElement modulesElement = XmlElement.fromDomElement(response).getOnlyChildElement("data")
index ce52e49..5735947 100644 (file)
                             1
                     </extended-twice>
 
+                    <extended-enum>
+                        TWO
+                    </extended-enum>
+
                     <simple-long-2>44</simple-long-2>
                     <binaryLeaf>8</binaryLeaf>
                     <binaryLeaf>1</binaryLeaf>