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;
24 import java.io.PrintWriter;
25 import java.io.StringWriter;
27 import org.apache.commons.cli.CommandLine;
28 import org.apache.commons.cli.HelpFormatter;
29 import org.onap.policy.apex.context.parameters.SchemaParameters;
30 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
31 import org.onap.policy.apex.tools.common.CliOptions;
32 import org.onap.policy.apex.tools.common.CliParser;
33 import org.onap.policy.common.parameters.ParameterService;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
38 * Model 2 event generator with main method.
40 * @author Sven van der Meer (sven.van.der.meer@ericsson.com)
42 public final class Model2EventMain {
43 // Get a reference to the logger
44 private static final Logger LOGGER = LoggerFactory.getLogger(Model2EventMain.class);
46 /** The name of the application. */
47 public static final String APP_NAME = "gen-model2event";
49 /** The description 1-liner of the application. */
50 public static final String APP_DESCRIPTION = "generates JSON templates for events generated from a policy model";
53 * Constructor, run the command.
55 * @param args the command line arguments
56 * @param outStream the stream for command output
58 Model2EventMain(final String[] args, final PrintStream outStream) {
59 SchemaParameters schemaParameters = new SchemaParameters();
60 ParameterService.register(schemaParameters, true);
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 == null || cmd.hasOption('h') || cmd.hasOption("help")) {
72 outStream.println(getHelpString(cli));
77 // version is an exit option, print version and exit
78 if (cmd.hasOption('v') || cmd.hasOption("version")) {
79 outStream.println(APP_NAME + " " + cli.getAppVersion());
84 generateJsonEventSchema(cmd, outStream);
88 * Generate the JSON event schema.
90 * @param cmd the command to run
91 * @param outStream the output stream for output
93 private static void generateJsonEventSchema(final CommandLine cmd, final PrintStream outStream) {
94 String modelFile = cmd.getOptionValue('m');
95 if (modelFile == null) {
96 modelFile = cmd.getOptionValue("model");
98 if (modelFile == null) {
99 outStream.println(APP_NAME + ": no model file given, cannot proceed (try -h for help)");
103 String type = cmd.getOptionValue('t');
105 type = cmd.getOptionValue("type");
108 outStream.println(APP_NAME + ": no event type given, cannot proceed (try -h for help)");
111 if (!"stimuli".equals(type) && !"response".equals(type) && !"internal".equals(type)) {
112 outStream.println(APP_NAME + ": unknown type <" + type + ">, cannot proceed (try -h for help)");
117 outStream.println(APP_NAME + ": starting Event generator");
118 outStream.println(" --> model file: " + modelFile);
119 outStream.println(" --> type: " + type);
124 final Model2JsonEventSchema app = new Model2JsonEventSchema(modelFile, type, APP_NAME);
126 } catch (final ApexException aex) {
127 String message = APP_NAME + ": caught APEX exception with message: " + aex.getMessage();
128 outStream.println(message);
129 LOGGER.warn(message, aex);
134 * Get the help string for the application.
136 * @param cli the command line options
137 * @return the help string
139 private String getHelpString(final CliParser cli) {
140 HelpFormatter formatter = new HelpFormatter();
142 final StringWriter helpStringWriter = new StringWriter();
143 final PrintWriter helpPrintWriter = new PrintWriter(helpStringWriter);
145 formatter.printHelp(helpPrintWriter, 120, APP_NAME, APP_DESCRIPTION, cli.getOptions(), 2, 4, "");
147 return helpStringWriter.toString();
151 * Main method to start the application.
153 * @param args the command line arguments
155 public static void main(final String[] args) {
156 new Model2EventMain(args, System.out);