Changes for checkstyle 8.32
[policy/apex-pdp.git] / tools / model-generator / src / main / java / org / onap / policy / apex / tools / model / generator / model2cli / Model2CliMain.java
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.tools.model.generator.model2cli;
22
23 import java.io.PrintStream;
24 import java.io.PrintWriter;
25 import java.io.StringWriter;
26 import org.apache.commons.cli.CommandLine;
27 import org.apache.commons.cli.HelpFormatter;
28 import org.onap.policy.apex.tools.common.CliOptions;
29 import org.onap.policy.apex.tools.common.CliParser;
30 import org.onap.policy.apex.tools.common.OutputFile;
31
32 /**
33  * Process an Apex Policy Model file to generate the CLI commands to generate an equivalent Apex Policy Model.
34  *
35  * @author Sven van der Meer <sven.van.der.meer@ericsson.com>
36  */
37 public final class Model2CliMain {
38     /** The name of the application. */
39     public static final String APP_NAME = "gen-model2cli";
40
41     /** The description 1-liner of the application. */
42     public static final String APP_DESCRIPTION = "generates CLI Editor Commands from a policy model";
43
44     /**
45      * Run the tool.
46      * 
47      * @param args the command line arguments
48      * @param outStream for command output
49      * @param errStream for command errors
50      */
51     Model2CliMain(final String[] args, final PrintStream outStream, final PrintStream errStream) {
52         final CliParser cli = new CliParser();
53         cli.addOption(CliOptions.HELP);
54         cli.addOption(CliOptions.VERSION);
55         cli.addOption(CliOptions.SKIPVALIDATION);
56         cli.addOption(CliOptions.MODELFILE);
57         cli.addOption(CliOptions.FILEOUT);
58         cli.addOption(CliOptions.OVERWRITE);
59
60         CommandLine cmd = cli.parseCli(args);
61
62         // help is an exit option, print usage and exit
63         if (cmd == null || cmd.hasOption(CliOptions.HELP.getOpt())) {
64             outStream.println(getHelpString(cli));
65             outStream.println();
66             return;
67         }
68
69         // version is an exit option, print version and exit
70         if (cmd.hasOption(CliOptions.VERSION.getOpt())) {
71             outStream.println(APP_NAME + " " + cli.getAppVersion());
72             return;
73         }
74
75         String modelFile = cmd.getOptionValue(CliOptions.MODELFILE.getOpt());
76         if (modelFile == null) {
77             errStream.println(APP_NAME + ": no '-" + CliOptions.MODELFILE.getOpt()
78                             + "' model file given, cannot proceed (try -h for help)");
79             return;
80         } else {
81             modelFile = cmd.getOptionValue("model");
82         }
83
84         OutputFile outfile = null;
85         final String of = cmd.getOptionValue(CliOptions.FILEOUT.getOpt());
86         final boolean overwrite = cmd.hasOption(CliOptions.OVERWRITE.getOpt());
87         if (overwrite && of == null) {
88             errStream.println(APP_NAME + ": error with '-" + CliOptions.OVERWRITE.getOpt()
89                             + "' option. This option is only valid if a '-" + CliOptions.FILEOUT.getOpt()
90                             + "' option is also used. Cannot proceed (try -h for help)");
91             return;
92         }
93         if (of != null) {
94             outfile = new OutputFile(of, overwrite);
95             final String isoutfileok = outfile.validate();
96             if (isoutfileok != null) {
97                 errStream.println(APP_NAME + ": error with '-" + CliOptions.FILEOUT.getOpt() + "' option: \""
98                                 + isoutfileok + "\". Cannot proceed (try -h for help)");
99                 return;
100             }
101         }
102
103         if (outfile == null) {
104             outStream.println();
105             outStream.println(APP_NAME + ": starting CLI generator");
106             outStream.println(" --> model file: " + modelFile);
107             outStream.println();
108             outStream.println();
109         }
110
111         final Model2Cli app = new Model2Cli(modelFile, outfile, !cmd.hasOption("sv"), APP_NAME);
112         app.runApp();
113     }
114
115     /**
116      * Get the help string for the application.
117      * 
118      * @param cli the command line options
119      * @return the help string
120      */
121     private String getHelpString(final CliParser cli) {
122         HelpFormatter formatter = new HelpFormatter();
123
124         final StringWriter helpStringWriter = new StringWriter();
125         final PrintWriter helpPrintWriter = new PrintWriter(helpStringWriter);
126
127         formatter.printHelp(helpPrintWriter, 120, APP_NAME, APP_DESCRIPTION, cli.getOptions(), 2, 4, "");
128
129         return helpStringWriter.toString();
130     }
131
132     /**
133      * Main method to start the application.
134      *
135      * @param args the command line arguments
136      */
137     public static void main(final String[] args) {
138         new Model2CliMain(args, System.out, System.err);
139     }
140 }