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.HashMap;
23 import java.util.List;
25 import java.util.Map.Entry;
26 import java.util.Optional;
28 import org.apache.commons.io.IOUtils;
29 import org.onap.cli.fw.cmd.OnapCommand;
30 import org.onap.cli.fw.conf.OnapCommandConfig;
31 import org.onap.cli.fw.conf.OnapCommandConstants;
32 import org.onap.cli.fw.error.OnapCommandException;
33 import org.onap.cli.fw.error.OnapCommandHelpFailed;
34 import org.onap.cli.fw.error.OnapCommandInvalidSample;
35 import org.onap.cli.fw.error.OnapCommandWarning;
36 import org.onap.cli.fw.input.OnapCommandParameter;
37 import org.onap.cli.fw.output.OnapCommandPrintDirection;
38 import org.onap.cli.fw.output.OnapCommandResult;
39 import org.onap.cli.fw.output.OnapCommandResultAttribute;
40 import org.onap.cli.fw.output.OnapCommandResultAttributeScope;
41 import org.onap.cli.fw.output.OnapCommandResultType;
42 import org.onap.cli.fw.registrar.OnapCommandRegistrar;
43 import org.onap.cli.fw.store.OnapCommandExecutionStore;
44 import org.onap.cli.fw.store.OnapCommandExecutionStore.ExecutionStoreContext;
45 import org.onap.cli.fw.utils.OnapCommandDiscoveryUtils;
46 import org.onap.cli.main.conf.OnapCliConstants;
47 import org.onap.cli.main.interactive.StringCompleter;
48 import org.onap.cli.main.utils.OnapCliArgsParser;
49 import org.onap.cli.sample.yaml.SampleYamlGenerator;
50 import org.open.infc.grpc.Result;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
54 import jline.TerminalFactory;
55 import jline.console.ConsoleReader;
58 * OCLIP Command Line Interface (CLI).
61 public class OnapCli {
63 private static Logger log = LoggerFactory.getLogger(OnapCli.class);
65 private List<String> args = new ArrayList<>();
67 private List<String> argsParamFile = new ArrayList<>();
69 private String product = null;
71 private String profile = null;
73 private String paramFile = null;
75 private String rpcHost = null;
77 private String rpcPort = null;
79 private boolean printHelp = false;
81 private boolean printVersion = false;
83 private String requestId = null;
85 private String cmdName = null;
87 private int exitCode = -1;
89 public OnapCli(String[] args) {
96 public void resetExitCode() {
100 public void setArgs(String [] args) {
101 //--help --version --requestId --rpc-host xxx --rpc-port xxx --product xxx --profile xxx --param-file xxx CMD blah blah
103 int cmdIdx = 0; //index of CMD
104 while(args.length > cmdIdx) {
105 //no options given, directly command name invoked
106 if (!args[cmdIdx].startsWith("-")) {
110 if (args[cmdIdx].equals(OnapCommandParameter.printLongOption(OnapCommandConstants.RPC_PRODUCT))) {
111 this.product = args[++cmdIdx];
112 cmdIdx++; //move to next option
113 } else if (args[cmdIdx].equals(OnapCommandParameter.printLongOption(OnapCommandConstants.RPC_PROFILE))) {
114 this.profile = args[++cmdIdx];
115 cmdIdx++; //move to next option
116 } else if (args[cmdIdx].equals(OnapCommandParameter.printLongOption(OnapCommandConstants.RPC_HOST))) {
117 this.rpcHost = args[++cmdIdx];
118 cmdIdx++; //move to next option
119 } else if (args[cmdIdx].equals(OnapCommandParameter.printLongOption(OnapCommandConstants.RPC_PORT))) {
120 this.rpcPort = args[++cmdIdx];
121 cmdIdx++; //move to next option
122 } else if (args[cmdIdx].equals(OnapCommandParameter.printLongOption(OnapCliConstants.PARAM_PARAM_FILE_LONG))) {
123 this.paramFile = args[++cmdIdx];
124 cmdIdx++; //move to next option
125 } else if (args[cmdIdx].equals(OnapCommandParameter.printLongOption(OnapCommandConstants.RPC_REQID))) {
126 this.requestId = args[++cmdIdx];
127 cmdIdx++; //move to next option
128 } else if (args[cmdIdx].equals(OnapCommandParameter.printLongOption(OnapCliConstants.PARAM_HELP_LOGN)) ||
129 args[cmdIdx].equals(OnapCommandParameter.printShortOption(OnapCliConstants.PARAM_HELP_SHORT))) {
130 this.printHelp = true;
131 cmdIdx++; //move to next option
132 } else if (args[cmdIdx].equals(OnapCommandParameter.printLongOption(OnapCliConstants.PARAM_VERSION_LONG)) ||
133 args[cmdIdx].equals(OnapCommandParameter.printShortOption(OnapCliConstants.PARAM_VERSION_SHORT))) {
134 this.printVersion = true;
135 cmdIdx++; //move to next option
139 if (args.length > cmdIdx) {
140 this.cmdName = args[cmdIdx];
146 //add all args starting from the command name
147 for (int i=cmdIdx; i<args.length; i++) {
148 this.args.add(args[i]);
152 private void exitSuccessfully() {
153 this.exitCode = OnapCliConstants.EXIT_SUCCESS;
156 private void exitFailure() {
157 this.exitCode = OnapCliConstants.EXIT_FAILURE;
160 protected void print(String msg) {
161 System.out.println(msg); //NOSONAR
164 protected void printerr(String msg) {
165 System.err.println(msg); //NOSONAR
168 private void print(Throwable throwable) {
169 String error = throwable.getMessage() != null ? throwable.getMessage() : "";
171 log.error(error, throwable);
174 private String getShortOption(String opt) {
175 return OnapCommandParameter.printShortOption(opt);
178 private String getLongOption(String opt) {
179 return OnapCommandParameter.printLongOption(opt);
182 public int getExitCode() {
183 return this.exitCode;
187 * Handles help. --help or -h
189 public void handleHelp() {
191 if (this.printHelp) {
192 this.print(IOUtils.toString(this.getClass().getClassLoader().getResourceAsStream("oclip-readme.txt")));
193 String help = OnapCommandRegistrar.getRegistrar().getHelp();
195 this.exitSuccessfully();
197 } catch (Exception e) {
204 * Handles version. --version or -v
206 public void handleVersion() {
208 if (this.printVersion) {
209 String version = OnapCommandRegistrar.getRegistrar().getVersion();
211 this.exitSuccessfully();
213 } catch (Exception e) {
221 * Handles profile. --profile
223 public void handleProfile() {
225 if (this.profile != null) {
226 OnapCommandRegistrar.getRegistrar().setProfile(
231 } catch (Exception e) {
238 * Handles batch command. --param-file
240 public void handleBatchCommand() {
242 if (this.paramFile != null) {
243 //Read YAML and loop thru it
245 // - param-long-option-1: value
246 // - param-long-option-1: value
250 // - param-long-option-1: value
251 // - param-long-option-1: value
255 Map<String, Object> values = OnapCommandDiscoveryUtils.loadYaml(this.paramFile);
257 for (Entry<String, Object> cmdsParam: values.entrySet()) {
258 for (Object param: (List)cmdsParam.getValue()) {
259 if (param instanceof Map) { //optional args
260 Map <String, String> paramMap = (Map<String, String>) param;
261 String paramName = paramMap.keySet().iterator().next();
262 Object paramValue = paramMap.get(paramName);
263 argsParamFile.add(this.getLongOption(paramName));
264 argsParamFile.add(paramValue.toString());
265 } else { //positional args
266 argsParamFile.add(param.toString());
271 } catch (Exception e) { // NOSONAR
272 this.print("Failed to read param file " + this.paramFile);
276 } catch (Exception e) {
282 public void verifyCommand(OnapCommand cmd) throws OnapCommandException {
284 OnapCliArgsParser.populateParams(cmd.getParameters(), args);
286 Optional<OnapCommandParameter> contextOptArg = cmd.getParameters().stream()
287 .filter(e -> e.getName().equals(OnapCommandConstants.VERIFY_CONTEXT_PARAM))
290 List<Map<String, Object>> testSuite = OnapCommandRegistrar.getRegistrar().getTestSuite(
292 cmd.getInfo().getProduct());
294 OnapCommandResult testSuiteResult = new OnapCommandResult();
295 testSuiteResult.setType(OnapCommandResultType.TABLE);
296 testSuiteResult.setPrintDirection(OnapCommandPrintDirection.LANDSCAPE);
297 testSuiteResult.setIncludeTitle(true);
299 OnapCommandResultAttribute sampleFileAtt = new OnapCommandResultAttribute();
300 OnapCommandResultAttribute sampleIdAtt = new OnapCommandResultAttribute();
301 OnapCommandResultAttribute resultAtt = new OnapCommandResultAttribute();
303 sampleFileAtt.setName("Test");
304 sampleIdAtt.setName("SampleId");
305 resultAtt.setName("Result");
307 testSuiteResult.setRecords(Arrays.asList(sampleFileAtt,
311 for (Map<String, ?> sampleTest : testSuite) {
313 sampleFileAtt.getValues().add((String) sampleTest.get(OnapCommandConstants.VERIFY_SAMPLE_FILE_ID));
314 sampleIdAtt.getValues().add((String) sampleTest.get(OnapCommandConstants.VERIFY_SAMPLE_ID));
316 cmd = OnapCommandRegistrar.getRegistrar().get(cmd.getName(),
317 cmd.getInfo().getProduct());
318 List<String> arguments = (List<String>) sampleTest.get(OnapCommandConstants.VERIFY_INPUT);
320 OnapCliArgsParser.populateParams(cmd.getParameters(), arguments);
321 this.print("\n***************Test Command: \n" + sampleTest.get(OnapCommandConstants.VERIFY_INPUT).toString());
323 cmd.getParametersMap().get(OnapCommandConstants.DEFAULT_PARAMETER_DEBUG).setValue(Boolean.TRUE);
325 Optional<OnapCommandParameter> contextOpt = cmd.getParameters().stream()
326 .filter(e -> e.getName().equals(OnapCommandConstants.VERIFY_CONTEXT_PARAM))
329 if (contextOpt.isPresent()) {
330 HashMap<String, Object> map = new HashMap<>();
332 Object moco = sampleTest.get(OnapCommandConstants.VERIFY_MOCO);
336 map.put(OnapCommandConstants.VERIFY_MOCO, moco);
338 if (contextOptArg.isPresent()) {
339 OnapCommandParameter contextArg = contextOptArg.get();
340 map.putAll((Map) contextArg.getValue());
343 contextOpt.get().setValue(map);
346 OnapCommandResult testResult = cmd.execute();
347 String actualOutput = testResult.print().trim();
348 String expectedOutput = (String) sampleTest.get(OnapCommandConstants.VERIFY_OUPUT);
349 expectedOutput = expectedOutput == null ? "" : expectedOutput.trim();
351 if (actualOutput.equals(expectedOutput)) {
352 resultAtt.getValues().add(OnapCommandConstants.VERIFY_RESULT_PASS);
354 resultAtt.getValues().add(OnapCommandConstants.VERIFY_RESULT_FAIL);
356 this.printerr(testResult.getDebugInfo());
357 this.print("\n***************Expected Output: \n" + expectedOutput);
358 this.print("\n***************Actual Output: \n" + actualOutput);
361 this.print(testSuiteResult.print());
365 * Handles Interactive Mode.
367 public void handleInteractive() { // NOSONAR
368 if (this.cmdName == null) {
369 ConsoleReader console = null;
371 OnapCommandRegistrar.getRegistrar().setInteractiveMode(true);
372 console = createConsoleReader();
375 while ((line = console.readLine()) != null) {
377 if (OnapCliConstants.PARAM_INTERACTIVE_EXIT.equalsIgnoreCase(line)) {
379 } else if (OnapCliConstants.PARAM_INTERACTIVE_CLEAR.equalsIgnoreCase(line)) {
380 console.clearScreen();
382 this.args = new ArrayList<>();
383 this.args.addAll(Arrays.asList(line.split(OnapCliConstants.PARAM_INTERACTIVE_ARG_SPLIT_PATTERN)));
385 if (!args.isEmpty() && this.args.get(0).equals(OnapCliConstants.PARAM_INTERACTIVE_USE)) {
386 if (args.size() == 1) {
387 this.print("Please use it in the form of use <product-version>.\nSupported versions: " +
388 OnapCommandRegistrar.getRegistrar().getAvailableProductVersions());
390 OnapCommandRegistrar.getRegistrar().setEnabledProductVersion(args.get(1));
391 console = createConsoleReader();
394 } else if (!args.isEmpty() && this.args.get(0).equals(OnapCliConstants.PARAM_INTERACTIVE_HELP)) {
395 this.print(OnapCommandRegistrar.getRegistrar().getHelpForEnabledProductVersion());
396 this.print(OnapCli.getDirectiveHelp());
397 } else if (!args.isEmpty() && this.args.get(0).equals(OnapCliConstants.PARAM_INTERACTIVE_VERSION)) {
398 this.printVersion = true;
401 } else if (!args.isEmpty() && this.args.get(0).equals(OnapCliConstants.PARAM_INTERACTIVE_PROFILE)) {
402 if (args.size() == 1) {
403 this.print("Please use it in the form of 'profile <profile-name>'\n");
404 this.print("Available profiles: ");
405 this.print(OnapCommandRegistrar.getRegistrar().getUserProfiles().toString());
407 this.profile = args.get(1);
411 } else if (!args.isEmpty() && this.args.get(0).equals(OnapCliConstants.PARAM_INTERACTIVE_SET)) {
412 if (args.size() > 1) {
413 String[] paramEntry = args.get(1).trim().split("=", 2);
414 if (paramEntry.length == 2) {
415 OnapCommandRegistrar.getRegistrar().addParamCache(paramEntry[0].trim(), paramEntry[1].trim());
417 this.print("Please use it in the form of 'set <param-name>=<param-value>'");
420 this.print(OnapCommandRegistrar.getRegistrar().getParamCache().toString());
423 } else if (!args.isEmpty() && this.args.get(0).equals(OnapCliConstants.PARAM_INTERACTIVE_UNSET)) {
424 if (args.size() > 1) {
425 for (int i = 1; i < args.size(); i++) {
426 OnapCommandRegistrar.getRegistrar().removeParamCache(args.get(i));
430 if (!(args.size() == 1 && args.get(0).trim().isEmpty())) {
431 this.setArgs(this.args.toArray(new String[]{}));
437 } catch (IOException e) { // NOSONAR
438 this.print("Failed to read console, " + e.getMessage());
439 } catch (OnapCommandException e) {
444 TerminalFactory.get().restore();
445 } catch (Exception e) { // NOSONAR
447 this.exitSuccessfully();
455 public void handleCommand() {
456 OnapCommand cmd = null;
457 if (this.cmdName != null) {
459 if (this.product != null) {
460 cmd = OnapCommandRegistrar.getRegistrar().get(this.cmdName, this.product);
462 cmd = OnapCommandRegistrar.getRegistrar().get(this.cmdName);
464 } catch (Exception e) {
470 ExecutionStoreContext executionStoreContext = null;
473 //Registrar identified this command marked with rpc as true and it will make direct RPC command call...
474 if (cmd.isRpc() && !this.cmdName.equals("schema-rpc")) {
475 this.handleRpcCommand(cmd);
480 if(args.contains(OnapCommandConstants.VERIFY_LONG_OPTION)
481 || args.contains(OnapCommandConstants.VERIFY_SHORT_OPTION)) {
483 this.exitSuccessfully();
486 // check for help or version
487 if (args.size() == 2) {
488 if (this.getLongOption(OnapCliConstants.PARAM_HELP_LOGN).equals(args.get(1))
489 || this.getShortOption(OnapCliConstants.PARAM_HELP_SHORT).equals(args.get(1))) {
490 String help = cmd.printHelp();
492 this.exitSuccessfully();
494 } else if (this.getLongOption(OnapCliConstants.PARAM_VERSION_LONG).equals(args.get(1))
495 || this.getShortOption(OnapCliConstants.PARAM_VERSION_SHORT).equals(args.get(1))) {
496 String version = cmd.printVersion();
498 this.exitSuccessfully();
503 //refer params from profile
504 if (this.profile != null) {
506 Map<String, String> paramCache = new HashMap<>(); //NOSONAR
507 if (this.product == null)
508 paramCache = OnapCommandRegistrar.getRegistrar().getParamCache();
510 paramCache = OnapCommandRegistrar.getRegistrar().getParamCache(this.product);
512 for (OnapCommandParameter param: cmd.getParameters()) {
513 if (paramCache.containsKey(
514 cmd.getInfo().getService() + ":" + cmd.getName() + ":" + param.getLongOption())) {
515 param.setValue(paramCache.get(
516 cmd.getInfo().getService() + ":" + cmd.getName() + ":" + param.getLongOption()));
517 } else if (paramCache.containsKey(
518 cmd.getInfo().getService() + ":" + param.getLongOption())) {
519 param.setValue(paramCache.get(
520 cmd.getInfo().getService() + ":" + param.getLongOption()));
521 } else if (paramCache.containsKey(param.getLongOption())) {
522 param.setValue(paramCache.get(param.getLongOption()));
527 //load the parameters value from the map read from param-file
528 if (!this.argsParamFile.isEmpty()) {
529 OnapCliArgsParser.populateParams(cmd.getParameters(), this.argsParamFile);
532 OnapCliArgsParser.populateParams(cmd.getParameters(), this.args);
534 //start the execution
535 if (this.requestId != null && this.product != null && !this.requestId.isEmpty()&& !(this.product.equalsIgnoreCase("open-cli") &&
536 this.cmdName.equalsIgnoreCase("execution-list"))) {
537 String input = cmd.getArgsJson(true);
538 executionStoreContext = OnapCommandExecutionStore.getStore().storeExectutionStart(
540 cmd.getInfo().getProduct(),
541 cmd.getInfo().getService(),
547 cmd.setExecutionContext(executionStoreContext);
548 OnapCommandResult result = cmd.execute();
550 this.handleTracking(cmd);
552 if (result.isPassed()) {
553 this.exitSuccessfully();
554 generateSmapleYaml(cmd);
558 } catch (OnapCommandWarning w) {
559 if (cmd.getExecutionContext() != null) {
560 OnapCommandExecutionStore.getStore().storeExectutionEnd(
561 cmd.getExecutionContext(),
564 cmd.getResult().getDebugInfo(),
565 cmd.getResult().isPassed());
569 this.printerr(cmd.getResult().getDebugInfo());
570 this.exitSuccessfully();
571 } catch (Exception e) {
572 if (executionStoreContext != null) {
573 OnapCommandExecutionStore.getStore().storeExectutionEnd(
574 executionStoreContext,
577 cmd.getResult().getDebugInfo(),
582 this.printerr(cmd.getResult().getDebugInfo());
588 public void handleTracking(OnapCommand cmd) throws OnapCommandException {
589 if (cmd.getResult().isDebug())
590 this.printerr(cmd.getResult().getDebugInfo());
592 String printOut = cmd.getResult().print();
593 this.print(printOut);
595 if (cmd.getExecutionContext() != null) {
596 OnapCommandExecutionStore.getStore().storeExectutionEnd(
597 cmd.getExecutionContext(),
600 cmd.getResult().getDebugInfo(),
601 cmd.getResult().isPassed());
605 * When user invokes cli with RPC arguments...
607 public void handleRpc() {
608 if (this.rpcHost != null && this.rpcPort != null && this.product != null) {
610 OnapCommand cmd = OnapCommandRegistrar.getRegistrar().get("schema-rpc", "open-cli");
611 cmd.getParametersMap().get(OnapCommandConstants.RPC_HOST).setValue(this.rpcHost);
612 cmd.getParametersMap().get(OnapCommandConstants.RPC_PORT).setValue(this.rpcPort);
613 cmd.getParametersMap().get(OnapCommandConstants.RPC_PRODUCT).setValue(this.product);
614 cmd.getParametersMap().get(OnapCommandConstants.RPC_CMD).setValue(this.cmdName);
616 this.handleRpcCommand(cmd);
617 } catch (Exception e) {
624 private void handleRpcCommand(OnapCommand cmd) throws OnapCommandException {
625 Map<String, List<String>> argsMap = new HashMap<>();
626 argsMap.put(OnapCommandConstants.RPC_ARGS, this.args);
627 if (this.profile != null )
628 cmd.getParametersMap().get(OnapCommandConstants.RPC_PROFILE).setValue(this.profile);
629 cmd.getParametersMap().get(OnapCommandConstants.RPC_REQID).setValue(this.requestId);
630 cmd.getParametersMap().get(OnapCommandConstants.RPC_MODE).setValue(OnapCommandConstants.RPC_MODE_RUN_CLI);
631 cmd.getParametersMap().get(OnapCommandConstants.RPC_ARGS).setValue(argsMap);
633 OnapCommandResult result = cmd.execute();
634 Result output = (Result) result.getOutput();
636 this.exitCode = output.getExitCode();
637 this.print(output.getOutput());
640 * Handles all client input.
642 public void handle() {
645 if (this.exitCode == -1) {
649 if (this.exitCode == -1) {
650 this.handleVersion();
653 if (this.exitCode == -1) {
654 this.handleProfile();
657 if (this.exitCode == -1) {
658 this.handleBatchCommand();
661 if (this.exitCode == -1) {
662 this.handleInteractive();
665 if (this.exitCode == -1) {
666 this.handleCommand();
670 public static String getDirectiveHelp() throws OnapCommandHelpFailed {
671 OnapCommandResult help = new OnapCommandResult();
672 help.setType(OnapCommandResultType.TABLE);
673 help.setPrintDirection(OnapCommandPrintDirection.LANDSCAPE);
675 OnapCommandResultAttribute attr = new OnapCommandResultAttribute();
676 attr.setName(OnapCommandConstants.NAME.toUpperCase());
677 attr.setDescription(OnapCommandConstants.DESCRIPTION);
678 attr.setScope(OnapCommandResultAttributeScope.SHORT);
679 help.getRecords().add(attr);
681 OnapCommandResultAttribute attrDesc = new OnapCommandResultAttribute();
682 attrDesc.setName(OnapCommandConstants.DESCRIPTION.toUpperCase());
683 attrDesc.setDescription(OnapCommandConstants.DESCRIPTION);
684 attrDesc.setScope(OnapCommandResultAttributeScope.SHORT);
685 help.getRecords().add(attrDesc);
687 attr.getValues().add(OnapCliConstants.PARAM_INTERACTIVE_CLEAR);
688 attrDesc.getValues().add(OnapCliConstants.PARAM_INTERACTIVE_CLEAR_MSG);
690 attr.getValues().add(OnapCliConstants.PARAM_INTERACTIVE_EXIT);
691 attrDesc.getValues().add(OnapCliConstants.PARAM_INTERACTIVE_EXIT_MSG);
693 attr.getValues().add(OnapCliConstants.PARAM_INTERACTIVE_VERSION);
694 attrDesc.getValues().add(OnapCliConstants.PARAM_INTERACTIVE_VERSION_MSG);
696 attr.getValues().add(OnapCliConstants.PARAM_INTERACTIVE_USE);
697 attrDesc.getValues().add(OnapCliConstants.PARAM_INTERACTIVE_USE_MSG);
699 attr.getValues().add(OnapCliConstants.PARAM_INTERACTIVE_SET);
700 attrDesc.getValues().add(OnapCliConstants.PARAM_INTERACTIVE_SET_MSG);
702 attr.getValues().add(OnapCliConstants.PARAM_INTERACTIVE_UNSET);
703 attrDesc.getValues().add(OnapCliConstants.PARAM_INTERACTIVE_UNSET_MSG);
705 attr.getValues().add(OnapCliConstants.PARAM_INTERACTIVE_HELP);
706 attrDesc.getValues().add(OnapCliConstants.PARAM_INTERACTIVE_HELP_MSG);
708 attr.getValues().add(OnapCliConstants.PARAM_INTERACTIVE_PROFILE);
709 attrDesc.getValues().add(OnapCliConstants.PARAM_INTERACTIVE_PROFILE_MSG);
711 return "\n\nDirectives:\n" + help.print();
712 } catch (OnapCommandException e) {
713 throw new OnapCommandHelpFailed(e);
718 * Creates console reader object.
720 * @return ConsoleReader
721 * @throws IOException
724 private ConsoleReader createConsoleReader() throws IOException {
725 ConsoleReader console = new ConsoleReader(); // NOSONAR
727 //ignore system commands
728 StringCompleter strCompleter = new StringCompleter(OnapCommandRegistrar.getRegistrar().listCommandsForEnabledProductVersion());
729 strCompleter.add(OnapCliConstants.PARAM_INTERACTIVE_EXIT,
730 OnapCliConstants.PARAM_INTERACTIVE_CLEAR,
731 OnapCliConstants.PARAM_INTERACTIVE_USE,
732 OnapCliConstants.PARAM_INTERACTIVE_HELP,
733 OnapCliConstants.PARAM_INTERACTIVE_VERSION,
734 OnapCliConstants.PARAM_INTERACTIVE_SET,
735 OnapCliConstants.PARAM_INTERACTIVE_UNSET,
736 OnapCliConstants.PARAM_INTERACTIVE_PROFILE);
737 console.addCompleter(strCompleter);
738 console.setPrompt(OnapCliConstants.PARAM_INTERACTIVE_PROMPT + ":" + OnapCommandRegistrar.getRegistrar().getEnabledProductVersion() + ">");
739 } catch (OnapCommandException e) { // NOSONAR
740 this.print("Failed to load oclip commands," + e.getMessage());
747 private void generateSmapleYaml(OnapCommand cmd) throws OnapCommandException {
748 if (Boolean.parseBoolean(OnapCommandConfig.getPropertyValue(OnapCommandConstants.SAMPLE_GEN_ENABLED)) && this.getExitCode() == OnapCliConstants.EXIT_SUCCESS) {
750 SampleYamlGenerator.generateSampleYaml(cmd.getName(), args, cmd.getResult().print(),
751 cmd.getInfo().getProduct(),
752 OnapCommandConfig.getPropertyValue(OnapCommandConstants.SAMPLE_GEN_TARGET_FOLDER) + "/" + cmd.getSchemaName().replaceAll(".yaml", "") + "-sample.yaml",
753 cmd.getResult().isDebug(),
754 OnapCommandConfig.getPropertyValue(OnapCommandConstants.SAMPLE_GEN_NAME));
755 } catch (IOException error) {
756 throw new OnapCommandInvalidSample(this.cmdName, error);
767 public static void main(String[] args) {
768 OnapCli cli = new OnapCli(args); //NOSONAR
770 System.exit(cli.getExitCode());