diff options
Diffstat (limited to 'framework/src/main/java/org/onap/cli/fw/utils')
3 files changed, 133 insertions, 12 deletions
diff --git a/framework/src/main/java/org/onap/cli/fw/utils/OnapCommandDiscoveryUtils.java b/framework/src/main/java/org/onap/cli/fw/utils/OnapCommandDiscoveryUtils.java index 7c1f18f6..a94087ec 100644 --- a/framework/src/main/java/org/onap/cli/fw/utils/OnapCommandDiscoveryUtils.java +++ b/framework/src/main/java/org/onap/cli/fw/utils/OnapCommandDiscoveryUtils.java @@ -508,7 +508,6 @@ public class OnapCommandDiscoveryUtils { Map<String, String> sample = samples.get(sampleId); List<String> inputArgs = new ArrayList(); - inputArgs.add(cmd); if (sample.get(OnapCommandConstants.VERIFY_INPUT) != null) { inputArgs.addAll(Arrays.asList(sample.get(OnapCommandConstants.VERIFY_INPUT).trim().split(" "))); } 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 e279fa0b..96f864e0 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 @@ -31,6 +31,8 @@ import java.util.Map.Entry; import java.util.Set; import java.util.UUID; +import org.apache.commons.codec.binary.Base64; +import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.io.FileUtils; import org.onap.cli.fw.cmd.OnapCommand; import org.onap.cli.fw.conf.OnapCommandConfig; @@ -185,6 +187,49 @@ public class OnapCommandUtils { * @return */ public static String replaceLineForSpecialValues(String lineSpl) { + return replaceLineForSpecialValues(lineSpl, new HashMap<String, String>()); + } + + /** + * + * @param lineSpl + * @param values Value for the given entry already known by the caller. + * @return + */ + public static String replaceLineFromResults(String line, Map <String, String> values) { + String resultLine = ""; + + if (!line.contains("$r{")) { + return line; + } + + int currentIdx = 0; + while (currentIdx < line.length()) { + int idxS = line.indexOf("$r{", currentIdx); + if (idxS == -1) { + resultLine += line.substring(currentIdx); + break; + } + int idxE = line.indexOf("}", idxS); + String attr = line.substring(idxS + 3, idxE); + attr = attr.trim(); + + String value = ""; + + if (values.get(attr) != null) { + value = values.get(attr); + } else { + value = attr; + } + + resultLine += line.substring(currentIdx, idxS) + value; + currentIdx = idxE + 1; + } + + return resultLine; + } + + public static String replaceLineForSpecialValues(String lineSpl, Map <String, String> values) { String resultSpl = ""; if (!lineSpl.contains("$s{")) { @@ -232,8 +277,20 @@ public class OnapCommandUtils { //exist. value = ""; } + } else if (splEntry.startsWith(OnapCommandConstants.SPL_ENTRY_MD5)) { + //start to read after md5:entryname + String entryName = splEntry.substring(4); + String content = values.get(entryName); + if (content != null) + value = OnapCommandUtils.md5(content); + else + value = splEntry; } else { - value = splEntry; + if (values.get(splEntry) != null) { + value = values.get(splEntry); + } else { + value = splEntry; + } } } @@ -271,7 +328,11 @@ public class OnapCommandUtils { || OnapCommandParameterType.JSON.equals(param.getParameterType()) || OnapCommandParameterType.YAML.equals(param.getParameterType())) { // ignore the front and back double quotes in json body - result += line.substring(currentIdx, idxS - 1) + params.get(paramName).getValue().toString(); + String va_ = params.get(paramName).getValue().toString(); + if (idxS > 0) + result += line.substring(currentIdx, idxS - 1) + va_; + else + result += va_; currentIdx = idxE + 2; } else if (OnapCommandParameterType.MAP.equals(param.getParameterType())) { try { @@ -366,6 +427,12 @@ public class OnapCommandUtils { } } + public static String md5(String content) { + String md5 = DigestUtils.md5Hex(content); + + byte[] encodeBase64 = Base64.encodeBase64(md5.getBytes()); + return new String(encodeBase64); + } } diff --git a/framework/src/main/java/org/onap/cli/fw/utils/ProcessRunner.java b/framework/src/main/java/org/onap/cli/fw/utils/ProcessRunner.java index eb335837..db7245c7 100644 --- a/framework/src/main/java/org/onap/cli/fw/utils/ProcessRunner.java +++ b/framework/src/main/java/org/onap/cli/fw/utils/ProcessRunner.java @@ -17,18 +17,18 @@ package org.onap.cli.fw.utils; import java.io.BufferedReader; +import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.io.OutputStream; import java.io.StringWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.List; -import java.util.Map; import java.util.concurrent.TimeUnit; import org.apache.commons.io.IOUtils; -import org.onap.cli.fw.cmd.OnapCommand; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -43,8 +43,9 @@ public class ProcessRunner { private int exitCode = -1; private String output; private String error; - private Map<String, Object> results; - + private long timeout = 0; + private OutputStream stdout; + private OutputStream stderr; public ProcessRunner(String []cmd, String []env, String cwd) { this.cmd = cmd; @@ -55,6 +56,14 @@ public class ProcessRunner { this.env = env; } + public void setTimeout(long timeout) { + this.timeout = timeout; + } + + public long getTimeout() { + return this.timeout; + } + public void overrideToUnix() { this.shell = UNIX_SHELL; } @@ -82,9 +91,13 @@ public class ProcessRunner { @SuppressWarnings("unchecked") public void run() throws InterruptedException, IOException { Process p = null; + final StringWriter writerOutput = new StringWriter(); final StringWriter writerError = new StringWriter(); + final OutputStream stdout = this.getStdout(); + final OutputStream stderr = this.getStderr(); + if (this.cmd.length == 1) { p = Runtime.getRuntime().exec(this.shell + this.cmd[0], this.env, null); } else { @@ -98,7 +111,10 @@ public class ProcessRunner { new Thread(new Runnable() { public void run() { try { - IOUtils.copy(p1.getInputStream(), writerOutput); + if (stdout != null) { + IOUtils.copy(p1.getInputStream(), stdout); + } + else IOUtils.copy(p1.getInputStream(), writerOutput); } catch (IOException e) { } } @@ -107,21 +123,32 @@ public class ProcessRunner { new Thread(new Runnable() { public void run() { try { - IOUtils.copy(p1.getErrorStream(), writerError); + if (stderr != null) { + IOUtils.copy(p1.getErrorStream(), stderr); + } + else IOUtils.copy(p1.getErrorStream(), writerError); } catch (IOException e) { } } }).start(); - //mrkanag: handle the case if the given cmd does not exist - p.waitFor(1, TimeUnit.MINUTES); - this.exitCode = p.exitValue(); + boolean completed = p.waitFor(this.getTimeout(), TimeUnit.MILLISECONDS); + if (completed) { + this.exitCode = p.exitValue(); + } + this.output = writerOutput.toString(); this.error = writerError.toString(); log.debug("CMD: " + Arrays.asList(this.cmd).toString() + "\nWORKING_DIR: " + this.cwd + "\nENV: " + ((this.env == null) ? this.env : Arrays.asList(this.env).toString()) + "\nOUTPUT: " + this.output + "\nERROR: " + this.error + "\nEXIT_CODE: " + this.exitCode); p.destroy(); + + if (!completed) { + throw new RuntimeException("TIMEOUT:: cmd:" + Arrays.asList(this.cmd).toString()); + } else { + + } } public String streamToString(InputStream stream) throws IOException { @@ -152,4 +179,32 @@ public class ProcessRunner { public String getError() { return this.error; } + + public OutputStream getStdout() { + return stdout; + } + + public void setStdout(OutputStream stdout) { + this.stdout = stdout; + } + + public OutputStream getStderr() { + return stderr; + } + + public void setStderr(OutputStream stderr) { + this.stderr = stderr; + } + + public String toString() { + StringBuilder sb = new StringBuilder(); + + sb.append("COMMAND: " + this.shell + " " + Arrays.asList(this.cmd)); + sb.append("\nCWD: " + new File(this.cwd).getAbsolutePath()); + sb.append("\nTIMEOUT: " + this.timeout); + sb.append("\nEXIT-CODE: " + this.getExitCode()); + sb.append("\nENVIRONMENTS: " + Arrays.asList(this.env)); + + return sb.toString(); + } }
\ No newline at end of file |