summaryrefslogtreecommitdiffstats
path: root/framework/src/main/java
diff options
context:
space:
mode:
authorsubhash kumar singh <subhash.kumar.singh@huawei.com>2018-02-23 06:05:28 +0000
committersubhash kumar singh <subhash.kumar.singh@huawei.com>2018-02-28 09:12:38 +0000
commit225400a393da49322bdf500a61ae423ec00efead (patch)
tree2d55acf0d8b7833bd8e595cb902e29a4c789d9bb /framework/src/main/java
parent3903891dfe8f9e2488fc9d1bd0858fa4670cff0e (diff)
Make default value type object
Enhance default value type from String to Object so that it will directly support different type (BOOL, Array, Map). Issue-ID: CLI-74 Change-Id: Iaca438e58150c80047340acba92917ecae685242 Signed-off-by: subhash kumar singh <subhash.kumar.singh@huawei.com>
Diffstat (limited to 'framework/src/main/java')
-rw-r--r--framework/src/main/java/org/onap/cli/fw/input/OnapCommandParameter.java126
-rw-r--r--framework/src/main/java/org/onap/cli/fw/schema/OnapCommandSchemaLoader.java2
-rw-r--r--framework/src/main/java/org/onap/cli/fw/utils/OnapCommandHelperUtils.java2
3 files changed, 93 insertions, 37 deletions
diff --git a/framework/src/main/java/org/onap/cli/fw/input/OnapCommandParameter.java b/framework/src/main/java/org/onap/cli/fw/input/OnapCommandParameter.java
index 465dffa4..22f341d5 100644
--- a/framework/src/main/java/org/onap/cli/fw/input/OnapCommandParameter.java
+++ b/framework/src/main/java/org/onap/cli/fw/input/OnapCommandParameter.java
@@ -16,18 +16,20 @@
package org.onap.cli.fw.input;
-import java.io.File;
-import java.util.List;
-import java.util.Map;
-import java.util.UUID;
-
+import com.fasterxml.jackson.databind.ObjectMapper;
import org.onap.cli.fw.error.OnapCommandException;
import org.onap.cli.fw.error.OnapCommandInvalidParameterValue;
import org.onap.cli.fw.error.OnapCommandParameterMissing;
import org.onap.cli.fw.utils.OnapCommandUtils;
-import com.fasterxml.jackson.core.JsonProcessingException;
-import com.fasterxml.jackson.databind.ObjectMapper;
+import java.io.File;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.UUID;
/**
* Oclip Command's input parameter.
@@ -63,7 +65,7 @@ public class OnapCommandParameter {
/*
* Default value
*/
- private String defaultValue = "";
+ private Object defaultValue;
/*
* raw default value, stored with out processing it.
@@ -150,12 +152,24 @@ public class OnapCommandParameter {
public void setParameterType(OnapCommandParameterType parameterType) {
this.parameterType = parameterType;
- if (this.defaultValue.isEmpty()) {
- if (this.getParameterType().equals(OnapCommandParameterType.BOOL)) {
- // For bool type always the default param is false
- this.defaultValue = "false";
- } else if (this.getParameterType().equals(OnapCommandParameterType.UUID)) {
- this.defaultValue = UUID.randomUUID().toString();
+ if (defaultValue == null) {
+
+ switch (getParameterType()) {
+ case MAP:
+ this.defaultValue = new HashMap<String, String>();
+ break;
+ case ARRAY:
+ defaultValue = new ArrayList<String>();
+ break;
+ case BOOL:
+ defaultValue = false;
+ break;
+ case UUID:
+ this.defaultValue = UUID.randomUUID().toString();
+ break;
+ default:
+ this.defaultValue = null;
+ break;
}
}
}
@@ -165,7 +179,7 @@ public class OnapCommandParameter {
*
* @return string
*/
- public String getDefaultValue() {
+ public Object getDefaultValue() {
return defaultValue;
}
@@ -187,9 +201,63 @@ public class OnapCommandParameter {
return this.rawDefaultValue.trim().substring(7, this.rawDefaultValue.length() - 1);
}
- public void setDefaultValue(String defaultValue) {
- this.rawDefaultValue = defaultValue;
- this.defaultValue = OnapCommandUtils.replaceLineForSpecialValues(this.rawDefaultValue);
+ public void setRawDefaultValue(String value) throws OnapCommandInvalidParameterValue {
+ this.rawDefaultValue = value;
+ String processedValue= OnapCommandUtils.replaceLineForSpecialValues(value);
+
+ switch (parameterType) {
+ case MAP:
+ try {
+ defaultValue = new ObjectMapper().readValue(processedValue, Map.class);
+ } catch (IOException e) {
+ throw new OnapCommandInvalidParameterValue("Invalid default value for " + this.getName(), e);
+ }
+ break;
+
+ case ARRAY:
+ try {
+ defaultValue = new ObjectMapper().readValue(processedValue, List.class);
+ } catch (IOException e) {
+ throw new OnapCommandInvalidParameterValue("Invalid default value for " + this.getName(), e);
+ }
+ break;
+
+ case BOOL:
+ defaultValue = processedValue.equalsIgnoreCase("true");
+ break;
+
+ default:
+ defaultValue = processedValue;
+ break;
+ }
+ }
+
+ public void setDefaultValue(Object defaultValue) throws OnapCommandInvalidParameterValue {
+ //check type
+ Class<?> clz;
+ switch (parameterType) {
+ case BOOL:
+ clz = Boolean.class;
+ break;
+
+ case MAP:
+ clz = Map.class;
+ break;
+
+ case ARRAY:
+ clz = Collection.class;
+ break;
+
+ default:
+ clz = String.class;
+ break;
+ }
+
+ if (clz.isInstance(defaultValue)) {
+ this.defaultValue = defaultValue;
+ } else {
+ throw new OnapCommandInvalidParameterValue("Invalid default value for parameter: " + this.getName());
+ }
}
/**
@@ -212,34 +280,22 @@ public class OnapCommandParameter {
if (OnapCommandParameterType.URL.equals(parameterType) && !value.toString().isEmpty() && !value.toString().startsWith("http")
&& !value.toString().startsWith("/")) {
- this.value = "/" + value;
+ value = "/" + value;
} else if (OnapCommandParameterType.ARRAY.equals(parameterType)) {
if (!(value instanceof List)) {
throw new OnapCommandInvalidParameterValue(this.getName());
}
- List<String> list = (List<String>) value;
- ObjectMapper mapper = new ObjectMapper();
- try {
- this.value = mapper.writeValueAsString(list);
- } catch (JsonProcessingException e) {
- throw new OnapCommandInvalidParameterValue(this.getName(), e);
- }
} else if (OnapCommandParameterType.MAP.equals(parameterType)) {
if (!(value instanceof Map)) {
throw new OnapCommandInvalidParameterValue(this.getName());
}
-
- Map<String, String> map = (Map<String, String>) value;
- ObjectMapper mapper = new ObjectMapper();
- try {
- this.value = mapper.writeValueAsString(map);
- } catch (JsonProcessingException e) {
- throw new OnapCommandInvalidParameterValue(this.getName(), e);
+ } else if (OnapCommandParameterType.BOOL.equals(parameterType)) {
+ if (!(value instanceof Boolean)) {
+ throw new OnapCommandInvalidParameterValue(this.getName());
}
- } else {
- this.value = value;
}
+ this.value = value;
}
public boolean isOptional() {
diff --git a/framework/src/main/java/org/onap/cli/fw/schema/OnapCommandSchemaLoader.java b/framework/src/main/java/org/onap/cli/fw/schema/OnapCommandSchemaLoader.java
index b47000a1..439eb970 100644
--- a/framework/src/main/java/org/onap/cli/fw/schema/OnapCommandSchemaLoader.java
+++ b/framework/src/main/java/org/onap/cli/fw/schema/OnapCommandSchemaLoader.java
@@ -318,7 +318,7 @@ public class OnapCommandSchemaLoader {
case DEFAULT_VALUE:
Object obj = parameter.get(key2);
- param.setDefaultValue(obj.toString());
+ param.setRawDefaultValue(obj.toString());
break;
case TYPE:
diff --git a/framework/src/main/java/org/onap/cli/fw/utils/OnapCommandHelperUtils.java b/framework/src/main/java/org/onap/cli/fw/utils/OnapCommandHelperUtils.java
index e9ea81d4..1387ea3d 100644
--- a/framework/src/main/java/org/onap/cli/fw/utils/OnapCommandHelperUtils.java
+++ b/framework/src/main/java/org/onap/cli/fw/utils/OnapCommandHelperUtils.java
@@ -157,7 +157,7 @@ public class OnapCommandHelperUtils {
if (param.isRawDefaultValueAnEnv()) {
optSecondCol += defaultMsg + "read from environment variable " + param.getEnvVarNameFromrRawDefaultValue()
+ ".";
- } else if (param.getDefaultValue() != null && !((String)param.getDefaultValue()).isEmpty()) {
+ } else if (param.getDefaultValue() != null && param.getDefaultValue().toString().isEmpty()) {
optSecondCol += defaultMsg + param.getDefaultValue() + ".";
}