bd5b83518b3f1bd20939d009df35b8c691fa336f
[policy/apex-pdp.git] /
1 /*-
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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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.
19  *
20  * SPDX-License-Identifier: Apache-2.0
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.policy.apex.tools.model.generator.model2cli;
25
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;
32 import java.util.Map;
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;
67
68 /**
69  * Takes a model and generates the JSON event schemas.
70  *
71  * @author Sven van der Meer (sven.van.der.meer@ericsson.com)
72  */
73 public class Model2Cli {
74     // Logger for this class
75     private static final XLogger LOGGER = XLoggerFactory.getXLogger(Model2Cli.class);
76
77     /** Application name, used as prompt. */
78     private final String appName;
79
80     /** The file name of the policy model. */
81     private final String modelFile;
82
83     /** The output file, if any. */
84     private final OutputFile outFile;
85
86     /** Pre-validate the model. */
87     private final boolean validate;
88
89     /** utility for getting key information and parsing keys etc.. */
90     private KeyInfoGetter kig = null;
91
92     /**
93      * Creates a new model to CLI commands generator.
94      *
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
99      */
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");
103
104         this.modelFile = modelFile;
105         this.outFile = outFile;
106         this.appName = appName;
107         this.validate = validate;
108     }
109
110     /**
111      * Runs the application.
112      *
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
115      */
116     public int runApp() {
117         final CodeGeneratorCliEditor codeGen = new CodeGeneratorCliEditor();
118
119         final ApexModelFactory factory = new ApexModelFactory();
120         final ApexModel model = factory.createApexModel(new Properties(), true);
121
122         final ApexApiResult result = model.loadFromFile(modelFile);
123         if (result.isNok()) {
124             final String message = appName + ": " + result.getMessage();
125             LOGGER.error(message);
126             return -1;
127         }
128
129         final AxPolicyModel policyModel = model.getPolicyModel();
130         policyModel.register();
131
132         if (validate) {
133             final AxValidationResult val = new AxValidationResult();
134             policyModel.validate(val);
135             if (!val.isOk()) {
136                 final String message = "Cannot translate the model. The model is not valid: \n" + val.toString();
137                 LOGGER.error(message);
138                 return -1;
139             }
140         }
141
142         return generateCli(codeGen, policyModel);
143     }
144
145     /**
146      * Generate the CLI from the model.
147      *
148      * @param codeGen the code generator
149      * @param policyModel the policy model
150      */
151     private int generateCli(final CodeGeneratorCliEditor codeGen, final AxPolicyModel policyModel) {
152         kig = new KeyInfoGetter(policyModel);
153
154         // Order is important. 0: model, 1: context schemas, 2: tasks, 3: events, 4: ContextAlbums, 5: Policies
155         // 0: model
156         final AxArtifactKey pmkey = policyModel.getKey();
157         codeGen.addModelParams(kig.getName(pmkey), kig.getVersion(pmkey), kig.getUuid(pmkey), kig.getDesc(pmkey));
158
159         // 1: Context Schemas
160         for (final AxContextSchema s : policyModel.getSchemas().getSchemasMap().values()) {
161             final AxArtifactKey key = s.getKey();
162
163             codeGen.addSchemaDeclaration(kig.getName(key), kig.getVersion(key), kig.getUuid(key), kig.getDesc(key),
164                     s.getSchemaFlavour(), s.getSchema());
165         }
166
167         // 2: tasks
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);
173
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());
177         }
178
179         // 3: events
180         for (final AxEvent e : policyModel.getEvents().getEventMap().values()) {
181             final AxArtifactKey key = e.getKey();
182             final List<ST> fields = getParametersForEvent(codeGen, e);
183
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())
193                             .fields(fields)
194                             .build());
195         }
196
197         // 4: context albums
198         for (final AxContextAlbum a : policyModel.getAlbums().getAlbumsMap().values()) {
199             final AxArtifactKey key = a.getKey();
200
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());
205         }
206
207         // 5: policies
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);
213         }
214
215         final String out = codeGen.getModel().render();
216         if (outFile != null) {
217             final String message = "Error writing output to file " + outFile;
218             try {
219                 final Writer w = outFile.toWriter();
220                 if (w == null) {
221                     LOGGER.error(message);
222                     return -1;
223                 }
224                 w.write(out);
225                 w.close();
226             } catch (final IOException e) {
227                 LOGGER.error(message, e);
228                 return -1;
229             }
230         } else {
231             LOGGER.error(out);
232         }
233
234         return 0;
235     }
236
237     /**
238      * Gets the parameters for event.
239      *
240      * @param cg the code generator
241      * @param event the event
242      * @return the parameters for event
243      */
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();
249
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());
252
253             ret.add(val);
254         }
255         return ret;
256     }
257
258     /**
259      * Gets the context references for task.
260      *
261      * @param cg the code generator
262      * @param task the task
263      * @return the context references for task
264      */
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) {
270
271             final ST val = cg.createTaskDefinitionContextRef(kig.getName(tkey), kig.getVersion(tkey), kig.getName(ckey),
272                     kig.getVersion(ckey));
273
274             ret.add(val);
275         }
276         return ret;
277     }
278
279     /**
280      * Gets the parameters for task.
281      *
282      * @param cg the code generator
283      * @param task the task
284      * @return the parameters for task
285      */
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();
291
292             final ST val = cg.createTaskDefinitionParameters(kig.getPName(pkey), kig.getPVersion(pkey),
293                     kig.getLName(pkey), p.getTaskParameterValue());
294
295             ret.add(val);
296         }
297         return ret;
298     }
299
300     /**
301      * Gets the logic for task.
302      *
303      * @param cg the code generator
304      * @param task the task
305      * @return the logic for task
306      */
307     private ST getLogicForTask(final CodeGeneratorCliEditor cg, final AxTask task) {
308         final AxArtifactKey tkey = task.getKey();
309         final AxTaskLogic tl = task.getTaskLogic();
310
311         return cg.createTaskDefLogic(kig.getName(tkey), kig.getVersion(tkey), tl.getLogicFlavour(), tl.getLogic());
312     }
313
314     /**
315      * Gets the states for policy.
316      *
317      * @param cg the code generator
318      * @param pol the policy
319      * @return the states for policy
320      */
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);
331
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());
340
341             ret.add(val);
342         }
343         return ret;
344     }
345
346     /**
347      * Gets the finalizers for state.
348      *
349      * @param cg the code generator
350      * @param st the state
351      * @return the finalizers for state
352      */
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();
359
360             final ST val = cg.createPolicyStateDefFinalizerLogic(kig.getPName(skey), kig.getPVersion(skey),
361                     kig.getLName(skey), kig.getLName(finkey), fin.getLogicFlavour(), fin.getLogic());
362
363             ret.add(val);
364         }
365         return ret;
366     }
367
368     /**
369      * Gets the context references for state.
370      *
371      * @param cg the code generator
372      * @param st the state
373      * @return the context references for state
374      */
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) {
380
381             final ST val = cg.createPolicyStateDefContextRef(kig.getPName(skey), kig.getPVersion(skey),
382                     kig.getLName(skey), kig.getName(ctx), kig.getVersion(ctx));
383
384             ret.add(val);
385         }
386         return ret;
387     }
388
389     /**
390      * Gets the Task Selection Logic for state.
391      *
392      * @param cg the code generator
393      * @param st the state
394      * @return the TSL for state (if any) in a list
395      */
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);
403         } else {
404             return Collections.emptyList();
405         }
406     }
407
408     /**
409      * Gets the task references for state.
410      *
411      * @param cg the code generator
412      * @param st the state
413      * @return the task references for state
414      */
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();
423
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()))
430                     .build());
431
432             ret.add(val);
433         }
434         return ret;
435     }
436
437     /**
438      * Gets the state outputs for state.
439      *
440      * @param cg the code generator
441      * @param st the state
442      * @return the state outputs for state
443      */
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();
450
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()));
454
455             ret.add(val);
456         }
457         return ret;
458     }
459
460 }