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