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