2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.tools.model.generator.model2cli;
23 import org.apache.commons.cli.CommandLine;
24 import org.apache.commons.cli.HelpFormatter;
25 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
26 import org.onap.policy.apex.tools.common.CliOptions;
27 import org.onap.policy.apex.tools.common.CliParser;
28 import org.onap.policy.apex.tools.common.OutputFile;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
33 * Process an Apex Policy Model file to generate the CLI commands to generate an equivalent Apex Policy Model.
35 * @author Sven van der Meer <sven.van.der.meer@ericsson.com>
37 public final class Application {
38 // Get a reference to the logger
39 private static final Logger LOGGER = LoggerFactory.getLogger(Application.class);
41 /** The name of the application. */
42 public static final String APP_NAME = "gen-model2cli";
44 /** The description 1-liner of the application. */
45 public static final String APP_DESCRIPTION = "generates CLI Editor Commands from a policy model";
48 * Private constructor to prevent instantiation.
50 private Application() {}
53 * Main method to start the application.
55 * @param args the command line arguments
57 public static void main(final String[] args) {
58 final CliParser cli = new CliParser();
59 cli.addOption(CliOptions.HELP);
60 cli.addOption(CliOptions.VERSION);
61 cli.addOption(CliOptions.SKIPVALIDATION);
62 cli.addOption(CliOptions.MODELFILE);
63 cli.addOption(CliOptions.FILEOUT);
64 cli.addOption(CliOptions.OVERWRITE);
66 final CommandLine cmd = cli.parseCli(args);
68 // help is an exit option, print usage and exit
69 if (cmd.hasOption(CliOptions.HELP.getOpt())) {
70 final HelpFormatter formatter = new HelpFormatter();
71 System.out.println(APP_NAME + " v" + cli.getAppVersion() + " - " + APP_DESCRIPTION);
72 formatter.printHelp(APP_NAME, cli.getOptions());
77 // version is an exit option, print version and exit
78 if (cmd.hasOption(CliOptions.VERSION.getOpt())) {
79 System.out.println(APP_NAME + " " + cli.getAppVersion());
84 String modelFile = cmd.getOptionValue(CliOptions.MODELFILE.getOpt());
85 if (modelFile != null) {
86 modelFile = cmd.getOptionValue("model");
88 if (modelFile == null) {
89 System.err.println(APP_NAME + ": no '-" + CliOptions.MODELFILE.getOpt()
90 + "' model file given, cannot proceed (try -h for help)");
94 OutputFile outfile = null;
95 final String of = cmd.getOptionValue(CliOptions.FILEOUT.getOpt());
96 final boolean overwrite = cmd.hasOption(CliOptions.OVERWRITE.getOpt());
97 if (overwrite && of == null) {
98 System.err.println(APP_NAME + ": error with '-" + CliOptions.OVERWRITE.getOpt()
99 + "' option. This option is only valid if a '-" + CliOptions.FILEOUT.getOpt()
100 + "' option is also used. Cannot proceed (try -h for help)");
104 outfile = new OutputFile(of, overwrite);
105 final String isoutfileok = outfile.validate();
106 if (isoutfileok != null) {
107 System.err.println(APP_NAME + ": error with '-" + CliOptions.FILEOUT.getOpt() + "' option: \""
108 + isoutfileok + "\". Cannot proceed (try -h for help)");
113 if (outfile == null) {
114 System.out.println();
115 System.out.println(APP_NAME + ": starting CLI generator");
116 System.out.println(" --> model file: " + modelFile);
117 System.out.println();
118 System.out.println();
122 final Model2Cli app = new Model2Cli(modelFile, outfile, !cmd.hasOption("sv"), APP_NAME);
124 } catch (final ApexException aex) {
125 String message = APP_NAME + ": caught APEX exception with message: " + aex.getMessage();
126 System.err.println(message);
127 LOGGER.warn(message, aex);