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.CodeGeneratorCliEditor;
35 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
36 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
37 import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey;
38 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
39 import org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbum;
40 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema;
41 import org.onap.policy.apex.model.eventmodel.concepts.AxEvent;
42 import org.onap.policy.apex.model.eventmodel.concepts.AxField;
43 import org.onap.policy.apex.model.modelapi.ApexApiResult;
44 import org.onap.policy.apex.model.modelapi.ApexModel;
45 import org.onap.policy.apex.model.modelapi.ApexModelFactory;
46 import org.onap.policy.apex.model.policymodel.concepts.AxPolicy;
47 import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;
48 import org.onap.policy.apex.model.policymodel.concepts.AxState;
49 import org.onap.policy.apex.model.policymodel.concepts.AxStateFinalizerLogic;
50 import org.onap.policy.apex.model.policymodel.concepts.AxStateOutput;
51 import org.onap.policy.apex.model.policymodel.concepts.AxStateTaskReference;
52 import org.onap.policy.apex.model.policymodel.concepts.AxTask;
53 import org.onap.policy.apex.model.policymodel.concepts.AxTaskLogic;
54 import org.onap.policy.apex.model.policymodel.concepts.AxTaskParameter;
55 import org.onap.policy.apex.model.policymodel.concepts.AxTaskSelectionLogic;
56 import org.onap.policy.apex.tools.common.OutputFile;
57 import org.onap.policy.apex.tools.model.generator.KeyInfoGetter;
58 import org.slf4j.ext.XLogger;
59 import org.slf4j.ext.XLoggerFactory;
60 import org.stringtemplate.v4.ST;
63 * Takes a model and generates the JSON event schemas.
65 * @author Sven van der Meer (sven.van.der.meer@ericsson.com)
67 public class Model2Cli {
68 // Logger for this class
69 private static final XLogger LOGGER = XLoggerFactory.getXLogger(Model2Cli.class);
71 /** Application name, used as prompt. */
72 private final String appName;
74 /** The file name of the policy model. */
75 private final String modelFile;
77 /** The output file, if any. */
78 private final OutputFile outFile;
80 /** Pre-validate the model. */
81 private final boolean validate;
83 /** utility for getting key information and parsing keys etc.. */
84 private KeyInfoGetter kig = null;
87 * Creates a new model to CLI commands generator.
89 * @param modelFile the model file to be used
90 * @param outFile the out file
91 * @param validate true for model validation, false otherwise
92 * @param appName application name for printouts
94 public Model2Cli(final String modelFile, final OutputFile outFile, final boolean validate, final String appName) {
95 Validate.notNull(modelFile, "Model2Cli: given model file name was blank");
96 Validate.notNull(appName, "Model2Cli: given application name was blank");
98 this.modelFile = modelFile;
99 this.outFile = outFile;
100 this.appName = appName;
101 this.validate = validate;
105 * Runs the application.
107 * @return status of the application execution, 0 for success, positive integer for exit condition (such as help or
108 * version), negative integer for errors
109 * @throws ApexException if any problem occurred in the model
111 public int runApp() {
112 final CodeGeneratorCliEditor codeGen = new CodeGeneratorCliEditor();
114 final ApexModelFactory factory = new ApexModelFactory();
115 final ApexModel model = factory.createApexModel(new Properties(), true);
117 final ApexApiResult result = model.loadFromFile(modelFile);
118 if (result.isNok()) {
119 String message = appName + ": " + result.getMessage();
120 LOGGER.error(message);
124 final AxPolicyModel policyModel = model.getPolicyModel();
125 policyModel.register();
128 final AxValidationResult val = new AxValidationResult();
129 policyModel.validate(val);
131 String message = "Cannot translate the model. The model is not valid: \n" + val.toString();
132 LOGGER.error(message);
137 return generateCli(codeGen, policyModel);
141 * Generate the CLI from the model.
142 * @param codeGen the code generator
143 * @param policyModel the policy model
145 private int generateCli(final CodeGeneratorCliEditor codeGen, final AxPolicyModel policyModel) {
146 kig = new KeyInfoGetter(policyModel);
148 // Order is important. 0: model, 1: context schemas, 2: tasks, 3: events, 4: ContextAlbums, 5: Policies
150 final AxArtifactKey pmkey = policyModel.getKey();
151 codeGen.addModelParams(kig.getName(pmkey), kig.getVersion(pmkey), kig.getUuid(pmkey), kig.getDesc(pmkey));
153 // 1: Context Schemas
154 for (final AxContextSchema s : policyModel.getSchemas().getSchemasMap().values()) {
155 final AxArtifactKey key = s.getKey();
157 codeGen.addSchemaDeclaration(kig.getName(key), kig.getVersion(key), kig.getUuid(key), kig.getDesc(key),
158 s.getSchemaFlavour(), s.getSchema());
162 for (final AxTask t : policyModel.getTasks().getTaskMap().values()) {
163 final AxArtifactKey key = t.getKey();
164 final List<ST> infields = getInfieldsForTask(codeGen, t);
165 final List<ST> outfields = getOutfieldsForTask(codeGen, t);
166 final ST logic = getLogicForTask(codeGen, t);
167 final List<ST> parameters = getParametersForTask(codeGen, t);
168 final List<ST> contextRefs = getCtxtRefsForTask(codeGen, t);
170 codeGen.addTaskDeclaration(kig.getName(key), kig.getVersion(key), kig.getUuid(key), kig.getDesc(key),
171 infields, outfields, logic, parameters, contextRefs);
175 for (final AxEvent e : policyModel.getEvents().getEventMap().values()) {
176 final AxArtifactKey key = e.getKey();
177 final List<ST> fields = getParametersForEvent(codeGen, e);
179 codeGen.addEventDeclaration(kig.getName(key), kig.getVersion(key), kig.getUuid(key), kig.getDesc(key),
180 e.getNameSpace(), e.getSource(), e.getTarget(), fields);
184 for (final AxContextAlbum a : policyModel.getAlbums().getAlbumsMap().values()) {
185 final AxArtifactKey key = a.getKey();
187 codeGen.addContextAlbumDeclaration(kig.getName(key), kig.getVersion(key), kig.getUuid(key),
188 kig.getDesc(key), a.getScope(), a.isWritable(), kig.getName(a.getItemSchema()),
189 kig.getVersion(a.getItemSchema()));
193 for (final AxPolicy p : policyModel.getPolicies().getPolicyMap().values()) {
194 final AxArtifactKey key = p.getKey();
195 final List<ST> states = getStatesForPolicy(codeGen, p);
196 codeGen.addPolicyDefinition(kig.getName(key), kig.getVersion(key), kig.getUuid(key), kig.getDesc(key),
197 p.getTemplate(), p.getFirstState(), states);
200 final String out = codeGen.getModel().render();
201 if (outFile != null) {
202 String message = "Error writing output to file " + outFile;
204 final Writer w = outFile.toWriter();
206 LOGGER.error(message);
211 } catch (final IOException e) {
212 LOGGER.error(message, e);
223 * Gets the parameters for event.
225 * @param cg the code generator
226 * @param event the event
227 * @return the parameters for event
229 private List<ST> getParametersForEvent(final CodeGeneratorCliEditor cg, final AxEvent event) {
230 final Collection<AxField> fields = event.getFields();
231 final List<ST> ret = new ArrayList<>(fields.size());
232 for (final AxField f : fields) {
233 final AxReferenceKey fkey = f.getKey();
235 final ST val = cg.createEventFieldDefinition(kig.getPName(fkey), kig.getPVersion(fkey), kig.getLName(fkey),
236 kig.getName(f.getSchema()), kig.getVersion(f.getSchema()), f.getOptional());
244 * Gets the context references for task.
246 * @param cg the code generator
247 * @param task the task
248 * @return the context references for task
250 private List<ST> getCtxtRefsForTask(final CodeGeneratorCliEditor cg, final AxTask task) {
251 final Collection<AxArtifactKey> ctxs = task.getContextAlbumReferences();
252 final List<ST> ret = new ArrayList<>(ctxs.size());
253 final AxArtifactKey tkey = task.getKey();
254 for (final AxArtifactKey ckey : ctxs) {
256 final ST val = cg.createTaskDefinitionContextRef(kig.getName(tkey), kig.getVersion(tkey), kig.getName(ckey),
257 kig.getVersion(ckey));
265 * Gets the parameters for task.
267 * @param cg the code generator
268 * @param task the task
269 * @return the parameters for task
271 private List<ST> getParametersForTask(final CodeGeneratorCliEditor cg, final AxTask task) {
272 final Collection<AxTaskParameter> pars = task.getTaskParameters().values();
273 final List<ST> ret = new ArrayList<>(pars.size());
274 for (final AxTaskParameter p : pars) {
275 final AxReferenceKey pkey = p.getKey();
277 final ST val = cg.createTaskDefinitionParameters(kig.getPName(pkey), kig.getPVersion(pkey),
278 kig.getLName(pkey), p.getTaskParameterValue());
286 * Gets the logic for task.
288 * @param cg the code generator
289 * @param task the task
290 * @return the logic for task
292 private ST getLogicForTask(final CodeGeneratorCliEditor cg, final AxTask task) {
293 final AxArtifactKey tkey = task.getKey();
294 final AxTaskLogic tl = task.getTaskLogic();
296 return cg.createTaskDefLogic(kig.getName(tkey), kig.getVersion(tkey), tl.getLogicFlavour(), tl.getLogic());
300 * Gets the output fields for task.
302 * @param cg the code generator
303 * @param task the task
304 * @return the output fields for task
306 private List<ST> getOutfieldsForTask(final CodeGeneratorCliEditor cg, final AxTask task) {
307 final Collection<? extends AxField> fields = task.getOutputFields().values();
308 final List<ST> ret = new ArrayList<>(fields.size());
309 for (final AxField f : fields) {
310 final AxReferenceKey fkey = f.getKey();
312 final ST val = cg.createTaskDefinitionOutfields(kig.getPName(fkey), kig.getPVersion(fkey),
313 kig.getLName(fkey), kig.getName(f.getSchema()), kig.getVersion(f.getSchema()));
321 * Gets the input fields for task.
323 * @param cg the code generator
324 * @param task the task
325 * @return the input fields for task
327 private List<ST> getInfieldsForTask(final CodeGeneratorCliEditor cg, final AxTask task) {
328 final Collection<? extends AxField> fields = task.getInputFields().values();
329 final List<ST> ret = new ArrayList<>(fields.size());
330 for (final AxField f : fields) {
331 final AxReferenceKey fkey = f.getKey();
333 final ST val = cg.createTaskDefinitionInfields(kig.getPName(fkey), kig.getPVersion(fkey),
334 kig.getLName(fkey), kig.getName(f.getSchema()), kig.getVersion(f.getSchema()));
342 * Gets the states for policy.
344 * @param cg the code generator
345 * @param pol the policy
346 * @return the states for policy
348 private List<ST> getStatesForPolicy(final CodeGeneratorCliEditor cg, final AxPolicy pol) {
349 final Collection<AxState> states = pol.getStateMap().values();
350 final List<ST> ret = new ArrayList<>(states.size());
351 for (final AxState st : states) {
352 final AxReferenceKey skey = st.getKey();
353 final List<ST> outputs = getStateOutputsForState(cg, st);
354 final List<ST> finalizerLogics = getFinalizersForState(cg, st);
355 final List<ST> tasks = getTaskRefsForState(cg, st);
356 final List<ST> tsLogic = getTslForState(cg, st);
357 final List<ST> ctxRefs = getCtxtRefsForState(cg, st);
359 final ST val = cg.createPolicyStateDef(kig.getPName(skey), kig.getPVersion(skey), kig.getLName(skey),
360 kig.getName(st.getTrigger()), kig.getVersion(st.getTrigger()),
361 kig.getName(st.getDefaultTask()), kig.getVersion(st.getDefaultTask()), outputs, tasks,
362 tsLogic, finalizerLogics, ctxRefs);
370 * Gets the finalizers for state.
372 * @param cg the code generator
373 * @param st the state
374 * @return the finalizers for state
376 private List<ST> getFinalizersForState(final CodeGeneratorCliEditor cg, final AxState st) {
377 final Collection<AxStateFinalizerLogic> fins = st.getStateFinalizerLogicMap().values();
378 final List<ST> ret = new ArrayList<>(fins.size());
379 final AxReferenceKey skey = st.getKey();
380 for (final AxStateFinalizerLogic fin : fins) {
381 final AxReferenceKey finkey = fin.getKey();
383 final ST val = cg.createPolicyStateDefFinalizerLogic(kig.getPName(skey), kig.getPVersion(skey),
384 kig.getLName(skey), kig.getLName(finkey), fin.getLogicFlavour(), fin.getLogic());
392 * Gets the context references for state.
394 * @param cg the code generator
395 * @param st the state
396 * @return the context references for state
398 private List<ST> getCtxtRefsForState(final CodeGeneratorCliEditor cg, final AxState st) {
399 final Collection<AxArtifactKey> ctxs = st.getContextAlbumReferences();
400 final List<ST> ret = new ArrayList<>(ctxs.size());
401 final AxReferenceKey skey = st.getKey();
402 for (final AxArtifactKey ctx : ctxs) {
404 final ST val = cg.createPolicyStateDefContextRef(kig.getPName(skey), kig.getPVersion(skey),
405 kig.getLName(skey), kig.getName(ctx), kig.getVersion(ctx));
413 * Gets the Task Selection Logic for state.
415 * @param cg the code generator
416 * @param st the state
417 * @return the TSL for state (if any) in a list
419 private List<ST> getTslForState(final CodeGeneratorCliEditor cg, final AxState st) {
420 final AxReferenceKey skey = st.getKey();
421 if (st.checkSetTaskSelectionLogic()) {
422 final AxTaskSelectionLogic tsl = st.getTaskSelectionLogic();
423 final ST val = cg.createPolicyStateDefTaskSelLogic(kig.getPName(skey), kig.getPVersion(skey),
424 kig.getLName(skey), tsl.getLogicFlavour(), tsl.getLogic());
425 return Collections.singletonList(val);
427 return Collections.emptyList();
432 * Gets the task references for state.
434 * @param cg the code generator
435 * @param st the state
436 * @return the task references for state
438 private List<ST> getTaskRefsForState(final CodeGeneratorCliEditor cg, final AxState st) {
439 final Map<AxArtifactKey, AxStateTaskReference> taskrefs = st.getTaskReferences();
440 final List<ST> ret = new ArrayList<>(taskrefs.size());
441 final AxReferenceKey skey = st.getKey();
442 for (final Entry<AxArtifactKey, AxStateTaskReference> e : taskrefs.entrySet()) {
443 final AxArtifactKey tkey = e.getKey();
444 final AxStateTaskReference tr = e.getValue();
445 final AxReferenceKey trkey = tr.getKey();
447 final ST val = cg.createPolicyStateTask(kig.getPName(skey), kig.getPVersion(skey), kig.getLName(skey),
448 kig.getLName(trkey), kig.getName(tkey), kig.getVersion(tkey),
449 tr.getStateTaskOutputType().name(), kig.getLName(tr.getOutput()));
457 * Gets the state outputs for state.
459 * @param cg the code generator
460 * @param st the state
461 * @return the state outputs for state
463 private List<ST> getStateOutputsForState(final CodeGeneratorCliEditor cg, final AxState st) {
464 final Collection<AxStateOutput> outs = st.getStateOutputs().values();
465 final List<ST> ret = new ArrayList<>(outs.size());
466 final AxReferenceKey skey = st.getKey();
467 for (final AxStateOutput out : outs) {
468 final AxReferenceKey outkey = out.getKey();
470 final ST val = cg.createPolicyStateOutput(kig.getPName(skey), kig.getPVersion(skey), kig.getLName(skey),
471 kig.getLName(outkey), kig.getName(out.getOutgingEvent()),
472 kig.getVersion(out.getOutgingEvent()), kig.getLName(out.getNextState()));