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");
97 this.modelFile = modelFile;
98 this.outFile = outFile;
99 this.appName = appName;
100 this.validate = validate;
104 * Runs the application.
106 * @return status of the application execution, 0 for success, positive integer for exit condition (such as help or
107 * version), negative integer for errors
108 * @throws ApexException if any problem occurred in the model
110 public int runApp() throws ApexException {
111 final CodeGeneratorCliEditor codeGen = new CodeGeneratorCliEditor();
113 final ApexModelFactory factory = new ApexModelFactory();
114 final ApexModel model = factory.createApexModel(new Properties(), true);
116 final ApexApiResult result = model.loadFromFile(modelFile);
117 if (result.isNok()) {
118 String message = appName + ": " + result.getMessage();
119 LOGGER.error(message);
123 final AxPolicyModel policyModel = model.getPolicyModel();
124 policyModel.register();
127 final AxValidationResult val = new AxValidationResult();
128 policyModel.validate(val);
130 String message = "Cannot translate the model. The model is not valid: \n" + val.toString();
131 LOGGER.error(message);
136 kig = new KeyInfoGetter(policyModel);
138 // Order is important. 0: model, 1: context schemas, 2: tasks, 3: events, 4: ContextAlbums, 5: Policies
140 final AxArtifactKey pmkey = policyModel.getKey();
141 codeGen.addModelParams(kig.getName(pmkey), kig.getVersion(pmkey), kig.getUuid(pmkey), kig.getDesc(pmkey));
143 // 1: Context Schemas
144 for (final AxContextSchema s : policyModel.getSchemas().getSchemasMap().values()) {
145 final AxArtifactKey key = s.getKey();
147 codeGen.addSchemaDeclaration(kig.getName(key), kig.getVersion(key), kig.getUuid(key), kig.getDesc(key),
148 s.getSchemaFlavour(), s.getSchema());
152 for (final AxTask t : policyModel.getTasks().getTaskMap().values()) {
153 final AxArtifactKey key = t.getKey();
154 final List<ST> infields = getInfieldsForTask(codeGen, t);
155 final List<ST> outfields = getOutfieldsForTask(codeGen, t);
156 final ST logic = getLogicForTask(codeGen, t);
157 final List<ST> parameters = getParametersForTask(codeGen, t);
158 final List<ST> contextRefs = getCtxtRefsForTask(codeGen, t);
160 codeGen.addTaskDeclaration(kig.getName(key), kig.getVersion(key), kig.getUuid(key), kig.getDesc(key),
161 infields, outfields, logic, parameters, contextRefs);
165 for (final AxEvent e : policyModel.getEvents().getEventMap().values()) {
166 final AxArtifactKey key = e.getKey();
167 final List<ST> fields = getParametersForEvent(codeGen, e);
169 codeGen.addEventDeclaration(kig.getName(key), kig.getVersion(key), kig.getUuid(key), kig.getDesc(key),
170 e.getNameSpace(), e.getSource(), e.getTarget(), fields);
174 for (final AxContextAlbum a : policyModel.getAlbums().getAlbumsMap().values()) {
175 final AxArtifactKey key = a.getKey();
177 codeGen.addContextAlbumDeclaration(kig.getName(key), kig.getVersion(key), kig.getUuid(key),
178 kig.getDesc(key), a.getScope(), a.isWritable(), kig.getName(a.getItemSchema()),
179 kig.getVersion(a.getItemSchema()));
183 for (final AxPolicy p : policyModel.getPolicies().getPolicyMap().values()) {
184 final AxArtifactKey key = p.getKey();
185 final List<ST> states = getStatesForPolicy(codeGen, p);
186 codeGen.addPolicyDefinition(kig.getName(key), kig.getVersion(key), kig.getUuid(key), kig.getDesc(key),
187 p.getTemplate(), p.getFirstState(), states);
190 final String out = codeGen.getModel().render();
191 if (outFile != null) {
192 String message = "Error writing output to file " + outFile;
194 final Writer w = outFile.toWriter();
196 LOGGER.error(message);
201 } catch (final IOException e) {
202 LOGGER.error(message, e);
212 * Gets the parameters for event.
214 * @param cg the code generator
215 * @param event the event
216 * @return the parameters for event
218 private List<ST> getParametersForEvent(final CodeGeneratorCliEditor cg, final AxEvent event) {
219 final Collection<AxField> fields = event.getFields();
220 final List<ST> ret = new ArrayList<>(fields.size());
221 for (final AxField f : fields) {
222 final AxReferenceKey fkey = f.getKey();
224 final ST val = cg.createEventFieldDefinition(kig.getPName(fkey), kig.getPVersion(fkey), kig.getLName(fkey),
225 kig.getName(f.getSchema()), kig.getVersion(f.getSchema()), f.getOptional());
233 * Gets the context references for task.
235 * @param cg the code generator
236 * @param task the task
237 * @return the context references for task
239 private List<ST> getCtxtRefsForTask(final CodeGeneratorCliEditor cg, final AxTask task) {
240 final Collection<AxArtifactKey> ctxs = task.getContextAlbumReferences();
241 final List<ST> ret = new ArrayList<>(ctxs.size());
242 final AxArtifactKey tkey = task.getKey();
243 for (final AxArtifactKey ckey : ctxs) {
245 final ST val = cg.createTaskDefinitionContextRef(kig.getName(tkey), kig.getVersion(tkey), kig.getName(ckey),
246 kig.getVersion(ckey));
254 * Gets the parameters for task.
256 * @param cg the code generator
257 * @param task the task
258 * @return the parameters for task
260 private List<ST> getParametersForTask(final CodeGeneratorCliEditor cg, final AxTask task) {
261 final Collection<AxTaskParameter> pars = task.getTaskParameters().values();
262 final List<ST> ret = new ArrayList<>(pars.size());
263 for (final AxTaskParameter p : pars) {
264 final AxReferenceKey pkey = p.getKey();
266 final ST val = cg.createTaskDefinitionParameters(kig.getPName(pkey), kig.getPVersion(pkey),
267 kig.getLName(pkey), p.getTaskParameterValue());
275 * Gets the logic for task.
277 * @param cg the code generator
278 * @param task the task
279 * @return the logic for task
281 private ST getLogicForTask(final CodeGeneratorCliEditor cg, final AxTask task) {
282 final AxArtifactKey tkey = task.getKey();
283 final AxTaskLogic tl = task.getTaskLogic();
285 return cg.createTaskDefLogic(kig.getName(tkey), kig.getVersion(tkey), tl.getLogicFlavour(), tl.getLogic());
289 * Gets the output fields for task.
291 * @param cg the code generator
292 * @param task the task
293 * @return the output fields for task
295 private List<ST> getOutfieldsForTask(final CodeGeneratorCliEditor cg, final AxTask task) {
296 final Collection<? extends AxField> fields = task.getOutputFields().values();
297 final List<ST> ret = new ArrayList<>(fields.size());
298 for (final AxField f : fields) {
299 final AxReferenceKey fkey = f.getKey();
301 final ST val = cg.createTaskDefinitionOutfields(kig.getPName(fkey), kig.getPVersion(fkey),
302 kig.getLName(fkey), kig.getName(f.getSchema()), kig.getVersion(f.getSchema()));
310 * Gets the input fields for task.
312 * @param cg the code generator
313 * @param task the task
314 * @return the input fields for task
316 private List<ST> getInfieldsForTask(final CodeGeneratorCliEditor cg, final AxTask task) {
317 final Collection<? extends AxField> fields = task.getInputFields().values();
318 final List<ST> ret = new ArrayList<>(fields.size());
319 for (final AxField f : fields) {
320 final AxReferenceKey fkey = f.getKey();
322 final ST val = cg.createTaskDefinitionInfields(kig.getPName(fkey), kig.getPVersion(fkey),
323 kig.getLName(fkey), kig.getName(f.getSchema()), kig.getVersion(f.getSchema()));
331 * Gets the states for policy.
333 * @param cg the code generator
334 * @param pol the policy
335 * @return the states for policy
337 private List<ST> getStatesForPolicy(final CodeGeneratorCliEditor cg, final AxPolicy pol) {
338 final Collection<AxState> states = pol.getStateMap().values();
339 final List<ST> ret = new ArrayList<>(states.size());
340 for (final AxState st : states) {
341 final AxReferenceKey skey = st.getKey();
342 final List<ST> outputs = getStateOutputsForState(cg, st);
343 final List<ST> finalizerLogics = getFinalizersForState(cg, st);
344 final List<ST> tasks = getTaskRefsForState(cg, st);
345 final List<ST> tsLogic = getTslForState(cg, st);
346 final List<ST> ctxRefs = getCtxtRefsForState(cg, st);
348 final ST val = cg.createPolicyStateDef(kig.getPName(skey), kig.getPVersion(skey), kig.getLName(skey),
349 kig.getName(st.getTrigger()), kig.getVersion(st.getTrigger()), kig.getName(st.getDefaultTask()),
350 kig.getVersion(st.getDefaultTask()), outputs, tasks, tsLogic, finalizerLogics, ctxRefs);
358 * Gets the finalizers for state.
360 * @param cg the code generator
361 * @param st the state
362 * @return the finalizers for state
364 private List<ST> getFinalizersForState(final CodeGeneratorCliEditor cg, final AxState st) {
365 final Collection<AxStateFinalizerLogic> fins = st.getStateFinalizerLogicMap().values();
366 final List<ST> ret = new ArrayList<>(fins.size());
367 final AxReferenceKey skey = st.getKey();
368 for (final AxStateFinalizerLogic fin : fins) {
369 final AxReferenceKey finkey = fin.getKey();
371 final ST val = cg.createPolicyStateDefFinalizerLogic(kig.getPName(skey), kig.getPVersion(skey),
372 kig.getLName(skey), kig.getLName(finkey), fin.getLogicFlavour(), fin.getLogic());
380 * Gets the context references for state.
382 * @param cg the code generator
383 * @param st the state
384 * @return the context references for state
386 private List<ST> getCtxtRefsForState(final CodeGeneratorCliEditor cg, final AxState st) {
387 final Collection<AxArtifactKey> ctxs = st.getContextAlbumReferences();
388 final List<ST> ret = new ArrayList<>(ctxs.size());
389 final AxReferenceKey skey = st.getKey();
390 for (final AxArtifactKey ctx : ctxs) {
392 final ST val = cg.createPolicyStateDefContextRef(kig.getPName(skey), kig.getPVersion(skey),
393 kig.getLName(skey), kig.getName(ctx), kig.getVersion(ctx));
401 * Gets the Task Selection Logic for state.
403 * @param cg the code generator
404 * @param st the state
405 * @return the TSL for state (if any) in a list
407 private List<ST> getTslForState(final CodeGeneratorCliEditor cg, final AxState st) {
408 final AxReferenceKey skey = st.getKey();
409 if (st.checkSetTaskSelectionLogic()) {
410 final AxTaskSelectionLogic tsl = st.getTaskSelectionLogic();
411 final ST val = cg.createPolicyStateDefTaskSelLogic(kig.getPName(skey), kig.getPVersion(skey),
412 kig.getLName(skey), tsl.getLogicFlavour(), tsl.getLogic());
413 return Collections.singletonList(val);
415 return Collections.emptyList();
420 * Gets the task references for state.
422 * @param cg the code generator
423 * @param st the state
424 * @return the task references for state
426 private List<ST> getTaskRefsForState(final CodeGeneratorCliEditor cg, final AxState st) {
427 final Map<AxArtifactKey, AxStateTaskReference> taskrefs = st.getTaskReferences();
428 final List<ST> ret = new ArrayList<>(taskrefs.size());
429 final AxReferenceKey skey = st.getKey();
430 for (final Entry<AxArtifactKey, AxStateTaskReference> e : taskrefs.entrySet()) {
431 final AxArtifactKey tkey = e.getKey();
432 final AxStateTaskReference tr = e.getValue();
433 final AxReferenceKey trkey = tr.getKey();
435 final ST val = cg.createPolicyStateTask(kig.getPName(skey), kig.getPVersion(skey), kig.getLName(skey),
436 kig.getLName(trkey), kig.getName(tkey), kig.getVersion(tkey), tr.getStateTaskOutputType().name(),
437 kig.getLName(tr.getOutput()));
445 * Gets the state outputs for state.
447 * @param cg the code generator
448 * @param st the state
449 * @return the state outputs for state
451 private List<ST> getStateOutputsForState(final CodeGeneratorCliEditor cg, final AxState st) {
452 final Collection<AxStateOutput> outs = st.getStateOutputs().values();
453 final List<ST> ret = new ArrayList<>(outs.size());
454 final AxReferenceKey skey = st.getKey();
455 for (final AxStateOutput out : outs) {
456 final AxReferenceKey outkey = out.getKey();
458 final ST val = cg.createPolicyStateOutput(kig.getPName(skey), kig.getPVersion(skey), kig.getLName(skey),
459 kig.getLName(outkey), kig.getName(out.getOutgingEvent()), kig.getVersion(out.getOutgingEvent()),
460 kig.getLName(out.getNextState()));