diff options
author | Kanagaraj Manickam k00365106 <kanagaraj.manickam@huawei.com> | 2018-08-13 14:33:53 +0530 |
---|---|---|
committer | Kanagaraj Manickam k00365106 <kanagaraj.manickam@huawei.com> | 2018-08-13 15:10:28 +0530 |
commit | 559dd510d030ce475d74dcb2ec4a0f50c63a62f3 (patch) | |
tree | 321fc59445876dd143adb45cf74cfe113b8c46fa | |
parent | 7e5f6a0f9c1e7d8ee52d96fc4b1c28ccb65168f2 (diff) |
Command profile
Issue-ID: VNFSDK-300 CLI-123
Change-Id: Ib8609d73ebf445adb01e6e4afab6d5f1b6f768c1
Signed-off-by: Kanagaraj Manickam k00365106 <kanagaraj.manickam@huawei.com>
-rw-r--r-- | profiles/command/src/main/java/org/onap/cli/fw/cmd/cmd/OpenCommandShellCmd.java | 132 | ||||
-rw-r--r-- | profiles/command/src/main/java/org/onap/cli/fw/cmd/schema/OnapCommandSchemaCmdLoader.java | 91 |
2 files changed, 223 insertions, 0 deletions
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 new file mode 100644 index 00000000..d6e0e8c0 --- /dev/null +++ b/profiles/command/src/main/java/org/onap/cli/fw/cmd/cmd/OpenCommandShellCmd.java @@ -0,0 +1,132 @@ +/* + * 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. + */ + +package org.onap.cli.fw.cmd.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 org.onap.cli.fw.cmd.OnapCommand; +import org.onap.cli.fw.cmd.conf.OnapCommandCmdConstants; +import org.onap.cli.fw.cmd.schema.OnapCommandSchemaCmdLoader; +import org.onap.cli.fw.error.OnapCommandException; +import org.onap.cli.fw.error.OnapCommandExecutionFailed; +import org.onap.cli.fw.input.OnapCommandParameter; +import org.onap.cli.fw.output.OnapCommandResultType; +import org.onap.cli.fw.schema.OnapCommandSchema; +import org.onap.cli.fw.utils.OnapCommandUtils; + +/** + * Hello world. + */ +@OnapCommandSchema(type = "cmd") +public class OpenCommandShellCmd extends OnapCommand { + + public OpenCommandShellCmd() { + super.addDefaultSchemas(OnapCommandCmdConstants.DEFAULT_PARAMETER_CMD_FILE_NAME); + } + + private Map<String, String> resultMap = new HashMap<>(); + + private List<String> command; + + private Map<String, String> envs; + + private String wd = null; + + private List<Integer> successStatusCodes = new ArrayList<>(); + + public List<Integer> getSuccessStatusCodes() { + return successStatusCodes; + } + + public void setSuccessStatusCodes(ArrayList<Integer> successStatusCodes) { + this.successStatusCodes = successStatusCodes; + } + + public String getWd() { + return wd; + } + + public void setWd(String wd) { + this.wd = wd; + } + + public Map<String, String> getEnvs() { + return envs; + } + + public void setEnvs(Map<String, String> envs) { + this.envs = envs; + } + + + + public List<String> getCommand() { + return command; + } + + public void setCommand(List<String> command) { + this.command = command; + } + + public Map<String, String> getResultMap() { + return resultMap; + } + + public void setResultMap(Map<String, String> resultMap) { + this.resultMap = resultMap; + } + + @Override + protected List<String> initializeProfileSchema(Map<String, ?> schemaMap, boolean validate) throws OnapCommandException { + return OnapCommandSchemaCmdLoader.parseCmdSchema(this, schemaMap, validate); + } + + @Override + protected void run() throws OnapCommandException { + //Read the input arguments + Map<String, OnapCommandParameter> paramMap = this.getParametersMap(); + + //Process command + String []cmd = this.getCommand().toArray(new String []{}); + String cwd = this.getWd(); + List <String> envs = new ArrayList<>(); + + for (String env: this.getEnvs().keySet()) { + envs.add(env + "=" + this.getEnvs().get(env)); + } + + ProcessRunner pr = new ProcessRunner( + cmd, + (envs.size() > 0) ? envs.toArray(new String []{}) : null, + cwd); + try { + pr.run(); + } catch (InterruptedException | IOException e) { + throw new OnapCommandExecutionFailed(this.getName(), e); + } + + //Populate outputs + this.getResult().getRecordsMap().get("output").getValues().add(pr.getOutput()); + this.getResult().getRecordsMap().get("error").getValues().add(pr.getError()); + this.getResult().getRecordsMap().get("exitCode").getValues().add("" + pr.getExitCode()); + } +} diff --git a/profiles/command/src/main/java/org/onap/cli/fw/cmd/schema/OnapCommandSchemaCmdLoader.java b/profiles/command/src/main/java/org/onap/cli/fw/cmd/schema/OnapCommandSchemaCmdLoader.java new file mode 100644 index 00000000..c3764eaa --- /dev/null +++ b/profiles/command/src/main/java/org/onap/cli/fw/cmd/schema/OnapCommandSchemaCmdLoader.java @@ -0,0 +1,91 @@ +/* + * 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. + */ + +package org.onap.cli.fw.cmd.schema; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Map; + +import org.onap.cli.fw.cmd.cmd.OpenCommandShellCmd; +import org.onap.cli.fw.cmd.conf.OnapCommandCmdConstants; +import org.onap.cli.fw.conf.OnapCommandConfig; +import org.onap.cli.fw.error.OnapCommandException; +import org.onap.cli.fw.utils.OnapCommandUtils; + +public class OnapCommandSchemaCmdLoader { + + private OnapCommandSchemaCmdLoader() { + // to follow standards ! + } + + + /** + * Load the schema. + * + * @param cmd + * OpenCommandShellCmd + * @param schemaName + * schema name + * @throws OnapCommandException + * on error + */ + public static ArrayList<String> parseCmdSchema(OpenCommandShellCmd cmd, + final Map<String, ?> values, + boolean validate) throws OnapCommandException { + ArrayList<String> errorList = new ArrayList<>(); + Map<String, ?> valMap = (Map<String, ?>) values.get(OnapCommandCmdConstants.CMD); + + if (valMap != null) { + if (validate) { + OnapCommandUtils.validateTags(errorList, valMap, OnapCommandConfig.getCommaSeparatedList(OnapCommandCmdConstants.CMD_SECTIONS), + OnapCommandConfig.getCommaSeparatedList(OnapCommandCmdConstants.CMD_MANDATORY_SECTIONS), OnapCommandCmdConstants.CMD); + } + for (Map.Entry<String, ?> entry1 : valMap.entrySet()) { + String key1 = entry1.getKey(); + + switch (key1) { + case OnapCommandCmdConstants.COMMAND: + Object o = valMap.get(key1); + if (o instanceof List) { + cmd.setCommand((List<String>) o); + } else { + cmd.setCommand(Arrays.asList(new String [] {(String) o})); + } + break; + + case OnapCommandCmdConstants.ENVIRONMENT: + Map<String, String> envMap = (Map<String, String>) valMap.get(key1); + cmd.setEnvs(envMap); + + break; + + case OnapCommandCmdConstants.WD: + cmd.setWd((String)valMap.get(key1)); + break; + + case OnapCommandCmdConstants.SUCCESS_EXIT_CODE: + cmd.setSuccessStatusCodes((ArrayList) valMap.get(key1)); + break; + } + } + } + + + return errorList; + } +} |