support legacy enumeration mapping
authorSmokowski, Kevin (ks6305) <ks6305@att.com>
Tue, 24 Jul 2018 21:08:01 +0000 (21:08 +0000)
committerKevin Smokowski <ks6305@att.com>
Wed, 25 Jul 2018 13:28:54 +0000 (13:28 +0000)
support legacy enumeration mapping in MdsalHelper

Change-Id: I637f5dc368da75e24fb8cc3ebdebd002aae3f025
Issue-ID: CCSDK-396
Signed-off-by: Smokowski, Kevin (ks6305) <ks6305@att.com>
sli/provider/src/main/java/org/onap/ccsdk/sli/core/sli/provider/MdsalHelper.java
sli/provider/src/test/java/org/onap/ccsdk/sli/core/sli/provider/MdsalHelperTest.java
sli/provider/src/test/java/org/opendaylight/yang/gen/v1/test/CosModelType.java [new file with mode: 0644]
sli/provider/src/test/java/org/opendaylight/yang/gen/v1/test/WrapperObj.java [new file with mode: 0644]
sli/provider/src/test/resources/EnumerationMapping.properties [new file with mode: 0644]

index e50b997..10478dd 100644 (file)
@@ -50,7 +50,12 @@ public class MdsalHelper {
 
     private static final Logger LOG = LoggerFactory.getLogger(MdsalHelper.class);
     private static Properties yangMappingProperties = new Properties();
-
+    protected static boolean useLegacyEnumerationMapping = false;
+    
+    public static void useLegacyEnumerationMapping(Boolean bool) {
+        useLegacyEnumerationMapping = bool;
+    }
+    
     @Deprecated
     public static void setProperties(Properties input) {
         setYangMappingProperties(input);
@@ -188,9 +193,15 @@ public class MdsalHelper {
                                 }
                                 if (retValue != null) {
                                     String propName = propNamePfx + "." + fieldName;
-                                    Method method = retValue.getClass().getMethod("getName");
-                                    String yangValue = (String) method.invoke(retValue);
-                                    props.setProperty(propName, yangValue);
+                                    if(useLegacyEnumerationMapping) {
+                                        propVal = retValue.toString();
+                                        props.setProperty(propName, mapEnumeratedValue(fieldName, propVal));
+                                    }else {
+                                        Method method = retValue.getClass().getMethod("getName");
+                                        String yangValue = (String) method.invoke(retValue);
+                                        props.setProperty(propName, yangValue);
+                                    }
+
                                 }
                             } catch (Exception e) {
                                 LOG.error(
index e7f1e18..d5ca04d 100644 (file)
@@ -36,6 +36,8 @@ import org.onap.ccsdk.sli.core.sli.SvcLogicGraph;
 import org.opendaylight.yang.gen.v1.org.onap.ccsdk.sli.core.sliapi.rev161110.ExecuteGraphInputBuilder;
 import org.opendaylight.yang.gen.v1.org.onap.ccsdk.sli.core.sliapi.rev161110.execute.graph.input.SliParameter;
 import org.opendaylight.yang.gen.v1.org.onap.ccsdk.sli.core.sliapi.rev161110.execute.graph.input.SliParameterBuilder;
+import org.opendaylight.yang.gen.v1.test.CosModelType;
+import org.opendaylight.yang.gen.v1.test.WrapperObj;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddressBuilder;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -141,4 +143,32 @@ public class MdsalHelperTest extends TestCase {
         assertEquals("SomethingElse",MdsalHelper.toJavaEnum("Something.Else"));
         assertEquals("MyTestString",MdsalHelper.toJavaEnum("my-test-string"));
     }
+    
+    // During the default enumeration mapping no properties file is needed, the yang value is returned
+    // by the java object
+    public void testDefaultEnumerationMapping() throws Exception {
+        MdsalHelper.useLegacyEnumerationMapping(false);
+        Properties props = new Properties();
+        MdsalHelper.toProperties(props, new WrapperObj());
+        assertEquals("4COS", props.getProperty("wrapper-obj.cos-model-type"));
+    }
+
+    // When no properties file exists the default java value will be returned if legacy enumeration
+    // mapping is enabled
+    public void testLegacyEnumerationMappingNoProperties() throws Exception {
+        MdsalHelper.useLegacyEnumerationMapping(true);
+        Properties props = new Properties();
+        MdsalHelper.toProperties(props, new WrapperObj());
+        assertEquals("_4COS", props.getProperty("wrapper-obj.cos-model-type"));
+    }
+
+    // When a properties file exists & legacy enumeration mapping is enabled the value from the
+    // properties file should be returned
+    public void testLegacyEnumerationMappingWithProperties() throws Exception {
+        MdsalHelper.loadProperties("src/test/resources/EnumerationMapping.properties");
+        MdsalHelper.useLegacyEnumerationMapping(true);
+        Properties props = new Properties();
+        MdsalHelper.toProperties(props, new WrapperObj());
+        assertEquals("HelloWorld", props.getProperty("wrapper-obj.cos-model-type"));
+    }
 }
diff --git a/sli/provider/src/test/java/org/opendaylight/yang/gen/v1/test/CosModelType.java b/sli/provider/src/test/java/org/opendaylight/yang/gen/v1/test/CosModelType.java
new file mode 100644 (file)
index 0000000..2aaaf8a
--- /dev/null
@@ -0,0 +1,51 @@
+package org.opendaylight.yang.gen.v1.test;
+
+public enum CosModelType {
+    _4COS(0, "4COS"),
+    
+    _6COS(1, "6COS")
+    ;
+
+    private static final java.util.Map<java.lang.Integer, CosModelType> VALUE_MAP;
+
+    static {
+        final com.google.common.collect.ImmutableMap.Builder<java.lang.Integer, CosModelType> b = com.google.common.collect.ImmutableMap.builder();
+        for (CosModelType enumItem : CosModelType.values()) {
+            b.put(enumItem.value, enumItem);
+        }
+
+        VALUE_MAP = b.build();
+    }
+
+    private final java.lang.String name;
+    private final int value;
+
+    private CosModelType(int value, java.lang.String name) {
+        this.value = value;
+        this.name = name;
+    }
+
+    /**
+     * Returns the name of the enumeration item as it is specified in the input yang.
+     *
+     * @return the name of the enumeration item as it is specified in the input yang
+     */
+    public java.lang.String getName() {
+        return name;
+    }
+
+    /**
+     * @return integer value
+     */
+    public int getIntValue() {
+        return value;
+    }
+
+    /**
+     * @param valueArg integer value
+     * @return corresponding CosModelType item
+     */
+    public static CosModelType forValue(int valueArg) {
+        return VALUE_MAP.get(valueArg);
+    }
+}
\ No newline at end of file
diff --git a/sli/provider/src/test/java/org/opendaylight/yang/gen/v1/test/WrapperObj.java b/sli/provider/src/test/java/org/opendaylight/yang/gen/v1/test/WrapperObj.java
new file mode 100644 (file)
index 0000000..bae0bdb
--- /dev/null
@@ -0,0 +1,10 @@
+package org.opendaylight.yang.gen.v1.test;
+
+public class WrapperObj {
+
+    CosModelType cosModel = CosModelType._4COS;
+    
+    public CosModelType getCosModelType() {
+        return cosModel;
+    }
+}
diff --git a/sli/provider/src/test/resources/EnumerationMapping.properties b/sli/provider/src/test/resources/EnumerationMapping.properties
new file mode 100644 (file)
index 0000000..d0fb297
--- /dev/null
@@ -0,0 +1 @@
+yang.cos-model-type._4COS=HelloWorld
\ No newline at end of file