a5600ead12491675cea2163ca78e731c8e1cb6eb
[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.model2event;
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.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 /**
34  * Model 2 event generator with main method.
35  *
36  * @author Sven van der Meer (sven.van.der.meer@ericsson.com)
37  */
38 public final class Application {
39     // Get a reference to the logger
40     private static final Logger LOGGER = LoggerFactory.getLogger(Application.class);
41
42     /** The name of the application. */
43     public static final String APP_NAME = "gen-model2event";
44
45     /** The description 1-liner of the application. */
46     public static final String APP_DESCRIPTION = "generates JSON templates for events generated from a policy model";
47
48     /** Private constructor to prevent instantiation. */
49     private Application() {
50     }
51
52     // Input and output streams
53     private static final PrintStream OUT_STREAM = System.out;
54     private static final PrintStream ERR_STREAM = System.err;
55
56     /**
57      * Main method to start the application.
58      *
59      * @param args the command line arguments
60      */
61     public static void main(final String[] args) {
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.hasOption('h') || cmd.hasOption("help")) {
72             final HelpFormatter formatter = new HelpFormatter();
73             OUT_STREAM.println(APP_NAME + " v" + cli.getAppVersion() + " - " + APP_DESCRIPTION);
74             formatter.printHelp(APP_NAME, cli.getOptions());
75             OUT_STREAM.println();
76             return;
77         }
78
79         // version is an exit option, print version and exit
80         if (cmd.hasOption('v') || cmd.hasOption("version")) {
81             OUT_STREAM.println(APP_NAME + " " + cli.getAppVersion());
82             OUT_STREAM.println();
83             return;
84         }
85
86         generateJsonEventScheam(cmd);
87     }
88
89     /**
90      * Generate the JSON event schema.
91      * 
92      * @param cmd the command to run
93      */
94     private static void generateJsonEventScheam(final CommandLine cmd) {
95         String modelFile = cmd.getOptionValue('m');
96         if (modelFile == null) {
97             modelFile = cmd.getOptionValue("model");
98         }
99         if (modelFile == null) {
100             ERR_STREAM.println(APP_NAME + ": no model file given, cannot proceed (try -h for help)");
101             return;
102         }
103
104         String type = cmd.getOptionValue('t');
105         if (type == null) {
106             type = cmd.getOptionValue("type");
107         }
108         if (type == null) {
109             ERR_STREAM.println(APP_NAME + ": no event type given, cannot proceed (try -h for help)");
110             return;
111         }
112         if (!"stimuli".equals(type) && !"response".equals(type) && !"internal".equals(type)) {
113             ERR_STREAM.println(APP_NAME + ": unknown type <" + type + ">, cannot proceed (try -h for help)");
114             return;
115         }
116
117         OUT_STREAM.println();
118         OUT_STREAM.println(APP_NAME + ": starting Event generator");
119         OUT_STREAM.println(" --> model file: " + modelFile);
120         OUT_STREAM.println(" --> type: " + type);
121         OUT_STREAM.println();
122         OUT_STREAM.println();
123
124         try {
125             final Model2JsonEventSchema app = new Model2JsonEventSchema(modelFile, type, APP_NAME);
126             app.runApp();
127         } catch (final ApexException aex) {
128             String message = APP_NAME + ": caught APEX exception with message: " + aex.getMessage();
129             ERR_STREAM.println(message);
130             LOGGER.warn(message, aex);
131         }
132     }
133 }