7e02c7af5b0c74e23c02da1a2df553b3e6b49bec
[policy/apex-pdp.git] /
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.tools.model.generator.model2cli;
22
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;
29 import java.util.Map;
30 import java.util.Map.Entry;
31 import java.util.Properties;
32
33 import org.apache.commons.lang3.Validate;
34 import org.onap.policy.apex.auth.clicodegen.CodeGenCliEditorBuilder;
35 import org.onap.policy.apex.auth.clicodegen.CodeGeneratorCliEditor;
36 import org.onap.policy.apex.auth.clicodegen.EventDeclarationBuilder;
37 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
38 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
39 import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey;
40 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
41 import org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbum;
42 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema;
43 import org.onap.policy.apex.model.eventmodel.concepts.AxEvent;
44 import org.onap.policy.apex.model.eventmodel.concepts.AxField;
45 import org.onap.policy.apex.model.modelapi.ApexApiResult;
46 import org.onap.policy.apex.model.modelapi.ApexModel;
47 import org.onap.policy.apex.model.modelapi.ApexModelFactory;
48 import org.onap.policy.apex.model.policymodel.concepts.AxPolicy;
49 import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;
50 import org.onap.policy.apex.model.policymodel.concepts.AxState;
51 import org.onap.policy.apex.model.policymodel.concepts.AxStateFinalizerLogic;
52 import org.onap.policy.apex.model.policymodel.concepts.AxStateOutput;
53 import org.onap.policy.apex.model.policymodel.concepts.AxStateTaskReference;
54 import org.onap.policy.apex.model.policymodel.concepts.AxTask;
55 import org.onap.policy.apex.model.policymodel.concepts.AxTaskLogic;
56 import org.onap.policy.apex.model.policymodel.concepts.AxTaskParameter;
57 import org.onap.policy.apex.model.policymodel.concepts.AxTaskSelectionLogic;
58 import org.onap.policy.apex.tools.common.OutputFile;
59 import org.onap.policy.apex.tools.model.generator.KeyInfoGetter;
60 import org.slf4j.ext.XLogger;
61 import org.slf4j.ext.XLoggerFactory;
62 import org.stringtemplate.v4.ST;
63
64 /**
65  * Takes a model and generates the JSON event schemas.
66  *
67  * @author Sven van der Meer (sven.van.der.meer@ericsson.com)
68  */
69 public class Model2Cli {
70     // Logger for this class
71     private static final XLogger LOGGER = XLoggerFactory.getXLogger(Model2Cli.class);
72
73     /** Application name, used as prompt. */
74     private final String appName;
75
76     /** The file name of the policy model. */
77     private final String modelFile;
78
79     /** The output file, if any. */
80     private final OutputFile outFile;
81
82     /** Pre-validate the model. */
83     private final boolean validate;
84
85     /** utility for getting key information and parsing keys etc.. */
86     private KeyInfoGetter kig = null;
87
88     /**
89      * Creates a new model to CLI commands generator.
90      *
91      * @param modelFile the model file to be used
92      * @param outFile the out file
93      * @param validate true for model validation, false otherwise
94      * @param appName application name for printouts
95      */
96     public Model2Cli(final String modelFile, final OutputFile outFile, final boolean validate, final String appName) {
97         Validate.notNull(modelFile, "Model2Cli: given model file name was blank");
98         Validate.notNull(appName, "Model2Cli: given application name was blank");
99         
100         this.modelFile = modelFile;
101         this.outFile = outFile;
102         this.appName = appName;
103         this.validate = validate;
104     }
105
106     /**
107      * Runs the application.
108      *
109      * @return status of the application execution, 0 for success, positive integer for exit condition (such as help or
110      *         version), negative integer for errors
111      * @throws ApexException if any problem occurred in the model
112      */
113     public int runApp() {
114         final CodeGeneratorCliEditor codeGen = new CodeGeneratorCliEditor();
115
116         final ApexModelFactory factory = new ApexModelFactory();
117         final ApexModel model = factory.createApexModel(new Properties(), true);
118
119         final ApexApiResult result = model.loadFromFile(modelFile);
120         if (result.isNok()) {
121             String message = appName + ": " + result.getMessage();
122             LOGGER.error(message);
123             return -1;
124         }
125
126         final AxPolicyModel policyModel = model.getPolicyModel();
127         policyModel.register();
128
129         if (validate) {
130             final AxValidationResult val = new AxValidationResult();
131             policyModel.validate(val);
132             if (!val.isOk()) {
133                 String message = "Cannot translate the model. The model is not valid: \n" + val.toString();
134                 LOGGER.error(message);
135                 return -1;
136             }
137         }
138
139         return generateCli(codeGen, policyModel);
140     }
141
142     /**
143      * Generate the CLI from the model.
144      * @param codeGen the code generator
145      * @param policyModel the policy model
146      */
147     private int generateCli(final CodeGeneratorCliEditor codeGen, final AxPolicyModel policyModel) {
148         kig = new KeyInfoGetter(policyModel);
149
150         // Order is important. 0: model, 1: context schemas, 2: tasks, 3: events, 4: ContextAlbums, 5: Policies
151         // 0: model
152         final AxArtifactKey pmkey = policyModel.getKey();
153         codeGen.addModelParams(kig.getName(pmkey), kig.getVersion(pmkey), kig.getUuid(pmkey), kig.getDesc(pmkey));
154
155         // 1: Context Schemas
156         for (final AxContextSchema s : policyModel.getSchemas().getSchemasMap().values()) {
157             final AxArtifactKey key = s.getKey();
158
159             codeGen.addSchemaDeclaration(kig.getName(key), kig.getVersion(key), kig.getUuid(key), kig.getDesc(key),
160                             s.getSchemaFlavour(), s.getSchema());
161         }
162
163         // 2: tasks
164         for (final AxTask t : policyModel.getTasks().getTaskMap().values()) {
165             final AxArtifactKey key = t.getKey();
166             final List<ST> infields = getInfieldsForTask(codeGen, t);
167             final List<ST> outfields = getOutfieldsForTask(codeGen, t);
168             final ST logic = getLogicForTask(codeGen, t);
169             final List<ST> parameters = getParametersForTask(codeGen, t);
170             final List<ST> contextRefs = getCtxtRefsForTask(codeGen, t);
171
172             codeGen.addTaskDeclaration(kig.getName(key), kig.getVersion(key), kig.getUuid(key), kig.getDesc(key),
173                             infields, outfields, logic, parameters, contextRefs);
174         }
175
176         // 3: events
177         for (final AxEvent e : policyModel.getEvents().getEventMap().values()) {
178             final AxArtifactKey key = e.getKey();
179             final List<ST> fields = getParametersForEvent(codeGen, e);
180
181             codeGen.addEventDeclaration(
182                     new EventDeclarationBuilder()
183                             .setName(kig.getName(key))
184                             .setVersion(kig.getVersion(key))
185                             .setUuid(kig.getUuid(key))
186                             .setDescription(kig.getDesc(key))
187                             .setNameSpace(e.getNameSpace())
188                             .setSource(e.getSource())
189                             .setTarget(e.getTarget())
190                             .setFields(fields));
191         }
192
193         // 4: context albums
194         for (final AxContextAlbum a : policyModel.getAlbums().getAlbumsMap().values()) {
195             final AxArtifactKey key = a.getKey();
196
197             codeGen.addContextAlbumDeclaration(
198                     new CodeGenCliEditorBuilder()
199                             .setName(kig.getName(key))
200                             .setVersion(kig.getVersion(key))
201                             .setUuid(kig.getUuid(key))
202                             .setDescription(kig.getDesc(key))
203                             .setScope(a.getScope())
204                             .setWritable(a.isWritable())
205                             .setSchemaName(kig.getName(a.getItemSchema()))
206                             .setSchemaVersion(kig.getVersion(a.getItemSchema())));
207         }
208
209         // 5: policies
210         for (final AxPolicy p : policyModel.getPolicies().getPolicyMap().values()) {
211             final AxArtifactKey key = p.getKey();
212             final List<ST> states = getStatesForPolicy(codeGen, p);
213             codeGen.addPolicyDefinition(kig.getName(key), kig.getVersion(key), kig.getUuid(key), kig.getDesc(key),
214                             p.getTemplate(), p.getFirstState(), states);
215         }
216
217         final String out = codeGen.getModel().render();
218         if (outFile != null) {
219             String message = "Error writing output to file " + outFile;
220             try {
221                 final Writer w = outFile.toWriter();
222                 if (w == null) {
223                     LOGGER.error(message);
224                     return -1;
225                 }
226                 w.write(out);
227                 w.close();
228             } catch (final IOException e) {
229                 LOGGER.error(message, e);
230                 return -1;
231             }
232         } else {
233             LOGGER.error(out);
234         }
235         
236         return 0;
237     }
238
239     /**
240      * Gets the parameters for event.
241      *
242      * @param cg the code generator
243      * @param event the event
244      * @return the parameters for event
245      */
246     private List<ST> getParametersForEvent(final CodeGeneratorCliEditor cg, final AxEvent event) {
247         final Collection<AxField> fields = event.getFields();
248         final List<ST> ret = new ArrayList<>(fields.size());
249         for (final AxField f : fields) {
250             final AxReferenceKey fkey = f.getKey();
251
252             final ST val = cg.createEventFieldDefinition(kig.getPName(fkey), kig.getPVersion(fkey), kig.getLName(fkey),
253                             kig.getName(f.getSchema()), kig.getVersion(f.getSchema()), f.getOptional());
254
255             ret.add(val);
256         }
257         return ret;
258     }
259
260     /**
261      * Gets the context references for task.
262      *
263      * @param cg the code generator
264      * @param task the task
265      * @return the context references for task
266      */
267     private List<ST> getCtxtRefsForTask(final CodeGeneratorCliEditor cg, final AxTask task) {
268         final Collection<AxArtifactKey> ctxs = task.getContextAlbumReferences();
269         final List<ST> ret = new ArrayList<>(ctxs.size());
270         final AxArtifactKey tkey = task.getKey();
271         for (final AxArtifactKey ckey : ctxs) {
272
273             final ST val = cg.createTaskDefinitionContextRef(kig.getName(tkey), kig.getVersion(tkey), kig.getName(ckey),
274                             kig.getVersion(ckey));
275
276             ret.add(val);
277         }
278         return ret;
279     }
280
281     /**
282      * Gets the parameters for task.
283      *
284      * @param cg the code generator
285      * @param task the task
286      * @return the parameters for task
287      */
288     private List<ST> getParametersForTask(final CodeGeneratorCliEditor cg, final AxTask task) {
289         final Collection<AxTaskParameter> pars = task.getTaskParameters().values();
290         final List<ST> ret = new ArrayList<>(pars.size());
291         for (final AxTaskParameter p : pars) {
292             final AxReferenceKey pkey = p.getKey();
293
294             final ST val = cg.createTaskDefinitionParameters(kig.getPName(pkey), kig.getPVersion(pkey),
295                             kig.getLName(pkey), p.getTaskParameterValue());
296
297             ret.add(val);
298         }
299         return ret;
300     }
301
302     /**
303      * Gets the logic for task.
304      *
305      * @param cg the code generator
306      * @param task the task
307      * @return the logic for task
308      */
309     private ST getLogicForTask(final CodeGeneratorCliEditor cg, final AxTask task) {
310         final AxArtifactKey tkey = task.getKey();
311         final AxTaskLogic tl = task.getTaskLogic();
312
313         return cg.createTaskDefLogic(kig.getName(tkey), kig.getVersion(tkey), tl.getLogicFlavour(), tl.getLogic());
314     }
315
316     /**
317      * Gets the output fields for task.
318      *
319      * @param cg the code generator
320      * @param task the task
321      * @return the output fields for task
322      */
323     private List<ST> getOutfieldsForTask(final CodeGeneratorCliEditor cg, final AxTask task) {
324         final Collection<? extends AxField> fields = task.getOutputFields().values();
325         final List<ST> ret = new ArrayList<>(fields.size());
326         for (final AxField f : fields) {
327             final AxReferenceKey fkey = f.getKey();
328
329             final ST val = cg.createTaskDefinitionOutfields(kig.getPName(fkey), kig.getPVersion(fkey),
330                             kig.getLName(fkey), kig.getName(f.getSchema()), kig.getVersion(f.getSchema()));
331
332             ret.add(val);
333         }
334         return ret;
335     }
336
337     /**
338      * Gets the input fields for task.
339      *
340      * @param cg the code generator
341      * @param task the task
342      * @return the input fields for task
343      */
344     private List<ST> getInfieldsForTask(final CodeGeneratorCliEditor cg, final AxTask task) {
345         final Collection<? extends AxField> fields = task.getInputFields().values();
346         final List<ST> ret = new ArrayList<>(fields.size());
347         for (final AxField f : fields) {
348             final AxReferenceKey fkey = f.getKey();
349
350             final ST val = cg.createTaskDefinitionInfields(kig.getPName(fkey), kig.getPVersion(fkey),
351                             kig.getLName(fkey), kig.getName(f.getSchema()), kig.getVersion(f.getSchema()));
352
353             ret.add(val);
354         }
355         return ret;
356     }
357
358     /**
359      * Gets the states for policy.
360      *
361      * @param cg the code generator
362      * @param pol the policy
363      * @return the states for policy
364      */
365     private List<ST> getStatesForPolicy(final CodeGeneratorCliEditor cg, final AxPolicy pol) {
366         final Collection<AxState> states = pol.getStateMap().values();
367         final List<ST> ret = new ArrayList<>(states.size());
368         for (final AxState st : states) {
369             final AxReferenceKey skey = st.getKey();
370             final List<ST> outputs = getStateOutputsForState(cg, st);
371             final List<ST> finalizerLogics = getFinalizersForState(cg, st);
372             final List<ST> tasks = getTaskRefsForState(cg, st);
373             final List<ST> tsLogic = getTslForState(cg, st);
374             final List<ST> ctxRefs = getCtxtRefsForState(cg, st);
375
376             final ST val = cg.createPolicyStateDef(kig.getPName(skey), kig.getPVersion(skey), kig.getLName(skey),
377                             kig.getName(st.getTrigger()), kig.getVersion(st.getTrigger()),
378                             kig.getName(st.getDefaultTask()), kig.getVersion(st.getDefaultTask()), outputs, tasks,
379                             tsLogic, finalizerLogics, ctxRefs);
380
381             ret.add(val);
382         }
383         return ret;
384     }
385
386     /**
387      * Gets the finalizers for state.
388      *
389      * @param cg the code generator
390      * @param st the state
391      * @return the finalizers for state
392      */
393     private List<ST> getFinalizersForState(final CodeGeneratorCliEditor cg, final AxState st) {
394         final Collection<AxStateFinalizerLogic> fins = st.getStateFinalizerLogicMap().values();
395         final List<ST> ret = new ArrayList<>(fins.size());
396         final AxReferenceKey skey = st.getKey();
397         for (final AxStateFinalizerLogic fin : fins) {
398             final AxReferenceKey finkey = fin.getKey();
399
400             final ST val = cg.createPolicyStateDefFinalizerLogic(kig.getPName(skey), kig.getPVersion(skey),
401                             kig.getLName(skey), kig.getLName(finkey), fin.getLogicFlavour(), fin.getLogic());
402
403             ret.add(val);
404         }
405         return ret;
406     }
407
408     /**
409      * Gets the context references for state.
410      *
411      * @param cg the code generator
412      * @param st the state
413      * @return the context references for state
414      */
415     private List<ST> getCtxtRefsForState(final CodeGeneratorCliEditor cg, final AxState st) {
416         final Collection<AxArtifactKey> ctxs = st.getContextAlbumReferences();
417         final List<ST> ret = new ArrayList<>(ctxs.size());
418         final AxReferenceKey skey = st.getKey();
419         for (final AxArtifactKey ctx : ctxs) {
420
421             final ST val = cg.createPolicyStateDefContextRef(kig.getPName(skey), kig.getPVersion(skey),
422                             kig.getLName(skey), kig.getName(ctx), kig.getVersion(ctx));
423
424             ret.add(val);
425         }
426         return ret;
427     }
428
429     /**
430      * Gets the Task Selection Logic for state.
431      *
432      * @param cg the code generator
433      * @param st the state
434      * @return the TSL for state (if any) in a list
435      */
436     private List<ST> getTslForState(final CodeGeneratorCliEditor cg, final AxState st) {
437         final AxReferenceKey skey = st.getKey();
438         if (st.checkSetTaskSelectionLogic()) {
439             final AxTaskSelectionLogic tsl = st.getTaskSelectionLogic();
440             final ST val = cg.createPolicyStateDefTaskSelLogic(kig.getPName(skey), kig.getPVersion(skey),
441                             kig.getLName(skey), tsl.getLogicFlavour(), tsl.getLogic());
442             return Collections.singletonList(val);
443         } else {
444             return Collections.emptyList();
445         }
446     }
447
448     /**
449      * Gets the task references for state.
450      *
451      * @param cg the code generator
452      * @param st the state
453      * @return the task references for state
454      */
455     private List<ST> getTaskRefsForState(final CodeGeneratorCliEditor cg, final AxState st) {
456         final Map<AxArtifactKey, AxStateTaskReference> taskrefs = st.getTaskReferences();
457         final List<ST> ret = new ArrayList<>(taskrefs.size());
458         final AxReferenceKey skey = st.getKey();
459         for (final Entry<AxArtifactKey, AxStateTaskReference> e : taskrefs.entrySet()) {
460             final AxArtifactKey tkey = e.getKey();
461             final AxStateTaskReference tr = e.getValue();
462             final AxReferenceKey trkey = tr.getKey();
463
464             final ST val = cg.createPolicyStateTask(kig.getPName(skey), kig.getPVersion(skey), kig.getLName(skey),
465                             kig.getLName(trkey), kig.getName(tkey), kig.getVersion(tkey),
466                             tr.getStateTaskOutputType().name(), kig.getLName(tr.getOutput()));
467
468             ret.add(val);
469         }
470         return ret;
471     }
472
473     /**
474      * Gets the state outputs for state.
475      *
476      * @param cg the code generator
477      * @param st the state
478      * @return the state outputs for state
479      */
480     private List<ST> getStateOutputsForState(final CodeGeneratorCliEditor cg, final AxState st) {
481         final Collection<AxStateOutput> outs = st.getStateOutputs().values();
482         final List<ST> ret = new ArrayList<>(outs.size());
483         final AxReferenceKey skey = st.getKey();
484         for (final AxStateOutput out : outs) {
485             final AxReferenceKey outkey = out.getKey();
486
487             final ST val = cg.createPolicyStateOutput(kig.getPName(skey), kig.getPVersion(skey), kig.getLName(skey),
488                             kig.getLName(outkey), kig.getName(out.getOutgingEvent()),
489                             kig.getVersion(out.getOutgingEvent()), kig.getLName(out.getNextState()));
490
491             ret.add(val);
492         }
493         return ret;
494     }
495
496 }