diff options
42 files changed, 959 insertions, 249 deletions
diff --git a/framework/src/main/java/org/onap/cli/fw/cmd/OnapCommand.java b/framework/src/main/java/org/onap/cli/fw/cmd/OnapCommand.java index b814ba82..d73df4fb 100644 --- a/framework/src/main/java/org/onap/cli/fw/cmd/OnapCommand.java +++ b/framework/src/main/java/org/onap/cli/fw/cmd/OnapCommand.java @@ -39,6 +39,7 @@ import org.onap.cli.fw.schema.OnapCommandSchemaLoader; import org.onap.cli.fw.schema.OnapCommandSchemaMerger; import org.onap.cli.fw.store.OnapCommandArtifactStore; import org.onap.cli.fw.store.OnapCommandArtifactStore.Artifact; +import org.onap.cli.fw.store.OnapCommandExecutionStore.ExecutionStoreContext; import org.onap.cli.fw.utils.OnapCommandHelperUtils; import org.onap.cli.fw.utils.OnapCommandUtils; import org.slf4j.Logger; @@ -73,6 +74,8 @@ public abstract class OnapCommand { private boolean isRpc = false; + private ExecutionStoreContext executionStoreContext; + public boolean isRpc() { return isRpc; } @@ -234,6 +237,7 @@ public abstract class OnapCommand { protected void postRun() throws OnapCommandException { log.debug(this.getName() + " POST-RUN"); } + /** * Oclip command execute with given parameters on service. Before calling this method, its mandatory to set all * parameters value. @@ -314,6 +318,8 @@ public abstract class OnapCommand { log.info("OUTPUT: " + this.cmdResult.getRecords()); postRun(); + + return this.cmdResult; } @@ -343,4 +349,12 @@ public abstract class OnapCommand { return OnapCommandHelperUtils.help(this); } // (mrkanag) Add toString for all command, parameter, result, etc objects in JSON format + + public void setExecutionContext(ExecutionStoreContext executionStoreContext) { + this.executionStoreContext = executionStoreContext; + } + + public ExecutionStoreContext getExecutionContext() { + return this.executionStoreContext; + } } diff --git a/framework/src/main/java/org/onap/cli/fw/cmd/OnapCommandType.java b/framework/src/main/java/org/onap/cli/fw/cmd/OnapCommandType.java index 34ccc760..e874a14e 100644 --- a/framework/src/main/java/org/onap/cli/fw/cmd/OnapCommandType.java +++ b/framework/src/main/java/org/onap/cli/fw/cmd/OnapCommandType.java @@ -27,8 +27,7 @@ public enum OnapCommandType { AUTH, CATALOG, - CMD, - SYSTEM; + CMD; /** * Get parameter type. @@ -46,8 +45,6 @@ public enum OnapCommandType { return CATALOG; } else if (CMD.name().equalsIgnoreCase(name)) { return CMD; - } else if (SYSTEM.name().equalsIgnoreCase(name)) { - return SYSTEM; } else { throw new OnapCommandInvalidCommandType(name); } diff --git a/framework/src/main/java/org/onap/cli/fw/cmd/execution/OnapCommandExceutionShowDebugCommand.java b/framework/src/main/java/org/onap/cli/fw/cmd/execution/OnapCommandExceutionShowDebugCommand.java new file mode 100644 index 00000000..e6be04aa --- /dev/null +++ b/framework/src/main/java/org/onap/cli/fw/cmd/execution/OnapCommandExceutionShowDebugCommand.java @@ -0,0 +1,34 @@ +/* + * Copyright 2019 Huawei Technologies Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.onap.cli.fw.cmd.execution; + +import org.onap.cli.fw.cmd.OnapCommand; +import org.onap.cli.fw.error.OnapCommandException; +import org.onap.cli.fw.schema.OnapCommandSchema; +import org.onap.cli.fw.store.OnapCommandExecutionStore; + +@OnapCommandSchema(schema = "execution-show-debug.yaml") +public class OnapCommandExceutionShowDebugCommand extends OnapCommand { + + @Override + protected void run() throws OnapCommandException { + String executionId = getParametersMap().get("execution-id").getValue().toString(); + this.getResult().setOutput(OnapCommandExecutionStore.getStore().showExecutionDebug(executionId)); + } + + +} diff --git a/framework/src/main/java/org/onap/cli/fw/cmd/execution/OnapCommandExceutionShowErrCommand.java b/framework/src/main/java/org/onap/cli/fw/cmd/execution/OnapCommandExceutionShowErrCommand.java new file mode 100644 index 00000000..bd3ff9ec --- /dev/null +++ b/framework/src/main/java/org/onap/cli/fw/cmd/execution/OnapCommandExceutionShowErrCommand.java @@ -0,0 +1,34 @@ +/* + * Copyright 2019 Huawei Technologies Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.onap.cli.fw.cmd.execution; + +import org.onap.cli.fw.cmd.OnapCommand; +import org.onap.cli.fw.error.OnapCommandException; +import org.onap.cli.fw.schema.OnapCommandSchema; +import org.onap.cli.fw.store.OnapCommandExecutionStore; + +@OnapCommandSchema(schema = "execution-show-err.yaml") +public class OnapCommandExceutionShowErrCommand extends OnapCommand { + + @Override + protected void run() throws OnapCommandException { + String executionId = getParametersMap().get("execution-id").getValue().toString(); + this.getResult().setOutput(OnapCommandExecutionStore.getStore().showExecutionErr(executionId)); + } + + +} diff --git a/framework/src/main/java/org/onap/cli/fw/cmd/execution/OnapCommandExceutionShowOutCommand.java b/framework/src/main/java/org/onap/cli/fw/cmd/execution/OnapCommandExceutionShowOutCommand.java new file mode 100644 index 00000000..878ceb13 --- /dev/null +++ b/framework/src/main/java/org/onap/cli/fw/cmd/execution/OnapCommandExceutionShowOutCommand.java @@ -0,0 +1,32 @@ +/* + * Copyright 2019 Huawei Technologies Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.onap.cli.fw.cmd.execution; + +import org.onap.cli.fw.cmd.OnapCommand; +import org.onap.cli.fw.error.OnapCommandException; +import org.onap.cli.fw.schema.OnapCommandSchema; +import org.onap.cli.fw.store.OnapCommandExecutionStore; + +@OnapCommandSchema(schema = "execution-show-out.yaml") +public class OnapCommandExceutionShowOutCommand extends OnapCommand { + + @Override + protected void run() throws OnapCommandException { + String executionId = getParametersMap().get("execution-id").getValue().toString(); + this.getResult().setOutput(OnapCommandExecutionStore.getStore().showExecutionOut(executionId)); + } +} diff --git a/framework/src/main/java/org/onap/cli/fw/cmd/product/OnapServiceListCommand.java b/framework/src/main/java/org/onap/cli/fw/cmd/product/OnapServiceListCommand.java index 010cdc19..fa4f60b9 100644 --- a/framework/src/main/java/org/onap/cli/fw/cmd/product/OnapServiceListCommand.java +++ b/framework/src/main/java/org/onap/cli/fw/cmd/product/OnapServiceListCommand.java @@ -73,6 +73,7 @@ public class OnapServiceListCommand extends OnapCommand { } for (String service : rslt.getOrDefault(product, new HashSet<String>())) { + this.getResult().getRecordsMap().get("product").getValues().add(product); this.getResult().getRecordsMap().get("service").getValues().add(service); this.getResult().getRecordsMap().get("description").getValues().add( serviceDescs.containsKey(service) ? serviceDescs.get(service) : ""); diff --git a/framework/src/main/java/org/onap/cli/fw/cmd/schema/OnapSchemaListCommand.java b/framework/src/main/java/org/onap/cli/fw/cmd/schema/OnapSchemaListCommand.java index ef22e4af..5046f44a 100644 --- a/framework/src/main/java/org/onap/cli/fw/cmd/schema/OnapSchemaListCommand.java +++ b/framework/src/main/java/org/onap/cli/fw/cmd/schema/OnapSchemaListCommand.java @@ -47,6 +47,7 @@ public class OnapSchemaListCommand extends OnapCommand { if (service.length() > 0 && !service.equalsIgnoreCase(schema.getService())) { continue; } + this.getResult().getRecordsMap().get("product").getValues().add(schema.getProduct()); this.getResult().getRecordsMap().get("command").getValues().add(schema.getCmdName()); this.getResult().getRecordsMap().get("schema").getValues().add(schema.getSchemaName()); this.getResult().getRecordsMap().get("service").getValues().add(schema.getService()); diff --git a/framework/src/main/java/org/onap/cli/fw/conf/OnapCommandConfig.java b/framework/src/main/java/org/onap/cli/fw/conf/OnapCommandConfig.java index c59521e0..b395177f 100644 --- a/framework/src/main/java/org/onap/cli/fw/conf/OnapCommandConfig.java +++ b/framework/src/main/java/org/onap/cli/fw/conf/OnapCommandConfig.java @@ -22,6 +22,8 @@ import java.util.Map.Entry; import java.util.Properties; import java.util.stream.Collectors; +import org.onap.cli.fw.utils.OnapCommandUtils; + /** * Oclip command constants. * @@ -38,7 +40,8 @@ public final class OnapCommandConfig { } static { - loadProperties(prps, OnapCommandConstants.CONF); + addProperties(OnapCommandConstants.CONF); + for (String prpFile: getCommaSeparatedList(OnapCommandConstants.OPEN_CLI_PLUGIN_PRPS)) { addProperties(prpFile); } @@ -57,7 +60,7 @@ public final class OnapCommandConfig { loadProperties(ps, fileName); for (Entry<Object, Object> entry: ps.entrySet()) { - prps.put(entry.getKey(), entry.getValue()); + prps.put(entry.getKey(), OnapCommandUtils.replaceLineForSpecialValues(entry.getValue().toString())); } } diff --git a/framework/src/main/java/org/onap/cli/fw/conf/OnapCommandConstants.java b/framework/src/main/java/org/onap/cli/fw/conf/OnapCommandConstants.java index 56183399..53018025 100644 --- a/framework/src/main/java/org/onap/cli/fw/conf/OnapCommandConstants.java +++ b/framework/src/main/java/org/onap/cli/fw/conf/OnapCommandConstants.java @@ -36,14 +36,13 @@ public class OnapCommandConstants { public static final String RESULT_PARAMS_LIST = "cli.schema.base.results.sections"; public static final String RESULT_PARAMS_MANDATORY_LIST = "cli.schema.base.results.sections.mandatory"; public static final String BOOLEAN_VALUE = "cli.schema.boolean_values"; - public static final String COMMAND_TYPE_VALUES = "cli.schema.command.type"; public static final String SCHEMA_TYPES_SUPPORTED = "cli.schema.profile.available"; public static final String OPEN_CLI_PRODUCT_NAME = "cli.product_name"; public static final String OPEN_CLI_PLUGIN_PRPS = "cli.schema.profile.confs"; public static final String OPEN_CLI_DATA_DIR = "cli.data.dir"; public static final String OPEN_CLI_ARTIFACT_DIR = "cli.artifact.dir"; - + public static final String OPEN_CLI_GRPC_CLIENT_TIMEOUT = "cli.grpc.client.timeout"; //schema public static final String OPEN_CLI_SCHEMA_VERSION = "open_cli_schema_version"; public static final String OPEN_CLI_SCHEMA_VERSION_VALUE_1_0 = "1.0"; @@ -151,6 +150,7 @@ public class OnapCommandConstants { public static final String SPL_ENTRY_UUID = "uuid"; public static final String SPL_ENTRY_ENV = "env:"; public static final String SPL_ENTRY_FILE = "file:"; + public static final String SPL_ENTRY_MD5 = "md5:"; public static final String VERSION_INFO = "version.info"; public static final String VERSION_INFO_PLACE_HOLDER_VERSION = "__VERSION__"; 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 618c321c..3b13c9f8 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 @@ -288,7 +288,12 @@ public class OnapCommandParameter { public void setValue(Object value) throws OnapCommandInvalidParameterValue { this.rawValue = value; - if (OnapCommandParameterType.URL.equals(parameterType) && !value.toString().isEmpty() && !value.toString().startsWith("http") + if (OnapCommandParameterType.URL.equals(parameterType) && + !value.toString().isEmpty() && + !value.toString().startsWith("http") && + !value.toString().startsWith("ftp") && + !value.toString().startsWith("mqtt") && + !value.toString().startsWith("tcp") && !value.toString().startsWith("/")) { value = "/" + value; } else if (OnapCommandParameterType.ARRAY.equals(parameterType)) { diff --git a/framework/src/main/java/org/onap/cli/fw/input/OnapCommandParameterType.java b/framework/src/main/java/org/onap/cli/fw/input/OnapCommandParameterType.java index 391383d4..b6860b3c 100644 --- a/framework/src/main/java/org/onap/cli/fw/input/OnapCommandParameterType.java +++ b/framework/src/main/java/org/onap/cli/fw/input/OnapCommandParameterType.java @@ -23,7 +23,7 @@ import org.onap.cli.fw.error.OnapCommandInvalidParameterType; * */ public enum OnapCommandParameterType { - STRING, JSON, YAML, TEXT, URL, UUID, + STRING, BYTE, JSON, YAML, TEXT, URL, UUID, DIGIT, BOOL, ARRAY, @@ -42,6 +42,8 @@ public enum OnapCommandParameterType { public static OnapCommandParameterType get(String name) throws OnapCommandInvalidParameterType { if (JSON.name().equalsIgnoreCase(name)) { return JSON; + } else if (BYTE.name().equalsIgnoreCase(name)) { + return BYTE; } else if (YAML.name().equalsIgnoreCase(name)) { return YAML; } else if (STRING.name().equalsIgnoreCase(name)) { diff --git a/framework/src/main/java/org/onap/cli/fw/registrar/OnapCommandRegistrar.java b/framework/src/main/java/org/onap/cli/fw/registrar/OnapCommandRegistrar.java index d7937527..fdacbd1e 100644 --- a/framework/src/main/java/org/onap/cli/fw/registrar/OnapCommandRegistrar.java +++ b/framework/src/main/java/org/onap/cli/fw/registrar/OnapCommandRegistrar.java @@ -431,8 +431,8 @@ public class OnapCommandRegistrar { } } - public List<Map<String, ?>> getTestSuite(String cmd) throws OnapCommandException { - return OnapCommandDiscoveryUtils.createTestSuite(cmd, enabledProductVersion); + public List<Map<String, ?>> getTestSuite(String cmd, String product) throws OnapCommandException { + return OnapCommandDiscoveryUtils.createTestSuite(cmd, product); } public String getHost() { 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 fe191f5b..7d2cec92 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 @@ -344,7 +344,7 @@ public class OnapCommandSchemaLoader { case IS_OPTIONAL: if (validate && !OnapCommandUtils.validateBoolean(String.valueOf(parameter.get(key2)))) { exceptionList.add(OnapCommandUtils.invalidBooleanValueMessage(parameter.get(NAME), - IS_SECURED, parameter.get(key2))); + IS_OPTIONAL, parameter.get(key2))); } if (BOOLEAN_TRUE.equalsIgnoreCase(String.valueOf(parameter.get(key2)))) { @@ -510,6 +510,7 @@ public class OnapCommandSchemaLoader { break; } } + return exceptionList; } diff --git a/framework/src/main/java/org/onap/cli/fw/store/OnapCommandExecutionStore.java b/framework/src/main/java/org/onap/cli/fw/store/OnapCommandExecutionStore.java index 4fc2508a..d09e4304 100644 --- a/framework/src/main/java/org/onap/cli/fw/store/OnapCommandExecutionStore.java +++ b/framework/src/main/java/org/onap/cli/fw/store/OnapCommandExecutionStore.java @@ -19,7 +19,6 @@ package org.onap.cli.fw.store; import java.io.File; import java.io.FilenameFilter; import java.io.IOException; -import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Arrays; @@ -29,7 +28,6 @@ import java.util.Map; import java.util.TimeZone; import org.apache.commons.io.FileUtils; -import org.apache.commons.io.IOUtils; import org.onap.cli.fw.conf.OnapCommandConfig; import org.onap.cli.fw.conf.OnapCommandConstants; import org.onap.cli.fw.error.OnapCommandExecutionFailed; @@ -54,7 +52,7 @@ public class OnapCommandExecutionStore { private static SearchMode SEARCH_MODE = SearchMode.file; public static class ExecutionStoreContext { - + private String requestId; private String executionId; private String storePath; public String getExecutionId() { @@ -71,6 +69,13 @@ public class OnapCommandExecutionStore { this.storePath = storePath; return this; } + public String getRequestId() { + return requestId; + } + public ExecutionStoreContext setRequestId(String requestId) { + this.requestId = requestId; + return this; + } } public static class Execution { @@ -185,63 +190,113 @@ public class OnapCommandExecutionStore { public ExecutionStoreContext storeExectutionStart( String requestId, String product, String service, String cmd, String profile, String input) { + ExecutionStoreContext context = new ExecutionStoreContext(); + context.setRequestId(requestId); + String executionId = requestId + "-" + System.currentTimeMillis(); + context.setExecutionId(executionId); String storePath = getBasePath() + File.separator + executionId + SEPARATOR + product + SEPARATOR + service + SEPARATOR + cmd + (profile != null ? (SEPARATOR + profile) : "" ); + try { File dir = new File(storePath); FileUtils.forceMkdir(dir); + context.setStorePath(dir.getAbsolutePath()); if (product != null) - FileUtils.writeStringToFile(new File(dir.getAbsolutePath() + File.separator + "product"), product); + FileUtils.writeStringToFile(new File(context.getStorePath() + File.separator + "product"), product); if (service != null) - FileUtils.writeStringToFile(new File(dir.getAbsolutePath() + File.separator + "service"), service); + FileUtils.writeStringToFile(new File(context.getStorePath() + File.separator + "service"), service); if (cmd != null) - FileUtils.writeStringToFile(new File(dir.getAbsolutePath() + File.separator + "command"), cmd); + FileUtils.writeStringToFile(new File(context.getStorePath() + File.separator + "command"), cmd); - FileUtils.writeStringToFile(new File(dir.getAbsolutePath() + File.separator + "requestId"), requestId); + FileUtils.writeStringToFile(new File(context.getStorePath() + File.separator + "requestId"), requestId); - FileUtils.writeStringToFile(new File(dir.getAbsolutePath() + File.separator + "executionId"), executionId); + FileUtils.writeStringToFile(new File(context.getStorePath() + File.separator + "executionId"), executionId); if (input != null) - FileUtils.writeStringToFile(new File(dir.getAbsolutePath() + File.separator + "input"), input); + FileUtils.writeStringToFile(new File(context.getStorePath() + File.separator + "input"), input); if (profile != null) - FileUtils.writeStringToFile(new File(dir.getAbsolutePath() + File.separator + "profile"), profile); - FileUtils.touch(new File(dir.getAbsolutePath() + File.separator + "in-progress")); + FileUtils.writeStringToFile(new File(context.getStorePath() + File.separator + "profile"), profile); + + FileUtils.touch(new File(context.getStorePath() + File.separator + "stdout")); + FileUtils.touch(new File(context.getStorePath() + File.separator + "stderr")); + FileUtils.touch(new File(context.getStorePath() + File.separator + "debug")); + + FileUtils.touch(new File(context.getStorePath() + File.separator + "in-progress")); } catch (IOException e) { log.error("Failed to store the execution start details " + storePath); } - return new ExecutionStoreContext().setExecutionId(executionId).setStorePath(storePath); + return context; } public void storeExectutionEnd( - ExecutionStoreContext execContext, - String output, String error, boolean passed) { + ExecutionStoreContext context, + String output, String error, String debug, boolean passed) { try { - File dir = new File(execContext.getStorePath()); - FileUtils.forceMkdir(dir); - if (output != null) - FileUtils.writeStringToFile(new File(dir.getAbsolutePath() + File.separator + "output"), output); + FileUtils.writeStringToFile(new File(context.getStorePath() + File.separator + "output"), output); if (error != null) - FileUtils.writeStringToFile(new File(dir.getAbsolutePath() + File.separator + "error"), error); - + FileUtils.writeStringToFile(new File(context.getStorePath() + File.separator + "error"), error); + if (debug != null) + FileUtils.writeStringToFile(new File(context.getStorePath() + File.separator + "debug"), debug); if (passed) - FileUtils.touch(new File(dir.getAbsolutePath() + File.separator + "completed")); + FileUtils.touch(new File(context.getStorePath() + File.separator + "completed")); else - FileUtils.touch(new File(dir.getAbsolutePath() + File.separator + "failed")); + FileUtils.touch(new File(context.getStorePath() + File.separator + "failed")); + + new File(context.getStorePath() + File.separator + "in-progress").delete(); + } catch (IOException e) { + log.error("Failed to store the execution end details " + context.storePath); + } + } + + public void storeExectutionProgress( + ExecutionStoreContext context, + String output, String error, String debug) { + + try { + if (output != null) + FileUtils.writeStringToFile(new File(context.getStorePath() + File.separator + "output"), output); + if (error != null) + FileUtils.writeStringToFile(new File(context.getStorePath() + File.separator + "error"), error); + if (debug != null) + FileUtils.writeStringToFile(new File(context.getStorePath() + File.separator + "debug"), debug); + } catch (IOException e) { + log.error("Failed to store the execution end details " + context.storePath); + } + } + + public void storeExectutionDebug( + ExecutionStoreContext context, + String debug) { - new File(dir.getAbsolutePath() + File.separator + "in-progress").delete(); + try { + if (debug != null) { + FileUtils.writeStringToFile(new File(context.getStorePath() + File.separator + "debug"), debug); + } } catch (IOException e) { - log.error("Failed to store the execution end details " + execContext.storePath); + log.error("Failed to store the execution debug details " + context.storePath); } } + public void storeExectutionOutput( + ExecutionStoreContext context, + String output) { + + try { + if (output != null) { + FileUtils.writeStringToFile(new File(context.getStorePath() + File.separator + "output"), output); + } + } catch (IOException e) { + log.error("Failed to store the execution output details " + context.storePath); + } + } public List<OnapCommandExecutionStore.Execution> listExecutions(Map<String, String> search) throws OnapCommandExecutionFailed { List <OnapCommandExecutionStore.Execution> list = new ArrayList<>(); @@ -342,7 +397,8 @@ public class OnapCommandExecutionStore { return exectuion; } - public Execution getExecution(String executionId) throws OnapCommandExecutionNotFound, OnapCommandExecutionFailed { + + private File getExecutionDir(String executionId) throws OnapCommandExecutionNotFound { File []f = new File(getBasePath()).listFiles(new FilenameFilter() { @Override @@ -356,10 +412,37 @@ public class OnapCommandExecutionStore { throw new OnapCommandExecutionNotFound(executionId); } + return f[0]; + } + + public String showExecutionOut(String executionId) throws OnapCommandExecutionNotFound { + try { + return FileUtils.readFileToString(new File (this.getExecutionDir(executionId).getAbsolutePath() + File.separator + "stdout")); + } catch (IOException e) { + return ""; + } + } + + public String showExecutionErr(String executionId) throws OnapCommandExecutionNotFound { + try { + return FileUtils.readFileToString(new File (this.getExecutionDir(executionId).getAbsolutePath() + File.separator + "stderr")); + } catch (IOException e) { + return ""; + } + } + + public String showExecutionDebug(String executionId) throws OnapCommandExecutionNotFound { + try { + return FileUtils.readFileToString(new File (this.getExecutionDir(executionId).getAbsolutePath() + File.separator + "debug")); + } catch (IOException e) { + return ""; + } + } + public Execution getExecution(String executionId) throws OnapCommandExecutionNotFound, OnapCommandExecutionFailed { try { - return this.makeExecution(f[0].getAbsolutePath()); + return this.makeExecution(this.getExecutionDir(executionId).getAbsolutePath()); } catch (IOException e) { throw new OnapCommandExecutionFailed(e, "Failed to retrieve the execution"); } } -} +}
\ No newline at end of file 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 diff --git a/framework/src/main/resources/META-INF/services/org.onap.cli.fw.cmd.OnapCommand b/framework/src/main/resources/META-INF/services/org.onap.cli.fw.cmd.OnapCommand index 547d7872..7ca9c783 100644 --- a/framework/src/main/resources/META-INF/services/org.onap.cli.fw.cmd.OnapCommand +++ b/framework/src/main/resources/META-INF/services/org.onap.cli.fw.cmd.OnapCommand @@ -16,8 +16,13 @@ org.onap.cli.fw.cmd.schema.OnapSchemaValidateCommand org.onap.cli.fw.cmd.schema.OnapSchemaRefreshCommand org.onap.cli.fw.cmd.schema.OnapSchemaListCommand org.onap.cli.fw.cmd.schema.OnapSchemaShowCommand -#org.onap.cli.fw.cmd.schema.OnapSchemaSwitchCommand +org.onap.cli.fw.cmd.schema.OnapSchemaSwitchCommand + org.onap.cli.fw.cmd.product.OnapProductsListCommand org.onap.cli.fw.cmd.product.OnapServiceListCommand + org.onap.cli.fw.cmd.execution.OnapCommandExceutionListCommand org.onap.cli.fw.cmd.execution.OnapCommandExceutionShowCommand +org.onap.cli.fw.cmd.execution.OnapCommandExceutionShowDebugCommand +org.onap.cli.fw.cmd.execution.OnapCommandExceutionShowOutCommand +org.onap.cli.fw.cmd.execution.OnapCommandExceutionShowErrCommand diff --git a/framework/src/main/resources/open-cli-schema/default_input_parameters.yaml b/framework/src/main/resources/open-cli-schema/default_input_parameters.yaml index d1a074d7..f3d5e89d 100644 --- a/framework/src/main/resources/open-cli-schema/default_input_parameters.yaml +++ b/framework/src/main/resources/open-cli-schema/default_input_parameters.yaml @@ -72,4 +72,4 @@ parameters: short_option: D long_option: context is_default_param: true - is_optional: true
\ No newline at end of file + is_optional: true diff --git a/framework/src/main/resources/open-cli-schema/execution/execution-show-debug.yaml b/framework/src/main/resources/open-cli-schema/execution/execution-show-debug.yaml new file mode 100644 index 00000000..6e968747 --- /dev/null +++ b/framework/src/main/resources/open-cli-schema/execution/execution-show-debug.yaml @@ -0,0 +1,38 @@ +# Copyright 2018 Huawei Technologies Co., Ltd. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +open_cli_schema_version: 1.0 +name: execution-show-debug +description: Show the command execution debug information + +info: + product: open-cli + service: execution + type: cmd + author: Kanagaraj Manickam kanagaraj.manickam@huawei.com + +parameters: + - name: execution-id + type: string + description: Execution id + short_option: l + long_option: execution-id + is_optional: false + - name: format + type: string + description: Output formats, supported formats such as table, csv, json, yaml + short_option: f + long_option: format + default_value: text + is_default_param: true
\ No newline at end of file diff --git a/framework/src/main/resources/open-cli-schema/execution/execution-show-err.yaml b/framework/src/main/resources/open-cli-schema/execution/execution-show-err.yaml new file mode 100644 index 00000000..13ad274e --- /dev/null +++ b/framework/src/main/resources/open-cli-schema/execution/execution-show-err.yaml @@ -0,0 +1,38 @@ +# Copyright 2018 Huawei Technologies Co., Ltd. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +open_cli_schema_version: 1.0 +name: execution-show-err +description: Show the command execution console error information + +info: + product: open-cli + service: execution + type: cmd + author: Kanagaraj Manickam kanagaraj.manickam@huawei.com + +parameters: + - name: execution-id + type: string + description: Execution id + short_option: l + long_option: execution-id + is_optional: false + - name: format + type: string + description: Output formats, supported formats such as table, csv, json, yaml + short_option: f + long_option: format + default_value: text + is_default_param: true
\ No newline at end of file diff --git a/framework/src/main/resources/open-cli-schema/execution/execution-show-out.yaml b/framework/src/main/resources/open-cli-schema/execution/execution-show-out.yaml new file mode 100644 index 00000000..5b74f7aa --- /dev/null +++ b/framework/src/main/resources/open-cli-schema/execution/execution-show-out.yaml @@ -0,0 +1,38 @@ +# Copyright 2018 Huawei Technologies Co., Ltd. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +open_cli_schema_version: 1.0 +name: execution-show-out +description: Show the command execution console output information + +info: + product: open-cli + service: execution + type: cmd + author: Kanagaraj Manickam kanagaraj.manickam@huawei.com + +parameters: + - name: execution-id + type: string + description: Execution id + short_option: l + long_option: execution-id + is_optional: false + - name: format + type: string + description: Output formats, supported formats such as table, csv, json, yaml + short_option: f + long_option: format + default_value: text + is_default_param: true
\ No newline at end of file diff --git a/grpc/grpc-client/src/main/java/org/open/infc/grpc/client/OpenInterfaceGrpcClient.java b/grpc/grpc-client/src/main/java/org/open/infc/grpc/client/OpenInterfaceGrpcClient.java index 9a818d7a..e32110de 100644 --- a/grpc/grpc-client/src/main/java/org/open/infc/grpc/client/OpenInterfaceGrpcClient.java +++ b/grpc/grpc-client/src/main/java/org/open/infc/grpc/client/OpenInterfaceGrpcClient.java @@ -37,6 +37,9 @@ public class OpenInterfaceGrpcClient { private final ManagedChannel channel; private final OpenInterfaceGrpc.OpenInterfaceBlockingStub blockingStub; + //10 seconds + private int timeout = 60000; + public static class OpenInterfaceGrpcExecption extends Exception { private static final long serialVersionUID = -8755636432217894246L; @@ -64,6 +67,11 @@ public class OpenInterfaceGrpcClient { .build()); } + public OpenInterfaceGrpcClient(String host, int port, int timeout) { + this(host, port); + this.timeout = timeout; + + } OpenInterfaceGrpcClient(ManagedChannel channel) { this.channel = channel; blockingStub = OpenInterfaceGrpc.newBlockingStub(channel); @@ -78,7 +86,7 @@ public class OpenInterfaceGrpcClient { Output result = Output.newBuilder().build(); try { - result = blockingStub.withDeadlineAfter(10, TimeUnit.SECONDS).invoke(input); + result = blockingStub.withDeadlineAfter(timeout, TimeUnit.MILLISECONDS).invoke(input); } catch (StatusRuntimeException e) { logger.warn("RPC failed: {0}", e.getStatus()); //Status{code=DEADLINE_EXCEEDED} @@ -93,7 +101,7 @@ public class OpenInterfaceGrpcClient { Result result = Result.newBuilder().setExitCode(1).build(); try { - result = blockingStub.withDeadlineAfter(10, TimeUnit.SECONDS).remoteCli(args); + result = blockingStub.withDeadlineAfter(timeout, TimeUnit.MILLISECONDS).remoteCli(args); } catch (StatusRuntimeException e) { logger.warn("RPC failed: {0}", e.getStatus()); //Status{code=DEADLINE_EXCEEDED} diff --git a/grpc/grpc-client/src/main/java/org/open/infc/grpc/client/OpenRemoteCli.java b/grpc/grpc-client/src/main/java/org/open/infc/grpc/client/OpenRemoteCli.java index fc0c2aac..724267e8 100644 --- a/grpc/grpc-client/src/main/java/org/open/infc/grpc/client/OpenRemoteCli.java +++ b/grpc/grpc-client/src/main/java/org/open/infc/grpc/client/OpenRemoteCli.java @@ -26,6 +26,51 @@ import org.open.infc.grpc.Output; import org.open.infc.grpc.Result; public class OpenRemoteCli { + + private String host; + private int port; + private int timeout; + private String requestId; + + public OpenRemoteCli(String host, int port, int timeout, String requestId) { + this.host = host; + this.port = port; + this.timeout = timeout; + this.requestId = requestId; + + } + + public Result run (List <String> args) throws Exception { + OpenInterfaceGrpcClient client = new OpenInterfaceGrpcClient( + host, port, timeout); + try { + Result result = client.remoteCli(Args.newBuilder().setRequestId(this.requestId).addAllArgs(args).build()); + return result; + } finally { + client.shutdown(); + } + } + + public Output invoke (String product, String profile, String action, Map <String, String> params) throws Exception { + OpenInterfaceGrpcClient client = new OpenInterfaceGrpcClient( + host, port, timeout); + try { + + Map <String, String> options = new HashMap<>(); + options.put("product", product); + if (profile != null && !profile.isEmpty()) + options.put("profile", profile); + params.put("format", "json"); + Input input = Input.newBuilder().setAction(action).setRequestId(requestId).putAllOptions(options).putAllParams(params).build(); + + Output output = client.invoke(input); + return output; + } finally { + client.shutdown(); + } + } + + //Absolute the static methods /** * Runs CLI remotely * @param host @@ -48,7 +93,7 @@ public class OpenRemoteCli { } } - /** + /** * Runs commands as remote procedure call :) * @param host * @param port diff --git a/grpc/grpc-server/src/main/java/org/open/infc/grpc/server/OpenInterfaceGrpcServer.java b/grpc/grpc-server/src/main/java/org/open/infc/grpc/server/OpenInterfaceGrpcServer.java index 79d761d6..f34df89f 100644 --- a/grpc/grpc-server/src/main/java/org/open/infc/grpc/server/OpenInterfaceGrpcServer.java +++ b/grpc/grpc-server/src/main/java/org/open/infc/grpc/server/OpenInterfaceGrpcServer.java @@ -29,11 +29,13 @@ import org.onap.cli.fw.conf.OnapCommandConfig; import org.onap.cli.fw.conf.OnapCommandConstants; import org.onap.cli.fw.error.OnapCommandException; import org.onap.cli.fw.input.OnapCommandParameter; +import org.onap.cli.fw.input.OnapCommandParameterType; import org.onap.cli.fw.output.OnapCommandResultType; import org.onap.cli.fw.registrar.OnapCommandRegistrar; import org.onap.cli.fw.store.OnapCommandExecutionStore; import org.onap.cli.fw.store.OnapCommandExecutionStore.ExecutionStoreContext; import org.onap.cli.main.OnapCli; +import org.onap.cli.main.utils.OnapCliArgsParser; import org.open.infc.grpc.Args; import org.open.infc.grpc.Input; import org.open.infc.grpc.OpenInterfaceGrpc; @@ -181,6 +183,24 @@ public class OpenInterfaceGrpcServer { if (params.contains(arg.getKey())) cmd.getParametersMap().get(arg.getKey()).setValue(arg.getValue()); } + + //fill values from the file, if needed + for (OnapCommandParameter param: cmd.getParameters()) { + if (param.getParameterType().equals(OnapCommandParameterType.JSON)) { + param.setValue(OnapCliArgsParser.readJsonStringFromUrl(param.getValue().toString(), param.getName())); + + } else if (param.getParameterType().equals(OnapCommandParameterType.TEXT)) { + param.setValue(OnapCliArgsParser.readTextStringFromUrl(param.getValue().toString(), param.getName())); + + } else if (param.getParameterType().equals(OnapCommandParameterType.BYTE)) { + param.setValue(OnapCliArgsParser.readBytesFromUrl(param.getValue().toString(), param.getName())); + + } else if (param.getParameterType().equals(OnapCommandParameterType.YAML)) { + param.setValue(OnapCliArgsParser.readYamlStringFromUrl(param.getValue().toString(), param.getName())); + + } + } + } else { cmd.getParametersMap().get(OnapCommandConstants.INFO_PRODUCT).setValue(product); @@ -194,7 +214,9 @@ public class OpenInterfaceGrpcServer { if (!cmd.isRpc()) { //Start the execution - if (req.getRequestId() != null) { + if (req.getRequestId() != null && !req.getRequestId().isEmpty()) { + if (!(cmd.getInfo().getProduct().equalsIgnoreCase("open-cli") && + cmd.getName().equalsIgnoreCase("execution-list"))) { String input = cmd.getArgsJson(true); executionStoreContext = OnapCommandExecutionStore.getStore().storeExectutionStart( req.getRequestId(), @@ -203,12 +225,16 @@ public class OpenInterfaceGrpcServer { cmd.getName(), profile, input); + } } } - + cmd.setExecutionContext(executionStoreContext); cmd.execute(); if (!cmd.isRpc()) { + //Track and/or persist the execution context + new OnapCli().handleTracking(cmd); + String printOut = cmd.getResult().print(); Builder reply = Output.newBuilder(); reply.putAttrs(OnapCommandConstants.ERROR, "{}"); @@ -226,15 +252,6 @@ public class OpenInterfaceGrpcServer { } output = reply.build(); - - if (req.getRequestId() != null) { - //complete the execution recording - OnapCommandExecutionStore.getStore().storeExectutionEnd( - executionStoreContext, - printOut, - null, - cmd.getResult().isPassed()); - } logger.info(output.toString()); } else { //Rpc command will set the output. @@ -254,6 +271,7 @@ public class OpenInterfaceGrpcServer { executionStoreContext, null, e.getMessage(), + cmd.getResult().getDebugInfo(), false); reply.putAddons("execution-id", executionStoreContext.getExecutionId()); } diff --git a/main/src/main/java/org/onap/cli/main/OnapCli.java b/main/src/main/java/org/onap/cli/main/OnapCli.java index 582be3c8..75bf5d18 100644 --- a/main/src/main/java/org/onap/cli/main/OnapCli.java +++ b/main/src/main/java/org/onap/cli/main/OnapCli.java @@ -43,6 +43,7 @@ import org.onap.cli.fw.registrar.OnapCommandRegistrar; import org.onap.cli.fw.store.OnapCommandExecutionStore; import org.onap.cli.fw.store.OnapCommandExecutionStore.ExecutionStoreContext; import org.onap.cli.fw.utils.OnapCommandDiscoveryUtils; +import org.onap.cli.fw.utils.OnapCommandUtils; import org.onap.cli.main.conf.OnapCliConstants; import org.onap.cli.main.interactive.StringCompleter; import org.onap.cli.main.utils.OnapCliArgsParser; @@ -162,8 +163,9 @@ public class OnapCli { } private void print(Throwable throwable) { - this.print(throwable.getMessage() != null ? throwable.getMessage() : ""); - log.error(throwable.getMessage(), throwable); + String error = throwable.getMessage() != null ? throwable.getMessage() : ""; + this.print(error); + log.error(error, throwable); } private String getShortOption(String opt) { @@ -282,7 +284,9 @@ public class OnapCli { .filter(e -> e.getName().equals(OnapCommandConstants.VERIFY_CONTEXT_PARAM)) .findFirst(); - List<Map<String, ?>> testSuite = OnapCommandRegistrar.getRegistrar().getTestSuite(cmd.getName()); + List<Map<String, ?>> testSuite = OnapCommandRegistrar.getRegistrar().getTestSuite( + cmd.getName(), + cmd.getInfo().getProduct()); OnapCommandResult testSuiteResult = new OnapCommandResult(); testSuiteResult.setType(OnapCommandResultType.TABLE); @@ -306,11 +310,10 @@ public class OnapCli { sampleFileAtt.getValues().add((String) sampleTest.get(OnapCommandConstants.VERIFY_SAMPLE_FILE_ID)); sampleIdAtt.getValues().add((String) sampleTest.get(OnapCommandConstants.VERIFY_SAMPLE_ID)); - cmd = OnapCommandRegistrar.getRegistrar().get(this.cmdName); + cmd = OnapCommandRegistrar.getRegistrar().get(cmd.getName(), + cmd.getInfo().getProduct()); List<String> arguments = (List<String>) sampleTest.get(OnapCommandConstants.VERIFY_INPUT); - if (arguments.size() > 0 && arguments.get(0).equals(this.cmdName)) { - arguments.remove(0); - } + OnapCliArgsParser.populateParams(cmd.getParameters(), arguments); this.print("\n***************Test Command: \n" + sampleTest.get(OnapCommandConstants.VERIFY_INPUT).toString()); @@ -530,38 +533,31 @@ public class OnapCli { OnapCliArgsParser.populateParams(cmd.getParameters(), this.args); //start the execution - if (this.requestId != null) { - String input = cmd.getArgsJson(true); - executionStoreContext = OnapCommandExecutionStore.getStore().storeExectutionStart( - this.requestId, - cmd.getInfo().getProduct(), - cmd.getInfo().getService(), - this.cmdName, - this.profile, - input); + if (this.requestId != null && !this.requestId.isEmpty()) { + if (!(this.product.equalsIgnoreCase("open-cli") && + this.cmdName.equalsIgnoreCase("execution-list"))) { + String input = cmd.getArgsJson(true); + executionStoreContext = OnapCommandExecutionStore.getStore().storeExectutionStart( + this.requestId, + cmd.getInfo().getProduct(), + cmd.getInfo().getService(), + this.cmdName, + this.profile, + input); + } } + cmd.setExecutionContext(executionStoreContext); OnapCommandResult result = cmd.execute(); - String printOut = result.print(); - if (this.requestId != null) { - OnapCommandExecutionStore.getStore().storeExectutionEnd( - executionStoreContext, - printOut, - null, result.isPassed()); - } - - this.print(result.getDebugInfo()); - this.print(printOut); + this.handleTracking(cmd); if (result.isPassed()) { this.exitSuccessfully(); generateSmapleYaml(cmd); + } else { + this.exitFailure(); } - - else this.exitFailure(); - - } catch (OnapCommandWarning w) { this.print(w); this.print(cmd.getResult().getDebugInfo()); @@ -572,6 +568,7 @@ public class OnapCli { executionStoreContext, null, e.getMessage(), + cmd.getResult().getDebugInfo(), false); } @@ -582,21 +579,35 @@ public class OnapCli { } } + public void handleTracking(OnapCommand cmd) throws OnapCommandException { + if (cmd.getResult().isDebug()) + this.print(cmd.getResult().getDebugInfo()); + + String printOut = cmd.getResult().print(); + this.print(printOut); + + if (cmd.getExecutionContext() != null) { + OnapCommandExecutionStore.getStore().storeExectutionEnd( + cmd.getExecutionContext(), + printOut, + null, + cmd.getResult().getDebugInfo(), + cmd.getResult().isPassed()); + } + } /** * When user invokes cli with RPC arguments... */ public void handleRpc() { - if (!this.args.isEmpty()) { + if (this.rpcHost != null && this.rpcPort != null && this.product != null) { try { - if (this.rpcHost != null && this.rpcPort != null && this.product != null) { - OnapCommand cmd = OnapCommandRegistrar.getRegistrar().get("schema-rpc", "open-cli"); - cmd.getParametersMap().get(OnapCommandConstants.RPC_HOST).setValue(this.rpcHost); - cmd.getParametersMap().get(OnapCommandConstants.RPC_PORT).setValue(this.rpcPort); - cmd.getParametersMap().get(OnapCommandConstants.RPC_PRODUCT).setValue(this.product); - cmd.getParametersMap().get(OnapCommandConstants.RPC_CMD).setValue(this.cmdName); + OnapCommand cmd = OnapCommandRegistrar.getRegistrar().get("schema-rpc", "open-cli"); + cmd.getParametersMap().get(OnapCommandConstants.RPC_HOST).setValue(this.rpcHost); + cmd.getParametersMap().get(OnapCommandConstants.RPC_PORT).setValue(this.rpcPort); + cmd.getParametersMap().get(OnapCommandConstants.RPC_PRODUCT).setValue(this.product); + cmd.getParametersMap().get(OnapCommandConstants.RPC_CMD).setValue(this.cmdName); - this.handleRpcCommand(cmd); - } + this.handleRpcCommand(cmd); } catch (Exception e) { this.print(e); this.exitFailure(); @@ -730,8 +741,8 @@ public class OnapCli { private void generateSmapleYaml(OnapCommand cmd) throws OnapCommandException { if (Boolean.parseBoolean(OnapCommandConfig.getPropertyValue(OnapCommandConstants.SAMPLE_GEN_ENABLED)) && this.getExitCode() == OnapCliConstants.EXIT_SUCCESS) { try { - SampleYamlGenerator.generateSampleYaml(args, cmd.getResult().print(), - OnapCommandRegistrar.getRegistrar().getEnabledProductVersion(), + SampleYamlGenerator.generateSampleYaml(cmd.getName(), args, cmd.getResult().print(), + cmd.getInfo().getProduct(), OnapCommandConfig.getPropertyValue(OnapCommandConstants.SAMPLE_GEN_TARGET_FOLDER) + "/" + cmd.getSchemaName().replaceAll(".yaml", "") + "-sample.yaml", cmd.getResult().isDebug()); } catch (IOException error) { diff --git a/main/src/main/java/org/onap/cli/main/utils/OnapCliArgsParser.java b/main/src/main/java/org/onap/cli/main/utils/OnapCliArgsParser.java index a1bde717..a07c08f2 100644 --- a/main/src/main/java/org/onap/cli/main/utils/OnapCliArgsParser.java +++ b/main/src/main/java/org/onap/cli/main/utils/OnapCliArgsParser.java @@ -25,6 +25,7 @@ import java.util.List; import java.util.Map; import java.util.Set; +import org.apache.commons.codec.binary.Base64; import org.apache.commons.io.FileUtils; import org.onap.cli.fw.error.OnapCommandException; import org.onap.cli.fw.error.OnapCommandInvalidParameterValue; @@ -32,6 +33,7 @@ import org.onap.cli.fw.input.OnapCommandParameter; import org.onap.cli.fw.input.OnapCommandParameterType; import org.onap.cli.main.error.OnapCliArgumentValueMissing; import org.onap.cli.main.error.OnapCliInvalidArgument; +import org.yaml.snakeyaml.Yaml; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; @@ -116,12 +118,25 @@ public class OnapCliArgsParser { i++; continue; - } if (paramMap.get(paramName).getParameterType().equals(OnapCommandParameterType.TEXT)) { + } else if (paramMap.get(paramName).getParameterType().equals(OnapCommandParameterType.TEXT)) { paramMap.get(paramName).setValue(readTextStringFromUrl(args.get(i + 1), paramMap.get(paramName).getName())); i++; continue; + } else if (paramMap.get(paramName).getParameterType().equals(OnapCommandParameterType.YAML)) { + String value = readYamlStringFromUrl(args.get(i + 1), + paramMap.get(paramName).getName()); + paramMap.get(paramName).setValue(value); + i++; + continue; + + } else if (paramMap.get(paramName).getParameterType().equals(OnapCommandParameterType.BYTE)) { + paramMap.get(paramName).setValue(readBytesFromUrl(args.get(i + 1), + paramMap.get(paramName).getName())); + i++; + continue; + } else if (paramMap.get(paramName).getParameterType() .equals(OnapCommandParameterType.ARRAY)) { Object value = paramMap.get(paramName).getValue(); @@ -177,7 +192,7 @@ public class OnapCliArgsParser { params.addAll(paramMap.values()); } - private static String readJsonStringFromUrl(String input, String argName) throws OnapCliInvalidArgument { + public static String readJsonStringFromUrl(String input, String argName) throws OnapCliInvalidArgument { ObjectMapper mapper = new ObjectMapper(); try { File file = new File(input); @@ -194,7 +209,7 @@ public class OnapCliArgsParser { } } - private static String readTextStringFromUrl(String input, String argName) throws OnapCliInvalidArgument { + public static String readTextStringFromUrl(String input, String argName) throws OnapCliInvalidArgument { try { File file = new File(input); if (file.isFile()) { @@ -208,7 +223,38 @@ public class OnapCliArgsParser { } } - private static List<String> convertJsonToListString(String arg, String json) throws OnapCliInvalidArgument { + public static String readYamlStringFromUrl(String input, String argName) throws OnapCliInvalidArgument { + try { + File file = new File(input); + if (file.isFile()) { + String value = FileUtils.readFileToString(file); + new Yaml().load(value); + return value; + } else { + return input; + } + + } catch (IOException e) { + throw new OnapCliInvalidArgument(argName, e); + } + } + + public static String readBytesFromUrl(String input, String argName) throws OnapCliInvalidArgument { + try { + File file = new File(input); + if (file.isFile()) { + byte[] encodeBase64 = Base64.encodeBase64(FileUtils.readFileToByteArray(file)); + return new String(encodeBase64); + } else { + byte[] encodeBase64 = Base64.encodeBase64(input.getBytes()); + return new String(encodeBase64); + } + } catch (IOException e) { + throw new OnapCliInvalidArgument(argName, e); + } + } + + public static List<String> convertJsonToListString(String arg, String json) throws OnapCliInvalidArgument { TypeReference<List<String>> mapType = new TypeReference<List<String>>() { }; try { @@ -218,7 +264,7 @@ public class OnapCliArgsParser { } } - private static Map<String, String> convertJsonToMapString(String arg, String json) throws OnapCliInvalidArgument { + public static Map<String, String> convertJsonToMapString(String arg, String json) throws OnapCliInvalidArgument { TypeReference<Map<String, String>> mapType = new TypeReference<Map<String, String>>() { }; try { diff --git a/products/sample/src/main/java/org/onap/cli/sample/OnapHelloWorldCommand.java b/products/sample/src/main/java/org/onap/cli/sample/OnapHelloWorldCommand.java index 39311998..f3df340b 100644 --- a/products/sample/src/main/java/org/onap/cli/sample/OnapHelloWorldCommand.java +++ b/products/sample/src/main/java/org/onap/cli/sample/OnapHelloWorldCommand.java @@ -26,7 +26,7 @@ import org.onap.cli.fw.schema.OnapCommandSchema; /** * Hello world. */ -@OnapCommandSchema(schema = "hello-world.yaml") +@OnapCommandSchema(schema = "hello-world-basic.yaml") public class OnapHelloWorldCommand extends OnapCommand { @Override diff --git a/profiles/command/src/main/java/org/onap/cli/fw/cmd/cmd/OpenCommandShellCmd.java b/profiles/command/src/main/java/org/onap/cli/fw/cmd/cmd/OpenCommandShellCmd.java index 8e94db85..884cde12 100644 --- a/profiles/command/src/main/java/org/onap/cli/fw/cmd/cmd/OpenCommandShellCmd.java +++ b/profiles/command/src/main/java/org/onap/cli/fw/cmd/cmd/OpenCommandShellCmd.java @@ -16,9 +16,13 @@ package org.onap.cli.fw.cmd.cmd; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; +import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; @@ -27,19 +31,26 @@ import org.onap.cli.fw.cmd.OnapCommand; import org.onap.cli.fw.cmd.conf.OnapCommandCmdConstants; import org.onap.cli.fw.cmd.error.OnapCommandCmdFailure; import org.onap.cli.fw.cmd.schema.OnapCommandSchemaCmdLoader; +import org.onap.cli.fw.conf.OnapCommandConfig; import org.onap.cli.fw.error.OnapCommandException; import org.onap.cli.fw.error.OnapCommandExecutionFailed; import org.onap.cli.fw.error.OnapCommandResultEmpty; import org.onap.cli.fw.error.OnapCommandResultMapProcessingFailed; import org.onap.cli.fw.input.OnapCommandParameter; +import org.onap.cli.fw.output.OnapCommandResultAttribute; +import org.onap.cli.fw.output.OnapCommandResultType; +import org.onap.cli.fw.registrar.OnapCommandRegistrar; import org.onap.cli.fw.schema.OnapCommandSchema; +import org.onap.cli.fw.store.OnapCommandExecutionStore; import org.onap.cli.fw.utils.OnapCommandUtils; import org.onap.cli.fw.utils.ProcessRunner; +import com.google.gson.Gson; import com.jayway.jsonpath.JsonPath; import com.jayway.jsonpath.PathNotFoundException; import net.minidev.json.JSONArray; +import net.minidev.json.JSONObject; /** * Hello world. @@ -108,6 +119,25 @@ public class OpenCommandShellCmd extends OnapCommand { this.resultMap = resultMap; } + private String getStdoutPath() { + String storePath = this.getExecutionContext().getStorePath(); + storePath = storePath + File.separator + "stdout"; + return storePath; + } + + private String getStderrPath() { + String storePath = this.getExecutionContext().getStorePath(); + storePath = storePath + File.separator + "stderr"; + return storePath; + } + + private String getOutputAttributeFilePath(String attrName, boolean temp) { + String storePath = (!temp) ? this.getExecutionContext().getStorePath() : OnapCommandConfig.getPropertyValue("cli.tmp.dir"); + String randomId = (this.getExecutionContext() != null) ? this.getExecutionContext().getExecutionId() : ("" + System.currentTimeMillis()); + storePath = storePath + File.separator + randomId + "_" + attrName; + return storePath; + } + @Override protected List<String> initializeProfileSchema(Map<String, ?> schemaMap, boolean validate) throws OnapCommandException { return OnapCommandSchemaCmdLoader.parseCmdSchema(this, schemaMap, validate); @@ -118,16 +148,40 @@ public class OpenCommandShellCmd extends OnapCommand { //Read the input arguments Map<String, OnapCommandParameter> paramMap = this.getParametersMap(); + //process command section + Map<String, String> tmpFiles = new HashMap<>(); List<String> commandLine = new ArrayList<>(); for (String cmdTkn: this.getCommand()) { - commandLine.add(OnapCommandUtils.replaceLineFromInputParameters(cmdTkn, paramMap)); + tmpFiles.putAll(this.formTmpFiles(cmdTkn)); + String commandLine1 = OnapCommandUtils.replaceLineForSpecialValues( + cmdTkn, tmpFiles); + commandLine1 = OnapCommandUtils.replaceLineFromInputParameters(commandLine1, paramMap); + + commandLine.add(commandLine1); } + long timeout = Long.parseLong(this.getParametersMap().get(OnapCommandCmdConstants.TIMEOUT).getValue().toString()); + //Process command String []cmd = commandLine.toArray(new String []{}); String cwd = this.getWd(); List <String> envs = new ArrayList<>(); + //add current process environments to sub process + for (Map.Entry<String, String> env: System.getenv().entrySet()) { + envs.add(env.getKey() + "=" + env.getValue()); + } + + //add oclip specific environment variables + if (this.getExecutionContext() != null) { + envs.add("OPEN_CLI_REQUEST_ID=" + this.getExecutionContext().getRequestId()); + if (OnapCommandRegistrar.getRegistrar().getHost() != null) { + envs.add("OPEN_CLI_RPC_HOST=" + OnapCommandRegistrar.getRegistrar().getHost()); + envs.add("OPEN_CLI_RPC_PORT=" + OnapCommandRegistrar.getRegistrar().getPort()); + } + //mrkanag set the profile OPEN_CLI_PROFILE + } + for (String env: this.getEnvs().keySet()) { envs.add(env + "=" + this.getEnvs().get(env)); } @@ -136,38 +190,102 @@ public class OpenCommandShellCmd extends OnapCommand { cmd, (envs.size() > 0) ? envs.toArray(new String []{}) : null, cwd); + FileOutputStream stdoutStream = null; + FileOutputStream stderrStream = null; + String outputValue = ""; + try { + pr.setTimeout(timeout); + + if (this.getExecutionContext() != null) { + + stdoutStream = new FileOutputStream(this.getStdoutPath()); + stderrStream = new FileOutputStream(this.getStderrPath()); + + pr.setStdout(stdoutStream); + pr.setStderr(stderrStream); + + OnapCommandExecutionStore.getStore().storeExectutionDebug(this.getExecutionContext(), pr.toString()); + } else { + this.getResult().setDebugInfo(pr.toString()); + } + pr.run(); } catch (Exception e) { throw new OnapCommandExecutionFailed(this.getName(), e); + } finally { + if (stdoutStream != null) { + try { + stdoutStream.close(); + } catch (IOException e) { + //never occurs // NOSONAR + } + } + if (stderrStream != null) { + try { + stderrStream.close(); + } catch (IOException e) { + //never occurs // NOSONAR + } + } } if (!this.successStatusCodes.contains(pr.getExitCode())) { throw new OnapCommandExecutionFailed(this.getName(), pr.getError(), pr.getExitCode()); } - String outputValue = ""; - if (this.output.equals("$stdout")) { - outputValue = pr.getOutput(); + if (pr.getStdout() != null) { + try (FileInputStream is = new FileInputStream(this.getStdoutPath())){ + outputValue = pr.streamToString(is); + } catch (IOException e) { + //never occurs // NOSONAR + } + } else + outputValue = pr.getOutput(); + + } else if (this.output.equals("$stderr")) { + if (pr.getStderr() != null) { + try (FileInputStream is = new FileInputStream(this.getStderrPath())) { + outputValue = pr.streamToString(is); + } catch (IOException e) { + //never occurs // NOSONAR + } + } else + outputValue = pr.getError(); + } else { - outputValue = OnapCommandUtils.replaceLineFromInputParameters(this.output, paramMap); - outputValue = OnapCommandUtils.replaceLineForSpecialValues(outputValue); + //remove ${tmp: and closing } + String tmpName = this.output.substring(7, this.output.length()-1); + String tmpFile = tmpFiles.get("tmp:" + tmpName); + if (tmpFile != null) { + try (FileInputStream is = new FileInputStream(tmpFile)) { + outputValue = pr.streamToString(is); + } catch (IOException e) { + //never occurs // NOSONAR + } + } } + this.getResult().setDebugInfo(pr.toString() + "\n" + outputValue); this.getResult().setOutput(outputValue); //populate results - for (Entry<String, String> resultMapEntry : this.getResultMap().entrySet()) { - String value = OnapCommandUtils.replaceLineFromInputParameters(resultMapEntry.getValue(), paramMap); - value = OnapCommandUtils.replaceLineForSpecialValues(value); - this.getResult().getRecordsMap().get(resultMapEntry.getKey()).setValues( - this.replaceLineFromOutputResults(value, outputValue)); - } + if (!this.getResult().getType().name().equalsIgnoreCase(OnapCommandResultType.TEXT.name())) + for (Entry<String, String> resultMapEntry : this.getResultMap().entrySet()) { + String attrName = resultMapEntry.getKey(); + OnapCommandResultAttribute attr = this.getResult().getRecordsMap().get(attrName); + + String value = OnapCommandUtils.replaceLineFromInputParameters(resultMapEntry.getValue(), paramMap); + value = OnapCommandUtils.replaceLineForSpecialValues(value); + attr.setValues(this.replaceLineFromOutputResults(value, outputValue)); + } //check for pass/failure - if (!this.passCodes.contains(pr.getExitCode())) { + if (!this.passCodes.isEmpty() && !this.passCodes.contains(pr.getExitCode())) { this.getResult().setPassed(false); + } else { + this.getResult().setPassed(true); } } @@ -179,6 +297,30 @@ public class OpenCommandShellCmd extends OnapCommand { this.output = output; } + private Map<String, String> formTmpFiles(String line){ + + Map<String, String> result = new HashMap<>(); + + if (!line.contains("$s{tmp")) { + return result; + } + + int currentIdx = 0; + while (currentIdx < line.length()) { + int idxS = line.indexOf("$s{tmp:", currentIdx); //check for output stream + if (idxS == -1) { + break; + } + + int idxE = line.indexOf("}", idxS); + String tmpName = line.substring(idxS + 7, idxE); + tmpName = tmpName.trim(); + result.put("tmp:" + tmpName, this.getOutputAttributeFilePath(tmpName, true)); + currentIdx = idxE + 1; + } + return result; + } + private ArrayList<String> replaceLineFromOutputResults(String line, String output) throws OnapCommandException { @@ -217,8 +359,11 @@ public class OpenCommandShellCmd extends OnapCommand { jsonPath = jsonPath.trim(); Object value = new Object(); try { - // JSONArray or String - value = JsonPath.read(output, jsonPath); + // Instead of parsing, just assign the json as it is + if (!jsonPath.equals("$")) + value = JsonPath.read(output, jsonPath); + else + value = output; } catch (PathNotFoundException e1) { // NOSONAR //set to blank for those entries which are missing from the output json value = ""; diff --git a/profiles/command/src/main/java/org/onap/cli/fw/cmd/conf/OnapCommandCmdConstants.java b/profiles/command/src/main/java/org/onap/cli/fw/cmd/conf/OnapCommandCmdConstants.java index 6594ef71..4f2d36b4 100644 --- a/profiles/command/src/main/java/org/onap/cli/fw/cmd/conf/OnapCommandCmdConstants.java +++ b/profiles/command/src/main/java/org/onap/cli/fw/cmd/conf/OnapCommandCmdConstants.java @@ -38,6 +38,7 @@ public class OnapCommandCmdConstants { public static final String CMD_SECTIONS = "cli.schema.cmd.sections"; public static final String DEFAULT_PARAMETER_CMD_FILE_NAME = "default_input_parameters_cmd.yaml"; + public static final String TIMEOUT = "timeout"; private OnapCommandCmdConstants() { //as per coding standard ! } diff --git a/profiles/command/src/main/resources/open-cli-schema/cmd/default_input_parameters_cmd.yaml b/profiles/command/src/main/resources/open-cli-schema/cmd/default_input_parameters_cmd.yaml index b358a0c7..b6a97b0d 100644 --- a/profiles/command/src/main/resources/open-cli-schema/cmd/default_input_parameters_cmd.yaml +++ b/profiles/command/src/main/resources/open-cli-schema/cmd/default_input_parameters_cmd.yaml @@ -13,3 +13,13 @@ # limitations under the License. open_cli_schema_version: 1.0 + +parameters: + - name: timeout + type: string + description: timeout for command to complete the given task in milliseconds + short_option: u + long_option: timeout + default_value: 60000 + is_optional: true + is_default_param: false
\ No newline at end of file diff --git a/profiles/http/src/main/java/org/onap/cli/fw/http/auth/OnapCommandHttpAuthClient.java b/profiles/http/src/main/java/org/onap/cli/fw/http/auth/OnapCommandHttpAuthClient.java index 9b7b4039..e5f17bec 100644 --- a/profiles/http/src/main/java/org/onap/cli/fw/http/auth/OnapCommandHttpAuthClient.java +++ b/profiles/http/src/main/java/org/onap/cli/fw/http/auth/OnapCommandHttpAuthClient.java @@ -45,9 +45,9 @@ public class OnapCommandHttpAuthClient { private Map<String, String> loginCache = new HashMap<>(); - public OnapCommandHttpAuthClient(OnapHttpCommand cmd, boolean debug) throws OnapCommandHttpFailure { + public OnapCommandHttpAuthClient(OnapHttpCommand cmd) { this.cmd = cmd; - this.http = new OnapHttpConnection(debug); + this.http = new OnapHttpConnection(); } /** @@ -142,11 +142,6 @@ public class OnapCommandHttpAuthClient { } } - - public String getDebugInfo() { - return this.http.getDebugInfo(); - } - /** * Http call to external service. * diff --git a/profiles/http/src/main/java/org/onap/cli/fw/http/cmd/OnapHttpCommand.java b/profiles/http/src/main/java/org/onap/cli/fw/http/cmd/OnapHttpCommand.java index 14728ad0..53a2d042 100644 --- a/profiles/http/src/main/java/org/onap/cli/fw/http/cmd/OnapHttpCommand.java +++ b/profiles/http/src/main/java/org/onap/cli/fw/http/cmd/OnapHttpCommand.java @@ -16,6 +16,14 @@ package org.onap.cli.fw.http.cmd; +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Optional; + import org.onap.cli.fw.cmd.OnapCommand; import org.onap.cli.fw.cmd.OnapCommandType; import org.onap.cli.fw.conf.OnapCommandConfig; @@ -34,19 +42,12 @@ import org.onap.cli.fw.http.utils.OnapCommandHttpUtils; import org.onap.cli.fw.input.OnapCommandParameter; import org.onap.cli.fw.output.OnapCommandResultAttribute; import org.onap.cli.fw.schema.OnapCommandSchema; +import org.onap.cli.fw.store.OnapCommandExecutionStore; import org.onap.cli.fw.utils.OnapCommandUtils; import org.onap.cli.http.mock.MockJsonGenerator; import org.onap.cli.http.mock.MockRequest; import org.onap.cli.http.mock.MockResponse; -import java.io.IOException; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; -import java.util.Optional; - /** * Oclip http Command. * @@ -56,6 +57,8 @@ public class OnapHttpCommand extends OnapCommand { private HttpInput input = new HttpInput(); + private HttpResult output = new HttpResult(); + private List<Integer> successStatusCodes = new ArrayList<>(); private Map<String, String> resultMap = new HashMap<>(); @@ -201,14 +204,11 @@ public class OnapHttpCommand extends OnapCommand { @Override protected void run() throws OnapCommandException { + this.authClient = new OnapCommandHttpAuthClient(this); + try { // For auth/catalog type commands, login and logout logic is not required boolean isAuthRequired = this.isAuthRequired(); - - this.authClient = new OnapCommandHttpAuthClient( - this, - this.getResult().isDebug()); - if (isAuthRequired) { this.authClient.login(); } @@ -218,24 +218,28 @@ public class OnapHttpCommand extends OnapCommand { if (isAuthRequired) { this.authClient.logout(); } - - if (this.getResult().isDebug() && authClient != null) { - this.getResult().setDebugInfo(this.authClient.getDebugInfo()); - } } catch (OnapCommandException e) { - if (this.getResult().isDebug() && authClient != null) { - this.getResult().setDebugInfo(this.authClient.getDebugInfo()); - } throw e; + } finally { + this.getResult().setDebugInfo(this.input.toString() + "\n" + this.output.toString()); } } protected void processRequest() throws OnapCommandException { HttpInput httpInput = OnapCommandHttpUtils.populateParameters(this.getParametersMap(), this.getInput()); - httpInput.setUri(this.authClient.getServiceUrl() + httpInput.getUri()); + if (!httpInput.getUri().startsWith("http")) { + httpInput.setUri(this.authClient.getServiceUrl() + httpInput.getUri()); + } - HttpResult output = this.authClient.run(httpInput); + this.setInput(httpInput); + + if (this.getExecutionContext() != null) { + OnapCommandExecutionStore.getStore().storeExectutionDebug(this.getExecutionContext(), this.getInput().toString()); + } else { + this.getResult().setDebugInfo(this.getInput().toString()); + } + this.output = this.authClient.run(this.getInput()); this.getResult().setOutput(output); if (!this.getSuccessStatusCodes().contains(output.getStatus())) { @@ -250,12 +254,17 @@ public class OnapHttpCommand extends OnapCommand { } Map<String, ArrayList<String>> results = OnapCommandHttpUtils.populateOutputs(this.getResultMap(), output); - results = OnapCommandUtils.populateOutputsFromInputParameters(results, this.getParametersMap()); + //results = OnapCommandUtils.populateOutputsFromInputParameters(results, this.getParametersMap()); for (OnapCommandResultAttribute attr : this.getResult().getRecords()) { attr.setValues(results.get(attr.getName())); } - generateJsonMock(httpInput, output, this.getSchemaName()); + + try{ + generateJsonMock(this.getInput(), output, this.getSchemaName()); + } catch (OnapCommandFailedMocoGenerate e) { + //NO SONAR ignore it + } } private void generateJsonMock(HttpInput httpInput, HttpResult httpResult, String schemaName) diff --git a/profiles/http/src/main/java/org/onap/cli/fw/http/conf/OnapCommandHttpConstants.java b/profiles/http/src/main/java/org/onap/cli/fw/http/conf/OnapCommandHttpConstants.java index 2d14bb88..6a87ed13 100644 --- a/profiles/http/src/main/java/org/onap/cli/fw/http/conf/OnapCommandHttpConstants.java +++ b/profiles/http/src/main/java/org/onap/cli/fw/http/conf/OnapCommandHttpConstants.java @@ -94,6 +94,7 @@ public class OnapCommandHttpConstants { //context param public static final String CONTEXT = "context"; public static final String CONTEXT_REMOVE_EMPTY_JSON_NODES = "remove_empty_node"; + public static final String __BODY__ = "__body__"; // moco server const public static final String VERIFY_MOCO_HOST = "cli.verify.host"; diff --git a/profiles/http/src/main/java/org/onap/cli/fw/http/connect/OnapHttpConnection.java b/profiles/http/src/main/java/org/onap/cli/fw/http/connect/OnapHttpConnection.java index a2da0706..3f426bf3 100644 --- a/profiles/http/src/main/java/org/onap/cli/fw/http/connect/OnapHttpConnection.java +++ b/profiles/http/src/main/java/org/onap/cli/fw/http/connect/OnapHttpConnection.java @@ -27,15 +27,18 @@ import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; + import javax.net.ssl.SSLContext; import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager; + import org.apache.http.Header; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.annotation.NotThreadSafe; import org.apache.http.client.CookieStore; import org.apache.http.client.HttpClient; +import org.apache.http.client.config.RequestConfig; import org.apache.http.client.methods.HttpDelete; import org.apache.http.client.methods.HttpEntityEnclosingRequestBase; import org.apache.http.client.methods.HttpGet; @@ -79,10 +82,6 @@ public class OnapHttpConnection { Map<String, String> mapCommonHeaders = new HashMap<> (); - protected boolean debug = false; - - private String debugDetails = ""; - public static class TrustAllX509TrustManager implements X509TrustManager { @Override @@ -101,18 +100,6 @@ public class OnapHttpConnection { } } - /** - * OnapHttpConnection Constructor. - * - * @param debug - * boolean - * @throws OnapCommandHttpFailure - * exception - */ - public OnapHttpConnection(boolean debug) { - this.debug = debug; - } - private void initHttpClient(boolean isSecured) throws OnapCommandHttpFailure { if (this.httpClient == null) { try { @@ -137,10 +124,6 @@ public class OnapHttpConnection { } } - public String getDebugInfo() { - return this.debugDetails; - } - private Map<String, String> getHttpHeaders(HttpResponse resp) { Map<String, String> result = new HashMap<>(); @@ -244,9 +227,12 @@ public class OnapHttpConnection { } } - private void addCommonCookies(CookieStore cookieStore) { + private void addCommonCookies(HttpInput input, CookieStore cookieStore) { for (Entry<String, String> header : this.mapCommonHeaders.entrySet()) { - Cookie cookie = new BasicClientCookie(header.getKey(), header.getValue()); + //take care of overriden headers in OCS YAML + String value = input.getReqHeaders().getOrDefault(header.getKey(), + header.getValue()); + Cookie cookie = new BasicClientCookie(header.getKey(), value); cookieStore.addCookie(cookie); } } @@ -267,7 +253,7 @@ public class OnapHttpConnection { } private void updateInputFromCookies(HttpInput input, CookieStore cookieStore) { - addCommonCookies(cookieStore); + addCommonCookies(input, cookieStore); for (String cookieName : input.getReqCookies().keySet()) { BasicClientCookie cookie = new BasicClientCookie(cookieName, input.getReqCookies().get(cookieName)); cookie.setDomain(this.getDomain(input.getUri())); @@ -320,6 +306,8 @@ public class OnapHttpConnection { } requestBase.setURI(URI.create(input.getUri())); + requestBase.setConfig(RequestConfig.custom() + .setSocketTimeout(30000).setConnectTimeout(50000).build()); for (Entry<String, String> h : input.getReqHeaders().entrySet()) { requestBase.addHeader(h.getKey(), h.getValue()); @@ -328,7 +316,6 @@ public class OnapHttpConnection { HttpResult result = new HttpResult(); try { - this.debugDetails = ""; CookieStore cookieStore = new BasicCookieStore(); updateInputFromCookies(input, cookieStore); HttpContext localContext = new BasicHttpContext(); @@ -344,12 +331,6 @@ public class OnapHttpConnection { this.updateResultFromCookies(result, cookieStore.getCookies()); } catch (Exception e) { // NOSONAR throw new OnapCommandHttpFailure(e); - } finally { - String info = input + " " + result; - log.info(info); - if (this.debug) { - this.debugDetails = info; - } } return result; diff --git a/profiles/http/src/main/java/org/onap/cli/fw/http/utils/OnapCommandHttpUtils.java b/profiles/http/src/main/java/org/onap/cli/fw/http/utils/OnapCommandHttpUtils.java index f4a6cee5..5cab0a68 100644 --- a/profiles/http/src/main/java/org/onap/cli/fw/http/utils/OnapCommandHttpUtils.java +++ b/profiles/http/src/main/java/org/onap/cli/fw/http/utils/OnapCommandHttpUtils.java @@ -76,24 +76,22 @@ public class OnapCommandHttpUtils { } } + inp.setUri(OnapCommandUtils.replaceLineForSpecialValues(input.getUri())); inp.setUri(OnapCommandUtils.replaceLineFromInputParameters(input.getUri(), params)); inp.setMethod(input.getMethod().toLowerCase()); - for (String h : input.getReqHeaders().keySet()) { - String value = input.getReqHeaders().get(h); - inp.getReqHeaders().put(h, OnapCommandUtils.replaceLineFromInputParameters(value, params)); - } + boolean isRemoveEmptyNodes = Boolean.parseBoolean(input.getContext().getOrDefault(OnapCommandHttpConstants.CONTEXT_REMOVE_EMPTY_JSON_NODES, "false")); - for (String h : input.getReqQueries().keySet()) { - String value = input.getReqQueries().get(h); - inp.getReqQueries().put(h, OnapCommandUtils.replaceLineFromInputParameters(value, params)); + //Process for md5 + Map <String, String> values = new HashMap<>(); + for (Map.Entry<String, OnapCommandParameter> param: params.entrySet()) { + values.put(param.getKey(), param.getValue().getValue().toString()); } - boolean isRemoveEmptyNodes = Boolean.parseBoolean(input.getContext().getOrDefault(OnapCommandHttpConstants.CONTEXT_REMOVE_EMPTY_JSON_NODES, "false")); - if (input.getMultiparts().size() > 0) { for (HttpInput.Part part: input.getMultiparts()) { + part.setContent(OnapCommandUtils.replaceLineForSpecialValues(part.getContent(), values)); part.setContent(OnapCommandUtils.replaceLineFromInputParameters(part.getContent(), params)); if (isRemoveEmptyNodes) { part.setContent(OnapCommandHttpUtils.normalizeJson(part.getContent())); @@ -103,11 +101,28 @@ public class OnapCommandHttpUtils { inp.setMultiparts(input.getMultiparts()); } else { inp.setMultipartEntityName(input.getMultipartEntityName()); - inp.setBody(OnapCommandUtils.replaceLineFromInputParameters(input.getBody(), params)); + inp.setBody(OnapCommandUtils.replaceLineForSpecialValues(input.getBody(), values)); + inp.setBody(OnapCommandUtils.replaceLineFromInputParameters(inp.getBody(), params)); if (isRemoveEmptyNodes) { inp.setBody(OnapCommandHttpUtils.normalizeJson(inp.getBody())); } } + + //consider __body__ spl entry + values.put(OnapCommandHttpConstants.__BODY__, inp.getBody()); + + for (String h : input.getReqHeaders().keySet()) { + String value = input.getReqHeaders().get(h); + value = OnapCommandUtils.replaceLineForSpecialValues(value, values); + inp.getReqHeaders().put(h, OnapCommandUtils.replaceLineFromInputParameters(value, params)); + } + + for (String h : input.getReqQueries().keySet()) { + String value = input.getReqQueries().get(h); + value = OnapCommandUtils.replaceLineForSpecialValues(value, values); + inp.getReqQueries().put(h, OnapCommandUtils.replaceLineFromInputParameters(value, params)); + } + return inp; } diff --git a/profiles/http/src/test/java/org/onap/cli/fw/http/OnapHttpConnectionTest.java b/profiles/http/src/test/java/org/onap/cli/fw/http/OnapHttpConnectionTest.java index fab02ff2..f0115580 100644 --- a/profiles/http/src/test/java/org/onap/cli/fw/http/OnapHttpConnectionTest.java +++ b/profiles/http/src/test/java/org/onap/cli/fw/http/OnapHttpConnectionTest.java @@ -72,8 +72,7 @@ public class OnapHttpConnectionTest { } }; inp.setMethod("get"); - con = new OnapHttpConnection(true); - con.getDebugInfo(); + con = new OnapHttpConnection(); con.get(inp); } @@ -90,7 +89,7 @@ public class OnapHttpConnectionTest { }; inp.setMethod("post"); - con = new OnapHttpConnection(true); + con = new OnapHttpConnection(); con.post(inp); } @@ -108,7 +107,7 @@ public class OnapHttpConnectionTest { inp.setMethod("post"); inp.setBinaryData(true); - con = new OnapHttpConnection(true); + con = new OnapHttpConnection(); con.post(inp); } @@ -123,7 +122,7 @@ public class OnapHttpConnectionTest { } }; inp.setMethod("put"); - con = new OnapHttpConnection(true); + con = new OnapHttpConnection(); con.put(inp); } @@ -138,7 +137,7 @@ public class OnapHttpConnectionTest { } }; inp.setMethod("delete"); - con = new OnapHttpConnection(true); + con = new OnapHttpConnection(); con.delete(inp); } @@ -153,14 +152,14 @@ public class OnapHttpConnectionTest { } }; inp.setMethod("other"); - con = new OnapHttpConnection(true); + con = new OnapHttpConnection(); con.request(inp); } @Test() public void httpUnSecuredCloseExceptionTest() throws OnapCommandHttpFailure { inp.setMethod("other"); - con = new OnapHttpConnection(true); + con = new OnapHttpConnection(); con.close(); } @@ -185,7 +184,7 @@ public class OnapHttpConnectionTest { inp.setReqHeaders(new HashMap<String, String>()); inp.setReqQueries(new HashMap<String, String>()); inp.setUri("https://192.168.99.10:80"); - OnapHttpConnection con = new OnapHttpConnection(false); + OnapHttpConnection con = new OnapHttpConnection(); con.get(inp); } catch (OnapCommandHttpFailure e) { assertEquals("0x3001::IO Exception", e.getMessage()); diff --git a/validate/sample-mock-generator/src/main/java/org/onap/cli/http/mock/MockRequest.java b/validate/sample-mock-generator/src/main/java/org/onap/cli/http/mock/MockRequest.java index cbb2a26a..d1d8b08d 100644 --- a/validate/sample-mock-generator/src/main/java/org/onap/cli/http/mock/MockRequest.java +++ b/validate/sample-mock-generator/src/main/java/org/onap/cli/http/mock/MockRequest.java @@ -20,6 +20,7 @@ import java.io.IOException; import java.net.URL; import java.util.Map; +import com.fasterxml.jackson.core.JsonFactory; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; @@ -61,8 +62,12 @@ public class MockRequest { public void setJson(String json) throws IOException { if (!json.isEmpty()) { - ObjectMapper objectMapper = new ObjectMapper(); - this.json = objectMapper.readTree(json); + try { + ObjectMapper objectMapper = new ObjectMapper(); + this.json = objectMapper.readTree(json); + } catch (Exception e) { + this.json = new ObjectMapper().readTree("{}"); + } } } diff --git a/validate/sample-mock-generator/src/main/java/org/onap/cli/http/mock/MockResponse.java b/validate/sample-mock-generator/src/main/java/org/onap/cli/http/mock/MockResponse.java index 7e8cdca8..d7999bea 100644 --- a/validate/sample-mock-generator/src/main/java/org/onap/cli/http/mock/MockResponse.java +++ b/validate/sample-mock-generator/src/main/java/org/onap/cli/http/mock/MockResponse.java @@ -38,9 +38,13 @@ public class MockResponse { } public void setJson(String json) throws IOException { - if (json != null && !json.isEmpty()) { - ObjectMapper objectMapper = new ObjectMapper(); - this.json = objectMapper.readTree(json); + if (!json.isEmpty()) { + try { + ObjectMapper objectMapper = new ObjectMapper(); + this.json = objectMapper.readTree(json); + } catch (Exception e) { + this.json = new ObjectMapper().readTree("{}"); + } } } } diff --git a/validate/sample-mock-generator/src/test/java/org/onap/cli/http/mock/MockJsonGeneratorTest.java b/validate/sample-mock-generator/src/test/java/org/onap/cli/http/mock/MockJsonGeneratorTest.java index 3b79057f..0e0c0f59 100644 --- a/validate/sample-mock-generator/src/test/java/org/onap/cli/http/mock/MockJsonGeneratorTest.java +++ b/validate/sample-mock-generator/src/test/java/org/onap/cli/http/mock/MockJsonGeneratorTest.java @@ -34,32 +34,4 @@ public class MockJsonGeneratorTest { MockJsonGenerator.generateMocking(mockRequest, mockResponse, "target/test"); } - - @Test(expected=IOException.class) - public void mocoGenerateFailedInvalidBodyTest() throws IOException { - MockRequest mockRequest = new MockRequest(); - mockRequest.setJson("{\"value\" : \"234sdf-345\""); - mockRequest.setMethod("get"); - mockRequest.setUri("http://1.1.1.1:80/getResource"); - - MockResponse mockResponse = new MockResponse(); - mockResponse.setStatus(200); - mockResponse.setJson("{\"value\" : \"234sdf-345\""); - - MockJsonGenerator.generateMocking(mockRequest, mockResponse, "target/test"); - } - - @Test(expected=IOException.class) - public void mocoGenerateFailedInvalidUrlTest() throws IOException { - MockRequest mockRequest = new MockRequest(); - mockRequest.setJson("{\"value\" : \"234sdf-345\""); - mockRequest.setMethod("get"); - mockRequest.setUri("http://1.1.1.1:80:invalid"); - - MockResponse mockResponse = new MockResponse(); - mockResponse.setStatus(200); - mockResponse.setJson("{\"value\" : \"234sdf-345\""); - - MockJsonGenerator.generateMocking(mockRequest, mockResponse, "target/test"); - } } diff --git a/validate/sample-yaml-generator/src/main/java/org/onap/cli/sample/yaml/SampleYamlGenerator.java b/validate/sample-yaml-generator/src/main/java/org/onap/cli/sample/yaml/SampleYamlGenerator.java index 179eaa36..b2e2dc88 100644 --- a/validate/sample-yaml-generator/src/main/java/org/onap/cli/sample/yaml/SampleYamlGenerator.java +++ b/validate/sample-yaml-generator/src/main/java/org/onap/cli/sample/yaml/SampleYamlGenerator.java @@ -27,11 +27,9 @@ public class SampleYamlGenerator { static int nTab; - public static void generateSampleYaml(List<String> input, String output, String version, + public static void generateSampleYaml(String cmdName, List<String> input, String output, String version, String targetPath, boolean debug) throws IOException { - String cmdName = input.get(0); - PrintWriter writer = new PrintWriter(targetPath, "UTF-8"); writeKeyValuePair(writer, "open_cli_sample_version", "1.0"); writeKeyValuePair(writer, "name", cmdName); @@ -41,7 +39,7 @@ public class SampleYamlGenerator { writeKey(writer, "sample1"); writeKeyValuePair(writer, "name", cmdName); - writeKeyValuePair(writer, "input", input.stream().skip(1).collect(Collectors.joining(" ")).trim()); + writeKeyValuePair(writer, "input", input.stream().collect(Collectors.joining(" ")).trim()); writeKeyValuePair(writer, "moco", new File(targetPath).getName().replaceAll("-sample.yaml", "-moco.json")); writeMultilineKeyValue(writer, "output", output.trim(), debug); @@ -60,7 +58,7 @@ public class SampleYamlGenerator { writer.write(" |\n"); nTab++; String[] lines = value.split("\n"); - long skipLines = debug ? 11 : 0; + long skipLines = debug ? 0 : 0; Arrays.stream(lines).skip(skipLines ).forEach(line -> writer.write(printTabs() + line + "\n")); // NOSONAR } diff --git a/validate/sample-yaml-generator/src/test/java/org/onap/cli/sample/yaml/SampleYamlGeneratorTest.java b/validate/sample-yaml-generator/src/test/java/org/onap/cli/sample/yaml/SampleYamlGeneratorTest.java index 98c0ce9d..fba93e62 100644 --- a/validate/sample-yaml-generator/src/test/java/org/onap/cli/sample/yaml/SampleYamlGeneratorTest.java +++ b/validate/sample-yaml-generator/src/test/java/org/onap/cli/sample/yaml/SampleYamlGeneratorTest.java @@ -25,7 +25,7 @@ public class SampleYamlGeneratorTest { @Test public void testGenerateSampleYaml() throws IOException { - SampleYamlGenerator.generateSampleYaml(Arrays.asList("testcmd", "-a", "argument"), + SampleYamlGenerator.generateSampleYaml("testcmd", Arrays.asList("-a", "argument"), "+--------+\n+val +\n+argument+", "test-version-1.0", "target/test.yaml", false); } |