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