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.model2cli;
23 import java.io.IOException;
24 import java.io.Writer;
25 import java.util.ArrayList;
26 import java.util.Collection;
27 import java.util.Collections;
28 import java.util.List;
30 import java.util.Map.Entry;
31 import java.util.Properties;
33 import org.apache.commons.lang3.Validate;
34 import org.onap.policy.apex.auth.clicodegen.CodeGenCliEditorBuilder;
35 import org.onap.policy.apex.auth.clicodegen.CodeGeneratorCliEditor;
36 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
37 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
38 import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey;
39 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
40 import org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbum;
41 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema;
42 import org.onap.policy.apex.model.eventmodel.concepts.AxEvent;
43 import org.onap.policy.apex.model.eventmodel.concepts.AxField;
44 import org.onap.policy.apex.model.modelapi.ApexApiResult;
45 import org.onap.policy.apex.model.modelapi.ApexModel;
46 import org.onap.policy.apex.model.modelapi.ApexModelFactory;
47 import org.onap.policy.apex.model.policymodel.concepts.AxPolicy;
48 import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;
49 import org.onap.policy.apex.model.policymodel.concepts.AxState;
50 import org.onap.policy.apex.model.policymodel.concepts.AxStateFinalizerLogic;
51 import org.onap.policy.apex.model.policymodel.concepts.AxStateOutput;
52 import org.onap.policy.apex.model.policymodel.concepts.AxStateTaskReference;
53 import org.onap.policy.apex.model.policymodel.concepts.AxTask;
54 import org.onap.policy.apex.model.policymodel.concepts.AxTaskLogic;
55 import org.onap.policy.apex.model.policymodel.concepts.AxTaskParameter;
56 import org.onap.policy.apex.model.policymodel.concepts.AxTaskSelectionLogic;
57 import org.onap.policy.apex.tools.common.OutputFile;
58 import org.onap.policy.apex.tools.model.generator.KeyInfoGetter;
59 import org.slf4j.ext.XLogger;
60 import org.slf4j.ext.XLoggerFactory;
61 import org.stringtemplate.v4.ST;
64 * Takes a model and generates the JSON event schemas.
66 * @author Sven van der Meer (sven.van.der.meer@ericsson.com)
68 public class Model2Cli {
69 // Logger for this class
70 private static final XLogger LOGGER = XLoggerFactory.getXLogger(Model2Cli.class);
72 /** Application name, used as prompt. */
73 private final String appName;
75 /** The file name of the policy model. */
76 private final String modelFile;
78 /** The output file, if any. */
79 private final OutputFile outFile;
81 /** Pre-validate the model. */
82 private final boolean validate;
84 /** utility for getting key information and parsing keys etc.. */
85 private KeyInfoGetter kig = null;
88 * Creates a new model to CLI commands generator.
90 * @param modelFile the model file to be used
91 * @param outFile the out file
92 * @param validate true for model validation, false otherwise
93 * @param appName application name for printouts
95 public Model2Cli(final String modelFile, final OutputFile outFile, final boolean validate, final String appName) {
96 Validate.notNull(modelFile, "Model2Cli: given model file name was blank");
97 Validate.notNull(appName, "Model2Cli: given application name was blank");
99 this.modelFile = modelFile;
100 this.outFile = outFile;
101 this.appName = appName;
102 this.validate = validate;
106 * Runs the application.
108 * @return status of the application execution, 0 for success, positive integer for exit condition (such as help or
109 * version), negative integer for errors
110 * @throws ApexException if any problem occurred in the model
112 public int runApp() {
113 final CodeGeneratorCliEditor codeGen = new CodeGeneratorCliEditor();
115 final ApexModelFactory factory = new ApexModelFactory();
116 final ApexModel model = factory.createApexModel(new Properties(), true);
118 final ApexApiResult result = model.loadFromFile(modelFile);
119 if (result.isNok()) {
120 String message = appName + ": " + result.getMessage();
121 LOGGER.error(message);
125 final AxPolicyModel policyModel = model.getPolicyModel();
126 policyModel.register();
129 final AxValidationResult val = new AxValidationResult();
130 policyModel.validate(val);
132 String message = "Cannot translate the model. The model is not valid: \n" + val.toString();
133 LOGGER.error(message);
138 return generateCli(codeGen, policyModel);
142 * Generate the CLI from the model.
143 * @param codeGen the code generator
144 * @param policyModel the policy model
146 private int generateCli(final CodeGeneratorCliEditor codeGen, final AxPolicyModel policyModel) {
147 kig = new KeyInfoGetter(policyModel);
149 // Order is important. 0: model, 1: context schemas, 2: tasks, 3: events, 4: ContextAlbums, 5: Policies
151 final AxArtifactKey pmkey = policyModel.getKey();
152 codeGen.addModelParams(kig.getName(pmkey), kig.getVersion(pmkey), kig.getUuid(pmkey), kig.getDesc(pmkey));
154 // 1: Context Schemas
155 for (final AxContextSchema s : policyModel.getSchemas().getSchemasMap().values()) {
156 final AxArtifactKey key = s.getKey();
158 codeGen.addSchemaDeclaration(kig.getName(key), kig.getVersion(key), kig.getUuid(key), kig.getDesc(key),
159 s.getSchemaFlavour(), s.getSchema());
163 for (final AxTask t : policyModel.getTasks().getTaskMap().values()) {
164 final AxArtifactKey key = t.getKey();
165 final List<ST> infields = getInfieldsForTask(codeGen, t);
166 final List<ST> outfields = getOutfieldsForTask(codeGen, t);
167 final ST logic = getLogicForTask(codeGen, t);
168 final List<ST> parameters = getParametersForTask(codeGen, t);
169 final List<ST> contextRefs = getCtxtRefsForTask(codeGen, t);
171 codeGen.addTaskDeclaration(kig.getName(key), kig.getVersion(key), kig.getUuid(key), kig.getDesc(key),
172 infields, outfields, logic, parameters, contextRefs);
176 for (final AxEvent e : policyModel.getEvents().getEventMap().values()) {
177 final AxArtifactKey key = e.getKey();
178 final List<ST> fields = getParametersForEvent(codeGen, e);
180 codeGen.addEventDeclaration(kig.getName(key), kig.getVersion(key), kig.getUuid(key), kig.getDesc(key),
181 e.getNameSpace(), e.getSource(), e.getTarget(), fields);
185 for (final AxContextAlbum a : policyModel.getAlbums().getAlbumsMap().values()) {
186 final AxArtifactKey key = a.getKey();
188 codeGen.addContextAlbumDeclaration(
189 new CodeGenCliEditorBuilder()
190 .setName(kig.getName(key))
191 .setVersion(kig.getVersion(key))
192 .setUuid(kig.getUuid(key))
193 .setDescription(kig.getDesc(key))
194 .setScope(a.getScope())
195 .setWritable(a.isWritable())
196 .setSchemaName(kig.getName(a.getItemSchema()))
197 .setSchemaVersion(kig.getVersion(a.getItemSchema())));
201 for (final AxPolicy p : policyModel.getPolicies().getPolicyMap().values()) {
202 final AxArtifactKey key = p.getKey();
203 final List<ST> states = getStatesForPolicy(codeGen, p);
204 codeGen.addPolicyDefinition(kig.getName(key), kig.getVersion(key), kig.getUuid(key), kig.getDesc(key),
205 p.getTemplate(), p.getFirstState(), states);
208 final String out = codeGen.getModel().render();
209 if (outFile != null) {
210 String message = "Error writing output to file " + outFile;
212 final Writer w = outFile.toWriter();
214 LOGGER.error(message);
219 } catch (final IOException e) {
220 LOGGER.error(message, e);
231 * Gets the parameters for event.
233 * @param cg the code generator
234 * @param event the event
235 * @return the parameters for event
237 private List<ST> getParametersForEvent(final CodeGeneratorCliEditor cg, final AxEvent event) {
238 final Collection<AxField> fields = event.getFields();
239 final List<ST> ret = new ArrayList<>(fields.size());
240 for (final AxField f : fields) {
241 final AxReferenceKey fkey = f.getKey();
243 final ST val = cg.createEventFieldDefinition(kig.getPName(fkey), kig.getPVersion(fkey), kig.getLName(fkey),
244 kig.getName(f.getSchema()), kig.getVersion(f.getSchema()), f.getOptional());
252 * Gets the context references for task.
254 * @param cg the code generator
255 * @param task the task
256 * @return the context references for task
258 private List<ST> getCtxtRefsForTask(final CodeGeneratorCliEditor cg, final AxTask task) {
259 final Collection<AxArtifactKey> ctxs = task.getContextAlbumReferences();
260 final List<ST> ret = new ArrayList<>(ctxs.size());
261 final AxArtifactKey tkey = task.getKey();
262 for (final AxArtifactKey ckey : ctxs) {
264 final ST val = cg.createTaskDefinitionContextRef(kig.getName(tkey), kig.getVersion(tkey), kig.getName(ckey),
265 kig.getVersion(ckey));
273 * Gets the parameters for task.
275 * @param cg the code generator
276 * @param task the task
277 * @return the parameters for task
279 private List<ST> getParametersForTask(final CodeGeneratorCliEditor cg, final AxTask task) {
280 final Collection<AxTaskParameter> pars = task.getTaskParameters().values();
281 final List<ST> ret = new ArrayList<>(pars.size());
282 for (final AxTaskParameter p : pars) {
283 final AxReferenceKey pkey = p.getKey();
285 final ST val = cg.createTaskDefinitionParameters(kig.getPName(pkey), kig.getPVersion(pkey),
286 kig.getLName(pkey), p.getTaskParameterValue());
294 * Gets the logic for task.
296 * @param cg the code generator
297 * @param task the task
298 * @return the logic for task
300 private ST getLogicForTask(final CodeGeneratorCliEditor cg, final AxTask task) {
301 final AxArtifactKey tkey = task.getKey();
302 final AxTaskLogic tl = task.getTaskLogic();
304 return cg.createTaskDefLogic(kig.getName(tkey), kig.getVersion(tkey), tl.getLogicFlavour(), tl.getLogic());
308 * Gets the output fields for task.
310 * @param cg the code generator
311 * @param task the task
312 * @return the output fields for task
314 private List<ST> getOutfieldsForTask(final CodeGeneratorCliEditor cg, final AxTask task) {
315 final Collection<? extends AxField> fields = task.getOutputFields().values();
316 final List<ST> ret = new ArrayList<>(fields.size());
317 for (final AxField f : fields) {
318 final AxReferenceKey fkey = f.getKey();
320 final ST val = cg.createTaskDefinitionOutfields(kig.getPName(fkey), kig.getPVersion(fkey),
321 kig.getLName(fkey), kig.getName(f.getSchema()), kig.getVersion(f.getSchema()));
329 * Gets the input fields for task.
331 * @param cg the code generator
332 * @param task the task
333 * @return the input fields for task
335 private List<ST> getInfieldsForTask(final CodeGeneratorCliEditor cg, final AxTask task) {
336 final Collection<? extends AxField> fields = task.getInputFields().values();
337 final List<ST> ret = new ArrayList<>(fields.size());
338 for (final AxField f : fields) {
339 final AxReferenceKey fkey = f.getKey();
341 final ST val = cg.createTaskDefinitionInfields(kig.getPName(fkey), kig.getPVersion(fkey),
342 kig.getLName(fkey), kig.getName(f.getSchema()), kig.getVersion(f.getSchema()));
350 * Gets the states for policy.
352 * @param cg the code generator
353 * @param pol the policy
354 * @return the states for policy
356 private List<ST> getStatesForPolicy(final CodeGeneratorCliEditor cg, final AxPolicy pol) {
357 final Collection<AxState> states = pol.getStateMap().values();
358 final List<ST> ret = new ArrayList<>(states.size());
359 for (final AxState st : states) {
360 final AxReferenceKey skey = st.getKey();
361 final List<ST> outputs = getStateOutputsForState(cg, st);
362 final List<ST> finalizerLogics = getFinalizersForState(cg, st);
363 final List<ST> tasks = getTaskRefsForState(cg, st);
364 final List<ST> tsLogic = getTslForState(cg, st);
365 final List<ST> ctxRefs = getCtxtRefsForState(cg, st);
367 final ST val = cg.createPolicyStateDef(kig.getPName(skey), kig.getPVersion(skey), kig.getLName(skey),
368 kig.getName(st.getTrigger()), kig.getVersion(st.getTrigger()),
369 kig.getName(st.getDefaultTask()), kig.getVersion(st.getDefaultTask()), outputs, tasks,
370 tsLogic, finalizerLogics, ctxRefs);
378 * Gets the finalizers for state.
380 * @param cg the code generator
381 * @param st the state
382 * @return the finalizers for state
384 private List<ST> getFinalizersForState(final CodeGeneratorCliEditor cg, final AxState st) {
385 final Collection<AxStateFinalizerLogic> fins = st.getStateFinalizerLogicMap().values();
386 final List<ST> ret = new ArrayList<>(fins.size());
387 final AxReferenceKey skey = st.getKey();
388 for (final AxStateFinalizerLogic fin : fins) {
389 final AxReferenceKey finkey = fin.getKey();
391 final ST val = cg.createPolicyStateDefFinalizerLogic(kig.getPName(skey), kig.getPVersion(skey),
392 kig.getLName(skey), kig.getLName(finkey), fin.getLogicFlavour(), fin.getLogic());
400 * Gets the context references for state.
402 * @param cg the code generator
403 * @param st the state
404 * @return the context references for state
406 private List<ST> getCtxtRefsForState(final CodeGeneratorCliEditor cg, final AxState st) {
407 final Collection<AxArtifactKey> ctxs = st.getContextAlbumReferences();
408 final List<ST> ret = new ArrayList<>(ctxs.size());
409 final AxReferenceKey skey = st.getKey();
410 for (final AxArtifactKey ctx : ctxs) {
412 final ST val = cg.createPolicyStateDefContextRef(kig.getPName(skey), kig.getPVersion(skey),
413 kig.getLName(skey), kig.getName(ctx), kig.getVersion(ctx));
421 * Gets the Task Selection Logic for state.
423 * @param cg the code generator
424 * @param st the state
425 * @return the TSL for state (if any) in a list
427 private List<ST> getTslForState(final CodeGeneratorCliEditor cg, final AxState st) {
428 final AxReferenceKey skey = st.getKey();
429 if (st.checkSetTaskSelectionLogic()) {
430 final AxTaskSelectionLogic tsl = st.getTaskSelectionLogic();
431 final ST val = cg.createPolicyStateDefTaskSelLogic(kig.getPName(skey), kig.getPVersion(skey),
432 kig.getLName(skey), tsl.getLogicFlavour(), tsl.getLogic());
433 return Collections.singletonList(val);
435 return Collections.emptyList();
440 * Gets the task references for state.
442 * @param cg the code generator
443 * @param st the state
444 * @return the task references for state
446 private List<ST> getTaskRefsForState(final CodeGeneratorCliEditor cg, final AxState st) {
447 final Map<AxArtifactKey, AxStateTaskReference> taskrefs = st.getTaskReferences();
448 final List<ST> ret = new ArrayList<>(taskrefs.size());
449 final AxReferenceKey skey = st.getKey();
450 for (final Entry<AxArtifactKey, AxStateTaskReference> e : taskrefs.entrySet()) {
451 final AxArtifactKey tkey = e.getKey();
452 final AxStateTaskReference tr = e.getValue();
453 final AxReferenceKey trkey = tr.getKey();
455 final ST val = cg.createPolicyStateTask(kig.getPName(skey), kig.getPVersion(skey), kig.getLName(skey),
456 kig.getLName(trkey), kig.getName(tkey), kig.getVersion(tkey),
457 tr.getStateTaskOutputType().name(), kig.getLName(tr.getOutput()));
465 * Gets the state outputs for state.
467 * @param cg the code generator
468 * @param st the state
469 * @return the state outputs for state
471 private List<ST> getStateOutputsForState(final CodeGeneratorCliEditor cg, final AxState st) {
472 final Collection<AxStateOutput> outs = st.getStateOutputs().values();
473 final List<ST> ret = new ArrayList<>(outs.size());
474 final AxReferenceKey skey = st.getKey();
475 for (final AxStateOutput out : outs) {
476 final AxReferenceKey outkey = out.getKey();
478 final ST val = cg.createPolicyStateOutput(kig.getPName(skey), kig.getPVersion(skey), kig.getLName(skey),
479 kig.getLName(outkey), kig.getName(out.getOutgingEvent()),
480 kig.getVersion(out.getOutgingEvent()), kig.getLName(out.getNextState()));