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.CGCliEditor;
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.stringtemplate.v4.ST;
61 * Takes a model and generates the JSON event schemas.
63 * @author Sven van der Meer (sven.van.der.meer@ericsson.com)
65 public class Model2Cli {
67 /** Application name, used as prompt. */
68 private final String appName;
70 /** The file name of the policy model. */
71 private final String modelFile;
73 /** The output file, if any. */
74 private final OutputFile outFile;
76 /** Pre-validate the model. */
77 private final boolean validate;
79 /** utility for getting key information and parsing keys etc.. */
80 private KeyInfoGetter kig = null;
83 * Creates a new model to CLI commands generator.
85 * @param modelFile the model file to be used
86 * @param outFile the out file
87 * @param validate true for model validation, false otherwise
88 * @param appName application name for printouts
90 public Model2Cli(final String modelFile, final OutputFile outFile, final boolean validate, final String appName) {
91 Validate.notNull(modelFile, "Model2Cli: given model file name was blank");
92 Validate.notNull(appName, "Model2Cli: given application name was blank");
93 this.modelFile = modelFile;
94 this.outFile = outFile;
95 this.appName = appName;
96 this.validate = validate;
100 * Runs the application.
102 * @return status of the application execution, 0 for success, positive integer for exit condition (such as help or
103 * version), negative integer for errors
104 * @throws ApexException if any problem occurred in the model
106 public int runApp() throws ApexException {
107 final CGCliEditor codeGen = new CGCliEditor();
109 final ApexModelFactory factory = new ApexModelFactory();
110 final ApexModel model = factory.createApexModel(new Properties(), true);
112 final ApexAPIResult result = model.loadFromFile(modelFile);
113 if (result.isNOK()) {
114 System.err.println(appName + ": " + result.getMessage());
118 final AxPolicyModel policyModel = model.getPolicyModel();
119 policyModel.register();
122 final AxValidationResult val = new AxValidationResult();
123 policyModel.validate(val);
125 System.err.println("Cannot translate the model. The model is not valid: \n" + val.toString());
130 kig = new KeyInfoGetter(policyModel);
132 // Order is important. 0: model, 1: context schemas, 2: tasks, 3: events, 4: ContextAlbums, 5: Policies
134 final AxArtifactKey pmkey = policyModel.getKey();
135 codeGen.addModelParams(kig.getName(pmkey), kig.getVersion(pmkey), kig.getUUID(pmkey), kig.getDesc(pmkey));
137 // 1: Context Schemas
138 for (final AxContextSchema s : policyModel.getSchemas().getSchemasMap().values()) {
139 final AxArtifactKey key = s.getKey();
141 codeGen.addSchemaDeclaration(kig.getName(key), kig.getVersion(key), kig.getUUID(key), kig.getDesc(key),
142 s.getSchemaFlavour(), s.getSchema());
146 for (final AxTask t : policyModel.getTasks().getTaskMap().values()) {
147 final AxArtifactKey key = t.getKey();
148 final List<ST> infields = getInfieldsForTask(codeGen, t);
149 final List<ST> outfields = getOutfieldsForTask(codeGen, t);
150 final ST logic = getLogicForTask(codeGen, t);
151 final List<ST> parameters = getParametersForTask(codeGen, t);
152 final List<ST> contextRefs = getCtxtRefsForTask(codeGen, t);
154 codeGen.addTaskDeclaration(kig.getName(key), kig.getVersion(key), kig.getUUID(key), kig.getDesc(key),
155 infields, outfields, logic, parameters, contextRefs);
159 for (final AxEvent e : policyModel.getEvents().getEventMap().values()) {
160 final AxArtifactKey key = e.getKey();
161 final List<ST> fields = getParametersForEvent(codeGen, e);
163 codeGen.addEventDeclaration(kig.getName(key), kig.getVersion(key), kig.getUUID(key), kig.getDesc(key),
164 e.getNameSpace(), e.getSource(), e.getTarget(), fields);
168 for (final AxContextAlbum a : policyModel.getAlbums().getAlbumsMap().values()) {
169 final AxArtifactKey key = a.getKey();
171 codeGen.addContextAlbumDeclaration(kig.getName(key), kig.getVersion(key), kig.getUUID(key),
172 kig.getDesc(key), a.getScope(), a.isWritable(), kig.getName(a.getItemSchema()),
173 kig.getVersion(a.getItemSchema()));
177 for (final AxPolicy p : policyModel.getPolicies().getPolicyMap().values()) {
178 final AxArtifactKey key = p.getKey();
179 final List<ST> states = getStatesForPolicy(codeGen, p);
180 codeGen.addPolicyDefinition(kig.getName(key), kig.getVersion(key), kig.getUUID(key), kig.getDesc(key),
181 p.getTemplate(), p.getFirstState(), states);
184 final String out = codeGen.getModel().render();
185 if (outFile != null) {
187 final Writer w = outFile.toWriter();
189 System.err.println("Error writing output to file " + outFile);
194 } catch (final IOException e) {
195 System.err.println("Error writing output to file " + outFile + ": " + e.getMessage());
199 System.err.println(out);
205 * Gets the parameters for event.
207 * @param cg the code generator
208 * @param event the event
209 * @return the parameters for event
211 private List<ST> getParametersForEvent(final CGCliEditor cg, final AxEvent event) {
212 final Collection<AxField> fields = event.getFields();
213 final List<ST> ret = new ArrayList<>(fields.size());
214 for (final AxField f : fields) {
215 final AxReferenceKey fkey = f.getKey();
217 final ST val = cg.createEventFieldDefinition(kig.getPName(fkey), kig.getPVersion(fkey), kig.getLName(fkey),
218 kig.getName(f.getSchema()), kig.getVersion(f.getSchema()), f.getOptional());
226 * Gets the context references for task.
228 * @param cg the code generator
229 * @param task the task
230 * @return the context references for task
232 private List<ST> getCtxtRefsForTask(final CGCliEditor cg, final AxTask task) {
233 final Collection<AxArtifactKey> ctxs = task.getContextAlbumReferences();
234 final List<ST> ret = new ArrayList<>(ctxs.size());
235 final AxArtifactKey tkey = task.getKey();
236 for (final AxArtifactKey ckey : ctxs) {
238 final ST val = cg.createTaskDefinitionContextRef(kig.getName(tkey), kig.getVersion(tkey), kig.getName(ckey),
239 kig.getVersion(ckey));
247 * Gets the parameters for task.
249 * @param cg the code generator
250 * @param task the task
251 * @return the parameters for task
253 private List<ST> getParametersForTask(final CGCliEditor cg, final AxTask task) {
254 final Collection<AxTaskParameter> pars = task.getTaskParameters().values();
255 final List<ST> ret = new ArrayList<>(pars.size());
256 for (final AxTaskParameter p : pars) {
257 final AxReferenceKey pkey = p.getKey();
259 final ST val = cg.createTaskDefinitionParameters(kig.getPName(pkey), kig.getPVersion(pkey),
260 kig.getLName(pkey), p.getTaskParameterValue());
268 * Gets the logic for task.
270 * @param cg the code generator
271 * @param task the task
272 * @return the logic for task
274 private ST getLogicForTask(final CGCliEditor cg, final AxTask task) {
275 final AxArtifactKey tkey = task.getKey();
276 final AxTaskLogic tl = task.getTaskLogic();
279 cg.createTaskDefLogic(kig.getName(tkey), kig.getVersion(tkey), tl.getLogicFlavour(), tl.getLogic());
285 * Gets the output fields for task.
287 * @param cg the code generator
288 * @param task the task
289 * @return the output fields for task
291 private List<ST> getOutfieldsForTask(final CGCliEditor cg, final AxTask task) {
292 final Collection<? extends AxField> fields = task.getOutputFields().values();
293 final List<ST> ret = new ArrayList<>(fields.size());
294 for (final AxField f : fields) {
295 final AxReferenceKey fkey = f.getKey();
297 final ST val = cg.createTaskDefinitionOutfields(kig.getPName(fkey), kig.getPVersion(fkey),
298 kig.getLName(fkey), kig.getName(f.getSchema()), kig.getVersion(f.getSchema()));
306 * Gets the input fields for task.
308 * @param cg the code generator
309 * @param task the task
310 * @return the input fields for task
312 private List<ST> getInfieldsForTask(final CGCliEditor cg, final AxTask task) {
313 final Collection<? extends AxField> fields = task.getInputFields().values();
314 final List<ST> ret = new ArrayList<>(fields.size());
315 for (final AxField f : fields) {
316 final AxReferenceKey fkey = f.getKey();
318 final ST val = cg.createTaskDefinitionInfields(kig.getPName(fkey), kig.getPVersion(fkey),
319 kig.getLName(fkey), kig.getName(f.getSchema()), kig.getVersion(f.getSchema()));
327 * Gets the states for policy.
329 * @param cg the code generator
330 * @param pol the policy
331 * @return the states for policy
333 private List<ST> getStatesForPolicy(final CGCliEditor cg, final AxPolicy pol) {
334 final Collection<AxState> states = pol.getStateMap().values();
335 final List<ST> ret = new ArrayList<>(states.size());
336 for (final AxState st : states) {
337 final AxReferenceKey skey = st.getKey();
338 final List<ST> outputs = getStateOutputsForState(cg, st);
339 final List<ST> finalizerLogics = getFinalizersForState(cg, st);
340 final List<ST> tasks = getTaskRefsForState(cg, st);
341 final List<ST> tsLogic = getTSLForState(cg, st);
342 final List<ST> ctxRefs = getCtxtRefsForState(cg, st);
344 final ST val = cg.createPolicyStateDef(kig.getPName(skey), kig.getPVersion(skey), kig.getLName(skey),
345 kig.getName(st.getTrigger()), kig.getVersion(st.getTrigger()), kig.getName(st.getDefaultTask()),
346 kig.getVersion(st.getDefaultTask()), outputs, tasks, tsLogic, finalizerLogics, ctxRefs);
354 * Gets the finalizers for state.
356 * @param cg the code generator
357 * @param st the state
358 * @return the finalizers for state
360 private List<ST> getFinalizersForState(final CGCliEditor cg, final AxState st) {
361 final Collection<AxStateFinalizerLogic> fins = st.getStateFinalizerLogicMap().values();
362 final List<ST> ret = new ArrayList<>(fins.size());
363 final AxReferenceKey skey = st.getKey();
364 for (final AxStateFinalizerLogic fin : fins) {
365 final AxReferenceKey finkey = fin.getKey();
367 final ST val = cg.createPolicyStateDefFinalizerLogic(kig.getPName(skey), kig.getPVersion(skey),
368 kig.getLName(skey), kig.getLName(finkey), fin.getLogicFlavour(), fin.getLogic());
376 * Gets the context references for state.
378 * @param cg the code generator
379 * @param st the state
380 * @return the context references for state
382 private List<ST> getCtxtRefsForState(final CGCliEditor cg, final AxState st) {
383 final Collection<AxArtifactKey> ctxs = st.getContextAlbumReferences();
384 final List<ST> ret = new ArrayList<>(ctxs.size());
385 final AxReferenceKey skey = st.getKey();
386 for (final AxArtifactKey ctx : ctxs) {
388 final ST val = cg.createPolicyStateDefContextRef(kig.getPName(skey), kig.getPVersion(skey),
389 kig.getLName(skey), kig.getName(ctx), kig.getVersion(ctx));
397 * Gets the Task Selection Logic for state.
399 * @param cg the code generator
400 * @param st the state
401 * @return the TSL for state (if any) in a list
403 private List<ST> getTSLForState(final CGCliEditor cg, final AxState st) {
404 final AxReferenceKey skey = st.getKey();
405 if (st.checkSetTaskSelectionLogic()) {
406 final AxTaskSelectionLogic tsl = st.getTaskSelectionLogic();
407 final ST val = cg.createPolicyStateDefTaskSelLogic(kig.getPName(skey), kig.getPVersion(skey),
408 kig.getLName(skey), tsl.getLogicFlavour(), tsl.getLogic());
409 return Collections.singletonList(val);
411 return Collections.emptyList();
416 * Gets the task references for state.
418 * @param cg the code generator
419 * @param st the state
420 * @return the task references for state
422 private List<ST> getTaskRefsForState(final CGCliEditor cg, final AxState st) {
423 final Map<AxArtifactKey, AxStateTaskReference> taskrefs = st.getTaskReferences();
424 final List<ST> ret = new ArrayList<>(taskrefs.size());
425 final AxReferenceKey skey = st.getKey();
426 for (final Entry<AxArtifactKey, AxStateTaskReference> e : taskrefs.entrySet()) {
427 final AxArtifactKey tkey = e.getKey();
428 final AxStateTaskReference tr = e.getValue();
429 final AxReferenceKey trkey = tr.getKey();
431 final ST val = cg.createPolicyStateTask(kig.getPName(skey), kig.getPVersion(skey), kig.getLName(skey),
432 kig.getLName(trkey), kig.getName(tkey), kig.getVersion(tkey), tr.getStateTaskOutputType().name(),
433 kig.getLName(tr.getOutput()));
441 * Gets the state outputs for state.
443 * @param cg the code generator
444 * @param st the state
445 * @return the state outputs for state
447 private List<ST> getStateOutputsForState(final CGCliEditor cg, final AxState st) {
448 final Collection<AxStateOutput> outs = st.getStateOutputs().values();
449 final List<ST> ret = new ArrayList<>(outs.size());
450 final AxReferenceKey skey = st.getKey();
451 for (final AxStateOutput out : outs) {
452 final AxReferenceKey outkey = out.getKey();
454 final ST val = cg.createPolicyStateOutput(kig.getPName(skey), kig.getPVersion(skey), kig.getLName(skey),
455 kig.getLName(outkey), kig.getName(out.getOutgingEvent()), kig.getVersion(out.getOutgingEvent()),
456 kig.getLName(out.getNextState()));