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