fb9a8c60c2627c0fd950539146e6902bdfbaa96c
[policy/apex-pdp.git] / auth / cli-codegen / src / main / java / org / onap / policy / apex / auth / clicodegen / CodeGeneratorCliEditor.java
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) 2019 Nordix Foundation.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.apex.auth.clicodegen;
24
25 import static org.onap.policy.apex.auth.clicodegen.CliEditorContants.ALBUM_NAME;
26 import static org.onap.policy.apex.auth.clicodegen.CliEditorContants.ALBUM_VERSION;
27 import static org.onap.policy.apex.auth.clicodegen.CliEditorContants.CONTEXT_REFS;
28 import static org.onap.policy.apex.auth.clicodegen.CliEditorContants.CTX_REFS;
29 import static org.onap.policy.apex.auth.clicodegen.CliEditorContants.DECLARATION;
30 import static org.onap.policy.apex.auth.clicodegen.CliEditorContants.DEFAULT_TASK;
31 import static org.onap.policy.apex.auth.clicodegen.CliEditorContants.DEFAULT_TASK_VERSION;
32 import static org.onap.policy.apex.auth.clicodegen.CliEditorContants.DEFAULT_VALUE;
33 import static org.onap.policy.apex.auth.clicodegen.CliEditorContants.DEFINITIONS;
34 import static org.onap.policy.apex.auth.clicodegen.CliEditorContants.DESCRIPTION;
35 import static org.onap.policy.apex.auth.clicodegen.CliEditorContants.EVENT_NAME;
36 import static org.onap.policy.apex.auth.clicodegen.CliEditorContants.EVENT_VERSION;
37 import static org.onap.policy.apex.auth.clicodegen.CliEditorContants.FIELDS;
38 import static org.onap.policy.apex.auth.clicodegen.CliEditorContants.FIELD_NAME;
39 import static org.onap.policy.apex.auth.clicodegen.CliEditorContants.FIELD_SCHEMA;
40 import static org.onap.policy.apex.auth.clicodegen.CliEditorContants.FIELD_SCHEMA_VERSION;
41 import static org.onap.policy.apex.auth.clicodegen.CliEditorContants.FINALIZER_LOGICS;
42 import static org.onap.policy.apex.auth.clicodegen.CliEditorContants.FINALIZER_LOGIC_NAME;
43 import static org.onap.policy.apex.auth.clicodegen.CliEditorContants.FIRST_STATE;
44 import static org.onap.policy.apex.auth.clicodegen.CliEditorContants.FLAVOUR;
45 import static org.onap.policy.apex.auth.clicodegen.CliEditorContants.INFIELDS;
46 import static org.onap.policy.apex.auth.clicodegen.CliEditorContants.LOGIC;
47 import static org.onap.policy.apex.auth.clicodegen.CliEditorContants.LOGIC_FLAVOUR;
48 import static org.onap.policy.apex.auth.clicodegen.CliEditorContants.NAME;
49 import static org.onap.policy.apex.auth.clicodegen.CliEditorContants.NAME_SPACE;
50 import static org.onap.policy.apex.auth.clicodegen.CliEditorContants.NEXT_STATE;
51 import static org.onap.policy.apex.auth.clicodegen.CliEditorContants.OPTIONAL;
52 import static org.onap.policy.apex.auth.clicodegen.CliEditorContants.OUTFIELDS;
53 import static org.onap.policy.apex.auth.clicodegen.CliEditorContants.OUTPUTS;
54 import static org.onap.policy.apex.auth.clicodegen.CliEditorContants.OUTPUT_NAME;
55 import static org.onap.policy.apex.auth.clicodegen.CliEditorContants.OUTPUT_TYPE;
56 import static org.onap.policy.apex.auth.clicodegen.CliEditorContants.PARAMS;
57 import static org.onap.policy.apex.auth.clicodegen.CliEditorContants.PAR_NAME;
58 import static org.onap.policy.apex.auth.clicodegen.CliEditorContants.POLICY_NAME;
59 import static org.onap.policy.apex.auth.clicodegen.CliEditorContants.SCHEMA;
60 import static org.onap.policy.apex.auth.clicodegen.CliEditorContants.SCHEMA_NAME;
61 import static org.onap.policy.apex.auth.clicodegen.CliEditorContants.SCHEMA_VERSION;
62 import static org.onap.policy.apex.auth.clicodegen.CliEditorContants.SCOPE;
63 import static org.onap.policy.apex.auth.clicodegen.CliEditorContants.SOURCE;
64 import static org.onap.policy.apex.auth.clicodegen.CliEditorContants.STATES;
65 import static org.onap.policy.apex.auth.clicodegen.CliEditorContants.STATE_NAME;
66 import static org.onap.policy.apex.auth.clicodegen.CliEditorContants.TARGET;
67 import static org.onap.policy.apex.auth.clicodegen.CliEditorContants.TASKS;
68 import static org.onap.policy.apex.auth.clicodegen.CliEditorContants.TASK_LOCAL_NAME;
69 import static org.onap.policy.apex.auth.clicodegen.CliEditorContants.TASK_NAME;
70 import static org.onap.policy.apex.auth.clicodegen.CliEditorContants.TASK_VERSION;
71 import static org.onap.policy.apex.auth.clicodegen.CliEditorContants.TEMPLATE;
72 import static org.onap.policy.apex.auth.clicodegen.CliEditorContants.TRIGGER_NAME;
73 import static org.onap.policy.apex.auth.clicodegen.CliEditorContants.TRIGGER_VERSION;
74 import static org.onap.policy.apex.auth.clicodegen.CliEditorContants.TS_LOGIC;
75 import static org.onap.policy.apex.auth.clicodegen.CliEditorContants.UUID;
76 import static org.onap.policy.apex.auth.clicodegen.CliEditorContants.VERSION;
77 import static org.onap.policy.apex.auth.clicodegen.CliEditorContants.WRITABLE;
78
79 import java.util.List;
80
81 import org.stringtemplate.v4.ST;
82 import org.stringtemplate.v4.STGroupFile;
83
84 /**
85  * Code generator generating expressions for the APEX CLI Editor.
86  *
87  * @author Sven van der Meer (sven.van.der.meer@ericsson.com)
88  */
89 public class CodeGeneratorCliEditor {
90
91     // CHECKSTYLE:OFF: ParameterNumber
92
93     /**
94      * The name of the STG file for the code generator.
95      */
96     public static final String STG_FILE = "org/onap/policy/apex/auth/clicodegen/cli-editor.stg";
97
98     /**
99      * The String Template Group, taken from the context.
100      */
101     private final STGroupFile stg;
102
103     /**
104      * The ST for the model, loaded from the STG.
105      */
106     private final ST model;
107
108     /**
109      * A default name space, set from specification.
110      */
111     private String defaultNamespace;
112
113     /**
114      * Creates a new code generator.
115      */
116     public CodeGeneratorCliEditor() {
117         stg = new STGroupFile(STG_FILE);
118         stg.registerRenderer(String.class, new CgStringRenderer(), true);
119         model = stg.getInstanceOf("policyModel");
120     }
121
122     /**
123      * Adds model parameters to the template.
124      *
125      * @param name the name of the mode, must not be blank
126      * @param version a version, can be null
127      * @param uuid a UUID, can be null
128      * @param description a description, must not be blank
129      */
130     public void addModelParams(final String name, final String version, final String uuid, final String description) {
131         if (name == null || name.isEmpty()) {
132             throw new IllegalArgumentException("model name should not be blank");
133         }
134         if (description == null || description.isEmpty()) {
135             throw new IllegalArgumentException("model description should not be blank");
136         }
137
138         model.add(NAME, name);
139         model.add(VERSION, version);
140         model.add(UUID, uuid);
141         model.add(DESCRIPTION, description);
142     }
143
144     /**
145      * Returns the model.
146      *
147      * @return the model
148      */
149     public ST getModel() {
150         return model;
151     }
152
153     /**
154      * Sets the default name space.
155      *
156      * @param nameSpace new name space, ignored if blank
157      */
158     public void setDefaultNamespace(final String nameSpace) {
159         if (nameSpace != null && !nameSpace.isEmpty()) {
160             defaultNamespace = nameSpace;
161         }
162     }
163
164     /**
165      * Adds a new schema declaration to the model.
166      *
167      * @param name the name of the schema
168      * @param version the version of the declaration
169      * @param uuid the UUID for the declaration
170      * @param description a description of the schema
171      * @param flavour the flavour of the schema declaration, e.g. Java or Avro
172      * @param schema the actual schema declaration, either a string or as <code>LS schema LE</code>
173      */
174     public void addSchemaDeclaration(final String name, final String version, final String uuid,
175             final String description, final String flavour, final String schema) {
176         final ST st = stg.getInstanceOf("schemaDecl");
177         st.add(NAME, name);
178         st.add(VERSION, version);
179         st.add(UUID, uuid);
180         st.add(DESCRIPTION, description);
181         st.add(FLAVOUR, flavour);
182         st.add(SCHEMA, schema);
183         model.add(DECLARATION, st);
184     }
185
186     /**
187      * Adds a new context album declaration to the model.
188      *
189      * @param codeGenCliEditorBuilder The parameters for the context album
190      */
191     public void addContextAlbumDeclaration(CodeGenCliEditorBuilder codeGenCliEditorBuilder) {
192         final ST st = stg.getInstanceOf("ctxAlbumDecl");
193         st.add(NAME, codeGenCliEditorBuilder.getName());
194         st.add(VERSION, codeGenCliEditorBuilder.getVersion());
195         st.add(UUID, codeGenCliEditorBuilder.getUuid());
196         st.add(DESCRIPTION, codeGenCliEditorBuilder.getDescription());
197         st.add(SCOPE, codeGenCliEditorBuilder.getScope());
198         st.add(WRITABLE, codeGenCliEditorBuilder.isWritable());
199         st.add(SCHEMA_NAME, codeGenCliEditorBuilder.getSchemaName());
200         st.add(SCHEMA_VERSION, codeGenCliEditorBuilder.getSchemaVersion());
201         model.add(DECLARATION, st);
202     }
203
204     /**
205      * Creates a new event field definition which belongs to an event.
206      *
207      * @param eventName the event name
208      * @param version the event version
209      * @param fieldName the name for the field in the event
210      * @param fieldSchema the schema of the field
211      * @param fieldSchemaVersion the version of the schema
212      * @param optional a flag for optional fields
213      * @return a CLI command for event field definition
214      */
215     public ST createEventFieldDefinition(final String eventName, final String version, final String fieldName,
216             final String fieldSchema, final String fieldSchemaVersion, final boolean optional) {
217         final ST st = stg.getInstanceOf("eventDefField");
218         st.add(EVENT_NAME, eventName);
219         st.add(VERSION, version);
220         st.add(FIELD_NAME, fieldName);
221         st.add(FIELD_SCHEMA, fieldSchema);
222         st.add(FIELD_SCHEMA_VERSION, fieldSchemaVersion);
223         st.add(OPTIONAL, optional);
224         return st;
225     }
226
227     /**
228      * Creates a new task logic definition which belongs to a task.
229      *
230      * @param taskName the name of the task
231      * @param version the task version
232      * @param flavour the flavour, e.g. JAVA or JAVASCRIPT
233      * @param logic the actual logic (use either a string or a multi-line with <code>LS some code LE</code>
234      * @return a CLI command for task definition, logic
235      */
236     public ST createTaskDefLogic(final String taskName, final String version, final String flavour,
237             final String logic) {
238         final ST st = stg.getInstanceOf("taskDefLogic");
239         st.add(TASK_NAME, taskName);
240         st.add(VERSION, version);
241         st.add(FLAVOUR, flavour);
242         st.add(LOGIC, logic);
243         return st;
244     }
245
246     /**
247      * Adds a new event declaration to the model.
248      *
249      * @param eventDeclarationBuilder param object for event declaration
250      */
251     public void addEventDeclaration(EventDeclarationBuilder eventDeclarationBuilder) {
252         final ST st = stg.getInstanceOf("eventDecl");
253         st.add(NAME, eventDeclarationBuilder.getName());
254         st.add(VERSION, eventDeclarationBuilder.getVersion());
255         st.add(UUID, eventDeclarationBuilder.getUuid());
256         st.add(DESCRIPTION, eventDeclarationBuilder.getDescription());
257         st.add(SOURCE, eventDeclarationBuilder.getSource());
258         st.add(TARGET, eventDeclarationBuilder.getTarget());
259         st.add(FIELDS, eventDeclarationBuilder.getFields());
260
261         if (eventDeclarationBuilder.getNameSpace() != null) {
262             st.add(NAME_SPACE, eventDeclarationBuilder.getNameSpace());
263         } else if (defaultNamespace != null) {
264             st.add(NAME_SPACE, defaultNamespace);
265         }
266
267         model.add(DECLARATION, st);
268     }
269
270     /**
271      * Adds a new task declaration to the model.
272      *
273      * @param taskDeclarationBuilder builder for the task declaration parameters
274      */
275     public void addTaskDeclaration(TaskDeclarationBuilder taskDeclarationBuilder) {
276         final ST st = stg.getInstanceOf("taskDecl");
277         st.add(NAME, taskDeclarationBuilder.getName());
278         st.add(VERSION, taskDeclarationBuilder.getVersion());
279         st.add(UUID, taskDeclarationBuilder.getUuid());
280         st.add(DESCRIPTION, taskDeclarationBuilder.getDescription());
281         st.add(INFIELDS, taskDeclarationBuilder.getInfields());
282         st.add(OUTFIELDS, taskDeclarationBuilder.getOutfields());
283         st.add(LOGIC, taskDeclarationBuilder.getLogic());
284         st.add(PARAMS, taskDeclarationBuilder.getParameters());
285         st.add(CONTEXT_REFS, taskDeclarationBuilder.getContextRefs());
286         model.add(DECLARATION, st);
287     }
288
289     /**
290      * Adds a new policy definition to the model.
291      *
292      * @param name the name of the policy
293      * @param version the version of the policy
294      * @param uuid a UUID for the definition
295      * @param description a description of the policy
296      * @param template the template type for this policy
297      * @param firstState the first state of the policy
298      * @param states all policy states
299      */
300     public void addPolicyDefinition(final String name, final String version, final String uuid,
301             final String description, final String template, final String firstState, final List<ST> states) {
302         final ST st = stg.getInstanceOf("policyDef");
303         st.add(NAME, name);
304         st.add(VERSION, version);
305         st.add(UUID, uuid);
306         st.add(DESCRIPTION, description);
307         st.add(TEMPLATE, template);
308         st.add(FIRST_STATE, firstState);
309         st.add(STATES, states);
310         model.add(DEFINITIONS, st);
311     }
312
313     /**
314      * Creates a new task infield definition.
315      *
316      * @param taskName the name of the task
317      * @param version the version of the task
318      * @param fieldName the name of the infield
319      * @param fieldSchema the schema for the infield
320      * @param fieldSchemaVersion the version of the schema
321      * @return a CLI command for task infield definition
322      */
323     public ST createTaskDefinitionInfields(final String taskName, final String version, final String fieldName,
324             final String fieldSchema, final String fieldSchemaVersion) {
325         final ST st = stg.getInstanceOf("taskDefInputFields");
326         st.add(TASK_NAME, taskName);
327         st.add(VERSION, version);
328         st.add(FIELD_NAME, fieldName);
329         st.add(FIELD_SCHEMA, fieldSchema);
330         st.add(FIELD_SCHEMA_VERSION, fieldSchemaVersion);
331         return st;
332     }
333
334     /**
335      * Creates a new task outfield definition.
336      *
337      * @param taskName the name of the task
338      * @param version the version of the task
339      * @param fieldName the name of the outfield
340      * @param fieldSchema the schema for the outfield
341      * @param fieldSchemaVersion the version of the schema
342      * @return a CLI command for task outfield definition
343      */
344     public ST createTaskDefinitionOutfields(final String taskName, final String version, final String fieldName,
345             final String fieldSchema, final String fieldSchemaVersion) {
346         final ST st = stg.getInstanceOf("taskDefOutputFields");
347         st.add(TASK_NAME, taskName);
348         st.add(VERSION, version);
349         st.add(FIELD_NAME, fieldName);
350         st.add(FIELD_SCHEMA, fieldSchema);
351         st.add(FIELD_SCHEMA_VERSION, fieldSchemaVersion);
352         return st;
353     }
354
355     /**
356      * Creates a new task parameter definition belonging to a task.
357      *
358      * @param name the name of the task
359      * @param version the version of the task
360      * @param parName the parameter name
361      * @param defaultValue a default value for the parameter
362      * @return a CLI command for a task parameter definition
363      */
364     public ST createTaskDefinitionParameters(final String name, final String version, final String parName,
365             final String defaultValue) {
366         final ST st = stg.getInstanceOf("taskDefParameter");
367         st.add(NAME, name);
368         st.add(VERSION, version);
369         st.add(PAR_NAME, parName);
370         st.add(DEFAULT_VALUE, defaultValue);
371         return st;
372     }
373
374     /**
375      * Creates a new task definition context reference which belongs to a task.
376      *
377      * @param name the name of the task
378      * @param version the version of the task
379      * @param albumName the name of the context album
380      * @param albumVersion the version of the context album
381      * @return a CLI command for a task context reference definition
382      */
383     public ST createTaskDefinitionContextRef(final String name, final String version, final String albumName,
384             final String albumVersion) {
385         final ST st = stg.getInstanceOf("taskDefCtxRef");
386         st.add(NAME, name);
387         st.add(VERSION, version);
388         st.add(ALBUM_NAME, albumName);
389         st.add(ALBUM_VERSION, albumVersion);
390         return st;
391     }
392
393     /**
394      * Creates a new policy state task definition for a task which belongs to a state which belongs to a policy.
395      *
396      * @param policyStateTaskBuilder builder for the state task parameters
397      * @return a CLI command for a policy state task definition
398      */
399     public ST createPolicyStateTask(PolicyStateTaskBuilder policyStateTaskBuilder) {
400         final ST st = stg.getInstanceOf("policyStateTask");
401         st.add(POLICY_NAME, policyStateTaskBuilder.getPolicyName());
402         st.add(VERSION, policyStateTaskBuilder.getVersion());
403         st.add(STATE_NAME, policyStateTaskBuilder.getStateName());
404         st.add(TASK_LOCAL_NAME, policyStateTaskBuilder.getTaskLocalName());
405         st.add(TASK_NAME, policyStateTaskBuilder.getTaskName());
406         st.add(TASK_VERSION, policyStateTaskBuilder.getTaskVersion());
407         st.add(OUTPUT_TYPE, policyStateTaskBuilder.getOutputType());
408         st.add(OUTPUT_NAME, policyStateTaskBuilder.getOutputName());
409         return st;
410     }
411
412     /**
413      * Creates a new policy state output definition for a state which belongs to a policy.
414      *
415      * @param policyName the name of the policy
416      * @param version the version of the policy
417      * @param stateName the name of the new state
418      * @param outputName the name of the new output
419      * @param eventName the event name for the output
420      * @param eventVersion the version of the event for the output
421      * @param nextState the next state if any
422      * @return a CLI command for a state output definition
423      */
424     public ST createPolicyStateOutput(final String policyName, final String version, final String stateName,
425             final String outputName, final String eventName, final String eventVersion,
426             final String nextState) {
427         final ST st = stg.getInstanceOf("policyStateOutput");
428         st.add(POLICY_NAME, policyName);
429         st.add(VERSION, version);
430         st.add(STATE_NAME, stateName);
431         st.add(OUTPUT_NAME, outputName);
432         st.add(EVENT_NAME, eventName);
433         st.add(EVENT_VERSION, eventVersion);
434         st.add(NEXT_STATE, nextState);
435         return st;
436     }
437
438     /**
439      * Creates a new policy state definition for a state which belongs to a policy.
440      *
441      * @param policyStateDefBuilder builder for the state definition parameters
442      * @return a CLI command for a policy state definition
443      */
444     public ST createPolicyStateDef(PolicyStateDefBuilder policyStateDefBuilder) {
445         final ST st = stg.getInstanceOf("policyStateDef");
446         st.add(POLICY_NAME, policyStateDefBuilder.getPolicyName());
447         st.add(VERSION, policyStateDefBuilder.getVersion());
448         st.add(STATE_NAME, policyStateDefBuilder.getStateName());
449         st.add(TRIGGER_NAME, policyStateDefBuilder.getTriggerName());
450         st.add(TRIGGER_VERSION, policyStateDefBuilder.getTriggerVersion());
451         st.add(DEFAULT_TASK, policyStateDefBuilder.getDefaultTask());
452         st.add(DEFAULT_TASK_VERSION, policyStateDefBuilder.getDefaultTaskVersion());
453         st.add(OUTPUTS, policyStateDefBuilder.getOutputs());
454         st.add(TASKS, policyStateDefBuilder.getTasks());
455         st.add(TS_LOGIC, policyStateDefBuilder.getTsLogic());
456         st.add(FINALIZER_LOGICS, policyStateDefBuilder.getFinalizerLogics());
457         st.add(CTX_REFS, policyStateDefBuilder.getCtxRefs());
458         return st;
459     }
460
461     /**
462      * Creates a new task selection logic definition for a state which belongs to a policy.
463      *
464      * @param name the name of the policy
465      * @param version the version of the policy
466      * @param stateName the name of the state
467      * @param logicFlavour the flavour, e.g. JAVA or JAVASCRIPT
468      * @param logic the actual logic (use either a string or a multi-line with <code>LS some code LE</code>
469      * @return a CLI command for task selection logic definition
470      */
471     public ST createPolicyStateDefTaskSelLogic(final String name, final String version, final String stateName,
472             final String logicFlavour, final String logic) {
473         final ST st = stg.getInstanceOf("policyStateTaskSelectionLogic");
474         st.add(NAME, name);
475         st.add(VERSION, version);
476         st.add(STATE_NAME, stateName);
477         st.add(LOGIC_FLAVOUR, logicFlavour);
478         st.add(LOGIC, logic);
479         return st;
480     }
481
482     /**
483      * Creates a new state finalizer definition for a state which belongs to a policy.
484      *
485      * @param name the name of the policy
486      * @param version the version of the policy
487      * @param stateName the name of the state
488      * @param finalizerLogicName name of the finalizer logic
489      * @param logicFlavour the flavour, e.g. JAVA or JAVASCRIPT
490      * @param logic the actual logic (use either a string or a multi-line with <code>LS some code LE</code>
491      * @return a CLI command for finalizer definition
492      */
493     public ST createPolicyStateDefFinalizerLogic(final String name, final String version, final String stateName,
494             final String finalizerLogicName, final String logicFlavour, final String logic) {
495         final ST st = stg.getInstanceOf("policyStateFinalizerLogic");
496         st.add(NAME, name);
497         st.add(VERSION, version);
498         st.add(STATE_NAME, stateName);
499         st.add(FINALIZER_LOGIC_NAME, finalizerLogicName);
500         st.add(LOGIC_FLAVOUR, logicFlavour);
501         st.add(LOGIC, logic);
502         return st;
503     }
504
505     /**
506      * Creates a new policy state context reference for a state which belongs to a policy.
507      *
508      * @param name the name of the policy
509      * @param version the version of the policy
510      * @param stateName the name of the state
511      * @param albumName the name of the album
512      * @param albumVersion the version of the album
513      * @return a CLI command for state context reference
514      */
515     public ST createPolicyStateDefContextRef(final String name, final String version, final String stateName,
516             final String albumName, final String albumVersion) {
517         final ST st = stg.getInstanceOf("policyStateContextRef");
518         st.add(NAME, name);
519         st.add(VERSION, version);
520         st.add(STATE_NAME, stateName);
521         st.add(ALBUM_NAME, albumName);
522         st.add(ALBUM_VERSION, albumVersion);
523         return st;
524     }
525
526 }