Merge default params files
[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() throws OnapCommandException {
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
153     public List<String> initializeSchema(String schema, boolean validate) throws OnapCommandException {
154         this.setSchemaName(schema);
155
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;
160
161         return errors;
162     }
163     /**
164      * Any additional profile based such as http schema could be initialized.
165      */
166     protected List<String> initializeProfileSchema(Map<String, ?> schemaMap, boolean validate) throws OnapCommandException {
167         return new ArrayList<>();
168     }
169
170     /*
171      * Validate input parameters. This can be overridden in derived commands
172      */
173     protected void validate() throws OnapCommandException {
174         for (OnapCommandParameter param : this.getParameters()) {
175              if (param.isInclude()) {
176                  param.validate();
177              }
178          }
179     }
180
181     /**
182      * Oclip command execute with given parameters on service. Before calling this method, its mandatory to set all
183      * parameters value.
184      *
185      * @throws OnapCommandException
186      *             : General Command Exception
187      */
188     public OnapCommandResult execute() throws OnapCommandException {
189         if (!this.isInitialzied) {
190             throw new OnapCommandNotInitialized(this.getClass().getName());
191         }
192
193         LOG.info("CMD: " + this.getName());
194
195         Map<String, OnapCommandParameter> paramMap = this.getParametersMap();
196
197         LOG.info("INPUT: " + paramMap);
198
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;
204         }
205
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;
211         }
212
213         // validate
214         this.validate();
215
216         // -f or --format
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);
221         }
222         // --no-title
223         if (OnapCommandConstants.BOOLEAN_TRUE.equals(paramMap.get(OnapCommandConstants.DEFAULT_PARAMETER_OUTPUT_NO_TITLE).getValue())) {
224             this.cmdResult.setIncludeTitle(false);
225         }
226
227         // --debug
228         if (OnapCommandConstants.BOOLEAN_TRUE.equals(paramMap.get(OnapCommandConstants.DEFAULT_PARAMETER_DEBUG).getValue())) {
229             this.cmdResult.setDebug(true);
230         }
231
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()));
238             }
239         }
240
241         this.run();
242
243         LOG.info("OUTPUT: " + this.cmdResult.getRecords());
244
245         return this.cmdResult;
246     }
247
248     /*
249      * Each command implements run method to executing the command.
250      *
251      */
252     protected abstract void run() throws OnapCommandException;
253
254     /**
255      * Returns the service service version it supports.
256      *
257      * @return version
258      */
259     public String printVersion() {
260         return this.getInfo().getProduct() + "::" + this.getInfo().getService();
261     }
262
263     /**
264      * Provides help message for this command.
265      *
266      * @return help message
267      * @throws OnapCommandHelpFailed
268      *             Failed to execute Help command.
269      */
270     public String printHelp() throws OnapCommandHelpFailed {
271         return OnapCommandHelperUtils.help(this);
272     }
273
274     // (mrkanag) Add toString for all command, parameter, result, etc objects in JSON format
275 }