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