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.auth.clicodegen.EventDeclarationBuilder;
37 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
38 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
39 import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey;
40 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
41 import org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbum;
42 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema;
43 import org.onap.policy.apex.model.eventmodel.concepts.AxEvent;
44 import org.onap.policy.apex.model.eventmodel.concepts.AxField;
45 import org.onap.policy.apex.model.modelapi.ApexApiResult;
46 import org.onap.policy.apex.model.modelapi.ApexModel;
47 import org.onap.policy.apex.model.modelapi.ApexModelFactory;
48 import org.onap.policy.apex.model.policymodel.concepts.AxPolicy;
49 import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;
50 import org.onap.policy.apex.model.policymodel.concepts.AxState;
51 import org.onap.policy.apex.model.policymodel.concepts.AxStateFinalizerLogic;
52 import org.onap.policy.apex.model.policymodel.concepts.AxStateOutput;
53 import org.onap.policy.apex.model.policymodel.concepts.AxStateTaskReference;
54 import org.onap.policy.apex.model.policymodel.concepts.AxTask;
55 import org.onap.policy.apex.model.policymodel.concepts.AxTaskLogic;
56 import org.onap.policy.apex.model.policymodel.concepts.AxTaskParameter;
57 import org.onap.policy.apex.model.policymodel.concepts.AxTaskSelectionLogic;
58 import org.onap.policy.apex.tools.common.OutputFile;
59 import org.onap.policy.apex.tools.model.generator.KeyInfoGetter;
60 import org.slf4j.ext.XLogger;
61 import org.slf4j.ext.XLoggerFactory;
62 import org.stringtemplate.v4.ST;
65 * Takes a model and generates the JSON event schemas.
67 * @author Sven van der Meer (sven.van.der.meer@ericsson.com)
69 public class Model2Cli {
70 // Logger for this class
71 private static final XLogger LOGGER = XLoggerFactory.getXLogger(Model2Cli.class);
73 /** Application name, used as prompt. */
74 private final String appName;
76 /** The file name of the policy model. */
77 private final String modelFile;
79 /** The output file, if any. */
80 private final OutputFile outFile;
82 /** Pre-validate the model. */
83 private final boolean validate;
85 /** utility for getting key information and parsing keys etc.. */
86 private KeyInfoGetter kig = null;
89 * Creates a new model to CLI commands generator.
91 * @param modelFile the model file to be used
92 * @param outFile the out file
93 * @param validate true for model validation, false otherwise
94 * @param appName application name for printouts
96 public Model2Cli(final String modelFile, final OutputFile outFile, final boolean validate, final String appName) {
97 Validate.notNull(modelFile, "Model2Cli: given model file name was blank");
98 Validate.notNull(appName, "Model2Cli: given application name was blank");
100 this.modelFile = modelFile;
101 this.outFile = outFile;
102 this.appName = appName;
103 this.validate = validate;
107 * Runs the application.
109 * @return status of the application execution, 0 for success, positive integer for exit condition (such as help or
110 * version), negative integer for errors
111 * @throws ApexException if any problem occurred in the model
113 public int runApp() {
114 final CodeGeneratorCliEditor codeGen = new CodeGeneratorCliEditor();
116 final ApexModelFactory factory = new ApexModelFactory();
117 final ApexModel model = factory.createApexModel(new Properties(), true);
119 final ApexApiResult result = model.loadFromFile(modelFile);
120 if (result.isNok()) {
121 String message = appName + ": " + result.getMessage();
122 LOGGER.error(message);
126 final AxPolicyModel policyModel = model.getPolicyModel();
127 policyModel.register();
130 final AxValidationResult val = new AxValidationResult();
131 policyModel.validate(val);
133 String message = "Cannot translate the model. The model is not valid: \n" + val.toString();
134 LOGGER.error(message);
139 return generateCli(codeGen, policyModel);
143 * Generate the CLI from the model.
144 * @param codeGen the code generator
145 * @param policyModel the policy model
147 private int generateCli(final CodeGeneratorCliEditor codeGen, final AxPolicyModel policyModel) {
148 kig = new KeyInfoGetter(policyModel);
150 // Order is important. 0: model, 1: context schemas, 2: tasks, 3: events, 4: ContextAlbums, 5: Policies
152 final AxArtifactKey pmkey = policyModel.getKey();
153 codeGen.addModelParams(kig.getName(pmkey), kig.getVersion(pmkey), kig.getUuid(pmkey), kig.getDesc(pmkey));
155 // 1: Context Schemas
156 for (final AxContextSchema s : policyModel.getSchemas().getSchemasMap().values()) {
157 final AxArtifactKey key = s.getKey();
159 codeGen.addSchemaDeclaration(kig.getName(key), kig.getVersion(key), kig.getUuid(key), kig.getDesc(key),
160 s.getSchemaFlavour(), s.getSchema());
164 for (final AxTask t : policyModel.getTasks().getTaskMap().values()) {
165 final AxArtifactKey key = t.getKey();
166 final List<ST> infields = getInfieldsForTask(codeGen, t);
167 final List<ST> outfields = getOutfieldsForTask(codeGen, t);
168 final ST logic = getLogicForTask(codeGen, t);
169 final List<ST> parameters = getParametersForTask(codeGen, t);
170 final List<ST> contextRefs = getCtxtRefsForTask(codeGen, t);
172 codeGen.addTaskDeclaration(kig.getName(key), kig.getVersion(key), kig.getUuid(key), kig.getDesc(key),
173 infields, outfields, logic, parameters, contextRefs);
177 for (final AxEvent e : policyModel.getEvents().getEventMap().values()) {
178 final AxArtifactKey key = e.getKey();
179 final List<ST> fields = getParametersForEvent(codeGen, e);
181 codeGen.addEventDeclaration(
182 new EventDeclarationBuilder()
183 .setName(kig.getName(key))
184 .setVersion(kig.getVersion(key))
185 .setUuid(kig.getUuid(key))
186 .setDescription(kig.getDesc(key))
187 .setNameSpace(e.getNameSpace())
188 .setSource(e.getSource())
189 .setTarget(e.getTarget())
194 for (final AxContextAlbum a : policyModel.getAlbums().getAlbumsMap().values()) {
195 final AxArtifactKey key = a.getKey();
197 codeGen.addContextAlbumDeclaration(
198 new CodeGenCliEditorBuilder()
199 .setName(kig.getName(key))
200 .setVersion(kig.getVersion(key))
201 .setUuid(kig.getUuid(key))
202 .setDescription(kig.getDesc(key))
203 .setScope(a.getScope())
204 .setWritable(a.isWritable())
205 .setSchemaName(kig.getName(a.getItemSchema()))
206 .setSchemaVersion(kig.getVersion(a.getItemSchema())));
210 for (final AxPolicy p : policyModel.getPolicies().getPolicyMap().values()) {
211 final AxArtifactKey key = p.getKey();
212 final List<ST> states = getStatesForPolicy(codeGen, p);
213 codeGen.addPolicyDefinition(kig.getName(key), kig.getVersion(key), kig.getUuid(key), kig.getDesc(key),
214 p.getTemplate(), p.getFirstState(), states);
217 final String out = codeGen.getModel().render();
218 if (outFile != null) {
219 String message = "Error writing output to file " + outFile;
221 final Writer w = outFile.toWriter();
223 LOGGER.error(message);
228 } catch (final IOException e) {
229 LOGGER.error(message, e);
240 * Gets the parameters for event.
242 * @param cg the code generator
243 * @param event the event
244 * @return the parameters for event
246 private List<ST> getParametersForEvent(final CodeGeneratorCliEditor cg, final AxEvent event) {
247 final Collection<AxField> fields = event.getFields();
248 final List<ST> ret = new ArrayList<>(fields.size());
249 for (final AxField f : fields) {
250 final AxReferenceKey fkey = f.getKey();
252 final ST val = cg.createEventFieldDefinition(kig.getPName(fkey), kig.getPVersion(fkey), kig.getLName(fkey),
253 kig.getName(f.getSchema()), kig.getVersion(f.getSchema()), f.getOptional());
261 * Gets the context references for task.
263 * @param cg the code generator
264 * @param task the task
265 * @return the context references for task
267 private List<ST> getCtxtRefsForTask(final CodeGeneratorCliEditor cg, final AxTask task) {
268 final Collection<AxArtifactKey> ctxs = task.getContextAlbumReferences();
269 final List<ST> ret = new ArrayList<>(ctxs.size());
270 final AxArtifactKey tkey = task.getKey();
271 for (final AxArtifactKey ckey : ctxs) {
273 final ST val = cg.createTaskDefinitionContextRef(kig.getName(tkey), kig.getVersion(tkey), kig.getName(ckey),
274 kig.getVersion(ckey));
282 * Gets the parameters for task.
284 * @param cg the code generator
285 * @param task the task
286 * @return the parameters for task
288 private List<ST> getParametersForTask(final CodeGeneratorCliEditor cg, final AxTask task) {
289 final Collection<AxTaskParameter> pars = task.getTaskParameters().values();
290 final List<ST> ret = new ArrayList<>(pars.size());
291 for (final AxTaskParameter p : pars) {
292 final AxReferenceKey pkey = p.getKey();
294 final ST val = cg.createTaskDefinitionParameters(kig.getPName(pkey), kig.getPVersion(pkey),
295 kig.getLName(pkey), p.getTaskParameterValue());
303 * Gets the logic for task.
305 * @param cg the code generator
306 * @param task the task
307 * @return the logic for task
309 private ST getLogicForTask(final CodeGeneratorCliEditor cg, final AxTask task) {
310 final AxArtifactKey tkey = task.getKey();
311 final AxTaskLogic tl = task.getTaskLogic();
313 return cg.createTaskDefLogic(kig.getName(tkey), kig.getVersion(tkey), tl.getLogicFlavour(), tl.getLogic());
317 * Gets the output fields for task.
319 * @param cg the code generator
320 * @param task the task
321 * @return the output fields for task
323 private List<ST> getOutfieldsForTask(final CodeGeneratorCliEditor cg, final AxTask task) {
324 final Collection<? extends AxField> fields = task.getOutputFields().values();
325 final List<ST> ret = new ArrayList<>(fields.size());
326 for (final AxField f : fields) {
327 final AxReferenceKey fkey = f.getKey();
329 final ST val = cg.createTaskDefinitionOutfields(kig.getPName(fkey), kig.getPVersion(fkey),
330 kig.getLName(fkey), kig.getName(f.getSchema()), kig.getVersion(f.getSchema()));
338 * Gets the input fields for task.
340 * @param cg the code generator
341 * @param task the task
342 * @return the input fields for task
344 private List<ST> getInfieldsForTask(final CodeGeneratorCliEditor cg, final AxTask task) {
345 final Collection<? extends AxField> fields = task.getInputFields().values();
346 final List<ST> ret = new ArrayList<>(fields.size());
347 for (final AxField f : fields) {
348 final AxReferenceKey fkey = f.getKey();
350 final ST val = cg.createTaskDefinitionInfields(kig.getPName(fkey), kig.getPVersion(fkey),
351 kig.getLName(fkey), kig.getName(f.getSchema()), kig.getVersion(f.getSchema()));
359 * Gets the states for policy.
361 * @param cg the code generator
362 * @param pol the policy
363 * @return the states for policy
365 private List<ST> getStatesForPolicy(final CodeGeneratorCliEditor cg, final AxPolicy pol) {
366 final Collection<AxState> states = pol.getStateMap().values();
367 final List<ST> ret = new ArrayList<>(states.size());
368 for (final AxState st : states) {
369 final AxReferenceKey skey = st.getKey();
370 final List<ST> outputs = getStateOutputsForState(cg, st);
371 final List<ST> finalizerLogics = getFinalizersForState(cg, st);
372 final List<ST> tasks = getTaskRefsForState(cg, st);
373 final List<ST> tsLogic = getTslForState(cg, st);
374 final List<ST> ctxRefs = getCtxtRefsForState(cg, st);
376 final ST val = cg.createPolicyStateDef(kig.getPName(skey), kig.getPVersion(skey), kig.getLName(skey),
377 kig.getName(st.getTrigger()), kig.getVersion(st.getTrigger()),
378 kig.getName(st.getDefaultTask()), kig.getVersion(st.getDefaultTask()), outputs, tasks,
379 tsLogic, finalizerLogics, ctxRefs);
387 * Gets the finalizers for state.
389 * @param cg the code generator
390 * @param st the state
391 * @return the finalizers for state
393 private List<ST> getFinalizersForState(final CodeGeneratorCliEditor cg, final AxState st) {
394 final Collection<AxStateFinalizerLogic> fins = st.getStateFinalizerLogicMap().values();
395 final List<ST> ret = new ArrayList<>(fins.size());
396 final AxReferenceKey skey = st.getKey();
397 for (final AxStateFinalizerLogic fin : fins) {
398 final AxReferenceKey finkey = fin.getKey();
400 final ST val = cg.createPolicyStateDefFinalizerLogic(kig.getPName(skey), kig.getPVersion(skey),
401 kig.getLName(skey), kig.getLName(finkey), fin.getLogicFlavour(), fin.getLogic());
409 * Gets the context references for state.
411 * @param cg the code generator
412 * @param st the state
413 * @return the context references for state
415 private List<ST> getCtxtRefsForState(final CodeGeneratorCliEditor cg, final AxState st) {
416 final Collection<AxArtifactKey> ctxs = st.getContextAlbumReferences();
417 final List<ST> ret = new ArrayList<>(ctxs.size());
418 final AxReferenceKey skey = st.getKey();
419 for (final AxArtifactKey ctx : ctxs) {
421 final ST val = cg.createPolicyStateDefContextRef(kig.getPName(skey), kig.getPVersion(skey),
422 kig.getLName(skey), kig.getName(ctx), kig.getVersion(ctx));
430 * Gets the Task Selection Logic for state.
432 * @param cg the code generator
433 * @param st the state
434 * @return the TSL for state (if any) in a list
436 private List<ST> getTslForState(final CodeGeneratorCliEditor cg, final AxState st) {
437 final AxReferenceKey skey = st.getKey();
438 if (st.checkSetTaskSelectionLogic()) {
439 final AxTaskSelectionLogic tsl = st.getTaskSelectionLogic();
440 final ST val = cg.createPolicyStateDefTaskSelLogic(kig.getPName(skey), kig.getPVersion(skey),
441 kig.getLName(skey), tsl.getLogicFlavour(), tsl.getLogic());
442 return Collections.singletonList(val);
444 return Collections.emptyList();
449 * Gets the task references for state.
451 * @param cg the code generator
452 * @param st the state
453 * @return the task references for state
455 private List<ST> getTaskRefsForState(final CodeGeneratorCliEditor cg, final AxState st) {
456 final Map<AxArtifactKey, AxStateTaskReference> taskrefs = st.getTaskReferences();
457 final List<ST> ret = new ArrayList<>(taskrefs.size());
458 final AxReferenceKey skey = st.getKey();
459 for (final Entry<AxArtifactKey, AxStateTaskReference> e : taskrefs.entrySet()) {
460 final AxArtifactKey tkey = e.getKey();
461 final AxStateTaskReference tr = e.getValue();
462 final AxReferenceKey trkey = tr.getKey();
464 final ST val = cg.createPolicyStateTask(kig.getPName(skey), kig.getPVersion(skey), kig.getLName(skey),
465 kig.getLName(trkey), kig.getName(tkey), kig.getVersion(tkey),
466 tr.getStateTaskOutputType().name(), kig.getLName(tr.getOutput()));
474 * Gets the state outputs for state.
476 * @param cg the code generator
477 * @param st the state
478 * @return the state outputs for state
480 private List<ST> getStateOutputsForState(final CodeGeneratorCliEditor cg, final AxState st) {
481 final Collection<AxStateOutput> outs = st.getStateOutputs().values();
482 final List<ST> ret = new ArrayList<>(outs.size());
483 final AxReferenceKey skey = st.getKey();
484 for (final AxStateOutput out : outs) {
485 final AxReferenceKey outkey = out.getKey();
487 final ST val = cg.createPolicyStateOutput(kig.getPName(skey), kig.getPVersion(skey), kig.getLName(skey),
488 kig.getLName(outkey), kig.getName(out.getOutgingEvent()),
489 kig.getVersion(out.getOutgingEvent()), kig.getLName(out.getNextState()));