diff options
author | Kanagaraj Manickam k00365106 <kanagaraj.manickam@huawei.com> | 2017-09-20 14:58:43 +0530 |
---|---|---|
committer | Kanagaraj Manickam k00365106 <kanagaraj.manickam@huawei.com> | 2017-11-14 14:37:55 +0530 |
commit | 4b04eaa325a972ec38a7d317a60faff1664c8d39 (patch) | |
tree | a14ff8fe984a499e15661ced75895fa060cc49d1 /framework/src/main/java/org/onap | |
parent | 8782d93af5c1a65dfe1f07aabe6afa69d6f3e155 (diff) |
Add default_value for output attributes
Special enteries are $s{uuit} and $s{env:EVN-VAR}
Issue-Id: CLI-66
Change-Id: I6c16d8815f4934b7759adee3a41372303cef9a65
Signed-off-by: Kanagaraj Manickam k00365106 <kanagaraj.manickam@huawei.com>
Diffstat (limited to 'framework/src/main/java/org/onap')
5 files changed, 158 insertions, 52 deletions
diff --git a/framework/src/main/java/org/onap/cli/fw/OnapCommand.java b/framework/src/main/java/org/onap/cli/fw/OnapCommand.java index 9ca43fdf..4f125aea 100644 --- a/framework/src/main/java/org/onap/cli/fw/OnapCommand.java +++ b/framework/src/main/java/org/onap/cli/fw/OnapCommand.java @@ -42,6 +42,7 @@ import org.onap.cli.fw.error.OnapCommandRegistrationFailed; import org.onap.cli.fw.error.OnapCommandSchemaNotFound; import org.onap.cli.fw.input.OnapCommandParameter; import org.onap.cli.fw.output.OnapCommandResult; +import org.onap.cli.fw.output.OnapCommandResultAttribute; import org.onap.cli.fw.output.OnapCommandResultAttributeScope; import org.onap.cli.fw.output.ResultType; import org.onap.cli.fw.utils.OnapCommandUtils; @@ -267,6 +268,15 @@ public abstract class OnapCommand { this.cmdResult.setDebug(true); } + //pre-process result attributes for spl entries and input parameters + for (OnapCommandResultAttribute attr: this.cmdResult.getRecords()) { + if (!attr.getDefaultValue().isEmpty()) { + attr.setDefaultValue(OnapCommandUtils.replaceLineForSpecialValues(attr.getDefaultValue())); + attr.setDefaultValue(OnapCommandUtils.replaceLineFromInputParameters( + attr.getDefaultValue(), this.getParametersMap())); + } + } + try { OnapCredentials creds = OnapCommandUtils.fromParameters(this.getParameters()); diff --git a/framework/src/main/java/org/onap/cli/fw/conf/Constants.java b/framework/src/main/java/org/onap/cli/fw/conf/Constants.java index 9fccc668..29cdbce9 100644 --- a/framework/src/main/java/org/onap/cli/fw/conf/Constants.java +++ b/framework/src/main/java/org/onap/cli/fw/conf/Constants.java @@ -202,6 +202,9 @@ public class Constants { public static final String SAMPLE_GEN_ENABLED = "cli.sample.gen.enable"; public static final String SAMPLE_GEN_TARGET_FOLDER = "cli.sample.gen.target"; + public static final String SPL_ENTRY_UUID = "uuid"; + public static final String SPL_ENTRY_ENV = "env:"; + private Constants() { } 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 31b83abb..3ba15ff4 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 @@ -22,6 +22,7 @@ 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 java.io.File; import java.util.List; @@ -65,6 +66,11 @@ public class OnapCommandParameter { private String defaultValue = ""; /* + * raw default value, stored with out processing it. + */ + private String rawDefaultValue = ""; + + /* * Is optional */ private boolean isOptional = false; @@ -77,7 +83,12 @@ public class OnapCommandParameter { /* * Parameter Value */ - private Object value; + private Object value = null; + + /* + * raw value, get stored as its without processing it. + */ + private Object rawValue = null; public String getName() { return cmdName; @@ -117,6 +128,15 @@ public class OnapCommandParameter { public void setParameterType(ParameterType parameterType) { this.parameterType = parameterType; + + if (this.defaultValue.isEmpty()) { + if (this.getParameterType().equals(ParameterType.BOOL)) { + // For bool type always the default param is false + this.defaultValue = "false"; + } else if (this.getParameterType().equals(ParameterType.UUID)) { + this.defaultValue = UUID.randomUUID().toString(); + } + } } /** @@ -125,16 +145,6 @@ public class OnapCommandParameter { * @return string */ public String getDefaultValue() { - if (this.isDefaultValueAnEnv()) { - String envVar = this.getEnvVarNameFromDefaultValue(); - this.defaultValue = System.getenv(envVar); - } else if (this.getParameterType().equals(ParameterType.BOOL)) { - // For bool type always the default param is false - this.defaultValue = "false"; - } else if (this.defaultValue.isEmpty() && this.getParameterType().equals(ParameterType.UUID)) { - this.defaultValue = UUID.randomUUID().toString(); - } - return defaultValue; } @@ -143,21 +153,22 @@ public class OnapCommandParameter { * * @return boolean */ - public boolean isDefaultValueAnEnv() { - return this.defaultValue.trim().startsWith("${") && this.defaultValue.trim().endsWith("}"); + public boolean isRawDefaultValueAnEnv() { + return this.rawDefaultValue.trim().startsWith("$s{env:") && this.rawDefaultValue.trim().endsWith("}"); } /** - * check if the default value is ${ENV_VAR_NAME} and return the ENV_VAR_NAME. + * check if the default value is $s{env:ENV_VAR_NAME} and return the ENV_VAR_NAME. * * @return ENV_VAR_NAME */ - public String getEnvVarNameFromDefaultValue() { - return this.defaultValue.trim().substring(2, this.defaultValue.length() - 1); + public String getEnvVarNameFromrRawDefaultValue() { + return this.rawDefaultValue.trim().substring(7, this.rawDefaultValue.length() - 1); } public void setDefaultValue(String defaultValue) { - this.defaultValue = defaultValue; + this.rawDefaultValue = defaultValue; + this.defaultValue = OnapCommandUtils.replaceLineForSpecialValues(this.rawDefaultValue); } /** @@ -167,44 +178,46 @@ public class OnapCommandParameter { * @throws OnapCommandInvalidParameterValue * exception */ - public Object getValue() throws OnapCommandInvalidParameterValue { + public Object getValue() { if (value != null) { - if (ParameterType.URL.equals(parameterType) && !value.toString().startsWith("http") - && !value.toString().startsWith("/")) { - value = "/" + value; - } else if (ParameterType.ARRAY.equals(parameterType)) { - if (!(value instanceof List)) { - throw new OnapCommandInvalidParameterValue(this.getName()); - } - - List<String> list = (List<String>) value; - ObjectMapper mapper = new ObjectMapper(); - try { - return mapper.writeValueAsString(list); - } catch (JsonProcessingException e) { - throw new OnapCommandInvalidParameterValue(this.getName(), e); - } - } else if (ParameterType.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 { - return mapper.writeValueAsString(map); - } catch (JsonProcessingException e) { - throw new OnapCommandInvalidParameterValue(this.getName(), e); - } - } - return value; } return getDefaultValue(); } - public void setValue(Object value) { - this.value = value; + public void setValue(Object value) throws OnapCommandInvalidParameterValue { + this.rawValue = value; + + if (ParameterType.URL.equals(parameterType) && !value.toString().isEmpty() && !value.toString().startsWith("http") + && !value.toString().startsWith("/")) { + this.value = "/" + value; + } else if (ParameterType.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 (ParameterType.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 { + this.value = value; + } } public boolean isOptional() { diff --git a/framework/src/main/java/org/onap/cli/fw/output/OnapCommandResultAttribute.java b/framework/src/main/java/org/onap/cli/fw/output/OnapCommandResultAttribute.java index 73617049..fcd6f1a2 100644 --- a/framework/src/main/java/org/onap/cli/fw/output/OnapCommandResultAttribute.java +++ b/framework/src/main/java/org/onap/cli/fw/output/OnapCommandResultAttribute.java @@ -19,6 +19,7 @@ package org.onap.cli.fw.output; import org.onap.cli.fw.input.ParameterType; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; /** @@ -43,6 +44,11 @@ public class OnapCommandResultAttribute { private List<String> values = new ArrayList<>(); /* + * default value, useful to set when a command want to set the default value for a output attributes. + */ + private String defaultValue = ""; + + /* * Output scope */ private OnapCommandResultAttributeScope outScope = OnapCommandResultAttributeScope.SHORT; @@ -72,6 +78,9 @@ public class OnapCommandResultAttribute { } public List<String> getValues() { + if (this.values.isEmpty() && !this.defaultValue.isEmpty()) { + return Arrays.asList(new String [] {this.defaultValue}); + } return values; } @@ -99,4 +108,12 @@ public class OnapCommandResultAttribute { this.isSecured = isSecured; } + public String getDefaultValue() { + return defaultValue; + } + + public void setDefaultValue(String defaultValue) { + this.defaultValue = defaultValue; + } + } diff --git a/framework/src/main/java/org/onap/cli/fw/utils/OnapCommandUtils.java b/framework/src/main/java/org/onap/cli/fw/utils/OnapCommandUtils.java index f891fd09..9c4306c1 100644 --- a/framework/src/main/java/org/onap/cli/fw/utils/OnapCommandUtils.java +++ b/framework/src/main/java/org/onap/cli/fw/utils/OnapCommandUtils.java @@ -102,6 +102,7 @@ import java.util.Map; import java.util.Map.Entry; import java.util.ServiceLoader; import java.util.Set; +import java.util.UUID; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -682,6 +683,11 @@ public class OnapCommandUtils { } break; + case DEFAULT_VALUE: + Object obj = map.get(key4); + attr.setDefaultValue(obj.toString()); + break; + case IS_SECURED: if (validate) { if (!validateBoolean(String.valueOf(map.get(key4)))) { @@ -1136,8 +1142,8 @@ public class OnapCommandUtils { } String defaultMsg = " By default, it is "; - if (param.isDefaultValueAnEnv()) { - optSecondCol += defaultMsg + "read from environment variable " + param.getEnvVarNameFromDefaultValue() + if (param.isRawDefaultValueAnEnv()) { + optSecondCol += defaultMsg + "read from environment variable " + param.getEnvVarNameFromrRawDefaultValue() + "."; } else if (param.getDefaultValue() != null && !((String)param.getDefaultValue()).isEmpty()) { optSecondCol += defaultMsg + param.getDefaultValue() + "."; @@ -1296,7 +1302,63 @@ public class OnapCommandUtils { return methodName; } - private static String replaceLineFromInputParameters(String line, Map<String, OnapCommandParameter> params) + /** + * There are unique values like uuid is supported, so when input, output (default) values has + * these special entries, then it will get replaced with it's value + * + * @param line + * @return + */ + public static String replaceLineForSpecialValues(String line) { + String result = ""; + + if (!line.contains("$s{")) { + return line; + } + + int currentIdx = 0; + while (currentIdx < line.length()) { + int idxS = line.indexOf("$s{", currentIdx); + if (idxS == -1) { + result += line.substring(currentIdx); + break; + } + int idxE = line.indexOf("}", idxS); + String splEntry = line.substring(idxS + 3, idxE); + splEntry = splEntry.trim(); + + String value = ""; + + switch (splEntry) { + case Constants.SPL_ENTRY_UUID: + value = UUID.randomUUID().toString(); + break; + + default: + + if (splEntry.startsWith(Constants.SPL_ENTRY_ENV)) { + //start to read after env:ENV_VAR_NAME + String envVarName = splEntry.substring(4); + value = System.getenv(envVarName); + if (value == null) { + //when env is not defined, assign the same env:ENV_VAR_NAME + //so that it will given hit to user that ENV_VAR_NAME to be + //defined. + value = splEntry; + } + } else { + value = splEntry; + } + } + + result += line.substring(currentIdx, idxS) + value; + currentIdx = idxE + 1; + } + + return result; + } + + public static String replaceLineFromInputParameters(String line, Map<String, OnapCommandParameter> params) throws OnapCommandException { String result = ""; @@ -1487,6 +1549,7 @@ public class OnapCommandUtils { inp.getReqQueries().put(h, replaceLineFromInputParameters(value, params)); } + //mrkanag replaceLineFromInputParameters for result_map, to support input param in result output return inp; } |