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 java.io.PrintStream;
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;
34 * Model 2 event generator with main method.
36 * @author Sven van der Meer (sven.van.der.meer@ericsson.com)
38 public final class Application {
39 // Get a reference to the logger
40 private static final Logger LOGGER = LoggerFactory.getLogger(Application.class);
42 /** The name of the application. */
43 public static final String APP_NAME = "gen-model2event";
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";
48 /** Private constructor to prevent instantiation. */
49 private Application() {
52 // Input and output streams
53 private static final PrintStream OUT_STREAM = System.out;
54 private static final PrintStream ERR_STREAM = System.err;
57 * Main method to start the application.
59 * @param args the command line arguments
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);
68 final CommandLine cmd = cli.parseCli(args);
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());
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());
86 generateJsonEventScheam(cmd);
90 * Generate the JSON event schema.
92 * @param cmd the command to run
94 private static void generateJsonEventScheam(final CommandLine cmd) {
95 String modelFile = cmd.getOptionValue('m');
96 if (modelFile == null) {
97 modelFile = cmd.getOptionValue("model");
99 if (modelFile == null) {
100 ERR_STREAM.println(APP_NAME + ": no model file given, cannot proceed (try -h for help)");
104 String type = cmd.getOptionValue('t');
106 type = cmd.getOptionValue("type");
109 ERR_STREAM.println(APP_NAME + ": no event type given, cannot proceed (try -h for help)");
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)");
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();
125 final Model2JsonEventSchema app = new Model2JsonEventSchema(modelFile, type, APP_NAME);
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);