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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.tools.model.generator.model2event;
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;
32 * Model 2 event generator with main method.
34 * @author Sven van der Meer (sven.van.der.meer@ericsson.com)
36 public final class Application {
37 // Get a reference to the logger
38 private static final Logger LOGGER = LoggerFactory.getLogger(Application.class);
40 /** The name of the application. */
41 public static final String APP_NAME = "gen-model2event";
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";
46 /** Private constructor to prevent instantiation. */
47 private Application() {}
50 * Main method to start the application.
52 * @param args the command line arguments
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);
61 final CommandLine cmd = cli.parseCli(args);
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());
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());
79 String modelFile = cmd.getOptionValue('m');
80 if (modelFile == null) {
81 modelFile = cmd.getOptionValue("model");
83 if (modelFile == null) {
84 System.err.println(APP_NAME + ": no model file given, cannot proceed (try -h for help)");
88 String type = cmd.getOptionValue('t');
90 type = cmd.getOptionValue("type");
93 System.err.println(APP_NAME + ": no event type given, cannot proceed (try -h for help)");
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)");
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();
109 final Model2JsonEventSchema app = new Model2JsonEventSchema(modelFile, type, APP_NAME);
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);