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