2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2019 Samsung Electronics Co., Ltd.
5 * Modifications Copyright (C) 2021 Bell Canada. All rights reserved.
6 * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
20 * SPDX-License-Identifier: Apache-2.0
21 * ============LICENSE_END=========================================================
24 package org.onap.policy.apex.tools.model.generator.model2cli;
26 import java.io.IOException;
27 import java.io.Writer;
28 import java.util.ArrayList;
29 import java.util.Collection;
30 import java.util.Collections;
31 import java.util.List;
33 import java.util.Map.Entry;
34 import java.util.Properties;
35 import org.apache.commons.lang3.Validate;
36 import org.onap.policy.apex.auth.clicodegen.CodeGenCliEditor;
37 import org.onap.policy.apex.auth.clicodegen.CodeGeneratorCliEditor;
38 import org.onap.policy.apex.auth.clicodegen.EventDeclaration;
39 import org.onap.policy.apex.auth.clicodegen.PolicyStateDef;
40 import org.onap.policy.apex.auth.clicodegen.PolicyStateTask;
41 import org.onap.policy.apex.auth.clicodegen.TaskDeclaration;
42 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
43 import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey;
44 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
45 import org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbum;
46 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema;
47 import org.onap.policy.apex.model.eventmodel.concepts.AxEvent;
48 import org.onap.policy.apex.model.eventmodel.concepts.AxField;
49 import org.onap.policy.apex.model.modelapi.ApexApiResult;
50 import org.onap.policy.apex.model.modelapi.ApexModel;
51 import org.onap.policy.apex.model.modelapi.ApexModelFactory;
52 import org.onap.policy.apex.model.policymodel.concepts.AxPolicy;
53 import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;
54 import org.onap.policy.apex.model.policymodel.concepts.AxState;
55 import org.onap.policy.apex.model.policymodel.concepts.AxStateFinalizerLogic;
56 import org.onap.policy.apex.model.policymodel.concepts.AxStateOutput;
57 import org.onap.policy.apex.model.policymodel.concepts.AxStateTaskReference;
58 import org.onap.policy.apex.model.policymodel.concepts.AxTask;
59 import org.onap.policy.apex.model.policymodel.concepts.AxTaskLogic;
60 import org.onap.policy.apex.model.policymodel.concepts.AxTaskParameter;
61 import org.onap.policy.apex.model.policymodel.concepts.AxTaskSelectionLogic;
62 import org.onap.policy.apex.tools.common.OutputFile;
63 import org.onap.policy.apex.tools.model.generator.KeyInfoGetter;
64 import org.slf4j.ext.XLogger;
65 import org.slf4j.ext.XLoggerFactory;
66 import org.stringtemplate.v4.ST;
69 * Takes a model and generates the JSON event schemas.
71 * @author Sven van der Meer (sven.van.der.meer@ericsson.com)
73 public class Model2Cli {
74 // Logger for this class
75 private static final XLogger LOGGER = XLoggerFactory.getXLogger(Model2Cli.class);
77 /** Application name, used as prompt. */
78 private final String appName;
80 /** The file name of the policy model. */
81 private final String modelFile;
83 /** The output file, if any. */
84 private final OutputFile outFile;
86 /** Pre-validate the model. */
87 private final boolean validate;
89 /** utility for getting key information and parsing keys etc.. */
90 private KeyInfoGetter kig = null;
93 * Creates a new model to CLI commands generator.
95 * @param modelFile the model file to be used
96 * @param outFile the out file
97 * @param validate true for model validation, false otherwise
98 * @param appName application name for printouts
100 public Model2Cli(final String modelFile, final OutputFile outFile, final boolean validate, final String appName) {
101 Validate.notNull(modelFile, "Model2Cli: given model file name was blank");
102 Validate.notNull(appName, "Model2Cli: given application name was blank");
104 this.modelFile = modelFile;
105 this.outFile = outFile;
106 this.appName = appName;
107 this.validate = validate;
111 * Runs the application.
113 * @return status of the application execution, 0 for success, positive integer for exit condition (such as help or
114 * version), negative integer for errors
116 public int runApp() {
117 final CodeGeneratorCliEditor codeGen = new CodeGeneratorCliEditor();
119 final ApexModelFactory factory = new ApexModelFactory();
120 final ApexModel model = factory.createApexModel(new Properties(), true);
122 final ApexApiResult result = model.loadFromFile(modelFile);
123 if (result.isNok()) {
124 final String message = appName + ": " + result.getMessage();
125 LOGGER.error(message);
129 final AxPolicyModel policyModel = model.getPolicyModel();
130 policyModel.register();
133 final AxValidationResult val = new AxValidationResult();
134 policyModel.validate(val);
136 final String message = "Cannot translate the model. The model is not valid: \n" + val.toString();
137 LOGGER.error(message);
142 return generateCli(codeGen, policyModel);
146 * Generate the CLI from the model.
148 * @param codeGen the code generator
149 * @param policyModel the policy model
151 private int generateCli(final CodeGeneratorCliEditor codeGen, final AxPolicyModel policyModel) {
152 kig = new KeyInfoGetter(policyModel);
154 // Order is important. 0: model, 1: context schemas, 2: tasks, 3: events, 4: ContextAlbums, 5: Policies
156 final AxArtifactKey pmkey = policyModel.getKey();
157 codeGen.addModelParams(kig.getName(pmkey), kig.getVersion(pmkey), kig.getUuid(pmkey), kig.getDesc(pmkey));
159 // 1: Context Schemas
160 for (final AxContextSchema s : policyModel.getSchemas().getSchemasMap().values()) {
161 final AxArtifactKey key = s.getKey();
163 codeGen.addSchemaDeclaration(kig.getName(key), kig.getVersion(key), kig.getUuid(key), kig.getDesc(key),
164 s.getSchemaFlavour(), s.getSchema());
168 for (final AxTask t : policyModel.getTasks().getTaskMap().values()) {
169 final AxArtifactKey key = t.getKey();
170 final ST logic = getLogicForTask(codeGen, t);
171 final List<ST> parameters = getParametersForTask(codeGen, t);
172 final List<ST> contextRefs = getCtxtRefsForTask(codeGen, t);
174 codeGen.addTaskDeclaration(TaskDeclaration.builder().name(kig.getName(key))
175 .version(kig.getVersion(key)).uuid(kig.getUuid(key)).description(kig.getDesc(key))
176 .logic(logic).parameters(parameters).contextRefs(contextRefs).build());
180 for (final AxEvent e : policyModel.getEvents().getEventMap().values()) {
181 final AxArtifactKey key = e.getKey();
182 final List<ST> fields = getParametersForEvent(codeGen, e);
184 codeGen.addEventDeclaration(
185 EventDeclaration.builder()
186 .name(kig.getName(key))
187 .version(kig.getVersion(key))
188 .uuid(kig.getUuid(key))
189 .description(kig.getDesc(key))
190 .nameSpace(e.getNameSpace())
191 .source(e.getSource())
192 .target(e.getTarget())
198 for (final AxContextAlbum a : policyModel.getAlbums().getAlbumsMap().values()) {
199 final AxArtifactKey key = a.getKey();
201 codeGen.addContextAlbumDeclaration(CodeGenCliEditor.builder().name(kig.getName(key))
202 .version(kig.getVersion(key)).uuid(kig.getUuid(key)).description(kig.getDesc(key))
203 .scope(a.getScope()).writable(a.isWritable()).schemaName(kig.getName(a.getItemSchema()))
204 .schemaVersion(kig.getVersion(a.getItemSchema())).build());
208 for (final AxPolicy p : policyModel.getPolicies().getPolicyMap().values()) {
209 final AxArtifactKey key = p.getKey();
210 final List<ST> states = getStatesForPolicy(codeGen, p);
211 codeGen.addPolicyDefinition(kig.getName(key), kig.getVersion(key), kig.getUuid(key), kig.getDesc(key),
212 p.getTemplate(), p.getFirstState(), states);
215 final String out = codeGen.getModel().render();
216 if (outFile != null) {
217 final String message = "Error writing output to file " + outFile;
219 final Writer w = outFile.toWriter();
221 LOGGER.error(message);
226 } catch (final IOException e) {
227 LOGGER.error(message, e);
238 * Gets the parameters for event.
240 * @param cg the code generator
241 * @param event the event
242 * @return the parameters for event
244 private List<ST> getParametersForEvent(final CodeGeneratorCliEditor cg, final AxEvent event) {
245 final Collection<AxField> fields = event.getFields();
246 final List<ST> ret = new ArrayList<>(fields.size());
247 for (final AxField f : fields) {
248 final AxReferenceKey fkey = f.getKey();
250 final ST val = cg.createEventFieldDefinition(kig.getPName(fkey), kig.getPVersion(fkey), kig.getLName(fkey),
251 kig.getName(f.getSchema()), kig.getVersion(f.getSchema()), f.getOptional());
259 * Gets the context references for task.
261 * @param cg the code generator
262 * @param task the task
263 * @return the context references for task
265 private List<ST> getCtxtRefsForTask(final CodeGeneratorCliEditor cg, final AxTask task) {
266 final Collection<AxArtifactKey> ctxs = task.getContextAlbumReferences();
267 final List<ST> ret = new ArrayList<>(ctxs.size());
268 final AxArtifactKey tkey = task.getKey();
269 for (final AxArtifactKey ckey : ctxs) {
271 final ST val = cg.createTaskDefinitionContextRef(kig.getName(tkey), kig.getVersion(tkey), kig.getName(ckey),
272 kig.getVersion(ckey));
280 * Gets the parameters for task.
282 * @param cg the code generator
283 * @param task the task
284 * @return the parameters for task
286 private List<ST> getParametersForTask(final CodeGeneratorCliEditor cg, final AxTask task) {
287 final Collection<AxTaskParameter> pars = task.getTaskParameters().values();
288 final List<ST> ret = new ArrayList<>(pars.size());
289 for (final AxTaskParameter p : pars) {
290 final AxReferenceKey pkey = p.getKey();
292 final ST val = cg.createTaskDefinitionParameters(kig.getPName(pkey), kig.getPVersion(pkey),
293 kig.getLName(pkey), p.getTaskParameterValue());
301 * Gets the logic for task.
303 * @param cg the code generator
304 * @param task the task
305 * @return the logic for task
307 private ST getLogicForTask(final CodeGeneratorCliEditor cg, final AxTask task) {
308 final AxArtifactKey tkey = task.getKey();
309 final AxTaskLogic tl = task.getTaskLogic();
311 return cg.createTaskDefLogic(kig.getName(tkey), kig.getVersion(tkey), tl.getLogicFlavour(), tl.getLogic());
315 * Gets the states for policy.
317 * @param cg the code generator
318 * @param pol the policy
319 * @return the states for policy
321 private List<ST> getStatesForPolicy(final CodeGeneratorCliEditor cg, final AxPolicy pol) {
322 final Collection<AxState> states = pol.getStateMap().values();
323 final List<ST> ret = new ArrayList<>(states.size());
324 for (final AxState st : states) {
325 final AxReferenceKey skey = st.getKey();
326 final List<ST> outputs = getStateOutputsForState(cg, st);
327 final List<ST> finalizerLogics = getFinalizersForState(cg, st);
328 final List<ST> tasks = getTaskRefsForState(cg, st);
329 final List<ST> tsLogic = getTslForState(cg, st);
330 final List<ST> ctxRefs = getCtxtRefsForState(cg, st);
332 final ST val = cg.createPolicyStateDef(PolicyStateDef.builder()
333 .policyName(kig.getPName(skey)).version(kig.getPVersion(skey))
334 .stateName(kig.getLName(skey)).triggerName(kig.getName(st.getTrigger()))
335 .triggerVersion(kig.getVersion(st.getTrigger()))
336 .defaultTask(kig.getName(st.getDefaultTask()))
337 .defaultTaskVersion(kig.getVersion(st.getDefaultTask())).outputs(outputs)
338 .tasks(tasks).tsLogic(tsLogic).finalizerLogics(finalizerLogics)
339 .ctxRefs(ctxRefs).build());
347 * Gets the finalizers for state.
349 * @param cg the code generator
350 * @param st the state
351 * @return the finalizers for state
353 private List<ST> getFinalizersForState(final CodeGeneratorCliEditor cg, final AxState st) {
354 final Collection<AxStateFinalizerLogic> fins = st.getStateFinalizerLogicMap().values();
355 final List<ST> ret = new ArrayList<>(fins.size());
356 final AxReferenceKey skey = st.getKey();
357 for (final AxStateFinalizerLogic fin : fins) {
358 final AxReferenceKey finkey = fin.getKey();
360 final ST val = cg.createPolicyStateDefFinalizerLogic(kig.getPName(skey), kig.getPVersion(skey),
361 kig.getLName(skey), kig.getLName(finkey), fin.getLogicFlavour(), fin.getLogic());
369 * Gets the context references for state.
371 * @param cg the code generator
372 * @param st the state
373 * @return the context references for state
375 private List<ST> getCtxtRefsForState(final CodeGeneratorCliEditor cg, final AxState st) {
376 final Collection<AxArtifactKey> ctxs = st.getContextAlbumReferences();
377 final List<ST> ret = new ArrayList<>(ctxs.size());
378 final AxReferenceKey skey = st.getKey();
379 for (final AxArtifactKey ctx : ctxs) {
381 final ST val = cg.createPolicyStateDefContextRef(kig.getPName(skey), kig.getPVersion(skey),
382 kig.getLName(skey), kig.getName(ctx), kig.getVersion(ctx));
390 * Gets the Task Selection Logic for state.
392 * @param cg the code generator
393 * @param st the state
394 * @return the TSL for state (if any) in a list
396 private List<ST> getTslForState(final CodeGeneratorCliEditor cg, final AxState st) {
397 final AxReferenceKey skey = st.getKey();
398 if (st.checkSetTaskSelectionLogic()) {
399 final AxTaskSelectionLogic tsl = st.getTaskSelectionLogic();
400 final ST val = cg.createPolicyStateDefTaskSelLogic(kig.getPName(skey), kig.getPVersion(skey),
401 kig.getLName(skey), tsl.getLogicFlavour(), tsl.getLogic());
402 return Collections.singletonList(val);
404 return Collections.emptyList();
409 * Gets the task references for state.
411 * @param cg the code generator
412 * @param st the state
413 * @return the task references for state
415 private List<ST> getTaskRefsForState(final CodeGeneratorCliEditor cg, final AxState st) {
416 final Map<AxArtifactKey, AxStateTaskReference> taskrefs = st.getTaskReferences();
417 final List<ST> ret = new ArrayList<>(taskrefs.size());
418 final AxReferenceKey skey = st.getKey();
419 for (final Entry<AxArtifactKey, AxStateTaskReference> e : taskrefs.entrySet()) {
420 final AxArtifactKey tkey = e.getKey();
421 final AxStateTaskReference tr = e.getValue();
422 final AxReferenceKey trkey = tr.getKey();
424 final ST val = cg.createPolicyStateTask(PolicyStateTask.builder()
425 .policyName(kig.getPName(skey)).version(kig.getPVersion(skey))
426 .stateName(kig.getLName(skey)).taskLocalName(kig.getLName(trkey))
427 .taskName(kig.getName(tkey)).taskVersion(kig.getVersion(tkey))
428 .outputType(tr.getStateTaskOutputType().name())
429 .outputName(kig.getLName(tr.getOutput()))
438 * Gets the state outputs for state.
440 * @param cg the code generator
441 * @param st the state
442 * @return the state outputs for state
444 private List<ST> getStateOutputsForState(final CodeGeneratorCliEditor cg, final AxState st) {
445 final Collection<AxStateOutput> outs = st.getStateOutputs().values();
446 final List<ST> ret = new ArrayList<>(outs.size());
447 final AxReferenceKey skey = st.getKey();
448 for (final AxStateOutput out : outs) {
449 final AxReferenceKey outkey = out.getKey();
451 final ST val = cg.createPolicyStateOutput(kig.getPName(skey), kig.getPVersion(skey), kig.getLName(skey),
452 kig.getLName(outkey), kig.getName(out.getOutgoingEvent()), kig.getVersion(out.getOutgoingEvent()),
453 kig.getLName(out.getNextState()));