2 * Copyright 2017 Huawei Technologies Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.onap.cli.fw.cmd;
19 import java.util.ArrayList;
20 import java.util.HashSet;
21 import java.util.List;
25 import org.onap.cli.fw.conf.OnapCommandConstants;
26 import org.onap.cli.fw.error.OnapCommandException;
27 import org.onap.cli.fw.error.OnapCommandHelpFailed;
28 import org.onap.cli.fw.error.OnapCommandNotInitialized;
29 import org.onap.cli.fw.info.OnapCommandInfo;
30 import org.onap.cli.fw.input.OnapCommandParameter;
31 import org.onap.cli.fw.output.OnapCommandResult;
32 import org.onap.cli.fw.output.OnapCommandResultAttribute;
33 import org.onap.cli.fw.output.OnapCommandResultAttributeScope;
34 import org.onap.cli.fw.output.OnapCommandResultType;
35 import org.onap.cli.fw.schema.OnapCommandSchemaLoader;
36 import org.onap.cli.fw.schema.OnapCommandSchemaMerger;
37 import org.onap.cli.fw.utils.OnapCommandHelperUtils;
38 import org.onap.cli.fw.utils.OnapCommandUtils;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
46 public abstract class OnapCommand {
48 private static Logger LOG = LoggerFactory.getLogger(OnapCommand.class);
50 private String cmdDescription;
52 private String cmdName;
54 private String cmdSchemaName;
56 private OnapCommandInfo info = new OnapCommandInfo();
58 private Set<OnapCommandParameter> cmdParameters = new HashSet<>();
60 private OnapCommandResult cmdResult = new OnapCommandResult();
62 private List<String> defaultSchemas = new ArrayList<>();
64 protected boolean isInitialzied = false;
66 protected OnapCommand() {
67 this.addDefaultSchemas(OnapCommandConstants.DEFAULT_PARAMETER_FILE_NAME);
70 public List<String> getSchemas() throws OnapCommandException {
71 List<String> schemas = new ArrayList<>();
72 schemas.addAll(this.defaultSchemas);
73 schemas.add(this.getSchemaName());
77 public String getSchemaVersion() {
78 return OnapCommandConstants.OPEN_CLI_SCHEMA_VERSION_VALUE_1_0;
81 public String getDescription() {
82 return this.cmdDescription;
85 public void setDescription(String description) {
86 this.cmdDescription = description;
89 public String getName() {
93 public void setName(String name) {
97 public OnapCommandInfo getInfo() {
101 public void setInfo(OnapCommandInfo info) {
105 public void setParameters(Set<OnapCommandParameter> parameters) {
106 this.cmdParameters = parameters;
109 public Set<OnapCommandParameter> getParameters() {
110 return this.cmdParameters;
113 public Map<String, OnapCommandParameter> getParametersMap() {
114 return OnapCommandUtils.getInputMap(this.getParameters());
117 public OnapCommandResult getResult() {
118 return this.cmdResult;
121 public void setResult(OnapCommandResult result) {
122 this.cmdResult = result;
125 public String getSchemaName() {
126 return cmdSchemaName;
129 protected void setSchemaName(String schemaName) {
130 this.cmdSchemaName = schemaName;
133 protected void addDefaultSchemas(String schema) {
134 this.defaultSchemas.add(schema);
137 public List<String> getDefaultSchema() {
138 return this.defaultSchemas;
142 * Initialize this command from command schema and assumes schema is already validated.
144 * @throws OnapCommandException
146 * @return List of error strings
148 public List<String> initializeSchema(String schema) throws OnapCommandException {
149 return this.initializeSchema(schema, false);
153 public List<String> initializeSchema(String schema, boolean validate) throws OnapCommandException {
154 this.setSchemaName(schema);
156 Map<String, ?> schemaMap = OnapCommandSchemaMerger.mergeSchemas(this);
157 List<String> errors = OnapCommandSchemaLoader.parseSchema(this, schemaMap, validate);
158 errors.addAll(this.initializeProfileSchema(schemaMap, validate));
159 this.isInitialzied = true;
164 * Any additional profile based such as http schema could be initialized.
166 protected List<String> initializeProfileSchema(Map<String, ?> schemaMap, boolean validate) throws OnapCommandException {
167 return new ArrayList<>();
171 * Validate input parameters. This can be overridden in derived commands
173 protected void validate() throws OnapCommandException {
174 for (OnapCommandParameter param : this.getParameters()) {
175 if (param.isInclude()) {
182 * Oclip command execute with given parameters on service. Before calling this method, its mandatory to set all
185 * @throws OnapCommandException
186 * : General Command Exception
188 public OnapCommandResult execute() throws OnapCommandException {
189 if (!this.isInitialzied) {
190 throw new OnapCommandNotInitialized(this.getClass().getName());
193 LOG.info("CMD: " + this.getName());
195 Map<String, OnapCommandParameter> paramMap = this.getParametersMap();
197 LOG.info("INPUT: " + paramMap);
199 // -h or --help is always higher precedence !, user can set this value to get help message
200 if (OnapCommandConstants.BOOLEAN_TRUE.equals(paramMap.get(OnapCommandConstants.DEFAULT_PARAMETER_HELP).getValue())) {
201 this.cmdResult.setType(OnapCommandResultType.TEXT);
202 this.cmdResult.setOutput(this.printHelp());
203 return this.cmdResult;
206 // -v or --version is next higher precedence !, user can set this value to get help message
207 if (OnapCommandConstants.BOOLEAN_TRUE.equals(paramMap.get(OnapCommandConstants.DEFAULT_PARAMETER_VERSION).getValue())) {
208 this.cmdResult.setType(OnapCommandResultType.TEXT);
209 this.cmdResult.setOutput(this.printVersion());
210 return this.cmdResult;
217 this.cmdResult.setType(
218 OnapCommandResultType.get(paramMap.get(OnapCommandConstants.DEFAULT_PARAMETER_OUTPUT_FORMAT).getValue().toString()));
219 if (OnapCommandConstants.BOOLEAN_TRUE.equals(paramMap.get(OnapCommandConstants.DEFAULT_PARAMETER_OUTPUT_ATTR_LONG).getValue())) {
220 this.cmdResult.setScope(OnapCommandResultAttributeScope.LONG);
223 if (OnapCommandConstants.BOOLEAN_TRUE.equals(paramMap.get(OnapCommandConstants.DEFAULT_PARAMETER_OUTPUT_NO_TITLE).getValue())) {
224 this.cmdResult.setIncludeTitle(false);
228 if (OnapCommandConstants.BOOLEAN_TRUE.equals(paramMap.get(OnapCommandConstants.DEFAULT_PARAMETER_DEBUG).getValue())) {
229 this.cmdResult.setDebug(true);
232 //pre-process result attributes for spl entries and input parameters
233 for (OnapCommandResultAttribute attr: this.cmdResult.getRecords()) {
234 if (!attr.getDefaultValue().isEmpty()) {
235 attr.setDefaultValue(OnapCommandUtils.replaceLineForSpecialValues(attr.getDefaultValue()));
236 attr.setDefaultValue(OnapCommandUtils.replaceLineFromInputParameters(
237 attr.getDefaultValue(), this.getParametersMap()));
243 LOG.info("OUTPUT: " + this.cmdResult.getRecords());
245 return this.cmdResult;
249 * Each command implements run method to executing the command.
252 protected abstract void run() throws OnapCommandException;
255 * Returns the service service version it supports.
259 public String printVersion() {
260 return this.getInfo().getProduct() + "::" + this.getInfo().getService();
264 * Provides help message for this command.
266 * @return help message
267 * @throws OnapCommandHelpFailed
268 * Failed to execute Help command.
270 public String printHelp() throws OnapCommandHelpFailed {
271 return OnapCommandHelperUtils.help(this);
274 // (mrkanag) Add toString for all command, parameter, result, etc objects in JSON format