d3264508b4346395584173c0cf30e9b94f4e3383
[cli.git] / framework / src / main / java / org / onap / cli / fw / cmd / OnapCommand.java
1 /*
2  * Copyright 2017 Huawei Technologies Co., Ltd.
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.onap.cli.fw.cmd;
18
19 import java.util.ArrayList;
20 import java.util.HashSet;
21 import java.util.List;
22 import java.util.Map;
23 import java.util.Set;
24
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;
41
42 /**
43  * Oclip Command.
44  *
45  */
46 public abstract class OnapCommand {
47
48     private static Logger log = LoggerFactory.getLogger(OnapCommand.class);
49
50     private String cmdDescription;
51
52     private String cmdName;
53
54     private String cmdSchemaName;
55
56     private OnapCommandInfo info = new OnapCommandInfo();
57
58     private Set<OnapCommandParameter> cmdParameters = new HashSet<>();
59
60     private OnapCommandResult cmdResult = new OnapCommandResult();
61
62     private List<String> defaultSchemas = new ArrayList<>();
63
64     protected boolean isInitialzied = false;
65
66     protected OnapCommand() {
67         this.addDefaultSchemas(OnapCommandConstants.DEFAULT_PARAMETER_FILE_NAME);
68     }
69
70     public List<String> getSchemas() {
71         List<String> schemas = new ArrayList<>();
72         schemas.addAll(this.defaultSchemas);
73         schemas.add(this.getSchemaName());
74         return schemas;
75     }
76
77     public String getSchemaVersion() {
78         return OnapCommandConstants.OPEN_CLI_SCHEMA_VERSION_VALUE_1_0;
79     }
80
81     public String getDescription() {
82         return this.cmdDescription;
83     }
84
85     public void setDescription(String description) {
86         this.cmdDescription = description;
87     }
88
89     public String getName() {
90         return this.cmdName;
91     }
92
93     public void setName(String name) {
94         this.cmdName = name;
95     }
96
97     public OnapCommandInfo getInfo() {
98         return info;
99     }
100
101     public void setInfo(OnapCommandInfo info) {
102         this.info = info;
103     }
104
105     public void setParameters(Set<OnapCommandParameter> parameters) {
106         this.cmdParameters = parameters;
107     }
108
109     public Set<OnapCommandParameter> getParameters() {
110         return this.cmdParameters;
111     }
112
113     public Map<String, OnapCommandParameter> getParametersMap() {
114         return OnapCommandUtils.getInputMap(this.getParameters());
115     }
116
117     public OnapCommandResult getResult() {
118         return this.cmdResult;
119     }
120
121     public void setResult(OnapCommandResult result) {
122         this.cmdResult = result;
123     }
124
125     public String getSchemaName() {
126         return cmdSchemaName;
127     }
128
129     protected void setSchemaName(String schemaName) {
130         this.cmdSchemaName = schemaName;
131     }
132
133     protected void addDefaultSchemas(String schema) {
134         this.defaultSchemas.add(schema);
135     }
136
137     public List<String> getDefaultSchema() {
138         return this.defaultSchemas;
139     }
140
141     /**
142      * Initialize this command from command schema and assumes schema is already validated.
143      *
144      * @throws OnapCommandException
145      *
146      * @return List of error strings
147      */
148     public List<String> initializeSchema(String schema) throws OnapCommandException {
149         return this.initializeSchema(schema, false);
150     }
151
152     public List<String> initializeSchema(String schema, boolean validate) throws OnapCommandException {
153         this.setSchemaName(schema);
154
155         Map<String, ?> schemaMap = OnapCommandSchemaMerger.mergeSchemas(this);
156         List<String> errors = OnapCommandSchemaLoader.parseSchema(this, schemaMap, validate);
157         errors.addAll(this.initializeProfileSchema(schemaMap, validate));
158         this.isInitialzied = true;
159
160         return errors;
161     }
162     /**
163      * Any additional profile based such as http schema could be initialized.
164      */
165     protected List<String> initializeProfileSchema(Map<String, ?> schemaMap, boolean validate) throws OnapCommandException {
166         return new ArrayList<>();
167     }
168
169     /*
170      * Validate input parameters. This can be overridden in derived commands
171      */
172     protected void validate() throws OnapCommandException {
173         for (OnapCommandParameter param : this.getParameters()) {
174              if (param.isInclude()) {
175                  param.validate();
176              }
177          }
178     }
179
180     protected void preRun() throws OnapCommandException {
181         log.debug(this.getName() + " PRE-RUN");
182     }
183
184     protected void postRun() throws OnapCommandException {
185         log.debug(this.getName() + " POST-RUN");
186     }
187     /**
188      * Oclip command execute with given parameters on service. Before calling this method, its mandatory to set all
189      * parameters value.
190      *
191      * @throws OnapCommandException
192      *             : General Command Exception
193      */
194     public OnapCommandResult execute() throws OnapCommandException {
195         if (!this.isInitialzied) {
196             throw new OnapCommandNotInitialized(this.getClass().getName());
197         }
198
199         log.info("CMD: " + this.getName());
200
201         Map<String, OnapCommandParameter> paramMap = this.getParametersMap();
202
203         log.info("INPUT: " + paramMap);
204
205         // -h or --help is always higher precedence !, user can set this value to get help message
206         if ((Boolean)(paramMap.get(OnapCommandConstants.DEFAULT_PARAMETER_HELP).getValue())) {
207             this.cmdResult.setType(OnapCommandResultType.TEXT);
208             this.cmdResult.setOutput(this.printHelp());
209             return this.cmdResult;
210         }
211
212         // -v or --version is next higher precedence !, user can set this value to get help message
213         if ((Boolean)(paramMap.get(OnapCommandConstants.DEFAULT_PARAMETER_VERSION).getValue())) {
214             this.cmdResult.setType(OnapCommandResultType.TEXT);
215             this.cmdResult.setOutput(this.printVersion());
216             return this.cmdResult;
217         }
218
219         // validate
220         this.validate();
221
222         // -f or --format
223         this.cmdResult.setType(
224                 OnapCommandResultType.get(paramMap.get(OnapCommandConstants.DEFAULT_PARAMETER_OUTPUT_FORMAT).getValue().toString()));
225         if (OnapCommandConstants.BOOLEAN_TRUE.equals(paramMap.get(OnapCommandConstants.DEFAULT_PARAMETER_OUTPUT_ATTR_LONG).getValue())) {
226             this.cmdResult.setScope(OnapCommandResultAttributeScope.LONG);
227         }
228         // --no-title
229         if ((Boolean)paramMap.get(OnapCommandConstants.DEFAULT_PARAMETER_OUTPUT_NO_TITLE).getValue()) {
230             this.cmdResult.setIncludeTitle(false);
231         }
232
233         // --debug
234         if ((Boolean)(paramMap.get(OnapCommandConstants.DEFAULT_PARAMETER_DEBUG).getValue())) {
235             this.cmdResult.setDebug(true);
236         }
237
238         //pre-process result attributes for spl entries and input parameters
239         for (OnapCommandResultAttribute attr: this.cmdResult.getRecords()) {
240             if (!attr.getDefaultValue().isEmpty()) {
241                 attr.setDefaultValue(OnapCommandUtils.replaceLineForSpecialValues(attr.getDefaultValue()));
242                 attr.setDefaultValue(OnapCommandUtils.replaceLineFromInputParameters(
243                         attr.getDefaultValue(), this.getParametersMap()));
244             }
245         }
246
247         preRun();
248
249         this.run();
250
251         log.info("OUTPUT: " + this.cmdResult.getRecords());
252
253         postRun();
254         return this.cmdResult;
255     }
256
257     /*
258      * Each command implements run method to executing the command.
259      *
260      */
261     protected abstract void run() throws OnapCommandException;
262
263     /**
264      * Returns the service service version it supports.
265      *
266      * @return version
267      */
268     public String printVersion() {
269         return this.getInfo().getProduct() + "::" + this.getInfo().getService();
270     }
271
272     /**
273      * Provides help message for this command.
274      *
275      * @return help message
276      * @throws OnapCommandHelpFailed
277      *             Failed to execute Help command.
278      */
279     public String printHelp() throws OnapCommandHelpFailed {
280         return OnapCommandHelperUtils.help(this);
281     }
282     // (mrkanag) Add toString for all command, parameter, result, etc objects in JSON format
283 }