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