[ONOS-6354] CFG values and applied values are mismatching while trying to set a property with an invalid value

Change-Id: Id2e343c61419441d0b9554df3291b35829e850ab
(cherry picked from commit c86d95f969864664beb3948bc5ff2c455e372393)
diff --git a/core/net/src/main/java/org/onosproject/cfg/impl/ComponentConfigManager.java b/core/net/src/main/java/org/onosproject/cfg/impl/ComponentConfigManager.java
index 144c0e7..9fd99f8 100644
--- a/core/net/src/main/java/org/onosproject/cfg/impl/ComponentConfigManager.java
+++ b/core/net/src/main/java/org/onosproject/cfg/impl/ComponentConfigManager.java
@@ -168,6 +168,7 @@
                       COMPONENT_MISSING, componentName);
         checkArgument(properties.get(componentName).containsKey(name),
                       PROPERTY_MISSING, name, componentName);
+        checkValidity(componentName, name, value);
         store.setProperty(componentName, name, value);
     }
 
@@ -178,7 +179,7 @@
 
         checkNotNull(componentName, COMPONENT_NULL);
         checkNotNull(name, PROPERTY_NULL);
-
+        checkValidity(componentName, name, value);
         store.setProperty(componentName, name, value);
     }
 
@@ -281,6 +282,48 @@
         }
     }
 
+    // Checks whether the value of the specified configuration property is a valid one or not.
+    private void checkValidity(String componentName, String name, String newValue) {
+        Map<String, ConfigProperty> map = properties.get(componentName);
+        if (map == null) {
+            return;
+        }
+        ConfigProperty prop = map.get(name);
+        ConfigProperty.Type type = prop.type();
+        try {
+            switch (type) {
+                case INTEGER:
+                    Integer.parseInt(newValue);
+                    break;
+                case LONG:
+                    Long.parseLong(newValue);
+                    break;
+                case FLOAT:
+                    Float.parseFloat(newValue);
+                    break;
+                case DOUBLE:
+                    Double.parseDouble(newValue);
+                    break;
+                case BOOLEAN:
+                    if (!((newValue != null) && (newValue.equalsIgnoreCase("true") ||
+                                newValue.equalsIgnoreCase("false")))) {
+                        throw new IllegalArgumentException("Invalid " + type + " value");
+                    }
+                    break;
+                case STRING:
+                case BYTE:
+                    //do nothing
+                    break;
+                default:
+                    log.warn("Not a valid config property type");
+                    break;
+
+            }
+        } catch (NumberFormatException e) {
+                throw new NumberFormatException("Invalid " + type + " value");
+        }
+    }
+
     // Loads existing property values that may have been set.
     private void loadExistingValues(String componentName) {
         try {
@@ -292,8 +335,14 @@
                 while (it.hasMoreElements()) {
                     String name = it.nextElement();
                     ConfigProperty p = map.get(name);
-                    if (p != null) {
-                        map.put(name, ConfigProperty.setProperty(p, (String) props.get(name)));
+                    try {
+                        if (p != null) {
+                            checkValidity(componentName, name, (String) props.get(name));
+                            map.put(name, ConfigProperty.setProperty(p, (String) props.get(name)));
+                        }
+                    } catch (IllegalArgumentException e) {
+                        log.warn("Default values will be applied " +
+                                "since presetted value is not a valid " + p.type());
                     }
                 }
             }