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.main;
19 import java.io.IOException;
20 import java.util.ArrayList;
21 import java.util.Arrays;
22 import java.util.List;
24 import org.apache.commons.io.IOUtils;
25 import org.onap.cli.fw.cmd.OnapCommand;
26 import org.onap.cli.fw.conf.OnapCommandConfig;
27 import org.onap.cli.fw.conf.OnapCommandConstants;
28 import org.onap.cli.fw.error.OnapCommandException;
29 import org.onap.cli.fw.error.OnapCommandHelpFailed;
30 import org.onap.cli.fw.error.OnapCommandInvalidSample;
31 import org.onap.cli.fw.error.OnapCommandWarning;
32 import org.onap.cli.fw.input.OnapCommandParameter;
33 import org.onap.cli.fw.output.OnapCommandPrintDirection;
34 import org.onap.cli.fw.output.OnapCommandResult;
35 import org.onap.cli.fw.output.OnapCommandResultAttribute;
36 import org.onap.cli.fw.output.OnapCommandResultAttributeScope;
37 import org.onap.cli.fw.output.OnapCommandResultType;
38 import org.onap.cli.fw.registrar.OnapCommandRegistrar;
39 import org.onap.cli.main.conf.OnapCliConstants;
40 import org.onap.cli.main.interactive.StringCompleter;
41 import org.onap.cli.main.utils.OnapCliArgsParser;
42 import org.onap.cli.sample.yaml.SampleYamlGenerator;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
46 import jline.TerminalFactory;
47 import jline.console.ConsoleReader;
50 * Oclip Command Line Interface (CLI).
53 public class OnapCli {
55 private static Logger LOG = LoggerFactory.getLogger(OnapCli.class);
57 private List<String> args = new ArrayList<>();
59 private String product = null;
61 private int exitCode = -1;
63 public OnapCli(String[] args) {
64 this.args = Arrays.asList(args);
67 public OnapCli(String product, String[] args) {
69 this.product = product;
72 private void exitSuccessfully() {
73 this.exitCode = OnapCliConstants.EXIT_SUCCESS;
76 private void exitFailure() {
77 this.exitCode = OnapCliConstants.EXIT_FAILURE;
80 protected void print(String msg) {
81 System.out.println(msg);
84 private void print(Throwable throwable) {
85 this.print(throwable.getMessage());
86 LOG.error(throwable.getMessage(), throwable);
89 private String getShortOption(String opt) {
90 return OnapCommandParameter.printShortOption(opt);
93 private String getLongOption(String opt) {
94 return OnapCommandParameter.printLongOption(opt);
97 public int getExitCode() {
102 * Handles help. --help or -h
104 public void handleHelp() {
106 if ((args.size() == 1) && (this.getLongOption(OnapCliConstants.PARAM_HELP_LOGN).equals(args.get(0))
107 || this.getShortOption(OnapCliConstants.PARAM_HELP_SHORT).equals(args.get(0)))) {
108 this.print(IOUtils.toString(this.getClass().getClassLoader().getResourceAsStream("oclip-readme.txt")));
109 String help = OnapCommandRegistrar.getRegistrar().getHelp();
111 this.exitSuccessfully();
113 } catch (Exception e) {
120 * Handles version. --version or -v
122 public void handleVersion() {
124 if ((args.size() == 1) && (this.getLongOption(OnapCliConstants.PARAM_VERSION_LONG).equals(args.get(0))
125 || this.getShortOption(OnapCliConstants.PARAM_VERSION_SHORT).equals(args.get(0)))) {
126 String version = OnapCommandRegistrar.getRegistrar().getVersion();
128 this.exitSuccessfully();
130 } catch (Exception e) {
138 * Handles profile. --profile or -c
140 public void handleProfile() {
142 if ((args.size() == 2) && (this.getLongOption(OnapCliConstants.PARAM_PROFILE_LONG).equals(args.get(0))
143 || this.getShortOption(OnapCliConstants.PARAM_PROFILE_SHORT).equals(args.get(0)))) {
145 OnapCommandRegistrar.getRegistrar().setProfile(args.get(1));
146 //Make space of interactive mode
147 this.args = new ArrayList<>();
149 } catch (Exception e) {
155 public static String getDirectiveHelp() throws OnapCommandHelpFailed {
156 OnapCommandResult help = new OnapCommandResult();
157 help.setType(OnapCommandResultType.TABLE);
158 help.setPrintDirection(OnapCommandPrintDirection.LANDSCAPE);
160 OnapCommandResultAttribute attr = new OnapCommandResultAttribute();
161 attr.setName(OnapCommandConstants.NAME.toUpperCase());
162 attr.setDescription(OnapCommandConstants.DESCRIPTION);
163 attr.setScope(OnapCommandResultAttributeScope.SHORT);
164 help.getRecords().add(attr);
166 OnapCommandResultAttribute attrDesc = new OnapCommandResultAttribute();
167 attrDesc.setName(OnapCommandConstants.DESCRIPTION.toUpperCase());
168 attrDesc.setDescription(OnapCommandConstants.DESCRIPTION);
169 attrDesc.setScope(OnapCommandResultAttributeScope.SHORT);
170 help.getRecords().add(attrDesc);
172 attr.getValues().add(OnapCliConstants.PARAM_INTERACTIVE_CLEAR);
173 attrDesc.getValues().add(OnapCliConstants.PARAM_INTERACTIVE_CLEAR_MSG);
175 attr.getValues().add(OnapCliConstants.PARAM_INTERACTIVE_EXIT);
176 attrDesc.getValues().add(OnapCliConstants.PARAM_INTERACTIVE_EXIT_MSG);
178 attr.getValues().add(OnapCliConstants.PARAM_INTERACTIVE_VERSION);
179 attrDesc.getValues().add(OnapCliConstants.PARAM_INTERACTIVE_VERSION_MSG);
181 attr.getValues().add(OnapCliConstants.PARAM_INTERACTIVE_USE);
182 attrDesc.getValues().add(OnapCliConstants.PARAM_INTERACTIVE_USE_MSG);
184 attr.getValues().add(OnapCliConstants.PARAM_INTERACTIVE_SET);
185 attrDesc.getValues().add(OnapCliConstants.PARAM_INTERACTIVE_SET_MSG);
187 attr.getValues().add(OnapCliConstants.PARAM_INTERACTIVE_UNSET);
188 attrDesc.getValues().add(OnapCliConstants.PARAM_INTERACTIVE_UNSET_MSG);
190 attr.getValues().add(OnapCliConstants.PARAM_INTERACTIVE_HELP);
191 attrDesc.getValues().add(OnapCliConstants.PARAM_INTERACTIVE_HELP_MSG);
193 attr.getValues().add(OnapCliConstants.PARAM_INTERACTIVE_PROFILE);
194 attrDesc.getValues().add(OnapCliConstants.PARAM_INTERACTIVE_PROFILE_MSG);
196 return "\n\nDirectives:\n" + help.print();
197 } catch (OnapCommandException e) {
198 throw new OnapCommandHelpFailed(e);
202 * Handles Interactive Mode.
204 public void handleInteractive() { // NOSONAR
205 if (args.isEmpty()) {
206 ConsoleReader console = null;
208 OnapCommandRegistrar.getRegistrar().setInteractiveMode(true);
209 console = createConsoleReader();
212 while ((line = console.readLine()) != null) {
214 if (OnapCliConstants.PARAM_INTERACTIVE_EXIT.equalsIgnoreCase(line)) {
216 } else if (OnapCliConstants.PARAM_INTERACTIVE_CLEAR.equalsIgnoreCase(line)) {
217 console.clearScreen();
220 this.args = Arrays.asList(line.split(OnapCliConstants.PARAM_INTERACTIVE_ARG_SPLIT_PATTERN));
222 if (!args.isEmpty() && this.args.get(0).equals(OnapCliConstants.PARAM_INTERACTIVE_USE)) {
223 if (args.size() == 1) {
224 this.print("Please input the product version to use, supported versions: " +
225 OnapCommandRegistrar.getRegistrar().getAvailableProductVersions());
228 OnapCommandRegistrar.getRegistrar().setEnabledProductVersion(args.get(1));
230 console = createConsoleReader();
231 } catch (OnapCommandException e) {
235 } else if (!args.isEmpty() && this.args.get(0).equals(OnapCliConstants.PARAM_INTERACTIVE_HELP)) {
237 this.print(OnapCommandRegistrar.getRegistrar().getHelpForEnabledProductVersion());
238 this.print(this.getDirectiveHelp());
239 } catch (OnapCommandException e) {
242 } else if (!args.isEmpty() && this.args.get(0).equals(OnapCliConstants.PARAM_INTERACTIVE_VERSION)) {
243 this.args = Arrays.asList(new String [] {this.getLongOption(OnapCliConstants.PARAM_VERSION_LONG)});
245 } else if (!args.isEmpty() && this.args.get(0).equals(OnapCliConstants.PARAM_INTERACTIVE_PROFILE)) {
246 if (args.size() == 1) {
247 this.print("Please use it in the form of 'profile <profile-name>'");
249 this.args = Arrays.asList(new String [] {
250 this.getLongOption(OnapCliConstants.PARAM_PROFILE_LONG),
254 } else if (!args.isEmpty() && this.args.get(0).equals(OnapCliConstants.PARAM_INTERACTIVE_SET)) {
255 if (args.size() > 1) {
256 String [] paramEntry = args.get(1).trim().split("=");
257 if (paramEntry.length >= 2) {
258 OnapCommandRegistrar.getRegistrar().addParamCache(paramEntry[0].trim(), paramEntry[1].trim());
260 this.print("Please use it in the form of 'set param-name=param-value'");
263 this.print(OnapCommandRegistrar.getRegistrar().getParamCache().toString());
265 } else if (!args.isEmpty() && this.args.get(0).equals(OnapCliConstants.PARAM_INTERACTIVE_UNSET)) {
266 if (args.size() > 1) {
267 for (int i = 1; i <args.size(); i++) {
268 OnapCommandRegistrar.getRegistrar().removeParamCache(args.get(i));
272 if (args.size() == 1 && args.get(0).trim().isEmpty()) {
273 //Ignore blanks // NOSONAR
280 } catch (IOException e) { // NOSONAR
281 this.print("Failed to read console, " + e.getMessage());
282 } catch (OnapCommandException e) {
287 TerminalFactory.get().restore();
288 } catch (Exception e) { // NOSONAR
290 if (console != null) {
293 this.exitSuccessfully();
299 * Creates console reader object.
301 * @return ConsoleReader
302 * @throws IOException
305 private ConsoleReader createConsoleReader() throws IOException {
306 try(ConsoleReader console = new ConsoleReader()){
308 StringCompleter strCompleter = new StringCompleter(OnapCommandRegistrar.getRegistrar().listCommandsForEnabledProductVersion());
309 strCompleter.add(OnapCliConstants.PARAM_INTERACTIVE_EXIT,
310 OnapCliConstants.PARAM_INTERACTIVE_CLEAR,
311 OnapCliConstants.PARAM_INTERACTIVE_USE,
312 OnapCliConstants.PARAM_INTERACTIVE_HELP,
313 OnapCliConstants.PARAM_INTERACTIVE_VERSION,
314 OnapCliConstants.PARAM_INTERACTIVE_SET,
315 OnapCliConstants.PARAM_INTERACTIVE_UNSET,
316 OnapCliConstants.PARAM_INTERACTIVE_PROFILE);
317 console.addCompleter(strCompleter);
318 console.setPrompt(OnapCliConstants.PARAM_INTERACTIVE_PROMPT + ":" + OnapCommandRegistrar.getRegistrar().getEnabledProductVersion() + ">");
319 } catch (OnapCommandException e) { // NOSONAR
320 this.print("Failed to load oclip commands," + e.getMessage());
329 public void handleCommand() {
331 if (!args.isEmpty()) {
333 if (this.product != null) {
334 cmd = OnapCommandRegistrar.getRegistrar().get(args.get(0), this.product);
336 cmd = OnapCommandRegistrar.getRegistrar().get(args.get(0));
338 } catch (Exception e) {
344 // check for help or version
345 if (args.size() == 2) {
346 if (this.getLongOption(OnapCliConstants.PARAM_HELP_LOGN).equals(args.get(1))
347 || this.getShortOption(OnapCliConstants.PARAM_HELP_SHORT).equals(args.get(1))) {
348 String help = cmd.printHelp();
350 this.exitSuccessfully();
352 } else if (this.getLongOption(OnapCliConstants.PARAM_VERSION_LONG).equals(args.get(1))
353 || this.getShortOption(OnapCliConstants.PARAM_VERSION_SHORT).equals(args.get(1))) {
354 String version = cmd.printVersion();
356 this.exitSuccessfully();
361 for (OnapCommandParameter param: cmd.getParameters()) {
362 if (OnapCommandRegistrar.getRegistrar().getParamCache().containsKey(
363 cmd.getInfo().getService() + ":" + param.getLongOption())) {
364 param.setValue(OnapCommandRegistrar.getRegistrar().getParamCache().get(
365 cmd.getInfo().getService() + ":" + param.getLongOption()));
366 } else if (OnapCommandRegistrar.getRegistrar().getParamCache().containsKey(param.getLongOption())) {
367 param.setValue(OnapCommandRegistrar.getRegistrar().getParamCache().get(param.getLongOption()));
371 OnapCliArgsParser.populateParams(cmd.getParameters(), args);
372 OnapCommandResult result = cmd.execute();
374 this.print(result.getDebugInfo());
375 this.print(result.print());
376 this.exitSuccessfully();
378 generateSmapleYaml(cmd);
379 } catch (OnapCommandWarning w) {
381 this.print(cmd.getResult().getDebugInfo());
382 this.exitSuccessfully();
383 } catch (Exception e) {
385 this.print(cmd.getResult().getDebugInfo());
391 private void generateSmapleYaml(OnapCommand cmd) throws OnapCommandException {
392 if (Boolean.parseBoolean(OnapCommandConfig.getPropertyValue(OnapCommandConstants.SAMPLE_GEN_ENABLED)) && this.getExitCode() == OnapCliConstants.EXIT_SUCCESS) {
394 SampleYamlGenerator.generateSampleYaml(args, cmd.getResult().print(),
395 OnapCommandRegistrar.getRegistrar().getEnabledProductVersion(),
396 OnapCommandConfig.getPropertyValue(OnapCommandConstants.SAMPLE_GEN_TARGET_FOLDER) + "/" + cmd.getSchemaName().replaceAll(".yaml", "") + "-sample.yaml",
397 cmd.getResult().isDebug());
398 } catch (IOException error) {
399 throw new OnapCommandInvalidSample(args.get(0), error);
405 * Handles all client input.
407 public void handle() {
410 if (this.exitCode == -1) {
411 this.handleVersion();
414 if (this.exitCode == -1) {
415 this.handleProfile();
418 if (this.exitCode == -1) {
419 this.handleInteractive();
422 if (this.exitCode == -1) {
423 this.handleCommand();
433 public static void main(String[] args) {
434 OnapCli cli = new OnapCli(args);
436 System.exit(cli.getExitCode());