package org.onap.cli.fw.cmd;
import java.util.ArrayList;
+import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import org.onap.cli.fw.error.OnapCommandNotInitialized;
import org.onap.cli.fw.info.OnapCommandInfo;
import org.onap.cli.fw.input.OnapCommandParameter;
+import org.onap.cli.fw.input.OnapCommandParameterType;
import org.onap.cli.fw.output.OnapCommandResult;
import org.onap.cli.fw.output.OnapCommandResultAttribute;
import org.onap.cli.fw.output.OnapCommandResultAttributeScope;
import org.onap.cli.fw.output.OnapCommandResultType;
+import org.onap.cli.fw.schema.OnapCommandSchemaInfo;
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.utils.OnapCommandHelperUtils;
import org.onap.cli.fw.utils.OnapCommandUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
/**
* Oclip Command.
*/
public abstract class OnapCommand {
+ private static Logger log = LoggerFactory.getLogger(OnapCommand.class);
+
private String cmdDescription;
private String cmdName;
private OnapCommandResult cmdResult = new OnapCommandResult();
+ private List<String> defaultSchemas = new ArrayList<>();
+
protected boolean isInitialzied = false;
+ private boolean isRpc = false;
+
+ public boolean isRpc() {
+ return isRpc;
+ }
+
+ public void setRpc(boolean isRpc) {
+ this.isRpc = isRpc;
+ }
+
+ protected OnapCommand() {
+ this.addDefaultSchemas(OnapCommandConstants.DEFAULT_PARAMETER_FILE_NAME);
+ }
+
+ public List<String> getSchemas() {
+ List<String> schemas = new ArrayList<>();
+ schemas.addAll(this.defaultSchemas);
+ schemas.add(this.getSchemaName());
+ return schemas;
+ }
+
public String getSchemaVersion() {
return OnapCommandConstants.OPEN_CLI_SCHEMA_VERSION_VALUE_1_0;
}
this.cmdSchemaName = schemaName;
}
+ protected void addDefaultSchemas(String schema) {
+ this.defaultSchemas.add(schema);
+ }
+
+ public List<String> getDefaultSchema() {
+ return this.defaultSchemas;
+ }
+
+ public List<String> getArgs() {
+ List <String> args = new ArrayList<>();
+ for (OnapCommandParameter param: this.getParameters()) {
+ args.add(OnapCommandParameter.printLongOption(param.getName()));
+ args.add(param.getValue().toString());
+ }
+
+ return args;
+ }
+
+ public String getArgsJson(boolean ignoreDefaults) {
+ Map <String, String> args = new HashMap<>();
+
+ for (OnapCommandParameter param: this.getParameters()) {
+ if (ignoreDefaults && param.isDefaultParam())
+ continue;
+
+ args.put(param.getName(), param.getValue().toString());
+ }
+
+ try {
+ return new ObjectMapper().writeValueAsString(args);
+ } catch (JsonProcessingException e) {
+ return "{}";
+ }
+ }
+
/**
* Initialize this command from command schema and assumes schema is already validated.
*
return this.initializeSchema(schema, false);
}
+ public List<String> initializeSchema(OnapCommandSchemaInfo schema) throws OnapCommandException {
+ return this.initializeSchema(schema.getSchemaName(), false);
+ }
+
public List<String> initializeSchema(String schema, boolean validate) throws OnapCommandException {
this.setSchemaName(schema);
- List<String> errors = OnapCommandSchemaLoader.loadSchema(this, schema, true, validate);
- errors.addAll(this.initializeProfileSchema());
+ Map<String, ?> schemaMap = OnapCommandSchemaMerger.mergeSchemas(this);
+ List<String> errors = OnapCommandSchemaLoader.parseSchema(this, schemaMap, validate);
+ errors.addAll(this.initializeProfileSchema(schemaMap, validate));
this.isInitialzied = true;
return errors;
/**
* Any additional profile based such as http schema could be initialized.
*/
- protected List<String> initializeProfileSchema() throws OnapCommandException {
+ protected List<String> initializeProfileSchema(Map<String, ?> schemaMap, boolean validate) throws OnapCommandException {
return new ArrayList<>();
}
}
}
+ protected void preRun() throws OnapCommandException {
+ log.debug(this.getName() + " PRE-RUN");
+ }
+
+ 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.
throw new OnapCommandNotInitialized(this.getClass().getName());
}
+ log.info("CMD: " + this.getName());
+
Map<String, OnapCommandParameter> paramMap = this.getParametersMap();
+ log.info("INPUT: " + paramMap);
+
// -h or --help is always higher precedence !, user can set this value to get help message
- if (OnapCommandConstants.BOOLEAN_TRUE.equals(paramMap.get(OnapCommandConstants.DEFAULT_PARAMETER_HELP).getValue())) {
- OnapCommandResult result = new OnapCommandResult();
- result.setType(OnapCommandResultType.TEXT);
- result.setOutput(this.printHelp());
- return result;
+ if ((Boolean)(paramMap.get(OnapCommandConstants.DEFAULT_PARAMETER_HELP).getValue())) {
+ this.cmdResult.setType(OnapCommandResultType.TEXT);
+ this.cmdResult.setOutput(this.printHelp());
+ return this.cmdResult;
}
// -v or --version is next higher precedence !, user can set this value to get help message
- if (OnapCommandConstants.BOOLEAN_TRUE.equals(paramMap.get(OnapCommandConstants.DEFAULT_PARAMETER_VERSION).getValue())) {
- OnapCommandResult result = new OnapCommandResult();
- result.setType(OnapCommandResultType.TEXT);
- result.setOutput(this.printVersion());
- return result;
+ if ((Boolean)(paramMap.get(OnapCommandConstants.DEFAULT_PARAMETER_VERSION).getValue())) {
+ this.cmdResult.setType(OnapCommandResultType.TEXT);
+ this.cmdResult.setOutput(this.printVersion());
+ return this.cmdResult;
+ }
+
+ //set the artifact content path.
+ for (OnapCommandParameter param: this.getParameters()) {
+ if (!param.getParameterType().equals(OnapCommandParameterType.BINARY))
+ continue;
+
+ if (param.getValue().toString().matches("artifact://*:*")) {
+ String categoryAndName = param.getValue().toString().replaceFirst("artifact://", "");
+ String[] categoryAndNameTokens = categoryAndName.split(":");
+ Artifact a = OnapCommandArtifactStore.getStore().getArtifact(categoryAndNameTokens[1], categoryAndNameTokens[0]);
+ param.setValue(a.getPath());
+ }
}
// validate
// -f or --format
this.cmdResult.setType(
OnapCommandResultType.get(paramMap.get(OnapCommandConstants.DEFAULT_PARAMETER_OUTPUT_FORMAT).getValue().toString()));
- if (OnapCommandConstants.BOOLEAN_TRUE.equals(paramMap.get(OnapCommandConstants.DEFAULT_PARAMETER_OUTPUT_ATTR_LONG).getValue())) {
+ if ((Boolean)(paramMap.get(OnapCommandConstants.DEFAULT_PARAMETER_OUTPUT_ATTR_LONG).getValue())) {
this.cmdResult.setScope(OnapCommandResultAttributeScope.LONG);
}
// --no-title
- if (OnapCommandConstants.BOOLEAN_TRUE.equals(paramMap.get(OnapCommandConstants.DEFAULT_PARAMETER_OUTPUT_NO_TITLE).getValue())) {
+ if ((Boolean)paramMap.get(OnapCommandConstants.DEFAULT_PARAMETER_OUTPUT_NO_TITLE).getValue()) {
this.cmdResult.setIncludeTitle(false);
}
// --debug
- if (OnapCommandConstants.BOOLEAN_TRUE.equals(paramMap.get(OnapCommandConstants.DEFAULT_PARAMETER_DEBUG).getValue())) {
+ if ((Boolean)(paramMap.get(OnapCommandConstants.DEFAULT_PARAMETER_DEBUG).getValue())) {
this.cmdResult.setDebug(true);
}
}
}
+ preRun();
+
this.run();
+ log.info("OUTPUT: " + this.cmdResult.getRecords());
+
+ postRun();
return this.cmdResult;
}
* @return version
*/
public String printVersion() {
- return this.getInfo().getService();
+ return this.getInfo().getProduct() + "::" + this.getInfo().getService();
}
/**
public String printHelp() throws OnapCommandHelpFailed {
return OnapCommandHelperUtils.help(this);
}
-
// (mrkanag) Add toString for all command, parameter, result, etc objects in JSON format
}