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