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.OnapCommand;
26 import org.onap.cli.fw.OnapCommandRegistrar;
27 import org.onap.cli.fw.conf.Constants;
28 import org.onap.cli.fw.conf.OnapCommandConfg;
29 import org.onap.cli.fw.error.OnapCommandException;
30 import org.onap.cli.fw.error.OnapCommandHelpFailed;
31 import org.onap.cli.fw.error.OnapCommandInvalidSample;
32 import org.onap.cli.fw.error.OnapCommandWarning;
33 import org.onap.cli.fw.input.OnapCommandParameter;
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.PrintDirection;
38 import org.onap.cli.fw.output.ResultType;
39 import org.onap.cli.main.conf.OnapCliConstants;
40 import org.onap.cli.main.interactive.StringCompleter;
41 import org.onap.cli.main.utils.OnapCliUtils;
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 int exitCode = -1;
61 public OnapCli(String[] args) {
62 this.args = Arrays.asList(args);
65 private void exitSuccessfully() {
66 this.exitCode = OnapCliConstants.EXIT_SUCCESS;
69 private void exitFailure() {
70 this.exitCode = OnapCliConstants.EXIT_FAILURE;
73 private void print(String msg) {
74 System.out.println(msg);
77 private void print(Throwable throwable) {
78 this.print(throwable.getMessage());
79 LOG.error(throwable.getMessage(), throwable);
82 private String getShortOption(String opt) {
83 return OnapCommandParameter.printShortOption(opt);
86 private String getLongOption(String opt) {
87 return OnapCommandParameter.printLongOption(opt);
90 public int getExitCode() {
95 * Handles help. --help or -h
97 public void handleHelp() {
99 if ((args.size() == 1) && (this.getLongOption(OnapCliConstants.PARAM_HELP_LOGN).equals(args.get(0))
100 || this.getShortOption(OnapCliConstants.PARAM_HELP_SHORT).equals(args.get(0)))) {
101 this.print(IOUtils.toString(this.getClass().getClassLoader().getResourceAsStream("oclip-readme.txt")));
102 String help = OnapCommandRegistrar.getRegistrar().getHelp();
104 this.exitSuccessfully();
106 } catch (Exception e) {
113 * Handles version. --version or -v
115 public void handleVersion() {
117 if ((args.size() == 1) && (this.getLongOption(OnapCliConstants.PARAM_VERSION_LONG).equals(args.get(0))
118 || this.getShortOption(OnapCliConstants.PARAM_VERSION_SHORT).equals(args.get(0)))) {
119 String version = OnapCommandRegistrar.getRegistrar().getVersion();
121 this.exitSuccessfully();
123 } catch (Exception e) {
131 * Handles profile. --profile or -c
133 public void handleProfile() {
135 if ((args.size() == 2) && (this.getLongOption(OnapCliConstants.PARAM_PROFILE_LONG).equals(args.get(0))
136 || this.getShortOption(OnapCliConstants.PARAM_PROFILE_SHORT).equals(args.get(0)))) {
138 OnapCommandRegistrar.getRegistrar().setProfile(args.get(1));
139 //Make space of interactive mode
140 this.args = new ArrayList<>();
142 } catch (Exception e) {
148 public static String getDirectiveHelp() throws OnapCommandHelpFailed {
149 OnapCommandResult help = new OnapCommandResult();
150 help.setType(ResultType.TABLE);
151 help.setPrintDirection(PrintDirection.LANDSCAPE);
153 OnapCommandResultAttribute attr = new OnapCommandResultAttribute();
154 attr.setName(Constants.NAME.toUpperCase());
155 attr.setDescription(Constants.DESCRIPTION);
156 attr.setScope(OnapCommandResultAttributeScope.SHORT);
157 help.getRecords().add(attr);
159 OnapCommandResultAttribute attrDesc = new OnapCommandResultAttribute();
160 attrDesc.setName(Constants.DESCRIPTION.toUpperCase());
161 attrDesc.setDescription(Constants.DESCRIPTION);
162 attrDesc.setScope(OnapCommandResultAttributeScope.SHORT);
163 help.getRecords().add(attrDesc);
165 attr.getValues().add(OnapCliConstants.PARAM_INTERACTIVE_CLEAR);
166 attrDesc.getValues().add(OnapCliConstants.PARAM_INTERACTIVE_CLEAR_MSG);
168 attr.getValues().add(OnapCliConstants.PARAM_INTERACTIVE_EXIT);
169 attrDesc.getValues().add(OnapCliConstants.PARAM_INTERACTIVE_EXIT_MSG);
171 attr.getValues().add(OnapCliConstants.PARAM_INTERACTIVE_VERSION);
172 attrDesc.getValues().add(OnapCliConstants.PARAM_INTERACTIVE_VERSION_MSG);
174 attr.getValues().add(OnapCliConstants.PARAM_INTERACTIVE_USE);
175 attrDesc.getValues().add(OnapCliConstants.PARAM_INTERACTIVE_USE_MSG);
177 attr.getValues().add(OnapCliConstants.PARAM_INTERACTIVE_SET);
178 attrDesc.getValues().add(OnapCliConstants.PARAM_INTERACTIVE_SET_MSG);
180 attr.getValues().add(OnapCliConstants.PARAM_INTERACTIVE_UNSET);
181 attrDesc.getValues().add(OnapCliConstants.PARAM_INTERACTIVE_UNSET_MSG);
183 attr.getValues().add(OnapCliConstants.PARAM_INTERACTIVE_HELP);
184 attrDesc.getValues().add(OnapCliConstants.PARAM_INTERACTIVE_HELP_MSG);
186 attr.getValues().add(OnapCliConstants.PARAM_INTERACTIVE_PROFILE);
187 attrDesc.getValues().add(OnapCliConstants.PARAM_INTERACTIVE_PROFILE_MSG);
189 return "\n\nDirectives:\n" + help.print();
190 } catch (OnapCommandException e) {
191 throw new OnapCommandHelpFailed(e);
195 * Handles Interactive Mode.
197 public void handleInteractive() { // NOSONAR
198 if (args.isEmpty()) {
199 ConsoleReader console = null;
201 OnapCommandRegistrar.getRegistrar().setInteractiveMode(true);
202 console = createConsoleReader();
205 while ((line = console.readLine()) != null) {
206 if (OnapCliConstants.PARAM_INTERACTIVE_EXIT.equalsIgnoreCase(line)) {
208 } else if (OnapCliConstants.PARAM_INTERACTIVE_CLEAR.equalsIgnoreCase(line)) {
209 console.clearScreen();
212 this.args = Arrays.asList(line.split(OnapCliConstants.PARAM_INTERACTIVE_ARG_SPLIT_PATTERN));
214 if (!args.isEmpty() && this.args.get(0).equals(OnapCliConstants.PARAM_INTERACTIVE_USE)) {
215 if (args.size() == 1) {
216 this.print("Please input the product version to use, supported versions: " +
217 OnapCommandRegistrar.getRegistrar().getAvailableProductVersions());
220 OnapCommandRegistrar.getRegistrar().setEnabledProductVersion(args.get(1));
222 console = createConsoleReader();
223 } catch (OnapCommandException e) {
227 } else if (!args.isEmpty() && this.args.get(0).equals(OnapCliConstants.PARAM_INTERACTIVE_HELP)) {
229 this.print(OnapCommandRegistrar.getRegistrar().getHelpForEnabledProductVersion());
230 this.print(this.getDirectiveHelp());
231 } catch (OnapCommandException e) {
234 } else if (!args.isEmpty() && this.args.get(0).equals(OnapCliConstants.PARAM_INTERACTIVE_VERSION)) {
235 this.args = Arrays.asList(new String [] {this.getLongOption(OnapCliConstants.PARAM_VERSION_LONG)});
237 } else if (!args.isEmpty() && this.args.get(0).equals(OnapCliConstants.PARAM_INTERACTIVE_PROFILE)) {
238 if (args.size() == 1) {
239 this.print("Please use it in the form of 'profile <profile-name>'");
241 this.args = Arrays.asList(new String [] {
242 this.getLongOption(OnapCliConstants.PARAM_PROFILE_LONG),
246 } else if (!args.isEmpty() && this.args.get(0).equals(OnapCliConstants.PARAM_INTERACTIVE_SET)) {
247 if (args.size() > 1) {
248 String [] paramEntry = args.get(1).trim().split("=");
249 if (paramEntry.length >= 2) {
250 OnapCommandRegistrar.getRegistrar().addParamCache(paramEntry[0].trim(), paramEntry[1].trim());
252 this.print("Please use it in the form of 'set param-name=param-value'");
255 this.print(OnapCommandRegistrar.getRegistrar().getParamCache().toString());
257 } else if (!args.isEmpty() && this.args.get(0).equals(OnapCliConstants.PARAM_INTERACTIVE_UNSET)) {
258 if (args.size() > 1) {
259 for (int i = 1; i <args.size(); i++) {
260 OnapCommandRegistrar.getRegistrar().removeParamCache(args.get(i));
264 if (args.size() == 1 && args.get(0).trim().isEmpty()) {
265 //Ignore blanks // NOSONAR
272 } catch (IOException e) { // NOSONAR
273 this.print("Failed to read console, " + e.getMessage());
274 } catch (OnapCommandException e) {
279 TerminalFactory.get().restore();
280 } catch (Exception e) { // NOSONAR
282 if (console != null) {
285 this.exitSuccessfully();
291 * Creates console reader object.
293 * @return ConsoleReader
294 * @throws IOException
297 private ConsoleReader createConsoleReader() throws IOException {
298 ConsoleReader console = new ConsoleReader();
300 StringCompleter strCompleter = new StringCompleter(OnapCommandRegistrar.getRegistrar().listCommandsForEnabledProductVersion());
301 strCompleter.add(OnapCliConstants.PARAM_INTERACTIVE_EXIT,
302 OnapCliConstants.PARAM_INTERACTIVE_CLEAR,
303 OnapCliConstants.PARAM_INTERACTIVE_USE,
304 OnapCliConstants.PARAM_INTERACTIVE_HELP,
305 OnapCliConstants.PARAM_INTERACTIVE_VERSION,
306 OnapCliConstants.PARAM_INTERACTIVE_SET,
307 OnapCliConstants.PARAM_INTERACTIVE_UNSET,
308 OnapCliConstants.PARAM_INTERACTIVE_PROFILE);
309 console.addCompleter(strCompleter);
310 console.setPrompt(OnapCliConstants.PARAM_INTERACTIVE_PROMPT + ":" + OnapCommandRegistrar.getRegistrar().getEnabledProductVersion() + ">");
311 } catch (OnapCommandException e) { // NOSONAR
312 this.print("Failed to load oclip commands," + e.getMessage());
321 public void handleCommand() {
323 if (!args.isEmpty()) {
325 cmd = OnapCommandRegistrar.getRegistrar().get(args.get(0));
326 } catch (Exception e) {
332 // check for help or version
333 if (args.size() == 2) {
334 if (this.getLongOption(OnapCliConstants.PARAM_HELP_LOGN).equals(args.get(1))
335 || this.getShortOption(OnapCliConstants.PARAM_HELP_SHORT).equals(args.get(1))) {
336 String help = cmd.printHelp();
338 this.exitSuccessfully();
340 } else if (this.getLongOption(OnapCliConstants.PARAM_VERSION_LONG).equals(args.get(1))
341 || this.getShortOption(OnapCliConstants.PARAM_VERSION_SHORT).equals(args.get(1))) {
342 String version = cmd.printVersion();
344 this.exitSuccessfully();
349 for (OnapCommandParameter param: cmd.getParameters()) {
350 if (OnapCommandRegistrar.getRegistrar().getParamCache().containsKey(
351 cmd.getInfo().getService() + ":" + param.getLongOption())) {
352 param.setValue(OnapCommandRegistrar.getRegistrar().getParamCache().get(
353 cmd.getInfo().getService() + ":" + param.getLongOption()));
354 } else if (OnapCommandRegistrar.getRegistrar().getParamCache().containsKey(param.getLongOption())) {
355 param.setValue(OnapCommandRegistrar.getRegistrar().getParamCache().get(param.getLongOption()));
359 OnapCliUtils.populateParams(cmd.getParameters(), args);
360 OnapCommandResult result = cmd.execute();
362 this.print(result.getDebugInfo());
363 this.print(result.print());
364 this.exitSuccessfully();
366 generateSmapleYaml(cmd);
367 } catch (Exception e) {
368 this.print(cmd.getResult().getDebugInfo());
369 if (e instanceof OnapCommandWarning) {
370 this.exitSuccessfully();
379 private void generateSmapleYaml(OnapCommand cmd) throws OnapCommandException {
380 if (OnapCommandConfg.isSampleGenerateEnabled() && this.getExitCode() == OnapCliConstants.EXIT_SUCCESS) {
382 SampleYamlGenerator.generateSampleYaml(args, cmd.getResult().print(),
383 OnapCommandRegistrar.getRegistrar().getEnabledProductVersion(),
384 OnapCommandConfg.getSampleGenerateTargetFolder() + "/" + cmd.getSchemaName().replaceAll(".yaml", "") + "-sample.yaml",
385 cmd.getResult().isDebug());
386 } catch (IOException error) {
387 throw new OnapCommandInvalidSample(args.get(0), error);
393 * Handles all client input.
395 public void handle() {
398 if (this.exitCode == -1) {
399 this.handleVersion();
402 if (this.exitCode == -1) {
403 this.handleProfile();
406 if (this.exitCode == -1) {
407 this.handleInteractive();
410 if (this.exitCode == -1) {
411 this.handleCommand();
421 public static void main(String[] args) {
422 OnapCli cli = new OnapCli(args);
424 System.exit(cli.getExitCode());