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;
26 import org.apache.commons.cli.CommandLine;
27 import org.apache.commons.cli.HelpFormatter;
28 import org.onap.policy.apex.context.parameters.SchemaParameters;
29 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
30 import org.onap.policy.apex.tools.common.CliOptions;
31 import org.onap.policy.apex.tools.common.CliParser;
32 import org.onap.policy.common.parameters.ParameterService;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
37 * Model 2 event generator with main method.
39 * @author Sven van der Meer (sven.van.der.meer@ericsson.com)
41 public final class Model2EventMain {
42 // Get a reference to the logger
43 private static final Logger LOGGER = LoggerFactory.getLogger(Model2EventMain.class);
45 /** The name of the application. */
46 public static final String APP_NAME = "gen-model2event";
48 /** The description 1-liner of the application. */
49 public static final String APP_DESCRIPTION = "generates JSON templates for events generated from a policy model";
52 * Constructor, run the command.
54 * @param args the command line arguments
55 * @param outStream the stream for command output
57 Model2EventMain(final String[] args, final PrintStream outStream) {
58 SchemaParameters schemaParameters = new SchemaParameters();
59 ParameterService.register(schemaParameters, true);
61 final CliParser cli = new CliParser();
62 cli.addOption(CliOptions.HELP);
63 cli.addOption(CliOptions.VERSION);
64 cli.addOption(CliOptions.MODELFILE);
65 cli.addOption(CliOptions.TYPE);
67 final CommandLine cmd = cli.parseCli(args);
69 // help is an exit option, print usage and exit
70 if (cmd == null || cmd.hasOption('h') || cmd.hasOption("help")) {
71 outStream.println(getHelpString(cli));
76 // version is an exit option, print version and exit
77 if (cmd.hasOption('v') || cmd.hasOption("version")) {
78 outStream.println(APP_NAME + " " + cli.getAppVersion());
83 generateJsonEventSchema(cmd, outStream);
87 * Generate the JSON event schema.
89 * @param cmd the command to run
90 * @param outStream the output stream for output
92 private static void generateJsonEventSchema(final CommandLine cmd, final PrintStream outStream) {
93 String modelFile = cmd.getOptionValue('m');
94 if (modelFile == null) {
95 modelFile = cmd.getOptionValue("model");
97 if (modelFile == null) {
98 outStream.println(APP_NAME + ": no model file given, cannot proceed (try -h for help)");
102 String type = cmd.getOptionValue('t');
104 type = cmd.getOptionValue("type");
107 outStream.println(APP_NAME + ": no event type given, cannot proceed (try -h for help)");
110 if (!"stimuli".equals(type) && !"response".equals(type) && !"internal".equals(type)) {
111 outStream.println(APP_NAME + ": unknown type <" + type + ">, cannot proceed (try -h for help)");
116 outStream.println(APP_NAME + ": starting Event generator");
117 outStream.println(" --> model file: " + modelFile);
118 outStream.println(" --> type: " + type);
123 final Model2JsonEventSchema app = new Model2JsonEventSchema(modelFile, type, APP_NAME);
125 } catch (final ApexException aex) {
126 String message = APP_NAME + ": caught APEX exception with message: " + aex.getMessage();
127 outStream.println(message);
128 LOGGER.warn(message, aex);
133 * Get the help string for the application.
135 * @param cli the command line options
136 * @return the help string
138 private String getHelpString(final CliParser cli) {
139 HelpFormatter formatter = new HelpFormatter();
141 final StringWriter helpStringWriter = new StringWriter();
142 final PrintWriter helpPrintWriter = new PrintWriter(helpStringWriter);
144 formatter.printHelp(helpPrintWriter, 120, APP_NAME, APP_DESCRIPTION, cli.getOptions(), 2, 4, "");
146 return helpStringWriter.toString();
150 * Main method to start the application.
152 * @param args the command line arguments
154 public static void main(final String[] args) {
155 new Model2EventMain(args, System.out);