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;
19 import org.onap.cli.fw.ad.OnapAuthClient;
20 import org.onap.cli.fw.ad.OnapCredentials;
21 import org.onap.cli.fw.ad.OnapService;
22 import org.onap.cli.fw.conf.Constants;
23 import org.onap.cli.fw.conf.OnapCommandConfg;
24 import org.onap.cli.fw.error.OnapCommandException;
25 import org.onap.cli.fw.error.OnapCommandHelpFailed;
26 import org.onap.cli.fw.error.OnapCommandInvalidParameterType;
27 import org.onap.cli.fw.error.OnapCommandInvalidPrintDirection;
28 import org.onap.cli.fw.error.OnapCommandInvalidResultAttributeScope;
29 import org.onap.cli.fw.error.OnapCommandInvalidSchema;
30 import org.onap.cli.fw.error.OnapCommandInvalidSchemaVersion;
31 import org.onap.cli.fw.error.OnapCommandNotInitialized;
32 import org.onap.cli.fw.error.OnapCommandParameterMissing;
33 import org.onap.cli.fw.error.OnapCommandParameterNameConflict;
34 import org.onap.cli.fw.error.OnapCommandParameterOptionConflict;
35 import org.onap.cli.fw.error.OnapCommandRegistrationFailed;
36 import org.onap.cli.fw.error.OnapCommandSchemaNotFound;
37 import org.onap.cli.fw.input.OnapCommandParameter;
38 import org.onap.cli.fw.output.OnapCommandResult;
39 import org.onap.cli.fw.output.OnapCommandResultAttributeScope;
40 import org.onap.cli.fw.output.ResultType;
41 import org.onap.cli.fw.utils.OnapCommandUtils;
43 import java.util.ArrayList;
44 import java.util.List;
52 public abstract class OnapCommand {
54 private String cmdDescription;
56 private String cmdName;
58 private String cmdSchemaName;
60 private OnapService onapService = new OnapService();
62 private List<OnapCommandParameter> cmdParameters = new ArrayList<>();
64 private OnapCommandResult cmdResult = new OnapCommandResult();
66 protected OnapAuthClient authClient;
68 protected boolean isInitialzied = false;
70 public String getSchemaVersion() {
71 return Constants.ONAP_CMD_SCHEMA_VERSION_VALUE;
75 * Onap command description, defined by derived command.
77 public String getDescription() {
78 return this.cmdDescription;
81 public void setDescription(String description) {
82 this.cmdDescription = description;
86 * Onap command name like user-create, ns-list, etc , defined by derived command
88 public String getName() {
92 public void setName(String name) {
96 public boolean isCommandInternal() {
97 return onapService.getName() != null
98 && onapService.getName().equalsIgnoreCase(OnapCommandConfg.getInternalCmd());
102 * Onap service, this command uses to execute it. , defined by derived command
104 public OnapService getService() {
105 return this.onapService;
108 public void setService(OnapService service) {
109 this.onapService = service;
112 public void setParameters(List<OnapCommandParameter> parameters) {
113 this.cmdParameters = parameters;
117 * Onap command input parameters, defined by derived command
119 public List<OnapCommandParameter> getParameters() {
120 return this.cmdParameters;
124 * Onap command input parameters, defined by derived command
126 public Map<String, OnapCommandParameter> getParametersMap() {
127 return OnapCommandUtils.getInputMap(this.getParameters());
131 * Onap command output results, defined by derived command
133 public OnapCommandResult getResult() {
134 return this.cmdResult;
137 public void setResult(OnapCommandResult result) {
138 this.cmdResult = result;
141 public String getSchemaName() {
142 return cmdSchemaName;
145 protected void setSchemaName(String schemaName) {
146 this.cmdSchemaName = schemaName;
150 * Initialize this command from command schema.
152 * @throws OnapCommandRegistrationFailed
153 * Command Registration Exception
154 * @throws OnapCommandInvalidResultAttributeScope
155 * InvalidResultAttribute Exception
156 * @throws OnapCommandInvalidPrintDirection
157 * InvalidPrintDirection Exception
158 * @throws OnapCommandInvalidParameterType
159 * InvalidParameterType Exception
160 * @throws OnapCommandSchemaNotFound
161 * SchemaNotFound Exception
162 * @throws OnapCommandInvalidSchema
163 * InvalidSchema Exception
164 * @throws OnapCommandParameterOptionConflict
165 * ParameterOptionConflict Exception
166 * @throws OnapCommandParameterNameConflict
167 * ParameterNameConflict Exception
168 * @throws OnapCommandInvalidSchemaVersion
169 * InvalidSchemaVersion Exception
171 public void initializeSchema(String schema) throws OnapCommandException {
172 this.setSchemaName(schema);
173 OnapCommandUtils.loadSchema(this, schema, true);
174 this.initializeProfileSchema();
175 this.isInitialzied = true;
179 * Any additional profile based such as http/swagger schema could be initialized.
181 protected void initializeProfileSchema() throws OnapCommandException {
185 * Validate input parameters. This can be overridden in derived commands
187 protected void validate() throws OnapCommandException {
188 for (OnapCommandParameter param : this.getParameters()) {
191 } catch (OnapCommandParameterMissing e) {
192 if (OnapCommandConfg.getExcludeParamsForNoAuthEnableExternalCmd().contains(param.getName())) {
193 OnapCommandParameter noAuthParam = this.getParameters().stream().filter(p -> p.getName()
194 .equalsIgnoreCase(Constants.DEFAULT_PARAMETER_OUTPUT_NO_AUTH)).findFirst().get();
196 if ("true".equalsIgnoreCase(noAuthParam.getValue().toString())) {
201 } catch (OnapCommandException e) {
209 * Onap command execute with given parameters on service. Before calling this method, its mandatory to set all
212 * @throws OnapCommandException
213 * : General Command Exception
215 public OnapCommandResult execute() throws OnapCommandException {
216 if (!this.isInitialzied) {
217 throw new OnapCommandNotInitialized(this.getClass().getName());
220 Map<String, OnapCommandParameter> paramMap = this.getParametersMap();
222 // -h or --help is always higher precedence !, user can set this value to get help message
223 if ("true".equals(paramMap.get(Constants.DEFAULT_PARAMETER_HELP).getValue())) {
224 OnapCommandResult result = new OnapCommandResult();
225 result.setType(ResultType.TEXT);
226 result.setOutput(this.printHelp());
230 // -v or --version is next higher precedence !, user can set this value to get help message
231 if ("true".equals(paramMap.get(Constants.DEFAULT_PARAMETER_VERSION).getValue())) {
232 OnapCommandResult result = new OnapCommandResult();
233 result.setType(ResultType.TEXT);
234 result.setOutput(this.printVersion());
242 this.cmdResult.setType(
243 ResultType.get(paramMap.get(Constants.DEFAULT_PARAMETER_OUTPUT_FORMAT).getValue().toString()));
244 if ("true".equals(paramMap.get(Constants.DEFAULT_PARAMETER_OUTPUT_ATTR_LONG).getValue())) {
245 this.cmdResult.setScope(OnapCommandResultAttributeScope.LONG);
248 if ("true".equals(paramMap.get(Constants.DEFAULT_PARAMETER_OUTPUT_NO_TITLE).getValue())) {
249 this.cmdResult.setIncludeTitle(false);
253 if ("true".equals(paramMap.get(Constants.DEFAULT_PARAMETER_DEBUG).getValue())) {
254 this.cmdResult.setDebug(true);
258 OnapCredentials creds = OnapCommandUtils.fromParameters(this.getParameters());
259 boolean isAuthRequired = !this.onapService.isNoAuth()
260 && "true".equals(paramMap.get(Constants.DEFAULT_PARAMETER_OUTPUT_NO_AUTH).getValue());
262 if (!isCommandInternal()) {
263 this.authClient = new OnapAuthClient(creds, this.getResult().isDebug());
266 if (isAuthRequired) {
267 this.authClient.login();
272 if (isAuthRequired) {
273 this.authClient.logout();
276 if (this.cmdResult.isDebug() && authClient != null) {
277 this.cmdResult.setDebugInfo(this.authClient.getDebugInfo());
279 } catch (OnapCommandException e) {
280 if (this.cmdResult.isDebug() && authClient != null) {
281 this.cmdResult.setDebugInfo(this.authClient.getDebugInfo());
286 return this.cmdResult;
290 * Each command implements run method to executing the command.
293 protected abstract void run() throws OnapCommandException;
296 * Get my service base path (endpoint).
298 protected String getBasePath() throws OnapCommandException {
299 return this.authClient.getServiceBasePath(this.getService());
302 protected String getAuthToken() {
303 return this.authClient.getAuthToken();
307 * Returns the service service version it supports.
311 public String printVersion() {
312 return this.getService().toString();
316 * Provides help message for this command.
318 * @return help message
319 * @throws OnapCommandHelpFailed
320 * Failed to execute Help command.
322 public String printHelp() throws OnapCommandHelpFailed {
323 return OnapCommandUtils.help(this);
325 // (mrkanag) Add toString for all command, parameter, result, etc objects in JSON format