diff options
Diffstat (limited to 'tools/model-generator')
4 files changed, 187 insertions, 94 deletions
diff --git a/tools/model-generator/src/main/java/org/onap/policy/apex/tools/model/generator/model2cli/Application.java b/tools/model-generator/src/main/java/org/onap/policy/apex/tools/model/generator/model2cli/Application.java index b6c6b774c..70436150c 100644 --- a/tools/model-generator/src/main/java/org/onap/policy/apex/tools/model/generator/model2cli/Application.java +++ b/tools/model-generator/src/main/java/org/onap/policy/apex/tools/model/generator/model2cli/Application.java @@ -20,6 +20,8 @@ package org.onap.policy.apex.tools.model.generator.model2cli; +import java.io.PrintStream; + import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.HelpFormatter; import org.onap.policy.apex.model.basicmodel.concepts.ApexException; @@ -44,10 +46,15 @@ public final class Application { /** The description 1-liner of the application. */ public static final String APP_DESCRIPTION = "generates CLI Editor Commands from a policy model"; + // Input and output streams + private static final PrintStream OUT_STREAM = System.out; + private static final PrintStream ERR_STREAM = System.err; + /** * Private constructor to prevent instantiation. */ - private Application() {} + private Application() { + } /** * Main method to start the application. @@ -68,16 +75,16 @@ public final class Application { // help is an exit option, print usage and exit if (cmd.hasOption(CliOptions.HELP.getOpt())) { final HelpFormatter formatter = new HelpFormatter(); - System.out.println(APP_NAME + " v" + cli.getAppVersion() + " - " + APP_DESCRIPTION); + OUT_STREAM.println(APP_NAME + " v" + cli.getAppVersion() + " - " + APP_DESCRIPTION); formatter.printHelp(APP_NAME, cli.getOptions()); - System.out.println(); + OUT_STREAM.println(); return; } // version is an exit option, print version and exit if (cmd.hasOption(CliOptions.VERSION.getOpt())) { - System.out.println(APP_NAME + " " + cli.getAppVersion()); - System.out.println(); + OUT_STREAM.println(APP_NAME + " " + cli.getAppVersion()); + OUT_STREAM.println(); return; } @@ -86,8 +93,8 @@ public final class Application { modelFile = cmd.getOptionValue("model"); } if (modelFile == null) { - System.err.println(APP_NAME + ": no '-" + CliOptions.MODELFILE.getOpt() - + "' model file given, cannot proceed (try -h for help)"); + ERR_STREAM.println(APP_NAME + ": no '-" + CliOptions.MODELFILE.getOpt() + + "' model file given, cannot proceed (try -h for help)"); return; } @@ -95,27 +102,27 @@ public final class Application { final String of = cmd.getOptionValue(CliOptions.FILEOUT.getOpt()); final boolean overwrite = cmd.hasOption(CliOptions.OVERWRITE.getOpt()); if (overwrite && of == null) { - System.err.println(APP_NAME + ": error with '-" + CliOptions.OVERWRITE.getOpt() - + "' option. This option is only valid if a '-" + CliOptions.FILEOUT.getOpt() - + "' option is also used. Cannot proceed (try -h for help)"); + ERR_STREAM.println(APP_NAME + ": error with '-" + CliOptions.OVERWRITE.getOpt() + + "' option. This option is only valid if a '-" + CliOptions.FILEOUT.getOpt() + + "' option is also used. Cannot proceed (try -h for help)"); return; } if (of != null) { outfile = new OutputFile(of, overwrite); final String isoutfileok = outfile.validate(); if (isoutfileok != null) { - System.err.println(APP_NAME + ": error with '-" + CliOptions.FILEOUT.getOpt() + "' option: \"" - + isoutfileok + "\". Cannot proceed (try -h for help)"); + ERR_STREAM.println(APP_NAME + ": error with '-" + CliOptions.FILEOUT.getOpt() + "' option: \"" + + isoutfileok + "\". Cannot proceed (try -h for help)"); return; } } if (outfile == null) { - System.out.println(); - System.out.println(APP_NAME + ": starting CLI generator"); - System.out.println(" --> model file: " + modelFile); - System.out.println(); - System.out.println(); + OUT_STREAM.println(); + OUT_STREAM.println(APP_NAME + ": starting CLI generator"); + OUT_STREAM.println(" --> model file: " + modelFile); + OUT_STREAM.println(); + OUT_STREAM.println(); } try { @@ -123,7 +130,7 @@ public final class Application { app.runApp(); } catch (final ApexException aex) { String message = APP_NAME + ": caught APEX exception with message: " + aex.getMessage(); - System.err.println(message); + ERR_STREAM.println(message); LOGGER.warn(message, aex); } } diff --git a/tools/model-generator/src/main/java/org/onap/policy/apex/tools/model/generator/model2cli/Model2Cli.java b/tools/model-generator/src/main/java/org/onap/policy/apex/tools/model/generator/model2cli/Model2Cli.java index e6002208c..f58faf0eb 100644 --- a/tools/model-generator/src/main/java/org/onap/policy/apex/tools/model/generator/model2cli/Model2Cli.java +++ b/tools/model-generator/src/main/java/org/onap/policy/apex/tools/model/generator/model2cli/Model2Cli.java @@ -94,6 +94,7 @@ public class Model2Cli { public Model2Cli(final String modelFile, final OutputFile outFile, final boolean validate, final String appName) { Validate.notNull(modelFile, "Model2Cli: given model file name was blank"); Validate.notNull(appName, "Model2Cli: given application name was blank"); + this.modelFile = modelFile; this.outFile = outFile; this.appName = appName; @@ -133,6 +134,15 @@ public class Model2Cli { } } + return generateCli(codeGen, policyModel); + } + + /** + * Generate the CLI from the model. + * @param codeGen the code generator + * @param policyModel the policy model + */ + private int generateCli(final CodeGeneratorCliEditor codeGen, final AxPolicyModel policyModel) { kig = new KeyInfoGetter(policyModel); // Order is important. 0: model, 1: context schemas, 2: tasks, 3: events, 4: ContextAlbums, 5: Policies @@ -145,7 +155,7 @@ public class Model2Cli { final AxArtifactKey key = s.getKey(); codeGen.addSchemaDeclaration(kig.getName(key), kig.getVersion(key), kig.getUuid(key), kig.getDesc(key), - s.getSchemaFlavour(), s.getSchema()); + s.getSchemaFlavour(), s.getSchema()); } // 2: tasks @@ -158,7 +168,7 @@ public class Model2Cli { final List<ST> contextRefs = getCtxtRefsForTask(codeGen, t); codeGen.addTaskDeclaration(kig.getName(key), kig.getVersion(key), kig.getUuid(key), kig.getDesc(key), - infields, outfields, logic, parameters, contextRefs); + infields, outfields, logic, parameters, contextRefs); } // 3: events @@ -167,7 +177,7 @@ public class Model2Cli { final List<ST> fields = getParametersForEvent(codeGen, e); codeGen.addEventDeclaration(kig.getName(key), kig.getVersion(key), kig.getUuid(key), kig.getDesc(key), - e.getNameSpace(), e.getSource(), e.getTarget(), fields); + e.getNameSpace(), e.getSource(), e.getTarget(), fields); } // 4: context albums @@ -175,8 +185,8 @@ public class Model2Cli { final AxArtifactKey key = a.getKey(); codeGen.addContextAlbumDeclaration(kig.getName(key), kig.getVersion(key), kig.getUuid(key), - kig.getDesc(key), a.getScope(), a.isWritable(), kig.getName(a.getItemSchema()), - kig.getVersion(a.getItemSchema())); + kig.getDesc(key), a.getScope(), a.isWritable(), kig.getName(a.getItemSchema()), + kig.getVersion(a.getItemSchema())); } // 5: policies @@ -184,7 +194,7 @@ public class Model2Cli { final AxArtifactKey key = p.getKey(); final List<ST> states = getStatesForPolicy(codeGen, p); codeGen.addPolicyDefinition(kig.getName(key), kig.getVersion(key), kig.getUuid(key), kig.getDesc(key), - p.getTemplate(), p.getFirstState(), states); + p.getTemplate(), p.getFirstState(), states); } final String out = codeGen.getModel().render(); @@ -205,6 +215,7 @@ public class Model2Cli { } else { LOGGER.error(out); } + return 0; } @@ -222,7 +233,7 @@ public class Model2Cli { final AxReferenceKey fkey = f.getKey(); final ST val = cg.createEventFieldDefinition(kig.getPName(fkey), kig.getPVersion(fkey), kig.getLName(fkey), - kig.getName(f.getSchema()), kig.getVersion(f.getSchema()), f.getOptional()); + kig.getName(f.getSchema()), kig.getVersion(f.getSchema()), f.getOptional()); ret.add(val); } @@ -243,7 +254,7 @@ public class Model2Cli { for (final AxArtifactKey ckey : ctxs) { final ST val = cg.createTaskDefinitionContextRef(kig.getName(tkey), kig.getVersion(tkey), kig.getName(ckey), - kig.getVersion(ckey)); + kig.getVersion(ckey)); ret.add(val); } @@ -264,7 +275,7 @@ public class Model2Cli { final AxReferenceKey pkey = p.getKey(); final ST val = cg.createTaskDefinitionParameters(kig.getPName(pkey), kig.getPVersion(pkey), - kig.getLName(pkey), p.getTaskParameterValue()); + kig.getLName(pkey), p.getTaskParameterValue()); ret.add(val); } @@ -299,7 +310,7 @@ public class Model2Cli { final AxReferenceKey fkey = f.getKey(); final ST val = cg.createTaskDefinitionOutfields(kig.getPName(fkey), kig.getPVersion(fkey), - kig.getLName(fkey), kig.getName(f.getSchema()), kig.getVersion(f.getSchema())); + kig.getLName(fkey), kig.getName(f.getSchema()), kig.getVersion(f.getSchema())); ret.add(val); } @@ -320,7 +331,7 @@ public class Model2Cli { final AxReferenceKey fkey = f.getKey(); final ST val = cg.createTaskDefinitionInfields(kig.getPName(fkey), kig.getPVersion(fkey), - kig.getLName(fkey), kig.getName(f.getSchema()), kig.getVersion(f.getSchema())); + kig.getLName(fkey), kig.getName(f.getSchema()), kig.getVersion(f.getSchema())); ret.add(val); } @@ -346,8 +357,9 @@ public class Model2Cli { final List<ST> ctxRefs = getCtxtRefsForState(cg, st); final ST val = cg.createPolicyStateDef(kig.getPName(skey), kig.getPVersion(skey), kig.getLName(skey), - kig.getName(st.getTrigger()), kig.getVersion(st.getTrigger()), kig.getName(st.getDefaultTask()), - kig.getVersion(st.getDefaultTask()), outputs, tasks, tsLogic, finalizerLogics, ctxRefs); + kig.getName(st.getTrigger()), kig.getVersion(st.getTrigger()), + kig.getName(st.getDefaultTask()), kig.getVersion(st.getDefaultTask()), outputs, tasks, + tsLogic, finalizerLogics, ctxRefs); ret.add(val); } @@ -369,7 +381,7 @@ public class Model2Cli { final AxReferenceKey finkey = fin.getKey(); final ST val = cg.createPolicyStateDefFinalizerLogic(kig.getPName(skey), kig.getPVersion(skey), - kig.getLName(skey), kig.getLName(finkey), fin.getLogicFlavour(), fin.getLogic()); + kig.getLName(skey), kig.getLName(finkey), fin.getLogicFlavour(), fin.getLogic()); ret.add(val); } @@ -390,7 +402,7 @@ public class Model2Cli { for (final AxArtifactKey ctx : ctxs) { final ST val = cg.createPolicyStateDefContextRef(kig.getPName(skey), kig.getPVersion(skey), - kig.getLName(skey), kig.getName(ctx), kig.getVersion(ctx)); + kig.getLName(skey), kig.getName(ctx), kig.getVersion(ctx)); ret.add(val); } @@ -409,7 +421,7 @@ public class Model2Cli { if (st.checkSetTaskSelectionLogic()) { final AxTaskSelectionLogic tsl = st.getTaskSelectionLogic(); final ST val = cg.createPolicyStateDefTaskSelLogic(kig.getPName(skey), kig.getPVersion(skey), - kig.getLName(skey), tsl.getLogicFlavour(), tsl.getLogic()); + kig.getLName(skey), tsl.getLogicFlavour(), tsl.getLogic()); return Collections.singletonList(val); } else { return Collections.emptyList(); @@ -433,8 +445,8 @@ public class Model2Cli { final AxReferenceKey trkey = tr.getKey(); final ST val = cg.createPolicyStateTask(kig.getPName(skey), kig.getPVersion(skey), kig.getLName(skey), - kig.getLName(trkey), kig.getName(tkey), kig.getVersion(tkey), tr.getStateTaskOutputType().name(), - kig.getLName(tr.getOutput())); + kig.getLName(trkey), kig.getName(tkey), kig.getVersion(tkey), + tr.getStateTaskOutputType().name(), kig.getLName(tr.getOutput())); ret.add(val); } @@ -456,8 +468,8 @@ public class Model2Cli { final AxReferenceKey outkey = out.getKey(); final ST val = cg.createPolicyStateOutput(kig.getPName(skey), kig.getPVersion(skey), kig.getLName(skey), - kig.getLName(outkey), kig.getName(out.getOutgingEvent()), kig.getVersion(out.getOutgingEvent()), - kig.getLName(out.getNextState())); + kig.getLName(outkey), kig.getName(out.getOutgingEvent()), + kig.getVersion(out.getOutgingEvent()), kig.getLName(out.getNextState())); ret.add(val); } diff --git a/tools/model-generator/src/main/java/org/onap/policy/apex/tools/model/generator/model2event/Application.java b/tools/model-generator/src/main/java/org/onap/policy/apex/tools/model/generator/model2event/Application.java index 9735293cf..a5600ead1 100644 --- a/tools/model-generator/src/main/java/org/onap/policy/apex/tools/model/generator/model2event/Application.java +++ b/tools/model-generator/src/main/java/org/onap/policy/apex/tools/model/generator/model2event/Application.java @@ -20,6 +20,8 @@ package org.onap.policy.apex.tools.model.generator.model2event; +import java.io.PrintStream; + import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.HelpFormatter; import org.onap.policy.apex.model.basicmodel.concepts.ApexException; @@ -44,7 +46,12 @@ public final class Application { public static final String APP_DESCRIPTION = "generates JSON templates for events generated from a policy model"; /** Private constructor to prevent instantiation. */ - private Application() {} + private Application() { + } + + // Input and output streams + private static final PrintStream OUT_STREAM = System.out; + private static final PrintStream ERR_STREAM = System.err; /** * Main method to start the application. @@ -63,25 +70,34 @@ public final class Application { // help is an exit option, print usage and exit if (cmd.hasOption('h') || cmd.hasOption("help")) { final HelpFormatter formatter = new HelpFormatter(); - System.out.println(APP_NAME + " v" + cli.getAppVersion() + " - " + APP_DESCRIPTION); + OUT_STREAM.println(APP_NAME + " v" + cli.getAppVersion() + " - " + APP_DESCRIPTION); formatter.printHelp(APP_NAME, cli.getOptions()); - System.out.println(); + OUT_STREAM.println(); return; } // version is an exit option, print version and exit if (cmd.hasOption('v') || cmd.hasOption("version")) { - System.out.println(APP_NAME + " " + cli.getAppVersion()); - System.out.println(); + OUT_STREAM.println(APP_NAME + " " + cli.getAppVersion()); + OUT_STREAM.println(); return; } + generateJsonEventScheam(cmd); + } + + /** + * Generate the JSON event schema. + * + * @param cmd the command to run + */ + private static void generateJsonEventScheam(final CommandLine cmd) { String modelFile = cmd.getOptionValue('m'); if (modelFile == null) { modelFile = cmd.getOptionValue("model"); } if (modelFile == null) { - System.err.println(APP_NAME + ": no model file given, cannot proceed (try -h for help)"); + ERR_STREAM.println(APP_NAME + ": no model file given, cannot proceed (try -h for help)"); return; } @@ -90,27 +106,27 @@ public final class Application { type = cmd.getOptionValue("type"); } if (type == null) { - System.err.println(APP_NAME + ": no event type given, cannot proceed (try -h for help)"); + ERR_STREAM.println(APP_NAME + ": no event type given, cannot proceed (try -h for help)"); return; } - if (!type.equals("stimuli") && !type.equals("response") && !type.equals("internal")) { - System.err.println(APP_NAME + ": unknown type <" + type + ">, cannot proceed (try -h for help)"); + if (!"stimuli".equals(type) && !"response".equals(type) && !"internal".equals(type)) { + ERR_STREAM.println(APP_NAME + ": unknown type <" + type + ">, cannot proceed (try -h for help)"); return; } - System.out.println(); - System.out.println(APP_NAME + ": starting Event generator"); - System.out.println(" --> model file: " + modelFile); - System.out.println(" --> type: " + type); - System.out.println(); - System.out.println(); + OUT_STREAM.println(); + OUT_STREAM.println(APP_NAME + ": starting Event generator"); + OUT_STREAM.println(" --> model file: " + modelFile); + OUT_STREAM.println(" --> type: " + type); + OUT_STREAM.println(); + OUT_STREAM.println(); try { final Model2JsonEventSchema app = new Model2JsonEventSchema(modelFile, type, APP_NAME); app.runApp(); } catch (final ApexException aex) { String message = APP_NAME + ": caught APEX exception with message: " + aex.getMessage(); - System.err.println(message); + ERR_STREAM.println(message); LOGGER.warn(message, aex); } } diff --git a/tools/model-generator/src/main/java/org/onap/policy/apex/tools/model/generator/model2event/Model2JsonEventSchema.java b/tools/model-generator/src/main/java/org/onap/policy/apex/tools/model/generator/model2event/Model2JsonEventSchema.java index 6acbbbc02..cfd7cb357 100644 --- a/tools/model-generator/src/main/java/org/onap/policy/apex/tools/model/generator/model2event/Model2JsonEventSchema.java +++ b/tools/model-generator/src/main/java/org/onap/policy/apex/tools/model/generator/model2event/Model2JsonEventSchema.java @@ -82,6 +82,7 @@ public class Model2JsonEventSchema { Validate.notNull(modelFile, "Model2JsonEvent: given model file name was blank"); Validate.notNull(type, "Model2JsonEvent: given type was blank"); Validate.notNull(appName, "Model2JsonEvent: given application name was blank"); + this.modelFile = modelFile; this.type = type; this.appName = appName; @@ -124,13 +125,7 @@ public class Model2JsonEventSchema { break; case RECORD: - ret = stg.getInstanceOf("fieldTypeRecord"); - for (final Field field : schema.getFields()) { - final ST st = stg.getInstanceOf("field"); - st.add("name", field.name()); - st.add("type", this.addFieldType(field.schema(), stg)); - ret.add("fields", st); - } + ret = processRecord(schema, stg); break; case NULL: @@ -144,11 +139,29 @@ public class Model2JsonEventSchema { } /** + * Process a record entry. + * @param schema the schema to add a type for + * @param stg the STG + * @return a template with the type + */ + private ST processRecord(final Schema schema, final STGroup stg) { + ST ret; + ret = stg.getInstanceOf("fieldTypeRecord"); + for (final Field field : schema.getFields()) { + final ST st = stg.getInstanceOf("field"); + st.add("name", field.name()); + st.add("type", this.addFieldType(field.schema(), stg)); + ret.add("fields", st); + } + return ret; + } + + /** * Runs the application. * * - * @return status of the application execution, 0 for success, positive integer for exit - * condition (such as help or version), negative integer for errors + * @return status of the application execution, 0 for success, positive integer for exit condition (such as help or + * version), negative integer for errors * @throws ApexException if any problem occurred in the model */ public int runApp() throws ApexException { @@ -174,41 +187,13 @@ public class Model2JsonEventSchema { final AxPolicies policies = policyModel.getPolicies(); switch (type) { case "stimuli": - for (final AxPolicy policy : policies.getPolicyMap().values()) { - final String firsState = policy.getFirstState(); - for (final AxState state : policy.getStateMap().values()) { - if (state.getKey().getLocalName().equals(firsState)) { - eventKeys.add(state.getTrigger()); - } - } - } + processStimuli(eventKeys, policies); break; case "response": - for (final AxPolicy policy : policies.getPolicyMap().values()) { - for (final AxState state : policy.getStateMap().values()) { - if (state.getNextStateSet().iterator().next().equals("NULL")) { - for (final AxStateOutput output : state.getStateOutputs().values()) { - eventKeys.add(output.getOutgingEvent()); - } - } - } - } + processResponse(eventKeys, policies); break; case "internal": - for (final AxPolicy policy : policies.getPolicyMap().values()) { - final String firsState = policy.getFirstState(); - for (final AxState state : policy.getStateMap().values()) { - if (state.getKey().getLocalName().equals(firsState)) { - continue; - } - if (state.getNextStateSet().iterator().next().equals("NULL")) { - continue; - } - for (final AxStateOutput output : state.getStateOutputs().values()) { - eventKeys.add(output.getOutgingEvent()); - } - } - } + processInternal(eventKeys, policies); break; default: LOGGER.error("{}: unknown type <{}>, cannot proceed", appName, type); @@ -253,6 +238,79 @@ public class Model2JsonEventSchema { } /** + * Process the "stimuli" keyword. + * @param eventKeys the event keys + * @param policies the policies to process + */ + private void processStimuli(final Set<AxArtifactKey> eventKeys, final AxPolicies policies) { + for (final AxPolicy policy : policies.getPolicyMap().values()) { + final String firsState = policy.getFirstState(); + for (final AxState state : policy.getStateMap().values()) { + if (state.getKey().getLocalName().equals(firsState)) { + eventKeys.add(state.getTrigger()); + } + } + } + } + + /** + * Process the "response" keyword. + * @param eventKeys the event keys + * @param policies the policies to process + */ + private void processResponse(final Set<AxArtifactKey> eventKeys, final AxPolicies policies) { + for (final AxPolicy policy : policies.getPolicyMap().values()) { + processState(eventKeys, policy); + } + } + + /** + * Process the state in the response. + * @param eventKeys the event keys + * @param policies the policies to process + */ + private void processState(final Set<AxArtifactKey> eventKeys, final AxPolicy policy) { + for (final AxState state : policy.getStateMap().values()) { + if ("NULL".equals(state.getNextStateSet().iterator().next())) { + for (final AxStateOutput output : state.getStateOutputs().values()) { + eventKeys.add(output.getOutgingEvent()); + } + } + } + } + + /** + * Process the "internal" keyword. + * @param eventKeys the event keys + * @param policies the policies to process + */ + private void processInternal(final Set<AxArtifactKey> eventKeys, final AxPolicies policies) { + for (final AxPolicy policy : policies.getPolicyMap().values()) { + final String firsState = policy.getFirstState(); + for (final AxState state : policy.getStateMap().values()) { + processInternalState(eventKeys, firsState, state); + } + } + } + + /** + * Process the internal state. + * @param eventKeys the event keys + * @param policies the policies to process + */ + private void processInternalState(final Set<AxArtifactKey> eventKeys, final String firsState, final AxState state) { + if (state.getKey().getLocalName().equals(firsState)) { + return; + } + if ("NULL".equals(state.getNextStateSet().iterator().next())) { + return; + } + for (final AxStateOutput output : state.getStateOutputs().values()) { + eventKeys.add(output.getOutgingEvent()); + } + } + + /** * Adds a field to the output. * * @param field the field from the event |