Changes for checkstyle 8.32
[policy/apex-pdp.git] / tools / model-generator / src / main / java / org / onap / policy / apex / tools / model / generator / model2event / Model2EventMain.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.model2event;
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.context.parameters.SchemaParameters;
29 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
30 import org.onap.policy.apex.tools.common.CliOptions;
31 import org.onap.policy.apex.tools.common.CliParser;
32 import org.onap.policy.common.parameters.ParameterService;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 /**
37  * Model 2 event generator with main method.
38  *
39  * @author Sven van der Meer (sven.van.der.meer@ericsson.com)
40  */
41 public final class Model2EventMain {
42     // Get a reference to the logger
43     private static final Logger LOGGER = LoggerFactory.getLogger(Model2EventMain.class);
44
45     /** The name of the application. */
46     public static final String APP_NAME = "gen-model2event";
47
48     /** The description 1-liner of the application. */
49     public static final String APP_DESCRIPTION = "generates JSON templates for events generated from a policy model";
50
51     /**
52      * Constructor, run the command.
53      * 
54      * @param args the command line arguments
55      * @param outStream the stream for command output
56      */
57     Model2EventMain(final String[] args, final PrintStream outStream) {
58         SchemaParameters schemaParameters = new SchemaParameters();
59         ParameterService.register(schemaParameters, true);
60         
61         final CliParser cli = new CliParser();
62         cli.addOption(CliOptions.HELP);
63         cli.addOption(CliOptions.VERSION);
64         cli.addOption(CliOptions.MODELFILE);
65         cli.addOption(CliOptions.TYPE);
66
67         final CommandLine cmd = cli.parseCli(args);
68
69         // help is an exit option, print usage and exit
70         if (cmd == null || cmd.hasOption('h') || cmd.hasOption("help")) {
71             outStream.println(getHelpString(cli));
72             outStream.println();
73             return;
74         }
75
76         // version is an exit option, print version and exit
77         if (cmd.hasOption('v') || cmd.hasOption("version")) {
78             outStream.println(APP_NAME + " " + cli.getAppVersion());
79             outStream.println();
80             return;
81         }
82
83         generateJsonEventSchema(cmd, outStream);
84     }
85
86     /**
87      * Generate the JSON event schema.
88      * 
89      * @param cmd the command to run
90      * @param outStream the output stream for output
91      */
92     private static void generateJsonEventSchema(final CommandLine cmd, final PrintStream outStream) {
93         String modelFile = cmd.getOptionValue('m');
94         if (modelFile == null) {
95             modelFile = cmd.getOptionValue("model");
96         }
97         if (modelFile == null) {
98             outStream.println(APP_NAME + ": no model file given, cannot proceed (try -h for help)");
99             return;
100         }
101
102         String type = cmd.getOptionValue('t');
103         if (type == null) {
104             type = cmd.getOptionValue("type");
105         }
106         if (type == null) {
107             outStream.println(APP_NAME + ": no event type given, cannot proceed (try -h for help)");
108             return;
109         }
110         if (!"stimuli".equals(type) && !"response".equals(type) && !"internal".equals(type)) {
111             outStream.println(APP_NAME + ": unknown type <" + type + ">, cannot proceed (try -h for help)");
112             return;
113         }
114
115         outStream.println();
116         outStream.println(APP_NAME + ": starting Event generator");
117         outStream.println(" --> model file: " + modelFile);
118         outStream.println(" --> type: " + type);
119         outStream.println();
120         outStream.println();
121
122         try {
123             final Model2JsonEventSchema app = new Model2JsonEventSchema(modelFile, type, APP_NAME);
124             app.runApp();
125         } catch (final ApexException aex) {
126             String message = APP_NAME + ": caught APEX exception with message: " + aex.getMessage();
127             outStream.println(message);
128             LOGGER.warn(message, aex);
129         }
130     }
131
132     /**
133      * Get the help string for the application.
134      * 
135      * @param cli the command line options
136      * @return the help string
137      */
138     private String getHelpString(final CliParser cli) {
139         HelpFormatter formatter = new HelpFormatter();
140
141         final StringWriter helpStringWriter = new StringWriter();
142         final PrintWriter helpPrintWriter = new PrintWriter(helpStringWriter);
143
144         formatter.printHelp(helpPrintWriter, 120, APP_NAME, APP_DESCRIPTION, cli.getOptions(), 2, 4, "");
145
146         return helpStringWriter.toString();
147     }
148
149     /**
150      * Main method to start the application.
151      *
152      * @param args the command line arguments
153      */
154     public static void main(final String[] args) {
155         new Model2EventMain(args, System.out);
156     }
157 }